-
Notifications
You must be signed in to change notification settings - Fork 0
/
keymapview.go
160 lines (138 loc) · 4.25 KB
/
keymapview.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
// Copyright (c) 2018, 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 giv
import (
"fmt"
"reflect"
"github.com/goki/gi/gi"
"github.com/goki/gi/oswin"
"github.com/goki/gi/units"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
)
// KeyMapsView opens a view of a key maps table
func KeyMapsView(km *gi.KeyMaps) {
winm := "gogi-key-maps"
width := 800
height := 800
win, recyc := gi.RecycleMainWindow(km, winm, "GoGi Key Maps", width, height)
if recyc {
return
}
vp := win.WinViewport2D()
updt := vp.UpdateStart()
mfr := win.SetMainFrame()
mfr.Lay = gi.LayoutVert
title := mfr.AddNewChild(gi.KiT_Label, "title").(*gi.Label)
title.SetText("Available Key Maps: Duplicate an existing map (using Ctxt Menu) as starting point for creating a custom map")
title.SetProp("width", units.NewCh(30)) // need for wrap
title.SetStretchMaxWidth()
title.SetProp("white-space", gi.WhiteSpaceNormal) // wrap
tv := mfr.AddNewChild(KiT_TableView, "tv").(*TableView)
tv.Viewport = vp
tv.SetSlice(km)
tv.SetStretchMax()
gi.AvailKeyMapsChanged = false
tv.ViewSig.Connect(mfr.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
gi.AvailKeyMapsChanged = true
})
mmen := win.MainMenu
MainMenuView(km, win, mmen)
inClosePrompt := false
win.OSWin.SetCloseReqFunc(func(w oswin.Window) {
if !gi.AvailKeyMapsChanged || km != &gi.AvailKeyMaps { // only for main avail map..
win.Close()
return
}
if inClosePrompt {
return
}
inClosePrompt = true
gi.ChoiceDialog(vp, gi.DlgOpts{Title: "Save KeyMaps Before Closing?",
Prompt: "Do you want to save any changes to std preferences keymaps file before closing, or Cancel the close and do a Save to a different file?"},
[]string{"Save and Close", "Discard and Close", "Cancel"},
win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
switch sig {
case 0:
km.SavePrefs()
fmt.Printf("Preferences Saved to %v\n", gi.PrefsKeyMapsFileName)
win.Close()
case 1:
if km == &gi.AvailKeyMaps {
km.OpenPrefs() // revert
}
win.Close()
case 2:
inClosePrompt = false
// default is to do nothing, i.e., cancel
}
})
})
win.MainMenuUpdated()
if !win.HasGeomPrefs() { // resize to contents
vpsz := vp.PrefSize(win.OSWin.Screen().PixSize)
win.SetSize(vpsz)
}
vp.UpdateEndNoSig(updt)
win.GoStartEventLoop()
}
////////////////////////////////////////////////////////////////////////////////////////
// KeyMapValueView
// KeyMapValueView presents an action for displaying a KeyMapName and selecting
// from chooser
type KeyMapValueView struct {
ValueViewBase
}
var KiT_KeyMapValueView = kit.Types.AddType(&KeyMapValueView{}, nil)
func (vv *KeyMapValueView) WidgetType() reflect.Type {
vv.WidgetTyp = gi.KiT_Action
return vv.WidgetTyp
}
func (vv *KeyMapValueView) UpdateWidget() {
if vv.Widget == nil {
return
}
ac := vv.Widget.(*gi.Action)
txt := kit.ToString(vv.Value.Interface())
ac.SetFullReRender()
ac.SetText(txt)
}
func (vv *KeyMapValueView) ConfigWidget(widg gi.Node2D) {
vv.Widget = widg
vv.StdConfigWidget(widg)
ac := vv.Widget.(*gi.Action)
ac.SetProp("border-radius", units.NewPx(4))
ac.ActionSig.ConnectOnly(vv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
vvv, _ := recv.Embed(KiT_KeyMapValueView).(*KeyMapValueView)
ac := vvv.Widget.(*gi.Action)
vvv.Activate(ac.ViewportSafe(), nil, nil)
})
vv.UpdateWidget()
}
func (vv *KeyMapValueView) HasAction() bool {
return true
}
func (vv *KeyMapValueView) Activate(vp *gi.Viewport2D, dlgRecv ki.Ki, dlgFunc ki.RecvFunc) {
if vv.IsInactive() {
return
}
cur := kit.ToString(vv.Value.Interface())
_, curRow, _ := gi.AvailKeyMaps.MapByName(gi.KeyMapName(cur))
desc, _ := vv.Tag("desc")
TableViewSelectDialog(vp, &gi.AvailKeyMaps, DlgOpts{Title: "Select a KeyMap", Prompt: desc}, curRow, nil,
vv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.DialogAccepted) {
ddlg, _ := send.(*gi.Dialog)
si := TableViewSelectDialogValue(ddlg)
if si >= 0 {
km := gi.AvailKeyMaps[si]
vv.SetValue(km.Name)
vv.UpdateWidget()
}
}
if dlgRecv != nil && dlgFunc != nil {
dlgFunc(dlgRecv, send, sig, data)
}
})
}