|
1 | 1 | use crate::window_handler::OpenWindowExample; |
2 | 2 | use crate::ExamplePluginMainThread; |
3 | | -use baseview::dpi::PhysicalSize; |
| 3 | +use baseview::dpi::*; |
4 | 4 | use baseview::gl::GlConfig; |
5 | | -use baseview::{WindowHandle, WindowOpenOptions}; |
| 5 | +use baseview::{WindowHandle, WindowOpenOptions, WindowSize}; |
6 | 6 | use clack_extensions::gui::{ |
7 | | - GuiApiType, GuiConfiguration, GuiResizeHints, GuiSize, PluginGuiImpl, Window as ClapWindow, |
| 7 | + AspectRatioStrategy, GuiApiType, GuiConfiguration, GuiResizeHints, GuiSize, PluginGuiImpl, |
| 8 | + Window as ClapWindow, |
8 | 9 | }; |
9 | 10 | use clack_plugin::plugin::PluginError; |
10 | 11 |
|
@@ -39,30 +40,51 @@ impl PluginGuiImpl for ExamplePluginMainThread { |
39 | 40 | gui.handle.close() |
40 | 41 | } |
41 | 42 |
|
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 | + |
44 | 49 | Ok(()) |
45 | 50 | } |
46 | 51 |
|
47 | 52 | 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())) |
50 | 59 | } |
51 | 60 |
|
52 | 61 | fn can_resize(&mut self) -> bool { |
53 | | - false // Non-resizeable windows not supported yet |
| 62 | + true // Non-resizeable windows not supported yet |
54 | 63 | } |
55 | 64 |
|
56 | 65 | 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 | + }) |
58 | 73 | } |
59 | 74 |
|
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 |
62 | 77 | } |
63 | 78 |
|
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(()) |
66 | 88 | } |
67 | 89 |
|
68 | 90 | #[allow(deprecated)] |
@@ -98,3 +120,28 @@ impl PluginGuiImpl for ExamplePluginMainThread { |
98 | 120 | Ok(()) // Not supported yet |
99 | 121 | } |
100 | 122 | } |
| 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 | +} |
0 commit comments