1
0
Fork 0
baseview/examples/open_window.rs

69 lines
1.8 KiB
Rust
Raw Normal View History

use std::time::Duration;
use rtrb::{Consumer, RingBuffer};
2023-02-22 11:29:38 +11:00
#[cfg(target_os = "macos")]
2023-02-22 10:45:11 +11:00
use baseview::copy_to_clipboard;
use baseview::{Event, EventStatus, MouseEvent, Window, WindowHandler, WindowScalePolicy};
#[derive(Debug, Clone)]
enum Message {
Hello,
}
2020-12-09 11:37:17 +11:00
struct OpenWindowExample {
rx: Consumer<Message>,
}
impl WindowHandler for OpenWindowExample {
fn on_frame(&mut self, _window: &mut Window) {
2020-12-09 11:37:17 +11:00
while let Ok(message) = self.rx.pop() {
println!("Message: {:?}", message);
}
}
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
match event {
2023-02-22 10:45:11 +11:00
Event::Mouse(e) => {
println!("Mouse event: {:?}", e);
#[cfg(target_os = "macos")]
match e {
MouseEvent::ButtonPressed { button, modifiers } => {
2023-02-23 05:14:54 +11:00
copy_to_clipboard(&"This is a test!")
2023-02-22 10:45:11 +11:00
}
_ => (),
}
}
2020-09-12 01:21:05 +10:00
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
2020-09-12 03:38:06 +10:00
Event::Window(e) => println!("Window event: {:?}", e),
}
EventStatus::Captured
}
2020-09-03 07:23:03 +10:00
}
2020-09-12 02:25:50 +10:00
fn main() {
let window_open_options = baseview::WindowOpenOptions {
title: "baseview".into(),
2020-10-21 11:02:45 +11:00
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,
2020-09-12 02:25:50 +10:00
};
let (mut tx, rx) = RingBuffer::new(128);
2020-12-09 11:37:17 +11:00
::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 });
2020-09-12 02:25:50 +10:00
}