2024-03-26 10:41:39 +11:00
|
|
|
use std::num::NonZeroU32;
|
2020-11-30 00:37:59 +11:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-11-10 17:57:54 +11:00
|
|
|
use rtrb::{Consumer, RingBuffer};
|
2020-09-03 07:22:49 +10:00
|
|
|
|
2023-02-22 11:29:38 +11:00
|
|
|
#[cfg(target_os = "macos")]
|
2024-03-27 14:46:47 +11:00
|
|
|
use baseview::{copy_to_clipboard, MouseEvent};
|
2024-03-26 10:41:39 +11:00
|
|
|
use baseview::{
|
2024-03-27 14:46:47 +11:00
|
|
|
Event, EventStatus, PhySize, Window, WindowEvent, WindowHandler, WindowScalePolicy,
|
2024-03-26 10:41:39 +11:00
|
|
|
};
|
2020-11-30 00:37:59 +11:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum Message {
|
2021-11-10 17:57:54 +11:00
|
|
|
Hello,
|
2020-11-30 00:37:59 +11:00
|
|
|
}
|
|
|
|
|
2020-12-09 11:37:17 +11:00
|
|
|
struct OpenWindowExample {
|
|
|
|
rx: Consumer<Message>,
|
2024-03-26 10:41:39 +11:00
|
|
|
|
2024-03-27 14:46:47 +11:00
|
|
|
_ctx: softbuffer::Context,
|
2024-03-26 10:41:39 +11:00
|
|
|
surface: softbuffer::Surface,
|
|
|
|
current_size: PhySize,
|
|
|
|
damaged: bool,
|
2020-12-09 11:37:17 +11:00
|
|
|
}
|
2020-11-30 00:37:59 +11:00
|
|
|
|
2020-10-21 06:31:44 +11:00
|
|
|
impl WindowHandler for OpenWindowExample {
|
2021-01-27 18:09:05 +11:00
|
|
|
fn on_frame(&mut self, _window: &mut Window) {
|
2024-03-26 10:41:39 +11:00
|
|
|
let mut buf = self.surface.buffer_mut().unwrap();
|
|
|
|
if self.damaged {
|
|
|
|
buf.fill(0xFFAAAAAA);
|
|
|
|
self.damaged = false;
|
|
|
|
}
|
|
|
|
buf.present().unwrap();
|
|
|
|
|
2020-12-09 11:37:17 +11:00
|
|
|
while let Ok(message) = self.rx.pop() {
|
|
|
|
println!("Message: {:?}", message);
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 05:41:26 +10:00
|
|
|
|
2021-02-10 07:47:31 +11:00
|
|
|
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
|
2024-03-26 10:41:39 +11:00
|
|
|
match &event {
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
Event::Mouse(MouseEvent::ButtonPressed { .. }) => copy_to_clipboard(&"This is a test!"),
|
|
|
|
Event::Window(WindowEvent::Resized(info)) => {
|
|
|
|
println!("Resized: {:?}", info);
|
|
|
|
let new_size = info.physical_size();
|
|
|
|
self.current_size = new_size;
|
|
|
|
|
|
|
|
if let (Some(width), Some(height)) =
|
|
|
|
(NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
|
|
|
|
{
|
|
|
|
self.surface.resize(width, height).unwrap();
|
|
|
|
self.damaged = true;
|
2023-02-22 10:45:11 +11:00
|
|
|
}
|
|
|
|
}
|
2024-03-26 10:41:39 +11:00
|
|
|
_ => {}
|
2020-09-03 07:22:49 +10:00
|
|
|
}
|
2021-02-10 07:47:31 +11:00
|
|
|
|
2024-03-26 10:41:39 +11:00
|
|
|
log_event(&event);
|
|
|
|
|
2021-02-10 07:47:31 +11:00
|
|
|
EventStatus::Captured
|
2020-09-03 07:22:49 +10:00
|
|
|
}
|
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,
|
2022-02-08 05:00:03 +11:00
|
|
|
|
|
|
|
// TODO: Add an example that uses the OpenGL context
|
|
|
|
#[cfg(feature = "opengl")]
|
|
|
|
gl_config: None,
|
2020-09-12 02:25:50 +10:00
|
|
|
};
|
|
|
|
|
2021-11-16 17:00:22 +11:00
|
|
|
let (mut tx, rx) = RingBuffer::new(128);
|
2020-12-09 11:37:17 +11:00
|
|
|
|
2024-03-26 10:41:39 +11:00
|
|
|
std::thread::spawn(move || loop {
|
|
|
|
std::thread::sleep(Duration::from_secs(5));
|
2020-11-30 00:37:59 +11:00
|
|
|
|
2021-11-10 17:57:54 +11:00
|
|
|
if let Err(_) = tx.push(Message::Hello) {
|
|
|
|
println!("Failed sending message");
|
2020-11-30 02:57:34 +11:00
|
|
|
}
|
|
|
|
});
|
2020-11-30 00:37:59 +11:00
|
|
|
|
2024-03-26 10:41:39 +11:00
|
|
|
Window::open_blocking(window_open_options, |window| {
|
|
|
|
let ctx = unsafe { softbuffer::Context::new(window) }.unwrap();
|
|
|
|
let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap();
|
|
|
|
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();
|
|
|
|
|
2024-03-27 14:46:47 +11:00
|
|
|
OpenWindowExample {
|
|
|
|
_ctx: ctx,
|
|
|
|
surface,
|
|
|
|
rx,
|
|
|
|
current_size: PhySize::new(512, 512),
|
|
|
|
damaged: true,
|
|
|
|
}
|
2024-03-26 10:41:39 +11:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log_event(event: &Event) {
|
|
|
|
match event {
|
|
|
|
Event::Mouse(e) => println!("Mouse event: {:?}", e),
|
|
|
|
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
|
|
|
|
Event::Window(e) => println!("Window event: {:?}", e),
|
|
|
|
}
|
2020-09-12 02:25:50 +10:00
|
|
|
}
|