diff --git a/architecture.md b/architecture.md index ff7073f..77ccd25 100644 --- a/architecture.md +++ b/architecture.md @@ -502,9 +502,15 @@ configuration, and global controls. Hiding is deliberately assigned to Shift+H so the common help key does not perform a destructive presentation edit. The selected main-list entry can be moved directly, and separators can be -added, renamed, moved, or removed. Adding or renaming a separator uses a -centered naming dialog, keeping transient text entry out of the mixer status -line. Editing a fader presents domain fields rather than a serialized command: +added, renamed, moved, or removed. A centered separator dialog edits its name, +size, and decoration. Separator size is independent of fader density: +`slim`, `standard` (default), `thick`, or `fill`. Size controls height in the +stacked layout and width in the side-by-side layout. Decorations are `plain`, +`line` (default), `double`, or `box`. A slim stacked separator occupies one +row, or three for a box. Viewports fit entries using their minimum extents +before distributing remaining space evenly among visible Fill separators; +expansion never evicts a fader or wraps the horizontal row. Editing a fader +presents domain fields rather than a serialized command: - **Capture:** application selector field, exact/regex mode, and pattern. Pattern values open in a cursor-aware modal with insertion, Backspace, Delete, diff --git a/config/src/document.rs b/config/src/document.rs index 390684f..2d67d38 100644 --- a/config/src/document.rs +++ b/config/src/document.rs @@ -4,7 +4,7 @@ use toml_edit::{Array, ArrayOfTables, DocumentMut, InlineTable, Item, Table, Val use crate::{ Action, ConfigError, FaderDensity, MessageKind, MixerOrientation, Profile, SelectorField, - StatusDisplayMode, TargetKind, UiTheme, + SeparatorDecoration, SeparatorSize, StatusDisplayMode, TargetKind, UiTheme, }; /// Match expression used when creating an application selector. @@ -448,6 +448,37 @@ pickup_tolerance = 0.02 }) } + /// Updates separator spacing and decoration without changing its order. + pub fn set_separator_presentation( + &mut self, + id: &str, + size: SeparatorSize, + decoration: SeparatorDecoration, + ) -> Result<(), ConfigError> { + self.transaction(|document| { + let table = find_table_mut(document, "mixer_layout", id)?; + if table.get("kind").and_then(Item::as_str) != Some("separator") { + return Err(ConfigError::validation( + "mixer_layout", + "expected separator", + )); + } + table["size"] = value(match size { + SeparatorSize::Slim => "slim", + SeparatorSize::Standard => "standard", + SeparatorSize::Thick => "thick", + SeparatorSize::Fill => "fill", + }); + table["decoration"] = value(match decoration { + SeparatorDecoration::Plain => "plain", + SeparatorDecoration::Line => "line", + SeparatorDecoration::Double => "double", + SeparatorDecoration::Box => "box", + }); + Ok(()) + }) + } + /// Removes only the selected presentation separator. pub fn remove_separator(&mut self, id: &str) -> Result<(), ConfigError> { self.transaction(|document| { diff --git a/config/src/lib.rs b/config/src/lib.rs index e8c31c5..1996ec9 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -29,7 +29,8 @@ pub use paths::{XdgEnvironment, XdgPaths}; pub use profile::{ Action, Adapter, Application, Audio, Binding, Fader, FaderDensity, Group, MessageKind, MixerLayoutEntry, MixerOrientation, NewStreamPolicy, Profile, Selector, SelectorField, - StartupPolicy, StatusDisplayMode, Surface, TargetKind, UiTheme, + SeparatorDecoration, SeparatorSize, StartupPolicy, StatusDisplayMode, Surface, TargetKind, + UiTheme, }; pub use state::{ CachedObservation, DesiredWrite, PersistedState, ProfileState, StartupState, UiView, diff --git a/config/src/profile.rs b/config/src/profile.rs index b6062fa..f46981b 100644 --- a/config/src/profile.rs +++ b/config/src/profile.rs @@ -245,6 +245,28 @@ pub enum FaderDensity { Thick, } +/// Space reserved for a separator along the mixer layout axis. +#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum SeparatorSize { + Slim, + #[default] + Standard, + Thick, + Fill, +} + +/// Independent decoration for a separator. +#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum SeparatorDecoration { + Plain, + #[default] + Line, + Double, + Box, +} + /// One durable entry in the user-arranged mixer presentation. #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)] @@ -258,7 +280,14 @@ pub enum MixerLayoutEntry { density: FaderDensity, }, /// A presentation-only named divider. - Separator { id: String, label: String }, + Separator { + id: String, + label: String, + #[serde(default)] + size: SeparatorSize, + #[serde(default)] + decoration: SeparatorDecoration, + }, } fn is_standard_density(density: &FaderDensity) -> bool { @@ -895,7 +924,7 @@ fn validate_mixer_layout( } (fader, true) } - MixerLayoutEntry::Separator { id, label } => { + MixerLayoutEntry::Separator { id, label, .. } => { parse_id::(id, &format!("mixer_layout[{index}].id"))?; if label.trim().is_empty() { return Err(ConfigError::validation( diff --git a/config/tests/editor.rs b/config/tests/editor.rs index 4ecadda..338ba86 100644 --- a/config/tests/editor.rs +++ b/config/tests/editor.rs @@ -5,6 +5,29 @@ use fader_config::{ const PROFILE: &str = include_str!("../../examples/profiles/desktop.toml"); +#[test] +fn separator_presentation_round_trips_without_changing_order() { + use fader_config::{SeparatorDecoration, SeparatorSize}; + let mut draft = ProfileDocument::parse(PROFILE).unwrap(); + draft.add_separator("heading", "Heading", 0).unwrap(); + draft + .set_separator_presentation("heading", SeparatorSize::Fill, SeparatorDecoration::Double) + .unwrap(); + let draft = ProfileDocument::parse(&draft.finish().unwrap()).unwrap(); + assert!(matches!( + &draft.preview().unwrap().mixer_layout()[0], + MixerLayoutEntry::Separator { + size: SeparatorSize::Fill, + decoration: SeparatorDecoration::Double, + .. + } + )); + assert!( + fader_config::Profile::parse(&draft.source().replace("size = \"fill\"", "size = \"huge\"")) + .is_err() + ); +} + #[test] fn draft_adds_a_discovered_midi_surface_with_durable_identity() { let mut draft = ProfileDocument::empty("default").unwrap(); @@ -271,6 +294,8 @@ fn mixer_layout_draft_reorders_separators_and_retains_hidden_faders() { MixerLayoutEntry::Separator { id: "outputs".to_owned(), label: "Main outputs".to_owned(), + size: fader_config::SeparatorSize::Standard, + decoration: fader_config::SeparatorDecoration::Line, }, ] ); diff --git a/docs/configuration.md b/docs/configuration.md index b916ef5..2c01eb4 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -166,6 +166,8 @@ for review and bulk editing: kind = "separator" id = "applications" label = "Applications" +size = "slim" +decoration = "double" [[mixer_layout]] kind = "fader" @@ -178,6 +180,20 @@ Fader density is `slim`, `bordered_slim`, `standard`, or `thick`. Slim uses one terminal row; Bordered slim adds a border around one content row. Hidden faders retain their place and routing and can be restored from the Config tab. +Select a separator and press `E` to edit its name, size, and decoration in a +popup (`A` opens the same controls for a new separator). Use Up/Down or Tab to +choose a field, Left/Right to change its options, Enter to accept, and Escape +to cancel. Config → Save persists the draft. + +Separator `size` is `slim`, `standard`, `thick`, or `fill`; `decoration` +is `plain` (label only), `line`, `double`, or `box`. Existing separators +default to Standard with Line decoration. Size controls height in Vertical +mode and width in Horizontal mode. Slim uses one row in Vertical mode (three +with Box); Standard uses three and Thick uses five. Horizontal Slim uses three +columns, Standard fits the label within 8–18 columns, and Thick uses 22. +Fill shares unused viewport space with other Fill separators after faders +have been fitted, so it does not force faders off-screen or cause wrapping. + ### MIDI surfaces and bindings Use `fader_master devices` to inspect available ports and durable USB identity diff --git a/fader_master/src/editor.rs b/fader_master/src/editor.rs index 21c1a6b..747830a 100644 --- a/fader_master/src/editor.rs +++ b/fader_master/src/editor.rs @@ -289,6 +289,37 @@ impl ConfigurationEditor { self.document.set_separator_label(&id, &label)? } ConfigurationEdit::RemoveSeparator { id } => self.document.remove_separator(&id)?, + ConfigurationEdit::SetSeparatorPresentation { + id, + size, + decoration, + } => { + self.document.set_separator_presentation( + &id, + match size { + crate::ipc::SeparatorSize::Slim => fader_config::SeparatorSize::Slim, + crate::ipc::SeparatorSize::Standard => { + fader_config::SeparatorSize::Standard + } + crate::ipc::SeparatorSize::Thick => fader_config::SeparatorSize::Thick, + crate::ipc::SeparatorSize::Fill => fader_config::SeparatorSize::Fill, + }, + match decoration { + crate::ipc::SeparatorDecoration::Plain => { + fader_config::SeparatorDecoration::Plain + } + crate::ipc::SeparatorDecoration::Line => { + fader_config::SeparatorDecoration::Line + } + crate::ipc::SeparatorDecoration::Double => { + fader_config::SeparatorDecoration::Double + } + crate::ipc::SeparatorDecoration::Box => { + fader_config::SeparatorDecoration::Box + } + }, + )?; + } } self.revision = self.revision.saturating_add(1); self.dirty = true; @@ -456,6 +487,46 @@ mod tests { assert_eq!(editor.profile().unwrap().faders().len(), 2); } + #[test] + fn separator_presentation_is_saved_and_published_after_reload() { + let root = tempdir().unwrap(); + let path = root.path().join("desktop.toml"); + fs::write(&path, PROFILE).unwrap(); + let mut editor = + ConfigurationEditor::new(ProfileDocument::parse(PROFILE).unwrap(), path.clone()); + editor + .apply( + 0, + ConfigurationEdit::AddSeparator { + id: "heading".into(), + label: "Heading".into(), + position: 0, + }, + ) + .unwrap(); + editor + .apply( + 1, + ConfigurationEdit::SetSeparatorPresentation { + id: "heading".into(), + size: crate::ipc::SeparatorSize::Fill, + decoration: crate::ipc::SeparatorDecoration::Box, + }, + ) + .unwrap(); + editor.save(2).unwrap(); + let profile = fader_config::Profile::parse(&fs::read_to_string(path).unwrap()).unwrap(); + let snapshot = crate::live::configuration_snapshot(&profile, 0, false, 0); + assert!(matches!( + &snapshot.mixer_layout[0], + crate::ipc::MixerLayoutEntry::Separator { + size: crate::ipc::SeparatorSize::Fill, + decoration: crate::ipc::SeparatorDecoration::Box, + .. + } + )); + } + #[test] fn layout_edits_are_revisioned_without_changing_fader_routing() { let root = tempdir().unwrap(); diff --git a/fader_master/src/ipc.rs b/fader_master/src/ipc.rs index afd0765..e18852f 100644 --- a/fader_master/src/ipc.rs +++ b/fader_master/src/ipc.rs @@ -3,7 +3,7 @@ use std::io::{self, Read, Write}; use serde::{Deserialize, Serialize, de::DeserializeOwned}; use thiserror::Error; -pub const PROTOCOL_VERSION: u16 = 18; +pub const PROTOCOL_VERSION: u16 = 19; pub const MAX_FRAME_BYTES: usize = 64 * 1024; /// Kind of mixer object addressed by a client. @@ -190,6 +190,11 @@ pub enum ConfigurationEdit { id: String, label: String, }, + SetSeparatorPresentation { + id: String, + size: SeparatorSize, + decoration: SeparatorDecoration, + }, RemoveSeparator { id: String, }, @@ -273,9 +278,33 @@ pub enum MixerLayoutEntry { Separator { id: String, label: String, + size: SeparatorSize, + decoration: SeparatorDecoration, }, } +/// Space reserved for a separator along the mixer layout axis. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "snake_case")] +pub enum SeparatorSize { + Slim, + #[default] + Standard, + Thick, + Fill, +} + +/// Independent separator decoration. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "snake_case")] +pub enum SeparatorDecoration { + Plain, + #[default] + Line, + Double, + Box, +} + #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(rename_all = "snake_case")] pub enum FaderDensity { diff --git a/fader_master/src/live.rs b/fader_master/src/live.rs index 75016fb..3fc3f66 100644 --- a/fader_master/src/live.rs +++ b/fader_master/src/live.rs @@ -209,12 +209,37 @@ pub(crate) fn configuration_snapshot( fader_config::FaderDensity::Thick => crate::ipc::FaderDensity::Thick, }, }, - fader_config::MixerLayoutEntry::Separator { id, label } => { - IpcLayoutEntry::Separator { - id: id.clone(), - label: label.clone(), - } - } + fader_config::MixerLayoutEntry::Separator { + id, + label, + size, + decoration, + } => IpcLayoutEntry::Separator { + id: id.clone(), + label: label.clone(), + size: match size { + fader_config::SeparatorSize::Slim => crate::ipc::SeparatorSize::Slim, + fader_config::SeparatorSize::Standard => { + crate::ipc::SeparatorSize::Standard + } + fader_config::SeparatorSize::Thick => crate::ipc::SeparatorSize::Thick, + fader_config::SeparatorSize::Fill => crate::ipc::SeparatorSize::Fill, + }, + decoration: match decoration { + fader_config::SeparatorDecoration::Plain => { + crate::ipc::SeparatorDecoration::Plain + } + fader_config::SeparatorDecoration::Line => { + crate::ipc::SeparatorDecoration::Line + } + fader_config::SeparatorDecoration::Double => { + crate::ipc::SeparatorDecoration::Double + } + fader_config::SeparatorDecoration::Box => { + crate::ipc::SeparatorDecoration::Box + } + }, + }, }) .collect(), bindings: profile diff --git a/fader_master/src/tui_client.rs b/fader_master/src/tui_client.rs index 7578626..2ee3391 100644 --- a/fader_master/src/tui_client.rs +++ b/fader_master/src/tui_client.rs @@ -211,9 +211,37 @@ impl IpcMixerClient { crate::ipc::FaderDensity::Thick => tui::FaderDensity::Thick, }, }, - crate::ipc::MixerLayoutEntry::Separator { id, label } => { - tui::MixerLayoutEntry::Separator { id, label } - } + crate::ipc::MixerLayoutEntry::Separator { + id, + label, + size, + decoration, + } => tui::MixerLayoutEntry::Separator { + id, + label, + size: match size { + crate::ipc::SeparatorSize::Slim => tui::SeparatorSize::Slim, + crate::ipc::SeparatorSize::Standard => { + tui::SeparatorSize::Standard + } + crate::ipc::SeparatorSize::Thick => tui::SeparatorSize::Thick, + crate::ipc::SeparatorSize::Fill => tui::SeparatorSize::Fill, + }, + decoration: match decoration { + crate::ipc::SeparatorDecoration::Plain => { + tui::SeparatorDecoration::Plain + } + crate::ipc::SeparatorDecoration::Line => { + tui::SeparatorDecoration::Line + } + crate::ipc::SeparatorDecoration::Double => { + tui::SeparatorDecoration::Double + } + crate::ipc::SeparatorDecoration::Box => { + tui::SeparatorDecoration::Box + } + }, + }, }) .collect(), bindings: snapshot @@ -453,6 +481,25 @@ fn configuration_edit(edit: TuiConfigurationEdit) -> crate::ipc::ConfigurationEd IpcEdit::SetSeparatorLabel { id, label } } TuiConfigurationEdit::RemoveSeparator { id } => IpcEdit::RemoveSeparator { id }, + TuiConfigurationEdit::SetSeparatorPresentation { + id, + size, + decoration, + } => IpcEdit::SetSeparatorPresentation { + id, + size: match size { + tui::SeparatorSize::Slim => crate::ipc::SeparatorSize::Slim, + tui::SeparatorSize::Standard => crate::ipc::SeparatorSize::Standard, + tui::SeparatorSize::Thick => crate::ipc::SeparatorSize::Thick, + tui::SeparatorSize::Fill => crate::ipc::SeparatorSize::Fill, + }, + decoration: match decoration { + tui::SeparatorDecoration::Plain => crate::ipc::SeparatorDecoration::Plain, + tui::SeparatorDecoration::Line => crate::ipc::SeparatorDecoration::Line, + tui::SeparatorDecoration::Double => crate::ipc::SeparatorDecoration::Double, + tui::SeparatorDecoration::Box => crate::ipc::SeparatorDecoration::Box, + }, + }, TuiConfigurationEdit::SetApplicationSelector { id, index, diff --git a/task.md b/task.md index 4b6cdee..b76ae06 100644 --- a/task.md +++ b/task.md @@ -405,6 +405,11 @@ Dependencies: M8, M2, M1. optimistic TUI swap to snap back. Regression tests cover repeated file reloads, both move directions, separator insertion, hidden entries and comments, plus IPC edit/refresh/Save and reordering after server restart. + Separators now have independent Slim/Standard/Thick/Fill sizes and + Plain/Line/Double line/Box decorations in their popup. Fill shares remaining + viewport space after fitting the other entries. Profile round trips, + server save/snapshot, popup accept/cancel, both orientations, and tiny + viewports are covered by regression tests. - [x] **M9.5 Guided fader editor:** replace pipe-delimited table commands with a selected-fader form. Application faders edit capture field/mode/pattern; groups select eligible application faders and show the absolute group mode; diff --git a/tui/examples/doc_screenshots.rs b/tui/examples/doc_screenshots.rs index 71f238f..fa308ed 100644 --- a/tui/examples/doc_screenshots.rs +++ b/tui/examples/doc_screenshots.rs @@ -170,6 +170,8 @@ fn snapshot(mixer_orientation: MixerOrientation) -> MixerSnapshot { MixerLayoutEntry::Separator { id: "applications".to_owned(), label: "Applications".to_owned(), + size: tui::SeparatorSize::Standard, + decoration: tui::SeparatorDecoration::Line, }, layout_fader( "music", @@ -182,11 +184,15 @@ fn snapshot(mixer_orientation: MixerOrientation) -> MixerSnapshot { MixerLayoutEntry::Separator { id: "groups".to_owned(), label: "Groups".to_owned(), + size: tui::SeparatorSize::Standard, + decoration: tui::SeparatorDecoration::Line, }, layout_fader("media", FaderDensity::Thick), MixerLayoutEntry::Separator { id: "outputs".to_owned(), label: "Outputs".to_owned(), + size: tui::SeparatorSize::Standard, + decoration: tui::SeparatorDecoration::Line, }, layout_fader( "default", diff --git a/tui/src/lib.rs b/tui/src/lib.rs index ce8df90..87c7aa7 100644 --- a/tui/src/lib.rs +++ b/tui/src/lib.rs @@ -184,9 +184,77 @@ pub enum MixerLayoutEntry { Separator { id: String, label: String, + size: SeparatorSize, + decoration: SeparatorDecoration, }, } +/// Space reserved for a separator along the mixer layout axis. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub enum SeparatorSize { + Slim, + #[default] + Standard, + Thick, + Fill, +} + +/// Independent separator decoration. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub enum SeparatorDecoration { + Plain, + #[default] + Line, + Double, + Box, +} +impl SeparatorSize { + const fn label(self) -> &'static str { + match self { + Self::Slim => "Slim", + Self::Standard => "Standard", + Self::Thick => "Thick", + Self::Fill => "Fill", + } + } + + const fn cycle(self, forward: bool) -> Self { + match (self, forward) { + (Self::Slim, true) => Self::Standard, + (Self::Slim, false) => Self::Fill, + (Self::Standard, true) => Self::Thick, + (Self::Standard, false) => Self::Slim, + (Self::Thick, true) => Self::Fill, + (Self::Thick, false) => Self::Standard, + (Self::Fill, true) => Self::Slim, + (Self::Fill, false) => Self::Thick, + } + } +} +impl SeparatorDecoration { + const fn label(self) -> &'static str { + match self { + Self::Plain => "Plain", + Self::Line => "Line", + Self::Double => "Double line", + Self::Box => "Box", + } + } + + const fn cycle(self, forward: bool) -> Self { + match (self, forward) { + (Self::Plain, true) => Self::Line, + (Self::Plain, false) => Self::Box, + (Self::Line, true) => Self::Double, + (Self::Line, false) => Self::Plain, + (Self::Double, true) => Self::Box, + (Self::Double, false) => Self::Line, + (Self::Box, true) => Self::Plain, + (Self::Box, false) => Self::Double, + } + } +} + #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub enum FaderDensity { Slim, @@ -343,6 +411,11 @@ pub enum ConfigurationEdit { id: String, label: String, }, + SetSeparatorPresentation { + id: String, + size: SeparatorSize, + decoration: SeparatorDecoration, + }, RemoveSeparator { id: String, }, @@ -965,10 +1038,13 @@ enum Tab { Configuration, } -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Default, Eq, PartialEq)] struct EditorInput { value: String, target: Option, + size: SeparatorSize, + decoration: SeparatorDecoration, + selected: usize, } enum SettingsRow { @@ -1089,6 +1165,8 @@ enum MixerRow<'a> { Separator { id: &'a str, label: &'a str, + size: SeparatorSize, + decoration: SeparatorDecoration, }, } @@ -1117,8 +1195,20 @@ impl MixerRow<'_> { Self::Fader { density: FaderDensity::Standard, .. - } - | Self::Separator { .. } => 4, + } => 4, + Self::Separator { + size, decoration, .. + } => match size { + SeparatorSize::Slim | SeparatorSize::Fill => { + if matches!(decoration, SeparatorDecoration::Box) { + 3 + } else { + 1 + } + } + SeparatorSize::Standard => 3, + SeparatorSize::Thick => 5, + }, Self::Fader { density: FaderDensity::Thick, .. @@ -1127,6 +1217,86 @@ impl MixerRow<'_> { } } +/// Select a viewport using minimum extents, then share leftover cells between +/// Fill separators. Expanding a spacer must never push a fader out of view. +fn mixer_viewport( + rows: &[MixerRow<'_>], + selected: usize, + available: u16, + horizontal: bool, +) -> (usize, Vec) { + if rows.is_empty() || available == 0 { + return (0, Vec::new()); + } + let extent = |row: &MixerRow<'_>| { + if !horizontal { + return row.height(); + } + match row { + MixerRow::Fader { density, .. } => match density { + FaderDensity::Slim => 10, + FaderDensity::BorderedSlim => 13, + FaderDensity::Standard => 16, + FaderDensity::Thick => 22, + }, + MixerRow::Separator { label, size, .. } => match size { + SeparatorSize::Slim | SeparatorSize::Fill => 3, + SeparatorSize::Standard => u16::try_from(label.chars().count()) + .unwrap_or(u16::MAX) + .saturating_add(2) + .clamp(8, 18), + SeparatorSize::Thick => 22, + }, + } + }; + let selected = selected.min(rows.len() - 1); + let mut start = selected; + let mut used = extent(&rows[selected]).min(available); + while start > 0 && extent(&rows[start - 1]) <= available.saturating_sub(used) { + start -= 1; + used += extent(&rows[start]); + } + let mut end = selected + 1; + while end < rows.len() && extent(&rows[end]) <= available.saturating_sub(used) { + used += extent(&rows[end]); + end += 1; + } + let mut fills = rows[start..end] + .iter() + .filter(|row| { + matches!( + row, + MixerRow::Separator { + size: SeparatorSize::Fill, + .. + } + ) + }) + .count(); + let mut spare = available - used; + let extents = rows[start..end] + .iter() + .map(|row| { + let base = extent(row).min(available); + if matches!( + row, + MixerRow::Separator { + size: SeparatorSize::Fill, + .. + } + ) { + let extra = spare / u16::try_from(fills).unwrap_or(u16::MAX); + spare -= extra; + fills -= 1; + base + extra + } else { + base + } + }) + .collect(); + (start, extents) +} + impl LiveTui { pub fn new(mut client: C) -> io::Result { let snapshot = client.snapshot()?; @@ -1253,9 +1423,17 @@ impl LiveTui { density: *density, }) } - MixerLayoutEntry::Separator { id, label } => { - Some(MixerRow::Separator { id, label }) - } + MixerLayoutEntry::Separator { + id, + label, + size, + decoration, + } => Some(MixerRow::Separator { + id, + label, + size: *size, + decoration: *decoration, + }), _ => None, }) .collect() @@ -1436,6 +1614,7 @@ impl LiveTui { self.editor = Some(EditorInput { value: String::new(), target: None, + ..EditorInput::default() }); self.client_error = None; } @@ -1443,7 +1622,7 @@ impl LiveTui { fn edit_selected_entry(&mut self) { enum Selected { Fader(String), - Separator(String, String), + Separator(String, String, SeparatorSize, SeparatorDecoration), } let selected = self .mixer_rows() @@ -1453,9 +1632,17 @@ impl LiveTui { layout_id: Some(id), .. } => Some(Selected::Fader((*id).to_owned())), - MixerRow::Separator { id, label } => { - Some(Selected::Separator((*id).to_owned(), (*label).to_owned())) - } + MixerRow::Separator { + id, + label, + size, + decoration, + } => Some(Selected::Separator( + (*id).to_owned(), + (*label).to_owned(), + *size, + *decoration, + )), MixerRow::Fader { layout_id: None, .. } => None, @@ -1464,10 +1651,13 @@ impl LiveTui { Some(Selected::Fader(fader)) => { self.fader_editor = Some(FaderEditor { fader, selected: 0 }); } - Some(Selected::Separator(id, label)) => { + Some(Selected::Separator(id, label, size, decoration)) => { self.editor = Some(EditorInput { value: label, target: Some(id), + size, + decoration, + selected: 0, }); } None => return, @@ -1476,6 +1666,29 @@ impl LiveTui { } fn handle_editor_key(&mut self, code: KeyCode) { + if let Some(editor) = &mut self.editor { + match code { + KeyCode::Up => { + editor.selected = editor.selected.saturating_sub(1); + return; + } + KeyCode::Down | KeyCode::Tab => { + editor.selected = (editor.selected + 1) % 3; + return; + } + KeyCode::Left | KeyCode::Right if editor.selected > 0 => { + let forward = code == KeyCode::Right; + if editor.selected == 1 { + editor.size = editor.size.cycle(forward); + } else { + editor.decoration = editor.decoration.cycle(forward); + } + return; + } + KeyCode::Char(_) | KeyCode::Backspace if editor.selected != 0 => return, + _ => {} + } + } match code { KeyCode::Esc => self.editor = None, KeyCode::Backspace => { @@ -1503,9 +1716,12 @@ impl LiveTui { self.editor = Some(editor); return; } + let id = editor.target.clone().unwrap_or_else(|| { + unique_separator_id(label, &self.snapshot.configuration.mixer_layout) + }); let edit = editor.target.map_or_else( || ConfigurationEdit::AddSeparator { - id: unique_separator_id(label, &self.snapshot.configuration.mixer_layout), + id: id.clone(), label: label.to_owned(), position: self.selected.saturating_add(1), }, @@ -1518,7 +1734,14 @@ impl LiveTui { .client .edit_configuration(edit, self.snapshot.configuration.revision) { - Ok(snapshot) => self.replace_snapshot(snapshot), + Ok(snapshot) => { + self.replace_snapshot(snapshot); + self.apply_layout_edit(ConfigurationEdit::SetSeparatorPresentation { + id, + size: editor.size, + decoration: editor.decoration, + }); + } Err(error) => self.client_error = Some(error.to_string()), } } @@ -2887,9 +3110,9 @@ impl LiveTui { return; }; let theme = self.theme.palette(); - let popup = centered_rect(area, 56, 7); + let popup = centered_rect(area, 68, 9); let title = if editor.target.is_some() { - " Rename separator " + " Edit separator " } else { " Add separator " }; @@ -2907,16 +3130,33 @@ impl LiveTui { Clear.render(popup, buf); Paragraph::new(Text::from(vec![ Line::from(""), - Line::from(format!("Name {}▏", editor.value)) - .fg(theme.foreground) - .bold(), + Line::from(format!( + "{} Name {}{}", + if editor.selected == 0 { "▶" } else { " " }, + editor.value, + if editor.selected == 0 { "▏" } else { "" } + )) + .fg(theme.foreground) + .bold(), + Line::from(format!( + "{} Size {} [←→]", + if editor.selected == 1 { "▶" } else { " " }, + editor.size.label() + )), + Line::from(format!( + "{} Decoration {} [←→]", + if editor.selected == 2 { "▶" } else { " " }, + editor.decoration.label() + )), Line::from(guidance).style(guidance_style), ])) .alignment(Alignment::Center) .block( Block::bordered() .title(title.fg(theme.accent).bold()) - .title_bottom(" Enter save · Esc cancel ".fg(theme.muted)) + .title_bottom( + " ↑↓/Tab select · ←→ change · Enter accept · Esc cancel ".fg(theme.muted), + ) .border_set(border::DOUBLE) .border_style(Style::default().fg(theme.accent)) .style(Style::default().bg(theme.background)), @@ -3627,51 +3867,10 @@ impl LiveTui { } fn render_fader_strips(&self, area: Rect, buf: &mut Buffer, rows: &[MixerRow<'_>]) { - let selected = self.selected.min(rows.len().saturating_sub(1)); - let width = |row: &MixerRow<'_>| match row { - MixerRow::Fader { - density: FaderDensity::Slim, - .. - } => 10, - MixerRow::Fader { - density: FaderDensity::BorderedSlim, - .. - } => 13, - MixerRow::Fader { - density: FaderDensity::Standard, - .. - } => 16, - MixerRow::Fader { - density: FaderDensity::Thick, - .. - } => 22, - MixerRow::Separator { label, .. } => u16::try_from(label.chars().count()) - .unwrap_or(u16::MAX) - .saturating_add(2) - .clamp(8, 18), - }; - let mut start = selected; - let mut used = width(&rows[selected]).min(area.width); - while start > 0 { - let candidate = width(&rows[start - 1]); - if used.saturating_add(candidate) > area.width { - break; - } - start -= 1; - used = used.saturating_add(candidate); - } - let mut end = selected + 1; - while end < rows.len() { - let candidate = width(&rows[end]); - if used.saturating_add(candidate) > area.width { - break; - } - used = used.saturating_add(candidate); - end += 1; - } + let (start, extents) = mixer_viewport(rows, self.selected, area.width, true); + let used = extents.iter().copied().sum::(); let mut x = area.x.saturating_add(area.width.saturating_sub(used) / 2); - for (index, row) in rows.iter().enumerate().take(end).skip(start) { - let strip_width = width(row).min(area.right().saturating_sub(x)); + for ((index, row), strip_width) in rows.iter().enumerate().skip(start).zip(extents) { let strip_area = Rect::new(x, area.y, strip_width, area.height); match row { MixerRow::Fader { .. } => { @@ -3692,7 +3891,10 @@ impl LiveTui { row: &MixerRow<'_>, selected: bool, ) { - let MixerRow::Separator { label, .. } = row else { + let MixerRow::Separator { + label, decoration, .. + } = row + else { return; }; let theme = self.theme.palette(); @@ -3701,11 +3903,19 @@ impl LiveTui { } else { Style::default().fg(theme.muted) }; - let divider_x = area.x.saturating_add(area.width / 2); - for y in area.y..area.bottom().saturating_sub(1) { - if let Some(cell) = buf.cell_mut((divider_x, y)) { - cell.set_symbol("│"); - cell.set_style(style); + if *decoration == SeparatorDecoration::Box { + Block::bordered().border_style(style).render(area, buf); + } else if *decoration != SeparatorDecoration::Plain { + let divider_x = area.x.saturating_add(area.width / 2); + for y in area.y..area.bottom().saturating_sub(1) { + if let Some(cell) = buf.cell_mut((divider_x, y)) { + cell.set_symbol(if *decoration == SeparatorDecoration::Double { + "║" + } else { + "│" + }); + cell.set_style(style); + } } } if area.height > 0 { @@ -3861,46 +4071,59 @@ impl LiveTui { fn render_vertical_streams(&self, area: Rect, buf: &mut Buffer, rows: &[MixerRow<'_>]) { let theme = self.theme.palette(); - let selected = self.selected.min(rows.len().saturating_sub(1)); - let mut start = selected; - let mut occupied = rows[selected].height(); - while start > 0 { - let previous_height = rows[start - 1].height(); - if occupied.saturating_add(previous_height) > area.height { - break; - } - start -= 1; - occupied = occupied.saturating_add(previous_height); - } + let (start, extents) = mixer_viewport(rows, self.selected, area.height, false); let mut y = area.y; - for (index, layout_row) in rows.iter().enumerate().skip(start) { + for ((index, layout_row), extent) in rows.iter().enumerate().skip(start).zip(extents) { let remaining = area.bottom().saturating_sub(y); if remaining == 0 { break; } - let row_area = Rect::new(area.x, y, area.width, layout_row.height().min(remaining)); + let row_area = Rect::new(area.x, y, area.width, extent.min(remaining)); y = y.saturating_add(row_area.height); let selected = index == self.selected; let MixerRow::Fader { item, density, .. } = layout_row else { - let MixerRow::Separator { label, .. } = layout_row else { + let MixerRow::Separator { + label, decoration, .. + } = layout_row + else { continue; }; - if row_area.height < 2 { - break; + let style = if selected { + Style::default().fg(theme.accent).bold() + } else { + Style::default().fg(theme.muted) + }; + if *decoration == SeparatorDecoration::Box { + Block::bordered().border_style(style).render(row_area, buf); } - Paragraph::new(format!("── {label} ──")) + let line_area = Rect::new( + row_area.x, + row_area.y + row_area.height / 2, + row_area.width, + 1, + ); + if matches!( + decoration, + SeparatorDecoration::Line | SeparatorDecoration::Double + ) { + let glyph = if *decoration == SeparatorDecoration::Double { + "═" + } else { + "─" + }; + Paragraph::new(glyph.repeat(usize::from(row_area.width))) + .style(style) + .render(line_area, buf); + } + let label = if *decoration == SeparatorDecoration::Plain { + (*label).to_owned() + } else { + format!(" {label} ") + }; + Paragraph::new(label) .alignment(Alignment::Center) - .style(if selected { - Style::default().fg(theme.accent).bold() - } else { - Style::default().fg(theme.muted) - }) - .block(Block::bordered().border_style(if selected { - Style::default().fg(theme.accent) - } else { - Style::default().fg(theme.muted) - })) - .render(row_area, buf); + .style(style) + .render(line_area, buf); continue; }; let percent = item @@ -4170,6 +4393,8 @@ mod tests { MixerLayoutEntry::Separator { id: id.clone(), label: label.clone(), + size: SeparatorSize::default(), + decoration: SeparatorDecoration::default(), }, ), ConfigurationEdit::SetSeparatorLabel { id, label } => { @@ -4181,6 +4406,13 @@ mod tests { current.clone_from(label); } } + ConfigurationEdit::SetSeparatorPresentation { id, size, decoration } => { + if let Some(MixerLayoutEntry::Separator { size: current_size, decoration: current_decoration, .. }) = + self.snapshot.configuration.mixer_layout.iter_mut().find(|entry| matches!(entry, MixerLayoutEntry::Separator { id: separator, .. } if separator == id)) { + *current_size = *size; + *current_decoration = *decoration; + } + } ConfigurationEdit::RemoveSeparator { id } => self .snapshot .configuration @@ -4454,6 +4686,143 @@ mod tests { assert!(question_rendered.contains("Keyboard help")); } + #[test] + fn separator_popup_edits_size_and_decoration_and_cancel_discards_changes() { + let edits = Arc::new(Mutex::new(Vec::new())); + let mut snapshot = DemoClient::default().view(); + snapshot.configuration.mixer_layout = vec![MixerLayoutEntry::Separator { + id: "heading".into(), + label: "Heading".into(), + size: SeparatorSize::Standard, + decoration: SeparatorDecoration::Line, + }]; + let mut app = LiveTui::new(LayoutClient { + snapshot, + edits: Arc::clone(&edits), + }) + .unwrap(); + app.handle_key_event(press(KeyCode::Char('e'))); + app.handle_key_event(press(KeyCode::Down)); + app.handle_key_event(press(KeyCode::Right)); + app.handle_key_event(press(KeyCode::Right)); + app.handle_key_event(press(KeyCode::Down)); + app.handle_key_event(press(KeyCode::Right)); + app.handle_key_event(press(KeyCode::Enter)); + assert!(matches!( + &app.snapshot.configuration.mixer_layout[0], + MixerLayoutEntry::Separator { + size: SeparatorSize::Fill, + decoration: SeparatorDecoration::Double, + .. + } + )); + let count = edits.lock().unwrap().len(); + app.handle_key_event(press(KeyCode::Char('e'))); + assert_eq!(app.editor.as_ref().unwrap().size, SeparatorSize::Fill); + app.handle_key_event(press(KeyCode::Down)); + app.handle_key_event(press(KeyCode::Left)); + app.handle_key_event(press(KeyCode::Esc)); + assert_eq!(edits.lock().unwrap().len(), count); + assert!(app.editor.is_none()); + } + + #[test] + fn fill_separators_share_leftover_space_without_displacing_faders() { + let snapshot = DemoClient::default().view(); + let fader = || MixerRow::Fader { + layout_id: None, + item: &snapshot.items[0], + density: FaderDensity::Standard, + }; + let spacer = || MixerRow::Separator { + id: "gap", + label: "Gap", + size: SeparatorSize::Fill, + decoration: SeparatorDecoration::Plain, + }; + let rows = vec![fader(), spacer(), fader(), spacer()]; + assert_eq!( + mixer_viewport(&rows, 0, 60, true), + (0, vec![16, 14, 16, 14]) + ); + assert_eq!(mixer_viewport(&rows, 0, 20, false), (0, vec![4, 6, 4, 6])); + for horizontal in [false, true] { + for available in 0..80 { + for selected in 0..rows.len() { + let (start, extents) = mixer_viewport(&rows, selected, available, horizontal); + assert!(extents.iter().copied().sum::() <= available); + if available > 0 { + assert!(start <= selected && selected < start + extents.len()); + assert!(extents.iter().all(|extent| *extent > 0)); + } + } + } + } + } + + #[test] + fn separator_decorations_render_in_both_orientations_at_small_sizes() { + let app = LiveTui::new(DemoClient::default()).unwrap(); + for horizontal in [false, true] { + for size in [ + SeparatorSize::Slim, + SeparatorSize::Standard, + SeparatorSize::Thick, + SeparatorSize::Fill, + ] { + for decoration in [ + SeparatorDecoration::Plain, + SeparatorDecoration::Line, + SeparatorDecoration::Double, + SeparatorDecoration::Box, + ] { + let rows = vec![MixerRow::Separator { + id: "heading", + label: "Heading", + size, + decoration, + }]; + for (width, height) in [(1, 1), (8, 3), (60, 20)] { + let area = Rect::new(0, 0, width, height); + let mut buffer = Buffer::empty(area); + if horizontal { + app.render_fader_strips(area, &mut buffer, &rows); + } else { + app.render_vertical_streams(area, &mut buffer, &rows); + } + if width == 60 { + let rendered = buffer + .content + .iter() + .map(|cell| cell.symbol()) + .collect::(); + match decoration { + SeparatorDecoration::Plain => { + assert!(!rendered.contains(['│', '─', '║', '═', '┌'])) + } + SeparatorDecoration::Line => { + assert!(rendered.contains(if horizontal { + '│' + } else { + '─' + })) + } + SeparatorDecoration::Double => { + assert!(rendered.contains(if horizontal { + '║' + } else { + '═' + })) + } + SeparatorDecoration::Box => assert!(rendered.contains('┌')), + } + } + } + } + } + } + } + #[test] fn separator_name_is_edited_in_a_centered_popup() { let mut app = LiveTui::new(DemoClient::default()).unwrap(); @@ -4471,14 +4840,15 @@ mod tests { .map(|row| row.iter().map(|cell| cell.symbol()).collect::()) .collect::>(); - assert!(rows[6..14].iter().any(|row| row.contains("Add separator"))); + assert!(rows[5..15].iter().any(|row| row.contains("Add separator"))); assert!(rows[6..14].iter().any(|row| row.contains("Name Music"))); - assert!(rows[6..14].iter().any(|row| row.contains("Enter save"))); + assert!(rows[5..15].iter().any(|row| row.contains("Enter accept"))); assert!(!rows.iter().any(|row| row.contains("separator name:"))); app.editor = Some(EditorInput { value: "Players".to_owned(), target: Some("players".to_owned()), + ..EditorInput::default() }); let mut rename_buffer = Buffer::empty(area); (&app).render(area, &mut rename_buffer); @@ -4487,7 +4857,7 @@ mod tests { .iter() .map(|cell| cell.symbol()) .collect::(); - assert!(renamed.contains("Rename separator")); + assert!(renamed.contains("Edit separator")); assert!(renamed.contains("Name Players")); } @@ -4685,6 +5055,8 @@ mod tests { MixerLayoutEntry::Separator { id: "players".into(), label: "Players".into(), + size: SeparatorSize::Standard, + decoration: SeparatorDecoration::Line, }, MixerLayoutEntry::Fader { fader: "music-fader".into(), @@ -5201,6 +5573,8 @@ mod tests { MixerLayoutEntry::Separator { id: "players".into(), label: "Players".into(), + size: SeparatorSize::Standard, + decoration: SeparatorDecoration::Line, }, MixerLayoutEntry::Fader { fader: "browser-fader".into(), @@ -5996,6 +6370,8 @@ mod tests { let layout = |id: &str| MixerLayoutEntry::Separator { id: id.to_owned(), label: id.to_owned(), + size: SeparatorSize::Standard, + decoration: SeparatorDecoration::Line, }; let mut snapshot = DemoClient::default().view(); snapshot.configuration.mixer_layout = vec![layout("a"), layout("b"), layout("c")]; @@ -6285,6 +6661,8 @@ mod tests { let layout = |id: &str| MixerLayoutEntry::Separator { id: id.to_owned(), label: id.to_owned(), + size: SeparatorSize::Standard, + decoration: SeparatorDecoration::Line, }; let mut confirmed = DemoClient::default().view(); confirmed.configuration.revision = 2;