Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
SaifAqqad committed Jan 20, 2024
1 parent 125ff96 commit dbb5f44
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/Lib/DisplayDevices.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class DisplayDevices {

DisplayDevices_UpdateDisplays() {
static method := ObjBindMethod(DisplayDevices, "updateCachedDisplays")
SetTimer, %method%, -10
SetTimer, %method%, -100
}

DisplayDevices.updateCachedDisplays()
Expand Down
5 changes: 4 additions & 1 deletion src/Lib/WinUtils.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ util_log(msg){
static logNum:=0
msg:= Format("#{:i} {}: {}`n", ++logNum, A_TickCount, IsObject(msg)? "Error: " . msg.Message : msg)
A_log.= msg
Try FileOpen(arg_logFile, "a").Write(msg)
if (arg_isDebug || A_DebuggerName)
OutputDebug, % msg
else
Try FileOpen(arg_logFile, "a").Write(msg)
}

util_toString(obj){
Expand Down
24 changes: 12 additions & 12 deletions src/MicMute.ahk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Requires AutoHotkey v1.1.36+
#Requires AutoHotkey v1.1.36+

;compiler directives
;@Ahk2Exe-Let Res = %A_ScriptDir%\resources
Expand Down Expand Up @@ -87,7 +87,7 @@ Global A_startupTime:= A_TickCount
, watched_profile
, mic_actions
, last_modif_time
, arg_isDebug:=0
, arg_isDebug:= A_DebuggerName ? 1 : 0
, arg_profile:=""
, arg_noUI:=0
, arg_reload:= 0
Expand All @@ -97,7 +97,8 @@ Global A_startupTime:= A_TickCount
, args_str:=""
, resources_obj:= new ResourcesManager()
, isFirstLaunch:=0
, isAfterUpdate:=0
, A_AfterUpdate:=0
, A_PreviousVersion:=""
, A_Version:= A_IsCompiled? util_getFileSemVer(A_ScriptFullPath) : U_Version
, sound_player
, osd_wnd
Expand All @@ -109,14 +110,13 @@ Global A_startupTime:= A_TickCount
, WM_SETTINGCHANGE:= 0x001A
, WM_DEVICECHANGE := 0x0219

; parse cli args
util_log("MicMute v" A_Version)
parseArgs()
tray_defaults()
util_log("MicMute v" . A_Version)
util_log(Format("[Main] Running as user {}, A_IsAdmin = {}", A_UserName, A_IsAdmin))
util_log(Format("[Main] {} as user {}, A_IsAdmin = {}, A_Args = {}", arg_reload ? "Reloading" : "Running", A_UserName, A_IsAdmin, args_str))
OnError(Func("util_log"))
if(arg_isUpdater){
util_log("[Main] Updater mode")
util_log("[Main] Starting updater mode")
tray_init_updater()
updater_UI:= new UpdaterUI()
return
Expand Down Expand Up @@ -416,7 +416,7 @@ checkIsIdle(){
}

if (!wasIdle){
util_log("[Main] Detected idling for " current_profile.afkTimeout " ms")
util_log("[Main] Detected idling for " A_TimeIdlePhysical " ms")
wasIdle := true
}

Expand All @@ -441,7 +441,7 @@ checkConfigDiff(){
checkLinkedApps(){
if(watched_profile){
if(!isAppActive(watched_profile.LinkedApp, watched_profile.ForegroundAppsOnly)){
util_log("[Main] Linked app closed: " . watched_profile.LinkedApp)
util_log("[Main] Linked app closed: " watched_profile.LinkedApp)
watched_profile:=""
switchProfile(config_obj.DefaultProfile)
}
Expand All @@ -450,7 +450,7 @@ checkLinkedApps(){

for _i, p in watched_profiles {
if(isAppActive(p.LinkedApp, p.ForegroundAppsOnly)){
util_log("[Main] Detected linked app: " . p.LinkedApp)
util_log("[Main] Detected linked app: " p.LinkedApp)
watched_profile:= p
switchProfile(p.ProfileName)
break
Expand All @@ -460,7 +460,7 @@ checkLinkedApps(){

isAppActive(appFile, foregroundOnly){
if (foregroundOnly) {
windowExists := WinExist("ahk_exe " . appFile)
windowExists := WinExist("ahk_exe " appFile)
WinGet, minState, MinMax, ahk_exe %appFile%

; An app is active in the foreground if it has a window that's not hidden
Expand Down Expand Up @@ -581,7 +581,7 @@ showElevatedWarning(){

configMsg(err){
Thread, NoTimers, 1
MsgBox, 65, MicMute, % (IsObject(err)? err.Message : err) . "`nClick OK to edit configuration"
MsgBox, 65, MicMute, % (IsObject(err)? err.Message : err) "`nClick OK to edit configuration"
IfMsgBox, OK
editConfig()
IfMsgBox, Cancel
Expand Down
64 changes: 30 additions & 34 deletions src/UI/Overlay.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
if (!this._setPosConfig(positionConfig))
Continue

this.options.pos[i] := this.currentPos
this.options.pos[i] := this.relativePosition
isPositionSet := 1
Break
}
Expand All @@ -71,13 +71,13 @@
if (!isPositionSet) {
display := DisplayDevices.getPrimary()

this.windowPosition := display.getAbsolutePosition(this.options.pos[1].X, this.options.pos[1].Y)
this.absolutePosition := display.getAbsolutePosition(this.options.pos[1].X, this.options.pos[1].Y)

; position is not set -> use default position
if (!this.windowPosition)
if (!this.absolutePosition)
this._setDefaultPos(display)
else
this.currentPos := this.options.pos[1] := display.getRelativePosition(this.windowPosition.X, this.windowPosition.Y)
this.relativePosition := this.options.pos[1] := display.getRelativePosition(this.absolutePosition.X, this.absolutePosition.Y)
}
} else {
positionConfig := this.options.pos[1]
Expand All @@ -97,18 +97,18 @@
if (!display)
display := DisplayDevices.getPrimary()

this.windowPosition := display.getAbsolutePosition(positionConfig.X, positionConfig.Y)
this.absolutePosition := display.getAbsolutePosition(positionConfig.X, positionConfig.Y)

; position is not set -> use default position
if (!this.windowPosition)
if (!this.absolutePosition)
this._setDefaultPos(display)
else
this.currentPos := this.options.pos[1] := display.getRelativePosition(this.windowPosition.X, this.windowPosition.Y)
this.relativePosition := this.options.pos[1] := display.getRelativePosition(this.absolutePosition.X, this.absolutePosition.Y)
}
}

if (this.hwnd && this.shown && this.windowPosition.x && this.windowPosition.y)
WinMove, % "ahk_id " this.hwnd, , % this.windowPosition.x, % this.windowPosition.y
if (this.hwnd && this.shown && this.absolutePosition.x && this.absolutePosition.y)
WinMove, % "ahk_id " this.hwnd, , % this.absolutePosition.x, % this.absolutePosition.y
}

_setPosConfig(positionConfig){
Expand All @@ -122,22 +122,22 @@
if (!display)
return false

this.windowPosition := display.getAbsolutePosition(positionConfig.X, positionConfig.Y)
if (!this.windowPosition)
this.absolutePosition := display.getAbsolutePosition(positionConfig.X, positionConfig.Y)
if (!this.absolutePosition)
return false

this.currentPos := display.getRelativePosition(this.windowPosition.X, this.windowPosition.Y)
this.relativePosition := display.getRelativePosition(this.absolutePosition.X, this.absolutePosition.Y)
return true
}

_setDefaultPos(display := "") {
if (!display)
display := DisplayDevices.getPrimary()

this.currentPos := this.options.pos[1] := Overlay.DEFAULT_POSITION.Clone()
this.relativePosition := this.options.pos[1] := Overlay.DEFAULT_POSITION.Clone()
this.options.pos[1].DisplayId := display.id

this.windowPosition := display.getAbsolutePosition(this.options.pos[1].X, this.options.pos[1].Y)
this.absolutePosition := display.getAbsolutePosition(this.options.pos[1].X, this.options.pos[1].Y)
}

_createWindow() {
Expand Down Expand Up @@ -206,11 +206,7 @@
}

_updateLayeredWindow() {
pos := this.windowPosition
if (this.changedPos)
pos := this.changedPos

UpdateLayeredWindow(this.hwnd, this.deviceContext, pos.x, pos.y, this.options.size, this.options.size)
UpdateLayeredWindow(this.hwnd, this.deviceContext, this.absolutePosition.x, this.absolutePosition.y, this.options.size, this.options.size)
}

_clear() {
Expand Down Expand Up @@ -249,20 +245,20 @@
if (!this.changedPos)
return

this.windowPosition.X := xPos := this.changedPos.x
this.windowPosition.Y := yPos := this.changedPos.y
this.absolutePosition.X := xPos := this.changedPos.x
this.absolutePosition.Y := yPos := this.changedPos.y

util_log("[Overlay] Overlay position changed to: " xPos ", " yPos)

this.changedPos := ""
display := DisplayDevices.getByPosition(xPos, yPos)
newPosDisplay := DisplayDevices.getByPosition(xPos, yPos)

; Check if we're on the same display
if (display.Id == this.windowPosition.DisplayId){
relativePos := display.getRelativePosition(xPos, yPos)
if (newPosDisplay.Id == this.absolutePosition.DisplayId){
relativePos := newPosDisplay.getRelativePosition(xPos, yPos)

this.currentPos.X := relativePos.X
this.currentPos.Y := relativePos.Y
this.relativePosition.X := relativePos.X
this.relativePosition.Y := relativePos.Y

config_obj.exportConfig()
return
Expand All @@ -271,32 +267,32 @@
; Check if there's a config for the new display
existingConfigIndex := 0
for i, positionConfig in this.options.pos {
if (positionConfig.displayId = display.Id) {
if (positionConfig.displayId = newPosDisplay.Id) {
existingConfigIndex := i
break
}
}

if (existingConfigIndex) {
this.currentPos := existingConfig := this.options.pos[existingConfigIndex]
this.relativePosition := existingConfig := this.options.pos[existingConfigIndex]

this.options.pos.Delete(existingConfigIndex)
this.options.pos.InsertAt(1, existingConfig)

relativePos := display.getRelativePosition(xPos, yPos)
relativePos := newPosDisplay.getRelativePosition(xPos, yPos)
existingConfig.X := relativePos.X
existingConfig.Y := relativePos.Y

this.windowPosition.DisplayId := display.Id
this.absolutePosition.DisplayId := newPosDisplay.Id

config_obj.exportConfig()
return
}

; No config for the new display, add a new one
this.currentPos := display.getRelativePosition(xPos, yPos)
this.options.pos.InsertAt(1, this.currentPos)
this.windowPosition.DisplayId := display.Id
this.relativePosition := newPosDisplay.getRelativePosition(xPos, yPos)
this.options.pos.InsertAt(1, this.relativePosition)
this.absolutePosition.DisplayId := newPosDisplay.Id

config_obj.exportConfig()
}
Expand All @@ -315,7 +311,7 @@
Gui, % this.hwnd ":Default"
Gui, Show, % Format("w{} h{} x{} y{} NA"
, this.options.size, this.options.size
, this.windowPosition.x, this.windowPosition.y)
, this.absolutePosition.x, this.absolutePosition.y)
this._updateLayeredWindow()
this.shown := 1
}
Expand Down

0 comments on commit dbb5f44

Please sign in to comment.