Skip to content

[Light Switch] Add CLI for status and theme control - #50459

Merged
Yu Leng (moooyo) merged 16 commits into
mainfrom
yuleng/light-switch-cli-49904
Oct 9, 2026
Merged

Yu Leng (moooyo) merged 16 commits into
mainfrom
yuleng/light-switch-cli-49904

Conversation

@moooyo

@moooyo Yu Leng (moooyo) commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary of the Pull Request

Adds PowerToys.LightSwitch.CLI.exe for querying and controlling the running Light Switch service. Scripts can read status, set or toggle the configured theme targets, and enable or disable the existing schedule, with JSON output and documented exit codes.

Closes #49904.

PR Checklist

  • Closes: [Light Switch] Add CLI for status and theme control #49904
  • Communication: Discussed with core contributors.
  • Tests: Added/updated; 140 native, 221 managed CLI, and 49 Light Switch settings tests pass locally.
  • Localization: User-facing text uses localized resources with English fallback. JSON fields, protocol values, and exit codes remain invariant.
  • Dev docs: Added/updated.
  • New binaries: Added to the solution, signing list, CLI shim manifest, and installer shim components.
  • Documentation updated: External PowerToys documentation PR.

Detailed Description of the Pull Request / Additional comments

  • src/modules/LightSwitch/LightSwitch.Cli/ adds status, light, dark, toggle, schedule enable [--mode], and schedule disable. All commands support --json; help and version work without the service. Enabling a schedule from Off requires an explicit existing mode: fixed-hours, sunset-to-sunrise, or follow-night-light.
  • src/modules/LightSwitch/LightSwitchService/CliPipeServer.cpp and CliProtocol.cpp implement bounded, versioned request/response IPC with user/session/logon isolation, server identity checks in the client, timeouts, and shutdown handling. The CLI requires a running service and does not automatically retry mutations after an uncertain result.
  • src/modules/LightSwitch/LightSwitchService/LightSwitchStateManager.cpp exposes serialized theme operations and status snapshots using the existing scheduling rules. src/modules/LightSwitch/LightSwitchModuleInterface/dllmain.cpp submits counted toggle requests to the service so theme writes and manual-override updates occur together. Theme access failures and partial changes are reported to callers; verified recovery preserves downstream theme notifications.
  • Schedule commands update the existing scheduleMode setting and complete the worker's settings handling before replying. Duplicate settings notifications preserve manual overrides, and settings loaded before a delayed notification are not mistaken for an external theme change. Overrides account for sleep, clock changes, and unavailable Night Light data. src/settings-ui/Settings.UI/SettingsXAML/Views/LightSwitchPage.xaml.cs refreshes after settings-file replacement. Concurrent editors retain the existing whole-file save behavior.
  • CLI help, text labels, and service diagnostics use localized resources. Available CLI and parser satellite resources are packaged without requiring missing translations. Machine-readable protocol and JSON values remain stable.
  • tools/CliShim/CliShimManifest.props, installer/PowerToysSetupVNext/CliShims.wxs, and .pipelines/ESRPSigning_core.json register deployment and signing. The CLI and service are included in BugReport diagnostics. Usage, protocol behavior, and exit codes are documented in doc/devdocs/modules/lightswitch-cli.md.

Validation Steps Performed

  • Latest x64 Debug builds passed for LightSwitchService.UnitTests, LightSwitch.Cli.UnitTests, and Settings.UI.UnitTests, including their Light Switch service, CLI, and Settings UI dependencies. Each build returned exit code 0 with 0 warnings and 0 errors.
  • 140 native service tests passed via vstest.console.exe, covering protocol/identity/framing, shutdown and queue handling, settings persistence, manual overrides, recovery, and schedule boundaries.
  • 221 managed CLI tests passed via vstest.console.exe, covering parsing, output/exit codes, bounded IPC, response validation, and resource-based output.
  • 49 Light Switch settings tests passed via vstest.console.exe with FullyQualifiedName~LightSwitch, covering the ViewModel and settings watcher. This was a filtered run, not the complete Settings test suite.
  • git diff --check passed. The cleanup preserves behavior, removes redundant theme-observation and observer-stop calls, and removes unused imports; existing regression tests cover the affected paths.

Desktop theme/hotkey acceptance, real translated-satellite acceptance, ARM64/Release verification, and full installer acceptance are not covered by these latest checks.

Yu Leng (from Dev Box) and others added 7 commits September 9, 2026 14:44
Authenticate pipe connections by user, session, and logon SID while restoring the service identity before command execution. Initialize missing settings safely at startup and refresh the settings page after atomic file replacement.

Validated x64 Debug builds and 168 tests: 59 native, 106 CLI, and 3 settings tests.
Parse presentation flags with the command-line library and expand response
files once. Refresh the initial settings cache and all derived UI values
without writing stale binding values back to settings.

Preserve manual choices across delayed notifications, unchanged settings,
sleep, and clock rollback. Track full dates for schedule and solar state.
Keep a share-delete settings handle during concurrent startup reads, and
include the CLI and shim in installer process cleanup.

Validation: x64 Debug service, CLI and related UI builds; 277 passing unit
tests; 7 independent state probes; 20 repeated startup diagnostic runs;
local CLI help/version smoke; x64 Release installer custom-action build.
Reject unsupported parser directives before any command executes. Apply
location settings as a complete snapshot, preserve valid offsets, and
project missing sun times safely to TimePicker controls.

Reconcile Night Light after transient failures without discarding newer
manual choices. Distinguish recovered and unverified own theme writes
from external changes. Serialize sun-time persistence with CLI settings
updates and discard calculations for superseded configurations.

Validation: x64 Debug service, CLI and settings UI builds; 323 passing
unit tests and 21 independent probes; x64 Release Native AOT publish and
three local executable smoke checks.
Distinguish unavailable Night Light data from OFF and preserve manual
choices across read failures and recovery. Reuse verified theme snapshots
and replay pending scheduled PowerDisplay notifications without discarding
them when a manual write fails.

Add 18 regression and boundary tests and document recovery behavior.
Validated x64 Debug builds for the service, module interface, and native
test project; all 120 native tests pass.
Preserve pending external theme choices across failed explicit commands and
notify PowerDisplay when an idempotent command or verification retry confirms
a change. Keep scheduled boundaries and newer configurations authoritative.

Share worker recovery between Command Palette and hotkey requests, serialize
module lifecycle operations, and keep saved solar offsets visible in Settings.

Validation: x64 Debug builds passed; 130 native, 193 CLI, and 44 settings tests
passed. Real module event and callback recovery probes each restarted the
service and consumed exactly one request, with the environment restored.
@moooyo

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
1 pipeline(s) were filtered out due to trigger conditions.
Yu Leng (moooyo) and others added 4 commits September 23, 2026 16:37
Localize CLI and service user-facing text while preserving JSON fields,
protocol values, error codes, and exit codes. Package available CLI and
parser satellite resources without requiring missing translations.

Simplify theme verification, minute-tick handling, and unused settings/UI
paths. Include the Light Switch service and CLI in BugReport diagnostics.

Validation: x64 Debug builds; 136 native and 209 CLI tests passed; x64
Release Native AOT publish and JSON smoke checks passed; per-user and
per-machine satellite MSI fixtures passed; git diff --check passed.
Preserve PowerDisplay notifications after unverified or partially applied
toggles without replacing an earlier pending scheduled notification.
Normalize effective solar preview times while retaining saved offsets, and
post the pipe test's accept before a rejected client can disconnect.

Add regression coverage for notification recovery, mixed themes, pending
notifications, wrapped solar times, and overflow-safe display projections.

Validation: x64 Debug service, CLI, and Settings UI builds passed; 140 native,
221 CLI, and 49 settings tests passed. The pipe rejection test also passed
10 repeated runs. git diff --check passed.
Reuse the theme observations already recorded by ToggleTheme and let the
Night Light observer destructor stop its worker. Remove unused native
headers and Settings UI imports without changing behavior.

Validation: x64 Debug builds passed with no warnings or errors; 140 native,
221 CLI, and 49 Light Switch settings tests passed via vstest.console.exe.
@moooyo
Yu Leng (moooyo) marked this pull request as ready for review September 28, 2026 13:01
@moooyo
Yu Leng (moooyo) requested a review from a team as a code owner September 28, 2026 13:01
@github-actions github-actions Bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Sep 28, 2026
@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

🧭 PR intake

Visual evidence: Required — The pull request changes product UI files, so reviewers need to see the visible result. Visual evidence is currently missing.

Author action

Yu Leng (@moooyo), please update the following before review:

  • Add a screenshot, GIF, or video to the PR description so reviewers can validate the visible change.

See the contribution guide for the full checklist.

If there is no author response within 7 days, this PR will be automatically closed.

Automated PR intake; PowerToys maintainers make final decisions.

@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it has been marked as requiring author feedback but has not had any activity for 7 days. If you would like to continue working on it, please reopen the PR and push your changes or leave a comment.

Preserve both the Settings CLI added on main and the Light Switch CLI in the
shared manifest and installer component references, including their original
target paths and component identities.

Validated the six mappings, components, and references; the x64 Debug
CliShim.UnitTests build succeeded and all 8 tests passed via VSTest.
@moooyo

Copy link
Copy Markdown
Contributor Author
Image
@moooyo
Yu Leng (moooyo) merged commit 2742ac2 into main Oct 9, 2026
13 checks passed
@moooyo
Yu Leng (moooyo) deleted the yuleng/light-switch-cli-49904 branch October 9, 2026 05:47
@microsoft-github-policy-service microsoft-github-policy-service Bot added Needs-Triage For issues raised to be triaged and prioritized by internal Microsoft teams Needs-Team-Response An issue author responded so the team needs to follow up and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Oct 9, 2026
Clint Rutkas (crutkas) added a commit that referenced this pull request Oct 9, 2026
Light Switch CLI (#50459) and Settings CLI (#50507) merged to main using System.CommandLine beta APIs, breaking the 2.0 GA upgrade build. Port them to the GA API while preserving command names, options, output, and exit codes. Light Switch clears the default RootCommand help/version/directives, ignores implicit option results, and reports leading [directive] tokens as unrecognized to keep the previous parsing behavior.
Yu Leng (moooyo) added a commit that referenced this pull request Oct 15, 2026
## Summary of the Pull Request

Add `LightSwitch_CLICommand` and `PowerDisplay_CLICommand` using the
Settings CLI telemetry pattern, providing a basic usage signal for the
two CLIs. Each invocation records an allowlisted `CommandName` and a
`Successful` flag based on its final exit code, through the existing
diagnostics consent check.

Related CLI work: #50459 and #48632. Telemetry pattern: #50507.

## PR Checklist

- [x] **Tests:** Existing CLI regression suites pass.
- [x] **Localization:** No new user-facing strings.
- [x] **Dev docs:** Not applicable to this telemetry-only change.
- [x] **New binaries:** No new binaries or external dependencies;
ManagedTelemetry was already a transitive dependency.

## Detailed Description of the Pull Request / Additional comments

- Wrap execution in `src/modules/LightSwitch/LightSwitch.Cli/Program.cs`
and `src/modules/powerdisplay/PowerDisplay.Cli/Program.cs` to attempt
one event per invocation, including help/version, validation failures,
and exceptions. Telemetry failures preserve the command's output, exit
code, and exception.
- Match Settings CLI's first-token allowlist. Option-first and
response-file invocations use `unknown`; raw arguments, monitor IDs,
profile values, and file paths are excluded from telemetry.
- Enable `EventSourceSupport` in both NativeAOT projects and preserve
the event properties for trimming.

## Validation Steps Performed

- Repository `build-essentials.ps1`, Debug/x64: exit 0.
- Existing CLI regression tests passed with `vstest.console.exe`:
LightSwitch **221/221**, PowerDisplay **130/130**.
- Publish both CLIs with `tools/build/build.ps1 /t:Publish`,
Release/win-x64 NativeAOT: exit 0, 0 warnings, 0 errors.
- Eight checks against the published native executables passed: help,
version, and no-argument behavior for both CLIs, LightSwitch JSON help,
and PowerDisplay JSON argument errors. PowerDisplay's existing
missing-command exit code remains 1.
- `git diff --check` passed. Restores used the configured host NuGet
source.

---------

Co-authored-by: Yu Leng (from Dev Box) <yuleng@microsoft.com>
Boliang Zhang (LegendaryBlair) pushed a commit that referenced this pull request Oct 16, 2026
## Summary of the Pull Request

Add `LightSwitch_CLICommand` and `PowerDisplay_CLICommand` using the
Settings CLI telemetry pattern, providing a basic usage signal for the
two CLIs. Each invocation records an allowlisted `CommandName` and a
`Successful` flag based on its final exit code, through the existing
diagnostics consent check.

Related CLI work: #50459 and #48632. Telemetry pattern: #50507.

## PR Checklist

- [x] **Tests:** Existing CLI regression suites pass.
- [x] **Localization:** No new user-facing strings.
- [x] **Dev docs:** Not applicable to this telemetry-only change.
- [x] **New binaries:** No new binaries or external dependencies;
ManagedTelemetry was already a transitive dependency.

## Detailed Description of the Pull Request / Additional comments

- Wrap execution in `src/modules/LightSwitch/LightSwitch.Cli/Program.cs`
and `src/modules/powerdisplay/PowerDisplay.Cli/Program.cs` to attempt
one event per invocation, including help/version, validation failures,
and exceptions. Telemetry failures preserve the command's output, exit
code, and exception.
- Match Settings CLI's first-token allowlist. Option-first and
response-file invocations use `unknown`; raw arguments, monitor IDs,
profile values, and file paths are excluded from telemetry.
- Enable `EventSourceSupport` in both NativeAOT projects and preserve
the event properties for trimming.

## Validation Steps Performed

- Repository `build-essentials.ps1`, Debug/x64: exit 0.
- Existing CLI regression tests passed with `vstest.console.exe`:
LightSwitch **221/221**, PowerDisplay **130/130**.
- Publish both CLIs with `tools/build/build.ps1 /t:Publish`,
Release/win-x64 NativeAOT: exit 0, 0 warnings, 0 errors.
- Eight checks against the published native executables passed: help,
version, and no-argument behavior for both CLIs, LightSwitch JSON help,
and PowerDisplay JSON argument errors. PowerDisplay's existing
missing-command exit code remains 1.
- `git diff --check` passed. Restores used the configured host NuGet
source.

---------

Co-authored-by: Yu Leng (from Dev Box) <yuleng@microsoft.com>
(cherry picked from commit 7f149ab)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs-Team-Response An issue author responded so the team needs to follow up Needs-Triage For issues raised to be triaged and prioritized by internal Microsoft teams Product-LightSwitch

2 participants