Skip to content

Commit 65d9704

Browse files
authored
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.
1 parent bad50d8 commit 65d9704

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

examples/open_window.rs

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
use std::num::NonZeroU32;
12
use std::time::Duration;
23

34
use rtrb::{Consumer, RingBuffer};
45

56
#[cfg(target_os = "macos")]
67
use baseview::copy_to_clipboard;
7-
use baseview::{Event, EventStatus, MouseEvent, Window, WindowHandler, WindowScalePolicy};
8+
use baseview::{
9+
Event, EventStatus, MouseEvent, PhySize, Window, WindowEvent, WindowHandler, WindowScalePolicy,
10+
};
811

912
#[derive(Debug, Clone)]
1013
enum Message {
@@ -13,32 +16,48 @@ enum Message {
1316

1417
struct OpenWindowExample {
1518
rx: Consumer<Message>,
19+
20+
ctx: softbuffer::Context,
21+
surface: softbuffer::Surface,
22+
current_size: PhySize,
23+
damaged: bool,
1624
}
1725

1826
impl WindowHandler for OpenWindowExample {
1927
fn on_frame(&mut self, _window: &mut Window) {
28+
let mut buf = self.surface.buffer_mut().unwrap();
29+
if self.damaged {
30+
buf.fill(0xFFAAAAAA);
31+
self.damaged = false;
32+
}
33+
buf.present().unwrap();
34+
2035
while let Ok(message) = self.rx.pop() {
2136
println!("Message: {:?}", message);
2237
}
2338
}
2439

2540
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
26-
match event {
27-
Event::Mouse(e) => {
28-
println!("Mouse event: {:?}", e);
29-
30-
#[cfg(target_os = "macos")]
31-
match e {
32-
MouseEvent::ButtonPressed { .. } => {
33-
copy_to_clipboard(&"This is a test!")
34-
}
35-
_ => (),
41+
match &event {
42+
#[cfg(target_os = "macos")]
43+
Event::Mouse(MouseEvent::ButtonPressed { .. }) => copy_to_clipboard(&"This is a test!"),
44+
Event::Window(WindowEvent::Resized(info)) => {
45+
println!("Resized: {:?}", info);
46+
let new_size = info.physical_size();
47+
self.current_size = new_size;
48+
49+
if let (Some(width), Some(height)) =
50+
(NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
51+
{
52+
self.surface.resize(width, height).unwrap();
53+
self.damaged = true;
3654
}
3755
}
38-
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
39-
Event::Window(e) => println!("Window event: {:?}", e),
56+
_ => {}
4057
}
4158

59+
log_event(&event);
60+
4261
EventStatus::Captured
4362
}
4463
}
@@ -56,13 +75,27 @@ fn main() {
5675

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

59-
::std::thread::spawn(move || loop {
60-
::std::thread::sleep(Duration::from_secs(5));
78+
std::thread::spawn(move || loop {
79+
std::thread::sleep(Duration::from_secs(5));
6180

6281
if let Err(_) = tx.push(Message::Hello) {
6382
println!("Failed sending message");
6483
}
6584
});
6685

67-
Window::open_blocking(window_open_options, |_| OpenWindowExample { rx });
86+
Window::open_blocking(window_open_options, |window| {
87+
let ctx = unsafe { softbuffer::Context::new(window) }.unwrap();
88+
let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap();
89+
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();
90+
91+
OpenWindowExample { ctx, surface, rx, current_size: PhySize::new(512, 512), damaged: true }
92+
});
93+
}
94+
95+
fn log_event(event: &Event) {
96+
match event {
97+
Event::Mouse(e) => println!("Mouse event: {:?}", e),
98+
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
99+
Event::Window(e) => println!("Window event: {:?}", e),
100+
}
68101
}

0 commit comments

Comments
 (0)