Skip to content

Commit

Permalink
fixed the dimmer lib #266
Browse files Browse the repository at this point in the history
  • Loading branch information
ewerybody committed May 4, 2024
1 parent 4d6fbc1 commit cd4781e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 24 deletions.
50 changes: 26 additions & 24 deletions lib/Autohotkey/lib/dimmer.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,64 @@ dimmer_create(area := "") {
global _screen_dimmer_alpha, _screen_dimmer_id
_screen_dimmer_alpha := 128

Gui, New , +AlwaysOnTop -Caption -Border +ToolWindow -Resize +Disabled -DPIScale, ScreenDimmerGui
Gui, +LastFound
WinGet, new_id, ID
Gui, Color, 000000
WinSet, Transparent, 0

if (new_id != _screen_dimmer_id) AND WinExist("ahk_id " _screen_dimmer_id) {
WinClose, ahk_id %_screen_dimmer_id%
dimmer_gui := Gui("+AlwaysOnTop -Caption -Border +ToolWindow -Resize +Disabled -DPIScale +LastFound")
; Gui, New , +AlwaysOnTop -Caption -Border +ToolWindow -Resize +Disabled -DPIScale, ScreenDimmerGui
; Gui, +LastFound
; new_id := WinGetID
dimmer_gui.BackColor := "000000"
try {
if (dimmer_gui.Hwnd != _screen_dimmer_id) AND WinExist("ahk_id " _screen_dimmer_id)
WinClose("ahk_id " _screen_dimmer_id)
}
_screen_dimmer_id := new_id
_screen_dimmer_id := dimmer_gui.Hwnd

if (!IsObject(area)) {
area := screen_get_work_area()
}
area_str := "x" area.left " y" area.top " w" area.w " h" area.h
Gui, Show, %area_str%, ScreenDimmerGui
SetTimer, _dimmer_turn_up, 25
dimmer_gui.Show("x" area.left " y" area.top " w" area.w " h" area.h)
WinSetTransparent(0, "ahk_id " dimmer_gui.Hwnd)
SetTimer _dimmer_turn_up, 25
return _screen_dimmer_id
}

_dimmer_turn_up() {
global _screen_dimmer_alpha, _screen_dimmer_id

WinGet, value, Transparent, ahk_id %_screen_dimmer_id%
value := WinGetTransparent("ahk_id " _screen_dimmer_id)
value += 20
if (value > _screen_dimmer_alpha) {
SetTimer, _dimmer_turn_up, Off
SetTimer _dimmer_turn_up, 0
return
}

WinSet, Transparent, %value%, ahk_id %_screen_dimmer_id%
WinSetTransparent(value, "ahk_id " _screen_dimmer_id)
}

dimmer_off(finished_func := "") {
SetTimer, _dimmer_turn_down, 25
SetTimer _dimmer_turn_down, 25
global _screen_dimmer_finished_func
if IsFunc(finished_func)
if IsObject(finished_func)
_screen_dimmer_finished_func := finished_func
}

_dimmer_turn_down() {
global _screen_dimmer_id, _screen_dimmer_finished_func
id_str := "ahk_id " _screen_dimmer_id
if (!WinExist(id_str)) {
SetTimer, _dimmer_turn_down, Off
SetTimer _dimmer_turn_down, 0
return
}
WinGet, value, Transparent,
value := WinGetTransparent(id_str)
value -= 20
if (value < 0) {
SetTimer, _dimmer_turn_down, Off
WinHide, %id_str%
if IsFunc(_screen_dimmer_finished_func)
%_screen_dimmer_finished_func%()
SetTimer _dimmer_turn_down, 0
WinHide(id_str)
try {
if IsObject(_screen_dimmer_finished_func)
%_screen_dimmer_finished_func%()
}
return
}

WinSet, Transparent, %value%, %id_str%
WinSetTransparent(value, id_str)
}
21 changes: 21 additions & 0 deletions lib/Autohotkey/lib/example/dimmer_example.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#SingleInstance
#include <dimmer>
#Include <screen>
; #Include %A_ScriptDir%\..\
#Include ../test/a2test.ahk

dimmer_id := dimmer_create()
m1 := assertmsg(dimmer_id)
MsgBox(m1 " Created dimmer gui hwnd: " dimmer_id)
dimmer_off()

area := screen_get_work_area()
area.w := Floor(area.w /2)
MsgBox("area:`nx:" area.x " y:" area.y " w:" area.w " h:" area.h)
dimmer_id := dimmer_create(area)
MsgBox(m1 " Created half a dimmer gui hwnd: " dimmer_id)
dimmer_off("_test_dimmer_exit")

_test_dimmer_exit() {
ExitApp
}

0 comments on commit cd4781e

Please sign in to comment.