-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathValueFunctions.ahk
55 lines (50 loc) · 1.96 KB
/
ValueFunctions.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
; Returns function that returns some setting value when called with controller.
; Example usage:
/*
new ShowMessage(GetEnvironmentSetting("browser")).SetDescription("Show current browser")
*/
GetEnvironmentSetting(setting) {
return Func("GetEnvironmentSettingFunction").Bind(setting)
}
GetEnvironmentSettingFunction(setting, contr, context) {
return contr.GetEnvironment().GetSetting(GetValue(setting, contr, context))
}
; Shows input in gui. Returns entered string value.
; If user closes gui before pressing Return key, `options.defaultValue` is returned.
; Known limitation: gets stuck when previous command was selected using Enter key.
GetUserInput(options := "") {
static DEFAULT_OPTIONS := { title: "", defaultValue: "Default value" }
options := MergeArrays(DEFAULT_OPTIONS, options)
return Func("_GetUserInput").Bind(options.title, options.defaultValue)
}
_GetUserInput(title, defaultValue, contr, context) {
gui := contr.GetGui()
title := GetValue(title, contr, context)
if (title != "") {
textControl := gui.AddText({ text: title })
}
inputControl := gui.AddTextInput()
state := { submitted: false
, input: ""
, inputControl: inputControl
, defaultValue: defaultValue}
state.returnSubscription := inputControl.SubscribeReturnPressed(Func("_GetUserInputHandler").Bind(state), { duration: "once" })
; this subscription unsubscribes itself, sooner or later gui has to be destroyed
gui.SubscribeGuiDestroyed(Func("_GetUserGuiDestroyedHandler").Bind(state), { duration: "once" })
gui.Show()
while (!state.submitted) {
; spinlock this thread
}
textControl.Disable()
inputControl.Disable()
return state.input
}
_GetUserInputHandler(ByRef state, input) {
state.input := input
state.submitted := true
}
_GetUserGuiDestroyedHandler(ByRef state) {
state.input := state.defaultValue
state.submitted := true
state.returnSubscription.Unsubscribe()
}