Skip to content

Commit 998ced8

Browse files
authored
Added functional open_parented example (#172)
This PR adds a simple example that allows to test and showcase the `Window::open_parented` method. That example first creates a parent window using `Window::open_blocking`, and then creates a smaller child window using `Window::open_parented`. Both window's handlers log all of their events to the console, in a similar fashion to the `open_window` example. Both windows actually do rendering (unlike the `open_window` example for now): the parent fills its window with a grey backround, and the child fills its window with a red background. This example also uses the `softbuffer` crate to perform the rendering, which allows testing it in a more portable manner and in the simplest use case possible, without having to involve OpenGL or any 3D rendering pipeline at all.
1 parent 26b019d commit 998ced8

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ uuid = { version = "0.8", features = ["v4"] }
4040

4141
[dev-dependencies]
4242
rtrb = "0.2"
43+
softbuffer = "0.3.4"

examples/open_parented.rs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
use baseview::{
2+
Event, EventStatus, PhySize, Window, WindowEvent, WindowHandle, WindowHandler,
3+
WindowScalePolicy,
4+
};
5+
use std::num::NonZeroU32;
6+
7+
struct ParentWindowHandler {
8+
ctx: softbuffer::Context,
9+
surface: softbuffer::Surface,
10+
current_size: PhySize,
11+
damaged: bool,
12+
13+
child_window: Option<WindowHandle>,
14+
}
15+
16+
impl ParentWindowHandler {
17+
pub fn new(window: &mut Window) -> Self {
18+
let ctx = unsafe { softbuffer::Context::new(window) }.unwrap();
19+
let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap();
20+
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();
21+
22+
let window_open_options = baseview::WindowOpenOptions {
23+
title: "baseview child".into(),
24+
size: baseview::Size::new(256.0, 256.0),
25+
scale: WindowScalePolicy::SystemScaleFactor,
26+
27+
// TODO: Add an example that uses the OpenGL context
28+
#[cfg(feature = "opengl")]
29+
gl_config: None,
30+
};
31+
let child_window =
32+
Window::open_parented(window, window_open_options, ChildWindowHandler::new);
33+
34+
// TODO: no way to query physical size initially?
35+
Self {
36+
ctx,
37+
surface,
38+
current_size: PhySize::new(512, 512),
39+
damaged: true,
40+
child_window: Some(child_window),
41+
}
42+
}
43+
}
44+
45+
impl WindowHandler for ParentWindowHandler {
46+
fn on_frame(&mut self, _window: &mut Window) {
47+
let mut buf = self.surface.buffer_mut().unwrap();
48+
if self.damaged {
49+
buf.fill(0xFFAAAAAA);
50+
self.damaged = false;
51+
}
52+
buf.present().unwrap();
53+
}
54+
55+
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
56+
match event {
57+
Event::Window(WindowEvent::Resized(info)) => {
58+
println!("Parent Resized: {:?}", info);
59+
let new_size = info.physical_size();
60+
self.current_size = new_size;
61+
62+
if let (Some(width), Some(height)) =
63+
(NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
64+
{
65+
self.surface.resize(width, height).unwrap();
66+
self.damaged = true;
67+
}
68+
}
69+
Event::Mouse(e) => println!("Parent Mouse event: {:?}", e),
70+
Event::Keyboard(e) => println!("Parent Keyboard event: {:?}", e),
71+
Event::Window(e) => println!("Parent Window event: {:?}", e),
72+
}
73+
74+
EventStatus::Captured
75+
}
76+
}
77+
78+
struct ChildWindowHandler {
79+
ctx: softbuffer::Context,
80+
surface: softbuffer::Surface,
81+
current_size: PhySize,
82+
damaged: bool,
83+
}
84+
85+
impl ChildWindowHandler {
86+
pub fn new(window: &mut Window) -> Self {
87+
let ctx = unsafe { softbuffer::Context::new(window) }.unwrap();
88+
let mut surface = unsafe { softbuffer::Surface::new(&ctx, window) }.unwrap();
89+
surface.resize(NonZeroU32::new(512).unwrap(), NonZeroU32::new(512).unwrap()).unwrap();
90+
91+
// TODO: no way to query physical size initially?
92+
Self { ctx, surface, current_size: PhySize::new(256, 256), damaged: true }
93+
}
94+
}
95+
96+
impl WindowHandler for ChildWindowHandler {
97+
fn on_frame(&mut self, _window: &mut Window) {
98+
let mut buf = self.surface.buffer_mut().unwrap();
99+
if self.damaged {
100+
buf.fill(0xFFAA0000);
101+
self.damaged = false;
102+
}
103+
buf.present().unwrap();
104+
}
105+
106+
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
107+
match event {
108+
Event::Window(WindowEvent::Resized(info)) => {
109+
println!("Child Resized: {:?}", info);
110+
let new_size = info.physical_size();
111+
self.current_size = new_size;
112+
113+
if let (Some(width), Some(height)) =
114+
(NonZeroU32::new(new_size.width), NonZeroU32::new(new_size.height))
115+
{
116+
self.surface.resize(width, height).unwrap();
117+
self.damaged = true;
118+
}
119+
}
120+
Event::Mouse(e) => println!("Child Mouse event: {:?}", e),
121+
Event::Keyboard(e) => println!("Child Keyboard event: {:?}", e),
122+
Event::Window(e) => println!("Child Window event: {:?}", e),
123+
}
124+
125+
EventStatus::Captured
126+
}
127+
}
128+
129+
fn main() {
130+
let window_open_options = baseview::WindowOpenOptions {
131+
title: "baseview".into(),
132+
size: baseview::Size::new(512.0, 512.0),
133+
scale: WindowScalePolicy::SystemScaleFactor,
134+
135+
// TODO: Add an example that uses the OpenGL context
136+
#[cfg(feature = "opengl")]
137+
gl_config: None,
138+
};
139+
140+
Window::open_blocking(window_open_options, ParentWindowHandler::new);
141+
}

0 commit comments

Comments
 (0)