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.
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
2734use crate :: { RuleError , RuleErrorCode } ;
2835use 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) ]
154162pub 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.
245266pub 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.
314340struct Layout {
315341 zones : [ Vec < usize > ; ZONE_COUNT ] ,
316342 open : [ Option < usize > ; ZONE_COUNT ] ,
317343 zone_of : Vec < Zone > ,
344+ hidden : Vec < bool > ,
318345}
319346
320347impl 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) ]
451518mod 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 [
0 commit comments