Closes #37100 This does not migrate the rest of the solutions (why do we have so many?) Not migrated: - TemplateCmdPalExtension.sln - FancyZonesEditor.sln - BugReportTool.sln - CleanUp_tool.sln - FancyZones_DrawLayoutTest.sln - FancyZones_zonable_tester.sln - FancyZone_HitTest.sln - MonitorReportTool.sln - PowerToyTemplate.sln - StylesReportTool.sln --------- Co-authored-by: vanzue <vanzue@outlook.com>
4.0 KiB
Light Switch
Public Overview – Microsoft Learn
Quick Links
Overview
The Light Switch module lets users automatically transition between light and dark mode using a timed schedule or a keyboard shortcut.
Features
-
Set custom times to start and stop dark mode.
-
Use geolocation to determine local sunrise and sunset times.
-
Apply offsets in sunrise mode (e.g., 15 minutes before sunset).
-
Quickly toggle between modes with a keyboard shortcut (
Ctrl+Shift+Win+Dby default). -
Choose whether theme changes apply to:
- Apps only
- System only
- Both apps and system
Architecture
Main Components
-
Shortcut/Hotkey Listens for a hotkey event. Calling
onHotkey()flips the theme flags.Note: Using the shortcut overrides the current schedule until the next transition event.
-
LightSwitchService.cpp 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.
-
LightSwitchStateManager.cpp handles updating the state based on the signals sent by LightSwitchService.
-
SettingsXAML/LightSwitch Provides the settings UI for configuring schedules, syncing location, and customizing shortcuts.
-
Settings.UI/ViewModels/LightSwitchViewModel.cs Handles updates to the settings file and communicates changes to the front end.
-
modules/LightSwitch/Tests Contains UI tests that verify interactions between the settings UI, system state, and
settings.json.
Data Flow
-
User configures settings in the UI (default: manual mode, light mode from 06:00–18:00).
-
Every minute, the service checks the time.
- If it’s not a threshold, the service sleeps until the next minute.
- If it matches a threshold, the service applies the theme based on settings and returns to sleep.
-
At midnight, when in Sunrise to Sunset mode, the service updates daily sunrise and sunset times.
-
If the machine was asleep during a scheduled event, the service applies the correct settings at the next check.
User Interface
The module’s settings are exposed in the PowerToys Settings UI. Options include:
- Shortcut customization
- Mode selection (Manual or Sunrise to Sunset)
- Manual start/stop times (manual mode only)
- Automatic sunrise/sunset calculation (location-based)
- Time offsets (sunrise mode)
- Target scope (system, apps, or both)
Development Environment Setup
Prerequisites
- Visual Studio 2019 or later
- Windows 10 SDK
- PowerToys repository cloned from GitHub
Building and Testing
-
Clone the repo:
git clone https://github.com/microsoft/PowerToys.git -
Initialize submodules:
git submodule update --init --recursive -
Build the solution:
msbuild -restore -p:RestorePackagesConfig=true -p:Platform=ARM64 -m PowerToys.slnxNote: This may take some time.
-
Set
runneras the startup project and press F5. -
Enable Light Switch in PowerToys Settings.
-
To debug the service:
- Press
Ctrl+Alt+Por go to Debug > Attach to Process. - Select
LightSwitchService.exeand click Attach. - You can now set breakpoints in the service files.
- Press
-
To debug the Settings UI:
- Set the startup project to
PowerToys.Settingsand press F5. - Note: Light Switch settings will not persist in this mode (they depend on the service executable).
- Alternatively, you can attach
PowerToys.Settings.exeto the debugger whilerunneris running to test the full flow with breakpoints.
- Set the startup project to