-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_MenuToggleOption.ahk
114 lines (94 loc) · 3.24 KB
/
class_MenuToggleOption.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
/*
* Author: Gerrard Lukacs
*/
/* This include script facilitates adding toggleable options to menus.
* To use, create your MenuToggleOption instances, and have their labels point
* to a block of code which contains the following:
* MenuToggleOption.options[A_ThisLabel].toggleVariable()
* You may also include additional processing in the label block; note that
* the state of the controlled variable is updated after the call.
*/
class MenuToggleOption
{
; A dictionary mapping from labels to the corresponding MenuToggleOption instances.
static options := {}
menu := ""
item := ""
label := ""
variableName := ""
menuItemExists := false
__New(menuName, itemName, labelName, variableName, addImmediately=true)
{
/* Construct a new MenuToggleOption.
*
* menuName - The name of the menu this option will reside in.
* itemName - The name of the menu item to represent this option.
* labelName - The name of the label the menu item triggers.
* variableName - The name of the variable this option controls.
* addImmediately - If true, immediately create the menu item for this
* option. True by default.
*/
this.menu := menuName
this.item := itemName
this.label := labelName
this.variableName := variableName
MenuToggleOption.options[this.label] := this
if (addImmediately)
this.add()
}
getVariable()
{
; Get the value of the variable this option controls.
variableName := this.variableName
return (%variableName%)
}
setVariable(value)
{
; Set the value of the variable this option controls to 'value'.
; This automatically checks/unchecks the menu item, if it exists.
variableName := this.variableName
%variableName% := value
if (this.menuItemExists)
{
if (value)
Menu % this.menu, Check, % this.item
else
Menu % this.menu, Uncheck, % this.item
}
}
toggleVariable()
{
; Toggle the variable this option controls, and return its resulting value.
; This automatically checks/unchecks the menu item, if it exists.
variableName := this.variableName
%variableName% := !%variableName%
if (this.menuItemExists)
Menu % this.menu, ToggleCheck, % this.item
return (%variableName%)
}
add()
{
; Add the option to its menu.
Menu % this.menu, Add, % this.item, % this.label
if (this.getVariable())
Menu % this.menu, Check, % this.item
this.menuItemExists := true
}
remove()
{
; Remove the option from its menu.
Menu % this.menu, Delete, % this.item
this.menuItemExists := false
}
setEnabled(enabled)
{
; Enable/disable the menu item, if it exists
if (this.menuItemExists)
{
if (enabled)
Menu % this.menu, Enable, % this.item
else
Menu % this.menu, Disable, % this.item
}
}
}