-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
menu_item.go
381 lines (344 loc) · 9.65 KB
/
menu_item.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) 2021-2024 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"fmt"
"github.com/richardwilkes/toolbox"
"github.com/richardwilkes/toolbox/xmath"
"github.com/richardwilkes/unison/enums/check"
"github.com/richardwilkes/unison/enums/paintstyle"
"github.com/richardwilkes/unison/enums/side"
)
var _ MenuItem = &menuItem{}
// MenuItem describes a choice that can be made from a Menu.
type MenuItem interface {
// Factory returns the MenuFactory that created this MenuItem.
Factory() MenuFactory
// ID returns the id of this menu item.
ID() int
// IsSame returns true if the two items represent the same object. Do not use == to test for equality.
IsSame(other MenuItem) bool
// Menu returns the owning menu.
Menu() Menu
// Index returns the index of the menu item within its menu. Returns -1 if it is not yet attached to a menu.
Index() int
// IsSeparator returns true if this menu item is a separator.
IsSeparator() bool
// Title returns the menu item's title.
Title() string
// SetTitle sets the menu item's title.
SetTitle(title string)
// KeyBinding returns the key binding for the menu item.
KeyBinding() KeyBinding
// SetKeyBinding sets the key binding for the menu item.
SetKeyBinding(keyBinding KeyBinding)
// SubMenu returns the menu item's sub-menu, if any.
SubMenu() Menu
// CheckState returns the menu item's current check state.
CheckState() check.Enum
// SetCheckState sets the menu item's check state.
SetCheckState(s check.Enum)
}
// DefaultMenuItemTheme holds the default MenuItemTheme values for menu items. Modifying this data will not alter
// existing menu items, but will alter any menu items created in the future.
var DefaultMenuItemTheme = MenuItemTheme{
TitleFont: SystemFont,
KeyFont: KeyboardFont,
BackgroundColor: ThemeSurface,
OnBackgroundColor: ThemeOnSurface,
SelectionColor: ThemeFocus,
OnSelectionColor: ThemeOnFocus,
ItemBorder: NewEmptyBorder(StdInsets()),
SeparatorBorder: NewEmptyBorder(NewVerticalInsets(4)),
KeyGap: 16,
}
// MenuItemTheme holds theming data for a menu item.
type MenuItemTheme struct {
TitleFont Font
KeyFont Font
BackgroundColor Ink
OnBackgroundColor Ink
SelectionColor Ink
OnSelectionColor Ink
ItemBorder Border
SeparatorBorder Border
KeyGap float32
}
type menuItem struct {
factory *inWindowMenuFactory
menu *menu
subMenu *menu
panel *Panel
validator func(MenuItem) bool
handler func(MenuItem)
title string
id int
keyBinding KeyBinding
state check.Enum
isSeparator bool
enabled bool
over bool
}
func (mi *menuItem) Factory() MenuFactory {
return mi.factory
}
func (mi *menuItem) ID() int {
return mi.id
}
func (mi *menuItem) IsSame(other MenuItem) bool {
return mi == other
}
func (mi *menuItem) Menu() Menu {
return mi.menu
}
func (mi *menuItem) Index() int {
if mi.menu != nil {
count := mi.menu.Count()
for i := 0; i < count; i++ {
if mi.IsSame(mi.menu.ItemAtIndex(i)) {
return i
}
}
}
return -1
}
func (mi *menuItem) IsSeparator() bool {
return mi.isSeparator
}
func (mi *menuItem) Title() string {
return mi.title
}
func (mi *menuItem) String() string {
return fmt.Sprintf("[%d] %s", mi.id, mi.title)
}
func (mi *menuItem) SetTitle(title string) {
mi.title = title
}
func (mi *menuItem) KeyBinding() KeyBinding {
return mi.keyBinding
}
func (mi *menuItem) SetKeyBinding(keyBinding KeyBinding) {
mi.keyBinding = keyBinding
}
func (mi *menuItem) SubMenu() Menu {
return mi.subMenu
}
func (mi *menuItem) CheckState() check.Enum {
return mi.state
}
func (mi *menuItem) SetCheckState(s check.Enum) {
mi.state = s
}
func (mi *menuItem) newPanel() *Panel {
mi.panel = NewPanel()
if mi.isSeparator {
mi.panel.SetBorder(DefaultMenuItemTheme.SeparatorBorder)
} else {
mi.panel.SetBorder(DefaultMenuItemTheme.ItemBorder)
}
mi.over = false
mi.panel.DrawCallback = mi.paint
mi.panel.MouseEnterCallback = mi.mouseEnter
mi.panel.MouseMoveCallback = mi.mouseMove
mi.panel.MouseExitCallback = mi.mouseExit
mi.panel.MouseDownCallback = mi.mouseDown
mi.panel.MouseUpCallback = mi.mouseUp
mi.panel.SetSizer(mi.sizer)
return mi.panel
}
func (mi *menuItem) mouseDown(_ Point, _, _ int, _ Modifiers) bool {
if mi.subMenu != nil {
mi.showSubMenu()
}
return true
}
func (mi *menuItem) mouseUp(where Point, _ int, _ Modifiers) bool {
if mi.subMenu == nil && where.In(mi.panel.ContentRect(true)) {
mi.execute()
}
return true
}
func (mi *menuItem) click() {
if mi.subMenu != nil {
mi.showSubMenu()
} else {
mi.execute()
}
}
func (mi *menuItem) showSubMenu() {
if !mi.factory.showInProgress && mi.subMenu != nil && mi.subMenu.popupPanel == nil {
mi.factory.showInProgress = true
defer func() { mi.factory.showInProgress = false }()
mi.subMenu.createPopup()
pr := mi.panel.RectToRoot(mi.panel.ContentRect(true))
fr := mi.subMenu.popupPanel.FrameRect()
if mi.isRoot() {
fr.X = pr.X
fr.Y = pr.Bottom()
} else {
fr.X = pr.Right()
fr.Y = pr.Y
}
mi.subMenu.ensureInWindow(fr)
}
}
func (mi *menuItem) scrollIntoView() {
mi.panel.ScrollIntoView()
}
func (mi *menuItem) mouseEnter(_ Point, _ Modifiers) bool {
if mi.menu != nil {
for _, item := range mi.menu.items {
if item.over {
item.over = false
item.panel.MarkForRedraw()
}
}
}
mi.over = true
mi.panel.MarkForRedraw()
if mi.subMenu != nil && len(mi.panel.Window().root.openMenuPanels) != 0 {
mi.showSubMenu()
}
return false
}
func (mi *menuItem) mouseMove(_ Point, _ Modifiers) bool {
stopAt := mi.menu
if mi.subMenu != nil && mi.subMenu.popupPanel != nil {
stopAt = mi.subMenu
}
if w := ActiveWindow(); w != nil {
mi.menu.closeMenuStackStoppingAt(w, stopAt)
}
return false
}
func (mi *menuItem) mouseExit() bool {
mi.over = false
mi.panel.MarkForRedraw()
return false
}
func (mi *menuItem) sizer(hint Size) (minSize, prefSize, maxSize Size) {
if mi.isSeparator {
prefSize.Height = 1
} else {
prefSize, _ = LabelContentSizes(NewText(mi.Title(), &TextDecoration{
Font: DefaultMenuItemTheme.TitleFont,
OnBackgroundInk: DefaultMenuItemTheme.OnBackgroundColor,
}), nil, DefaultMenuItemTheme.TitleFont, side.Left, 0)
if !mi.isRoot() {
prefSize.Width += (DefaultMenuItemTheme.KeyFont.Baseline() + 2) * 2
}
if !mi.keyBinding.KeyCode.ShouldOmit() {
keys := mi.keyBinding.String()
if keys != "" {
size := NewText(keys, &TextDecoration{
Font: DefaultMenuItemTheme.KeyFont,
OnBackgroundInk: DefaultMenuItemTheme.OnBackgroundColor,
}).Extents()
prefSize.Width += DefaultMenuItemTheme.KeyGap + size.Width
prefSize.Height = max(prefSize.Height, size.Height)
}
}
}
prefSize = prefSize.Add(DefaultMenuItemTheme.ItemBorder.Insets().Size()).Ceil().ConstrainForHint(hint)
return prefSize, prefSize, prefSize
}
func (mi *menuItem) paint(gc *Canvas, rect Rect) {
var fg, bg Ink
if !mi.over || !mi.enabled {
fg = DefaultMenuItemTheme.OnBackgroundColor
bg = DefaultMenuItemTheme.BackgroundColor
} else {
fg = DefaultMenuItemTheme.OnSelectionColor
bg = DefaultMenuItemTheme.SelectionColor
}
gc.DrawRect(rect, bg.Paint(gc, rect, paintstyle.Fill))
if !mi.enabled {
fg = &ColorFilteredInk{
OriginalInk: fg,
ColorFilter: Grayscale30Filter(),
}
}
rect = mi.panel.ContentRect(false)
if mi.isSeparator {
gc.DrawLine(rect.X, rect.Y, rect.Right(), rect.Y, fg.Paint(gc, rect, paintstyle.Fill))
} else {
t := NewText(mi.Title(), &TextDecoration{
Font: DefaultMenuItemTheme.TitleFont,
OnBackgroundInk: fg,
})
size := t.Extents()
baseline := DefaultMenuItemTheme.KeyFont.Baseline()
var shifted float32
if !mi.isRoot() {
shifted = baseline + 2
}
t.Draw(gc, rect.X+shifted, xmath.Floor(rect.Y+(rect.Height-size.Height)/2)+t.Baseline())
if mi.subMenu == nil {
if !mi.isRoot() && mi.state != check.Off {
r := rect
r.Width = baseline
r.Height = baseline
r.Y += (rect.Height - baseline) / 2
drawable := &DrawableSVG{Size: Size{Width: baseline, Height: baseline}}
if mi.state == check.On {
drawable.SVG = CheckmarkSVG
} else {
drawable.SVG = DashSVG
}
drawable.DrawInRect(gc, r, nil, fg.Paint(gc, r, paintstyle.Fill))
}
if !mi.keyBinding.KeyCode.ShouldOmit() {
keys := mi.keyBinding.String()
if keys != "" {
t = NewText(keys, &TextDecoration{
Font: DefaultMenuItemTheme.KeyFont,
OnBackgroundInk: fg,
})
size = t.Extents()
t.Draw(gc, xmath.Floor(rect.Right()-size.Width),
xmath.Floor(rect.Y+(rect.Height-size.Height)/2)+t.Baseline())
}
}
} else if !mi.isRoot() {
rect.X = rect.Right() - baseline
rect.Width = baseline
drawable := &DrawableSVG{
SVG: ChevronRightSVG,
Size: Size{Width: baseline, Height: baseline},
}
drawable.DrawInRect(gc, rect, nil, fg.Paint(gc, rect, paintstyle.Fill))
}
}
}
func (mi *menuItem) isRoot() bool {
return mi.menu.popupPanel == nil
}
func (mi *menuItem) validate() {
if mi.isSeparator {
return
}
if DisableMenus {
mi.enabled = false
} else {
mi.enabled = true
if mi.validator != nil {
mi.enabled = false
toolbox.Call(func() { mi.enabled = mi.validator(mi) })
}
}
}
func (mi *menuItem) execute() {
if mi.isSeparator {
return
}
mi.menu.closeMenuStack()
if mi.enabled && mi.handler != nil {
toolbox.Call(func() { mi.handler(mi) })
}
}