Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 37 additions & 13 deletions desktop/internal/core/chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ type Placement struct {
// an empty layout and is a valid input: the kernel completes it from the catalog.
//
// A layout returned by ReduceChromeLayout lists every catalog item exactly once, zone by zone in
// the order of the Zone constants, and Open names at most one panel item per zone, also in zone
// order.
// the order of the Zone constants, Open names at most one panel item per zone, also in zone
// order, and Hidden lists the hidden tool stripe items in the order of Placements.
type ChromeLayout struct {
// Placements is every item with its zone.
// Placements is every item with its zone. A hidden item keeps its placement, so showing it
// again puts it back where it was.
Placements []Placement `json:"placements"`
// Open lists the panel items whose panel is shown.
// Open lists the panel items whose panel is shown. A hidden item's panel can be open: a
// shortcut or command opens it without showing the button.
Open []string `json:"open"`
// Hidden lists the tool stripe items whose button the user hid from the stripe.
Hidden []string `json:"hidden"`
}

// MarshalJSON encodes the layout with empty lists as [] instead of null, because the kernel
Expand All @@ -84,8 +88,9 @@ func (l ChromeLayout) MarshalJSON() ([]byte, error) {
type wire struct {
Placements []Placement `json:"placements"`
Open []string `json:"open"`
Hidden []string `json:"hidden"`
}
return json.Marshal(wire{Placements: nonNil(l.Placements), Open: nonNil(l.Open)})
return json.Marshal(wire{Placements: nonNil(l.Placements), Open: nonNil(l.Open), Hidden: nonNil(l.Hidden)})
}

// chromeKind names the variants of a ChromeAction. The values are the "kind" tags of the Rust
Expand All @@ -98,17 +103,19 @@ const (
chromeMove chromeKind = "move"
chromeToggle chromeKind = "toggle"
chromeSetOpen chromeKind = "setOpen"
chromeSetHidden chromeKind = "setHidden"
)

// ChromeAction is one transition of the chrome layout. Build it with NormalizeChrome,
// ResetChrome, MoveChrome, ToggleChrome or SetChromeOpen; the zero value is not a valid action
// and cannot be encoded.
// ResetChrome, MoveChrome, ToggleChrome, SetChromeOpen or SetChromeHidden; the zero value is not
// a valid action and cannot be encoded.
type ChromeAction struct {
kind chromeKind
id string
zone Zone
before string
open bool
hidden bool
}

// NormalizeChrome repairs a saved layout against the catalog without changing anything else:
Expand All @@ -118,7 +125,8 @@ func NormalizeChrome() ChromeAction {
return ChromeAction{kind: chromeNormalize}
}

// ResetChrome replaces the layout with the default layout of the catalog.
// ResetChrome replaces the layout with the default layout of the catalog, which shows every
// item.
func ResetChrome() ChromeAction {
return ChromeAction{kind: chromeReset}
}
Expand All @@ -142,8 +150,15 @@ func SetChromeOpen(id string, open bool) ChromeAction {
return ChromeAction{kind: chromeSetOpen, id: id, open: open}
}

// chromeKindWire, chromeMoveWire, chromeIDWire and chromeSetOpenWire are the exact JSON shapes
// of the action variants. The kernel rejects unknown fields, so each variant encodes only its
// SetChromeHidden hides or shows the tool stripe button of the item id. Hiding closes the item's
// panel; showing leaves the panel as it is. A hidden item keeps its zone and position. Only tool
// stripe items can be hidden; a title or status bar item fails with CodeInvalidRequest.
func SetChromeHidden(id string, hidden bool) ChromeAction {
return ChromeAction{kind: chromeSetHidden, id: id, hidden: hidden}
}

// chromeKindWire, chromeMoveWire, chromeIDWire, chromeSetOpenWire and chromeSetHiddenWire are
// the exact JSON shapes of the action variants. The kernel rejects unknown fields, so each variant encodes only its
// own fields.
type chromeKindWire struct {
Kind chromeKind `json:"kind"`
Expand All @@ -167,6 +182,12 @@ type chromeSetOpenWire struct {
Open bool `json:"open"`
}

type chromeSetHiddenWire struct {
Kind chromeKind `json:"kind"`
ID string `json:"id"`
Hidden bool `json:"hidden"`
}

// MarshalJSON encodes the action as the exact object of its variant.
func (a ChromeAction) MarshalJSON() ([]byte, error) {
switch a.kind {
Expand All @@ -178,8 +199,10 @@ func (a ChromeAction) MarshalJSON() ([]byte, error) {
return json.Marshal(chromeIDWire{Kind: a.kind, ID: a.id})
case chromeSetOpen:
return json.Marshal(chromeSetOpenWire{Kind: a.kind, ID: a.id, Open: a.open})
case chromeSetHidden:
return json.Marshal(chromeSetHiddenWire{Kind: a.kind, ID: a.id, Hidden: a.hidden})
}
return nil, errors.New("core: zero ChromeAction; build one with NormalizeChrome, ResetChrome, MoveChrome, ToggleChrome or SetChromeOpen")
return nil, errors.New("core: zero ChromeAction; build one with NormalizeChrome, ResetChrome, MoveChrome, ToggleChrome, SetChromeOpen or SetChromeHidden")
}

// chromeLayoutRequest is the payload of workbench.chromeLayout.reduce. The kernel rejects
Expand Down Expand Up @@ -209,8 +232,8 @@ type chromeLayoutResponse struct {
//
// The kernel answers CodeInvalidRequest for a malformed catalog (an empty or repeated id, a
// panel outside the tool stripes, a default-open item that is not a panel) and for an action
// that names an unknown item, moves an item to another surface or opens an item that is not a
// panel.
// that names an unknown item, moves an item to another surface, opens an item that is not a
// panel or hides an item outside the tool stripes.
//
// The call only computes over its arguments and is fast enough for an event handler.
func (c *Client) ReduceChromeLayout(ctx context.Context, catalog []ChromeItem, state ChromeLayout, action ChromeAction) (ChromeLayout, bool, error) {
Expand All @@ -226,5 +249,6 @@ func (c *Client) ReduceChromeLayout(ctx context.Context, catalog []ChromeItem, s
return ChromeLayout{
Placements: nonNil(response.State.Placements),
Open: nonNil(response.State.Open),
Hidden: nonNil(response.State.Hidden),
}, response.Changed, nil
}
30 changes: 28 additions & 2 deletions desktop/internal/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,30 @@ func TestReduceChromeLayout(t *testing.T) {
}
})

// A hidden button keeps its place and its panel closes; showing it again leaves the panel
// closed, and a shortcut can still open the panel of a hidden button.
t.Run("hide and show a stripe button", func(t *testing.T) {
layout, changed := reduce(t, defaults, SetChromeHidden("project", true))
if !changed || !slices.Equal(layout.Hidden, []string{"project"}) || len(layout.Open) != 0 {
t.Fatalf("hide project: hidden %q, open %q, changed %v, want [project], [], true", layout.Hidden, layout.Open, changed)
}
if !slices.Equal(layout.Placements, defaults.Placements) {
t.Errorf("hide project: placements %+v, want them unchanged", layout.Placements)
}
layout, _ = reduce(t, layout, SetChromeOpen("project", true))
if !slices.Equal(layout.Hidden, []string{"project"}) || !slices.Equal(layout.Open, []string{"project"}) {
t.Errorf("open hidden project: hidden %q, open %q, want [project], [project]", layout.Hidden, layout.Open)
}
layout, changed = reduce(t, layout, SetChromeHidden("project", false))
if !changed || len(layout.Hidden) != 0 || layout.Hidden == nil || !slices.Equal(layout.Open, []string{"project"}) {
t.Errorf("show project: hidden %#v, open %q, changed %v, want a non-nil empty list, [project], true", layout.Hidden, layout.Open, changed)
}
_, _, err := client.ReduceChromeLayout(ctx, catalog, defaults, SetChromeHidden("run", true))
requireCode(t, err, CodeInvalidRequest)
_, _, err = client.ReduceChromeLayout(ctx, catalog, defaults, SetChromeHidden("missing", true))
requireCode(t, err, CodeInvalidRequest)
})

t.Run("only known panels open", func(t *testing.T) {
_, _, err := client.ReduceChromeLayout(ctx, catalog, defaults, ToggleChrome("run"))
requireCode(t, err, CodeInvalidRequest)
Expand Down Expand Up @@ -812,8 +836,10 @@ func TestChromeJSON(t *testing.T) {
{"move before", MoveChrome("git", ZoneRightTop, "run"), `{"kind":"move","id":"git","zone":"rightTop","before":"run"}`},
{"toggle", ToggleChrome("git"), `{"kind":"toggle","id":"git"}`},
{"set open", SetChromeOpen("git", false), `{"kind":"setOpen","id":"git","open":false}`},
{"empty layout", ChromeLayout{}, `{"placements":[],"open":[]}`},
{"layout", ChromeLayout{Placements: []Placement{{ID: "a", Zone: ZoneLeftTop}}, Open: []string{"a"}}, `{"placements":[{"id":"a","zone":"leftTop"}],"open":["a"]}`},
{"hide", SetChromeHidden("git", true), `{"kind":"setHidden","id":"git","hidden":true}`},
{"show", SetChromeHidden("git", false), `{"kind":"setHidden","id":"git","hidden":false}`},
{"empty layout", ChromeLayout{}, `{"placements":[],"open":[],"hidden":[]}`},
{"layout", ChromeLayout{Placements: []Placement{{ID: "a", Zone: ZoneLeftTop}}, Open: []string{"a"}, Hidden: []string{"a"}}, `{"placements":[{"id":"a","zone":"leftTop"}],"open":["a"],"hidden":["a"]}`},
{"plain item", ChromeItem{ID: "a", DefaultZone: ZoneLeftTop}, `{"id":"a","defaultZone":"leftTop"}`},
{"panel item", ChromeItem{ID: "a", DefaultZone: ZoneLeftTop, Panel: true, OpenByDefault: true}, `{"id":"a","defaultZone":"leftTop","panel":true,"openByDefault":true}`},
}
Expand Down
6 changes: 6 additions & 0 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions desktop/internal/workbench/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,9 @@ type App struct {
// is whether this workbench's stripe shows its popover (workbench_background.go).
backdrop *workbenchBackground
backdropOpen bool
// hiddenListOpen is whether the "…" button under the left stripe's top group shows the
// list of the hidden stripe buttons (chrome_view.go hiddenToolWindows).
hiddenListOpen bool
// shownTitle is the system title last given to the window (window_title.go).
shownTitle string
// updates is the update check, download and install, which every tab of the window
Expand Down Expand Up @@ -964,6 +967,36 @@ func (a *App) saveSettings() {
// resetLayout puts every rearrangeable button back where the catalog puts it.
func (a *App) resetLayout() { a.reduceChrome(core.ResetChrome()) }

// hideEntry takes the stripe button of catalog item index off its stripe. The kernel keeps its
// zone and position and closes its tool window; the background button's popover closes with
// it, because it hangs off the button.
func (a *App) hideEntry(index int) {
id := a.chrome.catalog[index].id
if id == itemBackground {
a.backdropOpen = false
}
a.reduceChrome(core.SetChromeHidden(id, true))
}

// showEntry puts the hidden stripe button of catalog item index back where it was and, when
// open is set, opens what the button opens, as a click on it does: its tool window, or the
// background button's popover. A tool window that is already open, from a shortcut, stays
// open.
func (a *App) showEntry(index int, open bool) {
it := a.chrome.catalog[index]
a.reduceChrome(core.SetChromeHidden(it.id, false))
if !open || a.chrome.isHidden(index) {
// The kernel refused, which reduceChrome reported.
return
}
switch {
case it.id == itemBackground:
a.backdropOpen = true
case it.panel && !a.chrome.isOpen(index):
a.activate(index)
}
}

// activate is a click on the rearrangeable button of catalog item index: a tool
// button opens or closes its tool window, the settings button opens or closes its menu
// and the commands that need a provider that is not connected yet say so.
Expand Down
Loading
Loading