From 1a93b2324b5da9a7884406a3a2aa0bdb95398515 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 25 Mar 2025 14:11:52 +0100 Subject: [PATCH] Allow focussing the main view In this commit this is only possible by pressing '0' in a side panel; we'll add mouse clicking later in the branch. Also, you can't really do anything in the focused view except press escape to leave it again. We'll add some more functionality in a following commit. --- pkg/config/user_config.go | 2 + pkg/gui/context/main_context.go | 11 ++-- pkg/gui/controllers.go | 28 +++++++++ pkg/gui/controllers/main_view_controller.go | 63 +++++++++++++++++++ .../switch_to_focused_main_view_controller.go | 49 +++++++++++++++ pkg/i18n/english.go | 4 ++ 6 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 pkg/gui/controllers/main_view_controller.go create mode 100644 pkg/gui/controllers/switch_to_focused_main_view_controller.go diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 165de3e34..49e8e13c8 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -395,6 +395,7 @@ type KeybindingUniversalConfig struct { NextBlockAlt2 string `yaml:"nextBlock-alt2"` PrevBlockAlt2 string `yaml:"prevBlock-alt2"` JumpToBlock []string `yaml:"jumpToBlock"` + FocusMainView string `yaml:"focusMainView"` NextMatch string `yaml:"nextMatch"` PrevMatch string `yaml:"prevMatch"` StartSearch string `yaml:"startSearch"` @@ -876,6 +877,7 @@ func GetDefaultConfig() *UserConfig { PrevBlockAlt2: "", NextBlockAlt2: "", JumpToBlock: []string{"1", "2", "3", "4", "5"}, + FocusMainView: "0", NextMatch: "n", PrevMatch: "N", StartSearch: "/", diff --git a/pkg/gui/context/main_context.go b/pkg/gui/context/main_context.go index 3749d1d1a..7ca6ffdd1 100644 --- a/pkg/gui/context/main_context.go +++ b/pkg/gui/context/main_context.go @@ -17,11 +17,12 @@ func NewMainContext( ctx := &MainContext{ SimpleContext: NewSimpleContext( NewBaseContext(NewBaseContextOpts{ - Kind: types.MAIN_CONTEXT, - View: view, - WindowName: windowName, - Key: key, - Focusable: false, + Kind: types.MAIN_CONTEXT, + View: view, + WindowName: windowName, + Key: key, + Focusable: true, + HighlightOnFocus: false, })), } diff --git a/pkg/gui/controllers.go b/pkg/gui/controllers.go index a6391c6ab..6abd93406 100644 --- a/pkg/gui/controllers.go +++ b/pkg/gui/controllers.go @@ -189,6 +189,8 @@ func (gui *Gui) resetHelpersAndControllers() { patchExplorerControllerFactory := controllers.NewPatchExplorerControllerFactory(common) stagingController := controllers.NewStagingController(common, gui.State.Contexts.Staging, gui.State.Contexts.StagingSecondary, false) stagingSecondaryController := controllers.NewStagingController(common, gui.State.Contexts.StagingSecondary, gui.State.Contexts.Staging, true) + mainViewController := controllers.NewMainViewController(common, gui.State.Contexts.Normal, gui.State.Contexts.NormalSecondary) + secondaryViewController := controllers.NewMainViewController(common, gui.State.Contexts.NormalSecondary, gui.State.Contexts.Normal) patchBuildingController := controllers.NewPatchBuildingController(common) snakeController := controllers.NewSnakeController(common) reflogCommitsController := controllers.NewReflogCommitsController(common) @@ -263,6 +265,22 @@ func (gui *Gui) resetHelpersAndControllers() { )) } + for _, context := range []types.Context{ + gui.State.Contexts.Files, + gui.State.Contexts.Branches, + gui.State.Contexts.RemoteBranches, + gui.State.Contexts.Tags, + gui.State.Contexts.LocalCommits, + gui.State.Contexts.ReflogCommits, + gui.State.Contexts.SubCommits, + gui.State.Contexts.CommitFiles, + gui.State.Contexts.Stash, + } { + controllers.AttachControllers(context, controllers.NewSwitchToFocusedMainViewController( + common, context, + )) + } + for _, context := range []controllers.ContainsCommits{ gui.State.Contexts.LocalCommits, gui.State.Contexts.ReflogCommits, @@ -306,6 +324,16 @@ func (gui *Gui) resetHelpersAndControllers() { mergeConflictsController, ) + controllers.AttachControllers(gui.State.Contexts.Normal, + mainViewController, + verticalScrollControllerFactory.Create(gui.State.Contexts.Normal), + ) + + controllers.AttachControllers(gui.State.Contexts.NormalSecondary, + secondaryViewController, + verticalScrollControllerFactory.Create(gui.State.Contexts.NormalSecondary), + ) + controllers.AttachControllers(gui.State.Contexts.Files, filesController, ) diff --git a/pkg/gui/controllers/main_view_controller.go b/pkg/gui/controllers/main_view_controller.go new file mode 100644 index 000000000..e09cb27c0 --- /dev/null +++ b/pkg/gui/controllers/main_view_controller.go @@ -0,0 +1,63 @@ +package controllers + +import ( + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type MainViewController struct { + baseController + c *ControllerCommon + + context types.Context + otherContext types.Context +} + +var _ types.IController = &MainViewController{} + +func NewMainViewController( + c *ControllerCommon, + context types.Context, + otherContext types.Context, +) *MainViewController { + return &MainViewController{ + baseController: baseController{}, + c: c, + context: context, + otherContext: otherContext, + } +} + +func (self *MainViewController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + return []*types.Binding{ + { + Key: opts.GetKey(opts.Config.Universal.TogglePanel), + Handler: self.togglePanel, + Description: self.c.Tr.ToggleStagingView, + Tooltip: self.c.Tr.ToggleStagingViewTooltip, + DisplayOnScreen: true, + }, + { + Key: opts.GetKey(opts.Config.Universal.Return), + Handler: self.escape, + Description: self.c.Tr.ExitFocusedMainView, + }, + } +} + +func (self *MainViewController) Context() types.Context { + return self.context +} + +func (self *MainViewController) togglePanel() error { + if self.otherContext.GetView().Visible { + self.otherContext.SetParentContext(self.context.GetParentContext()) + self.c.Context().Push(self.otherContext, types.OnFocusOpts{}) + } + + return nil +} + +func (self *MainViewController) escape() error { + self.c.Context().Pop() + return nil +} diff --git a/pkg/gui/controllers/switch_to_focused_main_view_controller.go b/pkg/gui/controllers/switch_to_focused_main_view_controller.go new file mode 100644 index 000000000..f2d48448f --- /dev/null +++ b/pkg/gui/controllers/switch_to_focused_main_view_controller.go @@ -0,0 +1,49 @@ +package controllers + +import ( + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +// This controller is for all contexts that can focus their main view. + +var _ types.IController = &SwitchToFocusedMainViewController{} + +type SwitchToFocusedMainViewController struct { + baseController + c *ControllerCommon + context types.Context +} + +func NewSwitchToFocusedMainViewController( + c *ControllerCommon, + context types.Context, +) *SwitchToFocusedMainViewController { + return &SwitchToFocusedMainViewController{ + baseController: baseController{}, + c: c, + context: context, + } +} + +func (self *SwitchToFocusedMainViewController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + bindings := []*types.Binding{ + { + Key: opts.GetKey(opts.Config.Universal.FocusMainView), + Handler: self.handleFocusMainView, + Description: self.c.Tr.FocusMainView, + }, + } + + return bindings +} + +func (self *SwitchToFocusedMainViewController) Context() types.Context { + return self.context +} + +func (self *SwitchToFocusedMainViewController) handleFocusMainView() error { + mainViewContext := self.c.Helpers().Window.GetContextForWindow("main") + mainViewContext.SetParentContext(self.context) + self.c.Context().Push(mainViewContext, types.OnFocusOpts{}) + return nil +} diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index ab9f7cdf4..53c376293 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -264,6 +264,7 @@ type TranslationSet struct { IgnoreFile string ExcludeFile string RefreshFiles string + FocusMainView string Merge string RegularMerge string MergeBranchTooltip string @@ -508,6 +509,7 @@ type TranslationSet struct { EnterCommitFile string EnterCommitFileTooltip string ExitCustomPatchBuilder string + ExitFocusedMainView string EnterUpstream string InvalidUpstream string ReturnToRemotesList string @@ -1328,6 +1330,7 @@ func EnglishTranslationSet() *TranslationSet { IgnoreFile: `Add to .gitignore`, ExcludeFile: `Add to .git/info/exclude`, RefreshFiles: `Refresh files`, + FocusMainView: "Focus main view", Merge: `Merge`, RegularMerge: "Regular merge", MergeBranchTooltip: "View options for merging the selected item into the current branch (regular merge, squash merge)", @@ -1581,6 +1584,7 @@ func EnglishTranslationSet() *TranslationSet { EnterCommitFile: "Enter file / Toggle directory collapsed", EnterCommitFileTooltip: "If a file is selected, enter the file so that you can add/remove individual lines to the custom patch. If a directory is selected, toggle the directory.", ExitCustomPatchBuilder: `Exit custom patch builder`, + ExitFocusedMainView: "Exit back to side panel", EnterUpstream: `Enter upstream as ' '`, InvalidUpstream: "Invalid upstream. Must be in the format ' '", ReturnToRemotesList: `Return to remotes list`,