use atomic_refcell::AtomicRefCell;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;
use nih_plug::prelude::{Param, ParamFlags, ParamPtr, Params};
use super::{ParamMessage, ParamSlider};
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, Row, Scrollable, Shell, Space, Text, Widget,
};
pub trait ParamWidget {
type State: Default;
fn into_widget_element<'a, P: Param>(
param: &'a P,
state: &'a mut Self::State,
) -> Element<'a, ParamMessage>;
unsafe fn into_widget_element_raw<'a>(
param: &ParamPtr,
state: &'a mut Self::State,
) -> Element<'a, ParamMessage> {
match param {
ParamPtr::FloatParam(p) => Self::into_widget_element(&**p, state),
ParamPtr::IntParam(p) => Self::into_widget_element(&**p, state),
ParamPtr::BoolParam(p) => Self::into_widget_element(&**p, state),
ParamPtr::EnumParam(p) => Self::into_widget_element(&**p, state),
}
}
}
#[derive(Default)]
pub struct GenericSlider;
pub struct GenericUi<'a, W: ParamWidget> {
state: &'a mut State<W>,
params: Arc<dyn Params>,
width: Length,
height: Length,
max_width: u32,
max_height: u32,
pad_scrollbar: bool,
_phantom: PhantomData<W>,
}
#[derive(Debug, Default)]
pub struct State<W: ParamWidget> {
scrollable_state: AtomicRefCell<widget::scrollable::State>,
widget_state: AtomicRefCell<HashMap<ParamPtr, W::State>>,
}
impl<'a, W> GenericUi<'a, W>
where
W: ParamWidget,
{
pub fn new(state: &'a mut State<W>, params: Arc<dyn Params>) -> Self {
Self {
state,
params,
width: Length::Fill,
height: Length::Fill,
max_width: u32::MAX,
max_height: u32::MAX,
pad_scrollbar: false,
_phantom: PhantomData,
}
}
pub fn width(mut self, width: Length) -> Self {
self.width = width;
self
}
pub fn height(mut self, height: Length) -> Self {
self.height = height;
self
}
pub fn max_width(mut self, width: u32) -> Self {
self.max_width = width;
self
}
pub fn max_height(mut self, height: u32) -> Self {
self.max_height = height;
self
}
pub fn pad_scrollbar(mut self) -> Self {
self.pad_scrollbar = true;
self
}
fn with_scrollable_widget<T, R, F>(
&'a self,
scrollable_state: &'a mut widget::scrollable::State,
widget_state: &'a mut HashMap<ParamPtr, W::State>,
renderer: R,
f: F,
) -> T
where
F: FnOnce(Scrollable<'a, ParamMessage>, R) -> T,
R: Borrow<Renderer>,
{
let text_size = renderer.borrow().default_size();
let spacing = (text_size as f32 * 0.2).round() as u16;
let padding = (text_size as f32 * 0.5).round() as u16;
let mut scrollable = Scrollable::new(scrollable_state)
.width(self.width)
.height(self.height)
.max_width(self.max_width)
.max_height(self.max_height)
.spacing(spacing)
.padding(padding)
.align_items(Alignment::Center);
let param_map = self.params.param_map();
for (_, param_ptr, _) in ¶m_map {
let flags = unsafe { param_ptr.flags() };
if flags.contains(ParamFlags::HIDE_IN_GENERIC_UI) {
continue;
}
if !widget_state.contains_key(param_ptr) {
widget_state.insert(*param_ptr, Default::default());
}
}
for (_, param_ptr, _) in param_map {
let flags = unsafe { param_ptr.flags() };
if flags.contains(ParamFlags::HIDE_IN_GENERIC_UI) {
continue;
}
let widget_state: &'a mut W::State =
unsafe { &mut *(widget_state.get_mut(¶m_ptr).unwrap() as *mut _) };
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, widget_state) });
if self.pad_scrollbar {
row = row.push(Space::with_width(Length::Units(0)));
}
scrollable = scrollable.push(row);
}
f(scrollable, renderer)
}
}
impl<'a, W> Widget<ParamMessage, Renderer> for GenericUi<'a, W>
where
W: ParamWidget,
{
fn width(&self) -> Length {
self.width
}
fn height(&self) -> Length {
self.height
}
fn layout(&self, renderer: &Renderer, limits: &layout::Limits) -> layout::Node {
let mut scrollable_state = self.state.scrollable_state.borrow_mut();
let mut widget_state = self.state.widget_state.borrow_mut();
self.with_scrollable_widget(
&mut scrollable_state,
&mut widget_state,
renderer,
|scrollable, _| scrollable.layout(renderer, limits),
)
}
fn draw(
&self,
renderer: &mut Renderer,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
viewport: &Rectangle,
) {
let mut scrollable_state = self.state.scrollable_state.borrow_mut();
let mut widget_state = self.state.widget_state.borrow_mut();
self.with_scrollable_widget(
&mut scrollable_state,
&mut widget_state,
renderer,
|scrollable, renderer| {
scrollable.draw(renderer, style, layout, cursor_position, viewport)
},
)
}
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, ParamMessage>,
) -> event::Status {
let mut scrollable_state = self.state.scrollable_state.borrow_mut();
let mut widget_state = self.state.widget_state.borrow_mut();
self.with_scrollable_widget(
&mut scrollable_state,
&mut widget_state,
renderer,
|mut scrollable, _| {
scrollable.on_event(event, layout, cursor_position, renderer, clipboard, shell)
},
)
}
}
impl ParamWidget for GenericSlider {
type State = super::param_slider::State;
fn into_widget_element<'a, P: Param>(
param: &'a P,
state: &'a mut Self::State,
) -> Element<'a, ParamMessage> {
ParamSlider::new(state, param).into()
}
}
impl<'a, W: ParamWidget> GenericUi<'a, W> {
pub fn map<Message, F>(self, f: F) -> Element<'a, Message>
where
Message: 'static,
F: Fn(ParamMessage) -> Message + 'static,
{
Element::from(self).map(f)
}
}
impl<'a, W> From<GenericUi<'a, W>> for Element<'a, ParamMessage>
where
W: ParamWidget,
{
fn from(widget: GenericUi<'a, W>) -> Self {
Element::new(widget)
}
}