This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
/
menus.go
218 lines (190 loc) · 6.55 KB
/
menus.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
214
215
216
217
218
// Copyright (c) 2018, Randall C. O'Reilly. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gi
import (
"image"
"log"
"github.com/rcoreilly/goki/gi/units"
"github.com/rcoreilly/goki/ki"
"github.com/rcoreilly/goki/ki/bitflag"
"github.com/rcoreilly/goki/ki/kit"
)
// Menu is a list of Node2D actions, which can contain sub-actions (though it
// can contain anything -- it is just added to a column layout and displayed
// in a popup) -- don't use stretchy sizes in general for these items!
// MakeMenuFunc is a callback for making the menu on demand
type MakeMenuFunc func(mb *ButtonBase)
////////////////////////////////////////////////////////////////////////////////////////
// MenuButton pops up a menu
type MenuButton struct {
ButtonBase
}
var KiT_MenuButton = kit.Types.AddType(&MenuButton{}, MenuButtonProps)
func (n *MenuButton) New() ki.Ki { return &MenuButton{} }
var MenuButtonProps = ki.Props{
"border-width": units.NewValue(1, units.Px),
"border-radius": units.NewValue(4, units.Px),
"border-color": &Prefs.BorderColor,
"border-style": BorderSolid,
"padding": units.NewValue(4, units.Px),
"margin": units.NewValue(4, units.Px),
"box-shadow.color": &Prefs.ShadowColor,
"text-align": AlignCenter,
"vertical-align": AlignMiddle,
"background-color": &Prefs.ControlColor,
"#icon": ki.Props{
"width": units.NewValue(1, units.Em),
"height": units.NewValue(1, units.Em),
"margin": units.NewValue(0, units.Px),
"padding": units.NewValue(0, units.Px),
"fill": &Prefs.IconColor,
"stroke": &Prefs.FontColor,
},
"#label": ki.Props{
"margin": units.NewValue(0, units.Px),
"padding": units.NewValue(0, units.Px),
},
"#indicator": ki.Props{
"width": units.NewValue(1.5, units.Ex),
"height": units.NewValue(1.5, units.Ex),
"margin": units.NewValue(0, units.Px),
"padding": units.NewValue(0, units.Px),
"vertical-align": AlignBottom,
"fill": &Prefs.IconColor,
"stroke": &Prefs.FontColor,
},
ButtonSelectors[ButtonActive]: ki.Props{},
ButtonSelectors[ButtonInactive]: ki.Props{
"border-color": "lighter-50",
"color": "lighter-50",
},
ButtonSelectors[ButtonHover]: ki.Props{
"background-color": "darker-10",
},
ButtonSelectors[ButtonFocus]: ki.Props{
"border-width": units.NewValue(2, units.Px),
"background-color": "lighter-40",
},
ButtonSelectors[ButtonDown]: ki.Props{
"color": "lighter-90",
"background-color": "darker-30",
},
ButtonSelectors[ButtonSelected]: ki.Props{
"background-color": &Prefs.SelectColor,
},
}
func (g *MenuButton) ButtonAsBase() *ButtonBase {
return &(g.ButtonBase)
}
func (g *MenuButton) ConfigParts() {
config, icIdx, lbIdx := g.ConfigPartsIconLabel(g.Icon, g.Text)
indIdx := g.ConfigPartsAddIndicator(&config, true) // default on
mods, updt := g.Parts.ConfigChildren(config, false) // not unique names
g.ConfigPartsSetIconLabel(g.Icon, g.Text, icIdx, lbIdx)
g.ConfigPartsIndicator(indIdx)
if mods {
g.UpdateEnd(updt)
}
}
// check for interface implementation
var _ Node2D = &MenuButton{}
////////////////////////////////////////////////////////////////////////////////////////
// PopupMenu function
var MenuFrameProps = ki.Props{
"border-width": units.NewValue(0, units.Px),
"border-color": "none",
"margin": units.NewValue(4, units.Px),
"padding": units.NewValue(2, units.Px),
"box-shadow.h-offset": units.NewValue(2, units.Px),
"box-shadow.v-offset": units.NewValue(2, units.Px),
"box-shadow.blur": units.NewValue(2, units.Px),
"box-shadow.color": &Prefs.ShadowColor,
}
// PopupMenu just pops up a viewport with a layout that draws the supplied
// actions positions are relative to given viewport -- name is relevant base
// name to which Menu is appended
func PopupMenu(menu ki.Slice, x, y int, parVp *Viewport2D, name string) *Viewport2D {
win := parVp.Win
mainVp := win.Viewport
if len(menu) == 0 {
log.Printf("GoGi PopupMenu: empty menu given\n")
return nil
}
pvp := Viewport2D{}
pvp.InitName(&pvp, name+"Menu")
pvp.Win = win
updt := pvp.UpdateStart()
pvp.Fill = true
bitflag.Set(&pvp.Flag, int(VpFlagPopup))
bitflag.Set(&pvp.Flag, int(VpFlagMenu))
pvp.ViewBox.Min = image.Point{x, y}
// note: not setting VpFlagPopopDestroyAll -- we keep the menu list intact
frame := pvp.AddNewChild(KiT_Frame, "Frame").(*Frame)
frame.Lay = LayoutCol
frame.SetProps(MenuFrameProps, false)
for _, ac := range menu {
acn, _ := KiToNode2D(ac)
frame.AddChild(acn)
}
frame.Init2DTree()
frame.Style2DTree() // sufficient to get sizes
frame.LayData.AllocSize = mainVp.LayData.AllocSize // give it the whole vp initially
frame.Size2DTree() // collect sizes
pvp.Win = nil
vpsz := frame.LayData.Size.Pref.Min(mainVp.LayData.AllocSize).ToPoint()
x = kit.MinInt(x, mainVp.ViewBox.Size.X-vpsz.X) // fit
y = kit.MinInt(y, mainVp.ViewBox.Size.Y-vpsz.Y) // fit
pvp.Resize(vpsz.X, vpsz.Y)
pvp.ViewBox.Min = image.Point{x, y}
pvp.UpdateEndNoSig(updt)
win.NextPopup = pvp.This
return &pvp
}
////////////////////////////////////////////////////////////////////////////////////////
// Separator
// Separator draws a vertical or horizontal line
type Separator struct {
WidgetBase
Horiz bool `xml:"horiz" desc:"is this a horizontal separator -- otherwise vertical"`
}
var KiT_Separator = kit.Types.AddType(&Separator{}, SeparatorProps)
func (n *Separator) New() ki.Ki { return &Separator{} }
var SeparatorProps = ki.Props{
"padding": units.NewValue(2, units.Px),
"margin": units.NewValue(2, units.Px),
"align-vert": AlignCenter,
"align-horiz": AlignCenter,
"stroke-width": units.NewValue(2, units.Px),
"color": &Prefs.FontColor,
"stroke": &Prefs.FontColor,
// todo: dotted
}
func (g *Separator) Style2D() {
g.Style2DWidget()
g.Style2DSVG()
}
func (g *Separator) Render2D() {
if g.PushBounds() {
pc := &g.Paint
rs := &g.Viewport.Render
st := &g.Style
pc.FontStyle = st.Font
pc.TextStyle = st.Text
g.RenderStdBox(st)
pc.StrokeStyle.SetColor(&st.Color) // ink color
spc := st.BoxSpace()
pos := g.LayData.AllocPos.AddVal(spc)
sz := g.LayData.AllocSize.AddVal(-2.0 * spc)
if g.Horiz {
pc.DrawLine(rs, pos.X, pos.Y+0.5*sz.Y, pos.X+sz.X, pos.Y+0.5*sz.Y)
} else {
pc.DrawLine(rs, pos.X+0.5*sz.X, pos.Y, pos.X+0.5*sz.X, pos.Y+sz.Y)
}
pc.FillStrokeClear(rs)
g.Render2DChildren()
g.PopBounds()
}
}
// check for interface implementation
var _ Node2D = &Separator{}