mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-25 12:24:47 +02:00
move more actions into controller
This commit is contained in:
parent
71753770ad
commit
f8c9ce33c2
@ -18,13 +18,20 @@ type ContextMgr struct {
|
|||||||
ContextStack []types.Context
|
ContextStack []types.Context
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
gui *Gui
|
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{
|
return &ContextMgr{
|
||||||
ContextStack: []types.Context{initialContext},
|
ContextStack: []types.Context{initialContext},
|
||||||
RWMutex: sync.RWMutex{},
|
RWMutex: sync.RWMutex{},
|
||||||
gui: gui,
|
gui: gui,
|
||||||
|
allContexts: allContexts,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -286,3 +293,28 @@ func (self *ContextMgr) ForEach(f func(types.Context)) {
|
|||||||
func (self *ContextMgr) IsCurrent(c types.Context) bool {
|
func (self *ContextMgr) IsCurrent(c types.Context) bool {
|
||||||
return self.Current().GetKey() == c.GetKey()
|
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
|
||||||
|
}
|
||||||
|
@ -15,6 +15,8 @@ type ListContextTrait struct {
|
|||||||
getDisplayStrings func(startIdx int, length int) [][]string
|
getDisplayStrings func(startIdx int, length int) [][]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *ListContextTrait) IsListContext() {}
|
||||||
|
|
||||||
func (self *ListContextTrait) GetList() types.IList {
|
func (self *ListContextTrait) GetList() types.IList {
|
||||||
return self.list
|
return self.list
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,8 @@ func NewPatchExplorerContext(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *PatchExplorerContext) IsPatchExplorerContext() {}
|
||||||
|
|
||||||
func (self *PatchExplorerContext) GetState() *patch_exploring.State {
|
func (self *PatchExplorerContext) GetState() *patch_exploring.State {
|
||||||
return self.state
|
return self.state
|
||||||
}
|
}
|
||||||
|
@ -45,29 +45,3 @@ func (gui *Gui) TransientContexts() []types.Context {
|
|||||||
return context.IsTransient()
|
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -314,7 +314,7 @@ func (gui *Gui) resetControllers() {
|
|||||||
|
|
||||||
// this must come last so that we've got our click handlers defined against the context
|
// this must come last so that we've got our click handlers defined against the context
|
||||||
listControllerFactory := controllers.NewListControllerFactory(common)
|
listControllerFactory := controllers.NewListControllerFactory(common)
|
||||||
for _, context := range gui.getListContexts() {
|
for _, context := range gui.c.Context().AllList() {
|
||||||
controllers.AttachControllers(context, listControllerFactory.Create(context))
|
controllers.AttachControllers(context, listControllerFactory.Create(context))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,9 +31,34 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type
|
|||||||
Description: self.c.Tr.ViewPatchOptions,
|
Description: self.c.Tr.ViewPatchOptions,
|
||||||
OpensMenu: true,
|
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 {
|
func (self *GlobalController) customCommand() error {
|
||||||
return (&CustomCommandAction{c: self.c}).Call()
|
return (&CustomCommandAction{c: self.c}).Call()
|
||||||
}
|
}
|
||||||
@ -42,6 +67,14 @@ func (self *GlobalController) createCustomPatchOptionsMenu() error {
|
|||||||
return (&CustomPatchOptionsMenuAction{c: self.c}).Call()
|
return (&CustomPatchOptionsMenuAction{c: self.c}).Call()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *GlobalController) Context() types.Context {
|
func (self *GlobalController) refresh() error {
|
||||||
return nil
|
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()
|
||||||
}
|
}
|
||||||
|
79
pkg/gui/controllers/screen_mode_actions.go
Normal file
79
pkg/gui/controllers/screen_mode_actions.go
Normal 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]
|
||||||
|
}
|
@ -11,55 +11,6 @@ import (
|
|||||||
|
|
||||||
const HORIZONTAL_SCROLL_FACTOR = 3
|
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) {
|
func (gui *Gui) scrollUpView(view *gocui.View) {
|
||||||
view.ScrollUp(gui.c.UserConfig.Gui.ScrollHeight)
|
view.ScrollUp(gui.c.UserConfig.Gui.ScrollHeight)
|
||||||
}
|
}
|
||||||
@ -157,10 +108,6 @@ func (gui *Gui) scrollDownConfirmationPanel() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleRefresh() error {
|
|
||||||
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error {
|
func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error {
|
||||||
// important to note that this assumes we've selected an item in a side context
|
// important to note that this assumes we've selected an item in a side context
|
||||||
currentSideContext := gui.c.CurrentSideContext()
|
currentSideContext := gui.c.CurrentSideContext()
|
||||||
@ -190,13 +137,3 @@ func (gui *Gui) handleCopySelectedSideContextItemToClipboard() error {
|
|||||||
|
|
||||||
return nil
|
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()
|
|
||||||
}
|
|
||||||
|
@ -347,8 +347,8 @@ func (gui *Gui) resetState(startArgs appTypes.StartArgs, reuseState bool) {
|
|||||||
Diffing: diffing.New(),
|
Diffing: diffing.New(),
|
||||||
},
|
},
|
||||||
ScreenMode: initialScreenMode,
|
ScreenMode: initialScreenMode,
|
||||||
// TODO: put contexts in the context manager
|
// TODO: only use contexts from context manager
|
||||||
ContextMgr: NewContextMgr(initialContext, gui),
|
ContextMgr: NewContextMgr(initialContext, gui, contextTree),
|
||||||
Contexts: contextTree,
|
Contexts: contextTree,
|
||||||
WindowViewNameMap: initialWindowViewNameMap(contextTree),
|
WindowViewNameMap: initialWindowViewNameMap(contextTree),
|
||||||
}
|
}
|
||||||
|
@ -134,19 +134,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
|
|||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: self.scrollDownMain,
|
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: "",
|
ViewName: "",
|
||||||
Key: opts.GetKey(opts.Config.Universal.OptionMenu),
|
Key: opts.GetKey(opts.Config.Universal.OptionMenu),
|
||||||
@ -162,19 +149,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
|
|||||||
Description: self.c.Tr.LcOpenMenu,
|
Description: self.c.Tr.LcOpenMenu,
|
||||||
Handler: self.handleCreateOptionsMenu,
|
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",
|
ViewName: "files",
|
||||||
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
|
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
|
||||||
|
@ -124,7 +124,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
|||||||
gui.State.ViewsSetup = true
|
gui.State.ViewsSetup = true
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, listContext := range gui.getListContexts() {
|
for _, listContext := range gui.c.Context().AllList() {
|
||||||
view, err := gui.g.View(listContext.GetViewName())
|
view, err := gui.g.View(listContext.GetViewName())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
@ -138,7 +138,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
|
|||||||
view.SetOnSelectItem(gui.onSelectItemWrapper(listContext.OnSearchSelect))
|
view.SetOnSelectItem(gui.onSelectItemWrapper(listContext.OnSearchSelect))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, context := range gui.getPatchExplorerContexts() {
|
for _, context := range gui.c.Context().AllPatchExplorer() {
|
||||||
context := context
|
context := context
|
||||||
context.GetView().SetOnSelectItem(gui.onSelectItemWrapper(
|
context.GetView().SetOnSelectItem(gui.onSelectItemWrapper(
|
||||||
func(selectedLineIdx int) error {
|
func(selectedLineIdx int) error {
|
||||||
|
@ -106,6 +106,7 @@ type IListContext interface {
|
|||||||
|
|
||||||
OnSearchSelect(selectedLineIdx int) error
|
OnSearchSelect(selectedLineIdx int) error
|
||||||
FocusLine()
|
FocusLine()
|
||||||
|
IsListContext() // used for type switch
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPatchExplorerContext interface {
|
type IPatchExplorerContext interface {
|
||||||
@ -120,6 +121,7 @@ type IPatchExplorerContext interface {
|
|||||||
GetContentToRender(isFocused bool) string
|
GetContentToRender(isFocused bool) string
|
||||||
NavigateTo(isFocused bool, selectedLineIdx int) error
|
NavigateTo(isFocused bool, selectedLineIdx int) error
|
||||||
GetMutex() *deadlock.Mutex
|
GetMutex() *deadlock.Mutex
|
||||||
|
IsPatchExplorerContext() // used for type switch
|
||||||
}
|
}
|
||||||
|
|
||||||
type IViewTrait interface {
|
type IViewTrait interface {
|
||||||
@ -208,4 +210,6 @@ type IContextMgr interface {
|
|||||||
CurrentSide() Context
|
CurrentSide() Context
|
||||||
IsCurrent(c Context) bool
|
IsCurrent(c Context) bool
|
||||||
ForEach(func(Context))
|
ForEach(func(Context))
|
||||||
|
AllList() []IListContext
|
||||||
|
AllPatchExplorer() []IPatchExplorerContext
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user