Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render a background for the open_window example #175

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Changes from 1 commit
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
45 changes: 38 additions & 7 deletions examples/open_window.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::num::NonZeroU32;
use std::time::Duration;

use rtrb::{Consumer, RingBuffer};

#[cfg(target_os = "macos")]
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)]
enum Message {
Expand All @@ -13,10 +16,22 @@ enum Message {

struct OpenWindowExample {
rx: Consumer<Message>,

ctx: softbuffer::Context,
surface: softbuffer::Surface,
current_size: PhySize,
damaged: bool,
}

impl WindowHandler for OpenWindowExample {
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() {
println!("Message: {:?}", message);
}
Expand All @@ -29,12 +44,22 @@ impl WindowHandler for OpenWindowExample {

#[cfg(target_os = "macos")]
match e {
MouseEvent::ButtonPressed { .. } => {
copy_to_clipboard(&"This is a test!")
}
MouseEvent::ButtonPressed { .. } => copy_to_clipboard(&"This is a test!"),
_ => (),
}
}
Event::Window(WindowEvent::Resized(info)) => {
micahrj marked this conversation as resolved.
Show resolved Hide resolved
println!("Resized: {:?}", info);
let new_size = info.physical_size();
self.current_size = 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;
}
}
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
Event::Window(e) => println!("Window event: {:?}", e),
}
Expand All @@ -56,13 +81,19 @@ fn main() {

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

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

if let Err(_) = tx.push(Message::Hello) {
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 }
});
}
Loading