Skip to content

Commit

Permalink
Cleanup settings code (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex authored Dec 30, 2023
1 parent 0ff5366 commit 39f6a84
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 74 deletions.
142 changes: 72 additions & 70 deletions neothesia/src/scene/menu_scene/iced_menu/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
output_manager::OutputDescriptor,
scene::menu_scene::{
icons,
layout::{BarLayout, Layout},
layout::{BarLayout, Layout, PushIf},
neo_btn::NeoBtn,
preferences_group,
},
Expand Down Expand Up @@ -77,89 +77,91 @@ pub(super) fn update(
Command::none()
}

pub(super) fn view<'a>(data: &'a Data, target: &Target) -> Element<'a, Message> {
let output_list = {
let outputs = &data.outputs;
let selected_output = data.selected_output.clone();

let is_synth = matches!(selected_output, Some(OutputDescriptor::Synth(_)));

let output_list = pick_list(outputs, selected_output, |v| {
fn output_group<'a>(data: &'a Data, target: &Target) -> Element<'a, Message> {
let output_settings = {
let output_list = pick_list(&data.outputs, data.selected_output.clone(), |v| {
SettingsMessage::SelectOutput(v).into()
})
.style(theme::pick_list());

let mut group = preferences_group::PreferencesGroup::new()
preferences_group::ActionRow::new()
.title("Output")
.push(
preferences_group::ActionRow::new()
.title("Output")
.suffix(output_list),
);
.suffix(output_list)
};

if is_synth {
let subtitle = target
.config
.soundfont_path
.as_ref()
.and_then(|path| path.file_name())
.map(|name| name.to_string_lossy().to_string());

let mut row = preferences_group::ActionRow::new()
.title("SourdFont")
.suffix(
iced_widget::button(centered_text("Select File"))
.style(theme::button())
.on_press(SettingsMessage::OpenSoundFontPicker.into()),
);

if let Some(subtitle) = subtitle {
row = row.subtitle(subtitle);
}
let is_synth = matches!(data.selected_output, Some(OutputDescriptor::Synth(_)));
let synth_settings = is_synth.then(|| {
let subtitle = target
.config
.soundfont_path
.as_ref()
.and_then(|path| path.file_name())
.map(|name| name.to_string_lossy().to_string());

let mut row = preferences_group::ActionRow::new()
.title("SourdFont")
.suffix(
iced_widget::button(centered_text("Select File"))
.style(theme::button())
.on_press(SettingsMessage::OpenSoundFontPicker.into()),
);

group = group.push(row);
if let Some(subtitle) = subtitle {
row = row.subtitle(subtitle);
}

group.build()
};
row
});

let input_list = {
let inputs = &data.inputs;
let selected_input = data.selected_input.clone();
preferences_group::PreferencesGroup::new()
.title("Output")
.push(output_settings)
.push_if(synth_settings)
.build()
}

let input_list = pick_list(inputs, selected_input, |v| {
SettingsMessage::SelectInput(v).into()
})
.style(theme::pick_list());
fn input_group<'a>(data: &'a Data, _target: &Target) -> Element<'a, Message> {
let inputs = &data.inputs;
let selected_input = data.selected_input.clone();

let input_list = pick_list(inputs, selected_input, |v| {
SettingsMessage::SelectInput(v).into()
})
.style(theme::pick_list());

preferences_group::PreferencesGroup::new()
.title("Input")
.push(
preferences_group::ActionRow::new()
.title("Input")
.suffix(input_list),
)
.build()
}

preferences_group::PreferencesGroup::new()
.title("Input")
.push(
preferences_group::ActionRow::new()
.title("Input")
.suffix(input_list),
)
.build()
};
fn guidelines_group<'a>(_data: &'a Data, target: &Target) -> Element<'a, Message> {
let toggler = toggler(None, target.config.vertical_guidelines, |v| {
SettingsMessage::VerticalGuidelines(v).into()
})
.style(theme::toggler());

preferences_group::PreferencesGroup::new()
.title("Render")
.push(
preferences_group::ActionRow::new()
.title("Vertical Guidelines")
.subtitle("Display octave indicators")
.suffix(toggler),
)
.build()
}

let guidelines = {
let toggler = toggler(None, target.config.vertical_guidelines, |v| {
SettingsMessage::VerticalGuidelines(v).into()
})
.style(theme::toggler());

preferences_group::PreferencesGroup::new()
.title("Render")
.push(
preferences_group::ActionRow::new()
.title("Vertical Guidelines")
.subtitle("Display octave indicators")
.suffix(toggler),
)
.build()
};
pub(super) fn view<'a>(data: &'a Data, target: &Target) -> Element<'a, Message> {
let output_group = output_group(data, target);
let input_group = input_group(data, target);
let guidelines_group = guidelines_group(data, target);

let column = col![output_list, input_list, guidelines]
let column = col![output_group, input_group, guidelines_group]
.spacing(10)
.width(Length::Fill)
.align_items(Alignment::Center);
Expand Down
16 changes: 14 additions & 2 deletions neothesia/src/scene/menu_scene/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub trait PushIf<'a, M, R> {
fn push_if(self, item: Option<impl Into<iced_core::Element<'a, M, R>>>) -> Self;
}

impl<'a, M, R: iced_core::Renderer> PushIf<'a, M, R> for iced_widget::Row<'a, M, R> {
impl<'a, M, R> PushIf<'a, M, R> for iced_widget::Row<'a, M, R> {
fn push_if(self, item: Option<impl Into<iced_core::Element<'a, M, R>>>) -> Self {
if let Some(item) = item {
self.push(item)
Expand All @@ -139,7 +139,7 @@ impl<'a, M, R: iced_core::Renderer> PushIf<'a, M, R> for iced_widget::Row<'a, M,
}
}

impl<'a, M, R: iced_core::Renderer> PushIf<'a, M, R> for iced_widget::Column<'a, M, R> {
impl<'a, M, R> PushIf<'a, M, R> for iced_widget::Column<'a, M, R> {
fn push_if(self, item: Option<impl Into<iced_core::Element<'a, M, R>>>) -> Self {
if let Some(item) = item {
self.push(item)
Expand All @@ -148,3 +148,15 @@ impl<'a, M, R: iced_core::Renderer> PushIf<'a, M, R> for iced_widget::Column<'a,
}
}
}

impl<'a, M: 'a> PushIf<'a, M, super::Renderer>
for super::preferences_group::PreferencesGroup<'a, M>
{
fn push_if(self, item: Option<impl Into<iced_core::Element<'a, M, super::Renderer>>>) -> Self {
if let Some(item) = item {
self.push(item)
} else {
self
}
}
}
16 changes: 14 additions & 2 deletions neothesia/src/scene/menu_scene/preferences_group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl<'a, MSG: 'a> PreferencesGroup<'a, MSG> {
self
}

pub fn push(mut self, item: ActionRow<'a, MSG>) -> Self {
self.items.push(item.build().into());
pub fn push(mut self, item: impl Into<Element<'a, MSG>>) -> Self {
self.items.push(item.into());
self
}

Expand Down Expand Up @@ -126,6 +126,12 @@ pub struct ActionRow<'a, MSG> {
suffix: Option<Element<'a, MSG>>,
}

impl<'a, MSG: 'a> Default for ActionRow<'a, MSG> {
fn default() -> Self {
Self::new()
}
}

impl<'a, MSG: 'a> ActionRow<'a, MSG> {
pub fn new() -> Self {
Self {
Expand Down Expand Up @@ -169,3 +175,9 @@ impl<'a, MSG: 'a> ActionRow<'a, MSG> {
.padding(15)
}
}

impl<'a, M: 'a> From<ActionRow<'a, M>> for Element<'a, M> {
fn from(val: ActionRow<'a, M>) -> Self {
val.build().into()
}
}

0 comments on commit 39f6a84

Please sign in to comment.