2020-11-29 14:37:59 +01:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-11-10 17:57:54 +11:00
|
|
|
use rtrb::{Consumer, RingBuffer};
|
2020-09-02 16:22:49 -05:00
|
|
|
|
2023-02-21 16:29:38 -08:00
|
|
|
#[cfg(target_os = "macos")]
|
2023-02-21 15:45:11 -08:00
|
|
|
use baseview::copy_to_clipboard;
|
|
|
|
use baseview::{Event, EventStatus, MouseEvent, Window, WindowHandler, WindowScalePolicy};
|
2020-11-29 14:37:59 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum Message {
|
2021-11-10 17:57:54 +11:00
|
|
|
Hello,
|
2020-11-29 14:37:59 +01:00
|
|
|
}
|
|
|
|
|
2020-12-08 18:37:17 -06:00
|
|
|
struct OpenWindowExample {
|
|
|
|
rx: Consumer<Message>,
|
|
|
|
}
|
2020-11-29 14:37:59 +01:00
|
|
|
|
2020-10-20 21:31:44 +02:00
|
|
|
impl WindowHandler for OpenWindowExample {
|
2021-01-27 02:09:05 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-09-05 14:41:26 -05:00
|
|
|
|
2021-02-09 21:47:31 +01:00
|
|
|
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
|
2020-09-03 11:38:22 -05:00
|
|
|
match event {
|
2023-02-21 15:45:11 -08:00
|
|
|
Event::Mouse(e) => {
|
|
|
|
println!("Mouse event: {:?}", e);
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
match e {
|
2023-12-16 19:29:27 -06:00
|
|
|
MouseEvent::ButtonPressed { .. } => {
|
2023-02-22 10:14:54 -08:00
|
|
|
copy_to_clipboard(&"This is a test!")
|
2023-02-21 15:45:11 -08:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2020-09-11 10:21:05 -05:00
|
|
|
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
|
2020-09-11 12:38:06 -05:00
|
|
|
Event::Window(e) => println!("Window event: {:?}", e),
|
2020-09-02 16:22:49 -05:00
|
|
|
}
|
2021-02-09 21:47:31 +01:00
|
|
|
|
|
|
|
EventStatus::Captured
|
2020-09-02 16:22:49 -05:00
|
|
|
}
|
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,
|
2022-02-07 19:00:03 +01:00
|
|
|
|
|
|
|
// TODO: Add an example that uses the OpenGL context
|
|
|
|
#[cfg(feature = "opengl")]
|
|
|
|
gl_config: None,
|
2020-09-11 18:25:50 +02:00
|
|
|
};
|
|
|
|
|
2021-11-16 00:00:22 -06:00
|
|
|
let (mut tx, rx) = RingBuffer::new(128);
|
2020-12-08 18:37:17 -06:00
|
|
|
|
2021-11-10 17:57:54 +11:00
|
|
|
::std::thread::spawn(move || loop {
|
|
|
|
::std::thread::sleep(Duration::from_secs(5));
|
2020-11-29 14:37:59 +01:00
|
|
|
|
2021-11-10 17:57:54 +11:00
|
|
|
if let Err(_) = tx.push(Message::Hello) {
|
|
|
|
println!("Failed sending message");
|
2020-11-29 16:57:34 +01:00
|
|
|
}
|
|
|
|
});
|
2020-11-29 14:37:59 +01:00
|
|
|
|
2021-11-10 17:57:54 +11:00
|
|
|
Window::open_blocking(window_open_options, |_| OpenWindowExample { rx });
|
2020-09-11 18:25:50 +02:00
|
|
|
}
|