lightswitch.md
1 # Light Switch 2 3 [Public Overview – Microsoft Learn](https://learn.microsoft.com/en-us/windows/powertoys/light-switch) 4 5 ## Quick Links 6 7 * [All Issues](https://github.com/microsoft/PowerToys/issues?q=is%3Aissue%20state%3Aopen%20label%3AProduct-LightSwitch) 8 * [Bugs](https://github.com/microsoft/PowerToys/issues?q=is%3Aissue%20state%3Aopen%20label%3AProduct-LightSwitch%20label%3AIssue-Bug) 9 * [Pull Requests](https://github.com/microsoft/PowerToys/pulls?q=is%3Apr+is%3Aopen+label%3AProduct-LightSwitch) 10 11 ## Overview 12 13 The **Light Switch** module lets users automatically transition between light and dark mode using a timed schedule or a keyboard shortcut. 14 15 ## Features 16 17 * Set custom times to start and stop dark mode. 18 * Use geolocation to determine local sunrise and sunset times. 19 * Apply offsets in sunrise mode (e.g., 15 minutes before sunset). 20 * Quickly toggle between modes with a keyboard shortcut (`Ctrl+Shift+Win+D` by default). 21 * Choose whether theme changes apply to: 22 23 * Apps only 24 * System only 25 * Both apps and system 26 27 ## Architecture 28 29 ### Main Components 30 31 * **Shortcut/Hotkey** 32 Listens for a hotkey event. Calling `onHotkey()` flips the theme flags. 33 34 > **Note:** Using the shortcut overrides the current schedule until the next transition event. 35 36 * **LightSwitchService.cpp** 37 is the heart beat of the module. Controls ticking every minute and depending on user actions (manual override, settings changing, etc) triggers the state manager to perform the corresponding operation. 38 39 * **LightSwitchStateManager.cpp** 40 handles updating the state based on the signals sent by LightSwitchService. 41 42 * **SettingsXAML/LightSwitch** 43 Provides the settings UI for configuring schedules, syncing location, and customizing shortcuts. 44 45 * **Settings.UI/ViewModels/LightSwitchViewModel.cs** 46 Handles updates to the settings file and communicates changes to the front end. 47 48 * **modules/LightSwitch/Tests** 49 Contains UI tests that verify interactions between the settings UI, system state, and `settings.json`. 50 51 ### Data Flow 52 53 1. User configures settings in the UI (default: manual mode, light mode from 06:00–18:00). 54 2. Every minute, the service checks the time. 55 56 * If it’s not a threshold, the service sleeps until the next minute. 57 * If it matches a threshold, the service applies the theme based on settings and returns to sleep. 58 3. At **midnight**, when in *Sunrise to Sunset* mode, the service updates daily sunrise and sunset times. 59 4. If the machine was asleep during a scheduled event, the service applies the correct settings at the next check. 60 61 ## User Interface 62 63 The module’s settings are exposed in the PowerToys Settings UI. Options include: 64 65 * Shortcut customization 66 * Mode selection (Manual or Sunrise to Sunset) 67 * Manual start/stop times (manual mode only) 68 * Automatic sunrise/sunset calculation (location-based) 69 * Time offsets (sunrise mode) 70 * Target scope (system, apps, or both) 71 72 ## Development Environment Setup 73 74 ### Prerequisites 75 76 * Visual Studio 2019 or later 77 * Windows 10 SDK 78 * PowerToys repository cloned from GitHub 79 80 ### Building and Testing 81 82 1. Clone the repo: 83 84 ```sh 85 git clone https://github.com/microsoft/PowerToys.git 86 ``` 87 2. Initialize submodules: 88 89 ```sh 90 git submodule update --init --recursive 91 ``` 92 3. Build the solution: 93 94 ```sh 95 msbuild -restore -p:RestorePackagesConfig=true -p:Platform=ARM64 -m PowerToys.slnx 96 ``` 97 98 > Note: This may take some time. 99 4. Set `runner` as the startup project and press **F5**. 100 5. Enable Light Switch in PowerToys Settings. 101 6. To debug the service: 102 103 * Press `Ctrl+Alt+P` or go to **Debug > Attach to Process**. 104 * Select `LightSwitchService.exe` and click **Attach**. 105 * You can now set breakpoints in the service files. 106 7. To debug the Settings UI: 107 108 * Set the startup project to `PowerToys.Settings` and press **F5**. 109 * Note: Light Switch settings will not persist in this mode (they depend on the service executable). 110 * Alternatively, you can attach `PowerToys.Settings.exe` to the debugger while `runner` is running to test the full flow with breakpoints.