diff --git a/src/command/cmd.rs b/src/command/cmd.rs index ff23a60..fff927f 100644 --- a/src/command/cmd.rs +++ b/src/command/cmd.rs @@ -1434,64 +1434,38 @@ impl Cmd for ToggleRecordsMode { "toggle-records-mode" } fn execute(&self, ctx: &CmdContext) -> Vec> { - use crate::view::Axis; - let view = ctx.model.active_view(); - - // Detect current state - let is_records = view - .category_axes - .get("_Index") - .copied() - == Some(Axis::Row) - && view.category_axes.get("_Dim").copied() == Some(Axis::Column); - - let mut effects: Vec> = Vec::new(); + let is_records = ctx.layout.is_records_mode(); if is_records { - // Switch back to pivot: auto-assign axes - // First regular category → Row, second → Column, rest → Page, - // virtuals/labels → None. - let mut row_done = false; - let mut col_done = false; - for (name, cat) in &ctx.model.categories { - let axis = if !cat.kind.is_regular() { - Axis::None - } else if !row_done { - row_done = true; - Axis::Row - } else if !col_done { - col_done = true; - Axis::Column - } else { - Axis::Page - }; + // Navigate back to the previous view (restores original axes) + return vec![Box::new(effect::ViewBack), effect::set_status("Pivot mode")]; + } + + let mut effects: Vec> = Vec::new(); + let records_name = "_Records".to_string(); + + // Create (or replace) a _Records view and switch to it + effects.push(Box::new(effect::CreateView(records_name.clone()))); + effects.push(Box::new(effect::SwitchView(records_name))); + + // _Index on Row, _Dim on Column, everything else → None + effects.push(Box::new(effect::SetAxis { + category: "_Index".to_string(), + axis: crate::view::Axis::Row, + })); + effects.push(Box::new(effect::SetAxis { + category: "_Dim".to_string(), + axis: crate::view::Axis::Column, + })); + for name in ctx.model.categories.keys() { + if name != "_Index" && name != "_Dim" { effects.push(Box::new(effect::SetAxis { category: name.clone(), - axis, + axis: crate::view::Axis::None, })); } - effects.push(effect::set_status("Pivot mode")); - } else { - // Switch to records mode - effects.push(Box::new(effect::SetAxis { - category: "_Index".to_string(), - axis: Axis::Row, - })); - effects.push(Box::new(effect::SetAxis { - category: "_Dim".to_string(), - axis: Axis::Column, - })); - // Everything else → None - for name in ctx.model.categories.keys() { - if name != "_Index" && name != "_Dim" { - effects.push(Box::new(effect::SetAxis { - category: name.clone(), - axis: Axis::None, - })); - } - } - effects.push(effect::set_status("Records mode")); } + effects.push(effect::set_status("Records mode")); effects } }