Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/open_parented/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct ParentWindowHandler {
surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>,
damaged: Cell<bool>,

_child_window: Option<WindowHandle>,
child_window: WindowHandle,
}

impl ParentWindowHandler {
Expand All @@ -27,11 +27,7 @@ impl ParentWindowHandler {

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

Ok(Self {
surface: surface.into(),
damaged: true.into(),
_child_window: Some(child_window),
})
Ok(Self { surface: surface.into(), damaged: true.into(), child_window })
}
}

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

let child_size =
LogicalSize::new(new_size.logical.width / 2., new_size.logical.height / 2.);
self.child_window.suggest_fallback_scale_factor(new_size.scale_factor)?;
self.child_window.resize(child_size.into())?;
Ok(())
}

Expand Down
73 changes: 60 additions & 13 deletions examples/plugin_clack/src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::window_handler::OpenWindowExample;
use crate::ExamplePluginMainThread;
use baseview::dpi::PhysicalSize;
use baseview::dpi::*;
use baseview::gl::GlConfig;
use baseview::{WindowHandle, WindowOpenOptions};
use baseview::{WindowHandle, WindowOpenOptions, WindowSize};
use clack_extensions::gui::{
GuiApiType, GuiConfiguration, GuiResizeHints, GuiSize, PluginGuiImpl, Window as ClapWindow,
AspectRatioStrategy, GuiApiType, GuiConfiguration, GuiResizeHints, GuiSize, PluginGuiImpl,
Window as ClapWindow,
};
use clack_plugin::plugin::PluginError;

Expand Down Expand Up @@ -39,30 +40,51 @@ impl PluginGuiImpl for ExamplePluginMainThread {
gui.handle.close()
}

fn set_scale(&mut self, _scale: f64) -> Result<(), PluginError> {
// Unsupported
fn set_scale(&mut self, scale: f64) -> Result<(), PluginError> {
let Some(gui) = &self.gui else {
return Err(PluginError::Message("set_scale called without a GUI active"));
};
gui.handle.suggest_fallback_scale_factor(scale)?;

Ok(())
}

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

fn can_resize(&mut self) -> bool {
false // Non-resizeable windows not supported yet
true // Non-resizeable windows not supported yet
}

fn get_resize_hints(&mut self) -> Option<GuiResizeHints> {
None // Not supported yet
Some(GuiResizeHints {
strategy: AspectRatioStrategy::Disregard, // Not supported

// Non-resizeable windows not supported yet
can_resize_vertically: true,
can_resize_horizontally: true,
})
}

fn adjust_size(&mut self, _size: GuiSize) -> Option<GuiSize> {
None // Not supported yet
fn adjust_size(&mut self, size: GuiSize) -> Option<GuiSize> {
Some(size) // Not supported yet
}

fn set_size(&mut self, _size: GuiSize) -> Result<(), PluginError> {
Ok(()) // Not supported yet
fn set_size(&mut self, size: GuiSize) -> Result<(), PluginError> {
let Some(gui) = &self.gui else {
return Err(PluginError::Message("set_size called without a GUI active"));
};

let size = gui_size_to_window_size(size);
gui.handle.resize(size)?;

Ok(())
}

#[allow(deprecated)]
Expand Down Expand Up @@ -98,3 +120,28 @@ impl PluginGuiImpl for ExamplePluginMainThread {
Ok(()) // Not supported yet
}
}

fn window_size_to_gui_size(size: WindowSize) -> GuiSize {
#[cfg(target_os = "macos")]
{
let size = size.logical.cast();
GuiSize { width: size.width, height: size.height }
}

#[cfg(not(target_os = "macos"))]
{
let size = size.physical.cast();
GuiSize { width: size.width, height: size.height }
}
}

fn gui_size_to_window_size(size: GuiSize) -> Size {
#[cfg(target_os = "macos")]
{
Size::Logical(LogicalSize::new(size.width, size.height).cast())
}
#[cfg(not(target_os = "macos"))]
{
Size::Physical(PhysicalSize::new(size.width, size.height))
}
}
2 changes: 1 addition & 1 deletion examples/plugin_clack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl DefaultPluginFactory for ExamplePlugin {
fn get_descriptor() -> PluginDescriptor {
use clack_plugin::plugin::features::*;

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

Expand Down
11 changes: 0 additions & 11 deletions examples/plugin_clack/src/window_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ impl WindowHandler for OpenWindowExample {
_ => {}
}

log_event(&event);

EventStatus::Captured
}
}
Expand All @@ -136,12 +134,3 @@ impl OpenWindowExample {
})
}
}

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),
_ => {}
}
}
20 changes: 19 additions & 1 deletion src/platform/macos/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dpi::LogicalSize;
use dpi::{LogicalSize, Size};
use objc2::rc::{autoreleasepool, Retained, Weak};
use objc2::MainThreadMarker;
use objc2_app_kit::{NSApplication, NSPasteboard, NSPasteboardTypeString, NSView, NSWindow};
Expand Down Expand Up @@ -91,6 +91,24 @@ impl WindowHandle {
pub fn is_open(&self) -> bool {
self.state.closed.get()
}

pub fn size(&self) -> WindowSize {
WindowSize::from_logical(self.state.size.get(), self.state.scale_factor.get())
}

pub fn resize(&self, size: Size) -> Result<()> {
let Some(view) = self.view.load() else { return Ok(()) };
let Some(view) = view.inner_ref() else { return Ok(()) };

BaseviewView::resize(view, size);

Ok(())
}

pub fn suggest_scale_factor(&self, _scale_factor: f64) -> Result<()> {
// This does not do anything on macOS: all coordinates are already logical
Ok(())
}
}

fn create_window_with_options(
Expand Down
3 changes: 3 additions & 0 deletions src/platform/win/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Win32(windows_core::Error),
ResizeFailed,
Handler(HandlerError),
}

Expand All @@ -26,6 +27,7 @@ impl Display for Error {
match self {
Error::Win32(e) => Display::fmt(e, f),
Error::Handler(e) => Display::fmt(e, f),
Error::ResizeFailed => f.write_str("Window resize request failed."),
}
}
}
Expand All @@ -35,6 +37,7 @@ impl std::error::Error for Error {
match self {
Error::Win32(e) => Some(e),
Error::Handler(e) => Some(e.source()),
_ => None,
}
}
}
Loading