Skip to content
Draft
13 changes: 3 additions & 10 deletions examples/cursors/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use baseview::{
};
use femtovg::renderer::OpenGl;
use femtovg::{Canvas, Color};
use std::cell::{Cell, RefCell};
use std::cell::RefCell;

struct CursorsExample {
window_context: WindowContext,
gl_context: GlContext,
canvas: RefCell<Canvas<OpenGl>>,
damaged: Cell<bool>,
}

impl CursorsExample {
Expand All @@ -29,7 +28,7 @@ impl CursorsExample {
canvas.set_size(size.physical.width, size.physical.height, size.scale_factor as f32);

unsafe { gl_context.make_not_current()? };
Ok(Self { gl_context, window_context, canvas: canvas.into(), damaged: true.into() })
Ok(Self { gl_context, window_context, canvas: canvas.into() })
}

fn in_blue_area(&self, position: PhysicalPosition<f64>) -> bool {
Expand All @@ -43,11 +42,7 @@ impl CursorsExample {
}

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

fn draw(&self) -> Result<(), HandlerError> {
let context = &self.gl_context;
unsafe { context.make_current()? };

Expand All @@ -72,15 +67,13 @@ impl WindowHandler for CursorsExample {
canvas.flush();
context.swap_buffers()?;
unsafe { context.make_not_current()? };
self.damaged.set(false);

Ok(())
}

fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
let size = new_size.physical;
self.canvas.borrow_mut().set_size(size.width, size.height, new_size.scale_factor as f32);
self.damaged.set(true);

Ok(())
}
Expand Down
25 changes: 7 additions & 18 deletions examples/open_parented/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ use baseview::{
Event, EventStatus, HandlerError, Window, WindowContext, WindowHandler, WindowSettings,
WindowSize,
};
use std::cell::{Cell, RefCell};
use std::cell::RefCell;
use std::num::NonZeroU32;

struct ParentWindowHandler {
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
damaged: Cell<bool>,

child_window: Window,
}

Expand All @@ -26,18 +24,15 @@ impl ParentWindowHandler {
let child_window = Window::create(window_open_options, ChildWindowHandler::new)?;
child_window.show()?;

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

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

Ok(())
Expand All @@ -50,7 +45,6 @@ impl WindowHandler for ParentWindowHandler {
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height)?;
self.damaged.set(true);
}

self.child_window.suggest_fallback_scale_factor(new_size.scale_factor)?;
Expand All @@ -71,7 +65,6 @@ impl WindowHandler for ParentWindowHandler {

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

impl ChildWindowHandler {
Expand All @@ -81,18 +74,15 @@ impl ChildWindowHandler {
let size = window.size().physical;
surface.resize(size.width.try_into()?, size.height.try_into()?)?;

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

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

Ok(())
Expand All @@ -105,7 +95,6 @@ impl WindowHandler for ChildWindowHandler {
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height)?;
self.damaged.set(true);
}

Ok(())
Expand Down
53 changes: 25 additions & 28 deletions examples/open_window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct OpenWindowExample {
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
mouse_pos: Cell<PhysicalPosition<f64>>,
is_cursor_inside: Cell<bool>,
damaged: Cell<bool>,
}

impl WindowHandler for OpenWindowExample {
Expand All @@ -35,17 +34,12 @@ impl WindowHandler for OpenWindowExample {
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height)?;
self.damaged.set(true);
}

Ok(())
}

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

fn draw(&self) -> Result<(), HandlerError> {
let mut surface = self.surface.borrow_mut();
let mut pixels = surface.buffer_mut()?;
let size = self.window_context.size();
Expand Down Expand Up @@ -105,13 +99,15 @@ impl WindowHandler for OpenWindowExample {
}

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

Ok(())
}

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

Ok(())
}

fn on_event(&self, event: Event) -> EventStatus {
Expand All @@ -120,21 +116,19 @@ impl WindowHandler for OpenWindowExample {
Event::Mouse(MouseEvent::ButtonPressed { .. }) => copy_to_clipboard("This is a test!"),
Event::Mouse(MouseEvent::CursorMoved { position, .. }) => {
self.mouse_pos.set(position);
self.damaged.set(true);
self.window_context.request_redraw();
}
Event::Mouse(MouseEvent::CursorEntered) => {
self.is_cursor_inside.set(true);
self.damaged.set(true);
self.window_context.request_redraw();
}
Event::Mouse(MouseEvent::CursorLeft) => {
self.is_cursor_inside.set(false);
self.damaged.set(true);
self.window_context.request_redraw();
}
_ => {}
event => log_event(&event),
}

log_event(&event);

EventStatus::Captured
}
}
Expand All @@ -146,15 +140,7 @@ fn main() -> Result<(), baseview::Error> {

let (mut tx, rx) = RingBuffer::new(128);

std::thread::spawn(move || loop {
std::thread::sleep(Duration::from_secs(5));

if tx.push(Message::Hello).is_err() {
println!("Failed sending message");
}
});

Window::create(window_open_options, |window| {
let window = Window::create(window_open_options, |window| {
let ctx = softbuffer::Context::new(window.clone())?;
let mut surface = softbuffer::Surface::new(&ctx, window.clone())?;
let size = window.size().physical;
Expand All @@ -166,10 +152,21 @@ fn main() -> Result<(), baseview::Error> {
rx: rx.into(),
mouse_pos: PhysicalPosition::new(0., 0.).into(),
is_cursor_inside: false.into(),
damaged: true.into(),
})
})?
.run_until_closed()?;
})?;

let waker = window.waker();
std::thread::spawn(move || loop {
std::thread::sleep(Duration::from_secs(5));

if tx.push(Message::Hello).is_err() {
println!("Failed sending message");
} else {
waker.request_poll();
}
});

window.run_until_closed()?;

Ok(())
}
Expand Down
16 changes: 4 additions & 12 deletions examples/plugin_clack/src/window_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub struct OpenWindowExample {
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
mouse_pos: Cell<PhysicalPosition<f64>>,
is_cursor_inside: Cell<bool>,
damaged: Cell<bool>,
}

impl WindowHandler for OpenWindowExample {
Expand All @@ -23,17 +22,12 @@ impl WindowHandler for OpenWindowExample {
(NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height))
{
self.surface.borrow_mut().resize(width, height)?;
self.damaged.set(true);
}

Ok(())
}

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

fn draw(&self) -> Result<(), HandlerError> {
let mut surface = self.surface.borrow_mut();
let mut pixels = surface.buffer_mut()?;
let size = self.window_context.size();
Expand Down Expand Up @@ -93,7 +87,6 @@ impl WindowHandler for OpenWindowExample {
}

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

Ok(())
}
Expand All @@ -102,15 +95,15 @@ impl WindowHandler for OpenWindowExample {
match event {
Event::Mouse(MouseEvent::CursorMoved { position, .. }) => {
self.mouse_pos.set(position);
self.damaged.set(true);
self.window_context.request_redraw();
}
Event::Mouse(MouseEvent::CursorEntered) => {
self.is_cursor_inside.set(true);
self.damaged.set(true);
self.window_context.request_redraw();
}
Event::Mouse(MouseEvent::CursorLeft) => {
self.is_cursor_inside.set(false);
self.damaged.set(true);
self.window_context.request_redraw();
}
Event::Mouse(MouseEvent::ButtonPressed { button: MouseButton::Left, .. }) => {
let mut size = self.window_context.size().physical;
Expand Down Expand Up @@ -141,7 +134,6 @@ impl OpenWindowExample {
surface: surface.into(),
mouse_pos: PhysicalPosition::new(0., 0.).into(),
is_cursor_inside: false.into(),
damaged: true.into(),
})
}
}
12 changes: 2 additions & 10 deletions examples/plugin_clack_femtovg/src/window_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ pub struct FemtovgExample {
gl_context: GlContext,
canvas: RefCell<Canvas<OpenGl>>,
current_mouse_position: Cell<PhysicalPosition<f64>>,
damaged: Cell<bool>,
}

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

fn draw(&self) -> Result<(), HandlerError> {
let context = &self.gl_context;
unsafe { context.make_current()? };

Expand Down Expand Up @@ -56,15 +51,13 @@ impl WindowHandler for FemtovgExample {
canvas.flush();
context.swap_buffers()?;
unsafe { context.make_not_current()? };
self.damaged.set(false);

Ok(())
}

fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> {
let size = new_size.physical;
self.canvas.borrow_mut().set_size(size.width, size.height, new_size.scale_factor as f32);
self.damaged.set(true);

Ok(())
}
Expand All @@ -81,7 +74,7 @@ impl WindowHandler for FemtovgExample {
if position.y > 400. && !self.window_context.has_focus() {
let _ = self.window_context.focus();
}
self.damaged.set(true);
self.window_context.request_redraw();
};

EventStatus::Captured
Expand All @@ -106,7 +99,6 @@ impl FemtovgExample {
gl_context,
window_context,
canvas: canvas.into(),
damaged: true.into(),
current_mouse_position: Cell::new(PhysicalPosition::default()),
})
}
Expand Down
Loading
Loading