summaryrefslogtreecommitdiffstats
path: root/pkg/gui/context/list_context_trait.go
blob: 776056fad9312a94a31fd7db64db32e881aa1d97 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package context

import (
	"fmt"

	"github.com/jesseduffield/gocui"
	"github.com/jesseduffield/lazygit/pkg/config"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
	"github.com/jesseduffield/lazygit/pkg/utils"
)

type Thing interface {
	// the boolean here tells us whether the item is nil. This is needed because you can't work it out on the calling end once the pointer is wrapped in an interface (unless you want to use reflection)
	GetSelectedItem() (types.ListItem, bool)
}

type ListContextTrait struct {
	base      types.IBaseContext
	thing     Thing
	listTrait *ListTrait
	viewTrait *ViewTrait

	takeFocus func() error

	GetDisplayStrings func(startIdx int, length int) [][]string
	OnFocus           func(...types.OnFocusOpts) error
	OnRenderToMain    func(...types.OnFocusOpts) error
	OnFocusLost       func() error

	// if this is true, we'll call GetDisplayStrings for just the visible part of the
	// view and re-render that. This is useful when you need to render different
	// content based on the selection (e.g. for showing the selected commit)
	RenderSelection bool

	c *types.ControllerCommon
}

func (self *ListContextTrait) GetPanelState() types.IListPanelState {
	return self.listTrait
}

func (self *ListContextTrait) FocusLine() {
	// we need a way of knowing whether we've rendered to the view yet.
	self.viewTrait.FocusPoint(self.listTrait.GetSelectedLineIdx())
	if self.RenderSelection {
		min, max := self.viewTrait.ViewPortYBounds()
		displayStrings := self.GetDisplayStrings(min, max)
		content := utils.RenderDisplayStrings(displayStrings)
		self.viewTrait.SetViewPortContent(content)
	}
	self.viewTrait.SetFooter(formatListFooter(self.listTrait.GetSelectedLineIdx(), self.listTrait.GetItemsLength()))
}

func formatListFooter(selectedLineIdx int, length int) string {
	return fmt.Sprintf("%d of %d", selectedLineIdx+1, length)
}

func (self *ListContextTrait) GetSelectedItemId() string {
	item, ok := self.thing.GetSelectedItem()

	if !ok {
		return ""
	}

	return item.ID()
}

// OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view
func (self *ListContextTrait) HandleRender() error {
	if self.GetDisplayStrings != nil {
		self.listTrait.RefreshSelectedIdx()
		content := utils.RenderDisplayStrings(self.GetDisplayStrings(0, self.listTrait.GetItemsLength()))
		self.viewTrait.SetContent(content)
		self.c.Render()
	}

	return nil
}

func (self *ListContextTrait) HandleFocusLost() error {
	if self.OnFocusLost != nil {
		return self.OnFocusLost()
	}

	self.viewTrait.SetOriginX(0)

	return nil
}

func (self *ListContextTrait) HandleFocus(opts ...types.OnFocusOpts) error {
	self.FocusLine()

	if self.OnFocus != nil {
		if err := self.OnFocus(opts...); err != nil {
			return err
		}
	}

	if self.OnRenderToMain != nil {
		if err := self.OnRenderToMain(opts...); err != nil {
			return err
		}
	}

	return nil
}

func (self *ListContextTrait) HandlePrevLine() error {
	return self.handleLineChange(-1)
}

func (self *ListContextTrait) HandleNextLine() error {
	return self.handleLineChange(1)
}

func (self *ListContextTrait) HandleScrollLeft() error {
	return self.scroll(self.viewTrait.ScrollLeft)
}

func (self *ListContextTrait) HandleScrollRight() error {
	return self.scroll(self.viewTrait.ScrollRight)
}

func (self *ListContextTrait) scroll(scrollFunc func()) error {
	scrollFunc()

	return self.HandleFocus()
}

func (self *ListContextTrait) handleLineChange(change int) error {
	before := self.listTrait.GetSelectedLineIdx()
	self.listTrait.MoveSelectedLine(change)
	after := self.listTrait.GetSelectedLineIdx()

	if before != after {
		return self.HandleFocus()
	}

	return nil
}

func (self *ListContextTrait) HandlePrevPage() error {
	return self.handleLineChange(-self.viewTrait.PageDelta())
}

func (self *ListContextTrait) HandleNextPage() error {
	return self.handleLineChange(self.viewTrait.PageDelta())
}

func (self *ListContextTrait) HandleGotoTop() error {
	return self.handleLineChange(-self.listTrait.GetItemsLength())
}

func (self *ListContextTrait) HandleGotoBottom() error {
	return self.handleLineChange(self.listTrait.GetItemsLength())
}

func (self *ListContextTrait) HandleClick(onClick func() error) error {
	prevSelectedLineIdx := self.listTrait.GetSelectedLineIdx()
	// because we're handling a click, we need to determine the new line idx based
	// on the view itself.
	newSelectedLineIdx := self.viewTrait.SelectedLineIdx()

	currentContextKey := self.c.CurrentContext().GetKey()
	alreadyFocused := currentContextKey == self.base.GetKey()

	// we need to focus the view
	if !alreadyFocused {

		if err := self.takeFocus(); err != nil {
			return err
		}
	}

	if newSelectedLineIdx > self.listTrait.GetItemsLength()-1 {
		return nil
	}

	self.listTrait.SetSelectedLineIdx(newSelectedLineIdx)

	if prevSelectedLineIdx == newSelectedLineIdx && alreadyFocused && onClick != nil {
		return onClick()
	}
	return self.HandleFocus()
}

func (self *ListContextTrait) OnSearchSelect(selectedLineIdx int) error {
	self.listTrait.SetSelectedLineIdx(selectedLineIdx)
	return self.HandleFocus()
}

func (self *ListContextTrait) HandleRenderToMain() error {
	if self.OnRenderToMain != nil {
		return self.OnRenderToMain()
	}

	return nil
}

func (self *ListContextTrait) Keybindings(
	getKey func(key string) interface{},
	config config.KeybindingConfig,
	guards types.KeybindingGuards,
) []*types.Binding {
	return []*types.Binding{
		{Tag: "navigation", Key: getKey(config.Universal.PrevItemAlt), Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
		{Tag: "navigation", Key: getKey(config.Universal.PrevItem), Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
		{Tag: "navigation", Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
		{Tag: "navigation", Key: getKey(config.Universal.NextItemAlt), Modifier: gocui.ModNone, Handler: self.HandleNextLine},
		{Tag: "navigation", Key: getKey(config.Universal.NextItem), Modifier: gocui.ModNone, Handler: self.HandleNextLine},
		{Tag: "navigation", Key: getKey(config.Universal.PrevPage), Modifier: gocui.ModNone, Handler: self.HandlePrevPage, Description: self.c.Tr.LcPrevPage},
		{Tag: "navigation", Key: getKey(config.Universal.NextPage), Modifier: gocui.ModNone, Handler: self.HandleNextPage, Description: self.c.Tr.LcNextPage},
		{Tag: "navigation", Key: getKey(config.Universal.GotoTop), Modifier: gocui.ModNone, Handler: self.HandleGotoTop, Description: self.c.Tr.LcGotoTop},
		{Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: func() error { return self.HandleClick(nil) }},
		{Tag