-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayin.ahk
262 lines (220 loc) · 8.54 KB
/
layin.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
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
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance, Force
CoordMode, Mouse, Screen
class Configuration {
static Width := "30"
static Height := "20"
static FontColor := "ffffff"
static FontSize := "12"
static DefaultBackground := "555555"
static DefaultText := "##"
static Transparency := 255
static LayoutMap := { 1033: { Name: "EN", Color: "00CC00" }}
static LayoutScanPeriodMs := 200
static EnableAlwaysOn := true
static AlwaysOnCursorType := "IBeam"
static NameSpecificConfigFileName := SubStr( A_ScriptName, 1, -3 ) . "ini"
FindLayoutParams(code) {
local layoutParams := this.LayoutMap[code]
local defaultParams := { Name: this.DefaultText, Color: this.DefaultBackground }
return ((layoutParams) ? (layoutParams) : (defaultParams))
}
HandleParseError(line, context="") {
OutputDebug, Bad parameter line read from config file: %line% (context: %context%)
}
/**
Example config:
[Indicator]
width = 29
height = 20
fontColor = ffffff
fontSize = 12
transparency = 180
layoutScanPeriodMs = 200
[Languages]
EN = 1033;00bb00
HE = 1037;5555ff
*/
ReadConfigFile() {
local ConfigFileContents, FirstChar, SeparatorPosition, SectionName, ParamKey, ParamValue, ParamValueValid
if (FileExist(this.NameSpecificConfigFileName)) {
FileRead, ConfigFileContents, % this.NameSpecificConfigFileName
} else {
MsgBox, Conguration INI file was not found, will use default configuration
return
}
if (ErrorLevel != 0 or !ConfigFileContents ) {
MsgBox, Failed to read INI file: %A_LastError%, will use default configuration
return
}
Loop, Parse, ConfigFileContents, `n`r, %A_Space%%A_Tab%
{
if (!Trim(A_LoopField)) {
continue
}
FirstChar := SubStr(A_LoopField, 1, 1)
if (FirstChar = "[") {
SectionName := SubStr(A_LoopField, 2, -1)
continue
}
if (FirstChar = ";") {
continue
}
SeparatorPosition := InStr(A_LoopField, "=")
if (!SeparatorPosition) {
OutputDebug, Bad parameter line read from config file: %A_LoopField%
continue
}
ParamKey := Trim(SubStr(A_LoopField, 1, SeparatorPosition - 1))
ParamValue := Trim(SubStr(A_LoopField, SeparatorPosition + 1))
switch SectionName {
case "Indicator":
ParamValueValid := false
switch ParamKey {
case "Width":
ParamValueValid := RegExMatch(ParamValue, "\d+")
if (ParamValueValid) {
this.Width := ParamValue
}
case "Height":
ParamValueValid := RegExMatch(ParamValue, "\d+")
if (ParamValueValid) {
this.Height := ParamValue
}
case "FontColor":
ParamValueValid := RegExMatch(ParamValue, "i)[\da-f]{6}")
if (ParamValueValid) {
this.FontColor := ParamValue
}
case "FontSize":
ParamValueValid := RegExMatch(ParamValue, "\d+")
if (ParamValueValid) {
this.FontSize := ParamValue
}
case "Transparency":
ParamValueValid := RegExMatch(ParamValue, "\d+")
if (ParamValueValid and 0 < ParamValue and ParamValue <= 255) {
this.Transparency := ParamValue
}
case "LayoutScanPeriodMs":
ParamValueValid := RegExMatch(ParamValue, "\d+")
if (ParamValueValid) {
this.LayoutScanPeriodMs := ParamValue
}
}
if (!ParamValueValid) {
this.HandleParseError(A_LoopField, SectionName)
}
case "Languages":
if (RegExMatch(ParamKey, "i)[a-z]{2,5}") and RegExMatch(ParamValue, "i)(\d{4,5})\s*;\s*([\da-f]{6})", ParamValueGroup)) {
this.LayoutMap[ParamValueGroup1] := { Name: ParamKey, Color: ParamValueGroup2 }
}
else {
this.HandleParseError(A_LoopField, SectionName)
}
default:
this.HandleParseError(A_LoopField)
}
}
ConfigFileContents := ""
}
}
GuiLangName := ""
class IndicatorGui {
static Width := 30
static Height := 20
static GuiHwnd := ""
static TransparencyLevel := 255
Init(width, height, fontSize, fontColor, backgroundColor, transparencyLevel, initialText="EN") {
this.Width := width
this.Height := height
this.TransparencyLevel := transparencyLevel
Gui, +AlwaysOnTop +Disabled -SysMenu -Caption +Owner +HwndIndicatorGuiHwnd
this.GuiHwnd := IndicatorGuiHwnd
Gui, Margin, 2, 1
Gui, Font, s%fontSize% c%fontColor% Bold
Gui, Add, Text, vGuiLangName, % initialText
Gui, Color, backgroundColor
}
Show(xPos, yPos, color, text, doHide=true) {
GuiControl,, GuiLangName, %text%
Gui, Color, c%color%
Gui, Show, % "w" . this.Width . " h" . this.Height . " x" . xPos . " y" . yPos . " NoActivate"
WinSet, Transparent, % this.TransparencyLevel, % "ahk_id " . this.GuiHwnd
if (doHide) {
HideMethodRef := ObjBindMethod(this, "Hide")
SetTimer, % HideMethodRef, -1000
}
}
Hide() {
Gui, Cancel
}
}
class Util {
GetActiveLayout() {
active_hwnd := WinExist("A")
threadID := dllCall("GetWindowThreadProcessId", "uint", active_hwnd, "uint", 0)
code := dllCall("GetKeyboardLayout", "uint", threadID, "uint") & 0xFFFF
return code
}
GetCurrentCursor() {
return A_cursor
}
GetCoordsAtMousePosition() {
MouseGetPos, xPos, yPos
xPos := xPos + 15
yPos := yPos + 15
return { x: xPos, y: yPos }
}
GetCoordsAtCaretPosition() {
xPos := A_CaretX + 5
yPos := A_CaretY + 12
return { x: xPos, y: yPos }
}
}
Configuration.ReadConfigFile()
ActiveLayout := Util.GetActiveLayout()
PreviousLayout := ActiveLayout
LayoutParams := Configuration.FindLayoutParams(ActiveLayout)
IndicatorGui.Init(Configuration.Width
, Configuration.Height
, Configuration.FontSize
, Configuration.FontColor
, Configuration.DefaultBackground
, Configuration.Transparency
, LayoutParams.Name)
IndicatorCoords := Util.GetCoordsAtMousePosition()
IndicatorGui.Show(IndicatorCoords.x, IndicatorCoords.y, LayoutParams.Color, LayoutParams.Name)
Cursor := Util.GetCurrentCursor()
PrevCursor := Cursor
loop {
sleep, Configuration.LayoutScanPeriodMs
AutoHide := false
ActiveLayout := Util.GetActiveLayout()
IndicatorCoords := ""
ControlGetFocus, FocusedControl, A
if (!ErrorLevel) {
IndicatorCoords := Util.GetCoordsAtCaretPosition()
}
if (!IndicatorCoords.x) {
IndicatorCoords := Util.GetCoordsAtMousePosition()
Cursor := Util.GetCurrentCursor()
if (Cursor != Configuration.AlwaysOnCursorType) {
if (PrevCursor = Configuration.AlwaysOnCursorType) {
IndicatorGui.Hide()
}
if (ActiveLayout = PreviousLayout) {
continue
}
AutoHide := true
}
}
LayoutParams := Configuration.FindLayoutParams(ActiveLayout)
IndicatorGui.Show(IndicatorCoords.x, IndicatorCoords.y, LayoutParams.Color, LayoutParams.Name, AutoHide)
PreviousLayout := ActiveLayout
PrevCursor := Cursor
}
+esc::exitapp ;press Shift-Escape to close script