Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ uuid = [] # Deprecated; this feature was provided by the now unneeded uuid depen

[dependencies]
keyboard-types = { version = "0.6.1", default-features = false }
raw-window-handle = "0.5"
raw-window-handle = "0.6.2"

[target.'cfg(target_os="linux")'.dependencies]
x11rb = { version = "0.13.2", features = ["cursor", "resource_manager", "allow-unsafe-code", "dl-libxcb"], default-features = false }
Expand Down
3 changes: 1 addition & 2 deletions examples/open_parented/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ publish = false

[dependencies]
baseview = { path = "../.." }
softbuffer = "0.3.4"
rtrb = "0.3.4"
softbuffer = "0.4.8"
32 changes: 14 additions & 18 deletions examples/open_parented/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
use baseview::{
Event, EventStatus, PhySize, Window, WindowEvent, WindowHandle, WindowHandler,
Event, EventStatus, PhySize, Window, WindowContext, WindowEvent, WindowHandle, WindowHandler,
WindowOpenOptions,
};
use std::cell::{Cell, RefCell};
use std::num::NonZeroU32;

struct ParentWindowHandler {
_ctx: softbuffer::Context,
surface: RefCell<softbuffer::Surface>,
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
current_size: Cell<PhySize>,
damaged: Cell<bool>,

_child_window: Option<WindowHandle>,
}

impl ParentWindowHandler {
pub fn new(window: &mut Window) -> Self {
let ctx = unsafe { softbuffer::Context::new(window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap();
pub fn new(window: WindowContext) -> Self {
let ctx = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();

let window_open_options =
WindowOpenOptions::new().with_size(256.0, 256.0).with_title("baseview child");

let child_window =
Window::open_parented(window, window_open_options, ChildWindowHandler::new);
Window::open_parented(&window, window_open_options, ChildWindowHandler::new);

// TODO: no way to query physical size initially?
Self {
_ctx: ctx,
surface: surface.into(),
current_size: PhySize::new(512, 512).into(),
damaged: true.into(),
Expand All @@ -38,7 +36,7 @@ impl ParentWindowHandler {
}

impl WindowHandler for ParentWindowHandler {
fn on_frame(&self, _window: &mut Window) {
fn on_frame(&self) {
let mut surface = self.surface.borrow_mut();
let mut buf = surface.buffer_mut().unwrap();
if self.damaged.get() {
Expand All @@ -48,7 +46,7 @@ impl WindowHandler for ParentWindowHandler {
buf.present().unwrap();
}

fn on_event(&self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&self, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(info)) => {
println!("Parent Resized: {:?}", info);
Expand All @@ -72,21 +70,19 @@ impl WindowHandler for ParentWindowHandler {
}

struct ChildWindowHandler {
_ctx: softbuffer::Context,
surface: RefCell<softbuffer::Surface>,
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
current_size: Cell<PhySize>,
damaged: Cell<bool>,
}

impl ChildWindowHandler {
pub fn new(window: &mut Window) -> Self {
let ctx = unsafe { softbuffer::Context::new(window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap();
pub fn new(window: WindowContext) -> Self {
let ctx = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window).unwrap();
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();

// TODO: no way to query physical size initially?
Self {
_ctx: ctx,
surface: surface.into(),
current_size: PhySize::new(256, 256).into(),
damaged: true.into(),
Expand All @@ -95,7 +91,7 @@ impl ChildWindowHandler {
}

impl WindowHandler for ChildWindowHandler {
fn on_frame(&self, _window: &mut Window) {
fn on_frame(&self) {
let mut surface = self.surface.borrow_mut();
let mut buf = surface.buffer_mut().unwrap();
if self.damaged.get() {
Expand All @@ -105,7 +101,7 @@ impl WindowHandler for ChildWindowHandler {
buf.present().unwrap();
}

fn on_event(&self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&self, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(info)) => {
println!("Child Resized: {:?}", info);
Expand Down
2 changes: 1 addition & 1 deletion examples/open_window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ publish = false

[dependencies]
baseview = { path = "../.." }
softbuffer = "0.3.4"
softbuffer = "0.4.8"
rtrb = "0.3.4"
16 changes: 7 additions & 9 deletions examples/open_window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use rtrb::{Consumer, RingBuffer};
#[cfg(target_os = "macos")]
use baseview::copy_to_clipboard;
use baseview::{
Event, EventStatus, MouseEvent, PhyPoint, PhySize, Window, WindowEvent, WindowHandler,
WindowInfo, WindowOpenOptions,
Event, EventStatus, MouseEvent, PhyPoint, PhySize, Window, WindowContext, WindowEvent,
WindowHandler, WindowInfo, WindowOpenOptions,
};

#[derive(Debug, Clone)]
Expand All @@ -19,16 +19,15 @@ enum Message {
struct OpenWindowExample {
rx: RefCell<Consumer<Message>>,

_ctx: softbuffer::Context,
surface: RefCell<softbuffer::Surface>,
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
current_size: Cell<WindowInfo>,
mouse_pos: Cell<PhyPoint>,
is_cursor_inside: Cell<bool>,
damaged: Cell<bool>,
}

impl WindowHandler for OpenWindowExample {
fn on_frame(&self, _window: &mut Window) {
fn on_frame(&self) {
if !self.damaged.get() {
return;
}
Expand Down Expand Up @@ -98,7 +97,7 @@ impl WindowHandler for OpenWindowExample {
}
}

fn on_event(&self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&self, event: Event) -> EventStatus {
match &event {
#[cfg(target_os = "macos")]
Event::Mouse(MouseEvent::ButtonPressed { .. }) => copy_to_clipboard("This is a test!"),
Expand Down Expand Up @@ -151,12 +150,11 @@ fn main() {
});

Window::open_blocking(window_open_options, |window| {
let ctx = unsafe { softbuffer::Context::new(window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap();
let ctx = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window).unwrap();
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();

OpenWindowExample {
_ctx: ctx,
surface: surface.into(),
rx: rx.into(),
current_size: WindowInfo::from_physical_size(PhySize::new(512, 512), 1.0).into(),
Expand Down
2 changes: 1 addition & 1 deletion examples/render_femtovg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ publish = false

[dependencies]
baseview = { path = "../..", features = ["opengl"] }
femtovg = "0.9.0"
femtovg = "0.25.1"
29 changes: 18 additions & 11 deletions examples/render_femtovg/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
use baseview::gl::GlConfig;
use baseview::gl::{GlConfig, GlContext};
use baseview::{
Event, EventStatus, MouseEvent, PhyPoint, Size, Window, WindowEvent, WindowHandler, WindowInfo,
WindowOpenOptions,
Event, EventStatus, MouseEvent, PhyPoint, Size, Window, WindowContext, WindowEvent,
WindowHandler, WindowInfo, WindowOpenOptions,
};
use femtovg::renderer::OpenGl;
use femtovg::{Canvas, Color};
use std::cell::{Cell, RefCell};

struct FemtovgExample {
window_context: WindowContext,
gl_context: GlContext,
canvas: RefCell<Canvas<OpenGl>>,
current_size: Cell<WindowInfo>,
current_mouse_position: Cell<PhyPoint>,
damaged: Cell<bool>,
}

impl FemtovgExample {
fn new(window: &mut Window) -> Self {
let context = window.gl_context().unwrap();
unsafe { context.make_current() };
fn new(window_context: WindowContext) -> Self {
let gl_context = window_context.gl_context().unwrap();
unsafe { gl_context.make_current() };

let renderer =
unsafe { OpenGl::new_from_function(|s| context.get_proc_address(s)) }.unwrap();
unsafe { OpenGl::new_from_function(|s| gl_context.get_proc_address(s)) }.unwrap();

let mut canvas = Canvas::new(renderer).unwrap();
// TODO: get actual window width
canvas.set_size(512, 512, 1.0);

unsafe { context.make_not_current() };
unsafe { gl_context.make_not_current() };
Self {
gl_context,
window_context,
canvas: canvas.into(),
current_size: WindowInfo::from_logical_size(Size { width: 512.0, height: 512.0 }, 1.0)
.into(),
Expand All @@ -38,12 +42,12 @@ impl FemtovgExample {
}

impl WindowHandler for FemtovgExample {
fn on_frame(&self, window: &mut Window) {
fn on_frame(&self) {
if !self.damaged.get() {
return;
}

let context = window.gl_context().unwrap();
let context = &self.gl_context;
unsafe { context.make_current() };

let mut canvas = self.canvas.borrow_mut();
Expand Down Expand Up @@ -79,7 +83,7 @@ impl WindowHandler for FemtovgExample {
self.damaged.set(false);
}

fn on_event(&self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&self, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(size)) => {
let phy_size = size.physical_size();
Expand All @@ -98,6 +102,9 @@ impl WindowHandler for FemtovgExample {
| MouseEvent::DragDropped { position, .. },
) => {
self.current_mouse_position.set(position.to_physical(&self.current_size.get()));
if self.current_mouse_position.get().y > 400 && !self.window_context.has_focus() {
self.window_context.focus()
}
self.damaged.set(true);
}
_ => {}
Expand Down
52 changes: 52 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::{platform, MouseCursor, Size};
use raw_window_handle::{
DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, WindowHandle,
};

#[derive(Clone)]
pub struct WindowContext {
inner: platform::WindowContext,
}

impl WindowContext {
pub(crate) fn new(inner: platform::WindowContext) -> Self {
Self { inner }
}

pub fn set_mouse_cursor(&self, mouse_cursor: MouseCursor) {
self.inner.set_mouse_cursor(mouse_cursor);
}

pub fn close(&self) {
self.inner.close();
}

pub fn has_focus(&self) -> bool {
self.inner.has_focus()
}

pub fn focus(&self) {
self.inner.focus();
}

pub fn resize(&self, size: Size) {
self.inner.resize(size);
}

#[cfg(feature = "opengl")]
pub fn gl_context(&self) -> Option<crate::gl::GlContext> {
self.inner.gl_context()
}
}

impl HasWindowHandle for WindowContext {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
self.inner.window_handle().ok_or(HandleError::Unavailable)
}
}

impl HasDisplayHandle for WindowContext {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
Ok(self.inner.display_handle())
}
}
13 changes: 6 additions & 7 deletions src/gl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::platform::gl::*;
use std::ffi::c_void;
use std::marker::PhantomData;
use std::panic::AssertUnwindSafe;

#[derive(Clone, Debug, PartialEq)]
pub struct GlConfig {
Expand Down Expand Up @@ -45,22 +44,22 @@ pub enum Profile {
}

#[derive(Debug)]
#[non_exhaustive]
pub enum GlError {
InvalidWindowHandle,
VersionNotSupported,
CreationFailed(CreationFailedError),
}

#[derive(Clone)]
pub struct GlContext {
// AssertUnwindSafe should *not* be here, but this is needed for now to keep semver compatibility
// Remove this in 0.2
pub(crate) inner: AssertUnwindSafe<crate::platform::gl::GlContext>,
phantom: PhantomData<*mut ()>,
inner: crate::platform::gl::GlContext,
// To make sure this is !Send, !Sync, and !UnwindSafe on all platforms
phantom: PhantomData<(*mut (), &'static mut ())>,
}

impl GlContext {
pub(crate) fn new(context: crate::platform::gl::GlContext) -> GlContext {
GlContext { inner: AssertUnwindSafe(context), phantom: PhantomData }
GlContext { inner: context, phantom: PhantomData }
}

pub unsafe fn make_current(&self) {
Expand Down
6 changes: 6 additions & 0 deletions src/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::{Event, EventStatus};

pub trait WindowHandler: 'static {
fn on_frame(&self);
fn on_event(&self, event: Event) -> EventStatus;
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod clipboard;
mod context;
mod event;
mod handler;
mod keyboard;
mod mouse_cursor;
mod window;
Expand All @@ -12,7 +14,9 @@ pub(crate) mod platform;
pub mod gl;

pub use clipboard::*;
pub use context::WindowContext;
pub use event::*;
pub use handler::WindowHandler;
pub use mouse_cursor::MouseCursor;
pub use window::*;
pub use window_info::*;
Expand Down
Loading