1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00

move more actions into controller

This commit is contained in:
Jesse Duffield 2023-03-23 22:05:25 +11:00
parent 71753770ad
commit f8c9ce33c2
12 changed files with 160 additions and 123 deletions

View File

@ -18,13 +18,20 @@ type ContextMgr struct {
ContextStack []types.Context
sync.RWMutex
gui *Gui
allContexts *context.ContextTree
}
func NewContextMgr(initialContext types.Context, gui *Gui) *ContextMgr {
func NewContextMgr(
initialContext types.Context,
gui *Gui,
allContexts *context.ContextTree,
) *ContextMgr {
return &ContextMgr{
ContextStack: []types.Context{initialContext},
RWMutex: sync.RWMutex{},
gui: gui,
allContexts: allContexts,
}
}
@ -286,3 +293,28 @@ func (self *ContextMgr) ForEach(f func(types.Context)) {
func (self *ContextMgr) IsCurrent(c types.Context) bool {
return self.Current().GetKey() == c.GetKey()
}
// all list contexts
func (self *ContextMgr) AllList() []types.IListContext {
var listContexts []types.IListContext
for _, context := range self.allContexts.Flatten() {
if listContext, ok := context.(types.IListContext); ok {
listContexts = append(listContexts, listContext)
}
}
return listContexts
}
func (self *ContextMgr) AllPatchExplorer() []types.IPatchExplorerContext {
var listContexts []types.IPatchExplorerContext
for _, context := range self.allContexts.Flatten() {
if listContext, ok := context.(types.IPatchExplorerContext); ok {
listContexts = append(listContexts, listContext)
}
}
return listContexts
}

View File

@ -15,6 +15,8 @@ type ListContextTrait struct {
getDisplayStrings func(startIdx int, length int) [][]string
}
func (self *ListContextTrait) IsListContext() {}
func (self *ListContextTrait) GetList() types.IList {
return self.list
}

View File

@ -45,6 +45,8 @@ func NewPatchExplorerContext(
}
}
func (self *PatchExplorerContext) IsPatchExplorerContext() {}
func (self *PatchExplorerContext) GetState() *patch_exploring.State {
return self.state
}

View File

@ -45,29 +45,3 @@ func (gui *Gui) TransientContexts() []types.Context {
return context.IsTransient()
})
}
func (gui *Gui) getListContexts() []types.IListContext {
return []types.IListContext{
gui.State.Contexts.Menu,
gui.State.Contexts.Files,
gui.State.Contexts.Branches,
gui.State.Contexts.Remotes,
gui.State.Contexts.RemoteBranches,
gui.State.Contexts.Tags,
gui.State.Contexts.LocalCommits,
gui.State.Contexts.ReflogCommits,
gui.State.Contexts.SubCommits,
gui.State.Contexts.Stash,
gui.State.Contexts.CommitFiles,
gui.State.Contexts.Submodules,
gui.State.Contexts.Suggestions,
}
}
func (gui *Gui) getPatchExplorerContexts() []types.IPatchExplorerContext {
return []types.IPatchExplorerContext{
gui.State.Contexts.Staging,
gui.State.Contexts.StagingSecondary,
gui.State.Contexts.CustomPatchBuilder,
}
}

View File

@ -314,7 +314,7 @@ func (gui *Gui) resetControllers() {
// this must come last so that we've got our click handlers defined against the context
listControllerFactory := controllers.NewListControllerFactory(common)
for _, context := range gui.getListContexts() {
for _, context := range gui.c.Context().AllList() {
controllers.AttachControllers(context, listControllerFactory.Create(context))
}
}

View File

@ -31,9 +31,34 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type
Description: self.c.Tr.ViewPatchOptions,
OpensMenu: true,
},
{
Key: opts.GetKey(opts.Config.Universal.CreateRebaseOptionsMenu),
Handler: self.c.Helpers().MergeAndRebase.CreateRebaseOptionsMenu,
Description: self.c.Tr.ViewMergeRebaseOptions,
OpensMenu: true,
},
{
Key: opts.GetKey(opts.Config.Universal.Refresh),
Handler: self.refresh,
Description: self.c.Tr.LcRefresh,
},
{
Key: opts.GetKey(opts.Config.Universal.NextScreenMode),
Handler: self.nextScreenMode,
Description: self.c.Tr.LcNextScreenMode,
},
{
Key: opts.GetKey(opts.Config.Universal.PrevScreenMode),
Handler: self.prevScreenMode,
Description: self.c.Tr.LcPrevScreenMode,
},
}
}
func (self *GlobalController) Context() types.Context {
return nil
}
func (self *GlobalController) customCommand() error {
return (&CustomCommandAction{c: self.c}).Call()
}
@ -42,6 +67,14 @@ func (self *GlobalController) createCustomPatchOptionsMenu() error {
return (&CustomPatchOptionsMenuAction{c: self.c}).Call()
}
func (self *GlobalController) Context() types.Context {
return nil
func (self *GlobalController) refresh() error {
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
}
func (self *GlobalController) nextScreenMode() error {
return (&ScreenModeActions{c: self.c}).Next()
}
func (self *GlobalController) prevScreenMode() error {
return (&ScreenModeActions{c: self.c}).Prev()
}

View File

@ -0,0 +1,79 @@
package controllers
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type ScreenModeActions struct {
c *ControllerCommon
}
func (self *ScreenModeActions) Next() error {
self.c.State().GetRepoState().SetScreenMode(
nextIntInCycle(
[]types.WindowMaximisation{types.SCREEN_NORMAL, types.SCREEN_HALF, types.SCREEN_FULL},
self.c.State().GetRepoState().GetScreenMode(),
),
)
return self.rerenderViewsWithScreenModeDependentContent()
}
func (self *ScreenModeActions) Prev() error {
self.c.State().GetRepoState().SetScreenMode(
prevIntInCycle(
[]types.WindowMaximisation{types.SCREEN_NORMAL, types.SCREEN_HALF, types.SCREEN_FULL},
self.c.State().GetRepoState().GetScreenMode(),
),
)
return self.rerenderViewsWithScreenModeDependentContent()
}
// these views need to be re-rendered when the screen mode changes. The commits view,
// for example, will show authorship information in half and full screen mode.
func (self *ScreenModeActions) rerenderViewsWithScreenModeDependentContent() error {
// for now we re-render all list views.
for _, context := range self.c.Context().AllList() {
if err := self.rerenderView(context.GetView()); err != nil {
return err
}
}
return nil
}
func (self *ScreenModeActions) rerenderView(view *gocui.View) error {
context, ok := self.c.Helpers().View.ContextForView(view.Name())
if !ok {
self.c.Log.Errorf("no context found for view %s", view.Name())
return nil
}
return context.HandleRender()
}
func nextIntInCycle(sl []types.WindowMaximisation, current types.WindowMaximisation) types.WindowMaximisation {
for i, val := range sl {
if val == current {
if i == len(sl)-1 {
return sl[0]
}
return sl[i+1]
}
}
return sl[0]
}
func prevIntInCycle(sl []types.WindowMaximisation, current types.WindowMaximisation) types.WindowMaximisation {
for i, val := range sl {
if val == current {
if i > 0 {
return sl[i-1]
}
return sl[len(sl)-1]
}
}
return sl[len(sl)-1]
}

View File

@ -11,55 +11,6 @@ import (
const HORIZONTAL_SCROLL_FACTOR = 3
// these views need to be re-rendered when the screen mode changes. The commits view,
// for example, will show authorship information in half and full screen mode.
func (gui *Gui) rerenderViewsWithScreenModeDependentContent() error {
// for now we re-render all list views.
for _, context := range gui.getListContexts() {
if err := gui.rerenderView(context.GetView()); err != nil {
return err
}
}
return nil
}
func nextIntInCycle(sl []types.WindowMaximisation, current types.WindowMaximisation) types.WindowMaximisation {
for i, val := range sl {
if val == current {
if i == len(sl)-1 {
return sl[0]
}
return sl[i+1]
}
}
return sl[0]
}
func prevIntInCycle(sl []types.WindowMaximisation, current types.WindowMaximisation) types.WindowMaximisation {
for i, val := range sl {
if val == current {
if i > 0 {
return sl[i-1]
}
return sl[len(sl)-1]
}
}
return sl[len(sl)-1]
}
func (gui *Gui) nextScreenMode() error {
gui.State.ScreenMode = nextIntInCycle([]types.WindowMaximisation{types.SCREEN_NORMAL, types.SCREEN_HALF, types.SCREEN_FULL}, gui.State.ScreenMode)
return gui.rerenderViewsWithScreenModeDependentContent()
}
func (gui *Gui) prevScreenMode() error {
gui.State.ScreenMode = prevIntInCycle([]types.WindowMaximisation{types.SCREEN_NORMAL, types.SCREEN_HALF, types.SCREEN_FULL}, gui.State.ScreenMode)
return gui.rerenderViewsWithScreenModeDependentContent()
}
func (gui *Gui) scrollUpView(view *gocui.View) {
view.ScrollUp(gui.c.UserConfig.Gui.ScrollHeight)
}
@ -157,10 +108,6 @@ func (gui *Gui) scrollDownConfirmationPanel() error {
return nil
}
func (gui *Gui) handleRefresh() error {
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
}
func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error {
// important to note that this assumes we've selected an item in a side context
currentSideContext := gui.c.CurrentSideContext()
@ -190,13 +137,3 @@ func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error {
return nil
}
func (gui *Gui) rerenderView(view *gocui.View) error {
context, ok := gui.helpers.View.ContextForView(view.Name())
if !ok {
gui.Log.Errorf("no context found for view %s", view.Name())
return nil
}
return context.HandleRender()
}

View File

@ -347,8 +347,8 @@ func (gui *Gui) resetState(startArgs appTypes.StartArgs, reuseState bool) {
Diffing: diffing.New(),
},
ScreenMode: initialScreenMode,
// TODO: put contexts in the context manager
ContextMgr: NewContextMgr(initialContext, gui),
// TODO: only use contexts from context manager
ContextMgr: NewContextMgr(initialContext, gui, contextTree),
Contexts: contextTree,
WindowViewNameMap: initialWindowViewNameMap(contextTree),
}

View File

@ -134,19 +134,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
Modifier: gocui.ModNone,
Handler: self.scrollDownMain,
},
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.CreateRebaseOptionsMenu),
Handler: self.helpers.MergeAndRebase.CreateRebaseOptionsMenu,
Description: self.c.Tr.ViewMergeRebaseOptions,
OpensMenu: true,
},
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.Refresh),
Handler: self.handleRefresh,
Description: self.c.Tr.LcRefresh,
},
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.OptionMenu),
@ -162,19 +149,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
Description: self.c.Tr.LcOpenMenu,
Handler: self.handleCreateOptionsMenu,
},
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.NextScreenMode),
Handler: self.nextScreenMode,
Description: self.c.Tr.LcNextScreenMode,
},
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.PrevScreenMode),
Handler: self.prevScreenMode,
Description: self.c.Tr.LcPrevScreenMode,
},
{
ViewName: "files",
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),

View File

@ -124,7 +124,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
gui.State.ViewsSetup = true
}
for _, listContext := range gui.getListContexts() {
for _, listContext := range gui.c.Context().AllList() {
view, err := gui.g.View(listContext.GetViewName())
if err != nil {
continue
@ -138,7 +138,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
view.SetOnSelectItem(gui.onSelectItemWrapper(listContext.OnSearchSelect))
}
for _, context := range gui.getPatchExplorerContexts() {
for _, context := range gui.c.Context().AllPatchExplorer() {
context := context
context.GetView().SetOnSelectItem(gui.onSelectItemWrapper(
func(selectedLineIdx int) error {

View File

@ -106,6 +106,7 @@ type IListContext interface {
OnSearchSelect(selectedLineIdx int) error
FocusLine()
IsListContext() // used for type switch
}
type IPatchExplorerContext interface {
@ -120,6 +121,7 @@ type IPatchExplorerContext interface {
GetContentToRender(isFocused bool) string
NavigateTo(isFocused bool, selectedLineIdx int) error
GetMutex() *deadlock.Mutex
IsPatchExplorerContext() // used for type switch
}
type IViewTrait interface {
@ -208,4 +210,6 @@ type IContextMgr interface {
CurrentSide() Context
IsCurrent(c Context) bool
ForEach(func(Context))
AllList() []IListContext
AllPatchExplorer() []IPatchExplorerContext
}