From 82f1923399428f1bc988fffa36e1c16055778c0c Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Wed, 16 Mar 2022 01:27:38 +0100 Subject: [PATCH] Add scrollbar padding option to iced generic UI This makes the scroll bar look less cramped. --- Cargo.lock | 2 +- nih_plug_iced/src/widgets/generic_ui.rs | 48 +++++++++++++++---------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0b6fa85e..c2ec8e09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -614,7 +614,7 @@ name = "crisp" version = "0.1.0" dependencies = [ "nih_plug", - "nih_plug_egui", + "nih_plug_iced", ] [[package]] diff --git a/nih_plug_iced/src/widgets/generic_ui.rs b/nih_plug_iced/src/widgets/generic_ui.rs index 3e6d83f2..e7380ccd 100644 --- a/nih_plug_iced/src/widgets/generic_ui.rs +++ b/nih_plug_iced/src/widgets/generic_ui.rs @@ -2,7 +2,6 @@ //! list of sliders and labels. use atomic_refcell::AtomicRefCell; -use iced_baseview::Row; use std::borrow::Borrow; use std::collections::HashMap; use std::marker::PhantomData; @@ -16,7 +15,7 @@ use crate::backend::Renderer; use crate::text::Renderer as TextRenderer; use crate::{ alignment, event, layout, renderer, widget, Alignment, Clipboard, Element, Event, Layout, - Length, Point, Rectangle, Scrollable, Shell, Text, Widget, + Length, Point, Rectangle, Row, Scrollable, Shell, Space, Text, Widget, }; /// A widget that can be used to create a generic UI with. This is used in conjuction with empty @@ -69,6 +68,7 @@ pub struct GenericUi<'a, W: ParamWidget> { height: Length, max_width: u32, max_height: u32, + pad_scrollbar: bool, /// We don't emit any messages or store the actual widgets, but iced requires us to define some /// message type anyways. @@ -104,6 +104,7 @@ where height: Length::Fill, max_width: u32::MAX, max_height: u32::MAX, + pad_scrollbar: false, _phantom: PhantomData, } @@ -133,6 +134,12 @@ where self } + /// Include additional room on the right for the scroll bar. + pub fn pad_scrollbar(mut self) -> Self { + self.pad_scrollbar = true; + self + } + /// Create a temporary [`Scrollable`]. This needs to be created on demand because it needs to /// mutably borrow the `Scrollable`'s widget state. fn with_scrollable_widget( @@ -178,22 +185,27 @@ where unsafe { &mut *(widget_state.get_mut(¶m_ptr).unwrap() as *mut _) }; // Show the label next to the parameter for better use of the space - scrollable = scrollable.push( - Row::new() - .width(Length::Fill) - .align_items(Alignment::Center) - .spacing(spacing * 2) - .push( - Text::new(unsafe { param_ptr.name() }) - .height(20.into()) - .width(Length::Fill) - .horizontal_alignment(alignment::Horizontal::Right) - .vertical_alignment(alignment::Vertical::Center), - ) - .push(unsafe { - W::into_widget_element_raw(¶m_ptr, self.context, widget_state) - }), - ); + let mut row = Row::new() + .width(Length::Fill) + .align_items(Alignment::Center) + .spacing(spacing * 2) + .push( + Text::new(unsafe { param_ptr.name() }) + .height(20.into()) + .width(Length::Fill) + .horizontal_alignment(alignment::Horizontal::Right) + .vertical_alignment(alignment::Vertical::Center), + ) + .push(unsafe { + W::into_widget_element_raw(¶m_ptr, self.context, widget_state) + }); + if self.pad_scrollbar { + // There's already spacing applied, so this element doesn't actually need to hae any + // size of its own + row = row.push(Space::with_width(Length::Units(0))); + } + + scrollable = scrollable.push(row); } f(scrollable, renderer)