-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathclass_MenuEnumOption.ahk
128 lines (108 loc) · 3.57 KB
/
class_MenuEnumOption.ahk
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
/*
* Author: Gerrard Lukacs
*/
/* This include script facilitates adding multi-item options to menus.
* To use, create your MenuEnumOption instances, and have their items' labels
* point to a block of code which contains the following:
* MenuEnumOption.options[A_ThisLabel].setVariable(A_ThisLabel)
* You may also include additional processing in the label block; note that
* the state of the controlled variable is updated after the call.
*/
class MenuEnumItem
{
name := ""
label := ""
__New(name, label)
{
this.name := name
this.label := label
}
add(menuName)
{
; Add the item to the 'menuName' menu.
Menu % menuName, Add, % this.name, % this.label
}
remove(menuName)
{
; Remove the item from the 'menuName' menu.
Menu % menuName, Delete, % this.name
}
setChecked(menuName, checked)
{
; Check/uncheck the item in the 'menuName' menu.
if (checked)
Menu % menuName, Check, % this.name
else
Menu % menuName, Uncheck, % this.name
}
}
class MenuEnumOption
{
; A dictionary mapping from labels of all MenuEnumItems to the
; corresponding MenuEnumOption instances.
static options := {}
menu := ""
variableName := ""
itemsList := 0 ; MenuEnumItems in the order they appear in the menu.
itemsMap := 0 ; A dictionary from item labels to MenuEnumItem instances.
menuItemsExist := false
__New(menuName, variableName, enumItems, addImmediately=true)
{
/* Construct a new MenuEnumOption.
*
* menuName - The name of the menu this option will reside in.
* variableName - The name of the variable this option controls.
* enumItems - A list of MenuEnumItems representing the choices this
* option allows. Menu items will appear in the same order
* they appear in this list.
* addImmediately - If true, immediately create the menu items for
* this option. True by default.
*/
this.menu := menuName
this.variableName := variableName
this.itemsList := enumItems
this.itemsMap := {}
for _, item in enumItems
{
this.itemsMap[item.label] := item
MenuEnumOption.options[item.label] := this
}
if (addImmediately)
this.add()
}
getVariable()
{
; Get the value of the variable this option controls.
variableName := this.variableName
return (%variableName%)
}
setVariable(itemLabel)
{
; Set the value of the variable this option controls to the
; MenuEnumItem corresponding to itemLabel. This automatically
; checks/unchecks the menu items, if they exist.
variableName := this.variableName
oldItem := %variableName%
%variableName% := this.itemsMap[itemLabel]
if (this.menuItemsExist)
{
oldItem.setChecked(this.menu, false)
%variableName%.setChecked(this.menu, true)
}
}
add()
{
; Add the option's items to its menu.
for _, item in this.itemsList
item.add(this.menu)
this.getVariable().setChecked(this.menu, true)
this.menuItemsExist := true
}
remove()
{
; Remove the option's items from its menu.
for _, item in this.itemsList
item.remove(this.menu)
this.menuItemsExist := false
}
}