Skip to content

Commit

Permalink
#902 Show user presets categorized by namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
helgoboss committed Jan 24, 2024
1 parent 5b22f24 commit 5f501c4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 28 deletions.
1 change: 0 additions & 1 deletion api/src/persistence/glue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::hash::Hash;

#[derive(PartialEq, Default, Serialize, Deserialize)]
pub struct Glue {
Expand Down
1 change: 0 additions & 1 deletion dialogs/src/hidden_panel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::base::*;

pub fn create(context: ScopedContext, ids: &mut IdGenerator) -> Dialog {
use Style::*;
Dialog {
id: ids.named_id("ID_HIDDEN_PANEL"),
caption: "Hidden panel",
Expand Down
1 change: 1 addition & 0 deletions extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl HelgoboxExtension {
Ok(extension)
}

#[allow(dead_code)]
pub fn get() -> &'static HelgoboxExtension {
EXTENSION
.get()
Expand Down
2 changes: 1 addition & 1 deletion main/src/domain/backbone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use pot::{PotFavorites, PotFilterExcludes};
use fragile::Fragile;
use once_cell::sync::Lazy;
use realearn_api::persistence::TargetTouchCause;
use reaper_high::{Fx, Reaper};
use reaper_high::Fx;
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
Expand Down
1 change: 0 additions & 1 deletion main/src/domain/lua_support.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::anyhow;
use mlua::{ChunkMode, Function, Lua, Table, Value};
use std::error::Error;
use std::sync::Arc;
use std::time::{Duration, Instant};

#[derive(Debug)]
Expand Down
76 changes: 52 additions & 24 deletions main/src/infrastructure/ui/menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ use crate::domain::{
use crate::infrastructure::data::CommonPresetInfo;
use crate::infrastructure::plugin::{ActionSection, BackboneShell, ACTION_DEFS};
use crate::infrastructure::ui::Item;
use camino::Utf8Path;
use indexmap::IndexMap;
use itertools::Itertools;
use reaper_high::{FxChainContext, Reaper};
use std::collections::{BTreeMap, HashMap};
use std::fmt::{Display, Formatter};
use std::iter;
use strum::IntoEnumIterator;
use swell_ui::menu_tree::{item_with_opts, menu, root_menu, separator, Entry, ItemOpts, Menu};
Expand Down Expand Up @@ -333,30 +338,58 @@ pub fn menu_containing_compartment_presets(
)
}

#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, derive_more::Display)]
enum PresetCategory<'a> {
#[display(fmt = "Factory")]
Factory,
#[display(fmt = "User ({_0})")]
UserSorted(&'a str),
#[display(fmt = "User (Unsorted)")]
UserUnsorted,
}

pub fn build_compartment_preset_menu_entries<'a, T: 'static>(
preset_infos: impl Iterator<Item = &'a CommonPresetInfo> + 'a,
build_id: impl Fn(&CommonPresetInfo) -> T + 'a,
is_current_value: impl Fn(&CommonPresetInfo) -> bool + 'a,
) -> impl Iterator<Item = Entry<T>> + 'a {
let (user_preset_infos, factory_preset_infos): (Vec<_>, Vec<_>) =
preset_infos.partition(|info| info.origin.is_user());
[
("User presets", user_preset_infos),
("Factory presets", factory_preset_infos),
]
.into_iter()
.map(move |(label, mut infos)| {
infos.sort_by_key(|info| &info.meta_data.name);
menu(
label,
build_compartment_preset_menu_entries_internal(
infos.into_iter(),
&build_id,
&is_current_value,
let preset_infos: Vec<_> = preset_infos.collect();
let mut categorized_infos: IndexMap<PresetCategory, Vec<&CommonPresetInfo>> = IndexMap::new();
for info in preset_infos {
let path = Utf8Path::new(&info.id);
let category = if path.components().count() == 1 {
// Preset directly in user preset root = "Unsorted"
PresetCategory::UserUnsorted
} else {
// Preset in sub directory (good)
let component = path
.components()
.next()
.expect("preset ID should have at least one component")
.as_str();
if component == "factory" {
PresetCategory::Factory
} else {
PresetCategory::UserSorted(component)
}
};
categorized_infos.entry(category).or_default().push(info);
}
categorized_infos.sort_keys();
categorized_infos
.into_iter()
.map(move |(category, mut infos)| {
infos.sort_by_key(|info| &info.meta_data.name);
menu(
category.to_string(),
build_compartment_preset_menu_entries_internal(
infos.into_iter(),
&build_id,
&is_current_value,
)
.collect(),
)
.collect(),
)
})
})
}

fn build_compartment_preset_menu_entries_internal<'a, T: 'static>(
Expand All @@ -366,13 +399,8 @@ fn build_compartment_preset_menu_entries_internal<'a, T: 'static>(
) -> impl Iterator<Item = Entry<T>> + 'a {
preset_infos.map(move |info| {
let id = build_id(info);
let label = if info.meta_data.name == info.id {
info.meta_data.name.clone()
} else {
format!("{} ({})", info.meta_data.name, info.id)
};
item_with_opts(
label,
info.meta_data.name.clone(),
ItemOpts {
enabled: true,
checked: is_current_value(info),
Expand Down

0 comments on commit 5f501c4

Please sign in to comment.