Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions docs/architecture/mapped-organize.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,54 @@ offers as opt-in: `IncludeKeptOnDesktopItems`, one call in
`MappedOrganizeReorganizePolicy.SelectKeptOptIns`, once per preview so a
refresh does not re-tick what the user unticked). Confirming maps them and
clears the mark (`ApplyGroupsAsync`); unticked entries stay kept. Entry
points: 「重新整理桌面」 in a mapped widget's blank-area menu and in the
points: 「整理预览…」 in a mapped widget's blank-area menu, in the
title-bar 「…」 menu of a mapped widget or of a tab group holding one
(`ContentWidgetWindow.MappedOrganize.cs`, one call in `CreateMoreFlyout`;
`MappedOrganizeReorganizePolicy.ShouldOfferReorganize`). Both open the
organize window (an already open window keeps its preview; 刷新 rescans).
`MappedOrganizeReorganizePolicy.ShouldOfferReorganize`) and in the tray menu.
The one-click organize below includes 「放回桌面」 entries without a preview.

### 一键整理桌面 (one click, replaces opening the preview)

「整理桌面」 finishes in one click; the preview with its 待整理 / 留在桌面 split
is no longer the default. Entries that now run `ForkOneClickOrganizer`
(`Services/ForkOneClickOrganize.cs`, host glue `App.ForkOneClickOrganize.cs`):
the tray 「整理桌面…」 (`OpenDesktopOrganizationFromTray`), the settings
buttons 「一键整理桌面」 (桌面整理) and 「整理桌面」 (文件格子) through
`InProcessSettingsAppActions.ShowDesktopOrganizationWindow` (IPC unchanged),
the mapped widget's blank-area 「一键整理」 and the title-bar 「…」
「一键整理桌面」 (formerly 「重新整理桌面」).

* 移动到格子: the preview's untouched default plan is executed
(`BuildPlanAsync` → `CreateExecutionPlan(plan, [])` → `ExecuteAsync`), so
history, 撤销 and recovery are the same; opt-in kinds (folders, large files)
stay on the desktop as in the preview.
* 只映射: every 「放回桌面」 mark is cleared first
(`ReleaseAllKeptOnDesktop`), the plan includes all opt-in kinds (nothing
moves), entries go to the mapped widget of their own category when one
exists, otherwise to the planner's bucket
(`ForkOneClickOrganizePolicy.BuildMappedGroups`), then 「一键整理」 routing
sweeps the rest.
* Result: a notification (with 撤销 when files were moved). A pending
interrupted organize, an open preview or a host that is not ready opens the
preview instead.
* The preview stays reachable as 「整理预览…」 (tray menu, mapped widget
blank-area menu, title-bar 「…」 menu).
* Automatic sorting of new files still skips 「放回桌面」 entries.

### Same category → same widget (duplicate 「其他」 fix)

Cause: a widget created by an earlier organize gets a rule for the
categories it received (e.g. 「其他」 = [Other]). A category with a single
item (e.g. one video, Media) is merged into the 「其他」 bucket by
`MergeSmallAndOverflowCategories`; that item matches no rule, so the planner
proposed a second, new 「其他」. `DesktopOrganizationCategoryReuse` (one call in
`DesktopOrganizationPlanner.CreatePlan`) now lets a bucket join the widget of
an enabled rule listing its category (unless the rule excludes one of the
bucket's extensions), else a widget whose name equals the bucket's name.
Mapped groups likewise join an existing mapped widget by category, else by
name (`ForkOneClickOrganizePolicy.FindMappedTarget` in `ApplyGroupsAsync`).
Limit: in 只映射 mode a real-folder (moved-files) widget named 「其他」 can't
receive mapped entries, so it still sits next to the mapped 「其他」.

### 拖到桌面 = 放回桌面

Expand Down Expand Up @@ -164,6 +207,12 @@ topology profiles and wake recovery apply unchanged.
* `ContentWidgetWindow.Commands.cs`: one `AppendMappedOrganizeMoreMenuItems`
call in `CreateMoreFlyout`; `DesktopOrganizationTaskView.xaml.cs`: one
`IncludeKeptOnDesktopItems` call in `ScanAsync` (重新整理桌面).
* `App.Tray.cs`: `OpenDesktopOrganizationFromTray` runs the one-click organize,
one `CreateForkOrganizePreviewTrayItem` line (整理预览…).
* `Services/InProcessSettingsAppActions.cs`: `ShowDesktopOrganizationWindow`
→ `OrganizeDesktopOneClick` (one click from the settings buttons).
* `DesktopOrganizationPlanner.cs`: one `DesktopOrganizationCategoryReuse` block
in `CreatePlan` (no duplicate same-category widget).
* `FileService.cs`: DeskBox-hidden members stay displayable (3 conditions).
* `DesktopAutoOrganizationWatcher.cs`: 只映射 hand-off (one block).
* `DesktopOrganizationSettingsSection.xaml`: one host panel for the mode card.
Expand All @@ -175,6 +224,8 @@ topology profiles and wake recovery apply unchanged.

* Manual mode: new desktop files stay unassigned (visible on the desktop)
until 「一键整理」.
* The tray item keeps the upstream text 「整理桌面…」 (upstream string; its
ellipsis is now slightly misleading).
* Public Desktop entries need admin rights to change attributes; when that
fails they stay visible on the desktop as well as in the widget.
* Hidden entries are also hidden in File Explorer's Desktop folder view.
Expand Down
199 changes: 199 additions & 0 deletions src/DeskBox/App.ForkOneClickOrganize.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
using DeskBox.Services;
using H.NotifyIcon.Core;
using Microsoft.UI.Xaml.Controls;

namespace DeskBox;

/// <summary>
/// GOXF fork: 「整理桌面」 finishes in one click. The tray item, the settings
/// buttons (一键整理桌面 / 整理桌面, via ISettingsAppActions) and the mapped
/// widgets' menus run <see cref="ForkOneClickOrganizer"/> directly and report
/// with a notification (with 撤销 when files were moved); the old preview
/// window stays available as 「整理预览…」 in the tray menu and the mapped
/// widgets' menus.
/// </summary>
public partial class App
{
private int _forkOneClickOrganizeRunning;

/// <summary>One-click organize (fire and forget; safe from any thread).</summary>
public void OrganizeDesktopOneClick(IntPtr ownerHwnd = default)
{
if (UiDispatcherQueue is { HasThreadAccess: false } dispatcherQueue)
{
dispatcherQueue.TryEnqueue(() => OrganizeDesktopOneClick(ownerHwnd));
return;
}

_ = OrganizeDesktopOneClickAsync(ownerHwnd);
}

/// <summary>The previous behavior of 「整理桌面」: the organize preview window.</summary>
public void OpenDesktopOrganizationPreview(IntPtr ownerHwnd = default)
{
try
{
CancelBackgroundMemoryCleanup();
ShowDesktopOrganizationWindow(ownerHwnd);
}
catch (Exception ex)
{
Log($"[OneClickOrganize] Opening the organize preview failed: {ex.Message}");
}
}

private async Task OrganizeDesktopOneClickAsync(IntPtr ownerHwnd)
{
if (Interlocked.Exchange(ref _forkOneClickOrganizeRunning, 1) == 1)
{
Log("[OneClickOrganize] Already running; ignored.");
return;
}

try
{
CancelBackgroundMemoryCleanup();
if (WidgetManager is not { } widgetManager || _desktopOrganizationWindow is not null)
{
// Not ready yet, or a preview is open: finish in the preview.
OpenDesktopOrganizationPreview(ownerHwnd);
return;
}

var coordinator = new DesktopOrganizationCoordinator(
SettingsService,
FileService,
widgetManager,
OrganizerService,
LocalizationService);
var organizer = new ForkOneClickOrganizer(coordinator, MappedOrganizeService.Current, ResolveForkCategoryName);
ForkOneClickOrganizeResult result = await organizer.RunAsync(ownerHwnd);
Log($"[OneClickOrganize] mode={result.Mode} outcome={result.Outcome} organized={result.OrganizedCount} retained={result.RetainedCount}");
if (result.Outcome == ForkOneClickOrganizeOutcome.NeedsPreview)
{
OpenDesktopOrganizationPreview(ownerHwnd);
return;
}

ShowForkOneClickOrganizeNotification(result);
if (result.Outcome == ForkOneClickOrganizeOutcome.Organized)
{
_settingsWindow?.RefreshDesktopOrganizationState();
_ = PushSettingsIpcEventAsync(SettingsIpcMethods.HostDesktopOrgRefresh, null);
}
}
catch (Exception ex)
{
Log($"[OneClickOrganize] Failed: {ex}");
ShowForkOrganizeNotice(
ForkOrganizeModeText.Get(ForkOrganizeModeText.Key.OneClickFailedTitle),
ForkOrganizeModeText.Get(ForkOrganizeModeText.Key.OneClickFailedBody),
historyId: null);
}
finally
{
Interlocked.Exchange(ref _forkOneClickOrganizeRunning, 0);
ScheduleLightMemoryCleanup(completedHeavyOperation: true);
}
}

private string ResolveForkCategoryName(string categoryId)
{
string key = $"DesktopOrganization.Category.{categoryId}";
string localized = LocalizationService.T(key);
return string.IsNullOrWhiteSpace(localized) || string.Equals(localized, key, StringComparison.Ordinal)
? categoryId
: localized;
}

private void ShowForkOneClickOrganizeNotification(ForkOneClickOrganizeResult result)
{
if (result.Outcome == ForkOneClickOrganizeOutcome.NothingToOrganize)
{
ShowForkOrganizeNotice(
ForkOrganizeModeText.Get(ForkOrganizeModeText.Key.OneClickNothingTitle),
ForkOrganizeModeText.Get(ForkOrganizeModeText.Key.OneClickNothingBody),
historyId: null);
return;
}

string separator = LocalizationService.CurrentCultureName.StartsWith("zh", StringComparison.OrdinalIgnoreCase) ? "、" : ", ";
string targets = string.Join(separator, (result.TargetNames ?? []).Take(4));
string body = result.RetainedCount > 0
? ForkOrganizeModeText.Format(ForkOrganizeModeText.Key.OneClickPartialFormat, result.OrganizedCount, result.RetainedCount)
: ForkOrganizeModeText.Format(ForkOrganizeModeText.Key.OneClickDoneFormat, result.OrganizedCount,
targets.Length > 0 ? targets : ForkOrganizeModeText.ModeName(result.Mode));
ShowForkOrganizeNotice(
ForkOrganizeModeText.Get(ForkOrganizeModeText.Key.OneClickDoneTitle),
body,
result.UndoHistoryId);
}

private void ShowForkOrganizeNotice(string title, string message, string? historyId)
{
if (IsShuttingDown)
{
return;
}

var arguments = new Dictionary<string, string> { ["type"] = "desktop-organization" };
var actions = new List<NativeAppNotificationAction>();
if (!string.IsNullOrWhiteSpace(historyId))
{
arguments["historyId"] = historyId;
actions.Add(new NativeAppNotificationAction(
LocalizationService.T("DesktopOrganization.Notification.Undo"),
new Dictionary<string, string>
{
["type"] = "desktop-organization",
["action"] = "undo",
["historyId"] = historyId
}));
}

if (_nativeNotificationService?.TryShow(
title,
message,
arguments,
actions.Count > 0 ? actions : null,
options: new NativeAppNotificationOptions(
Tag: "fork-one-click-organize",
Group: DesktopOrganizationNotificationGroup)) == true)
{
return;
}

try
{
_trayIcon?.ShowNotification(
title,
message,
NotificationIcon.Info,
customIconHandle: null,
largeIcon: true,
sound: false,
respectQuietTime: true,
realtime: false,
timeout: TimeSpan.FromSeconds(6));
}
catch (Exception ex)
{
Log($"[OneClickOrganize] Tray notification failed: {ex.Message}");
}
}

/// <summary>Tray: 「整理预览…」 right below 「整理桌面…」 (which is one click now).</summary>
private MenuFlyoutItem CreateForkOrganizePreviewTrayItem(MenuFlyout contextMenu)
{
var item = new MenuFlyoutItem
{
Text = ForkOrganizeModeText.Get(ForkOrganizeModeText.Key.MenuOrganizePreview),
Width = TrayMenuItemWidth,
Icon = new FontIcon { Glyph = "\uE8A1" }
};
AttachTrayMenuItemInteraction(
item,
() => RunTraySettingsActionAsync(contextMenu, () => OpenDesktopOrganizationPreview()));
return item;
}
}
3 changes: 2 additions & 1 deletion src/DeskBox/App.Tray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private void CreateTrayIcon()

_trayCreateWidgetItems.Clear();
contextMenu.Items.Add(organizeDesktopItem);
contextMenu.Items.Add(CreateForkOrganizePreviewTrayItem(contextMenu)); // GOXF fork: 「整理预览…」
contextMenu.Items.Add(new MenuFlyoutSeparator());
foreach (var descriptor in new WidgetContentFactory(LocalizationService).GetCreateEntryDescriptors())
{
Expand Down Expand Up @@ -1501,7 +1502,7 @@ private void OpenFeatureWidgetsFromTray()
private void OpenDesktopOrganizationFromTray()
{
CancelBackgroundMemoryCleanup();
ShowDesktopOrganizationWindow();
OrganizeDesktopOneClick(); // GOXF fork: one click; the preview is 「整理预览…」
}

private void OpenAboutSettingsFromTray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed partial class DesktopOrganizationTaskView
private readonly HashSet<string> _keptOnDesktopOptInsApplied = new(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// 「重新整理桌面」: in 「只映射」 mode, entries put back on the desktop are
/// 「整理预览…」: in 「只映射」 mode, entries put back on the desktop are
/// sorted by the normal rules and start ticked, including the kinds the
/// scanner only offers as opt-in (folders, large files). Confirming maps
/// them and clears the 「放回桌面」 mark; unticking keeps it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,41 @@ private bool TryKeepMappedItemsOnDesktop(IReadOnlyList<WidgetItem> items)
return true;
}

/// <summary>「重新整理桌面」: opens the organize preview (放回桌面 items included, see MappedOrganizeReorganizePolicy).</summary>
internal static void OpenMappedReorganizePreview()
/// <summary>
/// 「一键整理」 (blank menu) and 「一键整理桌面」 (title 「…」 menu): organize the
/// whole desktop in one click by the global 「整理方式」, 「放回桌面」 entries
/// included (see ForkOneClickOrganizer).
/// </summary>
internal static void RunOneClickOrganize()
{
try
{
App.Current.ShowDesktopOrganizationWindow();
App.Current.OrganizeDesktopOneClick();
}
catch (Exception ex)
{
App.Log($"[MappedOrganize] Opening the organize preview failed: {ex.Message}");
App.Log($"[MappedOrganize] One-click organize failed to start: {ex.Message}");
}
}

/// <summary>「整理预览…」: the old organize preview window, kept as a secondary entry.</summary>
internal static void OpenOrganizePreview() => App.Current.OpenDesktopOrganizationPreview();

internal static MenuFlyoutItem CreateOrganizePreviewMenuItem(MenuFlyout flyout)
{
var preview = new MenuFlyoutItem
{
Text = ForkOrganizeModeText.Get(ForkOrganizeModeText.Key.MenuOrganizePreview),
Icon = new FontIcon { Glyph = "\uE8A1" }
};
preview.Click += (_, _) =>
{
flyout.Hide();
OpenOrganizePreview();
};
return preview;
}

private void AddMappedOrganizeMenuItems(MenuFlyout flyout)
{
if (!ViewModel.IsMappedOrganizeWidget || MappedOrganizeService.Current is not { } service)
Expand All @@ -59,24 +81,13 @@ private void AddMappedOrganizeMenuItems(MenuFlyout flyout)
flyout.Items.Add(new MenuFlyoutSeparator());

MenuFlyoutItem organizeNow = CreateMenuItem("MappedOrganize.Menu.OrganizeNow", "\uE8CB");
organizeNow.Click += async (_, _) =>
organizeNow.Click += (_, _) =>
{
flyout.Hide();
await RunAsync(() => service.OrganizeNowAsync());
RunOneClickOrganize();
};
flyout.Items.Add(organizeNow);

var reorganize = new MenuFlyoutItem
{
Text = ForkOrganizeModeText.Get(ForkOrganizeModeText.Key.MenuReorganizeDesktop),
Icon = new FontIcon { Glyph = "\uE8B1" }
};
reorganize.Click += (_, _) =>
{
flyout.Hide();
OpenMappedReorganizePreview();
};
flyout.Items.Add(reorganize);
flyout.Items.Add(CreateOrganizePreviewMenuItem(flyout));

var autoAssign = new ToggleMenuFlyoutItem
{
Expand Down
Loading
Loading