-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathopen_window.rs
More file actions
107 lines (88 loc) · 2.96 KB
/
open_window.rs
File metadata and controls
107 lines (88 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::num::NonZeroU32;
use std::time::Duration;
use rtrb::{Consumer, RingBuffer};
#[cfg(target_os = "macos")]
use baseview::{copy_to_clipboard, MouseEvent};
use baseview::{
Event, EventStatus, PhySize, Window, WindowEvent, WindowHandler, WindowScalePolicy,
};
#[derive(Debug, Clone)]
enum Message {
Hello,
}
struct OpenWindowExample {
rx: Consumer<Message>,
_ctx: softbuffer::Context,
surface: softbuffer::Surface,
current_size: PhySize,
damaged: bool,
}
impl WindowHandler for OpenWindowExample {
fn on_frame(&mut self) {
let mut buf = self.surface.buffer_mut().unwrap();
if self.damaged {
buf.fill(0xFFAAAAAA);
self.damaged = false;
}
buf.present().unwrap();
while let Ok(message) = self.rx.pop() {
println!("Message: {:?}", message);
}
}
fn on_event(&mut self, event: Event) -> EventStatus {
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;
}
}
_ => {}
}
log_event(&event);
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 tx.push(Message::Hello).is_err() {
println!("Failed sending message");
}
});
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();
OpenWindowExample {
_ctx: ctx,
surface,
rx,
current_size: PhySize::new(512, 512),
damaged: true,
}
});
}
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),
}
}