Skip to content

AI-native design editor. Open-source Figma alternative.

fix(desktop): keep View check states the menu hides without a document (#1709)

Danila Poyarkova02830cc6c60
diff --git a/desktop/src/menu/mod.rs b/desktop/src/menu/mod.rs
index 391c96811..761392dc9 100644
--- a/desktop/src/menu/mod.rs
+++ b/desktop/src/menu/mod.rs
@@ -34,18 +34,20 @@ struct MenuEntry {
sub: Vec<MenuEntry>,
}
-/// What the schema menu is built from besides the schema: recent files, enabled features, and
-/// the items whose editor command is unavailable, such as Group with nothing selected.
+/// What the schema menu is built from besides the schema: recent files, enabled features, the
+/// items whose editor command is unavailable, such as Group with nothing selected, and the check
+/// states the frontend set, which outlive an item the menu hides, such as Rulers on Home.
#[derive(Default)]
pub struct NativeMenuInputs {
pub recent_files: Vec<String>,
pub features: Vec<String>,
pub disabled: HashSet<String>,
+ pub checked: HashMap<String, bool>,
}
pub struct NativeMenuState(pub Mutex<NativeMenuInputs>);
-/// What one build reads: the inputs, and the check states the menu being replaced had.
+/// What one build reads: the inputs, and the check states it gives its check items.
struct MenuBuild<'a> {
inputs: &'a NativeMenuInputs,
checked: HashMap<String, bool>,
@@ -184,7 +186,7 @@ fn build_submenu<R: tauri::Runtime>(
}
if entry.checkbox {
- // A rebuild keeps what the menu it replaces had checked.
+ // A rebuild keeps what the menu it replaces had checked, or the frontend set.
let checked = entry
.id
.as_ref()
@@ -254,11 +256,22 @@ fn build_recent_files_submenu<R: tauri::Runtime>(
builder.build()
}
+fn schema_groups() -> serde_json::Result<Vec<MenuGroup>> {
+ serde_json::from_str(include_str!("../../generated/menu.json"))
+}
+
+/// Whether the schema has a check item `id`, shown or hidden.
+fn is_schema_check_item(entries: &[MenuEntry], id: &str) -> bool {
+ entries.iter().any(|entry| {
+ (entry.checkbox && entry.id.as_deref() == Some(id)) || is_schema_check_item(&entry.sub, id)
+ })
+}
+
fn build_schema_menus<R: tauri::Runtime>(
app: &tauri::AppHandle<R>,
build: &MenuBuild,
) -> tauri::Result<Vec<Submenu<R>>> {
- let groups: Vec<MenuGroup> = serde_json::from_str(include_str!("../../generated/menu.json"))?;
+ let groups = schema_groups()?;
groups
.iter()
.filter(|group| {
@@ -347,22 +360,48 @@ pub fn set_native_menu_checked<R: tauri::Runtime>(
id: String,
checked: bool,
) -> Result<(), String> {
+ let groups = schema_groups().map_err(|error| error.to_string())?;
+ if !groups
+ .iter()
+ .any(|group| is_schema_check_item(&group.items, &id))
+ {
+ return Err(format!("Menu item is not a check item: {id}"));
+ }
+ let state = tauri::Manager::state::<NativeMenuState>(&app);
+ let mut inputs = state
+ .0
+ .lock()
+ .map_err(|_| "Menu state is unavailable".to_string())?;
+ inputs.checked.insert(id.clone(), checked);
let menu = editor_menu(&app).ok_or_else(|| "Application menu is unavailable".to_string())?;
- let item = find_menu_item(menu.items().map_err(|error| error.to_string())?, &id)
- .ok_or_else(|| format!("Menu item not found: {id}"))?;
- match item {
- MenuItemKind::Check(item) => item.set_checked(checked).map_err(|error| error.to_string()),
- _ => Err(format!("Menu item is not checkable: {id}")),
+ match find_menu_item(menu.items().map_err(|error| error.to_string())?, &id) {
+ Some(MenuItemKind::Check(item)) => {
+ item.set_checked(checked).map_err(|error| error.to_string())
+ }
+ Some(_) => Err(format!("Menu item is not checkable: {id}")),
+ // A check item the menu hides now takes the state when it shows again.
+ None => Ok(()),
}
}
+/// The check states a rebuild gives its items: the menu's own where it has the item, since a
+/// click checks it there, and what the frontend set for the items it hides.
+fn rebuilt_checks(
+ set: &HashMap<String, bool>,
+ current: HashMap<String, bool>,
+) -> HashMap<String, bool> {
+ let mut checked = set.clone();
+ checked.extend(current);
+ checked
+}
+
pub fn install_app_menu<R: tauri::Runtime>(
app: &tauri::AppHandle<R>,
inputs: &NativeMenuInputs,
) -> tauri::Result<()> {
let build = MenuBuild {
inputs,
- checked: current_checked(app),
+ checked: rebuilt_checks(&inputs.checked, current_checked(app)),
};
#[cfg(target_os = "macos")]
let app_menu = {
@@ -434,6 +473,9 @@ pub fn update_app_menu<R: tauri::Runtime>(
.0
.lock()
.map_err(|_| "Menu state is unavailable".to_string())?;
+ // Keep the states the menu has now for the items this rebuild may hide.
+ let current = current_checked(app);
+ inputs.checked = rebuilt_checks(&inputs.checked, current);
change(&mut inputs);
install_app_menu(app, &inputs).map_err(|error| error.to_string())
}
@@ -506,8 +548,34 @@ pub fn set_native_menu_disabled<R: tauri::Runtime>(
mod tests {
use super::*;
+ #[test]
+ fn knows_check_items_the_menu_hides() {
+ let groups = groups();
+ let has = |id: &str| {
+ groups
+ .iter()
+ .any(|group| is_schema_check_item(&group.items, id))
+ };
+ assert!(has("view-rulers"));
+ assert!(!has("view-rulerz"));
+ assert!(!has("new"));
+ }
+
+ #[test]
+ fn keeps_check_states_of_hidden_items() {
+ let set = HashMap::from([
+ ("view-rulers".to_string(), false),
+ ("snap-objects".into(), true),
+ ]);
+ // Rulers is hidden on Home; Snap to objects was clicked off in the menu.
+ let current = HashMap::from([("snap-objects".to_string(), false)]);
+ let checked = rebuilt_checks(&set, current);
+ assert_eq!(checked.get("view-rulers"), Some(&false));
+ assert_eq!(checked.get("snap-objects"), Some(&false));
+ }
+
fn groups() -> Vec<MenuGroup> {
- serde_json::from_str(include_str!("../../generated/menu.json")).expect("menu.json")
+ schema_groups().expect("menu.json")
}
fn shown(group: &MenuGroup, features: &[String]) -> Vec<String> {