-
Notifications
You must be signed in to change notification settings - Fork 23
/
types_menumodel.go
503 lines (431 loc) · 17.5 KB
/
types_menumodel.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
package cef
import (
"github.com/energye/energy/v2/cef/internal/def"
. "github.com/energye/energy/v2/common"
"github.com/energye/energy/v2/common/imports"
. "github.com/energye/energy/v2/consts"
"github.com/energye/energy/v2/types"
"github.com/energye/golcl/lcl/api"
"strings"
"unsafe"
)
// KeyAccelerator 自定义实现快捷键
var KeyAccelerator = &keyEventAccelerator{
menuId: MENU_ID_USER_FIRST, //menuId的启始位置
commandItems: make(map[MenuId]*MenuItem), //右键菜单命令ID快捷键
acceleratorItems: make(map[string]*MenuItem), //右键菜单命令名称快捷键
acceleratorCustom: make(map[string]*AcceleratorCustom), //浏览器界面自定义快捷键
}
type FuncCallback func(browser *ICefBrowser, commandId MenuId, params *ICefContextMenuParams, menuType TCefContextMenuType, eventFlags uint32, result *bool)
type keyEventAccelerator struct {
shift, ctrl, alt bool
keyCode int32
menuId MenuId
commandItems map[MenuId]*MenuItem
acceleratorItems map[string]*MenuItem
acceleratorCustom map[string]*AcceleratorCustom
}
// MenuItem 菜单项
type MenuItem struct {
CommandId MenuId // >= 26500 and <= 28500
Accelerator string // 快捷键 shift ctrl alt【按键VK..】(shift+ctrl+alt+n)
Text string //显示文本
Label string
GroupId int32 //分组 配合 MenuType 使用
MenuType TCefContextMenuType
Callback FuncCallback //点击 或 快捷键触发的回调
}
// AddAcceleratorCustom 添加自定义快捷键
func (m *keyEventAccelerator) AddAcceleratorCustom(accelerator *AcceleratorCustom) {
if accelerator == nil {
return
}
accelerator.Accelerator = strings.Replace(strings.ToUpper(accelerator.Accelerator), " ", "", -1)
as := strings.Split(accelerator.Accelerator, "+")
if len(as) > 0 && len(as) <= 4 {
var shift = ArrayIndexOf(as, MA_Shift) != -1
var ctrl = ArrayIndexOf(as, MA_Ctrl) != -1
var alt = ArrayIndexOf(as, MA_Alt) != -1
var keyCode = rune(strings.ToUpper(as[len(as)-1])[0])
accelerator.Accelerator = acceleratorCode(shift, ctrl, alt, keyCode)
m.acceleratorCustom[accelerator.Accelerator] = accelerator
}
}
func (m *keyEventAccelerator) acceleratorCustomCallback(accelerator string, browse *ICefBrowser, event *TCefKeyEvent, result *bool) bool {
if item, ok := m.acceleratorCustom[accelerator]; ok {
if item.Callback != nil {
item.Callback(browse, event, result)
return true
}
}
return false
}
// NextCommandId 生成并返回下一个快捷键ID
func (m *keyEventAccelerator) NextCommandId() MenuId {
m.menuId++
return m.menuId
}
func (m *keyEventAccelerator) commandIdEventCallback(browse *ICefBrowser, commandId MenuId, params *ICefContextMenuParams, eventFlags uint32, result *bool) bool {
if item, ok := m.commandItems[commandId]; ok {
if item.Callback != nil {
item.Callback(browse, commandId, params, item.MenuType, eventFlags, result)
return true
}
}
return false
}
func (m *keyEventAccelerator) accelerator(browse *ICefBrowser, event *TCefKeyEvent, result *bool) bool {
if event.KeyDown() {
if event.WindowsKeyCode == MA_Shift_Code {
m.shift = true
} else if event.WindowsKeyCode == MA_Ctrl_Code {
m.ctrl = true
} else if event.WindowsKeyCode == MA_Alt_Code {
m.alt = true
} else {
var accelerator = acceleratorCode(m.shift, m.ctrl, m.alt, rune(event.WindowsKeyCode))
//m.shift = false
//m.ctrl = false
//m.alt = false
m.keyCode = -1
if m.acceleratorEventCallback(browse, accelerator, result) {
return true
} else if m.acceleratorCustomCallback(accelerator, browse, event, result) {
return true
}
}
} else {
m.shift = false
m.ctrl = false
m.alt = false
m.keyCode = -1
}
return false
}
func (m *keyEventAccelerator) acceleratorEventCallback(browse *ICefBrowser, accelerator string, result *bool) bool {
if item, ok := m.acceleratorItems[accelerator]; ok {
if item.Callback != nil {
item.Callback(browse, item.CommandId, nil, item.MenuType, 0, result)
return true
}
}
return false
}
func (m *keyEventAccelerator) clear() {
m.menuId = MENU_ID_USER_FIRST
m.commandItems = make(map[MenuId]*MenuItem)
m.acceleratorItems = make(map[string]*MenuItem)
}
// AddMenuItem 添加一个菜单项 MenuItem
func (m *ICefMenuModel) AddMenuItem(item *MenuItem) bool {
if item == nil {
return false
}
if item.CommandId == 0 {
item.CommandId = KeyAccelerator.NextCommandId()
}
//if item.CommandId >= MENU_ID_USER_FIRST && item.CommandId <= MENU_ID_USER_LAST {
if item.MenuType == CMT_NONE {
m.AddItem(item.CommandId, item.Text)
} else if item.MenuType == CMT_CHECK {
m.AddCheckItem(item.CommandId, item.Text)
} else if item.MenuType == CMT_RADIO {
m.AddRadioItem(item.CommandId, item.Text, item.GroupId)
} else {
return false
}
if item.Label != "" {
m.SetLabel(item.CommandId, item.Label)
}
if item.Accelerator != "" {
item.Accelerator = strings.Replace(strings.ToUpper(item.Accelerator), " ", "", -1)
as := strings.Split(item.Accelerator, "+")
if len(as) > 0 && len(as) <= 4 {
var shift = ArrayIndexOf(as, MA_Shift) != -1
var ctrl = ArrayIndexOf(as, MA_Ctrl) != -1
var alt = ArrayIndexOf(as, MA_Alt) != -1
var keyCode = rune(strings.ToUpper(as[len(as)-1])[0])
item.Accelerator = acceleratorCode(shift, ctrl, alt, keyCode)
m.SetAccelerator(item.CommandId, keyCode, shift, ctrl, alt)
KeyAccelerator.acceleratorItems[item.Accelerator] = item
}
}
KeyAccelerator.commandItems[item.CommandId] = item
return true
}
// MenuModelRef -> ICefMenuModel
var MenuModelRef menuModel
type menuModel uintptr
func (*menuModel) New(delegate *ICefMenuModelDelegate) *ICefMenuModel {
var result uintptr
imports.Proc(def.CefMenuModelRef_New).Call(delegate.Instance(), uintptr(unsafe.Pointer(&result)))
if result != 0 {
return &ICefMenuModel{instance: unsafe.Pointer(result)}
}
return nil
}
// Instance 实例
func (m *ICefMenuModel) Instance() uintptr {
if m == nil {
return 0
}
return uintptr(m.instance)
}
func (m *ICefMenuModel) Free() {
if m.instance != nil {
m.base.Free(m.Instance())
m.instance = nil
}
}
func (m *ICefMenuModel) IsValid() bool {
if m == nil || m.instance == nil {
return false
}
return m.instance != nil
}
// AddSeparator 添加一个分隔线
func (m *ICefMenuModel) AddSeparator() bool {
return cefMenuModel_AddSeparator(uintptr(m.instance))
}
// Clear 清空菜单
func (m *ICefMenuModel) Clear() bool {
return cefMenuModel_Clear(uintptr(m.instance))
}
// IsSubMenu 当前菜单项是否为子菜单
func (m *ICefMenuModel) IsSubMenu() bool {
return cefMenuModel_IsSubMenu(uintptr(m.instance))
}
// GetCount 菜单项数量
func (m *ICefMenuModel) GetCount() int32 {
return cefMenuModel_GetCount(uintptr(m.instance))
}
// AddItem 添加一个菜单项
func (m *ICefMenuModel) AddItem(commandId MenuId, text string) bool {
return cefMenuModel_AddItem(uintptr(m.instance), commandId, text)
}
// AddCheckItem 添加一个复选框菜单项
func (m *ICefMenuModel) AddCheckItem(commandId MenuId, text string) bool {
return cefMenuModel_AddCheckItem(uintptr(m.instance), commandId, text)
}
// AddRadioItem 添加一个单选框菜单项-按分组
func (m *ICefMenuModel) AddRadioItem(commandId MenuId, text string, groupId int32) bool {
return cefMenuModel_AddRadioItem(uintptr(m.instance), commandId, text, groupId)
}
// AddSubMenu 创建添加并返回一个子菜单项, 使用返回的 ICefMenuModel 给子菜单添加菜单项
func (m *ICefMenuModel) AddSubMenu(commandId MenuId, text string) *ICefMenuModel {
return cefMenuModel_AddSubMenu(uintptr(m.instance), commandId, text)
}
// Remove 指定移除一个菜单项,可直接移除Chromium默认实现的菜单项 consts.MenuId => MENU_ID_XXX
func (m *ICefMenuModel) Remove(commandId MenuId) bool {
return cefMenuModel_Remove(uintptr(m.instance), commandId)
}
// RemoveAt 指定根据下标移除一个菜单项,可直接移除Chromium默认实现的菜单项 consts.MenuId => MENU_ID_XXX
func (m *ICefMenuModel) RemoveAt(index int32) bool {
return cefMenuModel_RemoveAt(uintptr(m.instance), index)
}
// SetChecked 设置Check | Radio 选中
func (m *ICefMenuModel) SetChecked(commandId MenuId, check bool) bool {
return cefMenuModel_SetChecked(uintptr(m.instance), commandId, check)
}
func (m *ICefMenuModel) IsChecked(commandId MenuId) bool {
return cefMenuModel_IsChecked(uintptr(m.instance), commandId)
}
// SetColor 设置可用菜单项 cef.NewCefARGB(a, r, g, b), 颜色根据 consts.TCefMenuColorType
func (m *ICefMenuModel) SetColor(commandId MenuId, colorType TCefMenuColorType, color *TCefARGB) bool {
return cefMenuModel_SetColor(uintptr(m.instance), commandId, colorType, color)
}
func (m *ICefMenuModel) SetFontList(commandId MenuId, fontList string) bool {
return cefMenuModel_SetFontList(uintptr(m.instance), commandId, fontList)
}
// HasAccelerator 是否有快捷键
func (m *ICefMenuModel) HasAccelerator(commandId MenuId) bool {
return cefMenuModel_HasAccelerator(uintptr(m.instance), commandId)
}
// SetAccelerator 设置快捷键
func (m *ICefMenuModel) SetAccelerator(commandId MenuId, keyCode int32, shiftPressed, ctrlPressed, altPressed bool) bool {
return cefMenuModel_SetAccelerator(uintptr(m.instance), commandId, keyCode, shiftPressed, ctrlPressed, altPressed)
}
// RemoveAccelerator 删除快捷键
func (m *ICefMenuModel) RemoveAccelerator(commandId MenuId) bool {
return cefMenuModel_RemoveAccelerator(uintptr(m.instance), commandId)
}
// IsVisible 是否显示
func (m *ICefMenuModel) IsVisible(commandId MenuId) bool {
return cefMenuModel_IsVisible(uintptr(m.instance), commandId)
}
// SetVisible 设置是否显示
func (m *ICefMenuModel) SetVisible(commandId MenuId, visible bool) bool {
return cefMenuModel_SetVisible(uintptr(m.instance), commandId, visible)
}
// IsEnabled 是启用
func (m *ICefMenuModel) IsEnabled(commandId MenuId) bool {
return cefMenuModel_IsEnabled(uintptr(m.instance), commandId)
}
// SetEnabled 设置是启用
func (m *ICefMenuModel) SetEnabled(commandId MenuId, enabled bool) bool {
return cefMenuModel_SetEnabled(uintptr(m.instance), commandId, enabled)
}
// SetLabel 设置标签
func (m *ICefMenuModel) SetLabel(commandId MenuId, text string) bool {
return cefMenuModel_SetLabel(uintptr(m.instance), commandId, text)
}
// GetIndexOf 获取下标
func (m *ICefMenuModel) GetIndexOf(commandId MenuId) int32 {
return cefMenuModel_GetIndexOf(uintptr(m.instance), commandId)
}
func (m *ICefMenuModel) GetType(commandId MenuId) TCefMenuItemType {
r1, _, _ := imports.Proc(def.CefMenuModel_GetType).Call(m.Instance(), uintptr(commandId))
return TCefMenuItemType(r1)
}
func (m *ICefMenuModel) GetLabel(commandId MenuId) string {
r1, _, _ := imports.Proc(def.CefMenuModel_GetLabel).Call(m.Instance(), uintptr(commandId))
return api.GoStr(r1)
}
func (m *ICefMenuModel) GetGroupId(commandId MenuId) int32 {
r1, _, _ := imports.Proc(def.CefMenuModel_GetGroupId).Call(m.Instance(), uintptr(commandId))
return int32(r1)
}
func (m *ICefMenuModel) SetGroupId(commandId MenuId, groupId int32) bool {
r1, _, _ := imports.Proc(def.CefMenuModel_SetGroupId).Call(m.Instance(), uintptr(commandId), uintptr(groupId))
return api.GoBool(r1)
}
func (m *ICefMenuModel) GetSubMenu(commandId MenuId) *ICefMenuModel {
r1, _, _ := imports.Proc(def.CefMenuModel_GetSubMenu).Call(m.Instance(), uintptr(commandId))
if r1 != 0 {
return &ICefMenuModel{instance: unsafe.Pointer(r1)}
}
return nil
}
func (m *ICefMenuModel) GetColor(commandId MenuId, colorType TCefMenuColorType) (color types.TCefColor, result bool) {
r1, _, _ := imports.Proc(def.CefMenuModel_GetColor).Call(m.Instance(), uintptr(commandId), uintptr(colorType), uintptr(unsafe.Pointer(&color)))
return color, api.GoBool(r1)
}
// ------------------------------------ PROC
// ICefMenuModel cefMenuModel_AddSeparator
func cefMenuModel_AddSeparator(instance uintptr) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_AddSeparator).Call(instance)
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_Clear
func cefMenuModel_Clear(instance uintptr) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_Clear).Call(instance)
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_IsSubMenu
func cefMenuModel_IsSubMenu(instance uintptr) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_IsSubMenu).Call(instance)
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_GetCount
func cefMenuModel_GetCount(instance uintptr) int32 {
r1, _, _ := imports.Proc(def.CEFMenuModel_GetCount).Call(instance)
return int32(r1)
}
// ICefMenuModel cefMenuModel_AddItem
func cefMenuModel_AddItem(instance uintptr, commandId MenuId, text string) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_AddItem).Call(instance, uintptr(commandId), api.PascalStr(text))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_AddCheckItem
func cefMenuModel_AddCheckItem(instance uintptr, commandId MenuId, text string) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_AddCheckItem).Call(instance, uintptr(commandId), api.PascalStr(text))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_AddRadioItem
func cefMenuModel_AddRadioItem(instance uintptr, commandId MenuId, text string, groupId int32) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_AddRadioItem).Call(instance, uintptr(commandId), api.PascalStr(text), uintptr(groupId))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_AddSubMenu
func cefMenuModel_AddSubMenu(instance uintptr, commandId MenuId, text string) *ICefMenuModel {
var ret uintptr
imports.Proc(def.CEFMenuModel_AddSubMenu).Call(instance, uintptr(commandId), api.PascalStr(text), uintptr(unsafe.Pointer(&ret)))
return &ICefMenuModel{
instance: unsafe.Pointer(ret),
}
}
// ICefMenuModel cefMenuModel_Remove
func cefMenuModel_Remove(instance uintptr, commandId MenuId) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_Remove).Call(instance, uintptr(commandId))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_Remove
func cefMenuModel_RemoveAt(instance uintptr, index int32) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_RemoveAt).Call(instance, uintptr(index))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_SetChecked
func cefMenuModel_SetChecked(instance uintptr, commandId MenuId, check bool) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_SetChecked).Call(instance, uintptr(commandId), api.PascalBool(check))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_IsChecked
func cefMenuModel_IsChecked(instance uintptr, commandId MenuId) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_IsChecked).Call(instance, uintptr(commandId))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_SetColor
func cefMenuModel_SetColor(instance uintptr, commandId MenuId, colorType TCefMenuColorType, color *TCefARGB) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_SetColor).Call(instance, uintptr(commandId), uintptr(colorType), uintptr(color.ARGB()))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_SetFontList
func cefMenuModel_SetFontList(instance uintptr, commandId MenuId, fontList string) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_SetFontList).Call(instance, uintptr(commandId), api.PascalStr(fontList))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_HasAccelerator
func cefMenuModel_HasAccelerator(instance uintptr, commandId MenuId) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_HasAccelerator).Call(instance, uintptr(commandId))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_SetAccelerator
func cefMenuModel_SetAccelerator(instance uintptr, commandId MenuId, keyCode int32, shiftPressed, ctrlPressed, altPressed bool) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_SetAccelerator).Call(instance, uintptr(commandId), uintptr(keyCode),
api.PascalBool(shiftPressed), api.PascalBool(ctrlPressed), api.PascalBool(altPressed))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_RemoveAccelerator
func cefMenuModel_RemoveAccelerator(instance uintptr, commandId MenuId) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_RemoveAccelerator).Call(instance, uintptr(commandId))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_IsVisible
func cefMenuModel_IsVisible(instance uintptr, commandId MenuId) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_IsVisible).Call(instance, uintptr(commandId))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_SetVisible
func cefMenuModel_SetVisible(instance uintptr, commandId MenuId, visible bool) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_SetVisible).Call(instance, uintptr(commandId), api.PascalBool(visible))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_IsEnabled
func cefMenuModel_IsEnabled(instance uintptr, commandId MenuId) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_IsEnabled).Call(instance, uintptr(commandId))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_SetEnabled
func cefMenuModel_SetEnabled(instance uintptr, commandId MenuId, enabled bool) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_SetEnabled).Call(instance, uintptr(commandId), api.PascalBool(enabled))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_SetLabel
func cefMenuModel_SetLabel(instance uintptr, commandId MenuId, text string) bool {
r1, _, _ := imports.Proc(def.CEFMenuModel_SetLabel).Call(instance, uintptr(commandId), api.PascalStr(text))
return api.GoBool(r1)
}
// ICefMenuModel cefMenuModel_GetIndexOf
func cefMenuModel_GetIndexOf(instance uintptr, commandId MenuId) int32 {
r1, _, _ := imports.Proc(def.CEFMenuModel_GetIndexOf).Call(instance, uintptr(commandId))
return int32(r1)
}