Skip to content
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ opengl = [
keyboard-types = { version = "0.8.3" }
raw-window-handle = "0.6.2"
dpi = "0.1.2"
tracing = { version = "0.1", optional = true }

[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
67 changes: 39 additions & 28 deletions examples/open_parented/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use baseview::dpi::LogicalSize;
use baseview::{
Event, EventStatus, WindowContext, WindowHandle, WindowHandler, WindowOpenOptions, WindowSize,
Event, EventStatus, HandlerError, WindowContext, WindowHandle, WindowHandler,
WindowOpenOptions, WindowSize,
};
use std::cell::{Cell, RefCell};
use std::num::NonZeroU32;
Expand All @@ -13,45 +14,51 @@ struct ParentWindowHandler {
}

impl ParentWindowHandler {
pub fn new(window: WindowContext) -> Self {
let ctx = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
pub fn new(window: WindowContext) -> Result<Self, HandlerError> {
let ctx = softbuffer::Context::new(window.clone())?;
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
let size = window.size().physical;
surface
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())
.unwrap();
surface.resize(size.width.try_into()?, size.height.try_into()?)?;

let window_open_options = WindowOpenOptions::new()
.with_size(LogicalSize::new(256, 256))
.with_parent(&window)
.with_title("baseview child");

let child_window = baseview::create_window(window_open_options, ChildWindowHandler::new);
let child_window = baseview::create_window(window_open_options, ChildWindowHandler::new)?;

Self { surface: surface.into(), damaged: true.into(), _child_window: Some(child_window) }
Ok(Self {
surface: surface.into(),
damaged: true.into(),
_child_window: Some(child_window),
})
}
}

impl WindowHandler for ParentWindowHandler {
fn on_frame(&self) {
fn on_frame(&self) -> Result<(), HandlerError> {
let mut surface = self.surface.borrow_mut();
let mut buf = surface.buffer_mut().unwrap();
let mut buf = surface.buffer_mut()?;
if self.damaged.get() {
buf.fill(0xFFAAAAAA);
self.damaged.set(false);
}
buf.present().unwrap();
buf.present()?;

Ok(())
}

fn resized(&self, new_size: WindowSize) {
fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
println!("Parent Resized: {new_size:?}");

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height).unwrap();
self.surface.borrow_mut().resize(width, height)?;
self.damaged.set(true);
}

Ok(())
}

fn on_event(&self, event: Event) -> EventStatus {
Expand All @@ -72,38 +79,40 @@ struct ChildWindowHandler {
}

impl ChildWindowHandler {
pub fn new(window: WindowContext) -> Self {
let ctx = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
pub fn new(window: WindowContext) -> Result<Self, HandlerError> {
let ctx = softbuffer::Context::new(window.clone())?;
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
let size = window.size().physical;
surface
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())
.unwrap();
surface.resize(size.width.try_into()?, size.height.try_into()?)?;

Self { surface: surface.into(), damaged: true.into() }
Ok(Self { surface: surface.into(), damaged: true.into() })
}
}

impl WindowHandler for ChildWindowHandler {
fn on_frame(&self) {
fn on_frame(&self) -> Result<(), HandlerError> {
let mut surface = self.surface.borrow_mut();
let mut buf = surface.buffer_mut().unwrap();
let mut buf = surface.buffer_mut()?;
if self.damaged.get() {
buf.fill(0xFFAA0000);
self.damaged.set(false);
}
buf.present().unwrap();
buf.present()?;

Ok(())
}

fn resized(&self, new_size: WindowSize) {
fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
println!("Child Resized: {new_size:?}");

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height).unwrap();
self.surface.borrow_mut().resize(width, height)?;
self.damaged.set(true);
}

Ok(())
}

fn on_event(&self, event: Event) -> EventStatus {
Expand All @@ -118,8 +127,10 @@ impl WindowHandler for ChildWindowHandler {
}
}

fn main() {
fn main() -> Result<(), baseview::Error> {
let window_open_options = WindowOpenOptions::new().with_size(LogicalSize::new(512.0, 512.0));

baseview::create_window(window_open_options, ParentWindowHandler::new).run_until_closed();
baseview::create_window(window_open_options, ParentWindowHandler::new)?.run_until_closed();

Ok(())
}
37 changes: 21 additions & 16 deletions examples/open_window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use rtrb::{Consumer, RingBuffer};
use baseview::copy_to_clipboard;
use baseview::dpi::{LogicalSize, PhysicalPosition};
use baseview::{
Event, EventStatus, MouseEvent, WindowContext, WindowHandler, WindowOpenOptions, WindowSize,
Event, EventStatus, HandlerError, MouseEvent, WindowContext, WindowHandler, WindowOpenOptions,
WindowSize,
};

#[derive(Debug, Clone)]
Expand All @@ -27,24 +28,26 @@ struct OpenWindowExample {
}

impl WindowHandler for OpenWindowExample {
fn resized(&self, new_size: WindowSize) {
fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
println!("Resized: {new_size:?}");

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height).unwrap();
self.surface.borrow_mut().resize(width, height)?;
self.damaged.set(true);
}

Ok(())
}

fn on_frame(&self) {
fn on_frame(&self) -> Result<(), HandlerError> {
if !self.damaged.get() {
return;
return Ok(());
}

let mut surface = self.surface.borrow_mut();
let mut pixels = surface.buffer_mut().unwrap();
let mut pixels = surface.buffer_mut()?;
let size = self.window_context.size();
let scale_factor = self.window_context.scale_factor();
let (width, height) = (size.physical.width, size.physical.height);
Expand Down Expand Up @@ -101,12 +104,14 @@ impl WindowHandler for OpenWindowExample {
}
}

pixels.present().unwrap();
pixels.present()?;
self.damaged.set(false);

while let Ok(message) = self.rx.borrow_mut().pop() {
println!("Message: {:?}", message);
}

Ok(())
}

fn on_event(&self, event: Event) -> EventStatus {
Expand Down Expand Up @@ -134,7 +139,7 @@ impl WindowHandler for OpenWindowExample {
}
}

fn main() {
fn main() -> Result<(), baseview::Error> {
let window_open_options = WindowOpenOptions::new().with_size(LogicalSize::new(512.0, 512.0));

let (mut tx, rx) = RingBuffer::new(128);
Expand All @@ -148,23 +153,23 @@ fn main() {
});

baseview::create_window(window_open_options, |window| {
let ctx = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
let ctx = softbuffer::Context::new(window.clone())?;
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
let size = window.size().physical;
surface
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())
.unwrap();
surface.resize(size.width.try_into()?, size.height.try_into()?)?;

OpenWindowExample {
Ok(OpenWindowExample {
window_context: window,
surface: surface.into(),
rx: rx.into(),
mouse_pos: PhysicalPosition::new(0., 0.).into(),
is_cursor_inside: false.into(),
damaged: true.into(),
}
})
})
})?
.run_until_closed();

Ok(())
}

fn log_event(event: &Event) {
Expand Down
7 changes: 4 additions & 3 deletions examples/plugin_clack/src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::window_handler::OpenWindowExample;
use crate::ExamplePluginMainThread;
use baseview::dpi::PhysicalSize;
use baseview::gl::GlConfig;
use baseview::{Window, WindowHandle, WindowOpenOptions};
use baseview::{WindowHandle, WindowOpenOptions};
use clack_extensions::gui::{
GuiApiType, GuiConfiguration, GuiResizeHints, GuiSize, PluginGuiImpl, Window as ClapWindow,
};
Expand Down Expand Up @@ -72,9 +72,10 @@ impl PluginGuiImpl for ExamplePluginMainThread {

let options = WindowOpenOptions::new()
.with_size(PhysicalSize::new(400, 200))
.with_gl_config(GlConfig::default());
.with_gl_config(GlConfig::default())
.with_parent(&parent);

let window = Window::open_parented(&parent, options, OpenWindowExample::new);
let window = baseview::create_window(options, OpenWindowExample::new)?;

self.gui = Some(ExamplePluginGui { handle: window });

Expand Down
34 changes: 19 additions & 15 deletions examples/plugin_clack/src/window_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use baseview::dpi::PhysicalPosition;
use baseview::{Event, EventStatus, MouseEvent, WindowContext, WindowHandler, WindowSize};
use baseview::{
Event, EventStatus, HandlerError, MouseEvent, WindowContext, WindowHandler, WindowSize,
};
use std::cell::{Cell, RefCell};
use std::num::NonZeroU32;

Expand All @@ -13,24 +15,26 @@ pub struct OpenWindowExample {
}

impl WindowHandler for OpenWindowExample {
fn resized(&self, new_size: WindowSize) {
fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
println!("Resized: {new_size:?}");

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height).unwrap();
self.surface.borrow_mut().resize(width, height)?;
self.damaged.set(true);
}

Ok(())
}

fn on_frame(&self) {
fn on_frame(&self) -> Result<(), HandlerError> {
if !self.damaged.get() {
return;
return Ok(());
}

let mut surface = self.surface.borrow_mut();
let mut pixels = surface.buffer_mut().unwrap();
let mut pixels = surface.buffer_mut()?;
let size = self.window_context.size();
let scale_factor = self.window_context.scale_factor();
let (width, height) = (size.physical.width, size.physical.height);
Expand Down Expand Up @@ -87,8 +91,10 @@ impl WindowHandler for OpenWindowExample {
}
}

pixels.present().unwrap();
pixels.present()?;
self.damaged.set(false);

Ok(())
}

fn on_event(&self, event: Event) -> EventStatus {
Expand All @@ -115,21 +121,19 @@ impl WindowHandler for OpenWindowExample {
}

impl OpenWindowExample {
pub fn new(window: WindowContext) -> Self {
let ctx = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap();
pub fn new(window: WindowContext) -> Result<Self, HandlerError> {
let ctx = softbuffer::Context::new(window.clone())?;
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
let size = window.size().physical;
surface
.resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap())
.unwrap();
surface.resize(size.width.try_into()?, size.height.try_into()?)?;

OpenWindowExample {
Ok(OpenWindowExample {
window_context: window,
surface: surface.into(),
mouse_pos: PhysicalPosition::new(0., 0.).into(),
is_cursor_inside: false.into(),
damaged: true.into(),
}
})
}
}

Expand Down
Loading