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
24 changes: 24 additions & 0 deletions desktop/internal/settingswindow/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type Values struct {
Theme string
// MarketplaceWidth is a layout preference, applied on completed resize, not a draft setting.
MarketplaceWidth float32
// WindowWidth and WindowHeight are the size the window opens at, the one the user last
// gave it (Options.WindowResized); zero opens it at its default size. Like
// MarketplaceWidth they are a layout preference, not a draft setting.
WindowWidth, WindowHeight int
// EditorTabRows is TabRowsMultiple when an editor group's tabs wrap onto more rows; empty
// keeps them in one row. EditorTabOverflow says what one row does with tabs that do not
// fit: TabOverflowSqueeze makes them narrower, and empty scrolls them sideways.
Expand Down Expand Up @@ -406,6 +410,12 @@ type Options struct {
MarketplaceRejected func(int)
// MarketplaceResized persists a completed splitter gesture or keyboard step.
MarketplaceResized func(float32)
// WindowResized keeps the size the user gave the window, for Values.WindowWidth and
// WindowHeight of the next one. It runs once, on the settings window's thread, when the
// window closes at a size other than the one it opened at, so it must hand the size to
// its owner. An owner that closes the window as it closes itself takes the size from
// Controller.ResizedWindow first, because this report comes after the close.
WindowResized func(width, height int)
// PluginState reports what the plugin page shows about one plugin. It is read
// while painting, so it must answer from memory rather than from disk. Nil
// treats every plugin as not installed and hides the plugin actions.
Expand Down Expand Up @@ -740,6 +750,20 @@ func (c *Controller) Close() {
}
}

// ResizedWindow is the size the user gave the open window since it opened, with ok false
// when no window is open or it still has the size it opened at. It is for an owner that
// closes the window as it closes itself: Options.WindowResized reports the size only after
// the close. It is safe from any goroutine.
func (c *Controller) ResizedWindow() (width, height int, ok bool) {
c.mu.Lock()
defer c.mu.Unlock()
if c.current == nil {
return 0, 0, false
}
size, resized := c.current.resized()
return size.width, size.height, resized
}

// Invalidate repaints the open settings window after shared application state changes.
func (c *Controller) Invalidate() {
c.mu.Lock()
Expand Down
6 changes: 3 additions & 3 deletions desktop/internal/settingswindow/ui_baseline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func TestBaselineSettingsControlsFitReferenceWindow(t *testing.T) {
w := newWindow(NewController(Options{}), Values{Theme: "dark", Language: "en"})
d := openWindow(t, w)
shot := d.driver.Snapshot()
if shot.Width != 900 || shot.Height != 700 {
t.Fatalf("initial window=%dx%d, want 900x700", shot.Width, shot.Height)
if shot.Width != 1270 || shot.Height != 700 {
t.Fatalf("initial window=%dx%d, want 1270x700", shot.Width, shot.Height)
}
for _, label := range []string{"Search settings", "Language", "Cancel", "OK"} {
hit := d.hit(label)
if hit.Bounds.X < 0 || hit.Bounds.Y < 0 || hit.Bounds.X+hit.Bounds.Width > 900 || hit.Bounds.Y+hit.Bounds.Height > 700 {
if hit.Bounds.X < 0 || hit.Bounds.Y < 0 || hit.Bounds.X+hit.Bounds.Width > 1270 || hit.Bounds.Y+hit.Bounds.Height > 700 {
t.Fatalf("%s outside reference window: %v", label, hit.Bounds)
}
}
Expand Down
61 changes: 56 additions & 5 deletions desktop/internal/settingswindow/window.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package settingswindow

import (
"math"
"sync"
"sync/atomic"

Expand All @@ -16,7 +17,9 @@ import (

// The metrics of the window, after the macOS client's settings window and the log window.
const (
windowWidth, windowHeight = 900, 700
// windowWidth and windowHeight are the size the window opens at until the user gives it
// another, which it then keeps (Values.WindowWidth, Options.WindowResized).
windowWidth, windowHeight = 1270, 700
windowMinWidth = 900
windowMinHeight = 668

Expand Down Expand Up @@ -89,6 +92,12 @@ type window struct {
win *app.Window
stopWatch func()
gone bool
// openedSize is the size the window opened at and size the size of its content in the last
// frame the system reported, which is kept when it differs (closed, Controller.ResizedWindow).
// size is guarded by ctrl.mu too: the window's thread sets it, and the owner may read it
// from its own thread.
openedSize windowSize
size windowSize

inv app.Invalidator
page *ui.Page
Expand Down Expand Up @@ -243,9 +252,25 @@ type labelsKey struct {
systemDark bool
}

// windowSize is the size of a window's content area, in logical pixels.
type windowSize struct {
width, height int
}

// openingSize is the size a window opens at: the one the user last gave it, or the default
// when none is kept, and never under the smallest.
func openingSize(width, height int) windowSize {
if width <= 0 || height <= 0 {
width, height = windowWidth, windowHeight
}
return windowSize{width: max(width, windowMinWidth), height: max(height, windowMinHeight)}
}

// newWindow returns a window whose values start from the settings in force.
func newWindow(c *Controller, current Values) *window {
w := &window{ctrl: c, sess: newSession(current)}
w.openedSize = openingSize(current.WindowWidth, current.WindowHeight)
w.size = w.openedSize
width := current.MarketplaceWidth
if width <= 0 {
width = marketListWidth
Expand Down Expand Up @@ -288,15 +313,16 @@ func (w *window) config() app.Config {
config := ui.Configure(w.page, app.Config{
Title: w.ctrl.text().Settings.Title,
Icon: appicon.Window(),
Width: windowWidth,
Height: windowHeight,
Width: w.openedSize.width,
Height: w.openedSize.height,
MinWidth: windowMinWidth,
MinHeight: windowMinHeight,
WindowMode: app.WindowCustomTitlebar,
TitlebarHeight: titleHeight,
WindowControls: func() app.WindowControlsStyle { return windowcontrols.Style(w.pal.text, w.ctrl.text()) },
Invalidator: &w.inv,
OnWake: w.wake,
OnFrameChange: w.framed,
OnClosed: w.closed,
// Chords of printable keys reach the window as shortcuts, not as keys of the
// control that has the focus; the one the Keymap page records arrives here.
Expand Down Expand Up @@ -398,9 +424,30 @@ func (w *window) native() *app.Window {
return w.win
}

// framed takes the size of the window's content from a frame the system reports. It is the
// window's own size: Viva reports no frame while the window is full screen, maximized or
// minimized. A live resize reports every step, so the size is only noted here and kept when
// the window closes, never written per frame.
func (w *window) framed(frame gfx.Rect) {
size := windowSize{width: int(math.Round(float64(frame.Width))), height: int(math.Round(float64(frame.Height)))}
if size.width <= 0 || size.height <= 0 {
return
}
w.ctrl.mu.Lock()
w.size = size
w.ctrl.mu.Unlock()
}

// resized is the size the user gave the window since it opened, if any. The caller holds
// ctrl.mu.
func (w *window) resized() (windowSize, bool) {
return w.size, w.size != w.openedSize
}

// closed runs once the native window is gone, however it was closed: the session is over,
// the system look is no longer watched and the controller forgets the window, so the next
// Open makes a new one. The Page has cancelled its effects by then.
// the system look is no longer watched, the controller forgets the window, so the next Open
// makes a new one, and a size the user gave the window is kept for that one. The Page has
// cancelled its effects by then.
func (w *window) closed() {
w.closeRegion()
w.dismissPluginInstall()
Expand All @@ -416,10 +463,14 @@ func (w *window) closed() {
if c.current == w {
c.current = nil
}
size, resized := w.resized()
c.mu.Unlock()
if stop != nil {
stop()
}
if resized && c.opts.WindowResized != nil {
c.opts.WindowResized(size.width, size.height)
}
}

// accept is OK: it writes the Build pages' drafts, then closes the window (build_apply.go).
Expand Down
114 changes: 114 additions & 0 deletions desktop/internal/settingswindow/window_size_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package settingswindow

import (
"testing"
)

// TestWindowOpensAtTheSizeTheUserLastGaveIt covers where a new window takes its size: the one
// kept from the last window, the default 1270×700 when none is kept or the kept one is
// unusable, and never under the smallest.
func TestWindowOpensAtTheSizeTheUserLastGaveIt(t *testing.T) {
for _, test := range []struct {
name string
width, height int
want windowSize
}{
{"none kept", 0, 0, windowSize{1270, 700}},
{"kept", 1100, 760, windowSize{1100, 760}},
{"one side missing", 1100, 0, windowSize{windowWidth, windowHeight}},
{"negative", -1, 760, windowSize{windowWidth, windowHeight}},
{"under the smallest", 640, 480, windowSize{windowMinWidth, windowMinHeight}},
} {
t.Run(test.name, func(t *testing.T) {
w := newWindow(NewController(Options{}), Values{WindowWidth: test.width, WindowHeight: test.height})
d := openWindow(t, w)
shot := d.driver.Snapshot()
if shot.Width != test.want.width || shot.Height != test.want.height {
t.Fatalf("window opened at %dx%d, want %dx%d", shot.Width, shot.Height, test.want.width, test.want.height)
}
})
}
}

// TestClosingKeepsTheSizeTheUserGaveTheWindow covers the report a closing window makes: the
// last size of a live resize, once, and nothing when the window was only moved or ends at the
// size it opened at.
func TestClosingKeepsTheSizeTheUserGaveTheWindow(t *testing.T) {
type report struct{ width, height, count int }
open := func(t *testing.T, current Values) (*windowDriver, *report) {
t.Helper()
got := &report{}
c := NewController(Options{WindowResized: func(width, height int) {
got.width, got.height = width, height
got.count++
}})
return openWindow(t, newWindow(c, current)), got
}

t.Run("resized", func(t *testing.T) {
d, got := open(t, Values{})
for _, size := range [][2]int{{1000, 690}, {1100, 760}, {1180, 820}} {
if err := d.driver.Resize(size[0], size[1]); err != nil {
t.Fatal(err)
}
}
if got.count != 0 {
t.Fatalf("the window reported its size %d times while it was being resized", got.count)
}
d.driver.Close()
if got.count != 1 || got.width != 1180 || got.height != 820 {
t.Fatalf("closing reported %+v, want 1180x820 once", *got)
}
})
t.Run("moved only", func(t *testing.T) {
d, got := open(t, Values{WindowWidth: 1100, WindowHeight: 760})
if err := d.driver.MoveWindow(40, 60); err != nil {
t.Fatal(err)
}
d.driver.Close()
if got.count != 0 {
t.Fatalf("a window that was only moved reported its size: %+v", *got)
}
})
t.Run("back to where it opened", func(t *testing.T) {
d, got := open(t, Values{WindowWidth: 1100, WindowHeight: 760})
if err := d.driver.Resize(1000, 700); err != nil {
t.Fatal(err)
}
if err := d.driver.Resize(1100, 760); err != nil {
t.Fatal(err)
}
d.driver.Close()
if got.count != 0 {
t.Fatalf("a window back at the size it opened at reported it: %+v", *got)
}
})
}

// TestResizedWindowGivesTheSizeBeforeTheWindowCloses covers the owner that closes the window
// as it closes itself: it reads the size the user gave the open window before the close,
// because the window's own report comes after it.
func TestResizedWindowGivesTheSizeBeforeTheWindowCloses(t *testing.T) {
c := NewController(Options{})
if _, _, ok := c.ResizedWindow(); ok {
t.Fatal("a controller with no window reported a size")
}
w := newWindow(c, Values{})
d := openWindow(t, w)
c.mu.Lock()
c.current = w
c.mu.Unlock()
if _, _, ok := c.ResizedWindow(); ok {
t.Fatal("a window at the size it opened at reported a size")
}
if err := d.driver.Resize(1150, 780); err != nil {
t.Fatal(err)
}
if width, height, ok := c.ResizedWindow(); !ok || width != 1150 || height != 780 {
t.Fatalf("ResizedWindow() = %d, %d, %v, want 1150, 780, true", width, height, ok)
}
d.driver.Close()
if _, _, ok := c.ResizedWindow(); ok {
t.Fatal("a closed window still reported a size")
}
}
6 changes: 5 additions & 1 deletion desktop/internal/workbench/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ type settings struct {
Language string `json:"language,omitempty"`
Theme themeChoice `json:"theme,omitempty"`
MarketplaceWidth float32 `json:"marketplaceWidth,omitempty"`
// SettingsWindowWidth and SettingsWindowHeight are the size the user last gave the settings
// window, which the next one opens at; zero opens it at its default size.
SettingsWindowWidth int `json:"settingsWindowWidth,omitempty"`
SettingsWindowHeight int `json:"settingsWindowHeight,omitempty"`
// MarketplaceSources are the custom plugin index URLs the user added, in the order they were added.
MarketplaceSources []string `json:"marketplaceSources,omitempty"`
// AutoUpdatePlugins lets approved marketplace plugins update with unchanged grants.
Expand Down Expand Up @@ -1183,7 +1187,7 @@ func (a *App) closed() {
a.closeClone()
a.closeNewProject()
a.closeProject()
a.settingsWindow.Close()
a.closeSettingsWindow()
a.stopUpdates()
}

Expand Down
29 changes: 29 additions & 0 deletions desktop/internal/workbench/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,35 @@ func TestMarketplaceWidthPersistsWithOtherSettings(t *testing.T) {
}
}

// TestSettingsWindowSizePersistsWithOtherSettings covers the size the user gave the settings
// window: it is saved with the other settings, only when it changes, and the next workbench
// reads it back for the settings window it opens.
func TestSettingsWindowSizePersistsWithOtherSettings(t *testing.T) {
h := newHarness(t, &testKernel{Client: core.NewClient()})
h.app.setLanguage("zh-Hans")
h.app.setSettingsWindowSize(1100, 760)
var saved settings
found, err := h.store.Load(settingsFile, &saved)
if err != nil || !found {
t.Fatalf("load saved settings: found=%v err=%v", found, err)
}
if saved.SettingsWindowWidth != 1100 || saved.SettingsWindowHeight != 760 || saved.Language != "zh-Hans" || saved.Theme != themeDark {
t.Fatalf("saved settings lost values: %+v", saved)
}
if err := os.Remove(filepath.Join(h.store.Dir(), settingsFile)); err != nil {
t.Fatal(err)
}
h.app.setSettingsWindowSize(1100, 760)
if _, err := os.Stat(filepath.Join(h.store.Dir(), settingsFile)); !os.IsNotExist(err) {
t.Fatalf("an unchanged size saved the settings again: %v", err)
}
h.app.setSettingsWindowSize(1180, 820)
reloaded := newHarnessWithOptions(t, Options{Kernel: &testKernel{Client: core.NewClient()}, Store: h.store, Languages: []string{"zh-Hans"}})
if got := reloaded.app.settings; got.SettingsWindowWidth != 1180 || got.SettingsWindowHeight != 820 {
t.Fatalf("reloaded settings window size = %dx%d, want 1180x820", got.SettingsWindowWidth, got.SettingsWindowHeight)
}
}

// TestWindowControlsFollowTheTitleBar covers the buttons Viva draws in the custom title bar on
// Windows and Linux: unstyled, they took Viva's ink for its default dark clear color and were
// white on the light title bar. They must take the ink of the title bar and the language of
Expand Down
Loading
Loading