Skip to content

Commit d8094c6

Browse files
authored
feat(workbench): 标题栏按钮可以放到左右工具条 (#90)
工具条布局目录新增 alsoOnToolStripes:标了它的标题栏按钮除标题栏外还能放进六个工具条区, 也能移回标题栏。它在工具条上仍然不打开面板、不能隐藏,也不能放进状态栏。只有默认在标题栏的 按钮能标;标在别处的目录报 invalidRequest。
1 parent d833d6a commit d8094c6

1 file changed

Lines changed: 193 additions & 12 deletions

File tree

‎rust/crates/core-rules/src/workbench/chrome_layout.rs‎

Lines changed: 193 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
//! invents items: it drops saved placements of items the catalog no longer lists, and puts catalog
1515
//! items the saved state does not mention yet at the end of their default zone, so a saved layout
1616
//! survives added and removed buttons. Items never leave their surface: a stripe button cannot move
17-
//! into the status bar. Fixed chrome, such as the status bar path or the project and branch
18-
//! switchers, is not part of the model.
17+
//! into the status bar. The one exception is a title bar item the catalog marks
18+
//! `also_on_tool_stripes`: it may also sit in the six stripe zones and come back to the title bar,
19+
//! and it keeps the title bar rules there, so it opens no panel and cannot be hidden. Fixed chrome,
20+
//! such as the status bar path or the project and branch switchers, is not part of the model.
1921
//!
2022
//! Each stripe zone owns one panel slot, so at most one panel item per zone is open. Opening a
2123
//! panel closes the other open panel of its zone, and moving an open panel takes its open state
@@ -29,7 +31,8 @@
2931
//! and its place in the zone order, so showing it again puts the button back where it was. Hiding
3032
//! closes the item's panel; a host may still open the panel of a hidden item, from a shortcut or a
3133
//! command, and the item stays hidden. Where the host lists hidden items for the user to restore
32-
//! is presentation. Title bar and status bar items cannot be hidden.
34+
//! is presentation. Title bar and status bar items cannot be hidden, not even a title bar item
35+
//! that sits on a stripe.
3336
3437
use crate::{RuleError, RuleErrorCode};
3538
use serde::{Deserialize, Serialize};
@@ -132,14 +135,19 @@ impl Zone {
132135
pub struct CatalogItem {
133136
/// Stable host identifier, persisted in placements.
134137
pub id: String,
135-
/// Zone of the default layout. Its surface is the only surface the item may sit in.
138+
/// Zone of the default layout. The item may sit in the zones of this surface only, unless
139+
/// `also_on_tool_stripes` adds the stripe zones.
136140
pub default_zone: Zone,
137141
/// The button opens a tool panel in its zone's panel slot. Only stripe items can.
138142
#[serde(default)]
139143
pub panel: bool,
140144
/// The panel is open in the default layout. Requires `panel`.
141145
#[serde(default)]
142146
pub open_by_default: bool,
147+
/// The item may also sit in the six tool stripe zones. Only title bar items can; on a stripe
148+
/// the item still opens no panel and cannot be hidden.
149+
#[serde(default)]
150+
pub also_on_tool_stripes: bool,
143151
}
144152

145153
/// Zone of one item. The order of placements with the same zone is the display order.
@@ -191,7 +199,8 @@ pub enum Action {
191199
Move {
192200
/// Item to move.
193201
id: String,
194-
/// Target zone; must belong to the item's surface.
202+
/// Target zone; must belong to the item's surface, or be a stripe zone for an item marked
203+
/// `also_on_tool_stripes`.
195204
zone: Zone,
196205
/// Item the moved one is inserted in front of.
197206
#[serde(default)]
@@ -260,9 +269,10 @@ pub fn reduce_json(payload: Value) -> Result<Value, RuleError> {
260269
/// Applies `action` to `state` after repairing `state` against `catalog`.
261270
///
262271
/// Fails with `invalid_request` when the catalog is malformed (empty or repeated identifiers, a
263-
/// panel outside the stripes, a default-open item that is not a panel), or when the action names
264-
/// an unknown item, moves an item to another surface, opens an item that is not a panel, or hides
265-
/// an item that is not on a tool stripe.
272+
/// panel outside the stripes, a default-open item that is not a panel, `also_on_tool_stripes` on an
273+
/// item outside the title bar), or when the action names an unknown item, moves an item to a zone
274+
/// it may not sit in, opens an item that is not a panel, or hides an item whose default zone is not
275+
/// on a tool stripe.
266276
pub fn reduce(
267277
catalog: &[CatalogItem],
268278
state: State,
@@ -321,6 +331,12 @@ fn catalog_index(catalog: &[CatalogItem]) -> Result<HashMap<&str, usize>, RuleEr
321331
invalid("Only panel items can be open by default").with_details(item.id.clone())
322332
);
323333
}
334+
if item.also_on_tool_stripes && item.default_zone.surface() != Surface::TitleBar {
335+
return Err(
336+
invalid("Only title bar items can also sit on the tool stripes")
337+
.with_details(item.id.clone()),
338+
);
339+
}
324340
if index.insert(item.id.as_str(), position).is_some() {
325341
return Err(invalid("Chrome catalog repeats an id").with_details(item.id.clone()));
326342
}
@@ -378,7 +394,7 @@ impl Layout {
378394
let Some(&item) = index.get(placement.id.as_str()) else {
379395
continue;
380396
};
381-
if placed[item] || placement.zone.surface() != catalog[item].default_zone.surface() {
397+
if placed[item] || !allowed(&catalog[item], placement.zone) {
382398
continue;
383399
}
384400
placed[item] = true;
@@ -418,8 +434,8 @@ impl Layout {
418434
zone: Zone,
419435
before: Option<usize>,
420436
) -> Result<(), RuleError> {
421-
if zone.surface() != catalog[item].default_zone.surface() {
422-
return Err(invalid("Chrome item cannot leave its surface")
437+
if !allowed(&catalog[item], zone) {
438+
return Err(invalid("Chrome item cannot sit in that zone")
423439
.with_details(catalog[item].id.clone()));
424440
}
425441
if before == Some(item) && self.zone_of[item] == zone {
@@ -508,8 +524,17 @@ impl Layout {
508524
}
509525
}
510526

527+
/// The item may sit in `zone`: a zone of its default surface, or a stripe zone when the catalog
528+
/// marks it `also_on_tool_stripes`.
529+
fn allowed(item: &CatalogItem, zone: Zone) -> bool {
530+
let surface = zone.surface();
531+
surface == item.default_zone.surface()
532+
|| (item.also_on_tool_stripes && surface == Surface::ToolStripe)
533+
}
534+
511535
/// Only tool stripe buttons can be hidden: the title bar and status bar have no place that
512-
/// lists hidden items.
536+
/// lists hidden items. A title bar item that sits on a stripe keeps this rule, so it never
537+
/// disappears from both places.
513538
fn hideable(item: &CatalogItem) -> bool {
514539
item.default_zone.surface() == Surface::ToolStripe
515540
}
@@ -524,9 +549,20 @@ mod tests {
524549
default_zone: zone,
525550
panel,
526551
open_by_default: open,
552+
also_on_tool_stripes: false,
527553
}
528554
}
529555

556+
/// The test catalog plus a title bar item that may also sit on the stripes.
557+
fn catalog_with_settings() -> Vec<CatalogItem> {
558+
let mut catalog = catalog();
559+
catalog.push(CatalogItem {
560+
also_on_tool_stripes: true,
561+
..item("settings", Zone::TitleTrailing, false, false)
562+
});
563+
catalog
564+
}
565+
530566
fn catalog() -> Vec<CatalogItem> {
531567
vec![
532568
item("project", Zone::LeftTop, true, true),
@@ -882,8 +918,153 @@ mod tests {
882918
],
883919
vec![item("a", Zone::TitleTrailing, true, false)],
884920
vec![item("a", Zone::LeftTop, false, true)],
921+
vec![CatalogItem {
922+
also_on_tool_stripes: true,
923+
..item("a", Zone::LeftTop, false, false)
924+
}],
925+
vec![CatalogItem {
926+
also_on_tool_stripes: true,
927+
..item("a", Zone::StatusTrailing, false, false)
928+
}],
885929
] {
886930
assert!(reduce(&broken, State::default(), Action::Normalize).is_err());
887931
}
888932
}
933+
934+
#[test]
935+
fn a_title_item_marked_for_the_stripes_moves_onto_them_and_back() {
936+
let catalog = catalog_with_settings();
937+
let reduce_ok =
938+
|state: State, action: Action| reduce(&catalog, state, action).unwrap().state;
939+
let defaults = reduce_ok(State::default(), Action::Reset);
940+
assert_eq!(
941+
ids(&defaults, Zone::TitleTrailing),
942+
["run", "search", "settings"]
943+
);
944+
let state = reduce_ok(
945+
defaults.clone(),
946+
Action::Move {
947+
id: "settings".into(),
948+
zone: Zone::LeftTop,
949+
before: Some("commit".into()),
950+
},
951+
);
952+
assert_eq!(
953+
ids(&state, Zone::LeftTop),
954+
["project", "settings", "commit"]
955+
);
956+
assert_eq!(ids(&state, Zone::TitleTrailing), ["run", "search"]);
957+
assert_eq!(state.open, defaults.open);
958+
let again = reduce(&catalog, state.clone(), Action::Normalize).unwrap();
959+
assert!(!again.changed);
960+
let state = reduce_ok(
961+
state,
962+
Action::Move {
963+
id: "settings".into(),
964+
zone: Zone::BottomRight,
965+
before: None,
966+
},
967+
);
968+
assert_eq!(ids(&state, Zone::BottomRight), ["update", "settings"]);
969+
let back = reduce_ok(
970+
state,
971+
Action::Move {
972+
id: "settings".into(),
973+
zone: Zone::TitleTrailing,
974+
before: Some("run".into()),
975+
},
976+
);
977+
assert_eq!(
978+
ids(&back, Zone::TitleTrailing),
979+
["settings", "run", "search"]
980+
);
981+
assert_eq!(ids(&back, Zone::BottomRight), ["update"]);
982+
}
983+
984+
#[test]
985+
fn a_title_item_on_a_stripe_keeps_the_title_bar_rules() {
986+
let catalog = catalog_with_settings();
987+
let on_stripe = reduce(
988+
&catalog,
989+
State::default(),
990+
Action::Move {
991+
id: "settings".into(),
992+
zone: Zone::RightTop,
993+
before: None,
994+
},
995+
)
996+
.unwrap()
997+
.state;
998+
for action in [
999+
Action::Move {
1000+
id: "settings".into(),
1001+
zone: Zone::StatusTrailing,
1002+
before: None,
1003+
},
1004+
Action::SetHidden {
1005+
id: "settings".into(),
1006+
hidden: true,
1007+
},
1008+
Action::Toggle {
1009+
id: "settings".into(),
1010+
},
1011+
// Unmarked title bar items still stay in the title bar.
1012+
Action::Move {
1013+
id: "search".into(),
1014+
zone: Zone::RightTop,
1015+
before: None,
1016+
},
1017+
] {
1018+
let error = reduce(&catalog, on_stripe.clone(), action).unwrap_err();
1019+
assert_eq!(error.code, RuleErrorCode::InvalidRequest);
1020+
}
1021+
}
1022+
1023+
#[test]
1024+
fn normalize_keeps_a_marked_title_item_on_its_stripe_and_drops_foreign_ones() {
1025+
let saved = State {
1026+
placements: vec![
1027+
Placement {
1028+
id: "settings".into(),
1029+
zone: Zone::RightTop,
1030+
},
1031+
Placement {
1032+
id: "run".into(),
1033+
zone: Zone::RightTop,
1034+
},
1035+
],
1036+
open: Vec::new(),
1037+
hidden: vec!["settings".into()],
1038+
};
1039+
let state = reduce(&catalog_with_settings(), saved, Action::Normalize)
1040+
.unwrap()
1041+
.state;
1042+
assert_eq!(ids(&state, Zone::RightTop), ["settings", "notifications"]);
1043+
assert_eq!(ids(&state, Zone::TitleTrailing), ["run", "search"]);
1044+
assert!(state.hidden.is_empty());
1045+
}
1046+
1047+
#[test]
1048+
fn the_stripe_flag_decodes_from_the_wire_and_defaults_to_off() {
1049+
let request = serde_json::json!({
1050+
"catalog": [
1051+
{"id": "settings", "defaultZone": "titleTrailing", "alsoOnToolStripes": true},
1052+
{"id": "search", "defaultZone": "titleTrailing"},
1053+
],
1054+
"action": {"kind": "move", "id": "settings", "zone": "leftTop"},
1055+
});
1056+
let answer = reduce_json(request).unwrap();
1057+
assert_eq!(
1058+
answer["state"]["placements"],
1059+
serde_json::json!([
1060+
{"id": "settings", "zone": "leftTop"},
1061+
{"id": "search", "zone": "titleTrailing"},
1062+
])
1063+
);
1064+
let refused = serde_json::json!({
1065+
"catalog": [{"id": "search", "defaultZone": "titleTrailing"}],
1066+
"action": {"kind": "move", "id": "search", "zone": "leftTop"},
1067+
});
1068+
assert!(reduce_json(refused).is_err());
1069+
}
8891070
}

0 commit comments

Comments
 (0)