-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
213 lines (194 loc) · 6.53 KB
/
events.go
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
// Copyright (c) 2023, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package filetree
import (
"fmt"
"strings"
"goki.dev/gi/v2/gi"
"goki.dev/gi/v2/giv"
"goki.dev/gi/v2/keyfun"
"goki.dev/girl/states"
"goki.dev/girl/styles"
"goki.dev/goosi/events"
"goki.dev/icons"
"goki.dev/vci/v2"
)
func (fn *Node) HandleEvents() {
fn.On(events.KeyChord, func(e events.Event) {
kt := e.(*events.Key)
fn.KeyInput(kt)
})
}
func (fn *Node) KeyInput(kt events.Event) {
if gi.KeyEventTrace {
fmt.Printf("TreeView KeyInput: %v\n", fn.Path())
}
kf := keyfun.Of(kt.KeyChord())
selMode := events.SelectModeBits(kt.Modifiers())
if selMode == events.SelectOne {
if fn.SelectMode() {
selMode = events.ExtendContinuous
}
}
// first all the keys that work for ReadOnly and active
if !fn.IsReadOnly() && !kt.IsHandled() {
switch kf {
case keyfun.Delete:
fn.DeleteFiles()
kt.SetHandled()
// todo: remove when gi issue 237 is resolved
case keyfun.Backspace:
fn.DeleteFiles()
kt.SetHandled()
case keyfun.Duplicate:
fn.DuplicateFiles()
kt.SetHandled()
case keyfun.Insert: // New File
giv.CallFunc(fn, fn.NewFile)
kt.SetHandled()
case keyfun.InsertAfter: // New Folder
giv.CallFunc(fn, fn.NewFolder)
kt.SetHandled()
}
}
}
// VCSLabelFunc gets the appropriate label for removing from version control
func VCSLabelFunc(fn *Node, label string) string {
repo, _ := fn.Repo()
if repo != nil {
label = strings.Replace(label, "VCS", string(repo.Vcs()), 1)
}
return label
}
func (fn *Node) FileNodeVCSContextMenu(m *gi.Scene) {
giv.NewFuncButton(m, fn.AddToVCSSel).SetText(VCSLabelFunc(fn, "Add to VCS")).SetIcon(icons.Add).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || fn.Info.Vcs != vci.Untracked, states.Disabled)
})
giv.NewFuncButton(m, fn.DeleteFromVCSSel).SetText(VCSLabelFunc(fn, "Delete from VCS")).SetIcon(icons.Delete).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || fn.Info.Vcs == vci.Untracked, states.Disabled)
})
giv.NewFuncButton(m, fn.CommitToVCSSel).SetText(VCSLabelFunc(fn, "Commit to VCS")).SetIcon(icons.Star).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || fn.Info.Vcs == vci.Untracked, states.Disabled)
})
giv.NewFuncButton(m, fn.RevertVCSSel).SetText(VCSLabelFunc(fn, "Revert from VCS")).SetIcon(icons.Undo).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || fn.Info.Vcs == vci.Untracked, states.Disabled)
})
gi.NewSeparator(m)
giv.NewFuncButton(m, fn.DiffVCSSel).SetText(VCSLabelFunc(fn, "Diff VCS")).SetIcon(icons.Add).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || fn.Info.Vcs == vci.Untracked, states.Disabled)
})
giv.NewFuncButton(m, fn.LogVCSSel).SetText(VCSLabelFunc(fn, "Log VCS")).SetIcon(icons.List).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || fn.Info.Vcs == vci.Untracked, states.Disabled)
})
giv.NewFuncButton(m, fn.BlameVCSSel).SetText(VCSLabelFunc(fn, "Blame VCS")).SetIcon(icons.CreditScore).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || fn.Info.Vcs == vci.Untracked, states.Disabled)
})
}
func (fn *Node) FileNodeContextMenu(m *gi.Scene) {
gi.NewButton(m).SetText("File info").SetIcon(icons.Info).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
}).OnClick(func(e events.Event) {
fn.This().(Filer).ShowFileInfo()
})
gi.NewButton(m).SetText("Open (w/default app)").SetIcon(icons.Open).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
}).OnClick(func(e events.Event) {
fn.This().(Filer).OpenFilesDefault()
})
gi.NewSeparator(m)
gi.NewButton(m).SetText("Duplicate").SetIcon(icons.Copy).
SetKey(keyfun.Duplicate).Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
}).OnClick(func(e events.Event) {
fn.This().(Filer).DuplicateFiles()
})
gi.NewButton(m).SetText("Delete").SetIcon(icons.Delete).
SetKey(keyfun.Delete).Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
}).OnClick(func(e events.Event) {
fn.This().(Filer).DeleteFiles()
})
gi.NewButton(m).SetText("Rename").SetIcon(icons.NewLabel).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
}).OnClick(func(e events.Event) {
fn.This().(Filer).RenameFiles()
})
gi.NewSeparator(m)
giv.NewFuncButton(m, fn.OpenAll).SetText("Open All").SetIcon(icons.KeyboardArrowDown).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || !fn.IsDir(), states.Disabled)
})
giv.NewFuncButton(m, fn.CloseAll).SetIcon(icons.KeyboardArrowRight).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || !fn.IsDir(), states.Disabled)
})
giv.NewFuncButton(m, fn.SortBys).SetText("Sort by").SetIcon(icons.Sort).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection() || !fn.IsDir(), states.Disabled)
})
gi.NewSeparator(m)
giv.NewFuncButton(m, fn.NewFiles).SetText("New file").SetIcon(icons.OpenInNew).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
})
giv.NewFuncButton(m, fn.NewFolders).SetText("New folder").SetIcon(icons.CreateNewFolder).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
})
gi.NewSeparator(m)
fn.FileNodeVCSContextMenu(m)
gi.NewSeparator(m)
giv.NewFuncButton(m, fn.RemoveFromExterns).SetIcon(icons.Delete).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
})
gi.NewSeparator(m)
gi.NewButton(m).SetText("Copy").SetIcon(icons.ContentCopy).SetKey(keyfun.Copy).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
}).
OnClick(func(e events.Event) {
fn.Copy(true)
})
gi.NewButton(m).SetText("Cut").SetIcon(icons.ContentCut).SetKey(keyfun.Cut).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
}).
OnClick(func(e events.Event) {
fn.Cut()
})
pbt := gi.NewButton(m).SetText("Paste").SetIcon(icons.ContentPaste).SetKey(keyfun.Paste).
Style(func(s *styles.Style) {
s.SetState(!fn.HasSelection(), states.Disabled)
}).
OnClick(func(e events.Event) {
fn.Paste()
})
cb := fn.Sc.EventMgr.ClipBoard()
if cb != nil {
pbt.SetState(cb.IsEmpty(), states.Disabled)
}
}
func (fn *Node) ContextMenu(m *gi.Scene) {
// derived types put native menu code here
if fn.CustomContextMenu != nil {
fn.CustomContextMenu(m)
return
}
if fn.IsReadOnly() {
fn.TreeViewContextMenuReadOnly(m)
} else {
fn.FileNodeContextMenu(m)
}
}