Skip to content

Commit 285c6ea

Browse files
authored
Host sizing operations (#295)
1 parent 6e7dec0 commit 285c6ea

16 files changed

Lines changed: 521 additions & 160 deletions

File tree

examples/open_parented/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct ParentWindowHandler {
1010
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
1111
damaged: Cell<bool>,
1212

13-
_child_window: Option<WindowHandle>,
13+
child_window: WindowHandle,
1414
}
1515

1616
impl ParentWindowHandler {
@@ -27,11 +27,7 @@ impl ParentWindowHandler {
2727

2828
let child_window = baseview::create_window(window_open_options, ChildWindowHandler::new)?;
2929

30-
Ok(Self {
31-
surface: surface.into(),
32-
damaged: true.into(),
33-
_child_window: Some(child_window),
34-
})
30+
Ok(Self { surface: surface.into(), damaged: true.into(), child_window })
3531
}
3632
}
3733

@@ -58,6 +54,10 @@ impl WindowHandler for ParentWindowHandler {
5854
self.damaged.set(true);
5955
}
6056

57+
let child_size =
58+
LogicalSize::new(new_size.logical.width / 2., new_size.logical.height / 2.);
59+
self.child_window.suggest_fallback_scale_factor(new_size.scale_factor)?;
60+
self.child_window.resize(child_size.into())?;
6161
Ok(())
6262
}
6363

examples/plugin_clack/src/gui.rs

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::window_handler::OpenWindowExample;
22
use crate::ExamplePluginMainThread;
3-
use baseview::dpi::PhysicalSize;
3+
use baseview::dpi::*;
44
use baseview::gl::GlConfig;
5-
use baseview::{WindowHandle, WindowOpenOptions};
5+
use baseview::{WindowHandle, WindowOpenOptions, WindowSize};
66
use clack_extensions::gui::{
7-
GuiApiType, GuiConfiguration, GuiResizeHints, GuiSize, PluginGuiImpl, Window as ClapWindow,
7+
AspectRatioStrategy, GuiApiType, GuiConfiguration, GuiResizeHints, GuiSize, PluginGuiImpl,
8+
Window as ClapWindow,
89
};
910
use clack_plugin::plugin::PluginError;
1011

@@ -39,30 +40,51 @@ impl PluginGuiImpl for ExamplePluginMainThread {
3940
gui.handle.close()
4041
}
4142

42-
fn set_scale(&mut self, _scale: f64) -> Result<(), PluginError> {
43-
// Unsupported
43+
fn set_scale(&mut self, scale: f64) -> Result<(), PluginError> {
44+
let Some(gui) = &self.gui else {
45+
return Err(PluginError::Message("set_scale called without a GUI active"));
46+
};
47+
gui.handle.suggest_fallback_scale_factor(scale)?;
48+
4449
Ok(())
4550
}
4651

4752
fn get_size(&mut self) -> Option<GuiSize> {
48-
// Unsupported
49-
Some(GuiSize { width: 400, height: 200 })
53+
let Some(gui) = self.gui.as_ref() else {
54+
// Because we delayed the window creation, this will get called without a GUI active.
55+
// During that time, return the default UI size.
56+
return Some(GuiSize { width: 400, height: 200 });
57+
};
58+
Some(window_size_to_gui_size(gui.handle.size()))
5059
}
5160

5261
fn can_resize(&mut self) -> bool {
53-
false // Non-resizeable windows not supported yet
62+
true // Non-resizeable windows not supported yet
5463
}
5564

5665
fn get_resize_hints(&mut self) -> Option<GuiResizeHints> {
57-
None // Not supported yet
66+
Some(GuiResizeHints {
67+
strategy: AspectRatioStrategy::Disregard, // Not supported
68+
69+
// Non-resizeable windows not supported yet
70+
can_resize_vertically: true,
71+
can_resize_horizontally: true,
72+
})
5873
}
5974

60-
fn adjust_size(&mut self, _size: GuiSize) -> Option<GuiSize> {
61-
None // Not supported yet
75+
fn adjust_size(&mut self, size: GuiSize) -> Option<GuiSize> {
76+
Some(size) // Not supported yet
6277
}
6378

64-
fn set_size(&mut self, _size: GuiSize) -> Result<(), PluginError> {
65-
Ok(()) // Not supported yet
79+
fn set_size(&mut self, size: GuiSize) -> Result<(), PluginError> {
80+
let Some(gui) = &self.gui else {
81+
return Err(PluginError::Message("set_size called without a GUI active"));
82+
};
83+
84+
let size = gui_size_to_window_size(size);
85+
gui.handle.resize(size)?;
86+
87+
Ok(())
6688
}
6789

6890
#[allow(deprecated)]
@@ -98,3 +120,28 @@ impl PluginGuiImpl for ExamplePluginMainThread {
98120
Ok(()) // Not supported yet
99121
}
100122
}
123+
124+
fn window_size_to_gui_size(size: WindowSize) -> GuiSize {
125+
#[cfg(target_os = "macos")]
126+
{
127+
let size = size.logical.cast();
128+
GuiSize { width: size.width, height: size.height }
129+
}
130+
131+
#[cfg(not(target_os = "macos"))]
132+
{
133+
let size = size.physical.cast();
134+
GuiSize { width: size.width, height: size.height }
135+
}
136+
}
137+
138+
fn gui_size_to_window_size(size: GuiSize) -> Size {
139+
#[cfg(target_os = "macos")]
140+
{
141+
Size::Logical(LogicalSize::new(size.width, size.height).cast())
142+
}
143+
#[cfg(not(target_os = "macos"))]
144+
{
145+
Size::Physical(PhysicalSize::new(size.width, size.height))
146+
}
147+
}

examples/plugin_clack/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl DefaultPluginFactory for ExamplePlugin {
2626
fn get_descriptor() -> PluginDescriptor {
2727
use clack_plugin::plugin::features::*;
2828

29-
PluginDescriptor::new("org.rust-audio.clack.gain-egui", "Clack Gain EGUI Example")
29+
PluginDescriptor::new("org.rust-audio.clack.gain-baseview", "Clack Gain Baseview Example")
3030
.with_features([AUDIO_EFFECT, STEREO])
3131
}
3232

examples/plugin_clack/src/window_handler.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ impl WindowHandler for OpenWindowExample {
114114
_ => {}
115115
}
116116

117-
log_event(&event);
118-
119117
EventStatus::Captured
120118
}
121119
}
@@ -136,12 +134,3 @@ impl OpenWindowExample {
136134
})
137135
}
138136
}
139-
140-
fn log_event(event: &Event) {
141-
match event {
142-
Event::Mouse(e) => println!("Mouse event: {:?}", e),
143-
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
144-
Event::Window(e) => println!("Window event: {:?}", e),
145-
_ => {}
146-
}
147-
}

src/platform/macos/window.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use dpi::LogicalSize;
1+
use dpi::{LogicalSize, Size};
22
use objc2::rc::{autoreleasepool, Retained, Weak};
33
use objc2::MainThreadMarker;
44
use objc2_app_kit::{NSApplication, NSPasteboard, NSPasteboardTypeString, NSView, NSWindow};
@@ -91,6 +91,24 @@ impl WindowHandle {
9191
pub fn is_open(&self) -> bool {
9292
self.state.closed.get()
9393
}
94+
95+
pub fn size(&self) -> WindowSize {
96+
WindowSize::from_logical(self.state.size.get(), self.state.scale_factor.get())
97+
}
98+
99+
pub fn resize(&self, size: Size) -> Result<()> {
100+
let Some(view) = self.view.load() else { return Ok(()) };
101+
let Some(view) = view.inner_ref() else { return Ok(()) };
102+
103+
BaseviewView::resize(view, size);
104+
105+
Ok(())
106+
}
107+
108+
pub fn suggest_scale_factor(&self, _scale_factor: f64) -> Result<()> {
109+
// This does not do anything on macOS: all coordinates are already logical
110+
Ok(())
111+
}
94112
}
95113

96114
fn create_window_with_options(

src/platform/win/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub type Result<T> = std::result::Result<T, Error>;
66
#[derive(Debug)]
77
pub enum Error {
88
Win32(windows_core::Error),
9+
ResizeFailed,
910
Handler(HandlerError),
1011
}
1112

@@ -26,6 +27,7 @@ impl Display for Error {
2627
match self {
2728
Error::Win32(e) => Display::fmt(e, f),
2829
Error::Handler(e) => Display::fmt(e, f),
30+
Error::ResizeFailed => f.write_str("Window resize request failed."),
2931
}
3032
}
3133
}
@@ -35,6 +37,7 @@ impl std::error::Error for Error {
3537
match self {
3638
Error::Win32(e) => Some(e),
3739
Error::Handler(e) => Some(e.source()),
40+
_ => None,
3841
}
3942
}
4043
}

0 commit comments

Comments
 (0)