-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_GuiPrompt.ahk
122 lines (91 loc) · 2.87 KB
/
class_GuiPrompt.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
/**
Class:
GuiPrompt.ahk - Utility class for making prompts
Version:
v1.0.0
Requirements:
AutoHotkey v1.1.21.00+
Usage:
prompt := new GuiPrompt({ promptTitle: "PDF Splitter" })
prompt.AddInput({ textLabel: "How many pages per PDF?"
, variable: "amountInput" })
inputResult := prompt.Show()
if (inputResult) {
msgBox % inputResult["amountInput"]
} else {
msgBox % "User canceled"
}
Links:
Github - https://github.com/sidola/AHK-Utils
*/
class GuiPrompt {
__New(optionObject) {
this.promptTitle := optionObject.promptTitle
this.promptText := optionObject.promptText
this.elements := []
}
/**
* Adds an input to the prompt.
*
* @param optionObject
* An object containing the following properties:
* - textLabel, The field label
* - variable, The field variable
* - value (optional), An initial value
*/
AddInput(optionObject) {
this.elements.Push(optionObject)
}
/**
* Shows the prompt and blocks the current thread.
*
* When done, returns either an empty var if the user canceled,
* or an object containing key/value pairs of the input.
*/
Show() {
guiMargin := 10
innerGuiWidth := 200
outerGuiWidth := innerGuiWidth + (guiMargin * 2)
Gui, GuiPrompt:New, +LastFound +Border -SysMenu
Gui, Margin, %guiMargin%, %guiMargin%
Gui, Font, s9, Segoe UI ; Set Windows default Font
if (this.promptText) {
Gui, Add, Text, w%innerGuiWidth%, % this.promptText
}
for k, options in this.elements {
editVar := options.variable
Gui, Add, Text,, % options.textLabel
Gui, Add, Edit, w%innerGuiWidth% Hwnd%editVar%, % options.value
options.hwnd := %editVar%
}
Gui, Add, Button, w95 HwndOkBtn Section, OK
Gui, Add, Button, w95 HwndCancelBtn ys Default, Cancel
onOkFunc := ObjBindMethod(this, "OnOk")
onCancelFunc := ObjBindMethod(this, "OnCancel")
GuiControl +g, %OkBtn%, % onOkFunc
GuiControl +g, %CancelBtn%, % onCancelFunc
Gui, Show, w%outerGuiWidth%, % this.promptTitle
; "Threading" magic Pt. 1
Pause, On
Gui, Destroy
return this.inputResult
}
; ----------------------------------
; Private API
; ----------------------------------
OnOk() {
this.inputResult := {}
for k, options in this.elements {
hwnd := options.hwnd
GuiControlGet, editOutput,, %hwnd%
this.inputResult[options.variable] := editOutput
}
; "Threading" magic Pt. 2
Pause, Off
}
OnCancel() {
this.inputResult := ""
; "Threading" magic Pt. 2
Pause, Off
}
}