1
0
Fork 0
baseview/examples/open_window.rs
2023-02-22 10:14:54 -08:00

69 lines
1.8 KiB
Rust

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};
#[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);
#[cfg(target_os = "macos")]
match e {
MouseEvent::ButtonPressed { button, modifiers } => {
copy_to_clipboard(&"This is a test!")
}
_ => (),
}
}
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,
// TODO: Add an example that uses the OpenGL context
#[cfg(feature = "opengl")]
gl_config: None,
};
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 });
}