1
0
Fork 0

Render a background for the open_window example (#175)

This PR adds code to render a basic gray background to the opened window in the `open_window` example.

This also helps making the example a bit more useful, since most users will want to render to their window.

And also it looks nicer. 🙂 

This is done using the `softbuffer` crate, in the same manner of the `open_parented` introduced in #172.
This commit is contained in:
Adrien Prokopowicz 2024-03-26 00:41:39 +01:00 committed by GitHub
parent bad50d886a
commit 65d970495f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,10 +1,13 @@
use std::num::NonZeroU32;
use std::time::Duration; use std::time::Duration;
use rtrb::{Consumer, RingBuffer}; use rtrb::{Consumer, RingBuffer};
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
use baseview::copy_to_clipboard; use baseview::copy_to_clipboard;
use baseview::{Event, EventStatus, MouseEvent, Window, WindowHandler, WindowScalePolicy}; use baseview::{
Event, EventStatus, MouseEvent, PhySize, Window, WindowEvent, WindowHandler, WindowScalePolicy,
};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Message { enum Message {
@ -13,32 +16,48 @@ enum Message {
struct OpenWindowExample { struct OpenWindowExample {
rx: Consumer<Message>, rx: Consumer<Message>,
ctx: softbuffer::Context,
surface: softbuffer::Surface,
current_size: PhySize,
damaged: bool,
} }
impl WindowHandler for OpenWindowExample { impl WindowHandler for OpenWindowExample {
fn on_frame(&mut self, _window: &mut Window) { fn on_frame(&mut self, _window: &mut Window) {
let mut buf = self.surface.buffer_mut().unwrap();
if self.damaged {
buf.fill(0xFFAAAAAA);
self.damaged = false;
}
buf.present().unwrap();
while let Ok(message) = self.rx.pop() { while let Ok(message) = self.rx.pop() {
println!("Message: {:?}", message); println!("Message: {:?}", message);
} }
} }
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus { fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
match event { match &event {
Event::Mouse(e) => { #[cfg(target_os = "macos")]
println!("Mouse event: {:?}", e); 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;
#[cfg(target_os = "macos")] if let (Some(width), Some(height)) =
match e { (NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
MouseEvent::ButtonPressed { .. } => { {
copy_to_clipboard(&"This is a test!") self.surface.resize(width, height).unwrap();
} self.damaged = true;
_ => (),
} }
} }
Event::Keyboard(e) => println!("Keyboard event: {:?}", e), _ => {}
Event::Window(e) => println!("Window event: {:?}", e),
} }
log_event(&event);
EventStatus::Captured EventStatus::Captured
} }
} }
@ -56,13 +75,27 @@ fn main() {
let (mut tx, rx) = RingBuffer::new(128); let (mut tx, rx) = RingBuffer::new(128);
::std::thread::spawn(move || loop { std::thread::spawn(move || loop {
::std::thread::sleep(Duration::from_secs(5)); std::thread::sleep(Duration::from_secs(5));
if let Err(_) = tx.push(Message::Hello) { if let Err(_) = tx.push(Message::Hello) {
println!("Failed sending message"); println!("Failed sending message");
} }
}); });
Window::open_blocking(window_open_options, |_| OpenWindowExample { rx }); 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();
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();
OpenWindowExample { ctx, surface, rx, current_size: PhySize::new(512, 512), damaged: true }
});
}
fn log_event(event: &Event) {
match event {
Event::Mouse(e) => println!("Mouse event: {:?}", e),
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
Event::Window(e) => println!("Window event: {:?}", e),
}
} }