Files
bherbruck 59f68c9d8a Views: one view menu on tabs and cards, saved view layouts, and a compass per view (#20441)
* Claim the shared view menu

* Share the view menu between tabs and cards

* Open card view menus without relocking egui

* Cover the Shift F10 view menu shortcut

* Claim saved view layouts

* Save and apply named view layouts

* Keep saved layout dialogs responsive and return typed replies

* Pass the saved layout words to the layout menu as data

The layout menu now takes its words from LayoutText instead of
translating inside ui-kit, so the saved layout rows do the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Claim the per-view compass

* Give each view its own compass visibility

* Keep the View menu as a growing slice

* Drop a stray bracket left by the rebase

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Construct the saved layouts test app with services

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Use the kit's text field for layout names and keep the menu's row id clear of the panel layout command

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Compare the main window's views in the close and saved layout tests

ids() now lists the views of every window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Format the layout name field

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Nathan Dane <nathandane.dev@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 19:01:17 +00:00

92 lines
4.1 KiB
Rust

//! Validate requests for the host's reusable, application-wide view layouts.
use serde_json::Value;
use super::CommandSpec;
use crate::params::bad;
use crate::schema::tab_layouts::{TabLayoutListReply, TabLayoutNameParams, TabLayoutRenameParams};
use crate::schema::tab_views::TabViewNoParams;
use crate::schema::{TypedContract, output_schema_for};
use crate::{Result, Session};
const LIST_CONTRACT: TypedContract<TabViewNoParams> = TypedContract::new();
const NAME_CONTRACT: TypedContract<TabLayoutNameParams> = TypedContract::new();
const RENAME_CONTRACT: TypedContract<TabLayoutRenameParams> = TypedContract::new();
pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("tab.layout.apply", "Apply View Layout", crate::checked!("tab.layout.apply", apply))
.icon("list")
.noundo()
.doc_only()
.contract(&NAME_CONTRACT)
.output(output_schema_for::<TabLayoutNameParams>)
.params("name: a saved view layout → name; arranges the current design's views in the saved window setup"),
CommandSpec::new("tab.layout.list", "List Saved View Layouts", crate::checked!("tab.layout.list", list))
.icon("list")
.noundo()
.read_only()
.contract(&LIST_CONTRACT)
.output(output_schema_for::<TabLayoutListReply>)
.params("no parameters → layouts: names of the app's saved view layouts; read-only"),
CommandSpec::new("tab.layout.remove", "Remove Saved View Layout", crate::checked!("tab.layout.remove", remove))
.icon("close")
.noundo()
.contract(&NAME_CONTRACT)
.output(output_schema_for::<TabLayoutNameParams>)
.params("name: a saved view layout → name; removes it from the app's saved layouts"),
CommandSpec::new("tab.layout.rename", "Rename Saved View Layout", crate::checked!("tab.layout.rename", rename))
.icon("rename")
.noundo()
.contract(&RENAME_CONTRACT)
.output(output_schema_for::<TabLayoutRenameParams>)
.params("name: a saved view layout; new_name: 1 to 64 characters after trimming, no control characters → name, new_name"),
CommandSpec::new("tab.layout.save", "Save View Layout", crate::checked!("tab.layout.save", save))
.icon("save")
.noundo()
.doc_only()
.contract(&NAME_CONTRACT)
.output(output_schema_for::<TabLayoutNameParams>)
.params("name: 1 to 64 characters after trimming, no control characters → name; saves the current window setup for reuse with any design"),
];
fn apply(_s: &mut Session, p: &Value) -> Result<Value> {
named("tab.layout.apply", p)
}
fn list(_s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "tab.layout.list";
let p = if p.is_null() { Value::Object(Default::default()) } else { p.clone() };
let _: TabViewNoParams = serde_json::from_value(p).map_err(|e| bad(cmd, e.to_string()))?;
reply(cmd, TabLayoutListReply::default())
}
fn remove(_s: &mut Session, p: &Value) -> Result<Value> {
named("tab.layout.remove", p)
}
fn rename(_s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "tab.layout.rename";
let request: TabLayoutRenameParams = serde_json::from_value(p.clone()).map_err(|e| bad(cmd, e.to_string()))?;
reply(cmd, TabLayoutRenameParams { name: name(cmd, "name", &request.name)?, new_name: name(cmd, "new_name", &request.new_name)? })
}
fn save(_s: &mut Session, p: &Value) -> Result<Value> {
named("tab.layout.save", p)
}
fn named(cmd: &str, p: &Value) -> Result<Value> {
let request: TabLayoutNameParams = serde_json::from_value(p.clone()).map_err(|e| bad(cmd, e.to_string()))?;
reply(cmd, TabLayoutNameParams { name: name(cmd, "name", &request.name)? })
}
fn name(cmd: &str, key: &str, value: &str) -> Result<String> {
let value = value.trim();
if value.is_empty() || value.chars().count() > solidcraft_views::MAX_NAME || value.chars().any(char::is_control) {
return Err(bad(cmd, format!("{key} must be 1 to {} characters, without control characters", solidcraft_views::MAX_NAME)));
}
Ok(value.to_owned())
}
fn reply(cmd: &str, reply: impl serde::Serialize) -> Result<Value> {
serde_json::to_value(reply).map_err(|e| bad(cmd, e.to_string()))
}