-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathosd.ahk
175 lines (159 loc) · 5.6 KB
/
osd.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
Class OSD {
static ACCENT:= {"-1":"ffffff" ; MAIN ACCENT 4D5565
,"0":"DC3545" ; OFF ACCENT
,"1":"6eff74"} ; ON ACCENT 007BFF
__New(pos:="", excludeFullscreen:=0, posEditorCallback:=""){
this.excludeFullscreen:= excludeFullscreen
this.state:= 0
;get the primary monitor resolution
SysGet, res, Monitor
this.screenHeight:= resBottom
this.screenWidth:= resRight
;get the primary monitor scaling
this.scale:= A_ScreenDPI/96
;set the OSD width and height
this.width:= Format("{:i}", 200 * this.scale)
this.height:= Format("{:i}", 40 * this.scale)
;set the default pos object
pos:= pos? pos : {x:-1,y:-1}
;get the final pos object
this.pos:= this.getPosObj(pos.x, pos.y)
;set up bound func objects
this.hideFunc:= objBindMethod(this, "hide")
this.onDragFunc:= objBindMethod(this, "__onDrag")
this.onRClickFunc:= objBindMethod(this, "__onRClick")
this.selfDestroy:= objBindMethod(this, "destroy")
this.posEditorCallback:= posEditorCallback
;set the initial OSD theme
this.setTheme(0)
;create the OSD window
this.create()
}
; creates the OSD window
create(){
Gui, New, +Hwndhwnd, OSD
this.hwnd:= hwnd
Gui, +AlwaysOnTop -SysMenu +ToolWindow -caption -Border
Gui, Margin, 15
Gui, Color, % this.theme, % OSD.ACCENT["-1"]
Gui, Font,% Format("s{:i} w500 c{}", 12*this.scale, OSD.ACCENT["-1"]), Segoe UI
Gui, Add, Text,% Format("HwndtxtHwnd w{} Center", this.width-30)
this.hwndTxt:= txtHwnd
}
; hides and destroys the OSD window
destroy(){
Gui,% this.hwnd ":Default"
this.hide()
Gui, Destroy
this.hwnd:= ""
this.hwndTxt:=""
}
; shows the OSD window with the specified text and accent
show(text,accent:=-1){
;if the window can't be shown -> return
if(!this.canShow())
return
;if the window does not exist -> create it first
if(!this.hwnd)
this.create()
Gui, % this.hwnd ":Default" ;set the default window
;set the accent/theme colors
if(color:=OSD.ACCENT[accent ""])
accent:=color
Gui, Color, % this.theme, % accent
Gui, Font,% Format("s{:i} w500 c{}", 12*this.scale, accent)
GuiControl, Font, % this.hwndTxt
text:= this.processText(text)
GuiControl, Text, % this.hwndTxt, %text%
Gui, +LastFound
Winset, Redraw
;show the OSD
Gui, Show, % Format("w{} NoActivate NA x{} y{}", this.width, this.pos.x, this.pos.y)
;make the OSD corners rounded
WinGetPos,,,Width, Height, % "ahk_id " . this.hwnd
WinSet, Region, % Format("w{} h{} 0-0 R{3:i}-{3:i}", Width, Height, 15*this.scale ), % "ahk_id " . this.hwnd
;set the OSD transparency
WinSet, Transparent, 200, % "ahk_id " . this.hwnd
return this.state:= 1
}
; shows the OSD window with the specified text and accent
; and activates a timer to hide it
showAndHide(text, accent:=-1, seconds:=1){
hideFunc:= this.hideFunc
selfDestroy:=this.selfDestroy
this.show(text,accent)
SetTimer, % selfDestroy,% "-" . seconds*1000
}
; shows a draggable OSD window with the specified text and accent
showDraggable(text, accent:=-1){
Gui,% this.hwnd ":Default"
this.show(text, accent)
OnMessage(0x201, this.onDragFunc)
}
; shows a draggable OSD window to set the position
showPosEditor(){
Gui,% this.hwnd ":Default"
this.showdraggable("RClick to confirm")
OnMessage(0x205, this.onRClickFunc)
}
; hides the OSD window
hide(){
if(!this.hwnd)
return
Gui, % this.hwnd ":Default"
OnMessage(0x201, this.onDragFunc, 0)
OnMessage(0x205, this.onRClickFunc, 0)
SetWinDelay -1
duration := 100
MaxTemp := 200
N := 10
Loop %N%{
currentTp := Round(MaxTemp - A_Index * (255 / N))
WinSet, Transparent, %currentTp%, % "ahk_id " . this.hwnd
Sleep % duration / N
}
Gui, Hide
this.state:= 0
}
canShow(){
return this.excludeFullscreen? !this.isWindowFullscreen() : 1
}
setTheme(theme:=""){
if(theme != this.theme)
this.theme:= theme? (theme=1? "f2f2f2" : theme) : "232323"
}
processText(text){
if (StrLen(text)>28)
text:= SubStr(text, 1, 26) . Chr(0x2026) ; fix overflow with ellipsis
return text
}
isWindowFullscreen(win:="A"){
winID := WinExist(win)
if(!winID)
return 0
WinGet style, Style, ahk_id %WinID%
WinGetPos ,,,winW,winH, %winTitle%
return !((style & 0x20800000) or WinActive("ahk_class Progman")
or WinActive("ahk_class WorkerW") or winH < A_ScreenHeight or winW < A_ScreenWidth)
}
getPosObj(x:=-1,y:=-1){
p_obj:= {}
p_obj.x:= x=-1? Round(this.screenWidth/2 - this.width) : x
p_obj.y:= y=-1? this.screenHeight * 0.68 : y ; 0.68
return p_obj
}
__onDrag(wParam, lParam, msg, hwnd){
if(hwnd = this.hwnd)
PostMessage, 0xA1, 2,,, % "ahk_id " this.hwnd
}
__onRClick(wParam, lParam, msg, hwnd){
if(hwnd != this.hwnd)
return
WinGetPos, xPos,yPos
this.hide()
this.pos.x:= xPos
this.pos.y:= yPos
if(IsFunc(this.posEditorCallback))
this.posEditorCallback.Call(xPos,yPos)
}
}