summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/side_window_controller.go
blob: a2325c54d8323925c7d70d621286f13c72d69513 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package controllers

import (
	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
)

type SideWindowControllerFactory struct {
	c *ControllerCommon
}

func NewSideWindowControllerFactory(common *ControllerCommon) *SideWindowControllerFactory {
	return &SideWindowControllerFactory{c: common}
}

func (self *SideWindowControllerFactory) Create(context types.Context) types.IController {
	return NewSideWindowController(self.c, context)
}

type SideWindowController struct {
	baseController
	c       *ControllerCommon
	context types.Context
}

func NewSideWindowController(
	common *ControllerCommon,
	context types.Context,
) *SideWindowController {
	return &SideWindowController{
		baseController: baseController{},
		c:              common,
		context:        context,
	}
}

func (self *SideWindowController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
	return []*types.Binding{
		{Key: opts.GetKey(opts.Config.Universal.PrevBlock), Modifier: gocui.ModNone, Handler: self.previousSideWindow},
		{Key: opts.GetKey(opts.Config.Universal.NextBlock), Modifier: gocui.ModNone, Handler: self.nextSideWindow},
		{Key: opts.GetKey(opts.Config.Universal.PrevBlockAlt), Modifier: gocui.ModNone, Handler: self.previousSideWindow},
		{Key: opts.GetKey(opts.Config.Universal.NextBlockAlt), Modifier: gocui.ModNone, Handler: self.nextSideWindow},
		{Key: opts.GetKey(opts.Config.Universal.PrevBlockAlt2), Modifier: gocui.ModNone, Handler: self.previousSideWindow},
		{Key: opts.GetKey(opts.Config.Universal.NextBlockAlt2), Modifier: gocui.ModNone, Handler: self.nextSideWindow},
	}
}

func (self *SideWindowController) Context() types.Context {
	return nil
}

func (self *SideWindowController) previousSideWindow() error {
	windows := self.c.Helpers().Window.SideWindows()
	currentWindow := self.c.Helpers().Window.CurrentWindow()
	var newWindow string
	if currentWindow == "" || currentWindow == windows[0] {
		newWindow = windows[len(windows)-1]
	} else {
		for i := range windows {
			if currentWindow == windows[i] {
				newWindow = windows[i-1]
				break
			}
			if i == len(windows)-1 {
				return nil
			}
		}
	}

	context := self.c.Helpers().Window.GetContextForWindow(newWindow)

	return self.c.PushContext(context)
}

func (self *SideWindowController) nextSideWindow() error {
	windows := self.c.Helpers().Window.SideWindows()
	currentWindow := self.c.Helpers().Window.CurrentWindow()
	var newWindow string
	if currentWindow == "" || currentWindow == windows[len(windows)-1] {
		newWindow = windows[0]
	} else {
		for i := range windows {
			if currentWindow == windows[i] {
				newWindow = windows[i+1]
				break
			}
			if i == len(windows)-1 {
				return nil
			}
		}
	}

	context := self.c.Helpers().Window.GetContextForWindow(newWindow)

	return self.c.PushContext(context)
}