Skip to content

Commit 7fe52e0

Browse files
committed
feat(workbench): 侧栏按钮可以隐藏,布局状态记录隐藏的入口
- chrome layout 的 State 新增 hidden,按 placements 的顺序列出用户隐藏的侧栏按钮;旧状态没有这个字段也能读入。 - 新增 Action::SetHidden { id, hidden }。隐藏时关闭该入口的面板,取消隐藏不改变面板状态;隐藏的入口保留原来的分区和位置,取消隐藏后回到原处。 - 隐藏的入口仍可以由快捷键或命令打开面板,打开后依然保持隐藏;移动隐藏的入口也保持隐藏。 - 只有侧栏按钮能隐藏,标题栏和状态栏的按钮返回 invalid_request;Reset 恢复全部入口;Normalize 去掉未知、重复和非侧栏的 id。 - 同步更新 command.rs 与 rust-core-api.md 中 workbench.chromeLayout.reduce 的说明。
1 parent df95582 commit 7fe52e0

3 files changed

Lines changed: 168 additions & 8 deletions

File tree

‎rust/contracts/rust-core-api.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ or removing a command fails until the table changes in the same commit.
559559
| `editor.groupLayout.dropTargets` | Say which places of each editor group accept a dragged tab (from `lithe-editor-layout`) |
560560
| `workspace.classifyVisibility` | Classify a batch of workspace-relative paths as hidden or visible. The rule lives in `lithe-core-rules` so the Windows frontend can run it synchronously |
561561
| `workspace.visibilityDefaults` | Return the built-in hidden-path lists (from `lithe-core-rules`) |
562-
| `workbench.chromeLayout.reduce` | Apply one transition to the rearrangeable workbench chrome: stripe, title bar and status bar button placement and open tool panels (from `lithe-core-rules`) |
562+
| `workbench.chromeLayout.reduce` | Apply one transition to the rearrangeable workbench chrome: stripe, title bar and status bar button placement, hidden stripe buttons and open tool panels (from `lithe-core-rules`) |
563563
| `terminal.parseLinkTarget` | Resolve terminal link candidates into file or external targets |
564564
| `debug.createSession` | Create a transport-neutral DAP session and return its initialize frame |
565565
| `debug.launch` | Queue a launch or attach request, including during initialization |

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

Lines changed: 165 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
//! edges (six zones, one for each tool window anchor of the IDEA new UI: Left Top, Left Bottom and
55
//! Bottom Left on the left stripe, Right Top, Right Bottom and Bottom Right on the right one), the
66
//! trailing end of the title bar and the trailing end of the status bar. Every host persists the
7-
//! same fact: the zone of each button, the order inside each zone, and which tool panels are open.
7+
//! same fact: the zone of each button, the order inside each zone, which stripe buttons the user
8+
//! hid, and which tool panels are open.
89
//! This module owns that fact and every transition of it. The host keeps only what exists while a
910
//! gesture runs (pointer, drag ghost, insertion marker) and sends one [`Action`] when the gesture
1011
//! ends.
@@ -23,6 +24,12 @@
2324
//! out: `LeftBottom` is the lower group of the stripe whose panel opens in the lower half of the
2425
//! left column, while `BottomLeft` is the group packed at the stripe's bottom whose panel opens in
2526
//! the left half of the bottom area, and likewise `RightBottom` and `BottomRight` on the right.
27+
//!
28+
//! A user may hide a tool stripe button, as IDEA's Hide Entry does. A hidden item keeps its zone
29+
//! and its place in the zone order, so showing it again puts the button back where it was. Hiding
30+
//! closes the item's panel; a host may still open the panel of a hidden item, from a shortcut or a
31+
//! 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.
2633
2734
use crate::{RuleError, RuleErrorCode};
2835
use serde::{Deserialize, Serialize};
@@ -148,7 +155,8 @@ pub struct Placement {
148155
/// The persisted chrome layout.
149156
///
150157
/// A state returned by the reducer lists every catalog item exactly once, zone by zone in
151-
/// [`Zone`] order, and `open` names at most one panel item per zone, also in zone order.
158+
/// [`Zone`] order, `open` names at most one panel item per zone, also in zone order, and `hidden`
159+
/// names tool stripe items only, in the order of `placements`.
152160
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
153161
#[serde(rename_all = "camelCase", deny_unknown_fields)]
154162
pub struct State {
@@ -158,6 +166,10 @@ pub struct State {
158166
/// Panel items whose panel is shown.
159167
#[serde(default)]
160168
pub open: Vec<String>,
169+
/// Tool stripe items whose button the user hid. They keep their placement; a state saved
170+
/// before hiding existed has none.
171+
#[serde(default)]
172+
pub hidden: Vec<String>,
161173
}
162174

163175
/// One state transition requested by a host.
@@ -197,6 +209,14 @@ pub enum Action {
197209
/// Desired state.
198210
open: bool,
199211
},
212+
/// Hides or shows the button of the tool stripe item `id`. Hiding also closes its panel;
213+
/// showing leaves the panel as it is.
214+
SetHidden {
215+
/// Tool stripe item.
216+
id: String,
217+
/// Desired state.
218+
hidden: bool,
219+
},
200220
}
201221

202222
/// Outcome of one reduction.
@@ -241,7 +261,8 @@ pub fn reduce_json(payload: Value) -> Result<Value, RuleError> {
241261
///
242262
/// Fails with `invalid_request` when the catalog is malformed (empty or repeated identifiers, a
243263
/// panel outside the stripes, a default-open item that is not a panel), or when the action names
244-
/// an unknown item, moves an item to another surface, or opens an item that is not a panel.
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.
245266
pub fn reduce(
246267
catalog: &[CatalogItem],
247268
state: State,
@@ -268,6 +289,10 @@ pub fn reduce(
268289
let item = lookup(&index, &id)?;
269290
layout.set_open(catalog, item, open)?;
270291
}
292+
Action::SetHidden { id, hidden } => {
293+
let item = lookup(&index, &id)?;
294+
layout.set_hidden(catalog, item, hidden)?;
295+
}
271296
}
272297
let next = layout.into_state(catalog);
273298
Ok(Reduction {
@@ -310,11 +335,13 @@ fn lookup(index: &HashMap<&str, usize>, id: &str) -> Result<usize, RuleError> {
310335
.ok_or_else(|| invalid("Unknown chrome item").with_details(id.to_string()))
311336
}
312337

313-
/// Working form of [`State`]: catalog positions per zone and the open panel of each zone.
338+
/// Working form of [`State`]: catalog positions per zone, the open panel of each zone, and
339+
/// which items are hidden, indexed by catalog position.
314340
struct Layout {
315341
zones: [Vec<usize>; ZONE_COUNT],
316342
open: [Option<usize>; ZONE_COUNT],
317343
zone_of: Vec<Zone>,
344+
hidden: Vec<bool>,
318345
}
319346

320347
impl Layout {
@@ -323,6 +350,7 @@ impl Layout {
323350
zones: Default::default(),
324351
open: [None; ZONE_COUNT],
325352
zone_of: catalog.iter().map(|item| item.default_zone).collect(),
353+
hidden: vec![false; catalog.len()],
326354
}
327355
}
328356

@@ -341,7 +369,8 @@ impl Layout {
341369
}
342370

343371
/// Keeps the first valid placement of every known item, then appends unplaced items to their
344-
/// default zone in catalog order, then keeps the first listed open panel of every zone.
372+
/// default zone in catalog order, then keeps the first listed open panel of every zone, then
373+
/// hides the known tool stripe items `hidden` names.
345374
fn repaired(catalog: &[CatalogItem], index: &HashMap<&str, usize>, state: &State) -> Self {
346375
let mut layout = Self::empty(catalog);
347376
let mut placed = vec![false; catalog.len()];
@@ -370,6 +399,11 @@ impl Layout {
370399
*slot = Some(item);
371400
}
372401
}
402+
for id in &state.hidden {
403+
if let Some(&item) = index.get(id.as_str()) {
404+
layout.hidden[item] = hideable(&catalog[item]);
405+
}
406+
}
373407
layout
374408
}
375409

@@ -427,14 +461,37 @@ impl Layout {
427461
Ok(())
428462
}
429463

464+
/// Hides or shows the button of `item`. Hiding closes its panel, because an open panel whose
465+
/// button disappears would leave nothing on the stripe to close it with.
466+
fn set_hidden(
467+
&mut self,
468+
catalog: &[CatalogItem],
469+
item: usize,
470+
hidden: bool,
471+
) -> Result<(), RuleError> {
472+
if !hideable(&catalog[item]) {
473+
return Err(invalid("Only tool stripe items can be hidden")
474+
.with_details(catalog[item].id.clone()));
475+
}
476+
self.hidden[item] = hidden;
477+
if hidden && self.is_open(item) {
478+
self.open[self.zone_of[item].rank()] = None;
479+
}
480+
Ok(())
481+
}
482+
430483
fn into_state(self, catalog: &[CatalogItem]) -> State {
431484
let mut placements = Vec::with_capacity(catalog.len());
485+
let mut hidden = Vec::new();
432486
for zone in ZONES {
433487
for &item in &self.zones[zone.rank()] {
434488
placements.push(Placement {
435489
id: catalog[item].id.clone(),
436490
zone,
437491
});
492+
if self.hidden[item] {
493+
hidden.push(catalog[item].id.clone());
494+
}
438495
}
439496
}
440497
let open = self
@@ -443,10 +500,20 @@ impl Layout {
443500
.flatten()
444501
.map(|&item| catalog[item].id.clone())
445502
.collect();
446-
State { placements, open }
503+
State {
504+
placements,
505+
open,
506+
hidden,
507+
}
447508
}
448509
}
449510

511+
/// Only tool stripe buttons can be hidden: the title bar and status bar have no place that
512+
/// lists hidden items.
513+
fn hideable(item: &CatalogItem) -> bool {
514+
item.default_zone.surface() == Surface::ToolStripe
515+
}
516+
450517
#[cfg(test)]
451518
mod tests {
452519
use super::*;
@@ -596,6 +663,7 @@ mod tests {
596663
"terminal".into(),
597664
"notifications".into(),
598665
],
666+
hidden: Vec::new(),
599667
};
600668
let reduction = reduce(&catalog(), saved, Action::Normalize).unwrap();
601669
let state = reduction.state;
@@ -713,6 +781,97 @@ mod tests {
713781
}
714782
}
715783

784+
fn hidden(state: State, id: &str, hidden: bool) -> State {
785+
let action = Action::SetHidden {
786+
id: id.into(),
787+
hidden,
788+
};
789+
reduce(&catalog(), state, action).unwrap().state
790+
}
791+
792+
#[test]
793+
fn hiding_a_button_closes_its_panel_and_keeps_its_place() {
794+
let state = hidden(defaults(), "project", true);
795+
assert_eq!(state.hidden, ["project"]);
796+
assert_eq!(state.open, ["git"]);
797+
assert_eq!(ids(&state, Zone::LeftTop), ["project", "commit"]);
798+
// Showing it again puts the button back in front of commit and leaves the panel closed.
799+
let state = hidden(state, "project", false);
800+
assert_eq!(state, toggled(defaults(), "project"));
801+
}
802+
803+
#[test]
804+
fn a_hidden_item_keeps_its_hidden_state_while_its_panel_opens_and_it_moves() {
805+
let state = hidden(defaults(), "commit", true);
806+
// A shortcut or a command may still open the panel of a hidden item.
807+
let state = toggled(state, "commit");
808+
assert_eq!(state.open, ["commit", "git"]);
809+
assert_eq!(state.hidden, ["commit"]);
810+
let state = moved(state, "commit", Zone::RightBottom, None);
811+
assert_eq!(ids(&state, Zone::RightBottom), ["commit"]);
812+
assert_eq!(state.open, ["git", "commit"]);
813+
assert_eq!(state.hidden, ["commit"]);
814+
// Hiding an item that is already hidden still closes its panel.
815+
let state = hidden(state, "commit", true);
816+
assert_eq!(state.open, ["git"]);
817+
}
818+
819+
#[test]
820+
fn hidden_items_are_listed_in_placement_order_and_reset_shows_them() {
821+
let state = hidden(defaults(), "update", true);
822+
let state = hidden(state, "terminal", true);
823+
let state = hidden(state, "commit", true);
824+
assert_eq!(state.hidden, ["commit", "terminal", "update"]);
825+
let again = reduce(&catalog(), state.clone(), Action::Normalize).unwrap();
826+
assert!(!again.changed);
827+
let reset = reduce(&catalog(), state, Action::Reset).unwrap().state;
828+
assert!(reset.hidden.is_empty());
829+
}
830+
831+
#[test]
832+
fn normalize_drops_unknown_repeated_and_foreign_hidden_ids() {
833+
let saved = State {
834+
hidden: vec![
835+
"gone".into(),
836+
"terminal".into(),
837+
"run".into(),
838+
"caret".into(),
839+
"terminal".into(),
840+
"project".into(),
841+
],
842+
..defaults()
843+
};
844+
let reduction = reduce(&catalog(), saved, Action::Normalize).unwrap();
845+
assert!(reduction.changed);
846+
assert_eq!(reduction.state.hidden, ["project", "terminal"]);
847+
// A saved hidden panel that is open stays open: only hiding it closes it.
848+
assert_eq!(reduction.state.open, ["project", "git"]);
849+
}
850+
851+
#[test]
852+
fn only_tool_stripe_items_can_be_hidden() {
853+
for id in ["run", "caret", "missing"] {
854+
let action = Action::SetHidden {
855+
id: id.into(),
856+
hidden: true,
857+
};
858+
let error = reduce(&catalog(), defaults(), action).unwrap_err();
859+
assert_eq!(error.code, RuleErrorCode::InvalidRequest, "{id}");
860+
}
861+
}
862+
863+
#[test]
864+
fn a_state_saved_before_hiding_existed_decodes_and_answers_hidden() {
865+
let request = serde_json::json!({
866+
"catalog": [{"id": "project", "defaultZone": "leftTop", "panel": true}],
867+
"state": {"placements": [{"id": "project", "zone": "leftTop"}], "open": []},
868+
"action": {"kind": "setHidden", "id": "project", "hidden": true},
869+
});
870+
let answer = reduce_json(request).unwrap();
871+
assert_eq!(answer["state"]["hidden"], serde_json::json!(["project"]));
872+
assert_eq!(answer["changed"], true);
873+
}
874+
716875
#[test]
717876
fn malformed_catalogs_are_rejected() {
718877
for broken in [

‎rust/crates/core/src/protocol/command.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ pub enum CoreCommand {
129129
/// Editor reducers: workbench state, text edits, find, navigation and preferences (`editor.*`).
130130
Editor(EditorCommand),
131131
/// Applies one transition to the rearrangeable workbench chrome: stripe, title bar and status bar
132-
/// button placement and open tool panels (`workbench.chromeLayout.reduce`), from `lithe-core-rules`.
132+
/// button placement, hidden stripe buttons and open tool panels (`workbench.chromeLayout.reduce`),
133+
/// from `lithe-core-rules`.
133134
WorkbenchChromeLayoutReduce,
134135
/// Resolves terminal link candidates into file or external targets (`terminal.parseLinkTarget`).
135136
TerminalParseLinkTarget,

0 commit comments

Comments
 (0)