1
0
Fork 0
baseview/examples/open_window.rs
Billy Messenger f6e99e9aa6
add ability to close window from user code, add HostWindowHandle (#103)
* add ability to close window from user code, add HostWindowHandle

* fix manual close method for Mac, rename HostWindowHandle to ChildWindowHandle

* fix rustfmt.toml and run cargo format

* fix merge conflict mistake

* fix more merge conflict mistakes

* implement requested changes (with a non-broken commit this time)

* implement requested changes

* slight reordering of impls
2021-11-16 00:00:22 -06:00

53 lines
1.3 KiB
Rust

use std::time::Duration;
use rtrb::{Consumer, RingBuffer};
use baseview::{Event, EventStatus, Window, WindowHandler, WindowScalePolicy};
#[derive(Debug, Clone)]
enum Message {
Hello,
}
struct OpenWindowExample {
rx: Consumer<Message>,
}
impl WindowHandler for OpenWindowExample {
fn on_frame(&mut self, _window: &mut Window) {
while let Ok(message) = self.rx.pop() {
println!("Message: {:?}", message);
}
}
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
match event {
Event::Mouse(e) => println!("Mouse event: {:?}", e),
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
Event::Window(e) => println!("Window event: {:?}", e),
}
EventStatus::Captured
}
}
fn main() {
let window_open_options = baseview::WindowOpenOptions {
title: "baseview".into(),
size: baseview::Size::new(512.0, 512.0),
scale: WindowScalePolicy::SystemScaleFactor,
};
let (mut tx, rx) = RingBuffer::new(128);
::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 });
}