AutoDarkMode / Windows-Auto-Night-Mode

Automatically switches between the dark and light theme of Windows 10 and Windows 11
GNU General Public License v3.0
7.28k stars 259 forks source link

(Custom scripts issue) [SystemUnlock] Event changes the theme but does not trigger scripts execution #811

Open boredwz opened 8 months ago

boredwz commented 8 months ago

Description

[SystemUnlock] event changes the theme, but does not start script execution.

As I see it, other triggers check the time anyway, somehow using [TimeSwitchModule].

But then why does the script execution not work?


P.S. I also noticed that [TimeSwitchModule] is also triggered at system startup, and the current theme is recognized as Unknown:

log ```log 2024-01-08 23:30:18 | Info | Program.Main: commit hash: 7b6b6eb, build: 10.4.2.2 2024-01-08 23:30:18 | Info | Program.Main: cwd: C:\Users\wz\AppData\Local\Programs\AutoDarkMode\adm-app\AutoDarkModeSvc.exe 2024-01-08 23:30:18 | Debug | Program.Main: config file loaded 2024-01-08 23:30:18 | Debug | RegistryHandler.GetActiveThemePath: current theme tracked by windows: ADMUnmanagedDark, path: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\ADMUnmanagedDark.theme 2024-01-08 23:30:18 | Debug | Program.Main: action queue invoking $
b__11_1 2024-01-08 23:30:18 | Info | AsyncPipeServer.Loop: started npipe server with 5 workers (single in, multi out), request: admpipe_request_wz 2024-01-08 23:30:18 | Info | ComponentManager..ctor: current windows build: 19045.3803 2024-01-08 23:30:18 | Info | ComponentManager..ctor: using apps and system components for newer builds 19044 and up 2024-01-08 23:30:18 | Info | ComponentManager..ctor: using default wallpaper component 2024-01-08 23:30:18 | Info | ComponentManager..ctor: using colorization and cursor switcher for newer builds 19044 and up 2024-01-08 23:30:18 | Info | SystemEventHandler.RegisterThemeEvent: enabling event handler for dark mode on battery state discharging 2024-01-08 23:30:18 | Info | TimeSwitchGovernor.EnableHook: time switch governor selected 2024-01-08 23:30:18 | Debug | BaseComponent`1.RunEnableHook: running enable hook for SystemSwitchThemeFile 2024-01-08 23:30:19 | Debug | BaseComponent`1.RunEnableHook: running enable hook for ScriptSwitch 2024-01-08 23:30:19 | Info | ComponentManager.GetComponentsToUpdate: components queued for update: [ScriptSwitch] 2024-01-08 23:30:19 | Info | ThemeManager.UpdateTheme: dwm management: no refresh required 2024-01-08 23:30:19 | Info | ScriptHandler.Launch: running ADM_scripts_Loader (TimeSwitchModule): "powershell" "-ExecutionPolicy" "Bypass" "-File" ".\#ADM_scripts_Loader.ps1" "-TimeSwitchModule" "-Dark" 2024-01-08 23:30:19 | Info | ThemeManager.UpdateTheme: dark theme switch performed, source: TimeSwitchModule, sunset: 07/01/2024 4:00:00 pm 2024-01-08 23:30:19 | Debug | ModuleTimer.RegisterModule: registered GovernorModule to timer main 2024-01-08 23:30:19 | Debug | ModuleTimer.RegisterModule: registered ModuleWarden to timer main 2024-01-08 23:30:19 | Info | SystemEventHandler.RegisterResumeEvent: enabling theme refresh at system resume (win 10) 2024-01-08 23:30:24 | Info | ScriptHandler.Launch: ADM_scripts_Loader (TimeSwitchModule)'s output: ================== Start ================== Src: TimeSwitchModule [#scripts\qBittorrent.ps1] -Dark [#scripts\YF2_W11.ps1] -Dark =================== End =================== 2024-01-08 23:30:24 | Info | ScriptSwitch.HandleSwitch: update info - previous: Unknown, now: Dark ```

Expected Behavior

No response

Log Data

service.log

```log ... 2024-01-08 10:55:53 | Info | ComponentManager.GetComponentsToUpdate: components queued for update: [SystemSwitchThemeFile, ScriptSwitch] 2024-01-08 10:55:53 | Info | SystemSwitchThemeFile.SwitchSystemTheme: update info - previous: Dark/NoAccent, pending: Light/NoAccent, mode: Switch, accent: no 2024-01-08 10:55:53 | Info | ThemeManager.UpdateTheme: dwm management: full refresh requested by user 2024-01-08 10:55:53 | Info | ScriptHandler.Launch: running ADM_scripts_Loader (TimeSwitchModule): "powershell" "-ExecutionPolicy" "Bypass" "-File" ".\#ADM_scripts_Loader.ps1" "-TimeSwitchModule" 2024-01-08 10:55:54 | Info | Tm2Handler.SetTheme: applied theme ADMUnmanagedLight, from origin: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\light.theme directly via IThemeManager2 2024-01-08 10:55:54 | Info | ThemeManager.UpdateTheme: light theme switch performed, source: TimeSwitchModule, sunrise: 07/01/2024 8:40:00 am 2024-01-08 10:55:54 | Debug | ModuleTimer.RegisterModule: registered GovernorModule to timer main 2024-01-08 10:55:54 | Debug | ModuleTimer.RegisterModule: registered ModuleWarden to timer main 2024-01-08 10:55:54 | Info | SystemEventHandler.RegisterResumeEvent: enabling theme refresh at system resume (win 10) 2024-01-08 10:55:56 | Info | ScriptHandler.Launch: ADM_scripts_Loader (TimeSwitchModule)'s output: ================== Start ================== Src: TimeSwitchModule [#scripts\qBittorrent.ps1] [#scripts\YF2_W11.ps1] =================== End =================== 2024-01-08 10:55:56 | Info | ScriptSwitch.HandleSwitch: update info - previous: Unknown, now: Light 2024-01-08 16:28:39 | Info | SystemEventHandler.HandleTimeChangedEvent: system time changed from 08/01/2024 4:28:39 pm 2024-01-08 16:28:39 | Info | ComponentManager.GetComponentsToUpdate: components queued for update: [SystemSwitchThemeFile, ScriptSwitch] 2024-01-08 16:28:39 | Info | SystemSwitchThemeFile.SwitchSystemTheme: update info - previous: Light/NoAccent, pending: Dark/NoAccent, mode: Switch, accent: no 2024-01-08 16:28:39 | Info | ThemeManager.UpdateTheme: dwm management: full refresh requested by user 2024-01-08 16:28:45 | Info | Tm2Handler.SetTheme: applied theme ADMUnmanagedDark, from origin: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\dark.theme directly via IThemeManager2 2024-01-08 16:28:45 | Info | ThemeManager.UpdateTheme: dark theme switch performed, source: SystemTimeChanged 2024-01-08 16:28:45 | Info | ScriptSwitch.HandleSwitch: update info - previous: Light, now: Dark 2024-01-08 16:28:45 | Info | SystemEventHandler.SystemEvents_PowerModeChanged: system resuming from suspended state, refreshing theme 2024-01-08 16:28:45 | Info | ThemeManager.UpdateTheme: dwm management: full refresh requested by user 2024-01-08 16:28:45 | Info | ThemeManager.UpdateTheme: dwm management: full refresh requested by user 2024-01-08 16:28:45 | Info | Tm2Handler.SetTheme: applied theme ADMUnmanagedDark, from origin: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\dark.theme directly via IThemeManager2 2024-01-08 16:28:45 | Info | ThemeManager.UpdateTheme: refreshed dark theme, source: SystemUnlock 2024-01-08 16:28:45 | Debug | SystemEventHandler.SystemEvents_RefreshPostponeTimers: resynchronizing postpone timers with system clock after resume 2024-01-08 16:28:59 | Info | SystemEventHandler.HandleTimeChangedEvent: system time changed from 08/01/2024 4:28:59 pm 2024-01-08 19:33:28 | Info | SystemEventHandler.HandleTimeChangedEvent: system time changed from 08/01/2024 7:33:28 pm 2024-01-08 19:33:30 | Info | SystemEventHandler.SystemEvents_PowerModeChanged: system resuming from suspended state, refreshing theme 2024-01-08 19:33:30 | Info | ThemeManager.UpdateTheme: dwm management: full refresh requested by user 2024-01-08 19:33:30 | Info | ThemeManager.UpdateTheme: dwm management: full refresh requested by user 2024-01-08 19:33:30 | Info | Tm2Handler.SetTheme: applied theme ADMUnmanagedDark, from origin: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\dark.theme directly via IThemeManager2 2024-01-08 19:33:30 | Info | ThemeManager.UpdateTheme: refreshed dark theme, source: SystemUnlock 2024-01-08 19:33:30 | Debug | SystemEventHandler.SystemEvents_RefreshPostponeTimers: resynchronizing postpone timers with system clock after resume 2024-01-08 19:33:33 | Info | SystemEventHandler.HandleTimeChangedEvent: system time changed from 08/01/2024 7:33:33 pm 2024-01-08 19:34:21 | Debug | AdmConfigMonitor.OnChangedScriptConfig: updated script config file 2024-01-08 19:34:21 | Debug | AdmConfigMonitor.OnChangedScriptConfig: updated script config file 2024-01-08 19:35:04 | Info | SystemEventHandler.HandleTimeChangedEvent: system time changed from 08/01/2024 7:35:04 pm 2024-01-08 19:35:06 | Info | SystemEventHandler.SystemEvents_PowerModeChanged: system resuming from suspended state, refreshing theme 2024-01-08 19:35:06 | Info | ThemeManager.UpdateTheme: dwm management: full refresh requested by user 2024-01-08 19:35:06 | Info | ThemeManager.UpdateTheme: dwm management: full refresh requested by user 2024-01-08 19:35:06 | Info | Tm2Handler.SetTheme: applied theme ADMUnmanagedDark, from origin: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\dark.theme directly via IThemeManager2 2024-01-08 19:35:06 | Info | ThemeManager.UpdateTheme: refreshed dark theme, source: SystemUnlock 2024-01-08 19:35:06 | Debug | SystemEventHandler.SystemEvents_RefreshPostponeTimers: resynchronizing postpone timers with system clock after resume 2024-01-08 19:35:08 | Info | SystemEventHandler.HandleTimeChangedEvent: system time changed from 08/01/2024 7:35:08 pm ... ```

scripts.yaml

```yaml Enabled: true Component: Scripts: - Name: ADM_scripts_Loader (TimeSwitchModule) Command: powershell WorkingDirectory: C:\Users\wz\AppData\Roaming\AutoDarkMode ArgsLight: [-ExecutionPolicy, Bypass, -File, .\#ADM_scripts_Loader.ps1, -TimeSwitchModule] ArgsDark: [-ExecutionPolicy, Bypass, -File, .\#ADM_scripts_Loader.ps1, -TimeSwitchModule, -Dark] AllowedSources: [TimeSwitchModule] - Name: ADM_scripts_Loader (BatteryStatusChanged) Command: powershell WorkingDirectory: C:\Users\wz\AppData\Roaming\AutoDarkMode ArgsLight: [-ExecutionPolicy, Bypass, -File, .\#ADM_scripts_Loader.ps1, -BatteryStatusChanged] ArgsDark: [-ExecutionPolicy, Bypass, -File, .\#ADM_scripts_Loader.ps1, -BatteryStatusChanged, -Dark] AllowedSources: [BatteryStatusChanged] - Name: ADM_scripts_Loader (Manual) Command: powershell WorkingDirectory: C:\Users\wz\AppData\Roaming\AutoDarkMode ArgsLight: [-ExecutionPolicy, Bypass, -File, .\#ADM_scripts_Loader.ps1, -Manual] ArgsDark: [-ExecutionPolicy, Bypass, -File, .\#ADM_scripts_Loader.ps1, -Manual, -Dark] AllowedSources: [Manual] - Name: ADM_scripts_Loader (SystemResume) Command: powershell WorkingDirectory: C:\Users\wz\AppData\Roaming\AutoDarkMode ArgsLight: [-ExecutionPolicy, Bypass, -File, .\#ADM_scripts_Loader.ps1, -SystemResume] ArgsDark: [-ExecutionPolicy, Bypass, -File, .\#ADM_scripts_Loader.ps1, -SystemResume, -Dark] AllowedSources: [SystemResume] - Name: ADM_scripts_Loader (SystemUnlock) Command: powershell WorkingDirectory: C:\Users\wz\AppData\Roaming\AutoDarkMode ArgsLight: [-ExecutionPolicy, Bypass, -File, .\#ADM_scripts_Loader.ps1, -SystemUnlock] ArgsDark: [-ExecutionPolicy, Bypass, -File, .\#ADM_scripts_Loader.ps1, -SystemUnlock, -Dark] AllowedSources: [SystemUnlock] ```

config.yaml

```yaml AutoThemeSwitchingEnabled: true Governor: Default Autostart: Validate: false WindowsThemeMode: Enabled: true DarkThemePath: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\dark.theme LightThemePath: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\light.theme MonitorActiveTheme: false ApplyFlags: [] AppsSwitch: Enabled: true Component: Mode: Switch SystemSwitch: Enabled: true Component: Mode: Switch TaskbarSwitchDelay: 1200 TaskbarColorOnAdaptive: false TaskbarColorWhenNonAdaptive: Light DWMPrevalenceSwitch: false DWMPrevalenceEnableTheme: Light TouchKeyboardSwitch: Enabled: false Component: {} ColorizationSwitch: Enabled: false Component: LightHex: '' DarkHex: '' LightAutoColorization: false DarkAutoColorization: false ColorFilterSwitch: Enabled: false Component: {} OfficeSwitch: Enabled: false Component: Mode: Switch LightTheme: 0 DarkTheme: 4 CursorSwitch: Enabled: false Component: CursorsLight: CursorsDark: Sunrise: 2024-01-07T08:40:00.0000000 Sunset: 2024-01-07T16:00:00.0000000 Location: PollingCooldownTimeSpan: 1.00:00:00 Enabled: false UseGeolocatorService: true SunsetOffsetMin: 0 SunriseOffsetMin: 0 CustomLat: 0 CustomLon: 0 Tunable: BatterySliderDefaultValue: 25 DisableEnergySaverOnThemeSwitch: false UseLogonTask: false Debug: true Trace: false DebugTimerMessage: false ShowTrayIcon: true AlwaysFullDwmRefresh: true UICulture: en GPUMonitoring: Enabled: false Threshold: 30 Samples: 1 ProcessBlockList: ProcessNames: [] Enabled: false Events: DarkThemeOnBattery: true Win10AllowLockscreenSwitch: true Notifications: OnAutoThemeSwitching: true OnSkipNextSwitch: true AutoSwitchNotify: Enabled: false GracePeriodMinutes: 5 Hotkeys: Enabled: false ForceLight: ForceDark: NoForce: ToggleTheme: TogglePostpone: ToggleAutoThemeSwitch: IdleChecker: Enabled: false Threshold: 5 WallpaperSwitch: Enabled: false Component: TypeLight: Individual TypeDark: Individual Position: Fill GlobalWallpaper: Light: Dark: SolidColors: Light: '#FFFFFF' Dark: '#000000' Monitors: [] Updater: AutoInstall: false Enabled: false Silent: false DaysBetweenUpdateCheck: 7 CheckOnStart: false VersionQueryUrl: https://raw.githubusercontent.com/AutoDarkMode/AutoDarkModeVersion/master/version-beta.yaml DownloadBaseUrl: ZipCustomUrl: HashCustomUrl: ```

Full service.log

Commit Hash, Version and Windows Build

Screenshots / Videos

Image

![Screenshot_2](https://github.com/AutoDarkMode/Windows-Auto-Night-Mode/assets/87862400/220e6675-497a-43e8-8ade-263f8c17ddc5)
Spiritreader commented 8 months ago

Hey, thanks for the detailed report!

I also noticed that [TimeSwitchModule] is also triggered at system startup, and the current theme is recognized as Unknown:`

At startup, ADM has no way of knowing which state the system is in when it comes to user scripts as it can't track what the user is doing when the application is not active. So it will run the scripts once at startup to bring the system into a known state. That is normal.


The reason you're not seeing the SystemUnlock event triggering your script is actually not because the trigger defective, but because there is a

2024-01-08 16:28:45 | Info | ThemeManager.UpdateTheme: dark theme switch performed, source: SystemTimeChanged 

event that predates the trigger events you are looking to use.

Since you're on Windows 10 and you have Win10AllowLockscreenSwitch: true enabled, the system time change will "eat" the event.

See: https://github.com/AutoDarkMode/Windows-Auto-Night-Mode/commit/776a22f24f43fd4aa86956617fcc3ec0c7c9a435

I've pushed a new beta build that fixes a DWM refresh issue with the theme mode that you encountered, as well as disallowing time synchronization theme switches to occur during the lock screen, which should yield the correct event. It also fixes theme application at system unlock with the theme mode when the active target theme was already correct by introducing an additional synchronization.

Lemme know if that fixes your issue. What you're experiencing is sort of a cumulative result of multiple little bugs with the theme mode, gotta squash them one by one.

boredwz commented 7 months ago

Since you're on Windows 10 and you have Win10AllowLockscreenSwitch: true enabled, the system time change will "eat" the event.

Indeed, after disabling that, it now works correctly.

<Theme Switcher time change to 18:07>

2024-01-12 18:05:17 | Debug | AdmConfigMonitor.PerformConfigUpdate: updated configuration from file 
2024-01-12 18:05:17 | Info | MessageParser.Parse: signal received: invoke theme switch request 
2024-01-12 18:05:17 | Info | ComponentManager.GetComponentsToUpdate: components queued for update: [ScriptSwitch] 
2024-01-12 18:05:17 | Info | ThemeManager.UpdateTheme: dwm management: full refresh requested by user 
2024-01-12 18:05:17 | Info | ScriptHandler.Launch: running ADM_scripts_Loader (Manual): "powershell" "-ExecutionPolicy" "Bypass" "-File" ".\#ADM_scripts_Loader.ps1" "-Manual" 
2024-01-12 18:05:21 | Info | Tm2Handler.SetTheme: applied theme ADMUnmanagedLight, from origin: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\light.theme directly via IThemeManager2 
2024-01-12 18:05:21 | Info | ThemeManager.UpdateTheme: light theme switch performed, source: Manual 
2024-01-12 18:05:23 | Info | ScriptHandler.Launch: ADM_scripts_Loader (Manual)'s output:  
================== Start ==================
Src: Manual
[#scripts\qBittorrent.ps1]
[#scripts\YF2_W11.ps1]
=================== End =================== 
2024-01-12 18:05:23 | Info | ScriptSwitch.HandleSwitch: update info - previous: Dark, now: Light 
2024-01-12 18:05:34 | Debug | PostponeManager.Add: added SessionLock to postpone queue: [SessionLock] 

<Sleep or Hibernate>

2024-01-12 18:08:35 | Debug | SystemEventHandler.SystemEvents_RefreshPostponeTimers: resynchronizing postpone timers with system clock after resume 
2024-01-12 18:08:48 | Debug | PostponeManager.Remove: removed SessionLock from postpone queue: [] 
2024-01-12 18:08:48 | Info | PostponeManager.Remove: postpone queue cleared 
2024-01-12 18:08:48 | Info | SystemEventHandler.SystemEvents_Windows11_SessionSwitch: system unlocked, refreshing theme 
2024-01-12 18:08:48 | Debug | RegistryHandler.GetActiveThemePath: current theme tracked by windows: ADMUnmanagedLight, path: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\ADMUnmanagedLight.theme 
2024-01-12 18:08:48 | Info | ComponentManager.GetComponentsToUpdate: components queued for update: [ScriptSwitch] 
2024-01-12 18:08:48 | Info | ThemeManager.UpdateTheme: dwm management: full refresh requested by user 
2024-01-12 18:08:48 | Info | ScriptHandler.Launch: running ADM_scripts_Loader (SystemUnlock): "powershell" "-ExecutionPolicy" "Bypass" "-File" ".\#ADM_scripts_Loader.ps1" "-SystemUnlock" "-Dark" 
2024-01-12 18:08:52 | Info | Tm2Handler.SetTheme: applied theme ADMUnmanagedDark, from origin: C:\Users\wz\AppData\Local\Microsoft\Windows\Themes\dark.theme directly via IThemeManager2 
2024-01-12 18:08:52 | Info | ThemeManager.UpdateTheme: refreshed dark theme, source: SystemUnlock 
2024-01-12 18:08:54 | Info | ScriptHandler.Launch: ADM_scripts_Loader (SystemUnlock)'s output:  
================== Start ==================
Src: SystemUnlock
[#scripts\qBittorrent.ps1] -Dark
[#scripts\YF2_W11.ps1] -Dark
=================== End =================== 
2024-01-12 18:08:54 | Info | ScriptSwitch.HandleSwitch: update info - previous: Light, now: Dark