diff --git a/pkg/config/app_state.go b/pkg/config/app_state.go index 907365e7f..c00bdf8d3 100644 --- a/pkg/config/app_state.go +++ b/pkg/config/app_state.go @@ -245,6 +245,21 @@ type AppState struct { ShellCommandsHistory []string `yaml:"customcommandshistory"` HideCommandLog bool + + // The sizes of the side panels that the user set by dragging the boundary + // between them and the main view, for each layout + SidePanelSizes map[SidePanelLayout][]SidePanelSize +} + +// SidePanelSize is a size of the side panels that the user set by dragging the +// boundary between them and the main view. +type SidePanelSize struct { + // The size of the screen at the time: its width if the side panels are + // left of the main view, otherwise its height without the line at the + // bottom of the screen + ScreenSize int + // The fraction of ScreenSize that the side panels took up + Ratio float64 } // SidePanelLayout names an arrangement of the side panels relative to the main diff --git a/pkg/gui/controllers/helpers/side_panel_sizes.go b/pkg/gui/controllers/helpers/side_panel_sizes.go new file mode 100644 index 000000000..3cae1c1e5 --- /dev/null +++ b/pkg/gui/controllers/helpers/side_panel_sizes.go @@ -0,0 +1,78 @@ +package helpers + +import ( + "math" + "slices" + + "github.com/jesseduffield/lazygit/pkg/config" + "github.com/samber/lo" +) + +// minSideSectionSize is the smallest size of the side section, and of the main +// section, when the user has set the size of the side panels. It leaves room +// for one line or column inside the frame. +const minSideSectionSize = 3 + +// sidePanelSizeForScreen returns the size of the side panels for a screen of the +// given size, based on the sizes that the user set at other screen sizes (sorted +// by ScreenSize). Between two of those screen sizes, it interpolates. Beyond the +// smallest or the largest one, it keeps the ratio of that one. It returns false +// if the user hasn't set any. +func sidePanelSizeForScreen(sizes []config.SidePanelSize, screenSize int) (int, bool) { + if len(sizes) == 0 { + return 0, false + } + + sizeFromRatio := func(ratio float64) int { + return int(math.Round(ratio * float64(screenSize))) + } + first, last := sizes[0], sizes[len(sizes)-1] + if screenSize <= first.ScreenSize { + return sizeFromRatio(first.Ratio), true + } + if screenSize >= last.ScreenSize { + return sizeFromRatio(last.Ratio), true + } + + // We interpolate the size rather than the ratio. Interpolating the ratio + // can result in a size that is outside the range of the two sizes, e.g. + // wider than both of them. + i := slices.IndexFunc(sizes, func(size config.SidePanelSize) bool { + return size.ScreenSize >= screenSize + }) + lower, upper := sizes[i-1], sizes[i] + lowerSize := lower.Ratio * float64(lower.ScreenSize) + upperSize := upper.Ratio * float64(upper.ScreenSize) + t := float64(screenSize-lower.ScreenSize) / float64(upper.ScreenSize-lower.ScreenSize) + return int(math.Round(lowerSize + t*(upperSize-lowerSize))), true +} + +func clampSideSectionSize(size int, screenSize int) int { + return max(minSideSectionSize, min(size, screenSize-minSideSectionSize)) +} + +// ValidSidePanelSizes returns the side panel sizes from state.yml without the +// ones that can't be used (e.g. because the file was edited by hand), sorted by +// screen size as sidePanelSizeForScreen expects them. +func ValidSidePanelSizes( + sizesByLayout map[config.SidePanelLayout][]config.SidePanelSize, +) map[config.SidePanelLayout][]config.SidePanelSize { + result := map[config.SidePanelLayout][]config.SidePanelSize{} + for layout, sizes := range sizesByLayout { + valid := lo.Filter(sizes, func(size config.SidePanelSize, _ int) bool { + return size.ScreenSize > 0 && size.Ratio > 0 && size.Ratio < 1 + }) + slices.SortStableFunc(valid, func(a, b config.SidePanelSize) int { + return a.ScreenSize - b.ScreenSize + }) + // Interpolating between two sizes for the same screen size would divide + // by zero, so keep only the last one. + valid = lo.Filter(valid, func(size config.SidePanelSize, i int) bool { + return i == len(valid)-1 || valid[i+1].ScreenSize != size.ScreenSize + }) + if len(valid) > 0 { + result[layout] = valid + } + } + return result +} diff --git a/pkg/gui/controllers/helpers/side_panel_sizes_test.go b/pkg/gui/controllers/helpers/side_panel_sizes_test.go new file mode 100644 index 000000000..2956e8cf2 --- /dev/null +++ b/pkg/gui/controllers/helpers/side_panel_sizes_test.go @@ -0,0 +1,120 @@ +package helpers + +import ( + "testing" + + "github.com/jesseduffield/lazygit/pkg/config" + "github.com/stretchr/testify/assert" +) + +func TestSidePanelSizeForScreen(t *testing.T) { + laptopAndMonitor := []config.SidePanelSize{ + {ScreenSize: 160, Ratio: 0.3125}, // 50 columns + {ScreenSize: 380, Ratio: 0.2}, // 76 columns + } + + scenarios := []struct { + name string + sizes []config.SidePanelSize + screenSize int + expectedSize int + expectedOk bool + }{ + { + name: "no sizes", + sizes: nil, + screenSize: 100, + expectedOk: false, + }, + { + name: "a single size keeps its ratio at any screen size", + sizes: []config.SidePanelSize{{ScreenSize: 160, Ratio: 0.25}}, + screenSize: 80, + expectedSize: 20, + expectedOk: true, + }, + { + name: "at a screen size that the user set it for", + sizes: laptopAndMonitor, + screenSize: 380, + expectedSize: 76, + expectedOk: true, + }, + { + name: "below the smallest screen size, keeps its ratio", + sizes: laptopAndMonitor, + screenSize: 80, + expectedSize: 25, + expectedOk: true, + }, + { + name: "above the largest screen size, keeps its ratio", + sizes: laptopAndMonitor, + screenSize: 500, + expectedSize: 100, + expectedOk: true, + }, + { + name: "in between, interpolates the size", + sizes: laptopAndMonitor, + screenSize: 270, + expectedSize: 63, + expectedOk: true, + }, + { + name: "in between, interpolates the size rather than the ratio", + sizes: []config.SidePanelSize{ + {ScreenSize: 100, Ratio: 0.5}, // 50 columns + {ScreenSize: 400, Ratio: 0.1}, // 40 columns + }, + screenSize: 250, + expectedSize: 45, + expectedOk: true, + }, + { + name: "picks the two neighboring sizes", + sizes: []config.SidePanelSize{ + {ScreenSize: 100, Ratio: 0.3}, + {ScreenSize: 200, Ratio: 0.3}, + {ScreenSize: 300, Ratio: 0.2}, + }, + screenSize: 250, + expectedSize: 60, + expectedOk: true, + }, + } + + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + size, ok := sidePanelSizeForScreen(s.sizes, s.screenSize) + assert.Equal(t, s.expectedOk, ok) + assert.Equal(t, s.expectedSize, size) + }) + } +} + +func TestValidSidePanelSizes(t *testing.T) { + input := map[config.SidePanelLayout][]config.SidePanelSize{ + config.SidePanelLayoutNormal: { + {ScreenSize: 300, Ratio: 0.2}, + {ScreenSize: 0, Ratio: 0.3}, + {ScreenSize: 100, Ratio: 0.4}, + {ScreenSize: 200, Ratio: 0}, + {ScreenSize: 200, Ratio: 1}, + {ScreenSize: 300, Ratio: 0.25}, + }, + config.SidePanelLayoutHalfScreen: { + {ScreenSize: -1, Ratio: 0.5}, + }, + } + + assert.Equal(t, + map[config.SidePanelLayout][]config.SidePanelSize{ + config.SidePanelLayoutNormal: { + {ScreenSize: 100, Ratio: 0.4}, + {ScreenSize: 300, Ratio: 0.25}, + }, + }, + ValidSidePanelSizes(input), + ) +} diff --git a/pkg/gui/controllers/helpers/window_arrangement_helper.go b/pkg/gui/controllers/helpers/window_arrangement_helper.go index bf55db79e..03a15363a 100644 --- a/pkg/gui/controllers/helpers/window_arrangement_helper.go +++ b/pkg/gui/controllers/helpers/window_arrangement_helper.go @@ -80,6 +80,9 @@ type WindowArrangementArgs struct { InSearchPrompt bool // One of '' (not searching), 'Search: ', and 'Filter: ' SearchPrefix string + // The sizes of the side panels that the user set, for each layout (see + // config.AppState.SidePanelSizes) + SidePanelSizes map[config.SidePanelLayout][]config.SidePanelSize } func (self *WindowArrangementHelper) GetWindowDimensions(informationStr string, appStatus string) map[string]boxlayout.Dimensions { @@ -112,6 +115,7 @@ func (self *WindowArrangementHelper) GetWindowDimensions(informationStr string, IsAnyModeActive: self.modeHelper.IsAnyModeActive(), InSearchPrompt: repoState.InSearchPrompt(), SearchPrefix: searchPrefix, + SidePanelSizes: self.c.State().GetSidePanelSizes(), } return GetWindowDimensions(args) @@ -141,24 +145,29 @@ func GetWindowDimensions(args WindowArrangementArgs) map[string]boxlayout.Dimens sidePanelsDirection = boxlayout.ROW } + sideSectionBox := &boxlayout.Box{ + Direction: boxlayout.ROW, + Weight: sideSectionWeight, + ConditionalChildren: sidePanelChildren(args), + } + mainSectionBox := &boxlayout.Box{ + Direction: boxlayout.ROW, + Weight: mainSectionWeight, + Children: mainPanelChildren(args), + } + if size, ok := sideSectionSizeSetByUser(args); ok { + sideSectionBox.Weight = 0 + sideSectionBox.Size = size + mainSectionBox.Weight = 1 + } + root := &boxlayout.Box{ Direction: boxlayout.ROW, Children: []*boxlayout.Box{ { Direction: sidePanelsDirection, Weight: 1, - Children: []*boxlayout.Box{ - { - Direction: boxlayout.ROW, - Weight: sideSectionWeight, - ConditionalChildren: sidePanelChildren(args), - }, - { - Direction: boxlayout.ROW, - Weight: mainSectionWeight, - Children: mainPanelChildren(args), - }, - }, + Children: []*boxlayout.Box{sideSectionBox, mainSectionBox}, }, { Direction: boxlayout.COLUMN, @@ -254,6 +263,32 @@ func sidePanelLayout(args WindowArrangementArgs) (config.SidePanelLayout, bool) return config.SidePanelLayoutNormal, true } +// sidePanelScreenSize returns the size of the screen that config.SidePanelSize +// refers to. +func sidePanelScreenSize(args WindowArrangementArgs) int { + if shouldUsePortraitMode(args) { + return args.Height - infoSectionSize(args) + } + return args.Width +} + +// sideSectionSizeSetByUser returns the size of the side section if the user has +// set the size of the side panels for the current layout. Otherwise, the weights +// from getMidSectionWeights apply. +func sideSectionSizeSetByUser(args WindowArrangementArgs) (int, bool) { + layout, ok := sidePanelLayout(args) + if !ok { + return 0, false + } + + screenSize := sidePanelScreenSize(args) + size, ok := sidePanelSizeForScreen(args.SidePanelSizes[layout], screenSize) + if !ok { + return 0, false + } + return clampSideSectionSize(size, screenSize), true +} + func isMainWindow(window string) bool { return window == "main" || window == "secondary" } diff --git a/pkg/gui/controllers/helpers/window_arrangement_helper_test.go b/pkg/gui/controllers/helpers/window_arrangement_helper_test.go index 52f85b392..ec2d5a284 100644 --- a/pkg/gui/controllers/helpers/window_arrangement_helper_test.go +++ b/pkg/gui/controllers/helpers/window_arrangement_helper_test.go @@ -439,6 +439,140 @@ func TestGetWindowDimensions(t *testing.T) { B: information `, }, + { + name: "side panel size set by user", + mutateArgs: func(args *WindowArrangementArgs) { + args.SidePanelSizes = map[config.SidePanelLayout][]config.SidePanelSize{ + config.SidePanelLayoutNormal: {{ScreenSize: 75, Ratio: 0.2}}, + } + }, + expected: ` + ╭status───────╮╭main──────────────────────────────────────────────────────╮ + │ ││ │ + ╰─────────────╯│ │ + ╭files────────╮│ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + ╰─────────────╯│ │ + ╭branches─────╮│ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + ╰─────────────╯│ │ + ╭commits──────╮│ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + ╰─────────────╯│ │ + ╭stash────────╮│ │ + │ ││ │ + ╰─────────────╯╰──────────────────────────────────────────────────────────╯ + A + A: statusSpacer1 + B: information + `, + }, + { + name: "side panel size set by user is too small", + mutateArgs: func(args *WindowArrangementArgs) { + args.SidePanelSizes = map[config.SidePanelLayout][]config.SidePanelSize{ + config.SidePanelLayoutNormal: {{ScreenSize: 75, Ratio: 0.01}}, + } + }, + expected: ` + ╭A╮╭main──────────────────────────────────────────────────────────────────╮ + │ ││ │ + ╰─╯│ │ + ╭B╮│ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + ╰─╯│ │ + ╭C╮│ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + ╰─╯│ │ + ╭D╮│ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + │ ││ │ + ╰─╯│ │ + ╭E╮│ │ + │ ││ │ + ╰─╯╰──────────────────────────────────────────────────────────────────────╯ + F + A: status + B: files + C: branches + D: commits + E: stash + F: statusSpacer1 + G: information + `, + }, + { + name: "side panel size set by user in half screen mode, enlargedSideViewLocation top", + mutateArgs: func(args *WindowArrangementArgs) { + args.ScreenMode = types.SCREEN_HALF + args.UserConfig.Gui.EnlargedSideViewLocation = "top" + // The screen size doesn't include the line at the bottom + args.SidePanelSizes = map[config.SidePanelLayout][]config.SidePanelSize{ + config.SidePanelLayoutHalfScreenTop: {{ScreenSize: 29, Ratio: 0.4}}, + } + }, + expected: ` + ╭status───────────────────────────────────────────────────────────────────╮ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + ╰─────────────────────────────────────────────────────────────────────────╯ + ╭main─────────────────────────────────────────────────────────────────────╮ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + │ │ + ╰─────────────────────────────────────────────────────────────────────────╯ + A + A: statusSpacer1 + B: information + `, + }, { name: "half screen mode, enlargedSideViewLocation left", mutateArgs: func(args *WindowArrangementArgs) { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 319bac2c6..85a85399b 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -121,6 +121,10 @@ type Gui struct { // the extras window contains things like the command log ShowExtrasWindow bool + // The sizes of the side panels that the user set, for each layout (see + // config.AppState.SidePanelSizes) + SidePanelSizes map[config.SidePanelLayout][]config.SidePanelSize + PopupHandler types.IPopupHandler // Bumped every time we switch to a different repository (in resetState). @@ -202,6 +206,10 @@ func (self *StateAccessor) SetShowExtrasWindow(value bool) { self.gui.ShowExtrasWindow = value } +func (self *StateAccessor) GetSidePanelSizes() map[config.SidePanelLayout][]config.SidePanelSize { + return self.gui.SidePanelSizes +} + func (self *StateAccessor) GetRetainOriginalDir() bool { return self.gui.RetainOriginalDir } @@ -874,11 +882,17 @@ func NewGui( gui.BackgroundRoutineMgr = &BackgroundRoutineMgr{gui: gui} gui.stateAccessor = &StateAccessor{gui: gui} + gui.loadSidePanelSizes() + gui.diffRendererConfig = config.NewDiffRendererConfigManager(func() *config.UserConfig { return gui.UserConfig() }) return gui, nil } +func (gui *Gui) loadSidePanelSizes() { + gui.SidePanelSizes = helpers.ValidSidePanelSizes(gui.c.ReadAppState().SidePanelSizes) +} + var RuneReplacements = map[rune]string{ // for the commit graph graph.MergeSymbol: "M", diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index 2d2ec134b..a5ab8d888 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -441,6 +441,7 @@ type IStateAccessor interface { SetUpdating(bool) GetShowExtrasWindow() bool SetShowExtrasWindow(bool) + GetSidePanelSizes() map[config.SidePanelLayout][]config.SidePanelSize GetRetainOriginalDir() bool SetRetainOriginalDir(bool) GetItemOperation(item HasUrn) ItemOperation