From bcd28b62d20d84a5675aa7537dc0c2a5dec76029 Mon Sep 17 00:00:00 2001 From: GOXF <131693427+GOXF@users.noreply.github.com> Date: Sun, 8 Nov 2026 03:49:52 +0000 Subject: [PATCH 1/2] =?UTF-8?q?feat(desktop-organize):=20=E6=98=A0?= =?UTF-8?q?=E5=B0=84=E6=95=B4=E7=90=86=20=E2=80=94=20map=20desktop=20items?= =?UTF-8?q?=20into=20widgets=20without=20moving=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a mapped (virtual) organize mode: the preview option 只映射,不移动文件 creates mapped-organize widgets that map the Desktop and only record membership in their metadata. Native desktop icons are hidden while mapped widgets exist and restored on dissolve/exit/startup repair. New desktop items are auto-assigned after the auto-organize settle delay (switchable to manual 一键整理). See docs/architecture/mapped-organize.md. --- docs/architecture/mapped-organize.md | 90 ++ src/DeskBox/App.MappedOrganize.cs | 45 + src/DeskBox/App.xaml.cs | 3 + .../DesktopOrganizationTaskView.Actions.cs | 5 + ...ktopOrganizationTaskView.MappedOrganize.cs | 116 +++ .../Controls/DesktopOrganizationTaskView.xaml | 2 + .../FileSurfaceContent.MappedOrganize.cs | 42 + .../FileSurfaceContent.SelectionAndMenus.cs | 1 + src/DeskBox/Models/MappedOrganizeModels.cs | 59 ++ .../Services/DesktopIconVisibilityService.cs | 66 ++ .../DesktopOrganizationCoordinator.cs | 6 +- .../DesktopOrganizationRuleResolver.cs | 3 +- .../Services/MappedOrganizeMembership.cs | 259 ++++++ src/DeskBox/Services/MappedOrganizeService.cs | 804 ++++++++++++++++++ .../Services/WidgetManager.MappedOrganize.cs | 31 + src/DeskBox/Services/WidgetManager.cs | 3 +- src/DeskBox/Strings/ar-SA.json | 6 + src/DeskBox/Strings/bn-BD.json | 6 + src/DeskBox/Strings/de-DE.json | 6 + src/DeskBox/Strings/en-US.json | 6 + src/DeskBox/Strings/es-ES.json | 6 + src/DeskBox/Strings/fr-FR.json | 6 + src/DeskBox/Strings/hi-IN.json | 6 + src/DeskBox/Strings/ja-JP.json | 6 + src/DeskBox/Strings/ko-KR.json | 6 + src/DeskBox/Strings/pt-BR.json | 6 + src/DeskBox/Strings/ru-RU.json | 6 + src/DeskBox/Strings/zh-CN.json | 6 + src/DeskBox/Strings/zh-TW.json | 6 + .../WidgetViewModel.ItemHydration.cs | 1 + .../WidgetViewModel.MappedOrganize.cs | 110 +++ .../ViewModels/WidgetViewModel.Operations.cs | 7 + .../WidgetViewModel.SortingAndWatchers.cs | 6 + src/DeskBox/ViewModels/WidgetViewModel.cs | 2 + .../MappedOrganizeMembershipTests.cs | 162 ++++ 35 files changed, 1897 insertions(+), 4 deletions(-) create mode 100644 docs/architecture/mapped-organize.md create mode 100644 src/DeskBox/App.MappedOrganize.cs create mode 100644 src/DeskBox/Controls/DesktopOrganizationTaskView.MappedOrganize.cs create mode 100644 src/DeskBox/Controls/WidgetContents/FileSurfaceContent.MappedOrganize.cs create mode 100644 src/DeskBox/Models/MappedOrganizeModels.cs create mode 100644 src/DeskBox/Services/DesktopIconVisibilityService.cs create mode 100644 src/DeskBox/Services/MappedOrganizeMembership.cs create mode 100644 src/DeskBox/Services/MappedOrganizeService.cs create mode 100644 src/DeskBox/Services/WidgetManager.MappedOrganize.cs create mode 100644 src/DeskBox/ViewModels/WidgetViewModel.MappedOrganize.cs create mode 100644 tests/DeskBox.Tests/MappedOrganizeMembershipTests.cs diff --git a/docs/architecture/mapped-organize.md b/docs/architecture/mapped-organize.md new file mode 100644 index 000000000..9f6748982 --- /dev/null +++ b/docs/architecture/mapped-organize.md @@ -0,0 +1,90 @@ +# 映射整理 (mapped / virtual desktop organize) — fork feature + +Fork-only feature (GOXF/DeskBox `custom`). Like 360桌面助手 / WPS 桌面整理: +desktop items are grouped into widgets **without moving any file**. + +## Model + +* A mapped-organize widget is an ordinary `WidgetKind.File` widget whose + `MappedFolderPath` is the user Desktop. The existing desktop branch of + `WidgetViewModel.LoadFolderContentsAsync` already merges the Public Desktop + and the folder watchers already watch both folders. +* `WidgetConfig.Metadata` carries the feature state, so nothing new is + serialized and backup/restore/removal follow the widget automatically: + * `DeskBox.MappedOrganize = "1"` – marks the widget. + * `DeskBox.MappedOrganize.Category` – classifier category it collects. + * `DeskBox.MappedOrganize.Members` – newline-separated absolute paths. +* Membership is exclusive (one widget per desktop entry). All rules are in the + pure `Services/MappedOrganizeMembership.cs` (unit-tested in + `tests/DeskBox.Tests/MappedOrganizeMembershipTests.cs`). +* `Services/MappedOrganizeService.cs` is the runtime owner (UI-thread + mutations, desktop `FileSystemWatcher`s, auto-assignment, icon state). +* Global flags live in `data/mapped-organize.state` (plain `key=value`, no new + JSON context so the serialization inventory contract stays frozen): + `autoAssignNewItems` (default true) and `desktopIconsHiddenByDeskBox`. + +## Flows + +| Action | Effect | +|---|---| +| Preview option 「只映射,不移动文件」 | `DesktopOrganizationTaskView.MappedOrganize.cs` turns the selected buckets into `MappedOrganizeGroup`s (one per category; existing mapped widget of that category is reused). Files untouched. Everything not selected is then routed by the same rules when auto mode is on, because it would otherwise be invisible. | +| Open / rename / delete inside a mapped widget | Ordinary folder pipeline on the real file. Rename keeps membership (watcher rename → `RenamePath`); delete drops it. | +| Drag between mapped widgets / drop desktop items on one | `WidgetViewModel.ImportPathsAsync` → `ClaimMappedOrganizeDrop` → membership only. | +| Drop non-desktop items on a mapped widget | Normal import (moves into the Desktop folder); the new entry is claimed by that widget (`ExpectIncoming`, 2 min). | +| Drag from a mapped widget into a normal folder widget | Normal move; watcher delete drops the membership. | +| Changes outside DeskBox | Service watcher: rename → keep membership; delete → drop; create → auto-assign. Startup prunes members that no longer exist. | +| New desktop files | Auto mode (default): after `DesktopAutoOrganizationDelaySeconds` + 1 s size-stability check, classified with `DesktopOrganizationClassifier`; same-category mapped widget, else 「其他」 (created on demand). Manual mode: widget menu 「一键整理」 only. Toggle: widget menu 「自动归类新文件」. | +| Dissolve a mapped widget (close/remove widget) | `RemoveWidgetAsync` with `RemoveWidgetOnly` (mapped widgets never follow managed storage) – only the membership in its config disappears. Remaining mapped widgets pick up the orphans (auto mode). | +| 「退出映射整理(恢复桌面图标)」 | Dissolves every mapped widget, icons restored. | + +Ordinary organization never targets mapped widgets: they are filtered out in +`DesktopOrganizationRuleResolver.Resolve`, `CreateExecutionPlan` and +`GetDestinationOptions`. + +## Native desktop icons (user accepted hiding) + +Choice: hide the desktop `SysListView32` (child of the desktop +`SHELLDLL_DefView`) with `ShowWindow(SW_HIDE)` +(`Services/DesktopIconVisibilityService.cs`), **not** Explorer's persisted +"Show desktop icons" setting. + +* Not persisted by Explorer → no failure mode can leave the user permanently + without icons (Explorer restart / sign-out always shows them again). +* Hidden while ≥1 mapped widget exists; shown when the last one is dissolved, + on DeskBox shutdown (`ShutdownStep "mapped-organize"`), and best-effort in + `AppDomain.ProcessExit`. +* Crash safety: `desktopIconsHiddenByDeskBox` is written *before* hiding. On + the next start `MappedOrganizeService.Initialize` either re-applies (mapped + widgets exist) or restores (none left). It runs even if widget restore fails. +* Explorer restarts recreate a visible list view; a 4 s dispatcher timer + re-hides it while mapped widgets exist. + +## Multi-monitor / sleep + +Mapped widgets are normal widgets, so the existing monitor binding, +topology profiles and wake recovery apply unchanged. + +## Upstream-touching edits (keep minimal for the 6-hourly upstream merge) + +* `App.xaml.cs`: 2× `InitializeMappedOrganize(widgetManager);` after widget + restore (success and failure path), 1 shutdown step. +* `WidgetViewModel.cs` (attach/detach), `.ItemHydration.cs` (filter desktop + snapshot), `.SortingAndWatchers.cs` (upsert guard), `.Operations.cs` (drop + claim). +* `DesktopOrganizationRuleResolver.cs`, `DesktopOrganizationCoordinator.cs`: + exclude mapped widgets. +* `DesktopOrganizationTaskView.xaml` (+ `.Actions.cs` one call), + `FileSurfaceContent.SelectionAndMenus.cs` (one call). +* `Strings/*.json`: 6 keys inserted after `DesktopOrganization.Preview.Title` + (zh-CN / zh-TW translated, other locales English fallback). + +## Known gaps + +* Manual mode: new desktop files stay unassigned (and therefore invisible + while icons are hidden) until 「一键整理」. +* Preview "use existing widget" destination choices are ignored in mapped mode + (grouping is per category). +* No dedicated entry in the Settings window yet; the auto/manual switch is in + the mapped widget's blank-area menu. +* Not runtime-verified on a real desktop in this change (CI builds and runs + unit tests only). diff --git a/src/DeskBox/App.MappedOrganize.cs b/src/DeskBox/App.MappedOrganize.cs new file mode 100644 index 000000000..261ffc46a --- /dev/null +++ b/src/DeskBox/App.MappedOrganize.cs @@ -0,0 +1,45 @@ +using DeskBox.Services; + +namespace DeskBox; + +/// Fork feature 「映射整理」: host lifecycle (start after widget restore, always restore icons on exit). +public partial class App +{ + private MappedOrganizeService? _mappedOrganizeService; + + private void InitializeMappedOrganize(WidgetManager widgetManager) + { + if (_mappedOrganizeService is not null) + { + return; + } + + try + { + var service = new MappedOrganizeService( + SettingsService, + widgetManager, + LocalizationService, + UiDispatcherQueue); + _mappedOrganizeService = service; + MappedOrganizeService.Current = service; + service.Initialize(); + // Last-chance restore for paths that bypass the orderly shutdown + // (Environment.Exit from fatal handlers, session end). A hard kill + // cannot run this; the next start repairs it from the durable + // marker, and Explorer itself shows the icons again on restart. + AppDomain.CurrentDomain.ProcessExit += (_, _) => _mappedOrganizeService?.RestoreIconsForExit(); + } + catch (Exception ex) + { + Log($"[Startup] Optional step 'mapped-organize' failed: {ex}"); + } + } + + private void ShutdownMappedOrganize() + { + _mappedOrganizeService?.Dispose(); + _mappedOrganizeService = null; + MappedOrganizeService.Current = null; + } +} diff --git a/src/DeskBox/App.xaml.cs b/src/DeskBox/App.xaml.cs index 39a1f2ade..dc973b810 100644 --- a/src/DeskBox/App.xaml.cs +++ b/src/DeskBox/App.xaml.cs @@ -1551,6 +1551,7 @@ await RunOptionalStartupStepAsync( try { await widgetManager.RestoreWidgetsAsync(); + InitializeMappedOrganize(widgetManager); } catch (Exception ex) { @@ -1559,6 +1560,7 @@ await RunOptionalStartupStepAsync( // blocking startup. Log($"[Startup] Optional step 'restore-widgets' failed: {ex}"); RecordStartupDegradation("restore-widgets", ex.ToString()); + InitializeMappedOrganize(widgetManager); if (startupDesktopLayerReadinessTask is not null) { startupDesktopLayerReadinessTask = null; @@ -6785,6 +6787,7 @@ private async Task ShutdownCoreAsync() shutdownGrace), // Hooks must be removed while their owning tray window still exists. ShutdownStep.Sync("global-hotkey", () => { GlobalHotkeyService?.Dispose(); GlobalHotkeyService = null; }), + ShutdownStep.Sync("mapped-organize", ShutdownMappedOrganize), ShutdownStep.Sync("widgets", () => WidgetManager?.CloseAll()), ShutdownStep.Sync("desktop-organization-window", () => { _desktopOrganizationWindow?.CloseForShutdown(); _desktopOrganizationWindow = null; }), ShutdownStep.Sync("settings-window", () => diff --git a/src/DeskBox/Controls/DesktopOrganizationTaskView.Actions.cs b/src/DeskBox/Controls/DesktopOrganizationTaskView.Actions.cs index fa3674639..ad8057ef8 100644 --- a/src/DeskBox/Controls/DesktopOrganizationTaskView.Actions.cs +++ b/src/DeskBox/Controls/DesktopOrganizationTaskView.Actions.cs @@ -16,6 +16,11 @@ private async void ExecuteButton_Click(object sender, RoutedEventArgs e) return; } + if (await TryRunMappedOrganizeAsync(previewPlan)) + { + return; + } + DesktopOrganizationPlan plan; try { diff --git a/src/DeskBox/Controls/DesktopOrganizationTaskView.MappedOrganize.cs b/src/DeskBox/Controls/DesktopOrganizationTaskView.MappedOrganize.cs new file mode 100644 index 000000000..384cfcc1f --- /dev/null +++ b/src/DeskBox/Controls/DesktopOrganizationTaskView.MappedOrganize.cs @@ -0,0 +1,116 @@ +using DeskBox.Models; +using DeskBox.Services; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; + +namespace DeskBox.Controls; + +/// +/// Fork feature 「映射整理」: the preview's 「只映射,不移动文件」 option. When +/// checked, confirming the preview creates mapped-organize widgets that only +/// record membership; no file leaves the Desktop folders. +/// +public sealed partial class DesktopOrganizationTaskView +{ + private bool _mapOnlyInitialized; + + private void MapOnlyCheckBox_Loaded(object sender, RoutedEventArgs e) + { + if (_mapOnlyInitialized) + { + return; + } + + _mapOnlyInitialized = true; + MapOnlyCheckBox.Content = T("MappedOrganize.Preview.MapOnly"); + ToolTipService.SetToolTip(MapOnlyCheckBox, T("MappedOrganize.Preview.MapOnlyDescription")); + OrganizationCompleted += (_, _) => MapOnlyCheckBox.Visibility = Visibility.Collapsed; + } + + /// Returns true when the mapped path handled the confirm click. + private async Task TryRunMappedOrganizeAsync(DesktopOrganizationPlan previewPlan) + { + if (MapOnlyCheckBox.IsChecked != true) + { + return false; + } + + if (MappedOrganizeService.Current is not { } service) + { + ResultInfo.Severity = InfoBarSeverity.Error; + ResultInfo.Title = T("DesktopOrganization.Result.FailedTitle"); + ResultInfo.Message = T("DesktopOrganization.Result.FailedBody"); + ResultInfo.IsOpen = true; + return true; + } + + List groups = previewPlan.Targets + .Where(target => !_targetSelections.TryGetValue(target.SourceBucketId, out DesktopOrganizationTargetSelection? selection) || + selection.IsSelected) + .GroupBy(target => target.CategoryId, StringComparer.Ordinal) + .Select(group => new MappedOrganizeGroup( + group.Key, + ResolveMappedCategoryName(group.Key), + group.First().PlannedBounds, + group.SelectMany(target => target.Items) + .Where(item => !_excludedSourcePaths.Contains(item.SourcePath)) + .Select(item => item.SourcePath) + .ToList())) + .Where(group => group.Paths.Count > 0) + .ToList(); + if (groups.Count == 0) + { + ResultInfo.Severity = InfoBarSeverity.Warning; + ResultInfo.Title = T("DesktopOrganization.Preview.NothingSelectedTitle"); + ResultInfo.Message = T("DesktopOrganization.Preview.NothingSelectedBody"); + ResultInfo.IsOpen = true; + return true; + } + + _isExecuting = true; + ExecuteButton.IsEnabled = false; + CancelButton.IsEnabled = false; + MapOnlyCheckBox.IsEnabled = false; + try + { + int count = await service.ApplyGroupsAsync(groups); + _hasCompletedExecution = true; + ResultInfo.Severity = InfoBarSeverity.Success; + ResultInfo.Title = T("DesktopOrganization.Result.SuccessTitle"); + ResultInfo.Message = Format("MappedOrganize.Result.Success", count, groups.Count); + ResultInfo.IsOpen = true; + ExecuteButton.Visibility = Visibility.Collapsed; + CancelButton.Visibility = Visibility.Collapsed; + DoneButton.Visibility = Visibility.Visible; + DoneButton.Style = (Style)Application.Current.Resources["AccentButtonStyle"]; + SourceSelectionHost.IsEnabled = false; + TargetSelectionHost.IsEnabled = false; + OrganizationCompleted?.Invoke(this, EventArgs.Empty); + } + catch (Exception ex) + { + App.Log($"[MappedOrganize] Mapped organize failed: {ex}"); + ResultInfo.Severity = InfoBarSeverity.Error; + ResultInfo.Title = T("DesktopOrganization.Result.FailedTitle"); + ResultInfo.Message = T("DesktopOrganization.Result.FailedBody"); + ResultInfo.IsOpen = true; + ExecuteButton.IsEnabled = true; + } + finally + { + _isExecuting = false; + CancelButton.IsEnabled = true; + MapOnlyCheckBox.IsEnabled = true; + DoneButton.IsEnabled = true; + } + + return true; + } + + private static string ResolveMappedCategoryName(string categoryId) + { + string key = $"DesktopOrganization.Category.{categoryId}"; + string localized = T(key); + return string.Equals(localized, key, StringComparison.Ordinal) ? categoryId : localized; + } +} diff --git a/src/DeskBox/Controls/DesktopOrganizationTaskView.xaml b/src/DeskBox/Controls/DesktopOrganizationTaskView.xaml index 2c5890ddd..8a5ecd449 100644 --- a/src/DeskBox/Controls/DesktopOrganizationTaskView.xaml +++ b/src/DeskBox/Controls/DesktopOrganizationTaskView.xaml @@ -228,6 +228,8 @@ +