Skip to content
Draft
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
60 changes: 34 additions & 26 deletions examples/open_parented/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use baseview::{
Event, EventStatus, PhySize, Window, WindowEvent, WindowHandle, WindowHandler,
WindowOpenOptions,
};
use std::cell::{Cell, RefCell};
use std::num::NonZeroU32;

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

_child_window: Option<WindowHandle>,
}
Expand All @@ -28,36 +29,37 @@ impl ParentWindowHandler {
// TODO: no way to query physical size initially?
Self {
_ctx: ctx,
surface,
current_size: PhySize::new(512, 512),
damaged: true,
surface: surface.into(),
current_size: PhySize::new(512, 512).into(),
damaged: true.into(),
_child_window: Some(child_window),
}
}
}

impl WindowHandler for ParentWindowHandler {
fn on_frame(&mut self, _window: &mut Window) {
let mut buf = self.surface.buffer_mut().unwrap();
if self.damaged {
fn on_frame(&self, _window: &mut Window) {
let mut surface = self.surface.borrow_mut();
let mut buf = surface.buffer_mut().unwrap();
if self.damaged.get() {
buf.fill(0xFFAAAAAA);
self.damaged = false;
self.damaged.set(false);
}
buf.present().unwrap();
}

fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&self, _window: &mut Window, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(info)) => {
println!("Parent Resized: {:?}", info);
let new_size = info.physical_size();
self.current_size = new_size;
self.current_size.set(new_size);

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
{
self.surface.resize(width, height).unwrap();
self.damaged = true;
self.surface.borrow_mut().resize(width, height).unwrap();
self.damaged.set(true);
}
}
Event::Mouse(e) => println!("Parent Mouse event: {:?}", e),
Expand All @@ -71,9 +73,9 @@ impl WindowHandler for ParentWindowHandler {

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

impl ChildWindowHandler {
Expand All @@ -83,32 +85,38 @@ impl ChildWindowHandler {
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();

// TODO: no way to query physical size initially?
Self { _ctx: ctx, surface, current_size: PhySize::new(256, 256), damaged: true }
Self {
_ctx: ctx,
surface: surface.into(),
current_size: PhySize::new(256, 256).into(),
damaged: true.into(),
}
}
}

impl WindowHandler for ChildWindowHandler {
fn on_frame(&mut self, _window: &mut Window) {
let mut buf = self.surface.buffer_mut().unwrap();
if self.damaged {
fn on_frame(&self, _window: &mut Window) {
let mut surface = self.surface.borrow_mut();
let mut buf = surface.buffer_mut().unwrap();
if self.damaged.get() {
buf.fill(0xFFAA0000);
self.damaged = false;
self.damaged.set(false);
}
buf.present().unwrap();
}

fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&self, _window: &mut Window, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(info)) => {
println!("Child Resized: {:?}", info);
let new_size = info.physical_size();
self.current_size = new_size;
self.current_size.set(new_size);

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
{
self.surface.resize(width, height).unwrap();
self.damaged = true;
self.surface.borrow_mut().resize(width, height).unwrap();
self.damaged.set(true);
}
}
Event::Mouse(e) => println!("Child Mouse event: {:?}", e),
Expand Down
36 changes: 19 additions & 17 deletions examples/open_window/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::{Cell, RefCell};
use std::num::NonZeroU32;
use std::time::Duration;

Expand All @@ -15,42 +16,43 @@ enum Message {
}

struct OpenWindowExample {
rx: Consumer<Message>,
rx: RefCell<Consumer<Message>>,

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

impl WindowHandler for OpenWindowExample {
fn on_frame(&mut self, _window: &mut Window) {
let mut buf = self.surface.buffer_mut().unwrap();
if self.damaged {
fn on_frame(&self, _window: &mut Window) {
let mut surface = self.surface.borrow_mut();
let mut buf = surface.buffer_mut().unwrap();
if self.damaged.get() {
buf.fill(0xFFAAAAAA);
self.damaged = false;
self.damaged.set(false);
}
buf.present().unwrap();

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

fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&self, _window: &mut Window, event: Event) -> EventStatus {
match &event {
#[cfg(target_os = "macos")]
Event::Mouse(MouseEvent::ButtonPressed { .. }) => copy_to_clipboard("This is a test!"),
Event::Window(WindowEvent::Resized(info)) => {
println!("Resized: {:?}", info);
let new_size = info.physical_size();
self.current_size = new_size;
self.current_size.set(new_size);

if let (Some(width), Some(height)) =
(NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
{
self.surface.resize(width, height).unwrap();
self.damaged = true;
self.surface.borrow_mut().resize(width, height).unwrap();
self.damaged.set(true);
}
}
_ => {}
Expand Down Expand Up @@ -82,10 +84,10 @@ fn main() {

OpenWindowExample {
_ctx: ctx,
surface,
rx,
current_size: PhySize::new(512, 512),
damaged: true,
surface: surface.into(),
rx: rx.into(),
current_size: PhySize::new(512, 512).into(),
damaged: true.into(),
}
});
}
Expand Down
58 changes: 33 additions & 25 deletions examples/render_femtovg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use baseview::{
};
use femtovg::renderer::OpenGl;
use femtovg::{Canvas, Color};
use std::cell::{Cell, RefCell};

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

impl FemtovgExample {
Expand All @@ -27,31 +28,34 @@ impl FemtovgExample {

unsafe { context.make_not_current() };
Self {
canvas,
current_size: WindowInfo::from_logical_size(Size { width: 512.0, height: 512.0 }, 1.0),
current_mouse_position: PhyPoint { x: 256, y: 256 },
damaged: true,
canvas: canvas.into(),
current_size: WindowInfo::from_logical_size(Size { width: 512.0, height: 512.0 }, 1.0)
.into(),
current_mouse_position: PhyPoint { x: 256, y: 256 }.into(),
damaged: true.into(),
}
}
}

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

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

let screen_height = self.canvas.height();
let screen_width = self.canvas.width();
let mut canvas = self.canvas.borrow_mut();

let screen_height = canvas.height();
let screen_width = canvas.width();

// Clear
self.canvas.clear_rect(0, 0, screen_width, screen_height, Color::rgb(0xAA, 0xAA, 0xAA));
canvas.clear_rect(0, 0, screen_width, screen_height, Color::rgb(0xAA, 0xAA, 0xAA));

// Make big blue rectangle
self.canvas.clear_rect(
canvas.clear_rect(
(screen_width as f32 * 0.1).floor() as u32,
(screen_height as f32 * 0.1).floor() as u32,
(screen_width as f32 * 0.8).floor() as u32,
Expand All @@ -60,37 +64,41 @@ impl WindowHandler for FemtovgExample {
);

// Make smol orange rectangle
self.canvas.clear_rect(
(self.current_mouse_position.x - 15).clamp(0, screen_width as i32 - 30) as u32,
(self.current_mouse_position.y - 15).clamp(0, screen_height as i32 - 30) as u32,
canvas.clear_rect(
(self.current_mouse_position.get().x - 15).clamp(0, screen_width as i32 - 30) as u32,
(self.current_mouse_position.get().y - 15).clamp(0, screen_height as i32 - 30) as u32,
30,
30,
Color::rgbf(0.9, 0.3, 0.),
);

// Tell renderer to execute all drawing commands
self.canvas.flush();
canvas.flush();
context.swap_buffers();
unsafe { context.make_not_current() };
self.damaged = false;
self.damaged.set(false);
}

fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&self, _window: &mut Window, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(size)) => {
let phy_size = size.physical_size();
self.current_size = size;
self.canvas.set_size(phy_size.width, phy_size.height, size.scale() as f32);
self.damaged = true;
self.current_size.set(size);
self.canvas.borrow_mut().set_size(
phy_size.width,
phy_size.height,
size.scale() as f32,
);
self.damaged.set(true);
}
Event::Mouse(
MouseEvent::CursorMoved { position, .. }
| MouseEvent::DragEntered { position, .. }
| MouseEvent::DragMoved { position, .. }
| MouseEvent::DragDropped { position, .. },
) => {
self.current_mouse_position = position.to_physical(&self.current_size);
self.damaged = true;
self.current_mouse_position.set(position.to_physical(&self.current_size.get()));
self.damaged.set(true);
}
_ => {}
};
Expand Down
Loading