diff options
Diffstat (limited to 'src/gui.rs')
| -rw-r--r-- | src/gui.rs | 92 |
1 files changed, 76 insertions, 16 deletions
@@ -4,26 +4,75 @@ use crate::{ RetainPluginMainThread, RetainPluginShared, params::RetainParams, window_size::WindowSize, window_type::WindowType, }; +use atomic_float::AtomicF32; use baseview::{Size, WindowHandle, WindowOpenOptions, WindowScalePolicy, gl::GlConfig}; use clack_extensions::gui::{GuiApiType, GuiConfiguration, GuiSize, PluginGuiImpl, Window}; use clack_plugin::prelude::*; use egui_baseview::{ EguiWindow, GraphicsConfig, Queue, - egui::{self, Align, ComboBox, Context, Layout, Slider}, + egui::{Align, CentralPanel, ComboBox, Context, Layout, Slider, Vec2, ViewportCommand}, }; -use std::sync::Arc; +use std::sync::{Arc, atomic::Ordering}; + +const DEFAULT_SIZE_X: f32 = 600.0; +const DEFAULT_SIZE_Y: f32 = 350.0; + +pub struct AtomicVec2 { + x: AtomicF32, + y: AtomicF32, +} + +impl Default for AtomicVec2 { + fn default() -> Self { + Self { + x: AtomicF32::new(DEFAULT_SIZE_X), + y: AtomicF32::new(DEFAULT_SIZE_Y), + } + } +} + +impl AtomicVec2 { + pub fn new() -> Self { + Self::default() + } + + pub fn get_x(&self) -> f32 { + self.x.load(Ordering::SeqCst) + } + + pub fn set_x(&self, x: f32) { + self.x.store(x, Ordering::SeqCst); + } + + pub fn get_y(&self) -> f32 { + self.y.load(Ordering::SeqCst) + } + + pub fn set_y(&self, x: f32) { + self.y.store(x, Ordering::SeqCst); + } + + pub fn as_vec2(&self) -> Vec2 { + Vec2 { + x: self.get_x(), + y: self.get_y(), + } + } +} /// The EGUI application state struct AppState { /// A handle to the shared params state params: Arc<RetainParams>, + gui_size: Arc<AtomicVec2>, } impl AppState { /// Initializes a new [`AppState`] from the given shared params state handle - pub fn new(params: &Arc<RetainParams>) -> Self { + pub fn new(params: &Arc<RetainParams>, gui_size: &Arc<AtomicVec2>) -> Self { Self { params: Arc::clone(params), + gui_size: Arc::clone(gui_size), } } } @@ -41,7 +90,10 @@ impl RetainPluginGui { pub fn new(parent: Window<'_>, plugin_state: &RetainPluginShared) -> Self { let settings = WindowOpenOptions { title: "Retain".to_string(), - size: Size::new(600.0, 350.0), + size: Size::new( + plugin_state.gui_size.get_x().into(), + plugin_state.gui_size.get_y().into(), + ), scale: WindowScalePolicy::SystemScaleFactor, gl_config: Some(GlConfig::default()), }; @@ -52,15 +104,15 @@ impl RetainPluginGui { &parent, settings, GraphicsConfig::default(), - AppState::new(&plugin_state.params), - move |egui_ctx: &Context, _queue: &mut Queue, _state: &mut AppState| { - tx.send(egui_ctx.clone()).unwrap(); + AppState::new(&plugin_state.params, &plugin_state.gui_size), + move |ctx: &Context, _queue: &mut Queue, _state: &mut AppState| { + tx.send(ctx.clone()).unwrap(); }, - |egui_ctx: &Context, _queue: &mut Queue, state: &mut AppState| { - egui::CentralPanel::default().show(egui_ctx, |ui| { - ui.with_layout(Layout::top_down(Align::Center), |ui| { - ui.heading("Retain"); - }); + |ctx: &Context, _queue: &mut Queue, state: &mut AppState| { + ctx.send_viewport_cmd(ViewportCommand::InnerSize(state.gui_size.as_vec2())); + + CentralPanel::default().show(ctx, |ui| { + ui.heading("Retain"); ui.horizontal(|ui| { ui.with_layout(Layout::top_down(Align::LEFT), |ui| { @@ -195,21 +247,29 @@ impl PluginGuiImpl for RetainPluginMainThread<'_> { fn get_size(&mut self) -> Option<GuiSize> { Some(GuiSize { - width: 600, - height: 350, + width: self.shared.gui_size.get_x() as u32, + height: self.shared.gui_size.get_y() as u32, }) } fn can_resize(&mut self) -> bool { - false + true } - fn set_size(&mut self, _size: GuiSize) -> Result<(), PluginError> { + fn set_size(&mut self, size: GuiSize) -> Result<(), PluginError> { + self.shared.gui_size.set_x(size.width as f32); + self.shared.gui_size.set_y(size.height as f32); + + if let Some(gui) = &self.gui { + gui.request_repaint(); + } + Ok(()) } fn set_parent(&mut self, window: Window) -> Result<(), PluginError> { self.gui = Some(RetainPluginGui::new(window, self.shared)); + Ok(()) } |
