summaryrefslogtreecommitdiffstats
path: root/pkg/gui/context_config.go
blob: 9e12a18961b8cdae865f9ddca1c931c6026584cb (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package gui

type ContextKey string

const (
	STATUS_CONTEXT_KEY              ContextKey = "status"
	FILES_CONTEXT_KEY               ContextKey = "files"
	LOCAL_BRANCHES_CONTEXT_KEY      ContextKey = "localBranches"
	REMOTES_CONTEXT_KEY             ContextKey = "remotes"
	REMOTE_BRANCHES_CONTEXT_KEY     ContextKey = "remoteBranches"
	TAGS_CONTEXT_KEY                ContextKey = "tags"
	BRANCH_COMMITS_CONTEXT_KEY      ContextKey = "commits"
	REFLOG_COMMITS_CONTEXT_KEY      ContextKey = "reflogCommits"
	SUB_COMMITS_CONTEXT_KEY         ContextKey = "subCommits"
	COMMIT_FILES_CONTEXT_KEY        ContextKey = "commitFiles"
	STASH_CONTEXT_KEY               ContextKey = "stash"
	MAIN_NORMAL_CONTEXT_KEY         ContextKey = "normal"
	MAIN_MERGING_CONTEXT_KEY        ContextKey = "merging"
	MAIN_PATCH_BUILDING_CONTEXT_KEY ContextKey = "patchBuilding"
	MAIN_STAGING_CONTEXT_KEY        ContextKey = "staging"
	MENU_CONTEXT_KEY                ContextKey = "menu"
	CREDENTIALS_CONTEXT_KEY         ContextKey = "credentials"
	CONFIRMATION_CONTEXT_KEY        ContextKey = "confirmation"
	SEARCH_CONTEXT_KEY              ContextKey = "search"
	COMMIT_MESSAGE_CONTEXT_KEY      ContextKey = "commitMessage"
	SUBMODULES_CONTEXT_KEY          ContextKey = "submodules"
	SUGGESTIONS_CONTEXT_KEY         ContextKey = "suggestions"
	COMMAND_LOG_CONTEXT_KEY         ContextKey = "cmdLog"
)

var allContextKeys = []ContextKey{
	STATUS_CONTEXT_KEY,
	FILES_CONTEXT_KEY,
	LOCAL_BRANCHES_CONTEXT_KEY,
	REMOTES_CONTEXT_KEY,
	REMOTE_BRANCHES_CONTEXT_KEY,
	TAGS_CONTEXT_KEY,
	BRANCH_COMMITS_CONTEXT_KEY,
	REFLOG_COMMITS_CONTEXT_KEY,
	SUB_COMMITS_CONTEXT_KEY,
	COMMIT_FILES_CONTEXT_KEY,
	STASH_CONTEXT_KEY,
	MAIN_NORMAL_CONTEXT_KEY,
	MAIN_MERGING_CONTEXT_KEY,
	MAIN_PATCH_BUILDING_CONTEXT_KEY,
	MAIN_STAGING_CONTEXT_KEY,
	MENU_CONTEXT_KEY,
	CREDENTIALS_CONTEXT_KEY,
	CONFIRMATION_CONTEXT_KEY,
	SEARCH_CONTEXT_KEY,
	COMMIT_MESSAGE_CONTEXT_KEY,
	SUBMODULES_CONTEXT_KEY,
	SUGGESTIONS_CONTEXT_KEY,
	COMMAND_LOG_CONTEXT_KEY,
}

type ContextTree struct {
	Status         Context
	Files          IListContext
	Submodules     IListContext
	Menu           IListContext
	Branches       IListContext
	Remotes        IListContext
	RemoteBranches IListContext
	Tags           IListContext
	BranchCommits  IListContext
	CommitFiles    IListContext
	ReflogCommits  IListContext
	SubCommits     IListContext
	Stash          IListContext
	Suggestions    IListContext
	Normal         Context
	Staging        Context
	PatchBuilding  Context
	Merging        Context
	Credentials    Context
	Confirmation   Context
	CommitMessage  Context
	Search         Context
	CommandLog     Context
}

func (gui *Gui) allContexts() []Context {
	return []Context{
		gui.State.Contexts.Status,
		gui.State.Contexts.Files,
		gui.State.Contexts.Submodules,
		gui.State.Contexts.Branches,
		gui.State.Contexts.Remotes,
		gui.State.Contexts.RemoteBranches,
		gui.State.Contexts.Tags,
		gui.State.Contexts.BranchCommits,
		gui.State.Contexts.CommitFiles,
		gui.State.Contexts.ReflogCommits,
		gui.State.Contexts.Stash,
		gui.State.Contexts.Menu,
		gui.State.Contexts.Confirmation,
		gui.State.Contexts.Credentials,
		gui.State.Contexts.CommitMessage,
		gui.State.Contexts.Normal,
		gui.State.Contexts.Staging,
		gui.State.Contexts.Merging,
		gui.State.Contexts.PatchBuilding,
		gui.State.Contexts.SubCommits,
		gui.State.Contexts.Suggestions,
		gui.State.Contexts.CommandLog,
	}
}

func (gui *Gui) contextTree() ContextTree {
	return ContextTree{
		Status: &BasicContext{
			OnRenderToMain: OnFocusWrapper(gui.statusRenderToMain),
			Kind:           SIDE_CONTEXT,
			ViewName:       "status",
			Key:            STATUS_CONTEXT_KEY,
		},
		Files:          gui.filesListContext(),
		Submodules:     gui.submodulesListContext(),
		Menu:           gui.menuListContext(),
		Remotes:        gui.remotesListContext(),
		RemoteBranches: gui.remoteBranchesListContext(),
		BranchCommits:  gui.branchCommitsListContext(),
		CommitFiles:    gui.commitFilesListContext(),
		ReflogCommits:  gui.reflogCommitsListContext(),
		SubCommits:     gui.subCommitsListContext(),
		Branches:       gui.branchesListContext(),
		Tags:           gui.tagsListContext(),
		Stash:          gui.stashListContext(),
		Normal: &BasicContext{
			OnFocus: func(opts ...OnFocusOpts) error {
				return nil // TODO: should we do something here? We should allow for scrolling the panel
			},
			Kind:     MAIN_CONTEXT,
			ViewName: "main",
			Key:      MAIN_NORMAL_CONTEXT_KEY,
		},
		Staging: &BasicContext{
			OnRenderToMain: func(opts ...OnFocusOpts) error {
				forceSecondaryFocused := false
				selectedLineIdx := -1
				if len(opts) > 0 && opts[0].ClickedViewName != "" {
					if opts[0].ClickedViewName == "main" || opts[0].ClickedViewName == "secondary" {
						selectedLineIdx = opts[0].ClickedViewLineIdx
					}
					if opts[0].ClickedViewName == "secondary" {
						forceSecondaryFocused = true
					}
				}
				return gui.handleRefreshStagingPanel(forceSecondaryFocused, selectedLineIdx)
			},
			Kind:     MAIN_CONTEXT,
			ViewName: "main",
			Key:      MAIN_STAGING_CONTEXT_KEY,
		},
		PatchBuilding: &BasicContext{
			OnRenderToMain: func(opts ...OnFocusOpts) error {
				selectedLineIdx := -1
				if len(opts) > 0 && (opts[0].ClickedViewName == "main" || opts[0].ClickedViewName == "secondary") {
					selectedLineIdx = opts[0].ClickedViewLineIdx
				}

				return gui.handleRefreshPatchBuildingPanel(selectedLineIdx)
			},
			Kind:     MAIN_CONTEXT,
			ViewName: "main",
			Key:      MAIN_PATCH_BUILDING_CONTEXT_KEY,
		},
		Merging: &BasicContext{
			OnFocus:         OnFocusWrapper(gui.refreshMergePanelWithLock),
			Kind:            MAIN_CONTEXT,
			ViewName:        "main",
			Key:             MAIN_MERGING_CONTEXT_KEY,
			OnGetOptionsMap: gui.getMergingOptions,
		},
		Credentials: &BasicContext{
			OnFocus:  OnFocusWrapper(gui.handleCredentialsViewFocused),
			Kind:     PERSISTENT_POPUP,
			ViewName: "credentials",
			Key:      CREDENTIALS_CONTEXT_KEY,
		},
		Confirmation: &BasicContext{
			Kind:     TEMPORARY_POPUP,
			ViewName: "confirmation",
			Key:      CONFIRMATION_CONTEXT_KEY,
		},
		Suggestions: gui.suggestionsListContext(),
		CommitMessage: &BasicContext{
			OnFocus:  OnFocusWrapper(gui.handleCommitMessageFocused),
			Kind:     PERSISTENT_POPUP,
			ViewName: "commitMessage",
			Key:      COMMIT_MESSAGE_CONTEXT_KEY,
		},
		Search: &BasicContext{
			Kind:     PERSISTENT_POPUP,
			ViewName: "search",
			Key:      SEARCH_CONTEXT_KEY,
		},
		CommandLog: &BasicContext{
			Kind:            EXTRAS_CONTEXT,
			ViewName:        "extras",
			Key:             COMMAND_LOG_CONTEXT_KEY,
			OnGetOptionsMap: gui.getMergingOptions,
			OnFocusLost: func() error {
				gui.Views.Extras.Autoscroll = true
				return nil
			},
		},
	}
}

// using this wrapper for when an onFocus function doesn't care about any potential
// props that could be passed
func OnFocusWrapper(f func() error) func(opts ...OnFocusOpts) error {
	return func(opts ...OnFocusOpts) error {
		return f()
	}
}

func (tree ContextTree) initialViewContextMap() map[string]Context {
	return map[string]Context{
		"status":        tree.Status,
		"files":         tree.Files,
		"branches":      tree.Branches,
		"commits":       tree.BranchCommits,
		"commitFiles":   tree.CommitFiles,
		"stash":         tree.Stash,
		"menu":          tree.Menu,
		"confirmation":  tree.Confirmation,
		"credentials":   tree.Credentials,
		"commitMessage": tree.CommitMessage,
		"main":          tree.Normal,
		"secondary":     tree.