diff --git a/.github/actions/spell-check/expect.txt b/.github/actions/spell-check/expect.txt index 2d7280082c..676195d71e 100644 --- a/.github/actions/spell-check/expect.txt +++ b/.github/actions/spell-check/expect.txt @@ -228,6 +228,7 @@ CImage cla CLASSDC CLASSNOTAVAILABLE +CLEARTYPE clickable clickonce clientedge @@ -266,6 +267,7 @@ colorhistory colorhistorylimit COLORKEY colorref +Convs comctl comdlg comexp @@ -414,6 +416,7 @@ devpal dfx DIALOGEX digicert +diffs DINORMAL DISABLEASACTIONKEY DISABLENOSCROLL @@ -562,10 +565,13 @@ eyetracker FANCYZONESDRAWLAYOUTTEST FANCYZONESEDITOR FARPROC +fdw fdx +FErase fesf FFFF FFh +FInc Figma FILEEXPLORER fileexploreraddons @@ -609,6 +615,7 @@ foundrylocal FPrimary FRAMECHANGED Framechanged +FRestore frm FROMTOUCH fsanitize @@ -643,6 +650,7 @@ GETSCREENSAVERRUNNING GETSECKEY GETSTICKYKEYS GETTEXTLENGTH +gfx GHND gitmodules GMEM @@ -697,6 +705,7 @@ hdwwiz Helpline helptext HGFE +hgdiobj hglobal hhk HHmmssfff @@ -746,7 +755,7 @@ hotspot HPAINTBUFFER HPhysical HRAWINPUT -HREDRAW +hredraw hres hresult hrgn @@ -932,7 +941,7 @@ LINKOVERLAY LINQTo listview LIVEDRAW -LIVEZOOM +livezoom LLKH llkhf LMEM @@ -961,6 +970,7 @@ LPBITMAPINFOHEADER LPCFHOOKPROC LPCITEMIDLIST LPCLSID +lpch lpcmi LPCMINVOKECOMMANDINFO LPCREATESTRUCT @@ -985,6 +995,7 @@ lptpm LPTR LPTSTR lpv +LPrivate LPW lpwcx lpwndpl @@ -1036,6 +1047,7 @@ mdwn Mccs mccs meme +mcp memicmp MENUITEMINFO MENUITEMINFOW @@ -1129,6 +1141,8 @@ muxxc muxxh MVPs mvvm +myorg +myrepo MVVMTK MWBEx MYICON @@ -1278,6 +1292,7 @@ OPENFILENAME openrdp opensource openxmlformats +openurl OPTIMIZEFORINVOKE Optronics ORPHANEDDIALOGTITLE @@ -1359,6 +1374,7 @@ phwnd pici pidl PIDLIST +PII pinfo pinvoke pipename @@ -1409,6 +1425,7 @@ ppv ppwsz prc Prefixer +Premul prependpath prepopulate prevhost @@ -1562,6 +1579,7 @@ riid RKey RNumber rop +rollups ROUNDSMALL ROWSETEXT rpcrt @@ -1707,6 +1725,7 @@ SKIPOWNPROCESS sku SLGP sln +slnf slnx SMALLICON smartphone @@ -1971,7 +1990,7 @@ valuegenerator variantassignment VARTYPE vcamp -VCENTER +vcenter vcgtq VCINSTALLDIR Vcp @@ -2009,7 +2028,7 @@ vorrq VOS vpaddlq vqsubq -VREDRAW +vredraw vreinterpretq VSC VSCBD @@ -2179,6 +2198,7 @@ xstyler XTimer XUP XVIRTUALSCREEN +XXL xxxxxx YAxis ycombinator diff --git a/.github/prompts/create-commit-title.prompt.md b/.github/prompts/create-commit-title.prompt.md index 5d84aef163..abce114f4f 100644 --- a/.github/prompts/create-commit-title.prompt.md +++ b/.github/prompts/create-commit-title.prompt.md @@ -1,6 +1,6 @@ --- mode: 'agent' -model: GPT-5-Codex (Preview) +model: Claude Sonnet 4.5 description: 'Generate an 80-character git commit title for the local diff.' --- diff --git a/.github/prompts/create-pr-summary.prompt.md b/.github/prompts/create-pr-summary.prompt.md index 0c324e7578..de8d360a61 100644 --- a/.github/prompts/create-pr-summary.prompt.md +++ b/.github/prompts/create-pr-summary.prompt.md @@ -1,6 +1,6 @@ --- mode: 'agent' -model: GPT-5-Codex (Preview) +model: Claude Sonnet 4.5 description: 'Generate a PowerToys-ready pull request description from the local diff.' --- diff --git a/.github/prompts/fix-issue.prompt.md b/.github/prompts/fix-issue.prompt.md new file mode 100644 index 0000000000..e8127d0dae --- /dev/null +++ b/.github/prompts/fix-issue.prompt.md @@ -0,0 +1,71 @@ +--- +mode: 'agent' +model: GPT-5-Codex (Preview) +description: " Execute the fix for a GitHub issue using the previously generated implementation plan. Apply code & tests directly in the repo. Output only a PR description (and optional manual steps)." +--- + +# DEPENDENCY +Source review prompt (for generating the implementation plan if missing): +- .github/prompts/review-issue.prompt.md + +Required plan file (single source of truth): +- Generated Files/issueReview/{{issue_number}}/implementation-plan.md + +## Dependency Handling +1) If `implementation-plan.md` exists → proceed. +2) If missing → run the review prompt: + - Invoke: `.github/prompts/review-issue.prompt.md` + - Pass: `issue_number={{issue_number}}` + - Then re-check for `implementation-plan.md`. +3) If still missing → stop and generate: + - `Generated Files/issueFix/{{issue_number}}/manual-steps.md` containing: + “implementation-plan.md not found; please run .github/prompts/review-issue.prompt.md for #{{issue_number}}.” + +# GOAL +For **#{{issue_number}}**: +- Use implementation-plan.md as the single authority. +- Apply code and test changes directly in the repository. +- Produce a PR-ready description. + +# OUTPUT FILES +1) Generated Files/issueFix/{{issue_number}}/pr-description.md +2) Generated Files/issueFix/{{issue_number}}/manual-steps.md # only if human interaction or external setup is required + +# EXECUTION RULES +1) Read implementation-plan.md and execute: + - Layers & Files → edit/create as listed + - Pattern Choices → follow repository conventions + - Fundamentals (perf, security, compatibility, accessibility) + - Logging & Exceptions + - Telemetry (only if explicitly included in the plan) + - Risks & Mitigations + - Tests to Add +2) Locate affected files via `rg` or `git grep`. +3) Add/update tests to enforce the fixed behavior. +4) If any ambiguity exists, add: +// TODO(Human input needed): +5) Verify locally: build & tests run successfully. + +# pr-description.md should include: +- Title: `Fix: (#{{issue_number}})` +- What changed and why the fix works +- Files or modules touched +- Risks & mitigations (implemented) +- Tests added/updated and how to run them +- Telemetry behavior (if applicable) +- Validation / reproduction steps +- `Closes #{{issue_number}}` + +# manual-steps.md (only if needed) +- List required human actions: secrets, config, approvals, missing info, or code comments requiring human decisions. + +# IMPORTANT +- Apply code and tests directly; do not produce patch files. +- Follow implementation-plan.md as the source of truth. +- Insert comments for human review where a decision or input is required. +- Use repository conventions and deterministic, minimal changes. + +# FINALIZE +- Write pr-description.md +- Write manual-steps.md only if needed +- Print concise success message or note items requiring human interaction diff --git a/.github/prompts/review-issue.prompt.md b/.github/prompts/review-issue.prompt.md new file mode 100644 index 0000000000..c5a6a7adf7 --- /dev/null +++ b/.github/prompts/review-issue.prompt.md @@ -0,0 +1,158 @@ +--- +mode: 'agent' +model: Claude Sonnet 4.5 +description: "You are github issue review and planning expertise, Score (0–100) and write one Implementation Plan. Outputs: overview.md, implementation-plan.md." +--- + +# GOAL +For **#{{issue_number}}** produce: +1) `Generated Files/issueReview/{{issue_number}}/overview.md` +2) `Generated Files/issueReview/{{issue_number}}/implementation-plan.md` + +## Inputs + figure out from the prompt on the + +# CONTEXT (brief) +Ground evidence using `gh issue view {{issue_number}} --json number,title,body,author,createdAt,updatedAt,state,labels,milestone,reactions,comments,linkedPullRequests`, and download the image for understand the context of the issue more. +Locate source code in current workspace, but also free feel to use via `rg`/`git grep`. Link related issues/PRs. + +# OVERVIEW.MD +## Summary +Issue, state, milestone, labels. **Signals**: 👍/❤️/👎, comment count, last activity, linked PRs. + +## At-a-Glance Score Table +Present all ratings in a compact table for quick scanning: + +| Dimension | Score | Assessment | Key Drivers | +|-----------|-------|------------|-------------| +| **A) Business Importance** | X/100 | Low/Medium/High | Top 2 factors with scores | +| **B) Community Excitement** | X/100 | Low/Medium/High | Top 2 factors with scores | +| **C) Technical Feasibility** | X/100 | Low/Medium/High | Top 2 factors with scores | +| **D) Requirement Clarity** | X/100 | Low/Medium/High | Top 2 factors with scores | +| **Overall Priority** | X/100 | Low/Medium/High/Critical | Average or weighted summary | +| **Effort Estimate** | X days (T-shirt) | XS/S/M/L/XL/XXL/Epic | Type: bug/feature/chore | +| **Similar Issues Found** | X open, Y closed | — | Quick reference to related work | +| **Potential Assignees** | @username, @username | — | Top contributors to module | + +**Assessment bands**: 0-25 Low, 26-50 Medium, 51-75 High, 76-100 Critical + +## Ratings (0–100) — add evidence & short rationale +### A) Business Importance +- Labels (priority/security/regression): **≤35** +- Milestone/roadmap: **≤25** +- Customer/contract impact: **≤20** +- Unblocks/platform leverage: **≤20** +### B) Community Excitement +- 👍+❤️ normalized: **≤45** +- Comment volume & unique participants: **≤25** +- Recent activity (≤30d): **≤15** +- Duplicates/related issues: **≤15** +### C) Technical Feasibility +- Contained surface/clear seams: **≤30** +- Existing patterns/utilities: **≤25** +- Risk (perf/sec/compat) manageable: **≤25** +- Testability & CI support: **≤20** +### D) Requirement Clarity +- Behavior/repro/constraints: **≤60** +- Non-functionals (perf/sec/i18n/a11y): **≤25** +- Decision owners/acceptance signals: **≤15** + +## Effort +Days + **T-shirt** (XS 0.5–1d, S 1–2, M 2–4, L 4–7, XL 7–14, XXL 14–30, Epic >30). +Type/level: bug/feature/chore/docs/refactor/test-only; severity/value tier. + +## Suggested Actions +Provide actionable recommendations for issue triage and assignment: + +### A) Requirement Clarification (if Clarity score <50) +**When Requirement Clarity (Dimension D) is Medium or Low:** +- Identify specific gaps in issue description: missing repro steps, unclear expected behavior, undefined acceptance criteria, missing non-functional requirements +- Draft 3-5 clarifying questions to post as issue comment +- Suggest additional information needed: screenshots, logs, environment details, OS version, PowerToys version, error messages +- If behavior is ambiguous, propose 2-3 interpretation scenarios and ask reporter to confirm +- Example questions: + - "Can you provide exact steps to reproduce this issue?" + - "What is the expected behavior vs. what you're actually seeing?" + - "Does this happen on Windows 10, 11, or both?" + - "Can you attach a screenshot or screen recording?" + +### B) Correct Label Suggestions +- Analyze issue type, module, and severity to suggest missing or incorrect labels +- Recommend labels from: `Issue-Bug`, `Issue-Feature`, `Issue-Docs`, `Issue-Task`, `Priority-High`, `Priority-Medium`, `Priority-Low`, `Needs-Triage`, `Needs-Author-Feedback`, `Product-`, etc. +- If Requirement Clarity is low (<50), add `Needs-Author-Feedback` label +- If current labels are incorrect or incomplete, provide specific label changes with rationale + +### C) Find Similar Issues & Past Fixes +- Search for similar issues using `gh issue list --search "keywords" --state all --json number,title,state,closedAt` +- Identify patterns: duplicate issues, related bugs, or similar feature requests +- For closed issues, find linked PRs that fixed them: check `linkedPullRequests` in issue data +- Provide 3-5 examples of similar issues with format: `# - (closed by PR #<pr>)` or `(still open)` + +### D) Identify Subject Matter Experts +- Use git blame/log to find who fixed similar issues in the past +- Search for PR authors who touched relevant files: `git log --all --format='%aN' -- <file_paths> | sort | uniq -c | sort -rn | head -5` +- Check issue/PR history for frequent contributors to the affected module +- Suggest 2-3 potential assignees with context: `@<username> - <reason>` (e.g., "fixed similar rendering bug in #12345", "maintains FancyZones module") + +### E) Semantic Search for Related Work +- Use semantic_search tool to find similar issues, code patterns, or past discussions +- Search queries should include: issue keywords, module names, error messages, feature descriptions +- Cross-reference semantic results with GitHub issue search for comprehensive coverage + +**Output format for Suggested Actions section in overview.md:** +```markdown +## Suggested Actions + +### Clarifying Questions (if Clarity <50) +Post these questions as issue comment to gather missing information: +1. <question> +2. <question> +3. <question> + +**Recommended label**: `Needs-Author-Feedback` + +### Label Recommendations +- Add: `<label>` - <reason> +- Remove: `<label>` - <reason> +- Current labels are appropriate ✓ + +### Similar Issues Found +1. #<number> - <title> (<state>, closed by PR #<pr> on <date>) +2. #<number> - <title> (<state>) +... + +### Potential Assignees +- @<username> - <reason> +- @<username> - <reason> + +### Related Code/Discussions +- <semantic search findings> +``` + +# IMPLEMENTATION-PLAN.MD +1) **Problem Framing** — restate problem; current vs expected; scope boundaries. +2) **Layers & Files** — layers (UI/domain/data/infra/build). For each, list **files/dirs to modify** and **new files** (exact paths + why). Prefer repo patterns; cite examples/PRs. +3) **Pattern Choices** — reuse existing; if new, justify trade-offs & transition. +4) **Fundamentals** (brief plan or N/A + reason): +- Performance (hot paths, allocs, caching/streaming) +- Security (validation, authN/Z, secrets, SSRF/XSS/CSRF) +- G11N/L10N (resources, number/date, pluralization) +- Compatibility (public APIs, formats, OS/runtime/toolchain) +- Extensibility (DI seams, options/flags, plugin points) +- Accessibility (roles, labels, focus, keyboard, contrast) +- SOLID & repo conventions (naming, folders, dependency direction) +5) **Logging & Exception Handling** +- Where to log; levels; structured fields; correlation/traces. +- What to catch vs rethrow; retries/backoff; user-visible errors. +- **Privacy**: never log secrets/PII; redaction policy. +6) **Telemetry (optional — business metrics only)** +- Events/metrics (name, when, props); success signal; privacy/sampling; dashboards/alerts. +7) **Risks & Mitigations** — flags/canary/shadow-write/config guards. +8) **Task Breakdown (agent-ready)** — table (leave a blank line before the header so Markdown renders correctly): + +| Task | Intent | Files/Areas | Steps | Tests (brief) | Owner (Agent/Human) | Human interaction needed? (why) | +|---|---|---|---|---|---|---| + +9) **Tests to Add (only)** +- **Unit**: targets, cases (success/edge/error), mocks/fixtures, path, notes. +- **UI** (if applicable): flows, locator strategy, env/data/flags, path, flake mitigation. \ No newline at end of file diff --git a/.github/prompts/review-pr.prompt.md b/.github/prompts/review-pr.prompt.md new file mode 100644 index 0000000000..677970a6ce --- /dev/null +++ b/.github/prompts/review-pr.prompt.md @@ -0,0 +1,199 @@ +--- +mode: 'agent' +model: Claude Sonnet 4.5 +description: "gh-driven PR review; per-step Markdown + machine-readable outputs" +--- + +# PR Review — gh + stepwise + +**Goal**: Given `{{pr_number}}`, run a *one-topic-per-step* review. Write files to `Generated Files/prReview/{{pr_number}}/` (replace `{{pr_number}}` with the integer). Emit machine‑readable blocks for a GitHub MCP to post review comments. + +## PR selection +Resolve the target PR using these fallbacks in order: +1. Parse the invocation text for an explicit identifier (first integer following patterns such as a leading hash and digits or the text `PR:` followed by digits). +2. If no PR is found yet, locate the newest `Generated Files/prReview/_batch/batch-overview-*.md` file (highest timestamp in filename, fallback newest mtime) and take the first entry in its `## PRs` list whose review folder is missing `00-OVERVIEW.md` or contains `__error.flag`. +3. If the batch file has no pending PRs, query assignments with `gh pr list --assignee @me --state open --json number,updatedAt --limit 20` and pick the most recently updated PR that does not already have a completed review folder. +4. If still unknown, run `gh pr view --json number` in the current branch and use that result when it is unambiguous. +5. If every step above fails, prompt the user for a PR number before proceeding. + +## Fetch PR data with `gh` +- `gh pr view {{pr_number}} --json number,baseRefName,headRefName,baseRefOid,headRefOid,changedFiles,files` +- `gh api repos/:owner/:repo/pulls/{{pr_number}}/files?per_page=250` # patches for line mapping + +### Incremental review workflow +1. **Check for existing review**: Read `Generated Files/prReview/{{pr_number}}/00-OVERVIEW.md` +2. **Extract state**: Parse `Last reviewed SHA:` from review metadata section +3. **Detect changes**: Run `Get-PrIncrementalChanges.ps1 -PullRequestNumber {{pr_number}} -LastReviewedCommitSha {{sha}}` +4. **Analyze result**: + - `NeedFullReview: true` → Review all files in the PR + - `NeedFullReview: false` and `IsIncremental: true` → Review only files in `ChangedFiles` array + - `ChangedFiles` is empty → No changes, skip review (update iteration history with "No changes since last review") +5. **Apply smart filtering**: Use the file patterns in smart step filtering table to skip irrelevant steps +6. **Update metadata**: After completing review, save current `headRefOid` as `Last reviewed SHA:` in `00-OVERVIEW.md` + +### Reusable PowerShell scripts +Scripts live in `.github/review-tools/` to avoid repeated manual approvals during PR reviews: + +| Script | Usage | +| --- | --- | +| `.github/review-tools/Get-GitHubRawFile.ps1` | Download a repository file at a given ref, optionally with line numbers. | +| `.github/review-tools/Get-GitHubPrFilePatch.ps1` | Fetch the unified diff for a specific file within a pull request via `gh api`. | +| `.github/review-tools/Get-PrIncrementalChanges.ps1` | Compare last reviewed SHA with current PR head to identify incremental changes. Returns JSON with changed files, new commits, and whether full review is needed. | +| `.github/review-tools/Test-IncrementalReview.ps1` | Test helper to preview incremental review detection for a PR. Use before running full review to see what changed. | + +Always prefer these scripts (or new ones added under `.github/review-tools/`) over raw `gh api` or similar shell commands so the review flow does not trigger interactive approval prompts. + +## Output files +Folder: `Generated Files/prReview/{{pr_number}}/` +Files: `00-OVERVIEW.md`, `01-functionality.md`, `02-compatibility.md`, `03-performance.md`, `04-accessibility.md`, `05-security.md`, `06-localization.md`, `07-globalization.md`, `08-extensibility.md`, `09-solid-design.md`, `10-repo-patterns.md`, `11-docs-automation.md`, `12-code-comments.md`, `13-copilot-guidance.md` *(only if guidance md exists).* +- **Write-after-step rule:** Immediately after completing each TODO step, persist that step's markdown file before proceeding to the next. Generate `00-OVERVIEW.md` only after every step file has been refreshed for the current run. + +## Iteration management +- Determine the current review iteration by reading `00-OVERVIEW.md` (look for `Review iteration:`). If missing, assume iteration `1`. +- Extract the last reviewed SHA from `00-OVERVIEW.md` (look for `Last reviewed SHA:` in the review metadata section). If missing, this is iteration 1. +- **Incremental review detection**: + 1. Call `.github/review-tools/Get-PrIncrementalChanges.ps1 -PullRequestNumber {{pr_number}} -LastReviewedCommitSha {{last_sha}}` to get delta analysis. + 2. Parse the JSON result to determine if incremental review is possible (`IsIncremental: true`, `NeedFullReview: false`). + 3. If force-push detected or first review, proceed with full review of all changed files. + 4. If incremental, review only the files listed in `ChangedFiles` array and apply smart step filtering (see below). +- Increment the iteration for each review run and propagate the new value to all step files and the overview. +- Preserve prior iteration notes by keeping/expanding an `## Iteration history` section in each markdown file, appending the newest summary under `### Iteration <N>`. +- Summaries should capture key deltas since the previous iteration so reruns can pick up context quickly. +- **After review completion**, update `Last reviewed SHA:` in `00-OVERVIEW.md` with the current `headRefOid` and update the timestamp. + +### Smart step filtering (incremental reviews only) +When performing incremental review, skip steps that are irrelevant based on changed file types: + +| File pattern | Required steps | Skippable steps | +| --- | --- | --- | +| `**/*.cs`, `**/*.cpp`, `**/*.h` | Functionality, Compatibility, Performance, Security, SOLID, Repo patterns, Code comments | (depends on files) | +| `**/*.resx`, `**/Resources/*.xaml` | Localization, Globalization | Most others | +| `**/*.md` (docs) | Docs & automation | Most others (unless copilot guidance) | +| `**/*copilot*.md`, `.github/prompts/*.md` | Copilot guidance, Docs & automation | Most others | +| `**/*.csproj`, `**/*.vcxproj`, `**/packages.config` | Compatibility, Security, Repo patterns | Localization, Globalization, Accessibility | +| `**/UI/**`, `**/*View.xaml` | Accessibility, Localization | Performance (unless perf-sensitive controls) | + +**Default**: If uncertain or files span multiple categories, run all applicable steps. When in doubt, be conservative and review more rather than less. + +## TODO steps (one concern each) +1) Functionality +2) Compatibility +3) Performance +4) Accessibility +5) Security +6) Localization +7) Globalization +8) Extensibility +9) SOLID principles +10) Repo patterns +11) Docs & automation coverage for the changes +12) Code comments +13) Copilot guidance (conditional): if changed folders contain `*copilot*.md` or `.github/prompts/*.md`, review diffs **against** that guidance and write `13-copilot-guidance.md` (omit if none). + +## Per-step file template (use verbatim) +```md +# <STEP TITLE> +**PR:** (populate with PR identifier) — Base:<baseRefName> Head:<headRefName> +**Review iteration:** ITERATION + +## Iteration history +- Maintain subsections titled `### Iteration N` in reverse chronological order (append the latest at the top) with 2–4 bullet highlights. + +### Iteration ITERATION +- <Latest key point 1> +- <Latest key point 2> + +## Checks executed +- List the concrete checks for *this step only* (5–10 bullets). + +## Findings +(If none, write **None**. Defaults have one or more blocks:) + +```mcp-review-comment +{"file":"relative/path.ext","start_line":123,"end_line":125,"severity":"high|medium|low|info","tags":["<step-slug>","pr-tag-here"],"related_files":["optional/other/file1"],"body":"Problem → Why it matters → Concrete fix. If spans multiple files, name them here."} +``` +Use the second tag to encode the PR number. + +``` +## Overview file (`00-OVERVIEW.md`) template +```md +# PR Review Overview — (populate with PR identifier) +**Review iteration:** ITERATION +**Changed files:** <n> | **High severity issues:** <count> + +## Review metadata +**Last reviewed SHA:** <headRefOid from gh pr view> +**Last review timestamp:** <ISO8601 timestamp> +**Review mode:** <Full|Incremental (N files changed since iteration X)> +**Base ref:** <baseRefName> +**Head ref:** <headRefName> + +## Step results +Write lines like: `01 Functionality — <OK|Issues|Skipped> (see 01-functionality.md)` … through step 13. +Mark steps as "Skipped" when using incremental review smart filtering. + +## Iteration history +- Maintain subsections titled `### Iteration N` mirroring the per-step convention with concise deltas and cross-links to the relevant step files. +- For incremental reviews, list the specific files that changed and which commits were added. +``` + +## Line numbers & multi‑file issues +- Map head‑side lines from `patch` hunks (`@@ -a,b +c,d @@` → new lines `+c..+c+d-1`). +- For cross‑file issues: set the primary `"file"`, list others in `"related_files"`, and name them in `"body"`. + +## Posting (for MCP) +- Parse all ```mcp-review-comment``` blocks across step files and post as PR review comments. +- If posting isn’t available, still write all files. + +## Constraint +Read/analyze only; don't modify code. Keep comments small, specific, and fix‑oriented. + +**Testing**: Use `.github/review-tools/Test-IncrementalReview.ps1 -PullRequestNumber 42374` to preview incremental detection before running full review. + +## Scratch cache for large PRs + +Create a local scratch workspace to progressively summarize diffs and reload state across runs. + +### Paths +- Root: `Generated Files/prReview/{{pr_number}}/__tmp/` +- Files: + - `index.jsonl` — append-only JSON Lines index of artifacts. + - `todo-queue.json` — pending items (files/chunks/steps). + - `rollup-<step>-v<N>.md` — iterative per-step aggregates. + - `file-<hash>.txt` — optional saved chunk text (when needed). + +### JSON schema (per line in `index.jsonl`) +```json +{"type":"chunk|summary|issue|crosslink", + "path":"relative/file.ext","chunk_id":"f-12","step":"functionality|compatibility|...", + "base_sha":"...", "head_sha":"...", "range":[start,end], "version":1, + "notes":"short text or key:value map", "created_utc":"ISO8601"} +``` + +### Phases (stateful; resume-safe) +0. **Discover** PR + SHAs: `gh pr view <PR> --json baseRefName,headRefName,baseRefOid,headRefOid,files`. +1. **Chunk** each changed file (head): split into ~300–600 LOC or ~4k chars; stable `chunk_id` = hash(path+start). + - Save `chunk` records. Optionally write `file-<hash>.txt` for expensive chunks. +2. **Summarize** per chunk: intent, APIs, risks per TODO step; emit `summary` records (≤600 tokens each). +3. **Issues**: convert findings to machine-readable blocks and emit `issue` records (later rendered to step MD). +4. **Rollups**: build/update `rollup-<step>-v<N>.md` from `summary`+`issue`. Keep prior versions. +5. **Finalize**: write per-step files + `00-OVERVIEW.md` from rollups. Post comments via MCP if available. + +### Re-use & token limits +- Always **reload** `index.jsonl` first; skip chunks with same `head_sha` and `range`. +- **Incremental review optimization**: When `Get-PrIncrementalChanges.ps1` returns a subset of changed files, load only chunks from those files. Reuse existing chunks/summaries for unchanged files. +- Prefer re-summarizing only changed chunks; merge chunk summaries → file summaries → step rollups. +- When context is tight, load only the minimal chunk text (or its saved `file-<hash>.txt`) needed for a comment. + +### Original vs diff +- Fetch base content when needed: prefer `git show <baseRefName>:<path>`; fallback `gh api repos/:owner/:repo/contents/<path>?ref=<base_sha>` (base64). +- Use patch hunks from `gh api .../pulls/<PR>/files` to compute **head** line numbers. + +### Queue-driven loop +- Seed `todo-queue.json` with all changed files. +- Process: chunk → summarize → detect issues → roll up. +- Append to `index.jsonl` after each step; never rewrite previous lines (append-only). + +### Hygiene +- `__tmp/` is implementation detail; do not include in final artifacts. +- It is safe to delete to force a clean pass; the next run rebuilds it. \ No newline at end of file diff --git a/.github/review-tools/Get-GitHubPrFilePatch.ps1 b/.github/review-tools/Get-GitHubPrFilePatch.ps1 new file mode 100644 index 0000000000..1b20ea59f5 --- /dev/null +++ b/.github/review-tools/Get-GitHubPrFilePatch.ps1 @@ -0,0 +1,79 @@ +<# +.SYNOPSIS + Retrieves the unified diff patch for a specific file in a GitHub pull request. + +.DESCRIPTION + This script fetches the patch content (unified diff format) for a specified file + within a pull request. It uses the GitHub CLI (gh) to query the GitHub API and + retrieve file change information. + +.PARAMETER PullRequestNumber + The pull request number to query. + +.PARAMETER FilePath + The relative path to the file in the repository (e.g., "src/modules/main.cpp"). + +.PARAMETER RepositoryOwner + The GitHub repository owner. Defaults to "microsoft". + +.PARAMETER RepositoryName + The GitHub repository name. Defaults to "PowerToys". + +.EXAMPLE + .\Get-GitHubPrFilePatch.ps1 -PullRequestNumber 42374 -FilePath "src/modules/cmdpal/main.cpp" + Retrieves the patch for main.cpp in PR #42374. + +.EXAMPLE + .\Get-GitHubPrFilePatch.ps1 -PullRequestNumber 42374 -FilePath "README.md" -RepositoryOwner "myorg" -RepositoryName "myrepo" + Retrieves the patch from a different repository. + +.NOTES + Requires GitHub CLI (gh) to be installed and authenticated. + Run 'gh auth login' if not already authenticated. + +.LINK + https://cli.github.com/ +#> + +[CmdletBinding()] +param( + [Parameter(Mandatory = $true, HelpMessage = "Pull request number")] + [int]$PullRequestNumber, + + [Parameter(Mandatory = $true, HelpMessage = "Relative path to the file in the repository")] + [string]$FilePath, + + [Parameter(Mandatory = $false, HelpMessage = "Repository owner")] + [string]$RepositoryOwner = "microsoft", + + [Parameter(Mandatory = $false, HelpMessage = "Repository name")] + [string]$RepositoryName = "PowerToys" +) + +# Construct GitHub API path for pull request files +$apiPath = "repos/$RepositoryOwner/$RepositoryName/pulls/$PullRequestNumber/files?per_page=250" + +# Query GitHub API to get all files in the pull request +try { + $pullRequestFiles = gh api $apiPath | ConvertFrom-Json +} catch { + Write-Error "Failed to query GitHub API for PR #$PullRequestNumber. Ensure gh CLI is authenticated. Details: $_" + exit 1 +} + +# Find the matching file in the pull request +$matchedFile = $pullRequestFiles | Where-Object { $_.filename -eq $FilePath } + +if (-not $matchedFile) { + Write-Error "File '$FilePath' not found in PR #$PullRequestNumber." + exit 1 +} + +# Check if patch content exists +if (-not $matchedFile.patch) { + Write-Warning "File '$FilePath' has no patch content (possibly binary or too large)." + return +} + +# Output the patch content +$matchedFile.patch diff --git a/.github/review-tools/Get-GitHubRawFile.ps1 b/.github/review-tools/Get-GitHubRawFile.ps1 new file mode 100644 index 0000000000..d75f519334 --- /dev/null +++ b/.github/review-tools/Get-GitHubRawFile.ps1 @@ -0,0 +1,91 @@ +<# +.SYNOPSIS + Downloads and displays the content of a file from a GitHub repository at a specific git reference. + +.DESCRIPTION + This script fetches the raw content of a file from a GitHub repository using GitHub's raw content API. + It can optionally display line numbers and supports any valid git reference (branch, tag, or commit SHA). + +.PARAMETER FilePath + The relative path to the file in the repository (e.g., "src/modules/main.cpp"). + +.PARAMETER GitReference + The git reference (branch name, tag, or commit SHA) to fetch the file from. Defaults to "main". + +.PARAMETER RepositoryOwner + The GitHub repository owner. Defaults to "microsoft". + +.PARAMETER RepositoryName + The GitHub repository name. Defaults to "PowerToys". + +.PARAMETER ShowLineNumbers + When specified, displays line numbers before each line of content. + +.PARAMETER StartLineNumber + The starting line number to use when ShowLineNumbers is enabled. Defaults to 1. + +.EXAMPLE + .\Get-GitHubRawFile.ps1 -FilePath "README.md" -GitReference "main" + Downloads and displays the README.md file from the main branch. + +.EXAMPLE + .\Get-GitHubRawFile.ps1 -FilePath "src/runner/main.cpp" -GitReference "dev/feature-branch" -ShowLineNumbers + Downloads main.cpp from a feature branch and displays it with line numbers. + +.EXAMPLE + .\Get-GitHubRawFile.ps1 -FilePath "LICENSE" -GitReference "abc123def" -ShowLineNumbers -StartLineNumber 10 + Downloads the LICENSE file from a specific commit and displays it with line numbers starting at 10. + +.NOTES + Requires internet connectivity to access GitHub's raw content API. + Does not require GitHub CLI authentication for public repositories. + +.LINK + https://docs.github.com/en/rest/repos/contents +#> + +[CmdletBinding()] +param( + [Parameter(Mandatory = $true, HelpMessage = "Relative path to the file in the repository")] + [string]$FilePath, + + [Parameter(Mandatory = $false, HelpMessage = "Git reference (branch, tag, or commit SHA)")] + [string]$GitReference = "main", + + [Parameter(Mandatory = $false, HelpMessage = "Repository owner")] + [string]$RepositoryOwner = "microsoft", + + [Parameter(Mandatory = $false, HelpMessage = "Repository name")] + [string]$RepositoryName = "PowerToys", + + [Parameter(Mandatory = $false, HelpMessage = "Display line numbers before each line")] + [switch]$ShowLineNumbers, + + [Parameter(Mandatory = $false, HelpMessage = "Starting line number for display")] + [int]$StartLineNumber = 1 +) + +# Construct the raw content URL +$rawContentUrl = "https://raw.githubusercontent.com/$RepositoryOwner/$RepositoryName/$GitReference/$FilePath" + +# Fetch the file content from GitHub +try { + $response = Invoke-WebRequest -UseBasicParsing -Uri $rawContentUrl +} catch { + Write-Error "Failed to fetch file from $rawContentUrl. Details: $_" + exit 1 +} + +# Split content into individual lines +$contentLines = $response.Content -split "`n" + +# Display the content with or without line numbers +if ($ShowLineNumbers) { + $currentLineNumber = $StartLineNumber + foreach ($line in $contentLines) { + Write-Output ("{0:d4}: {1}" -f $currentLineNumber, $line) + $currentLineNumber++ + } +} else { + $contentLines | ForEach-Object { Write-Output $_ } +} diff --git a/.github/review-tools/Get-PrIncrementalChanges.ps1 b/.github/review-tools/Get-PrIncrementalChanges.ps1 new file mode 100644 index 0000000000..b9bcf8025e --- /dev/null +++ b/.github/review-tools/Get-PrIncrementalChanges.ps1 @@ -0,0 +1,173 @@ +<# +.SYNOPSIS + Detects changes between the last reviewed commit and current head of a pull request. + +.DESCRIPTION + This script compares a previously reviewed commit SHA with the current head of a pull request + to determine what has changed. It helps enable incremental reviews by identifying new commits + and modified files since the last review iteration. + + The script handles several scenarios: + - First review (no previous SHA provided) + - No changes (current SHA matches last reviewed SHA) + - Force-push detected (last reviewed SHA no longer in history) + - Incremental changes (new commits added since last review) + +.PARAMETER PullRequestNumber + The pull request number to analyze. + +.PARAMETER LastReviewedCommitSha + The commit SHA that was last reviewed. If omitted, this is treated as a first review. + +.PARAMETER RepositoryOwner + The GitHub repository owner. Defaults to "microsoft". + +.PARAMETER RepositoryName + The GitHub repository name. Defaults to "PowerToys". + +.OUTPUTS + JSON object containing: + - PullRequestNumber: The PR number being analyzed + - CurrentHeadSha: The current head commit SHA + - LastReviewedSha: The last reviewed commit SHA (if provided) + - BaseRefName: Base branch name + - HeadRefName: Head branch name + - IsIncremental: Boolean indicating if incremental review is possible + - NeedFullReview: Boolean indicating if a full review is required + - ChangedFiles: Array of files that changed (filename, status, additions, deletions) + - NewCommits: Array of commits added since last review (sha, message, author, date) + - Summary: Human-readable description of changes + +.EXAMPLE + .\Get-PrIncrementalChanges.ps1 -PullRequestNumber 42374 + Analyzes PR #42374 with no previous review (first review scenario). + +.EXAMPLE + .\Get-PrIncrementalChanges.ps1 -PullRequestNumber 42374 -LastReviewedCommitSha "abc123def456" + Compares current PR state against the last reviewed commit to identify incremental changes. + +.EXAMPLE + $changes = .\Get-PrIncrementalChanges.ps1 -PullRequestNumber 42374 -LastReviewedCommitSha "abc123" | ConvertFrom-Json + if ($changes.IsIncremental) { Write-Host "Can perform incremental review" } + Captures the output as a PowerShell object for further processing. + +.NOTES + Requires GitHub CLI (gh) to be installed and authenticated. + Run 'gh auth login' if not already authenticated. + +.LINK + https://cli.github.com/ +#> + +[CmdletBinding()] +param( + [Parameter(Mandatory = $true, HelpMessage = "Pull request number")] + [int]$PullRequestNumber, + + [Parameter(Mandatory = $false, HelpMessage = "Commit SHA that was last reviewed")] + [string]$LastReviewedCommitSha, + + [Parameter(Mandatory = $false, HelpMessage = "Repository owner")] + [string]$RepositoryOwner = "microsoft", + + [Parameter(Mandatory = $false, HelpMessage = "Repository name")] + [string]$RepositoryName = "PowerToys" +) + +# Fetch current pull request state from GitHub +try { + $pullRequestData = gh pr view $PullRequestNumber --json headRefOid,headRefName,baseRefName,baseRefOid | ConvertFrom-Json +} catch { + Write-Error "Failed to fetch PR #$PullRequestNumber details. Details: $_" + exit 1 +} + +$currentHeadSha = $pullRequestData.headRefOid +$baseRefName = $pullRequestData.baseRefName +$headRefName = $pullRequestData.headRefName + +# Initialize result object +$analysisResult = @{ + PullRequestNumber = $PullRequestNumber + CurrentHeadSha = $currentHeadSha + BaseRefName = $baseRefName + HeadRefName = $headRefName + LastReviewedSha = $LastReviewedCommitSha + IsIncremental = $false + NeedFullReview = $true + ChangedFiles = @() + NewCommits = @() + Summary = "" +} + +# Scenario 1: First review (no previous SHA provided) +if ([string]::IsNullOrWhiteSpace($LastReviewedCommitSha)) { + $analysisResult.Summary = "Initial review - no previous iteration found" + $analysisResult.NeedFullReview = $true + return $analysisResult | ConvertTo-Json -Depth 10 +} + +# Scenario 2: No changes since last review +if ($currentHeadSha -eq $LastReviewedCommitSha) { + $analysisResult.Summary = "No changes since last review (SHA: $currentHeadSha)" + $analysisResult.NeedFullReview = $false + $analysisResult.IsIncremental = $true + return $analysisResult | ConvertTo-Json -Depth 10 +} + +# Scenario 3: Check for force-push (last reviewed SHA no longer exists in history) +try { + $null = gh api "repos/$RepositoryOwner/$RepositoryName/commits/$LastReviewedCommitSha" 2>&1 + if ($LASTEXITCODE -ne 0) { + # SHA not found - likely force-push or branch rewrite + $analysisResult.Summary = "Force-push detected - last reviewed SHA $LastReviewedCommitSha no longer exists. Full review required." + $analysisResult.NeedFullReview = $true + return $analysisResult | ConvertTo-Json -Depth 10 + } +} catch { + $analysisResult.Summary = "Cannot verify last reviewed SHA $LastReviewedCommitSha - assuming force-push. Full review required." + $analysisResult.NeedFullReview = $true + return $analysisResult | ConvertTo-Json -Depth 10 +} + +# Scenario 4: Get incremental changes between last reviewed SHA and current head +try { + $compareApiPath = "repos/$RepositoryOwner/$RepositoryName/compare/$LastReviewedCommitSha...$currentHeadSha" + $comparisonData = gh api $compareApiPath | ConvertFrom-Json + + # Extract new commits information + $analysisResult.NewCommits = $comparisonData.commits | ForEach-Object { + @{ + Sha = $_.sha.Substring(0, 7) + Message = $_.commit.message.Split("`n")[0] # First line only + Author = $_.commit.author.name + Date = $_.commit.author.date + } + } + + # Extract changed files information + $analysisResult.ChangedFiles = $comparisonData.files | ForEach-Object { + @{ + Filename = $_.filename + Status = $_.status # added, modified, removed, renamed + Additions = $_.additions + Deletions = $_.deletions + Changes = $_.changes + } + } + + $fileCount = $analysisResult.ChangedFiles.Count + $commitCount = $analysisResult.NewCommits.Count + + $analysisResult.IsIncremental = $true + $analysisResult.NeedFullReview = $false + $analysisResult.Summary = "Incremental review: $commitCount new commit(s), $fileCount file(s) changed since SHA $($LastReviewedCommitSha.Substring(0, 7))" + +} catch { + Write-Error "Failed to compare commits. Details: $_" + $analysisResult.Summary = "Error comparing commits - defaulting to full review" + $analysisResult.NeedFullReview = $true +} + +# Return the analysis result as JSON +return $analysisResult | ConvertTo-Json -Depth 10 diff --git a/.github/review-tools/Test-IncrementalReview.ps1 b/.github/review-tools/Test-IncrementalReview.ps1 new file mode 100644 index 0000000000..b03bbe7cd5 --- /dev/null +++ b/.github/review-tools/Test-IncrementalReview.ps1 @@ -0,0 +1,170 @@ +<# +.SYNOPSIS + Tests and previews incremental review detection for a pull request. + +.DESCRIPTION + This helper script validates the incremental review detection logic by analyzing an existing + PR review folder. It reads the last reviewed SHA from the overview file, compares it with + the current PR state, and displays detailed information about what has changed. + + This is useful for: + - Testing the incremental review system before running a full review + - Understanding what changed since the last review iteration + - Verifying that review metadata was properly recorded + +.PARAMETER PullRequestNumber + The pull request number to test incremental review detection for. + +.PARAMETER RepositoryOwner + The GitHub repository owner. Defaults to "microsoft". + +.PARAMETER RepositoryName + The GitHub repository name. Defaults to "PowerToys". + +.OUTPUTS + Colored console output displaying: + - Current and last reviewed commit SHAs + - Whether incremental review is possible + - List of new commits since last review + - List of changed files with status indicators + - Recommended review strategy + +.EXAMPLE + .\Test-IncrementalReview.ps1 -PullRequestNumber 42374 + Tests incremental review detection for PR #42374. + +.EXAMPLE + .\Test-IncrementalReview.ps1 -PullRequestNumber 42374 -RepositoryOwner "myorg" -RepositoryName "myrepo" + Tests incremental review for a PR in a different repository. + +.NOTES + Requires GitHub CLI (gh) to be installed and authenticated. + Run 'gh auth login' if not already authenticated. + + Prerequisites: + - PR review folder must exist at "Generated Files\prReview\{PRNumber}" + - 00-OVERVIEW.md must exist in the review folder + - For incremental detection, overview must contain "Last reviewed SHA" metadata + +.LINK + https://cli.github.com/ +#> + +[CmdletBinding()] +param( + [Parameter(Mandatory = $true, HelpMessage = "Pull request number to test")] + [int]$PullRequestNumber, + + [Parameter(Mandatory = $false, HelpMessage = "Repository owner")] + [string]$RepositoryOwner = "microsoft", + + [Parameter(Mandatory = $false, HelpMessage = "Repository name")] + [string]$RepositoryName = "PowerToys" +) + +# Resolve paths to review folder and overview file +$repositoryRoot = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent +$reviewFolderPath = Join-Path $repositoryRoot "Generated Files\prReview\$PullRequestNumber" +$overviewFilePath = Join-Path $reviewFolderPath "00-OVERVIEW.md" + +Write-Host "=== Testing Incremental Review for PR #$PullRequestNumber ===" -ForegroundColor Cyan +Write-Host "" + +# Check if review folder exists +if (-not (Test-Path $reviewFolderPath)) { + Write-Host "❌ Review folder not found: $reviewFolderPath" -ForegroundColor Red + Write-Host "This appears to be a new review (iteration 1)" -ForegroundColor Yellow + exit 0 +} + +# Check if overview file exists +if (-not (Test-Path $overviewFilePath)) { + Write-Host "❌ Overview file not found: $overviewFilePath" -ForegroundColor Red + Write-Host "This appears to be an incomplete review" -ForegroundColor Yellow + exit 0 +} + +# Read overview file and extract last reviewed SHA +Write-Host "📄 Reading overview file..." -ForegroundColor Green +$overviewFileContent = Get-Content $overviewFilePath -Raw + +if ($overviewFileContent -match '\*\*Last reviewed SHA:\*\*\s+(\w+)') { + $lastReviewedSha = $Matches[1] + Write-Host "✅ Found last reviewed SHA: $lastReviewedSha" -ForegroundColor Green +} else { + Write-Host "⚠️ No 'Last reviewed SHA' found in overview - this may be an old format" -ForegroundColor Yellow + Write-Host "Proceeding without incremental detection (full review will be needed)" -ForegroundColor Yellow + exit 0 +} + +Write-Host "" +Write-Host "🔍 Running incremental change detection..." -ForegroundColor Cyan + +# Call the incremental changes detection script +$incrementalChangesScriptPath = Join-Path $PSScriptRoot "Get-PrIncrementalChanges.ps1" +if (-not (Test-Path $incrementalChangesScriptPath)) { + Write-Host "❌ Script not found: $incrementalChangesScriptPath" -ForegroundColor Red + exit 1 +} + +try { + $analysisResult = & $incrementalChangesScriptPath ` + -PullRequestNumber $PullRequestNumber ` + -LastReviewedCommitSha $lastReviewedSha ` + -RepositoryOwner $RepositoryOwner ` + -RepositoryName $RepositoryName | ConvertFrom-Json + + # Display analysis results + Write-Host "" + Write-Host "=== Incremental Review Analysis ===" -ForegroundColor Cyan + Write-Host "Current HEAD SHA: $($analysisResult.CurrentHeadSha)" -ForegroundColor White + Write-Host "Last reviewed SHA: $($analysisResult.LastReviewedSha)" -ForegroundColor White + Write-Host "Base branch: $($analysisResult.BaseRefName)" -ForegroundColor White + Write-Host "Head branch: $($analysisResult.HeadRefName)" -ForegroundColor White + Write-Host "" + Write-Host "Is incremental? $($analysisResult.IsIncremental)" -ForegroundColor $(if ($analysisResult.IsIncremental) { "Green" } else { "Yellow" }) + Write-Host "Need full review? $($analysisResult.NeedFullReview)" -ForegroundColor $(if ($analysisResult.NeedFullReview) { "Yellow" } else { "Green" }) + Write-Host "" + Write-Host "Summary: $($analysisResult.Summary)" -ForegroundColor Cyan + Write-Host "" + + # Display new commits if any + if ($analysisResult.NewCommits -and $analysisResult.NewCommits.Count -gt 0) { + Write-Host "📝 New commits ($($analysisResult.NewCommits.Count)):" -ForegroundColor Green + foreach ($commit in $analysisResult.NewCommits) { + Write-Host " - $($commit.Sha): $($commit.Message)" -ForegroundColor Gray + } + Write-Host "" + } + + # Display changed files if any + if ($analysisResult.ChangedFiles -and $analysisResult.ChangedFiles.Count -gt 0) { + Write-Host "📁 Changed files ($($analysisResult.ChangedFiles.Count)):" -ForegroundColor Green + foreach ($file in $analysisResult.ChangedFiles) { + $statusDisplayColor = switch ($file.Status) { + "added" { "Green" } + "removed" { "Red" } + "modified" { "Yellow" } + "renamed" { "Cyan" } + default { "White" } + } + Write-Host " - [$($file.Status)] $($file.Filename) (+$($file.Additions)/-$($file.Deletions))" -ForegroundColor $statusDisplayColor + } + Write-Host "" + } + + # Suggest review strategy based on analysis + Write-Host "=== Recommended Review Strategy ===" -ForegroundColor Cyan + if ($analysisResult.NeedFullReview) { + Write-Host "🔄 Full review recommended" -ForegroundColor Yellow + } elseif ($analysisResult.IsIncremental -and ($analysisResult.ChangedFiles.Count -eq 0)) { + Write-Host "✅ No changes detected - no review needed" -ForegroundColor Green + } elseif ($analysisResult.IsIncremental) { + Write-Host "⚡ Incremental review possible - review only changed files" -ForegroundColor Green + Write-Host "💡 Consider applying smart step filtering based on file types" -ForegroundColor Cyan + } + +} catch { + Write-Host "❌ Error running incremental change detection: $_" -ForegroundColor Red + exit 1 +} diff --git a/.github/review-tools/review-tools.instructions.md b/.github/review-tools/review-tools.instructions.md new file mode 100644 index 0000000000..e70e0c02d6 --- /dev/null +++ b/.github/review-tools/review-tools.instructions.md @@ -0,0 +1,313 @@ +--- +description: PowerShell scripts for efficient PR reviews in PowerToys repository +applyTo: '**' +--- + +# PR Review Tools - Reference Guide + +PowerShell scripts to support efficient and incremental pull request reviews in the PowerToys repository. + +## Quick Start + +### Prerequisites +- PowerShell 7+ (or Windows PowerShell 5.1+) +- GitHub CLI (`gh`) installed and authenticated (`gh auth login`) +- Access to the PowerToys repository + +### Testing Your Setup + +Run the full test suite (recommended): +```powershell +cd "d:\PowerToys-00c1\.github\review-tools" +.\Run-ReviewToolsTests.ps1 +``` + +Expected: 9-10 tests passing + +### Individual Script Tests + +**Test incremental change detection:** +```powershell +.\Get-PrIncrementalChanges.ps1 -PullRequestNumber 42374 +``` +Expected: JSON output showing review analysis + +**Preview incremental review:** +```powershell +.\Test-IncrementalReview.ps1 -PullRequestNumber 42374 +``` +Expected: Analysis showing current vs last reviewed SHA + +**Fetch file content:** +```powershell +.\Get-GitHubRawFile.ps1 -FilePath "README.md" -GitReference "main" +``` +Expected: README content displayed + +**Get PR file patch:** +```powershell +.\Get-GitHubPrFilePatch.ps1 -PullRequestNumber 42374 -FilePath ".github/actions/spell-check/expect.txt" +``` +Expected: Unified diff output + +## Available Scripts + +### Get-GitHubRawFile.ps1 + +Downloads and displays file content from a GitHub repository at a specific git reference. + +**Purpose:** Retrieve baseline file content for comparison during PR reviews. + +**Parameters:** +- `FilePath` (required): Relative path to file in repository +- `GitReference` (optional): Git ref (branch, tag, SHA). Default: "main" +- `RepositoryOwner` (optional): Repository owner. Default: "microsoft" +- `RepositoryName` (optional): Repository name. Default: "PowerToys" +- `ShowLineNumbers` (switch): Prefix each line with line number +- `StartLineNumber` (optional): Starting line number when using `-ShowLineNumbers`. Default: 1 + +**Usage:** +```powershell +.\Get-GitHubRawFile.ps1 -FilePath "src/runner/main.cpp" -GitReference "main" -ShowLineNumbers +``` + +### Get-GitHubPrFilePatch.ps1 + +Fetches the unified diff (patch) for a specific file in a pull request. + +**Purpose:** Get the exact changes made to a file in a PR for detailed review. + +**Parameters:** +- `PullRequestNumber` (required): Pull request number +- `FilePath` (required): Relative path to file in the PR +- `RepositoryOwner` (optional): Repository owner. Default: "microsoft" +- `RepositoryName` (optional): Repository name. Default: "PowerToys" + +**Usage:** +```powershell +.\Get-GitHubPrFilePatch.ps1 -PullRequestNumber 42374 -FilePath "src/modules/cmdpal/main.cpp" +``` + +**Output:** Unified diff showing changes made to the file. + +### Get-PrIncrementalChanges.ps1 + +Compares the last reviewed commit with the current PR head to identify incremental changes. + +**Purpose:** Enable efficient incremental reviews by detecting what changed since the last review iteration. + +**Parameters:** +- `PullRequestNumber` (required): Pull request number +- `LastReviewedCommitSha` (optional): SHA of the commit that was last reviewed. If omitted, assumes first review. +- `RepositoryOwner` (optional): Repository owner. Default: "microsoft" +- `RepositoryName` (optional): Repository name. Default: "PowerToys" + +**Usage:** +```powershell +.\Get-PrIncrementalChanges.ps1 -PullRequestNumber 42374 -LastReviewedCommitSha "abc123def456" +``` + +**Output:** JSON object with detailed change analysis: +```json +{ + "PullRequestNumber": 42374, + "CurrentHeadSha": "xyz789abc123", + "LastReviewedSha": "abc123def456", + "IsIncremental": true, + "NeedFullReview": false, + "ChangedFiles": [ + { + "Filename": "src/modules/cmdpal/main.cpp", + "Status": "modified", + "Additions": 15, + "Deletions": 8, + "Changes": 23 + } + ], + "NewCommits": [ + { + "Sha": "def456", + "Message": "Fix memory leak", + "Author": "John Doe", + "Date": "2025-11-07T10:30:00Z" + } + ], + "Summary": "Incremental review: 1 new commit(s), 1 file(s) changed since SHA abc123d" +} +``` + +**Scenarios Handled:** +- **No LastReviewedCommitSha**: Returns `NeedFullReview: true` (first review) +- **SHA matches current HEAD**: Returns empty `ChangedFiles` (no changes) +- **Force-push detected**: Returns `NeedFullReview: true` (SHA not in history) +- **Incremental changes**: Returns list of changed files and new commits + +### Test-IncrementalReview.ps1 + +Helper script to test and preview incremental review detection before running the full review. + +**Purpose:** Validate incremental review functionality and preview what changed. + +**Parameters:** +- `PullRequestNumber` (required): Pull request number +- `RepositoryOwner` (optional): Repository owner. Default: "microsoft" +- `RepositoryName` (optional): Repository name. Default: "PowerToys" + +**Usage:** +```powershell +.\Test-IncrementalReview.ps1 -PullRequestNumber 42374 +``` + +**Output:** Colored console output showing: +- Current and last reviewed SHAs +- Whether incremental review is possible +- List of new commits and changed files +- Recommended review strategy + +## Workflow Integration + +These scripts integrate with the PR review prompt (`.github/prompts/review-pr.prompt.md`). + +### Typical Review Flow + +1. **Initial Review (Iteration 1)** + - Review prompt processes the PR + - Creates `Generated Files/prReview/{PR}/00-OVERVIEW.md` + - Includes review metadata section with current HEAD SHA + +2. **Subsequent Reviews (Iteration 2+)** + - Review prompt reads `00-OVERVIEW.md` to get last reviewed SHA + - Calls `Get-PrIncrementalChanges.ps1` to detect what changed + - If incremental: + - Reviews only changed files + - Skips irrelevant review steps (e.g., skip Localization if no `.resx` files changed) + - Uses `Get-GitHubPrFilePatch.ps1` to get patches for changed files + - Updates `00-OVERVIEW.md` with new SHA and iteration number + +### Manual Testing Workflow + +Preview changes before review: +```powershell +# Check what changed in PR #42374 since last review +.\Test-IncrementalReview.ps1 -PullRequestNumber 42374 + +# Get incremental changes programmatically +$changes = .\Get-PrIncrementalChanges.ps1 -PullRequestNumber 42374 -LastReviewedCommitSha "abc123" | ConvertFrom-Json + +if (-not $changes.NeedFullReview) { + Write-Host "Only need to review $($changes.ChangedFiles.Count) files" + + # Review each changed file + foreach ($file in $changes.ChangedFiles) { + Write-Host "Reviewing $($file.Filename)..." + .\Get-GitHubPrFilePatch.ps1 -PullRequestNumber 42374 -FilePath $file.Filename + } +} +``` + +## Error Handling and Troubleshooting + +### Common Requirements + +All scripts: +- Exit with code 1 on error +- Write detailed error messages to stderr +- Require `gh` CLI to be installed and authenticated + +### Common Issues + +**Error: "gh not found"** +- **Solution**: Install GitHub CLI from https://cli.github.com/ and run `gh auth login` + +**Error: "Failed to query GitHub API"** +- **Solution**: Verify `gh` authentication with `gh auth status` +- **Solution**: Check PR number exists and you have repository access + +**Error: "PR not found"** +- **Solution**: Verify the PR number is correct and still exists +- **Solution**: Ensure repository owner and name are correct + +**Error: "SHA not found" or "Force-push detected"** +- **Explanation**: Last reviewed SHA no longer exists in branch history (force-push occurred) +- **Solution**: A full review is required; incremental review not possible + +**Tests show "FAIL" but functionality works** +- **Explanation**: Some tests may show exit code failures even when logic is correct +- **Solution**: Check test output message - if it says "Correctly detected", functionality is working + +**Error: "Could not find insertion point"** +- **Explanation**: Overview file doesn't have expected "**Changed files:**" line +- **Solution**: Verify overview file format is correct or regenerate it + +### Verification Checklist + +After setup, verify: +- [ ] `Run-ReviewToolsTests.ps1` shows 9+ tests passing +- [ ] `Get-PrIncrementalChanges.ps1` returns valid JSON +- [ ] `Test-IncrementalReview.ps1` analyzes a PR without errors +- [ ] `Get-GitHubRawFile.ps1` downloads files correctly +- [ ] `Get-GitHubPrFilePatch.ps1` retrieves patches correctly + +## Best Practices + +### For Review Authors + +1. **Test before full review**: Use `Test-IncrementalReview.ps1` to preview changes +2. **Check for force-push**: Review the analysis output - force-pushes require full reviews +3. **Smart step filtering**: Skip review steps for file types that didn't change + +### For Script Users + +1. **Use absolute paths**: When specifying folders, use absolute paths to avoid ambiguity +2. **Check exit codes**: Scripts exit with code 1 on error - check `$LASTEXITCODE` in automation +3. **Parse JSON output**: Use `ConvertFrom-Json` to work with structured output from `Get-PrIncrementalChanges.ps1` +4. **Handle empty results**: Check `ChangedFiles.Count` before iterating + +### Performance Tips + +1. **Batch operations**: When reviewing multiple PRs, collect all PR numbers and process in batch +2. **Cache raw files**: Download baseline files once and reuse for multiple comparisons +3. **Filter early**: Use incremental detection to skip unnecessary file reviews +4. **Parallel processing**: Consider processing independent PRs in parallel + +## Integration with AI Review Systems + +These tools are designed to work with AI-powered review systems: + +1. **Copilot Instructions**: This file serves as reference documentation for GitHub Copilot +2. **Structured Output**: JSON output from scripts is easily parsed by AI systems +3. **Incremental Intelligence**: AI can focus on changed files for more efficient reviews +4. **Metadata Tracking**: Review iterations are tracked for context-aware suggestions + +### Example AI Integration + +```powershell +# Get incremental changes +$analysis = .\Get-PrIncrementalChanges.ps1 -PullRequestNumber $PR | ConvertFrom-Json + +# Feed to AI review system +$reviewPrompt = @" +Review the following changed files in PR #$PR: +$($analysis.ChangedFiles | ForEach-Object { "- $($_.Filename) ($($_.Status))" } | Out-String) + +Focus on incremental changes only. Previous review was at SHA $($analysis.LastReviewedSha). +"@ + +# Execute AI review with context +Invoke-AIReview -Prompt $reviewPrompt -Files $analysis.ChangedFiles +``` + +## Support and Further Information + +For detailed script documentation, use PowerShell's help system: +```powershell +Get-Help .\Get-PrIncrementalChanges.ps1 -Full +Get-Help .\Test-IncrementalReview.ps1 -Detailed +``` + +Related documentation: +- `.github/prompts/review-pr.prompt.md` - Complete review workflow guide +- `doc/devdocs/` - PowerToys development documentation +- GitHub CLI documentation: https://cli.github.com/manual/ + +For issues or questions, refer to the PowerToys contribution guidelines. diff --git a/.gitignore b/.gitignore index 1318abc22c..1ed1bbcbc1 100644 --- a/.gitignore +++ b/.gitignore @@ -358,4 +358,4 @@ src/common/Telemetry/*.etl /src/settings-ui/Settings.UI/Assets/Settings/search.index.json # PowerToysInstaller Build Temp Files -installer/*/*.wxs.bk \ No newline at end of file +installer/*/*.wxs.bk diff --git a/.pipelines/ESRPSigning_core.json b/.pipelines/ESRPSigning_core.json index 2d59f68985..4504c797cf 100644 --- a/.pipelines/ESRPSigning_core.json +++ b/.pipelines/ESRPSigning_core.json @@ -240,6 +240,14 @@ "PowerToys.CmdPalModuleInterface.dll", "CmdPalKeyboardService.dll", + "PowerToys.ModuleContracts.dll", + "Awake.ModuleServices.dll", + "ColorPicker.ModuleServices.dll", + "Workspaces.ModuleServices.dll", + "Microsoft.CommandPalette.Extensions.dll", + "Microsoft.CommandPalette.Extensions.Toolkit.dll", + "Microsoft.CmdPal.Ext.PowerToys.dll", + "Microsoft.CmdPal.Ext.PowerToys.exe", "*Microsoft.CmdPal.UI_*.msix", "PowerToys.DSC.dll", @@ -363,11 +371,15 @@ "boost_regex-vc143-mt-x32-1_87.dll", "boost_regex-vc143-mt-x64-1_87.dll", + "Microsoft.ML.OnnxRuntime.dll", + "UnitsNet.dll", "UtfUnknown.dll", "Wpf.Ui.dll", "WmiLight.dll", - "WmiLight.Native.dll" + "WmiLight.Native.dll", + "Shmuelie.WinRTServer.dll", + "ToolGood.Words.Pinyin.dll" ], "SigningInfo": { "Operations": [ diff --git a/.pipelines/UpdateVersions.ps1 b/.pipelines/UpdateVersions.ps1 index 0be3e3d30b..4e68663236 100644 --- a/.pipelines/UpdateVersions.ps1 +++ b/.pipelines/UpdateVersions.ps1 @@ -1,7 +1,7 @@ Param( # Using the default value of 1.7 for winAppSdkVersionNumber and useExperimentalVersion as false [Parameter(Mandatory=$False,Position=1)] - [string]$winAppSdkVersionNumber = "1.7", + [string]$winAppSdkVersionNumber = "1.8", # When the pipeline calls the PS1 file, the passed parameters are converted to string type [Parameter(Mandatory=$False,Position=2)] @@ -16,32 +16,7 @@ Param( [string]$sourceLink = "https://microsoft.pkgs.visualstudio.com/ProjectReunion/_packaging/Project.Reunion.nuget.internal/nuget/v3/index.json" ) -function Update-NugetConfig { - param ( - [string]$filePath = [System.IO.Path]::Combine($rootPath, "nuget.config") - ) - Write-Host "Updating nuget.config file" - [xml]$xml = Get-Content -Path $filePath - - # Add localpackages source into nuget.config - $packageSourcesNode = $xml.configuration.packageSources - $addNode = $xml.CreateElement("add") - $addNode.SetAttribute("key", "localpackages") - $addNode.SetAttribute("value", "localpackages") - $packageSourcesNode.AppendChild($addNode) | Out-Null - - # Remove <packageSourceMapping> tag and its content - $packageSourceMappingNode = $xml.configuration.packageSourceMapping - if ($packageSourceMappingNode) { - $xml.configuration.RemoveChild($packageSourceMappingNode) | Out-Null - } - - # print nuget.config after modification - $xml.OuterXml - # Save the modified nuget.config file - $xml.Save($filePath) -} function Read-FileWithEncoding { param ( @@ -71,6 +46,132 @@ function Write-FileWithEncoding { $writer.Close() } + +function Add-NuGetSourceAndMapping { + param ( + [xml]$Xml, + [string]$Key, + [string]$Value, + [string[]]$Patterns + ) + + # Ensure packageSources exists + if (-not $Xml.configuration.packageSources) { + $Xml.configuration.AppendChild($Xml.CreateElement("packageSources")) | Out-Null + } + $sources = $Xml.configuration.packageSources + + # Add/Update Source + $sourceNode = $sources.SelectSingleNode("add[@key='$Key']") + if (-not $sourceNode) { + $sourceNode = $Xml.CreateElement("add") + $sourceNode.SetAttribute("key", $Key) + $sources.AppendChild($sourceNode) | Out-Null + } + $sourceNode.SetAttribute("value", $Value) + + # Ensure packageSourceMapping exists + if (-not $Xml.configuration.packageSourceMapping) { + $Xml.configuration.AppendChild($Xml.CreateElement("packageSourceMapping")) | Out-Null + } + $mapping = $Xml.configuration.packageSourceMapping + + # Remove invalid packageSource nodes (missing key or empty key) + $invalidNodes = $mapping.SelectNodes("packageSource[not(@key) or @key='']") + if ($invalidNodes) { + foreach ($node in $invalidNodes) { + $mapping.RemoveChild($node) | Out-Null + } + } + + # Add/Update Mapping Source + $mappingSource = $mapping.SelectSingleNode("packageSource[@key='$Key']") + if (-not $mappingSource) { + $mappingSource = $Xml.CreateElement("packageSource") + $mappingSource.SetAttribute("key", $Key) + # Insert at top for priority + if ($mapping.HasChildNodes) { + $mapping.InsertBefore($mappingSource, $mapping.FirstChild) | Out-Null + } else { + $mapping.AppendChild($mappingSource) | Out-Null + } + } + + # Double check and force attribute + if (-not $mappingSource.HasAttribute("key")) { + $mappingSource.SetAttribute("key", $Key) + } + + # Update Patterns + # RemoveAll() removes all child nodes AND attributes, so we must re-set the key afterwards + $mappingSource.RemoveAll() + $mappingSource.SetAttribute("key", $Key) + + foreach ($pattern in $Patterns) { + $pkg = $Xml.CreateElement("package") + $pkg.SetAttribute("pattern", $pattern) + $mappingSource.AppendChild($pkg) | Out-Null + } +} + +function Resolve-WinAppSdkSplitDependencies { + Write-Host "Version $WinAppSDKVersion detected. Resolving split dependencies..." + $installDir = Join-Path $rootPath "localpackages\output" + New-Item -ItemType Directory -Path $installDir -Force | Out-Null + + # Create a temporary nuget.config to avoid interference from the repo's config + $tempConfig = Join-Path $env:TEMP "nuget_$(Get-Random).config" + Set-Content -Path $tempConfig -Value "<?xml version='1.0' encoding='utf-8'?><configuration><packageSources><clear /><add key='TempSource' value='$sourceLink' /></packageSources></configuration>" + + try { + # Extract BuildTools version from Directory.Packages.props to ensure we have the required version + $dirPackagesProps = Join-Path $rootPath "Directory.Packages.props" + if (Test-Path $dirPackagesProps) { + $propsContent = Get-Content $dirPackagesProps -Raw + if ($propsContent -match '<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="([^"]+)"') { + $buildToolsVersion = $Matches[1] + Write-Host "Downloading Microsoft.Windows.SDK.BuildTools version $buildToolsVersion..." + $nugetArgsBuildTools = "install Microsoft.Windows.SDK.BuildTools -Version $buildToolsVersion -ConfigFile $tempConfig -OutputDirectory $installDir -NonInteractive -NoCache" + Invoke-Expression "nuget $nugetArgsBuildTools" | Out-Null + } + } + + # Download package to inspect nuspec and keep it for the build + $nugetArgs = "install Microsoft.WindowsAppSDK -Version $WinAppSDKVersion -ConfigFile $tempConfig -OutputDirectory $installDir -NonInteractive -NoCache" + Invoke-Expression "nuget $nugetArgs" | Out-Null + + # Parse dependencies from the installed folders + # Folder structure is typically {PackageId}.{Version} + $directories = Get-ChildItem -Path $installDir -Directory + $allLocalPackages = @() + foreach ($dir in $directories) { + # Match any package pattern: PackageId.Version + if ($dir.Name -match "^(.+?)\.(\d+\..*)$") { + $pkgId = $Matches[1] + $pkgVer = $Matches[2] + $allLocalPackages += $pkgId + + $packageVersions[$pkgId] = $pkgVer + Write-Host "Found dependency: $pkgId = $pkgVer" + } + } + + # Update repo's nuget.config to use localpackages + $nugetConfig = Join-Path $rootPath "nuget.config" + $configData = Read-FileWithEncoding -Path $nugetConfig + [xml]$xml = $configData.Content + + Add-NuGetSourceAndMapping -Xml $xml -Key "localpackages" -Value $installDir -Patterns $allLocalPackages + + $xml.Save($nugetConfig) + Write-Host "Updated nuget.config with localpackages mapping." + } catch { + Write-Warning "Failed to resolve dependencies: $_" + } finally { + Remove-Item $tempConfig -Force -ErrorAction SilentlyContinue + } +} + # Execute nuget list and capture the output if ($useExperimentalVersion) { # The nuget list for experimental versions will cost more time @@ -112,56 +213,36 @@ if ($latestVersion) { exit 1 } -# Update packages.config files -Get-ChildItem -Path $rootPath -Recurse packages.config | ForEach-Object { - $file = Read-FileWithEncoding -Path $_.FullName - $content = $file.Content - if ($content -match 'package id="Microsoft.WindowsAppSDK"') { - $newVersionString = 'package id="Microsoft.WindowsAppSDK" version="' + $WinAppSDKVersion + '"' - $oldVersionString = 'package id="Microsoft.WindowsAppSDK" version="[-.0-9a-zA-Z]*"' - $content = $content -replace $oldVersionString, $newVersionString - Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding - Write-Host "Modified " $_.FullName - } -} +# Resolve dependencies for 1.8+ +$packageVersions = @{ "Microsoft.WindowsAppSDK" = $WinAppSDKVersion } + +Resolve-WinAppSdkSplitDependencies # Update Directory.Packages.props file Get-ChildItem -Path $rootPath -Recurse "Directory.Packages.props" | ForEach-Object { $file = Read-FileWithEncoding -Path $_.FullName $content = $file.Content - if ($content -match '<PackageVersion Include="Microsoft.WindowsAppSDK"') { - $newVersionString = '<PackageVersion Include="Microsoft.WindowsAppSDK" Version="' + $WinAppSDKVersion + '" />' - $oldVersionString = '<PackageVersion Include="Microsoft.WindowsAppSDK" Version="[-.0-9a-zA-Z]*" />' - $content = $content -replace $oldVersionString, $newVersionString + $isModified = $false + + foreach ($pkgId in $packageVersions.Keys) { + $ver = $packageVersions[$pkgId] + # Escape dots in package ID for regex + $pkgIdRegex = $pkgId -replace '\.', '\.' + + $newVersionString = "<PackageVersion Include=""$pkgId"" Version=""$ver"" />" + $oldVersionString = "<PackageVersion Include=""$pkgIdRegex"" Version=""[-.0-9a-zA-Z]*"" />" + + if ($content -match "<PackageVersion Include=""$pkgIdRegex""") { + # Update existing package + if ($content -notmatch [regex]::Escape($newVersionString)) { + $content = $content -replace $oldVersionString, $newVersionString + $isModified = $true + } + } + } + + if ($isModified) { Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding Write-Host "Modified " $_.FullName } } - -# Update .vcxproj files -Get-ChildItem -Path $rootPath -Recurse *.vcxproj | ForEach-Object { - $file = Read-FileWithEncoding -Path $_.FullName - $content = $file.Content - if ($content -match '\\Microsoft.WindowsAppSDK.') { - $newVersionString = '\Microsoft.WindowsAppSDK.' + $WinAppSDKVersion - $oldVersionString = '\\Microsoft.WindowsAppSDK.(?=[-.0-9a-zA-Z]*\d)[-.0-9a-zA-Z]*' #positive lookahead for at least a digit - $content = $content -replace $oldVersionString, $newVersionString - Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding - Write-Host "Modified " $_.FullName - } -} - -# Update .csproj files -Get-ChildItem -Path $rootPath -Recurse *.csproj | ForEach-Object { - $file = Read-FileWithEncoding -Path $_.FullName - $content = $file.Content - if ($content -match 'PackageReference Include="Microsoft.WindowsAppSDK"') { - $newVersionString = 'PackageReference Include="Microsoft.WindowsAppSDK" Version="'+ $WinAppSDKVersion + '"' - $oldVersionString = 'PackageReference Include="Microsoft.WindowsAppSDK" Version="[-.0-9a-zA-Z]*"' - $content = $content -replace $oldVersionString, $newVersionString - Write-FileWithEncoding -Path $_.FullName -Content $content -Encoding $file.encoding - Write-Host "Modified " $_.FullName - } -} - -Update-NugetConfig diff --git a/.pipelines/v2/ci-using-the-latest-winappsdk.yml b/.pipelines/v2/ci-using-the-latest-winappsdk.yml index 4e642ddfd6..16639f44c0 100644 --- a/.pipelines/v2/ci-using-the-latest-winappsdk.yml +++ b/.pipelines/v2/ci-using-the-latest-winappsdk.yml @@ -19,7 +19,7 @@ parameters: - name: enableMsBuildCaching type: boolean displayName: "Enable MSBuild Caching" - default: true + default: false - name: runTests type: boolean displayName: "Run Tests" @@ -33,7 +33,7 @@ parameters: default: true - name: winAppSDKVersionNumber type: string - default: 1.7 + default: 1.8 - name: useExperimentalVersion type: boolean default: false diff --git a/.pipelines/v2/templates/job-build-project.yml b/.pipelines/v2/templates/job-build-project.yml index 4ce0c0e7c0..57fdba6397 100644 --- a/.pipelines/v2/templates/job-build-project.yml +++ b/.pipelines/v2/templates/job-build-project.yml @@ -129,7 +129,7 @@ jobs: MSBuildMainBuildTargets: Build ${{ insert }}: ${{ parameters.variables }} ${{ if eq(parameters.useLatestWinAppSDK, true) }}: - RestoreAdditionalProjectSourcesArg: '/p:RestoreAdditionalProjectSources="$(Build.SourcesDirectory)\localpackages\NugetPackages"' + RestoreAdditionalProjectSourcesArg: '/p:RestoreAdditionalProjectSources="$(Build.SourcesDirectory)\localpackages\NugetPackages" /p:IgnoreExperimentalWarnings=true' ${{ else }}: RestoreAdditionalProjectSourcesArg: '' displayName: Build @@ -624,4 +624,4 @@ jobs: - publish: $(JobOutputDirectory) artifact: $(JobOutputArtifactName)-failure-$(System.JobAttempt) displayName: Publish failure logs - condition: or(failed(), canceled()) + condition: or(failed(), canceled()) \ No newline at end of file diff --git a/.pipelines/v2/templates/pipeline-ci-build.yml b/.pipelines/v2/templates/pipeline-ci-build.yml index 30c1dbc757..23b422196f 100644 --- a/.pipelines/v2/templates/pipeline-ci-build.yml +++ b/.pipelines/v2/templates/pipeline-ci-build.yml @@ -63,3 +63,20 @@ stages: winAppSDKVersionNumber: ${{ parameters.winAppSDKVersionNumber }} useExperimentalVersion: ${{ parameters.useExperimentalVersion }} timeoutInMinutes: 90 + + - stage: Build_SDK + displayName: Build Command Palette Toolkit SDK + dependsOn: [] + jobs: + - template: job-build-sdk.yml + parameters: + pool: + ${{ if eq(variables['System.CollectionId'], 'cb55739e-4afe-46a3-970f-1b49d8ee7564') }}: + name: SHINE-INT-L + ${{ else }}: + name: SHINE-OSS-L + ${{ if eq(parameters.useVSPreview, true) }}: + demands: ImageOverride -equals SHINE-VS17-Preview + buildConfigurations: [Release] + official: false + codeSign: false diff --git a/.pipelines/v2/templates/steps-update-winappsdk-and-restore-nuget.yml b/.pipelines/v2/templates/steps-update-winappsdk-and-restore-nuget.yml index cdb28b572c..566c8045c4 100644 --- a/.pipelines/v2/templates/steps-update-winappsdk-and-restore-nuget.yml +++ b/.pipelines/v2/templates/steps-update-winappsdk-and-restore-nuget.yml @@ -19,48 +19,20 @@ steps: -useExperimentalVersion $${{ parameters.useExperimentalVersion }} -rootPath "$(build.sourcesdirectory)" -- script: echo $(WinAppSDKVersion) - displayName: 'Display WinAppSDK Version Found' +# - task: NuGetCommand@2 +# displayName: 'Restore NuGet packages (slnx)' +# inputs: +# command: 'restore' +# feedsToUse: 'config' +# nugetConfigPath: '$(build.sourcesdirectory)\nuget.config' +# restoreSolution: '$(build.sourcesdirectory)\**\*.slnx' +# includeNuGetOrg: false -- task: DownloadPipelineArtifact@2 - displayName: 'Download WindowsAppSDK' - inputs: - buildType: 'specific' - project: '55e8140e-57ac-4e5f-8f9c-c7c15b51929d' - definition: '104083' - buildVersionToDownload: 'latestFromBranch' - branchName: 'refs/heads/release/${{ parameters.versionNumber }}-stable' - artifactName: 'WindowsAppSDK_Nuget_And_MSIX' - targetPath: '$(Build.SourcesDirectory)\localpackages' - -- script: dir $(Build.SourcesDirectory)\localpackages\NugetPackages - displayName: 'List downloaded packages' - -- task: NuGetCommand@2 - displayName: 'Install WindowsAppSDK' - inputs: - command: 'custom' - arguments: > - install "Microsoft.WindowsAppSDK" - -Source "$(Build.SourcesDirectory)\localpackages\NugetPackages" - -Version "$(WinAppSDKVersion)" - -OutputDirectory "$(Build.SourcesDirectory)\localpackages\output" - -FallbackSource "https://microsoft.pkgs.visualstudio.com/ProjectReunion/_packaging/Project.Reunion.nuget.internal/nuget/v3/index.json" - -- task: NuGetCommand@2 - displayName: 'Restore NuGet packages' +- task: DotNetCoreCLI@2 + displayName: 'Restore NuGet packages (dotnet)' inputs: command: 'restore' + projects: '$(build.sourcesdirectory)\**\*.slnx' feedsToUse: 'config' nugetConfigPath: '$(build.sourcesdirectory)\nuget.config' - restoreSolution: '$(build.sourcesdirectory)\**\*.sln' - includeNuGetOrg: false - -- task: NuGetCommand@2 - displayName: 'Restore NuGet packages (slnx)' - inputs: - command: 'restore' - feedsToUse: 'config' - nugetConfigPath: '$(build.sourcesdirectory)\nuget.config' - restoreSolution: '$(build.sourcesdirectory)\**\*.slnx' - includeNuGetOrg: false + workingDirectory: '$(build.sourcesdirectory)' diff --git a/.pipelines/versionAndSignCheck.ps1 b/.pipelines/versionAndSignCheck.ps1 index cf1f515e78..5b03250dd6 100644 --- a/.pipelines/versionAndSignCheck.ps1 +++ b/.pipelines/versionAndSignCheck.ps1 @@ -104,4 +104,4 @@ if ($totalFailure -gt 0) { exit 1 } -exit 0 +exit 0 \ No newline at end of file diff --git a/DATA_AND_PRIVACY.md b/DATA_AND_PRIVACY.md index 8aba94f12f..c2699e7f9d 100644 --- a/DATA_AND_PRIVACY.md +++ b/DATA_AND_PRIVACY.md @@ -262,6 +262,7 @@ _If you want to find diagnostic data events in the source code, these two links </table> ### Command Palette + <table style="width:100%"> <tr> <th>Event Name</th> @@ -315,6 +316,14 @@ _If you want to find diagnostic data events in the source code, these two links <td>Microsoft.PowerToys.CmdPalProcessStarted</td> <td>Triggered when the Command Palette process is started.</td> </tr> + <tr> + <td>Microsoft.PowerToys.CmdPal_ExtensionInvoked</td> + <td>Tracks extension usage including extension ID, command details, success status, and execution time.</td> + </tr> + <tr> + <td>Microsoft.PowerToys.CmdPal_SessionDuration</td> + <td>Logs session metrics from launch to dismissal including duration, commands executed, pages visited, search queries, navigation depth, and errors.</td> + </tr> </table> ### Crop And Lock diff --git a/Directory.Build.targets b/Directory.Build.targets index 6da66bc8a8..ab9bad297e 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -8,4 +8,24 @@ <PropertyGroup Label="ManifestToolOverride"> <ManifestTool Condition="Exists('$(WindowsSdkDir)bin\x64\mt.exe')">$(WindowsSdkDir)bin\x64\mt.exe</ManifestTool> </PropertyGroup> + + <!-- Auto-restore NuGet for native vcxproj (PackageReference) when building inside VS --> + <Target Name="EnsureNuGetRestoreForVcxproj" BeforeTargets="PrepareForBuild" Condition=" + '$(BuildingInsideVisualStudio)' == 'true' + and '$(DesignTimeBuild)' != 'true' + and '$(RestoreInProgress)' != 'true' + and '$(MSBuildProjectExtension)' == '.vcxproj' + and '$(RestoreProjectStyle)' == 'PackageReference' + and '$(MSBuildProjectExtensionsPath)' != '' + and !Exists('$(MSBuildProjectExtensionsPath)project.assets.json') + "> + + <Message Importance="normal" Text="NuGet assets missing for $(MSBuildProjectName); running Restore...; IntDir=$(IntDir); BaseIntermediateOutputPath=$(BaseIntermediateOutputPath)" /> + + <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="Restore" Properties="RestoreInProgress=true" BuildInParallel="false" /> + </Target> + + <PropertyGroup Condition="'$(IgnoreExperimentalWarnings)' == 'true'"> + <NoWarn>$(NoWarn);CS8305;SA1500;CA1852</NoWarn> + </PropertyGroup> </Project> \ No newline at end of file diff --git a/Directory.Packages.props b/Directory.Packages.props index ef708baa0f..6c6f5e3b0b 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -37,6 +37,7 @@ <!-- Including MessagePack to force version, since it's used by StreamJsonRpc but contains vulnerabilities. After StreamJsonRpc updates the version of MessagePack, we can upgrade StreamJsonRpc instead. --> <PackageVersion Include="MessagePack" Version="3.1.3" /> <PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="9.0.0" /> + <PackageVersion Include="Microsoft.CommandPalette.Extensions" Version="0.5.250829002" /> <PackageVersion Include="Microsoft.Data.Sqlite" Version="9.0.10" /> <!-- Including Microsoft.Bcl.AsyncInterfaces to force version, since it's used by Microsoft.SemanticKernel. --> <PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.10" /> @@ -95,6 +96,7 @@ <PackageVersion Include="RtfPipe" Version="2.0.7677.4303" /> <PackageVersion Include="ScipBe.Common.Office.OneNote" Version="3.0.1" /> <PackageVersion Include="SharpCompress" Version="0.37.2" /> + <PackageVersion Include="Shmuelie.WinRTServer" Version="2.1.1" /> <!-- Don't update SkiaSharp.Views.WinUI to version 3.* branch as this brakes the HexBox control in Registry Preview. --> <PackageVersion Include="SkiaSharp.Views.WinUI" Version="2.88.9" /> <PackageVersion Include="StreamJsonRpc" Version="2.21.69" /> @@ -148,4 +150,4 @@ <PackageVersion Include="Microsoft.VariantAssignment.Client" Version="2.4.17140001" /> <PackageVersion Include="Microsoft.VariantAssignment.Contract" Version="3.0.16990001" /> </ItemGroup> -</Project> \ No newline at end of file +</Project> diff --git a/NOTICE.md b/NOTICE.md index 08a99bafcf..e1a32d6f76 100644 --- a/NOTICE.md +++ b/NOTICE.md @@ -1591,6 +1591,7 @@ SOFTWARE. - ReverseMarkdown - ScipBe.Common.Office.OneNote - SharpCompress +- Shmuelie.WinRTServer - SkiaSharp.Views.WinUI - StreamJsonRpc - StyleCop.Analyzers diff --git a/PowerToys.slnx b/PowerToys.slnx index 7c880497f8..d62aadb7a9 100644 --- a/PowerToys.slnx +++ b/PowerToys.slnx @@ -44,6 +44,10 @@ <Platform Solution="*|ARM64" Project="ARM64" /> <Platform Solution="*|x64" Project="x64" /> </Project> + <Project Path="src/common/PowerToys.ModuleContracts/PowerToys.ModuleContracts.csproj"> + <Platform Solution="*|ARM64" Project="ARM64" /> + <Platform Solution="*|x64" Project="x64" /> + </Project> <Project Path="src/common/SettingsAPI/SettingsAPI.vcxproj" Id="6955446d-23f7-4023-9bb3-8657f904af99" /> <Project Path="src/common/Themes/Themes.vcxproj" Id="98537082-0fdb-40de-abd8-0dc5a4269bab" /> <Project Path="src/common/UITestAutomation/UITestAutomation.csproj"> @@ -156,6 +160,10 @@ <Project Path="src/modules/alwaysontop/AlwaysOnTopModuleInterface/AlwaysOnTopModuleInterface.vcxproj" Id="48a0a19e-a0be-4256-acf8-cc3b80291af9" /> </Folder> <Folder Name="/modules/awake/"> + <Project Path="src/modules/awake/Awake.ModuleServices/Awake.ModuleServices.csproj"> + <Platform Solution="*|ARM64" Project="ARM64" /> + <Platform Solution="*|x64" Project="x64" /> + </Project> <Project Path="src/modules/awake/Awake/Awake.csproj"> <Platform Solution="*|ARM64" Project="ARM64" /> <Platform Solution="*|x64" Project="x64" /> @@ -166,6 +174,10 @@ <Project Path="src/modules/cmdNotFound/CmdNotFoundModuleInterface/CmdNotFoundModuleInterface.vcxproj" Id="0014d652-901f-4456-8d65-06fc5f997fb0" /> </Folder> <Folder Name="/modules/colorpicker/"> + <Project Path="src/modules/colorPicker/ColorPicker.ModuleServices/ColorPicker.ModuleServices.csproj"> + <Platform Solution="*|ARM64" Project="ARM64" /> + <Platform Solution="*|x64" Project="x64" /> + </Project> <Project Path="src/modules/colorPicker/ColorPicker/ColorPicker.vcxproj" Id="655c9af2-18d3-4da6-80e4-85504a7722ba"> <BuildDependency Project="src/common/logger/logger.vcxproj" /> </Project> @@ -206,6 +218,11 @@ <Platform Solution="*|x64" Project="x64" /> <Deploy /> </Project> + <Project Path="src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Microsoft.CmdPal.Ext.PowerToys.csproj"> + <Platform Solution="*|ARM64" Project="ARM64" /> + <Platform Solution="*|x64" Project="x64" /> + <Deploy /> + </Project> <Project Path="src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Registry/Microsoft.CmdPal.Ext.Registry.csproj"> <Platform Solution="*|ARM64" Project="ARM64" /> <Platform Solution="*|x64" Project="x64" /> @@ -949,6 +966,10 @@ <Project Path="src/modules/ShortcutGuide/ShortcutGuideModuleInterface/ShortcutGuideModuleInterface.vcxproj" Id="2d604c07-51fc-46bb-9eb7-75aecc7f5e81" /> </Folder> <Folder Name="/modules/Workspaces/"> + <Project Path="src/modules/Workspaces/Workspaces.ModuleServices/Workspaces.ModuleServices.csproj"> + <Platform Solution="*|ARM64" Project="ARM64" /> + <Platform Solution="*|x64" Project="x64" /> + </Project> <Project Path="src/modules/Workspaces/WorkspacesCsharpLibrary/WorkspacesCsharpLibrary.csproj"> <Platform Solution="*|ARM64" Project="ARM64" /> <Platform Solution="*|x64" Project="x64" /> diff --git a/installer/PowerToysSetupVNext/BaseApplications.wxs b/installer/PowerToysSetupVNext/BaseApplications.wxs index 1947cbf1f2..57a9c71637 100644 --- a/installer/PowerToysSetupVNext/BaseApplications.wxs +++ b/installer/PowerToysSetupVNext/BaseApplications.wxs @@ -7,11 +7,18 @@ <Fragment> <DirectoryRef Id="INSTALLFOLDER"> + <Component Id="Microsoft_CommandPalette_Extensions_winmd" Guid="304AD25A-A986-4058-940E-61DB79EBD78C" Bitness="always64"> + <RegistryKey Root="$(var.RegistryScope)" Key="Software\Classes\powertoys\components"> + <RegistryValue Type="string" Name="Microsoft_CommandPalette_Extensions_winmd" Value="" KeyPath="yes" /> + </RegistryKey> + <File Id="Microsoft.CommandPalette.Extensions.winmd" Source="$(var.BinDir)Microsoft.CommandPalette.Extensions.winmd" /> + </Component> <!-- Generated by generateFileComponents.ps1 --> <!--BaseApplicationsFiles_Component_Def--> </DirectoryRef> <ComponentGroup Id="BaseApplicationsComponentGroup"> + <ComponentRef Id="Microsoft_CommandPalette_Extensions_winmd" /> </ComponentGroup> </Fragment> diff --git a/installer/PowerToysSetupVNext/PowerToysInstallerVNext.wixproj b/installer/PowerToysSetupVNext/PowerToysInstallerVNext.wixproj index c1f92f9e0a..4000503edf 100644 --- a/installer/PowerToysSetupVNext/PowerToysInstallerVNext.wixproj +++ b/installer/PowerToysSetupVNext/PowerToysInstallerVNext.wixproj @@ -175,4 +175,4 @@ call powershell.exe -NonInteractive -executionpolicy Unrestricted -File $(MSBuil </ItemGroup> </Target> <Target Name="Restore" /> -</Project> \ No newline at end of file +</Project> diff --git a/installer/PowerToysSetupVNext/Product.wxs b/installer/PowerToysSetupVNext/Product.wxs index 5fa48a1f65..5256af42fd 100644 --- a/installer/PowerToysSetupVNext/Product.wxs +++ b/installer/PowerToysSetupVNext/Product.wxs @@ -125,7 +125,7 @@ <Custom Action="SetBundleInstallLocation" After="InstallFiles" Condition="NOT Installed OR WIX_UPGRADE_DETECTED" /> <Custom Action="ApplyModulesRegistryChangeSets" After="InstallFiles" Condition="NOT Installed" /> <Custom Action="InstallCmdPalPackage" After="InstallFiles" Condition="NOT Installed" /> - <Custom Action="InstallPackageIdentityMSIX" After="InstallFiles" Condition="NOT Installed" /> + <Custom Action="InstallPackageIdentityMSIX" After="InstallFiles" Condition="NOT Installed AND WINDOWSBUILDNUMBER >= 22000" /> <Custom Action="override Wix4CloseApplications_$(sys.BUILDARCHSHORT)" Before="RemoveFiles" /> <Custom Action="RemovePowerToysSchTasks" After="RemoveFiles" /> <!-- TODO: Use to activate embedded MSIX --> diff --git a/src/PackageIdentity/AppxManifest.xml b/src/PackageIdentity/AppxManifest.xml index 822daae8bc..502cc33ff0 100644 --- a/src/PackageIdentity/AppxManifest.xml +++ b/src/PackageIdentity/AppxManifest.xml @@ -10,7 +10,8 @@ xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" xmlns:uap10="http://schemas.microsoft.com/appx/manifest/uap/windows10/10" xmlns:systemai="http://schemas.microsoft.com/appx/manifest/systemai/windows10" - IgnorableNamespaces="uap uap2 uap3 rescap desktop uap10 systemai"> + xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10" + IgnorableNamespaces="uap uap2 uap3 rescap desktop uap10 systemai com"> <Identity Name="Microsoft.PowerToys.SparseApp" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" @@ -30,6 +31,7 @@ <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.19000.0" MaxVersionTested="10.0.26226.0" /> </Dependencies> <Capabilities> + <Capability Name="internetClient" /> <rescap:Capability Name="runFullTrust" /> <systemai:Capability Name="systemAIModels"/> <rescap:Capability Name="unvirtualizedResources"/> @@ -66,5 +68,42 @@ AppListEntry="none"> </uap:VisualElements> </Application> + <Application Id="PowerToys.CmdPalExtension" Executable="Microsoft.CmdPal.Ext.PowerToys.exe" EntryPoint="Windows.FullTrustApplication"> + <uap:VisualElements + DisplayName="PowerToys.CommandPaletteExtension" + Description="PowerToys Command Palette Extension" + BackgroundColor="transparent" + Square150x150Logo="Images\Square150x150Logo.png" + Square44x44Logo="Images\Square44x44Logo.png" + AppListEntry="none"> + </uap:VisualElements> + <Extensions> + <com:Extension Category="windows.comServer"> + <com:ComServer> + <com:ExeServer Executable="Microsoft.CmdPal.Ext.PowerToys.exe" Arguments="-RegisterProcessAsComServer" DisplayName="PowerToys Command Palette Extension"> + <com:Class Id="7EC02C7D-8F98-4A2E-9F23-B58C2C2F2B17" DisplayName="PowerToys Command Palette Extension" /> + </com:ExeServer> + </com:ComServer> + </com:Extension> + <uap3:Extension Category="windows.appExtension"> + <uap3:AppExtension Name="com.microsoft.commandpalette" + Id="PowerToys" + PublicFolder="Public" + DisplayName="PowerToys" + Description="Surface PowerToys commands inside Command Palette"> + <uap3:Properties> + <CmdPalProvider> + <Activation> + <CreateInstance ClassId="7EC02C7D-8F98-4A2E-9F23-B58C2C2F2B17" /> + </Activation> + <SupportedInterfaces> + <Commands/> + </SupportedInterfaces> + </CmdPalProvider> + </uap3:Properties> + </uap3:AppExtension> + </uap3:Extension> + </Extensions> + </Application> </Applications> -</Package> \ No newline at end of file +</Package> diff --git a/src/common/Common.UI/SettingsDeepLink.cs b/src/common/Common.UI/SettingsDeepLink.cs index 7023b3001d..fedf5480e3 100644 --- a/src/common/Common.UI/SettingsDeepLink.cs +++ b/src/common/Common.UI/SettingsDeepLink.cs @@ -2,8 +2,10 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Diagnostics; using System.IO; +using ManagedCommon; namespace Common.UI { @@ -123,28 +125,33 @@ namespace Common.UI } } - public static void OpenSettings(SettingsWindow window, bool mainExecutableIsOnTheParentFolder) + // What about debug build? Should also consider debug build, maybe tray window message? + public static void OpenSettings(SettingsWindow window) { try { - var directoryPath = System.AppContext.BaseDirectory; - if (mainExecutableIsOnTheParentFolder) + var exePath = Path.Combine( + PowerToysPathResolver.GetPowerToysInstallPath(), + "PowerToys.exe"); + + if (exePath == null || !File.Exists(exePath)) { - // Need to go into parent folder for PowerToys.exe. Likely a WinUI3 App SDK application. - directoryPath = Path.Combine(directoryPath, ".."); - directoryPath = Path.Combine(directoryPath, "PowerToys.exe"); - } - else - { - // PowerToys.exe is in the same path as the application. - directoryPath = Path.Combine(directoryPath, "PowerToys.exe"); + Logger.LogError($"Failed to find powertoys exe path, {exePath}"); + return; } - Process.Start(new ProcessStartInfo(directoryPath) { Arguments = "--open-settings=" + SettingsWindowNameToString(window) }); + var args = "--open-settings=" + SettingsWindowNameToString(window); + + Process.Start(new ProcessStartInfo + { + FileName = exePath, + Arguments = args, + UseShellExecute = false, + }); } - catch + catch (Exception ex) { - // TODO(stefan): Log exception once unified logging is implemented + Logger.LogError(ex.Message); } } } diff --git a/src/common/ManagedCommon/PowerToysPathResolver.cs b/src/common/ManagedCommon/PowerToysPathResolver.cs new file mode 100644 index 0000000000..fc6afee818 --- /dev/null +++ b/src/common/ManagedCommon/PowerToysPathResolver.cs @@ -0,0 +1,168 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics; +using System.IO; +using System.Runtime.Versioning; +using Microsoft.Win32; + +namespace ManagedCommon +{ + [SupportedOSPlatform("windows")] + public class PowerToysPathResolver + { + private const string PowerToysRegistryKey = @"Software\Classes\powertoys"; + private const string PowerToysExe = "PowerToys.exe"; + + /// <summary> + /// Gets the PowerToys installation path by checking registry entries + /// </summary> + /// <returns>The path to PowerToys installation directory, or null if not found</returns> + public static string GetPowerToysInstallPath() + { +#if DEBUG + // In debug builds, resolve directly from the running process (no installer/registry involved). + return GetPathFromCurrentProcess(); +#else + // Try to get path from Per-User installation first + string path = GetPathFromRegistry(RegistryHive.CurrentUser); + if (!string.IsNullOrEmpty(path)) + { + return path; + } + + // Fall back to Per-Machine installation + path = GetPathFromRegistry(RegistryHive.LocalMachine); + if (!string.IsNullOrEmpty(path)) + { + return path; + } + + return null; +#endif + } + + private static string GetPathFromRegistry(RegistryHive hive) + { + try + { + using var baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Registry64); + + // First try to get path from the powertoys protocol registration + string path = GetPathFromProtocolRegistration(baseKey); + if (!string.IsNullOrEmpty(path)) + { + return path; + } + } + catch (Exception) + { + // Ignore registry access errors + } + + return null; + } + + private static string GetPathFromProtocolRegistration(RegistryKey baseKey) + { + try + { + using var key = baseKey.OpenSubKey($@"{PowerToysRegistryKey}\shell\open\command"); + + if (key != null) + { + string command = key.GetValue(string.Empty)?.ToString(); + if (!string.IsNullOrEmpty(command)) + { + // Parse command like: "C:\Program Files\PowerToys\PowerToys.exe" "%1" + return ExtractPathFromCommand(command); + } + } + } + catch (Exception) + { + // Ignore registry access errors + } + + return null; + } + + private static string GetPathFromCurrentProcess() + { + try + { + // If we're running inside PowerToys.exe (dev/debug builds), use the executable location. + var processPath = Process.GetCurrentProcess().MainModule?.FileName; + if (!string.IsNullOrEmpty(processPath)) + { + var processDir = Path.GetDirectoryName(processPath); + if (!string.IsNullOrEmpty(processDir) && File.Exists(Path.Combine(processDir, PowerToysExe))) + { + return processDir; + } + } + + // As a fallback, walk up from AppContext.BaseDirectory to find PowerToys.exe. + var directory = new DirectoryInfo(AppContext.BaseDirectory); + while (directory != null) + { + var candidate = Path.Combine(directory.FullName, PowerToysExe); + if (File.Exists(candidate)) + { + return directory.FullName; + } + + directory = directory.Parent; + } + } + catch + { + // Ignore reflection/process permission errors; caller will see null and handle accordingly. + } + + return null; + } + + private static string ExtractPathFromCommand(string command) + { + if (string.IsNullOrEmpty(command)) + { + return null; + } + + try + { + // Handle quoted paths: "C:\Program Files\PowerToys\PowerToys.exe" "%1" + if (command.StartsWith('\"')) + { + int endQuote = command.IndexOf('\"', 1); + if (endQuote > 1) + { + string exePath = command.Substring(1, endQuote - 1); + if (File.Exists(exePath)) + { + return Path.GetDirectoryName(exePath); + } + } + } + else + { + // Handle unquoted paths (less common) + string[] parts = command.Split(' '); + if (parts.Length > 0 && File.Exists(parts[0])) + { + return Path.GetDirectoryName(parts[0]); + } + } + } + catch (Exception) + { + // Ignore path parsing errors + } + + return null; + } + } +} diff --git a/src/common/PowerToys.ModuleContracts/IModuleService.cs b/src/common/PowerToys.ModuleContracts/IModuleService.cs new file mode 100644 index 0000000000..845d40e656 --- /dev/null +++ b/src/common/PowerToys.ModuleContracts/IModuleService.cs @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Common.UI; + +namespace PowerToys.ModuleContracts; + +/// <summary> +/// Base contract for PowerToys modules exposed to the Command Palette. +/// </summary> +public interface IModuleService +{ + /// <summary> + /// Gets module identifier (e.g., Workspaces, Awake). + /// </summary> + string Key { get; } + + Task<OperationResult> LaunchAsync(CancellationToken cancellationToken = default); + + Task<OperationResult> OpenSettingsAsync(CancellationToken cancellationToken = default); +} + +/// <summary> +/// Helper base to reduce duplication for simple modules. +/// </summary> +public abstract class ModuleServiceBase : IModuleService +{ + public abstract string Key { get; } + + protected abstract SettingsDeepLink.SettingsWindow SettingsWindow { get; } + + public abstract Task<OperationResult> LaunchAsync(CancellationToken cancellationToken = default); + + public virtual Task<OperationResult> OpenSettingsAsync(CancellationToken cancellationToken = default) + { + try + { + SettingsDeepLink.OpenSettings(SettingsWindow); + return Task.FromResult(OperationResult.Ok()); + } + catch (Exception ex) + { + return Task.FromResult(OperationResult.Fail($"Failed to open settings for {Key}: {ex.Message}")); + } + } +} diff --git a/src/common/PowerToys.ModuleContracts/OperationResult.cs b/src/common/PowerToys.ModuleContracts/OperationResult.cs new file mode 100644 index 0000000000..a20aa26a3f --- /dev/null +++ b/src/common/PowerToys.ModuleContracts/OperationResult.cs @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace PowerToys.ModuleContracts; + +/// <summary> +/// Lightweight result type for module operations. +/// </summary> +public readonly record struct OperationResult(bool Success, string? Error = null) +{ + public static OperationResult Ok() => new(true, null); + + public static OperationResult Fail(string error) => new(false, error); +} + +/// <summary> +/// Result type with a payload. +/// </summary> +public readonly record struct OperationResult<T>(bool Success, T? Value, string? Error = null); + +/// <summary> +/// Factory helpers for creating operation results. +/// </summary> +public static class OperationResults +{ + public static OperationResult<T> Ok<T>(T value) => new(true, value, null); + + public static OperationResult<T> Fail<T>(string error) => new(false, default, error); +} diff --git a/src/common/PowerToys.ModuleContracts/PowerToys.ModuleContracts.csproj b/src/common/PowerToys.ModuleContracts/PowerToys.ModuleContracts.csproj new file mode 100644 index 0000000000..aa80bb05fb --- /dev/null +++ b/src/common/PowerToys.ModuleContracts/PowerToys.ModuleContracts.csproj @@ -0,0 +1,16 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <!-- Look at Directory.Build.props in root for common stuff as well --> + <Import Project="..\..\Common.Dotnet.CsWinRT.props" /> + <Import Project="..\..\Common.Dotnet.AotCompatibility.props" /> + + <PropertyGroup> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> + <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\Common.UI\Common.UI.csproj" /> + </ItemGroup> +</Project> diff --git a/src/common/interop/Constants.cpp b/src/common/interop/Constants.cpp index 6ae09fce8f..c326b08565 100644 --- a/src/common/interop/Constants.cpp +++ b/src/common/interop/Constants.cpp @@ -75,10 +75,62 @@ namespace winrt::PowerToys::Interop::implementation { return CommonSharedConstants::ADVANCED_PASTE_CUSTOM_ACTION_MESSAGE; } + hstring Constants::AdvancedPasteShowUIEvent() + { + return CommonSharedConstants::ADVANCED_PASTE_SHOW_UI_EVENT; + } hstring Constants::AdvancedPasteTerminateAppMessage() { return CommonSharedConstants::ADVANCED_PASTE_TERMINATE_APP_MESSAGE; } + hstring Constants::AlwaysOnTopPinEvent() + { + return CommonSharedConstants::ALWAYS_ON_TOP_PIN_EVENT; + } + hstring Constants::FindMyMouseTriggerEvent() + { + return CommonSharedConstants::FIND_MY_MOUSE_TRIGGER_EVENT; + } + hstring Constants::MouseHighlighterTriggerEvent() + { + return CommonSharedConstants::MOUSE_HIGHLIGHTER_TRIGGER_EVENT; + } + hstring Constants::MouseCrosshairsTriggerEvent() + { + return CommonSharedConstants::MOUSE_CROSSHAIRS_TRIGGER_EVENT; + } + hstring Constants::CursorWrapTriggerEvent() + { + return CommonSharedConstants::CURSOR_WRAP_TRIGGER_EVENT; + } + hstring Constants::LightSwitchToggleEvent() + { + return CommonSharedConstants::LIGHTSWITCH_TOGGLE_EVENT; + } + hstring Constants::ZoomItZoomEvent() + { + return CommonSharedConstants::ZOOMIT_ZOOM_EVENT; + } + hstring Constants::ZoomItDrawEvent() + { + return CommonSharedConstants::ZOOMIT_DRAW_EVENT; + } + hstring Constants::ZoomItBreakEvent() + { + return CommonSharedConstants::ZOOMIT_BREAK_EVENT; + } + hstring Constants::ZoomItLiveZoomEvent() + { + return CommonSharedConstants::ZOOMIT_LIVEZOOM_EVENT; + } + hstring Constants::ZoomItSnipEvent() + { + return CommonSharedConstants::ZOOMIT_SNIP_EVENT; + } + hstring Constants::ZoomItRecordEvent() + { + return CommonSharedConstants::ZOOMIT_RECORD_EVENT; + } hstring Constants::ShowPowerOCRSharedEvent() { return CommonSharedConstants::SHOW_POWEROCR_SHARED_EVENT; diff --git a/src/common/interop/Constants.h b/src/common/interop/Constants.h index d99e36d29a..e329f043ef 100644 --- a/src/common/interop/Constants.h +++ b/src/common/interop/Constants.h @@ -23,6 +23,20 @@ namespace winrt::PowerToys::Interop::implementation static hstring AdvancedPasteAdditionalActionMessage(); static hstring AdvancedPasteCustomActionMessage(); static hstring AdvancedPasteTerminateAppMessage(); + static hstring AdvancedPasteShowUIEvent(); + static hstring AlwaysOnTopPinEvent(); + static hstring MeasureToolTriggerEvent(); + static hstring FindMyMouseTriggerEvent(); + static hstring MouseHighlighterTriggerEvent(); + static hstring MouseCrosshairsTriggerEvent(); + static hstring CursorWrapTriggerEvent(); + static hstring LightSwitchToggleEvent(); + static hstring ZoomItZoomEvent(); + static hstring ZoomItDrawEvent(); + static hstring ZoomItBreakEvent(); + static hstring ZoomItLiveZoomEvent(); + static hstring ZoomItSnipEvent(); + static hstring ZoomItRecordEvent(); static hstring ShowPowerOCRSharedEvent(); static hstring TerminatePowerOCRSharedEvent(); static hstring MouseJumpShowPreviewEvent(); @@ -33,7 +47,6 @@ namespace winrt::PowerToys::Interop::implementation static hstring PowerAccentExitEvent(); static hstring ShortcutGuideTriggerEvent(); static hstring RegistryPreviewTriggerEvent(); - static hstring MeasureToolTriggerEvent(); static hstring GcodePreviewResizeEvent(); static hstring BgcodePreviewResizeEvent(); static hstring QoiPreviewResizeEvent(); diff --git a/src/common/interop/Constants.idl b/src/common/interop/Constants.idl index be1dccabbd..335854c9e3 100644 --- a/src/common/interop/Constants.idl +++ b/src/common/interop/Constants.idl @@ -20,6 +20,19 @@ namespace PowerToys static String AdvancedPasteAdditionalActionMessage(); static String AdvancedPasteCustomActionMessage(); static String AdvancedPasteTerminateAppMessage(); + static String AdvancedPasteShowUIEvent(); + static String AlwaysOnTopPinEvent(); + static String FindMyMouseTriggerEvent(); + static String MouseHighlighterTriggerEvent(); + static String MouseCrosshairsTriggerEvent(); + static String CursorWrapTriggerEvent(); + static String LightSwitchToggleEvent(); + static String ZoomItZoomEvent(); + static String ZoomItDrawEvent(); + static String ZoomItBreakEvent(); + static String ZoomItLiveZoomEvent(); + static String ZoomItSnipEvent(); + static String ZoomItRecordEvent(); static String ShowPowerOCRSharedEvent(); static String TerminatePowerOCRSharedEvent(); static String MouseJumpShowPreviewEvent(); @@ -59,4 +72,4 @@ namespace PowerToys static String HotkeyUpdatedPowerDisplayEvent(); } } -} \ No newline at end of file +} diff --git a/src/common/interop/shared_constants.h b/src/common/interop/shared_constants.h index 9422bbba09..1f9faaa3be 100644 --- a/src/common/interop/shared_constants.h +++ b/src/common/interop/shared_constants.h @@ -40,6 +40,8 @@ namespace CommonSharedConstants const wchar_t ADVANCED_PASTE_CUSTOM_ACTION_MESSAGE[] = L"CustomAction"; const wchar_t ADVANCED_PASTE_TERMINATE_APP_MESSAGE[] = L"TerminateApp"; + + const wchar_t ADVANCED_PASTE_SHOW_UI_EVENT[] = L"Local\\PowerToys_AdvancedPaste_ShowUI"; // Path to the event used to show Color Picker const wchar_t SHOW_COLOR_PICKER_SHARED_EVENT[] = L"Local\\ShowColorPickerEvent-8c46be2a-3e05-4186-b56b-4ae986ef2525"; @@ -83,12 +85,21 @@ namespace CommonSharedConstants const wchar_t TERMINATE_MOUSE_JUMP_SHARED_EVENT[] = L"Local\\TerminateMouseJumpEvent-252fa337-317f-4c37-a61f-99464c3f9728"; + // Paths to the events used by other Mouse Utilities + const wchar_t FIND_MY_MOUSE_TRIGGER_EVENT[] = L"Local\\FindMyMouseTriggerEvent-5a9dc5f4-1c74-4f2f-a66f-1b9b6a2f9b23"; + const wchar_t MOUSE_HIGHLIGHTER_TRIGGER_EVENT[] = L"Local\\MouseHighlighterTriggerEvent-1e3c9c3d-3fdf-4f9a-9a52-31c9b3c3a8f4"; + const wchar_t MOUSE_CROSSHAIRS_TRIGGER_EVENT[] = L"Local\\MouseCrosshairsTriggerEvent-0d4c7f92-0a5c-4f5c-b64b-8a2a2f7e0b21"; + const wchar_t CURSOR_WRAP_TRIGGER_EVENT[] = L"Local\\CursorWrapTriggerEvent-1f8452b5-4e6e-45b3-8b09-13f14a5900c9"; + // Path to the event used by RegistryPreview const wchar_t REGISTRY_PREVIEW_TRIGGER_EVENT[] = L"Local\\RegistryPreviewEvent-4C559468-F75A-4E7F-BC4F-9C9688316687"; // Path to the event used by MeasureTool const wchar_t MEASURE_TOOL_TRIGGER_EVENT[] = L"Local\\MeasureToolEvent-3d46745f-09b3-4671-a577-236be7abd199"; + // Path to the event used by LightSwitch + const wchar_t LIGHTSWITCH_TOGGLE_EVENT[] = L"Local\\PowerToys-LightSwitch-ToggleEvent-d8dc2f29-8c94-4ca1-8c5f-3e2b1e3c4f5a"; + // Path to the event used by GcodePreviewHandler const wchar_t GCODE_PREVIEW_RESIZE_EVENT[] = L"Local\\PowerToysGcodePreviewResizeEvent-6ff1f9bd-ccbd-4b24-a79f-40a34fb0317d"; @@ -130,6 +141,12 @@ namespace CommonSharedConstants // Path to the events used by ZoomIt const wchar_t ZOOMIT_REFRESH_SETTINGS_EVENT[] = L"Local\\PowerToysZoomIt-RefreshSettingsEvent-f053a563-d519-4b0d-8152-a54489c13324"; const wchar_t ZOOMIT_EXIT_EVENT[] = L"Local\\PowerToysZoomIt-ExitEvent-36641ce6-df02-4eac-abea-a3fbf9138220"; + const wchar_t ZOOMIT_ZOOM_EVENT[] = L"Local\\PowerToysZoomIt-ZoomEvent-1e4190d7-94bc-4ad5-adc0-9a8fd07cb393"; + const wchar_t ZOOMIT_DRAW_EVENT[] = L"Local\\PowerToysZoomIt-DrawEvent-56338997-404d-4549-bd9a-d132b6766975"; + const wchar_t ZOOMIT_BREAK_EVENT[] = L"Local\\PowerToysZoomIt-BreakEvent-17f2e63c-4c56-41dd-90a0-2d12f9f50c6b"; + const wchar_t ZOOMIT_LIVEZOOM_EVENT[] = L"Local\\PowerToysZoomIt-LiveZoomEvent-390bf0c7-616f-47dc-bafe-a2d228add20d"; + const wchar_t ZOOMIT_SNIP_EVENT[] = L"Local\\PowerToysZoomIt-SnipEvent-2fd9c211-436d-4f17-a902-2528aaae3e30"; + const wchar_t ZOOMIT_RECORD_EVENT[] = L"Local\\PowerToysZoomIt-RecordEvent-74539344-eaad-4711-8e83-23946e424512"; // Path to the events used by PowerDisplay const wchar_t TOGGLE_POWER_DISPLAY_EVENT[] = L"Local\\PowerToysPowerDisplay-ToggleEvent-5f1a9c3e-7d2b-4e8f-9a6c-3b5d7e9f1a2c"; diff --git a/src/common/utils/EventWaiter.h b/src/common/utils/EventWaiter.h index b9f420c81d..c2db880530 100644 --- a/src/common/utils/EventWaiter.h +++ b/src/common/utils/EventWaiter.h @@ -3,78 +3,128 @@ #include <functional> #include <thread> #include <string> +#include <atomic> #include <windows.h> +/// <summary> +/// A reusable utility class that listens for a named Windows event and invokes a callback when triggered. +/// Provides RAII-based resource management for event handles and the listener thread. +/// The thread is properly joined on destruction to ensure clean shutdown. +/// </summary> class EventWaiter { public: - EventWaiter() {} - EventWaiter(const std::wstring& name, std::function<void(DWORD)> callback) + EventWaiter() = default; + + EventWaiter(const EventWaiter&) = delete; + EventWaiter& operator=(const EventWaiter&) = delete; + EventWaiter(EventWaiter&&) = delete; + EventWaiter& operator=(EventWaiter&&) = delete; + + ~EventWaiter() { - // Create localExitThreadEvent and localWaitingEvent for capturing. We cannot capture 'this' as we implement move constructor. - auto localExitThreadEvent = exitThreadEvent = CreateEvent(nullptr, false, false, nullptr); - HANDLE localWaitingEvent = waitingEvent = CreateEvent(nullptr, false, false, name.c_str()); - std::thread([=]() { - HANDLE events[2] = { localWaitingEvent, localExitThreadEvent }; - while (true) + stop(); + } + + /// <summary> + /// Starts listening for the specified named event. When the event is signaled, the callback is invoked. + /// </summary> + /// <param name="name">The name of the Windows event to listen for.</param> + /// <param name="callback">The callback function to invoke when the event is triggered. Receives ERROR_SUCCESS on success.</param> + /// <returns>true if listening started successfully, false otherwise.</returns> + bool start(const std::wstring& name, std::function<void(DWORD)> callback) + { + if (m_listening) + { + return false; + } + + m_exitThreadEvent = CreateEventW(nullptr, false, false, nullptr); + m_waitingEvent = CreateEventW(nullptr, false, false, name.c_str()); + + if (!m_exitThreadEvent || !m_waitingEvent) + { + cleanup(); + return false; + } + + m_listening = true; + m_eventThread = std::thread([this, cb = std::move(callback)]() { + HANDLE events[2] = { m_waitingEvent, m_exitThreadEvent }; + while (m_listening) { auto waitResult = WaitForMultipleObjects(2, events, false, INFINITE); + if (!m_listening) + { + break; + } + if (waitResult == WAIT_OBJECT_0 + 1) { + // Exit event signaled break; } if (waitResult == WAIT_FAILED) { - callback(GetLastError()); + cb(GetLastError()); continue; } if (waitResult == WAIT_OBJECT_0) { - callback(ERROR_SUCCESS); + cb(ERROR_SUCCESS); } } - }).detach(); + }); + + return true; } - EventWaiter(EventWaiter&) = delete; - EventWaiter& operator=(EventWaiter&) = delete; - - EventWaiter(EventWaiter&& a) noexcept + /// <summary> + /// Stops listening for the event and cleans up resources. + /// Waits for the listener thread to finish before returning. + /// Safe to call multiple times. + /// </summary> + void stop() { - this->exitThreadEvent = a.exitThreadEvent; - this->waitingEvent = a.waitingEvent; - - a.exitThreadEvent = nullptr; - a.waitingEvent = nullptr; - } - - EventWaiter& operator=(EventWaiter&& a) noexcept - { - this->exitThreadEvent = a.exitThreadEvent; - this->waitingEvent = a.waitingEvent; - - a.exitThreadEvent = nullptr; - a.waitingEvent = nullptr; - return *this; - } - - ~EventWaiter() - { - if (exitThreadEvent) + m_listening = false; + if (m_exitThreadEvent) { - SetEvent(exitThreadEvent); - CloseHandle(exitThreadEvent); + SetEvent(m_exitThreadEvent); } - - if (waitingEvent) + if (m_eventThread.joinable()) { - CloseHandle(waitingEvent); + m_eventThread.join(); } + cleanup(); + } + + /// <summary> + /// Returns whether the listener is currently active. + /// </summary> + bool is_listening() const + { + return m_listening; } private: - HANDLE exitThreadEvent = nullptr; - HANDLE waitingEvent = nullptr; + void cleanup() + { + if (m_exitThreadEvent) + { + CloseHandle(m_exitThreadEvent); + m_exitThreadEvent = nullptr; + } + if (m_waitingEvent) + { + CloseHandle(m_waitingEvent); + m_waitingEvent = nullptr; + } + } + + HANDLE m_exitThreadEvent = nullptr; + HANDLE m_waitingEvent = nullptr; + std::thread m_eventThread; + std::atomic_bool m_listening{ false }; }; \ No newline at end of file diff --git a/src/dsc/v3/PowerToys.DSC/PowerToys.DSC.csproj b/src/dsc/v3/PowerToys.DSC/PowerToys.DSC.csproj index 9dc11a0a8a..a87508604f 100644 --- a/src/dsc/v3/PowerToys.DSC/PowerToys.DSC.csproj +++ b/src/dsc/v3/PowerToys.DSC/PowerToys.DSC.csproj @@ -45,6 +45,7 @@ <Target Name="GenerateDscResourceJsonFiles" AfterTargets="Build" Condition="'$(CIBuild)' != 'true'"> <Message Text="Generating DSC resource JSON files to DSCModules subfolder..." Importance="high" /> <MakeDir Directories="$(TargetDir)DSCModules" /> + <Exec Command="dotnet "$(TargetPath)" manifest --resource settings --outputDir "$(TargetDir)DSCModules"" /> </Target> </Project> \ No newline at end of file diff --git a/src/modules/AdvancedPaste/AdvancedPaste.UnitTests/ServicesTests/AIServiceBatchIntegrationTests.cs b/src/modules/AdvancedPaste/AdvancedPaste.UnitTests/ServicesTests/AIServiceBatchIntegrationTests.cs index 17b8139bad..1f7829a0bd 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste.UnitTests/ServicesTests/AIServiceBatchIntegrationTests.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste.UnitTests/ServicesTests/AIServiceBatchIntegrationTests.cs @@ -144,7 +144,7 @@ public sealed class AIServiceBatchIntegrationTests switch (format) { case PasteFormats.CustomTextTransformation: - var transformResult = await services.CustomActionTransformService.TransformTextAsync(batchTestInput.Prompt, batchTestInput.Clipboard, CancellationToken.None, progress); + var transformResult = await services.CustomActionTransformService.TransformAsync(batchTestInput.Prompt, batchTestInput.Clipboard, null, CancellationToken.None, progress); return DataPackageHelpers.CreateFromText(transformResult.Content ?? string.Empty); case PasteFormats.KernelQuery: diff --git a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml.cs b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml.cs index 80376e5f72..5bef6389f0 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/AdvancedPasteXAML/Pages/MainPage.xaml.cs @@ -198,20 +198,14 @@ namespace AdvancedPaste.Pages } } - private async void ClipboardHistory_ItemInvoked(ItemsView sender, ItemsViewItemInvokedEventArgs args) + private void ClipboardHistory_ItemInvoked(ItemsView sender, ItemsViewItemInvokedEventArgs args) { - if (args.InvokedItem is ClipboardItem item) + if (args.InvokedItem is ClipboardItem item && item.Item is not null) { PowerToysTelemetry.Log.WriteEvent(new Telemetry.AdvancedPasteClipboardItemClicked()); - if (!string.IsNullOrEmpty(item.Content)) - { - ClipboardHelper.SetTextContent(item.Content); - } - else if (item.Image is not null) - { - RandomAccessStreamReference image = await item.Item.Content.GetBitmapAsync(); - ClipboardHelper.SetImageContent(image); - } + + // Use SetHistoryItemAsContent to set the clipboard content without creating a new history entry + Clipboard.SetHistoryItemAsContent(item.Item); } } } diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/DataPackageHelpers.cs b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/DataPackageHelpers.cs index 2cd7554a50..f5439aecf1 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/DataPackageHelpers.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/DataPackageHelpers.cs @@ -225,6 +225,24 @@ internal static class DataPackageHelpers internal static async Task<string> GetHtmlContentAsync(this DataPackageView dataPackageView) => dataPackageView.Contains(StandardDataFormats.Html) ? await dataPackageView.GetHtmlFormatAsync() : string.Empty; + internal static async Task<byte[]> GetImageAsPngBytesAsync(this DataPackageView dataPackageView) + { + var bitmap = await dataPackageView.GetImageContentAsync(); + if (bitmap == null) + { + return null; + } + + using var pngStream = new InMemoryRandomAccessStream(); + var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, pngStream); + encoder.SetSoftwareBitmap(bitmap); + await encoder.FlushAsync(); + + using var memoryStream = new MemoryStream(); + await pngStream.AsStreamForRead().CopyToAsync(memoryStream); + return memoryStream.ToArray(); + } + internal static async Task<SoftwareBitmap> GetImageContentAsync(this DataPackageView dataPackageView) { using var stream = await dataPackageView.GetImageStreamAsync(); diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/NativeMethods.cs b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/NativeMethods.cs index 6e53e9b618..08293d4be0 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Helpers/NativeMethods.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Helpers/NativeMethods.cs @@ -166,5 +166,8 @@ namespace AdvancedPaste.Helpers [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode)] internal static extern HResult AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut); + + [DllImport("user32.dll", SetLastError = true)] + internal static extern uint GetClipboardSequenceNumber(); } } diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Models/PasteFormats.cs b/src/modules/AdvancedPaste/AdvancedPaste/Models/PasteFormats.cs index 99243ebb5e..1479912e66 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Models/PasteFormats.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Models/PasteFormats.cs @@ -46,7 +46,7 @@ public enum PasteFormats CanPreview = true, SupportedClipboardFormats = ClipboardFormat.Image, IPCKey = AdvancedPasteAdditionalActions.PropertyNames.ImageToText, - KernelFunctionDescription = "Takes an image in the clipboard and extracts all text from it using OCR.")] + KernelFunctionDescription = "Takes an image from the clipboard and extracts text using OCR. This function is intended only for explicit text extraction or OCR requests.")] ImageToText, [PasteFormatMetadata( @@ -118,8 +118,8 @@ public enum PasteFormats IconGlyph = "\uE945", RequiresAIService = true, CanPreview = true, - SupportedClipboardFormats = ClipboardFormat.Text, - KernelFunctionDescription = "Takes input instructions and transforms clipboard text (not TXT files) with these input instructions, putting the result back on the clipboard. This uses AI to accomplish the task.", + SupportedClipboardFormats = ClipboardFormat.Text | ClipboardFormat.Image, + KernelFunctionDescription = "Takes user instructions and applies them to the current clipboard content (text or image). Use this function for image analysis, description, or transformation tasks beyond simple OCR.", RequiresPrompt = true)] CustomTextTransformation, } diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/CustomActionTransformService.cs b/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/CustomActionTransformService.cs index 57d55492a4..05cdcbe81f 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/CustomActionTransformService.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/CustomActionTransformService.cs @@ -40,15 +40,15 @@ namespace AdvancedPaste.Services.CustomActions this.userSettings = userSettings; } - public async Task<CustomActionTransformResult> TransformTextAsync(string prompt, string inputText, CancellationToken cancellationToken, IProgress<double> progress) + public async Task<CustomActionTransformResult> TransformAsync(string prompt, string inputText, byte[] imageBytes, CancellationToken cancellationToken, IProgress<double> progress) { var pasteConfig = userSettings?.PasteAIConfiguration; var providerConfig = BuildProviderConfig(pasteConfig); - return await TransformAsync(prompt, inputText, providerConfig, cancellationToken, progress); + return await TransformAsync(prompt, inputText, imageBytes, providerConfig, cancellationToken, progress); } - private async Task<CustomActionTransformResult> TransformAsync(string prompt, string inputText, PasteAIConfig providerConfig, CancellationToken cancellationToken, IProgress<double> progress) + private async Task<CustomActionTransformResult> TransformAsync(string prompt, string inputText, byte[] imageBytes, PasteAIConfig providerConfig, CancellationToken cancellationToken, IProgress<double> progress) { ArgumentNullException.ThrowIfNull(providerConfig); @@ -57,9 +57,9 @@ namespace AdvancedPaste.Services.CustomActions return new CustomActionTransformResult(string.Empty, AIServiceUsage.None); } - if (string.IsNullOrWhiteSpace(inputText)) + if (string.IsNullOrWhiteSpace(inputText) && imageBytes is null) { - Logger.LogWarning("Clipboard has no usable text data"); + Logger.LogWarning("Clipboard has no usable data"); return new CustomActionTransformResult(string.Empty, AIServiceUsage.None); } @@ -80,6 +80,8 @@ namespace AdvancedPaste.Services.CustomActions { Prompt = prompt, InputText = inputText, + ImageBytes = imageBytes, + ImageMimeType = imageBytes != null ? "image/png" : null, SystemPrompt = systemPrompt, }; diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/ICustomActionTransformService.cs b/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/ICustomActionTransformService.cs index 1c3ecb980c..564db3fdc5 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/ICustomActionTransformService.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/ICustomActionTransformService.cs @@ -12,6 +12,6 @@ namespace AdvancedPaste.Services.CustomActions { public interface ICustomActionTransformService { - Task<CustomActionTransformResult> TransformTextAsync(string prompt, string inputText, CancellationToken cancellationToken, IProgress<double> progress); + Task<CustomActionTransformResult> TransformAsync(string prompt, string inputText, byte[] imageBytes, CancellationToken cancellationToken, IProgress<double> progress); } } diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/PasteAIRequest.cs b/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/PasteAIRequest.cs index 0e15c93e05..96dabbfa05 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/PasteAIRequest.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/PasteAIRequest.cs @@ -12,6 +12,10 @@ namespace AdvancedPaste.Services.CustomActions public string InputText { get; init; } + public byte[] ImageBytes { get; init; } + + public string ImageMimeType { get; init; } + public string SystemPrompt { get; init; } public AIServiceUsage Usage { get; set; } = AIServiceUsage.None; diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/SemanticKernelPasteProvider.cs b/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/SemanticKernelPasteProvider.cs index eb2f56e01f..636d2e3e78 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/SemanticKernelPasteProvider.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Services/CustomActions/SemanticKernelPasteProvider.cs @@ -64,21 +64,13 @@ namespace AdvancedPaste.Services.CustomActions var prompt = request.Prompt; var inputText = request.InputText; - if (string.IsNullOrWhiteSpace(prompt) || string.IsNullOrWhiteSpace(inputText)) + var imageBytes = request.ImageBytes; + + if (string.IsNullOrWhiteSpace(prompt) || (string.IsNullOrWhiteSpace(inputText) && imageBytes is null)) { - throw new ArgumentException("Prompt and input text must be provided", nameof(request)); + throw new ArgumentException("Prompt and input content must be provided", nameof(request)); } - var userMessageContent = $""" - User instructions: - {prompt} - - Clipboard Content: - {inputText} - - Output: - """; - var executionSettings = CreateExecutionSettings(); var kernel = CreateKernel(); var modelId = _config.Model; @@ -102,7 +94,32 @@ namespace AdvancedPaste.Services.CustomActions var chatHistory = new ChatHistory(); chatHistory.AddSystemMessage(systemPrompt); - chatHistory.AddUserMessage(userMessageContent); + + if (imageBytes != null) + { + var collection = new ChatMessageContentItemCollection(); + if (!string.IsNullOrWhiteSpace(inputText)) + { + collection.Add(new TextContent($"Clipboard Content:\n{inputText}")); + } + + collection.Add(new ImageContent(imageBytes, request.ImageMimeType ?? "image/png")); + collection.Add(new TextContent($"User instructions:\n{prompt}\n\nOutput:")); + chatHistory.AddUserMessage(collection); + } + else + { + var userMessageContent = $""" + User instructions: + {prompt} + + Clipboard Content: + {inputText} + + Output: + """; + chatHistory.AddUserMessage(userMessageContent); + } var response = await chatService.GetChatMessageContentAsync(chatHistory, executionSettings, kernel, cancellationToken); chatHistory.Add(response); diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Services/KernelServiceBase.cs b/src/modules/AdvancedPaste/AdvancedPaste/Services/KernelServiceBase.cs index 47e208eb49..0d753d1ec3 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Services/KernelServiceBase.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Services/KernelServiceBase.cs @@ -67,12 +67,36 @@ public abstract class KernelServiceBase( LogResult(cacheUsed, isSavedQuery, kernel.GetOrAddActionChain(), usage); + var outputPackage = kernel.GetDataPackage(); + var hasUsableData = await outputPackage.GetView().HasUsableDataAsync(); + if (kernel.GetLastError() is Exception ex) { - throw ex; + // If we have an error, but the AI provided a final text response, we can ignore the error (likely a tool failure that the AI handled). + // However, if we have usable data (e.g. from a successful tool call before the error?), we might want to keep it? + // In the case of ImageToText failure, outputPackage is empty (new DataPackage), hasUsableData is false. + // So we check if there is a valid response in the chat history. + var lastMessage = chatHistory.LastOrDefault(); + bool hasAssistantResponse = lastMessage != null && lastMessage.Role == AuthorRole.Assistant && !string.IsNullOrEmpty(lastMessage.Content); + + if (!hasAssistantResponse && !hasUsableData) + { + throw ex; + } + + // If we have a response or data, we log the error but proceed. + Logger.LogWarning($"Kernel operation encountered an error but proceeded with available response/data: {ex.Message}"); } - var outputPackage = kernel.GetDataPackage(); + if (!hasUsableData) + { + var lastMessage = chatHistory.LastOrDefault(); + if (lastMessage != null && lastMessage.Role == AuthorRole.Assistant && !string.IsNullOrEmpty(lastMessage.Content)) + { + outputPackage = DataPackageHelpers.CreateFromText(lastMessage.Content); + kernel.SetDataPackage(outputPackage); + } + } if (!(await outputPackage.GetView().HasUsableDataAsync())) { @@ -148,7 +172,21 @@ public abstract class KernelServiceBase( var systemPrompt = string.IsNullOrWhiteSpace(runtimeConfig.SystemPrompt) ? DefaultSystemPrompt : runtimeConfig.SystemPrompt; chatHistory.AddSystemMessage(systemPrompt); chatHistory.AddSystemMessage($"Available clipboard formats: {await kernel.GetDataFormatsAsync()}"); - chatHistory.AddUserMessage(prompt); + + var imageBytes = await kernel.GetDataPackageView().GetImageAsPngBytesAsync(); + if (imageBytes != null) + { + var collection = new ChatMessageContentItemCollection + { + new TextContent(prompt), + new ImageContent(imageBytes, "image/png"), + }; + chatHistory.AddUserMessage(collection); + } + else + { + chatHistory.AddUserMessage(prompt); + } if (ShouldModerateAdvancedAI()) { @@ -302,8 +340,16 @@ public abstract class KernelServiceBase( new ActionChainItem(PasteFormats.CustomTextTransformation, Arguments: new() { { PromptParameterName, fixedPrompt } }), async dataPackageView => { - var input = await dataPackageView.GetClipboardTextOrThrowAsync(kernel.GetCancellationToken()); - var result = await _customActionTransformService.TransformTextAsync(fixedPrompt, input, kernel.GetCancellationToken(), kernel.GetProgress()); + var imageBytes = await dataPackageView.GetImageAsPngBytesAsync(); + var input = await dataPackageView.GetTextOrHtmlTextAsync(); + + if (string.IsNullOrEmpty(input) && imageBytes == null) + { + // If we have no text and no image, try to get text via OCR or throw if nothing exists + input = await dataPackageView.GetClipboardTextOrThrowAsync(kernel.GetCancellationToken()); + } + + var result = await _customActionTransformService.TransformAsync(fixedPrompt, input, imageBytes, kernel.GetCancellationToken(), kernel.GetProgress()); return DataPackageHelpers.CreateFromText(result?.Content ?? string.Empty); }); @@ -313,15 +359,22 @@ public abstract class KernelServiceBase( new ActionChainItem(format, Arguments: new() { { PromptParameterName, prompt } }), async dataPackageView => { - var input = await dataPackageView.GetClipboardTextOrThrowAsync(kernel.GetCancellationToken()); - string output = await GetPromptBasedOutput(format, prompt, input, kernel.GetCancellationToken(), kernel.GetProgress()); + var imageBytes = await dataPackageView.GetImageAsPngBytesAsync(); + var input = await dataPackageView.GetTextOrHtmlTextAsync(); + + if (string.IsNullOrEmpty(input) && imageBytes == null) + { + input = await dataPackageView.GetClipboardTextOrThrowAsync(kernel.GetCancellationToken()); + } + + string output = await GetPromptBasedOutput(format, prompt, input, imageBytes, kernel.GetCancellationToken(), kernel.GetProgress()); return DataPackageHelpers.CreateFromText(output); }); - private async Task<string> GetPromptBasedOutput(PasteFormats format, string prompt, string input, CancellationToken cancellationToken, IProgress<double> progress) => + private async Task<string> GetPromptBasedOutput(PasteFormats format, string prompt, string input, byte[] imageBytes, CancellationToken cancellationToken, IProgress<double> progress) => format switch { - PasteFormats.CustomTextTransformation => (await _customActionTransformService.TransformTextAsync(prompt, input, cancellationToken, progress))?.Content ?? string.Empty, + PasteFormats.CustomTextTransformation => (await _customActionTransformService.TransformAsync(prompt, input, imageBytes, cancellationToken, progress))?.Content ?? string.Empty, _ => throw new ArgumentException($"Unsupported format {format} for prompt transform", nameof(format)), }; diff --git a/src/modules/AdvancedPaste/AdvancedPaste/Services/PasteFormatExecutor.cs b/src/modules/AdvancedPaste/AdvancedPaste/Services/PasteFormatExecutor.cs index aef9e39bb9..ff64a5ad83 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/Services/PasteFormatExecutor.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/Services/PasteFormatExecutor.cs @@ -37,7 +37,7 @@ public sealed class PasteFormatExecutor(IKernelService kernelService, ICustomAct pasteFormat.Format switch { PasteFormats.KernelQuery => await _kernelService.TransformClipboardAsync(pasteFormat.Prompt, clipboardData, pasteFormat.IsSavedQuery, cancellationToken, progress), - PasteFormats.CustomTextTransformation => DataPackageHelpers.CreateFromText((await _customActionTransformService.TransformTextAsync(pasteFormat.Prompt, await clipboardData.GetClipboardTextOrThrowAsync(cancellationToken), cancellationToken, progress))?.Content ?? string.Empty), + PasteFormats.CustomTextTransformation => DataPackageHelpers.CreateFromText((await _customActionTransformService.TransformAsync(pasteFormat.Prompt, await clipboardData.GetTextOrHtmlTextAsync(), await clipboardData.GetImageAsPngBytesAsync(), cancellationToken, progress))?.Content ?? string.Empty), _ => await TransformHelpers.TransformAsync(format, clipboardData, cancellationToken, progress), }); } diff --git a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs index 8edd9b76ad..b474b8215a 100644 --- a/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs +++ b/src/modules/AdvancedPaste/AdvancedPaste/ViewModels/OptionsViewModel.cs @@ -45,6 +45,7 @@ namespace AdvancedPaste.ViewModels private CancellationTokenSource _pasteActionCancellationTokenSource; private string _currentClipboardHistoryId; + private uint _lastClipboardSequenceNumber; private DateTimeOffset? _currentClipboardTimestamp; private ClipboardFormat _lastClipboardFormats = ClipboardFormat.None; private bool _clipboardHistoryUnavailableLogged; @@ -455,6 +456,7 @@ namespace AdvancedPaste.ViewModels { ResetClipboardPreview(); _currentClipboardHistoryId = null; + _lastClipboardSequenceNumber = 0; _currentClipboardTimestamp = null; _lastClipboardFormats = ClipboardFormat.None; return; @@ -477,6 +479,13 @@ namespace AdvancedPaste.ViewModels { bool clipboardChanged = formatsChanged; + var currentSequenceNumber = NativeMethods.GetClipboardSequenceNumber(); + if (_lastClipboardSequenceNumber != currentSequenceNumber) + { + clipboardChanged = true; + _lastClipboardSequenceNumber = currentSequenceNumber; + } + if (Clipboard.IsHistoryEnabled()) { try @@ -652,7 +661,7 @@ namespace AdvancedPaste.ViewModels [RelayCommand] public void OpenSettings() { - SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.AdvancedPaste, true); + SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.AdvancedPaste); GetMainWindow()?.Close(); } diff --git a/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp b/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp index c7d22d474f..6cf2e8d9a9 100644 --- a/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp +++ b/src/modules/AdvancedPaste/AdvancedPasteModuleInterface/dllmain.cpp @@ -15,9 +15,11 @@ #include <common/utils/logger_helper.h> #include <common/utils/winapi_error.h> #include <common/utils/gpo.h> +#include <common/utils/EventWaiter.h> #include <algorithm> #include <cwctype> +#include <thread> #include <vector> BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, LPVOID /*lpReserved*/) @@ -101,6 +103,9 @@ private: bool m_is_advanced_ai_enabled = false; bool m_preview_custom_format_output = true; + // Event listening for external triggers (e.g., from CmdPal extension) + EventWaiter m_triggerEventWaiter; + Hotkey parse_single_hotkey(const wchar_t* keyName, const winrt::Windows::Data::Json::JsonObject& settingsObject) { try @@ -312,13 +317,39 @@ private: return false; } - void read_settings(PowerToysSettings::PowerToyValues& settings) + void read_settings(PowerToysSettings::PowerToyValues& settings) { const auto settingsObject = settings.get_raw_json(); // Migrate Paste As Plain text shortcut Hotkey old_paste_as_plain_hotkey; bool old_data_migrated = migrate_data_and_remove_data_file(old_paste_as_plain_hotkey); + + if (settingsObject.GetView().Size()) + { + const auto propertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES); + + m_is_advanced_ai_enabled = has_advanced_ai_provider(propertiesObject); + + if (propertiesObject.HasKey(JSON_KEY_IS_AI_ENABLED)) + { + m_is_ai_enabled = propertiesObject.GetNamedObject(JSON_KEY_IS_AI_ENABLED).GetNamedBoolean(JSON_KEY_VALUE, false); + } + else if (propertiesObject.HasKey(JSON_KEY_IS_OPEN_AI_ENABLED)) + { + m_is_ai_enabled = propertiesObject.GetNamedObject(JSON_KEY_IS_OPEN_AI_ENABLED).GetNamedBoolean(JSON_KEY_VALUE, false); + } + else + { + m_is_ai_enabled = false; + } + + if (propertiesObject.HasKey(JSON_KEY_SHOW_CUSTOM_PREVIEW)) + { + m_preview_custom_format_output = propertiesObject.GetNamedObject(JSON_KEY_SHOW_CUSTOM_PREVIEW).GetNamedBoolean(JSON_KEY_VALUE); + } + } + if (old_data_migrated) { m_paste_as_plain_hotkey = old_paste_as_plain_hotkey; @@ -405,31 +436,6 @@ private: } } } - - if (settingsObject.GetView().Size()) - { - const auto propertiesObject = settingsObject.GetNamedObject(JSON_KEY_PROPERTIES); - - m_is_advanced_ai_enabled = has_advanced_ai_provider(propertiesObject); - - if (propertiesObject.HasKey(JSON_KEY_IS_AI_ENABLED)) - { - m_is_ai_enabled = propertiesObject.GetNamedObject(JSON_KEY_IS_AI_ENABLED).GetNamedBoolean(JSON_KEY_VALUE, false); - } - else if (propertiesObject.HasKey(JSON_KEY_IS_OPEN_AI_ENABLED)) - { - m_is_ai_enabled = propertiesObject.GetNamedObject(JSON_KEY_IS_OPEN_AI_ENABLED).GetNamedBoolean(JSON_KEY_VALUE, false); - } - else - { - m_is_ai_enabled = false; - } - - if (propertiesObject.HasKey(JSON_KEY_SHOW_CUSTOM_PREVIEW)) - { - m_preview_custom_format_output = propertiesObject.GetNamedObject(JSON_KEY_SHOW_CUSTOM_PREVIEW).GetNamedBoolean(JSON_KEY_VALUE); - } - } } // Load the settings file. @@ -778,6 +784,17 @@ public: Trace::AdvancedPaste_Enable(true); m_enabled = true; m_process_manager.start(); + + // Start listening for external trigger event so we can invoke the same logic as the hotkey. + // Note: Use start() directly instead of constructor + move assignment to avoid dangling this pointer in the thread. + m_triggerEventWaiter.start(CommonSharedConstants::ADVANCED_PASTE_SHOW_UI_EVENT, [this](DWORD) { + // Same logic as hotkeyId == 1 (m_advanced_paste_ui_hotkey) + Logger::trace(L"AdvancedPaste ShowUI event triggered"); + m_process_manager.start(); + m_process_manager.bring_to_front(); + m_process_manager.send_message(CommonSharedConstants::ADVANCED_PASTE_SHOW_UI_MESSAGE); + Trace::AdvancedPaste_Invoked(L"AdvancedPasteUIEvent"); + }); }; void Disable(bool traceEvent) @@ -786,6 +803,9 @@ public: { m_process_manager.stop(); + // Stop event listening + m_triggerEventWaiter.stop(); + if (traceEvent) { Trace::AdvancedPaste_Enable(false); diff --git a/src/modules/AdvancedPaste/UITest-AdvancedPaste/AdvancedPasteUITest.cs b/src/modules/AdvancedPaste/UITest-AdvancedPaste/AdvancedPasteUITest.cs index 76ff3580e4..7b62533bdb 100644 --- a/src/modules/AdvancedPaste/UITest-AdvancedPaste/AdvancedPasteUITest.cs +++ b/src/modules/AdvancedPaste/UITest-AdvancedPaste/AdvancedPasteUITest.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Globalization; @@ -11,7 +12,6 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; - using Microsoft.AdvancedPaste.UITests.Helper; using Microsoft.CodeCoverage.Core.Reports.Coverage; using Microsoft.PowerToys.UITest; @@ -55,7 +55,7 @@ namespace Microsoft.AdvancedPaste.UITests } public AdvancedPasteUITest() - : base(PowerToysModule.PowerToysSettings, size: WindowSize.Small) + : base(PowerToysModule.PowerToysSettings, size: WindowSize.Large_Vertical) { Type currentTestType = typeof(AdvancedPasteUITest); string? dirName = Path.GetDirectoryName(currentTestType.Assembly.Location); @@ -254,13 +254,250 @@ namespace Microsoft.AdvancedPaste.UITests /* * Clipboard History - - [] Open Settings and Enable clipboard history (if not enabled already). Open Advanced Paste window with hotkey, click Clipboard history and try deleting some entry. Check OS clipboard history (Win+V), and confirm that the same entry no longer exist. - - [] Open Advanced Paste window with hotkey, click Clipboard history, and click any entry (but first). Observe that entry is put on top of clipboard history. Check OS clipboard history (Win+V), and confirm that the same entry is on top of the clipboard. - - [] Open Settings and Disable clipboard history. Open Advanced Paste window with hotkey and observe that Clipboard history button is disabled. + - [x] Open Settings and Enable clipboard history (if not enabled already). Open Advanced Paste window with hotkey, click Clipboard history and try deleting some entry. Check OS clipboard history (Win+V), and confirm that the same entry no longer exist. + - [x] Open Advanced Paste window with hotkey, click Clipboard history, and click any entry (but first). Observe that entry is put on top of clipboard history. Check OS clipboard history (Win+V), and confirm that the same entry is on top of the clipboard. + - [x] Open Settings and Disable clipboard history. Open Advanced Paste window with hotkey and observe that Clipboard history button is disabled. * Disable Advanced Paste, try different Advanced Paste hotkeys and confirm that it's disabled and nothing happens. */ - private void TestCaseClipboardHistory() + [TestMethod] + [TestCategory("AdvancedPasteUITest")] + [TestCategory("TestCaseClipboardHistoryDeleteTest")] + public void TestCaseClipboardHistoryDeleteTest() { + RestartScopeExe(); + Thread.Sleep(1500); + + // Find the PowerToys Settings window + var settingsWindow = Find<Window>("PowerToys Settings", global: true); + Assert.IsNotNull(settingsWindow, "Failed to open PowerToys Settings window"); + + if (FindAll<NavigationViewItem>("Advanced Paste").Count == 0) + { + // Expand Advanced list-group if needed + Find<NavigationViewItem>("System Tools").Click(); + } + + Find<NavigationViewItem>("Advanced Paste").Click(); + + Find<ToggleSwitch>("Clipboard history").Toggle(true); + + Session.CloseMainWindow(); + + // clear system clipboard + ClearSystemClipboardHistory(); + + // set test content to clipboard + const string textForTesting = "Test text"; + SetClipboardTextInSTAMode(textForTesting); + + // Open Advanced Paste window with hotkey, click Clipboard history and try deleting some entry. + this.SendKeys(Key.Win, Key.Shift, Key.V); + Thread.Sleep(1500); + + var apWind = this.Find<Window>("Advanced Paste", global: true); + apWind.Find<PowerToys.UITest.Button>("Clipboard history").Click(); + + var textGroup = apWind.Find<Group>(textForTesting); + Assert.IsNotNull(textGroup, "Cannot find the test string from advanced paste clipboard history."); + + textGroup.Find<PowerToys.UITest.Button>("More options").Click(); + apWind.Find<TextBlock>("Delete").Click(); + + // Check OS clipboard history (Win+V), and confirm that the same entry no longer exist. + this.SendKeys(Key.Win, Key.V); + + Thread.Sleep(1500); + + var clipboardWindow = this.Find<Window>("Windows Input Experience", global: true); + Assert.IsNotNull(clipboardWindow, "Cannot find system clipboard window."); + + var nothingText = clipboardWindow.Find<Group>("Nothing here, You'll see your clipboard history here once you've copied something."); + Assert.IsNotNull(nothingText, "System clipboard is not empty, which should be yes."); + } + + [TestMethod] + [TestCategory("AdvancedPasteUITest")] + [TestCategory("TestCaseClipboardHistorySelectTest")] + public void TestCaseClipboardHistorySelectTest() + { + RestartScopeExe(); + Thread.Sleep(1500); + + // Find the PowerToys Settings window + var settingsWindow = Find<Window>("PowerToys Settings", global: true); + Assert.IsNotNull(settingsWindow, "Failed to open PowerToys Settings window"); + + if (FindAll<NavigationViewItem>("Advanced Paste").Count == 0) + { + // Expand Advanced list-group if needed + Find<NavigationViewItem>("System Tools").Click(); + } + + Find<NavigationViewItem>("Advanced Paste").Click(); + + Find<ToggleSwitch>("Clipboard history").Toggle(true); + + Session.CloseMainWindow(); + + // clear system clipboard + ClearSystemClipboardHistory(); + + // set test content to clipboard + string[] textForTesting = { "Test text1", "Test text2", "Test text3", "Test text4", "Test text5", "Test text6", }; + foreach (var str in textForTesting) + { + SetClipboardTextInSTAMode(str); + Thread.Sleep(1000); + } + + // Open Advanced Paste window with hotkey + this.SendKeys(Key.Win, Key.Shift, Key.V); + Thread.Sleep(1500); + + var apWind = this.Find<Window>("Advanced Paste", global: true); + apWind.Find<PowerToys.UITest.Button>("Clipboard history").Click(); + + // click the 3rd item + var textGroup = apWind.Find<Group>(textForTesting[0]); + Assert.IsNotNull(textGroup, "Cannot find the test string from advanced paste clipboard history."); + textGroup.Click(); + + // Check OS clipboard history (Win+V) + this.SendKeys(Key.Win, Key.V); + + Thread.Sleep(1500); + + var clipboardWindow = this.Find<Window>("Windows Input Experience", global: true); + Assert.IsNotNull(clipboardWindow, "Cannot find system clipboard window."); + + var txtFound = clipboardWindow.Find<Element>(textForTesting[0]); + Assert.IsNotNull(txtFound, "Cannot find textblock"); + } + + // [x] Open Settings and Disable clipboard history.Open Advanced Paste window with hotkey and observe that Clipboard history button is disabled. + [TestMethod] + [TestCategory("AdvancedPasteUITest")] + [TestCategory("TestCaseClipboardHistoryDisableTest")] + public void TestCaseClipboardHistoryDisableTest() + { + RestartScopeExe(); + Thread.Sleep(1500); + + // Find the PowerToys Settings window + var settingsWindow = Find<Window>("PowerToys Settings", global: true); + Assert.IsNotNull(settingsWindow, "Failed to open PowerToys Settings window"); + + if (FindAll<NavigationViewItem>("Advanced Paste").Count == 0) + { + // Expand Advanced list-group if needed + Find<NavigationViewItem>("System Tools").Click(); + } + + Find<NavigationViewItem>("Advanced Paste").Click(); + + Find<ToggleSwitch>("Clipboard history").Toggle(false); + + Session.CloseMainWindow(); + + // set test content to clipboard + const string textForTesting = "Test text"; + SetClipboardTextInSTAMode(textForTesting); + + // Open Advanced Paste window with hotkey, click Clipboard history and try deleting some entry. + this.SendKeys(Key.Win, Key.Shift, Key.V); + Thread.Sleep(1500); + + var apWind = this.Find<Window>("Advanced Paste", global: true); + + // Click the button (which should still exist but be disabled) + apWind.Find<PowerToys.UITest.Button>("Clipboard history").Click(); + + // Verify that the clipboard content doesn't appear + // Use a short timeout to avoid a long wait when the element doesn't exist + Assert.IsFalse( + Has<Group>(textForTesting), + "Clipboard content should not appear when clipboard history is disabled"); + } + + // Disable Advanced Paste, try different Advanced Paste hotkeys and confirm that it's disabled and nothing happens. + [TestMethod] + [TestCategory("AdvancedPasteUITest")] + [TestCategory("TestCaseDisableAdvancedPaste")] + public void TestCaseDisableAdvancedPaste() + { + RestartScopeExe(); + Thread.Sleep(1500); + + // Find the PowerToys Settings window + var settingsWindow = Find<Window>("PowerToys Settings", global: true); + Assert.IsNotNull(settingsWindow, "Failed to open PowerToys Settings window"); + + if (FindAll<NavigationViewItem>("Advanced Paste").Count == 0) + { + // Expand System Tools if needed + Find<NavigationViewItem>("System Tools").Click(); + } + + Find<NavigationViewItem>("Advanced Paste").Click(); + + // Disable Advanced Paste module + var moduleToggle = Find<ToggleSwitch>("Enable Advanced Paste"); + moduleToggle.Toggle(false); + + Session.CloseMainWindow(); + + // Prepare some text to test with + const string textForTesting = "Test text for disabled module"; + SetClipboardTextInSTAMode(textForTesting); + + // Try main Advanced Paste hotkey + this.SendKeys(Key.Win, Key.Shift, Key.V); + Thread.Sleep(500); + + // Verify Advanced Paste window does not appear + Assert.IsFalse( + Has<Window>("Advanced Paste", global: true), + "Advanced Paste window should not appear when the module is disabled"); + + // Re-enable Advanced Paste for other tests + RestartScopeExe(); + Thread.Sleep(1500); + + settingsWindow = Find<Window>("PowerToys Settings", global: true); + + if (FindAll<NavigationViewItem>("Advanced Paste").Count == 0) + { + Find<NavigationViewItem>("System Tools").Click(); + } + + Find<NavigationViewItem>("Advanced Paste").Click(); + Find<ToggleSwitch>("Enable Advanced Paste").Toggle(true); + + Session.CloseMainWindow(); + } + + private void ClearSystemClipboardHistory() + { + this.SendKeys(Key.Win, Key.V); + + Thread.Sleep(1500); + + var clipboardWindow = this.Find<Window>("Windows Input Experience", global: true); + Assert.IsNotNull(clipboardWindow, "Cannot find system clipboard window."); + + clipboardWindow.Find<PowerToys.UITest.Button>("Clear all except pinned items").Click(); + } + + private void SetClipboardTextInSTAMode(string text) + { + var thread = new Thread(() => + { + System.Windows.Forms.Clipboard.SetText(text); + }); + + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + thread.Join(); } private void ContentCopyAndPasteDirectly(string fileName, bool isRTF = false) diff --git a/src/modules/EnvironmentVariables/EnvironmentVariablesModuleInterface/dllmain.cpp b/src/modules/EnvironmentVariables/EnvironmentVariablesModuleInterface/dllmain.cpp index a4158d1c66..89db922ddd 100644 --- a/src/modules/EnvironmentVariables/EnvironmentVariablesModuleInterface/dllmain.cpp +++ b/src/modules/EnvironmentVariables/EnvironmentVariablesModuleInterface/dllmain.cpp @@ -146,7 +146,7 @@ public: } } - m_showEventWaiter = EventWaiter(CommonSharedConstants::SHOW_ENVIRONMENT_VARIABLES_EVENT, [&](int err) { + m_showEventWaiter.start(CommonSharedConstants::SHOW_ENVIRONMENT_VARIABLES_EVENT, [&](DWORD err) { if (m_enabled && err == ERROR_SUCCESS) { Logger::trace(L"{} event was signaled", CommonSharedConstants::SHOW_ENVIRONMENT_VARIABLES_EVENT); @@ -164,7 +164,7 @@ public: } }); - m_showAdminEventWaiter = EventWaiter(CommonSharedConstants::SHOW_ENVIRONMENT_VARIABLES_ADMIN_EVENT, [&](int err) { + m_showAdminEventWaiter.start(CommonSharedConstants::SHOW_ENVIRONMENT_VARIABLES_ADMIN_EVENT, [&](DWORD err) { if (m_enabled && err == ERROR_SUCCESS) { Logger::trace(L"{} event was signaled", CommonSharedConstants::SHOW_ENVIRONMENT_VARIABLES_ADMIN_EVENT); diff --git a/src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs b/src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs index 8dbec70de8..0b2739ebe1 100644 --- a/src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs +++ b/src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs @@ -67,7 +67,7 @@ namespace Hosts services.AddSingleton<IElevationHelper, ElevationHelper>(); services.AddSingleton<OpenSettingsFunction>(() => { - SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.Hosts, true); + SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.Hosts); }); services.AddSingleton<MainViewModel, MainViewModel>(); diff --git a/src/modules/Hosts/HostsModuleInterface/dllmain.cpp b/src/modules/Hosts/HostsModuleInterface/dllmain.cpp index 993226ac2b..fb8dd40011 100644 --- a/src/modules/Hosts/HostsModuleInterface/dllmain.cpp +++ b/src/modules/Hosts/HostsModuleInterface/dllmain.cpp @@ -155,7 +155,7 @@ public: } } - m_showEventWaiter = EventWaiter(CommonSharedConstants::SHOW_HOSTS_EVENT, [&](int err) + m_showEventWaiter.start(CommonSharedConstants::SHOW_HOSTS_EVENT, [&](DWORD err) { if (m_enabled && err == ERROR_SUCCESS) { @@ -174,7 +174,7 @@ public: } }); - m_showAdminEventWaiter = EventWaiter(CommonSharedConstants::SHOW_HOSTS_ADMIN_EVENT, [&](int err) + m_showAdminEventWaiter.start(CommonSharedConstants::SHOW_HOSTS_ADMIN_EVENT, [&](DWORD err) { if (m_enabled && err == ERROR_SUCCESS) { diff --git a/src/modules/LightSwitch/LightSwitchModuleInterface/LightSwitchModuleInterface.vcxproj b/src/modules/LightSwitch/LightSwitchModuleInterface/LightSwitchModuleInterface.vcxproj index 261cfab1e6..b86b25a4d1 100644 --- a/src/modules/LightSwitch/LightSwitchModuleInterface/LightSwitchModuleInterface.vcxproj +++ b/src/modules/LightSwitch/LightSwitchModuleInterface/LightSwitchModuleInterface.vcxproj @@ -168,9 +168,6 @@ <ClCompile> <AdditionalIncludeDirectories>..\..\..\common\inc;..\..\..\common\Telemetry;..\..\;..\..\..\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> </ClCompile> - <Link> - <AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">$(CoreLibraryDependencies);%(AdditionalDependencies);advapi32.lib</AdditionalDependencies> - </Link> </ItemDefinitionGroup> <ItemGroup> <ClInclude Include="pch.h" /> @@ -222,4 +219,4 @@ <Error Condition="!Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.props'))" /> <Error Condition="!Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets'))" /> </Target> -</Project> \ No newline at end of file +</Project> diff --git a/src/modules/LightSwitch/LightSwitchModuleInterface/dllmain.cpp b/src/modules/LightSwitch/LightSwitchModuleInterface/dllmain.cpp index a5973a396f..11cfd412b0 100644 --- a/src/modules/LightSwitch/LightSwitchModuleInterface/dllmain.cpp +++ b/src/modules/LightSwitch/LightSwitchModuleInterface/dllmain.cpp @@ -8,6 +8,8 @@ #include <codecvt> #include <common/utils/logger_helper.h> #include "ThemeHelper.h" +#include <thread> +#include <atomic> extern "C" IMAGE_DOS_HEADER __ImageBase; @@ -103,12 +105,18 @@ private: HANDLE m_force_light_event_handle; HANDLE m_force_dark_event_handle; HANDLE m_manual_override_event_handle; + HANDLE m_toggle_event_handle{ nullptr }; + std::thread m_toggle_thread; + std::atomic<bool> m_toggle_thread_running{ false }; static const constexpr int NUM_DEFAULT_HOTKEYS = 4; Hotkey m_toggle_theme_hotkey = { .win = true, .ctrl = true, .shift = true, .alt = false, .key = 'D' }; void init_settings(); + void ToggleTheme(); + void StartToggleListener(); + void StopToggleListener(); public: LightSwitchInterface() @@ -118,6 +126,7 @@ public: m_force_light_event_handle = CreateDefaultEvent(L"POWERTOYS_LIGHTSWITCH_FORCE_LIGHT"); m_force_dark_event_handle = CreateDefaultEvent(L"POWERTOYS_LIGHTSWITCH_FORCE_DARK"); m_manual_override_event_handle = CreateEventW(nullptr, TRUE, FALSE, L"POWERTOYS_LIGHTSWITCH_MANUAL_OVERRIDE"); + m_toggle_event_handle = CreateDefaultEvent(L"Local\\PowerToys-LightSwitch-ToggleEvent-d8dc2f29-8c94-4ca1-8c5f-3e2b1e3c4f5a"); init_settings(); }; @@ -130,6 +139,8 @@ public: // Destroy the powertoy and free memory virtual void destroy() override { + // Ensure worker threads/process handles are cleaned up before destruction + disable(); delete this; } @@ -444,6 +455,8 @@ public: Logger::info(L"Light Switch process launched successfully (PID: {}).", pi.dwProcessId); m_process = pi.hProcess; CloseHandle(pi.hThread); + + StartToggleListener(); } // Disable the powertoy @@ -469,6 +482,8 @@ public: CloseHandle(m_process); m_process = nullptr; } + + StopToggleListener(); } // Returns if the powertoys is enabled @@ -530,31 +545,8 @@ public: } else if (hotkeyId == 0) { - // get current will return true if in light mode; otherwise false Logger::info(L"[Light Switch] Hotkey triggered: Toggle Theme"); - if (g_settings.m_changeSystem) - { - SetSystemTheme(!GetCurrentSystemTheme()); - } - if (g_settings.m_changeApps) - { - SetAppsTheme(!GetCurrentAppsTheme()); - } - - if (!m_manual_override_event_handle) - { - m_manual_override_event_handle = OpenEventW(SYNCHRONIZE | EVENT_MODIFY_STATE, FALSE, L"POWERTOYS_LIGHTSWITCH_MANUAL_OVERRIDE"); - if (!m_manual_override_event_handle) - { - m_manual_override_event_handle = CreateEventW(nullptr, TRUE, FALSE, L"POWERTOYS_LIGHTSWITCH_MANUAL_OVERRIDE"); - } - } - - if (m_manual_override_event_handle) - { - SetEvent(m_manual_override_event_handle); - Logger::debug(L"[Light Switch] Manual override event set"); - } + ToggleTheme(); } return true; @@ -567,8 +559,80 @@ public: { return WaitForSingleObject(m_process, 0) == WAIT_TIMEOUT; } + }; +void LightSwitchInterface::ToggleTheme() +{ + if (g_settings.m_changeSystem) + { + SetSystemTheme(!GetCurrentSystemTheme()); + } + if (g_settings.m_changeApps) + { + SetAppsTheme(!GetCurrentAppsTheme()); + } + + if (!m_manual_override_event_handle) + { + m_manual_override_event_handle = OpenEventW(SYNCHRONIZE | EVENT_MODIFY_STATE, FALSE, L"POWERTOYS_LIGHTSWITCH_MANUAL_OVERRIDE"); + if (!m_manual_override_event_handle) + { + m_manual_override_event_handle = CreateEventW(nullptr, TRUE, FALSE, L"POWERTOYS_LIGHTSWITCH_MANUAL_OVERRIDE"); + } + } + + if (m_manual_override_event_handle) + { + SetEvent(m_manual_override_event_handle); + Logger::debug(L"[Light Switch] Manual override event set"); + } +} + +void LightSwitchInterface::StartToggleListener() +{ + if (m_toggle_thread_running || !m_toggle_event_handle) + { + return; + } + + m_toggle_thread_running = true; + m_toggle_thread = std::thread([this]() { + while (m_toggle_thread_running) + { + const DWORD wait_result = WaitForSingleObject(m_toggle_event_handle, 500); + if (!m_toggle_thread_running) + { + break; + } + + if (wait_result == WAIT_OBJECT_0) + { + ToggleTheme(); + ResetEvent(m_toggle_event_handle); + } + } + }); +} + +void LightSwitchInterface::StopToggleListener() +{ + if (!m_toggle_thread_running) + { + return; + } + + m_toggle_thread_running = false; + if (m_toggle_event_handle) + { + SetEvent(m_toggle_event_handle); + } + if (m_toggle_thread.joinable()) + { + m_toggle_thread.join(); + } +} + std::wstring utf8_to_wstring(const std::string& str) { if (str.empty()) @@ -646,4 +710,4 @@ void LightSwitchInterface::init_settings() extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() { return new LightSwitchInterface(); -} \ No newline at end of file +} diff --git a/src/modules/MeasureTool/MeasureToolModuleInterface/dllmain.cpp b/src/modules/MeasureTool/MeasureToolModuleInterface/dllmain.cpp index cfac5ce640..0c31e7f9ef 100644 --- a/src/modules/MeasureTool/MeasureToolModuleInterface/dllmain.cpp +++ b/src/modules/MeasureTool/MeasureToolModuleInterface/dllmain.cpp @@ -149,7 +149,7 @@ public: init_settings(); triggerEvent = CreateEvent(nullptr, false, false, CommonSharedConstants::MEASURE_TOOL_TRIGGER_EVENT); - triggerEventWaiter = EventWaiter(CommonSharedConstants::MEASURE_TOOL_TRIGGER_EVENT, [this](int) { + triggerEventWaiter.start(CommonSharedConstants::MEASURE_TOOL_TRIGGER_EVENT, [this](DWORD) { on_hotkey(0); }); } diff --git a/src/modules/MouseUtils/CursorWrap/dllmain.cpp b/src/modules/MouseUtils/CursorWrap/dllmain.cpp index 74524ed9f9..09342d3a88 100644 --- a/src/modules/MouseUtils/CursorWrap/dllmain.cpp +++ b/src/modules/MouseUtils/CursorWrap/dllmain.cpp @@ -6,6 +6,7 @@ #include "../../../common/utils/resources.h" #include "../../../common/logger/logger.h" #include "../../../common/utils/logger_helper.h" +#include "../../../common/interop/shared_constants.h" #include <atomic> #include <thread> #include <vector> @@ -108,6 +109,12 @@ private: // Hotkey Hotkey m_activationHotkey{}; + // Event-driven trigger support (for CmdPal/automation) + HANDLE m_triggerEventHandle = nullptr; + HANDLE m_terminateEventHandle = nullptr; + std::thread m_eventThread; + std::atomic_bool m_listening{ false }; + public: // Constructor CursorWrap() @@ -121,7 +128,8 @@ public: // Destroy the powertoy and free memory virtual void destroy() override { - StopMouseHook(); + // Ensure hooks/threads/handles are torn down before deletion + disable(); g_cursorWrapInstance = nullptr; // Clear global instance pointer delete this; } @@ -195,11 +203,54 @@ public: { m_enabled = true; Trace::EnableCursorWrap(true); - - // Always start the mouse hook when the module is enabled - // This ensures cursor wrapping is active immediately after enabling - StartMouseHook(); - Logger::info("CursorWrap enabled - mouse hook started"); + + // Start listening for external trigger event so we can invoke the same logic as the activation hotkey. + m_triggerEventHandle = CreateEventW(nullptr, false, false, CommonSharedConstants::CURSOR_WRAP_TRIGGER_EVENT); + m_terminateEventHandle = CreateEventW(nullptr, false, false, nullptr); + if (m_triggerEventHandle && m_terminateEventHandle) + { + m_listening = true; + m_eventThread = std::thread([this]() { + HANDLE handles[2] = { m_triggerEventHandle, m_terminateEventHandle }; + + // WH_MOUSE_LL callbacks are delivered to the thread that installed the hook. + // Ensure this thread has a message queue and pumps messages while the hook is active. + MSG msg; + PeekMessage(&msg, nullptr, WM_USER, WM_USER, PM_NOREMOVE); + + StartMouseHook(); + Logger::info("CursorWrap enabled - mouse hook started"); + + while (m_listening) + { + auto res = MsgWaitForMultipleObjects(2, handles, false, INFINITE, QS_ALLINPUT); + if (!m_listening) + { + break; + } + + if (res == WAIT_OBJECT_0) + { + ToggleMouseHook(); + } + else if (res == WAIT_OBJECT_0 + 1) + { + break; + } + else + { + while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + } + + StopMouseHook(); + Logger::info("CursorWrap event listener stopped"); + }); + } } // Disable the powertoy @@ -207,8 +258,26 @@ public: { m_enabled = false; Trace::EnableCursorWrap(false); - StopMouseHook(); - Logger::info("CursorWrap disabled - mouse hook stopped"); + + m_listening = false; + if (m_terminateEventHandle) + { + SetEvent(m_terminateEventHandle); + } + if (m_eventThread.joinable()) + { + m_eventThread.join(); + } + if (m_triggerEventHandle) + { + CloseHandle(m_triggerEventHandle); + m_triggerEventHandle = nullptr; + } + if (m_terminateEventHandle) + { + CloseHandle(m_terminateEventHandle); + m_terminateEventHandle = nullptr; + } } // Returns if the powertoys is enabled @@ -240,7 +309,19 @@ public: return false; } - // Toggle cursor wrapping + // Toggle on the thread that owns the WH_MOUSE_LL hook (the event listener thread). + if (m_triggerEventHandle) + { + return SetEvent(m_triggerEventHandle); + } + + return false; + } + + private: + void ToggleMouseHook() + { + // Toggle cursor wrapping. if (m_hookActive) { StopMouseHook(); @@ -253,11 +334,8 @@ public: RunComprehensiveTests(); #endif } - - return true; } -private: // Load the settings file. void init_settings() { diff --git a/src/modules/MouseUtils/FindMyMouse/dllmain.cpp b/src/modules/MouseUtils/FindMyMouse/dllmain.cpp index b7ffb6177a..af99f45136 100644 --- a/src/modules/MouseUtils/FindMyMouse/dllmain.cpp +++ b/src/modules/MouseUtils/FindMyMouse/dllmain.cpp @@ -8,6 +8,8 @@ #include <common/utils/logger_helper.h> #include <common/utils/color.h> #include <common/utils/string_utils.h> +#include <common/utils/EventWaiter.h> +#include <common/interop/shared_constants.h> namespace { @@ -69,6 +71,9 @@ private: // Find My Mouse specific settings FindMyMouseSettings m_findMyMouseSettings; + // Event-driven trigger support + EventWaiter m_triggerEventWaiter; + // Load initial settings from the persisted values. void init_settings(); @@ -86,6 +91,8 @@ public: // Destroy the powertoy and free memory virtual void destroy() override { + // Ensure threads/handles are cleaned up before destruction + disable(); delete this; } @@ -150,6 +157,11 @@ public: m_enabled = true; Trace::EnableFindMyMouse(true); std::thread([=]() { FindMyMouseMain(m_hModule, m_findMyMouseSettings); }).detach(); + + // Start listening for external trigger event so we can invoke the same logic as the hotkey. + m_triggerEventWaiter.start(CommonSharedConstants::FIND_MY_MOUSE_TRIGGER_EVENT, [this](DWORD) { + OnHotkeyEx(); + }); } // Disable the powertoy @@ -158,6 +170,8 @@ public: m_enabled = false; Trace::EnableFindMyMouse(false); FindMyMouseDisable(); + + m_triggerEventWaiter.stop(); } // Returns if the powertoys is enabled @@ -216,7 +230,7 @@ inline static uint8_t LegacyOpacityToAlpha(int overlayOpacityPercent) overlayOpacityPercent = 100; } - // Round to nearest integer (0255) + // Round to nearest integer (0–255) return static_cast<uint8_t>((overlayOpacityPercent * 255 + 50) / 100); } @@ -532,4 +546,4 @@ void FindMyMouse::parse_settings(PowerToysSettings::PowerToyValues& settings) extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() { return new FindMyMouse(); -} \ No newline at end of file +} diff --git a/src/modules/MouseUtils/MouseHighlighter/dllmain.cpp b/src/modules/MouseUtils/MouseHighlighter/dllmain.cpp index 45c62ae9ca..83a8837409 100644 --- a/src/modules/MouseUtils/MouseHighlighter/dllmain.cpp +++ b/src/modules/MouseUtils/MouseHighlighter/dllmain.cpp @@ -4,6 +4,8 @@ #include "trace.h" #include "MouseHighlighter.h" #include "common/utils/color.h" +#include <common/utils/EventWaiter.h> +#include <common/interop/shared_constants.h> namespace { @@ -61,6 +63,9 @@ private: // Mouse Highlighter specific settings MouseHighlighterSettings m_highlightSettings; + // Event-driven trigger support + EventWaiter m_triggerEventWaiter; + public: // Constructor MouseHighlighter() @@ -72,6 +77,8 @@ public: // Destroy the powertoy and free memory virtual void destroy() override { + // Tear down threads/handles before deletion to avoid abort() on joinable threads during shutdown + disable(); delete this; } @@ -132,6 +139,11 @@ public: m_enabled = true; Trace::EnableMouseHighlighter(true); std::thread([=]() { MouseHighlighterMain(m_hModule, m_highlightSettings); }).detach(); + + // Start listening for external trigger event so we can invoke the same logic as the hotkey. + m_triggerEventWaiter.start(CommonSharedConstants::MOUSE_HIGHLIGHTER_TRIGGER_EVENT, [this](DWORD) { + OnHotkeyEx(); + }); } // Disable the powertoy @@ -140,6 +152,8 @@ public: m_enabled = false; Trace::EnableMouseHighlighter(false); MouseHighlighterDisable(); + + m_triggerEventWaiter.stop(); } // Returns if the powertoys is enabled diff --git a/src/modules/MouseUtils/MousePointerCrosshairs/dllmain.cpp b/src/modules/MouseUtils/MousePointerCrosshairs/dllmain.cpp index b460e29643..5697d83d30 100644 --- a/src/modules/MouseUtils/MousePointerCrosshairs/dllmain.cpp +++ b/src/modules/MouseUtils/MousePointerCrosshairs/dllmain.cpp @@ -4,7 +4,8 @@ #include "trace.h" #include "InclusiveCrosshairs.h" #include "common/utils/color.h" -#include <atomic> +#include <common/utils/EventWaiter.h> +#include <common/interop/shared_constants.h> #include <thread> #include <chrono> #include <memory> @@ -124,6 +125,9 @@ private: // Mouse Pointer Crosshairs specific settings InclusiveCrosshairsSettings m_inclusiveCrosshairsSettings; + // Event-driven trigger support + EventWaiter m_triggerEventWaiter; + public: // Constructor MousePointerCrosshairs() @@ -137,11 +141,9 @@ public: // Destroy the powertoy and free memory virtual void destroy() override { - UninstallKeyboardHook(); - StopXTimer(); - StopYTimer(); + // Ensure all background threads/handles are torn down before destruction to avoid std::terminate/abort on joinable threads + disable(); g_instance.store(nullptr, std::memory_order_release); - // Release shared state so worker threads (if any) exit when weak_ptr lock fails m_state.reset(); delete this; } @@ -203,6 +205,11 @@ public: m_enabled = true; Trace::EnableMousePointerCrosshairs(true); std::thread([=]() { InclusiveCrosshairsMain(m_hModule, m_inclusiveCrosshairsSettings); }).detach(); + + // Start listening for external trigger event so we can invoke the same logic as the activation hotkey. + m_triggerEventWaiter.start(CommonSharedConstants::MOUSE_CROSSHAIRS_TRIGGER_EVENT, [this](DWORD) { + on_hotkey(0); // activation hotkey + }); } // Disable the powertoy @@ -215,6 +222,8 @@ public: StopYTimer(); m_glideState = 0; InclusiveCrosshairsDisable(); + + m_triggerEventWaiter.stop(); } // Returns if the powertoys is enabled @@ -901,4 +910,4 @@ private: extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create() { return new MousePointerCrosshairs(); -} \ No newline at end of file +} diff --git a/src/modules/PowerOCR/PowerOCR/OCROverlay.xaml.cs b/src/modules/PowerOCR/PowerOCR/OCROverlay.xaml.cs index 2664b8f03b..88219a4110 100644 --- a/src/modules/PowerOCR/PowerOCR/OCROverlay.xaml.cs +++ b/src/modules/PowerOCR/PowerOCR/OCROverlay.xaml.cs @@ -426,7 +426,7 @@ public partial class OCROverlay : Window private void SettingsMenuItem_Click(object sender, RoutedEventArgs e) { WindowUtilities.CloseAllOCROverlays(); - SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.PowerOCR, false); + SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.PowerOCR); } private static bool CheckIfCheckingOrUnchecking(object? sender) diff --git a/src/modules/ShortcutGuide/ShortcutGuide/main.cpp b/src/modules/ShortcutGuide/ShortcutGuide/main.cpp index 713446403b..57a4491d41 100644 --- a/src/modules/ShortcutGuide/ShortcutGuide/main.cpp +++ b/src/modules/ShortcutGuide/ShortcutGuide/main.cpp @@ -121,7 +121,7 @@ int WINAPI wWinMain(_In_ HINSTANCE /*hInstance*/, _In_opt_ HINSTANCE /*hPrevInst else { auto mainThreadId = GetCurrentThreadId(); - exitEventWaiter = EventWaiter(CommonSharedConstants::SHORTCUT_GUIDE_EXIT_EVENT, [mainThreadId, &window](int err) { + exitEventWaiter.start(CommonSharedConstants::SHORTCUT_GUIDE_EXIT_EVENT, [mainThreadId, &window](DWORD err) { if (err != ERROR_SUCCESS) { Logger::error(L"Failed to wait for {} event. {}", CommonSharedConstants::SHORTCUT_GUIDE_EXIT_EVENT, get_last_error_or_default(err)); diff --git a/src/modules/ShortcutGuide/ShortcutGuideModuleInterface/dllmain.cpp b/src/modules/ShortcutGuide/ShortcutGuideModuleInterface/dllmain.cpp index 5e8fe9aa1b..a870fb9ad8 100644 --- a/src/modules/ShortcutGuide/ShortcutGuideModuleInterface/dllmain.cpp +++ b/src/modules/ShortcutGuide/ShortcutGuideModuleInterface/dllmain.cpp @@ -37,7 +37,7 @@ public: } triggerEvent = CreateEvent(nullptr, false, false, CommonSharedConstants::SHORTCUT_GUIDE_TRIGGER_EVENT); - triggerEventWaiter = EventWaiter(CommonSharedConstants::SHORTCUT_GUIDE_TRIGGER_EVENT, [this](int) { + triggerEventWaiter.start(CommonSharedConstants::SHORTCUT_GUIDE_TRIGGER_EVENT, [this](DWORD) { OnHotkeyEx(); }); diff --git a/src/modules/Workspaces/Workspaces.ModuleServices/IWorkspaceService.cs b/src/modules/Workspaces/Workspaces.ModuleServices/IWorkspaceService.cs new file mode 100644 index 0000000000..00300fea9f --- /dev/null +++ b/src/modules/Workspaces/Workspaces.ModuleServices/IWorkspaceService.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using PowerToys.ModuleContracts; +using WorkspacesCsharpLibrary.Data; + +namespace Workspaces.ModuleServices; + +/// <summary> +/// Workspaces-specific operations. +/// </summary> +public interface IWorkspaceService : IModuleService +{ + Task<OperationResult> LaunchWorkspaceAsync(string workspaceId, CancellationToken cancellationToken = default); + + Task<OperationResult> LaunchEditorAsync(CancellationToken cancellationToken = default); + + Task<OperationResult> SnapshotAsync(string? targetPath = null, CancellationToken cancellationToken = default); + + Task<OperationResult<IReadOnlyList<ProjectWrapper>>> GetWorkspacesAsync(CancellationToken cancellationToken = default); +} diff --git a/src/modules/Workspaces/Workspaces.ModuleServices/WorkspaceService.cs b/src/modules/Workspaces/Workspaces.ModuleServices/WorkspaceService.cs new file mode 100644 index 0000000000..eb916e24df --- /dev/null +++ b/src/modules/Workspaces/Workspaces.ModuleServices/WorkspaceService.cs @@ -0,0 +1,96 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; +using System.IO; +using Common.UI; +using ManagedCommon; +using PowerToys.Interop; +using PowerToys.ModuleContracts; +using WorkspacesCsharpLibrary.Data; + +namespace Workspaces.ModuleServices; + +/// <summary> +/// Implementation of workspace actions for reuse across hosts. +/// </summary> +public sealed class WorkspaceService : ModuleServiceBase, IWorkspaceService +{ + public static WorkspaceService Instance { get; } = new(); + + public override string Key => SettingsDeepLink.SettingsWindow.Workspaces.ToString(); + + protected override SettingsDeepLink.SettingsWindow SettingsWindow => SettingsDeepLink.SettingsWindow.Workspaces; + + public override Task<OperationResult> LaunchAsync(CancellationToken cancellationToken = default) + { + // Treat launch as invoking the Workspaces editor. + return LaunchEditorAsync(cancellationToken); + } + + public Task<OperationResult> LaunchEditorAsync(CancellationToken cancellationToken = default) + { + try + { + using var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.WorkspacesLaunchEditorEvent()); + eventHandle.Set(); + return Task.FromResult(OperationResult.Ok()); + } + catch (Exception ex) + { + return Task.FromResult(OperationResult.Fail($"Failed to launch Workspaces editor: {ex.Message}")); + } + } + + public Task<OperationResult> LaunchWorkspaceAsync(string workspaceId, CancellationToken cancellationToken = default) + { + if (string.IsNullOrWhiteSpace(workspaceId)) + { + return Task.FromResult(OperationResult.Fail("Workspace id is required.")); + } + + try + { + var powertoysBaseDir = PowerToysPathResolver.GetPowerToysInstallPath(); + if (string.IsNullOrEmpty(powertoysBaseDir)) + { + return Task.FromResult(OperationResult.Fail("PowerToys installation path not found.")); + } + + var launcherPath = Path.Combine(powertoysBaseDir, "PowerToys.WorkspacesLauncher.exe"); + var startInfo = new ProcessStartInfo(launcherPath) + { + Arguments = workspaceId, + UseShellExecute = true, + }; + + Process.Start(startInfo); + return Task.FromResult(OperationResult.Ok()); + } + catch (Exception ex) + { + return Task.FromResult(OperationResult.Fail($"Failed to launch workspace: {ex.Message}")); + } + } + + public Task<OperationResult> SnapshotAsync(string? targetPath = null, CancellationToken cancellationToken = default) + { + // Snapshot orchestration is not yet exposed via events; provide a clear failure for now. + return Task.FromResult(OperationResult.Fail("Snapshot is not implemented for Workspaces.")); + } + + public Task<OperationResult<IReadOnlyList<ProjectWrapper>>> GetWorkspacesAsync(CancellationToken cancellationToken = default) + { + try + { + var items = WorkspacesStorage.Load(); + + return Task.FromResult(OperationResults.Ok<IReadOnlyList<ProjectWrapper>>(items)); + } + catch (Exception ex) + { + return Task.FromResult(OperationResults.Fail<IReadOnlyList<ProjectWrapper>>($"Failed to read workspaces: {ex.Message}")); + } + } +} diff --git a/src/modules/Workspaces/Workspaces.ModuleServices/Workspaces.ModuleServices.csproj b/src/modules/Workspaces/Workspaces.ModuleServices/Workspaces.ModuleServices.csproj new file mode 100644 index 0000000000..f835138a27 --- /dev/null +++ b/src/modules/Workspaces/Workspaces.ModuleServices/Workspaces.ModuleServices.csproj @@ -0,0 +1,20 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <!-- Look at Directory.Build.props in root for common stuff as well --> + <Import Project="..\..\..\Common.Dotnet.CsWinRT.props" /> + <Import Project="..\..\..\Common.Dotnet.AotCompatibility.props" /> + + <PropertyGroup> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> + <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\..\..\common\ManagedCommon\ManagedCommon.csproj" /> + <ProjectReference Include="..\..\..\common\interop\PowerToys.Interop.vcxproj" /> + <ProjectReference Include="..\..\..\common\Common.UI\Common.UI.csproj" /> + <ProjectReference Include="..\..\..\common\PowerToys.ModuleContracts\PowerToys.ModuleContracts.csproj" /> + <ProjectReference Include="..\WorkspacesCsharpLibrary\WorkspacesCsharpLibrary.csproj" /> + </ItemGroup> +</Project> diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/ApplicationWrapper.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/ApplicationWrapper.cs new file mode 100644 index 0000000000..5bf07b9fc1 --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/ApplicationWrapper.cs @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Text.Json.Serialization; + +namespace WorkspacesCsharpLibrary.Data; + +public struct ApplicationWrapper +{ + public struct WindowPositionWrapper + { + [JsonPropertyName("x")] + public int X { get; set; } + + [JsonPropertyName("y")] + public int Y { get; set; } + + [JsonPropertyName("width")] + public int Width { get; set; } + + [JsonPropertyName("height")] + public int Height { get; set; } + } + + [JsonPropertyName("id")] + public string Id { get; set; } + + [JsonPropertyName("application")] + public string Application { get; set; } + + [JsonPropertyName("application-path")] + public string ApplicationPath { get; set; } + + [JsonPropertyName("title")] + public string Title { get; set; } + + [JsonPropertyName("package-full-name")] + public string PackageFullName { get; set; } + + [JsonPropertyName("app-user-model-id")] + public string AppUserModelId { get; set; } + + [JsonPropertyName("pwa-app-id")] + public string PwaAppId { get; set; } + + [JsonPropertyName("command-line-arguments")] + public string CommandLineArguments { get; set; } + + [JsonPropertyName("is-elevated")] + public bool IsElevated { get; set; } + + [JsonPropertyName("can-launch-elevated")] + public bool CanLaunchElevated { get; set; } + + [JsonPropertyName("minimized")] + public bool Minimized { get; set; } + + [JsonPropertyName("maximized")] + public bool Maximized { get; set; } + + [JsonPropertyName("position")] + public WindowPositionWrapper Position { get; set; } + + [JsonPropertyName("monitor")] + public int Monitor { get; set; } + + [JsonPropertyName("version")] + public string Version { get; set; } +} diff --git a/src/modules/Workspaces/WorkspacesEditor/Data/InvokePoint.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/InvokePoint.cs similarity index 51% rename from src/modules/Workspaces/WorkspacesEditor/Data/InvokePoint.cs rename to src/modules/Workspaces/WorkspacesCsharpLibrary/Data/InvokePoint.cs index fe41a65bd7..3f24d51f28 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Data/InvokePoint.cs +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/InvokePoint.cs @@ -2,13 +2,12 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -namespace WorkspacesEditor.Data +namespace WorkspacesCsharpLibrary.Data; + +public enum InvokePoint { - /* sync with workspaces-common */ - public enum InvokePoint - { - EditorButton = 0, - Shortcut, - LaunchAndEdit, - } + EditorButton = 0, + Shortcut, + LaunchAndEdit, + CommandPaletteExtension, } diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/MonitorConfigurationWrapper.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/MonitorConfigurationWrapper.cs new file mode 100644 index 0000000000..1c48dee1ab --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/MonitorConfigurationWrapper.cs @@ -0,0 +1,43 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Text.Json.Serialization; + +namespace WorkspacesCsharpLibrary.Data; + +public struct MonitorConfigurationWrapper +{ + public struct MonitorRectWrapper + { + [JsonPropertyName("top")] + public int Top { get; set; } + + [JsonPropertyName("left")] + public int Left { get; set; } + + [JsonPropertyName("width")] + public int Width { get; set; } + + [JsonPropertyName("height")] + public int Height { get; set; } + } + + [JsonPropertyName("id")] + public string Id { get; set; } + + [JsonPropertyName("instance-id")] + public string InstanceId { get; set; } + + [JsonPropertyName("monitor-number")] + public int MonitorNumber { get; set; } + + [JsonPropertyName("dpi")] + public int Dpi { get; set; } + + [JsonPropertyName("monitor-rect-dpi-aware")] + public MonitorRectWrapper MonitorRectDpiAware { get; set; } + + [JsonPropertyName("monitor-rect-dpi-unaware")] + public MonitorRectWrapper MonitorRectDpiUnaware { get; set; } +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/ProjectData.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/ProjectData.cs new file mode 100644 index 0000000000..04006cb2c5 --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/ProjectData.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using WorkspacesCsharpLibrary.Data; + +namespace WorkspacesCsharpLibrary.Data; + +public class ProjectData : WorkspacesEditorData<ProjectWrapper> +{ +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/ProjectWrapper.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/ProjectWrapper.cs new file mode 100644 index 0000000000..3f0f4dbc58 --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/ProjectWrapper.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; + +namespace WorkspacesCsharpLibrary.Data; + +public struct ProjectWrapper +{ + public string Id { get; set; } + + public string Name { get; set; } + + public long CreationTime { get; set; } + + public long LastLaunchedTime { get; set; } + + public bool IsShortcutNeeded { get; set; } + + public bool MoveExistingWindows { get; set; } + + public List<MonitorConfigurationWrapper> MonitorConfiguration { get; set; } + + public List<ApplicationWrapper> Applications { get; set; } +} diff --git a/src/modules/Workspaces/WorkspacesEditor/Data/TempProjectData.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/TempProjectData.cs similarity index 82% rename from src/modules/Workspaces/WorkspacesEditor/Data/TempProjectData.cs rename to src/modules/Workspaces/WorkspacesCsharpLibrary/Data/TempProjectData.cs index a1600885b9..c5e4a3ce25 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Data/TempProjectData.cs +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/TempProjectData.cs @@ -2,9 +2,10 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using WorkspacesEditor.Utils; +using WorkspacesCsharpLibrary.Data; +using WorkspacesCsharpLibrary.Utils; -namespace WorkspacesEditor.Data +namespace WorkspacesCsharpLibrary.Data { public class TempProjectData : ProjectData { diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesData.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesData.cs new file mode 100644 index 0000000000..6395bffdba --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesData.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using WorkspacesCsharpLibrary.Utils; +using static WorkspacesCsharpLibrary.Data.WorkspacesData; + +namespace WorkspacesCsharpLibrary.Data; + +public class WorkspacesData : WorkspacesEditorData<WorkspacesListWrapper> +{ + public string File => FolderUtils.DataFolder() + "\\workspaces.json"; + + public struct WorkspacesListWrapper + { + public List<ProjectWrapper> Workspaces { get; set; } + } + + public enum OrderBy + { + LastViewed = 0, + Created = 1, + Name = 2, + Unknown = 3, + } +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesEditorData`1.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesEditorData`1.cs new file mode 100644 index 0000000000..eed73af224 --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesEditorData`1.cs @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; +using WorkspacesCsharpLibrary.Utils; + +namespace WorkspacesCsharpLibrary.Data; + +/// <summary> +/// Shared JSON serializer helper for Workspaces payloads. +/// </summary> +public class WorkspacesEditorData<T> +{ + [RequiresUnreferencedCode("JSON serialization uses reflection-based serializer.")] + [RequiresDynamicCode("JSON serialization uses reflection-based serializer.")] + public T Read(string file) + { + IOUtils ioUtils = new(); + string data = ioUtils.ReadFile(file); + return JsonSerializer.Deserialize<T>(data, WorkspacesJsonOptions.EditorOptions)!; + } + + [RequiresUnreferencedCode("JSON serialization uses reflection-based serializer.")] + [RequiresDynamicCode("JSON serialization uses reflection-based serializer.")] + public string Serialize(T data) + { + return JsonSerializer.Serialize(data, WorkspacesJsonOptions.EditorOptions); + } + + [RequiresUnreferencedCode("JSON serialization uses reflection-based serializer.")] + [RequiresDynamicCode("JSON serialization uses reflection-based serializer.")] + public T Deserialize(string json) + { + return JsonSerializer.Deserialize<T>(json, WorkspacesJsonOptions.EditorOptions)!; + } +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesJsonOptions.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesJsonOptions.cs new file mode 100644 index 0000000000..d9d00152b3 --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesJsonOptions.cs @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Text.Json; +using WorkspacesCsharpLibrary.Utils; + +namespace WorkspacesCsharpLibrary.Data; + +internal static class WorkspacesJsonOptions +{ + internal static readonly JsonSerializerOptions EditorOptions = new() + { + PropertyNamingPolicy = new DashCaseNamingPolicy(), + WriteIndented = true, + }; +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesStorage.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesStorage.cs new file mode 100644 index 0000000000..ea33884577 --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesStorage.cs @@ -0,0 +1,96 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace WorkspacesCsharpLibrary.Data; + +/// <summary> +/// Lightweight reader for persisted workspaces. +/// </summary> +public static class WorkspacesStorage +{ + public static IReadOnlyList<ProjectWrapper> Load() + { + var filePath = GetDefaultFilePath(); + if (!File.Exists(filePath)) + { + return []; + } + + try + { + var json = File.ReadAllText(filePath); + var data = JsonSerializer.Deserialize(json, WorkspacesStorageJsonContext.Default.WorkspacesFile); + + if (data?.Workspaces == null) + { + return []; + } + + return data.Workspaces + .Where(ws => !string.IsNullOrWhiteSpace(ws.Id) && !string.IsNullOrWhiteSpace(ws.Name)) + .Select(ws => new ProjectWrapper + { + Id = ws.Id!, + Name = ws.Name!, + Applications = ws.Applications ?? new List<ApplicationWrapper>(), + CreationTime = ws.CreationTime, + LastLaunchedTime = ws.LastLaunchedTime, + IsShortcutNeeded = ws.IsShortcutNeeded, + MoveExistingWindows = ws.MoveExistingWindows, + MonitorConfiguration = ws.MonitorConfiguration ?? new List<MonitorConfigurationWrapper>(), + }) + .ToList() + .AsReadOnly(); + } + catch + { + return Array.Empty<ProjectWrapper>(); + } + } + + public static string GetDefaultFilePath() + { + var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + return Path.Combine(localAppData, "Microsoft", "PowerToys", "Workspaces", "workspaces.json"); + } + + internal sealed class WorkspacesFile + { + public List<WorkspaceProject> Workspaces { get; set; } = new(); + } + + internal sealed class WorkspaceProject + { + [JsonPropertyName("id")] + public string Id { get; set; } = string.Empty; + + [JsonPropertyName("name")] + public string Name { get; set; } = string.Empty; + + [JsonPropertyName("applications")] + public List<ApplicationWrapper> Applications { get; set; } = new(); + + [JsonPropertyName("monitor-configuration")] + public List<MonitorConfigurationWrapper> MonitorConfiguration { get; set; } = new(); + + [JsonPropertyName("creation-time")] + public long CreationTime { get; set; } + + [JsonPropertyName("last-launched-time")] + public long LastLaunchedTime { get; set; } + + [JsonPropertyName("is-shortcut-needed")] + public bool IsShortcutNeeded { get; set; } + + [JsonPropertyName("move-existing-windows")] + public bool MoveExistingWindows { get; set; } + } +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesStorageJsonContext.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesStorageJsonContext.cs new file mode 100644 index 0000000000..45ba31a03d --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Data/WorkspacesStorageJsonContext.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Text.Json.Serialization; + +namespace WorkspacesCsharpLibrary.Data; + +[JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true)] +[JsonSerializable(typeof(WorkspacesStorage.WorkspacesFile))] +[JsonSerializable(typeof(WorkspacesStorage.WorkspaceProject))] +[JsonSerializable(typeof(ApplicationWrapper))] +[JsonSerializable(typeof(ApplicationWrapper.WindowPositionWrapper))] +[JsonSerializable(typeof(MonitorConfigurationWrapper))] +[JsonSerializable(typeof(MonitorConfigurationWrapper.MonitorRectWrapper))] +internal sealed partial class WorkspacesStorageJsonContext : JsonSerializerContext +{ +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Models/BaseApplication.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Models/BaseApplication.cs index e3c0bff508..897bd97de5 100644 --- a/src/modules/Workspaces/WorkspacesCsharpLibrary/Models/BaseApplication.cs +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Models/BaseApplication.cs @@ -16,7 +16,7 @@ using Windows.Management.Deployment; namespace WorkspacesCsharpLibrary.Models { - public class BaseApplication : INotifyPropertyChanged, IDisposable + public partial class BaseApplication : INotifyPropertyChanged, IDisposable { public event PropertyChangedEventHandler PropertyChanged; diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/DashCaseNamingPolicy.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/DashCaseNamingPolicy.cs new file mode 100644 index 0000000000..cb1a0f1377 --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/DashCaseNamingPolicy.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Text.Json; +using WorkspacesCsharpLibrary.Utils; + +namespace WorkspacesCsharpLibrary.Utils; + +public class DashCaseNamingPolicy : JsonNamingPolicy +{ + public static DashCaseNamingPolicy Instance { get; } = new DashCaseNamingPolicy(); + + public override string ConvertName(string name) + { + return name.UpperCamelCaseToDashCase(); + } +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/FolderUtils.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/FolderUtils.cs new file mode 100644 index 0000000000..cef2aae957 --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/FolderUtils.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; + +namespace WorkspacesCsharpLibrary.Utils; + +public class FolderUtils +{ + public static string Desktop() + { + return Environment.GetFolderPath(Environment.SpecialFolder.Desktop); + } + + public static string Temp() + { + return Path.GetTempPath(); + } + + // Note: the same path should be used in SnapshotTool and Launcher + public static string DataFolder() + { + return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Microsoft\\PowerToys\\Workspaces"; + } +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/IOUtils.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/IOUtils.cs new file mode 100644 index 0000000000..8ceb703cc4 --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/IOUtils.cs @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using System.IO.Abstractions; +using System.Threading.Tasks; + +namespace WorkspacesCsharpLibrary.Utils; + +public class IOUtils +{ + private readonly IFileSystem _fileSystem = new FileSystem(); + + public void WriteFile(string fileName, string data) + { + _fileSystem.File.WriteAllText(fileName, data); + } + + public string ReadFile(string fileName) + { + if (_fileSystem.File.Exists(fileName)) + { + int attempts = 0; + while (attempts < 10) + { + try + { + using FileSystemStream inputStream = _fileSystem.File.Open(fileName, FileMode.Open); + using StreamReader reader = new(inputStream); + string data = reader.ReadToEnd(); + inputStream.Close(); + return data; + } + catch (Exception) + { + Task.Delay(10).Wait(); + } + + attempts++; + } + } + + return string.Empty; + } +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/StringUtils.cs b/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/StringUtils.cs new file mode 100644 index 0000000000..0b85911fa8 --- /dev/null +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/Utils/StringUtils.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Linq; + +namespace WorkspacesCsharpLibrary.Utils; + +public static class StringUtils +{ + public static string UpperCamelCaseToDashCase(this string str) + { + // If it's a single letter variable, leave it as it is + return str.Length == 1 + ? str + : string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "-" + x : x.ToString())).ToLowerInvariant(); + } +} diff --git a/src/modules/Workspaces/WorkspacesCsharpLibrary/WorkspacesCsharpLibrary.csproj b/src/modules/Workspaces/WorkspacesCsharpLibrary/WorkspacesCsharpLibrary.csproj index eea9001b12..501f6b4f0c 100644 --- a/src/modules/Workspaces/WorkspacesCsharpLibrary/WorkspacesCsharpLibrary.csproj +++ b/src/modules/Workspaces/WorkspacesCsharpLibrary/WorkspacesCsharpLibrary.csproj @@ -1,11 +1,13 @@ <Project Sdk="Microsoft.NET.Sdk"> <Import Project="..\..\..\Common.Dotnet.CsWinRT.props" /> + <Import Project="..\..\..\Common.Dotnet.AotCompatibility.props" /> <PropertyGroup> <AssemblyTitle>PowerToys.WorkspacesCsharpLibrary</AssemblyTitle> <AssemblyDescription>PowerToys Workspaces Csharp Library</AssemblyDescription> <Description>PowerToys Workspaces Csharp Library</Description> + <ImplicitUsings>enable</ImplicitUsings> <UseWPF>true</UseWPF> <UseWindowsForms>true</UseWindowsForms> <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> @@ -15,4 +17,8 @@ <AssemblyName>PowerToys.WorkspacesCsharpLibrary</AssemblyName> </PropertyGroup> -</Project> \ No newline at end of file + <ItemGroup> + <PackageReference Include="System.IO.Abstractions" /> + </ItemGroup> + +</Project> diff --git a/src/modules/Workspaces/WorkspacesEditor/Data/ProjectData.cs b/src/modules/Workspaces/WorkspacesEditor/Data/ProjectData.cs deleted file mode 100644 index 27c4290909..0000000000 --- a/src/modules/Workspaces/WorkspacesEditor/Data/ProjectData.cs +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Collections.Generic; -using static WorkspacesEditor.Data.ProjectData; - -namespace WorkspacesEditor.Data -{ - public class ProjectData : WorkspacesEditorData<ProjectWrapper> - { - public struct ApplicationWrapper - { - public struct WindowPositionWrapper - { - public int X { get; set; } - - public int Y { get; set; } - - public int Width { get; set; } - - public int Height { get; set; } - } - - public string Id { get; set; } - - public string Application { get; set; } - - public string ApplicationPath { get; set; } - - public string Title { get; set; } - - public string PackageFullName { get; set; } - - public string AppUserModelId { get; set; } - - public string PwaAppId { get; set; } - - public string CommandLineArguments { get; set; } - - public bool IsElevated { get; set; } - - public bool CanLaunchElevated { get; set; } - - public bool Minimized { get; set; } - - public bool Maximized { get; set; } - - public WindowPositionWrapper Position { get; set; } - - public int Monitor { get; set; } - - public string Version { get; set; } - } - - public struct MonitorConfigurationWrapper - { - public struct MonitorRectWrapper - { - public int Top { get; set; } - - public int Left { get; set; } - - public int Width { get; set; } - - public int Height { get; set; } - } - - public string Id { get; set; } - - public string InstanceId { get; set; } - - public int MonitorNumber { get; set; } - - public int Dpi { get; set; } - - public MonitorRectWrapper MonitorRectDpiAware { get; set; } - - public MonitorRectWrapper MonitorRectDpiUnaware { get; set; } - } - - public struct ProjectWrapper - { - public string Id { get; set; } - - public string Name { get; set; } - - public long CreationTime { get; set; } - - public long LastLaunchedTime { get; set; } - - public bool IsShortcutNeeded { get; set; } - - public bool MoveExistingWindows { get; set; } - - public List<MonitorConfigurationWrapper> MonitorConfiguration { get; set; } - - public List<ApplicationWrapper> Applications { get; set; } - } - } -} diff --git a/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesData.cs b/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesData.cs deleted file mode 100644 index 6e0d015905..0000000000 --- a/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesData.cs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Collections.Generic; -using WorkspacesEditor.Utils; - -using static WorkspacesEditor.Data.ProjectData; -using static WorkspacesEditor.Data.WorkspacesData; - -namespace WorkspacesEditor.Data -{ - public class WorkspacesData : WorkspacesEditorData<WorkspacesListWrapper> - { - public string File => FolderUtils.DataFolder() + "\\workspaces.json"; - - public struct WorkspacesListWrapper - { - public List<ProjectWrapper> Workspaces { get; set; } - } - - public enum OrderBy - { - LastViewed = 0, - Created = 1, - Name = 2, - Unknown = 3, - } - } -} diff --git a/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesEditorData`1.cs b/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesEditorData`1.cs deleted file mode 100644 index c2ad0a70a4..0000000000 --- a/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesEditorData`1.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Text.Json; -using WorkspacesEditor.Utils; - -namespace WorkspacesEditor.Data -{ - public class WorkspacesEditorData<T> - { - protected JsonSerializerOptions JsonOptions - { - get => new() - { - PropertyNamingPolicy = new DashCaseNamingPolicy(), - WriteIndented = true, - }; - } - - public T Read(string file) - { - IOUtils ioUtils = new(); - string data = ioUtils.ReadFile(file); - return JsonSerializer.Deserialize<T>(data, JsonOptions); - } - - public string Serialize(T data) - { - return JsonSerializer.Serialize(data, JsonOptions); - } - } -} diff --git a/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs b/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs index a3b82355b7..bb5fd1c93e 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs @@ -13,7 +13,7 @@ using System.Threading.Tasks; using System.Windows.Media.Imaging; using ManagedCommon; -using WorkspacesEditor.Data; +using WorkspacesCsharpLibrary.Data; using WorkspacesEditor.Utils; namespace WorkspacesEditor.Models @@ -226,7 +226,7 @@ namespace WorkspacesEditor.Models } } - public Project(ProjectData.ProjectWrapper project) + public Project(ProjectWrapper project) { Id = project.Id; Name = project.Name; @@ -237,7 +237,7 @@ namespace WorkspacesEditor.Models Monitors = []; Applications = []; - foreach (ProjectData.ApplicationWrapper app in project.Applications) + foreach (ApplicationWrapper app in project.Applications) { Models.Application newApp = new() { @@ -269,7 +269,7 @@ namespace WorkspacesEditor.Models Applications.Add(newApp); } - foreach (ProjectData.MonitorConfigurationWrapper monitor in project.MonitorConfiguration) + foreach (MonitorConfigurationWrapper monitor in project.MonitorConfiguration) { System.Windows.Rect dpiAware = new(monitor.MonitorRectDpiAware.Left, monitor.MonitorRectDpiAware.Top, monitor.MonitorRectDpiAware.Width, monitor.MonitorRectDpiAware.Height); System.Windows.Rect dpiUnaware = new(monitor.MonitorRectDpiUnaware.Left, monitor.MonitorRectDpiUnaware.Top, monitor.MonitorRectDpiUnaware.Width, monitor.MonitorRectDpiUnaware.Height); diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/DashCaseNamingPolicy.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/DashCaseNamingPolicy.cs deleted file mode 100644 index 3e8857a076..0000000000 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/DashCaseNamingPolicy.cs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Text.Json; - -namespace WorkspacesEditor.Utils -{ - public class DashCaseNamingPolicy : JsonNamingPolicy - { - public static DashCaseNamingPolicy Instance { get; } = new DashCaseNamingPolicy(); - - public override string ConvertName(string name) - { - return name.UpperCamelCaseToDashCase(); - } - } -} diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/FolderUtils.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/FolderUtils.cs deleted file mode 100644 index fc12593e09..0000000000 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/FolderUtils.cs +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.IO; - -namespace WorkspacesEditor.Utils -{ - public class FolderUtils - { - public static string Desktop() - { - return Environment.GetFolderPath(Environment.SpecialFolder.Desktop); - } - - public static string Temp() - { - return Path.GetTempPath(); - } - - // Note: the same path should be used in SnapshotTool and Launcher - public static string DataFolder() - { - return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Microsoft\\PowerToys\\Workspaces"; - } - } -} diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/IOUtils.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/IOUtils.cs deleted file mode 100644 index fe69777593..0000000000 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/IOUtils.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.IO; -using System.IO.Abstractions; -using System.Threading.Tasks; - -namespace WorkspacesEditor.Utils -{ - public class IOUtils - { - private readonly IFileSystem _fileSystem = new FileSystem(); - - public IOUtils() - { - } - - public void WriteFile(string fileName, string data) - { - _fileSystem.File.WriteAllText(fileName, data); - } - - public string ReadFile(string fileName) - { - if (_fileSystem.File.Exists(fileName)) - { - int attempts = 0; - while (attempts < 10) - { - try - { - using FileSystemStream inputStream = _fileSystem.File.Open(fileName, FileMode.Open); - using StreamReader reader = new(inputStream); - string data = reader.ReadToEnd(); - inputStream.Close(); - return data; - } - catch (Exception) - { - Task.Delay(10).Wait(); - } - - attempts++; - } - } - - return string.Empty; - } - } -} diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesEditorIO.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesEditorIO.cs index a66145b484..b0b3dc9a50 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesEditorIO.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesEditorIO.cs @@ -6,9 +6,9 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; - using ManagedCommon; -using WorkspacesEditor.Data; +using WorkspacesCsharpLibrary.Data; +using WorkspacesCsharpLibrary.Utils; using WorkspacesEditor.Models; using WorkspacesEditor.ViewModels; @@ -81,7 +81,7 @@ namespace WorkspacesEditor.Utils foreach (Project project in workspaces) { - ProjectData.ProjectWrapper wrapper = new() + ProjectWrapper wrapper = new() { Id = project.Id, Name = project.Name, @@ -95,7 +95,7 @@ namespace WorkspacesEditor.Utils foreach (Application app in project.Applications.Where(x => x.IsIncluded)) { - wrapper.Applications.Add(new ProjectData.ApplicationWrapper + wrapper.Applications.Add(new ApplicationWrapper { Id = app.Id, Application = app.AppName, @@ -110,7 +110,7 @@ namespace WorkspacesEditor.Utils Version = app.Version, Maximized = app.Maximized, Minimized = app.Minimized, - Position = new ProjectData.ApplicationWrapper.WindowPositionWrapper + Position = new ApplicationWrapper.WindowPositionWrapper { X = app.Position.X, Y = app.Position.Y, @@ -123,20 +123,20 @@ namespace WorkspacesEditor.Utils foreach (MonitorSetup monitor in project.Monitors) { - wrapper.MonitorConfiguration.Add(new ProjectData.MonitorConfigurationWrapper + wrapper.MonitorConfiguration.Add(new MonitorConfigurationWrapper { Id = monitor.MonitorName, InstanceId = monitor.MonitorInstanceId, MonitorNumber = monitor.MonitorNumber, Dpi = monitor.Dpi, - MonitorRectDpiAware = new ProjectData.MonitorConfigurationWrapper.MonitorRectWrapper + MonitorRectDpiAware = new MonitorConfigurationWrapper.MonitorRectWrapper { Left = (int)monitor.MonitorDpiAwareBounds.Left, Top = (int)monitor.MonitorDpiAwareBounds.Top, Width = (int)monitor.MonitorDpiAwareBounds.Width, Height = (int)monitor.MonitorDpiAwareBounds.Height, }, - MonitorRectDpiUnaware = new ProjectData.MonitorConfigurationWrapper.MonitorRectWrapper + MonitorRectDpiUnaware = new MonitorConfigurationWrapper.MonitorRectWrapper { Left = (int)monitor.MonitorDpiUnawareBounds.Left, Top = (int)monitor.MonitorDpiUnawareBounds.Top, @@ -163,7 +163,7 @@ namespace WorkspacesEditor.Utils private bool AddWorkspaces(MainViewModel mainViewModel, WorkspacesData.WorkspacesListWrapper workspaces) { - foreach (ProjectData.ProjectWrapper project in workspaces.Workspaces) + foreach (ProjectWrapper project in workspaces.Workspaces) { mainViewModel.Workspaces.Add(new Project(project)); } diff --git a/src/modules/Workspaces/WorkspacesEditor/ViewModels/MainViewModel.cs b/src/modules/Workspaces/WorkspacesEditor/ViewModels/MainViewModel.cs index 30b28f4cd8..9c76c26fa0 100644 --- a/src/modules/Workspaces/WorkspacesEditor/ViewModels/MainViewModel.cs +++ b/src/modules/Workspaces/WorkspacesEditor/ViewModels/MainViewModel.cs @@ -18,12 +18,12 @@ using ManagedCommon; using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Telemetry; using WorkspacesCsharpLibrary; -using WorkspacesEditor.Data; +using WorkspacesCsharpLibrary.Data; +using WorkspacesCsharpLibrary.Utils; using WorkspacesEditor.Models; using WorkspacesEditor.Telemetry; using WorkspacesEditor.Utils; - -using static WorkspacesEditor.Data.WorkspacesData; +using static WorkspacesCsharpLibrary.Data.WorkspacesData; namespace WorkspacesEditor.ViewModels { diff --git a/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditor.csproj b/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditor.csproj index 3f7d153e56..71e91979bd 100644 --- a/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditor.csproj +++ b/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditor.csproj @@ -78,6 +78,15 @@ <ProjectReference Include="..\..\..\settings-ui\Settings.UI.Library\Settings.UI.Library.csproj" /> <ProjectReference Include="..\WorkspacesCsharpLibrary\WorkspacesCsharpLibrary.csproj" /> </ItemGroup> + <ItemGroup> + <Compile Remove="Data\WorkspacesData.cs" /> + <Compile Remove="Data\ProjectData.cs" /> + <Compile Remove="Data\WorkspacesEditorData`1.cs" /> + <Compile Remove="Utils\IOUtils.cs" /> + <Compile Remove="Utils\FolderUtils.cs" /> + <Compile Remove="Utils\DashCaseNamingPolicy.cs" /> + </ItemGroup> + <ItemGroup> <Compile Update="Properties\Resources.Designer.cs"> <DesignTime>True</DesignTime> @@ -96,4 +105,4 @@ <LastGenOutput>Settings.Designer.cs</LastGenOutput> </None> </ItemGroup> -</Project> \ No newline at end of file +</Project> diff --git a/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditorPage.xaml.cs b/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditorPage.xaml.cs index 1dde4d1114..4a573584ad 100644 --- a/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditorPage.xaml.cs +++ b/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditorPage.xaml.cs @@ -9,7 +9,7 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Input; -using WorkspacesEditor.Data; +using WorkspacesCsharpLibrary.Data; using WorkspacesEditor.Models; using WorkspacesEditor.ViewModels; diff --git a/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchData.cs b/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchData.cs index 6e9ad24379..1e9bf665c5 100644 --- a/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchData.cs +++ b/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchData.cs @@ -3,15 +3,12 @@ // See the LICENSE file in the project root for more information. using System.Text.Json.Serialization; - -using Workspaces.Data; - using static WorkspacesLauncherUI.Data.AppLaunchData; using static WorkspacesLauncherUI.Data.AppLaunchInfosData; namespace WorkspacesLauncherUI.Data { - public class AppLaunchData : WorkspacesUIData<AppLaunchDataWrapper> + public class AppLaunchData : WorkspacesCsharpLibrary.Data.WorkspacesEditorData<AppLaunchDataWrapper> { public struct AppLaunchDataWrapper { diff --git a/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchInfoData.cs b/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchInfoData.cs index c01ffaba8c..5a19ccde15 100644 --- a/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchInfoData.cs +++ b/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchInfoData.cs @@ -3,14 +3,11 @@ // See the LICENSE file in the project root for more information. using System.Text.Json.Serialization; - -using Workspaces.Data; - using static WorkspacesLauncherUI.Data.AppLaunchInfoData; namespace WorkspacesLauncherUI.Data { - public class AppLaunchInfoData : WorkspacesUIData<AppLaunchInfoWrapper> + public class AppLaunchInfoData : WorkspacesCsharpLibrary.Data.WorkspacesEditorData<AppLaunchInfoWrapper> { public struct AppLaunchInfoWrapper { diff --git a/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchInfosData.cs b/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchInfosData.cs index cb00cb4478..a656712d9a 100644 --- a/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchInfosData.cs +++ b/src/modules/Workspaces/WorkspacesLauncherUI/Data/AppLaunchInfosData.cs @@ -4,15 +4,12 @@ using System.Collections.Generic; using System.Text.Json.Serialization; - -using Workspaces.Data; - using static WorkspacesLauncherUI.Data.AppLaunchInfoData; using static WorkspacesLauncherUI.Data.AppLaunchInfosData; namespace WorkspacesLauncherUI.Data { - public class AppLaunchInfosData : WorkspacesUIData<AppLaunchInfoListWrapper> + public class AppLaunchInfosData : WorkspacesCsharpLibrary.Data.WorkspacesEditorData<AppLaunchInfoListWrapper> { public struct AppLaunchInfoListWrapper { diff --git a/src/modules/Workspaces/WorkspacesLauncherUI/Data/WorkspacesUIData`1.cs b/src/modules/Workspaces/WorkspacesLauncherUI/Data/WorkspacesUIData`1.cs deleted file mode 100644 index 5e9b88a728..0000000000 --- a/src/modules/Workspaces/WorkspacesLauncherUI/Data/WorkspacesUIData`1.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Text.Json; - -using WorkspacesLauncherUI.Utils; - -namespace Workspaces.Data -{ - public class WorkspacesUIData<T> - { - protected JsonSerializerOptions JsonOptions - { - get - { - return new JsonSerializerOptions - { - PropertyNamingPolicy = new DashCaseNamingPolicy(), - WriteIndented = true, - }; - } - } - - public T Deserialize(string data) - { - return JsonSerializer.Deserialize<T>(data, JsonOptions); - } - - public string Serialize(T data) - { - return JsonSerializer.Serialize(data, JsonOptions); - } - } -} diff --git a/src/modules/Workspaces/WorkspacesLauncherUI/ViewModels/MainViewModel.cs b/src/modules/Workspaces/WorkspacesLauncherUI/ViewModels/MainViewModel.cs index aa029d7ea2..5b358686de 100644 --- a/src/modules/Workspaces/WorkspacesLauncherUI/ViewModels/MainViewModel.cs +++ b/src/modules/Workspaces/WorkspacesLauncherUI/ViewModels/MainViewModel.cs @@ -6,13 +6,11 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; -using System.Diagnostics; using ManagedCommon; using WorkspacesCsharpLibrary; using WorkspacesLauncherUI.Data; using WorkspacesLauncherUI.Models; -using WorkspacesLauncherUI.Utils; namespace WorkspacesLauncherUI.ViewModels { diff --git a/src/modules/Workspaces/WorkspacesLauncherUI/WorkspacesLauncherUI.csproj b/src/modules/Workspaces/WorkspacesLauncherUI/WorkspacesLauncherUI.csproj index 839c08f90d..f55d30205f 100644 --- a/src/modules/Workspaces/WorkspacesLauncherUI/WorkspacesLauncherUI.csproj +++ b/src/modules/Workspaces/WorkspacesLauncherUI/WorkspacesLauncherUI.csproj @@ -1,102 +1,102 @@ <Project Sdk="Microsoft.NET.Sdk"> - <!-- Look at Directory.Build.props in root for common stuff as well --> - <Import Project="..\..\..\Common.Dotnet.CsWinRT.props" /> - <Import Project="..\..\..\Common.SelfContained.props" /> - - <PropertyGroup> - <AssemblyTitle>PowerToys.WorkspacesLauncherUI</AssemblyTitle> - <AssemblyDescription>PowerToys Workspaces Editor</AssemblyDescription> - <Description>PowerToys Workspaces Editor</Description> - <OutputType>WinExe</OutputType> - <UseWPF>true</UseWPF> - <UseWindowsForms>true</UseWindowsForms> - <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> - <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> - <GenerateSatelliteAssembliesForCore>true</GenerateSatelliteAssembliesForCore> - <OutputPath>..\..\..\..\$(Platform)\$(Configuration)</OutputPath> - </PropertyGroup> - - <PropertyGroup> - <ProjectGuid>{9C53CC25-0623-4569-95BC-B05410675EE3}</ProjectGuid> - <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> - <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> - </PropertyGroup> + <!-- Look at Directory.Build.props in root for common stuff as well --> + <Import Project="..\..\..\Common.Dotnet.CsWinRT.props" /> + <Import Project="..\..\..\Common.SelfContained.props" /> - <PropertyGroup> - <ApplicationIcon>..\Assets\Workspaces\Workspaces.ico</ApplicationIcon> - </PropertyGroup> - <PropertyGroup> - <ApplicationManifest>app.manifest</ApplicationManifest> - <AssemblyName>PowerToys.WorkspacesLauncherUI</AssemblyName> - </PropertyGroup> - - <ItemGroup> - <Content Include="..\Assets\**\*.*"> - <Link>Assets\Workspaces\%(Filename)%(Extension)</Link> - <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> - </Content> - </ItemGroup> + <PropertyGroup> + <AssemblyTitle>PowerToys.WorkspacesLauncherUI</AssemblyTitle> + <AssemblyDescription>PowerToys Workspaces Launcher UI</AssemblyDescription> + <Description>PowerToys Workspaces Launcher UI</Description> + <OutputType>WinExe</OutputType> + <UseWPF>true</UseWPF> + <UseWindowsForms>true</UseWindowsForms> + <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> + <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> + <GenerateSatelliteAssembliesForCore>true</GenerateSatelliteAssembliesForCore> + <OutputPath>..\..\..\..\$(Platform)\$(Configuration)</OutputPath> + </PropertyGroup> - <ItemGroup> - <COMReference Include="IWshRuntimeLibrary"> - <WrapperTool>tlbimp</WrapperTool> - <VersionMinor>0</VersionMinor> - <VersionMajor>1</VersionMajor> - <Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid> - <Lcid>0</Lcid> - <Isolated>false</Isolated> - <EmbedInteropTypes>true</EmbedInteropTypes> - </COMReference> - <COMReference Include="Shell32"> - <WrapperTool>tlbimp</WrapperTool> - <VersionMinor>0</VersionMinor> - <VersionMajor>1</VersionMajor> - <Guid>50a7e9b0-70ef-11d1-b75a-00a0c90564fe</Guid> - <Lcid>0</Lcid> - <Isolated>false</Isolated> - <EmbedInteropTypes>true</EmbedInteropTypes> - </COMReference> - </ItemGroup> - - <ItemGroup> - <EmbeddedResource Update="Properties\Resources.resx"> - <Generator>PublicResXFileCodeGenerator</Generator> - <LastGenOutput>Resources.Designer.cs</LastGenOutput> - </EmbeddedResource> - <None Include="app.manifest" /> - </ItemGroup> + <PropertyGroup> + <ProjectGuid>{9C53CC25-0623-4569-95BC-B05410675EE3}</ProjectGuid> + <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> + </PropertyGroup> - <ItemGroup> - <PackageReference Include="ControlzEx" /> - <PackageReference Include="ModernWpfUI" /> - <PackageReference Include="System.IO.Abstractions" /> - </ItemGroup> + <PropertyGroup> + <ApplicationIcon>..\Assets\Workspaces\Workspaces.ico</ApplicationIcon> + </PropertyGroup> + <PropertyGroup> + <ApplicationManifest>app.manifest</ApplicationManifest> + <AssemblyName>PowerToys.WorkspacesLauncherUI</AssemblyName> + </PropertyGroup> - <ItemGroup> - <ProjectReference Include="..\..\..\common\Common.UI\Common.UI.csproj" /> - <ProjectReference Include="..\..\..\common\GPOWrapperProjection\GPOWrapperProjection.csproj" /> - <ProjectReference Include="..\..\..\common\interop\PowerToys.Interop.vcxproj" /> - <ProjectReference Include="..\..\..\common\ManagedCommon\ManagedCommon.csproj" /> - <ProjectReference Include="..\..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" /> - <ProjectReference Include="..\..\..\settings-ui\Settings.UI.Library\Settings.UI.Library.csproj" /> - <ProjectReference Include="..\WorkspacesCsharpLibrary\WorkspacesCsharpLibrary.csproj" /> - </ItemGroup> - <ItemGroup> - <Compile Update="Properties\Resources.Designer.cs"> - <DesignTime>True</DesignTime> - <AutoGen>True</AutoGen> - <DependentUpon>Resources.resx</DependentUpon> - </Compile> - <Compile Update="Properties\Settings.Designer.cs"> - <DesignTimeSharedInput>True</DesignTimeSharedInput> - <AutoGen>True</AutoGen> - <DependentUpon>Settings.settings</DependentUpon> - </Compile> - </ItemGroup> - <ItemGroup> - <None Update="Properties\Settings.settings"> - <Generator>SettingsSingleFileGenerator</Generator> - <LastGenOutput>Settings.Designer.cs</LastGenOutput> - </None> - </ItemGroup> + <ItemGroup> + <Content Include="..\Assets\**\*.*"> + <Link>Assets\Workspaces\%(Filename)%(Extension)</Link> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </Content> + </ItemGroup> + + <ItemGroup> + <COMReference Include="IWshRuntimeLibrary"> + <WrapperTool>tlbimp</WrapperTool> + <VersionMinor>0</VersionMinor> + <VersionMajor>1</VersionMajor> + <Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid> + <Lcid>0</Lcid> + <Isolated>false</Isolated> + <EmbedInteropTypes>true</EmbedInteropTypes> + </COMReference> + <COMReference Include="Shell32"> + <WrapperTool>tlbimp</WrapperTool> + <VersionMinor>0</VersionMinor> + <VersionMajor>1</VersionMajor> + <Guid>50a7e9b0-70ef-11d1-b75a-00a0c90564fe</Guid> + <Lcid>0</Lcid> + <Isolated>false</Isolated> + <EmbedInteropTypes>true</EmbedInteropTypes> + </COMReference> + </ItemGroup> + + <ItemGroup> + <EmbeddedResource Update="Properties\Resources.resx"> + <Generator>PublicResXFileCodeGenerator</Generator> + <LastGenOutput>Resources.Designer.cs</LastGenOutput> + </EmbeddedResource> + <None Include="app.manifest" /> + </ItemGroup> + + <ItemGroup> + <PackageReference Include="ControlzEx" /> + <PackageReference Include="ModernWpfUI" /> + <PackageReference Include="System.IO.Abstractions" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\..\..\common\Common.UI\Common.UI.csproj" /> + <ProjectReference Include="..\..\..\common\GPOWrapperProjection\GPOWrapperProjection.csproj" /> + <ProjectReference Include="..\..\..\common\interop\PowerToys.Interop.vcxproj" /> + <ProjectReference Include="..\..\..\common\ManagedCommon\ManagedCommon.csproj" /> + <ProjectReference Include="..\..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" /> + <ProjectReference Include="..\..\..\settings-ui\Settings.UI.Library\Settings.UI.Library.csproj" /> + <ProjectReference Include="..\WorkspacesCsharpLibrary\WorkspacesCsharpLibrary.csproj" /> + </ItemGroup> + <ItemGroup> + <Compile Update="Properties\Resources.Designer.cs"> + <DesignTime>True</DesignTime> + <AutoGen>True</AutoGen> + <DependentUpon>Resources.resx</DependentUpon> + </Compile> + <Compile Update="Properties\Settings.Designer.cs"> + <DesignTimeSharedInput>True</DesignTimeSharedInput> + <AutoGen>True</AutoGen> + <DependentUpon>Settings.settings</DependentUpon> + </Compile> + </ItemGroup> + <ItemGroup> + <None Update="Properties\Settings.settings"> + <Generator>SettingsSingleFileGenerator</Generator> + <LastGenOutput>Settings.Designer.cs</LastGenOutput> + </None> + </ItemGroup> </Project> \ No newline at end of file diff --git a/src/modules/Workspaces/WorkspacesModuleInterface/dllmain.cpp b/src/modules/Workspaces/WorkspacesModuleInterface/dllmain.cpp index 90c898fc5f..a4caf01649 100644 --- a/src/modules/Workspaces/WorkspacesModuleInterface/dllmain.cpp +++ b/src/modules/Workspaces/WorkspacesModuleInterface/dllmain.cpp @@ -201,7 +201,7 @@ public: Logger::error(message.value()); } } - m_toggleEditorEventWaiter = EventWaiter(CommonSharedConstants::WORKSPACES_LAUNCH_EDITOR_EVENT, [&](int err) { + m_toggleEditorEventWaiter.start(CommonSharedConstants::WORKSPACES_LAUNCH_EDITOR_EVENT, [&](DWORD err) { if (err == ERROR_SUCCESS) { Logger::trace(L"{} event was signaled", CommonSharedConstants::WORKSPACES_LAUNCH_EDITOR_EVENT); diff --git a/src/modules/ZoomIt/ZoomIt/ZoomIt.vcxproj b/src/modules/ZoomIt/ZoomIt/ZoomIt.vcxproj index b1f91a5228..77c299f303 100644 --- a/src/modules/ZoomIt/ZoomIt/ZoomIt.vcxproj +++ b/src/modules/ZoomIt/ZoomIt/ZoomIt.vcxproj @@ -369,4 +369,4 @@ <Import Project="..\..\..\..\packages\robmikh.common.0.0.23-beta\build\native\robmikh.common.targets" Condition="Exists('..\..\..\..\packages\robmikh.common.0.0.23-beta\build\native\robmikh.common.targets')" /> <Import Project="..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\..\..\..\packages\Microsoft.Windows.CppWinRT.2.0.240111.5\build\native\Microsoft.Windows.CppWinRT.targets')" /> <Import Project="..\..\..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.231216.1\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('..\..\..\..\packages\Microsoft.Windows.ImplementationLibrary.1.0.231216.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" /> -</Project> \ No newline at end of file +</Project> diff --git a/src/modules/ZoomIt/ZoomIt/Zoomit.cpp b/src/modules/ZoomIt/ZoomIt/Zoomit.cpp index 71f09fc578..e38ca07f66 100644 --- a/src/modules/ZoomIt/ZoomIt/Zoomit.cpp +++ b/src/modules/ZoomIt/ZoomIt/Zoomit.cpp @@ -28,6 +28,20 @@ #include <common/utils/logger_helper.h> #include <common/utils/winapi_error.h> #include <common/utils/gpo.h> +#include <array> +#include <vector> +#endif // __ZOOMIT_POWERTOYS__ + +#ifdef __ZOOMIT_POWERTOYS__ +enum class ZoomItCommand +{ + Zoom, + Draw, + Break, + LiveZoom, + Snip, + Record, +}; #endif // __ZOOMIT_POWERTOYS__ namespace winrt @@ -172,7 +186,6 @@ std::wstring g_RecordingSaveLocationGIF; winrt::IDirect3DDevice g_RecordDevice{ nullptr }; std::shared_ptr<VideoRecordingSession> g_RecordingSession = nullptr; std::shared_ptr<GifRecordingSession> g_GifRecordingSession = nullptr; - type_pGetMonitorInfo pGetMonitorInfo; type_MonitorFromPoint pMonitorFromPoint; type_pSHAutoComplete pSHAutoComplete; @@ -7712,6 +7725,53 @@ HWND InitInstance( HINSTANCE hInstance, int nCmdShow ) } +// Dispatch commands coming from the PowerToys IPC channel. +#ifdef __ZOOMIT_POWERTOYS__ +void ZoomIt_DispatchCommand(ZoomItCommand cmd) +{ + auto post_hotkey = [](WPARAM id) + { + if (g_hWndMain != nullptr) + { + PostMessage(g_hWndMain, WM_HOTKEY, id, 0); + } + }; + + switch (cmd) + { + case ZoomItCommand::Zoom: + if (g_hWndMain != nullptr) + { + PostMessage(g_hWndMain, WM_COMMAND, IDC_ZOOM, 0); + } + Trace::ZoomItActivateZoom(); + break; + case ZoomItCommand::Draw: + post_hotkey(DRAW_HOTKEY); + Trace::ZoomItActivateDraw(); + break; + case ZoomItCommand::Break: + post_hotkey(BREAK_HOTKEY); + Trace::ZoomItActivateBreak(); + break; + case ZoomItCommand::LiveZoom: + post_hotkey(LIVE_HOTKEY); + Trace::ZoomItActivateLiveZoom(); + break; + case ZoomItCommand::Snip: + post_hotkey(SNIP_HOTKEY); + Trace::ZoomItActivateSnip(); + break; + case ZoomItCommand::Record: + post_hotkey(RECORD_HOTKEY); + Trace::ZoomItActivateRecord(); + break; + default: + break; + } +} +#endif + //---------------------------------------------------------------------------- // // WinMain @@ -7746,7 +7806,6 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance // Initialize logger LoggerHelpers::init_logger(L"ZoomIt", L"", LogSettings::zoomItLoggerName); - ProcessWaiter::OnProcessTerminate(pid, [mainThreadId](int err) { if (err != ERROR_SUCCESS) { @@ -7905,27 +7964,63 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance #ifdef __ZOOMIT_POWERTOYS__ HANDLE m_reload_settings_event_handle = NULL; HANDLE m_exit_event_handle = NULL; + HANDLE m_zoom_event_handle = NULL; + HANDLE m_draw_event_handle = NULL; + HANDLE m_break_event_handle = NULL; + HANDLE m_live_zoom_event_handle = NULL; + HANDLE m_snip_event_handle = NULL; + HANDLE m_record_event_handle = NULL; std::thread m_event_triggers_thread; if( g_StartedByPowerToys ) { // Start a thread to listen to PowerToys Events. m_reload_settings_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::ZOOMIT_REFRESH_SETTINGS_EVENT); m_exit_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::ZOOMIT_EXIT_EVENT); - if (!m_reload_settings_event_handle || !m_exit_event_handle) + m_zoom_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::ZOOMIT_ZOOM_EVENT); + m_draw_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::ZOOMIT_DRAW_EVENT); + m_break_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::ZOOMIT_BREAK_EVENT); + m_live_zoom_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::ZOOMIT_LIVEZOOM_EVENT); + m_snip_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::ZOOMIT_SNIP_EVENT); + m_record_event_handle = CreateEventW(nullptr, false, false, CommonSharedConstants::ZOOMIT_RECORD_EVENT); + if (!m_reload_settings_event_handle || !m_exit_event_handle || !m_zoom_event_handle || !m_draw_event_handle || !m_break_event_handle || !m_live_zoom_event_handle || !m_snip_event_handle || !m_record_event_handle) { Logger::warn(L"Failed to create events. {}", get_last_error_or_default(GetLastError())); return 1; } - m_event_triggers_thread = std::thread([&]() { + const std::array<HANDLE, 8> event_handles{ + m_reload_settings_event_handle, + m_exit_event_handle, + m_zoom_event_handle, + m_draw_event_handle, + m_break_event_handle, + m_live_zoom_event_handle, + m_snip_event_handle, + m_record_event_handle, + }; + const DWORD handle_count = static_cast<DWORD>(event_handles.size()); + m_event_triggers_thread = std::thread([event_handles, handle_count]() { MSG msg; - HANDLE event_handles[2] = {m_reload_settings_event_handle, m_exit_event_handle}; while (g_running) { - DWORD dwEvt = MsgWaitForMultipleObjects(2, event_handles, false, INFINITE, QS_ALLINPUT); + DWORD dwEvt = MsgWaitForMultipleObjects(handle_count, event_handles.data(), false, INFINITE, QS_ALLINPUT); + if (dwEvt == WAIT_FAILED) + { + Logger::error(L"ZoomIt event wait failed. {}", get_last_error_or_default(GetLastError())); + break; + } if (!g_running) { break; } + if (dwEvt == WAIT_OBJECT_0 + handle_count) + { + if (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) + { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + continue; + } switch (dwEvt) { case WAIT_OBJECT_0: @@ -7938,19 +8033,28 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance case WAIT_OBJECT_0 + 1: { // Exit Event - Logger::trace(L"Received an exit event."); PostMessage(g_hWndMain, WM_QUIT, 0, 0); break; } case WAIT_OBJECT_0 + 2: - if (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) - { - TranslateMessage(&msg); - DispatchMessageW(&msg); - } + ZoomIt_DispatchCommand(ZoomItCommand::Zoom); break; - default: + case WAIT_OBJECT_0 + 3: + ZoomIt_DispatchCommand(ZoomItCommand::Draw); break; + case WAIT_OBJECT_0 + 4: + ZoomIt_DispatchCommand(ZoomItCommand::Break); + break; + case WAIT_OBJECT_0 + 5: + ZoomIt_DispatchCommand(ZoomItCommand::LiveZoom); + break; + case WAIT_OBJECT_0 + 6: + ZoomIt_DispatchCommand(ZoomItCommand::Snip); + break; + case WAIT_OBJECT_0 + 7: + ZoomIt_DispatchCommand(ZoomItCommand::Record); + break; + default: break; } } }); @@ -7980,6 +8084,12 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance SetEvent(m_reload_settings_event_handle); CloseHandle(m_reload_settings_event_handle); CloseHandle(m_exit_event_handle); + CloseHandle(m_zoom_event_handle); + CloseHandle(m_draw_event_handle); + CloseHandle(m_break_event_handle); + CloseHandle(m_live_zoom_event_handle); + CloseHandle(m_snip_event_handle); + CloseHandle(m_record_event_handle); m_event_triggers_thread.join(); } #endif // __ZOOMIT_POWERTOYS__ diff --git a/src/modules/ZoomIt/ZoomItModuleInterface/dllmain.cpp b/src/modules/ZoomIt/ZoomItModuleInterface/dllmain.cpp index eea809a0a2..40158ed68f 100644 --- a/src/modules/ZoomIt/ZoomItModuleInterface/dllmain.cpp +++ b/src/modules/ZoomIt/ZoomItModuleInterface/dllmain.cpp @@ -8,6 +8,7 @@ #include <common/utils/logger_helper.h> #include <common/utils/resources.h> #include <common/utils/winapi_error.h> +#include <common/interop/shared_constants.h> #include <shellapi.h> #include <common/interop/shared_constants.h> diff --git a/src/modules/awake/Awake.ModuleServices/Awake.ModuleServices.csproj b/src/modules/awake/Awake.ModuleServices/Awake.ModuleServices.csproj new file mode 100644 index 0000000000..52588938e4 --- /dev/null +++ b/src/modules/awake/Awake.ModuleServices/Awake.ModuleServices.csproj @@ -0,0 +1,20 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <!-- Look at Directory.Build.props in root for common stuff as well --> + <Import Project="..\..\..\Common.Dotnet.CsWinRT.props" /> + <Import Project="..\..\..\Common.Dotnet.AotCompatibility.props" /> + + <PropertyGroup> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> + <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\..\..\common\ManagedCommon\ManagedCommon.csproj" /> + <ProjectReference Include="..\..\..\common\interop\PowerToys.Interop.vcxproj" /> + <ProjectReference Include="..\..\..\common\Common.UI\Common.UI.csproj" /> + <ProjectReference Include="..\..\..\common\PowerToys.ModuleContracts\PowerToys.ModuleContracts.csproj" /> + <ProjectReference Include="..\..\..\settings-ui\Settings.UI.Library\Settings.UI.Library.csproj" /> + </ItemGroup> +</Project> diff --git a/src/modules/awake/Awake.ModuleServices/AwakeService.cs b/src/modules/awake/Awake.ModuleServices/AwakeService.cs new file mode 100644 index 0000000000..f783cdc3db --- /dev/null +++ b/src/modules/awake/Awake.ModuleServices/AwakeService.cs @@ -0,0 +1,151 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; +using System.Text.Json; +using Common.UI; +using Microsoft.PowerToys.Settings.UI.Library; +using PowerToys.ModuleContracts; + +namespace Awake.ModuleServices; + +/// <summary> +/// Provides CLI-based Awake control for reuse across hosts. +/// </summary> +public sealed class AwakeService : ModuleServiceBase, IAwakeService +{ + public static AwakeService Instance { get; } = new(); + + public override string Key => SettingsDeepLink.SettingsWindow.Awake.ToString(); + + protected override SettingsDeepLink.SettingsWindow SettingsWindow => SettingsDeepLink.SettingsWindow.Awake; + + public override Task<OperationResult> LaunchAsync(CancellationToken cancellationToken = default) + { + // Default launch -> indefinite, honoring Awake's own settings for display behavior. + return SetIndefiniteAsync(cancellationToken); + } + + public AwakeState GetCurrentState() + { + var isRunning = IsAwakeProcessRunning(); + var settings = ReadSettings(); + + if (settings is null) + { + return new AwakeState(isRunning, AwakeStateMode.Passive, false, null, null); + } + + var mode = settings.Properties.Mode switch + { + AwakeMode.PASSIVE => AwakeStateMode.Passive, + AwakeMode.INDEFINITE => AwakeStateMode.Indefinite, + AwakeMode.TIMED => AwakeStateMode.Timed, + AwakeMode.EXPIRABLE => AwakeStateMode.Expirable, + _ => AwakeStateMode.Passive, + }; + + TimeSpan? duration = null; + DateTimeOffset? expiration = null; + + switch (mode) + { + case AwakeStateMode.Timed: + duration = TimeSpan.FromHours(settings.Properties.IntervalHours) + TimeSpan.FromMinutes(settings.Properties.IntervalMinutes); + break; + case AwakeStateMode.Expirable: + expiration = settings.Properties.ExpirationDateTime; + break; + } + + return new AwakeState(isRunning, mode, settings.Properties.KeepDisplayOn, duration, expiration); + } + + public Task<OperationResult> SetIndefiniteAsync(CancellationToken cancellationToken = default) + { + return UpdateSettingsAsync( + settings => + { + settings.Properties.Mode = AwakeMode.INDEFINITE; + }, + cancellationToken); + } + + public Task<OperationResult> SetTimedAsync(int minutes, CancellationToken cancellationToken = default) + { + if (minutes <= 0) + { + return Task.FromResult(OperationResult.Fail("Minutes must be greater than zero.")); + } + + return UpdateSettingsAsync( + settings => + { + var totalMinutes = Math.Min(minutes, int.MaxValue); + settings.Properties.Mode = AwakeMode.TIMED; + settings.Properties.IntervalHours = (uint)(totalMinutes / 60); + settings.Properties.IntervalMinutes = (uint)(totalMinutes % 60); + }, + cancellationToken); + } + + public Task<OperationResult> SetOffAsync(CancellationToken cancellationToken = default) + { + return UpdateSettingsAsync( + settings => + { + settings.Properties.Mode = AwakeMode.PASSIVE; + }, + cancellationToken); + } + + private static Task<OperationResult> UpdateSettingsAsync(Action<AwakeSettings> mutateSettings, CancellationToken cancellationToken) + { + try + { + cancellationToken.ThrowIfCancellationRequested(); + + var settingsUtils = SettingsUtils.Default; + var settings = settingsUtils.GetSettingsOrDefault<AwakeSettings>(AwakeSettings.ModuleName); + + mutateSettings(settings); + + settingsUtils.SaveSettings(JsonSerializer.Serialize(settings, AwakeServiceJsonContext.Default.AwakeSettings), AwakeSettings.ModuleName); + return Task.FromResult(OperationResult.Ok()); + } + catch (OperationCanceledException) + { + return Task.FromResult(OperationResult.Fail("Awake update was cancelled.")); + } + catch (Exception ex) + { + return Task.FromResult(OperationResult.Fail($"Failed to update Awake settings: {ex.Message}")); + } + } + + private static bool IsAwakeProcessRunning() + { + try + { + return Process.GetProcessesByName("PowerToys.Awake").Length > 0; + } + catch + { + return false; + } + } + + private static AwakeSettings? ReadSettings() + { + try + { + var settingsUtils = SettingsUtils.Default; + return settingsUtils.GetSettingsOrDefault<AwakeSettings>(AwakeSettings.ModuleName); + } + catch + { + return null; + } + } +} diff --git a/src/modules/awake/Awake.ModuleServices/AwakeServiceJsonContext.cs b/src/modules/awake/Awake.ModuleServices/AwakeServiceJsonContext.cs new file mode 100644 index 0000000000..cfaeff5bed --- /dev/null +++ b/src/modules/awake/Awake.ModuleServices/AwakeServiceJsonContext.cs @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Library; + +namespace Awake.ModuleServices; + +[JsonSerializable(typeof(AwakeSettings))] +internal sealed partial class AwakeServiceJsonContext : JsonSerializerContext +{ +} diff --git a/src/modules/awake/Awake.ModuleServices/AwakeState.cs b/src/modules/awake/Awake.ModuleServices/AwakeState.cs new file mode 100644 index 0000000000..4a59291732 --- /dev/null +++ b/src/modules/awake/Awake.ModuleServices/AwakeState.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Awake.ModuleServices; + +/// <summary> +/// Represents the current state of the Awake module. +/// </summary> +/// <param name="IsRunning">Whether the Awake process is currently running.</param> +/// <param name="Mode">The current Awake mode (Passive, Indefinite, Timed, Expirable).</param> +/// <param name="KeepDisplayOn">Whether the display is kept on.</param> +/// <param name="Duration">For timed mode, the configured duration.</param> +/// <param name="Expiration">For expirable mode, the expiration date/time.</param> +public readonly record struct AwakeState(bool IsRunning, AwakeStateMode Mode, bool KeepDisplayOn, TimeSpan? Duration, DateTimeOffset? Expiration); + +/// <summary> +/// The mode of the Awake module. +/// </summary> +public enum AwakeStateMode +{ + Passive = 0, + Indefinite = 1, + Timed = 2, + Expirable = 3, +} diff --git a/src/modules/awake/Awake.ModuleServices/IAwakeService.cs b/src/modules/awake/Awake.ModuleServices/IAwakeService.cs new file mode 100644 index 0000000000..1e600f52fe --- /dev/null +++ b/src/modules/awake/Awake.ModuleServices/IAwakeService.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using PowerToys.ModuleContracts; + +namespace Awake.ModuleServices; + +public interface IAwakeService : IModuleService +{ + Task<OperationResult> SetIndefiniteAsync(CancellationToken cancellationToken = default); + + Task<OperationResult> SetTimedAsync(int minutes, CancellationToken cancellationToken = default); + + Task<OperationResult> SetOffAsync(CancellationToken cancellationToken = default); + + /// <summary> + /// Gets the current state of the Awake module. + /// </summary> + AwakeState GetCurrentState(); +} diff --git a/src/modules/cmdpal/CommandPalette.slnf b/src/modules/cmdpal/CommandPalette.slnf new file mode 100644 index 0000000000..aa8f1165d9 --- /dev/null +++ b/src/modules/cmdpal/CommandPalette.slnf @@ -0,0 +1,54 @@ +{ + "solution": { + "path": "..\\..\\..\\PowerToys.slnx", + "projects": [ + "src\\common\\CalculatorEngineCommon\\CalculatorEngineCommon.vcxproj", + "src\\common\\ManagedCommon\\ManagedCommon.csproj", + "src\\common\\ManagedCsWin32\\ManagedCsWin32.csproj", + "src\\common\\ManagedTelemetry\\Telemetry\\ManagedTelemetry.csproj", + "src\\common\\interop\\PowerToys.Interop.vcxproj", + "src\\common\\version\\version.vcxproj", + "src\\modules\\cmdpal\\CmdPalKeyboardService\\CmdPalKeyboardService.vcxproj", + "src\\modules\\cmdpal\\CmdPalModuleInterface\\CmdPalModuleInterface.vcxproj", + "src\\modules\\cmdpal\\Core\\Microsoft.CmdPal.Core.Common\\Microsoft.CmdPal.Core.Common.csproj", + "src\\modules\\cmdpal\\Core\\Microsoft.CmdPal.Core.ViewModels\\Microsoft.CmdPal.Core.ViewModels.csproj", + "src\\modules\\cmdpal\\Microsoft.CmdPal.UI.ViewModels\\Microsoft.CmdPal.UI.ViewModels.csproj", + "src\\modules\\cmdpal\\Microsoft.CmdPal.UI\\Microsoft.CmdPal.UI.csproj", + "src\\modules\\cmdpal\\Microsoft.Terminal.UI\\Microsoft.Terminal.UI.vcxproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.Apps.UnitTests\\Microsoft.CmdPal.Ext.Apps.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.Bookmarks.UnitTests\\Microsoft.CmdPal.Ext.Bookmarks.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.Calc.UnitTests\\Microsoft.CmdPal.Ext.Calc.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.ClipboardHistory.UnitTests\\Microsoft.CmdPal.Ext.ClipboardHistory.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.Registry.UnitTests\\Microsoft.CmdPal.Ext.Registry.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.RemoteDesktop.UnitTests\\Microsoft.CmdPal.Ext.RemoteDesktop.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.Shell.UnitTests\\Microsoft.CmdPal.Ext.Shell.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.System.UnitTests\\Microsoft.CmdPal.Ext.System.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.TimeDate.UnitTests\\Microsoft.CmdPal.Ext.TimeDate.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.UnitTestsBase\\Microsoft.CmdPal.Ext.UnitTestBase.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.WebSearch.UnitTests\\Microsoft.CmdPal.Ext.WebSearch.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.Ext.WindowWalker.UnitTests\\Microsoft.CmdPal.Ext.WindowWalker.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.UI.ViewModels.UnitTests\\Microsoft.CmdPal.UI.ViewModels.UnitTests.csproj", + "src\\modules\\cmdpal\\Tests\\Microsoft.CmdPal.UITests\\Microsoft.CmdPal.UITests.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.Apps\\Microsoft.CmdPal.Ext.Apps.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.Bookmark\\Microsoft.CmdPal.Ext.Bookmarks.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.Calc\\Microsoft.CmdPal.Ext.Calc.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.ClipboardHistory\\Microsoft.CmdPal.Ext.ClipboardHistory.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.Indexer\\Microsoft.CmdPal.Ext.Indexer.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.Registry\\Microsoft.CmdPal.Ext.Registry.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.RemoteDesktop\\Microsoft.CmdPal.Ext.RemoteDesktop.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.Shell\\Microsoft.CmdPal.Ext.Shell.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.System\\Microsoft.CmdPal.Ext.System.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.TimeDate\\Microsoft.CmdPal.Ext.TimeDate.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.WebSearch\\Microsoft.CmdPal.Ext.WebSearch.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.WinGet\\Microsoft.CmdPal.Ext.WinGet.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.WindowWalker\\Microsoft.CmdPal.Ext.WindowWalker.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.WindowsServices\\Microsoft.CmdPal.Ext.WindowsServices.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.WindowsSettings\\Microsoft.CmdPal.Ext.WindowsSettings.csproj", + "src\\modules\\cmdpal\\ext\\Microsoft.CmdPal.Ext.WindowsTerminal\\Microsoft.CmdPal.Ext.WindowsTerminal.csproj", + "src\\modules\\cmdpal\\ext\\ProcessMonitorExtension\\ProcessMonitorExtension.csproj", + "src\\modules\\cmdpal\\ext\\SamplePagesExtension\\SamplePagesExtension.csproj", + "src\\modules\\cmdpal\\extensionsdk\\Microsoft.CommandPalette.Extensions.Toolkit\\Microsoft.CommandPalette.Extensions.Toolkit.csproj", + "src\\modules\\cmdpal\\extensionsdk\\Microsoft.CommandPalette.Extensions\\Microsoft.CommandPalette.Extensions.vcxproj" + ] + } +} \ No newline at end of file diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/CommandItemViewModel.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/CommandItemViewModel.cs index e0d2c38262..f8e9478023 100644 --- a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/CommandItemViewModel.cs +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/CommandItemViewModel.cs @@ -8,6 +8,7 @@ using Microsoft.CmdPal.Core.ViewModels.Messages; using Microsoft.CmdPal.Core.ViewModels.Models; using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions.Toolkit; +using Windows.ApplicationModel.DataTransfer; namespace Microsoft.CmdPal.Core.ViewModels; @@ -16,6 +17,8 @@ public partial class CommandItemViewModel : ExtensionObjectViewModel, ICommandBa { public ExtensionObject<ICommandItem> Model => _commandItemModel; + private ExtensionObject<IExtendedAttributesProvider>? ExtendedAttributesProvider { get; set; } + private readonly ExtensionObject<ICommandItem> _commandItemModel = new(null); private CommandContextItemViewModel? _defaultCommandContextItemViewModel; @@ -65,6 +68,8 @@ public partial class CommandItemViewModel : ExtensionObjectViewModel, ICommandBa public bool ShouldBeVisible => !string.IsNullOrEmpty(Name); + public DataPackageView? DataPackage { get; private set; } + public List<IContextItemViewModel> AllCommands { get @@ -157,6 +162,13 @@ public partial class CommandItemViewModel : ExtensionObjectViewModel, ICommandBa // will never be able to load Hotkeys & aliases UpdateProperty(nameof(IsInitialized)); + if (model is IExtendedAttributesProvider extendedAttributesProvider) + { + ExtendedAttributesProvider = new ExtensionObject<IExtendedAttributesProvider>(extendedAttributesProvider); + var properties = extendedAttributesProvider.GetProperties(); + UpdateDataPackage(properties); + } + Initialized |= InitializedState.Initialized; } @@ -379,6 +391,9 @@ public partial class CommandItemViewModel : ExtensionObjectViewModel, ICommandBa UpdateProperty(nameof(SecondaryCommandName)); UpdateProperty(nameof(HasMoreCommands)); + break; + case nameof(DataPackage): + UpdateDataPackage(ExtendedAttributesProvider?.Unsafe?.GetProperties()); break; } @@ -431,6 +446,16 @@ public partial class CommandItemViewModel : ExtensionObjectViewModel, ICommandBa UpdateProperty(nameof(Icon)); } + private void UpdateDataPackage(IDictionary<string, object?>? properties) + { + DataPackage = + properties?.TryGetValue(WellKnownExtensionAttributes.DataPackage, out var dataPackageView) == true && + dataPackageView is DataPackageView view + ? view + : null; + UpdateProperty(nameof(DataPackage)); + } + protected override void UnsafeCleanup() { base.UnsafeCleanup(); diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/IconDataViewModel.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/IconDataViewModel.cs index 76b5f786c8..4044800be6 100644 --- a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/IconDataViewModel.cs +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/IconDataViewModel.cs @@ -5,6 +5,7 @@ using CommunityToolkit.Mvvm.ComponentModel; using Microsoft.CmdPal.Core.ViewModels.Models; using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; using Windows.Storage.Streams; namespace Microsoft.CmdPal.Core.ViewModels; @@ -57,7 +58,7 @@ public partial class IconDataViewModel : ObservableObject, IIconData // because each call to GetProperties() is a cross process hop, and if you // marshal-by-value the property set, then you don't want to throw it away and // re-marshal it for every property. MAKE SURE YOU CACHE IT. - if (props?.TryGetValue("FontFamily", out var family) ?? false) + if (props?.TryGetValue(WellKnownExtensionAttributes.FontFamily, out var family) ?? false) { FontFamily = family as string; } diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/ErrorOccurredMessage.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/ErrorOccurredMessage.cs new file mode 100644 index 0000000000..bfd60de675 --- /dev/null +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/ErrorOccurredMessage.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.Core.ViewModels.Messages; + +/// <summary> +/// Message sent when an error occurs during command execution. +/// Used to track session error count for telemetry. +/// </summary> +public record ErrorOccurredMessage(); diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/ExtensionInvokedMessage.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/ExtensionInvokedMessage.cs new file mode 100644 index 0000000000..a8a2ee0055 --- /dev/null +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/ExtensionInvokedMessage.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.Core.ViewModels.Messages; + +/// <summary> +/// Message sent when an extension command or page is invoked. +/// Captures extension usage metrics for telemetry tracking. +/// </summary> +public record ExtensionInvokedMessage(string ExtensionId, string CommandId, string CommandName, bool Success, ulong ExecutionTimeMs); diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/NavigationDepthMessage.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/NavigationDepthMessage.cs new file mode 100644 index 0000000000..b916f28244 --- /dev/null +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/NavigationDepthMessage.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.Core.ViewModels.Messages; + +/// <summary> +/// Message containing the current navigation depth (BackStack count) when navigating to a page. +/// Used to track maximum navigation depth reached during a session for telemetry. +/// </summary> +public record NavigationDepthMessage(int Depth); diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/SearchQueryMessage.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/SearchQueryMessage.cs new file mode 100644 index 0000000000..7516af0b34 --- /dev/null +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/SearchQueryMessage.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.Core.ViewModels.Messages; + +/// <summary> +/// Message sent when a search query is executed in the Command Palette. +/// Used to track session search activity for telemetry. +/// </summary> +public record SearchQueryMessage(); diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/SessionDurationMessage.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/SessionDurationMessage.cs new file mode 100644 index 0000000000..4b77a1fd06 --- /dev/null +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/SessionDurationMessage.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.Core.ViewModels.Messages; + +/// <summary> +/// Message containing session telemetry data from Command Palette launch to dismissal. +/// Used to aggregate metrics like duration, commands executed, pages visited, and search activity. +/// </summary> +public record SessionDurationMessage(ulong DurationMs, int CommandsExecuted, int PagesVisited, string DismissalReason, int SearchQueriesCount, int MaxNavigationDepth, int ErrorCount); diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/TelemetryBeginInvokeMessage.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/TelemetryBeginInvokeMessage.cs new file mode 100644 index 0000000000..87a1ae8aef --- /dev/null +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/TelemetryBeginInvokeMessage.cs @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.Core.ViewModels.Messages; + +/// <summary> +/// Telemetry message sent when command invocation begins. +/// </summary> +public record TelemetryBeginInvokeMessage; diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/TelemetryExtensionInvokedMessage.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/TelemetryExtensionInvokedMessage.cs new file mode 100644 index 0000000000..464d5ae696 --- /dev/null +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/TelemetryExtensionInvokedMessage.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.Core.ViewModels.Messages; + +/// <summary> +/// Telemetry message sent when an extension command or page is invoked. +/// Captures extension usage metrics for telemetry tracking. +/// </summary> +public record TelemetryExtensionInvokedMessage(string ExtensionId, string CommandId, string CommandName, bool Success, ulong ExecutionTimeMs); diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/TelemetryInvokeResultMessage.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/TelemetryInvokeResultMessage.cs new file mode 100644 index 0000000000..06e0b4fd53 --- /dev/null +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/Messages/TelemetryInvokeResultMessage.cs @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.Core.ViewModels.Messages; + +/// <summary> +/// Telemetry message sent when command invocation completes with a result. +/// </summary> +public record TelemetryInvokeResultMessage(Microsoft.CommandPalette.Extensions.CommandResultKind Kind); diff --git a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ShellViewModel.cs b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ShellViewModel.cs index 16ca5b1fca..62c70076ad 100644 --- a/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ShellViewModel.cs +++ b/src/modules/cmdpal/Core/Microsoft.CmdPal.Core.ViewModels/ShellViewModel.cs @@ -270,8 +270,18 @@ public partial class ShellViewModel : ObservableObject, var isMainPage = command == _rootPage; _isNested = !isMainPage; + // Telemetry: Track extension page navigation for session metrics + if (host is not null) + { + string extensionId = host.GetExtensionDisplayName() ?? "builtin"; + string commandId = command?.Id ?? "unknown"; + string commandName = command?.Name ?? "unknown"; + WeakReferenceMessenger.Default.Send<TelemetryExtensionInvokedMessage>( + new(extensionId, commandId, commandName, true, 0)); + } + // Construct our ViewModel of the appropriate type and pass it the UI Thread context. - var pageViewModel = _pageViewModelFactory.TryCreatePageViewModel(page, _isNested, host); + var pageViewModel = _pageViewModelFactory.TryCreatePageViewModel(page, _isNested, host!); if (pageViewModel is null) { CoreLogger.LogError($"Failed to create ViewModel for page {page.GetType().Name}"); @@ -306,7 +316,7 @@ public partial class ShellViewModel : ObservableObject, { CoreLogger.LogDebug($"Invoking command"); - WeakReferenceMessenger.Default.Send<BeginInvokeMessage>(); + WeakReferenceMessenger.Default.Send<TelemetryBeginInvokeMessage>(); StartInvoke(message, invokable, host); } } @@ -339,6 +349,14 @@ public partial class ShellViewModel : ObservableObject, private void SafeHandleInvokeCommandSynchronous(PerformCommandMessage message, IInvokableCommand invokable, AppExtensionHost? host) { + // Telemetry: Track command execution time and success + var stopwatch = System.Diagnostics.Stopwatch.StartNew(); + var command = message.Command.Unsafe; + string extensionId = host?.GetExtensionDisplayName() ?? "builtin"; + string commandId = command?.Id ?? "unknown"; + string commandName = command?.Name ?? "unknown"; + bool success = false; + try { // Call out to extension process. @@ -349,16 +367,28 @@ public partial class ShellViewModel : ObservableObject, // But if it did succeed, we need to handle the result. UnsafeHandleCommandResult(result); + success = true; _handleInvokeTask = null; } catch (Exception ex) { + success = false; _handleInvokeTask = null; + // Telemetry: Track errors for session metrics + WeakReferenceMessenger.Default.Send<ErrorOccurredMessage>(new()); + // TODO: It would be better to do this as a page exception, rather // than a silent log message. host?.Log(ex.Message); } + finally + { + // Telemetry: Send extension invocation metrics (always sent, even on failure) + stopwatch.Stop(); + WeakReferenceMessenger.Default.Send<TelemetryExtensionInvokedMessage>( + new(extensionId, commandId, commandName, success, (ulong)stopwatch.ElapsedMilliseconds)); + } } private void UnsafeHandleCommandResult(ICommandResult? result) @@ -372,7 +402,7 @@ public partial class ShellViewModel : ObservableObject, var kind = result.Kind; CoreLogger.LogDebug($"handling {kind.ToString()}"); - WeakReferenceMessenger.Default.Send<CmdPalInvokeResultMessage>(new(kind)); + WeakReferenceMessenger.Default.Send<TelemetryInvokeResultMessage>(new(kind)); switch (kind) { case CommandResultKind.Dismiss: diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/CommandPaletteHost.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/CommandPaletteHost.cs index da0972de8e..af089b3edc 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/CommandPaletteHost.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/CommandPaletteHost.cs @@ -34,6 +34,6 @@ public sealed partial class CommandPaletteHost : AppExtensionHost, IExtensionHos public override string? GetExtensionDisplayName() { - return Extension?.ExtensionDisplayName; + return Extension?.ExtensionDisplayName ?? _builtInProvider?.DisplayName ?? _builtInProvider?.Id; } } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/CommandProviderWrapper.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/CommandProviderWrapper.cs index c59c90814b..939b42de14 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/CommandProviderWrapper.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/CommandProviderWrapper.cs @@ -188,11 +188,12 @@ public sealed class CommandProviderWrapper Func<ICommandItem?, bool, TopLevelViewModel> makeAndAdd = (ICommandItem? i, bool fallback) => { CommandItemViewModel commandItemViewModel = new(new(i), pageContext); - TopLevelViewModel topLevelViewModel = new(commandItemViewModel, fallback, ExtensionHost, ProviderId, settings, providerSettings, serviceProvider); + TopLevelViewModel topLevelViewModel = new(commandItemViewModel, fallback, ExtensionHost, ProviderId, settings, providerSettings, serviceProvider, i); topLevelViewModel.InitializeProperties(); return topLevelViewModel; }; + if (commands is not null) { TopLevelItems = commands diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/BuiltInsCommandProvider.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/BuiltInsCommandProvider.cs index 55581e65f3..0167f6f7a1 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/BuiltInsCommandProvider.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/BuiltInsCommandProvider.cs @@ -2,7 +2,6 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using Microsoft.CmdPal.Core.ViewModels.Messages; using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions.Toolkit; @@ -27,7 +26,13 @@ public sealed partial class BuiltInsCommandProvider : CommandProvider public override IFallbackCommandItem[] FallbackCommands() => [ - new FallbackCommandItem(quitCommand, displayTitle: Properties.Resources.builtin_quit_subtitle) { Subtitle = Properties.Resources.builtin_quit_subtitle }, + new FallbackCommandItem( + quitCommand, + Properties.Resources.builtin_quit_subtitle, + quitCommand.Id) + { + Subtitle = Properties.Resources.builtin_quit_subtitle, + }, _fallbackReloadItem, _fallbackLogItem, ]; diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackLogItem.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackLogItem.cs index a96d49ff79..2f44b018e1 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackLogItem.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackLogItem.cs @@ -13,8 +13,10 @@ internal sealed partial class FallbackLogItem : FallbackCommandItem { private readonly LogMessagesPage _logMessagesPage; + private const string _id = "com.microsoft.cmdpal.log"; + public FallbackLogItem() - : base(new LogMessagesPage() { Id = "com.microsoft.cmdpal.log" }, Resources.builtin_log_subtitle) + : base(new LogMessagesPage() { Id = _id }, Resources.builtin_log_subtitle, _id) { _logMessagesPage = (LogMessagesPage)Command!; Title = string.Empty; diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackReloadItem.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackReloadItem.cs index 37a37d9283..489c73a537 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackReloadItem.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/FallbackReloadItem.cs @@ -2,7 +2,6 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using Microsoft.CommandPalette.Extensions.Toolkit; namespace Microsoft.CmdPal.UI.ViewModels.BuiltinCommands; @@ -11,10 +10,13 @@ internal sealed partial class FallbackReloadItem : FallbackCommandItem { private readonly ReloadExtensionsCommand _reloadCommand; + private const string _id = "com.microsoft.cmdpal.reload"; + public FallbackReloadItem() : base( - new ReloadExtensionsCommand() { Id = "com.microsoft.cmdpal.reload" }, - Properties.Resources.builtin_reload_display_title) + new ReloadExtensionsCommand() { Id = _id }, + Properties.Resources.builtin_reload_display_title, + _id) { _reloadCommand = (ReloadExtensionsCommand)Command!; Title = string.Empty; diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs index 4118ac64db..2489cd0817 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs @@ -17,7 +17,6 @@ using Microsoft.CmdPal.UI.ViewModels.Messages; using Microsoft.CmdPal.UI.ViewModels.Properties; using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions.Toolkit; -using Microsoft.Extensions.DependencyInjection; namespace Microsoft.CmdPal.UI.ViewModels.MainPage; @@ -29,26 +28,17 @@ public partial class MainListPage : DynamicListPage, IRecipient<ClearSearchMessage>, IRecipient<UpdateFallbackItemsMessage>, IDisposable { - private readonly string[] _specialFallbacks = [ - "com.microsoft.cmdpal.builtin.run", - "com.microsoft.cmdpal.builtin.calculator", - "com.microsoft.cmdpal.builtin.system", - "com.microsoft.cmdpal.builtin.core", - "com.microsoft.cmdpal.builtin.websearch", - "com.microsoft.cmdpal.builtin.windowssettings", - "com.microsoft.cmdpal.builtin.datetime", - "com.microsoft.cmdpal.builtin.remotedesktop", - ]; - - private readonly IServiceProvider _serviceProvider; private readonly TopLevelCommandManager _tlcManager; + private readonly AliasManager _aliasManager; + private readonly SettingsModel _settings; + private readonly AppStateModel _appStateModel; private List<Scored<IListItem>>? _filteredItems; private List<Scored<IListItem>>? _filteredApps; - private List<Scored<IListItem>>? _fallbackItems; // Keep as IEnumerable for deferred execution. Fallback item titles are updated // asynchronously, so scoring must happen lazily when GetItems is called. private IEnumerable<Scored<IListItem>>? _scoredFallbackItems; + private IEnumerable<Scored<IListItem>>? _fallbackItems; private bool _includeApps; private bool _filteredItemsIncludesApps; private int _appResultLimit = 10; @@ -58,14 +48,16 @@ public partial class MainListPage : DynamicListPage, private CancellationTokenSource? _cancellationTokenSource; - public MainListPage(IServiceProvider serviceProvider) + public MainListPage(TopLevelCommandManager topLevelCommandManager, SettingsModel settings, AliasManager aliasManager, AppStateModel appStateModel) { Title = Resources.builtin_home_name; Icon = IconHelpers.FromRelativePath("Assets\\StoreLogo.scale-200.png"); PlaceholderText = Properties.Resources.builtin_main_list_page_searchbar_placeholder; - _serviceProvider = serviceProvider; - _tlcManager = _serviceProvider.GetService<TopLevelCommandManager>()!; + _settings = settings; + _aliasManager = aliasManager; + _appStateModel = appStateModel; + _tlcManager = topLevelCommandManager; _tlcManager.PropertyChanged += TlcManager_PropertyChanged; _tlcManager.TopLevelCommands.CollectionChanged += Commands_CollectionChanged; @@ -83,7 +75,6 @@ public partial class MainListPage : DynamicListPage, WeakReferenceMessenger.Default.Register<ClearSearchMessage>(this); WeakReferenceMessenger.Default.Register<UpdateFallbackItemsMessage>(this); - var settings = _serviceProvider.GetService<SettingsModel>()!; settings.SettingsChanged += SettingsChangedHandler; HotReloadSettings(settings); _includeApps = _tlcManager.IsProviderActive(AllAppsCommandProvider.WellKnownId); @@ -163,14 +154,29 @@ public partial class MainListPage : DynamicListPage, { // Either return the top-level commands (no search text), or the merged and // filtered results. - return string.IsNullOrEmpty(SearchText) - ? _tlcManager.TopLevelCommands.Where(tlc => !tlc.IsFallback && !string.IsNullOrEmpty(tlc.Title)).ToArray() - : MainListPageResultFactory.Create( + if (string.IsNullOrWhiteSpace(SearchText)) + { + return _tlcManager.TopLevelCommands + .Where(tlc => !tlc.IsFallback && !string.IsNullOrEmpty(tlc.Title)) + .ToArray(); + } + else + { + var validScoredFallbacks = _scoredFallbackItems? + .Where(s => !string.IsNullOrWhiteSpace(s.Item.Title)) + .ToList(); + + var validFallbacks = _fallbackItems? + .Where(s => !string.IsNullOrWhiteSpace(s.Item.Title)) + .ToList(); + + return MainListPageResultFactory.Create( _filteredItems, - _scoredFallbackItems?.ToList(), + validScoredFallbacks, _filteredApps, - _fallbackItems, + validFallbacks, _appResultLimit); + } } } @@ -200,7 +206,7 @@ public partial class MainListPage : DynamicListPage, // Handle changes to the filter text here if (!string.IsNullOrEmpty(SearchText)) { - var aliases = _serviceProvider.GetService<AliasManager>()!; + var aliases = _aliasManager; if (token.IsCancellationRequested) { @@ -236,7 +242,8 @@ public partial class MainListPage : DynamicListPage, } // prefilter fallbacks - var specialFallbacks = new List<TopLevelViewModel>(_specialFallbacks.Length); + var globalFallbacks = _settings.GetGlobalFallbacks(); + var specialFallbacks = new List<TopLevelViewModel>(globalFallbacks.Length); var commonFallbacks = new List<TopLevelViewModel>(); foreach (var s in commands) @@ -246,7 +253,7 @@ public partial class MainListPage : DynamicListPage, continue; } - if (_specialFallbacks.Contains(s.CommandProviderId)) + if (globalFallbacks.Contains(s.Id)) { specialFallbacks.Add(s); } @@ -369,7 +376,7 @@ public partial class MainListPage : DynamicListPage, } } - var history = _serviceProvider.GetService<AppStateModel>()!.RecentCommands!; + var history = _appStateModel.RecentCommands!; Func<string, IListItem, int> scoreItem = (a, b) => { return ScoreTopLevelItem(a, b, history); }; // Produce a list of everything that matches the current filter. @@ -380,7 +387,7 @@ public partial class MainListPage : DynamicListPage, return; } - IEnumerable<IListItem> newFallbacksForScoring = commands.Where(s => s.IsFallback && _specialFallbacks.Contains(s.CommandProviderId)); + IEnumerable<IListItem> newFallbacksForScoring = commands.Where(s => s.IsFallback && globalFallbacks.Contains(s.Id)); if (token.IsCancellationRequested) { @@ -394,8 +401,8 @@ public partial class MainListPage : DynamicListPage, return; } - // Defaulting scored to 1 but we'll eventually use user rankings - _fallbackItems = [.. newFallbacks.Select(f => new Scored<IListItem> { Item = f, Score = 1 })]; + Func<string, IListItem, int> scoreFallbackItem = (a, b) => { return ScoreFallbackItem(a, b, _settings.FallbackRanks); }; + _fallbackItems = [.. ListHelpers.FilterListWithScores<IListItem>(newFallbacks ?? [], SearchText, scoreFallbackItem)]; if (token.IsCancellationRequested) { @@ -464,9 +471,8 @@ public partial class MainListPage : DynamicListPage, private bool ActuallyLoading() { - var tlcManager = _serviceProvider.GetService<TopLevelCommandManager>()!; var allApps = AllAppsCommandProvider.Page; - return allApps.IsLoading || tlcManager.IsLoading; + return allApps.IsLoading || _tlcManager.IsLoading; } // Almost verbatim ListHelpers.ScoreListItem, but also accounting for the @@ -558,13 +564,30 @@ public partial class MainListPage : DynamicListPage, return (int)finalScore; } + internal static int ScoreFallbackItem(string query, IListItem topLevelOrAppItem, string[] fallbackRanks) + { + // Default to 1 so it always shows in list. + var finalScore = 1; + + if (topLevelOrAppItem is TopLevelViewModel topLevelViewModel) + { + var index = Array.IndexOf(fallbackRanks, topLevelViewModel.Id); + + if (index >= 0) + { + finalScore = fallbackRanks.Length - index + 1; + } + } + + return finalScore; + } + public void UpdateHistory(IListItem topLevelOrAppItem) { var id = IdForTopLevelOrAppItem(topLevelOrAppItem); - var state = _serviceProvider.GetService<AppStateModel>()!; - var history = state.RecentCommands; + var history = _appStateModel.RecentCommands; history.AddHistoryItem(id); - AppStateModel.SaveState(state); + AppStateModel.SaveState(_appStateModel); } private static string IdForTopLevelOrAppItem(IListItem topLevelOrAppItem) @@ -596,10 +619,9 @@ public partial class MainListPage : DynamicListPage, _tlcManager.PropertyChanged -= TlcManager_PropertyChanged; _tlcManager.TopLevelCommands.CollectionChanged -= Commands_CollectionChanged; - var settings = _serviceProvider.GetService<SettingsModel>(); - if (settings is not null) + if (_settings is not null) { - settings.SettingsChanged -= SettingsChangedHandler; + _settings.SettingsChanged -= SettingsChangedHandler; } WeakReferenceMessenger.Default.UnregisterAll(this); diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPageResultFactory.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPageResultFactory.cs index f1bddf5197..d63c0e4f90 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPageResultFactory.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPageResultFactory.cs @@ -29,13 +29,19 @@ internal static class MainListPageResultFactory } int len1 = filteredItems?.Count ?? 0; + + // Empty fallbacks are removed prior to this merge. int len2 = scoredFallbackItems?.Count ?? 0; // Apps are pre-sorted, so we just need to take the top N, limited by appResultLimit. int len3 = Math.Min(filteredApps?.Count ?? 0, appResultLimit); + int nonEmptyFallbackCount = fallbackItems?.Count ?? 0; + // Allocate the exact size of the result array. - int totalCount = len1 + len2 + len3 + GetNonEmptyFallbackItemsCount(fallbackItems); + // We'll add an extra slot for the fallbacks section header if needed. + int totalCount = len1 + len2 + len3 + nonEmptyFallbackCount + (nonEmptyFallbackCount > 0 ? 1 : 0); + var result = new IListItem[totalCount]; // Three-way stable merge of already-sorted lists. @@ -119,9 +125,15 @@ internal static class MainListPageResultFactory } // Append filtered fallback items. Fallback items are added post-sort so they are - // always at the end of the list and eventually ordered based on user preference. + // always at the end of the list and are sorted by user settings. if (fallbackItems is not null) { + // Create the fallbacks section header + if (fallbackItems.Count > 0) + { + result[writePos++] = new Separator(Properties.Resources.fallbacks); + } + for (int i = 0; i < fallbackItems.Count; i++) { var item = fallbackItems[i].Item; @@ -143,7 +155,7 @@ internal static class MainListPageResultFactory { for (int i = 0; i < fallbackItems.Count; i++) { - if (!string.IsNullOrEmpty(fallbackItems[i].Item.Title)) + if (!string.IsNullOrWhiteSpace(fallbackItems[i].Item.Title)) { fallbackItemsCount++; } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FallbackSettings.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FallbackSettings.cs new file mode 100644 index 0000000000..38b76957c3 --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FallbackSettings.cs @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Text.Json.Serialization; + +namespace Microsoft.CmdPal.UI.ViewModels; + +public class FallbackSettings +{ + public bool IsEnabled { get; set; } = true; + + public bool IncludeInGlobalResults { get; set; } + + public FallbackSettings() + { + } + + public FallbackSettings(bool isBuiltIn) + { + IncludeInGlobalResults = isBuiltIn; + } + + [JsonConstructor] + public FallbackSettings(bool isEnabled, bool includeInGlobalResults) + { + IsEnabled = isEnabled; + IncludeInGlobalResults = includeInGlobalResults; + } +} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FallbackSettingsViewModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FallbackSettingsViewModel.cs new file mode 100644 index 0000000000..fbba4ce3f4 --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FallbackSettingsViewModel.cs @@ -0,0 +1,86 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Messaging; +using Microsoft.CmdPal.Core.ViewModels; +using Microsoft.CmdPal.UI.ViewModels.Messages; + +namespace Microsoft.CmdPal.UI.ViewModels; + +public partial class FallbackSettingsViewModel : ObservableObject +{ + private readonly SettingsModel _settings; + private readonly FallbackSettings _fallbackSettings; + + public string DisplayName { get; private set; } = string.Empty; + + public IconInfoViewModel Icon { get; private set; } = new(null); + + public string Id { get; private set; } = string.Empty; + + public bool IsEnabled + { + get => _fallbackSettings.IsEnabled; + set + { + if (value != _fallbackSettings.IsEnabled) + { + _fallbackSettings.IsEnabled = value; + + if (!_fallbackSettings.IsEnabled) + { + _fallbackSettings.IncludeInGlobalResults = false; + } + + Save(); + OnPropertyChanged(nameof(IsEnabled)); + } + } + } + + public bool IncludeInGlobalResults + { + get => _fallbackSettings.IncludeInGlobalResults; + set + { + if (value != _fallbackSettings.IncludeInGlobalResults) + { + _fallbackSettings.IncludeInGlobalResults = value; + + if (!_fallbackSettings.IsEnabled) + { + _fallbackSettings.IsEnabled = true; + } + + Save(); + OnPropertyChanged(nameof(IncludeInGlobalResults)); + } + } + } + + public FallbackSettingsViewModel( + TopLevelViewModel fallback, + FallbackSettings fallbackSettings, + SettingsModel settingsModel, + ProviderSettingsViewModel providerSettings) + { + _settings = settingsModel; + _fallbackSettings = fallbackSettings; + + Id = fallback.Id; + DisplayName = string.IsNullOrWhiteSpace(fallback.DisplayTitle) + ? (string.IsNullOrWhiteSpace(fallback.Title) ? providerSettings.DisplayName : fallback.Title) + : fallback.DisplayTitle; + + Icon = new(fallback.InitialIcon); + Icon.InitializeProperties(); + } + + private void Save() + { + SettingsModel.SaveSettings(_settings); + WeakReferenceMessenger.Default.Send<ReloadCommandsMessage>(new()); + } +} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.Designer.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.Designer.cs index 8bc2a42a92..8d9ff6d6bb 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.Designer.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.Designer.cs @@ -205,7 +205,7 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties { } /// <summary> - /// Looks up a localized string similar to Create a new extension. + /// Looks up a localized string similar to Create extension. /// </summary> public static string builtin_create_extension_title { get { @@ -349,7 +349,7 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties { } /// <summary> - /// Looks up a localized string similar to Creates a project for a new Command Palette extension. + /// Looks up a localized string similar to Generate a new Command Palette extension project. /// </summary> public static string builtin_new_extension_subtitle { get { @@ -358,7 +358,7 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties { } /// <summary> - /// Looks up a localized string similar to Open Settings. + /// Looks up a localized string similar to Open Command Palette settings. /// </summary> public static string builtin_open_settings_name { get { @@ -366,15 +366,6 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties { } } - /// <summary> - /// Looks up a localized string similar to Open Command Palette settings. - /// </summary> - public static string builtin_open_settings_subtitle { - get { - return ResourceManager.GetString("builtin_open_settings_subtitle", resourceCulture); - } - } - /// <summary> /// Looks up a localized string similar to Exit Command Palette. /// </summary> @@ -437,5 +428,14 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties { return ResourceManager.GetString("builtin_settings_extension_n_extensions_installed", resourceCulture); } } + + /// <summary> + /// Looks up a localized string similar to Fallbacks. + /// </summary> + public static string fallbacks { + get { + return ResourceManager.GetString("fallbacks", resourceCulture); + } + } } } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.resx b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.resx index bb7637e133..9f6b68c1bd 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.resx +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Properties/Resources.resx @@ -242,4 +242,7 @@ <data name="builtin_settings_appearance_pick_background_image_title" xml:space="preserve"> <value>Pick background image</value> </data> + <data name="fallbacks" xml:space="preserve"> + <value>Fallbacks</value> + </data> </root> \ No newline at end of file diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ProviderSettings.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ProviderSettings.cs index 1e20040d57..3bb9a43360 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ProviderSettings.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ProviderSettings.cs @@ -8,9 +8,15 @@ namespace Microsoft.CmdPal.UI.ViewModels; public class ProviderSettings { + // List of built-in fallbacks that should not have global results enabled by default + private readonly string[] _excludedBuiltInFallbacks = [ + "com.microsoft.cmdpal.builtin.indexer.fallback", + "com.microsoft.cmdpal.builtin.calculator.fallback", + ]; + public bool IsEnabled { get; set; } = true; - public Dictionary<string, bool> FallbackCommands { get; set; } = []; + public Dictionary<string, FallbackSettings> FallbackCommands { get; set; } = new(); [JsonIgnore] public string ProviderDisplayName { get; set; } = string.Empty; @@ -39,19 +45,21 @@ public class ProviderSettings ProviderDisplayName = wrapper.DisplayName; + if (wrapper.FallbackItems.Length > 0) + { + foreach (var fallback in wrapper.FallbackItems) + { + if (!FallbackCommands.ContainsKey(fallback.Id)) + { + var enableGlobalResults = IsBuiltin && !_excludedBuiltInFallbacks.Contains(fallback.Id); + FallbackCommands[fallback.Id] = new FallbackSettings(enableGlobalResults); + } + } + } + if (string.IsNullOrEmpty(ProviderId)) { throw new InvalidDataException("Did you add a built-in command and forget to set the Id? Make sure you do that!"); } } - - public bool IsFallbackEnabled(TopLevelViewModel command) - { - return FallbackCommands.TryGetValue(command.Id, out var enabled) ? enabled : true; - } - - public void SetFallbackEnabled(TopLevelViewModel command, bool enabled) - { - FallbackCommands[command.Id] = enabled; - } } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ProviderSettingsViewModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ProviderSettingsViewModel.cs index 52b2bea003..68e554e463 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ProviderSettingsViewModel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ProviderSettingsViewModel.cs @@ -9,26 +9,39 @@ using Microsoft.CmdPal.Core.Common.Services; using Microsoft.CmdPal.Core.ViewModels; using Microsoft.CmdPal.UI.ViewModels.Messages; using Microsoft.CmdPal.UI.ViewModels.Properties; -using Microsoft.Extensions.DependencyInjection; namespace Microsoft.CmdPal.UI.ViewModels; -public partial class ProviderSettingsViewModel( - CommandProviderWrapper _provider, - ProviderSettings _providerSettings, - IServiceProvider _serviceProvider) : ObservableObject +public partial class ProviderSettingsViewModel : ObservableObject { - private readonly SettingsModel _settings = _serviceProvider.GetService<SettingsModel>()!; + private readonly CommandProviderWrapper _provider; + private readonly ProviderSettings _providerSettings; + private readonly SettingsModel _settings; + private readonly Lock _initializeSettingsLock = new(); private Task? _initializeSettingsTask; + public ProviderSettingsViewModel( + CommandProviderWrapper provider, + ProviderSettings providerSettings, + SettingsModel settings) + { + _provider = provider; + _providerSettings = providerSettings; + _settings = settings; + + LoadingSettings = _provider.Settings?.HasSettings ?? false; + + BuildFallbackViewModels(); + } + public string DisplayName => _provider.DisplayName; public string ExtensionName => _provider.Extension?.ExtensionDisplayName ?? "Built-in"; public string ExtensionSubtext => IsEnabled ? HasFallbackCommands ? - $"{ExtensionName}, {TopLevelCommands.Count} commands, {FallbackCommands.Count} fallback commands" : + $"{ExtensionName}, {TopLevelCommands.Count} commands, {_provider.FallbackItems?.Length} fallback commands" : $"{ExtensionName}, {TopLevelCommands.Count} commands" : Resources.builtin_disabled_extension; @@ -42,7 +55,7 @@ public partial class ProviderSettingsViewModel( public IconInfoViewModel Icon => _provider.Icon; [ObservableProperty] - public partial bool LoadingSettings { get; set; } = _provider.Settings?.HasSettings ?? false; + public partial bool LoadingSettings { get; set; } public bool IsEnabled { @@ -145,28 +158,29 @@ public partial class ProviderSettingsViewModel( } [field: AllowNull] - public List<TopLevelViewModel> FallbackCommands - { - get - { - if (field is null) - { - field = BuildFallbackViewModels(); - } - - return field; - } - } + public List<FallbackSettingsViewModel> FallbackCommands { get; set; } = []; public bool HasFallbackCommands => _provider.FallbackItems?.Length > 0; - private List<TopLevelViewModel> BuildFallbackViewModels() + private void BuildFallbackViewModels() { var thisProvider = _provider; - var providersCommands = thisProvider.FallbackItems; + var providersFallbackCommands = thisProvider.FallbackItems; - // Remember! This comes in on the UI thread! - return [.. providersCommands]; + List<FallbackSettingsViewModel> fallbackViewModels = new(providersFallbackCommands.Length); + foreach (var fallbackItem in providersFallbackCommands) + { + if (_providerSettings.FallbackCommands.TryGetValue(fallbackItem.Id, out var fallbackSettings)) + { + fallbackViewModels.Add(new FallbackSettingsViewModel(fallbackItem, fallbackSettings, _settings, this)); + } + else + { + fallbackViewModels.Add(new FallbackSettingsViewModel(fallbackItem, new(), _settings, this)); + } + } + + FallbackCommands = fallbackViewModels; } private void Save() => SettingsModel.SaveSettings(_settings); diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/SettingsModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/SettingsModel.cs index e210359f76..483fe3fdc3 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/SettingsModel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/SettingsModel.cs @@ -50,6 +50,8 @@ public partial class SettingsModel : ObservableObject public Dictionary<string, ProviderSettings> ProviderSettings { get; set; } = []; + public string[] FallbackRanks { get; set; } = []; + public Dictionary<string, CommandAlias> Aliases { get; set; } = []; public List<TopLevelHotkey> CommandHotkeys { get; set; } = []; @@ -107,6 +109,25 @@ public partial class SettingsModel : ObservableObject return settings; } + public string[] GetGlobalFallbacks() + { + var globalFallbacks = new HashSet<string>(); + + foreach (var provider in ProviderSettings.Values) + { + foreach (var fallback in provider.FallbackCommands) + { + var fallbackSetting = fallback.Value; + if (fallbackSetting.IsEnabled && fallbackSetting.IncludeInGlobalResults) + { + globalFallbacks.Add(fallback.Key); + } + } + } + + return globalFallbacks.ToArray(); + } + public static SettingsModel LoadSettings() { if (string.IsNullOrEmpty(FilePath)) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/SettingsViewModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/SettingsViewModel.cs index 6ac9acacc4..947a025e69 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/SettingsViewModel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/SettingsViewModel.cs @@ -4,10 +4,9 @@ using System.Collections.ObjectModel; using System.ComponentModel; -using Microsoft.CmdPal.Core.Common.Services; using Microsoft.CmdPal.UI.ViewModels.Services; using Microsoft.CmdPal.UI.ViewModels.Settings; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.CommandPalette.Extensions.Toolkit; namespace Microsoft.CmdPal.UI.ViewModels; @@ -27,7 +26,7 @@ public partial class SettingsViewModel : INotifyPropertyChanged ]; private readonly SettingsModel _settings; - private readonly IServiceProvider _serviceProvider; + private readonly TopLevelCommandManager _topLevelCommandManager; public event PropertyChangedEventHandler? PropertyChanged; @@ -174,38 +173,76 @@ public partial class SettingsViewModel : INotifyPropertyChanged } } - public ObservableCollection<ProviderSettingsViewModel> CommandProviders { get; } = []; + public ObservableCollection<ProviderSettingsViewModel> CommandProviders { get; } = new(); + + public ObservableCollection<FallbackSettingsViewModel> FallbackRankings { get; set; } = new(); public SettingsExtensionsViewModel Extensions { get; } - public SettingsViewModel(SettingsModel settings, IServiceProvider serviceProvider, TaskScheduler scheduler) + public SettingsViewModel(SettingsModel settings, TopLevelCommandManager topLevelCommandManager, TaskScheduler scheduler, IThemeService themeService) { _settings = settings; - _serviceProvider = serviceProvider; + _topLevelCommandManager = topLevelCommandManager; - var themeService = serviceProvider.GetRequiredService<IThemeService>(); Appearance = new AppearanceSettingsViewModel(themeService, _settings); var activeProviders = GetCommandProviders(); var allProviderSettings = _settings.ProviderSettings; + var fallbacks = new List<FallbackSettingsViewModel>(); + var currentRankings = _settings.FallbackRanks; + var needsSave = false; + foreach (var item in activeProviders) { var providerSettings = settings.GetProviderSettings(item); - var settingsModel = new ProviderSettingsViewModel(item, providerSettings, _serviceProvider); + var settingsModel = new ProviderSettingsViewModel(item, providerSettings, _settings); CommandProviders.Add(settingsModel); + + fallbacks.AddRange(settingsModel.FallbackCommands); } + var fallbackRankings = new List<Scored<FallbackSettingsViewModel>>(fallbacks.Count); + foreach (var fallback in fallbacks) + { + var index = currentRankings.IndexOf(fallback.Id); + var score = fallbacks.Count; + + if (index >= 0) + { + score = index; + } + + fallbackRankings.Add(new Scored<FallbackSettingsViewModel>() { Item = fallback, Score = score }); + + if (index == -1) + { + needsSave = true; + } + } + + FallbackRankings = new ObservableCollection<FallbackSettingsViewModel>(fallbackRankings.OrderBy(o => o.Score).Select(fr => fr.Item)); Extensions = new SettingsExtensionsViewModel(CommandProviders, scheduler); + + if (needsSave) + { + ApplyFallbackSort(); + } } private IEnumerable<CommandProviderWrapper> GetCommandProviders() { - var manager = _serviceProvider.GetService<TopLevelCommandManager>()!; - var allProviders = manager.CommandProviders; + var allProviders = _topLevelCommandManager.CommandProviders; return allProviders; } + public void ApplyFallbackSort() + { + _settings.FallbackRanks = FallbackRankings.Select(s => s.Id).ToArray(); + Save(); + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FallbackRankings))); + } + private void Save() => SettingsModel.SaveSettings(_settings); } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs index fc5e36d1e2..6d7f830658 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs @@ -4,11 +4,9 @@ using System.Collections.ObjectModel; using CommunityToolkit.Mvvm.ComponentModel; -using CommunityToolkit.Mvvm.Messaging; using ManagedCommon; using Microsoft.CmdPal.Core.ViewModels; using Microsoft.CmdPal.Core.ViewModels.Messages; -using Microsoft.CmdPal.UI.ViewModels.Messages; using Microsoft.CmdPal.UI.ViewModels.Settings; using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions.Toolkit; @@ -18,7 +16,7 @@ using WyHash; namespace Microsoft.CmdPal.UI.ViewModels; -public sealed partial class TopLevelViewModel : ObservableObject, IListItem +public sealed partial class TopLevelViewModel : ObservableObject, IListItem, IExtendedAttributesProvider { private readonly SettingsModel _settings; private readonly ProviderSettings _providerSettings; @@ -27,7 +25,9 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem private readonly string _commandProviderId; - private string IdFromModel => _commandItemViewModel.Command.Id; + private string IdFromModel => IsFallback && !string.IsNullOrWhiteSpace(_fallbackId) ? _fallbackId : _commandItemViewModel.Command.Id; + + private string _fallbackId = string.Empty; private string _generatedId = string.Empty; @@ -41,7 +41,7 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem [ObservableProperty] public partial ObservableCollection<Tag> Tags { get; set; } = []; - public string Id => string.IsNullOrEmpty(IdFromModel) ? _generatedId : IdFromModel; + public string Id => string.IsNullOrWhiteSpace(IdFromModel) ? _generatedId : IdFromModel; public CommandPaletteHost ExtensionHost { get; private set; } @@ -158,14 +158,20 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem public bool IsEnabled { - get => _providerSettings.IsFallbackEnabled(this); - set + get { - if (value != IsEnabled) + if (IsFallback) { - _providerSettings.SetFallbackEnabled(this, value); - Save(); - WeakReferenceMessenger.Default.Send<ReloadCommandsMessage>(new()); + if (_providerSettings.FallbackCommands.TryGetValue(_fallbackId, out var fallbackSettings)) + { + return fallbackSettings.IsEnabled; + } + + return true; + } + else + { + return _providerSettings.IsEnabled; } } } @@ -177,7 +183,8 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem string commandProviderId, SettingsModel settings, ProviderSettings providerSettings, - IServiceProvider serviceProvider) + IServiceProvider serviceProvider, + ICommandItem? commandItem) { _serviceProvider = serviceProvider; _settings = settings; @@ -187,6 +194,10 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem IsFallback = isFallback; ExtensionHost = extensionHost; + if (isFallback && commandItem is FallbackCommandItem fallback) + { + _fallbackId = fallback.Id; + } item.PropertyChanged += Item_PropertyChanged; @@ -232,6 +243,13 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem { UpdateInitialIcon(); } + else if (e.PropertyName == nameof(CommandItem.DataPackage)) + { + DoOnUiThread(() => + { + OnPropertyChanged(nameof(CommandItem.DataPackage)); + }); + } } } @@ -394,4 +412,12 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem { return $"{nameof(TopLevelViewModel)}: {Id} ({Title}) - display: {DisplayTitle} - fallback: {IsFallback} - enabled: {IsEnabled}"; } + + public IDictionary<string, object?> GetProperties() + { + return new Dictionary<string, object?> + { + [WellKnownExtensionAttributes.DataPackage] = _commandItemViewModel?.DataPackage, + }; + } } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/BlurImageControl.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/BlurImageControl.cs index 743e68d690..d77cab0645 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/BlurImageControl.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/BlurImageControl.cs @@ -368,32 +368,69 @@ internal sealed partial class BlurImageControl : Control { try { - if (imageSource is Microsoft.UI.Xaml.Media.Imaging.BitmapImage bitmapImage) + if (imageSource is not Microsoft.UI.Xaml.Media.Imaging.BitmapImage bitmapImage) { - _imageBrush ??= _compositor?.CreateSurfaceBrush(); - if (_imageBrush is null) - { - return; - } - - var loadedSurface = LoadedImageSurface.StartLoadFromUri(bitmapImage.UriSource); - loadedSurface.LoadCompleted += (_, _) => - { - if (_imageBrush is not null) - { - _imageBrush.Surface = loadedSurface; - _imageBrush.Stretch = ConvertStretch(ImageStretch); - _imageBrush.BitmapInterpolationMode = CompositionBitmapInterpolationMode.Linear; - } - }; - - _effectBrush?.SetSourceParameter(ImageSourceParameterName, _imageBrush); + return; } + + _imageBrush ??= _compositor?.CreateSurfaceBrush(); + if (_imageBrush is null) + { + return; + } + + Logger.LogDebug($"Starting load of BlurImageControl from '{bitmapImage.UriSource}'"); + var loadedSurface = LoadedImageSurface.StartLoadFromUri(bitmapImage.UriSource); + loadedSurface.LoadCompleted += OnLoadedSurfaceOnLoadCompleted; + SetLoadedSurfaceToBrush(loadedSurface); + _effectBrush?.SetSourceParameter(ImageSourceParameterName, _imageBrush); } catch (Exception ex) { Logger.LogError("Failed to load image for BlurImageControl: {0}", ex); } + + return; + + void OnLoadedSurfaceOnLoadCompleted(LoadedImageSurface loadedSurface, LoadedImageSourceLoadCompletedEventArgs e) + { + switch (e.Status) + { + case LoadedImageSourceLoadStatus.Success: + Logger.LogDebug($"BlurImageControl loaded successfully: has _imageBrush? {_imageBrush != null}"); + + try + { + SetLoadedSurfaceToBrush(loadedSurface); + } + catch (Exception ex) + { + Logger.LogError("Failed to set surface in BlurImageControl", ex); + throw; + } + + break; + case LoadedImageSourceLoadStatus.NetworkError: + case LoadedImageSourceLoadStatus.InvalidFormat: + case LoadedImageSourceLoadStatus.Other: + default: + Logger.LogError($"Failed to load image for BlurImageControl: Load status {e.Status}"); + break; + } + } + } + + private void SetLoadedSurfaceToBrush(LoadedImageSurface loadedSurface) + { + var surfaceBrush = _imageBrush; + if (surfaceBrush is null) + { + return; + } + + surfaceBrush.Surface = loadedSurface; + surfaceBrush.Stretch = ConvertStretch(ImageStretch); + surfaceBrush.BitmapInterpolationMode = CompositionBitmapInterpolationMode.Linear; } private static CompositionStretch ConvertStretch(Stretch stretch) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRanker.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRanker.xaml new file mode 100644 index 0000000000..89610e53d4 --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRanker.xaml @@ -0,0 +1,74 @@ +<?xml version="1.0" encoding="utf-8" ?> +<UserControl + x:Class="Microsoft.CmdPal.UI.Controls.FallbackRanker" + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:cmdpalUI="using:Microsoft.CmdPal.UI" + xmlns:controls="using:CommunityToolkit.WinUI.Controls" + xmlns:converters="using:CommunityToolkit.WinUI.Converters" + xmlns:coreViewModels="using:Microsoft.CmdPal.Core.ViewModels" + xmlns:cpcontrols="using:Microsoft.CmdPal.UI.Controls" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:helpers="using:Microsoft.CmdPal.UI.Helpers" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:viewModels="using:Microsoft.CmdPal.UI.ViewModels" + mc:Ignorable="d"> + + <Grid> + <ListView + Padding="12,0,24,0" + AllowDrop="True" + CanDragItems="True" + CanReorderItems="True" + DragItemsCompleted="ListView_DragItemsCompleted" + ItemsSource="{x:Bind viewModel.FallbackRankings, Mode=OneWay}" + SelectionMode="None"> + <ListView.ItemTemplate> + <DataTemplate x:DataType="viewModels:FallbackSettingsViewModel"> + <Grid Padding="4,0,0,0"> + <Grid.ColumnDefinitions> + <ColumnDefinition Width="*" /> + <ColumnDefinition Width="Auto" /> + </Grid.ColumnDefinitions> + <Viewbox Grid.Column="1" Height="18"> + <PathIcon Data="M15.5 17C16.3284 17 17 17.6716 17 18.5C17 19.3284 16.3284 20 15.5 20C14.6716 20 14 19.3284 14 18.5C14 17.6716 14.6716 17 15.5 17ZM8.5 17C9.32843 17 10 17.6716 10 18.5C10 19.3284 9.32843 20 8.5 20C7.67157 20 7 19.3284 7 18.5C7 17.6716 7.67157 17 8.5 17ZM15.5 10C16.3284 10 17 10.6716 17 11.5C17 12.3284 16.3284 13 15.5 13C14.6716 13 14 12.3284 14 11.5C14 10.6716 14.6716 10 15.5 10ZM8.5 10C9.32843 10 10 10.6716 10 11.5C10 12.3284 9.32843 13 8.5 13C7.67157 13 7 12.3284 7 11.5C7 10.6716 7.67157 10 8.5 10ZM15.5 3C16.3284 3 17 3.67157 17 4.5C17 5.32843 16.3284 6 15.5 6C14.6716 6 14 5.32843 14 4.5C14 3.67157 14.6716 3 15.5 3ZM8.5 3C9.32843 3 10 3.67157 10 4.5C10 5.32843 9.32843 6 8.5 6C7.67157 6 7 5.32843 7 4.5C7 3.67157 7.67157 3 8.5 3Z" Foreground="{ThemeResource TextFillColorSecondaryBrush}" /> + </Viewbox> + <controls:SettingsCard + Width="560" + MinHeight="0" + Padding="8" + Background="Transparent" + BorderThickness="0" + Header="{x:Bind DisplayName}" + ToolTipService.ToolTip="{x:Bind Id}"> + <controls:SettingsCard.HeaderIcon> + <cpcontrols:ContentIcon> + <cpcontrols:ContentIcon.Content> + <cpcontrols:IconBox + Width="16" + Height="16" + AutomationProperties.AccessibilityView="Raw" + SourceKey="{x:Bind Icon, Mode=OneWay}" + SourceRequested="{x:Bind helpers:IconCacheProvider.SourceRequested}" /> + </cpcontrols:ContentIcon.Content> + </cpcontrols:ContentIcon> + </controls:SettingsCard.HeaderIcon> + </controls:SettingsCard> + </Grid> + </DataTemplate> + </ListView.ItemTemplate> + <!-- Customize Size of Item Container from ListView --> + <ListView.ItemsPanel> + <ItemsPanelTemplate> + <StackPanel Orientation="Vertical" Spacing="0" /> + </ItemsPanelTemplate> + </ListView.ItemsPanel> + <ListView.ItemContainerStyle> + <Style BasedOn="{StaticResource DefaultListViewItemStyle}" TargetType="ListViewItem"> + <Setter Property="Margin" Value="0" /> + <Setter Property="Padding" Value="4" /> + </Style> + </ListView.ItemContainerStyle> + </ListView> + </Grid> +</UserControl> \ No newline at end of file diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRanker.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRanker.xaml.cs new file mode 100644 index 0000000000..57ad2c1d8c --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRanker.xaml.cs @@ -0,0 +1,31 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CmdPal.UI.ViewModels; +using Microsoft.CmdPal.UI.ViewModels.Services; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.UI.Xaml.Controls; + +namespace Microsoft.CmdPal.UI.Controls; + +public sealed partial class FallbackRanker : UserControl +{ + private readonly TaskScheduler _mainTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); + private SettingsViewModel? viewModel; + + public FallbackRanker() + { + this.InitializeComponent(); + + var settings = App.Current.Services.GetService<SettingsModel>()!; + var topLevelCommandManager = App.Current.Services.GetService<TopLevelCommandManager>()!; + var themeService = App.Current.Services.GetService<IThemeService>()!; + viewModel = new SettingsViewModel(settings, topLevelCommandManager, _mainTaskScheduler, themeService); + } + + private void ListView_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args) + { + viewModel?.ApplyFallbackSort(); + } +} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRankerDialog.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRankerDialog.xaml new file mode 100644 index 0000000000..bbdf2f85ca --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRankerDialog.xaml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="utf-8" ?> +<UserControl + x:Class="Microsoft.CmdPal.UI.Controls.FallbackRankerDialog" + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:cpcontrols="using:Microsoft.CmdPal.UI.Controls" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + mc:Ignorable="d"> + <ContentDialog + x:Name="FallbackRankerContentDialog" + Width="420" + MinWidth="420" + PrimaryButtonText="OK"> + <ContentDialog.Title> + <TextBlock x:Uid="ManageFallbackRank" /> + </ContentDialog.Title> + <ContentDialog.Resources> + <x:Double x:Key="ContentDialogMaxWidth">800</x:Double> + </ContentDialog.Resources> + <Grid Width="560" MinWidth="420"> + <Grid.RowDefinitions> + <RowDefinition Height="Auto" /> + <RowDefinition Height="Auto" /> + <RowDefinition Height="*" /> + </Grid.RowDefinitions> + <TextBlock + x:Uid="ManageFallbackOrderDialogDescription" + Foreground="{ThemeResource TextFillColorSecondaryBrush}" + TextWrapping="Wrap" /> + <Rectangle + Grid.Row="1" + Height="1" + Margin="0,16,0,16" + HorizontalAlignment="Stretch" + Fill="{ThemeResource DividerStrokeColorDefaultBrush}" /> + <cpcontrols:FallbackRanker + x:Name="FallbackRanker" + Grid.Row="2" + Margin="-24,0,-24,0" /> + </Grid> + </ContentDialog> +</UserControl> diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRankerDialog.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRankerDialog.xaml.cs new file mode 100644 index 0000000000..a5186609be --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/FallbackRankerDialog.xaml.cs @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Data; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Navigation; +using Windows.Foundation; +using Windows.Foundation.Collections; + +namespace Microsoft.CmdPal.UI.Controls; + +public sealed partial class FallbackRankerDialog : UserControl +{ + public FallbackRankerDialog() + { + InitializeComponent(); + } + + public IAsyncOperation<ContentDialogResult> ShowAsync() + { + return FallbackRankerContentDialog!.ShowAsync()!; + } +} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml.cs index 0d6fd58afa..ca27af4719 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/SearchBar.xaml.cs @@ -379,6 +379,12 @@ public sealed partial class SearchBar : UserControl, if (CurrentPageViewModel is not null) { CurrentPageViewModel.SearchTextBox = FilterBox.Text; + + // Telemetry: Track search query count for session metrics (only non-empty queries) + if (!string.IsNullOrWhiteSpace(FilterBox.Text)) + { + WeakReferenceMessenger.Default.Send<SearchQueryMessage>(new()); + } } } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/WrapPanelCustom/WrapPanel.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/WrapPanelCustom/WrapPanel.cs index ea0101bfa3..61fa42202e 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/WrapPanelCustom/WrapPanel.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Controls/WrapPanelCustom/WrapPanel.cs @@ -8,6 +8,8 @@ using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Windows.Foundation; +using ToolkitStretchChild = CommunityToolkit.WinUI.Controls.StretchChild; + namespace Microsoft.CmdPal.UI.Controls; /// <summary> @@ -177,9 +179,9 @@ public sealed partial class WrapPanel : Panel /// <summary> /// Gets or sets a value indicating how to arrange child items /// </summary> - public StretchChild StretchChild + public ToolkitStretchChild StretchChild { - get { return (StretchChild)GetValue(StretchChildProperty); } + get { return (ToolkitStretchChild)GetValue(StretchChildProperty); } set { SetValue(StretchChildProperty, value); } } @@ -190,9 +192,9 @@ public sealed partial class WrapPanel : Panel public static readonly DependencyProperty StretchChildProperty = DependencyProperty.Register( nameof(StretchChild), - typeof(StretchChild), + typeof(ToolkitStretchChild), typeof(WrapPanel), - new PropertyMetadata(StretchChild.None, LayoutPropertyChanged)); + new PropertyMetadata(ToolkitStretchChild.None, LayoutPropertyChanged)); /// <summary> /// Identifies the IsFullLine attached dependency property. @@ -397,7 +399,7 @@ public sealed partial class WrapPanel : Panel Arrange(Children[i]); } - Arrange(Children[lastIndex], StretchChild == StretchChild.Last); + Arrange(Children[lastIndex], StretchChild == ToolkitStretchChild.Last); if (currentRow.ChildrenRects.Count > 0) { diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Events/CmdPalExtensionInvoked.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Events/CmdPalExtensionInvoked.cs new file mode 100644 index 0000000000..0113a4ad27 --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Events/CmdPalExtensionInvoked.cs @@ -0,0 +1,56 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Tracing; +using Microsoft.PowerToys.Telemetry; +using Microsoft.PowerToys.Telemetry.Events; + +namespace Microsoft.CmdPal.UI.Events; + +/// <summary> +/// Tracks extension usage with extension name and invocation details. +/// Purpose: Identify popular vs. unused plugins and track extension performance. +/// </summary> +[EventData] +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] +public class CmdPalExtensionInvoked : EventBase, IEvent +{ + /// <summary> + /// Gets or sets the unique identifier of the extension provider. + /// </summary> + public string ExtensionId { get; set; } + + /// <summary> + /// Gets or sets the non-localized identifier of the command being invoked. + /// </summary> + public string CommandId { get; set; } + + /// <summary> + /// Gets or sets the localized display name of the command being invoked. + /// </summary> + public string CommandName { get; set; } + + /// <summary> + /// Gets or sets whether the command executed successfully. + /// </summary> + public bool Success { get; set; } + + /// <summary> + /// Gets or sets the execution time in milliseconds. + /// </summary> + public ulong ExecutionTimeMs { get; set; } + + public CmdPalExtensionInvoked(string extensionId, string commandId, string commandName, bool success, ulong executionTimeMs) + { + EventName = "CmdPal_ExtensionInvoked"; + ExtensionId = extensionId; + CommandId = commandId; + CommandName = commandName; + Success = success; + ExecutionTimeMs = executionTimeMs; + } + + public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage; +} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Events/CmdPalInvokeResult.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Events/CmdPalInvokeResult.cs index f4f8b4d0e8..287471f977 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Events/CmdPalInvokeResult.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Events/CmdPalInvokeResult.cs @@ -18,6 +18,7 @@ public class CmdPalInvokeResult : EventBase, IEvent public CmdPalInvokeResult(CommandResultKind resultKind) { + EventName = "CmdPal_InvokeResult"; ResultKind = resultKind.ToString(); } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Events/CmdPalSessionDuration.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Events/CmdPalSessionDuration.cs new file mode 100644 index 0000000000..357cb9db53 --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Events/CmdPalSessionDuration.cs @@ -0,0 +1,68 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Tracing; +using Microsoft.PowerToys.Telemetry; +using Microsoft.PowerToys.Telemetry.Events; + +namespace Microsoft.CmdPal.UI.Events; + +/// <summary> +/// Tracks Command Palette session duration from launch to close. +/// Purpose: Understand user engagement patterns - quick actions vs. browsing behavior. +/// </summary> +[EventData] +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] +public class CmdPalSessionDuration : EventBase, IEvent +{ + /// <summary> + /// Gets or sets the session duration in milliseconds. + /// </summary> + public ulong DurationMs { get; set; } + + /// <summary> + /// Gets or sets the number of commands executed during the session. + /// </summary> + public int CommandsExecuted { get; set; } + + /// <summary> + /// Gets or sets the number of pages visited during the session. + /// </summary> + public int PagesVisited { get; set; } + + /// <summary> + /// Gets or sets the reason for dismissal (Escape, LostFocus, Command, etc.). + /// </summary> + public string DismissalReason { get; set; } + + /// <summary> + /// Gets or sets the number of search queries executed during the session. + /// </summary> + public int SearchQueriesCount { get; set; } + + /// <summary> + /// Gets or sets the maximum navigation depth reached during the session. + /// </summary> + public int MaxNavigationDepth { get; set; } + + /// <summary> + /// Gets or sets the number of errors encountered during the session. + /// </summary> + public int ErrorCount { get; set; } + + public CmdPalSessionDuration(ulong durationMs, int commandsExecuted, int pagesVisited, string dismissalReason, int searchQueriesCount, int maxNavigationDepth, int errorCount) + { + EventName = "CmdPal_SessionDuration"; + DurationMs = durationMs; + CommandsExecuted = commandsExecuted; + PagesVisited = pagesVisited; + DismissalReason = dismissalReason; + SearchQueriesCount = searchQueriesCount; + MaxNavigationDepth = maxNavigationDepth; + ErrorCount = errorCount; + } + + public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage; +} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ListPage.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ListPage.xaml index 859a74eb18..3508798078 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ListPage.xaml +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ListPage.xaml @@ -439,9 +439,12 @@ <ListView x:Name="ItemsList" Padding="0,2,0,0" + CanDragItems="True" ContextCanceled="Items_OnContextCanceled" ContextRequested="Items_OnContextRequested" DoubleTapped="Items_DoubleTapped" + DragItemsCompleted="Items_DragItemsCompleted" + DragItemsStarting="Items_DragItemsStarting" IsDoubleTapEnabled="True" IsItemClickEnabled="True" ItemClick="Items_ItemClick" @@ -458,9 +461,12 @@ <GridView x:Name="ItemsGrid" Padding="16,0" + CanDragItems="True" ContextCanceled="Items_OnContextCanceled" ContextRequested="Items_OnContextRequested" DoubleTapped="Items_DoubleTapped" + DragItemsCompleted="Items_DragItemsCompleted" + DragItemsStarting="Items_DragItemsStarting" IsDoubleTapEnabled="True" IsItemClickEnabled="True" ItemClick="Items_ItemClick" diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ListPage.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ListPage.xaml.cs index 3b15f54d01..f402e6100c 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ListPage.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/ExtViews/ListPage.xaml.cs @@ -18,6 +18,7 @@ using Microsoft.UI.Xaml.Controls.Primitives; using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Navigation; +using Windows.ApplicationModel.DataTransfer; using Windows.Foundation; using Windows.System; @@ -891,6 +892,89 @@ public sealed partial class ListPage : Page, ItemView.SelectedIndex = newIndex; } + private void Items_DragItemsStarting(object sender, DragItemsStartingEventArgs e) + { + try + { + if (e.Items.FirstOrDefault() is not ListItemViewModel item || item.DataPackage is null) + { + e.Cancel = true; + return; + } + + // copy properties + foreach (var (key, value) in item.DataPackage.Properties) + { + try + { + e.Data.Properties[key] = value; + } + catch (Exception) + { + // noop - skip any properties that fail + } + } + + // setup e.Data formats as deferred renderers to read from the item's DataPackage + foreach (var format in item.DataPackage.AvailableFormats) + { + try + { + e.Data.SetDataProvider(format, request => DelayRenderer(request, item, format)); + } + catch (Exception) + { + // noop - skip any formats that fail + } + } + + WeakReferenceMessenger.Default.Send(new DragStartedMessage()); + } + catch (Exception ex) + { + WeakReferenceMessenger.Default.Send(new DragCompletedMessage()); + Logger.LogError("Failed to start dragging an item", ex); + } + } + + private static void DelayRenderer(DataProviderRequest request, ListItemViewModel item, string format) + { + var deferral = request.GetDeferral(); + try + { + item.DataPackage?.GetDataAsync(format) + .AsTask() + .ContinueWith(dataTask => + { + try + { + if (dataTask.IsCompletedSuccessfully) + { + request.SetData(dataTask.Result); + } + else if (dataTask.IsFaulted && dataTask.Exception is not null) + { + Logger.LogError($"Failed to get data for format '{format}' during drag-and-drop", dataTask.Exception); + } + } + finally + { + deferral.Complete(); + } + }); + } + catch (Exception ex) + { + Logger.LogError($"Failed to set data for format '{format}' during drag-and-drop", ex); + deferral.Complete(); + } + } + + private void Items_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args) + { + WeakReferenceMessenger.Default.Send(new DragCompletedMessage()); + } + /// <summary> /// Code stealed from <see cref="Controls.ContextMenu.NavigateDown"/> /// </summary> diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/TelemetryForwarder.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/TelemetryForwarder.cs index e14d1abe3b..37139bb982 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/TelemetryForwarder.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/TelemetryForwarder.cs @@ -6,40 +6,78 @@ using CommunityToolkit.Mvvm.Messaging; using Microsoft.CmdPal.Core.Common.Services; using Microsoft.CmdPal.Core.ViewModels.Messages; using Microsoft.CmdPal.UI.Events; +using Microsoft.CommandPalette.Extensions; using Microsoft.PowerToys.Telemetry; namespace Microsoft.CmdPal.UI; /// <summary> /// TelemetryForwarder is responsible for forwarding telemetry events from the -/// command palette core to PowerToys Telemetry. -/// This allows us to emit telemetry events as messages from the core, -/// and then handle them by logging to our PT telemetry provider. -/// -/// We may in the future want to replace this with a more generic "ITelemetryService" -/// or something similar, but this works for now. +/// command palette to PowerToys Telemetry. +/// Listens to telemetry-specific messages from the core layer and logs them to PowerToys telemetry. +/// Also implements ITelemetryService for dependency injection in extensions. /// </summary> internal sealed class TelemetryForwarder : ITelemetryService, - IRecipient<BeginInvokeMessage>, - IRecipient<CmdPalInvokeResultMessage> + IRecipient<TelemetryBeginInvokeMessage>, + IRecipient<TelemetryInvokeResultMessage>, + IRecipient<TelemetryExtensionInvokedMessage> { public TelemetryForwarder() { - WeakReferenceMessenger.Default.Register<BeginInvokeMessage>(this); - WeakReferenceMessenger.Default.Register<CmdPalInvokeResultMessage>(this); + WeakReferenceMessenger.Default.Register<TelemetryBeginInvokeMessage>(this); + WeakReferenceMessenger.Default.Register<TelemetryInvokeResultMessage>(this); + WeakReferenceMessenger.Default.Register<TelemetryExtensionInvokedMessage>(this); } - public void Receive(CmdPalInvokeResultMessage message) - { - PowerToysTelemetry.Log.WriteEvent(new CmdPalInvokeResult(message.Kind)); - } - - public void Receive(BeginInvokeMessage message) + // Message handlers for telemetry events from core layer + public void Receive(TelemetryBeginInvokeMessage message) { PowerToysTelemetry.Log.WriteEvent(new BeginInvoke()); } + public void Receive(TelemetryInvokeResultMessage message) + { + PowerToysTelemetry.Log.WriteEvent(new CmdPalInvokeResult(message.Kind)); + } + + public void Receive(TelemetryExtensionInvokedMessage message) + { + PowerToysTelemetry.Log.WriteEvent(new CmdPalExtensionInvoked( + message.ExtensionId, + message.CommandId, + message.CommandName, + message.Success, + message.ExecutionTimeMs)); + + // Increment session counter for commands executed + if (App.Current.AppWindow is MainWindow mainWindow) + { + mainWindow.IncrementCommandsExecuted(); + } + } + + // Static method for logging session duration from UI layer + public static void LogSessionDuration( + ulong durationMs, + int commandsExecuted, + int pagesVisited, + string dismissalReason, + int searchQueriesCount, + int maxNavigationDepth, + int errorCount) + { + PowerToysTelemetry.Log.WriteEvent(new CmdPalSessionDuration( + durationMs, + commandsExecuted, + pagesVisited, + dismissalReason, + searchQueriesCount, + maxNavigationDepth, + errorCount)); + } + + // ITelemetryService implementation for dependency injection in extensions public void LogRunQuery(string query, int resultCount, ulong durationMs) { PowerToysTelemetry.Log.WriteEvent(new CmdPalRunQuery(query, resultCount, durationMs)); diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/MainWindow.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/MainWindow.xaml.cs index d9acdb48d9..abaf102a9e 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/MainWindow.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/MainWindow.xaml.cs @@ -52,6 +52,12 @@ public sealed partial class MainWindow : WindowEx, IRecipient<ShowWindowMessage>, IRecipient<HideWindowMessage>, IRecipient<QuitMessage>, + IRecipient<NavigateToPageMessage>, + IRecipient<NavigationDepthMessage>, + IRecipient<SearchQueryMessage>, + IRecipient<ErrorOccurredMessage>, + IRecipient<DragStartedMessage>, + IRecipient<DragCompletedMessage>, IDisposable { private const int DefaultWidth = 800; @@ -73,12 +79,22 @@ public sealed partial class MainWindow : WindowEx, private bool _ignoreHotKeyWhenFullScreen = true; private bool _themeServiceInitialized; + // Session tracking for telemetry + private Stopwatch? _sessionStopwatch; + private int _sessionCommandsExecuted; + private int _sessionPagesVisited; + private int _sessionSearchQueriesCount; + private int _sessionMaxNavigationDepth; + private int _sessionErrorCount; + private DesktopAcrylicController? _acrylicController; private SystemBackdropConfiguration? _configurationSource; private TimeSpan _autoGoHomeInterval = Timeout.InfiniteTimeSpan; private WindowPosition _currentWindowPosition = new(); + private bool _preventHideWhenDeactivated; + private MainWindowViewModel ViewModel { get; } public MainWindow() @@ -119,6 +135,12 @@ public sealed partial class MainWindow : WindowEx, WeakReferenceMessenger.Default.Register<QuitMessage>(this); WeakReferenceMessenger.Default.Register<ShowWindowMessage>(this); WeakReferenceMessenger.Default.Register<HideWindowMessage>(this); + WeakReferenceMessenger.Default.Register<NavigateToPageMessage>(this); + WeakReferenceMessenger.Default.Register<NavigationDepthMessage>(this); + WeakReferenceMessenger.Default.Register<SearchQueryMessage>(this); + WeakReferenceMessenger.Default.Register<ErrorOccurredMessage>(this); + WeakReferenceMessenger.Default.Register<DragStartedMessage>(this); + WeakReferenceMessenger.Default.Register<DragCompletedMessage>(this); // Hide our titlebar. // We need to both ExtendsContentIntoTitleBar, then set the height to Collapsed @@ -518,6 +540,11 @@ public sealed partial class MainWindow : WindowEx, { var settings = App.Current.Services.GetService<SettingsModel>()!; + // Start session tracking + _sessionStopwatch = Stopwatch.StartNew(); + _sessionCommandsExecuted = 0; + _sessionPagesVisited = 0; + ShowHwnd(message.Hwnd, settings.SummonOn); } @@ -526,6 +553,7 @@ public sealed partial class MainWindow : WindowEx, // This might come in off the UI thread. Make sure to hop back. DispatcherQueue.TryEnqueue(() => { + EndSession("Hide"); HideWindow(); }); } @@ -545,10 +573,67 @@ public sealed partial class MainWindow : WindowEx, // This might come in off the UI thread. Make sure to hop back. DispatcherQueue.TryEnqueue(() => { + EndSession("Dismiss"); HideWindow(); }); } + // Session telemetry: Track metrics during the Command Palette session + // These receivers increment counters that are sent when EndSession is called + public void Receive(NavigateToPageMessage message) + { + _sessionPagesVisited++; + } + + public void Receive(NavigationDepthMessage message) + { + if (message.Depth > _sessionMaxNavigationDepth) + { + _sessionMaxNavigationDepth = message.Depth; + } + } + + public void Receive(SearchQueryMessage message) + { + _sessionSearchQueriesCount++; + } + + public void Receive(ErrorOccurredMessage message) + { + _sessionErrorCount++; + } + + /// <summary> + /// Ends the current telemetry session and emits the CmdPal_SessionDuration event. + /// Aggregates all session metrics collected since ShowWindow and sends them to telemetry. + /// </summary> + /// <param name="dismissalReason">The reason the session ended (e.g., Dismiss, Hide, LostFocus).</param> + private void EndSession(string dismissalReason) + { + if (_sessionStopwatch is not null) + { + _sessionStopwatch.Stop(); + TelemetryForwarder.LogSessionDuration( + (ulong)_sessionStopwatch.ElapsedMilliseconds, + _sessionCommandsExecuted, + _sessionPagesVisited, + dismissalReason, + _sessionSearchQueriesCount, + _sessionMaxNavigationDepth, + _sessionErrorCount); + _sessionStopwatch = null; + } + } + + /// <summary> + /// Increments the session commands executed counter for telemetry. + /// Called by TelemetryForwarder when an extension command is invoked. + /// </summary> + internal void IncrementCommandsExecuted() + { + _sessionCommandsExecuted++; + } + private void HideWindow() { // Cloak our HWND to avoid all animations. @@ -751,7 +836,14 @@ public sealed partial class MainWindow : WindowEx, return; } + // We're doing something that requires us to lose focus, but we don't want to hide the window + if (_preventHideWhenDeactivated) + { + return; + } + // This will DWM cloak our window: + EndSession("LostFocus"); HideWindow(); PowerToysTelemetry.Log.WriteEvent(new CmdPalDismissedOnLostFocus()); @@ -1027,4 +1119,44 @@ public sealed partial class MainWindow : WindowEx, _windowThemeSynchronizer.Dispose(); DisposeAcrylic(); } + + public void Receive(DragStartedMessage message) + { + _preventHideWhenDeactivated = true; + } + + public void Receive(DragCompletedMessage message) + { + _preventHideWhenDeactivated = false; + Task.Delay(200).ContinueWith(_ => + { + DispatcherQueue.TryEnqueue(StealForeground); + }); + } + + private unsafe void StealForeground() + { + var foregroundWindow = PInvoke.GetForegroundWindow(); + if (foregroundWindow == _hwnd) + { + return; + } + + // This is bad, evil, and I'll have to forgo today's dinner dessert to punish myself + // for writing this. But there's no way to make this work without it. + // If the window is not reactivated, the UX breaks down: a deactivated window has to + // be activated and then deactivated again to hide. + var currentThreadId = PInvoke.GetCurrentThreadId(); + var foregroundThreadId = PInvoke.GetWindowThreadProcessId(foregroundWindow, null); + if (foregroundThreadId != currentThreadId) + { + PInvoke.AttachThreadInput(currentThreadId, foregroundThreadId, true); + PInvoke.SetForegroundWindow(_hwnd); + PInvoke.AttachThreadInput(currentThreadId, foregroundThreadId, false); + } + else + { + PInvoke.SetForegroundWindow(_hwnd); + } + } } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Messages/DragCompletedMessage.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Messages/DragCompletedMessage.cs new file mode 100644 index 0000000000..c2bd5300ed --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Messages/DragCompletedMessage.cs @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.UI.Messages; + +public record DragCompletedMessage; diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Messages/DragStartedMessage.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Messages/DragStartedMessage.cs new file mode 100644 index 0000000000..84b9915fc6 --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Messages/DragStartedMessage.cs @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CmdPal.UI.Messages; + +public record DragStartedMessage; diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Microsoft.CmdPal.UI.csproj b/src/modules/cmdpal/Microsoft.CmdPal.UI/Microsoft.CmdPal.UI.csproj index 54961a5828..79ce3ff5ee 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Microsoft.CmdPal.UI.csproj +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Microsoft.CmdPal.UI.csproj @@ -71,6 +71,7 @@ <None Remove="Controls\ColorPalette.xaml" /> <None Remove="Controls\CommandPalettePreview.xaml" /> <None Remove="Controls\DevRibbon.xaml" /> + <None Remove="Controls\FallbackRankerDialog.xaml" /> <None Remove="Controls\KeyVisual\KeyCharPresenter.xaml" /> <None Remove="Controls\ScreenPreview.xaml" /> <None Remove="Controls\SearchBar.xaml" /> @@ -231,6 +232,12 @@ </Page> </ItemGroup> + <ItemGroup> + <Page Update="Controls\FallbackRankerDialog.xaml"> + <Generator>MSBuild:Compile</Generator> + </Page> + </ItemGroup> + <ItemGroup> <Page Update="Styles\Theme.Colorful.xaml"> <SubType>Designer</SubType> diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/NativeMethods.txt b/src/modules/cmdpal/Microsoft.CmdPal.UI/NativeMethods.txt index fc5a608199..513db65b1a 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/NativeMethods.txt +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/NativeMethods.txt @@ -63,4 +63,7 @@ CreateWindowEx WNDCLASSEXW RegisterClassEx GetStockObject -GetModuleHandle \ No newline at end of file +GetModuleHandle + +GetWindowThreadProcessId +AttachThreadInput \ No newline at end of file diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Pages/ShellPage.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Pages/ShellPage.xaml.cs index 7e8dc9eebd..394325eb18 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Pages/ShellPage.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Pages/ShellPage.xaml.cs @@ -161,6 +161,9 @@ public sealed partial class ShellPage : Microsoft.UI.Xaml.Controls.Page, PowerToysTelemetry.Log.WriteEvent(new OpenPage(RootFrame.BackStackDepth, message.Page.Id)); + // Telemetry: Send navigation depth for session max depth tracking + WeakReferenceMessenger.Default.Send(new NavigationDepthMessage(RootFrame.BackStackDepth)); + if (!ViewModel.IsNested) { // todo BODGY diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/PowerToysRootPageService.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/PowerToysRootPageService.cs index d14dd391bd..9a877358f0 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/PowerToysRootPageService.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/PowerToysRootPageService.cs @@ -10,7 +10,6 @@ using Microsoft.CmdPal.Core.ViewModels; using Microsoft.CmdPal.UI.ViewModels; using Microsoft.CmdPal.UI.ViewModels.MainPage; using Microsoft.CommandPalette.Extensions; -using Microsoft.Extensions.DependencyInjection; using WinRT; // To learn more about WinUI, the WinUI project structure, @@ -19,24 +18,24 @@ namespace Microsoft.CmdPal.UI; internal sealed class PowerToysRootPageService : IRootPageService { - private readonly IServiceProvider _serviceProvider; + private readonly TopLevelCommandManager _tlcManager; + private IExtensionWrapper? _activeExtension; private Lazy<MainListPage> _mainListPage; - public PowerToysRootPageService(IServiceProvider serviceProvider) + public PowerToysRootPageService(TopLevelCommandManager topLevelCommandManager, SettingsModel settings, AliasManager aliasManager, AppStateModel appStateModel) { - _serviceProvider = serviceProvider; + _tlcManager = topLevelCommandManager; _mainListPage = new Lazy<MainListPage>(() => { - return new MainListPage(_serviceProvider); + return new MainListPage(_tlcManager, settings, aliasManager, appStateModel); }); } public async Task PreLoadAsync() { - var tlcManager = _serviceProvider.GetService<TopLevelCommandManager>()!; - await tlcManager.LoadBuiltinsAsync(); + await _tlcManager.LoadBuiltinsAsync(); } public Microsoft.CommandPalette.Extensions.IPage GetRootPage() @@ -46,13 +45,11 @@ internal sealed class PowerToysRootPageService : IRootPageService public async Task PostLoadRootPageAsync() { - var tlcManager = _serviceProvider.GetService<TopLevelCommandManager>()!; - // After loading built-ins, and starting navigation, kick off a thread to load extensions. - tlcManager.LoadExtensionsCommand.Execute(null); + _tlcManager.LoadExtensionsCommand.Execute(null); - await tlcManager.LoadExtensionsCommand.ExecutionTask!; - if (tlcManager.LoadExtensionsCommand.ExecutionTask.Status != TaskStatus.RanToCompletion) + await _tlcManager.LoadExtensionsCommand.ExecutionTask!; + if (_tlcManager.LoadExtensionsCommand.ExecutionTask.Status != TaskStatus.RanToCompletion) { // TODO: Handle failure case } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Properties/PublishProfiles/win-arm64.pubxml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Properties/PublishProfiles/win-arm64.pubxml index 882c64e3e7..a7bece87c1 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Properties/PublishProfiles/win-arm64.pubxml +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Properties/PublishProfiles/win-arm64.pubxml @@ -12,4 +12,4 @@ https://go.microsoft.com/fwlink/?LinkID=208121. <SelfContained>true</SelfContained> <PublishSingleFile>False</PublishSingleFile> </PropertyGroup> -</Project> \ No newline at end of file +</Project> diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Properties/PublishProfiles/win-x64.pubxml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Properties/PublishProfiles/win-x64.pubxml index c686bf808b..73aa1ac98f 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Properties/PublishProfiles/win-x64.pubxml +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Properties/PublishProfiles/win-x64.pubxml @@ -12,4 +12,4 @@ https://go.microsoft.com/fwlink/?LinkID=208121. <SelfContained>true</SelfContained> <PublishSingleFile>False</PublishSingleFile> </PropertyGroup> -</Project> \ No newline at end of file +</Project> diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/AppearancePage.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/AppearancePage.xaml.cs index 39a8ea4ae1..c2fd4a1b29 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/AppearancePage.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/AppearancePage.xaml.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using ManagedCommon; using Microsoft.CmdPal.UI.ViewModels; +using Microsoft.CmdPal.UI.ViewModels.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.UI; using Microsoft.UI.Xaml; @@ -28,7 +29,9 @@ public sealed partial class AppearancePage : Page InitializeComponent(); var settings = App.Current.Services.GetService<SettingsModel>()!; - ViewModel = new SettingsViewModel(settings, App.Current.Services, _mainTaskScheduler); + var themeService = App.Current.Services.GetRequiredService<IThemeService>(); + var topLevelCommandManager = App.Current.Services.GetService<TopLevelCommandManager>()!; + ViewModel = new SettingsViewModel(settings, topLevelCommandManager, _mainTaskScheduler, themeService); } private async void PickBackgroundImage_Click(object sender, RoutedEventArgs e) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionPage.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionPage.xaml index 72b51fd724..b5b219fe43 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionPage.xaml +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionPage.xaml @@ -122,33 +122,65 @@ </ItemsRepeater.ItemTemplate> </ItemsRepeater> - <TextBlock - x:Uid="ExtensionFallbackCommandsHeader" - Style="{StaticResource SettingsSectionHeaderTextBlockStyle}" - Visibility="{x:Bind ViewModel.HasFallbackCommands}" /> + <Grid Visibility="{x:Bind ViewModel.HasFallbackCommands}"> + <Grid.ColumnDefinitions> + <ColumnDefinition Width="*" /> + <ColumnDefinition Width="Auto" /> + </Grid.ColumnDefinitions> + <TextBlock x:Uid="ExtensionFallbackCommandsHeader" Style="{StaticResource SettingsSectionHeaderTextBlockStyle}" /> + <HyperlinkButton + x:Uid="ManageFallbackRankAutomation" + Grid.Column="1" + Margin="0,0,0,4" + Padding="0" + VerticalAlignment="Bottom" + Click="RankButton_Click"> + <HyperlinkButton.Content> + <StackPanel Orientation="Horizontal" Spacing="8"> + <FontIcon + AutomationProperties.AccessibilityView="Raw" + FontSize="16" + Glyph="" /> + <TextBlock x:Uid="ManageFallbackRank" /> + </StackPanel> + </HyperlinkButton.Content> + </HyperlinkButton> + </Grid> <ItemsRepeater ItemsSource="{x:Bind ViewModel.FallbackCommands, Mode=OneWay}" Layout="{StaticResource VerticalStackLayout}" Visibility="{x:Bind ViewModel.HasFallbackCommands}"> <ItemsRepeater.ItemTemplate> - <DataTemplate x:DataType="viewModels:TopLevelViewModel"> - <controls:SettingsCard DataContext="{x:Bind}" Header="{x:Bind DisplayTitle, Mode=OneWay}"> - <controls:SettingsCard.HeaderIcon> + <DataTemplate x:DataType="viewModels:FallbackSettingsViewModel"> + <controls:SettingsExpander + Grid.Column="1" + Header="{x:Bind DisplayName, Mode=OneWay}" + IsExpanded="False"> + <controls:SettingsExpander.HeaderIcon> <cpcontrols:ContentIcon> <cpcontrols:ContentIcon.Content> <cpcontrols:IconBox Width="20" Height="20" AutomationProperties.AccessibilityView="Raw" - SourceKey="{x:Bind InitialIcon, Mode=OneWay}" + SourceKey="{x:Bind Icon, Mode=OneWay}" SourceRequested="{x:Bind helpers:IconCacheProvider.SourceRequested}" /> </cpcontrols:ContentIcon.Content> </cpcontrols:ContentIcon> - </controls:SettingsCard.HeaderIcon> - <!-- Content goes here --> + </controls:SettingsExpander.HeaderIcon> + <ToggleSwitch IsOn="{x:Bind IsEnabled, Mode=TwoWay}" /> - </controls:SettingsCard> + + <controls:SettingsExpander.Items> + <controls:SettingsCard ContentAlignment="Left"> + <cpcontrols:CheckBoxWithDescriptionControl + x:Uid="Settings_FallbacksPage_GlobalResults_SettingsCard" + IsChecked="{x:Bind IncludeInGlobalResults, Mode=TwoWay}" + IsEnabled="{x:Bind IsEnabled, Mode=OneWay}" /> + </controls:SettingsCard> + </controls:SettingsExpander.Items> + </controls:SettingsExpander> </DataTemplate> </ItemsRepeater.ItemTemplate> </ItemsRepeater> @@ -198,7 +230,6 @@ Text="{x:Bind ViewModel.ExtensionVersion}" /> </controls:SettingsCard> </StackPanel> - </controls:Case> <controls:Case Value="False"> @@ -217,5 +248,6 @@ </StackPanel> </Grid> </ScrollViewer> + <cpcontrols:FallbackRankerDialog x:Name="FallbackRankerDialog" /> </Grid> </Page> diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionPage.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionPage.xaml.cs index 2bfdc1bcb3..ac05f356b3 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionPage.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionPage.xaml.cs @@ -25,4 +25,9 @@ public sealed partial class ExtensionPage : Page ? vm : throw new ArgumentException($"{nameof(ExtensionPage)} navigation args should be passed a {nameof(ProviderSettingsViewModel)}"); } + + private async void RankButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) + { + await FallbackRankerDialog.ShowAsync(); + } } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionsPage.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionsPage.xaml index 9e9600bd6e..af529a1bab 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionsPage.xaml +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionsPage.xaml @@ -177,6 +177,12 @@ <FontIcon Glyph="" /> </MenuFlyoutItem.Icon> </MenuFlyoutItem> + <MenuFlyoutSeparator /> + <MenuFlyoutItem x:Uid="Settings_ExtensionsPage_More_ReorderFallbacks_MenuFlyoutItem" Click="MenuFlyoutItem_OnClick"> + <MenuFlyoutItem.Icon> + <FontIcon Glyph="" /> + </MenuFlyoutItem.Icon> + </MenuFlyoutItem> </MenuFlyout> </Button.Flyout> <FontIcon @@ -240,6 +246,7 @@ </StackPanel> </Grid> </ScrollViewer> + <cpcontrols:FallbackRankerDialog x:Name="FallbackRankerDialog" /> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="LayoutVisualStates"> <VisualState x:Name="WideLayout"> diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionsPage.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionsPage.xaml.cs index e99296bad9..f19be9f0cf 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionsPage.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/ExtensionsPage.xaml.cs @@ -4,7 +4,9 @@ using CommunityToolkit.Mvvm.Messaging; using CommunityToolkit.WinUI.Controls; +using ManagedCommon; using Microsoft.CmdPal.UI.ViewModels; +using Microsoft.CmdPal.UI.ViewModels.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -23,7 +25,9 @@ public sealed partial class ExtensionsPage : Page this.InitializeComponent(); var settings = App.Current.Services.GetService<SettingsModel>()!; - viewModel = new SettingsViewModel(settings, App.Current.Services, _mainTaskScheduler); + var topLevelCommandManager = App.Current.Services.GetService<TopLevelCommandManager>()!; + var themeService = App.Current.Services.GetService<IThemeService>()!; + viewModel = new SettingsViewModel(settings, topLevelCommandManager, _mainTaskScheduler, themeService); } private void SettingsCard_Click(object sender, RoutedEventArgs e) @@ -42,4 +46,16 @@ public sealed partial class ExtensionsPage : Page SearchBox?.Focus(FocusState.Keyboard); args.Handled = true; } + + private async void MenuFlyoutItem_OnClick(object sender, RoutedEventArgs e) + { + try + { + await FallbackRankerDialog!.ShowAsync(); + } + catch (Exception ex) + { + Logger.LogError("Error when showing FallbackRankerDialog", ex); + } + } } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/GeneralPage.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/GeneralPage.xaml.cs index d732600c4e..e2a5b7938c 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/GeneralPage.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Settings/GeneralPage.xaml.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using Microsoft.CmdPal.UI.ViewModels; +using Microsoft.CmdPal.UI.ViewModels.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.UI.Xaml.Controls; using Windows.ApplicationModel; @@ -20,7 +21,9 @@ public sealed partial class GeneralPage : Page this.InitializeComponent(); var settings = App.Current.Services.GetService<SettingsModel>()!; - viewModel = new SettingsViewModel(settings, App.Current.Services, _mainTaskScheduler); + var topLevelCommandManager = App.Current.Services.GetService<TopLevelCommandManager>()!; + var themeService = App.Current.Services.GetService<IThemeService>()!; + viewModel = new SettingsViewModel(settings, topLevelCommandManager, _mainTaskScheduler, themeService); } public string ApplicationVersion diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Strings/en-us/Resources.resw b/src/modules/cmdpal/Microsoft.CmdPal.UI/Strings/en-us/Resources.resw index b2c0260a94..5de0447d35 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Strings/en-us/Resources.resw +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Strings/en-us/Resources.resw @@ -1,17 +1,17 @@ <?xml version="1.0" encoding="utf-8"?> <root> - <!-- - Microsoft ResX Schema - + <!-- + Microsoft ResX Schema + Version 2.0 - - The primary goals of this format is to allow a simple XML format - that is mostly human readable. The generation and parsing of the - various data types are done through the TypeConverter classes + + The primary goals of this format is to allow a simple XML format + that is mostly human readable. The generation and parsing of the + various data types are done through the TypeConverter classes associated with the data types. - + Example: - + ... ado.net/XML headers & schema ... <resheader name="resmimetype">text/microsoft-resx</resheader> <resheader name="version">2.0</resheader> @@ -26,36 +26,36 @@ <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> <comment>This is a comment</comment> </data> - - There are any number of "resheader" rows that contain simple + + There are any number of "resheader" rows that contain simple name/value pairs. - - Each data row contains a name, and value. The row also contains a - type or mimetype. Type corresponds to a .NET class that support - text/value conversion through the TypeConverter architecture. - Classes that don't support this are serialized and stored with the + + Each data row contains a name, and value. The row also contains a + type or mimetype. Type corresponds to a .NET class that support + text/value conversion through the TypeConverter architecture. + Classes that don't support this are serialized and stored with the mimetype set. - - The mimetype is used for serialized objects, and tells the - ResXResourceReader how to depersist the object. This is currently not + + The mimetype is used for serialized objects, and tells the + ResXResourceReader how to depersist the object. This is currently not extensible. For a given mimetype the value must be set accordingly: - - Note - application/x-microsoft.net.object.binary.base64 is the format - that the ResXResourceWriter will generate, however the reader can + + Note - application/x-microsoft.net.object.binary.base64 is the format + that the ResXResourceWriter will generate, however the reader can read any of the formats listed below. - + mimetype: application/x-microsoft.net.object.binary.base64 - value : The object must be serialized with + value : The object must be serialized with : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter : and then encoded with base64 encoding. - + mimetype: application/x-microsoft.net.object.soap.base64 - value : The object must be serialized with + value : The object must be serialized with : System.Runtime.Serialization.Formatters.Soap.SoapFormatter : and then encoded with base64 encoding. mimetype: application/x-microsoft.net.object.bytearray.base64 - value : The object must be serialized into a byte array + value : The object must be serialized into a byte array : using a System.ComponentModel.TypeConverter : and then encoded with base64 encoding. --> @@ -706,4 +706,22 @@ Right-click to remove the key combination, thereby deactivating the shortcut.</v <data name="Settings_GeneralPage_AppTheme_Mode_System_Automation.[using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name" xml:space="preserve"> <value>Use system settings</value> </data> + <data name="Settings_FallbacksPage_GlobalResults_SettingsCard.Header" xml:space="preserve"> + <value>Include in the Global result</value> + </data> + <data name="Settings_FallbacksPage_GlobalResults_SettingsCard.Description" xml:space="preserve"> + <value>Show results on queries without direct activation command</value> + </data> + <data name="ManageFallbackRankAutomation.[using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name" xml:space="preserve"> + <value>Manage fallback order</value> + </data> + <data name="ManageFallbackRank.Text" xml:space="preserve"> + <value>Manage fallback order</value> + </data> + <data name="ManageFallbackOrderDialogDescription.Text" xml:space="preserve"> + <value>Drag items to set which fallback commands run first; commands at the top take priority.</value> + </data> + <data name="Settings_ExtensionsPage_More_ReorderFallbacks_MenuFlyoutItem.Text" xml:space="preserve"> + <value>Manage fallback order</value> + </data> </root> \ No newline at end of file diff --git a/src/modules/cmdpal/Tests/Microsoft.CmdPal.UI.ViewModels.UnitTests/MainListPageResultFactoryTests.cs b/src/modules/cmdpal/Tests/Microsoft.CmdPal.UI.ViewModels.UnitTests/MainListPageResultFactoryTests.cs index 624fa2da73..5a1e4bff54 100644 --- a/src/modules/cmdpal/Tests/Microsoft.CmdPal.UI.ViewModels.UnitTests/MainListPageResultFactoryTests.cs +++ b/src/modules/cmdpal/Tests/Microsoft.CmdPal.UI.ViewModels.UnitTests/MainListPageResultFactoryTests.cs @@ -96,7 +96,7 @@ public partial class MainListPageResultFactoryTests var titles = result.Select(r => r.Title).ToArray(); #pragma warning disable CA1861 // Avoid constant arrays as arguments CollectionAssert.AreEqual( - new[] { "F1", "SF1", "A1", "SF2", "A2", "F2", "FB1", "FB2" }, + new[] { "F1", "SF1", "A1", "SF2", "A2", "F2", "Fallbacks", "FB1", "FB2" }, titles); #pragma warning restore CA1861 // Avoid constant arrays as arguments } @@ -129,7 +129,6 @@ public partial class MainListPageResultFactoryTests var fallbacks = new List<Scored<IListItem>> { S("FB1", 0), - S(string.Empty, 0), S("FB3", 0), }; @@ -140,9 +139,10 @@ public partial class MainListPageResultFactoryTests fallbacks, appResultLimit: 10); - Assert.AreEqual(2, result.Length); - Assert.AreEqual("FB1", result[0].Title); - Assert.AreEqual("FB3", result[1].Title); + Assert.AreEqual(3, result.Length); + Assert.AreEqual("Fallbacks", result[0].Title); + Assert.AreEqual("FB1", result[1].Title); + Assert.AreEqual("FB3", result[2].Title); } [TestMethod] diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs index 4367c67810..ebf33094f2 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Calc/Pages/FallbackCalculatorItem.cs @@ -10,11 +10,12 @@ namespace Microsoft.CmdPal.Ext.Calc.Pages; public sealed partial class FallbackCalculatorItem : FallbackCommandItem { + private const string _id = "com.microsoft.cmdpal.builtin.calculator.fallback"; private readonly CopyTextCommand _copyCommand = new(string.Empty); private readonly ISettingsInterface _settings; public FallbackCalculatorItem(ISettingsInterface settings) - : base(new NoOpCommand(), Resources.calculator_title) + : base(new NoOpCommand(), Resources.calculator_title, _id) { Command = _copyCommand; _copyCommand.Name = string.Empty; diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.ClipboardHistory/Pages/ClipboardListItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.ClipboardHistory/Pages/ClipboardListItem.cs index 865d8f6b91..bd1ad3d1c1 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.ClipboardHistory/Pages/ClipboardListItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.ClipboardHistory/Pages/ClipboardListItem.cs @@ -12,6 +12,8 @@ using Microsoft.CmdPal.Ext.ClipboardHistory.Helpers; using Microsoft.CmdPal.Ext.ClipboardHistory.Helpers.Analyzers; using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions.Toolkit; +using Windows.ApplicationModel.DataTransfer; +using WinRT; namespace Microsoft.CmdPal.Ext.ClipboardHistory.Models; @@ -62,6 +64,8 @@ internal sealed partial class ClipboardListItem : ListItem RequestedShortcut = KeyChords.DeleteEntry, }; + DataPackageView = _item.Item.Content; + if (item.IsImage) { Title = "Image"; diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Data/IndexerListItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Data/IndexerListItem.cs index 7bb1fb4733..51a30ddc86 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Data/IndexerListItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Data/IndexerListItem.cs @@ -2,14 +2,17 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Collections.Generic; using System.IO; using System.Linq; using Microsoft.CmdPal.Core.Common.Commands; +using Microsoft.CmdPal.Ext.Indexer.Helpers; using Microsoft.CmdPal.Ext.Indexer.Pages; using Microsoft.CmdPal.Ext.Indexer.Properties; using Microsoft.CommandPalette.Extensions.Toolkit; using Windows.Foundation.Metadata; +using FileAttributes = System.IO.FileAttributes; namespace Microsoft.CmdPal.Ext.Indexer.Data; @@ -36,6 +39,8 @@ internal sealed partial class IndexerListItem : ListItem Title = indexerItem.FileName; Subtitle = indexerItem.FullPath; + DataPackage = DataPackageHelper.CreateDataPackageForPath(this, FilePath); + var commands = FileCommands(indexerItem.FullPath, browseByDefault); if (commands.Any()) { diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/FallbackOpenFileItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/FallbackOpenFileItem.cs index 5e044247ba..967e962085 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/FallbackOpenFileItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/FallbackOpenFileItem.cs @@ -7,6 +7,7 @@ using System.Globalization; using System.IO; using System.Text; using Microsoft.CmdPal.Ext.Indexer.Data; +using Microsoft.CmdPal.Ext.Indexer.Helpers; using Microsoft.CmdPal.Ext.Indexer.Properties; using Microsoft.CommandPalette.Extensions.Toolkit; using Windows.Storage.Streams; @@ -15,7 +16,8 @@ namespace Microsoft.CmdPal.Ext.Indexer; internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System.IDisposable { - private static readonly NoOpCommand _baseCommandWithId = new() { Id = "com.microsoft.indexer.fallback" }; + private const string _id = "com.microsoft.cmdpal.builtin.indexer.fallback"; + private static readonly NoOpCommand _baseCommandWithId = new() { Id = _id }; private readonly CompositeFormat fallbackItemSearchPageTitleCompositeFormat = CompositeFormat.Parse(Resources.Indexer_fallback_searchPage_title); @@ -26,7 +28,7 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System private Func<string, bool> _suppressCallback; public FallbackOpenFileItem() - : base(_baseCommandWithId, Resources.Indexer_Find_Path_fallback_display_title) + : base(_baseCommandWithId, Resources.Indexer_Find_Path_fallback_display_title, _id) { Title = string.Empty; Subtitle = string.Empty; @@ -42,6 +44,7 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System Subtitle = string.Empty; Icon = null; MoreCommands = null; + DataPackage = null; return; } @@ -53,6 +56,7 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System Subtitle = string.Empty; Icon = null; MoreCommands = null; + DataPackage = null; return; } @@ -67,6 +71,7 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System Subtitle = item.FileName; Title = item.FullPath; Icon = listItemForUs.Icon; + DataPackage = DataPackageHelper.CreateDataPackageForPath(listItemForUs, item.FullPath); try { @@ -92,13 +97,15 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System _searchEngine.Query(query, _queryCookie); var results = _searchEngine.FetchItems(0, 20, _queryCookie, out var _); - if (results.Count == 0 || ((results[0] as IndexerListItem) is null)) + if (results.Count == 0 || (results[0] is not IndexerListItem indexerListItem)) { // Exit 2: We searched for the file, and found nothing. Oh well. // Hide ourselves. Title = string.Empty; Subtitle = string.Empty; Command = new NoOpCommand(); + MoreCommands = null; + DataPackage = null; return; } @@ -106,11 +113,12 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System { // Exit 3: We searched for the file, and found exactly one thing. Awesome! // Return it. - Title = results[0].Title; - Subtitle = results[0].Subtitle; - Icon = results[0].Icon; - Command = results[0].Command; - MoreCommands = results[0].MoreCommands; + Title = indexerListItem.Title; + Subtitle = indexerListItem.Subtitle; + Icon = indexerListItem.Icon; + Command = indexerListItem.Command; + MoreCommands = indexerListItem.MoreCommands; + DataPackage = DataPackageHelper.CreateDataPackageForPath(indexerListItem, indexerListItem.FilePath); return; } @@ -121,6 +129,8 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System Title = string.Format(CultureInfo.CurrentCulture, fallbackItemSearchPageTitleCompositeFormat, query); Icon = Icons.FileExplorerIcon; Command = indexerPage; + MoreCommands = null; + DataPackage = null; return; } @@ -131,6 +141,7 @@ internal sealed partial class FallbackOpenFileItem : FallbackCommandItem, System Icon = null; Command = new NoOpCommand(); MoreCommands = null; + DataPackage = null; } } } diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Helpers/DataPackageHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Helpers/DataPackageHelper.cs new file mode 100644 index 0000000000..65d18a0e2a --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Helpers/DataPackageHelper.cs @@ -0,0 +1,64 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.CommandPalette.Extensions; +using Windows.ApplicationModel.DataTransfer; +using Windows.Storage; +using File = System.IO.File; + +namespace Microsoft.CmdPal.Ext.Indexer.Helpers; + +internal static class DataPackageHelper +{ + public static DataPackage CreateDataPackageForPath(ICommandItem listItem, string path) + { + if (string.IsNullOrEmpty(path)) + { + return null; + } + + var dataPackage = new DataPackage(); + dataPackage.SetText(path); + _ = dataPackage.TrySetStorageItemsAsync(path); + dataPackage.Properties.Title = listItem.Title; + dataPackage.Properties.Description = listItem.Subtitle; + dataPackage.RequestedOperation = DataPackageOperation.Copy; + return dataPackage; + } + + public static async Task<bool> TrySetStorageItemsAsync(this DataPackage dataPackage, string filePath) + { + try + { + if (File.Exists(filePath)) + { + var file = await StorageFile.GetFileFromPathAsync(filePath); + dataPackage.SetStorageItems([file]); + return true; + } + + if (Directory.Exists(filePath)) + { + var folder = await StorageFolder.GetFolderFromPathAsync(filePath); + dataPackage.SetStorageItems([folder]); + return true; + } + + // nothing there + return false; + } + catch (UnauthorizedAccessException) + { + // Access denied – skip or report, but don't crash + return false; + } + catch (Exception) + { + return false; + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Pages/ExploreListItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Pages/ExploreListItem.cs index 79a47543b6..9bd575e87a 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Pages/ExploreListItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Indexer/Pages/ExploreListItem.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using Microsoft.CmdPal.Core.Common.Commands; using Microsoft.CmdPal.Ext.Indexer.Data; +using Microsoft.CmdPal.Ext.Indexer.Helpers; using Microsoft.CmdPal.Ext.Indexer.Properties; using Microsoft.CommandPalette.Extensions.Toolkit; using Windows.Foundation; @@ -28,6 +29,9 @@ internal sealed partial class ExploreListItem : ListItem Title = indexerItem.FileName; Subtitle = indexerItem.FullPath; + + DataPackage = DataPackageHelper.CreateDataPackageForPath(this, FilePath); + List<CommandContextItem> context = []; if (indexerItem.IsDirectory()) { diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/AdvancedPaste/OpenAdvancedPasteCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/AdvancedPaste/OpenAdvancedPasteCommand.cs new file mode 100644 index 0000000000..4e738a7342 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/AdvancedPaste/OpenAdvancedPasteCommand.cs @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Opens Advanced Paste UI by signaling the module's show event. +/// The DLL interface handles starting the process if it's not running. +/// </summary> +internal sealed partial class OpenAdvancedPasteCommand : InvokableCommand +{ + public OpenAdvancedPasteCommand() + { + Name = "Open Advanced Paste"; + } + + public override CommandResult Invoke() + { + try + { + using var showEvent = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.AdvancedPasteShowUIEvent()); + showEvent.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to open Advanced Paste: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Awake/RefreshAwakeStatusCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Awake/RefreshAwakeStatusCommand.cs new file mode 100644 index 0000000000..7327090fd3 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Awake/RefreshAwakeStatusCommand.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Microsoft.CommandPalette.Extensions.Toolkit; + +namespace PowerToysExtension.Commands; + +internal sealed partial class RefreshAwakeStatusCommand : InvokableCommand +{ + private readonly Action _refreshAction; + + internal RefreshAwakeStatusCommand(Action refreshAction) + { + ArgumentNullException.ThrowIfNull(refreshAction); + _refreshAction = refreshAction; + Name = "Refresh Awake status"; + } + + public override CommandResult Invoke() + { + _refreshAction(); + return CommandResult.KeepOpen(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Awake/StartAwakeCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Awake/StartAwakeCommand.cs new file mode 100644 index 0000000000..d4695535ff --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Awake/StartAwakeCommand.cs @@ -0,0 +1,59 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading.Tasks; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.ModuleContracts; + +namespace PowerToysExtension.Commands; + +internal sealed partial class StartAwakeCommand : InvokableCommand +{ + private readonly Func<Task<OperationResult>> _action; + private readonly string _successToast; + private readonly Action? _onSuccess; + + internal StartAwakeCommand(string title, Func<Task<OperationResult>> action, string successToast = "", Action? onSuccess = null) + { + ArgumentNullException.ThrowIfNull(action); + ArgumentException.ThrowIfNullOrWhiteSpace(title); + + _action = action; + _successToast = successToast ?? string.Empty; + _onSuccess = onSuccess; + Name = title; + } + + public override CommandResult Invoke() + { + try + { + var result = _action().GetAwaiter().GetResult(); + if (!result.Success) + { + return ShowToastKeepOpen(result.Error ?? "Failed to start Awake."); + } + + _onSuccess?.Invoke(); + + return string.IsNullOrWhiteSpace(_successToast) + ? CommandResult.KeepOpen() + : ShowToastKeepOpen(_successToast); + } + catch (Exception ex) + { + return ShowToastKeepOpen($"Launching Awake failed: {ex.Message}"); + } + } + + private static CommandResult ShowToastKeepOpen(string message) + { + return CommandResult.ShowToast(new ToastArgs() + { + Message = message, + Result = CommandResult.KeepOpen(), + }); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Awake/StopAwakeCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Awake/StopAwakeCommand.cs new file mode 100644 index 0000000000..426c039437 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Awake/StopAwakeCommand.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Awake.ModuleServices; +using Microsoft.CommandPalette.Extensions.Toolkit; + +namespace PowerToysExtension.Commands; + +internal sealed partial class StopAwakeCommand : InvokableCommand +{ + private readonly Action? _onSuccess; + + internal StopAwakeCommand(Action? onSuccess = null) + { + _onSuccess = onSuccess; + Name = "Set Awake to Off"; + } + + public override CommandResult Invoke() + { + try + { + var result = AwakeService.Instance.SetOffAsync().GetAwaiter().GetResult(); + if (result.Success) + { + _onSuccess?.Invoke(); + return ShowToastKeepOpen("Awake switched to Off."); + } + + return ShowToastKeepOpen(result.Error ?? "Awake does not appear to be running."); + } + catch (Exception ex) + { + return ShowToastKeepOpen($"Failed to switch Awake off: {ex.Message}"); + } + } + + private static CommandResult ShowToastKeepOpen(string message) + { + return CommandResult.ShowToast(new ToastArgs() + { + Message = message, + Result = CommandResult.KeepOpen(), + }); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ColorPicker/CopySavedColorCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ColorPicker/CopySavedColorCommand.cs new file mode 100644 index 0000000000..96b43a9a17 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ColorPicker/CopySavedColorCommand.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using ColorPicker.ModuleServices; +using Microsoft.CommandPalette.Extensions.Toolkit; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Copies a saved color in a chosen format. +/// </summary> +internal sealed partial class CopySavedColorCommand : InvokableCommand +{ + private readonly SavedColor _color; + private readonly string _copyValue; + + public CopySavedColorCommand(SavedColor color, string copyValue) + { + _color = color; + _copyValue = copyValue; + Name = $"Copy {_color.Hex}"; + } + + public override CommandResult Invoke() + { + try + { + ClipboardHelper.SetText(_copyValue); + + return CommandResult.ShowToast($"Copied {_copyValue}"); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to copy color: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ColorPicker/OpenColorPickerCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ColorPicker/OpenColorPickerCommand.cs new file mode 100644 index 0000000000..6982c5dffe --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ColorPicker/OpenColorPickerCommand.cs @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using ColorPicker.ModuleServices; +using Microsoft.CommandPalette.Extensions.Toolkit; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Opens the Color Picker picker session via shared event. +/// </summary> +internal sealed partial class OpenColorPickerCommand : InvokableCommand +{ + public OpenColorPickerCommand() + { + Name = "Open Color Picker"; + } + + public override CommandResult Invoke() + { + try + { + var result = ColorPickerService.Instance.OpenPickerAsync().GetAwaiter().GetResult(); + if (!result.Success) + { + return CommandResult.ShowToast(result.Error ?? "Failed to open Color Picker."); + } + + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to open Color Picker: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/CropAndLock/CropAndLockReparentCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/CropAndLock/CropAndLockReparentCommand.cs new file mode 100644 index 0000000000..417ab34a5d --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/CropAndLock/CropAndLockReparentCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Triggers Crop and Lock reparent mode via the shared event. +/// </summary> +internal sealed partial class CropAndLockReparentCommand : InvokableCommand +{ + public CropAndLockReparentCommand() + { + Name = "Crop and Lock (Reparent)"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.CropAndLockReparentEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to start Crop and Lock (Reparent): {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/CropAndLock/CropAndLockThumbnailCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/CropAndLock/CropAndLockThumbnailCommand.cs new file mode 100644 index 0000000000..b9996f7835 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/CropAndLock/CropAndLockThumbnailCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Triggers Crop and Lock thumbnail mode via the shared event. +/// </summary> +internal sealed partial class CropAndLockThumbnailCommand : InvokableCommand +{ + public CropAndLockThumbnailCommand() + { + Name = "Crop and Lock (Thumbnail)"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.CropAndLockThumbnailEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to start Crop and Lock (Thumbnail): {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/EnvironmentVariables/OpenEnvironmentVariablesAdminCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/EnvironmentVariables/OpenEnvironmentVariablesAdminCommand.cs new file mode 100644 index 0000000000..6961783325 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/EnvironmentVariables/OpenEnvironmentVariablesAdminCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Launches Environment Variables (admin) via the shared event. +/// </summary> +internal sealed partial class OpenEnvironmentVariablesAdminCommand : InvokableCommand +{ + public OpenEnvironmentVariablesAdminCommand() + { + Name = "Open Environment Variables (Admin)"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.ShowEnvironmentVariablesAdminSharedEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to open Environment Variables (Admin): {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/EnvironmentVariables/OpenEnvironmentVariablesCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/EnvironmentVariables/OpenEnvironmentVariablesCommand.cs new file mode 100644 index 0000000000..71bb81068d --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/EnvironmentVariables/OpenEnvironmentVariablesCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Launches Environment Variables (user) via the shared event. +/// </summary> +internal sealed partial class OpenEnvironmentVariablesCommand : InvokableCommand +{ + public OpenEnvironmentVariablesCommand() + { + Name = "Open Environment Variables"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.ShowEnvironmentVariablesSharedEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to open Environment Variables: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/ApplyFancyZonesLayoutCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/ApplyFancyZonesLayoutCommand.cs new file mode 100644 index 0000000000..b4ef1e55c9 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/ApplyFancyZonesLayoutCommand.cs @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Commands; + +internal sealed partial class ApplyFancyZonesLayoutCommand : InvokableCommand +{ + private readonly FancyZonesLayoutDescriptor _layout; + private readonly FancyZonesMonitorDescriptor? _targetMonitor; + + public ApplyFancyZonesLayoutCommand(FancyZonesLayoutDescriptor layout, FancyZonesMonitorDescriptor? monitor) + { + _layout = layout; + _targetMonitor = monitor; + + Name = monitor is null ? "Apply to all monitors" : $"Apply to Monitor {monitor.Value.Title}"; + + Icon = new IconInfo("\uF78C"); + } + + public override CommandResult Invoke() + { + var monitor = _targetMonitor; + var (success, message) = monitor is null + ? FancyZonesDataService.ApplyLayoutToAllMonitors(_layout) + : FancyZonesDataService.ApplyLayoutToMonitor(_layout, monitor.Value); + + return success + ? CommandResult.Dismiss() + : CommandResult.ShowToast(message); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/FancyZonesLayoutListItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/FancyZonesLayoutListItem.cs new file mode 100644 index 0000000000..b2e7077fee --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/FancyZonesLayoutListItem.cs @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using System.Threading.Tasks; +using ManagedCommon; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Pages; + +internal sealed partial class FancyZonesLayoutListItem : ListItem +{ + private readonly Lazy<Task<IconInfo?>> _iconLoadTask; + private readonly string _layoutId; + private readonly string _layoutTitle; + + private int _isLoadingIcon; + + public override IIconInfo? Icon + { + get + { + if (Interlocked.Exchange(ref _isLoadingIcon, 1) == 0) + { + _ = LoadIconAsync(); + } + + return base.Icon; + } + set => base.Icon = value; + } + + public FancyZonesLayoutListItem(ICommand command, FancyZonesLayoutDescriptor layout, IconInfo fallbackIcon) + : base(command) + { + Title = layout.Title; + Subtitle = layout.Subtitle; + Icon = fallbackIcon; + _layoutId = layout.Id; + _layoutTitle = layout.Title; + + _iconLoadTask = new Lazy<Task<IconInfo?>>(async () => await FancyZonesThumbnailRenderer.RenderLayoutIconAsync(layout)); + } + + private async Task LoadIconAsync() + { + try + { + Logger.LogDebug($"FancyZones layout icon load starting. LayoutId={_layoutId} Title=\"{_layoutTitle}\""); + var icon = await _iconLoadTask.Value; + if (icon is not null) + { + Icon = icon; + Logger.LogDebug($"FancyZones layout icon load succeeded. LayoutId={_layoutId} Title=\"{_layoutTitle}\""); + } + else + { + Logger.LogDebug($"FancyZones layout icon load returned null. LayoutId={_layoutId} Title=\"{_layoutTitle}\""); + } + } + catch (Exception ex) + { + Logger.LogWarning($"FancyZones layout icon load failed. LayoutId={_layoutId} Title=\"{_layoutTitle}\" Exception={ex}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/FancyZonesMonitorListItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/FancyZonesMonitorListItem.cs new file mode 100644 index 0000000000..c65db779df --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/FancyZonesMonitorListItem.cs @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Globalization; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Pages; + +internal sealed partial class FancyZonesMonitorListItem : ListItem +{ + public FancyZonesMonitorListItem(FancyZonesMonitorDescriptor monitor, string subtitle, IconInfo icon) + : base(new IdentifyFancyZonesMonitorCommand(monitor)) + { + Title = monitor.Title; + Subtitle = subtitle; + Icon = icon; + + Details = BuildMonitorDetails(monitor); + + var pickerPage = new FancyZonesMonitorLayoutPickerPage(monitor) + { + Name = "Set active layout", + }; + + MoreCommands = + [ + new CommandContextItem(pickerPage) + { + Title = "Set active layout", + Subtitle = "Pick a layout for this monitor", + }, + ]; + } + + public static Details BuildMonitorDetails(FancyZonesMonitorDescriptor monitor) + { + var currentVirtualDesktop = FancyZonesVirtualDesktop.GetCurrentVirtualDesktopIdString(); + var tags = new List<IDetailsElement> + { + DetailTag("Monitor", monitor.Data.Monitor), + DetailTag("Instance", monitor.Data.MonitorInstanceId), + DetailTag("Serial", monitor.Data.MonitorSerialNumber), + DetailTag("Number", monitor.Data.MonitorNumber.ToString(CultureInfo.InvariantCulture)), + DetailTag("Virtual desktop", currentVirtualDesktop), + DetailTag("Work area", $"{monitor.Data.LeftCoordinate},{monitor.Data.TopCoordinate} {monitor.Data.WorkAreaWidth}\u00D7{monitor.Data.WorkAreaHeight}"), + DetailTag("Resolution", $"{monitor.Data.MonitorWidth}\u00D7{monitor.Data.MonitorHeight}"), + DetailTag("DPI", monitor.Data.Dpi.ToString(CultureInfo.InvariantCulture)), + }; + + return new Details + { + Title = monitor.Title, + HeroImage = FancyZonesMonitorPreviewRenderer.TryRenderMonitorHeroImage(monitor) ?? + PowerToysResourcesHelper.IconFromSettingsIcon("FancyZones.png"), + Metadata = tags.ToArray(), + }; + } + + private static DetailsElement DetailTag(string key, string? value) + { + return new DetailsElement + { + Key = key, + Data = new DetailsTags + { + Tags = [new Tag(string.IsNullOrWhiteSpace(value) ? "n/a" : value)], + }, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/IdentifyFancyZonesMonitorCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/IdentifyFancyZonesMonitorCommand.cs new file mode 100644 index 0000000000..6e4b8b45c5 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/IdentifyFancyZonesMonitorCommand.cs @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Linq; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Commands; + +internal sealed partial class IdentifyFancyZonesMonitorCommand : InvokableCommand +{ + private readonly FancyZonesMonitorDescriptor _monitor; + + public IdentifyFancyZonesMonitorCommand(FancyZonesMonitorDescriptor monitor) + { + _monitor = monitor; + Name = $"Identify {_monitor.Title}"; + Icon = new IconInfo("\uE773"); + } + + public override CommandResult Invoke() + { + if (!FancyZonesDataService.TryGetMonitors(out var monitors, out var error)) + { + return CommandResult.ShowToast(error); + } + + var monitor = monitors.FirstOrDefault(m => m.Data.MonitorInstanceId == _monitor.Data.MonitorInstanceId); + + if (monitor == null) + { + return CommandResult.ShowToast("Monitor not found."); + } + + FancyZonesMonitorIdentifier.Show( + monitor.Data.LeftCoordinate, + monitor.Data.TopCoordinate, + monitor.Data.WorkAreaWidth, + monitor.Data.WorkAreaHeight, + _monitor.Title, + durationMs: 1200); + + return CommandResult.KeepOpen(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/OpenFancyZonesEditorCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/OpenFancyZonesEditorCommand.cs new file mode 100644 index 0000000000..9376fba709 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/FancyZones/OpenFancyZonesEditorCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Launches the FancyZones layout editor via the shared event. +/// </summary> +internal sealed partial class OpenFancyZonesEditorCommand : InvokableCommand +{ + public OpenFancyZonesEditorCommand() + { + Name = "Open FancyZones Editor"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.FZEToggleEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to open FancyZones editor: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Hosts/OpenHostsEditorAdminCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Hosts/OpenHostsEditorAdminCommand.cs new file mode 100644 index 0000000000..63bd74d62a --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Hosts/OpenHostsEditorAdminCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Launches Hosts File Editor (Admin) via the shared event. +/// </summary> +internal sealed partial class OpenHostsEditorAdminCommand : InvokableCommand +{ + public OpenHostsEditorAdminCommand() + { + Name = "Open Hosts File Editor (Admin)"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.ShowHostsAdminSharedEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to open Hosts File Editor (Admin): {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Hosts/OpenHostsEditorCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Hosts/OpenHostsEditorCommand.cs new file mode 100644 index 0000000000..fdf5c807d0 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Hosts/OpenHostsEditorCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Launches Hosts File Editor via the shared event. +/// </summary> +internal sealed partial class OpenHostsEditorCommand : InvokableCommand +{ + public OpenHostsEditorCommand() + { + Name = "Open Hosts File Editor"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.ShowHostsSharedEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to open Hosts File Editor: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/LaunchModuleCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/LaunchModuleCommand.cs new file mode 100644 index 0000000000..3101a73030 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/LaunchModuleCommand.cs @@ -0,0 +1,121 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Launches a PowerToys module either by raising its shared event or starting the backing executable. +/// </summary> +internal sealed partial class LaunchModuleCommand : InvokableCommand +{ + private readonly string _moduleName; + private readonly string _eventName; + private readonly string _executableName; + private readonly string _arguments; + + internal LaunchModuleCommand( + string moduleName, + string eventName = "", + string executableName = "", + string arguments = "", + string displayName = "") + { + if (string.IsNullOrWhiteSpace(moduleName)) + { + throw new ArgumentException("Module name is required", nameof(moduleName)); + } + + _moduleName = moduleName; + _eventName = eventName ?? string.Empty; + _executableName = executableName ?? string.Empty; + _arguments = arguments ?? string.Empty; + Name = string.IsNullOrWhiteSpace(displayName) ? $"Launch {moduleName}" : displayName; + } + + public override CommandResult Invoke() + { + try + { + if (TrySignalEvent()) + { + return CommandResult.Hide(); + } + + if (TryLaunchExecutable()) + { + return CommandResult.Hide(); + } + + return CommandResult.ShowToast($"Unable to launch {_moduleName}."); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Launching {_moduleName} failed: {ex.Message}"); + } + } + + private bool TrySignalEvent() + { + if (string.IsNullOrEmpty(_eventName)) + { + return false; + } + + try + { + using var existingHandle = EventWaitHandle.OpenExisting(_eventName); + return existingHandle.Set(); + } + catch (WaitHandleCannotBeOpenedException) + { + try + { + using var newHandle = new EventWaitHandle(false, EventResetMode.AutoReset, _eventName, out _); + return newHandle.Set(); + } + catch + { + return false; + } + } + catch + { + return false; + } + } + + private bool TryLaunchExecutable() + { + if (string.IsNullOrEmpty(_executableName)) + { + return false; + } + + var executablePath = PowerToysPathResolver.TryResolveExecutable(_executableName); + if (string.IsNullOrEmpty(executablePath)) + { + return false; + } + + var startInfo = new ProcessStartInfo(executablePath) + { + UseShellExecute = true, + }; + + if (!string.IsNullOrWhiteSpace(_arguments)) + { + startInfo.Arguments = _arguments; + startInfo.UseShellExecute = false; + } + + Process.Start(startInfo); + return true; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/LightSwitch/ToggleLightSwitchCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/LightSwitch/ToggleLightSwitchCommand.cs new file mode 100644 index 0000000000..8702d7630a --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/LightSwitch/ToggleLightSwitchCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Toggles Light Switch via the shared event. +/// </summary> +internal sealed partial class ToggleLightSwitchCommand : InvokableCommand +{ + public ToggleLightSwitchCommand() + { + Name = "Toggle Light Switch"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.LightSwitchToggleEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to toggle Light Switch: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ShowMouseJumpPreviewCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ShowMouseJumpPreviewCommand.cs new file mode 100644 index 0000000000..f0c3b9af32 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ShowMouseJumpPreviewCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Shows Mouse Jump preview via the shared event. +/// </summary> +internal sealed partial class ShowMouseJumpPreviewCommand : InvokableCommand +{ + public ShowMouseJumpPreviewCommand() + { + Name = "Show Mouse Jump Preview"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.MouseJumpShowPreviewEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to show Mouse Jump preview: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleCursorWrapCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleCursorWrapCommand.cs new file mode 100644 index 0000000000..9e6d9e6817 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleCursorWrapCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Toggles Cursor Wrap via the shared trigger event. +/// </summary> +internal sealed partial class ToggleCursorWrapCommand : InvokableCommand +{ + public ToggleCursorWrapCommand() + { + Name = "Toggle Cursor Wrap"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.CursorWrapTriggerEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to toggle Cursor Wrap: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleFindMyMouseCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleFindMyMouseCommand.cs new file mode 100644 index 0000000000..f8bb115789 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleFindMyMouseCommand.cs @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Triggers Find My Mouse via the shared event. +/// </summary> +internal sealed partial class ToggleFindMyMouseCommand : InvokableCommand +{ + public ToggleFindMyMouseCommand() + { + Name = "Trigger Find My Mouse"; + } + + public override CommandResult Invoke() + { + // Delay the trigger so the Command Palette dismisses first + _ = Task.Run(async () => + { + await Task.Delay(200).ConfigureAwait(false); + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.FindMyMouseTriggerEvent()); + evt.Set(); + } + catch + { + // Ignore errors in background task + } + }); + + return CommandResult.Dismiss(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleMouseCrosshairsCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleMouseCrosshairsCommand.cs new file mode 100644 index 0000000000..2209a60d58 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleMouseCrosshairsCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Toggles Mouse Pointer Crosshairs via the shared event. +/// </summary> +internal sealed partial class ToggleMouseCrosshairsCommand : InvokableCommand +{ + public ToggleMouseCrosshairsCommand() + { + Name = "Toggle Mouse Crosshairs"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.MouseCrosshairsTriggerEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to toggle Mouse Crosshairs: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleMouseHighlighterCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleMouseHighlighterCommand.cs new file mode 100644 index 0000000000..1485885723 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/MouseUtils/ToggleMouseHighlighterCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Toggles Mouse Highlighter via the shared event. +/// </summary> +internal sealed partial class ToggleMouseHighlighterCommand : InvokableCommand +{ + public ToggleMouseHighlighterCommand() + { + Name = "Toggle Mouse Highlighter"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.MouseHighlighterTriggerEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to toggle Mouse Highlighter: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/OpenInSettingsCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/OpenInSettingsCommand.cs new file mode 100644 index 0000000000..65860c249a --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/OpenInSettingsCommand.cs @@ -0,0 +1,28 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Common.UI; +using Microsoft.CommandPalette.Extensions.Toolkit; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Opens the PowerToys settings page for the given module via SettingsDeepLink. +/// </summary> +internal sealed partial class OpenInSettingsCommand : InvokableCommand +{ + private readonly SettingsDeepLink.SettingsWindow _module; + + internal OpenInSettingsCommand(SettingsDeepLink.SettingsWindow module, string title = "") + { + _module = module; + Name = string.IsNullOrWhiteSpace(title) ? $"Open {_module} settings" : title; + } + + public override CommandResult Invoke() + { + SettingsDeepLink.OpenSettings(_module); + return CommandResult.Hide(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/OpenPowerToysSettingsCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/OpenPowerToysSettingsCommand.cs new file mode 100644 index 0000000000..bd865fcab1 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/OpenPowerToysSettingsCommand.cs @@ -0,0 +1,61 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Diagnostics; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Opens the PowerToys settings application deep linked to a specific module. +/// </summary> +internal sealed partial class OpenPowerToysSettingsCommand : InvokableCommand +{ + private readonly string _moduleName; + private readonly string _settingsKey; + + internal OpenPowerToysSettingsCommand(string moduleName, string settingsKey) + { + if (string.IsNullOrWhiteSpace(moduleName)) + { + throw new ArgumentException("Module name is required", nameof(moduleName)); + } + + if (string.IsNullOrWhiteSpace(settingsKey)) + { + throw new ArgumentException("Settings key is required", nameof(settingsKey)); + } + + _moduleName = moduleName; + _settingsKey = settingsKey; + Name = $"Open {_moduleName} settings"; + } + + public override CommandResult Invoke() + { + try + { + var powerToysPath = PowerToysPathResolver.TryResolveExecutable("PowerToys.exe"); + if (string.IsNullOrEmpty(powerToysPath)) + { + return CommandResult.ShowToast("Unable to locate PowerToys."); + } + + var startInfo = new ProcessStartInfo(powerToysPath) + { + Arguments = $"--open-settings={_settingsKey}", + UseShellExecute = false, + }; + + Process.Start(startInfo); + return CommandResult.Hide(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Opening {_moduleName} settings failed: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/RegistryPreview/OpenRegistryPreviewCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/RegistryPreview/OpenRegistryPreviewCommand.cs new file mode 100644 index 0000000000..6df382256f --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/RegistryPreview/OpenRegistryPreviewCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Launches Registry Preview via the shared event. +/// </summary> +internal sealed partial class OpenRegistryPreviewCommand : InvokableCommand +{ + public OpenRegistryPreviewCommand() + { + Name = "Open Registry Preview"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.RegistryPreviewTriggerEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to open Registry Preview: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ScreenRuler/ToggleScreenRulerCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ScreenRuler/ToggleScreenRulerCommand.cs new file mode 100644 index 0000000000..889cb5d7b9 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ScreenRuler/ToggleScreenRulerCommand.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +internal sealed partial class ToggleScreenRulerCommand : InvokableCommand +{ + public ToggleScreenRulerCommand() + { + Name = "Toggle Screen Ruler"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.MeasureToolTriggerEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to toggle Screen Ruler: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ShortcutGuide/ToggleShortcutGuideCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ShortcutGuide/ToggleShortcutGuideCommand.cs new file mode 100644 index 0000000000..4c6d056eaf --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ShortcutGuide/ToggleShortcutGuideCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Toggles the Shortcut Guide UI via the shared trigger event. +/// </summary> +internal sealed partial class ToggleShortcutGuideCommand : InvokableCommand +{ + public ToggleShortcutGuideCommand() + { + Name = "Toggle Shortcut Guide"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.ShortcutGuideTriggerEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to toggle Shortcut Guide: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/TextExtractor/ToggleTextExtractorCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/TextExtractor/ToggleTextExtractorCommand.cs new file mode 100644 index 0000000000..615fb0e395 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/TextExtractor/ToggleTextExtractorCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +/// <summary> +/// Triggers the Text Extractor UI via the existing show event. +/// </summary> +internal sealed partial class ToggleTextExtractorCommand : InvokableCommand +{ + public ToggleTextExtractorCommand() + { + Name = "Toggle Text Extractor"; + } + + public override CommandResult Invoke() + { + try + { + using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, Constants.ShowPowerOCRSharedEvent()); + evt.Set(); + return CommandResult.Dismiss(); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to toggle Text Extractor: {ex.Message}"); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Workspaces/LaunchWorkspaceCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Workspaces/LaunchWorkspaceCommand.cs new file mode 100644 index 0000000000..4372e5b7ff --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Workspaces/LaunchWorkspaceCommand.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CommandPalette.Extensions.Toolkit; +using Workspaces.ModuleServices; + +namespace PowerToysExtension.Commands; + +internal sealed partial class LaunchWorkspaceCommand : InvokableCommand +{ + private readonly string _workspaceId; + + internal LaunchWorkspaceCommand(string workspaceId) + { + _workspaceId = workspaceId; + Name = "Launch workspace"; + } + + public override CommandResult Invoke() + { + if (string.IsNullOrEmpty(_workspaceId)) + { + return CommandResult.KeepOpen(); + } + + var result = WorkspaceService.Instance.LaunchWorkspaceAsync(_workspaceId).GetAwaiter().GetResult(); + if (!result.Success) + { + return CommandResult.ShowToast(result.Error ?? "Launching workspace failed."); + } + + return CommandResult.Hide(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Workspaces/OpenWorkspaceEditorCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Workspaces/OpenWorkspaceEditorCommand.cs new file mode 100644 index 0000000000..63150902a6 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Workspaces/OpenWorkspaceEditorCommand.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CommandPalette.Extensions.Toolkit; +using Workspaces.ModuleServices; + +namespace PowerToysExtension.Commands; + +internal sealed partial class OpenWorkspaceEditorCommand : InvokableCommand +{ + public override CommandResult Invoke() + { + var result = WorkspaceService.Instance.LaunchEditorAsync().GetAwaiter().GetResult(); + if (!result.Success) + { + return CommandResult.ShowToast(result.Error ?? "Unable to launch the Workspaces editor."); + } + + return CommandResult.Hide(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Workspaces/WorkspaceListItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Workspaces/WorkspaceListItem.cs new file mode 100644 index 0000000000..1b2024e759 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/Workspaces/WorkspaceListItem.cs @@ -0,0 +1,117 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Globalization; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Helpers; +using WorkspacesCsharpLibrary.Data; + +namespace PowerToysExtension.Commands; + +internal sealed partial class WorkspaceListItem : ListItem +{ + public WorkspaceListItem(ProjectWrapper workspace, IconInfo icon) + : base(new LaunchWorkspaceCommand(workspace.Id)) + { + Title = workspace.Name; + Subtitle = BuildSubtitle(workspace); + Icon = icon; + Details = BuildDetails(workspace, icon); + } + + private static string BuildSubtitle(ProjectWrapper workspace) + { + var appCount = workspace.Applications?.Count ?? 0; + var appsText = appCount switch + { + 0 => "No applications", + _ => string.Format(CultureInfo.CurrentCulture, "{0} applications", appCount), + }; + + var lastLaunched = workspace.LastLaunchedTime > 0 + ? $"Last launched {FormatRelativeTime(workspace.LastLaunchedTime)}" + : "Never launched"; + + return $"{appsText} \u2022 {lastLaunched}"; + } + + private static Details BuildDetails(ProjectWrapper workspace, IconInfo icon) + { + var appCount = workspace.Applications?.Count ?? 0; + var body = appCount switch + { + 0 => "No applications in this workspace", + 1 => "1 application", + _ => $"{appCount} applications", + }; + + return new Details + { + HeroImage = icon, + Title = workspace.Name ?? "Workspace", + Body = body, + Metadata = BuildAppMetadata(workspace), + }; + } + + private static IDetailsElement[] BuildAppMetadata(ProjectWrapper workspace) + { + if (workspace.Applications is null || workspace.Applications.Count == 0) + { + return Array.Empty<IDetailsElement>(); + } + + var elements = new List<IDetailsElement>(); + foreach (var app in workspace.Applications) + { + var appName = string.IsNullOrWhiteSpace(app.Application) ? "App" : app.Application; + var title = string.IsNullOrWhiteSpace(app.Title) ? appName : app.Title; + + var tags = new List<ITag>(); + + if (!string.IsNullOrWhiteSpace(app.ApplicationPath)) + { + tags.Add(new Tag(app.ApplicationPath)); + } + else + { + tags.Add(new Tag(appName)); + } + + elements.Add(new DetailsElement + { + Key = title, + Data = new DetailsTags { Tags = tags.ToArray() }, + }); + } + + return elements.ToArray(); + } + + private static string FormatRelativeTime(long unixSeconds) + { + var lastLaunch = DateTimeOffset.FromUnixTimeSeconds(unixSeconds).UtcDateTime; + var delta = DateTime.UtcNow - lastLaunch; + + if (delta.TotalMinutes < 1) + { + return "just now"; + } + + if (delta.TotalMinutes < 60) + { + return string.Format(CultureInfo.CurrentCulture, "{0} min ago", (int)delta.TotalMinutes); + } + + if (delta.TotalHours < 24) + { + return string.Format(CultureInfo.CurrentCulture, "{0} hr ago", (int)delta.TotalHours); + } + + return string.Format(CultureInfo.CurrentCulture, "{0} days ago", (int)delta.TotalDays); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ZoomIt/ZoomItActionCommand.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ZoomIt/ZoomItActionCommand.cs new file mode 100644 index 0000000000..44b5ea447a --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Commands/ZoomIt/ZoomItActionCommand.cs @@ -0,0 +1,84 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToys.Interop; + +namespace PowerToysExtension.Commands; + +internal sealed partial class ZoomItActionCommand : InvokableCommand +{ + private readonly string _action; + private readonly string _title; + + public ZoomItActionCommand(string action, string title) + { + _action = action; + _title = title; + Name = title; + } + + public override CommandResult Invoke() + { + try + { + if (!TryGetEventName(_action, out var eventName)) + { + return CommandResult.ShowToast($"Unknown ZoomIt action: {_action}."); + } + + var evt = EventWaitHandle.OpenExisting(eventName); + _ = Task.Run(async () => + { + using (evt) + { + // Hide CmdPal first, then signal shortly after so UI like snip/zoom won't capture it. + await Task.Delay(50).ConfigureAwait(false); + evt.Set(); + } + }); + + return CommandResult.Hide(); + } + catch (WaitHandleCannotBeOpenedException) + { + return CommandResult.ShowToast("ZoomIt is not running. Please start it from PowerToys and try again."); + } + catch (Exception ex) + { + return CommandResult.ShowToast($"Failed to invoke ZoomIt ({_title}): {ex.Message}"); + } + } + + private static bool TryGetEventName(string action, out string eventName) + { + switch (action.ToLowerInvariant()) + { + case "zoom": + eventName = Constants.ZoomItZoomEvent(); + return true; + case "draw": + eventName = Constants.ZoomItDrawEvent(); + return true; + case "break": + eventName = Constants.ZoomItBreakEvent(); + return true; + case "livezoom": + eventName = Constants.ZoomItLiveZoomEvent(); + return true; + case "snip": + eventName = Constants.ZoomItSnipEvent(); + return true; + case "record": + eventName = Constants.ZoomItRecordEvent(); + return true; + default: + eventName = string.Empty; + return false; + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/AwakeStatusService.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/AwakeStatusService.cs new file mode 100644 index 0000000000..102c9e90fc --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/AwakeStatusService.cs @@ -0,0 +1,60 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Awake.ModuleServices; +using Common.UI; + +namespace PowerToysExtension.Helpers; + +internal static class AwakeStatusService +{ + internal static string GetStatusSubtitle() + { + var state = AwakeService.Instance.GetCurrentState(); + if (!state.IsRunning) + { + return "Awake is idle"; + } + + if (state.Mode == AwakeStateMode.Passive) + { + // When the PowerToys Awake module is enabled, the Awake process stays resident + // even in passive mode. In that case "idle" is correct. If the module is disabled, + // a running process implies a standalone/session keep-awake, so report as active. + return ModuleEnablementService.IsModuleEnabled(SettingsDeepLink.SettingsWindow.Awake) + ? "Awake is idle" + : "Active - session running"; + } + + return state.Mode switch + { + AwakeStateMode.Indefinite => "Active - indefinite", + AwakeStateMode.Timed => state.Duration is { } span + ? $"Active - timer {FormatDuration(span)}" + : "Active - timer", + AwakeStateMode.Expirable => state.Expiration is { } expiry + ? $"Active - until {expiry.ToLocalTime():t}" + : "Active - scheduled", + _ => "Awake is running", + }; + } + + private static string FormatDuration(TimeSpan span) + { + if (span.TotalHours >= 1) + { + var hours = (int)Math.Floor(span.TotalHours); + var minutes = span.Minutes; + return minutes > 0 ? $"{hours}h {minutes}m" : $"{hours}h"; + } + + if (span.TotalMinutes >= 1) + { + return $"{(int)Math.Round(span.TotalMinutes)}m"; + } + + return span.TotalSeconds >= 1 ? $"{(int)Math.Round(span.TotalSeconds)}s" : "\u2014"; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/ColorSwatchIconFactory.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/ColorSwatchIconFactory.cs new file mode 100644 index 0000000000..3fde50882f --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/ColorSwatchIconFactory.cs @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Drawing.Imaging; +using System.IO; +using System.Runtime.InteropServices.WindowsRuntime; +using Microsoft.CommandPalette.Extensions.Toolkit; +using Windows.Storage.Streams; + +namespace PowerToysExtension.Helpers; + +internal static class ColorSwatchIconFactory +{ + public static IconInfo Create(byte r, byte g, byte b, byte a) + { + try + { + using var bmp = new Bitmap(32, 32, PixelFormat.Format32bppArgb); + using var gfx = Graphics.FromImage(bmp); + gfx.SmoothingMode = SmoothingMode.AntiAlias; + gfx.Clear(Color.Transparent); + + using var brush = new SolidBrush(Color.FromArgb(a, r, g, b)); + const int padding = 4; + gfx.FillEllipse(brush, padding, padding, bmp.Width - (padding * 2), bmp.Height - (padding * 2)); + + using var ms = new MemoryStream(); + bmp.Save(ms, ImageFormat.Png); + var ras = new InMemoryRandomAccessStream(); + var writer = new DataWriter(ras); + writer.WriteBytes(ms.ToArray()); + writer.StoreAsync().GetResults(); + ras.Seek(0); + return IconInfo.FromStream(ras); + } + catch + { + // Fallback to a simple colored glyph when drawing fails. + return new IconInfo("\u25CF"); // Black circle glyph + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesDataService.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesDataService.cs new file mode 100644 index 0000000000..0a55859409 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesDataService.cs @@ -0,0 +1,517 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.Json; + +using FancyZonesEditorCommon.Data; +using FancyZonesEditorCommon.Utils; +using ManagedCommon; + +using FZPaths = FancyZonesEditorCommon.Data.FancyZonesPaths; + +namespace PowerToysExtension.Helpers; + +internal static class FancyZonesDataService +{ + private const string ZeroUuid = "{00000000-0000-0000-0000-000000000000}"; + + public static bool TryGetMonitors(out IReadOnlyList<FancyZonesMonitorDescriptor> monitors, out string error) + { + monitors = Array.Empty<FancyZonesMonitorDescriptor>(); + error = string.Empty; + + Logger.LogInfo($"TryGetMonitors: Starting. EditorParametersPath={FZPaths.EditorParameters}"); + + try + { + if (!File.Exists(FZPaths.EditorParameters)) + { + error = "FancyZones monitor data not found. Open FancyZones Editor once to initialize."; + Logger.LogWarning($"TryGetMonitors: File not found. Path={FZPaths.EditorParameters}"); + return false; + } + + Logger.LogInfo($"TryGetMonitors: File exists, reading..."); + var editorParams = FancyZonesDataIO.ReadEditorParameters(); + Logger.LogInfo($"TryGetMonitors: ReadEditorParameters returned. Monitors={editorParams.Monitors?.Count ?? -1}"); + + var editorMonitors = editorParams.Monitors; + if (editorMonitors is null || editorMonitors.Count == 0) + { + error = "No FancyZones monitors found."; + Logger.LogWarning($"TryGetMonitors: No monitors in file."); + return false; + } + + monitors = editorMonitors + .Select((monitor, i) => new FancyZonesMonitorDescriptor(i + 1, monitor)) + .ToArray(); + Logger.LogInfo($"TryGetMonitors: Succeeded. MonitorCount={monitors.Count}"); + return true; + } + catch (Exception ex) + { + error = $"Failed to read FancyZones monitor data: {ex.Message}"; + Logger.LogError($"TryGetMonitors: Exception. Message={ex.Message} Stack={ex.StackTrace}"); + return false; + } + } + + public static IReadOnlyList<FancyZonesLayoutDescriptor> GetLayouts() + { + Logger.LogInfo($"GetLayouts: Starting. LayoutTemplatesPath={FZPaths.LayoutTemplates} CustomLayoutsPath={FZPaths.CustomLayouts}"); + var layouts = new List<FancyZonesLayoutDescriptor>(); + try + { + var templates = GetTemplateLayouts().ToArray(); + Logger.LogInfo($"GetLayouts: GetTemplateLayouts returned {templates.Length} layouts"); + layouts.AddRange(templates); + } + catch (Exception ex) + { + Logger.LogError($"GetLayouts: GetTemplateLayouts failed. Message={ex.Message} Stack={ex.StackTrace}"); + } + + try + { + var customLayouts = GetCustomLayouts().ToArray(); + Logger.LogInfo($"GetLayouts: GetCustomLayouts returned {customLayouts.Length} layouts"); + layouts.AddRange(customLayouts); + } + catch (Exception ex) + { + Logger.LogError($"GetLayouts: GetCustomLayouts failed. Message={ex.Message} Stack={ex.StackTrace}"); + } + + Logger.LogInfo($"GetLayouts: Total layouts={layouts.Count}"); + return layouts; + } + + public static bool TryGetAppliedLayoutForMonitor(EditorParameters.NativeMonitorDataWrapper monitor, out AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper? appliedLayout) + => TryGetAppliedLayoutForMonitor(monitor, FancyZonesVirtualDesktop.GetCurrentVirtualDesktopIdString(), out appliedLayout); + + public static bool TryGetAppliedLayoutForMonitor(EditorParameters.NativeMonitorDataWrapper monitor, string virtualDesktopId, out AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper? appliedLayout) + { + appliedLayout = null; + + if (!TryReadAppliedLayouts(out var file)) + { + return false; + } + + var match = FindAppliedLayoutEntry(file, monitor, virtualDesktopId); + if (match is not null) + { + appliedLayout = match.Value.AppliedLayout; + return true; + } + + return false; + } + + public static (bool Success, string Message) ApplyLayoutToAllMonitors(FancyZonesLayoutDescriptor layout) + { + if (!TryGetMonitors(out var monitors, out var error)) + { + return (false, error); + } + + return ApplyLayoutToMonitors(layout, monitors.Select(m => m.Data)); + } + + public static (bool Success, string Message) ApplyLayoutToMonitor(FancyZonesLayoutDescriptor layout, FancyZonesMonitorDescriptor monitor) + { + if (!TryGetMonitors(out var monitors, out var error)) + { + return (false, error); + } + + EditorParameters.NativeMonitorDataWrapper? monitorData = null; + foreach (var candidate in monitors) + { + if (candidate.Data.MonitorInstanceId == monitor.Data.MonitorInstanceId) + { + monitorData = candidate.Data; + break; + } + } + + if (monitorData is null) + { + return (false, "Monitor not found."); + } + + return ApplyLayoutToMonitors(layout, [monitorData.Value]); + } + + private static (bool Success, string Message) ApplyLayoutToMonitors(FancyZonesLayoutDescriptor layout, IEnumerable<EditorParameters.NativeMonitorDataWrapper> monitors) + { + AppliedLayouts.AppliedLayoutsListWrapper appliedFile; + if (!TryReadAppliedLayouts(out var existingFile)) + { + appliedFile = new AppliedLayouts.AppliedLayoutsListWrapper { AppliedLayouts = new List<AppliedLayouts.AppliedLayoutWrapper>() }; + } + else + { + appliedFile = existingFile; + } + + appliedFile.AppliedLayouts ??= new List<AppliedLayouts.AppliedLayoutWrapper>(); + + var currentVirtualDesktop = FancyZonesVirtualDesktop.GetCurrentVirtualDesktopIdString(); + + foreach (var monitor in monitors) + { + var existingEntry = FindAppliedLayoutEntry(appliedFile, monitor, currentVirtualDesktop); + if (existingEntry is not null) + { + // Remove the existing entry so we can add a new one + appliedFile.AppliedLayouts.Remove(existingEntry.Value); + } + + var newEntry = new AppliedLayouts.AppliedLayoutWrapper + { + Device = new AppliedLayouts.AppliedLayoutWrapper.DeviceIdWrapper + { + Monitor = monitor.Monitor, + MonitorInstance = monitor.MonitorInstanceId ?? string.Empty, + SerialNumber = monitor.MonitorSerialNumber ?? string.Empty, + MonitorNumber = monitor.MonitorNumber, + VirtualDesktop = currentVirtualDesktop, + }, + AppliedLayout = new AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper + { + Uuid = layout.ApplyLayout.Uuid, + Type = layout.ApplyLayout.Type, + ZoneCount = layout.ApplyLayout.ZoneCount, + ShowSpacing = layout.ApplyLayout.ShowSpacing, + Spacing = layout.ApplyLayout.Spacing, + SensitivityRadius = layout.ApplyLayout.SensitivityRadius, + }, + }; + + appliedFile.AppliedLayouts.Add(newEntry); + } + + try + { + FancyZonesDataIO.WriteAppliedLayouts(appliedFile); + } + catch (Exception ex) + { + return (false, $"Failed to write applied layouts: {ex.Message}"); + } + + try + { + FancyZonesNotifier.NotifyAppliedLayoutsChanged(); + } + catch (Exception ex) + { + return (true, $"Layout applied, but FancyZones could not be notified: {ex.Message}"); + } + + return (true, "Layout applied."); + } + + private static AppliedLayouts.AppliedLayoutWrapper? FindAppliedLayoutEntry(AppliedLayouts.AppliedLayoutsListWrapper file, EditorParameters.NativeMonitorDataWrapper monitor, string virtualDesktopId) + { + if (file.AppliedLayouts is null) + { + return null; + } + + return file.AppliedLayouts.FirstOrDefault(e => + string.Equals(e.Device.Monitor, monitor.Monitor, StringComparison.OrdinalIgnoreCase) && + string.Equals(e.Device.MonitorInstance ?? string.Empty, monitor.MonitorInstanceId ?? string.Empty, StringComparison.OrdinalIgnoreCase) && + string.Equals(e.Device.SerialNumber ?? string.Empty, monitor.MonitorSerialNumber ?? string.Empty, StringComparison.OrdinalIgnoreCase) && + e.Device.MonitorNumber == monitor.MonitorNumber && + string.Equals(e.Device.VirtualDesktop, virtualDesktopId, StringComparison.OrdinalIgnoreCase)); + } + + private static bool TryReadAppliedLayouts(out AppliedLayouts.AppliedLayoutsListWrapper file) + { + file = default; + try + { + if (!File.Exists(FZPaths.AppliedLayouts)) + { + return false; + } + + file = FancyZonesDataIO.ReadAppliedLayouts(); + return true; + } + catch + { + return false; + } + } + + private static IEnumerable<FancyZonesLayoutDescriptor> GetTemplateLayouts() + { + Logger.LogInfo($"GetTemplateLayouts: Starting. Path={FZPaths.LayoutTemplates} Exists={File.Exists(FZPaths.LayoutTemplates)}"); + + LayoutTemplates.TemplateLayoutsListWrapper templates; + try + { + if (!File.Exists(FZPaths.LayoutTemplates)) + { + Logger.LogWarning($"GetTemplateLayouts: File not found."); + yield break; + } + + templates = FancyZonesDataIO.ReadLayoutTemplates(); + Logger.LogInfo($"GetTemplateLayouts: ReadLayoutTemplates succeeded. Count={templates.LayoutTemplates?.Count ?? -1}"); + } + catch (Exception ex) + { + Logger.LogError($"GetTemplateLayouts: ReadLayoutTemplates failed. Message={ex.Message} Stack={ex.StackTrace}"); + yield break; + } + + var templateLayouts = templates.LayoutTemplates; + if (templateLayouts is null) + { + Logger.LogWarning($"GetTemplateLayouts: LayoutTemplates is null."); + yield break; + } + + foreach (var template in templateLayouts) + { + if (string.IsNullOrWhiteSpace(template.Type)) + { + continue; + } + + var type = template.Type.Trim(); + var zoneCount = type.Equals("blank", StringComparison.OrdinalIgnoreCase) + ? 0 + : template.ZoneCount > 0 ? template.ZoneCount : 3; + var title = $"Template: {type}"; + var subtitle = $"{zoneCount} zones"; + + yield return new FancyZonesLayoutDescriptor + { + Id = $"template:{type.ToLowerInvariant()}", + Source = FancyZonesLayoutSource.Template, + Title = title, + Subtitle = subtitle, + Template = template, + ApplyLayout = new AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper + { + Type = type.ToLowerInvariant(), + Uuid = ZeroUuid, + ZoneCount = zoneCount, + ShowSpacing = template.ShowSpacing, + Spacing = template.Spacing, + SensitivityRadius = template.SensitivityRadius, + }, + }; + } + } + + private static IEnumerable<FancyZonesLayoutDescriptor> GetCustomLayouts() + { + CustomLayouts.CustomLayoutListWrapper customLayouts; + try + { + if (!File.Exists(FZPaths.CustomLayouts)) + { + yield break; + } + + customLayouts = FancyZonesDataIO.ReadCustomLayouts(); + } + catch + { + yield break; + } + + var layouts = customLayouts.CustomLayouts; + if (layouts is null) + { + yield break; + } + + foreach (var custom in layouts) + { + if (string.IsNullOrWhiteSpace(custom.Uuid) || string.IsNullOrWhiteSpace(custom.Name)) + { + continue; + } + + var uuid = custom.Uuid.Trim(); + var customType = custom.Type?.Trim().ToLowerInvariant() ?? string.Empty; + + if (!TryBuildAppliedLayoutForCustom(custom, out var applied)) + { + continue; + } + + var title = custom.Name.Trim(); + var subtitle = customType switch + { + "grid" => $"Custom grid \u2022 {applied.ZoneCount} zones", + "canvas" => $"Custom canvas \u2022 {applied.ZoneCount} zones", + _ => $"Custom \u2022 {applied.ZoneCount} zones", + }; + + yield return new FancyZonesLayoutDescriptor + { + Id = $"custom:{uuid}", + Source = FancyZonesLayoutSource.Custom, + Title = title, + Subtitle = subtitle, + Custom = custom, + ApplyLayout = applied, + }; + } + } + + private static bool TryBuildAppliedLayoutForCustom(CustomLayouts.CustomLayoutWrapper custom, out AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper applied) + { + applied = new AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper + { + Type = "custom", + Uuid = custom.Uuid.Trim(), + ShowSpacing = false, + Spacing = 0, + ZoneCount = 0, + SensitivityRadius = 20, + }; + + if (custom.Info.ValueKind is JsonValueKind.Undefined or JsonValueKind.Null) + { + return false; + } + + var customType = custom.Type?.Trim().ToLowerInvariant() ?? string.Empty; + if (customType == "grid") + { + if (!TryParseCustomGridInfo(custom.Info, out var zoneCount, out var showSpacing, out var spacing, out var sensitivity)) + { + return false; + } + + applied.ZoneCount = zoneCount; + applied.ShowSpacing = showSpacing; + applied.Spacing = spacing; + applied.SensitivityRadius = sensitivity; + return true; + } + + if (customType == "canvas") + { + if (!TryParseCustomCanvasInfo(custom.Info, out var zoneCount, out var sensitivity)) + { + return false; + } + + applied.ZoneCount = zoneCount; + applied.SensitivityRadius = sensitivity; + applied.ShowSpacing = false; + applied.Spacing = 0; + return true; + } + + return false; + } + + internal static bool TryParseCustomGridInfo(JsonElement info, out int zoneCount, out bool showSpacing, out int spacing, out int sensitivityRadius) + { + zoneCount = 0; + showSpacing = false; + spacing = 0; + sensitivityRadius = 20; + + if (!info.TryGetProperty("rows", out var rowsProp) || + !info.TryGetProperty("columns", out var columnsProp) || + rowsProp.ValueKind != JsonValueKind.Number || + columnsProp.ValueKind != JsonValueKind.Number) + { + return false; + } + + var rows = rowsProp.GetInt32(); + var columns = columnsProp.GetInt32(); + if (rows <= 0 || columns <= 0) + { + return false; + } + + if (info.TryGetProperty("cell-child-map", out var cellMap) && cellMap.ValueKind == JsonValueKind.Array) + { + var max = -1; + foreach (var row in cellMap.EnumerateArray()) + { + if (row.ValueKind != JsonValueKind.Array) + { + continue; + } + + foreach (var cell in row.EnumerateArray()) + { + if (cell.ValueKind == JsonValueKind.Number && cell.TryGetInt32(out var value)) + { + max = Math.Max(max, value); + } + } + } + + zoneCount = max + 1; + } + else + { + zoneCount = rows * columns; + } + + if (zoneCount <= 0) + { + return false; + } + + if (info.TryGetProperty("show-spacing", out var showSpacingProp) && + (showSpacingProp.ValueKind == JsonValueKind.True || showSpacingProp.ValueKind == JsonValueKind.False)) + { + showSpacing = showSpacingProp.GetBoolean(); + } + + if (info.TryGetProperty("spacing", out var spacingProp) && spacingProp.ValueKind == JsonValueKind.Number) + { + spacing = spacingProp.GetInt32(); + } + + if (info.TryGetProperty("sensitivity-radius", out var sensitivityProp) && sensitivityProp.ValueKind == JsonValueKind.Number) + { + sensitivityRadius = sensitivityProp.GetInt32(); + } + + return true; + } + + internal static bool TryParseCustomCanvasInfo(JsonElement info, out int zoneCount, out int sensitivityRadius) + { + zoneCount = 0; + sensitivityRadius = 20; + + if (!info.TryGetProperty("zones", out var zones) || zones.ValueKind != JsonValueKind.Array) + { + return false; + } + + zoneCount = zones.GetArrayLength(); + + if (info.TryGetProperty("sensitivity-radius", out var sensitivityProp) && sensitivityProp.ValueKind == JsonValueKind.Number) + { + sensitivityRadius = sensitivityProp.GetInt32(); + } + + return zoneCount >= 0; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesLayoutDescriptor.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesLayoutDescriptor.cs new file mode 100644 index 0000000000..0c99dcc8f4 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesLayoutDescriptor.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using FancyZonesEditorCommon.Data; + +namespace PowerToysExtension.Helpers; + +internal sealed class FancyZonesLayoutDescriptor +{ + public required string Id { get; init; } // "template:<type>" or "custom:<uuid>" + + public required FancyZonesLayoutSource Source { get; init; } + + public required string Title { get; init; } + + public required string Subtitle { get; init; } + + public required AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper ApplyLayout { get; init; } + + public LayoutTemplates.TemplateLayoutWrapper? Template { get; init; } + + public CustomLayouts.CustomLayoutWrapper? Custom { get; init; } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesLayoutDescriptor1.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesLayoutDescriptor1.cs new file mode 100644 index 0000000000..194a9a206c --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesLayoutDescriptor1.cs @@ -0,0 +1,5 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// Intentionally empty: this file was an accidental duplicate of FancyZonesLayoutDescriptor.cs. diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesLayoutSource.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesLayoutSource.cs new file mode 100644 index 0000000000..d75e3faccd --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesLayoutSource.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace PowerToysExtension.Helpers; + +internal enum FancyZonesLayoutSource +{ + Template, + Custom, +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesMonitorDescriptor.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesMonitorDescriptor.cs new file mode 100644 index 0000000000..4e3f7092a4 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesMonitorDescriptor.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Globalization; + +using FancyZonesEditorCommon.Data; + +namespace PowerToysExtension.Helpers; + +internal readonly record struct FancyZonesMonitorDescriptor( + int Index, + EditorParameters.NativeMonitorDataWrapper Data) +{ + public string Title => Data.Monitor; + + public string Subtitle + { + get + { + var size = $"{Data.MonitorWidth}×{Data.MonitorHeight}"; + var scaling = Data.Dpi > 0 ? string.Format(CultureInfo.InvariantCulture, "{0}%", (int)Math.Round(Data.Dpi * 100 / 96.0)) : "n/a"; + return $"{size} \u2022 {scaling}"; + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesMonitorIdentifier.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesMonitorIdentifier.cs new file mode 100644 index 0000000000..afa908e08b --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesMonitorIdentifier.cs @@ -0,0 +1,461 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Threading.Tasks; + +namespace PowerToysExtension.Helpers; + +internal static class FancyZonesMonitorIdentifier +{ + private const string WindowClassName = "PowerToys_FancyZones_MonitorIdentify"; + + private const uint WsExToolWindow = 0x00000080; + private const uint WsExTopmost = 0x00000008; + private const uint WsExTransparent = 0x00000020; + private const uint WsPopup = 0x80000000; + + private const uint WmDestroy = 0x0002; + private const uint WmPaint = 0x000F; + private const uint WmTimer = 0x0113; + + private const uint CsVRedraw = 0x0001; + private const uint CsHRedraw = 0x0002; + + private const int SwShowNoActivate = 4; + + private const int Transparent = 1; + + private const int BaseFontHeightPx = 52; + private const int BaseDpi = 96; + + private const uint DtCenter = 0x00000001; + private const uint DtVCenter = 0x00000004; + private const uint DtSingleLine = 0x00000020; + + private const uint MonitorDefaultToNearest = 2; + + private static readonly nint DpiAwarenessContextUnaware = new(-1); + + private static readonly object Sync = new(); + private static bool classRegistered; + + private static GCHandle? currentPinnedTextHandle; + + public static void Show(int left, int top, int width, int height, string text, int durationMs = 1200) + { + if (string.IsNullOrWhiteSpace(text)) + { + text = "Monitor"; + } + + _ = Task.Run(() => RunWindow(left, top, width, height, text, durationMs)) + .ContinueWith(static t => _ = t.Exception, TaskContinuationOptions.OnlyOnFaulted); + } + + private static unsafe void RunWindow(int left, int top, int width, int height, string text, int durationMs) + { + EnsureClassRegistered(); + + var workArea = TryGetWorkAreaFromFancyZonesCoordinates(left, top, width, height, out var resolvedWorkArea) + ? resolvedWorkArea + : new RECT + { + Left = left, + Top = top, + Right = left + width, + Bottom = top + height, + }; + + var workAreaWidth = Math.Max(0, workArea.Right - workArea.Left); + var workAreaHeight = Math.Max(0, workArea.Bottom - workArea.Top); + + var overlayWidth = Math.Clamp(workAreaWidth / 4, 220, 420); + var overlayHeight = Math.Clamp(workAreaHeight / 6, 120, 240); + + var x = workArea.Left + ((workAreaWidth - overlayWidth) / 2); + var y = workArea.Top + ((workAreaHeight - overlayHeight) / 2); + + lock (Sync) + { + currentPinnedTextHandle?.Free(); + currentPinnedTextHandle = GCHandle.Alloc(text, GCHandleType.Pinned); + } + + var hwnd = CreateWindowExW( + WsExToolWindow | WsExTopmost | WsExTransparent, + WindowClassName, + "MonitorIdentify", + WsPopup, + x, + y, + overlayWidth, + overlayHeight, + nint.Zero, + nint.Zero, + GetModuleHandleW(null), + nint.Zero); + + if (hwnd == nint.Zero) + { + return; + } + + _ = ShowWindow(hwnd, SwShowNoActivate); + _ = UpdateWindow(hwnd); + + _ = SetTimer(hwnd, 1, (uint)durationMs, nint.Zero); + + MSG msg; + while (GetMessageW(out msg, nint.Zero, 0, 0) != 0) + { + _ = TranslateMessage(in msg); + _ = DispatchMessageW(in msg); + } + + lock (Sync) + { + currentPinnedTextHandle?.Free(); + currentPinnedTextHandle = null; + } + } + + private static unsafe void EnsureClassRegistered() + { + lock (Sync) + { + if (classRegistered) + { + return; + } + + fixed (char* className = WindowClassName ?? string.Empty) + { + var wc = new WNDCLASSEXW + { + CbSize = (uint)sizeof(WNDCLASSEXW), + Style = CsHRedraw | CsVRedraw, + LpfnWndProc = &WndProc, + HInstance = GetModuleHandleW(null), + HCursor = LoadCursorW(nint.Zero, new IntPtr(32512)), // IDC_ARROW + LpszClassName = className, + }; + + _ = RegisterClassExW(in wc); + classRegistered = true; + } + } + } + + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] + private static unsafe nint WndProc(nint hwnd, uint msg, nuint wParam, nint lParam) + { + switch (msg) + { + case WmTimer: + _ = KillTimer(hwnd, 1); + _ = DestroyWindow(hwnd); + return nint.Zero; + + case WmDestroy: + PostQuitMessage(0); + return nint.Zero; + + case WmPaint: + { + var hdc = BeginPaint(hwnd, out var ps); + + _ = GetClientRect(hwnd, out var rect); + + var bgBrush = CreateSolidBrush(0x202020); + _ = FillRect(hdc, in rect, bgBrush); + + _ = SetBkMode(hdc, Transparent); + _ = SetTextColor(hdc, 0xFFFFFF); + + var dpi = GetDpiForWindow(hwnd); + var fontHeight = -MulDiv(BaseFontHeightPx, (int)dpi, BaseDpi); + var font = CreateFontW( + fontHeight, + 0, + 0, + 0, + 700, + 0, + 0, + 0, + 1, // DEFAULT_CHARSET + 0, // OUT_DEFAULT_PRECIS + 0, // CLIP_DEFAULT_PRECIS + 5, // CLEARTYPE_QUALITY + 0x20, // FF_SWISS + "Segoe UI"); + + var oldFont = SelectObject(hdc, font); + + var textPtr = GetPinnedTextPointer(); + if (textPtr is not null) + { + var textNint = (nint)textPtr; + _ = DrawTextW(hdc, textNint, -1, ref rect, DtCenter | DtVCenter | DtSingleLine); + } + + _ = SelectObject(hdc, oldFont); + _ = DeleteObject(font); + _ = DeleteObject(bgBrush); + + _ = EndPaint(hwnd, ref ps); + return nint.Zero; + } + } + + return DefWindowProcW(hwnd, msg, wParam, lParam); + } + + private static unsafe char* GetPinnedTextPointer() + { + lock (Sync) + { + if (!currentPinnedTextHandle.HasValue || !currentPinnedTextHandle.Value.IsAllocated) + { + return null; + } + + return (char*)currentPinnedTextHandle.Value.AddrOfPinnedObject(); + } + } + + private static bool TryGetWorkAreaFromFancyZonesCoordinates(int left, int top, int width, int height, out RECT workArea) + { + workArea = default; + + if (width <= 0 || height <= 0) + { + return false; + } + + var logicalRect = new RECT + { + Left = left, + Top = top, + Right = left + width, + Bottom = top + height, + }; + + var previousContext = SetThreadDpiAwarenessContext(DpiAwarenessContextUnaware); + nint monitor; + try + { + monitor = MonitorFromRect(ref logicalRect, MonitorDefaultToNearest); + } + finally + { + _ = SetThreadDpiAwarenessContext(previousContext); + } + + if (monitor == nint.Zero) + { + return false; + } + + var mi = new MONITORINFOEXW + { + CbSize = (uint)Marshal.SizeOf<MONITORINFOEXW>(), + }; + + if (!GetMonitorInfoW(monitor, ref mi)) + { + return false; + } + + workArea = mi.RcWork; + return true; + } + + [StructLayout(LayoutKind.Sequential)] + private unsafe struct WNDCLASSEXW + { + public uint CbSize; + public uint Style; + public delegate* unmanaged[Stdcall]<nint, uint, nuint, nint, nint> LpfnWndProc; + public int CbClsExtra; + public int CbWndExtra; + public nint HInstance; + public nint HIcon; + public nint HCursor; + public nint HbrBackground; + public char* LpszMenuName; + public char* LpszClassName; + public nint HIconSm; + } + + [StructLayout(LayoutKind.Sequential)] + private struct POINT + { + public int X; + public int Y; + } + + [StructLayout(LayoutKind.Sequential)] + private struct MSG + { + public nint Hwnd; + public uint Message; + public nuint WParam; + public nint LParam; + public uint Time; + public POINT Pt; + public uint LPrivate; + } + + [StructLayout(LayoutKind.Sequential)] + private struct RECT + { + public int Left; + public int Top; + public int Right; + public int Bottom; + } + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + private struct MONITORINFOEXW + { + public uint CbSize; + public RECT RcMonitor; + public RECT RcWork; + public uint DwFlags; + + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] + public string SzDevice; + } + + [StructLayout(LayoutKind.Sequential)] + private unsafe struct PAINTSTRUCT + { + public nint Hdc; + public int FErase; + public RECT RcPaint; + public int FRestore; + public int FIncUpdate; + public fixed byte RgbReserved[32]; + } + + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern nint GetModuleHandleW(string? lpModuleName); + + [DllImport("kernel32.dll")] + private static extern int MulDiv(int nNumber, int nNumerator, int nDenominator); + + [DllImport("user32.dll")] + private static extern uint GetDpiForWindow(nint hwnd); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool UpdateWindow(nint hWnd); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool ShowWindow(nint hWnd, int nCmdShow); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool DestroyWindow(nint hWnd); + + [DllImport("user32.dll")] + private static extern void PostQuitMessage(int nExitCode); + + [DllImport("user32.dll", SetLastError = true)] + private static extern bool KillTimer(nint hWnd, nuint uIDEvent); + + [DllImport("user32.dll", SetLastError = true)] + private static extern nuint SetTimer(nint hWnd, nuint nIDEvent, uint uElapse, nint lpTimerFunc); + + [DllImport("user32.dll")] + private static extern int GetMessageW(out MSG lpMsg, nint hWnd, uint wMsgFilterMin, uint wMsgFilterMax); + + [DllImport("user32.dll")] + private static extern bool TranslateMessage(in MSG lpMsg); + + [DllImport("user32.dll")] + private static extern nint DispatchMessageW(in MSG lpMsg); + + [DllImport("user32.dll")] + private static extern nint SetThreadDpiAwarenessContext(nint dpiContext); + + [DllImport("user32.dll")] + private static extern nint MonitorFromRect(ref RECT lprc, uint dwFlags); + + [DllImport("user32.dll", CharSet = CharSet.Unicode)] + private static extern bool GetMonitorInfoW(nint hMonitor, ref MONITORINFOEXW lpmi); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern ushort RegisterClassExW(in WNDCLASSEXW lpwcx); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern nint CreateWindowExW( + uint dwExStyle, + string lpClassName, + string lpWindowName, + uint dwStyle, + int x, + int y, + int nWidth, + int nHeight, + nint hWndParent, + nint hMenu, + nint hInstance, + nint lpParam); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern nint LoadCursorW(nint hInstance, nint lpCursorName); + + [DllImport("user32.dll")] + private static extern nint DefWindowProcW(nint hWnd, uint msg, nuint wParam, nint lParam); + + [DllImport("user32.dll")] + private static extern nint BeginPaint(nint hWnd, out PAINTSTRUCT lpPaint); + + [DllImport("user32.dll")] + private static extern bool EndPaint(nint hWnd, ref PAINTSTRUCT lpPaint); + + [DllImport("user32.dll")] + private static extern bool GetClientRect(nint hWnd, out RECT lpRect); + + [DllImport("user32.dll")] + private static extern int FillRect(nint hDC, in RECT lprc, nint hbr); + + [DllImport("user32.dll", CharSet = CharSet.Unicode)] + private static extern int DrawTextW(nint hdc, nint lpchText, int cchText, ref RECT lprc, uint format); + + [DllImport("gdi32.dll")] + private static extern nint CreateSolidBrush(uint colorRef); + + [DllImport("gdi32.dll")] + private static extern int SetBkMode(nint hdc, int mode); + + [DllImport("gdi32.dll")] + private static extern uint SetTextColor(nint hdc, uint colorRef); + + [DllImport("gdi32.dll", CharSet = CharSet.Unicode)] + private static extern nint CreateFontW( + int nHeight, + int nWidth, + int nEscapement, + int nOrientation, + int fnWeight, + uint fdwItalic, + uint fdwUnderline, + uint fdwStrikeOut, + uint fdwCharSet, + uint fdwOutputPrecision, + uint fdwClipPrecision, + uint fdwQuality, + uint fdwPitchAndFamily, + string lpszFace); + + [DllImport("gdi32.dll")] + private static extern nint SelectObject(nint hdc, nint hgdiobj); + + [DllImport("gdi32.dll")] + private static extern bool DeleteObject(nint hObject); +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesMonitorPreviewRenderer.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesMonitorPreviewRenderer.cs new file mode 100644 index 0000000000..fc22b1a0cd --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesMonitorPreviewRenderer.cs @@ -0,0 +1,399 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +using FancyZonesEditorCommon.Data; + +using ManagedCommon; +using Microsoft.CommandPalette.Extensions.Toolkit; +using Windows.Graphics.Imaging; +using Windows.Storage.Streams; + +using InteropConstants = PowerToys.Interop.Constants; + +namespace PowerToysExtension.Helpers; + +internal static class FancyZonesMonitorPreviewRenderer +{ + public static IconInfo? TryRenderMonitorHeroImage(FancyZonesMonitorDescriptor monitor) + { + try + { + var cached = TryGetCachedIcon(monitor); + if (cached is not null) + { + return cached; + } + + var icon = RenderMonitorHeroImageAsync(monitor).GetAwaiter().GetResult(); + return icon; + } + catch (Exception ex) + { + Logger.LogWarning($"FancyZones monitor hero render failed. Monitor={monitor.Data.Monitor} Index={monitor.Index} Exception={ex}"); + return null; + } + } + + private static IconInfo? TryGetCachedIcon(FancyZonesMonitorDescriptor monitor) + { + var cachePath = GetCachePath(monitor); + if (string.IsNullOrEmpty(cachePath)) + { + return null; + } + + try + { + if (File.Exists(cachePath)) + { + return new IconInfo(cachePath); + } + } + catch (Exception ex) + { + Logger.LogWarning($"FancyZones monitor hero cache check failed. Path=\"{cachePath}\" Monitor={monitor.Data.Monitor} Index={monitor.Index} Exception={ex}"); + } + + return null; + } + + private static async Task<IconInfo?> RenderMonitorHeroImageAsync(FancyZonesMonitorDescriptor monitor) + { + var cachePath = GetCachePath(monitor); + if (string.IsNullOrEmpty(cachePath)) + { + return null; + } + + var (widthPx, heightPx) = ComputeCanvasSize(monitor.Data); + Logger.LogDebug($"FancyZones monitor hero render starting. Monitor={monitor.Data.Monitor} Index={monitor.Index} Size={widthPx}x{heightPx}"); + + var (layoutRectangles, spacing) = GetLayoutRectangles(monitor.Data); + var pixelBytes = RenderMonitorPreviewBgra(widthPx, heightPx, layoutRectangles, spacing); + + var stream = new InMemoryRandomAccessStream(); + var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); + encoder.SetPixelData( + BitmapPixelFormat.Bgra8, + BitmapAlphaMode.Premultiplied, + (uint)widthPx, + (uint)heightPx, + 96, + 96, + pixelBytes); + await encoder.FlushAsync(); + stream.Seek(0); + + try + { + var tempPath = FormattableString.Invariant($"{cachePath}.{Guid.NewGuid():N}.tmp"); + Directory.CreateDirectory(Path.GetDirectoryName(cachePath)!); + await WriteStreamToFileAsync(stream, tempPath); + File.Move(tempPath, cachePath, overwrite: true); + } + catch (Exception ex) + { + Logger.LogWarning($"FancyZones monitor hero cache write failed. Path=\"{cachePath}\" Monitor={monitor.Data.Monitor} Index={monitor.Index} Exception={ex}"); + return null; + } + + Logger.LogDebug($"FancyZones monitor hero render succeeded. Monitor={monitor.Data.Monitor} Index={monitor.Index} Path=\"{cachePath}\""); + return new IconInfo(cachePath); + } + + private static (int WidthPx, int HeightPx) ComputeCanvasSize(EditorParameters.NativeMonitorDataWrapper monitor) + { + const int maxDim = 320; + var w = monitor.WorkAreaWidth > 0 ? monitor.WorkAreaWidth : monitor.MonitorWidth; + var h = monitor.WorkAreaHeight > 0 ? monitor.WorkAreaHeight : monitor.MonitorHeight; + + if (w <= 0 || h <= 0) + { + return (maxDim, 180); + } + + var aspect = (float)w / h; + if (aspect >= 1) + { + var height = (int)Math.Clamp(Math.Round(maxDim / aspect), 90, maxDim); + return (maxDim, height); + } + else + { + var width = (int)Math.Clamp(Math.Round(maxDim * aspect), 90, maxDim); + return (width, maxDim); + } + } + + private static (List<FancyZonesThumbnailRenderer.NormalizedRect> Rects, int Spacing) GetLayoutRectangles(EditorParameters.NativeMonitorDataWrapper monitor) + { + if (!FancyZonesDataService.TryGetAppliedLayoutForMonitor(monitor, out var applied) || applied is null) + { + return ([], 0); + } + + var layout = FindLayoutDescriptor(applied.Value); + if (layout is null) + { + return ([], 0); + } + + var rects = FancyZonesThumbnailRenderer.GetNormalizedRectsForLayout(layout); + var spacing = layout.ApplyLayout.ShowSpacing && layout.ApplyLayout.Spacing > 0 ? layout.ApplyLayout.Spacing : 0; + return (rects, spacing); + } + + private static FancyZonesLayoutDescriptor? FindLayoutDescriptor(AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper applied) + { + try + { + var layouts = FancyZonesDataService.GetLayouts(); + + if (!string.IsNullOrWhiteSpace(applied.Uuid) && + !applied.Uuid.Equals("{00000000-0000-0000-0000-000000000000}", StringComparison.OrdinalIgnoreCase)) + { + return layouts.FirstOrDefault(l => l.Source == FancyZonesLayoutSource.Custom && + l.Custom is not null && + string.Equals(l.Custom.Value.Uuid?.Trim(), applied.Uuid.Trim(), StringComparison.OrdinalIgnoreCase)); + } + + var type = applied.Type?.Trim().ToLowerInvariant() ?? string.Empty; + var zoneCount = applied.ZoneCount; + return layouts.FirstOrDefault(l => + l.Source == FancyZonesLayoutSource.Template && + string.Equals(l.ApplyLayout.Type, type, StringComparison.OrdinalIgnoreCase) && + l.ApplyLayout.ZoneCount == zoneCount && + l.ApplyLayout.ShowSpacing == applied.ShowSpacing && + l.ApplyLayout.Spacing == applied.Spacing); + } + catch + { + return null; + } + } + + private static string? GetCachePath(FancyZonesMonitorDescriptor monitor) + { + try + { + var basePath = InteropConstants.AppDataPath(); + if (string.IsNullOrWhiteSpace(basePath)) + { + return null; + } + + var cacheFolder = Path.Combine(basePath, "CmdPal", "PowerToysExtension", "Cache", "FancyZones", "MonitorPreviews"); + var fileName = ComputeMonitorHash(monitor) + ".png"; + return Path.Combine(cacheFolder, fileName); + } + catch + { + return null; + } + } + + private static string ComputeMonitorHash(FancyZonesMonitorDescriptor monitor) + { + var currentVirtualDesktop = FancyZonesVirtualDesktop.GetCurrentVirtualDesktopIdString(); + var appliedFingerprint = string.Empty; + if (FancyZonesDataService.TryGetAppliedLayoutForMonitor(monitor.Data, out var applied) && applied is not null) + { + appliedFingerprint = FormattableString.Invariant($"{applied.Value.Type}|{applied.Value.Uuid}|{applied.Value.ZoneCount}|{applied.Value.ShowSpacing}|{applied.Value.Spacing}"); + } + + var identity = FormattableString.Invariant( + $"{monitor.Data.Monitor}|{monitor.Data.MonitorInstanceId}|{monitor.Data.MonitorSerialNumber}|{monitor.Data.MonitorNumber}|{currentVirtualDesktop}|{monitor.Data.WorkAreaWidth}x{monitor.Data.WorkAreaHeight}|{monitor.Data.MonitorWidth}x{monitor.Data.MonitorHeight}|{appliedFingerprint}"); + + var bytes = Encoding.UTF8.GetBytes(identity); + var hash = SHA256.HashData(bytes); + return Convert.ToHexString(hash).ToLowerInvariant(); + } + + private static byte[] RenderMonitorPreviewBgra( + int widthPx, + int heightPx, + IReadOnlyList<FancyZonesThumbnailRenderer.NormalizedRect> rects, + int spacing) + { + var pixels = new byte[widthPx * heightPx * 4]; + + var frame = Premultiply(new BgraColor(0x80, 0x80, 0x80, 0xFF)); + var bezelFill = Premultiply(new BgraColor(0x20, 0x20, 0x20, 0x18)); + var screenFill = Premultiply(new BgraColor(0x00, 0x00, 0x00, 0x00)); + var border = Premultiply(new BgraColor(0xFF, 0xD8, 0x8C, 0xFF)); + var fill = Premultiply(new BgraColor(0xFF, 0xD8, 0x8C, 0xC0)); + var background = Premultiply(new BgraColor(0x00, 0x00, 0x00, 0x00)); + + for (var i = 0; i < pixels.Length; i += 4) + { + pixels[i + 0] = background.B; + pixels[i + 1] = background.G; + pixels[i + 2] = background.R; + pixels[i + 3] = background.A; + } + + DrawRectBorder(pixels, widthPx, heightPx, 0, 0, widthPx, heightPx, frame); + + const int bezel = 3; + FillRect(pixels, widthPx, heightPx, 1, 1, widthPx - 1, heightPx - 1, bezelFill); + FillRect(pixels, widthPx, heightPx, 1 + bezel, 1 + bezel, widthPx - 1 - bezel, heightPx - 1 - bezel, screenFill); + + var innerLeft = 1 + bezel; + var innerTop = 1 + bezel; + var innerRight = widthPx - 1 - bezel; + var innerBottom = heightPx - 1 - bezel; + var innerWidth = Math.Max(1, innerRight - innerLeft); + var innerHeight = Math.Max(1, innerBottom - innerTop); + + var gapPx = spacing > 0 ? Math.Clamp(spacing / 8, 1, 3) : 0; + foreach (var rect in rects) + { + var (x1, y1, x2, y2) = ToPixelBounds(rect, innerLeft, innerTop, innerWidth, innerHeight, gapPx); + if (x2 <= x1 || y2 <= y1) + { + continue; + } + + FillRect(pixels, widthPx, heightPx, x1, y1, x2, y2, fill); + DrawRectBorder(pixels, widthPx, heightPx, x1, y1, x2, y2, border); + } + + return pixels; + } + + private static (int X1, int Y1, int X2, int Y2) ToPixelBounds( + FancyZonesThumbnailRenderer.NormalizedRect rect, + int originX, + int originY, + int widthPx, + int heightPx, + int gapPx) + { + var x1 = originX + (int)MathF.Round(rect.X * widthPx); + var y1 = originY + (int)MathF.Round(rect.Y * heightPx); + var x2 = originX + (int)MathF.Round((rect.X + rect.Width) * widthPx); + var y2 = originY + (int)MathF.Round((rect.Y + rect.Height) * heightPx); + + x1 = Math.Clamp(x1 + gapPx, originX, originX + widthPx - 1); + y1 = Math.Clamp(y1 + gapPx, originY, originY + heightPx - 1); + x2 = Math.Clamp(x2 - gapPx, originX + 1, originX + widthPx); + y2 = Math.Clamp(y2 - gapPx, originY + 1, originY + heightPx); + + if (x2 <= x1 + 1) + { + x2 = Math.Min(originX + widthPx, x1 + 2); + } + + if (y2 <= y1 + 1) + { + y2 = Math.Min(originY + heightPx, y1 + 2); + } + + return (x1, y1, x2, y2); + } + + private static void FillRect(byte[] pixels, int widthPx, int heightPx, int x1, int y1, int x2, int y2, BgraColor color) + { + for (var y = y1; y < y2; y++) + { + if ((uint)y >= (uint)heightPx) + { + continue; + } + + var rowStart = y * widthPx * 4; + for (var x = x1; x < x2; x++) + { + if ((uint)x >= (uint)widthPx) + { + continue; + } + + var i = rowStart + (x * 4); + pixels[i + 0] = color.B; + pixels[i + 1] = color.G; + pixels[i + 2] = color.R; + pixels[i + 3] = color.A; + } + } + } + + private static void DrawRectBorder(byte[] pixels, int widthPx, int heightPx, int x1, int y1, int x2, int y2, BgraColor color) + { + var left = x1; + var right = x2 - 1; + var top = y1; + var bottom = y2 - 1; + + for (var x = left; x <= right; x++) + { + SetPixel(pixels, widthPx, heightPx, x, top, color); + SetPixel(pixels, widthPx, heightPx, x, bottom, color); + } + + for (var y = top; y <= bottom; y++) + { + SetPixel(pixels, widthPx, heightPx, left, y, color); + SetPixel(pixels, widthPx, heightPx, right, y, color); + } + } + + private static void SetPixel(byte[] pixels, int widthPx, int heightPx, int x, int y, BgraColor color) + { + if ((uint)x >= (uint)widthPx || (uint)y >= (uint)heightPx) + { + return; + } + + var i = ((y * widthPx) + x) * 4; + pixels[i + 0] = color.B; + pixels[i + 1] = color.G; + pixels[i + 2] = color.R; + pixels[i + 3] = color.A; + } + + private static BgraColor Premultiply(BgraColor color) + { + if (color.A == 0 || color.A == 255) + { + return color; + } + + byte Premul(byte c) => (byte)(((c * color.A) + 127) / 255); + return new BgraColor(Premul(color.B), Premul(color.G), Premul(color.R), color.A); + } + + private readonly record struct BgraColor(byte B, byte G, byte R, byte A); + + private static async Task WriteStreamToFileAsync(IRandomAccessStream stream, string filePath) + { + stream.Seek(0); + var size = stream.Size; + if (size == 0) + { + File.WriteAllBytes(filePath, Array.Empty<byte>()); + return; + } + + if (size > int.MaxValue) + { + throw new InvalidOperationException("Icon stream too large."); + } + + using var input = stream.GetInputStreamAt(0); + using var reader = new DataReader(input); + await reader.LoadAsync((uint)size); + var bytes = new byte[(int)size]; + reader.ReadBytes(bytes); + File.WriteAllBytes(filePath, bytes); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesNotifier.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesNotifier.cs new file mode 100644 index 0000000000..ada8ee7b17 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesNotifier.cs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +namespace PowerToysExtension.Helpers; + +internal static class FancyZonesNotifier +{ + private const string AppliedLayoutsFileUpdateMessage = "{2ef2c8a7-e0d5-4f31-9ede-52aade2d284d}"; + private static readonly uint WmPrivAppliedLayoutsFileUpdate = RegisterWindowMessageW(AppliedLayoutsFileUpdateMessage); + + public static void NotifyAppliedLayoutsChanged() + { + _ = PostMessageW(new IntPtr(0xFFFF), WmPrivAppliedLayoutsFileUpdate, UIntPtr.Zero, IntPtr.Zero); + } + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern uint RegisterWindowMessageW(string lpString); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + private static extern bool PostMessageW(IntPtr hWnd, uint msg, UIntPtr wParam, IntPtr lParam); +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesThumbnailRenderer.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesThumbnailRenderer.cs new file mode 100644 index 0000000000..579b4e2d0e --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesThumbnailRenderer.cs @@ -0,0 +1,716 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using FancyZonesEditorCommon.Data; +using ManagedCommon; +using Microsoft.CommandPalette.Extensions.Toolkit; +using Windows.Graphics.Imaging; +using Windows.Storage.Streams; + +using InteropConstants = PowerToys.Interop.Constants; + +namespace PowerToysExtension.Helpers; + +internal static class FancyZonesThumbnailRenderer +{ + internal readonly record struct NormalizedRect(float X, float Y, float Width, float Height); + + private readonly record struct BgraColor(byte B, byte G, byte R, byte A); + + public static async Task<IconInfo?> RenderLayoutIconAsync(FancyZonesLayoutDescriptor layout, int sizePx = 72) + { + try + { + Logger.LogDebug($"FancyZones thumbnail render starting. LayoutId={layout.Id} Type={layout.ApplyLayout.Type} ZoneCount={layout.ApplyLayout.ZoneCount} Source={layout.Source}"); + if (sizePx < 16) + { + sizePx = 16; + } + + var cachedIcon = TryGetCachedIcon(layout); + if (cachedIcon is not null) + { + Logger.LogDebug($"FancyZones thumbnail cache hit. LayoutId={layout.Id}"); + return cachedIcon; + } + + var rects = GetNormalizedRectsForLayout(layout); + Logger.LogDebug($"FancyZones thumbnail rects computed. LayoutId={layout.Id} RectCount={rects.Count}"); + var pixelBytes = RenderBgra(rects, sizePx, layout.ApplyLayout.ShowSpacing && layout.ApplyLayout.Spacing > 0 ? layout.ApplyLayout.Spacing : 0); + var stream = new InMemoryRandomAccessStream(); + + var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); + encoder.SetPixelData( + BitmapPixelFormat.Bgra8, + BitmapAlphaMode.Premultiplied, + (uint)sizePx, + (uint)sizePx, + 96, + 96, + pixelBytes); + + await encoder.FlushAsync(); + stream.Seek(0); + + var cachePath = GetCachePath(layout); + if (!string.IsNullOrEmpty(cachePath)) + { + try + { + var tempPath = FormattableString.Invariant($"{cachePath}.{Guid.NewGuid():N}.tmp"); + Directory.CreateDirectory(Path.GetDirectoryName(cachePath)!); + await WriteStreamToFileAsync(stream, tempPath); + File.Move(tempPath, cachePath, overwrite: true); + + var fileIcon = new IconInfo(cachePath); + Logger.LogDebug($"FancyZones thumbnail render succeeded (file cache). LayoutId={layout.Id} Path=\"{cachePath}\""); + return fileIcon; + } + catch (Exception ex) + { + Logger.LogWarning($"FancyZones thumbnail write cache failed. LayoutId={layout.Id} Path=\"{cachePath}\" Exception={ex}"); + } + } + + // Fallback: return an in-memory stream icon. This may not marshal reliably cross-proc, + // so prefer the file-cached path above. + stream.Seek(0); + var inMemoryIcon = IconInfo.FromStream(stream); + Logger.LogDebug($"FancyZones thumbnail render succeeded (in-memory). LayoutId={layout.Id}"); + return inMemoryIcon; + } + catch (Exception ex) + { + Logger.LogWarning($"FancyZones thumbnail render failed. LayoutId={layout.Id} Type={layout.ApplyLayout.Type} ZoneCount={layout.ApplyLayout.ZoneCount} Source={layout.Source} Exception={ex}"); + return null; + } + } + + private static IconInfo? TryGetCachedIcon(FancyZonesLayoutDescriptor layout) + { + var cachePath = GetCachePath(layout); + if (string.IsNullOrEmpty(cachePath)) + { + return null; + } + + try + { + if (File.Exists(cachePath)) + { + return new IconInfo(cachePath); + } + } + catch (Exception ex) + { + Logger.LogWarning($"FancyZones thumbnail cache check failed. LayoutId={layout.Id} Path=\"{cachePath}\" Exception={ex}"); + } + + return null; + } + + /// <summary> + /// Removes cached thumbnail files that no longer correspond to any current layout. + /// Call this on startup or periodically to prevent unbounded cache growth. + /// </summary> + public static void PurgeOrphanedCache() + { + try + { + var cacheFolder = GetCacheFolder(); + if (string.IsNullOrEmpty(cacheFolder) || !Directory.Exists(cacheFolder)) + { + return; + } + + // Get all current layouts and compute their expected cache file names + var layouts = FancyZonesDataService.GetLayouts(); + var validHashes = new HashSet<string>(StringComparer.OrdinalIgnoreCase); + foreach (var layout in layouts) + { + validHashes.Add(ComputeLayoutHash(layout) + ".png"); + } + + // Delete any .png files not in the valid set + var deletedCount = 0; + foreach (var filePath in Directory.EnumerateFiles(cacheFolder, "*.png")) + { + var fileName = Path.GetFileName(filePath); + if (!validHashes.Contains(fileName)) + { + try + { + File.Delete(filePath); + deletedCount++; + } + catch (Exception ex) + { + Logger.LogWarning($"FancyZones thumbnail cache purge: failed to delete \"{filePath}\". Exception={ex.Message}"); + } + } + } + + if (deletedCount > 0) + { + Logger.LogInfo($"FancyZones thumbnail cache purge: deleted {deletedCount} orphaned file(s)."); + } + } + catch (Exception ex) + { + Logger.LogWarning($"FancyZones thumbnail cache purge failed. Exception={ex}"); + } + } + + private static string? GetCacheFolder() + { + var basePath = InteropConstants.AppDataPath(); + if (string.IsNullOrWhiteSpace(basePath)) + { + return null; + } + + return Path.Combine(basePath, "CmdPal", "PowerToysExtension", "Cache", "FancyZones", "LayoutThumbnails"); + } + + private static string? GetCachePath(FancyZonesLayoutDescriptor layout) + { + try + { + var cacheFolder = GetCacheFolder(); + if (string.IsNullOrEmpty(cacheFolder)) + { + return null; + } + + var fileName = ComputeLayoutHash(layout) + ".png"; + return Path.Combine(cacheFolder, fileName); + } + catch (Exception ex) + { + Logger.LogWarning($"FancyZones thumbnail cache path failed. LayoutId={layout.Id} Exception={ex}"); + return null; + } + } + + private static string ComputeLayoutHash(FancyZonesLayoutDescriptor layout) + { + var customType = layout.Custom?.Type?.Trim() ?? string.Empty; + var customInfo = layout.Custom is not null && layout.Custom.Value.Info.ValueKind is not JsonValueKind.Undefined and not JsonValueKind.Null + ? layout.Custom.Value.Info.GetRawText() + : string.Empty; + + var fingerprint = FormattableString.Invariant( + $"{layout.Id}|{layout.Source}|{layout.ApplyLayout.Type}|{layout.ApplyLayout.ZoneCount}|{layout.ApplyLayout.ShowSpacing}|{layout.ApplyLayout.Spacing}|{customType}|{customInfo}"); + + var bytes = Encoding.UTF8.GetBytes(fingerprint); + var hash = SHA256.HashData(bytes); + return Convert.ToHexString(hash).ToLowerInvariant(); + } + + private static async Task WriteStreamToFileAsync(IRandomAccessStream stream, string filePath) + { + stream.Seek(0); + var size = stream.Size; + if (size == 0) + { + File.WriteAllBytes(filePath, Array.Empty<byte>()); + return; + } + + if (size > int.MaxValue) + { + throw new InvalidOperationException("Icon stream too large."); + } + + using var input = stream.GetInputStreamAt(0); + using var reader = new DataReader(input); + await reader.LoadAsync((uint)size); + var bytes = new byte[(int)size]; + reader.ReadBytes(bytes); + File.WriteAllBytes(filePath, bytes); + } + + internal static List<NormalizedRect> GetNormalizedRectsForLayout(FancyZonesLayoutDescriptor layout) + { + var type = layout.ApplyLayout.Type.ToLowerInvariant(); + if (layout.Source == FancyZonesLayoutSource.Custom && layout.Custom is not null) + { + return GetCustomRects(layout.Custom.Value); + } + + return type switch + { + "columns" => GetColumnsRects(layout.ApplyLayout.ZoneCount), + "rows" => GetRowsRects(layout.ApplyLayout.ZoneCount), + "grid" => GetGridRects(layout.ApplyLayout.ZoneCount), + "priority-grid" => GetPriorityGridRects(layout.ApplyLayout.ZoneCount), + "focus" => GetFocusRects(layout.ApplyLayout.ZoneCount), + "blank" => new List<NormalizedRect>(), + _ => GetGridRects(layout.ApplyLayout.ZoneCount), + }; + } + + private static List<NormalizedRect> GetCustomRects(CustomLayouts.CustomLayoutWrapper custom) + { + var type = custom.Type?.Trim().ToLowerInvariant() ?? string.Empty; + if (custom.Info.ValueKind is JsonValueKind.Undefined or JsonValueKind.Null) + { + return new List<NormalizedRect>(); + } + + return type switch + { + "grid" => GetCustomGridRects(custom.Info), + "canvas" => GetCustomCanvasRects(custom.Info), + _ => new List<NormalizedRect>(), + }; + } + + private static List<NormalizedRect> GetCustomCanvasRects(JsonElement info) + { + if (!info.TryGetProperty("ref-width", out var refWidthProp) || + !info.TryGetProperty("ref-height", out var refHeightProp) || + !info.TryGetProperty("zones", out var zonesProp)) + { + return new List<NormalizedRect>(); + } + + if (refWidthProp.ValueKind != JsonValueKind.Number || refHeightProp.ValueKind != JsonValueKind.Number || zonesProp.ValueKind != JsonValueKind.Array) + { + return new List<NormalizedRect>(); + } + + var refWidth = Math.Max(1, refWidthProp.GetInt32()); + var refHeight = Math.Max(1, refHeightProp.GetInt32()); + var rects = new List<NormalizedRect>(zonesProp.GetArrayLength()); + + foreach (var zone in zonesProp.EnumerateArray()) + { + if (zone.ValueKind != JsonValueKind.Object) + { + continue; + } + + if (!zone.TryGetProperty("X", out var xProp) || + !zone.TryGetProperty("Y", out var yProp) || + !zone.TryGetProperty("width", out var wProp) || + !zone.TryGetProperty("height", out var hProp)) + { + continue; + } + + if (xProp.ValueKind != JsonValueKind.Number || + yProp.ValueKind != JsonValueKind.Number || + wProp.ValueKind != JsonValueKind.Number || + hProp.ValueKind != JsonValueKind.Number) + { + continue; + } + + var x = xProp.GetSingle() / refWidth; + var y = yProp.GetSingle() / refHeight; + var w = wProp.GetSingle() / refWidth; + var h = hProp.GetSingle() / refHeight; + rects.Add(NormalizeRect(x, y, w, h)); + } + + return rects; + } + + private static List<NormalizedRect> GetCustomGridRects(JsonElement info) + { + if (!TryGetGridDefinition(info, out var rows, out var cols, out var rowsPercents, out var colsPercents, out var cellMap)) + { + return new List<NormalizedRect>(); + } + + return BuildRectsFromGridDefinition(rows, cols, rowsPercents, colsPercents, cellMap); + } + + private static bool TryGetGridDefinition( + JsonElement info, + out int rows, + out int cols, + out int[] rowPercents, + out int[] colPercents, + out int[][] cellChildMap) + { + rows = 0; + cols = 0; + rowPercents = Array.Empty<int>(); + colPercents = Array.Empty<int>(); + cellChildMap = Array.Empty<int[]>(); + + if (!info.TryGetProperty("rows", out var rowsProp) || + !info.TryGetProperty("columns", out var colsProp) || + !info.TryGetProperty("rows-percentage", out var rowsPercentsProp) || + !info.TryGetProperty("columns-percentage", out var colsPercentsProp) || + !info.TryGetProperty("cell-child-map", out var cellMapProp)) + { + return false; + } + + if (rowsProp.ValueKind != JsonValueKind.Number || + colsProp.ValueKind != JsonValueKind.Number || + rowsPercentsProp.ValueKind != JsonValueKind.Array || + colsPercentsProp.ValueKind != JsonValueKind.Array || + cellMapProp.ValueKind != JsonValueKind.Array) + { + return false; + } + + rows = rowsProp.GetInt32(); + cols = colsProp.GetInt32(); + if (rows <= 0 || cols <= 0) + { + return false; + } + + rowPercents = rowsPercentsProp.EnumerateArray().Where(v => v.ValueKind == JsonValueKind.Number).Select(v => v.GetInt32()).ToArray(); + colPercents = colsPercentsProp.EnumerateArray().Where(v => v.ValueKind == JsonValueKind.Number).Select(v => v.GetInt32()).ToArray(); + + if (rowPercents.Length != rows || colPercents.Length != cols) + { + return false; + } + + var mapRows = new List<int[]>(rows); + foreach (var row in cellMapProp.EnumerateArray()) + { + if (row.ValueKind != JsonValueKind.Array) + { + return false; + } + + var cells = row.EnumerateArray().Where(v => v.ValueKind == JsonValueKind.Number).Select(v => v.GetInt32()).ToArray(); + if (cells.Length != cols) + { + return false; + } + + mapRows.Add(cells); + } + + if (mapRows.Count != rows) + { + return false; + } + + cellChildMap = mapRows.ToArray(); + return true; + } + + private static List<NormalizedRect> GetColumnsRects(int zoneCount) + { + zoneCount = Math.Clamp(zoneCount, 1, 16); + var rects = new List<NormalizedRect>(zoneCount); + for (var i = 0; i < zoneCount; i++) + { + rects.Add(new NormalizedRect(i / (float)zoneCount, 0, 1f / zoneCount, 1f)); + } + + return rects; + } + + private static List<NormalizedRect> GetRowsRects(int zoneCount) + { + zoneCount = Math.Clamp(zoneCount, 1, 16); + var rects = new List<NormalizedRect>(zoneCount); + for (var i = 0; i < zoneCount; i++) + { + rects.Add(new NormalizedRect(0, i / (float)zoneCount, 1f, 1f / zoneCount)); + } + + return rects; + } + + private static List<NormalizedRect> GetGridRects(int zoneCount) + { + zoneCount = Math.Clamp(zoneCount, 1, 25); + var rows = 1; + while (zoneCount / rows >= rows) + { + rows++; + } + + rows--; + var cols = zoneCount / rows; + if (zoneCount % rows != 0) + { + cols++; + } + + var rowPercents = Enumerable.Repeat(10000 / rows, rows).ToArray(); + var colPercents = Enumerable.Repeat(10000 / cols, cols).ToArray(); + var cellMap = new int[rows][]; + + var index = 0; + for (var r = 0; r < rows; r++) + { + cellMap[r] = new int[cols]; + for (var c = 0; c < cols; c++) + { + cellMap[r][c] = index; + index++; + if (index == zoneCount) + { + index--; + } + } + } + + return BuildRectsFromGridDefinition(rows, cols, rowPercents, colPercents, cellMap); + } + + private static List<NormalizedRect> GetPriorityGridRects(int zoneCount) + { + zoneCount = Math.Clamp(zoneCount, 1, 25); + + if (zoneCount is >= 1 and <= 11 && PriorityGrid.TryGetValue(zoneCount, out var def)) + { + return BuildRectsFromGridDefinition(def.Rows, def.Cols, def.RowPercents, def.ColPercents, def.CellMap); + } + + return GetGridRects(zoneCount); + } + + private static List<NormalizedRect> GetFocusRects(int zoneCount) + { + zoneCount = Math.Clamp(zoneCount, 1, 8); + var rects = new List<NormalizedRect>(zoneCount); + for (var i = 0; i < zoneCount; i++) + { + var offset = i * 0.06f; + rects.Add(new NormalizedRect(0.1f + offset, 0.1f + offset, 0.8f, 0.8f)); + } + + return rects; + } + + private static List<NormalizedRect> BuildRectsFromGridDefinition(int rows, int cols, int[] rowPercents, int[] colPercents, int[][] cellChildMap) + { + const float multiplier = 10000f; + + var rowPrefix = new float[rows + 1]; + var colPrefix = new float[cols + 1]; + + for (var r = 0; r < rows; r++) + { + rowPrefix[r + 1] = rowPrefix[r] + (rowPercents[r] / multiplier); + } + + for (var c = 0; c < cols; c++) + { + colPrefix[c + 1] = colPrefix[c] + (colPercents[c] / multiplier); + } + + var maxZone = -1; + for (var r = 0; r < rows; r++) + { + for (var c = 0; c < cols; c++) + { + maxZone = Math.Max(maxZone, cellChildMap[r][c]); + } + } + + var rects = new List<NormalizedRect>(maxZone + 1); + for (var i = 0; i <= maxZone; i++) + { + rects.Add(new NormalizedRect(1, 1, 0, 0)); + } + + for (var r = 0; r < rows; r++) + { + for (var c = 0; c < cols; c++) + { + var zoneId = cellChildMap[r][c]; + if (zoneId < 0 || zoneId >= rects.Count) + { + continue; + } + + var x1 = colPrefix[c]; + var y1 = rowPrefix[r]; + var x2 = colPrefix[c + 1]; + var y2 = rowPrefix[r + 1]; + + var existing = rects[zoneId]; + if (existing.Width <= 0 || existing.Height <= 0) + { + rects[zoneId] = new NormalizedRect(x1, y1, x2 - x1, y2 - y1); + } + else + { + var ex2 = existing.X + existing.Width; + var ey2 = existing.Y + existing.Height; + var nx1 = Math.Min(existing.X, x1); + var ny1 = Math.Min(existing.Y, y1); + var nx2 = Math.Max(ex2, x2); + var ny2 = Math.Max(ey2, y2); + rects[zoneId] = new NormalizedRect(nx1, ny1, nx2 - nx1, ny2 - ny1); + } + } + } + + return rects + .Where(r => r.Width > 0 && r.Height > 0) + .Select(r => NormalizeRect(r.X, r.Y, r.Width, r.Height)) + .ToList(); + } + + private static NormalizedRect NormalizeRect(float x, float y, float w, float h) + { + x = Math.Clamp(x, 0, 1); + y = Math.Clamp(y, 0, 1); + w = Math.Clamp(w, 0, 1 - x); + h = Math.Clamp(h, 0, 1 - y); + return new NormalizedRect(x, y, w, h); + } + + private static byte[] RenderBgra(IReadOnlyList<NormalizedRect> rects, int sizePx, int spacing) + { + var pixels = new byte[sizePx * sizePx * 4]; + + var border = Premultiply(new BgraColor(0x30, 0x30, 0x30, 0xFF)); + var frame = Premultiply(new BgraColor(0x40, 0x40, 0x40, 0xA0)); + var fill = Premultiply(new BgraColor(0xFF, 0xD8, 0x8C, 0xC0)); // light-ish blue with alpha + var background = Premultiply(new BgraColor(0x00, 0x00, 0x00, 0x00)); + + for (var i = 0; i < pixels.Length; i += 4) + { + pixels[i + 0] = background.B; + pixels[i + 1] = background.G; + pixels[i + 2] = background.R; + pixels[i + 3] = background.A; + } + + DrawRectBorder(pixels, sizePx, 1, 1, sizePx - 1, sizePx - 1, frame); + + var gapPx = spacing > 0 ? Math.Clamp(spacing / 8, 1, 3) : 0; + foreach (var rect in rects) + { + var (x1, y1, x2, y2) = ToPixelBounds(rect, sizePx, gapPx); + if (x2 <= x1 || y2 <= y1) + { + continue; + } + + FillRect(pixels, sizePx, x1, y1, x2, y2, fill); + DrawRectBorder(pixels, sizePx, x1, y1, x2, y2, border); + } + + return pixels; + } + + private static (int X1, int Y1, int X2, int Y2) ToPixelBounds(NormalizedRect rect, int sizePx, int gapPx) + { + var x1 = (int)MathF.Round(rect.X * sizePx); + var y1 = (int)MathF.Round(rect.Y * sizePx); + var x2 = (int)MathF.Round((rect.X + rect.Width) * sizePx); + var y2 = (int)MathF.Round((rect.Y + rect.Height) * sizePx); + + x1 = Math.Clamp(x1 + gapPx, 0, sizePx - 1); + y1 = Math.Clamp(y1 + gapPx, 0, sizePx - 1); + x2 = Math.Clamp(x2 - gapPx, 1, sizePx); + y2 = Math.Clamp(y2 - gapPx, 1, sizePx); + + if (x2 <= x1 + 1) + { + x2 = Math.Min(sizePx, x1 + 2); + } + + if (y2 <= y1 + 1) + { + y2 = Math.Min(sizePx, y1 + 2); + } + + return (x1, y1, x2, y2); + } + + private static void FillRect(byte[] pixels, int sizePx, int x1, int y1, int x2, int y2, BgraColor color) + { + for (var y = y1; y < y2; y++) + { + var rowStart = y * sizePx * 4; + for (var x = x1; x < x2; x++) + { + var i = rowStart + (x * 4); + pixels[i + 0] = color.B; + pixels[i + 1] = color.G; + pixels[i + 2] = color.R; + pixels[i + 3] = color.A; + } + } + } + + private static void DrawRectBorder(byte[] pixels, int sizePx, int x1, int y1, int x2, int y2, BgraColor color) + { + var left = x1; + var right = x2 - 1; + var top = y1; + var bottom = y2 - 1; + + for (var x = left; x <= right; x++) + { + SetPixel(pixels, sizePx, x, top, color); + SetPixel(pixels, sizePx, x, bottom, color); + } + + for (var y = top; y <= bottom; y++) + { + SetPixel(pixels, sizePx, left, y, color); + SetPixel(pixels, sizePx, right, y, color); + } + } + + private static void SetPixel(byte[] pixels, int sizePx, int x, int y, BgraColor color) + { + if ((uint)x >= (uint)sizePx || (uint)y >= (uint)sizePx) + { + return; + } + + var i = ((y * sizePx) + x) * 4; + pixels[i + 0] = color.B; + pixels[i + 1] = color.G; + pixels[i + 2] = color.R; + pixels[i + 3] = color.A; + } + + private static BgraColor Premultiply(BgraColor color) + { + if (color.A == 0 || color.A == 255) + { + return color; + } + + byte Premul(byte c) => (byte)(((c * color.A) + 127) / 255); + return new BgraColor(Premul(color.B), Premul(color.G), Premul(color.R), color.A); + } + + private sealed record PriorityGridDefinition(int Rows, int Cols, int[] RowPercents, int[] ColPercents, int[][] CellMap); + + private static readonly IReadOnlyDictionary<int, PriorityGridDefinition> PriorityGrid = new Dictionary<int, PriorityGridDefinition> + { + [1] = new PriorityGridDefinition(1, 1, [10000], [10000], [[0]]), + [2] = new PriorityGridDefinition(1, 2, [10000], [6667, 3333], [[0, 1]]), + [3] = new PriorityGridDefinition(1, 3, [10000], [2500, 5000, 2500], [[0, 1, 2]]), + [4] = new PriorityGridDefinition(2, 3, [5000, 5000], [2500, 5000, 2500], [[0, 1, 2], [0, 1, 3]]), + [5] = new PriorityGridDefinition(2, 3, [5000, 5000], [2500, 5000, 2500], [[0, 1, 2], [3, 1, 4]]), + [6] = new PriorityGridDefinition(3, 3, [3333, 3334, 3333], [2500, 5000, 2500], [[0, 1, 2], [0, 1, 3], [4, 1, 5]]), + [7] = new PriorityGridDefinition(3, 3, [3333, 3334, 3333], [2500, 5000, 2500], [[0, 1, 2], [3, 1, 4], [5, 1, 6]]), + [8] = new PriorityGridDefinition(3, 4, [3333, 3334, 3333], [2500, 2500, 2500, 2500], [[0, 1, 2, 3], [4, 1, 2, 5], [6, 1, 2, 7]]), + [9] = new PriorityGridDefinition(3, 4, [3333, 3334, 3333], [2500, 2500, 2500, 2500], [[0, 1, 2, 3], [4, 1, 2, 5], [6, 1, 7, 8]]), + [10] = new PriorityGridDefinition(3, 4, [3333, 3334, 3333], [2500, 2500, 2500, 2500], [[0, 1, 2, 3], [4, 1, 5, 6], [7, 1, 8, 9]]), + [11] = new PriorityGridDefinition(3, 4, [3333, 3334, 3333], [2500, 2500, 2500, 2500], [[0, 1, 2, 3], [4, 1, 5, 6], [7, 8, 9, 10]]), + }; +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesVirtualDesktop.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesVirtualDesktop.cs new file mode 100644 index 0000000000..274b6ef5c7 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/FancyZonesVirtualDesktop.cs @@ -0,0 +1,103 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using Microsoft.Win32; + +namespace PowerToysExtension.Helpers; + +internal static class FancyZonesVirtualDesktop +{ + private const string VirtualDesktopsKey = @"Software\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops"; + private const string SessionVirtualDesktopsKeyPrefix = @"Software\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\"; + private const string SessionVirtualDesktopsKeySuffix = @"\VirtualDesktops"; + private const string CurrentVirtualDesktopValue = "CurrentVirtualDesktop"; + private const string VirtualDesktopIdsValue = "VirtualDesktopIDs"; + + public static string GetCurrentVirtualDesktopIdString() + { + var id = TryGetCurrentVirtualDesktopId() + ?? TryGetCurrentVirtualDesktopIdFromSession() + ?? TryGetFirstVirtualDesktopId() + ?? Guid.Empty; + + return "{" + id.ToString().ToUpperInvariant() + "}"; + } + + private static Guid? TryGetCurrentVirtualDesktopId() + { + try + { + using var key = Registry.CurrentUser.OpenSubKey(VirtualDesktopsKey, writable: false); + var bytes = key?.GetValue(CurrentVirtualDesktopValue) as byte[]; + return TryGetGuid(bytes); + } + catch + { + return null; + } + } + + private static Guid? TryGetCurrentVirtualDesktopIdFromSession() + { + try + { + if (!ProcessIdToSessionId((uint)Environment.ProcessId, out var sessionId)) + { + return null; + } + + var path = SessionVirtualDesktopsKeyPrefix + sessionId + SessionVirtualDesktopsKeySuffix; + using var key = Registry.CurrentUser.OpenSubKey(path, writable: false); + var bytes = key?.GetValue(CurrentVirtualDesktopValue) as byte[]; + return TryGetGuid(bytes); + } + catch + { + return null; + } + } + + private static Guid? TryGetFirstVirtualDesktopId() + { + try + { + using var key = Registry.CurrentUser.OpenSubKey(VirtualDesktopsKey, writable: false); + var bytes = key?.GetValue(VirtualDesktopIdsValue) as byte[]; + if (bytes is null || bytes.Length < 16) + { + return null; + } + + var first = new byte[16]; + Array.Copy(bytes, 0, first, 0, 16); + return TryGetGuid(first); + } + catch + { + return null; + } + } + + private static Guid? TryGetGuid(byte[]? bytes) + { + try + { + if (bytes is null || bytes.Length < 16) + { + return null; + } + + return new Guid(bytes.AsSpan(0, 16)); + } + catch + { + return null; + } + } + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool ProcessIdToSessionId(uint dwProcessId, out uint pSessionId); +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/GpoEnablementService.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/GpoEnablementService.cs new file mode 100644 index 0000000000..244870ebe5 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/GpoEnablementService.cs @@ -0,0 +1,86 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Microsoft.Win32; + +namespace PowerToysExtension.Helpers; + +internal enum GpoRuleConfiguredValue +{ + WrongValue = -3, + Unavailable = -2, + NotConfigured = -1, + Disabled = 0, + Enabled = 1, +} + +/// <summary> +/// Lightweight GPO reader for module/feature enablement policies. +/// Mirrors the logic in src/common/utils/gpo.h but avoids taking a dependency on the full GPOWrapper. +/// </summary> +internal static class GpoEnablementService +{ + private const string PoliciesPath = @"SOFTWARE\Policies\PowerToys"; + private const string PolicyConfigureEnabledGlobalAllUtilities = "ConfigureGlobalUtilityEnabledState"; + + internal static GpoRuleConfiguredValue GetUtilityEnabledValue(string individualPolicyValueName) + { + if (!string.IsNullOrEmpty(individualPolicyValueName)) + { + var individual = GetConfiguredValue(individualPolicyValueName); + if (individual is GpoRuleConfiguredValue.Disabled or GpoRuleConfiguredValue.Enabled) + { + return individual; + } + } + + return GetConfiguredValue(PolicyConfigureEnabledGlobalAllUtilities); + } + + private static GpoRuleConfiguredValue GetConfiguredValue(string registryValueName) + { + try + { + // Machine scope has priority over user scope. + var value = ReadRegistryValue(Registry.LocalMachine, registryValueName); + value ??= ReadRegistryValue(Registry.CurrentUser, registryValueName); + + if (!value.HasValue) + { + return GpoRuleConfiguredValue.NotConfigured; + } + + return value.Value switch + { + 0 => GpoRuleConfiguredValue.Disabled, + 1 => GpoRuleConfiguredValue.Enabled, + _ => GpoRuleConfiguredValue.WrongValue, + }; + } + catch + { + return GpoRuleConfiguredValue.Unavailable; + } + } + + private static int? ReadRegistryValue(RegistryKey rootKey, string valueName) + { + try + { + using var key = rootKey.OpenSubKey(PoliciesPath, writable: false); + if (key is null) + { + return null; + } + + var value = key.GetValue(valueName); + return value as int?; + } + catch + { + return null; + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/ModuleCommandCatalog.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/ModuleCommandCatalog.cs new file mode 100644 index 0000000000..679c94bde0 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/ModuleCommandCatalog.cs @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Modules; + +namespace PowerToysExtension.Helpers; + +/// <summary> +/// Aggregates commands exposed by individual module providers and applies fuzzy filtering. +/// </summary> +internal static class ModuleCommandCatalog +{ + private static readonly ModuleCommandProvider[] Providers = + [ + new AwakeModuleCommandProvider(), + new AdvancedPasteModuleCommandProvider(), + new WorkspacesModuleCommandProvider(), + new LightSwitchModuleCommandProvider(), + new PowerToysRunModuleCommandProvider(), + new ScreenRulerModuleCommandProvider(), + new ShortcutGuideModuleCommandProvider(), + new TextExtractorModuleCommandProvider(), + new ZoomItModuleCommandProvider(), + new ColorPickerModuleCommandProvider(), + new AlwaysOnTopModuleCommandProvider(), + new CropAndLockModuleCommandProvider(), + new FancyZonesModuleCommandProvider(), + new KeyboardManagerModuleCommandProvider(), + new MouseUtilsModuleCommandProvider(), + new MouseWithoutBordersModuleCommandProvider(), + new QuickAccentModuleCommandProvider(), + new FileExplorerAddonsModuleCommandProvider(), + new FileLocksmithModuleCommandProvider(), + new ImageResizerModuleCommandProvider(), + new NewPlusModuleCommandProvider(), + new PeekModuleCommandProvider(), + new PowerRenameModuleCommandProvider(), + new CommandNotFoundModuleCommandProvider(), + new EnvironmentVariablesModuleCommandProvider(), + new HostsModuleCommandProvider(), + new RegistryPreviewModuleCommandProvider(), + ]; + + public static IListItem[] GetAllItems() + { + return [.. Providers.SelectMany(provider => provider.BuildCommands())]; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/ModuleEnablementService.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/ModuleEnablementService.cs new file mode 100644 index 0000000000..fccf5b8687 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/ModuleEnablementService.cs @@ -0,0 +1,162 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Helpers; + +/// <summary> +/// Reads PowerToys module enablement flags from the global settings.json. +/// </summary> +internal static class ModuleEnablementService +{ + internal static string SettingsFilePath { get; } = Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + "Microsoft", + "PowerToys", + "settings.json"); + + internal static bool IsModuleEnabled(SettingsWindow module) + { + var key = GetEnabledKey(module); + if (string.IsNullOrEmpty(key)) + { + var globalRule = GpoEnablementService.GetUtilityEnabledValue(string.Empty); + return globalRule != GpoRuleConfiguredValue.Disabled; + } + + return IsKeyEnabled(key); + } + + internal static bool IsKeyEnabled(string enabledKey) + { + if (string.IsNullOrWhiteSpace(enabledKey)) + { + return true; + } + + var gpoPolicy = GetGpoPolicyForEnabledKey(enabledKey); + var gpoRule = GpoEnablementService.GetUtilityEnabledValue(gpoPolicy); + if (gpoRule == GpoRuleConfiguredValue.Disabled) + { + return false; + } + + if (gpoRule == GpoRuleConfiguredValue.Enabled) + { + return true; + } + + try + { + var enabled = ReadEnabledFlags(); + return enabled is null || !enabled.TryGetValue(enabledKey, out var value) || value; + } + catch + { + return true; + } + } + + private static Dictionary<string, bool>? ReadEnabledFlags() + { + if (!File.Exists(SettingsFilePath)) + { + return null; + } + + var json = File.ReadAllText(SettingsFilePath).Trim('\0'); + using var doc = JsonDocument.Parse(json); + + if (!doc.RootElement.TryGetProperty("enabled", out var enabledRoot) || + enabledRoot.ValueKind != JsonValueKind.Object) + { + return null; + } + + var result = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase); + foreach (var prop in enabledRoot.EnumerateObject()) + { + if (prop.Value.ValueKind is JsonValueKind.True or JsonValueKind.False) + { + result[prop.Name] = prop.Value.GetBoolean(); + } + } + + return result; + } + + private static string GetEnabledKey(SettingsWindow module) => module switch + { + SettingsWindow.Awake => "Awake", + SettingsWindow.AdvancedPaste => "AdvancedPaste", + SettingsWindow.AlwaysOnTop => "AlwaysOnTop", + SettingsWindow.ColorPicker => "ColorPicker", + SettingsWindow.CropAndLock => "CropAndLock", + SettingsWindow.EnvironmentVariables => "EnvironmentVariables", + SettingsWindow.FancyZones => "FancyZones", + SettingsWindow.FileExplorer => "File Explorer Preview", + SettingsWindow.FileLocksmith => "FileLocksmith", + SettingsWindow.Hosts => "Hosts", + SettingsWindow.ImageResizer => "Image Resizer", + SettingsWindow.KBM => "Keyboard Manager", + SettingsWindow.LightSwitch => "LightSwitch", + SettingsWindow.MeasureTool => "Measure Tool", + SettingsWindow.MouseWithoutBorders => "MouseWithoutBorders", + SettingsWindow.NewPlus => "NewPlus", + SettingsWindow.Peek => "Peek", + SettingsWindow.PowerAccent => "QuickAccent", + SettingsWindow.PowerLauncher => "PowerToys Run", + SettingsWindow.Run => "PowerToys Run", + SettingsWindow.PowerRename => "PowerRename", + SettingsWindow.PowerOCR => "TextExtractor", + SettingsWindow.RegistryPreview => "RegistryPreview", + SettingsWindow.ShortcutGuide => "Shortcut Guide", + SettingsWindow.Workspaces => "Workspaces", + SettingsWindow.ZoomIt => "ZoomIt", + SettingsWindow.CmdNotFound => "CmdNotFound", + SettingsWindow.CmdPal => "CmdPal", + _ => string.Empty, + }; + + private static string GetGpoPolicyForEnabledKey(string enabledKey) => enabledKey switch + { + "AdvancedPaste" => "ConfigureEnabledUtilityAdvancedPaste", + "AlwaysOnTop" => "ConfigureEnabledUtilityAlwaysOnTop", + "Awake" => "ConfigureEnabledUtilityAwake", + "CmdNotFound" => "ConfigureEnabledUtilityCmdNotFound", + "CmdPal" => "ConfigureEnabledUtilityCmdPal", + "ColorPicker" => "ConfigureEnabledUtilityColorPicker", + "CropAndLock" => "ConfigureEnabledUtilityCropAndLock", + "CursorWrap" => "ConfigureEnabledUtilityCursorWrap", + "EnvironmentVariables" => "ConfigureEnabledUtilityEnvironmentVariables", + "FancyZones" => "ConfigureEnabledUtilityFancyZones", + "FileLocksmith" => "ConfigureEnabledUtilityFileLocksmith", + "FindMyMouse" => "ConfigureEnabledUtilityFindMyMouse", + "Hosts" => "ConfigureEnabledUtilityHostsFileEditor", + "Image Resizer" => "ConfigureEnabledUtilityImageResizer", + "Keyboard Manager" => "ConfigureEnabledUtilityKeyboardManager", + "LightSwitch" => "ConfigureEnabledUtilityLightSwitch", + "Measure Tool" => "ConfigureEnabledUtilityScreenRuler", + "MouseHighlighter" => "ConfigureEnabledUtilityMouseHighlighter", + "MouseJump" => "ConfigureEnabledUtilityMouseJump", + "MousePointerCrosshairs" => "ConfigureEnabledUtilityMousePointerCrosshairs", + "MouseWithoutBorders" => "ConfigureEnabledUtilityMouseWithoutBorders", + "NewPlus" => "ConfigureEnabledUtilityNewPlus", + "Peek" => "ConfigureEnabledUtilityPeek", + "PowerRename" => "ConfigureEnabledUtilityPowerRename", + "PowerToys Run" => "ConfigureEnabledUtilityPowerLauncher", + "QuickAccent" => "ConfigureEnabledUtilityQuickAccent", + "RegistryPreview" => "ConfigureEnabledUtilityRegistryPreview", + "Shortcut Guide" => "ConfigureEnabledUtilityShortcutGuide", + "TextExtractor" => "ConfigureEnabledUtilityTextExtractor", + "Workspaces" => "ConfigureEnabledUtilityWorkspaces", + "ZoomIt" => "ConfigureEnabledUtilityZoomIt", + _ => string.Empty, + }; +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/PowerToysFallbackCommandItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/PowerToysFallbackCommandItem.cs new file mode 100644 index 0000000000..7ce4d2b27b --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/PowerToysFallbackCommandItem.cs @@ -0,0 +1,88 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using Common.Search.FuzzSearch; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; + +namespace PowerToysExtension.Helpers; + +/// <summary> +/// A fallback item that filters itself based on the landing-page query. +/// It hides itself (empty Title) when the query doesn't fuzzy-match the title or subtitle. +/// </summary> +internal sealed partial class PowerToysFallbackCommandItem : FallbackCommandItem, IFallbackHandler +{ + private readonly string _baseTitle; + private readonly string _baseSubtitle; + private readonly string _baseName; + private readonly Command? _mutableCommand; + + public PowerToysFallbackCommandItem(ICommand command, string title, string subtitle, IIconInfo? icon, IContextItem[]? moreCommands) + : base(command, title) + { + _baseTitle = title ?? string.Empty; + _baseSubtitle = subtitle ?? string.Empty; + _baseName = command?.Name ?? string.Empty; + _mutableCommand = command as Command; + + // Start hidden; we only surface when the query matches + Title = string.Empty; + Subtitle = string.Empty; + if (_mutableCommand is not null) + { + _mutableCommand.Name = string.Empty; + } + + if (icon != null) + { + Icon = icon; + } + + MoreCommands = moreCommands ?? Array.Empty<IContextItem>(); + + // Ensure fallback updates route to this instance + FallbackHandler = this; + } + + public override void UpdateQuery(string query) + { + if (string.IsNullOrWhiteSpace(query)) + { + Title = string.Empty; + Subtitle = string.Empty; + if (_mutableCommand is not null) + { + _mutableCommand.Name = string.Empty; + } + + return; + } + + // Simple fuzzy match against title/subtitle; hide if neither match + var titleMatch = Common.Search.FuzzSearch.StringMatcher.FuzzyMatch(query, _baseTitle); + var subtitleMatch = Common.Search.FuzzSearch.StringMatcher.FuzzyMatch(query, _baseSubtitle); + var matches = (titleMatch.Success && titleMatch.Score > 0) || (subtitleMatch.Success && subtitleMatch.Score > 0); + + if (matches) + { + Title = _baseTitle; + Subtitle = _baseSubtitle; + if (_mutableCommand is not null) + { + _mutableCommand.Name = _baseName; + } + } + else + { + Title = string.Empty; + Subtitle = string.Empty; + if (_mutableCommand is not null) + { + _mutableCommand.Name = string.Empty; + } + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/PowerToysPathResolver.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/PowerToysPathResolver.cs new file mode 100644 index 0000000000..d9ac9443fe --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/PowerToysPathResolver.cs @@ -0,0 +1,161 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using Microsoft.Win32; + +namespace PowerToysExtension.Helpers; + +/// <summary> +/// Helper methods for locating the installed PowerToys binaries. +/// </summary> +internal static class PowerToysPathResolver +{ + private const string PowerToysProtocolKey = @"Software\Classes\powertoys"; + private const string PowerToysUserKey = @"Software\Microsoft\PowerToys"; + + internal static string GetPowerToysInstallPath() + { + var perUser = GetInstallPathFromRegistry(RegistryHive.CurrentUser); + if (!string.IsNullOrEmpty(perUser)) + { + return perUser; + } + + return GetInstallPathFromRegistry(RegistryHive.LocalMachine); + } + + internal static string TryResolveExecutable(string executableName) + { + if (string.IsNullOrEmpty(executableName)) + { + return string.Empty; + } + + var baseDirectory = GetPowerToysInstallPath(); + if (string.IsNullOrEmpty(baseDirectory)) + { + return string.Empty; + } + + var candidate = Path.Combine(baseDirectory, executableName); + return File.Exists(candidate) ? candidate : string.Empty; + } + + private static string GetInstallPathFromRegistry(RegistryHive hive) + { + try + { + using var baseKey = RegistryKey.OpenBaseKey(hive, RegistryView.Registry64); + + var protocolPath = GetPathFromProtocolRegistration(baseKey); + if (!string.IsNullOrEmpty(protocolPath)) + { + return protocolPath; + } + + if (hive == RegistryHive.CurrentUser) + { + var userPath = GetPathFromUserRegistration(baseKey); + if (!string.IsNullOrEmpty(userPath)) + { + return userPath; + } + } + } + catch + { + // Ignore registry access failures and fall back to other checks. + } + + return string.Empty; + } + + private static string GetPathFromProtocolRegistration(RegistryKey baseKey) + { + try + { + using var commandKey = baseKey.OpenSubKey($@"{PowerToysProtocolKey}\shell\open\command"); + if (commandKey == null) + { + return string.Empty; + } + + var command = commandKey.GetValue(string.Empty)?.ToString() ?? string.Empty; + if (string.IsNullOrEmpty(command)) + { + return string.Empty; + } + + return ExtractInstallDirectory(command); + } + catch + { + return string.Empty; + } + } + + private static string GetPathFromUserRegistration(RegistryKey baseKey) + { + try + { + using var userKey = baseKey.OpenSubKey(PowerToysUserKey); + if (userKey == null) + { + return string.Empty; + } + + var installedValue = userKey.GetValue("installed"); + if (installedValue != null && installedValue.ToString() == "1") + { + return GetPathFromProtocolRegistration(baseKey); + } + } + catch + { + // Ignore registry access failures. + } + + return string.Empty; + } + + private static string ExtractInstallDirectory(string command) + { + if (string.IsNullOrEmpty(command)) + { + return string.Empty; + } + + try + { + if (command.StartsWith('"')) + { + var closingQuote = command.IndexOf('"', 1); + if (closingQuote > 1) + { + var quotedPath = command.Substring(1, closingQuote - 1); + if (File.Exists(quotedPath)) + { + return Path.GetDirectoryName(quotedPath) ?? string.Empty; + } + } + } + else + { + var parts = command.Split(' '); + if (parts.Length > 0 && File.Exists(parts[0])) + { + return Path.GetDirectoryName(parts[0]) ?? string.Empty; + } + } + } + catch + { + // Fall through and report no path. + } + + return string.Empty; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/PowerToysResourcesHelper.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/PowerToysResourcesHelper.cs new file mode 100644 index 0000000000..91dd3f05b1 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/PowerToysResourcesHelper.cs @@ -0,0 +1,99 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CommandPalette.Extensions.Toolkit; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Helpers; + +internal static class PowerToysResourcesHelper +{ + private const string SettingsIconRoot = "WinUI3Apps\\Assets\\Settings\\Icons\\"; + + internal static IconInfo IconFromSettingsIcon(string fileName) => IconHelpers.FromRelativePath($"{SettingsIconRoot}{fileName}"); + + public static IconInfo ProviderIcon() => IconFromSettingsIcon("PowerToys.png"); + + public static IconInfo ModuleIcon(this SettingsWindow module) + { + var iconFile = module switch + { + SettingsWindow.ColorPicker => "ColorPicker.png", + SettingsWindow.FancyZones => "FancyZones.png", + SettingsWindow.Hosts => "Hosts.png", + SettingsWindow.PowerOCR => "TextExtractor.png", + SettingsWindow.RegistryPreview => "RegistryPreview.png", + SettingsWindow.MeasureTool => "ScreenRuler.png", + SettingsWindow.ShortcutGuide => "ShortcutGuide.png", + SettingsWindow.CropAndLock => "CropAndLock.png", + SettingsWindow.EnvironmentVariables => "EnvironmentVariables.png", + SettingsWindow.Awake => "Awake.png", + SettingsWindow.PowerRename => "PowerRename.png", + SettingsWindow.Run => "PowerToysRun.png", + SettingsWindow.ImageResizer => "ImageResizer.png", + SettingsWindow.KBM => "KeyboardManager.png", + SettingsWindow.MouseUtils => "MouseUtils.png", + SettingsWindow.Workspaces => "Workspaces.png", + SettingsWindow.AdvancedPaste => "AdvancedPaste.png", + SettingsWindow.CmdPal => "CmdPal.png", + SettingsWindow.ZoomIt => "ZoomIt.png", + SettingsWindow.FileExplorer => "FileExplorerPreview.png", + SettingsWindow.FileLocksmith => "FileLocksmith.png", + SettingsWindow.NewPlus => "NewPlus.png", + SettingsWindow.Peek => "Peek.png", + SettingsWindow.LightSwitch => "LightSwitch.png", + SettingsWindow.AlwaysOnTop => "AlwaysOnTop.png", + SettingsWindow.CmdNotFound => "CommandNotFound.png", + SettingsWindow.MouseWithoutBorders => "MouseWithoutBorders.png", + SettingsWindow.PowerAccent => "QuickAccent.png", + SettingsWindow.PowerLauncher => "PowerToysRun.png", + SettingsWindow.PowerPreview => "FileExplorerPreview.png", + SettingsWindow.Overview => "PowerToys.png", + SettingsWindow.Dashboard => "PowerToys.png", + _ => "PowerToys.png", + }; + + return IconFromSettingsIcon(iconFile); + } + + public static string ModuleDisplayName(this SettingsWindow module) + { + return module switch + { + SettingsWindow.ColorPicker => "Color Picker", + SettingsWindow.FancyZones => "FancyZones", + SettingsWindow.Hosts => "Hosts File Editor", + SettingsWindow.PowerOCR => "Text Extractor", + SettingsWindow.RegistryPreview => "Registry Preview", + SettingsWindow.MeasureTool => "Screen Ruler", + SettingsWindow.ShortcutGuide => "Shortcut Guide", + SettingsWindow.CropAndLock => "Crop And Lock", + SettingsWindow.EnvironmentVariables => "Environment Variables", + SettingsWindow.Awake => "Awake", + SettingsWindow.PowerRename => "PowerRename", + SettingsWindow.Run => "PowerToys Run", + SettingsWindow.ImageResizer => "Image Resizer", + SettingsWindow.KBM => "Keyboard Manager", + SettingsWindow.MouseUtils => "Mouse Utilities", + SettingsWindow.Workspaces => "Workspaces", + SettingsWindow.AdvancedPaste => "Advanced Paste", + SettingsWindow.CmdPal => "Command Palette", + SettingsWindow.ZoomIt => "ZoomIt", + SettingsWindow.FileExplorer => "File Explorer Add-ons", + SettingsWindow.FileLocksmith => "File Locksmith", + SettingsWindow.NewPlus => "New+", + SettingsWindow.Peek => "Peek", + SettingsWindow.LightSwitch => "Light Switch", + SettingsWindow.AlwaysOnTop => "Always On Top", + SettingsWindow.CmdNotFound => "Command Not Found", + SettingsWindow.MouseWithoutBorders => "Mouse Without Borders", + SettingsWindow.PowerAccent => "Quick Accent", + SettingsWindow.Overview => "General", + SettingsWindow.Dashboard => "Dashboard", + SettingsWindow.PowerLauncher => "PowerToys Run", + SettingsWindow.PowerPreview => "File Explorer Add-ons", + _ => module.ToString(), + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/SettingsChangeNotifier.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/SettingsChangeNotifier.cs new file mode 100644 index 0000000000..c271bc853b --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Helpers/SettingsChangeNotifier.cs @@ -0,0 +1,72 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using System.Threading; +using ManagedCommon; + +namespace PowerToysExtension.Helpers; + +/// <summary> +/// Watches the global PowerToys settings.json and notifies listeners when it changes. +/// </summary> +internal static class SettingsChangeNotifier +{ + private static readonly object Sync = new(); + private static FileSystemWatcher? _watcher; + private static Timer? _debounceTimer; + + internal static event Action? SettingsChanged; + + static SettingsChangeNotifier() + { + TryStartWatcher(); + } + + private static void TryStartWatcher() + { + try + { + var filePath = ModuleEnablementService.SettingsFilePath; + var directory = Path.GetDirectoryName(filePath); + var fileName = Path.GetFileName(filePath); + + if (string.IsNullOrEmpty(directory) || string.IsNullOrEmpty(fileName)) + { + return; + } + + _watcher = new FileSystemWatcher(directory) + { + Filter = fileName, + NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size, + IncludeSubdirectories = false, + EnableRaisingEvents = true, + }; + + _watcher.Changed += (_, _) => ScheduleRaise(); + _watcher.Created += (_, _) => ScheduleRaise(); + _watcher.Deleted += (_, _) => ScheduleRaise(); + _watcher.Renamed += (_, _) => ScheduleRaise(); + } + catch (Exception ex) + { + Logger.LogError($"SettingsChangeNotifier failed to start: {ex.Message}"); + } + } + + private static void ScheduleRaise() + { + lock (Sync) + { + _debounceTimer?.Dispose(); + _debounceTimer = new Timer( + _ => SettingsChanged?.Invoke(), + null, + 200, + Timeout.Infinite); + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Microsoft.CmdPal.Ext.PowerToys.csproj b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Microsoft.CmdPal.Ext.PowerToys.csproj new file mode 100644 index 0000000000..36cfeb93f4 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Microsoft.CmdPal.Ext.PowerToys.csproj @@ -0,0 +1,72 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <Import Project="..\..\..\..\Common.Dotnet.CsWinRT.props" /> + <Import Project="..\..\..\..\Common.Dotnet.AotCompatibility.props" /> + <Import Project="..\..\..\..\CmdPalVersion.props" /> + + <PropertyGroup> + <OutputType>WinExe</OutputType> + <RootNamespace>PowerToysExtension</RootNamespace> + <ApplicationManifest>app.manifest</ApplicationManifest> + <PublishProfile>win-$(Platform).pubxml</PublishProfile> + <EnableMsixTooling>false</EnableMsixTooling> + <WindowsPackageType>None</WindowsPackageType> + <GenerateAppxPackageOnBuild>false</GenerateAppxPackageOnBuild> + <OutputPath>$(SolutionDir)$(Platform)\$(Configuration)\</OutputPath> + <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> + <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> + <Nullable>enable</Nullable> + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> + <WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained> + <Version>$(CmdPalVersion)</Version> + </PropertyGroup> + + <PropertyGroup> + <RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' and '$(Platform)' == 'ARM64'">win-arm64</RuntimeIdentifier> + <RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' and '$(Platform)' == 'x64'">win-x64</RuntimeIdentifier> + </PropertyGroup> + + <ItemGroup> + <Content Include="..\..\..\..\settings-ui\Settings.UI\Assets\Settings\Icons\*.png" Link="WinUI3Apps\Assets\Settings\Icons\%(Filename)%(Extension)"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </Content> + </ItemGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.WindowsAppSDK" /> + <PackageReference Include="Microsoft.CommandPalette.Extensions" /> + <PackageReference Include="Microsoft.Windows.CsWin32"> + <PrivateAssets>all</PrivateAssets> + <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> + </PackageReference> + <PackageReference Include="Shmuelie.WinRTServer" /> + </ItemGroup> + + <!-- Enable Single-project MSIX packaging support --> + <ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'"> + <ProjectCapability Include="Msix" /> + </ItemGroup> + + <PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'"> + <HasPackageAndPublishMenu>true</HasPackageAndPublishMenu> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\..\..\..\common\ManagedCommon\ManagedCommon.csproj" /> + <ProjectReference Include="..\..\..\..\common\Common.Search\Common.Search.csproj" /> + <ProjectReference Include="..\..\..\..\common\Common.UI\Common.UI.csproj" /> + <ProjectReference Include="..\..\..\..\common\interop\PowerToys.Interop.vcxproj" /> + <ProjectReference Include="..\..\..\Awake\Awake.ModuleServices\Awake.ModuleServices.csproj" /> + <ProjectReference Include="..\..\..\colorPicker\ColorPicker.ModuleServices\ColorPicker.ModuleServices.csproj" /> + <ProjectReference Include="..\..\..\fancyzones\FancyZonesEditorCommon\FancyZonesEditorCommon.csproj" /> + <ProjectReference Include="..\..\..\Workspaces\Workspaces.ModuleServices\Workspaces.ModuleServices.csproj" /> + </ItemGroup> + + <PropertyGroup> + <!-- Always build/publish AOT so the extension ships as native code --> + <SelfContained>true</SelfContained> + <PublishAot>true</PublishAot> + <PublishSingleFile>false</PublishSingleFile> + <PublishTrimmed>true</PublishTrimmed> + <DisableRuntimeMarshalling>false</DisableRuntimeMarshalling> + </PropertyGroup> +</Project> diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/AdvancedPasteModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/AdvancedPasteModuleCommandProvider.cs new file mode 100644 index 0000000000..58083919c0 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/AdvancedPasteModuleCommandProvider.cs @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Common.UI; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Modules; + +internal sealed class AdvancedPasteModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsDeepLink.SettingsWindow.AdvancedPaste; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + yield return new ListItem(new OpenAdvancedPasteCommand()) + { + Title = "Open Advanced Paste", + Subtitle = "Launch the Advanced Paste UI", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Advanced Paste settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/AlwaysOnTopModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/AlwaysOnTopModuleCommandProvider.cs new file mode 100644 index 0000000000..cad8a282da --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/AlwaysOnTopModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class AlwaysOnTopModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.AlwaysOnTop.ModuleDisplayName(); + var icon = SettingsWindow.AlwaysOnTop.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.AlwaysOnTop, title)) + { + Title = title, + Subtitle = "Open Always On Top settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/AwakeModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/AwakeModuleCommandProvider.cs new file mode 100644 index 0000000000..935371fba4 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/AwakeModuleCommandProvider.cs @@ -0,0 +1,91 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using Awake.ModuleServices; +using Common.UI; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using PowerToysExtension.Pages; + +namespace PowerToysExtension.Modules; + +internal sealed class AwakeModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var items = new List<ListItem>(); + var module = SettingsDeepLink.SettingsWindow.Awake; + var title = module.ModuleDisplayName(); + var icon = PowerToysResourcesHelper.IconFromSettingsIcon("Awake.png"); + var moduleIcon = module.ModuleIcon(); + + items.Add(new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Awake settings", + Icon = moduleIcon, + }); + + if (!ModuleEnablementService.IsModuleEnabled(module)) + { + return items; + } + + // Direct commands surfaced in the PowerToys list page. + ListItem? statusItem = null; + Action refreshStatus = () => + { + if (statusItem is not null) + { + statusItem.Subtitle = AwakeStatusService.GetStatusSubtitle(); + } + }; + + var refreshCommand = new RefreshAwakeStatusCommand(refreshStatus); + + statusItem = new ListItem(new CommandItem(refreshCommand)) + { + Title = "Awake: Current status", + Subtitle = AwakeStatusService.GetStatusSubtitle(), + Icon = icon, + }; + items.Add(statusItem); + + items.Add(new ListItem(new StartAwakeCommand("Awake: Keep awake indefinitely", () => AwakeService.Instance.SetIndefiniteAsync(), "Awake set to indefinite", refreshStatus)) + { + Title = "Awake: Keep awake indefinitely", + Subtitle = "Run Awake in indefinite mode", + Icon = icon, + }); + items.Add(new ListItem(new StartAwakeCommand("Awake: Keep awake for 30 minutes", () => AwakeService.Instance.SetTimedAsync(30), "Awake set for 30 minutes", refreshStatus)) + { + Title = "Awake: Keep awake for 30 minutes", + Subtitle = "Run Awake timed for 30 minutes", + Icon = icon, + }); + items.Add(new ListItem(new StartAwakeCommand("Awake: Keep awake for 1 hour", () => AwakeService.Instance.SetTimedAsync(60), "Awake set for 1 hour", refreshStatus)) + { + Title = "Awake: Keep awake for 1 hour", + Subtitle = "Run Awake timed for 1 hour", + Icon = icon, + }); + items.Add(new ListItem(new StartAwakeCommand("Awake: Keep awake for 2 hours", () => AwakeService.Instance.SetTimedAsync(120), "Awake set for 2 hours", refreshStatus)) + { + Title = "Awake: Keep awake for 2 hours", + Subtitle = "Run Awake timed for 2 hours", + Icon = icon, + }); + items.Add(new ListItem(new StopAwakeCommand(refreshStatus)) + { + Title = "Awake: Turn off", + Subtitle = "Switch Awake back to Off", + Icon = icon, + }); + + return items; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ColorPickerModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ColorPickerModuleCommandProvider.cs new file mode 100644 index 0000000000..27b3be6f05 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ColorPickerModuleCommandProvider.cs @@ -0,0 +1,53 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Common.UI; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using PowerToysExtension.Pages; + +namespace PowerToysExtension.Modules; + +internal sealed class ColorPickerModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsDeepLink.SettingsWindow.ColorPicker; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + var commands = new List<ListItem>(); + + commands.Add(new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Color Picker settings", + Icon = icon, + }); + + if (!ModuleEnablementService.IsModuleEnabled(module)) + { + return commands; + } + + // Direct entries in the module list. + commands.Add(new ListItem(new OpenColorPickerCommand()) + { + Title = "Open Color Picker", + Subtitle = "Start a color pick session", + Icon = icon, + }); + + commands.Add(new ListItem(new CommandItem(new ColorPickerSavedColorsPage())) + { + Title = "Saved colors", + Subtitle = "Browse and copy saved colors", + Icon = icon, + }); + + return commands; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/CommandNotFoundModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/CommandNotFoundModuleCommandProvider.cs new file mode 100644 index 0000000000..2ec95172f9 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/CommandNotFoundModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class CommandNotFoundModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.CmdNotFound.ModuleDisplayName(); + var icon = SettingsWindow.CmdNotFound.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.CmdNotFound, title)) + { + Title = title, + Subtitle = "Open Command Not Found settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/CropAndLockModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/CropAndLockModuleCommandProvider.cs new file mode 100644 index 0000000000..c3f6d1ccd4 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/CropAndLockModuleCommandProvider.cs @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class CropAndLockModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.CropAndLock; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + yield return new ListItem(new CropAndLockReparentCommand()) + { + Title = "Crop and Lock (Reparent)", + Subtitle = "Create a cropped reparented window", + Icon = icon, + }; + + yield return new ListItem(new CropAndLockThumbnailCommand()) + { + Title = "Crop and Lock (Thumbnail)", + Subtitle = "Create a cropped thumbnail window", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Crop and Lock settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/EnvironmentVariablesModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/EnvironmentVariablesModuleCommandProvider.cs new file mode 100644 index 0000000000..d72644c1bf --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/EnvironmentVariablesModuleCommandProvider.cs @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class EnvironmentVariablesModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.EnvironmentVariables; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + yield return new ListItem(new OpenEnvironmentVariablesCommand()) + { + Title = "Open Environment Variables", + Subtitle = "Launch Environment Variables editor", + Icon = icon, + }; + + yield return new ListItem(new OpenEnvironmentVariablesAdminCommand()) + { + Title = "Open Environment Variables (Admin)", + Subtitle = "Launch Environment Variables editor as admin", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Environment Variables settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/FancyZonesModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/FancyZonesModuleCommandProvider.cs new file mode 100644 index 0000000000..6a4287d60f --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/FancyZonesModuleCommandProvider.cs @@ -0,0 +1,53 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using PowerToysExtension.Pages; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class FancyZonesModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.FancyZones; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + yield return new ListItem(new CommandItem(new FancyZonesLayoutsPage())) + { + Title = "FancyZones: Layouts", + Subtitle = "Apply a layout to all monitors or a specific monitor", + Icon = icon, + }; + + yield return new ListItem(new CommandItem(new FancyZonesMonitorsPage())) + { + Title = "FancyZones: Monitors", + Subtitle = "Identify monitors and apply layouts", + Icon = icon, + }; + + yield return new ListItem(new OpenFancyZonesEditorCommand()) + { + Title = "Open FancyZones Editor", + Subtitle = "Launch layout editor", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open FancyZones settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/FileExplorerAddonsModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/FileExplorerAddonsModuleCommandProvider.cs new file mode 100644 index 0000000000..5fa5162cf8 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/FileExplorerAddonsModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class FileExplorerAddonsModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.FileExplorer.ModuleDisplayName(); + var icon = SettingsWindow.FileExplorer.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.FileExplorer, title)) + { + Title = title, + Subtitle = "Open File Explorer add-ons settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/FileLocksmithModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/FileLocksmithModuleCommandProvider.cs new file mode 100644 index 0000000000..19e5a135e5 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/FileLocksmithModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class FileLocksmithModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.FileLocksmith.ModuleDisplayName(); + var icon = SettingsWindow.FileLocksmith.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.FileLocksmith, title)) + { + Title = title, + Subtitle = "Open File Locksmith settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/HostsModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/HostsModuleCommandProvider.cs new file mode 100644 index 0000000000..839598b428 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/HostsModuleCommandProvider.cs @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class HostsModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.Hosts; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + yield return new ListItem(new OpenHostsEditorCommand()) + { + Title = "Open Hosts File Editor", + Subtitle = "Launch Hosts File Editor", + Icon = icon, + }; + + yield return new ListItem(new OpenHostsEditorAdminCommand()) + { + Title = "Open Hosts File Editor (Admin)", + Subtitle = "Launch Hosts File Editor as admin", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Hosts File Editor settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ImageResizerModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ImageResizerModuleCommandProvider.cs new file mode 100644 index 0000000000..6ec2f1b3a6 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ImageResizerModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class ImageResizerModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.ImageResizer.ModuleDisplayName(); + var icon = SettingsWindow.ImageResizer.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.ImageResizer, title)) + { + Title = title, + Subtitle = "Open Image Resizer settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/KeyboardManagerModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/KeyboardManagerModuleCommandProvider.cs new file mode 100644 index 0000000000..bb42f484a7 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/KeyboardManagerModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class KeyboardManagerModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.KBM.ModuleDisplayName(); + var icon = SettingsWindow.KBM.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.KBM, title)) + { + Title = title, + Subtitle = "Open Keyboard Manager settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/LightSwitchModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/LightSwitchModuleCommandProvider.cs new file mode 100644 index 0000000000..a07c06fedb --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/LightSwitchModuleCommandProvider.cs @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class LightSwitchModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.LightSwitch; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + var items = new List<ListItem>(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + items.Add(new ListItem(new ToggleLightSwitchCommand()) + { + Title = "Toggle Light Switch", + Subtitle = "Toggle system/apps theme immediately", + Icon = icon, + }); + } + + items.Add(new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Light Switch settings", + Icon = icon, + }); + + return items; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ModuleCommandProvider.cs new file mode 100644 index 0000000000..4e06731a2d --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ModuleCommandProvider.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; + +namespace PowerToysExtension.Modules; + +/// <summary> +/// Base contract for a PowerToys module to expose its command palette entries. +/// </summary> +internal abstract class ModuleCommandProvider +{ + public abstract IEnumerable<ListItem> BuildCommands(); +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/MouseUtilsModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/MouseUtilsModuleCommandProvider.cs new file mode 100644 index 0000000000..34c6be193e --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/MouseUtilsModuleCommandProvider.cs @@ -0,0 +1,78 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class MouseUtilsModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.MouseUtils; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsKeyEnabled("FindMyMouse")) + { + yield return new ListItem(new ToggleFindMyMouseCommand()) + { + Title = "Trigger Find My Mouse", + Subtitle = "Focus the mouse pointer", + Icon = icon, + }; + } + + if (ModuleEnablementService.IsKeyEnabled("MouseHighlighter")) + { + yield return new ListItem(new ToggleMouseHighlighterCommand()) + { + Title = "Toggle Mouse Highlighter", + Subtitle = "Highlight mouse clicks", + Icon = icon, + }; + } + + if (ModuleEnablementService.IsKeyEnabled("MousePointerCrosshairs")) + { + yield return new ListItem(new ToggleMouseCrosshairsCommand()) + { + Title = "Toggle Mouse Crosshairs", + Subtitle = "Enable or disable pointer crosshairs", + Icon = icon, + }; + } + + if (ModuleEnablementService.IsKeyEnabled("CursorWrap")) + { + yield return new ListItem(new ToggleCursorWrapCommand()) + { + Title = "Toggle Cursor Wrap", + Subtitle = "Wrap the cursor across monitor edges", + Icon = icon, + }; + } + + if (ModuleEnablementService.IsKeyEnabled("MouseJump")) + { + yield return new ListItem(new ShowMouseJumpPreviewCommand()) + { + Title = "Show Mouse Jump Preview", + Subtitle = "Jump the pointer to a target", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Mouse Utilities settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/MouseWithoutBordersModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/MouseWithoutBordersModuleCommandProvider.cs new file mode 100644 index 0000000000..49a3f3635a --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/MouseWithoutBordersModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class MouseWithoutBordersModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.MouseWithoutBorders.ModuleDisplayName(); + var icon = SettingsWindow.MouseWithoutBorders.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.MouseWithoutBorders, title)) + { + Title = title, + Subtitle = "Open Mouse Without Borders settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/NewPlusModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/NewPlusModuleCommandProvider.cs new file mode 100644 index 0000000000..f88d104b73 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/NewPlusModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class NewPlusModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.NewPlus.ModuleDisplayName(); + var icon = SettingsWindow.NewPlus.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.NewPlus, title)) + { + Title = title, + Subtitle = "Open New+ settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/PeekModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/PeekModuleCommandProvider.cs new file mode 100644 index 0000000000..a55a187206 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/PeekModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class PeekModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.Peek.ModuleDisplayName(); + var icon = SettingsWindow.Peek.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.Peek, title)) + { + Title = title, + Subtitle = "Open Peek settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/PowerRenameModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/PowerRenameModuleCommandProvider.cs new file mode 100644 index 0000000000..434a1d53cf --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/PowerRenameModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class PowerRenameModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.PowerRename.ModuleDisplayName(); + var icon = SettingsWindow.PowerRename.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.PowerRename, title)) + { + Title = title, + Subtitle = "Open PowerRename settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/PowerToysRunModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/PowerToysRunModuleCommandProvider.cs new file mode 100644 index 0000000000..593bebb3a9 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/PowerToysRunModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class PowerToysRunModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.PowerLauncher.ModuleDisplayName(); + var icon = SettingsWindow.PowerLauncher.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.PowerLauncher, title)) + { + Title = title, + Subtitle = "Open PowerToys Run settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/QuickAccentModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/QuickAccentModuleCommandProvider.cs new file mode 100644 index 0000000000..9122b3534c --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/QuickAccentModuleCommandProvider.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class QuickAccentModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var title = SettingsWindow.PowerAccent.ModuleDisplayName(); + var icon = SettingsWindow.PowerAccent.ModuleIcon(); + + yield return new ListItem(new OpenInSettingsCommand(SettingsWindow.PowerAccent, title)) + { + Title = title, + Subtitle = "Open Quick Accent settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/RegistryPreviewModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/RegistryPreviewModuleCommandProvider.cs new file mode 100644 index 0000000000..7dbe3f841b --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/RegistryPreviewModuleCommandProvider.cs @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class RegistryPreviewModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.RegistryPreview; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + yield return new ListItem(new OpenRegistryPreviewCommand()) + { + Title = "Open Registry Preview", + Subtitle = "Launch Registry Preview", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Registry Preview settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ScreenRulerModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ScreenRulerModuleCommandProvider.cs new file mode 100644 index 0000000000..23674c3dfe --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ScreenRulerModuleCommandProvider.cs @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class ScreenRulerModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.MeasureTool; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + yield return new ListItem(new ToggleScreenRulerCommand()) + { + Title = "Toggle Screen Ruler", + Subtitle = "Start or close Screen Ruler", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Screen Ruler settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ShortcutGuideModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ShortcutGuideModuleCommandProvider.cs new file mode 100644 index 0000000000..20f487c1f3 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ShortcutGuideModuleCommandProvider.cs @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class ShortcutGuideModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.ShortcutGuide; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + yield return new ListItem(new ToggleShortcutGuideCommand()) + { + Title = "Toggle Shortcut Guide", + Subtitle = "Show or hide Shortcut Guide", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Shortcut Guide settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/TextExtractorModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/TextExtractorModuleCommandProvider.cs new file mode 100644 index 0000000000..a8e816ccc6 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/TextExtractorModuleCommandProvider.cs @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class TextExtractorModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.PowerOCR; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + yield return new ListItem(new ToggleTextExtractorCommand()) + { + Title = "Toggle Text Extractor", + Subtitle = "Start or close Text Extractor", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Text Extractor settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/WorkspacesModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/WorkspacesModuleCommandProvider.cs new file mode 100644 index 0000000000..49d585ba6d --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/WorkspacesModuleCommandProvider.cs @@ -0,0 +1,64 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Common.UI; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using Workspaces.ModuleServices; +using WorkspacesCsharpLibrary.Data; + +namespace PowerToysExtension.Modules; + +internal sealed class WorkspacesModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var items = new List<ListItem>(); + var module = SettingsDeepLink.SettingsWindow.Workspaces; + var title = module.ModuleDisplayName(); + var icon = PowerToysResourcesHelper.IconFromSettingsIcon("Workspaces.png"); + var moduleIcon = module.ModuleIcon(); + + items.Add(new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open Workspaces settings", + Icon = moduleIcon, + }); + + if (!ModuleEnablementService.IsModuleEnabled(module)) + { + return items; + } + + // Settings entry plus common actions. + items.Add(new ListItem(new OpenWorkspaceEditorCommand()) + { + Title = "Workspaces: Open editor", + Subtitle = "Create or edit workspaces", + Icon = icon, + }); + + // Per-workspace entries via the shared service. + foreach (var workspace in LoadWorkspaces()) + { + if (string.IsNullOrWhiteSpace(workspace.Id) || string.IsNullOrWhiteSpace(workspace.Name)) + { + continue; + } + + items.Add(new WorkspaceListItem(workspace, icon)); + } + + return items; + } + + private static IReadOnlyList<ProjectWrapper> LoadWorkspaces() + { + var result = WorkspaceService.Instance.GetWorkspacesAsync().GetAwaiter().GetResult(); + return result.Success && result.Value is not null ? result.Value : System.Array.Empty<ProjectWrapper>(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ZoomItModuleCommandProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ZoomItModuleCommandProvider.cs new file mode 100644 index 0000000000..a73ccdfbe3 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Modules/ZoomItModuleCommandProvider.cs @@ -0,0 +1,69 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; +using static Common.UI.SettingsDeepLink; + +namespace PowerToysExtension.Modules; + +internal sealed class ZoomItModuleCommandProvider : ModuleCommandProvider +{ + public override IEnumerable<ListItem> BuildCommands() + { + var module = SettingsWindow.ZoomIt; + var title = module.ModuleDisplayName(); + var icon = module.ModuleIcon(); + + if (ModuleEnablementService.IsModuleEnabled(module)) + { + // Action commands via ZoomIt IPC + yield return new ListItem(new ZoomItActionCommand("zoom", "ZoomIt: Zoom")) + { + Title = "ZoomIt: Zoom", + Subtitle = "Enter zoom mode", + Icon = icon, + }; + yield return new ListItem(new ZoomItActionCommand("draw", "ZoomIt: Draw")) + { + Title = "ZoomIt: Draw", + Subtitle = "Enter drawing mode", + Icon = icon, + }; + yield return new ListItem(new ZoomItActionCommand("break", "ZoomIt: Break")) + { + Title = "ZoomIt: Break", + Subtitle = "Enter break timer", + Icon = icon, + }; + yield return new ListItem(new ZoomItActionCommand("liveZoom", "ZoomIt: Live Zoom")) + { + Title = "ZoomIt: Live Zoom", + Subtitle = "Toggle live zoom", + Icon = icon, + }; + yield return new ListItem(new ZoomItActionCommand("snip", "ZoomIt: Snip")) + { + Title = "ZoomIt: Snip", + Subtitle = "Enter snip mode", + Icon = icon, + }; + yield return new ListItem(new ZoomItActionCommand("record", "ZoomIt: Record")) + { + Title = "ZoomIt: Record", + Subtitle = "Start recording", + Icon = icon, + }; + } + + yield return new ListItem(new OpenInSettingsCommand(module, title)) + { + Title = title, + Subtitle = "Open ZoomIt settings", + Icon = icon, + }; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/ColorPickerSavedColorsPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/ColorPickerSavedColorsPage.cs new file mode 100644 index 0000000000..5e06951794 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/ColorPickerSavedColorsPage.cs @@ -0,0 +1,96 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Linq; +using System.Text; +using ColorPicker.ModuleServices; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Pages; + +internal sealed partial class ColorPickerSavedColorsPage : DynamicListPage +{ + private readonly CommandItem _emptyContent; + + public ColorPickerSavedColorsPage() + { + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("ColorPicker.png"); + Title = "Saved colors"; + Name = "ColorPickerSavedColors"; + Id = "com.microsoft.powertoys.colorpicker.savedColors"; + + _emptyContent = new CommandItem() + { + Title = "No saved colors", + Subtitle = "Pick a color first, then try again.", + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("ColorPicker.png"), + }; + + EmptyContent = _emptyContent; + } + + public override IListItem[] GetItems() + { + var result = ColorPickerService.Instance.GetSavedColorsAsync().GetAwaiter().GetResult(); + if (!result.Success || result.Value is null || result.Value.Count == 0) + { + return Array.Empty<IListItem>(); + } + + var search = SearchText; + var filtered = string.IsNullOrWhiteSpace(search) + ? result.Value + : result.Value.Where(saved => + saved.Hex.Contains(search, StringComparison.OrdinalIgnoreCase) || + saved.Formats.Any(f => f.Value.Contains(search, StringComparison.OrdinalIgnoreCase) || + f.Format.Contains(search, StringComparison.OrdinalIgnoreCase))); + + var items = filtered.Select(saved => + { + var copyValue = SelectPreferredFormat(saved); + var subtitle = BuildSubtitle(saved); + + var command = new CopySavedColorCommand(saved, copyValue); + return (IListItem)new ListItem(new CommandItem(command)) + { + Title = saved.Hex, + Subtitle = subtitle, + Icon = ColorSwatchIconFactory.Create(saved.R, saved.G, saved.B, saved.A), + }; + }).ToArray(); + + return items; + } + + public override void UpdateSearchText(string oldSearch, string newSearch) + { + _emptyContent.Subtitle = string.IsNullOrWhiteSpace(newSearch) + ? "Pick a color first, then try again." + : $"No saved colors matching '{newSearch}'"; + + RaiseItemsChanged(0); + } + + private static string SelectPreferredFormat(SavedColor saved) => saved.Hex; + + private static string BuildSubtitle(SavedColor saved) + { + var sb = new StringBuilder(); + foreach (var format in saved.Formats.Take(3)) + { + if (sb.Length > 0) + { + sb.Append(" · "); + } + + sb.Append(format.Value); + } + + return sb.Length > 0 ? sb.ToString() : saved.Hex; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/FancyZonesLayoutsPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/FancyZonesLayoutsPage.cs new file mode 100644 index 0000000000..1b568da7d6 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/FancyZonesLayoutsPage.cs @@ -0,0 +1,103 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Pages; + +internal sealed partial class FancyZonesLayoutsPage : DynamicListPage +{ + private readonly CommandItem _emptyMessage; + + public FancyZonesLayoutsPage() + { + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("FancyZones.png"); + Name = Title = "FancyZones Layouts"; + Id = "com.microsoft.cmdpal.powertoys.fancyzones.layouts"; + + _emptyMessage = new CommandItem() + { + Title = "No layouts found", + Subtitle = "Open FancyZones Editor once to initialize layouts.", + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("FancyZones.png"), + }; + EmptyContent = _emptyMessage; + + // Purge orphaned cache files in background (non-blocking) + Task.Run(FancyZonesThumbnailRenderer.PurgeOrphanedCache); + } + + public override void UpdateSearchText(string oldSearch, string newSearch) + { + RaiseItemsChanged(0); + } + + public override IListItem[] GetItems() + { + try + { + var layouts = FancyZonesDataService.GetLayouts(); + if (!string.IsNullOrWhiteSpace(SearchText)) + { + layouts = layouts + .Where(l => l.Title.Contains(SearchText, StringComparison.CurrentCultureIgnoreCase) || + l.Subtitle.Contains(SearchText, StringComparison.CurrentCultureIgnoreCase)) + .ToArray(); + } + + if (layouts.Count == 0) + { + return Array.Empty<IListItem>(); + } + + _ = FancyZonesDataService.TryGetMonitors(out var monitors, out _); + var fallbackIcon = PowerToysResourcesHelper.IconFromSettingsIcon("FancyZones.png"); + + var items = new List<IListItem>(layouts.Count); + foreach (var layout in layouts) + { + var defaultCommand = new ApplyFancyZonesLayoutCommand(layout, monitor: null); + + var item = new FancyZonesLayoutListItem(defaultCommand, layout, fallbackIcon) + { + MoreCommands = BuildLayoutContext(layout, monitors), + }; + + items.Add(item); + } + + return items.ToArray(); + } + catch (Exception ex) + { + _emptyMessage.Subtitle = ex.Message; + return Array.Empty<IListItem>(); + } + } + + private static IContextItem[] BuildLayoutContext(FancyZonesLayoutDescriptor layout, IReadOnlyList<FancyZonesMonitorDescriptor> monitors) + { + var commands = new List<IContextItem>(monitors.Count); + + for (var i = 0; i < monitors.Count; i++) + { + var monitor = monitors[i]; + commands.Add(new CommandContextItem(new ApplyFancyZonesLayoutCommand(layout, monitor)) + { + Title = string.Format(CultureInfo.CurrentCulture, "Apply to {0}", monitor.Title), + Subtitle = monitor.Subtitle, + }); + } + + return commands.ToArray(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/FancyZonesMonitorLayoutPickerPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/FancyZonesMonitorLayoutPickerPage.cs new file mode 100644 index 0000000000..0269f84ed0 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/FancyZonesMonitorLayoutPickerPage.cs @@ -0,0 +1,68 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Pages; + +internal sealed partial class FancyZonesMonitorLayoutPickerPage : DynamicListPage +{ + private readonly FancyZonesMonitorDescriptor _monitor; + private readonly CommandItem _emptyMessage; + + public FancyZonesMonitorLayoutPickerPage(FancyZonesMonitorDescriptor monitor) + { + _monitor = monitor; + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("FancyZones.png"); + Name = Title = $"Set active layout for {_monitor.Title}"; + Id = $"com.microsoft.cmdpal.powertoys.fancyzones.monitor.{_monitor.Index}.layouts"; + + _emptyMessage = new CommandItem() + { + Title = "No layouts found", + Subtitle = "Open FancyZones Editor once to initialize layouts.", + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("FancyZones.png"), + }; + EmptyContent = _emptyMessage; + } + + public override void UpdateSearchText(string oldSearch, string newSearch) + { + RaiseItemsChanged(0); + } + + public override IListItem[] GetItems() + { + var layouts = FancyZonesDataService.GetLayouts(); + if (!string.IsNullOrWhiteSpace(SearchText)) + { + layouts = layouts + .Where(l => l.Title.Contains(SearchText, StringComparison.CurrentCultureIgnoreCase) || + l.Subtitle.Contains(SearchText, StringComparison.CurrentCultureIgnoreCase)) + .ToArray(); + } + + if (layouts.Count == 0) + { + return Array.Empty<IListItem>(); + } + + var fallbackIcon = PowerToysResourcesHelper.IconFromSettingsIcon("FancyZones.png"); + var items = new List<IListItem>(layouts.Count); + foreach (var layout in layouts) + { + var command = new ApplyFancyZonesLayoutCommand(layout, _monitor); + var item = new FancyZonesLayoutListItem(command, layout, fallbackIcon); + items.Add(item); + } + + return [.. items]; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/FancyZonesMonitorsPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/FancyZonesMonitorsPage.cs new file mode 100644 index 0000000000..8422038d3d --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/FancyZonesMonitorsPage.cs @@ -0,0 +1,67 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Pages; + +internal sealed partial class FancyZonesMonitorsPage : DynamicListPage +{ + private readonly CommandItem _emptyMessage; + + public FancyZonesMonitorsPage() + { + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("FancyZones.png"); + Name = Title = "FancyZones Monitors"; + Id = "com.microsoft.cmdpal.powertoys.fancyzones.monitors"; + + _emptyMessage = new CommandItem() + { + Title = "No monitors found", + Subtitle = "Open FancyZones Editor once to initialize monitor data.", + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("FancyZones.png"), + }; + EmptyContent = _emptyMessage; + } + + public override void UpdateSearchText(string oldSearch, string newSearch) + { + RaiseItemsChanged(0); + } + + public override IListItem[] GetItems() + { + if (!FancyZonesDataService.TryGetMonitors(out var monitors, out var error)) + { + _emptyMessage.Subtitle = error; + return Array.Empty<IListItem>(); + } + + var monitorIcon = new IconInfo("\uE7F4"); + var items = new List<IListItem>(monitors.Count); + + foreach (var monitor in monitors) + { + if (!string.IsNullOrWhiteSpace(SearchText) && + !monitor.Title.Contains(SearchText, StringComparison.CurrentCultureIgnoreCase) && + !monitor.Subtitle.Contains(SearchText, StringComparison.CurrentCultureIgnoreCase)) + { + continue; + } + + var layoutDescription = FancyZonesDataService.TryGetAppliedLayoutForMonitor(monitor.Data, out var applied) && applied is not null + ? $"Current layout: {applied.Value.Type}" + : "Current layout: unknown"; + + var item = new FancyZonesMonitorListItem(monitor, layoutDescription, monitorIcon); + items.Add(item); + } + + return [.. items]; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/PowerToysExtensionPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/PowerToysExtensionPage.cs new file mode 100644 index 0000000000..0d81573280 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/PowerToysExtensionPage.cs @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Awake.ModuleServices; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Commands; + +namespace PowerToysExtension; + +internal sealed partial class PowerToysExtensionPage : ListPage +{ + public PowerToysExtensionPage() + { + Icon = Helpers.PowerToysResourcesHelper.IconFromSettingsIcon("PowerToys.png"); + Title = "PowerToys"; + Name = "PowerToys commands"; + } + + public override IListItem[] GetItems() + { + return [ + new ListItem(new LaunchModuleCommand("PowerToys", executableName: "PowerToys.exe", displayName: "Open PowerToys")) + { + Title = "Open PowerToys", + Subtitle = "Launch the PowerToys shell", + }, + new ListItem(new OpenPowerToysSettingsCommand("PowerToys", "General")) + { + Title = "Open PowerToys settings", + Subtitle = "Open the main PowerToys settings window", + }, + new ListItem(new OpenPowerToysSettingsCommand("Workspaces", "Workspaces")) + { + Title = "Open Workspaces settings", + Subtitle = "Jump directly to Workspaces settings", + }, + new ListItem(new OpenWorkspaceEditorCommand()) + { + Title = "Open Workspaces editor", + Subtitle = "Launch the Workspaces editor", + }, + ]; + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/PowerToysListPage.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/PowerToysListPage.cs new file mode 100644 index 0000000000..c7eed2594f --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Pages/PowerToysListPage.cs @@ -0,0 +1,36 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension.Pages; + +internal sealed partial class PowerToysListPage : ListPage +{ + private readonly CommandItem _empty; + + public PowerToysListPage() + { + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("PowerToys.png"); + Name = Title = "PowerToys"; + Id = "com.microsoft.cmdpal.powertoys"; + SettingsChangeNotifier.SettingsChanged += OnSettingsChanged; + _empty = new CommandItem() + { + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("PowerToys.png"), + Title = "No matching module found", + Subtitle = SearchText, + }; + EmptyContent = _empty; + } + + private void OnSettingsChanged() + { + RaiseItemsChanged(0); + } + + public override IListItem[] GetItems() => ModuleCommandCatalog.GetAllItems(); +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/PowerToysCommandsProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/PowerToysCommandsProvider.cs new file mode 100644 index 0000000000..d4dde03b46 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/PowerToysCommandsProvider.cs @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using ManagedCommon; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension; + +public sealed partial class PowerToysCommandsProvider : CommandProvider +{ + public PowerToysCommandsProvider() + { + DisplayName = "PowerToys"; + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("PowerToys.png"); + } + + public override ICommandItem[] TopLevelCommands() => + [ + new CommandItem(new Pages.PowerToysListPage()) + { + Title = "PowerToys", + Subtitle = "PowerToys commands and settings", + } + ]; + + public override IFallbackCommandItem[] FallbackCommands() + { + var items = ModuleCommandCatalog.GetAllItems(); + var fallbacks = new List<IFallbackCommandItem>(items.Length); + foreach (var item in items) + { + if (item?.Command is not ICommand cmd) + { + continue; + } + + fallbacks.Add(new PowerToysFallbackCommandItem(cmd, item.Title, item.Subtitle, item.Icon, item.MoreCommands)); + } + + return fallbacks.ToArray(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/PowerToysExtension.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/PowerToysExtension.cs new file mode 100644 index 0000000000..f4100db51a --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/PowerToysExtension.cs @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Threading; +using ManagedCommon; +using Microsoft.CommandPalette.Extensions; + +namespace PowerToysExtension; + +[Guid("7EC02C7D-8F98-4A2E-9F23-B58C2C2F2B17")] +public sealed partial class PowerToysExtension : IExtension, IDisposable +{ + private readonly ManualResetEvent _extensionDisposedEvent; + + private readonly PowerToysExtensionCommandsProvider _provider = new(); + + public PowerToysExtension(ManualResetEvent extensionDisposedEvent) + { + this._extensionDisposedEvent = extensionDisposedEvent; + Logger.LogInfo($"PowerToysExtension constructed. ProcArch={RuntimeInformation.ProcessArchitecture} OSArch={RuntimeInformation.OSArchitecture} BaseDir={AppContext.BaseDirectory}"); + } + + public object? GetProvider(ProviderType providerType) + { + Logger.LogInfo($"GetProvider requested: {providerType}"); + return providerType switch + { + ProviderType.Commands => _provider, + _ => null, + }; + } + + public void Dispose() + { + Logger.LogInfo("PowerToysExtension disposing; signalling exit."); + this._extensionDisposedEvent.Set(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/PowerToysExtensionCommandsProvider.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/PowerToysExtensionCommandsProvider.cs new file mode 100644 index 0000000000..beba6b484a --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/PowerToysExtensionCommandsProvider.cs @@ -0,0 +1,50 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using PowerToysExtension.Helpers; + +namespace PowerToysExtension; + +public partial class PowerToysExtensionCommandsProvider : CommandProvider +{ + private readonly ICommandItem[] _commands; + + public PowerToysExtensionCommandsProvider() + { + DisplayName = "PowerToys"; + Icon = PowerToysResourcesHelper.IconFromSettingsIcon("PowerToys.png"); + _commands = [ + new CommandItem(new Pages.PowerToysListPage()) + { + Title = "PowerToys", + Subtitle = "PowerToys commands and settings", + }, + ]; + } + + public override ICommandItem[] TopLevelCommands() + { + return _commands; + } + + public override IFallbackCommandItem[] FallbackCommands() + { + var items = ModuleCommandCatalog.GetAllItems(); + var fallbacks = new List<IFallbackCommandItem>(items.Length); + foreach (var item in items) + { + if (item?.Command is not ICommand cmd) + { + continue; + } + + fallbacks.Add(new PowerToysFallbackCommandItem(cmd, item.Title, item.Subtitle, item.Icon, item.MoreCommands)); + } + + return fallbacks.ToArray(); + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Program.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Program.cs new file mode 100644 index 0000000000..2706f50f90 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Program.cs @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Threading; +using ManagedCommon; +using Microsoft.CommandPalette.Extensions; +using Shmuelie.WinRTServer; +using Shmuelie.WinRTServer.CsWinRT; + +namespace PowerToysExtension; + +public class Program +{ + [MTAThread] + public static void Main(string[] args) + { + try + { + // Initialize per-extension log under CmdPal/PowerToysExtension. + Logger.InitializeLogger("\\CmdPal\\PowerToysExtension\\Logs"); + Logger.LogInfo($"PowerToysExtension starting. Args=\"{string.Join(' ', args)}\" ProcArch={RuntimeInformation.ProcessArchitecture} OSArch={RuntimeInformation.OSArchitecture} BaseDir={AppContext.BaseDirectory}"); + } + catch + { + // Continue even if logging fails. + } + + try + { + if (args.Length > 0 && args[0] == "-RegisterProcessAsComServer") + { + Logger.LogInfo("RegisterProcessAsComServer mode detected."); + ComServer server = new(); + ManualResetEvent extensionDisposedEvent = new(false); + try + { + PowerToysExtension extensionInstance = new(extensionDisposedEvent); + Logger.LogInfo("Registering extension via Shmuelie.WinRTServer."); + server.RegisterClass<PowerToysExtension, IExtension>(() => extensionInstance); + server.Start(); + Logger.LogInfo("Extension instance registered; waiting for disposal signal."); + + extensionDisposedEvent.WaitOne(); + Logger.LogInfo("Extension disposed signal received; exiting server loop."); + } + finally + { + server.Stop(); + server.UnsafeDispose(); + } + } + else + { + Console.WriteLine("Not being launched as a Extension... exiting."); + Logger.LogInfo("Exited: not launched with -RegisterProcessAsComServer."); + } + } + catch (Exception ex) + { + Logger.LogError("Unhandled exception in PowerToysExtension.Main", ex); + throw; + } + finally + { + } + } +} diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Public/README.md b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Public/README.md new file mode 100644 index 0000000000..99bded1694 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/Public/README.md @@ -0,0 +1,6 @@ +# PowerToys Command Palette Extension + +This folder is exposed to the Windows Command Palette host via the +`PublicFolder` attribute in the AppExtension registration. It intentionally +contains only documentation today, but can be used for additional metadata in +the future without requiring code changes. diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/app.manifest b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/app.manifest new file mode 100644 index 0000000000..013aaee199 --- /dev/null +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.PowerToys/app.manifest @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> + <assemblyIdentity version="1.0.0.0" name="PowerToysExtension.app" /> + <msix xmlns="urn:schemas-microsoft-com:msix.v1" + packageName="Microsoft.PowerToys.SparseApp" + applicationId="PowerToys.CmdPalExtension" + publisher="CN=PowerToys Dev, O=PowerToys, L=Redmond, S=Washington, C=US" /> + + <asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"> + <asmv3:windowsSettings> + <ws2:dpiAwareness xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</ws2:dpiAwareness> + </asmv3:windowsSettings> + </asmv3:application> + + <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> + <application> + <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" /> + </application> + </compatibility> + +</assembly> diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.RemoteDesktop/Commands/FallbackRemoteDesktopItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.RemoteDesktop/Commands/FallbackRemoteDesktopItem.cs index e3692a763b..287a697c31 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.RemoteDesktop/Commands/FallbackRemoteDesktopItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.RemoteDesktop/Commands/FallbackRemoteDesktopItem.cs @@ -28,7 +28,7 @@ internal sealed partial class FallbackRemoteDesktopItem : FallbackCommandItem private readonly NoOpCommand _emptyCommand = new NoOpCommand(); public FallbackRemoteDesktopItem(IRdpConnectionsManager rdpConnectionsManager) - : base(Resources.remotedesktop_title) + : base(Resources.remotedesktop_title, _id) { _rdpConnectionsManager = rdpConnectionsManager; diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Shell/FallbackExecuteItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Shell/FallbackExecuteItem.cs index ab557ba258..46e86bfeed 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Shell/FallbackExecuteItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Shell/FallbackExecuteItem.cs @@ -11,6 +11,7 @@ namespace Microsoft.CmdPal.Ext.Shell; internal sealed partial class FallbackExecuteItem : FallbackCommandItem, IDisposable { + private const string _id = "com.microsoft.cmdpal.builtin.shell.fallback"; private static readonly char[] _systemDirectoryRoots = ['\\', '/']; private readonly Action<string>? _addToHistory; @@ -19,8 +20,9 @@ internal sealed partial class FallbackExecuteItem : FallbackCommandItem, IDispos public FallbackExecuteItem(SettingsManager settings, Action<string>? addToHistory, ITelemetryService telemetryService) : base( - new NoOpCommand() { Id = "com.microsoft.run.fallback" }, - ResourceLoaderInstance.GetString("shell_command_display_title")) + new NoOpCommand() { Id = _id }, + ResourceLoaderInstance.GetString("shell_command_display_title"), + _id) { Title = string.Empty; Subtitle = ResourceLoaderInstance.GetString("generic_run_command"); diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/FallbackSystemCommandItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/FallbackSystemCommandItem.cs index adaa9f7c26..8624953891 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/FallbackSystemCommandItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.System/FallbackSystemCommandItem.cs @@ -12,8 +12,10 @@ namespace Microsoft.CmdPal.Ext.System; internal sealed partial class FallbackSystemCommandItem : FallbackCommandItem { + private const string _id = "com.microsoft.cmdpal.builtin.system.fallback"; + public FallbackSystemCommandItem(ISettingsInterface settings) - : base(new NoOpCommand(), Resources.Microsoft_plugin_ext_fallback_display_title) + : base(new NoOpCommand(), Resources.Microsoft_plugin_ext_fallback_display_title, _id) { Title = string.Empty; Subtitle = string.Empty; diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/FallbackTimeDateItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/FallbackTimeDateItem.cs index d4b9994339..899163e621 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/FallbackTimeDateItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.TimeDate/FallbackTimeDateItem.cs @@ -13,12 +13,13 @@ namespace Microsoft.CmdPal.Ext.TimeDate; internal sealed partial class FallbackTimeDateItem : FallbackCommandItem { + private const string _id = "com.microsoft.cmdpal.builtin.timedate.fallback"; private readonly HashSet<string> _validOptions; private ISettingsInterface _settingsManager; private DateTime? _timestamp; public FallbackTimeDateItem(ISettingsInterface settings, DateTime? timestamp = null) - : base(new NoOpCommand(), Resources.Microsoft_plugin_timedate_fallback_display_title) + : base(new NoOpCommand(), Resources.Microsoft_plugin_timedate_fallback_display_title, _id) { Title = string.Empty; Subtitle = string.Empty; diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/FallbackExecuteSearchItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/FallbackExecuteSearchItem.cs index 61557d996a..8ce2349e3c 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/FallbackExecuteSearchItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/FallbackExecuteSearchItem.cs @@ -13,6 +13,7 @@ namespace Microsoft.CmdPal.Ext.WebSearch.Commands; internal sealed partial class FallbackExecuteSearchItem : FallbackCommandItem { + private const string _id = "com.microsoft.cmdpal.builtin.websearch.execute.fallback"; private readonly SearchWebCommand _executeItem; private static readonly CompositeFormat PluginOpen = System.Text.CompositeFormat.Parse(Properties.Resources.plugin_open); private static readonly CompositeFormat SubtitleText = System.Text.CompositeFormat.Parse(Properties.Resources.web_search_fallback_subtitle); @@ -20,7 +21,7 @@ internal sealed partial class FallbackExecuteSearchItem : FallbackCommandItem private readonly IBrowserInfoService _browserInfoService; public FallbackExecuteSearchItem(ISettingsInterface settings, IBrowserInfoService browserInfoService) - : base(new SearchWebCommand(string.Empty, settings, browserInfoService) { Id = "com.microsoft.websearch.fallback" }, Resources.command_item_title) + : base(new SearchWebCommand(string.Empty, settings, browserInfoService) { Id = _id }, Resources.command_item_title, _id) { _executeItem = (SearchWebCommand)Command!; _browserInfoService = browserInfoService; diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/FallbackOpenURLItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/FallbackOpenURLItem.cs index 7feb53b1de..cbb83c121d 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/FallbackOpenURLItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WebSearch/FallbackOpenURLItem.cs @@ -15,13 +15,14 @@ namespace Microsoft.CmdPal.Ext.WebSearch; internal sealed partial class FallbackOpenURLItem : FallbackCommandItem { + private const string _id = "com.microsoft.cmdpal.builtin.websearch.openurl.fallback"; private readonly IBrowserInfoService _browserInfoService; private readonly OpenURLCommand _executeItem; private static readonly CompositeFormat PluginOpenURL = System.Text.CompositeFormat.Parse(Properties.Resources.plugin_open_url); private static readonly CompositeFormat PluginOpenUrlInBrowser = System.Text.CompositeFormat.Parse(Properties.Resources.plugin_open_url_in_browser); public FallbackOpenURLItem(ISettingsInterface settings, IBrowserInfoService browserInfoService) - : base(new OpenURLCommand(string.Empty, browserInfoService), Resources.open_url_fallback_title) + : base(new OpenURLCommand(string.Empty, browserInfoService), Resources.open_url_fallback_title, _id) { ArgumentNullException.ThrowIfNull(browserInfoService); diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Pages/FallbackWindowsSettingsItem.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Pages/FallbackWindowsSettingsItem.cs index a63a2965bd..dc7d320a75 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Pages/FallbackWindowsSettingsItem.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.WindowsSettings/Pages/FallbackWindowsSettingsItem.cs @@ -2,11 +2,8 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Collections.Generic; using System.Globalization; using System.Linq; -using System.Xml.Linq; -using Microsoft.CmdPal.Ext.WindowsSettings.Classes; using Microsoft.CmdPal.Ext.WindowsSettings.Commands; using Microsoft.CmdPal.Ext.WindowsSettings.Helpers; using Microsoft.CmdPal.Ext.WindowsSettings.Properties; @@ -16,13 +13,15 @@ namespace Microsoft.CmdPal.Ext.WindowsSettings.Pages; internal sealed partial class FallbackWindowsSettingsItem : FallbackCommandItem { + private const string _id = "com.microsoft.cmdpal.builtin.windows.settings.fallback"; + private readonly Classes.WindowsSettings _windowsSettings; private readonly string _title = Resources.settings_fallback_title; private readonly string _subtitle = Resources.settings_fallback_subtitle; public FallbackWindowsSettingsItem(Classes.WindowsSettings windowsSettings) - : base(new NoOpCommand(), Resources.settings_title) + : base(new NoOpCommand(), Resources.settings_title, _id) { Icon = Icons.WindowsSettingsIcon; _windowsSettings = windowsSettings; diff --git a/src/modules/cmdpal/ext/SamplePagesExtension/SampleDataTransferPage.cs b/src/modules/cmdpal/ext/SamplePagesExtension/SampleDataTransferPage.cs new file mode 100644 index 0000000000..842f128842 --- /dev/null +++ b/src/modules/cmdpal/ext/SamplePagesExtension/SampleDataTransferPage.cs @@ -0,0 +1,254 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Globalization; +using Microsoft.CommandPalette.Extensions; +using Microsoft.CommandPalette.Extensions.Toolkit; +using Windows.ApplicationModel.DataTransfer; +using Windows.Storage.Streams; + +namespace SamplePagesExtension; + +internal sealed partial class SampleDataTransferPage : ListPage +{ + private readonly IListItem[] _items; + + public SampleDataTransferPage() + { + var dataPackageWithText = CreateDataPackageWithText(); + var dataPackageWithDelayedText = CreateDataPackageWithDelayedText(); + var dataPackageWithImage = CreateDataPackageWithImage(); + + _items = + [ + new ListItem(new NoOpCommand()) + { + Title = "Draggable item with a plain text", + Subtitle = "A sample page demonstrating how to drag and drop data", + DataPackage = dataPackageWithText, + }, + new ListItem(new NoOpCommand()) + { + Title = "Draggable item with a lazily rendered plain text", + Subtitle = "A sample page demonstrating how to drag and drop data with delayed rendering", + DataPackage = dataPackageWithDelayedText, + }, + new ListItem(new NoOpCommand()) + { + Title = "Draggable item with an image", + Subtitle = "This item has an image - package contains both file and a bitmap", + Icon = IconHelpers.FromRelativePath("Assets/Images/Swirls.png"), + DataPackage = dataPackageWithImage, + }, + new ListItem(new SampleDataTransferOnGridPage()) + { + Title = "Drag & drop grid", + Subtitle = "A sample page demonstrating a grid list of items", + Icon = new IconInfo("\uF0E2"), + } + ]; + } + + private static DataPackage CreateDataPackageWithText() + { + var dataPackageWithText = new DataPackage + { + Properties = + { + Title = "Item with data package with text", + Description = "This item has associated text with it", + }, + RequestedOperation = DataPackageOperation.Copy, + }; + dataPackageWithText.SetText("Text data in the Data Package"); + return dataPackageWithText; + } + + private static DataPackage CreateDataPackageWithDelayedText() + { + var dataPackageWithDelayedText = new DataPackage + { + Properties = + { + Title = "Item with delayed render data in the data package", + Description = "This items has an item associated with it that is evaluated when requested for the first time", + }, + RequestedOperation = DataPackageOperation.Copy, + }; + dataPackageWithDelayedText.SetDataProvider(StandardDataFormats.Text, request => + { + var d = request.GetDeferral(); + try + { + request.SetData(DateTime.Now.ToString("G", CultureInfo.CurrentCulture)); + } + finally + { + d.Complete(); + } + }); + return dataPackageWithDelayedText; + } + + private static DataPackage CreateDataPackageWithImage() + { + var dataPackageWithImage = new DataPackage + { + Properties = + { + Title = "Item with delayed render image in the data package", + Description = "This items has an image associated with it that is evaluated when requested for the first time", + }, + RequestedOperation = DataPackageOperation.Copy, + }; + dataPackageWithImage.SetDataProvider(StandardDataFormats.Bitmap, async void (request) => + { + var deferral = request.GetDeferral(); + try + { + var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Images/Swirls.png")); + var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); + var streamRef = RandomAccessStreamReference.CreateFromStream(stream); + request.SetData(streamRef); + } + finally + { + deferral.Complete(); + } + }); + dataPackageWithImage.SetDataProvider(StandardDataFormats.StorageItems, async void (request) => + { + var deferral = request.GetDeferral(); + try + { + var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Images/Swirls.png")); + var items = new[] { file }; + request.SetData(items); + } + finally + { + deferral.Complete(); + } + }); + return dataPackageWithImage; + } + + public override IListItem[] GetItems() => _items; +} + +[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Samples")] +internal sealed partial class SampleDataTransferOnGridPage : ListPage +{ + public SampleDataTransferOnGridPage() + { + GridProperties = new GalleryGridLayout + { + ShowTitle = true, + ShowSubtitle = true, + }; + } + + public override IListItem[] GetItems() + { + return [ + new ListItem(new NoOpCommand()) + { + Title = "Red Rectangle", + Subtitle = "Drag me", + Icon = IconHelpers.FromRelativePath("Assets/Images/RedRectangle.png"), + DataPackage = CreateDataPackageForImage("Assets/Images/RedRectangle.png"), + }, + new ListItem(new NoOpCommand()) + { + Title = "Swirls", + Subtitle = "Drop me", + Icon = IconHelpers.FromRelativePath("Assets/Images/Swirls.png"), + DataPackage = CreateDataPackageForImage("Assets/Images/Swirls.png"), + }, + new ListItem(new NoOpCommand()) + { + Title = "Windows Digital", + Subtitle = "Drag me", + Icon = IconHelpers.FromRelativePath("Assets/Images/Win-Digital.png"), + DataPackage = CreateDataPackageForImage("Assets/Images/Win-Digital.png"), + }, + new ListItem(new NoOpCommand()) + { + Title = "Red Rectangle", + Subtitle = "Drop me", + Icon = IconHelpers.FromRelativePath("Assets/Images/RedRectangle.png"), + DataPackage = CreateDataPackageForImage("Assets/Images/RedRectangle.png"), + }, + new ListItem(new NoOpCommand()) + { + Title = "Space", + Subtitle = "Drag me", + Icon = IconHelpers.FromRelativePath("Assets/Images/Space.png"), + DataPackage = CreateDataPackageForImage("Assets/Images/Space.png"), + }, + new ListItem(new NoOpCommand()) + { + Title = "Swirls", + Subtitle = "Drop me", + Icon = IconHelpers.FromRelativePath("Assets/Images/Swirls.png"), + DataPackage = CreateDataPackageForImage("Assets/Images/Swirls.png"), + }, + new ListItem(new NoOpCommand()) + { + Title = "Windows Digital", + Subtitle = "Drag me", + Icon = IconHelpers.FromRelativePath("Assets/Images/Win-Digital.png"), + DataPackage = CreateDataPackageForImage("Assets/Images/Win-Digital.png"), + }, + ]; + } + + private static DataPackage CreateDataPackageForImage(string relativePath) + { + var dataPackageWithImage = new DataPackage + { + Properties = + { + Title = "Image", + Description = "This item has an image associated with it.", + }, + RequestedOperation = DataPackageOperation.Copy, + }; + + var imageUri = new Uri($"ms-appx:///{relativePath}"); + + dataPackageWithImage.SetDataProvider(StandardDataFormats.Bitmap, async (request) => + { + var deferral = request.GetDeferral(); + try + { + var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(imageUri); + var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); + var streamRef = RandomAccessStreamReference.CreateFromStream(stream); + request.SetData(streamRef); + } + finally + { + deferral.Complete(); + } + }); + + dataPackageWithImage.SetDataProvider(StandardDataFormats.StorageItems, async (request) => + { + var deferral = request.GetDeferral(); + try + { + var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(imageUri); + var items = new[] { file }; + request.SetData(items); + } + finally + { + deferral.Complete(); + } + }); + return dataPackageWithImage; + } +} diff --git a/src/modules/cmdpal/ext/SamplePagesExtension/SamplesListPage.cs b/src/modules/cmdpal/ext/SamplePagesExtension/SamplesListPage.cs index 1c8ae56ee5..3dd67086c8 100644 --- a/src/modules/cmdpal/ext/SamplePagesExtension/SamplesListPage.cs +++ b/src/modules/cmdpal/ext/SamplePagesExtension/SamplesListPage.cs @@ -106,6 +106,13 @@ public partial class SamplesListPage : ListPage Subtitle = "A demo of the settings helpers", }, + // Data package samples + new ListItem(new SampleDataTransferPage()) + { + Title = "Clipboard and Drag-and-Drop Demo", + Subtitle = "Demonstrates clipboard integration and drag-and-drop functionality", + }, + // Evil edge cases // Anything weird that might break the palette - put that in here. new ListItem(new EvilSamplesPage()) diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandItem.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandItem.cs index cddb678fa3..46c32765a9 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandItem.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/CommandItem.cs @@ -2,14 +2,23 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using Windows.ApplicationModel.DataTransfer; +using Windows.Foundation.Collections; +using WinRT; + namespace Microsoft.CommandPalette.Extensions.Toolkit; -public partial class CommandItem : BaseObservable, ICommandItem +public partial class CommandItem : BaseObservable, ICommandItem, IExtendedAttributesProvider { + private readonly PropertySet _extendedAttributes = new(); + private ICommand? _command; private WeakEventListener<CommandItem, object, IPropChangedEventArgs>? _commandListener; private string _title = string.Empty; + private DataPackage? _dataPackage; + private DataPackageView? _dataPackageView; + public virtual IIconInfo? Icon { get => field; @@ -91,6 +100,32 @@ public partial class CommandItem : BaseObservable, ICommandItem = []; + public DataPackage? DataPackage + { + get => _dataPackage; + set + { + _dataPackage = value; + _dataPackageView = null; + _extendedAttributes[WellKnownExtensionAttributes.DataPackage] = value?.AsAgile().Get()?.GetView()!; + OnPropertyChanged(nameof(DataPackage)); + OnPropertyChanged(nameof(DataPackageView)); + } + } + + public DataPackageView? DataPackageView + { + get => _dataPackageView; + set + { + _dataPackage = null; + _dataPackageView = value; + _extendedAttributes[WellKnownExtensionAttributes.DataPackage] = value?.AsAgile().Get()!; + OnPropertyChanged(nameof(DataPackage)); + OnPropertyChanged(nameof(DataPackageView)); + } + } + public CommandItem() : this(new NoOpCommand()) { @@ -132,4 +167,9 @@ public partial class CommandItem : BaseObservable, ICommandItem Title = title; Subtitle = subtitle; } + + public IDictionary<string, object> GetProperties() + { + return _extendedAttributes; + } } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FallbackCommandItem.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FallbackCommandItem.cs index 1c0b5c18d0..0e14ce570f 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FallbackCommandItem.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FallbackCommandItem.cs @@ -4,18 +4,25 @@ namespace Microsoft.CommandPalette.Extensions.Toolkit; -public partial class FallbackCommandItem : CommandItem, IFallbackCommandItem, IFallbackHandler +public partial class FallbackCommandItem : CommandItem, IFallbackCommandItem, IFallbackHandler, IFallbackCommandItem2 { private readonly IFallbackHandler? _fallbackHandler; - public FallbackCommandItem(string displayTitle) + public FallbackCommandItem(string displayTitle, string id) { DisplayTitle = displayTitle; + Id = id; } - public FallbackCommandItem(ICommand command, string displayTitle) + public FallbackCommandItem(ICommand command, string displayTitle, string id) : base(command) { + if (string.IsNullOrWhiteSpace(id)) + { + throw new ArgumentException("A non-empty or whitespace Id must be provided.", nameof(id)); + } + + Id = id; DisplayTitle = displayTitle; if (command is IFallbackHandler f) { @@ -29,6 +36,8 @@ public partial class FallbackCommandItem : CommandItem, IFallbackCommandItem, IF init => _fallbackHandler = value; } + public virtual string Id { get; } + public virtual string DisplayTitle { get; } public virtual void UpdateQuery(string query) => _fallbackHandler?.UpdateQuery(query); diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FontIconData.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FontIconData.cs index 7e12d38d0c..3fcc01b96d 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FontIconData.cs +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/FontIconData.cs @@ -27,6 +27,6 @@ public partial class FontIconData : IconData, IExtendedAttributesProvider public IDictionary<string, object>? GetProperties() => new ValueSet() { - { "FontFamily", FontFamily }, + { WellKnownExtensionAttributes.FontFamily, FontFamily }, }; } diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Microsoft.CommandPalette.Extensions.Toolkit.csproj b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Microsoft.CommandPalette.Extensions.Toolkit.csproj index eb887e9e36..24f2c50c5a 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Microsoft.CommandPalette.Extensions.Toolkit.csproj +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/Microsoft.CommandPalette.Extensions.Toolkit.csproj @@ -3,10 +3,9 @@ <Import Project="..\..\..\..\Common.Dotnet.AotCompatibility.props" /> <PropertyGroup> - + <RepoRoot>$(MSBuildThisFileDirectory)..\..\..\..\..\</RepoRoot> <WindowsSdkPackageVersion>10.0.26100.57</WindowsSdkPackageVersion> - - <OutputPath>$(SolutionDir)$(Platform)\$(Configuration)\Microsoft.CommandPalette.Extensions.Toolkit</OutputPath> + <OutputPath>$(RepoRoot)$(Platform)\$(Configuration)\Microsoft.CommandPalette.Extensions.Toolkit</OutputPath> <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> <ImplicitUsings>enable</ImplicitUsings> @@ -21,7 +20,7 @@ <PropertyGroup Condition="'$(CIBuild)'=='true'"> <SignAssembly>true</SignAssembly> <DelaySign>true</DelaySign> - <AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)..\..\..\..\..\.pipelines\272MSSharedLibSN2048.snk</AssemblyOriginatorKeyFile> + <AssemblyOriginatorKeyFile>$(RepoRoot).pipelines\272MSSharedLibSN2048.snk</AssemblyOriginatorKeyFile> </PropertyGroup> <PropertyGroup> @@ -47,11 +46,19 @@ </ItemGroup> <ItemGroup> - <ProjectReference Include="..\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.vcxproj" /> - </ItemGroup> - + <ProjectReference Include="..\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.vcxproj"> + <ReferenceOutputAssembly>False</ReferenceOutputAssembly> + <BuildProject>True</BuildProject> + </ProjectReference> + <CsWinRTInputs Include="$(RepoRoot)$(Platform)\$(Configuration)\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.winmd" /> + <!-- Native implementation DLL --> + <None Include="$(RepoRoot)$(Platform)\$(Configuration)\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.dll"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> + </ItemGroup> + <ItemGroup> - <Content Include="$(SolutionDir)$(Platform)\$(Configuration)\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.winmd" Link="Microsoft.CommandPalette.Extensions.winmd" CopyToOutputDirectory="PreserveNewest" /> + <Content Include="$(RepoRoot)$(Platform)\$(Configuration)\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.winmd" Link="Microsoft.CommandPalette.Extensions.winmd" CopyToOutputDirectory="PreserveNewest" /> </ItemGroup> <ItemGroup> diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/WellKnownExtensionAttributes.cs b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/WellKnownExtensionAttributes.cs new file mode 100644 index 0000000000..508cda72c1 --- /dev/null +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions.Toolkit/WellKnownExtensionAttributes.cs @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace Microsoft.CommandPalette.Extensions.Toolkit; + +public static class WellKnownExtensionAttributes +{ + public const string DataPackage = "Microsoft.CommandPalette.DataPackage"; + + public const string FontFamily = "FontFamily"; +} diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/Microsoft.CommandPalette.Extensions.idl b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/Microsoft.CommandPalette.Extensions.idl index b302ce6d75..d7eed3bc15 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/Microsoft.CommandPalette.Extensions.idl +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/Microsoft.CommandPalette.Extensions.idl @@ -371,6 +371,11 @@ namespace Microsoft.CommandPalette.Extensions IFallbackHandler FallbackHandler{ get; }; String DisplayTitle { get; }; }; + + [contract(Microsoft.CommandPalette.Extensions.ExtensionsContract, 1)] + interface IFallbackCommandItem2 requires IFallbackCommandItem { + String Id { get; }; + }; [contract(Microsoft.CommandPalette.Extensions.ExtensionsContract, 1)] interface ICommandProvider requires Windows.Foundation.IClosable, INotifyItemsChanged diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/Microsoft.CommandPalette.Extensions.vcxproj b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/Microsoft.CommandPalette.Extensions.vcxproj index fb647cc444..69793c6281 100644 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/Microsoft.CommandPalette.Extensions.vcxproj +++ b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/Microsoft.CommandPalette.Extensions.vcxproj @@ -1,16 +1,16 @@ <?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> - <PropertyGroup> - <PathToRoot>..\..\..\..\..\</PathToRoot> - <WasdkNuget>$(PathToRoot)packages\Microsoft.WindowsAppSDK.1.8.250907003</WasdkNuget> - <CppWinRTNuget>$(PathToRoot)packages\Microsoft.Windows.CppWinRT.2.0.240111.5</CppWinRTNuget> - <WindowsSdkBuildToolsNuget>$(PathToRoot)packages\Microsoft.Windows.SDK.BuildTools.10.0.26100.6901</WindowsSdkBuildToolsNuget> - <WebView2Nuget>$(PathToRoot)packages\Microsoft.Web.WebView2.1.0.2903.40</WebView2Nuget> + <PropertyGroup Label="NuGet"> + <!-- Tell NuGet this is PackageReference style --> + <RestoreProjectStyle>PackageReference</RestoreProjectStyle> + <!-- Tell NuGet we're a native project --> + <NuGetTargetMoniker>native,Version=v0.0</NuGetTargetMoniker> + <!-- Tell NuGet we target Windows (use your existing WindowsTargetPlatformVersion) --> + <NuGetTargetPlatformIdentifier>Windows</NuGetTargetPlatformIdentifier> + <NuGetTargetPlatformVersion>$(WindowsTargetPlatformVersion)</NuGetTargetPlatformVersion> </PropertyGroup> - <Import Project="$(WasdkNuget)\build\native\Microsoft.WindowsAppSDK.props" Condition="Exists('$(WasdkNuget)\build\native\Microsoft.WindowsAppSDK.props')" /> - <Import Project="$(CppWinRTNuget)\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('$(CppWinRTNuget)\build\native\Microsoft.Windows.CppWinRT.props')" /> - <Import Project="$(WindowsSdkBuildToolsNuget)\build\Microsoft.Windows.SDK.BuildTools.props" Condition="Exists('$(WindowsSdkBuildToolsNuget)\build\Microsoft.Windows.SDK.BuildTools.props')" /> <PropertyGroup Label="Globals"> + <RepoRoot>$(MSBuildThisFileDirectory)..\..\..\..\..\</RepoRoot> <CppWinRTOptimized>true</CppWinRTOptimized> <CppWinRTRootNamespaceAutoMerge>true</CppWinRTRootNamespaceAutoMerge> <CppWinRTGenerateWindowsMetadata>true</CppWinRTGenerateWindowsMetadata> @@ -25,7 +25,13 @@ <ApplicationTypeRevision>10.0</ApplicationTypeRevision> <WindowsTargetPlatformMinVersion>10.0.19041.0</WindowsTargetPlatformMinVersion> <WindowsTargetPlatformVersion>10.0.26100.0</WindowsTargetPlatformVersion> + <WindowsAppSDKVerifyTransitiveDependencies>false</WindowsAppSDKVerifyTransitiveDependencies> </PropertyGroup> + <ItemGroup> + <PackageReference Include="Microsoft.WindowsAppSDK" GeneratePathProperty="true" /> + <PackageReference Include="Microsoft.Windows.CppWinRT" GeneratePathProperty="true" /> + <PackageReference Include="Microsoft.Windows.ImplementationLibrary" GeneratePathProperty="true" /> + </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|ARM64"> @@ -45,10 +51,6 @@ <Platform>x64</Platform> </ProjectConfiguration> </ItemGroup> - <PropertyGroup> - <OutDir>$(SolutionDir)$(Platform)\$(Configuration)\Microsoft.CommandPalette.Extensions\</OutDir> - <IntDir>obj\$(Platform)\$(Configuration)\</IntDir> - </PropertyGroup> <PropertyGroup Label="Configuration"> <ConfigurationType>DynamicLibrary</ConfigurationType> <PlatformToolset>v143</PlatformToolset> @@ -153,7 +155,6 @@ <Midl Include="Microsoft.CommandPalette.Extensions.idl" /> </ItemGroup> <ItemGroup> - <None Include="packages.config" /> <None Include="Microsoft.CommandPalette.Extensions.def" /> </ItemGroup> <ItemGroup> @@ -161,23 +162,9 @@ <DeploymentContent>false</DeploymentContent> </Text> </ItemGroup> + <PropertyGroup> + <OutDir>$(RepoRoot)$(Platform)\$(Configuration)\Microsoft.CommandPalette.Extensions\</OutDir> + <IntDir>obj\$(Platform)\$(Configuration)\</IntDir> + </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> - <ImportGroup Label="ExtensionTargets"> - <Import Project="$(WindowsSdkBuildToolsNuget)\build\Microsoft.Windows.SDK.BuildTools.targets" Condition="Exists('$(WindowsSdkBuildToolsNuget)\build\Microsoft.Windows.SDK.BuildTools.targets')" /> - <Import Project="$(CppWinRTNuget)\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('$(CppWinRTNuget)\build\native\Microsoft.Windows.CppWinRT.targets')" /> - <Import Project="$(WasdkNuget)\build\native\Microsoft.WindowsAppSDK.targets" Condition="Exists('$(WasdkNuget)\build\native\Microsoft.WindowsAppSDK.targets')" /> - <Import Project="$(WebView2Nuget)\build\native\Microsoft.Web.WebView2.targets" Condition="Exists('$(WebView2Nuget)\build\native\Microsoft.Web.WebView2.targets')" /> - </ImportGroup> - <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> - <PropertyGroup> - <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> - </PropertyGroup> - <Error Condition="!Exists('$(WindowsSdkBuildToolsNuget)\build\Microsoft.Windows.SDK.BuildTools.props')" Text="$([System.String]::Format('$(ErrorText)', '$(WindowsSdkBuildToolsNuget)\build\Microsoft.Windows.SDK.BuildTools.props'))" /> - <Error Condition="!Exists('$(WindowsSdkBuildToolsNuget)\build\Microsoft.Windows.SDK.BuildTools.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(WindowsSdkBuildToolsNuget)\build\Microsoft.Windows.SDK.BuildTools.targets'))" /> - <Error Condition="!Exists('$(CppWinRTNuget)\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '$(CppWinRTNuget)\build\native\Microsoft.Windows.CppWinRT.props'))" /> - <Error Condition="!Exists('$(CppWinRTNuget)\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(CppWinRTNuget)\build\native\Microsoft.Windows.CppWinRT.targets'))" /> - <Error Condition="!Exists('$(WasdkNuget)\build\native\Microsoft.WindowsAppSDK.props')" Text="$([System.String]::Format('$(ErrorText)', '$(WasdkNuget)\build\native\Microsoft.WindowsAppSDK.props'))" /> - <Error Condition="!Exists('$(WasdkNuget)\build\native\Microsoft.WindowsAppSDK.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(WasdkNuget)\build\native\Microsoft.WindowsAppSDK.targets'))" /> - <Error Condition="!Exists('$(WebView2Nuget)\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(WebView2Nuget)\build\native\Microsoft.Web.WebView2.targets'))" /> - </Target> </Project> \ No newline at end of file diff --git a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/packages.config b/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/packages.config deleted file mode 100644 index 091ef0782d..0000000000 --- a/src/modules/cmdpal/extensionsdk/Microsoft.CommandPalette.Extensions/packages.config +++ /dev/null @@ -1,17 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<packages> - <package id="Microsoft.Web.WebView2" version="1.0.2903.40" targetFramework="native" /> - <package id="Microsoft.Windows.CppWinRT" version="2.0.240111.5" targetFramework="native" /> - <package id="Microsoft.Windows.ImplementationLibrary" version="1.0.231216.1" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK" version="1.8.250907003" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.Base" version="1.8.250831001" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.Foundation" version="1.8.250906002" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.WinUI" version="1.8.250906003" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.Runtime" version="1.8.250907003" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.DWrite" version="1.8.25090401" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.InteractiveExperiences" version="1.8.250906004" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.Widgets" version="1.8.250904007" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.AI" version="1.8.37" targetFramework="native" /> - <package id="Microsoft.Windows.SDK.BuildTools" version="10.0.26100.6901" targetFramework="native" /> - <package id="Microsoft.Windows.SDK.BuildTools.MSIX" version="1.7.20250829.1" targetFramework="native" /> -</packages> diff --git a/src/modules/cmdpal/extensionsdk/nuget/Microsoft.CommandPalette.Extensions.SDK.nuspec b/src/modules/cmdpal/extensionsdk/nuget/Microsoft.CommandPalette.Extensions.SDK.nuspec index 8d2a7d5f54..4fa4297af9 100644 --- a/src/modules/cmdpal/extensionsdk/nuget/Microsoft.CommandPalette.Extensions.SDK.nuspec +++ b/src/modules/cmdpal/extensionsdk/nuget/Microsoft.CommandPalette.Extensions.SDK.nuspec @@ -25,19 +25,19 @@ <file src="Microsoft.CommandPalette.Extensions.props" target="build\"/> <file src="Microsoft.CommandPalette.Extensions.targets" target="build\"/> <!-- AnyCPU Managed dlls from SDK.Lib project --> - <file src="..\Microsoft.CommandPalette.Extensions.Toolkit\x64\release\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.dll" target="lib\net8.0-windows10.0.19041.0\"/> - <file src="..\Microsoft.CommandPalette.Extensions.Toolkit\x64\release\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.pdb" target="lib\net8.0-windows10.0.19041.0\"/> - <file src="..\Microsoft.CommandPalette.Extensions.Toolkit\x64\release\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.deps.json" target="lib\net8.0-windows10.0.19041.0\"/> + <file src="..\..\..\..\..\x64\release\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.dll" target="lib\net8.0-windows10.0.19041.0\"/> + <file src="..\..\..\..\..\x64\release\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.pdb" target="lib\net8.0-windows10.0.19041.0\"/> + <file src="..\..\..\..\..\x64\release\Microsoft.CommandPalette.Extensions.Toolkit\Microsoft.CommandPalette.Extensions.Toolkit.deps.json" target="lib\net8.0-windows10.0.19041.0\"/> <!-- Native dlls and winmd from SDK cpp project --> <!-- TODO: we may not need this, since there are no implementations in the Microsoft.CommandPalette.Extensions namespace --> - <file src="..\Microsoft.CommandPalette.Extensions\x64\release\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.dll" target="runtimes\win-x64\native\"/> - <file src="..\Microsoft.CommandPalette.Extensions\x64\release\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.pdb" target="runtimes\win-x64\native\"/> - <file src="..\Microsoft.CommandPalette.Extensions\arm64\release\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.dll" target="runtimes\win-arm64\native\"/> - <file src="..\Microsoft.CommandPalette.Extensions\arm64\release\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.pdb" target="runtimes\win-arm64\native\"/> + <file src="..\..\..\..\..\x64\release\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.dll" target="runtimes\win-x64\native\"/> + <file src="..\..\..\..\..\x64\release\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.pdb" target="runtimes\win-x64\native\"/> + <file src="..\..\..\..\..\arm64\release\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.dll" target="runtimes\win-arm64\native\"/> + <file src="..\..\..\..\..\arm64\release\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.pdb" target="runtimes\win-arm64\native\"/> <!-- Not putting the following into the lib folder because we don't want plugin project to directly reference the winmd --> - <file src="..\Microsoft.CommandPalette.Extensions\x64\release\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.winmd" target="winmd\"/> + <file src="..\..\..\..\..\x64\release\Microsoft.CommandPalette.Extensions\Microsoft.CommandPalette.Extensions.winmd" target="winmd\"/> <file src="..\README.md" target="docs\" /> </files> diff --git a/src/modules/colorPicker/ColorPicker.ModuleServices/ColorFormatValue.cs b/src/modules/colorPicker/ColorPicker.ModuleServices/ColorFormatValue.cs new file mode 100644 index 0000000000..90e71e6f18 --- /dev/null +++ b/src/modules/colorPicker/ColorPicker.ModuleServices/ColorFormatValue.cs @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace ColorPicker.ModuleServices; + +public sealed record ColorFormatValue(string Format, string Value); diff --git a/src/modules/colorPicker/ColorPicker.ModuleServices/ColorPicker.ModuleServices.csproj b/src/modules/colorPicker/ColorPicker.ModuleServices/ColorPicker.ModuleServices.csproj new file mode 100644 index 0000000000..1efe5cdc8d --- /dev/null +++ b/src/modules/colorPicker/ColorPicker.ModuleServices/ColorPicker.ModuleServices.csproj @@ -0,0 +1,29 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <!-- Look at Directory.Build.props in root for common stuff as well --> + <Import Project="..\..\..\Common.Dotnet.CsWinRT.props" /> + <Import Project="..\..\..\Common.Dotnet.AotCompatibility.props" /> + + <PropertyGroup> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> + <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> + <EnableDefaultCompileItems>false</EnableDefaultCompileItems> + </PropertyGroup> + + <ItemGroup> + <Compile Include="ColorFormatValue.cs" /> + <Compile Include="ColorPickerService.cs" /> + <Compile Include="IColorPickerService.cs" /> + <Compile Include="ColorPickerServiceJsonContext.cs" /> + <Compile Include="SavedColor.cs" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\..\..\common\ManagedCommon\ManagedCommon.csproj" /> + <ProjectReference Include="..\..\..\common\interop\PowerToys.Interop.vcxproj" /> + <ProjectReference Include="..\..\..\common\Common.UI\Common.UI.csproj" /> + <ProjectReference Include="..\..\..\common\PowerToys.ModuleContracts\PowerToys.ModuleContracts.csproj" /> + <ProjectReference Include="..\..\..\settings-ui\Settings.UI.Library\Settings.UI.Library.csproj" /> + </ItemGroup> +</Project> diff --git a/src/modules/colorPicker/ColorPicker.ModuleServices/ColorPickerService.cs b/src/modules/colorPicker/ColorPicker.ModuleServices/ColorPickerService.cs new file mode 100644 index 0000000000..6407ae6ed9 --- /dev/null +++ b/src/modules/colorPicker/ColorPicker.ModuleServices/ColorPickerService.cs @@ -0,0 +1,157 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Text.Json; +using Common.UI; +using ManagedCommon; +using Microsoft.PowerToys.Settings.UI.Library; +using PowerToys.Interop; +using PowerToys.ModuleContracts; + +namespace ColorPicker.ModuleServices; + +/// <summary> +/// Provides programmatic control for Color Picker actions. +/// </summary> +public sealed class ColorPickerService : ModuleServiceBase, IColorPickerService +{ + public static ColorPickerService Instance { get; } = new(); + + public override string Key => SettingsDeepLink.SettingsWindow.ColorPicker.ToString(); + + protected override SettingsDeepLink.SettingsWindow SettingsWindow => SettingsDeepLink.SettingsWindow.ColorPicker; + + public override Task<OperationResult> LaunchAsync(CancellationToken cancellationToken = default) + { + // Default launch -> open picker. + return OpenPickerAsync(cancellationToken); + } + + public Task<OperationResult> OpenPickerAsync(CancellationToken cancellationToken = default) + { + return SignalEventAsync(Constants.ShowColorPickerSharedEvent(), "Color Picker"); + } + + public Task<OperationResult<IReadOnlyList<SavedColor>>> GetSavedColorsAsync(CancellationToken cancellationToken = default) + { + try + { + cancellationToken.ThrowIfCancellationRequested(); + + var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + var historyPath = Path.Combine(localAppData, "Microsoft", "PowerToys", "ColorPicker", "colorHistory.json"); + if (!File.Exists(historyPath)) + { + return Task.FromResult(OperationResults.Ok<IReadOnlyList<SavedColor>>(Array.Empty<SavedColor>())); + } + + using var stream = File.OpenRead(historyPath); + var colors = JsonSerializer.Deserialize(stream, ColorPickerServiceJsonContext.Default.ListString) ?? new List<string>(); + + var settingsUtils = SettingsUtils.Default; + var settings = settingsUtils.GetSettingsOrDefault<ColorPickerSettings>(ColorPickerSettings.ModuleName); + + var results = new List<SavedColor>(colors.Count); + foreach (var entry in colors) + { + if (!TryParseArgb(entry, out var color)) + { + continue; + } + + var formats = BuildFormats(color, settings); + var hex = $"#{color.R:X2}{color.G:X2}{color.B:X2}"; + + results.Add(new SavedColor( + hex, + color.A, + color.R, + color.G, + color.B, + formats)); + } + + return Task.FromResult(OperationResults.Ok<IReadOnlyList<SavedColor>>(results)); + } + catch (OperationCanceledException) + { + return Task.FromResult(OperationResults.Fail<IReadOnlyList<SavedColor>>("Reading saved colors was cancelled.")); + } + catch (Exception ex) + { + return Task.FromResult(OperationResults.Fail<IReadOnlyList<SavedColor>>($"Failed to read saved colors: {ex.Message}")); + } + } + + private static Task<OperationResult> SignalEventAsync(string eventName, string actionDescription) + { + try + { + using var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, eventName); + if (!eventHandle.Set()) + { + return Task.FromResult(OperationResult.Fail($"Failed to signal {actionDescription}.")); + } + + return Task.FromResult(OperationResult.Ok()); + } + catch (Exception ex) + { + return Task.FromResult(OperationResult.Fail($"Failed to signal {actionDescription}: {ex.Message}")); + } + } + + private static bool TryParseArgb(string value, out Color color) + { + color = Color.Empty; + if (string.IsNullOrWhiteSpace(value)) + { + return false; + } + + var parts = value.Split('|'); + if (parts.Length != 4) + { + return false; + } + + if (byte.TryParse(parts[0], out var a) && + byte.TryParse(parts[1], out var r) && + byte.TryParse(parts[2], out var g) && + byte.TryParse(parts[3], out var b)) + { + color = Color.FromArgb(a, r, g, b); + return true; + } + + return false; + } + + private static IReadOnlyList<ColorFormatValue> BuildFormats(Color color, ColorPickerSettings settings) + { + var formats = new List<ColorFormatValue>(); + foreach (var kvp in settings.Properties.VisibleColorFormats) + { + var formatName = kvp.Key; + var (isVisible, formatString) = kvp.Value; + if (!isVisible) + { + continue; + } + + var formatted = ColorFormatHelper.GetStringRepresentation(color, formatString); + if (formatName.Equals("HEX", StringComparison.OrdinalIgnoreCase) && !formatted.StartsWith('#')) + { + formatted = "#" + formatted; + } + + formats.Add(new ColorFormatValue(formatName, formatted)); + } + + return formats; + } +} diff --git a/src/modules/colorPicker/ColorPicker.ModuleServices/ColorPickerServiceJsonContext.cs b/src/modules/colorPicker/ColorPicker.ModuleServices/ColorPickerServiceJsonContext.cs new file mode 100644 index 0000000000..f26e9009d3 --- /dev/null +++ b/src/modules/colorPicker/ColorPicker.ModuleServices/ColorPickerServiceJsonContext.cs @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.Text.Json.Serialization; +using Microsoft.PowerToys.Settings.UI.Library; + +namespace ColorPicker.ModuleServices; + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(List<string>))] +[JsonSerializable(typeof(List<SavedColor>))] +[JsonSerializable(typeof(SavedColor))] +[JsonSerializable(typeof(ColorFormatValue))] +[JsonSerializable(typeof(ColorPickerSettings))] +internal sealed partial class ColorPickerServiceJsonContext : JsonSerializerContext +{ +} diff --git a/src/modules/colorPicker/ColorPicker.ModuleServices/IColorPickerService.cs b/src/modules/colorPicker/ColorPicker.ModuleServices/IColorPickerService.cs new file mode 100644 index 0000000000..4ad2ca3da3 --- /dev/null +++ b/src/modules/colorPicker/ColorPicker.ModuleServices/IColorPickerService.cs @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using PowerToys.ModuleContracts; + +namespace ColorPicker.ModuleServices; + +public interface IColorPickerService : IModuleService +{ + Task<OperationResult> OpenPickerAsync(CancellationToken cancellationToken = default); + + Task<OperationResult<IReadOnlyList<SavedColor>>> GetSavedColorsAsync(CancellationToken cancellationToken = default); +} diff --git a/src/modules/colorPicker/ColorPicker.ModuleServices/SavedColor.cs b/src/modules/colorPicker/ColorPicker.ModuleServices/SavedColor.cs new file mode 100644 index 0000000000..3697129aa0 --- /dev/null +++ b/src/modules/colorPicker/ColorPicker.ModuleServices/SavedColor.cs @@ -0,0 +1,7 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace ColorPicker.ModuleServices; + +public sealed record SavedColor(string Hex, byte A, byte R, byte G, byte B, IReadOnlyList<ColorFormatValue> Formats); diff --git a/src/modules/colorPicker/ColorPickerUI/Helpers/AppStateHandler.cs b/src/modules/colorPicker/ColorPickerUI/Helpers/AppStateHandler.cs index 705324cb93..72c874d839 100644 --- a/src/modules/colorPicker/ColorPickerUI/Helpers/AppStateHandler.cs +++ b/src/modules/colorPicker/ColorPickerUI/Helpers/AppStateHandler.cs @@ -205,7 +205,7 @@ namespace ColorPicker.Helpers private void ColorEditorViewModel_OpenSettingsRequested(object sender, EventArgs e) { - SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.ColorPicker, false); + SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.ColorPicker); } internal void RegisterWindowHandle(System.Windows.Interop.HwndSource hwndSource) diff --git a/src/modules/fancyzones/FancyZones.FuzzTests/FancyZones.FuzzTests.csproj b/src/modules/fancyzones/FancyZones.FuzzTests/FancyZones.FuzzTests.csproj index 9890b67217..dd1665bc0d 100644 --- a/src/modules/fancyzones/FancyZones.FuzzTests/FancyZones.FuzzTests.csproj +++ b/src/modules/fancyzones/FancyZones.FuzzTests/FancyZones.FuzzTests.csproj @@ -10,9 +10,16 @@ <ItemGroup> <Compile Include="..\editor\FancyZonesEditor\Utils\ParsingResult.cs" Link="ParsingResult.cs" /> + <Compile Include="..\FancyZonesEditorCommon\Data\AppliedLayouts.cs" Link="AppliedLayouts.cs" /> <Compile Include="..\FancyZonesEditorCommon\Data\CustomLayouts.cs" Link="CustomLayouts.cs" /> + <Compile Include="..\FancyZonesEditorCommon\Data\DefaultLayouts.cs" Link="DefaultLayouts.cs" /> <Compile Include="..\FancyZonesEditorCommon\Data\EditorData`1.cs" Link="EditorData`1.cs" /> + <Compile Include="..\FancyZonesEditorCommon\Data\EditorParameters.cs" Link="EditorParameters.cs" /> + <Compile Include="..\FancyZonesEditorCommon\Data\FancyZonesJsonContext.cs" Link="FancyZonesJsonContext.cs" /> + <Compile Include="..\FancyZonesEditorCommon\Data\FancyZonesPaths.cs" Link="FancyZonesPaths.cs" /> <Compile Include="..\FancyZonesEditorCommon\Data\LayoutDefaultSettings.cs" Link="LayoutDefaultSettings.cs" /> + <Compile Include="..\FancyZonesEditorCommon\Data\LayoutHotkeys.cs" Link="LayoutHotkeys.cs" /> + <Compile Include="..\FancyZonesEditorCommon\Data\LayoutTemplates.cs" Link="LayoutTemplates.cs" /> <Compile Include="..\FancyZonesEditorCommon\Utils\DashCaseNamingPolicy.cs" Link="DashCaseNamingPolicy.cs" /> <Compile Include="..\FancyZonesEditorCommon\Utils\IOUtils.cs" Link="IOUtils.cs" /> <Compile Include="..\FancyZonesEditorCommon\Utils\StringUtils.cs" Link="StringUtils.cs" /> diff --git a/src/modules/fancyzones/FancyZones/FancyZonesApp.cpp b/src/modules/fancyzones/FancyZones/FancyZonesApp.cpp index 97faec65d5..a20a6fc7f6 100644 --- a/src/modules/fancyzones/FancyZones/FancyZonesApp.cpp +++ b/src/modules/fancyzones/FancyZones/FancyZonesApp.cpp @@ -22,7 +22,7 @@ FancyZonesApp::FancyZonesApp(const std::wstring& appName, const std::wstring& ap m_app = MakeFancyZones(reinterpret_cast<HINSTANCE>(&__ImageBase), std::bind(&FancyZonesApp::DisableModule, this)); m_mainThreadId = GetCurrentThreadId(); - m_exitEventWaiter = EventWaiter(CommonSharedConstants::FZE_EXIT_EVENT, [&](int err) { + m_exitEventWaiter.start(CommonSharedConstants::FZE_EXIT_EVENT, [&](DWORD err) { if (err == ERROR_SUCCESS) { DisableModule(); diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/FancyZonesBaseCommand.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/FancyZonesBaseCommand.cs new file mode 100644 index 0000000000..d47fc42cdf --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/FancyZonesBaseCommand.cs @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.CommandLine; +using System.CommandLine.Invocation; + +using FancyZonesCLI; +using FancyZonesCLI.CommandLine; + +namespace FancyZonesCLI.CommandLine.Commands; + +internal abstract class FancyZonesBaseCommand : Command +{ + protected FancyZonesBaseCommand(string name, string description) + : base(name, description) + { + this.SetHandler(InvokeInternal); + } + + protected abstract string Execute(InvocationContext context); + + private void InvokeInternal(InvocationContext context) + { + Logger.LogInfo($"Executing command '{Name}'"); + + if (!FancyZonesCliGuards.IsFancyZonesRunning()) + { + Logger.LogWarning($"Command '{Name}' blocked: FancyZones is not running"); + context.Console.Error.Write($"Error: FancyZones is not running. Start PowerToys (FancyZones) and retry.{Environment.NewLine}"); + context.ExitCode = 1; + return; + } + + try + { + string output = Execute(context); + context.ExitCode = 0; + + Logger.LogInfo($"Command '{Name}' completed successfully"); + Logger.LogDebug($"Command '{Name}' output length: {output?.Length ?? 0}"); + + if (!string.IsNullOrEmpty(output)) + { + context.Console.Out.Write(output); + context.Console.Out.Write(Environment.NewLine); + } + } + catch (Exception ex) + { + Logger.LogError($"Command '{Name}' failed", ex); + context.Console.Error.Write($"Error: {ex.Message}{Environment.NewLine}"); + context.ExitCode = 1; + } + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetActiveLayoutCommand.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetActiveLayoutCommand.cs new file mode 100644 index 0000000000..d2fc0f4f9d --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetActiveLayoutCommand.cs @@ -0,0 +1,85 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.CommandLine.Invocation; +using System.Globalization; + +using FancyZonesCLI.Utils; +using FancyZonesEditorCommon.Data; +using FancyZonesEditorCommon.Utils; + +namespace FancyZonesCLI.CommandLine.Commands; + +internal sealed partial class GetActiveLayoutCommand : FancyZonesBaseCommand +{ + public GetActiveLayoutCommand() + : base("get-active-layout", "Show currently active layout") + { + AddAlias("active"); + } + + protected override string Execute(InvocationContext context) + { + // Trigger FancyZones to save current monitor info and read it reliably. + var editorParams = EditorParametersRefresh.ReadEditorParametersWithRefresh( + () => NativeMethods.NotifyFancyZones(NativeMethods.WM_PRIV_SAVE_EDITOR_PARAMETERS)); + + if (editorParams.Monitors == null || editorParams.Monitors.Count == 0) + { + throw new InvalidOperationException("Could not get current monitor information."); + } + + // Read applied layouts. + var appliedLayouts = FancyZonesDataIO.ReadAppliedLayouts(); + + if (appliedLayouts.AppliedLayouts == null) + { + return "No layouts configured."; + } + + var sb = new System.Text.StringBuilder(); + sb.AppendLine("\n=== Active FancyZones Layout(s) ===\n"); + + // Show only layouts for currently connected monitors. + for (int i = 0; i < editorParams.Monitors.Count; i++) + { + var monitor = editorParams.Monitors[i]; + sb.AppendLine(CultureInfo.InvariantCulture, $"Monitor {i + 1}: {monitor.Monitor}"); + + var matchedLayout = AppliedLayoutsHelper.FindLayoutForMonitor( + appliedLayouts, + monitor.Monitor, + monitor.MonitorSerialNumber, + monitor.MonitorNumber, + monitor.VirtualDesktop); + + if (matchedLayout.HasValue) + { + var layout = matchedLayout.Value.AppliedLayout; + sb.AppendLine(CultureInfo.InvariantCulture, $" Layout UUID: {layout.Uuid}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Layout Type: {layout.Type}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Zone Count: {layout.ZoneCount}"); + + if (layout.ShowSpacing) + { + sb.AppendLine(CultureInfo.InvariantCulture, $" Spacing: {layout.Spacing}px"); + } + + sb.AppendLine(CultureInfo.InvariantCulture, $" Sensitivity Radius: {layout.SensitivityRadius}px"); + } + else + { + sb.AppendLine(" No layout applied"); + } + + if (i < editorParams.Monitors.Count - 1) + { + sb.AppendLine(); + } + } + + return sb.ToString().TrimEnd(); + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetHotkeysCommand.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetHotkeysCommand.cs new file mode 100644 index 0000000000..342598c822 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetHotkeysCommand.cs @@ -0,0 +1,43 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.CommandLine.Invocation; +using System.Globalization; +using System.Linq; + +using FancyZonesEditorCommon.Data; +using FancyZonesEditorCommon.Utils; + +namespace FancyZonesCLI.CommandLine.Commands; + +internal sealed partial class GetHotkeysCommand : FancyZonesBaseCommand +{ + public GetHotkeysCommand() + : base("get-hotkeys", "List all layout hotkeys") + { + AddAlias("hk"); + } + + protected override string Execute(InvocationContext context) + { + var hotkeys = FancyZonesDataIO.ReadLayoutHotkeys(); + + if (hotkeys.LayoutHotkeys == null || hotkeys.LayoutHotkeys.Count == 0) + { + return "No hotkeys configured."; + } + + var sb = new System.Text.StringBuilder(); + sb.AppendLine("=== Layout Hotkeys ===\n"); + sb.AppendLine("Press Win + Ctrl + Alt + <number> to switch layouts:\n"); + + foreach (var hotkey in hotkeys.LayoutHotkeys.OrderBy(h => h.Key)) + { + sb.AppendLine(CultureInfo.InvariantCulture, $" [{hotkey.Key}] => {hotkey.LayoutId}"); + } + + return sb.ToString().TrimEnd(); + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetLayoutsCommand.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetLayoutsCommand.cs new file mode 100644 index 0000000000..fc754fb95b --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetLayoutsCommand.cs @@ -0,0 +1,110 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.CommandLine.Invocation; +using System.Globalization; +using System.Text.Json; + +using FancyZonesEditorCommon.Data; +using FancyZonesEditorCommon.Utils; + +namespace FancyZonesCLI.CommandLine.Commands; + +internal sealed partial class GetLayoutsCommand : FancyZonesBaseCommand +{ + public GetLayoutsCommand() + : base("get-layouts", "List available layouts") + { + AddAlias("ls"); + } + + protected override string Execute(InvocationContext context) + { + var sb = new System.Text.StringBuilder(); + + // Print template layouts. + var templatesJson = FancyZonesDataIO.ReadLayoutTemplates(); + + if (templatesJson.LayoutTemplates != null) + { + sb.AppendLine(CultureInfo.InvariantCulture, $"=== Built-in Template Layouts ({templatesJson.LayoutTemplates.Count} total) ===\n"); + + for (int i = 0; i < templatesJson.LayoutTemplates.Count; i++) + { + var template = templatesJson.LayoutTemplates[i]; + sb.AppendLine(CultureInfo.InvariantCulture, $"[T{i + 1}] {template.Type}"); + sb.Append(CultureInfo.InvariantCulture, $" Zones: {template.ZoneCount}"); + if (template.ShowSpacing && template.Spacing > 0) + { + sb.Append(CultureInfo.InvariantCulture, $", Spacing: {template.Spacing}px"); + } + + sb.AppendLine(); + sb.AppendLine(); + + // Draw visual preview. + sb.Append(LayoutVisualizer.DrawTemplateLayout(template)); + + if (i < templatesJson.LayoutTemplates.Count - 1) + { + sb.AppendLine(); + } + } + + sb.AppendLine("\n"); + } + + // Print custom layouts. + var customLayouts = FancyZonesDataIO.ReadCustomLayouts(); + + if (customLayouts.CustomLayouts != null) + { + sb.AppendLine(CultureInfo.InvariantCulture, $"=== Custom Layouts ({customLayouts.CustomLayouts.Count} total) ==="); + + for (int i = 0; i < customLayouts.CustomLayouts.Count; i++) + { + var layout = customLayouts.CustomLayouts[i]; + sb.AppendLine(CultureInfo.InvariantCulture, $"[{i + 1}] {layout.Name}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" UUID: {layout.Uuid}"); + sb.Append(CultureInfo.InvariantCulture, $" Type: {layout.Type}"); + + bool isCanvasLayout = false; + if (layout.Info.ValueKind != JsonValueKind.Undefined && layout.Info.ValueKind != JsonValueKind.Null) + { + if (layout.Type == "grid" && layout.Info.TryGetProperty("rows", out var rows) && layout.Info.TryGetProperty("columns", out var cols)) + { + sb.Append(CultureInfo.InvariantCulture, $" ({rows.GetInt32()}x{cols.GetInt32()} grid)"); + } + else if (layout.Type == "canvas" && layout.Info.TryGetProperty("zones", out var zones)) + { + sb.Append(CultureInfo.InvariantCulture, $" ({zones.GetArrayLength()} zones)"); + isCanvasLayout = true; + } + } + + sb.AppendLine("\n"); + + // Draw visual preview. + sb.Append(LayoutVisualizer.DrawCustomLayout(layout)); + + // Add note for canvas layouts. + if (isCanvasLayout) + { + sb.AppendLine("\n Note: Canvas layout preview is approximate."); + sb.AppendLine(" Open FancyZones Editor for precise zone boundaries."); + } + + if (i < customLayouts.CustomLayouts.Count - 1) + { + sb.AppendLine(); + } + } + + sb.AppendLine("\nUse 'FancyZonesCLI.exe set-layout <UUID>' to apply a layout."); + } + + return sb.ToString().TrimEnd(); + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetMonitorsCommand.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetMonitorsCommand.cs new file mode 100644 index 0000000000..f8e8ebb189 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/GetMonitorsCommand.cs @@ -0,0 +1,95 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.CommandLine.Invocation; +using System.Globalization; + +using FancyZonesCLI.Utils; +using FancyZonesEditorCommon.Data; +using FancyZonesEditorCommon.Utils; + +namespace FancyZonesCLI.CommandLine.Commands; + +internal sealed partial class GetMonitorsCommand : FancyZonesBaseCommand +{ + public GetMonitorsCommand() + : base("get-monitors", "List monitors and FancyZones metadata") + { + AddAlias("m"); + } + + protected override string Execute(InvocationContext context) + { + // Request FancyZones to save current monitor configuration and read it reliably. + EditorParameters.ParamsWrapper editorParams; + try + { + editorParams = EditorParametersRefresh.ReadEditorParametersWithRefresh( + () => NativeMethods.NotifyFancyZones(NativeMethods.WM_PRIV_SAVE_EDITOR_PARAMETERS)); + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed to read monitor information. {ex.Message}{Environment.NewLine}Note: Ensure FancyZones is running to get current monitor information.", ex); + } + + if (editorParams.Monitors == null || editorParams.Monitors.Count == 0) + { + return "No monitors found."; + } + + // Also read applied layouts to show which layout is active on each monitor. + var appliedLayouts = FancyZonesDataIO.ReadAppliedLayouts(); + + var sb = new System.Text.StringBuilder(); + sb.AppendLine(CultureInfo.InvariantCulture, $"=== Monitors ({editorParams.Monitors.Count} total) ==="); + sb.AppendLine(); + + for (int i = 0; i < editorParams.Monitors.Count; i++) + { + var monitor = editorParams.Monitors[i]; + var monitorNum = i + 1; + + sb.AppendLine(CultureInfo.InvariantCulture, $"Monitor {monitorNum}:"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Monitor: {monitor.Monitor}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Monitor Instance: {monitor.MonitorInstanceId}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Monitor Number: {monitor.MonitorNumber}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Serial Number: {monitor.MonitorSerialNumber}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Virtual Desktop: {monitor.VirtualDesktop}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" DPI: {monitor.Dpi}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Resolution: {monitor.MonitorWidth}x{monitor.MonitorHeight}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Work Area: {monitor.WorkAreaWidth}x{monitor.WorkAreaHeight}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Position: ({monitor.LeftCoordinate}, {monitor.TopCoordinate})"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Selected: {monitor.IsSelected}"); + + // Find matching applied layout for this monitor using EditorCommon's matching logic. + if (appliedLayouts.AppliedLayouts != null) + { + var matchedLayout = AppliedLayoutsHelper.FindLayoutForMonitor( + appliedLayouts, + monitor.Monitor, + monitor.MonitorSerialNumber, + monitor.MonitorNumber, + monitor.VirtualDesktop); + + if (matchedLayout != null && matchedLayout.Value.AppliedLayout.Type != null) + { + sb.AppendLine(); + sb.AppendLine(CultureInfo.InvariantCulture, $" Active Layout: {matchedLayout.Value.AppliedLayout.Type}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Zone Count: {matchedLayout.Value.AppliedLayout.ZoneCount}"); + sb.AppendLine(CultureInfo.InvariantCulture, $" Sensitivity Radius: {matchedLayout.Value.AppliedLayout.SensitivityRadius}px"); + if (!string.IsNullOrEmpty(matchedLayout.Value.AppliedLayout.Uuid) && + matchedLayout.Value.AppliedLayout.Uuid != "{00000000-0000-0000-0000-000000000000}") + { + sb.AppendLine(CultureInfo.InvariantCulture, $" Layout UUID: {matchedLayout.Value.AppliedLayout.Uuid}"); + } + } + } + + sb.AppendLine(); + } + + return sb.ToString().TrimEnd(); + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/OpenEditorCommand.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/OpenEditorCommand.cs new file mode 100644 index 0000000000..122d3a000f --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/OpenEditorCommand.cs @@ -0,0 +1,44 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.CommandLine.Invocation; +using System.Diagnostics; +using System.Linq; +using System.Threading; + +namespace FancyZonesCLI.CommandLine.Commands; + +internal sealed partial class OpenEditorCommand : FancyZonesBaseCommand +{ + public OpenEditorCommand() + : base("open-editor", "Launch FancyZones layout editor") + { + AddAlias("e"); + } + + protected override string Execute(InvocationContext context) + { + const string FancyZonesEditorToggleEventName = "Local\\FancyZones-ToggleEditorEvent-1e174338-06a3-472b-874d-073b21c62f14"; + + // Check if editor is already running + var existingProcess = Process.GetProcessesByName("PowerToys.FancyZonesEditor").FirstOrDefault(); + if (existingProcess != null) + { + NativeMethods.SetForegroundWindow(existingProcess.MainWindowHandle); + return string.Empty; + } + + try + { + using var eventHandle = new EventWaitHandle(false, EventResetMode.AutoReset, FancyZonesEditorToggleEventName); + eventHandle.Set(); + return string.Empty; + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed to request FancyZones Editor launch. {ex.Message}", ex); + } + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/OpenSettingsCommand.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/OpenSettingsCommand.cs new file mode 100644 index 0000000000..84698bb9b6 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/OpenSettingsCommand.cs @@ -0,0 +1,50 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.CommandLine.Invocation; +using System.Diagnostics; +using System.IO; + +namespace FancyZonesCLI.CommandLine.Commands; + +internal sealed partial class OpenSettingsCommand : FancyZonesBaseCommand +{ + public OpenSettingsCommand() + : base("open-settings", "Open FancyZones settings page") + { + AddAlias("settings"); + } + + protected override string Execute(InvocationContext context) + { + // Check in the same directory as the CLI (typical for dev builds) + var powertoysExe = Path.Combine(AppContext.BaseDirectory, "PowerToys.exe"); + if (!File.Exists(powertoysExe)) + { + throw new FileNotFoundException("PowerToys.exe not found. Ensure PowerToys is installed, or run the CLI from the same folder as PowerToys.exe.", powertoysExe); + } + + try + { + var process = Process.Start(new ProcessStartInfo + { + FileName = powertoysExe, + Arguments = "--open-settings=FancyZones", + UseShellExecute = false, + }); + + if (process == null) + { + throw new InvalidOperationException("PowerToys.exe failed to start."); + } + + return string.Empty; + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed to open FancyZones Settings. {ex.Message}", ex); + } + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/RemoveHotkeyCommand.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/RemoveHotkeyCommand.cs new file mode 100644 index 0000000000..4f88e268c5 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/RemoveHotkeyCommand.cs @@ -0,0 +1,55 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.CommandLine; +using System.CommandLine.Invocation; + +using FancyZonesEditorCommon.Data; +using FancyZonesEditorCommon.Utils; + +namespace FancyZonesCLI.CommandLine.Commands; + +internal sealed partial class RemoveHotkeyCommand : FancyZonesBaseCommand +{ + private readonly Argument<int> _key; + + public RemoveHotkeyCommand() + : base("remove-hotkey", "Remove hotkey assignment") + { + AddAlias("rhk"); + + _key = new Argument<int>("key", "Hotkey index (0-9)"); + AddArgument(_key); + } + + protected override string Execute(InvocationContext context) + { + // FancyZones running guard is handled by FancyZonesBaseCommand. + int key = context.ParseResult.GetValueForArgument(_key); + + var hotkeysWrapper = FancyZonesDataIO.ReadLayoutHotkeys(); + + if (hotkeysWrapper.LayoutHotkeys == null) + { + return "No hotkeys configured."; + } + + var hotkeysList = hotkeysWrapper.LayoutHotkeys; + var removed = hotkeysList.RemoveAll(h => h.Key == key); + if (removed == 0) + { + return $"No hotkey assigned to key {key}"; + } + + // Save. + var newWrapper = new LayoutHotkeys.LayoutHotkeysWrapper { LayoutHotkeys = hotkeysList }; + FancyZonesDataIO.WriteLayoutHotkeys(newWrapper); + + // Notify FancyZones. + NativeMethods.NotifyFancyZones(NativeMethods.WM_PRIV_LAYOUT_HOTKEYS_FILE_UPDATE); + + return string.Empty; + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/SetHotkeyCommand.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/SetHotkeyCommand.cs new file mode 100644 index 0000000000..4982be284c --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/SetHotkeyCommand.cs @@ -0,0 +1,89 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.Linq; + +using FancyZonesEditorCommon.Data; +using FancyZonesEditorCommon.Utils; + +namespace FancyZonesCLI.CommandLine.Commands; + +internal sealed partial class SetHotkeyCommand : FancyZonesBaseCommand +{ + private readonly Argument<int> _key; + private readonly Argument<string> _layout; + + public SetHotkeyCommand() + : base("set-hotkey", "Assign hotkey (0-9) to a custom layout") + { + AddAlias("shk"); + + _key = new Argument<int>("key", "Hotkey index (0-9)"); + _layout = new Argument<string>("layout", "Custom layout UUID"); + + AddArgument(_key); + AddArgument(_layout); + } + + protected override string Execute(InvocationContext context) + { + // FancyZones running guard is handled by FancyZonesBaseCommand. + int key = context.ParseResult.GetValueForArgument(_key); + string layout = context.ParseResult.GetValueForArgument(_layout); + + if (key < 0 || key > 9) + { + throw new InvalidOperationException("Key must be between 0 and 9."); + } + + // Editor only allows assigning hotkeys to existing custom layouts. + var customLayouts = FancyZonesDataIO.ReadCustomLayouts(); + + CustomLayouts.CustomLayoutWrapper? matchedLayout = null; + if (customLayouts.CustomLayouts != null) + { + foreach (var candidate in customLayouts.CustomLayouts) + { + if (candidate.Uuid.Equals(layout, StringComparison.OrdinalIgnoreCase)) + { + matchedLayout = candidate; + break; + } + } + } + + if (!matchedLayout.HasValue) + { + throw new InvalidOperationException($"Layout '{layout}' is not a custom layout UUID."); + } + + string layoutName = matchedLayout.Value.Name; + + var hotkeysWrapper = FancyZonesDataIO.ReadLayoutHotkeys(); + + var hotkeysList = hotkeysWrapper.LayoutHotkeys ?? new List<LayoutHotkeys.LayoutHotkeyWrapper>(); + + // Match editor behavior: + // - One key maps to one layout + // - One layout maps to at most one key + hotkeysList.RemoveAll(h => h.Key == key); + hotkeysList.RemoveAll(h => string.Equals(h.LayoutId, layout, StringComparison.OrdinalIgnoreCase)); + + // Add new hotkey. + hotkeysList.Add(new LayoutHotkeys.LayoutHotkeyWrapper { Key = key, LayoutId = layout }); + + // Save. + var newWrapper = new LayoutHotkeys.LayoutHotkeysWrapper { LayoutHotkeys = hotkeysList }; + FancyZonesDataIO.WriteLayoutHotkeys(newWrapper); + + // Notify FancyZones. + NativeMethods.NotifyFancyZones(NativeMethods.WM_PRIV_LAYOUT_HOTKEYS_FILE_UPDATE); + + return string.Empty; + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/SetLayoutCommand.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/SetLayoutCommand.cs new file mode 100644 index 0000000000..3d2a204af9 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/Commands/SetLayoutCommand.cs @@ -0,0 +1,374 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.Globalization; + +using FancyZonesCLI.Utils; +using FancyZonesEditorCommon.Data; +using FancyZonesEditorCommon.Utils; + +namespace FancyZonesCLI.CommandLine.Commands; + +internal sealed partial class SetLayoutCommand : FancyZonesBaseCommand +{ + private static readonly string[] AliasesMonitor = ["--monitor", "-m"]; + private static readonly string[] AliasesAll = ["--all", "-a"]; + + private const string DefaultLayoutUuid = "{00000000-0000-0000-0000-000000000000}"; + + private readonly Argument<string> _layoutId; + private readonly Option<int?> _monitor; + private readonly Option<bool> _all; + + public SetLayoutCommand() + : base("set-layout", "Set layout by UUID or template name") + { + AddAlias("s"); + + _layoutId = new Argument<string>("layout", "Layout UUID or template type (e.g. focus, columns)"); + AddArgument(_layoutId); + + _monitor = new Option<int?>(AliasesMonitor, "Apply to monitor N (1-based)"); + _monitor.AddValidator(result => + { + if (result.Tokens.Count == 0) + { + return; + } + + int? monitor = result.GetValueOrDefault<int?>(); + if (monitor.HasValue && monitor.Value < 1) + { + result.ErrorMessage = "Monitor index must be >= 1."; + } + }); + + _all = new Option<bool>(AliasesAll, "Apply to all monitors"); + + AddOption(_monitor); + AddOption(_all); + + AddValidator(commandResult => + { + int? monitor = commandResult.GetValueForOption(_monitor); + bool all = commandResult.GetValueForOption(_all); + + if (monitor.HasValue && all) + { + commandResult.ErrorMessage = "Cannot specify both --monitor and --all."; + } + }); + } + + protected override string Execute(InvocationContext context) + { + // FancyZones running guard is handled by FancyZonesBaseCommand. + string layout = context.ParseResult.GetValueForArgument(_layoutId); + int? monitor = context.ParseResult.GetValueForOption(_monitor); + bool all = context.ParseResult.GetValueForOption(_all); + Logger.LogInfo($"SetLayout called with layout: '{layout}', monitor: {(monitor.HasValue ? monitor.Value.ToString(CultureInfo.InvariantCulture) : "<default>")}, all: {all}"); + + var (targetCustomLayout, targetTemplate) = ResolveTargetLayout(layout); + + var editorParams = ReadEditorParametersWithRefresh(); + var appliedLayouts = FancyZonesDataIO.ReadAppliedLayouts(); + appliedLayouts.AppliedLayouts ??= new List<AppliedLayouts.AppliedLayoutWrapper>(); + + List<int> monitorsToUpdate = GetMonitorsToUpdate(editorParams, monitor, all); + List<AppliedLayouts.AppliedLayoutWrapper> newLayouts = BuildNewLayouts(editorParams, monitorsToUpdate, targetCustomLayout, targetTemplate); + var updatedLayouts = MergeWithHistoricalLayouts(appliedLayouts, newLayouts); + + Logger.LogInfo($"Writing {updatedLayouts.AppliedLayouts?.Count ?? 0} layouts to file"); + FancyZonesDataIO.WriteAppliedLayouts(updatedLayouts); + Logger.LogInfo($"Applied layouts file updated for {monitorsToUpdate.Count} monitor(s)"); + + NativeMethods.NotifyFancyZones(NativeMethods.WM_PRIV_APPLIED_LAYOUTS_FILE_UPDATE); + Logger.LogInfo("FancyZones notified of layout change"); + + return BuildSuccessMessage(layout, monitor, all); + } + + private static string BuildSuccessMessage(string layout, int? monitor, bool all) + { + if (all) + { + return string.Format(CultureInfo.InvariantCulture, "Layout '{0}' applied to all monitors.", layout); + } + + if (monitor.HasValue) + { + return string.Format(CultureInfo.InvariantCulture, "Layout '{0}' applied to monitor {1}.", layout, monitor.Value); + } + + return string.Format(CultureInfo.InvariantCulture, "Layout '{0}' applied to monitor 1.", layout); + } + + private static (CustomLayouts.CustomLayoutWrapper? TargetCustomLayout, LayoutTemplates.TemplateLayoutWrapper? TargetTemplate) ResolveTargetLayout(string layout) + { + var customLayouts = FancyZonesDataIO.ReadCustomLayouts(); + CustomLayouts.CustomLayoutWrapper? targetCustomLayout = FindCustomLayout(customLayouts, layout); + + LayoutTemplates.TemplateLayoutWrapper? targetTemplate = null; + if (!targetCustomLayout.HasValue || string.IsNullOrEmpty(targetCustomLayout.Value.Uuid)) + { + var templates = FancyZonesDataIO.ReadLayoutTemplates(); + targetTemplate = FindTemplate(templates, layout); + + if (targetCustomLayout.HasValue && string.IsNullOrEmpty(targetCustomLayout.Value.Uuid)) + { + targetCustomLayout = null; + } + } + + if (!targetCustomLayout.HasValue && !targetTemplate.HasValue) + { + throw new InvalidOperationException( + $"Layout '{layout}' not found{Environment.NewLine}" + + "Tip: For templates, use the type name (e.g., 'focus', 'columns', 'rows', 'grid', 'priority-grid')" + + $"{Environment.NewLine} For custom layouts, use the UUID from 'get-layouts'"); + } + + return (targetCustomLayout, targetTemplate); + } + + private static CustomLayouts.CustomLayoutWrapper? FindCustomLayout(CustomLayouts.CustomLayoutListWrapper customLayouts, string layout) + { + if (customLayouts.CustomLayouts == null) + { + return null; + } + + foreach (var customLayout in customLayouts.CustomLayouts) + { + if (customLayout.Uuid.Equals(layout, StringComparison.OrdinalIgnoreCase)) + { + return customLayout; + } + } + + return null; + } + + private static LayoutTemplates.TemplateLayoutWrapper? FindTemplate(LayoutTemplates.TemplateLayoutsListWrapper templates, string layout) + { + if (templates.LayoutTemplates == null) + { + return null; + } + + foreach (var template in templates.LayoutTemplates) + { + if (template.Type.Equals(layout, StringComparison.OrdinalIgnoreCase)) + { + return template; + } + } + + return null; + } + + private static EditorParameters.ParamsWrapper ReadEditorParametersWithRefresh() + { + return EditorParametersRefresh.ReadEditorParametersWithRefresh( + () => NativeMethods.NotifyFancyZones(NativeMethods.WM_PRIV_SAVE_EDITOR_PARAMETERS)); + } + + private static List<int> GetMonitorsToUpdate(EditorParameters.ParamsWrapper editorParams, int? monitor, bool all) + { + var result = new List<int>(); + + if (all) + { + for (int i = 0; i < editorParams.Monitors.Count; i++) + { + result.Add(i); + } + + return result; + } + + if (monitor.HasValue) + { + int monitorIndex = monitor.Value - 1; // Convert to 0-based. + if (monitorIndex < 0 || monitorIndex >= editorParams.Monitors.Count) + { + throw new InvalidOperationException($"Monitor {monitor.Value} not found. Available monitors: 1-{editorParams.Monitors.Count}"); + } + + result.Add(monitorIndex); + return result; + } + + // Default: first monitor. + result.Add(0); + return result; + } + + private static List<AppliedLayouts.AppliedLayoutWrapper> BuildNewLayouts( + EditorParameters.ParamsWrapper editorParams, + List<int> monitorsToUpdate, + CustomLayouts.CustomLayoutWrapper? targetCustomLayout, + LayoutTemplates.TemplateLayoutWrapper? targetTemplate) + { + var newLayouts = new List<AppliedLayouts.AppliedLayoutWrapper>(); + + foreach (int monitorIndex in monitorsToUpdate) + { + var currentMonitor = editorParams.Monitors[monitorIndex]; + + var (layoutUuid, layoutType, showSpacing, spacing, zoneCount, sensitivityRadius) = + GetLayoutSettings(targetCustomLayout, targetTemplate); + + var deviceId = new AppliedLayouts.AppliedLayoutWrapper.DeviceIdWrapper + { + Monitor = currentMonitor.Monitor, + MonitorInstance = currentMonitor.MonitorInstanceId, + MonitorNumber = currentMonitor.MonitorNumber, + SerialNumber = currentMonitor.MonitorSerialNumber, + VirtualDesktop = currentMonitor.VirtualDesktop, + }; + + newLayouts.Add(new AppliedLayouts.AppliedLayoutWrapper + { + Device = deviceId, + AppliedLayout = new AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper + { + Uuid = layoutUuid, + Type = layoutType, + ShowSpacing = showSpacing, + Spacing = spacing, + ZoneCount = zoneCount, + SensitivityRadius = sensitivityRadius, + }, + }); + } + + if (newLayouts.Count == 0) + { + throw new InvalidOperationException("Internal error - no monitors to update."); + } + + return newLayouts; + } + + private static (string LayoutUuid, string LayoutType, bool ShowSpacing, int Spacing, int ZoneCount, int SensitivityRadius) GetLayoutSettings( + CustomLayouts.CustomLayoutWrapper? targetCustomLayout, + LayoutTemplates.TemplateLayoutWrapper? targetTemplate) + { + if (targetCustomLayout.HasValue) + { + var customLayoutsSerializer = new CustomLayouts(); + string type = targetCustomLayout.Value.Type?.ToLowerInvariant() ?? string.Empty; + + bool showSpacing = false; + int spacing = 0; + int zoneCount = 0; + int sensitivityRadius = 20; + + if (type == "canvas") + { + var info = customLayoutsSerializer.CanvasFromJsonElement(targetCustomLayout.Value.Info.GetRawText()); + zoneCount = info.Zones?.Count ?? 0; + sensitivityRadius = info.SensitivityRadius; + } + else if (type == "grid") + { + var info = customLayoutsSerializer.GridFromJsonElement(targetCustomLayout.Value.Info.GetRawText()); + showSpacing = info.ShowSpacing; + spacing = info.Spacing; + sensitivityRadius = info.SensitivityRadius; + + if (info.CellChildMap != null) + { + var uniqueZoneIds = new HashSet<int>(); + + for (int r = 0; r < info.CellChildMap.Length; r++) + { + int[] row = info.CellChildMap[r]; + if (row == null) + { + continue; + } + + for (int c = 0; c < row.Length; c++) + { + uniqueZoneIds.Add(row[c]); + } + } + + zoneCount = uniqueZoneIds.Count; + } + } + else + { + throw new InvalidOperationException($"Unsupported custom layout type '{targetCustomLayout.Value.Type}'."); + } + + return ( + targetCustomLayout.Value.Uuid, + Constants.CustomLayoutJsonTag, + ShowSpacing: showSpacing, + Spacing: spacing, + ZoneCount: zoneCount, + SensitivityRadius: sensitivityRadius); + } + + if (targetTemplate.HasValue) + { + return ( + DefaultLayoutUuid, + targetTemplate.Value.Type, + targetTemplate.Value.ShowSpacing, + targetTemplate.Value.Spacing, + targetTemplate.Value.ZoneCount, + targetTemplate.Value.SensitivityRadius); + } + + throw new InvalidOperationException("Internal error - no layout selected."); + } + + private static AppliedLayouts.AppliedLayoutsListWrapper MergeWithHistoricalLayouts( + AppliedLayouts.AppliedLayoutsListWrapper existingLayouts, + List<AppliedLayouts.AppliedLayoutWrapper> newLayouts) + { + var mergedLayoutsList = new List<AppliedLayouts.AppliedLayoutWrapper>(); + mergedLayoutsList.AddRange(newLayouts); + + if (existingLayouts.AppliedLayouts != null) + { + foreach (var existingLayout in existingLayouts.AppliedLayouts) + { + bool isUpdated = false; + + foreach (var newLayout in newLayouts) + { + if (AppliedLayoutsHelper.MatchesDevice( + existingLayout.Device, + newLayout.Device.Monitor, + newLayout.Device.SerialNumber, + newLayout.Device.MonitorNumber, + newLayout.Device.VirtualDesktop)) + { + isUpdated = true; + break; + } + } + + if (!isUpdated) + { + mergedLayoutsList.Add(existingLayout); + } + } + } + + return new AppliedLayouts.AppliedLayoutsListWrapper + { + AppliedLayouts = mergedLayoutsList, + }; + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/FancyZonesCliCommandFactory.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/FancyZonesCliCommandFactory.cs new file mode 100644 index 0000000000..864e221b34 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/FancyZonesCliCommandFactory.cs @@ -0,0 +1,28 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.CommandLine; +using FancyZonesCLI.CommandLine.Commands; + +namespace FancyZonesCLI.CommandLine; + +internal static class FancyZonesCliCommandFactory +{ + public static RootCommand CreateRootCommand() + { + var root = new RootCommand("FancyZones CLI - Command line interface for FancyZones"); + + root.AddCommand(new OpenEditorCommand()); + root.AddCommand(new GetMonitorsCommand()); + root.AddCommand(new GetLayoutsCommand()); + root.AddCommand(new GetActiveLayoutCommand()); + root.AddCommand(new SetLayoutCommand()); + root.AddCommand(new OpenSettingsCommand()); + root.AddCommand(new GetHotkeysCommand()); + root.AddCommand(new SetHotkeyCommand()); + root.AddCommand(new RemoveHotkeyCommand()); + + return root; + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/FancyZonesCliGuards.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/FancyZonesCliGuards.cs new file mode 100644 index 0000000000..e80af07c15 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/FancyZonesCliGuards.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Diagnostics; + +namespace FancyZonesCLI.CommandLine; + +internal static class FancyZonesCliGuards +{ + public static bool IsFancyZonesRunning() + { + try + { + return Process.GetProcessesByName("PowerToys.FancyZones").Length != 0; + } + catch + { + return false; + } + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/CommandLine/FancyZonesCliUsage.cs b/src/modules/fancyzones/FancyZonesCLI/CommandLine/FancyZonesCliUsage.cs new file mode 100644 index 0000000000..2d18624507 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/CommandLine/FancyZonesCliUsage.cs @@ -0,0 +1,62 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.CommandLine; +using System.Linq; + +namespace FancyZonesCLI.CommandLine; + +internal static class FancyZonesCliUsage +{ + public static void PrintUsage() + { + Console.OutputEncoding = System.Text.Encoding.UTF8; + Console.WriteLine("FancyZones CLI - Command line interface for FancyZones"); + Console.WriteLine(); + + var cmd = FancyZonesCliCommandFactory.CreateRootCommand(); + + Console.WriteLine("Usage: FancyZonesCLI [command] [options]"); + Console.WriteLine(); + + Console.WriteLine("Options:"); + foreach (var option in cmd.Options) + { + var aliases = string.Join(", ", option.Aliases); + var description = option.Description ?? string.Empty; + Console.WriteLine($" {aliases,-30} {description}"); + } + + Console.WriteLine(); + Console.WriteLine("Commands:"); + foreach (var command in cmd.Subcommands) + { + if (command.IsHidden) + { + continue; + } + + // Format: "command-name <args>, alias" + string argsLabel = string.Join(" ", command.Arguments.Select(a => $"<{a.Name}>")); + string baseLabel = string.IsNullOrEmpty(argsLabel) ? command.Name : $"{command.Name} {argsLabel}"; + + // Find first alias (Aliases includes Name) + string alias = command.Aliases.FirstOrDefault(a => !string.Equals(a, command.Name, StringComparison.OrdinalIgnoreCase)); + string label = string.IsNullOrEmpty(alias) ? baseLabel : $"{baseLabel}, {alias}"; + + var description = command.Description ?? string.Empty; + Console.WriteLine($" {label,-30} {description}"); + } + + Console.WriteLine(); + Console.WriteLine("Examples:"); + Console.WriteLine(" FancyZonesCLI --help"); + Console.WriteLine(" FancyZonesCLI --version"); + Console.WriteLine(" FancyZonesCLI get-monitors"); + Console.WriteLine(" FancyZonesCLI set-layout focus"); + Console.WriteLine(" FancyZonesCLI set-layout <uuid> --monitor 1"); + Console.WriteLine(" FancyZonesCLI get-hotkeys"); + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/Commands/EditorCommands.cs b/src/modules/fancyzones/FancyZonesCLI/Commands/EditorCommands.cs deleted file mode 100644 index 7bf15dda44..0000000000 --- a/src/modules/fancyzones/FancyZonesCLI/Commands/EditorCommands.cs +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics; -using System.IO; -using System.Linq; - -namespace FancyZonesCLI.Commands; - -/// <summary> -/// Editor and Settings commands. -/// </summary> -internal static class EditorCommands -{ - public static (int ExitCode, string Output) OpenEditor() - { - var editorExe = "PowerToys.FancyZonesEditor.exe"; - - // Check if editor-parameters.json exists - if (!FancyZonesData.EditorParametersExist()) - { - return (1, "Error: editor-parameters.json not found.\nPlease launch FancyZones Editor using Win+` (Win+Backtick) hotkey first."); - } - - // Check if editor is already running - var existingProcess = Process.GetProcessesByName("PowerToys.FancyZonesEditor").FirstOrDefault(); - if (existingProcess != null) - { - NativeMethods.SetForegroundWindow(existingProcess.MainWindowHandle); - return (0, "FancyZones Editor is already running. Brought window to foreground."); - } - - // Only check same directory as CLI - var editorPath = Path.Combine(AppContext.BaseDirectory, editorExe); - - if (File.Exists(editorPath)) - { - try - { - Process.Start(new ProcessStartInfo - { - FileName = editorPath, - UseShellExecute = true, - }); - return (0, "FancyZones Editor launched successfully."); - } - catch (Exception ex) - { - return (1, $"Failed to launch: {ex.Message}"); - } - } - - return (1, $"Error: Could not find {editorExe} in {AppContext.BaseDirectory}"); - } - - public static (int ExitCode, string Output) OpenSettings() - { - try - { - // Find PowerToys.exe in common locations - string powertoysExe = null; - - // Check in the same directory as the CLI (typical for dev builds) - var sameDirPath = Path.Combine(AppContext.BaseDirectory, "PowerToys.exe"); - if (File.Exists(sameDirPath)) - { - powertoysExe = sameDirPath; - } - - if (powertoysExe == null) - { - return (1, "Error: PowerToys.exe not found. Please ensure PowerToys is installed."); - } - - Process.Start(new ProcessStartInfo - { - FileName = powertoysExe, - Arguments = "--open-settings=FancyZones", - UseShellExecute = false, - }); - return (0, "FancyZones Settings opened successfully."); - } - catch (Exception ex) - { - return (1, $"Error: Failed to open FancyZones Settings. {ex.Message}"); - } - } -} diff --git a/src/modules/fancyzones/FancyZonesCLI/Commands/HotkeyCommands.cs b/src/modules/fancyzones/FancyZonesCLI/Commands/HotkeyCommands.cs deleted file mode 100644 index cfaf93a5d4..0000000000 --- a/src/modules/fancyzones/FancyZonesCLI/Commands/HotkeyCommands.cs +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; - -namespace FancyZonesCLI.Commands; - -/// <summary> -/// Hotkey-related commands. -/// </summary> -internal static class HotkeyCommands -{ - public static (int ExitCode, string Output) GetHotkeys() - { - var hotkeys = FancyZonesData.ReadLayoutHotkeys(); - if (hotkeys?.Hotkeys == null || hotkeys.Hotkeys.Count == 0) - { - return (0, "No hotkeys configured."); - } - - var sb = new System.Text.StringBuilder(); - sb.AppendLine("=== Layout Hotkeys ===\n"); - sb.AppendLine("Press Win + Ctrl + Alt + <number> to switch layouts:\n"); - - foreach (var hotkey in hotkeys.Hotkeys.OrderBy(h => h.Key)) - { - sb.AppendLine(CultureInfo.InvariantCulture, $" [{hotkey.Key}] => {hotkey.LayoutId}"); - } - - return (0, sb.ToString().TrimEnd()); - } - - public static (int ExitCode, string Output) SetHotkey(int key, string layoutUuid, Action<uint> notifyFancyZones, uint wmPrivLayoutHotkeysFileUpdate) - { - if (key < 0 || key > 9) - { - return (1, "Error: Key must be between 0 and 9"); - } - - // Check if this is a custom layout UUID - var customLayouts = FancyZonesData.ReadCustomLayouts(); - var matchedLayout = customLayouts?.Layouts?.FirstOrDefault(l => l.Uuid.Equals(layoutUuid, StringComparison.OrdinalIgnoreCase)); - bool isCustomLayout = matchedLayout != null; - string layoutName = matchedLayout?.Name ?? layoutUuid; - - var hotkeys = FancyZonesData.ReadLayoutHotkeys() ?? new LayoutHotkeys(); - - hotkeys.Hotkeys ??= new List<LayoutHotkey>(); - - // Remove existing hotkey for this key - hotkeys.Hotkeys.RemoveAll(h => h.Key == key); - - // Add new hotkey - hotkeys.Hotkeys.Add(new LayoutHotkey { Key = key, LayoutId = layoutUuid }); - - // Save - FancyZonesData.WriteLayoutHotkeys(hotkeys); - - // Notify FancyZones - notifyFancyZones(wmPrivLayoutHotkeysFileUpdate); - - if (isCustomLayout) - { - return (0, $"✓ Hotkey {key} assigned to custom layout '{layoutName}'\n Press Win + Ctrl + Alt + {key} to switch to this layout"); - } - else - { - return (0, $"⚠ Warning: Hotkey {key} assigned to '{layoutUuid}'\n Note: FancyZones hotkeys only work with CUSTOM layouts.\n Template layouts (focus, columns, rows, etc.) cannot be used with hotkeys.\n Create a custom layout in the FancyZones Editor to use this hotkey."); - } - } - - public static (int ExitCode, string Output) RemoveHotkey(int key, Action<uint> notifyFancyZones, uint wmPrivLayoutHotkeysFileUpdate) - { - var hotkeys = FancyZonesData.ReadLayoutHotkeys(); - if (hotkeys?.Hotkeys == null) - { - return (0, $"No hotkey assigned to key {key}"); - } - - var removed = hotkeys.Hotkeys.RemoveAll(h => h.Key == key); - if (removed == 0) - { - return (0, $"No hotkey assigned to key {key}"); - } - - // Save - FancyZonesData.WriteLayoutHotkeys(hotkeys); - - // Notify FancyZones - notifyFancyZones(wmPrivLayoutHotkeysFileUpdate); - - return (0, $"Hotkey {key} removed"); - } -} diff --git a/src/modules/fancyzones/FancyZonesCLI/Commands/LayoutCommands.cs b/src/modules/fancyzones/FancyZonesCLI/Commands/LayoutCommands.cs deleted file mode 100644 index 4400b32d46..0000000000 --- a/src/modules/fancyzones/FancyZonesCLI/Commands/LayoutCommands.cs +++ /dev/null @@ -1,276 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Text.Json; - -namespace FancyZonesCLI.Commands; - -/// <summary> -/// Layout-related commands. -/// </summary> -internal static class LayoutCommands -{ - public static (int ExitCode, string Output) GetLayouts() - { - var sb = new System.Text.StringBuilder(); - - // Print template layouts - var templatesJson = FancyZonesData.ReadLayoutTemplates(); - if (templatesJson?.Templates != null) - { - sb.AppendLine(CultureInfo.InvariantCulture, $"=== Built-in Template Layouts ({templatesJson.Templates.Count} total) ===\n"); - - for (int i = 0; i < templatesJson.Templates.Count; i++) - { - var template = templatesJson.Templates[i]; - sb.AppendLine(CultureInfo.InvariantCulture, $"[T{i + 1}] {template.Type}"); - sb.Append(CultureInfo.InvariantCulture, $" Zones: {template.ZoneCount}"); - if (template.ShowSpacing && template.Spacing > 0) - { - sb.Append(CultureInfo.InvariantCulture, $", Spacing: {template.Spacing}px"); - } - - sb.AppendLine(); - sb.AppendLine(); - - // Draw visual preview - sb.Append(LayoutVisualizer.DrawTemplateLayout(template)); - - if (i < templatesJson.Templates.Count - 1) - { - sb.AppendLine(); - } - } - - sb.AppendLine("\n"); - } - - // Print custom layouts - var customLayouts = FancyZonesData.ReadCustomLayouts(); - if (customLayouts?.Layouts != null) - { - sb.AppendLine(CultureInfo.InvariantCulture, $"=== Custom Layouts ({customLayouts.Layouts.Count} total) ==="); - - for (int i = 0; i < customLayouts.Layouts.Count; i++) - { - var layout = customLayouts.Layouts[i]; - sb.AppendLine(CultureInfo.InvariantCulture, $"[{i + 1}] {layout.Name}"); - sb.AppendLine(CultureInfo.InvariantCulture, $" UUID: {layout.Uuid}"); - sb.Append(CultureInfo.InvariantCulture, $" Type: {layout.Type}"); - - bool isCanvasLayout = false; - if (layout.Info.ValueKind != JsonValueKind.Undefined && layout.Info.ValueKind != JsonValueKind.Null) - { - if (layout.Type == "grid" && layout.Info.TryGetProperty("rows", out var rows) && layout.Info.TryGetProperty("columns", out var cols)) - { - sb.Append(CultureInfo.InvariantCulture, $" ({rows.GetInt32()}x{cols.GetInt32()} grid)"); - } - else if (layout.Type == "canvas" && layout.Info.TryGetProperty("zones", out var zones)) - { - sb.Append(CultureInfo.InvariantCulture, $" ({zones.GetArrayLength()} zones)"); - isCanvasLayout = true; - } - } - - sb.AppendLine("\n"); - - // Draw visual preview - sb.Append(LayoutVisualizer.DrawCustomLayout(layout)); - - // Add note for canvas layouts - if (isCanvasLayout) - { - sb.AppendLine("\n Note: Canvas layout preview is approximate."); - sb.AppendLine(" Open FancyZones Editor for precise zone boundaries."); - } - - if (i < customLayouts.Layouts.Count - 1) - { - sb.AppendLine(); - } - } - - sb.AppendLine("\nUse 'FancyZonesCLI.exe set-layout <UUID>' to apply a layout."); - } - - return (0, sb.ToString().TrimEnd()); - } - - public static (int ExitCode, string Output) GetActiveLayout() - { - if (!FancyZonesData.TryReadAppliedLayouts(out var appliedLayouts, out var error)) - { - return (1, $"Error: {error}"); - } - - if (appliedLayouts.Layouts == null || appliedLayouts.Layouts.Count == 0) - { - return (0, "No active layouts found."); - } - - var sb = new System.Text.StringBuilder(); - sb.AppendLine("\n=== Active FancyZones Layout(s) ===\n"); - - for (int i = 0; i < appliedLayouts.Layouts.Count; i++) - { - var layout = appliedLayouts.Layouts[i]; - sb.AppendLine(CultureInfo.InvariantCulture, $"Monitor {i + 1}:"); - sb.AppendLine(CultureInfo.InvariantCulture, $" Name: {layout.AppliedLayout.Type}"); - sb.AppendLine(CultureInfo.InvariantCulture, $" UUID: {layout.AppliedLayout.Uuid}"); - sb.AppendLine(CultureInfo.InvariantCulture, $" Type: {layout.AppliedLayout.Type} ({layout.AppliedLayout.ZoneCount} zones)"); - - if (layout.AppliedLayout.ShowSpacing) - { - sb.AppendLine(CultureInfo.InvariantCulture, $" Spacing: {layout.AppliedLayout.Spacing}px"); - } - - sb.AppendLine(CultureInfo.InvariantCulture, $" Sensitivity Radius: {layout.AppliedLayout.SensitivityRadius}px"); - - if (i < appliedLayouts.Layouts.Count - 1) - { - sb.AppendLine(); - } - } - - return (0, sb.ToString().TrimEnd()); - } - - public static (int ExitCode, string Output) SetLayout(string[] args, Action<uint> notifyFancyZones, uint wmPrivAppliedLayoutsFileUpdate) - { - Logger.LogInfo($"SetLayout called with args: [{string.Join(", ", args)}]"); - - if (args.Length == 0) - { - return (1, "Error: set-layout requires a UUID parameter"); - } - - string uuid = args[0]; - int? targetMonitor = null; - bool applyToAll = false; - - // Parse options - for (int i = 1; i < args.Length; i++) - { - if (args[i] == "--monitor" && i + 1 < args.Length) - { - if (int.TryParse(args[i + 1], out int monitorNum)) - { - targetMonitor = monitorNum; - i++; // Skip next arg - } - else - { - return (1, $"Error: Invalid monitor number: {args[i + 1]}"); - } - } - else if (args[i] == "--all") - { - applyToAll = true; - } - } - - if (targetMonitor.HasValue && applyToAll) - { - return (1, "Error: Cannot specify both --monitor and --all"); - } - - // Try to find layout in custom layouts first (by UUID) - var customLayouts = FancyZonesData.ReadCustomLayouts(); - var targetCustomLayout = customLayouts?.Layouts?.FirstOrDefault(l => l.Uuid.Equals(uuid, StringComparison.OrdinalIgnoreCase)); - - // If not found in custom layouts, try template layouts (by type name) - TemplateLayout targetTemplate = null; - if (targetCustomLayout == null) - { - var templates = FancyZonesData.ReadLayoutTemplates(); - targetTemplate = templates?.Templates?.FirstOrDefault(t => t.Type.Equals(uuid, StringComparison.OrdinalIgnoreCase)); - } - - if (targetCustomLayout == null && targetTemplate == null) - { - return (1, $"Error: Layout '{uuid}' not found\nTip: For templates, use the type name (e.g., 'focus', 'columns', 'rows', 'grid', 'priority-grid')\n For custom layouts, use the UUID from 'get-layouts'"); - } - - // Read current applied layouts - if (!FancyZonesData.TryReadAppliedLayouts(out var appliedLayouts, out var error)) - { - return (1, $"Error: {error}"); - } - - if (appliedLayouts.Layouts == null || appliedLayouts.Layouts.Count == 0) - { - return (1, "Error: No monitors configured"); - } - - // Determine which monitors to update - List<int> monitorsToUpdate = new List<int>(); - if (applyToAll) - { - for (int i = 0; i < appliedLayouts.Layouts.Count; i++) - { - monitorsToUpdate.Add(i); - } - } - else if (targetMonitor.HasValue) - { - int monitorIndex = targetMonitor.Value - 1; // Convert to 0-based - if (monitorIndex < 0 || monitorIndex >= appliedLayouts.Layouts.Count) - { - return (1, $"Error: Monitor {targetMonitor.Value} not found. Available monitors: 1-{appliedLayouts.Layouts.Count}"); - } - - monitorsToUpdate.Add(monitorIndex); - } - else - { - // Default: first monitor - monitorsToUpdate.Add(0); - } - - // Update selected monitors - foreach (int monitorIndex in monitorsToUpdate) - { - if (targetCustomLayout != null) - { - appliedLayouts.Layouts[monitorIndex].AppliedLayout.Uuid = targetCustomLayout.Uuid; - appliedLayouts.Layouts[monitorIndex].AppliedLayout.Type = targetCustomLayout.Type; - } - else if (targetTemplate != null) - { - // For templates, use all-zeros UUID and the template type - appliedLayouts.Layouts[monitorIndex].AppliedLayout.Uuid = "{00000000-0000-0000-0000-000000000000}"; - appliedLayouts.Layouts[monitorIndex].AppliedLayout.Type = targetTemplate.Type; - appliedLayouts.Layouts[monitorIndex].AppliedLayout.ZoneCount = targetTemplate.ZoneCount; - appliedLayouts.Layouts[monitorIndex].AppliedLayout.ShowSpacing = targetTemplate.ShowSpacing; - appliedLayouts.Layouts[monitorIndex].AppliedLayout.Spacing = targetTemplate.Spacing; - } - } - - // Write back to file - FancyZonesData.WriteAppliedLayouts(appliedLayouts); - Logger.LogInfo($"Applied layouts file updated for {monitorsToUpdate.Count} monitor(s)"); - - // Notify FancyZones to reload - notifyFancyZones(wmPrivAppliedLayoutsFileUpdate); - Logger.LogInfo("FancyZones notified of layout change"); - - string layoutName = targetCustomLayout?.Name ?? targetTemplate?.Type ?? uuid; - if (applyToAll) - { - return (0, $"Layout '{layoutName}' applied to all {monitorsToUpdate.Count} monitors"); - } - else if (targetMonitor.HasValue) - { - return (0, $"Layout '{layoutName}' applied to monitor {targetMonitor.Value}"); - } - else - { - return (0, $"Layout '{layoutName}' applied to monitor 1"); - } - } -} diff --git a/src/modules/fancyzones/FancyZonesCLI/Commands/MonitorCommands.cs b/src/modules/fancyzones/FancyZonesCLI/Commands/MonitorCommands.cs deleted file mode 100644 index f542b901cc..0000000000 --- a/src/modules/fancyzones/FancyZonesCLI/Commands/MonitorCommands.cs +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Globalization; - -namespace FancyZonesCLI.Commands; - -/// <summary> -/// Monitor-related commands. -/// </summary> -internal static class MonitorCommands -{ - public static (int ExitCode, string Output) GetMonitors() - { - if (!FancyZonesData.TryReadAppliedLayouts(out var appliedLayouts, out var error)) - { - return (1, $"Error: {error}"); - } - - if (appliedLayouts.Layouts == null || appliedLayouts.Layouts.Count == 0) - { - return (0, "No monitors found."); - } - - var sb = new System.Text.StringBuilder(); - sb.AppendLine(CultureInfo.InvariantCulture, $"=== Monitors ({appliedLayouts.Layouts.Count} total) ==="); - sb.AppendLine(); - - for (int i = 0; i < appliedLayouts.Layouts.Count; i++) - { - var layout = appliedLayouts.Layouts[i]; - var monitorNum = i + 1; - - sb.AppendLine(CultureInfo.InvariantCulture, $"Monitor {monitorNum}:"); - sb.AppendLine(CultureInfo.InvariantCulture, $" Monitor: {layout.Device.Monitor}"); - sb.AppendLine(CultureInfo.InvariantCulture, $" Monitor Instance: {layout.Device.MonitorInstance}"); - sb.AppendLine(CultureInfo.InvariantCulture, $" Monitor Number: {layout.Device.MonitorNumber}"); - sb.AppendLine(CultureInfo.InvariantCulture, $" Serial Number: {layout.Device.SerialNumber}"); - sb.AppendLine(CultureInfo.InvariantCulture, $" Virtual Desktop: {layout.Device.VirtualDesktop}"); - sb.AppendLine(CultureInfo.InvariantCulture, $" Sensitivity Radius: {layout.AppliedLayout.SensitivityRadius}px"); - sb.AppendLine(CultureInfo.InvariantCulture, $" Active Layout: {layout.AppliedLayout.Type}"); - sb.AppendLine(CultureInfo.InvariantCulture, $" Zone Count: {layout.AppliedLayout.ZoneCount}"); - sb.AppendLine(); - } - - return (0, sb.ToString().TrimEnd()); - } -} diff --git a/src/modules/fancyzones/FancyZonesCLI/FancyZonesCLI.csproj b/src/modules/fancyzones/FancyZonesCLI/FancyZonesCLI.csproj index 85c2fa30e5..36274c95ef 100644 --- a/src/modules/fancyzones/FancyZonesCLI/FancyZonesCLI.csproj +++ b/src/modules/fancyzones/FancyZonesCLI/FancyZonesCLI.csproj @@ -2,7 +2,6 @@ <!-- Look at Directory.Build.props in root for common stuff as well --> <Import Project="..\..\..\Common.Dotnet.CsWinRT.props" /> <Import Project="..\..\..\Common.SelfContained.props" /> - <Import Project="..\..\..\Common.Dotnet.AotCompatibility.props" /> <PropertyGroup> <AssemblyTitle>PowerToys.FancyZonesCLI</AssemblyTitle> @@ -10,8 +9,6 @@ <Description>PowerToys FancyZones CLI</Description> <OutputType>Exe</OutputType> <Platforms>x64;ARM64</Platforms> - <PublishAot>true</PublishAot> - <DisableRuntimeMarshalling>true</DisableRuntimeMarshalling> <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> <OutputPath>..\..\..\..\$(Platform)\$(Configuration)</OutputPath> @@ -21,9 +18,14 @@ <ItemGroup> <PackageReference Include="System.Text.Json" /> + <PackageReference Include="System.CommandLine" /> <PackageReference Include="Microsoft.Windows.CsWin32" PrivateAssets="all" /> </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\FancyZonesEditorCommon\FancyZonesEditorCommon.csproj" /> + </ItemGroup> + <!-- Force using WindowsDesktop runtime to ensure consistent dll versions with other projects --> <ItemGroup> <FrameworkReference Include="Microsoft.WindowsDesktop.App" /> diff --git a/src/modules/fancyzones/FancyZonesCLI/FancyZonesData.cs b/src/modules/fancyzones/FancyZonesCLI/FancyZonesData.cs deleted file mode 100644 index 2396c51f44..0000000000 --- a/src/modules/fancyzones/FancyZonesCLI/FancyZonesData.cs +++ /dev/null @@ -1,142 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.IO; -using System.Text.Json; -using System.Text.Json.Serialization.Metadata; - -namespace FancyZonesCLI; - -/// <summary> -/// Provides methods to read and write FancyZones configuration data. -/// </summary> -internal static class FancyZonesData -{ - /// <summary> - /// Try to read applied layouts configuration. - /// </summary> - public static bool TryReadAppliedLayouts(out AppliedLayouts result, out string error) - { - return TryReadJsonFile(FancyZonesPaths.AppliedLayouts, FancyZonesJsonContext.Default.AppliedLayouts, out result, out error); - } - - /// <summary> - /// Read applied layouts or return null if not found. - /// </summary> - public static AppliedLayouts ReadAppliedLayouts() - { - return ReadJsonFileOrDefault(FancyZonesPaths.AppliedLayouts, FancyZonesJsonContext.Default.AppliedLayouts); - } - - /// <summary> - /// Write applied layouts configuration. - /// </summary> - public static void WriteAppliedLayouts(AppliedLayouts layouts) - { - WriteJsonFile(FancyZonesPaths.AppliedLayouts, layouts, FancyZonesJsonContext.Default.AppliedLayouts); - } - - /// <summary> - /// Read custom layouts or return null if not found. - /// </summary> - public static CustomLayouts ReadCustomLayouts() - { - return ReadJsonFileOrDefault(FancyZonesPaths.CustomLayouts, FancyZonesJsonContext.Default.CustomLayouts); - } - - /// <summary> - /// Read layout templates or return null if not found. - /// </summary> - public static LayoutTemplates ReadLayoutTemplates() - { - return ReadJsonFileOrDefault(FancyZonesPaths.LayoutTemplates, FancyZonesJsonContext.Default.LayoutTemplates); - } - - /// <summary> - /// Read layout hotkeys or return null if not found. - /// </summary> - public static LayoutHotkeys ReadLayoutHotkeys() - { - return ReadJsonFileOrDefault(FancyZonesPaths.LayoutHotkeys, FancyZonesJsonContext.Default.LayoutHotkeys); - } - - /// <summary> - /// Write layout hotkeys configuration. - /// </summary> - public static void WriteLayoutHotkeys(LayoutHotkeys hotkeys) - { - WriteJsonFile(FancyZonesPaths.LayoutHotkeys, hotkeys, FancyZonesJsonContext.Default.LayoutHotkeys); - } - - /// <summary> - /// Check if editor parameters file exists. - /// </summary> - public static bool EditorParametersExist() - { - return File.Exists(FancyZonesPaths.EditorParameters); - } - - private static bool TryReadJsonFile<T>(string filePath, JsonTypeInfo<T> jsonTypeInfo, out T result, out string error) - where T : class - { - result = null; - error = null; - - Logger.LogDebug($"Reading file: {filePath}"); - - if (!File.Exists(filePath)) - { - error = $"File not found: {Path.GetFileName(filePath)}"; - Logger.LogWarning(error); - return false; - } - - try - { - var json = File.ReadAllText(filePath); - result = JsonSerializer.Deserialize(json, jsonTypeInfo); - if (result == null) - { - error = $"Failed to parse {Path.GetFileName(filePath)}"; - Logger.LogError(error); - return false; - } - - Logger.LogDebug($"Successfully read {Path.GetFileName(filePath)}"); - return true; - } - catch (JsonException ex) - { - error = $"JSON parse error in {Path.GetFileName(filePath)}: {ex.Message}"; - Logger.LogError(error, ex); - return false; - } - catch (IOException ex) - { - error = $"Failed to read {Path.GetFileName(filePath)}: {ex.Message}"; - Logger.LogError(error, ex); - return false; - } - } - - private static T ReadJsonFileOrDefault<T>(string filePath, JsonTypeInfo<T> jsonTypeInfo, T defaultValue = null) - where T : class - { - if (TryReadJsonFile(filePath, jsonTypeInfo, out var result, out _)) - { - return result; - } - - return defaultValue; - } - - private static void WriteJsonFile<T>(string filePath, T data, JsonTypeInfo<T> jsonTypeInfo) - { - Logger.LogDebug($"Writing file: {filePath}"); - var json = JsonSerializer.Serialize(data, jsonTypeInfo); - File.WriteAllText(filePath, json); - Logger.LogInfo($"Successfully wrote {Path.GetFileName(filePath)}"); - } -} diff --git a/src/modules/fancyzones/FancyZonesCLI/LayoutVisualizer.cs b/src/modules/fancyzones/FancyZonesCLI/LayoutVisualizer.cs index fecdf33dbe..bf4c658119 100644 --- a/src/modules/fancyzones/FancyZonesCLI/LayoutVisualizer.cs +++ b/src/modules/fancyzones/FancyZonesCLI/LayoutVisualizer.cs @@ -7,12 +7,13 @@ using System.Collections.Generic; using System.Globalization; using System.Text; using System.Text.Json; +using FancyZonesEditorCommon.Data; namespace FancyZonesCLI; public static class LayoutVisualizer { - public static string DrawTemplateLayout(TemplateLayout template) + public static string DrawTemplateLayout(LayoutTemplates.TemplateLayoutWrapper template) { var sb = new StringBuilder(); sb.AppendLine(" Visual Preview:"); @@ -62,7 +63,7 @@ public static class LayoutVisualizer return sb.ToString(); } - public static string DrawCustomLayout(CustomLayout layout) + public static string DrawCustomLayout(CustomLayouts.CustomLayoutWrapper layout) { if (layout.Info.ValueKind == JsonValueKind.Undefined || layout.Info.ValueKind == JsonValueKind.Null) { @@ -426,6 +427,11 @@ public static class LayoutVisualizer const int displayWidth = 49; const int displayHeight = 15; + if (refWidth <= 0 || refHeight <= 0) + { + return string.Empty; + } + // Create a 2D array to track which zones occupy each position var zoneGrid = new List<int>[displayHeight, displayWidth]; for (int i = 0; i < displayHeight; i++) @@ -442,10 +448,13 @@ public static class LayoutVisualizer foreach (var zone in zones.EnumerateArray()) { - int x = zone.GetProperty("X").GetInt32(); - int y = zone.GetProperty("Y").GetInt32(); - int w = zone.GetProperty("width").GetInt32(); - int h = zone.GetProperty("height").GetInt32(); + if (!TryGetInt32Property(zone, "x", "X", out int x) || + !TryGetInt32Property(zone, "y", "Y", out int y) || + !TryGetInt32Property(zone, "width", "Width", out int w) || + !TryGetInt32Property(zone, "height", "Height", out int h)) + { + continue; + } int dx = Math.Max(0, Math.Min(displayWidth - 1, x * displayWidth / refWidth)); int dy = Math.Max(0, Math.Min(displayHeight - 1, y * displayHeight / refHeight)); @@ -547,4 +556,23 @@ public static class LayoutVisualizer sb.AppendLine(); return sb.ToString(); } + + private static bool TryGetInt32Property(JsonElement element, string primaryName, string fallbackName, out int value) + { + if (element.TryGetProperty(primaryName, out var property) || element.TryGetProperty(fallbackName, out property)) + { + if (property.ValueKind == JsonValueKind.Number) + { + return property.TryGetInt32(out value); + } + + if (property.ValueKind == JsonValueKind.String) + { + return int.TryParse(property.GetString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out value); + } + } + + value = default; + return false; + } } diff --git a/src/modules/fancyzones/FancyZonesCLI/Models.cs b/src/modules/fancyzones/FancyZonesCLI/Models.cs deleted file mode 100644 index 0c8bbefe54..0000000000 --- a/src/modules/fancyzones/FancyZonesCLI/Models.cs +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace FancyZonesCLI; - -// JSON Source Generator for AOT compatibility -[JsonSerializable(typeof(LayoutTemplates))] -[JsonSerializable(typeof(CustomLayouts))] -[JsonSerializable(typeof(AppliedLayouts))] -[JsonSerializable(typeof(LayoutHotkeys))] -[JsonSourceGenerationOptions(WriteIndented = true)] -internal partial class FancyZonesJsonContext : JsonSerializerContext -{ -} - -// Layout Templates -public sealed class LayoutTemplates -{ - [JsonPropertyName("layout-templates")] - public List<TemplateLayout> Templates { get; set; } -} - -public sealed class TemplateLayout -{ - [JsonPropertyName("type")] - public string Type { get; set; } = string.Empty; - - [JsonPropertyName("zone-count")] - public int ZoneCount { get; set; } - - [JsonPropertyName("show-spacing")] - public bool ShowSpacing { get; set; } - - [JsonPropertyName("spacing")] - public int Spacing { get; set; } - - [JsonPropertyName("sensitivity-radius")] - public int SensitivityRadius { get; set; } -} - -// Custom Layouts -public sealed class CustomLayouts -{ - [JsonPropertyName("custom-layouts")] - public List<CustomLayout> Layouts { get; set; } -} - -public sealed class CustomLayout -{ - [JsonPropertyName("uuid")] - public string Uuid { get; set; } = string.Empty; - - [JsonPropertyName("name")] - public string Name { get; set; } = string.Empty; - - [JsonPropertyName("type")] - public string Type { get; set; } = string.Empty; - - [JsonPropertyName("info")] - public JsonElement Info { get; set; } -} - -// Applied Layouts -public sealed class AppliedLayouts -{ - [JsonPropertyName("applied-layouts")] - public List<AppliedLayoutWrapper> Layouts { get; set; } -} - -public sealed class AppliedLayoutWrapper -{ - [JsonPropertyName("device")] - public DeviceInfo Device { get; set; } = new(); - - [JsonPropertyName("applied-layout")] - public AppliedLayoutInfo AppliedLayout { get; set; } = new(); -} - -public sealed class DeviceInfo -{ - [JsonPropertyName("monitor")] - public string Monitor { get; set; } = string.Empty; - - [JsonPropertyName("monitor-instance")] - public string MonitorInstance { get; set; } = string.Empty; - - [JsonPropertyName("monitor-number")] - public int MonitorNumber { get; set; } - - [JsonPropertyName("serial-number")] - public string SerialNumber { get; set; } = string.Empty; - - [JsonPropertyName("virtual-desktop")] - public string VirtualDesktop { get; set; } = string.Empty; -} - -public sealed class AppliedLayoutInfo -{ - [JsonPropertyName("uuid")] - public string Uuid { get; set; } = string.Empty; - - [JsonPropertyName("type")] - public string Type { get; set; } = string.Empty; - - [JsonPropertyName("show-spacing")] - public bool ShowSpacing { get; set; } - - [JsonPropertyName("spacing")] - public int Spacing { get; set; } - - [JsonPropertyName("zone-count")] - public int ZoneCount { get; set; } - - [JsonPropertyName("sensitivity-radius")] - public int SensitivityRadius { get; set; } -} - -// Layout Hotkeys -public sealed class LayoutHotkeys -{ - [JsonPropertyName("layout-hotkeys")] - public List<LayoutHotkey> Hotkeys { get; set; } -} - -public sealed class LayoutHotkey -{ - [JsonPropertyName("key")] - public int Key { get; set; } - - [JsonPropertyName("layout-id")] - public string LayoutId { get; set; } = string.Empty; -} diff --git a/src/modules/fancyzones/FancyZonesCLI/NativeMethods.cs b/src/modules/fancyzones/FancyZonesCLI/NativeMethods.cs index efab0859bd..4d3f14db0c 100644 --- a/src/modules/fancyzones/FancyZonesCLI/NativeMethods.cs +++ b/src/modules/fancyzones/FancyZonesCLI/NativeMethods.cs @@ -15,6 +15,7 @@ internal static class NativeMethods // Registered Windows messages for notifying FancyZones private static uint wmPrivAppliedLayoutsFileUpdate; private static uint wmPrivLayoutHotkeysFileUpdate; + private static uint wmPrivSaveEditorParameters; /// <summary> /// Gets the Windows message ID for applied layouts file update notification. @@ -26,6 +27,11 @@ internal static class NativeMethods /// </summary> public static uint WM_PRIV_LAYOUT_HOTKEYS_FILE_UPDATE => wmPrivLayoutHotkeysFileUpdate; + /// <summary> + /// Gets the Windows message ID used to request saving editor-parameters.json. + /// </summary> + public static uint WM_PRIV_SAVE_EDITOR_PARAMETERS => wmPrivSaveEditorParameters; + /// <summary> /// Initializes the Windows messages used for FancyZones notifications. /// </summary> @@ -33,6 +39,7 @@ internal static class NativeMethods { wmPrivAppliedLayoutsFileUpdate = PInvoke.RegisterWindowMessage("{2ef2c8a7-e0d5-4f31-9ede-52aade2d284d}"); wmPrivLayoutHotkeysFileUpdate = PInvoke.RegisterWindowMessage("{07229b7e-4f22-4357-b136-33c289be2295}"); + wmPrivSaveEditorParameters = PInvoke.RegisterWindowMessage("{d8f9c0e3-5d77-4e83-8a4f-7c704c2bfb4a}"); } /// <summary> diff --git a/src/modules/fancyzones/FancyZonesCLI/Program.cs b/src/modules/fancyzones/FancyZonesCLI/Program.cs index 1b133dfa36..6c44653c79 100644 --- a/src/modules/fancyzones/FancyZonesCLI/Program.cs +++ b/src/modules/fancyzones/FancyZonesCLI/Program.cs @@ -3,113 +3,44 @@ // See the LICENSE file in the project root for more information. using System; -using System.Globalization; +using System.CommandLine; using System.Linq; -using FancyZonesCLI.Commands; +using System.Threading.Tasks; +using FancyZonesCLI.CommandLine; namespace FancyZonesCLI; internal sealed class Program { - private static int Main(string[] args) + private static async Task<int> Main(string[] args) { - // Initialize logger Logger.InitializeLogger(); Logger.LogInfo($"CLI invoked with args: [{string.Join(", ", args)}]"); - // Initialize Windows messages + // Initialize Windows messages used to notify FancyZones. NativeMethods.InitializeWindowMessages(); - (int ExitCode, string Output) result; - - if (args.Length == 0) + // Intercept help requests early and print custom usage. + if (args.Any(a => string.Equals(a, "--help", StringComparison.OrdinalIgnoreCase) || + string.Equals(a, "-h", StringComparison.OrdinalIgnoreCase) || + string.Equals(a, "-?", StringComparison.OrdinalIgnoreCase))) { - result = (1, GetUsageText()); + FancyZonesCliUsage.PrintUsage(); + return 0; + } + + RootCommand rootCommand = FancyZonesCliCommandFactory.CreateRootCommand(); + int exitCode = await rootCommand.InvokeAsync(args); + + if (exitCode == 0) + { + Logger.LogInfo("Command completed successfully"); } else { - var command = args[0].ToLowerInvariant(); - - result = command switch - { - "open-editor" or "editor" or "e" => EditorCommands.OpenEditor(), - "get-monitors" or "monitors" or "m" => MonitorCommands.GetMonitors(), - "get-layouts" or "layouts" or "ls" => LayoutCommands.GetLayouts(), - "get-active-layout" or "active" or "get-active" or "a" => LayoutCommands.GetActiveLayout(), - "set-layout" or "set" or "s" => args.Length >= 2 - ? LayoutCommands.SetLayout(args.Skip(1).ToArray(), NativeMethods.NotifyFancyZones, NativeMethods.WM_PRIV_APPLIED_LAYOUTS_FILE_UPDATE) - : (1, "Error: set-layout requires a UUID parameter"), - "open-settings" or "settings" => EditorCommands.OpenSettings(), - "get-hotkeys" or "hotkeys" or "hk" => HotkeyCommands.GetHotkeys(), - "set-hotkey" or "shk" => args.Length >= 3 - ? HotkeyCommands.SetHotkey(int.Parse(args[1], CultureInfo.InvariantCulture), args[2], NativeMethods.NotifyFancyZones, NativeMethods.WM_PRIV_LAYOUT_HOTKEYS_FILE_UPDATE) - : (1, "Error: set-hotkey requires <key> <uuid>"), - "remove-hotkey" or "rhk" => args.Length >= 2 - ? HotkeyCommands.RemoveHotkey(int.Parse(args[1], CultureInfo.InvariantCulture), NativeMethods.NotifyFancyZones, NativeMethods.WM_PRIV_LAYOUT_HOTKEYS_FILE_UPDATE) - : (1, "Error: remove-hotkey requires <key>"), - "help" or "--help" or "-h" => (0, GetUsageText()), - _ => (1, $"Error: Unknown command: {command}\n\n{GetUsageText()}"), - }; + Logger.LogWarning($"Command failed with exit code {exitCode}"); } - // Log result - if (result.ExitCode == 0) - { - Logger.LogInfo($"Command completed successfully"); - } - else - { - Logger.LogWarning($"Command failed with exit code {result.ExitCode}: {result.Output}"); - } - - // Output result - if (!string.IsNullOrEmpty(result.Output)) - { - Console.WriteLine(result.Output); - } - - return result.ExitCode; - } - - private static string GetUsageText() - { - return """ - FancyZones CLI - Command line interface for FancyZones - ====================================================== - - Usage: FancyZonesCLI.exe <command> [options] - - Commands: - open-editor (editor, e) Launch FancyZones layout editor - get-monitors (monitors, m) List all monitors and their properties - get-layouts (layouts, ls) List all available layouts - get-active-layout (get-active, active, a) - Show currently active layout - set-layout (set, s) <uuid> [options] - Set layout by UUID - --monitor <n> Apply to monitor N (1-based) - --all Apply to all monitors - open-settings (settings) Open FancyZones settings page - get-hotkeys (hotkeys, hk) List all layout hotkeys - set-hotkey (shk) <key> <uuid> Assign hotkey (0-9) to CUSTOM layout - Note: Only custom layouts work with hotkeys - remove-hotkey (rhk) <key> Remove hotkey assignment - help Show this help message - - - Examples: - FancyZonesCLI.exe e # Open editor (short) - FancyZonesCLI.exe m # List monitors (short) - FancyZonesCLI.exe ls # List layouts (short) - FancyZonesCLI.exe a # Get active layout (short) - FancyZonesCLI.exe s focus --all # Set layout (short) - FancyZonesCLI.exe open-editor # Open editor (long) - FancyZonesCLI.exe get-monitors - FancyZonesCLI.exe get-layouts - FancyZonesCLI.exe set-layout {12345678-1234-1234-1234-123456789012} - FancyZonesCLI.exe set-layout focus --monitor 2 - FancyZonesCLI.exe set-layout columns --all - FancyZonesCLI.exe set-hotkey 3 {12345678-1234-1234-1234-123456789012} - """; + return exitCode; } } diff --git a/src/modules/fancyzones/FancyZonesCLI/Utils/AppliedLayoutsHelper.cs b/src/modules/fancyzones/FancyZonesCLI/Utils/AppliedLayoutsHelper.cs new file mode 100644 index 0000000000..f403ee0de5 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/Utils/AppliedLayoutsHelper.cs @@ -0,0 +1,89 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using FancyZonesEditorCommon.Data; + +namespace FancyZonesCLI.Utils; + +/// <summary> +/// Helper for managing applied layouts across monitors. +/// CLI-only business logic for matching, finding, and updating applied layouts. +/// </summary> +internal static class AppliedLayoutsHelper +{ + public const string DefaultVirtualDesktopGuid = "{00000000-0000-0000-0000-000000000000}"; + + public static bool MatchesDevice( + AppliedLayouts.AppliedLayoutWrapper.DeviceIdWrapper device, + string monitorName, + string serialNumber, + int monitorNumber, + string virtualDesktop) + { + // Must match monitor name + if (device.Monitor != monitorName) + { + return false; + } + + // Must match virtual desktop + if (device.VirtualDesktop != virtualDesktop) + { + return false; + } + + // If serial numbers are both available, they must match + if (!string.IsNullOrEmpty(device.SerialNumber) && !string.IsNullOrEmpty(serialNumber)) + { + if (device.SerialNumber != serialNumber) + { + return false; + } + } + + // If we reach here: Monitor name, VirtualDesktop, and SerialNumber (if available) all match + // MonitorInstance and MonitorNumber can vary, so we accept any value + return true; + } + + public static bool MatchesDeviceWithDefaultVirtualDesktop( + AppliedLayouts.AppliedLayoutWrapper.DeviceIdWrapper device, + string monitorName, + string serialNumber, + int monitorNumber, + string virtualDesktop) + { + if (device.VirtualDesktop == DefaultVirtualDesktopGuid) + { + // For this one layout record only, match any virtual desktop. + return device.Monitor == monitorName; + } + + return MatchesDevice(device, monitorName, serialNumber, monitorNumber, virtualDesktop); + } + + public static AppliedLayouts.AppliedLayoutWrapper? FindLayoutForMonitor( + AppliedLayouts.AppliedLayoutsListWrapper layouts, + string monitorName, + string serialNumber, + int monitorNumber, + string virtualDesktop) + { + if (layouts.AppliedLayouts == null) + { + return null; + } + + foreach (var layout in layouts.AppliedLayouts) + { + if (MatchesDevice(layout.Device, monitorName, serialNumber, monitorNumber, virtualDesktop)) + { + return layout; + } + } + + return null; + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/Utils/EditorParametersRefresh.cs b/src/modules/fancyzones/FancyZonesCLI/Utils/EditorParametersRefresh.cs new file mode 100644 index 0000000000..898d4b2f9e --- /dev/null +++ b/src/modules/fancyzones/FancyZonesCLI/Utils/EditorParametersRefresh.cs @@ -0,0 +1,68 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using System.Text.Json; +using System.Threading; + +using FancyZonesEditorCommon.Data; +using FancyZonesEditorCommon.Utils; + +namespace FancyZonesCLI.Utils; + +/// <summary> +/// Helper for requesting FancyZones to save editor-parameters.json and reading it reliably. +/// </summary> +internal static class EditorParametersRefresh +{ + public static EditorParameters.ParamsWrapper ReadEditorParametersWithRefresh(Action requestSave) + { + const int maxWaitMilliseconds = 500; + const int pollIntervalMilliseconds = 50; + + string filePath = FancyZonesPaths.EditorParameters; + DateTime lastWriteBefore = File.Exists(filePath) ? File.GetLastWriteTimeUtc(filePath) : DateTime.MinValue; + + requestSave(); + + int elapsedMilliseconds = 0; + while (elapsedMilliseconds < maxWaitMilliseconds) + { + try + { + if (File.Exists(filePath)) + { + DateTime lastWriteNow = File.GetLastWriteTimeUtc(filePath); + + // Prefer reading after the file is updated, but don't block forever if the + // timestamp resolution is coarse or FancyZones rewrites identical content. + if (lastWriteNow >= lastWriteBefore || elapsedMilliseconds > 100) + { + var editorParams = FancyZonesDataIO.ReadEditorParameters(); + if (editorParams.Monitors != null && editorParams.Monitors.Count > 0) + { + return editorParams; + } + } + } + } + catch (Exception ex) when (ex is FileNotFoundException || ex is IOException || ex is UnauthorizedAccessException || ex is JsonException) + { + // File may be mid-write/locked or temporarily invalid JSON; retry. + } + + Thread.Sleep(pollIntervalMilliseconds); + elapsedMilliseconds += pollIntervalMilliseconds; + } + + var finalParams = FancyZonesDataIO.ReadEditorParameters(); + if (finalParams.Monitors == null || finalParams.Monitors.Count == 0) + { + throw new InvalidOperationException($"Could not get current monitor information (timed out after {maxWaitMilliseconds}ms waiting for '{Path.GetFileName(filePath)}')."); + } + + return finalParams; + } +} diff --git a/src/modules/fancyzones/FancyZonesEditor.UITests/Utils/AppZoneHistory.cs b/src/modules/fancyzones/FancyZonesEditor.UITests/Utils/AppZoneHistory.cs index e8daa83348..f246665aea 100644 --- a/src/modules/fancyzones/FancyZonesEditor.UITests/Utils/AppZoneHistory.cs +++ b/src/modules/fancyzones/FancyZonesEditor.UITests/Utils/AppZoneHistory.cs @@ -55,24 +55,24 @@ namespace FancyZonesEditorCommon.Data public JsonElement ToJsonElement(ZoneHistoryWrapper info) { - string json = JsonSerializer.Serialize(info, this.JsonOptions); + string json = JsonSerializer.Serialize(info, JsonOptions); return JsonSerializer.Deserialize<JsonElement>(json); } public JsonElement ToJsonElement(DeviceIdWrapper info) { - string json = JsonSerializer.Serialize(info, this.JsonOptions); + string json = JsonSerializer.Serialize(info, JsonOptions); return JsonSerializer.Deserialize<JsonElement>(json); } public ZoneHistoryWrapper ZoneHistoryFromJsonElement(string json) { - return JsonSerializer.Deserialize<ZoneHistoryWrapper>(json, this.JsonOptions); + return JsonSerializer.Deserialize<ZoneHistoryWrapper>(json, JsonOptions); } public DeviceIdWrapper GridFromJsonElement(string json) { - return JsonSerializer.Deserialize<DeviceIdWrapper>(json, this.JsonOptions); + return JsonSerializer.Deserialize<DeviceIdWrapper>(json, JsonOptions); } } } diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/AppliedLayouts.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/AppliedLayouts.cs index 188e834b37..2a5545d83b 100644 --- a/src/modules/fancyzones/FancyZonesEditorCommon/Data/AppliedLayouts.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/AppliedLayouts.cs @@ -12,7 +12,7 @@ namespace FancyZonesEditorCommon.Data { get { - return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\applied-layouts.json"; + return FancyZonesPaths.AppliedLayouts; } } diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/CustomLayouts.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/CustomLayouts.cs index 110250ce02..75671e8ed8 100644 --- a/src/modules/fancyzones/FancyZonesEditorCommon/Data/CustomLayouts.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/CustomLayouts.cs @@ -15,7 +15,7 @@ namespace FancyZonesEditorCommon.Data { get { - return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\custom-layouts.json"; + return FancyZonesPaths.CustomLayouts; } } @@ -78,24 +78,24 @@ namespace FancyZonesEditorCommon.Data public JsonElement ToJsonElement(CanvasInfoWrapper info) { - string json = JsonSerializer.Serialize(info, this.JsonOptions); + string json = JsonSerializer.Serialize(info, FancyZonesJsonContext.Default.CanvasInfoWrapper); return JsonSerializer.Deserialize<JsonElement>(json); } public JsonElement ToJsonElement(GridInfoWrapper info) { - string json = JsonSerializer.Serialize(info, this.JsonOptions); + string json = JsonSerializer.Serialize(info, FancyZonesJsonContext.Default.GridInfoWrapper); return JsonSerializer.Deserialize<JsonElement>(json); } public CanvasInfoWrapper CanvasFromJsonElement(string json) { - return JsonSerializer.Deserialize<CanvasInfoWrapper>(json, this.JsonOptions); + return JsonSerializer.Deserialize(json, FancyZonesJsonContext.Default.CanvasInfoWrapper); } public GridInfoWrapper GridFromJsonElement(string json) { - return JsonSerializer.Deserialize<GridInfoWrapper>(json, this.JsonOptions); + return JsonSerializer.Deserialize(json, FancyZonesJsonContext.Default.GridInfoWrapper); } } } diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/DefaultLayouts.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/DefaultLayouts.cs index 0a916559dc..03020c184f 100644 --- a/src/modules/fancyzones/FancyZonesEditorCommon/Data/DefaultLayouts.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/DefaultLayouts.cs @@ -14,7 +14,7 @@ namespace FancyZonesEditorCommon.Data { get { - return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\default-layouts.json"; + return FancyZonesPaths.DefaultLayouts; } } diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorData`1.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorData`1.cs index fdfa32cc28..83b35f4ac8 100644 --- a/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorData`1.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorData`1.cs @@ -4,6 +4,7 @@ using System; using System.Text.Json; +using System.Text.Json.Serialization.Metadata; using FancyZonesEditorCommon.Utils; @@ -16,28 +17,20 @@ namespace FancyZonesEditorCommon.Data return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); } - protected JsonSerializerOptions JsonOptions - { - get - { - return new JsonSerializerOptions - { - PropertyNamingPolicy = new DashCaseNamingPolicy(), - WriteIndented = true, - }; - } - } + protected static JsonSerializerOptions JsonOptions => FancyZonesJsonContext.Default.Options; + + protected static JsonTypeInfo<T> TypeInfo => (JsonTypeInfo<T>)FancyZonesJsonContext.Default.GetTypeInfo(typeof(T)); public T Read(string file) { IOUtils ioUtils = new IOUtils(); string data = ioUtils.ReadFile(file); - return JsonSerializer.Deserialize<T>(data, JsonOptions); + return JsonSerializer.Deserialize(data, TypeInfo); } public string Serialize(T data) { - return JsonSerializer.Serialize(data, JsonOptions); + return JsonSerializer.Serialize(data, TypeInfo); } } } diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorParameters.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorParameters.cs index e973c6070f..fe1f023e0a 100644 --- a/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorParameters.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorParameters.cs @@ -14,7 +14,7 @@ namespace FancyZonesEditorCommon.Data { get { - return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\editor-parameters.json"; + return FancyZonesPaths.EditorParameters; } } diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/FancyZonesJsonContext.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/FancyZonesJsonContext.cs new file mode 100644 index 0000000000..69fa8a97b7 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/FancyZonesJsonContext.cs @@ -0,0 +1,38 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Text.Json.Serialization; + +using FancyZonesEditorCommon.Utils; + +namespace FancyZonesEditorCommon.Data +{ + /// <summary> + /// JSON serialization context for AOT-compatible serialization of FancyZones data types. + /// </summary> + [JsonSourceGenerationOptions( + PropertyNamingPolicy = JsonKnownNamingPolicy.KebabCaseLower, + WriteIndented = true)] + [JsonSerializable(typeof(AppliedLayouts.AppliedLayoutsListWrapper))] + [JsonSerializable(typeof(AppliedLayouts.AppliedLayoutWrapper))] + [JsonSerializable(typeof(AppliedLayouts.AppliedLayoutWrapper.DeviceIdWrapper))] + [JsonSerializable(typeof(AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper), TypeInfoPropertyName = "AppliedLayoutLayoutWrapper")] + [JsonSerializable(typeof(CustomLayouts.CustomLayoutListWrapper))] + [JsonSerializable(typeof(CustomLayouts.CustomLayoutWrapper))] + [JsonSerializable(typeof(CustomLayouts.CanvasInfoWrapper))] + [JsonSerializable(typeof(CustomLayouts.CanvasInfoWrapper.CanvasZoneWrapper))] + [JsonSerializable(typeof(CustomLayouts.GridInfoWrapper))] + [JsonSerializable(typeof(LayoutTemplates.TemplateLayoutsListWrapper))] + [JsonSerializable(typeof(LayoutTemplates.TemplateLayoutWrapper))] + [JsonSerializable(typeof(LayoutHotkeys.LayoutHotkeysWrapper))] + [JsonSerializable(typeof(LayoutHotkeys.LayoutHotkeyWrapper))] + [JsonSerializable(typeof(EditorParameters.ParamsWrapper))] + [JsonSerializable(typeof(EditorParameters.NativeMonitorDataWrapper))] + [JsonSerializable(typeof(DefaultLayouts.DefaultLayoutsListWrapper))] + [JsonSerializable(typeof(DefaultLayouts.DefaultLayoutWrapper))] + [JsonSerializable(typeof(DefaultLayouts.DefaultLayoutWrapper.LayoutWrapper), TypeInfoPropertyName = "DefaultLayoutLayoutWrapper")] + public partial class FancyZonesJsonContext : JsonSerializerContext + { + } +} diff --git a/src/modules/fancyzones/FancyZonesCLI/FancyZonesPaths.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/FancyZonesPaths.cs similarity index 85% rename from src/modules/fancyzones/FancyZonesCLI/FancyZonesPaths.cs rename to src/modules/fancyzones/FancyZonesEditorCommon/Data/FancyZonesPaths.cs index f04d375392..3d47f9e4c1 100644 --- a/src/modules/fancyzones/FancyZonesCLI/FancyZonesPaths.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/FancyZonesPaths.cs @@ -5,12 +5,12 @@ using System; using System.IO; -namespace FancyZonesCLI; +namespace FancyZonesEditorCommon.Data; /// <summary> /// Provides paths to FancyZones configuration files. /// </summary> -internal static class FancyZonesPaths +public static class FancyZonesPaths { private static readonly string DataPath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @@ -27,4 +27,6 @@ internal static class FancyZonesPaths public static string LayoutHotkeys => Path.Combine(DataPath, "layout-hotkeys.json"); public static string EditorParameters => Path.Combine(DataPath, "editor-parameters.json"); + + public static string DefaultLayouts => Path.Combine(DataPath, "default-layouts.json"); } diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutHotkeys.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutHotkeys.cs index 9b4fc2661f..1f4dfabf7f 100644 --- a/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutHotkeys.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutHotkeys.cs @@ -14,7 +14,7 @@ namespace FancyZonesEditorCommon.Data { get { - return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\layout-hotkeys.json"; + return FancyZonesPaths.LayoutHotkeys; } } diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutTemplates.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutTemplates.cs index 4cb932571b..8dae1fbb3b 100644 --- a/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutTemplates.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutTemplates.cs @@ -14,7 +14,7 @@ namespace FancyZonesEditorCommon.Data { get { - return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\layout-templates.json"; + return FancyZonesPaths.LayoutTemplates; } } diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Utils/FancyZonesDataIO.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Utils/FancyZonesDataIO.cs new file mode 100644 index 0000000000..e07fbde0f6 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Utils/FancyZonesDataIO.cs @@ -0,0 +1,154 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using FancyZonesEditorCommon.Data; + +namespace FancyZonesEditorCommon.Utils +{ + /// <summary> + /// Unified helper for all FancyZones data file I/O operations. + /// Centralizes reading and writing of all JSON configuration files. + /// </summary> + public static class FancyZonesDataIO + { + private static TWrapper ReadData<TData, TWrapper>( + Func<TData> createInstance, + Func<TData, string> fileSelector, + Func<TData, string, TWrapper> readFunc) + { + var instance = createInstance(); + string filePath = fileSelector(instance); + + if (!File.Exists(filePath)) + { + throw new FileNotFoundException($"File not found: {Path.GetFileName(filePath)}", filePath); + } + + return readFunc(instance, filePath); + } + + private static void WriteData<TData, TWrapper>( + Func<TData> createInstance, + Func<TData, string> fileSelector, + Func<TData, TWrapper, string> serializeFunc, + TWrapper data) + { + var instance = createInstance(); + var filePath = fileSelector(instance); + + IOUtils ioUtils = new IOUtils(); + ioUtils.WriteFile(filePath, serializeFunc(instance, data)); + } + + // AppliedLayouts operations + public static AppliedLayouts.AppliedLayoutsListWrapper ReadAppliedLayouts() + { + return ReadData( + () => new AppliedLayouts(), + instance => instance.File, + (instance, file) => instance.Read(file)); + } + + public static void WriteAppliedLayouts(AppliedLayouts.AppliedLayoutsListWrapper data) + { + WriteData( + () => new AppliedLayouts(), + instance => instance.File, + (instance, wrapper) => instance.Serialize(wrapper), + data); + } + + // CustomLayouts operations + public static CustomLayouts.CustomLayoutListWrapper ReadCustomLayouts() + { + return ReadData( + () => new CustomLayouts(), + instance => instance.File, + (instance, file) => instance.Read(file)); + } + + public static void WriteCustomLayouts(CustomLayouts.CustomLayoutListWrapper data) + { + WriteData( + () => new CustomLayouts(), + instance => instance.File, + (instance, wrapper) => instance.Serialize(wrapper), + data); + } + + // LayoutTemplates operations + public static LayoutTemplates.TemplateLayoutsListWrapper ReadLayoutTemplates() + { + return ReadData( + () => new LayoutTemplates(), + instance => instance.File, + (instance, file) => instance.Read(file)); + } + + public static void WriteLayoutTemplates(LayoutTemplates.TemplateLayoutsListWrapper data) + { + WriteData( + () => new LayoutTemplates(), + instance => instance.File, + (instance, wrapper) => instance.Serialize(wrapper), + data); + } + + // LayoutHotkeys operations + public static LayoutHotkeys.LayoutHotkeysWrapper ReadLayoutHotkeys() + { + return ReadData( + () => new LayoutHotkeys(), + instance => instance.File, + (instance, file) => instance.Read(file)); + } + + public static void WriteLayoutHotkeys(LayoutHotkeys.LayoutHotkeysWrapper data) + { + WriteData( + () => new LayoutHotkeys(), + instance => instance.File, + (instance, wrapper) => instance.Serialize(wrapper), + data); + } + + // EditorParameters operations + public static EditorParameters.ParamsWrapper ReadEditorParameters() + { + return ReadData( + () => new EditorParameters(), + instance => instance.File, + (instance, file) => instance.Read(file)); + } + + public static void WriteEditorParameters(EditorParameters.ParamsWrapper data) + { + WriteData( + () => new EditorParameters(), + instance => instance.File, + (instance, wrapper) => instance.Serialize(wrapper), + data); + } + + // DefaultLayouts operations + public static DefaultLayouts.DefaultLayoutsListWrapper ReadDefaultLayouts() + { + return ReadData( + () => new DefaultLayouts(), + instance => instance.File, + (instance, file) => instance.Read(file)); + } + + public static void WriteDefaultLayouts(DefaultLayouts.DefaultLayoutsListWrapper data) + { + WriteData( + () => new DefaultLayouts(), + instance => instance.File, + (instance, wrapper) => instance.Serialize(wrapper), + data); + } + } +} diff --git a/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp b/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp index 1956c57a97..4e83a450f5 100644 --- a/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp +++ b/src/modules/fancyzones/FancyZonesLib/FancyZones.cpp @@ -261,7 +261,7 @@ FancyZones::Run() noexcept } }) .wait(); - m_toggleEditorEventWaiter = EventWaiter(CommonSharedConstants::FANCY_ZONES_EDITOR_TOGGLE_EVENT, [&](int err) { + m_toggleEditorEventWaiter.start(CommonSharedConstants::FANCY_ZONES_EDITOR_TOGGLE_EVENT, [&](DWORD err) { if (err == ERROR_SUCCESS) { Logger::trace(L"{} event was signaled", CommonSharedConstants::FANCY_ZONES_EDITOR_TOGGLE_EVENT); @@ -728,6 +728,13 @@ LRESULT FancyZones::WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lpa { FancyZonesSettings::instance().LoadSettings(); } + else if (message == WM_PRIV_SAVE_EDITOR_PARAMETERS) + { + if (!EditorParameters::Save(m_workAreaConfiguration, m_dpiUnawareThread)) + { + Logger::warn(L"Failed to save editor-parameters.json"); + } + } else { return DefWindowProc(window, message, wparam, lparam); diff --git a/src/modules/fancyzones/FancyZonesLib/FancyZonesWinHookEventIDs.cpp b/src/modules/fancyzones/FancyZonesLib/FancyZonesWinHookEventIDs.cpp index 404486797a..94c07ebd24 100644 --- a/src/modules/fancyzones/FancyZonesLib/FancyZonesWinHookEventIDs.cpp +++ b/src/modules/fancyzones/FancyZonesLib/FancyZonesWinHookEventIDs.cpp @@ -18,6 +18,7 @@ UINT WM_PRIV_DEFAULT_LAYOUTS_FILE_UPDATE; UINT WM_PRIV_SNAP_HOTKEY; UINT WM_PRIV_QUICK_LAYOUT_KEY; UINT WM_PRIV_SETTINGS_CHANGED; +UINT WM_PRIV_SAVE_EDITOR_PARAMETERS; std::once_flag init_flag; @@ -40,5 +41,6 @@ void InitializeWinhookEventIds() WM_PRIV_SNAP_HOTKEY = RegisterWindowMessage(L"{72f4fd8e-23f1-43ab-bbbc-029363df9a84}"); WM_PRIV_QUICK_LAYOUT_KEY = RegisterWindowMessage(L"{15baab3d-c67b-4a15-aFF0-13610e05e947}"); WM_PRIV_SETTINGS_CHANGED = RegisterWindowMessage(L"{89ca3Daa-bf2d-4e73-9f3f-c60716364e27}"); + WM_PRIV_SAVE_EDITOR_PARAMETERS = RegisterWindowMessage(L"{d8f9c0e3-5d77-4e83-8a4f-7c704c2bfb4a}"); }); } diff --git a/src/modules/fancyzones/FancyZonesLib/FancyZonesWinHookEventIDs.h b/src/modules/fancyzones/FancyZonesLib/FancyZonesWinHookEventIDs.h index b00c8c1f8f..214a5b1f75 100644 --- a/src/modules/fancyzones/FancyZonesLib/FancyZonesWinHookEventIDs.h +++ b/src/modules/fancyzones/FancyZonesLib/FancyZonesWinHookEventIDs.h @@ -16,5 +16,6 @@ extern UINT WM_PRIV_DEFAULT_LAYOUTS_FILE_UPDATE; // Scheduled when the watched d extern UINT WM_PRIV_SNAP_HOTKEY; // Scheduled when we receive a snap hotkey key down press extern UINT WM_PRIV_QUICK_LAYOUT_KEY; // Scheduled when we receive a key down press to quickly apply a layout extern UINT WM_PRIV_SETTINGS_CHANGED; // Scheduled when a watched settings file is updated +extern UINT WM_PRIV_SAVE_EDITOR_PARAMETERS; // Scheduled to request saving editor-parameters.json void InitializeWinhookEventIds(); diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/EditorWindow.cs b/src/modules/fancyzones/editor/FancyZonesEditor/EditorWindow.cs index 2543568436..c6e5e5aec5 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/EditorWindow.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/EditorWindow.cs @@ -4,7 +4,6 @@ using System; using System.Windows; - using FancyZonesEditor.Models; using ManagedCommon; diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs index 3562175bca..eebd0bb54f 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs @@ -563,7 +563,7 @@ namespace FancyZonesEditor private void SettingsBtn_Click(object sender, RoutedEventArgs e) { - SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.FancyZones, false); + SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.FancyZones); } private void EditLayoutDialogTitle_Loaded(object sender, RoutedEventArgs e) diff --git a/src/modules/imageresizer/ui/App.xaml.cs b/src/modules/imageresizer/ui/App.xaml.cs index e2b87d7746..ab0f59b779 100644 --- a/src/modules/imageresizer/ui/App.xaml.cs +++ b/src/modules/imageresizer/ui/App.xaml.cs @@ -6,9 +6,9 @@ using System; using System.Globalization; +using System.Runtime.InteropServices; using System.Text; using System.Windows; - using ImageResizer.Models; using ImageResizer.Properties; using ImageResizer.Utilities; @@ -20,8 +20,32 @@ namespace ImageResizer { public partial class App : Application, IDisposable { + private const string LogSubFolder = "\\ImageResizer\\Logs"; + + /// <summary> + /// Gets cached AI availability state, checked at app startup. + /// Can be updated after model download completes or background initialization. + /// </summary> + public static AiAvailabilityState AiAvailabilityState { get; internal set; } + + /// <summary> + /// Event fired when AI initialization completes in background. + /// Allows UI to refresh state when initialization finishes. + /// </summary> + public static event EventHandler<AiAvailabilityState> AiInitializationCompleted; + static App() { + try + { + // Initialize logger early (mirroring PowerOCR pattern) + Logger.InitializeLogger(LogSubFolder); + } + catch + { + /* swallow logger init issues silently */ + } + try { string appLanguage = LanguageHelper.LoadLanguage(); @@ -30,9 +54,9 @@ namespace ImageResizer System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(appLanguage); } } - catch (CultureNotFoundException) + catch (CultureNotFoundException ex) { - // error + Logger.LogError("CultureNotFoundException: " + ex.Message); } Console.InputEncoding = Encoding.Unicode; @@ -43,15 +67,59 @@ namespace ImageResizer // Fix for .net 3.1.19 making Image Resizer not adapt to DPI changes. NativeMethods.SetProcessDPIAware(); + // Check for AI detection mode (called by Runner in background) + if (e?.Args?.Length > 0 && e.Args[0] == "--detect-ai") + { + RunAiDetectionMode(); + return; + } + if (PowerToys.GPOWrapperProjection.GPOWrapper.GetConfiguredImageResizerEnabledValue() == PowerToys.GPOWrapperProjection.GpoRuleConfigured.Disabled) { /* TODO: Add logs to ImageResizer. * Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator."); */ + Logger.LogWarning("GPO policy disables ImageResizer. Exiting."); Environment.Exit(0); // Current.Exit won't work until there's a window opened. return; } + // AI Super Resolution is not supported on Windows 10 - skip cache check entirely + if (OSVersionHelper.IsWindows10()) + { + AiAvailabilityState = AiAvailabilityState.NotSupported; + ResizeBatch.SetAiSuperResolutionService(Services.NoOpAiSuperResolutionService.Instance); + Logger.LogInfo("AI Super Resolution not supported on Windows 10"); + } + else + { + // Load AI availability from cache (written by Runner's background detection) + var cachedState = Services.AiAvailabilityCacheService.LoadCache(); + + if (cachedState.HasValue) + { + AiAvailabilityState = cachedState.Value; + Logger.LogInfo($"AI state loaded from cache: {AiAvailabilityState}"); + } + else + { + // No valid cache - default to NotSupported (Runner will detect and cache for next startup) + AiAvailabilityState = AiAvailabilityState.NotSupported; + Logger.LogInfo("No AI cache found, defaulting to NotSupported"); + } + + // If AI is potentially available, start background initialization (non-blocking) + if (AiAvailabilityState == AiAvailabilityState.Ready) + { + _ = InitializeAiServiceAsync(); // Fire and forget - don't block UI + } + else + { + // AI not available - set NoOp service immediately + ResizeBatch.SetAiSuperResolutionService(Services.NoOpAiSuperResolutionService.Instance); + } + } + var batch = ResizeBatch.FromCommandLine(Console.In, e?.Args); // TODO: Add command-line parameters that can be used in lieu of the input page (issue #14) @@ -62,9 +130,121 @@ namespace ImageResizer WindowHelpers.BringToForeground(new System.Windows.Interop.WindowInteropHelper(mainWindow).Handle); } + /// <summary> + /// AI detection mode: perform detection, write to cache, and exit. + /// Called by Runner in background to avoid blocking ImageResizer UI startup. + /// </summary> + private void RunAiDetectionMode() + { + try + { + Logger.LogInfo("Running AI detection mode..."); + + // AI Super Resolution is not supported on Windows 10 + if (OSVersionHelper.IsWindows10()) + { + Logger.LogInfo("AI detection skipped: Windows 10 does not support AI Super Resolution"); + Services.AiAvailabilityCacheService.SaveCache(AiAvailabilityState.NotSupported); + Environment.Exit(0); + return; + } + + // Perform detection (reuse existing logic) + var state = CheckAiAvailability(); + + // Write result to cache file + Services.AiAvailabilityCacheService.SaveCache(state); + + Logger.LogInfo($"AI detection complete: {state}"); + } + catch (Exception ex) + { + Logger.LogError($"AI detection failed: {ex.Message}"); + Services.AiAvailabilityCacheService.SaveCache(AiAvailabilityState.NotSupported); + } + + // Exit silently without showing UI + Environment.Exit(0); + } + + /// <summary> + /// Check AI Super Resolution availability on this system. + /// Performs architecture check and model availability check. + /// </summary> + private static AiAvailabilityState CheckAiAvailability() + { + try + { + // Check Windows AI service model ready state + // it's so slow, why? + var readyState = Services.WinAiSuperResolutionService.GetModelReadyState(); + + // Map AI service state to our availability state + switch (readyState) + { + case Microsoft.Windows.AI.AIFeatureReadyState.Ready: + return AiAvailabilityState.Ready; + + case Microsoft.Windows.AI.AIFeatureReadyState.NotReady: + return AiAvailabilityState.ModelNotReady; + + case Microsoft.Windows.AI.AIFeatureReadyState.DisabledByUser: + case Microsoft.Windows.AI.AIFeatureReadyState.NotSupportedOnCurrentSystem: + default: + return AiAvailabilityState.NotSupported; + } + } + catch (Exception) + { + return AiAvailabilityState.NotSupported; + } + } + + /// <summary> + /// Initialize AI Super Resolution service asynchronously in background. + /// Runs without blocking UI startup - state change event notifies completion. + /// </summary> + private static async System.Threading.Tasks.Task InitializeAiServiceAsync() + { + AiAvailabilityState finalState; + + try + { + // Create and initialize AI service using async factory + var aiService = await Services.WinAiSuperResolutionService.CreateAsync(); + + if (aiService != null) + { + ResizeBatch.SetAiSuperResolutionService(aiService); + Logger.LogInfo("AI Super Resolution service initialized successfully."); + finalState = AiAvailabilityState.Ready; + } + else + { + // Initialization failed - use default NoOp service + ResizeBatch.SetAiSuperResolutionService(Services.NoOpAiSuperResolutionService.Instance); + Logger.LogWarning("AI Super Resolution service initialization failed. Using default service."); + finalState = AiAvailabilityState.NotSupported; + } + } + catch (Exception ex) + { + // Log error and use default NoOp service + ResizeBatch.SetAiSuperResolutionService(Services.NoOpAiSuperResolutionService.Instance); + Logger.LogError($"Exception during AI service initialization: {ex.Message}"); + finalState = AiAvailabilityState.NotSupported; + } + + // Update cached state and notify listeners + AiAvailabilityState = finalState; + AiInitializationCompleted?.Invoke(null, finalState); + } + public void Dispose() { - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method + // Dispose AI Super Resolution service + ResizeBatch.DisposeAiSuperResolutionService(); + GC.SuppressFinalize(this); } } diff --git a/src/modules/imageresizer/ui/ImageResizerUI.csproj b/src/modules/imageresizer/ui/ImageResizerUI.csproj index 3ce98d8386..3deb3ee5b7 100644 --- a/src/modules/imageresizer/ui/ImageResizerUI.csproj +++ b/src/modules/imageresizer/ui/ImageResizerUI.csproj @@ -10,6 +10,7 @@ <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> <GenerateSatelliteAssembliesForCore>true</GenerateSatelliteAssembliesForCore> <UseWPF>true</UseWPF> + <WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained> </PropertyGroup> <PropertyGroup> @@ -18,19 +19,20 @@ <RootNamespace>ImageResizer</RootNamespace> <AssemblyName>PowerToys.ImageResizer</AssemblyName> <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> </PropertyGroup> <PropertyGroup> <ApplicationIcon>Resources\ImageResizer.ico</ApplicationIcon> </PropertyGroup> - <!-- <PropertyGroup> + <PropertyGroup> <ApplicationManifest>ImageResizerUI.dev.manifest</ApplicationManifest> </PropertyGroup> <PropertyGroup Condition="'$(CIBuild)'=='true'"> <ApplicationManifest>ImageResizerUI.prod.manifest</ApplicationManifest> - </PropertyGroup> --> + </PropertyGroup> <ItemGroup> <EmbeddedResource Update="Properties\Resources.resx"> @@ -46,6 +48,8 @@ <Resource Include="Resources\ImageResizer.png" /> </ItemGroup> <ItemGroup> + <PackageReference Include="Microsoft.WindowsAppSDK" /> + <PackageReference Include="Microsoft.WindowsAppSDK.AI" /> <PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" /> <PackageReference Include="System.IO.Abstractions" /> <PackageReference Include="WPF-UI" /> diff --git a/src/modules/imageresizer/ui/Models/AiSize.cs b/src/modules/imageresizer/ui/Models/AiSize.cs new file mode 100644 index 0000000000..dcb9d521d8 --- /dev/null +++ b/src/modules/imageresizer/ui/Models/AiSize.cs @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Globalization; +using System.Text; +using System.Text.Json.Serialization; + +using ImageResizer.Properties; + +namespace ImageResizer.Models +{ + public class AiSize : ResizeSize + { + private static readonly CompositeFormat ScaleFormat = CompositeFormat.Parse(Resources.Input_AiScaleFormat); + private int _scale = 2; + + /// <summary> + /// Gets the formatted scale display string (e.g., "2×"). + /// </summary> + [JsonIgnore] + public string ScaleDisplay => string.Format(CultureInfo.CurrentCulture, ScaleFormat, _scale); + + [JsonPropertyName("scale")] + public int Scale + { + get => _scale; + set => Set(ref _scale, value); + } + + [JsonConstructor] + public AiSize(int scale) + { + Scale = scale; + } + + public AiSize() + { + } + } +} diff --git a/src/modules/imageresizer/ui/Models/ResizeBatch.cs b/src/modules/imageresizer/ui/Models/ResizeBatch.cs index 87e0b84e7b..b081094403 100644 --- a/src/modules/imageresizer/ui/Models/ResizeBatch.cs +++ b/src/modules/imageresizer/ui/Models/ResizeBatch.cs @@ -15,17 +15,30 @@ using System.Threading; using System.Threading.Tasks; using ImageResizer.Properties; +using ImageResizer.Services; namespace ImageResizer.Models { public class ResizeBatch { private readonly IFileSystem _fileSystem = new FileSystem(); + private static IAISuperResolutionService _aiSuperResolutionService; public string DestinationDirectory { get; set; } public ICollection<string> Files { get; } = new List<string>(); + public static void SetAiSuperResolutionService(IAISuperResolutionService service) + { + _aiSuperResolutionService = service; + } + + public static void DisposeAiSuperResolutionService() + { + _aiSuperResolutionService?.Dispose(); + _aiSuperResolutionService = null; + } + public static ResizeBatch FromCommandLine(TextReader standardInput, string[] args) { var batch = new ResizeBatch(); @@ -122,6 +135,9 @@ namespace ImageResizer.Models } protected virtual void Execute(string file, Settings settings) - => new ResizeOperation(file, DestinationDirectory, settings).Execute(); + { + var aiService = _aiSuperResolutionService ?? NoOpAiSuperResolutionService.Instance; + new ResizeOperation(file, DestinationDirectory, settings, aiService).Execute(); + } } } diff --git a/src/modules/imageresizer/ui/Models/ResizeOperation.cs b/src/modules/imageresizer/ui/Models/ResizeOperation.cs index a56cd2f658..4c3cb837a1 100644 --- a/src/modules/imageresizer/ui/Models/ResizeOperation.cs +++ b/src/modules/imageresizer/ui/Models/ResizeOperation.cs @@ -10,12 +10,14 @@ using System.Globalization; using System.IO; using System.IO.Abstractions; using System.Linq; +using System.Text; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; using ImageResizer.Extensions; using ImageResizer.Properties; +using ImageResizer.Services; using ImageResizer.Utilities; using Microsoft.VisualBasic.FileIO; @@ -30,6 +32,10 @@ namespace ImageResizer.Models private readonly string _file; private readonly string _destinationDirectory; private readonly Settings _settings; + private readonly IAISuperResolutionService _aiSuperResolutionService; + + // Cache CompositeFormat for AI error message formatting (CA1863) + private static readonly CompositeFormat _aiErrorFormat = CompositeFormat.Parse(Resources.Error_AiProcessingFailed); // Filenames to avoid according to https://learn.microsoft.com/windows/win32/fileio/naming-a-file#file-and-directory-names private static readonly string[] _avoidFilenames = @@ -39,11 +45,12 @@ namespace ImageResizer.Models "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", }; - public ResizeOperation(string file, string destinationDirectory, Settings settings) + public ResizeOperation(string file, string destinationDirectory, Settings settings, IAISuperResolutionService aiSuperResolutionService = null) { _file = file; _destinationDirectory = destinationDirectory; _settings = settings; + _aiSuperResolutionService = aiSuperResolutionService ?? NoOpAiSuperResolutionService.Instance; } public void Execute() @@ -167,6 +174,11 @@ namespace ImageResizer.Models private BitmapSource Transform(BitmapSource source) { + if (_settings.SelectedSize is AiSize) + { + return TransformWithAi(source); + } + int originalWidth = source.PixelWidth; int originalHeight = source.PixelHeight; @@ -257,6 +269,31 @@ namespace ImageResizer.Models return scaledBitmap; } + private BitmapSource TransformWithAi(BitmapSource source) + { + try + { + var result = _aiSuperResolutionService.ApplySuperResolution( + source, + _settings.AiSize.Scale, + _file); + + if (result == null) + { + throw new InvalidOperationException(Properties.Resources.Error_AiConversionFailed); + } + + return result; + } + catch (Exception ex) + { + // Wrap the exception with a localized message + // This will be caught by ResizeBatch.Process() and displayed to the user + var errorMessage = string.Format(CultureInfo.CurrentCulture, _aiErrorFormat, ex.Message); + throw new InvalidOperationException(errorMessage, ex); + } + } + /// <summary> /// Checks original metadata by writing an image containing the given metadata into a memory stream. /// In case of errors, we try to rebuild the metadata object and check again. @@ -363,19 +400,24 @@ namespace ImageResizer.Models } // Remove directory characters from the size's name. - string sizeNameSanitized = _settings.SelectedSize.Name; - sizeNameSanitized = sizeNameSanitized + // For AI Size, use the scale display (e.g., "2×") instead of the full name + string sizeName = _settings.SelectedSize is AiSize aiSize + ? aiSize.ScaleDisplay + : _settings.SelectedSize.Name; + string sizeNameSanitized = sizeName .Replace('\\', '_') .Replace('/', '_'); // Using CurrentCulture since this is user facing + var selectedWidth = _settings.SelectedSize is AiSize ? encoder.Frames[0].PixelWidth : _settings.SelectedSize.Width; + var selectedHeight = _settings.SelectedSize is AiSize ? encoder.Frames[0].PixelHeight : _settings.SelectedSize.Height; var fileName = string.Format( CultureInfo.CurrentCulture, _settings.FileNameFormat, originalFileName, sizeNameSanitized, - _settings.SelectedSize.Width, - _settings.SelectedSize.Height, + selectedWidth, + selectedHeight, encoder.Frames[0].PixelWidth, encoder.Frames[0].PixelHeight); diff --git a/src/modules/imageresizer/ui/Properties/Resources.Designer.cs b/src/modules/imageresizer/ui/Properties/Resources.Designer.cs index 4229ca31df..22166e73b9 100644 --- a/src/modules/imageresizer/ui/Properties/Resources.Designer.cs +++ b/src/modules/imageresizer/ui/Properties/Resources.Designer.cs @@ -19,7 +19,7 @@ namespace ImageResizer.Properties { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "18.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] public class Resources { @@ -78,6 +78,33 @@ namespace ImageResizer.Properties { } } + /// <summary> + /// Looks up a localized string similar to Failed to convert image format for AI processing.. + /// </summary> + public static string Error_AiConversionFailed { + get { + return ResourceManager.GetString("Error_AiConversionFailed", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to AI super resolution processing failed: {0}. + /// </summary> + public static string Error_AiProcessingFailed { + get { + return ResourceManager.GetString("Error_AiProcessingFailed", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to AI scaling operation failed.. + /// </summary> + public static string Error_AiScalingFailed { + get { + return ResourceManager.GetString("Error_AiScalingFailed", resourceCulture); + } + } + /// <summary> /// Looks up a localized string similar to Height. /// </summary> @@ -105,6 +132,132 @@ namespace ImageResizer.Properties { } } + /// <summary> + /// Looks up a localized string similar to Current:. + /// </summary> + public static string Input_AiCurrentLabel { + get { + return ResourceManager.GetString("Input_AiCurrentLabel", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Checking AI model availability.... + /// </summary> + public static string Input_AiModelChecking { + get { + return ResourceManager.GetString("Input_AiModelChecking", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to AI feature is disabled by system settings.. + /// </summary> + public static string Input_AiModelDisabledByUser { + get { + return ResourceManager.GetString("Input_AiModelDisabledByUser", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Download. + /// </summary> + public static string Input_AiModelDownloadButton { + get { + return ResourceManager.GetString("Input_AiModelDownloadButton", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Failed to download AI model. Please try again.. + /// </summary> + public static string Input_AiModelDownloadFailed { + get { + return ResourceManager.GetString("Input_AiModelDownloadFailed", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Downloading AI model.... + /// </summary> + public static string Input_AiModelDownloading { + get { + return ResourceManager.GetString("Input_AiModelDownloading", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to AI model not downloaded. Click Download to get started.. + /// </summary> + public static string Input_AiModelNotAvailable { + get { + return ResourceManager.GetString("Input_AiModelNotAvailable", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to AI feature is not supported on this system.. + /// </summary> + public static string Input_AiModelNotSupported { + get { + return ResourceManager.GetString("Input_AiModelNotSupported", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to New:. + /// </summary> + public static string Input_AiNewLabel { + get { + return ResourceManager.GetString("Input_AiNewLabel", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to {0}×. + /// </summary> + public static string Input_AiScaleFormat { + get { + return ResourceManager.GetString("Input_AiScaleFormat", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Scale. + /// </summary> + public static string Input_AiScaleLabel { + get { + return ResourceManager.GetString("Input_AiScaleLabel", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Super resolution. + /// </summary> + public static string Input_AiSuperResolution { + get { + return ResourceManager.GetString("Input_AiSuperResolution", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Upscale images using on-device AI. + /// </summary> + public static string Input_AiSuperResolutionDescription { + get { + return ResourceManager.GetString("Input_AiSuperResolutionDescription", resourceCulture); + } + } + + /// <summary> + /// Looks up a localized string similar to Unavailable. + /// </summary> + public static string Input_AiUnknownSize { + get { + return ResourceManager.GetString("Input_AiUnknownSize", resourceCulture); + } + } + /// <summary> /// Looks up a localized string similar to (auto). /// </summary> diff --git a/src/modules/imageresizer/ui/Properties/Resources.resx b/src/modules/imageresizer/ui/Properties/Resources.resx index 0d6e81d311..b549e2b06d 100644 --- a/src/modules/imageresizer/ui/Properties/Resources.resx +++ b/src/modules/imageresizer/ui/Properties/Resources.resx @@ -296,4 +296,55 @@ <data name="Input_ShrinkOnly.Content" xml:space="preserve"> <value>_Make pictures smaller but not larger</value> </data> + <data name="Input_AiSuperResolution" xml:space="preserve"> + <value>Super resolution</value> + </data> + <data name="Input_AiUnknownSize" xml:space="preserve"> + <value>Unavailable</value> + </data> + <data name="Input_AiScaleFormat" xml:space="preserve"> + <value>{0}×</value> + </data> + <data name="Input_AiScaleLabel" xml:space="preserve"> + <value>Scale</value> + </data> + <data name="Input_AiCurrentLabel" xml:space="preserve"> + <value>Current:</value> + </data> + <data name="Input_AiNewLabel" xml:space="preserve"> + <value>New:</value> + </data> + <data name="Input_AiModelChecking" xml:space="preserve"> + <value>Checking AI model availability...</value> + </data> + <data name="Input_AiModelNotAvailable" xml:space="preserve"> + <value>AI model not downloaded. Click Download to get started.</value> + </data> + <data name="Input_AiModelDisabledByUser" xml:space="preserve"> + <value>AI feature is disabled by system settings.</value> + </data> + <data name="Input_AiModelNotSupported" xml:space="preserve"> + <value>AI feature is not supported on this system.</value> + </data> + <data name="Input_AiModelDownloading" xml:space="preserve"> + <value>Downloading AI model...</value> + </data> + <data name="Input_AiModelDownloadFailed" xml:space="preserve"> + <value>Failed to download AI model. Please try again.</value> + </data> + <data name="Input_AiModelDownloadButton" xml:space="preserve"> + <value>Download</value> + </data> + <data name="Error_AiProcessingFailed" xml:space="preserve"> + <value>AI super resolution processing failed: {0}</value> + </data> + <data name="Error_AiConversionFailed" xml:space="preserve"> + <value>Failed to convert image format for AI processing.</value> + </data> + <data name="Error_AiScalingFailed" xml:space="preserve"> + <value>AI scaling operation failed.</value> + </data> + <data name="Input_AiSuperResolutionDescription" xml:space="preserve"> + <value>Upscale images using on-device AI</value> + </data> </root> \ No newline at end of file diff --git a/src/modules/imageresizer/ui/Properties/Settings.cs b/src/modules/imageresizer/ui/Properties/Settings.cs index 0f8690dcbb..63a6680d2e 100644 --- a/src/modules/imageresizer/ui/Properties/Settings.cs +++ b/src/modules/imageresizer/ui/Properties/Settings.cs @@ -19,10 +19,22 @@ using System.Threading; using System.Windows.Media.Imaging; using ImageResizer.Models; +using ImageResizer.Services; +using ImageResizer.ViewModels; using ManagedCommon; namespace ImageResizer.Properties { + /// <summary> + /// Represents the availability state of AI Super Resolution feature. + /// </summary> + public enum AiAvailabilityState + { + NotSupported, // System doesn't support AI (architecture issue or policy disabled) + ModelNotReady, // AI supported but model not downloaded + Ready, // AI fully ready to use + } + public sealed partial class Settings : IDataErrorInfo, INotifyPropertyChanged { private static readonly IFileSystem _fileSystem = new FileSystem(); @@ -50,6 +62,7 @@ namespace ImageResizer.Properties private bool _keepDateModified; private System.Guid _fallbackEncoder; private CustomSize _customSize; + private AiSize _aiSize; public Settings() { @@ -72,9 +85,28 @@ namespace ImageResizer.Properties KeepDateModified = false; FallbackEncoder = new System.Guid("19e4a5aa-5662-4fc5-a0c0-1758028e1057"); CustomSize = new CustomSize(ResizeFit.Fit, 1024, 640, ResizeUnit.Pixel); + AiSize = new AiSize(2); // Initialize with default scale of 2 AllSizes = new AllSizesCollection(this); } + /// <summary> + /// Validates the SelectedSizeIndex to ensure it's within the valid range. + /// This handles cross-device migration where settings saved on ARM64 with AI selected + /// are loaded on non-ARM64 devices. + /// </summary> + private void ValidateSelectedSizeIndex() + { + // Index structure: 0 to Sizes.Count-1 (regular), Sizes.Count (CustomSize), Sizes.Count+1 (AiSize) + var maxIndex = ImageResizer.App.AiAvailabilityState == AiAvailabilityState.NotSupported + ? Sizes.Count // CustomSize only + : Sizes.Count + 1; // CustomSize + AiSize + + if (_selectedSizeIndex > maxIndex) + { + _selectedSizeIndex = 0; // Reset to first size + } + } + [JsonIgnore] public IEnumerable<ResizeSize> AllSizes { get; set; } @@ -94,15 +126,40 @@ namespace ImageResizer.Properties [JsonIgnore] public ResizeSize SelectedSize { - get => SelectedSizeIndex >= 0 && SelectedSizeIndex < Sizes.Count - ? Sizes[SelectedSizeIndex] - : CustomSize; + get + { + if (SelectedSizeIndex >= 0 && SelectedSizeIndex < Sizes.Count) + { + return Sizes[SelectedSizeIndex]; + } + else if (SelectedSizeIndex == Sizes.Count) + { + return CustomSize; + } + else if (ImageResizer.App.AiAvailabilityState != AiAvailabilityState.NotSupported && SelectedSizeIndex == Sizes.Count + 1) + { + return AiSize; + } + else + { + // Fallback to CustomSize when index is out of range or AI is not available + return CustomSize; + } + } + set { var index = Sizes.IndexOf(value); if (index == -1) { - index = Sizes.Count; + if (value is AiSize) + { + index = Sizes.Count + 1; + } + else + { + index = Sizes.Count; + } } SelectedSizeIndex = index; @@ -138,13 +195,17 @@ namespace ImageResizer.Properties private class AllSizesCollection : IEnumerable<ResizeSize>, INotifyCollectionChanged, INotifyPropertyChanged { + private readonly Settings _settings; private ObservableCollection<ResizeSize> _sizes; private CustomSize _customSize; + private AiSize _aiSize; public AllSizesCollection(Settings settings) { + _settings = settings; _sizes = settings.Sizes; _customSize = settings.CustomSize; + _aiSize = settings.AiSize; _sizes.CollectionChanged += HandleCollectionChanged; ((INotifyPropertyChanged)_sizes).PropertyChanged += HandlePropertyChanged; @@ -163,6 +224,18 @@ namespace ImageResizer.Properties oldCustomSize, _sizes.Count)); } + else if (e.PropertyName == nameof(Models.AiSize)) + { + var oldAiSize = _aiSize; + _aiSize = settings.AiSize; + + OnCollectionChanged( + new NotifyCollectionChangedEventArgs( + NotifyCollectionChangedAction.Replace, + _aiSize, + oldAiSize, + _sizes.Count + 1)); + } else if (e.PropertyName == nameof(Sizes)) { var oldSizes = _sizes; @@ -185,12 +258,30 @@ namespace ImageResizer.Properties public event PropertyChangedEventHandler PropertyChanged; public int Count - => _sizes.Count + 1; + => _sizes.Count + 1 + (ImageResizer.App.AiAvailabilityState != AiAvailabilityState.NotSupported ? 1 : 0); public ResizeSize this[int index] - => index == _sizes.Count - ? _customSize - : _sizes[index]; + { + get + { + if (index < _sizes.Count) + { + return _sizes[index]; + } + else if (index == _sizes.Count) + { + return _customSize; + } + else if (ImageResizer.App.AiAvailabilityState != AiAvailabilityState.NotSupported && index == _sizes.Count + 1) + { + return _aiSize; + } + else + { + throw new ArgumentOutOfRangeException(nameof(index), index, $"Index {index} is out of range for AllSizesCollection."); + } + } + } public IEnumerator<ResizeSize> GetEnumerator() => new AllSizesEnumerator(this); @@ -410,6 +501,18 @@ namespace ImageResizer.Properties } } + [JsonConverter(typeof(WrappedJsonValueConverter))] + [JsonPropertyName("imageresizer_aiSize")] + public AiSize AiSize + { + get => _aiSize; + set + { + _aiSize = value; + NotifyPropertyChanged(); + } + } + public static string SettingsPath { get => _settingsPath; set => _settingsPath = value; } public event PropertyChangedEventHandler PropertyChanged; @@ -487,6 +590,7 @@ namespace ImageResizer.Properties KeepDateModified = jsonSettings.KeepDateModified; FallbackEncoder = jsonSettings.FallbackEncoder; CustomSize = jsonSettings.CustomSize; + AiSize = jsonSettings.AiSize ?? new AiSize(InputViewModel.DefaultAiScale); SelectedSizeIndex = jsonSettings.SelectedSizeIndex; if (jsonSettings.Sizes.Count > 0) @@ -497,6 +601,10 @@ namespace ImageResizer.Properties // Ensure Ids are unique and handle missing Ids IdRecoveryHelper.RecoverInvalidIds(Sizes); } + + // Validate SelectedSizeIndex after Sizes collection has been updated + // This handles cross-device migration (e.g., ARM64 -> non-ARM64) + ValidateSelectedSizeIndex(); } } } diff --git a/src/modules/imageresizer/ui/Services/AiAvailabilityCacheService.cs b/src/modules/imageresizer/ui/Services/AiAvailabilityCacheService.cs new file mode 100644 index 0000000000..d1a235297e --- /dev/null +++ b/src/modules/imageresizer/ui/Services/AiAvailabilityCacheService.cs @@ -0,0 +1,125 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Text.Json; +using ImageResizer.Properties; +using ManagedCommon; + +namespace ImageResizer.Services +{ + /// <summary> + /// Service for caching AI availability detection results. + /// Persists results to avoid slow API calls on every startup. + /// Runner calls ImageResizer --detect-ai to perform detection, + /// and ImageResizer reads the cached result on normal startup. + /// </summary> + public static class AiAvailabilityCacheService + { + private const string CacheFileName = "ai_capabilities.json"; + private const int CacheVersion = 1; + + private static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions + { + WriteIndented = true, + }; + + private static string CachePath => Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + "Microsoft", + "PowerToys", + CacheFileName); + + /// <summary> + /// Load AI availability state from cache. + /// Returns null if cache doesn't exist, is invalid, or read fails. + /// </summary> + public static AiAvailabilityState? LoadCache() + { + try + { + if (!File.Exists(CachePath)) + { + return null; + } + + var json = File.ReadAllText(CachePath); + var cache = JsonSerializer.Deserialize<AiCapabilityCache>(json); + + if (!IsCacheValid(cache)) + { + return null; + } + + return (AiAvailabilityState)cache.State; + } + catch (Exception ex) + { + // Read failure (file locked, corrupted JSON, etc.) - return null and use fallback + Logger.LogError($"Failed to load AI cache: {ex.Message}"); + return null; + } + } + + /// <summary> + /// Save AI availability state to cache. + /// Called by --detect-ai mode after performing detection. + /// </summary> + public static void SaveCache(AiAvailabilityState state) + { + try + { + var cache = new AiCapabilityCache + { + Version = CacheVersion, + State = (int)state, + WindowsBuild = Environment.OSVersion.Version.ToString(), + Architecture = RuntimeInformation.ProcessArchitecture.ToString(), + Timestamp = DateTime.UtcNow.ToString("o"), + }; + + var dir = Path.GetDirectoryName(CachePath); + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + + var json = JsonSerializer.Serialize(cache, SerializerOptions); + File.WriteAllText(CachePath, json); + + Logger.LogInfo($"AI cache saved: {state}"); + } + catch (Exception ex) + { + Logger.LogError($"Failed to save AI cache: {ex.Message}"); + } + } + + /// <summary> + /// Validate cache against current system environment. + /// Cache is invalid if version, architecture, or Windows build changed. + /// </summary> + private static bool IsCacheValid(AiCapabilityCache cache) + { + if (cache == null || cache.Version != CacheVersion) + { + return false; + } + + if (cache.Architecture != RuntimeInformation.ProcessArchitecture.ToString()) + { + return false; + } + + if (cache.WindowsBuild != Environment.OSVersion.Version.ToString()) + { + return false; + } + + return true; + } + } +} diff --git a/src/modules/imageresizer/ui/Services/AiCapabilityCache.cs b/src/modules/imageresizer/ui/Services/AiCapabilityCache.cs new file mode 100644 index 0000000000..f787f13569 --- /dev/null +++ b/src/modules/imageresizer/ui/Services/AiCapabilityCache.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace ImageResizer.Services +{ + /// <summary> + /// Data model for AI capability cache file. + /// </summary> + internal sealed class AiCapabilityCache + { + public int Version { get; set; } + + public int State { get; set; } + + public string WindowsBuild { get; set; } + + public string Architecture { get; set; } + + public string Timestamp { get; set; } + } +} diff --git a/src/modules/imageresizer/ui/Services/IAISuperResolutionService.cs b/src/modules/imageresizer/ui/Services/IAISuperResolutionService.cs new file mode 100644 index 0000000000..3db073c5e5 --- /dev/null +++ b/src/modules/imageresizer/ui/Services/IAISuperResolutionService.cs @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Windows.Media.Imaging; + +namespace ImageResizer.Services +{ + public interface IAISuperResolutionService : IDisposable + { + BitmapSource ApplySuperResolution(BitmapSource source, int scale, string filePath); + } +} diff --git a/src/modules/imageresizer/ui/Services/NoOpAiSuperResolutionService.cs b/src/modules/imageresizer/ui/Services/NoOpAiSuperResolutionService.cs new file mode 100644 index 0000000000..e59b5033ac --- /dev/null +++ b/src/modules/imageresizer/ui/Services/NoOpAiSuperResolutionService.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Windows.Media.Imaging; + +namespace ImageResizer.Services +{ + public sealed class NoOpAiSuperResolutionService : IAISuperResolutionService + { + public static NoOpAiSuperResolutionService Instance { get; } = new NoOpAiSuperResolutionService(); + + private NoOpAiSuperResolutionService() + { + } + + public BitmapSource ApplySuperResolution(BitmapSource source, int scale, string filePath) + { + return source; + } + + public void Dispose() + { + // No resources to dispose in no-op implementation + } + } +} diff --git a/src/modules/imageresizer/ui/Services/WinAiSuperResolutionService.cs b/src/modules/imageresizer/ui/Services/WinAiSuperResolutionService.cs new file mode 100644 index 0000000000..4cd752184a --- /dev/null +++ b/src/modules/imageresizer/ui/Services/WinAiSuperResolutionService.cs @@ -0,0 +1,261 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.WindowsRuntime; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using Microsoft.Windows.AI; +using Microsoft.Windows.AI.Imaging; +using Windows.Graphics.Imaging; + +namespace ImageResizer.Services +{ + public sealed class WinAiSuperResolutionService : IAISuperResolutionService + { + private readonly ImageScaler _imageScaler; + private readonly object _usageLock = new object(); + private bool _disposed; + + /// <summary> + /// Initializes a new instance of the <see cref="WinAiSuperResolutionService"/> class. + /// Private constructor. Use CreateAsync() factory method to create instances. + /// </summary> + private WinAiSuperResolutionService(ImageScaler imageScaler) + { + _imageScaler = imageScaler ?? throw new ArgumentNullException(nameof(imageScaler)); + } + + /// <summary> + /// Async factory method to create and initialize WinAiSuperResolutionService. + /// Returns null if initialization fails. + /// </summary> + public static async Task<WinAiSuperResolutionService> CreateAsync() + { + try + { + var imageScaler = await ImageScaler.CreateAsync(); + if (imageScaler == null) + { + return null; + } + + return new WinAiSuperResolutionService(imageScaler); + } + catch + { + return null; + } + } + + public static AIFeatureReadyState GetModelReadyState() + { + try + { + return ImageScaler.GetReadyState(); + } + catch (Exception) + { + // If we can't get the state, treat it as disabled by user + // The caller should check if it's Ready or NotReady + return AIFeatureReadyState.DisabledByUser; + } + } + + public static async Task<AIFeatureReadyResult> EnsureModelReadyAsync(IProgress<double> progress = null) + { + try + { + var operation = ImageScaler.EnsureReadyAsync(); + + // Register progress handler if provided + if (progress != null) + { + operation.Progress = (asyncInfo, progressValue) => + { + // progressValue is a double representing completion percentage (0.0 to 1.0 or 0 to 100) + progress.Report(progressValue); + }; + } + + return await operation; + } + catch (Exception) + { + return null; + } + } + + public BitmapSource ApplySuperResolution(BitmapSource source, int scale, string filePath) + { + if (source == null || _disposed) + { + return source; + } + + // Note: filePath parameter reserved for future use (e.g., logging, caching) + // Currently not used by the ImageScaler API + try + { + // Convert WPF BitmapSource to WinRT SoftwareBitmap + var softwareBitmap = ConvertBitmapSourceToSoftwareBitmap(source); + if (softwareBitmap == null) + { + return source; + } + + // Calculate target dimensions + var newWidth = softwareBitmap.PixelWidth * scale; + var newHeight = softwareBitmap.PixelHeight * scale; + + // Apply super resolution with thread-safe access + // _usageLock protects concurrent access from Parallel.ForEach threads + SoftwareBitmap scaledBitmap; + lock (_usageLock) + { + if (_disposed) + { + return source; + } + + scaledBitmap = _imageScaler.ScaleSoftwareBitmap(softwareBitmap, newWidth, newHeight); + } + + if (scaledBitmap == null) + { + return source; + } + + // Convert back to WPF BitmapSource + return ConvertSoftwareBitmapToBitmapSource(scaledBitmap); + } + catch (Exception) + { + // Any error, return original image gracefully + return source; + } + } + + private static SoftwareBitmap ConvertBitmapSourceToSoftwareBitmap(BitmapSource bitmapSource) + { + try + { + // Ensure the bitmap is in a compatible format + var convertedBitmap = new FormatConvertedBitmap(); + convertedBitmap.BeginInit(); + convertedBitmap.Source = bitmapSource; + convertedBitmap.DestinationFormat = PixelFormats.Bgra32; + convertedBitmap.EndInit(); + + int width = convertedBitmap.PixelWidth; + int height = convertedBitmap.PixelHeight; + int stride = width * 4; // 4 bytes per pixel for Bgra32 + byte[] pixels = new byte[height * stride]; + + convertedBitmap.CopyPixels(pixels, stride, 0); + + // Create SoftwareBitmap from pixel data + var softwareBitmap = new SoftwareBitmap( + BitmapPixelFormat.Bgra8, + width, + height, + BitmapAlphaMode.Premultiplied); + + using (var buffer = softwareBitmap.LockBuffer(BitmapBufferAccessMode.Write)) + using (var reference = buffer.CreateReference()) + { + unsafe + { + ((IMemoryBufferByteAccess)reference).GetBuffer(out byte* dataInBytes, out uint capacity); + System.Runtime.InteropServices.Marshal.Copy(pixels, 0, (IntPtr)dataInBytes, pixels.Length); + } + } + + return softwareBitmap; + } + catch (Exception) + { + return null; + } + } + + private static BitmapSource ConvertSoftwareBitmapToBitmapSource(SoftwareBitmap softwareBitmap) + { + try + { + // Convert to Bgra8 format if needed + var convertedBitmap = SoftwareBitmap.Convert( + softwareBitmap, + BitmapPixelFormat.Bgra8, + BitmapAlphaMode.Premultiplied); + + int width = convertedBitmap.PixelWidth; + int height = convertedBitmap.PixelHeight; + int stride = width * 4; // 4 bytes per pixel for Bgra8 + byte[] pixels = new byte[height * stride]; + + using (var buffer = convertedBitmap.LockBuffer(BitmapBufferAccessMode.Read)) + using (var reference = buffer.CreateReference()) + { + unsafe + { + ((IMemoryBufferByteAccess)reference).GetBuffer(out byte* dataInBytes, out uint capacity); + System.Runtime.InteropServices.Marshal.Copy((IntPtr)dataInBytes, pixels, 0, pixels.Length); + } + } + + // Create WPF BitmapSource from pixel data + var wpfBitmap = BitmapSource.Create( + width, + height, + 96, // DPI X + 96, // DPI Y + PixelFormats.Bgra32, + null, + pixels, + stride); + + wpfBitmap.Freeze(); // Make it thread-safe + return wpfBitmap; + } + catch (Exception) + { + return null; + } + } + + [ComImport] + [Guid("5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D")] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + private interface IMemoryBufferByteAccess + { + unsafe void GetBuffer(out byte* buffer, out uint capacity); + } + + public void Dispose() + { + if (_disposed) + { + return; + } + + lock (_usageLock) + { + if (_disposed) + { + return; + } + + // ImageScaler implements IDisposable + (_imageScaler as IDisposable)?.Dispose(); + + _disposed = true; + } + } + } +} diff --git a/src/modules/imageresizer/ui/ViewModels/InputViewModel.cs b/src/modules/imageresizer/ui/ViewModels/InputViewModel.cs index 54728defd4..c241728276 100644 --- a/src/modules/imageresizer/ui/ViewModels/InputViewModel.cs +++ b/src/modules/imageresizer/ui/ViewModels/InputViewModel.cs @@ -6,22 +6,41 @@ using System; using System.Collections.Generic; +using System.ComponentModel; +using System.Globalization; +using System.IO; using System.Linq; +using System.Runtime.InteropServices; +using System.Threading.Tasks; using System.Windows.Input; - +using System.Windows.Media.Imaging; using Common.UI; using ImageResizer.Helpers; using ImageResizer.Models; using ImageResizer.Properties; +using ImageResizer.Services; using ImageResizer.Views; namespace ImageResizer.ViewModels { public class InputViewModel : Observable { + public const int DefaultAiScale = 2; + private const int MinAiScale = 1; + private const int MaxAiScale = 8; + private readonly ResizeBatch _batch; private readonly MainViewModel _mainViewModel; private readonly IMainView _mainView; + private readonly bool _hasMultipleFiles; + private bool _originalDimensionsLoaded; + private int? _originalWidth; + private int? _originalHeight; + private string _currentResolutionDescription; + private string _newResolutionDescription; + private bool _isDownloadingModel; + private string _modelStatusMessage; + private double _modelDownloadProgress; public enum Dimension { @@ -45,24 +64,114 @@ namespace ImageResizer.ViewModels _batch = batch; _mainViewModel = mainViewModel; _mainView = mainView; + _hasMultipleFiles = _batch?.Files.Count > 1; Settings = settings; if (settings != null) { settings.CustomSize.PropertyChanged += (sender, e) => settings.SelectedSize = (CustomSize)sender; + settings.AiSize.PropertyChanged += (sender, e) => + { + if (e.PropertyName == nameof(AiSize.Scale)) + { + NotifyAiScaleChanged(); + } + }; + settings.PropertyChanged += HandleSettingsPropertyChanged; } - ResizeCommand = new RelayCommand(Resize); + ResizeCommand = new RelayCommand(Resize, () => CanResize); CancelCommand = new RelayCommand(Cancel); OpenSettingsCommand = new RelayCommand(OpenSettings); EnterKeyPressedCommand = new RelayCommand<KeyPressParams>(HandleEnterKeyPress); + DownloadModelCommand = new RelayCommand(async () => await DownloadModelAsync()); + + // Initialize AI UI state based on Settings availability + InitializeAiState(); } public Settings Settings { get; } - public IEnumerable<ResizeFit> ResizeFitValues => Enum.GetValues(typeof(ResizeFit)).Cast<ResizeFit>(); + public IEnumerable<ResizeFit> ResizeFitValues => Enum.GetValues<ResizeFit>(); - public IEnumerable<ResizeUnit> ResizeUnitValues => Enum.GetValues(typeof(ResizeUnit)).Cast<ResizeUnit>(); + public IEnumerable<ResizeUnit> ResizeUnitValues => Enum.GetValues<ResizeUnit>(); + + public int AiSuperResolutionScale + { + get => Settings?.AiSize?.Scale ?? DefaultAiScale; + set + { + if (Settings?.AiSize != null && Settings.AiSize.Scale != value) + { + Settings.AiSize.Scale = value; + NotifyAiScaleChanged(); + } + } + } + + public string AiScaleDisplay => Settings?.AiSize?.ScaleDisplay ?? string.Empty; + + public string CurrentResolutionDescription + { + get => _currentResolutionDescription; + private set => Set(ref _currentResolutionDescription, value); + } + + public string NewResolutionDescription + { + get => _newResolutionDescription; + private set => Set(ref _newResolutionDescription, value); + } + + // ==================== UI State Properties ==================== + + // Show AI size descriptions only when AI size is selected and not multiple files + public bool ShowAiSizeDescriptions => Settings?.SelectedSize is AiSize && !_hasMultipleFiles; + + // Helper property: Is model currently being downloaded? + public bool IsModelDownloading => _isDownloadingModel; + + public string ModelStatusMessage + { + get => _modelStatusMessage; + private set => Set(ref _modelStatusMessage, value); + } + + public double ModelDownloadProgress + { + get => _modelDownloadProgress; + private set => Set(ref _modelDownloadProgress, value); + } + + // Show download prompt when: AI size is selected and model is not ready (including downloading) + public bool ShowModelDownloadPrompt => + Settings?.SelectedSize is AiSize && + (App.AiAvailabilityState == Properties.AiAvailabilityState.ModelNotReady || _isDownloadingModel); + + // Show AI controls when: AI size is selected and AI is ready + public bool ShowAiControls => + Settings?.SelectedSize is AiSize && + App.AiAvailabilityState == Properties.AiAvailabilityState.Ready; + + /// <summary> + /// Gets a value indicating whether the resize operation can proceed. + /// For AI resize: only enabled when AI is fully ready. + /// For non-AI resize: always enabled. + /// </summary> + public bool CanResize + { + get + { + // If AI size is selected, only allow resize when AI is fully ready + if (Settings?.SelectedSize is AiSize) + { + return App.AiAvailabilityState == Properties.AiAvailabilityState.Ready; + } + + // Non-AI resize can always proceed + return true; + } + } public ICommand ResizeCommand { get; } @@ -72,9 +181,11 @@ namespace ImageResizer.ViewModels public ICommand EnterKeyPressedCommand { get; private set; } + public ICommand DownloadModelCommand { get; private set; } + // Any of the files is a gif public bool TryingToResizeGifFiles => - _batch.Files.Any(filename => filename.EndsWith(".gif", System.StringComparison.InvariantCultureIgnoreCase)); + _batch?.Files.Any(filename => filename.EndsWith(".gif", System.StringComparison.InvariantCultureIgnoreCase)) == true; public void Resize() { @@ -84,7 +195,7 @@ namespace ImageResizer.ViewModels public static void OpenSettings() { - SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.ImageResizer, false); + SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.ImageResizer); } private void HandleEnterKeyPress(KeyPressParams parameters) @@ -102,5 +213,234 @@ namespace ImageResizer.ViewModels public void Cancel() => _mainView.Close(); + + private void HandleSettingsPropertyChanged(object sender, PropertyChangedEventArgs e) + { + switch (e.PropertyName) + { + case nameof(Settings.SelectedSizeIndex): + case nameof(Settings.SelectedSize): + // Notify UI state properties that depend on SelectedSize + NotifyAiStateChanged(); + UpdateAiDetails(); + + // Trigger CanExecuteChanged for ResizeCommand + if (ResizeCommand is RelayCommand cmd) + { + cmd.OnCanExecuteChanged(); + } + + break; + } + } + + private void EnsureAiScaleWithinRange() + { + if (Settings?.AiSize != null) + { + Settings.AiSize.Scale = Math.Clamp( + Settings.AiSize.Scale, + MinAiScale, + MaxAiScale); + } + } + + private void UpdateAiDetails() + { + // Clear AI details if AI size not selected + if (Settings == null || Settings.SelectedSize is not AiSize) + { + CurrentResolutionDescription = string.Empty; + NewResolutionDescription = string.Empty; + return; + } + + EnsureAiScaleWithinRange(); + + if (_hasMultipleFiles) + { + CurrentResolutionDescription = string.Empty; + NewResolutionDescription = string.Empty; + return; + } + + EnsureOriginalDimensionsLoaded(); + + var hasConcreteSize = _originalWidth.HasValue && _originalHeight.HasValue; + CurrentResolutionDescription = hasConcreteSize + ? FormatDimensions(_originalWidth!.Value, _originalHeight!.Value) + : Resources.Input_AiUnknownSize; + + var scale = Settings.AiSize.Scale; + NewResolutionDescription = hasConcreteSize + ? FormatDimensions((long)_originalWidth!.Value * scale, (long)_originalHeight!.Value * scale) + : Resources.Input_AiUnknownSize; + } + + private static string FormatDimensions(long width, long height) + { + return string.Format(CultureInfo.CurrentCulture, "{0} × {1}", width, height); + } + + private void EnsureOriginalDimensionsLoaded() + { + if (_originalDimensionsLoaded) + { + return; + } + + var file = _batch?.Files.FirstOrDefault(); + if (string.IsNullOrEmpty(file)) + { + _originalDimensionsLoaded = true; + return; + } + + try + { + using var stream = File.OpenRead(file); + var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); + var frame = decoder.Frames.FirstOrDefault(); + if (frame != null) + { + _originalWidth = frame.PixelWidth; + _originalHeight = frame.PixelHeight; + } + } + catch (Exception) + { + // Failed to load image dimensions - clear values + _originalWidth = null; + _originalHeight = null; + } + finally + { + _originalDimensionsLoaded = true; + } + } + + /// <summary> + /// Initializes AI UI state based on App's cached availability state. + /// Subscribe to state change event to update UI when background initialization completes. + /// </summary> + private void InitializeAiState() + { + // Subscribe to initialization completion event to refresh UI + App.AiInitializationCompleted += OnAiInitializationCompleted; + + // Set initial status message based on current state + UpdateStatusMessage(); + } + + /// <summary> + /// Handles AI initialization completion event from App. + /// Refreshes UI when background initialization finishes. + /// </summary> + private void OnAiInitializationCompleted(object sender, Properties.AiAvailabilityState finalState) + { + UpdateStatusMessage(); + NotifyAiStateChanged(); + } + + /// <summary> + /// Updates status message based on current App availability state. + /// </summary> + private void UpdateStatusMessage() + { + ModelStatusMessage = App.AiAvailabilityState switch + { + Properties.AiAvailabilityState.Ready => string.Empty, + Properties.AiAvailabilityState.ModelNotReady => Resources.Input_AiModelNotAvailable, + Properties.AiAvailabilityState.NotSupported => Resources.Input_AiModelNotSupported, + _ => string.Empty, + }; + } + + /// <summary> + /// Notifies UI when AI state changes (model availability, download status). + /// </summary> + private void NotifyAiStateChanged() + { + OnPropertyChanged(nameof(IsModelDownloading)); + OnPropertyChanged(nameof(ShowModelDownloadPrompt)); + OnPropertyChanged(nameof(ShowAiControls)); + OnPropertyChanged(nameof(ShowAiSizeDescriptions)); + OnPropertyChanged(nameof(CanResize)); + + // Trigger CanExecuteChanged for ResizeCommand + if (ResizeCommand is RelayCommand resizeCommand) + { + resizeCommand.OnCanExecuteChanged(); + } + } + + /// <summary> + /// Notifies UI when AI scale changes (slider value). + /// </summary> + private void NotifyAiScaleChanged() + { + OnPropertyChanged(nameof(AiSuperResolutionScale)); + OnPropertyChanged(nameof(AiScaleDisplay)); + UpdateAiDetails(); + } + + private async Task DownloadModelAsync() + { + try + { + // Set downloading flag and show progress + _isDownloadingModel = true; + ModelStatusMessage = Resources.Input_AiModelDownloading; + ModelDownloadProgress = 0; + NotifyAiStateChanged(); + + // Create progress reporter to update UI + var progress = new Progress<double>(value => + { + // progressValue could be 0-1 or 0-100, normalize to 0-100 + ModelDownloadProgress = value > 1 ? value : value * 100; + }); + + // Call EnsureReadyAsync to download and prepare the AI model + var result = await WinAiSuperResolutionService.EnsureModelReadyAsync(progress); + + if (result?.Status == Microsoft.Windows.AI.AIFeatureReadyResultState.Success) + { + // Model successfully downloaded and ready + ModelDownloadProgress = 100; + + // Update App's cached state + App.AiAvailabilityState = Properties.AiAvailabilityState.Ready; + UpdateStatusMessage(); + + // Initialize the AI service now that model is ready + var aiService = await WinAiSuperResolutionService.CreateAsync(); + ResizeBatch.SetAiSuperResolutionService(aiService ?? (Services.IAISuperResolutionService)NoOpAiSuperResolutionService.Instance); + } + else + { + // Download failed + ModelStatusMessage = Resources.Input_AiModelDownloadFailed; + } + } + catch (Exception) + { + // Exception during download + ModelStatusMessage = Resources.Input_AiModelDownloadFailed; + } + finally + { + // Clear downloading flag + _isDownloadingModel = false; + + // Reset progress if not successful + if (App.AiAvailabilityState != Properties.AiAvailabilityState.Ready) + { + ModelDownloadProgress = 0; + } + + NotifyAiStateChanged(); + } + } } } diff --git a/src/modules/imageresizer/ui/Views/BoolValueConverter.cs b/src/modules/imageresizer/ui/Views/BoolValueConverter.cs index ac59068a28..f4e83fab0d 100644 --- a/src/modules/imageresizer/ui/Views/BoolValueConverter.cs +++ b/src/modules/imageresizer/ui/Views/BoolValueConverter.cs @@ -11,11 +11,21 @@ using System.Windows.Data; namespace ImageResizer.Views { - [ValueConversion(typeof(Enum), typeof(string))] + [ValueConversion(typeof(bool), typeof(Visibility))] internal class BoolValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) - => (bool)value ? Visibility.Visible : Visibility.Collapsed; + { + bool boolValue = (bool)value; + bool invert = parameter is string param && param.Equals("Inverted", StringComparison.OrdinalIgnoreCase); + + if (invert) + { + boolValue = !boolValue; + } + + return boolValue ? Visibility.Visible : Visibility.Collapsed; + } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => (Visibility)value == Visibility.Visible; diff --git a/src/modules/imageresizer/ui/Views/InputPage.xaml b/src/modules/imageresizer/ui/Views/InputPage.xaml index ca42f6d793..b45b2a66bd 100644 --- a/src/modules/imageresizer/ui/Views/InputPage.xaml +++ b/src/modules/imageresizer/ui/Views/InputPage.xaml @@ -7,6 +7,23 @@ xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" xmlns:v="clr-namespace:ImageResizer.Views"> + <UserControl.Resources> + <Style + x:Key="ReadableDisabledButtonStyle" + BasedOn="{StaticResource {x:Type ui:Button}}" + TargetType="ui:Button"> + <Style.Triggers> + <Trigger Property="IsEnabled" Value="False"> + <!-- Improved disabled state: keep readable but clearly disabled --> + <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> + <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> + <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" /> + <Setter Property="Opacity" Value="0.75" /> + </Trigger> + </Style.Triggers> + </Style> + </UserControl.Resources> + <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> @@ -15,61 +32,67 @@ <!-- other controls --> </Grid.RowDefinitions> - <ComboBox - Name="SizeComboBox" - Grid.Row="0" - Height="64" - Margin="16" - HorizontalAlignment="Stretch" - VerticalAlignment="Stretch" - VerticalContentAlignment="Stretch" - AutomationProperties.HelpText="{Binding Settings.SelectedSize, Converter={StaticResource SizeTypeToHelpTextConverter}}" - AutomationProperties.Name="{x:Static p:Resources.Image_Sizes}" - ItemsSource="{Binding Settings.AllSizes}" - SelectedIndex="{Binding Settings.SelectedSizeIndex}"> - <ComboBox.ItemContainerStyle> - <Style BasedOn="{StaticResource {x:Type ComboBoxItem}}" TargetType="ComboBoxItem"> - <Setter Property="AutomationProperties.Name" Value="{Binding Name}" /> - <Setter Property="AutomationProperties.HelpText" Value="{Binding ., Converter={StaticResource SizeTypeToHelpTextConverter}}" /> - </Style> - </ComboBox.ItemContainerStyle> - <ComboBox.Resources> - <DataTemplate DataType="{x:Type m:ResizeSize}"> - <Grid VerticalAlignment="Center"> - <Grid.RowDefinitions> - <RowDefinition Height="Auto" /> - <RowDefinition Height="Auto" /> - </Grid.RowDefinitions> - <TextBlock Style="{StaticResource BodyStrongTextBlockStyle}" Text="{Binding Name}" /> - <StackPanel Grid.Row="1" Orientation="Horizontal"> - <TextBlock Text="{Binding Fit, Converter={StaticResource EnumValueConverter}, ConverterParameter=ThirdPersonSingular}" /> - <TextBlock - Margin="4,0,0,0" - Style="{StaticResource BodyStrongTextBlockStyle}" - Text="{Binding Width, Converter={StaticResource AutoDoubleConverter}, ConverterParameter=Auto}" /> - <TextBlock - Margin="4,0,0,0" - Foreground="{DynamicResource TextFillColorSecondaryBrush}" - Text="×" - Visibility="{Binding ShowHeight, Converter={StaticResource BoolValueConverter}}" /> - <TextBlock - Margin="4,0,0,0" - Style="{StaticResource BodyStrongTextBlockStyle}" - Text="{Binding Height, Converter={StaticResource AutoDoubleConverter}, ConverterParameter=Auto}" - Visibility="{Binding ShowHeight, Converter={StaticResource BoolValueConverter}}" /> - <TextBlock Margin="4,0,0,0" Text="{Binding Unit, Converter={StaticResource EnumValueConverter}, ConverterParameter=ToLower}" /> + <StackPanel Grid.Row="0" Margin="16"> + <ComboBox + Name="SizeComboBox" + Height="64" + HorizontalAlignment="Stretch" + VerticalAlignment="Stretch" + VerticalContentAlignment="Stretch" + AutomationProperties.HelpText="{Binding Settings.SelectedSize, Converter={StaticResource SizeTypeToHelpTextConverter}}" + AutomationProperties.Name="{x:Static p:Resources.Image_Sizes}" + ItemsSource="{Binding Settings.AllSizes}" + SelectedItem="{Binding Settings.SelectedSize, Mode=TwoWay}"> + <ComboBox.ItemContainerStyle> + <Style BasedOn="{StaticResource {x:Type ComboBoxItem}}" TargetType="ComboBoxItem"> + <Setter Property="AutomationProperties.Name" Value="{Binding Name}" /> + <Setter Property="AutomationProperties.HelpText" Value="{Binding ., Converter={StaticResource SizeTypeToHelpTextConverter}}" /> + </Style> + </ComboBox.ItemContainerStyle> + <ComboBox.Resources> + <DataTemplate DataType="{x:Type m:ResizeSize}"> + <Grid VerticalAlignment="Center"> + <Grid.RowDefinitions> + <RowDefinition Height="Auto" /> + <RowDefinition Height="Auto" /> + </Grid.RowDefinitions> + <TextBlock Style="{StaticResource BodyStrongTextBlockStyle}" Text="{Binding Name}" /> + <StackPanel Grid.Row="1" Orientation="Horizontal"> + <TextBlock Text="{Binding Fit, Converter={StaticResource EnumValueConverter}, ConverterParameter=ThirdPersonSingular}" /> + <TextBlock + Margin="4,0,0,0" + Style="{StaticResource BodyStrongTextBlockStyle}" + Text="{Binding Width, Converter={StaticResource AutoDoubleConverter}, ConverterParameter=Auto}" /> + <TextBlock + Margin="4,0,0,0" + Foreground="{DynamicResource TextFillColorSecondaryBrush}" + Text="×" + Visibility="{Binding ShowHeight, Converter={StaticResource BoolValueConverter}}" /> + <TextBlock + Margin="4,0,0,0" + Style="{StaticResource BodyStrongTextBlockStyle}" + Text="{Binding Height, Converter={StaticResource AutoDoubleConverter}, ConverterParameter=Auto}" + Visibility="{Binding ShowHeight, Converter={StaticResource BoolValueConverter}}" /> + <TextBlock Margin="4,0,0,0" Text="{Binding Unit, Converter={StaticResource EnumValueConverter}, ConverterParameter=ToLower}" /> + </StackPanel> + </Grid> + </DataTemplate> + + <DataTemplate DataType="{x:Type m:CustomSize}"> + <Grid VerticalAlignment="Center"> + <TextBlock FontWeight="SemiBold" Text="{Binding Name}" /> + </Grid> + </DataTemplate> + + <DataTemplate DataType="{x:Type m:AiSize}"> + <StackPanel VerticalAlignment="Center" Orientation="Vertical"> + <TextBlock FontWeight="SemiBold" Text="{x:Static p:Resources.Input_AiSuperResolution}" /> + <TextBlock Text="{x:Static p:Resources.Input_AiSuperResolutionDescription}" /> </StackPanel> - </Grid> - </DataTemplate> - - <DataTemplate DataType="{x:Type m:CustomSize}"> - <Grid VerticalAlignment="Center"> - <TextBlock FontWeight="SemiBold" Text="{Binding Name}" /> - </Grid> - </DataTemplate> - </ComboBox.Resources> - </ComboBox> - + </DataTemplate> + </ComboBox.Resources> + </ComboBox> + </StackPanel> <Grid Grid.Row="1"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> @@ -84,6 +107,90 @@ BorderBrush="{DynamicResource CardStrokeColorDefaultBrush}" BorderThickness="0,1,0,0" /> + <!-- AI Configuration Panel --> + <Grid Margin="16"> + <!-- AI Model Download Prompt --> + <StackPanel> + <StackPanel.Style> + <Style TargetType="StackPanel"> + <Setter Property="Visibility" Value="Collapsed" /> + <Style.Triggers> + <DataTrigger Binding="{Binding ShowModelDownloadPrompt}" Value="True"> + <Setter Property="Visibility" Value="Visible" /> + </DataTrigger> + </Style.Triggers> + </Style> + </StackPanel.Style> + + <ui:InfoBar + IsClosable="False" + IsOpen="True" + Message="{Binding ModelStatusMessage}" + Severity="Informational" /> + + <ui:Button + Margin="0,8,0,0" + HorizontalAlignment="Stretch" + Appearance="Primary" + Command="{Binding DownloadModelCommand}" + Content="{x:Static p:Resources.Input_AiModelDownloadButton}" + Visibility="{Binding IsModelDownloading, Converter={StaticResource BoolValueConverter}, ConverterParameter=Inverted}" /> + + <StackPanel Margin="0,8,0,0" Visibility="{Binding IsModelDownloading, Converter={StaticResource BoolValueConverter}}"> + <ui:ProgressRing IsIndeterminate="True" /> + <TextBlock + Margin="0,8,0,0" + HorizontalAlignment="Center" + Text="{Binding ModelStatusMessage}" /> + </StackPanel> + </StackPanel> + + <!-- AI Scale Controls --> + <StackPanel> + <StackPanel.Style> + <Style TargetType="StackPanel"> + <Setter Property="Visibility" Value="Collapsed" /> + <Style.Triggers> + <DataTrigger Binding="{Binding ShowAiControls}" Value="True"> + <Setter Property="Visibility" Value="Visible" /> + </DataTrigger> + </Style.Triggers> + </Style> + </StackPanel.Style> + + <Grid> + <TextBlock Text="{x:Static p:Resources.Input_AiCurrentLabel}" /> + <TextBlock HorizontalAlignment="Right" Text="{Binding AiScaleDisplay}" /> + </Grid> + + <Slider + Margin="0,8,0,0" + AutomationProperties.Name="{x:Static p:Resources.Input_AiScaleLabel}" + IsSelectionRangeEnabled="False" + IsSnapToTickEnabled="True" + Maximum="8" + Minimum="1" + TickFrequency="1" + TickPlacement="BottomRight" + Ticks="1,2,3,4,5,6,7,8" + Value="{Binding AiSuperResolutionScale, Mode=TwoWay}" /> + + <StackPanel Margin="0,16,0,0" Visibility="{Binding ShowAiSizeDescriptions, Converter={StaticResource BoolValueConverter}}"> + <Grid> + <TextBlock Foreground="{DynamicResource TextFillColorSecondaryBrush}" Text="{x:Static p:Resources.Input_AiCurrentLabel}" /> + <TextBlock + HorizontalAlignment="Right" + Foreground="{DynamicResource TextFillColorSecondaryBrush}" + Text="{Binding CurrentResolutionDescription}" /> + </Grid> + <Grid Margin="0,8,0,0"> + <TextBlock Text="{x:Static p:Resources.Input_AiNewLabel}" /> + <TextBlock HorizontalAlignment="Right" Text="{Binding NewResolutionDescription}" /> + </Grid> + </StackPanel> + </StackPanel> + </Grid> + <!-- "Custom" input matrix --> <Grid Margin="16" Visibility="{Binding ElementName=SizeComboBox, Path=SelectedValue, Converter={StaticResource SizeTypeToVisibilityConverter}}"> <Grid.ColumnDefinitions> @@ -280,7 +387,8 @@ Appearance="Primary" AutomationProperties.Name="{x:Static p:Resources.Resize_Tooltip}" Command="{Binding ResizeCommand}" - IsDefault="True"> + IsDefault="True" + Style="{StaticResource ReadableDisabledButtonStyle}"> <StackPanel Orientation="Horizontal"> <ui:SymbolIcon FontSize="16" Symbol="ResizeLarge16" /> <TextBlock Margin="8,0,0,0" Text="{x:Static p:Resources.Input_Resize}" /> diff --git a/src/modules/keyboardmanager/KeyboardManagerEngine/main.cpp b/src/modules/keyboardmanager/KeyboardManagerEngine/main.cpp index 5969ed6cfd..df48555df5 100644 --- a/src/modules/keyboardmanager/KeyboardManagerEngine/main.cpp +++ b/src/modules/keyboardmanager/KeyboardManagerEngine/main.cpp @@ -51,7 +51,8 @@ int WINAPI wWinMain(_In_ HINSTANCE /*hInstance*/, auto mainThreadId = GetCurrentThreadId(); - EventWaiter ev = EventWaiter(CommonSharedConstants::TERMINATE_KBM_SHARED_EVENT, [&](int) { + EventWaiter ev; + ev.start(CommonSharedConstants::TERMINATE_KBM_SHARED_EVENT, [&](DWORD) { PostThreadMessage(mainThreadId, WM_QUIT, 0, 0); }); diff --git a/src/modules/keyboardmanager/KeyboardManagerEngineLibrary/KeyboardManager.cpp b/src/modules/keyboardmanager/KeyboardManagerEngineLibrary/KeyboardManager.cpp index 8d9ec63698..3eb3261524 100644 --- a/src/modules/keyboardmanager/KeyboardManagerEngineLibrary/KeyboardManager.cpp +++ b/src/modules/keyboardmanager/KeyboardManagerEngineLibrary/KeyboardManager.cpp @@ -70,7 +70,7 @@ KeyboardManager::KeyboardManager() }; editorIsRunningEvent = CreateEvent(nullptr, true, false, KeyboardManagerConstants::EditorWindowEventName.c_str()); - settingsEventWaiter = EventWaiter(KeyboardManagerConstants::SettingsEventName, changeSettingsCallback); + settingsEventWaiter.start(KeyboardManagerConstants::SettingsEventName, changeSettingsCallback); } void KeyboardManager::LoadSettings() diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.PowerToys/Components/Utility.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.PowerToys/Components/Utility.cs index 03f0090c80..0b154f8e9f 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.PowerToys/Components/Utility.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.PowerToys/Components/Utility.cs @@ -98,7 +98,7 @@ namespace Microsoft.PowerToys.Run.Plugin.PowerToys.Components AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift, Action = _ => { - SettingsDeepLink.OpenSettings(settingsWindow.Value, false); + SettingsDeepLink.OpenSettings(settingsWindow.Value); return true; }, }); diff --git a/src/modules/peek/Peek.FilePreviewer/Previewers/MediaPreviewer/ImagePreviewer.cs b/src/modules/peek/Peek.FilePreviewer/Previewers/MediaPreviewer/ImagePreviewer.cs index 47e488c4d3..fd190bf72a 100644 --- a/src/modules/peek/Peek.FilePreviewer/Previewers/MediaPreviewer/ImagePreviewer.cs +++ b/src/modules/peek/Peek.FilePreviewer/Previewers/MediaPreviewer/ImagePreviewer.cs @@ -54,8 +54,6 @@ namespace Peek.FilePreviewer.Previewers private bool IsPng() => Item.Extension == ".png"; - private bool IsSvg() => Item.Extension == ".svg"; - private bool IsQoi() => Item.Extension == ".qoi"; private DispatcherQueue Dispatcher { get; } @@ -63,7 +61,7 @@ namespace Peek.FilePreviewer.Previewers private static readonly HashSet<string> _supportedFileTypes = BitmapDecoder.GetDecoderInformationEnumerator() .SelectMany(di => di.FileExtensions) - .Union([".svg", ".qoi"]) + .Union([".qoi"]) .ToHashSet(StringComparer.OrdinalIgnoreCase); public static bool IsItemSupported(IFileSystemItem item) @@ -75,15 +73,7 @@ namespace Peek.FilePreviewer.Previewers { cancellationToken.ThrowIfCancellationRequested(); - if (IsSvg()) - { - var size = await Task.Run(Item.GetSvgSize); - if (size != null) - { - ImageSize = size.Value; - } - } - else if (IsQoi()) + if (IsQoi()) { var size = await Task.Run(Item.GetQoiSize); if (size != null) @@ -176,31 +166,16 @@ namespace Peek.FilePreviewer.Previewers { cancellationToken.ThrowIfCancellationRequested(); - using FileStream stream = ReadHelper.OpenReadOnly(Item.Path); - - if (IsSvg()) - { - var source = new SvgImageSource(); - source.RasterizePixelHeight = ImageSize?.Height ?? 0; - source.RasterizePixelWidth = ImageSize?.Width ?? 0; - - var loadStatus = await source.SetSourceAsync(stream.AsRandomAccessStream()); - if (loadStatus != SvgImageSourceLoadStatus.Success) - { - Logger.LogError("Error loading SVG: " + loadStatus.ToString()); - throw new ImageLoadingException(nameof(source)); - } - - Preview = source; - } - else if (IsQoi()) + if (IsQoi()) { + using FileStream stream = ReadHelper.OpenReadOnly(Item.Path); using var bitmap = QoiImage.FromStream(stream); Preview = await BitmapHelper.BitmapToImageSource(bitmap, true, cancellationToken); } else { + using FileStream stream = ReadHelper.OpenReadOnly(Item.Path); Preview = new BitmapImage(); await ((BitmapImage)Preview).SetSourceAsync(stream.AsRandomAccessStream()); } diff --git a/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/WebBrowserPreviewer.cs b/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/WebBrowserPreviewer.cs index 03f4461e50..4d4cf315b4 100644 --- a/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/WebBrowserPreviewer.cs +++ b/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/WebBrowserPreviewer.cs @@ -33,6 +33,10 @@ namespace Peek.FilePreviewer.Previewers // Markdown ".md", + + // SVG - using WebView2 for better compatibility with complex SVGs + // (e.g., from Adobe Illustrator, Inkscape) + ".svg", }; [ObservableProperty] @@ -111,9 +115,10 @@ namespace Peek.FilePreviewer.Previewers { bool isHtml = File.Extension == ".html" || File.Extension == ".htm"; bool isMarkdown = File.Extension == ".md"; + bool isSvg = File.Extension == ".svg"; bool supportedByMonaco = MonacoHelper.SupportedMonacoFileTypes.Contains(File.Extension); - bool useMonaco = supportedByMonaco && !isHtml && !isMarkdown; + bool useMonaco = supportedByMonaco && !isHtml && !isMarkdown && !isSvg; IsDevFilePreview = supportedByMonaco; CustomContextMenu = useMonaco; @@ -128,6 +133,13 @@ namespace Peek.FilePreviewer.Previewers var raw = await ReadHelper.Read(File.Path.ToString()); Preview = new Uri(MarkdownHelper.PreviewTempFile(raw, File.Path, TempFolderPath.Path)); } + else if (isSvg) + { + // SVG files are rendered directly by WebView2 for better compatibility + // with complex SVGs from Adobe Illustrator, Inkscape, etc. + IsDevFilePreview = false; + Preview = new Uri(File.Path); + } else { // Simple html file to preview. Shouldn't do things like enabling scripts or using a virtual mapped directory. diff --git a/src/modules/powerrename/PowerRenameUILib/packages.config b/src/modules/powerrename/PowerRenameUILib/packages.config deleted file mode 100644 index 74414b51bc..0000000000 --- a/src/modules/powerrename/PowerRenameUILib/packages.config +++ /dev/null @@ -1,19 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<packages> - <package id="boost" version="1.87.0" targetFramework="native" /> - <package id="boost_regex-vc143" version="1.87.0" targetFramework="native" /> - <package id="Microsoft.Web.WebView2" version="1.0.2903.40" targetFramework="native" /> - <package id="Microsoft.Windows.CppWinRT" version="2.0.240111.5" targetFramework="native" /> - <package id="Microsoft.Windows.ImplementationLibrary" version="1.0.231216.1" targetFramework="native" /> - <package id="Microsoft.Windows.SDK.BuildTools" version="10.0.26100.4188" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK" version="1.8.250907003" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.Base" version="1.8.250831001" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.Foundation" version="1.8.250906002" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.WinUI" version="1.8.250906003" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.Runtime" version="1.8.250907003" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.DWrite" version="1.8.25090401" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.InteractiveExperiences" version="1.8.250906004" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.Widgets" version="1.8.250904007" targetFramework="native" /> - <package id="Microsoft.WindowsAppSDK.AI" version="1.8.37" targetFramework="native" /> - <package id="Microsoft.Windows.SDK.BuildTools.MSIX" version="1.7.20250829.1" targetFramework="native" /> -</packages> \ No newline at end of file diff --git a/src/modules/powerrename/lib/Helpers.cpp b/src/modules/powerrename/lib/Helpers.cpp index c3902c7b93..03977a3732 100644 --- a/src/modules/powerrename/lib/Helpers.cpp +++ b/src/modules/powerrename/lib/Helpers.cpp @@ -407,15 +407,18 @@ HRESULT GetDatedFileName(_Out_ PWSTR result, UINT cchMax, _In_ PCWSTR source, SY hour12 = 12; } + // Order matters. Longer patterns are processed before any prefixes. + // Years. StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%04d"), L"$01", fileTime.wYear); res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$YYYY"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%02d"), L"$01", (fileTime.wYear % 100)); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$YY(?![A-Z])"), replaceTerm); // Negative lookahead prevents matching $YYY, $YYYY, or metadata patterns + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$YY"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%d"), L"$01", (fileTime.wYear % 10)); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$Y(?![A-Z])"), replaceTerm); // Negative lookahead prevents matching $YY, $YYYY, or metadata patterns + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$Y"), replaceTerm); + // Months. GetDateFormatEx(localeName, NULL, &fileTime, L"MMMM", formattedDate, MAX_PATH, NULL); formattedDate[0] = towupper(formattedDate[0]); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%s"), L"$01", formattedDate); @@ -424,14 +427,15 @@ HRESULT GetDatedFileName(_Out_ PWSTR result, UINT cchMax, _In_ PCWSTR source, SY GetDateFormatEx(localeName, NULL, &fileTime, L"MMM", formattedDate, MAX_PATH, NULL); formattedDate[0] = towupper(formattedDate[0]); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%s"), L"$01", formattedDate); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$MMM(?!M)"), replaceTerm); // Negative lookahead prevents matching $MMMM + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$MMM"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%02d"), L"$01", fileTime.wMonth); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$MM(?![A-Z])"), replaceTerm); // Negative lookahead prevents matching $MMM, $MMMM, or metadata patterns + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$MM"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%d"), L"$01", fileTime.wMonth); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$M(?![A-Z])"), replaceTerm); // Negative lookahead prevents matching $MM, $MMM, $MMMM, or metadata patterns + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$M"), replaceTerm); + // Days. GetDateFormatEx(localeName, NULL, &fileTime, L"dddd", formattedDate, MAX_PATH, NULL); formattedDate[0] = towupper(formattedDate[0]); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%s"), L"$01", formattedDate); @@ -440,19 +444,27 @@ HRESULT GetDatedFileName(_Out_ PWSTR result, UINT cchMax, _In_ PCWSTR source, SY GetDateFormatEx(localeName, NULL, &fileTime, L"ddd", formattedDate, MAX_PATH, NULL); formattedDate[0] = towupper(formattedDate[0]); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%s"), L"$01", formattedDate); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$DDD(?![A-Z])"), replaceTerm); // Negative lookahead prevents matching $DDDD or metadata patterns + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$DDD"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%02d"), L"$01", fileTime.wDay); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$DD(?![A-Z])"), replaceTerm); // Negative lookahead prevents matching $DDD, $DDDD, or metadata patterns + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$DD"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%d"), L"$01", fileTime.wDay); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$D(?![A-Z])"), replaceTerm); // Negative lookahead prevents matching $DD, $DDD, $DDDD, or metadata patterns like $DATE_TAKEN_YYYY + // $D overlaps with metadata patterns like $DATE_TAKEN_YYYY, so we use negative + // lookahead to prevent matching those. + res = regex_replace( + res, + std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$D(?!(ATE_TAKEN_|ESCRIPTION|OCUMENT_ID))"), /* #no-spell-check-line */ + replaceTerm); + // Time. StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%02d"), L"$01", hour12); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$HH(?![A-Z])"), replaceTerm); // Negative lookahead prevents matching $HHH or metadata patterns + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$HH"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%d"), L"$01", hour12); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$H(?![A-Z])"), replaceTerm); // Negative lookahead prevents matching $HH or metadata patterns + // $H overlaps with metadata's $HEIGHT, so we use negative lookahead to prevent + // matching that. + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$H(?!(EIGHT))"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%s"), L"$01", (fileTime.wHour < 12) ? L"AM" : L"PM"); res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$TT"), replaceTerm); @@ -461,31 +473,31 @@ HRESULT GetDatedFileName(_Out_ PWSTR result, UINT cchMax, _In_ PCWSTR source, SY res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$tt"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%02d"), L"$01", fileTime.wHour); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$hh(?!h)"), replaceTerm); // Negative lookahead prevents matching $hhh + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$hh"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%d"), L"$01", fileTime.wHour); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$h(?!h)"), replaceTerm); // Negative lookahead prevents matching $hh + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$h"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%02d"), L"$01", fileTime.wMinute); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$mm(?!m)"), replaceTerm); // Negative lookahead prevents matching $mmm + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$mm"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%d"), L"$01", fileTime.wMinute); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$m(?!m)"), replaceTerm); // Negative lookahead prevents matching $mm + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$m"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%02d"), L"$01", fileTime.wSecond); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$ss(?!s)"), replaceTerm); // Negative lookahead prevents matching $sss + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$ss"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%d"), L"$01", fileTime.wSecond); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$s(?!s)"), replaceTerm); // Negative lookahead prevents matching $ss + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$s"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%03d"), L"$01", fileTime.wMilliseconds); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$fff(?!f)"), replaceTerm); // Negative lookahead prevents matching $ffff + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$fff"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%02d"), L"$01", fileTime.wMilliseconds / 10); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$ff(?!f)"), replaceTerm); // Negative lookahead prevents matching $fff + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$ff"), replaceTerm); StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%d"), L"$01", fileTime.wMilliseconds / 100); - res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$f(?!f)"), replaceTerm); // Negative lookahead prevents matching $ff or $fff + res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$f"), replaceTerm); hr = StringCchCopy(result, cchMax, res.c_str()); } diff --git a/src/modules/powerrename/unittests/HelpersTests.cpp b/src/modules/powerrename/unittests/HelpersTests.cpp index 9a6d1b2028..426bebab02 100644 --- a/src/modules/powerrename/unittests/HelpersTests.cpp +++ b/src/modules/powerrename/unittests/HelpersTests.cpp @@ -507,24 +507,26 @@ namespace HelpersTests return testTime; } - // Category 1: Tests for invalid patterns with extra characters (verify negative lookahead prevents wrong matching) + // Category 1: Tests for patterns with extra characters. Verifies negative + // lookahead doesn't cause issues with partially matched patterns and the + // ordering of pattern matches is correct, i.e. longer patterns are matched + // first. - TEST_METHOD(InvalidPattern_YYY_NotMatched) + TEST_METHOD(ValidPattern_YYY_PartiallyMatched) { - // Test $YYY (3 Y's) is not a valid pattern and should remain unchanged - // Negative lookahead in $YY(?!Y) prevents matching $YYY + // Test $YYY (3 Y's) is recognized as a valid pattern $YY plus a verbatim 'Y' SYSTEMTIME testTime = GetTestTime(); wchar_t result[MAX_PATH] = { 0 }; HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$YYY", testTime); Assert::IsTrue(SUCCEEDED(hr)); - Assert::AreEqual(L"file_$YYY", result); // $YYY is invalid, should remain unchanged + Assert::AreEqual(L"file_24Y", result); } - TEST_METHOD(InvalidPattern_DDD_NotPartiallyMatched) + TEST_METHOD(ValidPattern_DDD_Matched) { // Test that $DDD (short weekday) is not confused with $DD (2-digit day) - // This verifies negative lookahead works correctly + // Verifies that the matching of $DDD before $DD works correctly SYSTEMTIME testTime = GetTestTime(); wchar_t result[MAX_PATH] = { 0 }; HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$DDD", testTime); @@ -533,9 +535,10 @@ namespace HelpersTests Assert::AreEqual(L"file_Fri", result); // Should be "Fri", not "15D" } - TEST_METHOD(InvalidPattern_MMM_NotPartiallyMatched) + TEST_METHOD(ValidPattern_MMM_Matched) { // Test that $MMM (short month name) is not confused with $MM (2-digit month) + // Verifies that the matching of $MMM before $MM works correctly SYSTEMTIME testTime = GetTestTime(); wchar_t result[MAX_PATH] = { 0 }; HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$MMM", testTime); @@ -544,15 +547,16 @@ namespace HelpersTests Assert::AreEqual(L"file_Mar", result); // Should be "Mar", not "03M" } - TEST_METHOD(InvalidPattern_HHH_NotMatched) + TEST_METHOD(ValidPattern_HHH_PartiallyMatched) { - // Test $HHH (3 H's) is not valid and negative lookahead prevents $HH from matching + // Test $HHH (3 H's) should match $HH and leave extra H unchanged + // Also confirms that $HH is matched before $H SYSTEMTIME testTime = GetTestTime(); wchar_t result[MAX_PATH] = { 0 }; HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$HHH", testTime); Assert::IsTrue(SUCCEEDED(hr)); - Assert::AreEqual(L"file_$HHH", result); // Should remain unchanged + Assert::AreEqual(L"file_02H", result); } TEST_METHOD(SeparatedPatterns_SingleY) @@ -669,9 +673,9 @@ namespace HelpersTests Assert::AreEqual(E_INVALIDARG, hr); } - // Category 4: Tests to explicitly verify negative lookahead is working + // Category 4: Tests to explicitly verify execution order - TEST_METHOD(NegativeLookahead_YearNotMatchedInYYYY) + TEST_METHOD(ExecutionOrder_YearNotMatchedInYYYY) { // Verify $Y doesn't match when part of $YYYY SYSTEMTIME testTime = GetTestTime(); @@ -682,9 +686,9 @@ namespace HelpersTests Assert::AreEqual(L"file_2024", result); // Should be "2024", not "202Y" } - TEST_METHOD(NegativeLookahead_MonthNotMatchedInMMM) + TEST_METHOD(ExecutionOrder_MonthNotMatchedInMMM) { - // Verify $M doesn't match when part of $MMM + // Verify $M or $MM don't match when $MMM is given SYSTEMTIME testTime = GetTestTime(); wchar_t result[MAX_PATH] = { 0 }; HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$MMM", testTime); @@ -693,9 +697,9 @@ namespace HelpersTests Assert::AreEqual(L"file_Mar", result); // Should be "Mar", not "3ar" } - TEST_METHOD(NegativeLookahead_DayNotMatchedInDDDD) + TEST_METHOD(ExecutionOrder_DayNotMatchedInDDDD) { - // Verify $D doesn't match when part of $DDDD + // Verify $D or $DD don't match when $DDDD is given SYSTEMTIME testTime = GetTestTime(); wchar_t result[MAX_PATH] = { 0 }; HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$DDDD", testTime); @@ -704,7 +708,7 @@ namespace HelpersTests Assert::AreEqual(L"file_Friday", result); // Should be "Friday", not "15riday" } - TEST_METHOD(NegativeLookahead_HourNotMatchedInHH) + TEST_METHOD(ExecutionOrder_HourNotMatchedInHH) { // Verify $H doesn't match when part of $HH // Note: $HH is 12-hour format, so 14:00 (2 PM) displays as "02" @@ -716,9 +720,9 @@ namespace HelpersTests Assert::AreEqual(L"file_02", result); // 14:00 in 12-hour format is "02 PM" } - TEST_METHOD(NegativeLookahead_MillisecondNotMatchedInFFF) + TEST_METHOD(ExecutionOrder_MillisecondNotMatchedInFFF) { - // Verify $f doesn't match when part of $fff + // Verify $f or $ff don't match when $fff is given SYSTEMTIME testTime = GetTestTime(); wchar_t result[MAX_PATH] = { 0 }; HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$fff", testTime); @@ -762,5 +766,68 @@ namespace HelpersTests Assert::IsTrue(SUCCEEDED(hr)); Assert::AreEqual(L"15-15-Fri-Friday", result); } + + // Category 6: Specific bug fixes and collision avoidance + + TEST_METHOD(BugFix_DDT_AllowsSuffixT) + { + // #44202 - $DDT should be allowed and matched as $DD plus verbatim 'T'. It + // was previously blocked due to the negative lookahead for any capital + // letter after $DD. + SYSTEMTIME testTime = GetTestTime(); + wchar_t result[MAX_PATH] = { 0 }; + HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$DDT", testTime); + + Assert::IsTrue(SUCCEEDED(hr)); + Assert::AreEqual(L"file_15T", result); + } + + TEST_METHOD(RelaxedConstraint_VerbatimCapitalAfterPatterns) + { + // Verify that patterns can be followed by capital letters that are not part + // of longer patterns, e.g., $DDC should match $DD + 'C'. + SYSTEMTIME testTime = GetTestTime(); + wchar_t result[MAX_PATH] = { 0 }; + HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$YYYYA_$MMB_$DDC", testTime); /* #no-spell-check-line */ + + Assert::IsTrue(SUCCEEDED(hr)); + Assert::AreEqual(L"file_2024A_03B_15C", result); + } + + TEST_METHOD(Collision_DateTaken_Protected) + { + // Verify that date patterns do not collide with metadata patterns like + // DATE_TAKEN_YYYY. + SYSTEMTIME testTime = GetTestTime(); + wchar_t result[MAX_PATH] = { 0 }; + HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$DATE_TAKEN_YYYY", testTime); + + Assert::IsTrue(SUCCEEDED(hr)); + Assert::AreEqual(L"file_$DATE_TAKEN_YYYY", result); // Not replaced + } + + TEST_METHOD(Collision_Height_Protected) + { + // Verify that HEIGHT metadata pattern does not collide with date pattern $H. + SYSTEMTIME testTime = GetTestTime(); + wchar_t result[MAX_PATH] = { 0 }; + HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$HEIGHT", testTime); + + Assert::IsTrue(SUCCEEDED(hr)); + Assert::AreEqual(L"file_$HEIGHT", result); // Not replaced + } + + TEST_METHOD(Collision_SafeSuffix_Deer) + { + // Verifies that patterns can be safely followed by certain suffix letters as + // long as they don't match a longer pattern. $DEER should be matched as + // $D + 'EER' + SYSTEMTIME testTime = GetTestTime(); + wchar_t result[MAX_PATH] = { 0 }; + HRESULT hr = GetDatedFileName(result, MAX_PATH, L"file_$DEER", testTime); + + Assert::IsTrue(SUCCEEDED(hr)); + Assert::AreEqual(L"file_15EER", result); + } }; } diff --git a/src/modules/registrypreview/RegistryPreviewExt/dllmain.cpp b/src/modules/registrypreview/RegistryPreviewExt/dllmain.cpp index fb39833553..70e63d9bb0 100644 --- a/src/modules/registrypreview/RegistryPreviewExt/dllmain.cpp +++ b/src/modules/registrypreview/RegistryPreviewExt/dllmain.cpp @@ -161,7 +161,7 @@ public: init_settings(); triggerEvent = CreateEvent(nullptr, false, false, CommonSharedConstants::REGISTRY_PREVIEW_TRIGGER_EVENT); - triggerEventWaiter = EventWaiter(CommonSharedConstants::REGISTRY_PREVIEW_TRIGGER_EVENT, [this](int) { + triggerEventWaiter.start(CommonSharedConstants::REGISTRY_PREVIEW_TRIGGER_EVENT, [this](DWORD) { on_hotkey(0); }); } diff --git a/src/runner/ai_detection.h b/src/runner/ai_detection.h new file mode 100644 index 0000000000..41aa1dc328 --- /dev/null +++ b/src/runner/ai_detection.h @@ -0,0 +1,11 @@ +#pragma once + +// Detect AI capabilities by calling ImageResizer in detection mode. +// This runs in a background thread to avoid blocking. +// ImageResizer writes the result to a cache file that it reads on normal startup. +// +// Parameters: +// skipSettingsCheck - If true, skip checking if ImageResizer is enabled in settings. +// Use this when called from apply_general_settings where we know +// ImageResizer is being enabled but settings file may not be saved yet. +void DetectAiCapabilitiesAsync(bool skipSettingsCheck = false); diff --git a/src/runner/general_settings.cpp b/src/runner/general_settings.cpp index 9f8ceeb8ad..c6770731a6 100644 --- a/src/runner/general_settings.cpp +++ b/src/runner/general_settings.cpp @@ -10,6 +10,7 @@ #include <common/themes/windows_colors.h> #include "trace.h" +#include "ai_detection.h" #include <common/utils/elevation.h> #include <common/version/version.h> #include <common/utils/resources.h> @@ -279,6 +280,13 @@ void apply_general_settings(const json::JsonObject& general_configs, bool save) powertoy->enable(); auto& hkmng = HotkeyConflictDetector::HotkeyConflictManager::GetInstance(); hkmng.EnableHotkeyByModule(name); + + // Trigger AI capability detection when ImageResizer is enabled + if (name == L"Image Resizer") + { + Logger::info(L"ImageResizer enabled, triggering AI capability detection"); + DetectAiCapabilitiesAsync(true); // Skip settings check since we know it's being enabled + } } else { diff --git a/src/runner/main.cpp b/src/runner/main.cpp index e258192bbd..f60445277d 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -34,8 +34,11 @@ #include <Psapi.h> #include <RestartManager.h> +#include <shellapi.h> #include "centralized_kb_hook.h" #include "centralized_hotkeys.h" +#include "ai_detection.h" +#include <common/utils/package.h> #if _DEBUG && _WIN64 #include "unhandled_exception_handler.h" @@ -76,6 +79,87 @@ void chdir_current_executable() } } +// Detect AI capabilities by calling ImageResizer in detection mode. +// This runs in a background thread to avoid blocking the main startup. +// ImageResizer writes the result to a cache file that it reads on normal startup. +void DetectAiCapabilitiesAsync(bool skipSettingsCheck) +{ + std::thread([skipSettingsCheck]() { + try + { + // Check if ImageResizer module is enabled (skip if called from apply_general_settings) + if (!skipSettingsCheck) + { + auto settings = PTSettingsHelper::load_general_settings(); + if (json::has(settings, L"enabled", json::JsonValueType::Object)) + { + auto enabledModules = settings.GetNamedObject(L"enabled"); + if (json::has(enabledModules, L"Image Resizer", json::JsonValueType::Boolean)) + { + bool isEnabled = enabledModules.GetNamedBoolean(L"Image Resizer", false); + if (!isEnabled) + { + Logger::info(L"ImageResizer module is disabled, skipping AI detection"); + return; + } + } + } + } + + // Get ImageResizer.exe path (located in WinUI3Apps folder) + std::wstring imageResizerPath = get_module_folderpath(); + imageResizerPath += L"\\WinUI3Apps\\PowerToys.ImageResizer.exe"; + + if (!std::filesystem::exists(imageResizerPath)) + { + Logger::warn(L"ImageResizer.exe not found at {}, skipping AI detection", imageResizerPath); + return; + } + + Logger::info(L"Starting AI capability detection via ImageResizer"); + + // Call ImageResizer --detect-ai + SHELLEXECUTEINFO sei = { sizeof(sei) }; + sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI; + sei.lpFile = imageResizerPath.c_str(); + sei.lpParameters = L"--detect-ai"; + sei.nShow = SW_HIDE; + + if (ShellExecuteExW(&sei)) + { + // Wait for detection to complete (with timeout) + DWORD waitResult = WaitForSingleObject(sei.hProcess, 30000); // 30 second timeout + CloseHandle(sei.hProcess); + + if (waitResult == WAIT_OBJECT_0) + { + Logger::info(L"AI capability detection completed successfully"); + } + else if (waitResult == WAIT_TIMEOUT) + { + Logger::warn(L"AI capability detection timed out"); + } + else + { + Logger::warn(L"AI capability detection wait failed"); + } + } + else + { + Logger::warn(L"Failed to launch ImageResizer for AI detection, error: {}", GetLastError()); + } + } + catch (const std::exception& e) + { + Logger::error("Exception during AI capability detection: {}", e.what()); + } + catch (...) + { + Logger::error("Unknown exception during AI capability detection"); + } + }).detach(); +} + inline wil::unique_mutex_nothrow create_msi_mutex() { return createAppMutex(POWERTOYS_MSI_MUTEX_NAME); @@ -127,6 +211,18 @@ int runner(bool isProcessElevated, bool openSettings, std::string settingsWindow PeriodicUpdateWorker(); } }.detach(); + // Start AI capability detection in background (Windows 11+ only) + // AI Super Resolution is not supported on Windows 10 + // This calls ImageResizer --detect-ai which writes result to cache file + if (package::IsWin11OrGreater()) + { + DetectAiCapabilitiesAsync(); + } + else + { + Logger::info(L"AI capability detection skipped: Windows 10 does not support AI Super Resolution"); + } + std::thread{ [] { if (updating::uninstall_previous_msix_version_async().get()) { diff --git a/src/runner/runner.vcxproj b/src/runner/runner.vcxproj index 35a68e220f..57cb55b6bd 100644 --- a/src/runner/runner.vcxproj +++ b/src/runner/runner.vcxproj @@ -81,6 +81,7 @@ </ItemGroup> <ItemGroup> <ClInclude Include="ActionRunnerUtils.h" /> + <ClInclude Include="ai_detection.h" /> <ClInclude Include="auto_start_helper.h" /> <ClInclude Include="bug_report.h" /> <ClInclude Include="centralized_hotkeys.h" /> @@ -132,9 +133,6 @@ <Project>{17da04df-e393-4397-9cf0-84dabe11032e}</Project> </ProjectReference> </ItemGroup> - <ItemGroup> - <None Include="packages.config" /> - </ItemGroup> <ItemGroup> <Manifest Include="PowerToys.exe.manifest" /> </ItemGroup> @@ -148,7 +146,6 @@ <ItemGroup> <!-- Remove ALL injected versions of the file --> <ClCompile Remove="@(ClCompile)" Condition="'%(Filename)' == 'WindowsAppRuntimeAutoInitializer'" /> - <!-- Add ONE copy back manually --> <ClCompile Include="$(PkgMicrosoft_WindowsAppSDK_Foundation)\include\WindowsAppRuntimeAutoInitializer.cpp"> <PrecompiledHeader>NotUsing</PrecompiledHeader> diff --git a/src/runner/runner.vcxproj.filters b/src/runner/runner.vcxproj.filters index 812d7857a2..904e213405 100644 --- a/src/runner/runner.vcxproj.filters +++ b/src/runner/runner.vcxproj.filters @@ -81,6 +81,9 @@ <ClInclude Include="ActionRunnerUtils.h"> <Filter>Utils</Filter> </ClInclude> + <ClInclude Include="ai_detection.h"> + <Filter>Utils</Filter> + </ClInclude> <ClInclude Include="resource.h"> <Filter>Utils</Filter> </ClInclude> @@ -115,7 +118,6 @@ <CopyFileToFolders Include="svgs\icon.ico" /> </ItemGroup> <ItemGroup> - <None Include="packages.config" /> <None Include="runner.base.rc" /> </ItemGroup> <ItemGroup> diff --git a/src/settings-ui/Settings.UI.Library/AdvancedPasteSettings.cs b/src/settings-ui/Settings.UI.Library/AdvancedPasteSettings.cs index ca9cdacff6..be001fd9d6 100644 --- a/src/settings-ui/Settings.UI.Library/AdvancedPasteSettings.cs +++ b/src/settings-ui/Settings.UI.Library/AdvancedPasteSettings.cs @@ -31,7 +31,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library Name = ModuleName; } - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.Library/CmdNotFoundSettings.cs b/src/settings-ui/Settings.UI.Library/CmdNotFoundSettings.cs index 1f48b218c9..e7e1bf8281 100644 --- a/src/settings-ui/Settings.UI.Library/CmdNotFoundSettings.cs +++ b/src/settings-ui/Settings.UI.Library/CmdNotFoundSettings.cs @@ -25,7 +25,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library Name = ModuleName; } - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file ArgumentNullException.ThrowIfNull(settingsUtils); diff --git a/src/settings-ui/Settings.UI.Library/ColorPickerSettings.cs b/src/settings-ui/Settings.UI.Library/ColorPickerSettings.cs index b601b75baa..6935e0afbc 100644 --- a/src/settings-ui/Settings.UI.Library/ColorPickerSettings.cs +++ b/src/settings-ui/Settings.UI.Library/ColorPickerSettings.cs @@ -33,7 +33,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library WriteIndented = true, }; - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.Library/ColorPickerSettingsVersion1.cs b/src/settings-ui/Settings.UI.Library/ColorPickerSettingsVersion1.cs index 1ddff1946f..409a25f578 100644 --- a/src/settings-ui/Settings.UI.Library/ColorPickerSettingsVersion1.cs +++ b/src/settings-ui/Settings.UI.Library/ColorPickerSettingsVersion1.cs @@ -28,7 +28,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library Name = ModuleName; } - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.Library/EnvironmentVariablesSettings.cs b/src/settings-ui/Settings.UI.Library/EnvironmentVariablesSettings.cs index d54641e977..ccb8f1747a 100644 --- a/src/settings-ui/Settings.UI.Library/EnvironmentVariablesSettings.cs +++ b/src/settings-ui/Settings.UI.Library/EnvironmentVariablesSettings.cs @@ -29,7 +29,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library Name = ModuleName; } - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.Library/HostsSettings.cs b/src/settings-ui/Settings.UI.Library/HostsSettings.cs index bb339f178c..8559c94f4f 100644 --- a/src/settings-ui/Settings.UI.Library/HostsSettings.cs +++ b/src/settings-ui/Settings.UI.Library/HostsSettings.cs @@ -29,7 +29,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library Name = ModuleName; } - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.Library/ISettingsPath.cs b/src/settings-ui/Settings.UI.Library/ISettingsPath.cs deleted file mode 100644 index 072058e4bc..0000000000 --- a/src/settings-ui/Settings.UI.Library/ISettingsPath.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -namespace Microsoft.PowerToys.Settings.UI.Library -{ - public interface ISettingsPath - { - bool SettingsFolderExists(string powertoy); - - void CreateSettingsFolder(string powertoy); - - void DeleteSettings(string powertoy = ""); - - string GetSettingsPath(string powertoy, string fileName); - } -} diff --git a/src/settings-ui/Settings.UI.Library/ISettingsUtils.cs b/src/settings-ui/Settings.UI.Library/ISettingsUtils.cs deleted file mode 100644 index 3d3ef95f06..0000000000 --- a/src/settings-ui/Settings.UI.Library/ISettingsUtils.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) Microsoft Corporation -// The Microsoft Corporation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; - -using Microsoft.PowerToys.Settings.UI.Library.Interfaces; - -namespace Microsoft.PowerToys.Settings.UI.Library -{ - public interface ISettingsUtils - { - public const string DefaultFileName = "settings.json"; - - T GetSettings<T>(string powertoy = "", string fileName = DefaultFileName) - where T : ISettingsConfig, new(); - - T GetSettingsOrDefault<T>(string powertoy = "", string fileName = DefaultFileName) - where T : ISettingsConfig, new(); - - void SaveSettings(string jsonSettings, string powertoy = "", string fileName = DefaultFileName); - - bool SettingsExists(string powertoy = "", string fileName = DefaultFileName); - - void DeleteSettings(string powertoy = ""); - - string GetSettingsFilePath(string powertoy = "", string fileName = DefaultFileName); - - T GetSettingsOrDefault<T, T2>(string powertoy = "", string fileName = DefaultFileName, Func<object, object> settingsUpgrader = null) - where T : ISettingsConfig, new() - where T2 : ISettingsConfig, new(); - } -} diff --git a/src/settings-ui/Settings.UI.Library/MouseJumpSettings.cs b/src/settings-ui/Settings.UI.Library/MouseJumpSettings.cs index a4c5a04555..91944368a1 100644 --- a/src/settings-ui/Settings.UI.Library/MouseJumpSettings.cs +++ b/src/settings-ui/Settings.UI.Library/MouseJumpSettings.cs @@ -33,7 +33,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library Version = "1.1"; } - public void Save(ISettingsUtils settingsUtils) + public void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.Library/MouseWithoutBordersSettings.cs b/src/settings-ui/Settings.UI.Library/MouseWithoutBordersSettings.cs index 3cab182fec..5074bf56f7 100644 --- a/src/settings-ui/Settings.UI.Library/MouseWithoutBordersSettings.cs +++ b/src/settings-ui/Settings.UI.Library/MouseWithoutBordersSettings.cs @@ -125,7 +125,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library #pragma warning restore CS0618 } - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.Library/PeekSettings.cs b/src/settings-ui/Settings.UI.Library/PeekSettings.cs index 8bc4f6ee76..a62d6e60e9 100644 --- a/src/settings-ui/Settings.UI.Library/PeekSettings.cs +++ b/src/settings-ui/Settings.UI.Library/PeekSettings.cs @@ -67,7 +67,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library return false; } - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.Library/PowerLauncherSettings.cs b/src/settings-ui/Settings.UI.Library/PowerLauncherSettings.cs index 18d2c2da1c..b9a438f472 100644 --- a/src/settings-ui/Settings.UI.Library/PowerLauncherSettings.cs +++ b/src/settings-ui/Settings.UI.Library/PowerLauncherSettings.cs @@ -35,7 +35,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library Name = ModuleName; } - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.Library/PowerOcrSettings.cs b/src/settings-ui/Settings.UI.Library/PowerOcrSettings.cs index 4a296832ae..f0300922d0 100644 --- a/src/settings-ui/Settings.UI.Library/PowerOcrSettings.cs +++ b/src/settings-ui/Settings.UI.Library/PowerOcrSettings.cs @@ -31,7 +31,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library Name = ModuleName; } - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.Library/SettingPath.cs b/src/settings-ui/Settings.UI.Library/SettingPath.cs index 94c5d83ca1..b97779008c 100644 --- a/src/settings-ui/Settings.UI.Library/SettingPath.cs +++ b/src/settings-ui/Settings.UI.Library/SettingPath.cs @@ -9,7 +9,7 @@ using Microsoft.PowerToys.Settings.UI.Library.Utilities; namespace Microsoft.PowerToys.Settings.UI.Library { - public class SettingPath : ISettingsPath + public class SettingPath { private const string DefaultFileName = "settings.json"; @@ -23,6 +23,11 @@ namespace Microsoft.PowerToys.Settings.UI.Library _path = path ?? throw new ArgumentNullException(nameof(path)); } + public SettingPath() + : this(new FileSystem().Directory, new FileSystem().Path) + { + } + public bool SettingsFolderExists(string powertoy) { return _directory.Exists(System.IO.Path.Combine(Helper.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}")); diff --git a/src/settings-ui/Settings.UI.Library/SettingsFactory.cs b/src/settings-ui/Settings.UI.Library/SettingsFactory.cs index 2bb9e79121..807a25f93c 100644 --- a/src/settings-ui/Settings.UI.Library/SettingsFactory.cs +++ b/src/settings-ui/Settings.UI.Library/SettingsFactory.cs @@ -17,10 +17,10 @@ namespace Microsoft.PowerToys.Settings.UI.Services /// </summary> public class SettingsFactory { - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private readonly Dictionary<string, Type> _settingsTypes; - public SettingsFactory(ISettingsUtils settingsUtils) + public SettingsFactory(SettingsUtils settingsUtils) { _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); _settingsTypes = DiscoverSettingsTypes(); @@ -100,7 +100,7 @@ namespace Microsoft.PowerToys.Settings.UI.Services try { // Create a generic method call to _settingsUtils.GetSettingsOrDefault<T>(moduleKey) - var getSettingsMethod = typeof(ISettingsUtils).GetMethod("GetSettingsOrDefault", new[] { typeof(string), typeof(string) }); + var getSettingsMethod = typeof(SettingsUtils).GetMethod("GetSettingsOrDefault", new[] { typeof(string), typeof(string) }); var genericMethod = getSettingsMethod?.MakeGenericMethod(settingsType); // Call GetSettingsOrDefault<T>(moduleKey) to get fresh settings from file diff --git a/src/settings-ui/Settings.UI.Library/SettingsRepository`1.cs b/src/settings-ui/Settings.UI.Library/SettingsRepository`1.cs index 136b92b63a..f579933828 100644 --- a/src/settings-ui/Settings.UI.Library/SettingsRepository`1.cs +++ b/src/settings-ui/Settings.UI.Library/SettingsRepository`1.cs @@ -16,7 +16,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library { private static readonly Lock _SettingsRepoLock = new Lock(); - private static ISettingsUtils _settingsUtils; + private static SettingsUtils _settingsUtils; private static SettingsRepository<T> settingsRepository; @@ -25,7 +25,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library // Suppressing the warning as this is a singleton class and this method is // necessarily static #pragma warning disable CA1000 // Do not declare static members on generic types - public static SettingsRepository<T> GetInstance(ISettingsUtils settingsUtils) + public static SettingsRepository<T> GetInstance(SettingsUtils settingsUtils) #pragma warning restore CA1000 // Do not declare static members on generic types { // To ensure that only one instance of Settings Repository is created in a multi-threaded environment. diff --git a/src/settings-ui/Settings.UI.Library/SettingsUtils.cs b/src/settings-ui/Settings.UI.Library/SettingsUtils.cs index eb4169f422..7266bd28cf 100644 --- a/src/settings-ui/Settings.UI.Library/SettingsUtils.cs +++ b/src/settings-ui/Settings.UI.Library/SettingsUtils.cs @@ -7,6 +7,7 @@ using System; using System.IO; using System.IO.Abstractions; +using System.Runtime.CompilerServices; using System.Text.Json; using ManagedCommon; @@ -14,12 +15,13 @@ using Microsoft.PowerToys.Settings.UI.Library.Interfaces; namespace Microsoft.PowerToys.Settings.UI.Library { - public class SettingsUtils : ISettingsUtils + // Some functions are marked as virtual to allow mocking in unit tests. + public class SettingsUtils { public const string DefaultFileName = "settings.json"; private const string DefaultModuleName = ""; private readonly IFile _file; - private readonly ISettingsPath _settingsPath; + private readonly SettingPath _settingsPath; private readonly JsonSerializerOptions _serializerOptions; /// <summary> @@ -39,7 +41,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library { } - public SettingsUtils(IFile file, ISettingsPath settingPath, JsonSerializerOptions? serializerOptions = null) + public SettingsUtils(IFile file, SettingPath settingPath, JsonSerializerOptions? serializerOptions = null) { _file = file ?? throw new ArgumentNullException(nameof(file)); _settingsPath = settingPath; @@ -62,7 +64,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library _settingsPath.DeleteSettings(powertoy); } - public T GetSettings<T>(string powertoy = DefaultModuleName, string fileName = DefaultFileName) + public virtual T GetSettings<T>(string powertoy = DefaultModuleName, string fileName = DefaultFileName) where T : ISettingsConfig, new() { if (!SettingsExists(powertoy, fileName)) @@ -87,7 +89,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library /// This function creates a file in the powertoy folder if it does not exist and returns an object with default properties. /// </summary> /// <returns>Deserialized json settings object.</returns> - public T GetSettingsOrDefault<T>(string powertoy = DefaultModuleName, string fileName = DefaultFileName) + public virtual T GetSettingsOrDefault<T>(string powertoy = DefaultModuleName, string fileName = DefaultFileName) where T : ISettingsConfig, new() { try @@ -118,7 +120,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library /// This function creates a file in the powertoy folder if it does not exist and returns an object with default properties. /// </summary> /// <returns>Deserialized json settings object.</returns> - public T GetSettingsOrDefault<T, T2>(string powertoy = DefaultModuleName, string fileName = DefaultFileName, Func<object, object>? settingsUpgrader = null) + public virtual T GetSettingsOrDefault<T, T2>(string powertoy = DefaultModuleName, string fileName = DefaultFileName, Func<object, object>? settingsUpgrader = null) where T : ISettingsConfig, new() where T2 : ISettingsConfig, new() { @@ -203,7 +205,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library } // Save settings to a json file. - public void SaveSettings(string jsonSettings, string powertoy = DefaultModuleName, string fileName = DefaultFileName) + public virtual void SaveSettings(string jsonSettings, string powertoy = DefaultModuleName, string fileName = DefaultFileName) { try { diff --git a/src/settings-ui/Settings.UI.Library/Utilities/CommandLineUtils.cs b/src/settings-ui/Settings.UI.Library/Utilities/CommandLineUtils.cs index a7702e17a2..3b10d0d63f 100644 --- a/src/settings-ui/Settings.UI.Library/Utilities/CommandLineUtils.cs +++ b/src/settings-ui/Settings.UI.Library/Utilities/CommandLineUtils.cs @@ -18,13 +18,13 @@ public class CommandLineUtils return settingsLibraryAssembly.GetType(typeof(CommandLineUtils).Namespace + "." + settingsClassName); } - public static ISettingsConfig GetSettingsConfigFor(string moduleName, ISettingsUtils settingsUtils, Assembly settingsLibraryAssembly) + public static ISettingsConfig GetSettingsConfigFor(string moduleName, SettingsUtils settingsUtils, Assembly settingsLibraryAssembly) { return GetSettingsConfigFor(GetSettingsConfigType(moduleName, settingsLibraryAssembly), settingsUtils); } /// Executes SettingsRepository<moduleSettingsType>.GetInstance(settingsUtils).SettingsConfig - public static ISettingsConfig GetSettingsConfigFor(Type moduleSettingsType, ISettingsUtils settingsUtils) + public static ISettingsConfig GetSettingsConfigFor(Type moduleSettingsType, SettingsUtils settingsUtils) { var genericSettingsRepositoryType = typeof(SettingsRepository<>); var moduleSettingsRepositoryType = genericSettingsRepositoryType.MakeGenericType(moduleSettingsType); diff --git a/src/settings-ui/Settings.UI.Library/Utilities/SetAdditionalSettingsCommandLineCommand.cs b/src/settings-ui/Settings.UI.Library/Utilities/SetAdditionalSettingsCommandLineCommand.cs index 29f47a4347..b6ba04dec8 100644 --- a/src/settings-ui/Settings.UI.Library/Utilities/SetAdditionalSettingsCommandLineCommand.cs +++ b/src/settings-ui/Settings.UI.Library/Utilities/SetAdditionalSettingsCommandLineCommand.cs @@ -244,7 +244,7 @@ public sealed class SetAdditionalSettingsCommandLineCommand } } - public static void Execute(string moduleName, JsonDocument settings, ISettingsUtils settingsUtils) + public static void Execute(string moduleName, JsonDocument settings, SettingsUtils settingsUtils) { Assembly settingsLibraryAssembly = CommandLineUtils.GetSettingsAssembly(); diff --git a/src/settings-ui/Settings.UI.Library/Utilities/SetSettingCommandLineCommand.cs b/src/settings-ui/Settings.UI.Library/Utilities/SetSettingCommandLineCommand.cs index ab5a88c5d8..3cd70856a8 100644 --- a/src/settings-ui/Settings.UI.Library/Utilities/SetSettingCommandLineCommand.cs +++ b/src/settings-ui/Settings.UI.Library/Utilities/SetSettingCommandLineCommand.cs @@ -25,7 +25,7 @@ public sealed class SetSettingCommandLineCommand return (parts[0], parts[1]); } - public static void Execute(string settingName, string settingValue, ISettingsUtils settingsUtils) + public static void Execute(string settingName, string settingValue, SettingsUtils settingsUtils) { Assembly settingsLibraryAssembly = CommandLineUtils.GetSettingsAssembly(); diff --git a/src/settings-ui/Settings.UI.Library/WorkspacesSettings.cs b/src/settings-ui/Settings.UI.Library/WorkspacesSettings.cs index fafb034935..18a364e2b3 100644 --- a/src/settings-ui/Settings.UI.Library/WorkspacesSettings.cs +++ b/src/settings-ui/Settings.UI.Library/WorkspacesSettings.cs @@ -56,7 +56,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library return hotkeyAccessors.ToArray(); } - public virtual void Save(ISettingsUtils settingsUtils) + public virtual void Save(SettingsUtils settingsUtils) { // Save settings to file var options = _serializerOptions; diff --git a/src/settings-ui/Settings.UI.UnitTests/BackwardsCompatibility/BackCompatTestProperties.cs b/src/settings-ui/Settings.UI.UnitTests/BackwardsCompatibility/BackCompatTestProperties.cs index b2048fa573..0eed821aff 100644 --- a/src/settings-ui/Settings.UI.UnitTests/BackwardsCompatibility/BackCompatTestProperties.cs +++ b/src/settings-ui/Settings.UI.UnitTests/BackwardsCompatibility/BackCompatTestProperties.cs @@ -27,10 +27,10 @@ namespace Microsoft.PowerToys.Settings.UI.UnitTests.BackwardsCompatibility internal sealed class MockSettingsRepository<T> : ISettingsRepository<T> where T : ISettingsConfig, new() { - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private T _settingsConfig; - public MockSettingsRepository(ISettingsUtils settingsUtils) + public MockSettingsRepository(SettingsUtils settingsUtils) { _settingsUtils = settingsUtils; } diff --git a/src/settings-ui/Settings.UI.UnitTests/Mocks/ISettingsUtilsMocks.cs b/src/settings-ui/Settings.UI.UnitTests/Mocks/ISettingsUtilsMocks.cs index d39aa03353..3f19e0b8d4 100644 --- a/src/settings-ui/Settings.UI.UnitTests/Mocks/ISettingsUtilsMocks.cs +++ b/src/settings-ui/Settings.UI.UnitTests/Mocks/ISettingsUtilsMocks.cs @@ -2,6 +2,7 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.IO.Abstractions; using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library.Interfaces; using Moq; @@ -11,10 +12,10 @@ namespace Microsoft.PowerToys.Settings.UI.UnitTests.Mocks internal static class ISettingsUtilsMocks { // Stubs out empty values for imageresizersettings and general settings as needed by the imageresizer view model - internal static Mock<ISettingsUtils> GetStubSettingsUtils<T>() + internal static Mock<SettingsUtils> GetStubSettingsUtils<T>() where T : ISettingsConfig, new() { - var settingsUtils = new Mock<ISettingsUtils>(); + var settingsUtils = new Mock<SettingsUtils>(new FileSystem(), null); settingsUtils .Setup(x => x.GetSettingsOrDefault<T>(It.IsAny<string>(), It.IsAny<string>())) .Returns(new T()); diff --git a/src/settings-ui/Settings.UI.UnitTests/ModelsTests/SettingsRepositoryTest.cs b/src/settings-ui/Settings.UI.UnitTests/ModelsTests/SettingsRepositoryTest.cs index 87d8c2d9d1..8db0cc09f9 100644 --- a/src/settings-ui/Settings.UI.UnitTests/ModelsTests/SettingsRepositoryTest.cs +++ b/src/settings-ui/Settings.UI.UnitTests/ModelsTests/SettingsRepositoryTest.cs @@ -14,7 +14,7 @@ namespace CommonLibTest [TestClass] public class SettingsRepositoryTest { - private static Task<SettingsRepository<GeneralSettings>> GetSettingsRepository(ISettingsUtils settingsUtils) + private static Task<SettingsRepository<GeneralSettings>> GetSettingsRepository(SettingsUtils settingsUtils) { return Task.Run(() => { diff --git a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs index 19e2ca3b67..ec90204eb5 100644 --- a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs +++ b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ColorPicker.cs @@ -27,7 +27,7 @@ namespace ViewModelTests { // Arrange var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider(version, ColorPickerSettings.ModuleName, fileName); - var settingPathMock = new Mock<ISettingsPath>(); + var settingPathMock = new Mock<SettingPath>(); var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object, settingPathMock.Object); ColorPickerSettings originalSettings = mockSettingsUtils.GetSettingsOrDefault<ColorPickerSettings>(ColorPickerSettings.ModuleName); diff --git a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/FancyZones.cs b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/FancyZones.cs index bf8f9396c4..e557f08bdf 100644 --- a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/FancyZones.cs +++ b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/FancyZones.cs @@ -32,7 +32,7 @@ namespace ViewModelTests [DataRow("v0.22.0", "settings.json")] public void OriginalFilesModificationTest(string version, string fileName) { - var settingPathMock = new Mock<ISettingsPath>(); + var settingPathMock = new Mock<SettingPath>(); var fileMock = BackCompatTestProperties.GetModuleIOProvider(version, FancyZonesSettings.ModuleName, fileName); var mockSettingsUtils = new SettingsUtils(fileMock.Object, settingPathMock.Object); @@ -89,9 +89,9 @@ namespace ViewModelTests return 0; } - private Mock<ISettingsUtils> mockGeneralSettingsUtils; + private Mock<SettingsUtils> mockGeneralSettingsUtils; - private Mock<ISettingsUtils> mockFancyZonesSettingsUtils; + private Mock<SettingsUtils> mockFancyZonesSettingsUtils; private Func<string, int> sendMockIPCConfigMSG = msg => { return 0; }; diff --git a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/General.cs b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/General.cs index 8b47e8147a..06b7044970 100644 --- a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/General.cs +++ b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/General.cs @@ -20,7 +20,7 @@ namespace ViewModelTests { public const string GeneralSettingsFileName = "Test\\GeneralSettings"; - private Mock<ISettingsUtils> mockGeneralSettingsUtils; + private Mock<SettingsUtils> mockGeneralSettingsUtils; [TestInitialize] public void SetUpStubSettingUtils() @@ -36,7 +36,7 @@ namespace ViewModelTests [DataRow("v0.22.0")] public void OriginalFilesModificationTest(string version) { - var settingPathMock = new Mock<ISettingsPath>(); + var settingPathMock = new Mock<SettingPath>(); var fileMock = BackCompatTestProperties.GetGeneralSettingsIOProvider(version); var mockGeneralSettingsUtils = new SettingsUtils(fileMock.Object, settingPathMock.Object); diff --git a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ImageResizer.cs b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ImageResizer.cs index c5175bc180..15eac53645 100644 --- a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ImageResizer.cs +++ b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ImageResizer.cs @@ -19,9 +19,9 @@ namespace ViewModelTests [TestClass] public class ImageResizer { - private Mock<ISettingsUtils> _mockGeneralSettingsUtils; + private Mock<SettingsUtils> _mockGeneralSettingsUtils; - private Mock<ISettingsUtils> _mockImgResizerSettingsUtils; + private Mock<SettingsUtils> _mockImgResizerSettingsUtils; [TestInitialize] public void SetUpStubSettingUtils() @@ -41,7 +41,7 @@ namespace ViewModelTests [DataRow("v0.22.0", "settings.json")] public void OriginalFilesModificationTest(string version, string fileName) { - var settingPathMock = new Mock<ISettingsPath>(); + var settingPathMock = new Mock<SettingPath>(); var fileMock = BackCompatTestProperties.GetModuleIOProvider(version, ImageResizerSettings.ModuleName, fileName); var mockSettingsUtils = new SettingsUtils(fileMock.Object, settingPathMock.Object); diff --git a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerLauncherViewModelTest.cs b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerLauncherViewModelTest.cs index 59b61559f4..a21fd51bc6 100644 --- a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerLauncherViewModelTest.cs +++ b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerLauncherViewModelTest.cs @@ -46,7 +46,7 @@ namespace ViewModelTests mockSettings = new PowerLauncherSettings(); sendCallbackMock = new SendCallbackMock(); - var settingPathMock = new Mock<ISettingsPath>(); + var settingPathMock = new Mock<SettingPath>(); var mockGeneralIOProvider = BackCompatTestProperties.GetGeneralSettingsIOProvider("v0.22.0"); var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralIOProvider.Object, settingPathMock.Object); mockGeneralSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils); @@ -81,7 +81,7 @@ namespace ViewModelTests [DataRow("v0.22.0", "settings.json")] public void OriginalFilesModificationTest(string version, string fileName) { - var settingPathMock = new Mock<ISettingsPath>(); + var settingPathMock = new Mock<SettingPath>(); var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider(version, PowerLauncherSettings.ModuleName, fileName); var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object, settingPathMock.Object); diff --git a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs index 636d96aec6..c0a3ad8514 100644 --- a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs +++ b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerPreview.cs @@ -17,9 +17,9 @@ namespace ViewModelTests [TestClass] public class PowerPreview { - private Mock<ISettingsUtils> mockPowerPreviewSettingsUtils; + private Mock<SettingsUtils> mockPowerPreviewSettingsUtils; - private Mock<ISettingsUtils> mockGeneralSettingsUtils; + private Mock<SettingsUtils> mockGeneralSettingsUtils; [TestInitialize] public void SetUpStubSettingUtils() @@ -39,7 +39,7 @@ namespace ViewModelTests [DataRow("v0.22.0", "settings.json")] public void OriginalFilesModificationTest(string version, string fileName) { - var settingPathMock = new Mock<ISettingsPath>(); + var settingPathMock = new Mock<SettingPath>(); var fileMock = BackCompatTestProperties.GetModuleIOProvider(version, PowerPreviewSettings.ModuleName, fileName); var mockSettingsUtils = new SettingsUtils(fileMock.Object, settingPathMock.Object); diff --git a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerRename.cs b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerRename.cs index 278975183b..77fd9328d6 100644 --- a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerRename.cs +++ b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/PowerRename.cs @@ -19,9 +19,9 @@ namespace ViewModelTests { public const string GeneralSettingsFileName = "Test\\PowerRename"; - private Mock<ISettingsUtils> mockGeneralSettingsUtils; + private Mock<SettingsUtils> mockGeneralSettingsUtils; - private Mock<ISettingsUtils> mockPowerRenamePropertiesUtils; + private Mock<SettingsUtils> mockPowerRenamePropertiesUtils; [TestInitialize] public void SetUpStubSettingUtils() @@ -40,7 +40,7 @@ namespace ViewModelTests [DataRow("v0.22.0", "power-rename-settings.json")] public void OriginalFilesModificationTest(string version, string fileName) { - var settingPathMock = new Mock<ISettingsPath>(); + var settingPathMock = new Mock<SettingPath>(); var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider(version, PowerRenameSettings.ModuleName, fileName); var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object, settingPathMock.Object); diff --git a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs index 897fc2bec6..6ebab902e7 100644 --- a/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs +++ b/src/settings-ui/Settings.UI.UnitTests/ViewModelTests/ShortcutGuide.cs @@ -31,7 +31,7 @@ namespace ViewModelTests [DataRow("v0.22.0", "settings.json")] public void OriginalFilesModificationTest(string version, string fileName) { - var settingPathMock = new Mock<ISettingsPath>(); + var settingPathMock = new Mock<SettingPath>(); var mockIOProvider = BackCompatTestProperties.GetModuleIOProvider(version, ShortcutGuideSettings.ModuleName, fileName); var mockSettingsUtils = new SettingsUtils(mockIOProvider.Object, settingPathMock.Object); ShortcutGuideSettings originalSettings = mockSettingsUtils.GetSettingsOrDefault<ShortcutGuideSettings>(ShortcutGuideSettings.ModuleName); @@ -56,9 +56,9 @@ namespace ViewModelTests BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralIOProvider, expectedCallCount); } - private Mock<ISettingsUtils> mockGeneralSettingsUtils; + private Mock<SettingsUtils> mockGeneralSettingsUtils; - private Mock<ISettingsUtils> mockShortcutGuideSettingsUtils; + private Mock<SettingsUtils> mockShortcutGuideSettingsUtils; [TestInitialize] public void SetUpStubSettingUtils() @@ -92,7 +92,7 @@ namespace ViewModelTests public void ThemeIndexShouldSetThemeToDarkWhenSuccessful() { // Arrange - var settingsUtilsMock = new Mock<ISettingsUtils>(); + var settingsUtilsMock = new Mock<SettingsUtils>(new FileSystem(), null); ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(settingsUtilsMock.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<ShortcutGuideSettings>.GetInstance(mockShortcutGuideSettingsUtils.Object), msg => { return 0; }, ShortCutGuideTestFolderName); // Initialize shortcut guide settings theme to 'system' to be in sync with shortcut_guide.h. @@ -110,7 +110,7 @@ namespace ViewModelTests public void OverlayOpacityShouldSeOverlayOpacityToOneHundredWhenSuccessful() { // Arrange - var settingsUtilsMock = new Mock<ISettingsUtils>(); + var settingsUtilsMock = new Mock<SettingsUtils>(new FileSystem(), null); ShortcutGuideViewModel viewModel = new ShortcutGuideViewModel(settingsUtilsMock.Object, SettingsRepository<GeneralSettings>.GetInstance(mockGeneralSettingsUtils.Object), SettingsRepository<ShortcutGuideSettings>.GetInstance(mockShortcutGuideSettingsUtils.Object), msg => { return 0; }, ShortCutGuideTestFolderName); Assert.AreEqual(90, viewModel.OverlayOpacity); diff --git a/src/settings-ui/Settings.UI.XamlIndexBuilder/Settings.UI.XamlIndexBuilder.csproj b/src/settings-ui/Settings.UI.XamlIndexBuilder/Settings.UI.XamlIndexBuilder.csproj index f8138b6bfa..dba7fdcd3b 100644 --- a/src/settings-ui/Settings.UI.XamlIndexBuilder/Settings.UI.XamlIndexBuilder.csproj +++ b/src/settings-ui/Settings.UI.XamlIndexBuilder/Settings.UI.XamlIndexBuilder.csproj @@ -40,4 +40,4 @@ <Message Importance="high" Text="[XamlIndexBuilder] Generating search index. Root='$(XamlRootDir)'; Out='$(GeneratedJsonFile)'; Tool='$(TargetPath)'; DotNet='$(DotNetExe)'." /> <Exec Command=""$(DotNetExe)" "$(TargetPath)" "$(XamlRootDir)" "$(GeneratedJsonFile)"" /> </Target> -</Project> +</Project> \ No newline at end of file diff --git a/src/settings-ui/Settings.UI/Helpers/HotkeyConflictIgnoreHelper.cs b/src/settings-ui/Settings.UI/Helpers/HotkeyConflictIgnoreHelper.cs index 1397fca0b7..59018bf74b 100644 --- a/src/settings-ui/Settings.UI/Helpers/HotkeyConflictIgnoreHelper.cs +++ b/src/settings-ui/Settings.UI/Helpers/HotkeyConflictIgnoreHelper.cs @@ -18,7 +18,7 @@ namespace Microsoft.PowerToys.Settings.UI.Helpers public static class HotkeyConflictIgnoreHelper { private static readonly ISettingsRepository<GeneralSettings> _generalSettingsRepository; - private static readonly ISettingsUtils _settingsUtils; + private static readonly SettingsUtils _settingsUtils; static HotkeyConflictIgnoreHelper() { diff --git a/src/settings-ui/Settings.UI/PowerToys.Settings.csproj b/src/settings-ui/Settings.UI/PowerToys.Settings.csproj index a5e184560c..8ce1598cff 100644 --- a/src/settings-ui/Settings.UI/PowerToys.Settings.csproj +++ b/src/settings-ui/Settings.UI/PowerToys.Settings.csproj @@ -215,4 +215,4 @@ <Message Importance="high" Text="[Settings] Building XamlIndexBuilder prior to compile. Views='$(MSBuildProjectDirectory)\SettingsXAML\Views' Out='$(GeneratedJsonFile)'" /> <MSBuild Projects="..\Settings.UI.XamlIndexBuilder\Settings.UI.XamlIndexBuilder.csproj" Targets="Build" Properties="Configuration=$(Configuration);Platform=Any CPU;TargetFramework=net9.0;XamlViewsDir=$(MSBuildProjectDirectory)\SettingsXAML\Views;GeneratedJsonFile=$(GeneratedJsonFile)" /> </Target> -</Project> +</Project> \ No newline at end of file diff --git a/src/settings-ui/Settings.UI/SettingsXAML/App.xaml.cs b/src/settings-ui/Settings.UI/SettingsXAML/App.xaml.cs index e75c8ab342..cebaa47c70 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/App.xaml.cs +++ b/src/settings-ui/Settings.UI/SettingsXAML/App.xaml.cs @@ -336,7 +336,7 @@ namespace Microsoft.PowerToys.Settings.UI }); #else /* If we try to run Settings as a standalone app, it will start PowerToys.exe if not running and open Settings again through it in the Dashboard page. */ - Common.UI.SettingsDeepLink.OpenSettings(Common.UI.SettingsDeepLink.SettingsWindow.Dashboard, true); + Common.UI.SettingsDeepLink.OpenSettings(Common.UI.SettingsDeepLink.SettingsWindow.Dashboard); Exit(); #endif } @@ -357,7 +357,7 @@ namespace Microsoft.PowerToys.Settings.UI return 0; } - private static ISettingsUtils settingsUtils = SettingsUtils.Default; + private static SettingsUtils settingsUtils = SettingsUtils.Default; private static ThemeService themeService = new ThemeService(SettingsRepository<GeneralSettings>.GetInstance(settingsUtils)); public static ThemeService ThemeService => themeService; diff --git a/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeShellPage.xaml.cs b/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeShellPage.xaml.cs index 7f8531286c..f3e7676a62 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeShellPage.xaml.cs +++ b/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeShellPage.xaml.cs @@ -51,7 +51,7 @@ namespace Microsoft.PowerToys.Settings.UI.OOBE.Views public ObservableCollection<OobePowerToysModule> Modules { get; } - private static ISettingsUtils settingsUtils = SettingsUtils.Default; + private static SettingsUtils settingsUtils = SettingsUtils.Default; /* NOTE: Experimentation for OOBE is currently turned off on server side. Keeping this code in a comment to allow future experiments. private bool ExperimentationToggleSwitchEnabled { get; set; } = true; diff --git a/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeWhatsNew.xaml.cs b/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeWhatsNew.xaml.cs index cf4488b759..9d7f0e78f3 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeWhatsNew.xaml.cs +++ b/src/settings-ui/Settings.UI/SettingsXAML/OOBE/Views/OobeWhatsNew.xaml.cs @@ -348,7 +348,7 @@ namespace Microsoft.PowerToys.Settings.UI.OOBE.Views private void DataDiagnostics_OpenSettings_Click(Microsoft.UI.Xaml.Documents.Hyperlink sender, Microsoft.UI.Xaml.Documents.HyperlinkClickEventArgs args) { - Common.UI.SettingsDeepLink.OpenSettings(Common.UI.SettingsDeepLink.SettingsWindow.Overview, true); + Common.UI.SettingsDeepLink.OpenSettings(Common.UI.SettingsDeepLink.SettingsWindow.Overview); } private async void LoadReleaseNotes_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPastePage.xaml.cs b/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPastePage.xaml.cs index faf31b0d4d..130230e2d9 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPastePage.xaml.cs +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/AdvancedPastePage.xaml.cs @@ -314,10 +314,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views string selectedType = draft.ServiceType ?? string.Empty; AIServiceType serviceKind = draft.ServiceTypeKind; - bool requiresEndpoint = serviceKind is AIServiceType.AzureOpenAI - or AIServiceType.AzureAIInference - or AIServiceType.Mistral - or AIServiceType.Ollama; + bool requiresEndpoint = RequiresEndpointForService(serviceKind); bool requiresDeployment = serviceKind == AIServiceType.AzureOpenAI; bool requiresApiVersion = serviceKind == AIServiceType.AzureOpenAI; bool requiresModelPath = serviceKind == AIServiceType.Onnx; @@ -788,12 +785,17 @@ namespace Microsoft.PowerToys.Settings.UI.Views string serviceType = draft.ServiceType ?? "OpenAI"; string apiKey = PasteAIApiKeyPasswordBox.Password; string trimmedApiKey = apiKey?.Trim() ?? string.Empty; + var serviceKind = draft.ServiceTypeKind; + bool requiresEndpoint = RequiresEndpointForService(serviceKind); string endpoint = (draft.EndpointUrl ?? string.Empty).Trim(); - if (endpoint == string.Empty) + + // Never persist placeholder text or stale values for services that don't use an endpoint. + if (!requiresEndpoint) { - endpoint = GetEndpointPlaceholder(draft.ServiceTypeKind); + endpoint = string.Empty; } + // For endpoint-based services, keep empty if the user didn't provide a value. if (RequiresApiKeyForService(serviceType) && string.IsNullOrWhiteSpace(trimmedApiKey)) { args.Cancel = true; @@ -833,6 +835,14 @@ namespace Microsoft.PowerToys.Settings.UI.Views }; } + private static bool RequiresEndpointForService(AIServiceType serviceKind) + { + return serviceKind is AIServiceType.AzureOpenAI + or AIServiceType.AzureAIInference + or AIServiceType.Mistral + or AIServiceType.Ollama; + } + private static string GetEndpointPlaceholder(AIServiceType serviceKind) { return serviceKind switch @@ -841,7 +851,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views AIServiceType.AzureAIInference => "https://{resource-name}.cognitiveservices.azure.com/", AIServiceType.Mistral => "https://api.mistral.ai/v1/", AIServiceType.Ollama => "http://localhost:11434/", - _ => "https://your-resource.openai.azure.com/", + _ => string.Empty, }; } diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerLauncherPage.xaml.cs b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerLauncherPage.xaml.cs index 19973a69bc..7f82ab4b97 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerLauncherPage.xaml.cs +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerLauncherPage.xaml.cs @@ -96,7 +96,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views private void NavigateCmdPalSettings_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) { - SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.CmdPal, true); + SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.CmdPal); } /* diff --git a/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs index ac051d7cd7..c98242d36b 100644 --- a/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/AdvancedPasteViewModel.cs @@ -38,7 +38,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private GeneralSettings GeneralSettingsConfig { get; set; } - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private readonly AdvancedPasteSettings _advancedPasteSettings; private readonly AdvancedPasteAdditionalActions _additionalActions; @@ -66,7 +66,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels }; public AdvancedPasteViewModel( - ISettingsUtils settingsUtils, + SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<AdvancedPasteSettings> advancedPasteSettingsRepository, Func<string, int> ipcMSGCallBackFunc) diff --git a/src/settings-ui/Settings.UI/ViewModels/AlwaysOnTopViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/AlwaysOnTopViewModel.cs index d7b03efad4..0a4860b016 100644 --- a/src/settings-ui/Settings.UI/ViewModels/AlwaysOnTopViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/AlwaysOnTopViewModel.cs @@ -22,7 +22,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { protected override string ModuleName => AlwaysOnTopSettings.ModuleName; - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -30,7 +30,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private Func<string, int> SendConfigMSG { get; } - public AlwaysOnTopViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<AlwaysOnTopSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc) + public AlwaysOnTopViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<AlwaysOnTopSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc) { ArgumentNullException.ThrowIfNull(settingsUtils); diff --git a/src/settings-ui/Settings.UI/ViewModels/CmdPalViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/CmdPalViewModel.cs index 6d7b2a0bae..a09cbadc7d 100644 --- a/src/settings-ui/Settings.UI/ViewModels/CmdPalViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/CmdPalViewModel.cs @@ -38,7 +38,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private Func<string, int> SendConfigMSG { get; } - public CmdPalViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, DispatcherQueue uiDispatcherQueue) + public CmdPalViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, DispatcherQueue uiDispatcherQueue) { ArgumentNullException.ThrowIfNull(settingsUtils); diff --git a/src/settings-ui/Settings.UI/ViewModels/ColorPickerViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/ColorPickerViewModel.cs index 5ea84d2caf..d827a07c08 100644 --- a/src/settings-ui/Settings.UI/ViewModels/ColorPickerViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/ColorPickerViewModel.cs @@ -31,7 +31,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private GeneralSettings GeneralSettingsConfig { get; set; } - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private readonly System.Threading.Lock _delayedActionLock = new System.Threading.Lock(); private readonly ColorPickerSettings _colorPickerSettings; @@ -47,7 +47,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private Dictionary<string, string> _colorFormatsPreview; public ColorPickerViewModel( - ISettingsUtils settingsUtils, + SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<ColorPickerSettings> colorPickerSettingsRepository, Func<string, int> ipcMSGCallBackFunc) diff --git a/src/settings-ui/Settings.UI/ViewModels/CropAndLockViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/CropAndLockViewModel.cs index e5e8a6383a..f7548bb573 100644 --- a/src/settings-ui/Settings.UI/ViewModels/CropAndLockViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/CropAndLockViewModel.cs @@ -21,7 +21,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { protected override string ModuleName => CropAndLockSettings.ModuleName; - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -29,7 +29,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private Func<string, int> SendConfigMSG { get; } - public CropAndLockViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<CropAndLockSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc) + public CropAndLockViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<CropAndLockSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc) { ArgumentNullException.ThrowIfNull(settingsUtils); diff --git a/src/settings-ui/Settings.UI/ViewModels/EnvironmentVariablesViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/EnvironmentVariablesViewModel.cs index e256278d00..0cb2feb503 100644 --- a/src/settings-ui/Settings.UI/ViewModels/EnvironmentVariablesViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/EnvironmentVariablesViewModel.cs @@ -23,7 +23,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private bool _enabledStateIsGPOConfigured; private bool _isEnabled; - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -79,7 +79,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels } } - public EnvironmentVariablesViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<EnvironmentVariablesSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, bool isElevated) + public EnvironmentVariablesViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<EnvironmentVariablesSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, bool isElevated) { SettingsUtils = settingsUtils; GeneralSettingsConfig = settingsRepository.SettingsConfig; diff --git a/src/settings-ui/Settings.UI/ViewModels/FancyZonesViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/FancyZonesViewModel.cs index 7ce6ec74c6..3fa21824e6 100644 --- a/src/settings-ui/Settings.UI/ViewModels/FancyZonesViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/FancyZonesViewModel.cs @@ -19,7 +19,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { protected override string ModuleName => FancyZonesSettings.ModuleName; - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -46,7 +46,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels Positional = 2, } - public FancyZonesViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<FancyZonesSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "") + public FancyZonesViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<FancyZonesSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "") { ArgumentNullException.ThrowIfNull(settingsUtils); diff --git a/src/settings-ui/Settings.UI/ViewModels/FileLocksmithViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/FileLocksmithViewModel.cs index 226d0c8b08..f3ecf45c92 100644 --- a/src/settings-ui/Settings.UI/ViewModels/FileLocksmithViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/FileLocksmithViewModel.cs @@ -18,7 +18,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { private GeneralSettings GeneralSettingsConfig { get; set; } - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private FileLocksmithSettings Settings { get; set; } @@ -26,7 +26,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private string _settingsConfigFileFolder = string.Empty; - public FileLocksmithViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "") + public FileLocksmithViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "") { _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); diff --git a/src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs index 04eea7c1e4..67c04ab948 100644 --- a/src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/HostsViewModel.cs @@ -23,7 +23,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private bool _enabledStateIsGPOConfigured; private bool _isEnabled; - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -214,7 +214,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels public int MinimumBackupsCount => DeleteBackupsMode == 1 ? 1 : 0; - public HostsViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<HostsSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, bool isElevated) + public HostsViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<HostsSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, bool isElevated) { SettingsUtils = settingsUtils; GeneralSettingsConfig = settingsRepository.SettingsConfig; diff --git a/src/settings-ui/Settings.UI/ViewModels/ImageResizerViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/ImageResizerViewModel.cs index de70cd3311..ffeb7e226e 100644 --- a/src/settings-ui/Settings.UI/ViewModels/ImageResizerViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/ImageResizerViewModel.cs @@ -45,7 +45,7 @@ public partial class ImageResizerViewModel : Observable private GeneralSettings GeneralSettingsConfig { get; set; } - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private ImageResizerSettings Settings { get; set; } @@ -53,7 +53,7 @@ public partial class ImageResizerViewModel : Observable private Func<string, int> SendConfigMSG { get; } - public ImageResizerViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<string, string> resourceLoader) + public ImageResizerViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<string, string> resourceLoader) { _isInitializing = true; diff --git a/src/settings-ui/Settings.UI/ViewModels/KeyboardManagerViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/KeyboardManagerViewModel.cs index a48742672c..d7bf9862bc 100644 --- a/src/settings-ui/Settings.UI/ViewModels/KeyboardManagerViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/KeyboardManagerViewModel.cs @@ -27,7 +27,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { private GeneralSettings GeneralSettingsConfig { get; set; } - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private const string PowerToyName = KeyboardManagerSettings.ModuleName; private const string JsonFileType = ".json"; @@ -60,7 +60,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private Func<List<KeysDataModel>, int> FilterRemapKeysList { get; } - public KeyboardManagerViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<List<KeysDataModel>, int> filterRemapKeysList) + public KeyboardManagerViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<List<KeysDataModel>, int> filterRemapKeysList) { ArgumentNullException.ThrowIfNull(settingsRepository); diff --git a/src/settings-ui/Settings.UI/ViewModels/MeasureToolViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/MeasureToolViewModel.cs index 023cc06032..855cf6e17a 100644 --- a/src/settings-ui/Settings.UI/ViewModels/MeasureToolViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/MeasureToolViewModel.cs @@ -20,7 +20,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { protected override string ModuleName => MeasureToolSettings.ModuleName; - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -30,7 +30,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private MeasureToolSettings Settings { get; set; } - public MeasureToolViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<MeasureToolSettings> measureToolSettingsRepository, Func<string, int> ipcMSGCallBackFunc) + public MeasureToolViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<MeasureToolSettings> measureToolSettingsRepository, Func<string, int> ipcMSGCallBackFunc) { SettingsUtils = settingsUtils; diff --git a/src/settings-ui/Settings.UI/ViewModels/MouseUtilsViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/MouseUtilsViewModel.cs index 27695d1037..0c3eb06649 100644 --- a/src/settings-ui/Settings.UI/ViewModels/MouseUtilsViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/MouseUtilsViewModel.cs @@ -19,7 +19,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { protected override string ModuleName => "MouseUtils"; - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -31,7 +31,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private CursorWrapSettings CursorWrapSettingsConfig { get; set; } - public MouseUtilsViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<FindMyMouseSettings> findMyMouseSettingsRepository, ISettingsRepository<MouseHighlighterSettings> mouseHighlighterSettingsRepository, ISettingsRepository<MouseJumpSettings> mouseJumpSettingsRepository, ISettingsRepository<MousePointerCrosshairsSettings> mousePointerCrosshairsSettingsRepository, ISettingsRepository<CursorWrapSettings> cursorWrapSettingsRepository, Func<string, int> ipcMSGCallBackFunc) + public MouseUtilsViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<FindMyMouseSettings> findMyMouseSettingsRepository, ISettingsRepository<MouseHighlighterSettings> mouseHighlighterSettingsRepository, ISettingsRepository<MouseJumpSettings> mouseJumpSettingsRepository, ISettingsRepository<MousePointerCrosshairsSettings> mousePointerCrosshairsSettingsRepository, ISettingsRepository<CursorWrapSettings> cursorWrapSettingsRepository, Func<string, int> ipcMSGCallBackFunc) { SettingsUtils = settingsUtils; diff --git a/src/settings-ui/Settings.UI/ViewModels/MouseWithoutBordersViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/MouseWithoutBordersViewModel.cs index 6e0bcaa444..447c62a6dd 100644 --- a/src/settings-ui/Settings.UI/ViewModels/MouseWithoutBordersViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/MouseWithoutBordersViewModel.cs @@ -187,7 +187,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels } } - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -425,7 +425,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private DispatcherQueue _uiDispatcherQueue; - public MouseWithoutBordersViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, DispatcherQueue uiDispatcherQueue) + public MouseWithoutBordersViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, DispatcherQueue uiDispatcherQueue) { SettingsUtils = settingsUtils; diff --git a/src/settings-ui/Settings.UI/ViewModels/NewPlusViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/NewPlusViewModel.cs index 0bd44b4fbf..ffb10883bd 100644 --- a/src/settings-ui/Settings.UI/ViewModels/NewPlusViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/NewPlusViewModel.cs @@ -26,13 +26,13 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { private GeneralSettings GeneralSettingsConfig { get; set; } - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private NewPlusSettings Settings { get; set; } private const string ModuleName = NewPlusSettings.ModuleName; - public NewPlusViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc) + public NewPlusViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc) { _settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); @@ -228,7 +228,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private Func<string, int> SendConfigMSG { get; } - public static NewPlusSettings LoadSettings(ISettingsUtils settingsUtils) + public static NewPlusSettings LoadSettings(SettingsUtils settingsUtils) { NewPlusSettings settings = null; diff --git a/src/settings-ui/Settings.UI/ViewModels/PeekViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/PeekViewModel.cs index 85ffbda2d9..5eebe1b8de 100644 --- a/src/settings-ui/Settings.UI/ViewModels/PeekViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/PeekViewModel.cs @@ -35,7 +35,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private readonly DispatcherQueue _dispatcherQueue; - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private readonly PeekPreviewSettings _peekPreviewSettings; private PeekSettings _peekSettings; @@ -47,7 +47,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private IFileSystemWatcher _watcher; public PeekViewModel( - ISettingsUtils settingsUtils, + SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, DispatcherQueue dispatcherQueue) diff --git a/src/settings-ui/Settings.UI/ViewModels/PowerAccentViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/PowerAccentViewModel.cs index f058275f96..342d90ff52 100644 --- a/src/settings-ui/Settings.UI/ViewModels/PowerAccentViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/PowerAccentViewModel.cs @@ -21,7 +21,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private readonly PowerAccentSettings _powerAccentSettings; - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private const string SpecialGroup = "QuickAccent_Group_Special"; private const string LanguageGroup = "QuickAccent_Group_Language"; @@ -89,7 +89,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private Func<string, int> SendConfigMSG { get; } - public PowerAccentViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc) + public PowerAccentViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc) { // To obtain the general settings configurations of PowerToys Settings. ArgumentNullException.ThrowIfNull(settingsRepository); diff --git a/src/settings-ui/Settings.UI/ViewModels/PowerOcrViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/PowerOcrViewModel.cs index cb67dfc237..f9dc35247c 100644 --- a/src/settings-ui/Settings.UI/ViewModels/PowerOcrViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/PowerOcrViewModel.cs @@ -32,7 +32,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private GeneralSettings GeneralSettingsConfig { get; set; } - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private readonly System.Threading.Lock _delayedActionLock = new System.Threading.Lock(); private readonly PowerOcrSettings _powerOcrSettings; @@ -72,7 +72,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private Func<string, int> SendConfigMSG { get; } public PowerOcrViewModel( - ISettingsUtils settingsUtils, + SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<PowerOcrSettings> powerOcrsettingsRepository, Func<string, int> ipcMSGCallBackFunc) diff --git a/src/settings-ui/Settings.UI/ViewModels/PowerRenameViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/PowerRenameViewModel.cs index e1dd68ed5f..b1d8e87197 100644 --- a/src/settings-ui/Settings.UI/ViewModels/PowerRenameViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/PowerRenameViewModel.cs @@ -18,7 +18,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { private GeneralSettings GeneralSettingsConfig { get; set; } - private readonly ISettingsUtils _settingsUtils; + private readonly SettingsUtils _settingsUtils; private const string ModuleName = PowerRenameSettings.ModuleName; @@ -28,7 +28,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private Func<string, int> SendConfigMSG { get; } - public PowerRenameViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "") + public PowerRenameViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "") { // Update Settings file folder: _settingsConfigFileFolder = configFileSubfolder; diff --git a/src/settings-ui/Settings.UI/ViewModels/ShortcutConflictViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/ShortcutConflictViewModel.cs index cfd3683080..6c6d624f97 100644 --- a/src/settings-ui/Settings.UI/ViewModels/ShortcutConflictViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/ShortcutConflictViewModel.cs @@ -36,7 +36,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private ResourceLoader resourceLoader; public ShortcutConflictViewModel( - ISettingsUtils settingsUtils, + SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc) { diff --git a/src/settings-ui/Settings.UI/ViewModels/ShortcutGuideViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/ShortcutGuideViewModel.cs index 1f25f02dfb..8c91cb4779 100644 --- a/src/settings-ui/Settings.UI/ViewModels/ShortcutGuideViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/ShortcutGuideViewModel.cs @@ -20,7 +20,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { protected override string ModuleName => ShortcutGuideSettings.ModuleName; - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -31,7 +31,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private string _settingsConfigFileFolder = string.Empty; private string _disabledApps; - public ShortcutGuideViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<ShortcutGuideSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "") + public ShortcutGuideViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<ShortcutGuideSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "") { SettingsUtils = settingsUtils; diff --git a/src/settings-ui/Settings.UI/ViewModels/WorkspacesViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/WorkspacesViewModel.cs index 842c3cf368..64346513f0 100644 --- a/src/settings-ui/Settings.UI/ViewModels/WorkspacesViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/WorkspacesViewModel.cs @@ -21,7 +21,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels { protected override string ModuleName => WorkspacesSettings.ModuleName; - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -31,7 +31,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels public ButtonClickCommand LaunchEditorEventHandler { get; set; } - public WorkspacesViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<WorkspacesSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc) + public WorkspacesViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<WorkspacesSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc) { ArgumentNullException.ThrowIfNull(settingsUtils); diff --git a/src/settings-ui/Settings.UI/ViewModels/ZoomItViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/ZoomItViewModel.cs index e1704e16fb..7e09f90147 100644 --- a/src/settings-ui/Settings.UI/ViewModels/ZoomItViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/ZoomItViewModel.cs @@ -27,7 +27,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private const string FormatGif = "GIF"; private const string FormatMp4 = "MP4"; - private ISettingsUtils SettingsUtils { get; set; } + private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } @@ -72,7 +72,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels IncludeFields = true, }; - public ZoomItViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<string, string, string, int, string> pickFileDialog, Func<LOGFONT, LOGFONT> pickFontDialog) + public ZoomItViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<string, string, string, int, string> pickFileDialog, Func<LOGFONT, LOGFONT> pickFontDialog) { ArgumentNullException.ThrowIfNull(settingsUtils); diff --git a/tools/build/build-installer.ps1 b/tools/build/build-installer.ps1 index 65497aeeaa..a0e1f3c809 100644 --- a/tools/build/build-installer.ps1 +++ b/tools/build/build-installer.ps1 @@ -3,14 +3,7 @@ Build and package PowerToys (CmdPal and installer) for a specific platform and configuration LOCALLY. .DESCRIPTION -This script automates the end-to-end build and packaging process for PowerToys, including: -- Restoring and building all necessary solutions (CmdPal, BugReportTool, etc.) -- Cleaning up old output -- Signing generated .msix packages -- Building the WiX v5 (VNext) MSI and bootstrapper installers - -It is designed to work in local development. -The cert used to sign the packages is generated by +Builds and packages PowerToys (CmdPal and installer) locally. Handles solution build, signing, and WiX installer generation. .PARAMETER Platform Specifies the target platform for the build (e.g., 'arm64', 'x64'). Default is 'x64'. @@ -34,10 +27,9 @@ Runs the pipeline for x64 Release. Runs the pipeline for x64 Release with machine-wide installer. .NOTES -- Make sure to run this script from a Developer PowerShell (e.g., VS2022 Developer PowerShell). - Generated MSIX files will be signed using cert-sign-package.ps1. -- This script will clean previous outputs under the build directories and installer directory (except *.exe files). -- First time run need admin permission to trust the certificate. +- If the working tree is not clean, the script will prompt before continuing (use -Force to skip the prompt). +- Use the -Clean parameter to clean build outputs (bin/obj) and MSBuild outputs. - The built installer will be placed under: installer/PowerToysSetupVNext/[Platform]/[Configuration]/User[Machine]Setup relative to the solution root directory. - To run the full installation in other machines, call "./cert-management.ps1" to export the cert used to sign the packages. @@ -45,11 +37,31 @@ Runs the pipeline for x64 Release with machine-wide installer. #> param ( - [string]$Platform = '', + [string]$Platform = 'x64', [string]$Configuration = 'Release', - [string]$PerUser = 'true' + [string]$PerUser = 'true', + [string]$Version, + [switch]$Force, + [switch]$EnableCmdPalAOT, + [switch]$Clean, + [switch]$SkipBuild, + [switch]$Help ) +if ($Help) { + Write-Host "Usage: .\build-installer.ps1 [-Platform <x64|arm64>] [-Configuration <Release|Debug>] [-PerUser <true|false>] [-Version <0.0.1>] [-Force] [-EnableCmdPalAOT] [-Clean] [-SkipBuild]" + Write-Host " -Platform Target platform (default: auto-detect or x64)" + Write-Host " -Configuration Build configuration (default: Release)" + Write-Host " -PerUser Build per-user installer (default: true)" + Write-Host " -Version Sets the PowerToys version (default: from src\Version.props)" + Write-Host " -Force Continue even if the git working tree is not clean (skips the interactive prompt)." + Write-Host " -EnableCmdPalAOT Enable AOT compilation for CmdPal (slower build)" + Write-Host " -Clean Clean output directories before building" + Write-Host " -SkipBuild Skip building the main solution and tools (assumes they are already built)" + Write-Host " -Help Show this help message" + exit 0 +} + # Ensure helpers are available . "$PSScriptRoot\build-common.ps1" @@ -89,62 +101,313 @@ if (-not $repoRoot -or -not (Test-Path (Join-Path $repoRoot "PowerToys.slnx"))) } Write-Host "PowerToys repository root detected: $repoRoot" -# WiX v5 projects use WixToolset.Sdk via NuGet/MSBuild; no separate WiX installation is required. -Write-Host ("[PIPELINE] Start | Platform={0} Configuration={1} PerUser={2}" -f $Platform, $Configuration, $PerUser) -Write-Host '' + +# Safety check: avoid mixing build outputs with existing local changes unless the user confirms. +if (-not $Force) { + Push-Location $repoRoot + try { + $gitStatus = $null + $gitRelevantStatus = @() + try { + $gitStatus = git status --porcelain=v1 --untracked-files=all --ignore-submodules=all + } catch { + Write-Warning ("[GIT] Failed to query git status: {0}" -f $_.Exception.Message) + } + + if ($gitStatus -and $gitStatus.Length -gt 0) { + foreach ($line in $gitStatus) { + if (-not $line) { continue } + + # Porcelain v1 format: XY <path> + # We only care about changes that affect the working tree (Y != ' ') or untracked files (??). + # Index-only changes (staged, Y == ' ') are ignored per user request. + if ($line.StartsWith('??')) { + $gitRelevantStatus += $line + continue + } + + if ($line.StartsWith('!!')) { + continue + } + + if ($line.Length -ge 2) { + $workTreeStatus = $line[1] + if ($workTreeStatus -ne ' ') { + $gitRelevantStatus += $line + } + } + } + } + + if ($gitRelevantStatus.Count -gt 0) { + Write-Warning "[GIT] Working tree is NOT clean." + Write-Warning "[GIT] This build will generate untracked files and may modify tracked files, which can mix with your current changes." + Write-Host "[GIT] Unstaged/untracked status (first 50 lines):" + $gitRelevantStatus | Select-Object -First 50 | ForEach-Object { Write-Host (" {0}" -f $_) } + + $shouldContinue = $false + try { + $choices = [System.Management.Automation.Host.ChoiceDescription[]]@( + (New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Continue the build."), + (New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Cancel the build.") + ) + $decision = $Host.UI.PromptForChoice("Working tree not clean", "Continue anyway?", $choices, 1) + $shouldContinue = ($decision -eq 0) + } catch { + Write-Warning "[GIT] Interactive prompt not available." + Write-Error "Refusing to proceed with a dirty working tree. Re-run with -Force to continue anyway." + exit 1 + } + + if (-not $shouldContinue) { + Write-Host "[GIT] Cancelled by user." + exit 1 + } + } + } finally { + Pop-Location + } +} $cmdpalOutputPath = Join-Path $repoRoot "$Platform\$Configuration\WinUI3Apps\CmdPal" +$buildOutputPath = Join-Path $repoRoot "$Platform\$Configuration" -if (Test-Path $cmdpalOutputPath) { - Write-Host "[CLEAN] Removing previous output: $cmdpalOutputPath" - Remove-Item $cmdpalOutputPath -Recurse -Force -ErrorAction Ignore -} - -$commonArgs = '/p:CIBuild=true' -# No local projects found (or continuing) - build full solution and tools -RestoreThenBuild 'PowerToys.slnx' $commonArgs $Platform $Configuration - -$msixSearchRoot = Join-Path $repoRoot "$Platform\$Configuration" -$msixFiles = Get-ChildItem -Path $msixSearchRoot -Recurse -Filter *.msix | -Select-Object -ExpandProperty FullName - -if ($msixFiles.Count) { - Write-Host ("[SIGN] .msix file(s): {0}" -f ($msixFiles -join '; ')) - & (Join-Path $PSScriptRoot "cert-sign-package.ps1") -TargetPaths $msixFiles -} -else { - Write-Warning "[SIGN] No .msix files found in $msixSearchRoot" -} - -# Generate DSC manifest files -Write-Host '[DSC] Generating DSC manifest files...' -$dscScriptPath = Join-Path $repoRoot '.\tools\build\generate-dsc-manifests.ps1' -if (Test-Path $dscScriptPath) { - & $dscScriptPath -BuildPlatform $Platform -BuildConfiguration $Configuration -RepoRoot $repoRoot - if ($LASTEXITCODE -ne 0) { - Write-Error "DSC manifest generation failed with exit code $LASTEXITCODE" - exit 1 +# Clean should be done first before any other steps +if ($Clean) { + if (Test-Path $cmdpalOutputPath) { + Write-Host "[CLEAN] Removing previous output: $cmdpalOutputPath" + Remove-Item $cmdpalOutputPath -Recurse -Force -ErrorAction Ignore } - Write-Host '[DSC] DSC manifest files generated successfully' -} else { - Write-Warning "[DSC] DSC manifest generator script not found at: $dscScriptPath" + if (Test-Path $buildOutputPath) { + Write-Host "[CLEAN] Removing previous build output: $buildOutputPath" + Remove-Item $buildOutputPath -Recurse -Force -ErrorAction Ignore + } + + Write-Host "[CLEAN] Cleaning solution (msbuild /t:Clean)..." + RunMSBuild 'PowerToys.slnx' '/t:Clean' $Platform $Configuration } -RestoreThenBuild 'tools\BugReportTool\BugReportTool.sln' $commonArgs $Platform $Configuration -RestoreThenBuild 'tools\StylesReportTool\StylesReportTool.sln' $commonArgs $Platform $Configuration - -Write-Host '[CLEAN] installer (keep *.exe)' -Push-Location $repoRoot try { - git clean -xfd -e '*.exe' -- .\installer\ | Out-Null + if ($Version) { + Write-Host "[VERSION] Setting PowerToys version to $Version using versionSetting.ps1..." + $versionScript = Join-Path $repoRoot ".pipelines\versionSetting.ps1" + if (Test-Path $versionScript) { + & $versionScript -versionNumber $Version -DevEnvironment 'Local' + if (-not $?) { + Write-Error "versionSetting.ps1 failed" + exit 1 + } + } else { + Write-Error "Could not find versionSetting.ps1 at: $versionScript" + exit 1 + } + } + + Write-Host "[VERSION] Setting up versioning using Microsoft.Windows.Terminal.Versioning..." + + # Check for nuget.exe - download to AppData if not available + $nugetDownloaded = $false + $nugetPath = $null + if (-not (Get-Command nuget -ErrorAction SilentlyContinue)) { + Write-Warning "nuget.exe not found in PATH. Attempting to download..." + $nugetUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" + $nugetDir = Join-Path $env:LOCALAPPDATA "PowerToys\BuildTools" + if (-not (Test-Path $nugetDir)) { New-Item -ItemType Directory -Path $nugetDir -Force | Out-Null } + $nugetPath = Join-Path $nugetDir "nuget.exe" + if (-not (Test-Path $nugetPath)) { + try { + Invoke-WebRequest $nugetUrl -OutFile $nugetPath + $nugetDownloaded = $true + } catch { + Write-Error "Failed to download nuget.exe. Please install it manually and add to PATH." + exit 1 + } + } + $env:Path += ";$nugetDir" + } + + # Install Terminal versioning package to AppData + $versioningDir = Join-Path $env:LOCALAPPDATA "PowerToys\BuildTools\.versioning" + if (-not (Test-Path $versioningDir)) { New-Item -ItemType Directory -Path $versioningDir -Force | Out-Null } + + $configFile = Join-Path $repoRoot ".pipelines\release-nuget.config" + + # Install the package + # Use -ExcludeVersion to make the path predictable + nuget install Microsoft.Windows.Terminal.Versioning -ConfigFile $configFile -OutputDirectory $versioningDir -ExcludeVersion -NonInteractive + + $versionRoot = Join-Path $versioningDir "Microsoft.Windows.Terminal.Versioning" + $setupScript = Join-Path $versionRoot "build\Setup.ps1" + + if (Test-Path $setupScript) { + & $setupScript -ProjectDirectory (Join-Path $repoRoot "src\modules\cmdpal") -Verbose + } else { + Write-Error "Could not find Setup.ps1 in $versionRoot" + } + + # WiX v5 projects use WixToolset.Sdk via NuGet/MSBuild; no separate WiX installation is required. + Write-Host ("[PIPELINE] Start | Platform={0} Configuration={1} PerUser={2}" -f $Platform, $Configuration, $PerUser) + Write-Host '' + + $commonArgs = '/p:CIBuild=true /p:IsPipeline=true' + + if ($EnableCmdPalAOT) { + $commonArgs += " /p:EnableCmdPalAOT=true" + } + + # No local projects found (or continuing) - build full solution and tools + if (-not $SkipBuild) { + RestoreThenBuild 'PowerToys.slnx' $commonArgs $Platform $Configuration + } + + $msixSearchRoot = Join-Path $repoRoot "$Platform\$Configuration" + $msixFiles = Get-ChildItem -Path $msixSearchRoot -Recurse -Filter *.msix | + Select-Object -ExpandProperty FullName + + if ($msixFiles.Count) { + Write-Host ("[SIGN] .msix file(s): {0}" -f ($msixFiles -join '; ')) + & (Join-Path $PSScriptRoot "cert-sign-package.ps1") -TargetPaths $msixFiles + } + else { + Write-Warning "[SIGN] No .msix files found in $msixSearchRoot" + } + + # Generate DSC v2 manifests (PowerToys.Settings.DSC.Schema.Generator) + # The csproj PostBuild event is skipped on ARM64, so we run it manually here if needed. + if ($Platform -eq 'arm64') { + Write-Host "[DSC] Manually generating DSC v2 manifests for ARM64..." + + # 1. Get Version + $versionPropsPath = Join-Path $repoRoot "src\Version.props" + [xml]$versionProps = Get-Content $versionPropsPath + $ptVersion = $versionProps.Project.PropertyGroup.Version + # Directory.Build.props appends .0 to the version for .csproj files + $ptVersionFull = "$ptVersion.0" + + # 2. Build the Generator + $generatorProj = Join-Path $repoRoot "src\dsc\PowerToys.Settings.DSC.Schema.Generator\PowerToys.Settings.DSC.Schema.Generator.csproj" + RunMSBuild $generatorProj "/t:Build" $Platform $Configuration + + # 3. Define paths + # The generator output path is in the project's bin folder + $generatorExe = Join-Path $repoRoot "src\dsc\PowerToys.Settings.DSC.Schema.Generator\bin\$Platform\$Configuration\PowerToys.Settings.DSC.Schema.Generator.exe" + + if (-not (Test-Path $generatorExe)) { + Write-Warning "Could not find generator at expected path: $generatorExe" + Write-Warning "Searching in build output..." + $found = Get-ChildItem -Path (Join-Path $repoRoot "$Platform\$Configuration") -Filter "PowerToys.Settings.DSC.Schema.Generator.exe" -Recurse | Select-Object -First 1 + if ($found) { + $generatorExe = $found.FullName + } + } + + $settingsLibDll = Join-Path $repoRoot "$Platform\$Configuration\WinUI3Apps\PowerToys.Settings.UI.Lib.dll" + + $dscGenDir = Join-Path $repoRoot "src\dsc\Microsoft.PowerToys.Configure\Generated\Microsoft.PowerToys.Configure\$ptVersionFull" + if (-not (Test-Path $dscGenDir)) { + New-Item -ItemType Directory -Path $dscGenDir -Force | Out-Null + } + + $outPsm1 = Join-Path $dscGenDir "Microsoft.PowerToys.Configure.psm1" + $outPsd1 = Join-Path $dscGenDir "Microsoft.PowerToys.Configure.psd1" + + # 4. Run Generator + if (Test-Path $generatorExe) { + Write-Host "[DSC] Executing: $generatorExe" + + $generatorDir = Split-Path -Parent $generatorExe + $winUI3AppsDir = Join-Path $repoRoot "$Platform\$Configuration\WinUI3Apps" + + # Copy dependencies from WinUI3Apps to Generator directory to satisfy WinRT/WinUI3 dependencies + # This avoids "Class not registered" errors without polluting the WinUI3Apps directory which is used for packaging. + if (Test-Path $winUI3AppsDir) { + Write-Host "[DSC] Copying dependencies from $winUI3AppsDir to $generatorDir" + Get-ChildItem -Path $winUI3AppsDir -Filter "*.dll" | ForEach-Object { + $destPath = Join-Path $generatorDir $_.Name + if (-not (Test-Path $destPath)) { + Copy-Item -Path $_.FullName -Destination $destPath -Force + } + } + # Also copy resources.pri if it exists, as it might be needed for resource lookup + $priFile = Join-Path $winUI3AppsDir "resources.pri" + if (Test-Path $priFile) { + Copy-Item -Path $priFile -Destination $generatorDir -Force + } + } + + Push-Location $generatorDir + try { + # Now we can use the local DLLs + $localSettingsLibDll = Join-Path $generatorDir "PowerToys.Settings.UI.Lib.dll" + + if (Test-Path $localSettingsLibDll) { + Write-Host "[DSC] Using local DLL: $localSettingsLibDll" + & $generatorExe $localSettingsLibDll $outPsm1 $outPsd1 + } else { + # Fallback (shouldn't happen if copy succeeded or build was correct) + Write-Warning "[DSC] Local DLL not found, falling back to: $settingsLibDll" + & $generatorExe $settingsLibDll $outPsm1 $outPsd1 + } + + if ($LASTEXITCODE -ne 0) { + Write-Error "DSC v2 generation failed with exit code $LASTEXITCODE" + exit 1 + } + } finally { + Pop-Location + } + + Write-Host "[DSC] DSC v2 manifests generated successfully." + } else { + Write-Error "Could not find generator executable at $generatorExe" + exit 1 + } + } + + # Generate DSC manifest files + Write-Host '[DSC] Generating DSC manifest files...' + $dscScriptPath = Join-Path $repoRoot '.\tools\build\generate-dsc-manifests.ps1' + if (Test-Path $dscScriptPath) { + & $dscScriptPath -BuildPlatform $Platform -BuildConfiguration $Configuration -RepoRoot $repoRoot + if ($LASTEXITCODE -ne 0) { + Write-Error "DSC manifest generation failed with exit code $LASTEXITCODE" + exit 1 + } + Write-Host '[DSC] DSC manifest files generated successfully' + } else { + Write-Warning "[DSC] DSC manifest generator script not found at: $dscScriptPath" + } + + if (-not $SkipBuild) { + RestoreThenBuild 'tools\BugReportTool\BugReportTool.sln' $commonArgs $Platform $Configuration + RestoreThenBuild 'tools\StylesReportTool\StylesReportTool.sln' $commonArgs $Platform $Configuration + } + + # Set NUGET_PACKAGES environment variable if not set, to help wixproj find heat.exe + if (-not $env:NUGET_PACKAGES) { + $env:NUGET_PACKAGES = Join-Path $env:USERPROFILE ".nuget\packages" + Write-Host "[ENV] Set NUGET_PACKAGES to $env:NUGET_PACKAGES" + } + + RunMSBuild 'installer\PowerToysSetup.slnx' "$commonArgs /t:restore /p:RestorePackagesConfig=true" $Platform $Configuration + + RunMSBuild 'installer\PowerToysSetup.slnx' "$commonArgs /m /t:PowerToysInstallerVNext /p:PerUser=$PerUser" $Platform $Configuration + + # Fix: WiX v5 locally puts the MSI in an 'en-us' subfolder, but the Bootstrapper expects it in the root of UserSetup/MachineSetup. + # We move it up one level to match expectations. + $setupType = if ($PerUser -eq 'true') { 'UserSetup' } else { 'MachineSetup' } + $msiParentDir = Join-Path $repoRoot "installer\PowerToysSetupVNext\$Platform\$Configuration\$setupType" + $msiEnUsDir = Join-Path $msiParentDir "en-us" + + if (Test-Path $msiEnUsDir) { + Write-Host "[FIX] Moving MSI files from $msiEnUsDir to $msiParentDir" + Get-ChildItem -Path $msiEnUsDir -Filter *.msi | Move-Item -Destination $msiParentDir -Force + } + + RunMSBuild 'installer\PowerToysSetup.slnx' "$commonArgs /m /t:PowerToysBootstrapperVNext /p:PerUser=$PerUser" $Platform $Configuration + } finally { - Pop-Location + # No git cleanup; leave workspace state as-is. } -RunMSBuild 'installer\PowerToysSetup.slnx' "$commonArgs /t:restore /p:RestorePackagesConfig=true" $Platform $Configuration - -RunMSBuild 'installer\PowerToysSetup.slnx' "$commonArgs /m /t:PowerToysInstallerVNext /p:PerUser=$PerUser" $Platform $Configuration - -RunMSBuild 'installer\PowerToysSetup.slnx' "$commonArgs /m /t:PowerToysBootstrapperVNext /p:PerUser=$PerUser" $Platform $Configuration - Write-Host '[PIPELINE] Completed' diff --git a/tools/build/cert-management.ps1 b/tools/build/cert-management.ps1 index f92146f730..bc7c758f70 100644 --- a/tools/build/cert-management.ps1 +++ b/tools/build/cert-management.ps1 @@ -61,8 +61,18 @@ function ImportAndVerifyCertificate { try { $null = Import-Certificate -FilePath $cerPath -CertStoreLocation $storePath -ErrorAction Stop } catch { - Write-Warning "Failed to import certificate to $storePath : $_" - return $false + if ($_.Exception.Message -match "Access is denied" -or $_.Exception.InnerException.Message -match "Access is denied") { + Write-Warning "Access denied to $storePath. Attempting to import with admin privileges..." + try { + Start-Process powershell -ArgumentList "-NoProfile", "-Command", "& { Import-Certificate -FilePath '$cerPath' -CertStoreLocation '$storePath' }" -Verb RunAs -Wait + } catch { + Write-Warning "Failed to request admin privileges: $_" + return $false + } + } else { + Write-Warning "Failed to import certificate to $storePath : $_" + return $false + } } $imported = Get-ChildItem -Path $storePath | Where-Object { $_.Thumbprint -eq $thumbprint }