1
0
Fork 0
baseview/examples/open_window.rs

58 lines
1.4 KiB
Rust
Raw Normal View History

use std::time::Duration;
2020-12-08 18:37:17 -06:00
use rtrb::{RingBuffer, Consumer};
use baseview::{Event, EventStatus, Window, WindowHandler, WindowScalePolicy};
#[derive(Debug, Clone)]
enum Message {
Hello
}
2020-12-08 18:37:17 -06:00
struct OpenWindowExample {
rx: Consumer<Message>,
}
impl WindowHandler for OpenWindowExample {
fn on_frame(&mut self, _window: &mut Window) {
2020-12-08 18:37:17 -06:00
while let Ok(message) = self.rx.pop() {
println!("Message: {:?}", message);
}
}
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
match event {
2020-09-11 10:21:05 -05:00
Event::Mouse(e) => println!("Mouse event: {:?}", e),
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
2020-09-11 12:38:06 -05:00
Event::Window(e) => println!("Window event: {:?}", e),
}
EventStatus::Captured
}
2020-09-02 16:23:03 -05:00
}
2020-09-11 18:25:50 +02:00
fn main() {
let window_open_options = baseview::WindowOpenOptions {
title: "baseview".into(),
2020-10-20 19:02:45 -05:00
size: baseview::Size::new(512.0, 512.0),
scale: WindowScalePolicy::SystemScaleFactor,
2020-09-11 18:25:50 +02:00
};
2020-12-08 18:37:17 -06:00
let (mut tx, rx) = RingBuffer::new(128).split();
::std::thread::spawn(move || {
loop {
::std::thread::sleep(Duration::from_secs(5));
2020-12-08 18:37:17 -06:00
if let Err(_) = tx.push(Message::Hello) {
println!("Failed sending message");
}
}
});
Window::open_blocking(
window_open_options,
|_| OpenWindowExample { rx }
);
2020-09-11 18:25:50 +02:00
}