Implement resizing for standalone editor window
This commit is contained in:
parent
da86d1c6b1
commit
8ba60eeab9
|
@ -1,3 +1,4 @@
|
||||||
|
use parking_lot::Mutex;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use super::wrapper::Wrapper;
|
use super::wrapper::Wrapper;
|
||||||
|
@ -11,6 +12,10 @@ use crate::plugin::Plugin;
|
||||||
/// with the host for things like setting parameters.
|
/// with the host for things like setting parameters.
|
||||||
pub(crate) struct WrapperGuiContext<P: Plugin> {
|
pub(crate) struct WrapperGuiContext<P: Plugin> {
|
||||||
pub(super) wrapper: Arc<Wrapper<P>>,
|
pub(super) wrapper: Arc<Wrapper<P>>,
|
||||||
|
|
||||||
|
/// If the widnow should be resized, then we will write the size here. This will be set on the
|
||||||
|
/// window at the start of the next frame.
|
||||||
|
pub(super) new_window_size: Arc<Mutex<Option<(u32, u32)>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A [`ProcessContext`] implementation for the standalone wrapper. This is a separate object so it
|
/// A [`ProcessContext`] implementation for the standalone wrapper. This is a separate object so it
|
||||||
|
@ -30,7 +35,11 @@ impl<P: Plugin> GuiContext for WrapperGuiContext<P> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn request_resize(&self) -> bool {
|
fn request_resize(&self) -> bool {
|
||||||
nih_debug_assert_failure!("TODO: WrapperGuiContext::request_resize()");
|
let new_size = self.wrapper.editor.as_ref().unwrap().size();
|
||||||
|
|
||||||
|
// This will cause the editor to be resized at the start of the next frame. If we need to do
|
||||||
|
// more of these things, then we should consider using a channel instead.
|
||||||
|
*self.new_window_size.lock() = Some(new_size);
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use baseview::{EventStatus, Window, WindowHandler, WindowOpenOptions};
|
use baseview::{EventStatus, Window, WindowHandler, WindowOpenOptions};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::{Mutex, RwLock};
|
||||||
use raw_window_handle::HasRawWindowHandle;
|
use raw_window_handle::HasRawWindowHandle;
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@ -40,7 +40,7 @@ pub struct Wrapper<P: Plugin> {
|
||||||
/// The plugin's editor, if it has one. This object does not do anything on its own, but we need
|
/// The plugin's editor, if it has one. This object does not do anything on its own, but we need
|
||||||
/// to instantiate this in advance so we don't need to lock the entire [`Plugin`] object when
|
/// to instantiate this in advance so we don't need to lock the entire [`Plugin`] object when
|
||||||
/// creating an editor.
|
/// creating an editor.
|
||||||
editor: Option<Arc<dyn Editor>>,
|
pub editor: Option<Arc<dyn Editor>>,
|
||||||
|
|
||||||
config: WrapperConfig,
|
config: WrapperConfig,
|
||||||
|
|
||||||
|
@ -58,16 +58,29 @@ pub enum WrapperError {
|
||||||
InitializationFailed,
|
InitializationFailed,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// We need a window handler for baseview, but since we only create a window to report the plugin in
|
|
||||||
/// this won't do much.
|
|
||||||
struct WrapperWindowHandler {
|
struct WrapperWindowHandler {
|
||||||
/// The editor handle for the plugin's open editor. The editor should clean itself up when it
|
/// The editor handle for the plugin's open editor. The editor should clean itself up when it
|
||||||
/// gets dropped.
|
/// gets dropped.
|
||||||
_editor_handle: Box<dyn Any>,
|
_editor_handle: Box<dyn Any>,
|
||||||
|
|
||||||
|
/// If contains a value, then the GUI will be resized at the start of the next frame. This is
|
||||||
|
/// set from [`WrapperGuiContext::request_resize()`].
|
||||||
|
new_window_size: Arc<Mutex<Option<(u32, u32)>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WindowHandler for WrapperWindowHandler {
|
impl WindowHandler for WrapperWindowHandler {
|
||||||
fn on_frame(&mut self, _window: &mut Window) {}
|
fn on_frame(&mut self, window: &mut Window) {
|
||||||
|
if let Some((new_width, new_height)) = self.new_window_size.lock().take() {
|
||||||
|
// Window resizing in baseview has only been implemented on Linux
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
{
|
||||||
|
window.resize(baseview::Size {
|
||||||
|
width: new_width as f64,
|
||||||
|
height: new_height as f64,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn on_event(&mut self, _window: &mut Window, _event: baseview::Event) -> EventStatus {
|
fn on_event(&mut self, _window: &mut Window, _event: baseview::Event) -> EventStatus {
|
||||||
EventStatus::Ignored
|
EventStatus::Ignored
|
||||||
|
@ -126,8 +139,11 @@ impl<P: Plugin> Wrapper<P> {
|
||||||
|
|
||||||
match self.editor.clone() {
|
match self.editor.clone() {
|
||||||
Some(editor) => {
|
Some(editor) => {
|
||||||
let context = self.clone().make_gui_context();
|
// We'll use this mutex to communicate window size changes. If we need to send a lot
|
||||||
let (width, height) = editor.size();
|
// more information to the window handler at some point, then consider replacing
|
||||||
|
// this with a channel.
|
||||||
|
let new_window_size = Arc::new(Mutex::new(None));
|
||||||
|
let context = self.clone().make_gui_context(new_window_size.clone());
|
||||||
|
|
||||||
// DPI scaling should not be used on macOS since the OS handles it there
|
// DPI scaling should not be used on macOS since the OS handles it there
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
|
@ -138,6 +154,7 @@ impl<P: Plugin> Wrapper<P> {
|
||||||
baseview::WindowScalePolicy::ScaleFactor(self.config.dpi_scale as f64)
|
baseview::WindowScalePolicy::ScaleFactor(self.config.dpi_scale as f64)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let (width, height) = editor.size();
|
||||||
Window::open_blocking(
|
Window::open_blocking(
|
||||||
WindowOpenOptions {
|
WindowOpenOptions {
|
||||||
title: String::from(P::NAME),
|
title: String::from(P::NAME),
|
||||||
|
@ -162,6 +179,7 @@ impl<P: Plugin> Wrapper<P> {
|
||||||
|
|
||||||
WrapperWindowHandler {
|
WrapperWindowHandler {
|
||||||
_editor_handle: editor_handle,
|
_editor_handle: editor_handle,
|
||||||
|
new_window_size,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -175,8 +193,14 @@ impl<P: Plugin> Wrapper<P> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_gui_context(self: Arc<Self>) -> Arc<WrapperGuiContext<P>> {
|
fn make_gui_context(
|
||||||
Arc::new(WrapperGuiContext { wrapper: self })
|
self: Arc<Self>,
|
||||||
|
new_window_size: Arc<Mutex<Option<(u32, u32)>>>,
|
||||||
|
) -> Arc<WrapperGuiContext<P>> {
|
||||||
|
Arc::new(WrapperGuiContext {
|
||||||
|
wrapper: self,
|
||||||
|
new_window_size,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_process_context(&self, transport: Transport) -> WrapperProcessContext<'_, P> {
|
fn make_process_context(&self, transport: Transport) -> WrapperProcessContext<'_, P> {
|
||||||
|
|
Loading…
Reference in a new issue