diff --git a/README.en.md b/README.en.md index 4347123..3e4ae8c 100644 --- a/README.en.md +++ b/README.en.md @@ -13,7 +13,7 @@ Most native Windows multi-finger gestures work as "recognize a gesture → play - **Three-finger swipe up · Window overview**: every window flies from where it is into the overview layout, with progress driven entirely by finger travel. Pause and the picture holds halfway; swipe back and it rewinds. - **Three-finger swipe down · App windows view**: tiles only the current app's windows, with minimized ones in a separate row at the bottom, while other windows fade out in place. It follows your fingers the whole way, and a swipe up dismisses it (like macOS App Exposé). In the overview, clicking the title under a window switches to that app's view, and clicking again goes back to the overview. - **Four-finger swipe down · Show desktop**: windows slide off the screen edges with your fingers to reveal the desktop; a four-finger swipe up pulls them back. When the desktop isn't showing, a four-finger swipe up opens the overview (as on macOS). -- **Desktop strip in the overview**: the top of the overview lists all virtual desktops. Hover over it and it expands into live thumbnails; click another desktop to switch right away and zoom from its thumbnail into that desktop. The "+" at the end adds a desktop and the "×" next to a name removes one. Drag a window onto a desktop to move it there, or onto "+" to create a new desktop and move it. +- **Desktop strip in the overview**: the top of the overview lists all virtual desktops. Hover over it and it expands into live thumbnails; click another desktop to switch right away and zoom from its thumbnail into that desktop. Drag thumbnails to reorder the actual desktops; four-finger swipes follow the new order too. The "+" at the end adds a desktop and the "×" next to a name removes one. Drag a window onto a desktop to move it there, or onto "+" to create a new desktop and move it. - **Four-finger horizontal swipe inside the overview**: without leaving the overview, the window area slides sideways with your fingers to the neighboring desktop's page, with its windows already laid out. - **Three-finger horizontal swipe · Switch windows**: a strip of windows pans with your fingers and, on release, snaps to the nearest window and brings it to the front. Alt+Tab uses the same switcher. - **Four-finger horizontal swipe · Switch virtual desktops**: the neighboring desktop slides in with your fingers, with no dead zone at the start. The first and last desktops rubber-band, there is no flicker when the switch finishes, and window shadows match the system's. diff --git a/README.md b/README.md index e628abd..232cd51 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Windows 原生的多指手势大多是「识别到手势 → 播放一段固定 - **三指上滑 · 窗口总览**:每个窗口从原位飞向总览布局,进度完全由手指位移驱动。停住,画面就停在半路;往回滑,画面就倒退。 - **三指下滑 · 应用窗口视图**:只把当前程序的窗口平铺出来,最小化的单独排在底部一排,其他窗口原地淡出;同样全程跟手,向上滑收起(macOS App Exposé)。总览里点某个窗口下面的标题,就直接换成那个程序的视图,再点一次回到总览。 - **四指下滑 · 显示桌面**:窗口跟着手指滑出屏幕边缘,露出桌面;再四指上滑把它们拉回来。桌面没显示时四指上滑打开总览(同 macOS)。 -- **总览桌面条**:总览顶部列出所有虚拟桌面;指针移上去就展开成实时缩略图,点另一个桌面会立刻切过去,并从缩略图放大进入那个桌面。末尾的「+」新建桌面,名字旁的「×」删除桌面。把窗口拖到某个桌面上就移过去,拖到「+」上就新建桌面并移过去。 +- **总览桌面条**:总览顶部列出所有虚拟桌面;指针移上去就展开成实时缩略图,点另一个桌面会立刻切过去,并从缩略图放大进入那个桌面。拖动缩略图可调整真实桌面顺序,四指横滑也按新顺序切换。末尾的「+」新建桌面,名字旁的「×」删除桌面。把窗口拖到某个桌面上就移过去,拖到「+」上就新建桌面并移过去。 - **总览里四指横滑**:不退出总览,窗口区域跟手横向滑到相邻桌面那一页,各桌面的窗口都已排好。 - **三指横滑 · 切换窗口**:窗口条随手指平移,松手吸附到最近的窗口并切到前台。Alt+Tab 也换成同一个切换条。 - **四指横滑 · 切换虚拟桌面**:相邻桌面跟着手指滑入,没有起滑死区。到首尾桌面时有橡皮筋,切换结束时没有闪烁,窗口阴影也与系统一致。 diff --git a/src/GlideShell.Core/Overview/DesktopBar.cs b/src/GlideShell.Core/Overview/DesktopBar.cs index 9ba9ead..2b6656c 100644 --- a/src/GlideShell.Core/Overview/DesktopBar.cs +++ b/src/GlideShell.Core/Overview/DesktopBar.cs @@ -1,4 +1,5 @@ using GlideShell.Core.Geometry; +using GlideShell.Core.Motion; namespace GlideShell.Core.Overview; @@ -31,6 +32,120 @@ public readonly record struct DesktopBarHit(DesktopBarPart Part, int Index) public static DesktopBarHit Nothing => new(DesktopBarPart.None, -1); } +/// 一次桌面拖动排序:从原来的下标移到目标下标。 +public readonly record struct DesktopBarMove(int From, int To); + +/// 桌面拖动排序的纯计算:所有桌面仍以原下标存储,只在画面上临时换到目标槽位。 +public static class DesktopBarReorder +{ + /// 原下标 的桌面在拖动预览里应占的槽位。 + public static int Slot(int index, int from, int to, int count) + { + if (count <= 0 || index < 0 || index >= count || from < 0 || from >= count || to < 0 || to >= count || from == to) return index; + if (index == from) return to; + if (from < to && index > from && index <= to) return index - 1; + if (from > to && index >= to && index < from) return index + 1; + return index; + } + + /// 被拖桌面的中心越过其他槽位中心时,预览顺序随之改变。 + public static int Target(DesktopBarGeometry bar, int from, double draggedCenterX) + { + if (from < 0 || from >= bar.Count) return -1; + int target = from; + if (draggedCenterX > bar.Thumbs[from].CenterX) + for (int i = from + 1; i < bar.Count && draggedCenterX > bar.Thumbs[i].CenterX; i++) target = i; + else + for (int i = from - 1; i >= 0 && draggedCenterX < bar.Thumbs[i].CenterX; i--) target = i; + return target; + } + + /// + /// 拖动排序这一帧第 个桌面的缩略图、收起时的桌面名条与展开后的桌面名位置(屏幕坐标): + /// 其余桌面在槽位之间滑动让位;被拖的跟着指针,松手后从松手处飞到它的槽位,桌面名始终跟着它的缩略图。 + /// 合成层的缩略图与装饰(名字、框)都按它摆,两边不会错开。 + /// + public static (RectD Thumb, RectD Pill, RectD Label) Layout(DesktopBarGeometry bar, in DesktopBarDragFrame drag, int index, double expansion) + { + double slot = drag.SlotOf(index, bar.Count); + var thumb = bar.ThumbRectAt(slot, expansion); + var pill = bar.PillAt(slot); + var label = bar.LabelAt(slot); + if (index != drag.Source) return (thumb, pill, label); + var follow = new RectD(drag.X - drag.GrabX * thumb.Width, drag.Y - drag.GrabY * thumb.Height, thumb.Width, thumb.Height); + var moved = drag.Fly < 0 ? follow : RectD.Lerp(follow, thumb, Math.Clamp(drag.Fly, 0, 1)); + return (moved, pill, label.Offset(moved.Left - thumb.Left, moved.Top - thumb.Top)); + } +} + +/// +/// 桌面拖动排序的让位动画(不可变:UI 线程整体替换,渲染线程只取样)。每个桌面一个显示槽位(可带小数)的弹簧; +/// 预览顺序变化时,各桌面从此刻的位置与速度出发驶向新槽位,所以让位连续、可以随时改道。 +/// +public sealed class DesktopReorderMotion +{ + /// 其余桌面让位 / 回原位的弹簧(与桌面条展开同一手感)。 + public static readonly SpringParameters SlotSpring = new(0.28, 1.0); + + /// 松手后被拖的缩略图飞到槽位的弹簧(与拖窗口飞回同一手感)。 + public static readonly SpringParameters FlySpring = new(0.3, 1.0); + + private readonly SpringAnimation[] _slots; + + private DesktopReorderMotion(SpringAnimation[] slots) => _slots = slots; + + public int Count => _slots.Length; + + /// 所有桌面都在原位(刚开始拖)。 + public static DesktopReorderMotion Identity(int count, long hostUs) + { + var slots = new SpringAnimation[Math.Max(0, count)]; + for (int i = 0; i < slots.Length; i++) slots[i] = new SpringAnimation(SlotSpring, i, i, 0, hostUs); + return new DesktopReorderMotion(slots); + } + + /// + /// 驶向「 移到 」的预览顺序( = 即原顺序)。 + /// 那个桌面直接到位:它正被拖着,画面上跟着指针,松手时要从它的目标槽位算起。 + /// + public DesktopReorderMotion Retarget(int from, int to, long hostUs, int snap = -1) + { + var next = new SpringAnimation[_slots.Length]; + for (int i = 0; i < next.Length; i++) + { + double target = DesktopBarReorder.Slot(i, from, to, next.Length); + var current = _slots[i]; + if (i == snap) next[i] = new SpringAnimation(SlotSpring, target, target, 0, hostUs); + else if (current.Target == target) next[i] = current; + else + { + var (value, velocity, _) = current.Sample(hostUs); + next[i] = new SpringAnimation(SlotSpring, value, target, velocity, hostUs); + } + } + return new DesktopReorderMotion(next); + } + + /// 所有桌面直接停到目标槽位(落定动画被别的输入打断)。 + public DesktopReorderMotion Snap(long hostUs) + { + var next = new SpringAnimation[_slots.Length]; + for (int i = 0; i < next.Length; i++) next[i] = new SpringAnimation(SlotSpring, _slots[i].Target, _slots[i].Target, 0, hostUs); + return new DesktopReorderMotion(next); + } + + /// 第 个桌面此刻的槽位(越界的按原位)。 + public double SlotAt(int index, long hostUs) => index >= 0 && index < _slots.Length ? _slots[index].Sample(hostUs).Value : index; + + /// 此刻所有桌面都已停在目标槽位。 + public bool IsSettled(long hostUs) + { + foreach (var slot in _slots) + if (!slot.Sample(hostUs).Settled) return false; + return true; + } +} + /// /// 一块屏上桌面条的几何(物理像素)。macOS 调度中心的样子:平时顶部一排桌面名(收起), /// 指针移到顶部时展开成一排桌面缩略图(壁纸 + 窗口),名字挪到缩略图下面;展开时下面的窗口区域整体往下缩,给它让位。 @@ -145,6 +260,34 @@ public RectD ThumbRect(int index, double expansion) return RectD.Lerp(seed, full, Math.Clamp(expansion, 0, 1.05)); } + /// 小数槽位 处(拖动排序让位途中)的缩略图:在相邻两个槽位之间插值(各槽位等宽等距)。 + public RectD ThumbRectAt(double slot, double expansion) + { + var (lo, hi, t) = Split(slot); + return RectD.Lerp(ThumbRect(lo, expansion), ThumbRect(hi, expansion), t); + } + + /// 小数槽位处收起时的桌面名条。 + public RectD PillAt(double slot) + { + var (lo, hi, t) = Split(slot); + return RectD.Lerp(Pills[lo], Pills[hi], t); + } + + /// 小数槽位处展开后的桌面名。 + public RectD LabelAt(double slot) + { + var (lo, hi, t) = Split(slot); + return RectD.Lerp(Labels[lo], Labels[hi], t); + } + + private (int Lo, int Hi, double T) Split(double slot) + { + double s = Math.Clamp(double.IsFinite(slot) ? slot : 0, 0, Math.Max(0, Count - 1)); + int lo = (int)s; + return (lo, Math.Min(lo + 1, Math.Max(0, Count - 1)), s - lo); + } + /// 屏幕上的矩形 (例如窗口原位)画在第 个桌面缩略图里的位置。 public RectD InThumb(int index, RectD source, double expansion = 1) { diff --git a/src/GlideShell.Core/Overview/OverviewController.cs b/src/GlideShell.Core/Overview/OverviewController.cs index 5f9b0ac..a120267 100644 --- a/src/GlideShell.Core/Overview/OverviewController.cs +++ b/src/GlideShell.Core/Overview/OverviewController.cs @@ -43,6 +43,13 @@ public interface IOverviewHost /// 宿主立刻在覆盖层下面切换真实桌面,总览留在新桌面上(macOS 调度中心)。 /// void PageChanged(int desktopIndex); + + /// + /// 拖动排序的落定动画结束了(渲染线程,或被手势 / 翻页打断时的那个线程;每次拖动只发一次,撤层时不发)。 + /// 系统排好了的停在新顺序上:宿主在 UI 线程调用 ,返回 true 时紧接着按新顺序重建桌面条; + /// 没排成的已经自己结束。两种情况下,拖动期间跳过的桌面条刷新此时都可以补上。 + /// + void DesktopReorderSettled(); } /// 总览的两种形态。 @@ -61,6 +68,19 @@ public enum OverviewMode Desktop, } +/// 拖动排序桌面这一帧的样子(各桌面的位置用 算)。 +/// 被拖的桌面(原下标)。 +/// 指针位置(屏幕坐标;松手后是松手处)。 +/// 按下处在缩略图里的相对位置(0 ~ 1)。 +/// 松手后被拖的缩略图从松手处飞到它槽位的进度(0 ~ 1);-1 = 还跟着指针。 +/// 各桌面显示槽位的弹簧,按 取样。 +public readonly record struct DesktopBarDragFrame(int Source, double X, double Y, double GrabX, double GrabY, double Fly, DesktopReorderMotion Motion, long TimeUs) +{ + /// 第 个桌面这一帧的槽位(可带小数);桌面数对不上(列表刚变过)时按原位。 + public double SlotOf(int index, int count) + => Motion.Count == count && count > 0 ? Math.Clamp(Motion.SlotAt(index, TimeUs), 0, count - 1) : index; +} + /// 渲染线程每帧取到的总览状态。 /// 桌面条展开程度(0 收起 ~ 1 展开)。 /// 点击后正在放大进入的桌面(-1 = 没有)。 @@ -70,6 +90,7 @@ public enum OverviewMode /// 正在拖动 / 放下的窗口(当前页场景下标,-1 = 没有)。 /// 它这一帧的矩形( ≥ 0 时是飞回的起点)。 /// 没放到别的桌面、正在飞回的进度(0 ~ 1):从 插值到它本来的位置;-1 = 不是在飞回。 +/// 正在拖动 / 等待系统排序 / 播放落位动画的桌面缩略图;null = 没有。 public readonly record struct OverviewFrame( double Progress, double BackdropOpacity, @@ -85,7 +106,8 @@ public readonly record struct OverviewFrame( int DragIndex = -1, RectD DragRect = default, double DragOpacity = 1, - double DragReturn = -1) + double DragReturn = -1, + DesktopBarDragFrame? DesktopDrag = null) { /// 有窗口在拖动或放下途中(标题、悬停框不画)。 public bool Dragging => DragIndex >= 0; @@ -139,6 +161,15 @@ public sealed class OverviewController : IGestureTarget private RectD _dropFrom; private RectD? _dropInto; // 放到别的桌面:飞到这里并淡出;null = 飞回原位 + // 拖动桌面缩略图(UI 线程改;渲染线程取样)。_desktopDragSource ≥ 0 ⇔ _desktopStage ≠ None + private DesktopDragStage _desktopStage; + private int _desktopDragSource = -1, _desktopDragTarget = -1; + private double _desktopDragX, _desktopDragY, _desktopDragGrabX, _desktopDragGrabY; + private DesktopReorderMotion? _desktopMotion; + private SpringAnimation _desktopFly; // 松手后被拖的缩略图飞到槽位:0 → 1 + private bool _desktopCommitted; // 系统已按新顺序排好:动画落定后要由宿主重建桌面条,不能在渲染线程自己结束预览 + private bool _desktopSettledSent; // 已通知宿主(只通知一次) + private enum DragStage { None, @@ -146,6 +177,20 @@ private enum DragStage Dropping, } + private enum DesktopDragStage + { + None, + + /// 跟着指针,其余桌面实时让位。 + Dragging, + + /// 松手了,被拖的缩略图飞向目标槽位,等系统排序返回。 + Moving, + + /// 系统已返回(或取消了):等动画落定后结束预览。 + Settling, + } + private volatile OverviewMode _mode; private bool _visible; private bool _prewarming; @@ -266,7 +311,7 @@ public void GoToDesktop(int index, long hostUs) bool current; lock (_gate) { - if (!_visible || _zoomDesktop >= 0 || index < 0 || index >= _desktops.Count) return; + if (!_visible || _zoomDesktop >= 0 || _desktopDragSource >= 0 || index < 0 || index >= _desktops.Count) return; current = index == _desktops.Current; if (!current) { @@ -279,6 +324,176 @@ public void GoToDesktop(int index, long hostUs) else _host.SwitchDesktop(index); } + // ---------- 拖动桌面缩略图(UI 线程) ---------- + + /// 正在拖动桌面、松手后等待系统排序,或排序动画还没落定(桌面条还按预览顺序画)。 + public bool IsDesktopDragging + { + get { lock (_gate) return _desktopDragSource >= 0; } + } + + /// 开始拖动第 个桌面; 是按下时的缩略图位置。 + public bool BeginDesktopDrag(int index, double x, double y, RectD thumb, long hostUs) + { + lock (_gate) + { + if (!_visible || _mode != OverviewMode.All || _tracking || _pagerTracking || _zoomDesktop >= 0 || _drag != DragStage.None || + _desktopDragSource >= 0 || index < 0 || index >= _desktops.Count || thumb.IsEmpty || _bar.Peek(hostUs) < 0.5) return false; + if (_transition.Target < 1 - SettleEpsilon || _transition.Peek(hostUs) < 1 - 0.02) return false; + if (_page >= 0 && Math.Abs(_pager.Peek(hostUs) - _page) > 0.01) return false; + _desktopDragSource = _desktopDragTarget = index; + _desktopDragX = x; + _desktopDragY = y; + _desktopDragGrabX = Math.Clamp((x - thumb.Left) / thumb.Width, 0, 1); + _desktopDragGrabY = Math.Clamp((y - thumb.Top) / thumb.Height, 0, 1); + _desktopStage = DesktopDragStage.Dragging; + _desktopMotion = DesktopReorderMotion.Identity(_desktops.Count, hostUs); + _desktopCommitted = _desktopSettledSent = false; + _hovered = null; + if (_bar.Target != 1) _bar.AnimateTo(1, hostUs); + return true; + } + } + + /// 拖动中更新指针与目标槽位;其他桌面按目标位置实时让位。 + public void DragDesktopTo(double x, double y, DesktopBarGeometry bar, long hostUs) + { + lock (_gate) + { + if (_desktopStage != DesktopDragStage.Dragging || _desktopMotion is not { } motion || bar.Count != _desktops.Count || + bar.Count != motion.Count) return; + _desktopDragX = x; + _desktopDragY = y; + double width = bar.Thumbs[_desktopDragSource].Width; + double center = x - _desktopDragGrabX * width + width / 2; + int target = DesktopBarReorder.Target(bar, _desktopDragSource, center); + if (target != _desktopDragTarget) + { + _desktopDragTarget = target; + _desktopMotion = motion.Retarget(_desktopDragSource, target, hostUs, snap: _desktopDragSource); + } + if (_bar.Target != 1) _bar.AnimateTo(1, hostUs); + } + } + + /// + /// 松手:被拖的缩略图开始飞向目标槽位,返回排序参数。宿主随后必须调用 + /// (系统排序返回时;不排序时立刻以失败调用,缩略图飞回原位)。 + /// + public DesktopBarMove? DropDesktop(long hostUs) + { + lock (_gate) + { + if (_desktopStage != DesktopDragStage.Dragging) return null; + _desktopStage = DesktopDragStage.Moving; + _desktopFly = new SpringAnimation(DesktopReorderMotion.FlySpring, 0, 1, 0, hostUs); + return new DesktopBarMove(_desktopDragSource, _desktopDragTarget); + } + } + + /// + /// 系统排序返回(UI 线程)。成功:动画落定后由 通知宿主收尾; + /// 返回 true 表示动画此刻已经落定、预览已结束,调用方紧接着按新顺序重建桌面条。 + /// 失败(或没有排序):所有桌面弹回原位,落定后自行结束预览。过时的结果(已撤层、换了一次拖动)什么也不做。 + /// + public bool CompleteDesktopMove(DesktopBarMove move, bool success, long hostUs) + { + lock (_gate) + { + if (_desktopStage != DesktopDragStage.Moving || _desktopDragSource != move.From || _desktopDragTarget != move.To || + _desktopMotion is not { } motion) return false; + _desktopStage = DesktopDragStage.Settling; + if (!success) + { + _desktopDragTarget = _desktopDragSource; + _desktopMotion = motion.Retarget(_desktopDragSource, _desktopDragSource, hostUs); + return false; + } + _desktopCommitted = true; + if (!_desktopFly.Sample(hostUs).Settled || !motion.IsSettled(hostUs)) return false; + ClearDesktopDrag(); + return true; + } + } + + /// 拖动中取消(Esc、捕获丢失):被拖的缩略图飞回原位,其余桌面弹回。 + public void CancelDesktopDrag(long hostUs) + { + if (DropDesktop(hostUs) is { } move) CompleteDesktopMove(move, success: false, hostUs); + } + + /// + /// 收到 后(UI 线程):结束已经由系统排好的预览。 + /// 返回 true 时调用方紧接着按新顺序重建桌面条(中间不能隔帧,否则会闪回旧顺序)。 + /// + public bool FinishSettledDesktopMove() + { + lock (_gate) + { + if (_desktopStage != DesktopDragStage.Settling || !_desktopCommitted) return false; + ClearDesktopDrag(); + return true; + } + } + + /// + /// 按下时排序动画还没落定(UI 线程):跳到终点、结束预览,这次按下照常处理。返回 false = 还在拖动或等系统排序,这次按下不接受。 + /// = 系统已按新顺序排好,调用方要紧接着重建桌面条。 + /// + public bool SkipDesktopDragAnimation(out bool refresh) + { + lock (_gate) + { + refresh = false; + if (_desktopStage == DesktopDragStage.None) return true; + if (_desktopStage != DesktopDragStage.Settling) return false; + refresh = _desktopCommitted; + ClearDesktopDrag(); + return true; + } + } + + /// 立刻结束拖动预览(撤层、场景作废)。 + public void FinishDesktopDrag() + { + lock (_gate) ClearDesktopDrag(); + } + + /// + /// 落定动画途中来了手势 / 翻页(任意线程):动画跳到终点,不让这次输入等它。没排成的直接结束预览(与不拖时的画面一致); + /// 排好了的停在新顺序上,像自然落定一样通知宿主收尾,返回 true:在宿主按新顺序重建之前,各页还是旧顺序。 + /// + private bool SkipDesktopSettle(long hostUs) + { + bool notify, waiting; + lock (_gate) + { + if (_desktopStage != DesktopDragStage.Settling || _desktopMotion is not { } motion) return false; + notify = !_desktopSettledSent; + waiting = _desktopCommitted; + if (!waiting) ClearDesktopDrag(); + else + { + _desktopFly = new SpringAnimation(DesktopReorderMotion.FlySpring, 1, 1, 0, hostUs); + _desktopMotion = motion.Snap(hostUs); + _desktopSettledSent = true; + } + } + if (notify) _host.DesktopReorderSettled(); + return waiting; + } + + /// 鼠标正拖着桌面,或松手后在等系统排序:别的输入要等它(落定动画不用等,见 )。 + private bool DesktopDragHolds => _desktopStage is DesktopDragStage.Dragging or DesktopDragStage.Moving; + + private void ClearDesktopDrag() + { + _desktopStage = DesktopDragStage.None; + _desktopDragSource = _desktopDragTarget = -1; + _desktopMotion = null; + _desktopCommitted = _desktopSettledSent = false; + } + // ---------- 拖动窗口(UI 线程) ---------- /// 正在拖动或放下途中。 @@ -295,7 +510,7 @@ public bool BeginDrag(long windowId, double x, double y, RectD small, long hostU { lock (_gate) { - if (!_visible || _tracking || _pagerTracking || _zoomDesktop >= 0 || _drag != DragStage.None) return false; + if (!_visible || _tracking || _pagerTracking || _zoomDesktop >= 0 || _drag != DragStage.None || _desktopDragSource >= 0) return false; if (_transition.Target < 1 - SettleEpsilon || _transition.Peek(hostUs) < 1 - 0.02) return false; if (_page >= 0 && Math.Abs(_pager.Peek(hostUs) - _page) > 0.01) return false; var scene = Scene; @@ -494,11 +709,12 @@ private void Begin(in GestureEvent e, bool desktopGesture) { bool show = false, cancelPrewarm = false; bool restore = desktopGesture && !_visible && e.Direction == GestureDirection.Up && DesktopShown?.Invoke() == true; + SkipDesktopSettle(e.HostTimeUs); // 桌面排序的落定动画不挡手势:跳到终点(排好了的由宿主在 UI 线程按新顺序重建,与手势无关) lock (_gate) { - if (_zoomDesktop >= 0 || _pagerTracking || _drag == DragStage.Dragging) + if (_zoomDesktop >= 0 || _pagerTracking || _drag == DragStage.Dragging || DesktopDragHolds) { - _ignoring = true; // 正在放大进入别的桌面 / 正在翻页 / 鼠标正拖着窗口,等它落定 + _ignoring = true; // 正在放大进入别的桌面 / 正在翻页 / 鼠标正拖着窗口或桌面(或在等系统排序),等它落定 return; } OverviewMode? open = null; // 收起态下这次手势要展开的形态(null = 不接管) @@ -722,9 +938,29 @@ public OverviewFrame Sample(long hostUs) int dragIndex = -1; RectD dragRect = default; double dragOpacity = 1, dragReturn = -1; + DesktopBarDragFrame? desktopDrag = null; + bool reorderSettled = false; lock (_gate) { zoomDesktop = _zoomDesktop; + if (_desktopStage != DesktopDragStage.None && _desktopMotion is { } motion) + { + double fly = -1; + if (_desktopStage != DesktopDragStage.Dragging) + { + var f = _desktopFly.Sample(hostUs); + fly = Math.Clamp(f.Value, 0, 1); + // 解析弹簧:按时间判断落定,不依赖「刚落定」那一帧有没有取到 + if (_desktopStage == DesktopDragStage.Settling && !_desktopSettledSent && f.Settled && motion.IsSettled(hostUs)) + { + reorderSettled = _desktopSettledSent = true; // 通知宿主:新顺序要等它重建桌面条才能结束 + if (!_desktopCommitted) ClearDesktopDrag(); // 弹回了原顺序:与不拖时的画面一致,直接结束(宿主只补刷新) + } + } + if (_desktopStage != DesktopDragStage.None) + desktopDrag = new DesktopBarDragFrame(_desktopDragSource, _desktopDragX, _desktopDragY, _desktopDragGrabX, _desktopDragGrabY, + fly, motion, hostUs); + } if (_drag == DragStage.Dragging) { dragIndex = _dragIndex; @@ -778,14 +1014,20 @@ public OverviewFrame Sample(long hostUs) activate = true; } } + if (hide) + { + ClearDesktopDrag(); + reorderSettled = false; + } visible = _visible || hide || activate; emphasized = _focusTarget ?? (_closingGesture ? _hovered : null); } if (hide) _host.Hide(focus); else if (activate) _host.Activate(); + if (reorderSettled) _host.DesktopReorderSettled(); return new OverviewFrame(s.Value, OverviewVisuals.BackdropOpacity(s.Value), OverviewVisuals.LabelOpacity(s.Value), s.Phase, visible, emphasized, bar.Value, zoomDesktop, zoomDesktop >= 0 ? zoom.Value : 0, _page >= 0 ? pager.Value : 0, - relayout.Value, dragIndex, dragRect, dragOpacity, dragReturn); + relayout.Value, dragIndex, dragRect, dragOpacity, dragReturn, desktopDrag); } /// @@ -860,9 +1102,11 @@ private sealed class DesktopGestureTarget(OverviewController owner) : IGestureTa public bool StepPage(int direction, long hostUs) { int changed = -1; + // 桌面排序的落定动画跳到终点;排好了的要等宿主按新顺序重建各页(通常不到一帧),这一下不翻 + if (SkipDesktopSettle(hostUs)) return false; lock (_gate) { - if (!_visible || _pages.Count < 2 || _page < 0 || _pagerTracking || _zoomDesktop >= 0 || _drag != DragStage.None) return false; + if (!_visible || _pages.Count < 2 || _page < 0 || _pagerTracking || _zoomDesktop >= 0 || _drag != DragStage.None || _desktopDragSource >= 0) return false; int target = Math.Clamp(_page + Math.Sign(direction), 0, _pages.Count - 1); if (target == _page) return true; _page = target; @@ -894,10 +1138,12 @@ public void Unprepare(long hostUs) private void BeginDesktopSwipe(in GestureEvent e) { + // 桌面排序的落定动画跳到终点;排好了的要等宿主按新顺序重建各页(宿主在 UI 线程上重建会打断跟手翻页),这一次不接 + bool waiting = SkipDesktopSettle(e.HostTimeUs); lock (_gate) { - _pagerIgnoring = !_visible || _tracking || _zoomDesktop >= 0 || _pages.Count < 2 || _page < 0 || - _transition.Target < 1 - SettleEpsilon || _drag != DragStage.None; + _pagerIgnoring = waiting || !_visible || _tracking || _zoomDesktop >= 0 || _pages.Count < 2 || _page < 0 || + _transition.Target < 1 - SettleEpsilon || _drag != DragStage.None || _desktopDragSource >= 0; if (_pagerIgnoring) return; // 总览还没展开好、或只有一个桌面:不接管 _pagerTracking = true; _pageAtBegin = _page; diff --git a/src/GlideShell.Platform/Overview/BarDecor.cs b/src/GlideShell.Platform/Overview/BarDecor.cs index ddfd05c..cb34325 100644 --- a/src/GlideShell.Platform/Overview/BarDecor.cs +++ b/src/GlideShell.Platform/Overview/BarDecor.cs @@ -198,19 +198,25 @@ public void Apply(in OverviewFrame frame, double alpha, long nowUs, double wallp int current = _current, hoverIndex = _hoverIndex; var part = _hoverPart; var origin = g.Area; + var drag = frame.DesktopDrag; double dpi = Dpi; lock (_res.Backend.Gate) { for (int i = 0; i < desktops.Length; i++) { var d = desktops[i]; - bool hovered = hoverIndex == i && part is DesktopBarPart.Desktop or DesktopBarPart.Remove; + bool hovered = drag is null && hoverIndex == i && part is DesktopBarPart.Desktop or DesktopBarPart.Remove; d.Current = _first ? (i == current ? 1 : 0) : Approach(d.Current, i == current ? 1 : 0, dt, 0.07); d.Hover = Approach(d.Hover, hovered ? 1 : 0, dt, hovered ? 0.05 : 0.09); d.RemoveHover = Approach(d.RemoveHover, hovered && part == DesktopBarPart.Remove ? 1 : 0, dt, 0.05); - var pill = Local(g.Pills[i], origin); - var thumb = Local(g.ThumbRect(i, e), origin); + // 拖动排序:与 DesktopBarView 的缩略图同一套位置(让位途中在槽位之间、被拖的跟着指针或飞向槽位) + var (thumbAt, pillAt, labelAt) = drag is { } dragged + ? DesktopBarReorder.Layout(g, dragged, i, e) + : (g.ThumbRect(i, e), g.Pills[i], g.Labels[i]); + var pill = Local(pillAt, origin); + var thumb = Local(thumbAt, origin); + var label = Local(labelAt, origin); double hover = d.Hover * (1 - d.Current); d.Glass?.Place(a * (1 - e) * Math.Max(d.Current, d.Hover), wallpaper); d.CurrentPill?.Place(pill, a * (1 - e) * d.Current); @@ -219,24 +225,23 @@ public void Apply(in OverviewFrame frame, double alpha, long nowUs, double wallp d.CurrentRing?.Place(thumb.Inflate(2 * dpi, 2 * dpi), a * e * d.Current * (1 - d.Hover)); d.HoverRing?.Place(thumb.Inflate(3 * dpi, 3 * dpi), a * e * d.Hover); - // 桌面名:收起时在名字条里,展开时在缩略图下面 + // 桌面名:收起时在名字条里,展开时在缩略图下面;拖动时跟着被拖的缩略图 if (d.TextW > 0) { - var label = Local(g.Labels[i], origin); double cx = pill.CenterX + (label.CenterX - pill.CenterX) * e, cy = pill.CenterY + (label.CenterY - pill.CenterY) * e; double bright = DimName + (1 - DimName) * Math.Max(d.Current, d.Hover); Place(d.Text, cx - d.TextW / 2, cy - d.TextH / 2, a * bright, ref d.TextHidden); } - // 「×」:展开后悬停的桌面名右端(最后一个桌面不能删) + // 「×」:展开后悬停的桌面名右端(最后一个桌面不能删);排序时不显示 var x = Local(g.RemoveButton(i), origin); - double removable = desktops.Length >= 2 ? a * e * d.Hover : 0; + double removable = drag is null && desktops.Length >= 2 ? a * e * d.Hover : 0; d.RemoveDisc?.Place(x, removable * d.RemoveHover); Place(d.RemoveGlyph, x.Left, x.Top, removable * (0.8 + 0.2 * d.RemoveHover), ref d.GlyphHidden); } // 末尾的「+」:收起时是小圆块(悬停才有底色),展开时是与缩略图同高的方块 - bool addHovered = part == DesktopBarPart.Add; + bool addHovered = drag is null && part == DesktopBarPart.Add; _addHover = Approach(_addHover, addHovered ? 1 : 0, dt, addHovered ? 0.05 : 0.09); var add = Local(RectD.Lerp(g.AddPill, g.AddTile, e), origin); _addBg?.Place(add, a * e * (1 - _addHover)); diff --git a/src/GlideShell.Platform/Overview/DesktopBarView.cs b/src/GlideShell.Platform/Overview/DesktopBarView.cs index a824e67..1e2b280 100644 --- a/src/GlideShell.Platform/Overview/DesktopBarView.cs +++ b/src/GlideShell.Platform/Overview/DesktopBarView.cs @@ -116,11 +116,13 @@ public bool Apply(in OverviewFrame frame) if (hidden && _hiddenApplied) return false; // 收起着:缩略图已经全透明,不必每帧再设 _hiddenApplied = hidden; + var drag = frame.DesktopDrag; for (int i = 0; i < slots.Length && i < g.Count; i++) { var s = slots[i]; bool chosen = i == target; - var r = chosen ? g.ZoomRect(i, e, z) : g.ThumbRect(i, e); + // 拖动排序:与 BarDecor 的名字、框同一套位置(拖动中不会有放大进入) + var r = drag is { } d ? DesktopBarReorder.Layout(g, d, i, e).Thumb : chosen ? g.ZoomRect(i, e, z) : g.ThumbRect(i, e); // 选中的那个点下去就不透明(可能还没完全展开就点了);其余随放大淡出 double alpha = chosen ? Math.Max(e, OverviewVisuals.SmoothStep(0, 0.15, z)) : e * (1 - z); double scale = r.Width / g.Area.Width; diff --git a/src/GlideShell.Platform/Overview/OverlayWindow.cs b/src/GlideShell.Platform/Overview/OverlayWindow.cs index f0f6cb5..635d7f9 100644 --- a/src/GlideShell.Platform/Overview/OverlayWindow.cs +++ b/src/GlideShell.Platform/Overview/OverlayWindow.cs @@ -608,6 +608,12 @@ public int PrepareBar(DesktopBarGeometry? geometry, DesktopState desktops, IRead return _bar.Prepare(shared ? _shared : null, shared ? _barRoot : null, Handle, Monitor, geometry, desktops, windows, foreground, CornerRadiusDip); } + /// 有可供拖动的桌面缩略图,且名字与框由合成层画(GDI 退路的 PaintBar 不按拖动预览的槽位画,不支持拖动)。 + public bool CanDragDesktops => BarDecorActive; + + /// UI 线程:开始拖动第 个桌面,把它挪到其余缩略图上面。 + public void BeginDesktopDrag(int index) => _bar.BringToFront(index); + /// UI 线程:点了第 个桌面,它要放大进入,挪到其余桌面上面。 public void BeginZoom(int index) => _bar.BringToFront(index); diff --git a/src/GlideShell.Platform/Overview/OverviewPresenter.cs b/src/GlideShell.Platform/Overview/OverviewPresenter.cs index c7402f6..2231841 100644 --- a/src/GlideShell.Platform/Overview/OverviewPresenter.cs +++ b/src/GlideShell.Platform/Overview/OverviewPresenter.cs @@ -1,4 +1,4 @@ -using GlideShell.Core.Configuration; +using GlideShell.Core.Configuration; using GlideShell.Core.Geometry; using GlideShell.Core.Motion; using GlideShell.Core.Overview; @@ -72,7 +72,10 @@ private sealed class PageBuffers(int pages, Func size) private OverlayWindow? _press; // 按下时所在的覆盖层(null = 没按着) private double _pressX, _pressY; private long? _pressWindow; // 按在了哪个窗口上(按在桌面条上为 null) + private int _pressDesktop = -1; // 按在了哪个桌面缩略图上(-1 = 没有) private bool _dragging; + private bool _draggingDesktop; + private bool _barRefreshPending; // UI 线程:拖动排序预览中跳过了桌面条刷新(窗口关了、桌面增删完成),预览结束时补上 private long _dragWindow; private Task? _dropMove; // 已放到别的桌面:移动窗口(新建桌面时先建)的任务,结果是目标桌面 private readonly HashSet _moved = []; // 本次会话移到别的桌面的窗口 @@ -507,6 +510,7 @@ private void PrepareScene() // 桌面条里各桌面的窗口:跨桌面再枚举一次(最小化的不在桌面画面里) long t3 = HostClock.NowUs(); _barState = bar ? desktops : DesktopState.Empty; + _barRefreshPending = false; _allWindows = bar ? WindowCatalog.Snapshot(monitors, OwnWindows.ToHashSet(), allDesktops: true) : []; _barWindows = _allWindows.Where(w => !w.IsMinimized).ToList(); nint foreground = User32.GetForegroundWindow(); @@ -799,7 +803,11 @@ private void ReleaseScene() _dropMove = null; _removeOnSwitch = null; _dragging = false; + _draggingDesktop = false; _press = null; + _pressWindow = null; + _pressDesktop = -1; + Controller.FinishDesktopDrag(); _dockApps = []; _dockHovered = -1; _dockOverlay = null; @@ -825,8 +833,7 @@ private void EnsureOverlays(List monitors) } /// - /// 鼠标:按下记下位置,没拖动就松开 = 点击(按下时的位置);按在窗口上拖过系统拖动阈值就开始拖动窗口 - /// (只在能移动窗口、有桌面条时;否则照旧是点击)。 + /// 鼠标:按下记下位置,没拖动就松开 = 点击(按下时的位置);按在窗口或桌面缩略图上拖过系统阈值就开始拖动。 /// private void OnMouse(OverlayWindow overlay, OverlayMouse kind, double x, double y) { @@ -845,43 +852,73 @@ private void OnMouse(OverlayWindow overlay, OverlayMouse kind, double x, double switch (kind) { case OverlayMouse.Down: + // 等系统排序时不再接受按下,避免并发新建 / 删除 / 再次排序;排好了只是动画还没落定:跳到终点,照常按下 + if (!Controller.SkipDesktopDragAnimation(out bool reordered)) break; + if (reordered || _barRefreshPending) RefreshBar(); _press = overlay; _pressX = x; _pressY = y; _pressWindow = PressedWindow(overlay, x, y); + _pressDesktop = PressedDesktop(overlay, x, y); break; case OverlayMouse.Move: if (_dragging) DragMove(OverlayAt(overlay, x, y), x, y); - else if (_press is not null && _pressWindow is { } id && Math.Max(Math.Abs(x - _pressX), Math.Abs(y - _pressY)) >= DragThreshold(overlay)) + else if (_draggingDesktop) DesktopDragMove(OverlayAt(overlay, x, y), x, y); + else if (_press is not null && Math.Max(Math.Abs(x - _pressX), Math.Abs(y - _pressY)) >= DragThreshold(overlay)) { - if (!BeginDrag(overlay, id, x, y)) _pressWindow = null; // 不能拖:松开时照旧按点击处理 - else DragMove(overlay, x, y); + if (_pressDesktop >= 0) + { + if (!BeginDesktopDrag(overlay, _pressDesktop, _pressX, _pressY)) _pressDesktop = -1; // 不能拖:松开时照旧按点击处理 + else DesktopDragMove(overlay, x, y); // 抓取点仍按最初按下处计算,避免越过阈值时跳动 + } + else if (_pressWindow is { } id) + { + if (!BeginDrag(overlay, id, x, y)) _pressWindow = null; + else DragMove(overlay, x, y); + } } else if (_press is null) OnMouseMove(overlay, x, y); break; case OverlayMouse.Up: if (_dragging) Drop(OverlayAt(overlay, x, y), x, y); + else if (_draggingDesktop) DropDesktop(OverlayAt(overlay, x, y), x, y); else if (_press is { } pressed) OnClick(pressed, _pressX, _pressY); _press = null; + _pressWindow = null; + _pressDesktop = -1; break; case OverlayMouse.Lost: EndPress(); break; case OverlayMouse.RightUp: - if (!_dragging && overlay.Dock is { } dock && dock.Contains(x, y)) ActivateDock(dock.HitTest(x, y), menu: true, (x, y)); + if (!_dragging && !_draggingDesktop && overlay.Dock is { } dock && dock.Contains(x, y)) ActivateDock(dock.HitTest(x, y), menu: true, (x, y)); break; } } - /// 取消按下 / 拖动(窗口飞回原位)。 + /// 取消按下 / 拖动(窗口或桌面缩略图回到原位)。 private void EndPress() { + long now = HostClock.NowUs(); if (_dragging) { _dragging = false; - Controller.EndDrag(null, HostClock.NowUs()); + Controller.EndDrag(null, now); + } + if (_draggingDesktop) + { + _draggingDesktop = false; + Controller.CancelDesktopDrag(now); // 飞回原位、其余桌面弹回(已撤层时什么也不做) + Controller.SetBarHovered(false, now); + if (_barHovered != DesktopBarHit.Nothing) + { + _barHovered = DesktopBarHit.Nothing; + UpdateDecorations(); + } } _press = null; + _pressWindow = null; + _pressDesktop = -1; } /// 指针所在那块屏的覆盖层:拖着窗口可以拖到别的屏上(按下的覆盖层捕获了鼠标);不在任何屏的工作区里时用 。 @@ -890,6 +927,83 @@ private OverlayWindow OverlayAt(OverlayWindow fallback, double x, double y) private static double DragThreshold(OverlayWindow overlay) => Math.Max(User32.GetSystemMetrics(User32.SM_CXDRAG), 6 * overlay.Monitor.DpiScale); + /// 按下处的桌面缩略图(展开后才可拖,「×」与「+」不算)。 + private int PressedDesktop(OverlayWindow overlay, double x, double y) + { + if (_desktops is not { Available: true } || !overlay.CanDragDesktops || overlay.Bar is not { } bar || _barState.Count < 2) return -1; + long now = HostClock.NowUs(); + double e = Controller.BarExpansionAt(now); + if (e < 0.5 || !bar.Contains(x, y, e)) return -1; + var hit = BarHit(bar, x, y, e); + return hit.Part == DesktopBarPart.Desktop && hit.Index < _barState.Count ? hit.Index : -1; + } + + private bool BeginDesktopDrag(OverlayWindow overlay, int index, double x, double y) + { + if (overlay.Bar is not { } bar) return false; + long now = HostClock.NowUs(); + var thumb = bar.ThumbRect(index, Controller.BarExpansionAt(now)); + if (!Controller.BeginDesktopDrag(index, x, y, thumb, now)) return false; + _draggingDesktop = true; + Select(-1); + foreach (var o in _overlays) o.BeginDesktopDrag(index); + if (_barHovered != DesktopBarHit.Nothing) + { + _barHovered = DesktopBarHit.Nothing; + UpdateDecorations(); + } + Log.Info($"总览桌面条:开始拖动桌面 {index + 1}「{_barState.Names[index]}」"); + return true; + } + + /// 桌面缩略图跟手移动,中心越过相邻槽位时实时预览新顺序。 + private void DesktopDragMove(OverlayWindow overlay, double x, double y) + { + if (overlay.Bar is not { } bar) return; + long now = HostClock.NowUs(); + Controller.SetBarHovered(true, now); + Controller.DragDesktopTo(x, y, bar, now); + } + + /// + /// 松手:缩略图飞向目标槽位,同时调用 explorer 的 MoveDesktop;动画落定且系统返回后才按真实状态重建桌面条与各页。 + /// 没换位置、或放在桌面条外:飞回原位。 + /// + private void DropDesktop(OverlayWindow overlay, double x, double y) + { + _draggingDesktop = false; + long now = HostClock.NowUs(); + if (Controller.DropDesktop(now) is not { } move) return; + if (move.From == move.To || _desktops is not { Available: true } desktops || overlay.Bar is not { } bar || !bar.Contains(x, y, 1) || + move.From < 0 || move.From >= _barState.Count || move.To < 0 || move.To >= _barState.Count) + { + Controller.CompleteDesktopMove(move, success: false, now); + return; + } + Guid desktop = _barState.Desktops[move.From]; + string name = _barState.Names[move.From]; + _ = desktops.MoveDesktopAsync(desktop, move.To).ContinueWith(t => + _ui.Post(() => FinishDesktopMove(t.Result, move, name)), TaskScheduler.Default); + } + + private void FinishDesktopMove(int hr, DesktopBarMove move, string name) + { + if (hr < 0) Log.Warn($"总览桌面条:移动「{name}」到位置 {move.To + 1} 失败 0x{hr:X8}"); + else Log.Info($"总览桌面条:「{name}」从位置 {move.From + 1} 移到 {move.To + 1}"); + // 成功且动画已落定:现在就按新顺序重建(桌面页按 id 跟随,不会切错桌面);还在动:落定时由 DesktopReorderSettled 收尾。 + // 失败:各桌面弹回原位,落定后控制器自己结束预览(原顺序不用重建) + if (Controller.CompleteDesktopMove(move, hr >= 0, HostClock.NowUs())) RefreshBar(); + } + + /// + /// 渲染线程(或被手势打断时的输入线程):桌面排序的动画落定了。排好了的:结束预览与按新顺序重建桌面条在 UI 线程上紧挨着做, + /// 不会闪回旧顺序;没排成的预览已经结束,拖动期间跳过的刷新在这里补上。 + /// + public void DesktopReorderSettled() => _ui.Post(() => + { + if (Controller.FinishSettledDesktopMove() || _barRefreshPending) RefreshBar(); + }); + /// 按下处的窗口(只有能把窗口移到别的桌面时才算,按在桌面条上不算)。 private long? PressedWindow(OverlayWindow overlay, double x, double y) { @@ -1257,10 +1371,17 @@ private void RefreshBar() private bool RefreshBarThumbnails() { if (!_shown || _desktops is null || Controller.Mode != OverviewMode.All) return false; + if (Controller.IsDesktopDragging) + { + // 拖动排序预览中:桌面条按原下标加预览槽位画,换成新列表会错位。记下来,预览结束时整条重建(DesktopReorderSettled / 按下) + _barRefreshPending = true; + return false; + } var state = _desktops.State; if (state.Count < 2 || state.CurrentIndex < 0) return false; var monitors = _overlays.Select(o => o.Monitor).ToList(); _barState = state; + _barRefreshPending = false; _barWindows = WindowCatalog.Snapshot(monitors, OwnWindows.ToHashSet(), allDesktops: true).Where(w => !w.IsMinimized).ToList(); nint foreground = User32.GetForegroundWindow(); foreach (var o in _overlays) diff --git a/src/GlideShell.Platform/VirtualDesktops/VirtualDesktopLayout.cs b/src/GlideShell.Platform/VirtualDesktops/VirtualDesktopLayout.cs index 0379b58..3df4649 100644 --- a/src/GlideShell.Platform/VirtualDesktops/VirtualDesktopLayout.cs +++ b/src/GlideShell.Platform/VirtualDesktops/VirtualDesktopLayout.cs @@ -4,7 +4,7 @@ namespace GlideShell.Platform.VirtualDesktops; /// 未公开虚拟桌面接口的「IID + vtable 槽位」表,按构建号选择(S3 用系统代理 DLL 的 MIDL 格式串逐槽核对过 26300)。 /// 同一个 IID 不代表同一布局:22631.3085+ 与 26100+ 的 IVirtualDesktopManagerInternal IID 相同, /// 但 26100 在 SwitchDesktop 之后插入了一个方法,后面的槽位整体后移。所以启动时还要核对方法数()。 -/// GlideShell 用 IVirtualDesktopManagerInternal 的槽 3/4/6/7/8/9(两种布局里位置相同)与新建 / 删除桌面(26100 起后移一位), +/// GlideShell 用 IVirtualDesktopManagerInternal 的槽 3/4/6/7/8/9(两种布局里位置相同)与新建 / 排序 / 删除桌面(26100 起后移一位), /// IVirtualDesktop 的 4/5,以及 IApplicationViewCollection 的 GetViewForHwnd(移动窗口用),其余槽位不调用。 /// internal sealed record VirtualDesktopLayout @@ -28,6 +28,7 @@ internal sealed record VirtualDesktopLayout public int GetAdjacentDesktop { get; init; } = 8; public int SwitchDesktop { get; init; } = 9; // 无动画;动画版(22/21)不用:并发调用可能让 explorer 崩溃 public int CreateDesktop { get; init; } = 11; // (out IVirtualDesktop):加在最后 + public int MoveDesktop { get; init; } = 12; // (IVirtualDesktop, i32 目标下标) public int RemoveDesktop { get; init; } = 13; // (IVirtualDesktop 删除, IVirtualDesktop 窗口并入) // IApplicationViewCollection(服务 id 与接口 id 相同) @@ -63,6 +64,7 @@ internal sealed record VirtualDesktopLayout MinBuild = 22631, ManagerInternalMethodCount = 24, CreateDesktop = 10, // 没有 26100 在 SwitchDesktop 之后插入的那个方法 + MoveDesktop = 11, RemoveDesktop = 12, }; diff --git a/src/GlideShell.Platform/VirtualDesktops/VirtualDesktopService.cs b/src/GlideShell.Platform/VirtualDesktops/VirtualDesktopService.cs index f2f4c2e..357e1c0 100644 --- a/src/GlideShell.Platform/VirtualDesktops/VirtualDesktopService.cs +++ b/src/GlideShell.Platform/VirtualDesktops/VirtualDesktopService.cs @@ -142,6 +142,25 @@ public Task RemoveDesktopAsync(Guid desktop, Guid fallback) return tcs.Task; } + /// 把桌面移到从左到右的第 个位置。返回 HRESULT;完成时 已更新。 + public Task MoveDesktopAsync(Guid desktop, int index) + { + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + Post(() => + { + if (!EnsureConnected() || index < 0 || index >= _state.Count || !_desktops.TryGetValue(desktop, out nint d)) + { + tcs.SetResult(unchecked((int)0x80004005)); + return; + } + int hr = ((delegate* unmanaged)Slot(_manager, _layout!.MoveDesktop))(_manager, d, index); + if (hr is RpcDisconnected or RpcServerUnavailable) Disconnect(); + else ReadState(); + tcs.SetResult(hr); + }); + return tcs.Task; + } + /// /// 把顶层窗口 移到桌面 (不切换桌面)。返回 HRESULT。 /// 公开的 IVirtualDesktopManager::MoveWindowToDesktop 只能移本进程的窗口,所以走 explorer 的视图对象。 diff --git a/tests/GlideShell.Tests/Overview/DesktopBarTests.cs b/tests/GlideShell.Tests/Overview/DesktopBarTests.cs index 5159aa0..24c5fc9 100644 --- a/tests/GlideShell.Tests/Overview/DesktopBarTests.cs +++ b/tests/GlideShell.Tests/Overview/DesktopBarTests.cs @@ -105,6 +105,7 @@ public void Show() { } public void Hide(long? focusWindowId) => Calls.Add($"Hide:{focusWindowId?.ToString() ?? "-"}"); public void SwitchDesktop(int desktopIndex) => Calls.Add($"SwitchDesktop:{desktopIndex}"); public void PageChanged(int desktopIndex) => Calls.Add($"PageChanged:{desktopIndex}"); + public void DesktopReorderSettled() => Calls.Add("DesktopReorderSettled"); } private static (OverviewController C, FakeHost Host, long T) Opened() @@ -256,6 +257,458 @@ public void Clicking_the_current_desktop_just_closes() Assert.Equal("Closing:-", host.Calls[^1]); } + // ---------- 拖动桌面排序 ---------- + + [Fact] + public void Reorder_slots_move_the_dragged_desktop_and_shift_the_others() + { + // 0 拖到 2:预览为 1、2、0 + Assert.Equal(2, DesktopBarReorder.Slot(0, 0, 2, 3)); + Assert.Equal(0, DesktopBarReorder.Slot(1, 0, 2, 3)); + Assert.Equal(1, DesktopBarReorder.Slot(2, 0, 2, 3)); + // 2 拖到 0:预览为 2、0、1 + Assert.Equal(1, DesktopBarReorder.Slot(0, 2, 0, 3)); + Assert.Equal(2, DesktopBarReorder.Slot(1, 2, 0, 3)); + Assert.Equal(0, DesktopBarReorder.Slot(2, 2, 0, 3)); + // 原样放下或无效下标不改变任何槽位 + for (int i = 0; i < 3; i++) + { + Assert.Equal(i, DesktopBarReorder.Slot(i, 1, 1, 3)); + Assert.Equal(i, DesktopBarReorder.Slot(i, -1, 2, 3)); + } + } + + [Fact] + public void Reorder_target_changes_only_after_crossing_neighbor_centers() + { + var bar = DesktopBarGeometry.Compute(Area, 3, 1.5); + Assert.Equal(0, DesktopBarReorder.Target(bar, 0, bar.Thumbs[0].CenterX)); + Assert.Equal(0, DesktopBarReorder.Target(bar, 0, bar.Thumbs[1].CenterX - 1)); + Assert.Equal(1, DesktopBarReorder.Target(bar, 0, bar.Thumbs[1].CenterX + 1)); + Assert.Equal(2, DesktopBarReorder.Target(bar, 0, bar.Thumbs[2].CenterX + 1)); + Assert.Equal(1, DesktopBarReorder.Target(bar, 2, bar.Thumbs[1].CenterX - 1)); + Assert.Equal(0, DesktopBarReorder.Target(bar, 2, bar.Thumbs[0].CenterX - 1)); + Assert.Equal(-1, DesktopBarReorder.Target(bar, 3, 0)); + } + + /// 展开好桌面条的总览(三个桌面),返回落定后的时间。 + private static (OverviewController C, FakeHost Host, long T, DesktopBarGeometry Bar) OpenedBar() + { + var (c, host, t) = Opened(); + c.SetBarHovered(true, t); + t += 2_000_000; + c.Sample(t); + return (c, host, t, DesktopBarGeometry.Compute(Area, 3, 1.5)); + } + + private static RectD ThumbOf(in OverviewFrame frame, DesktopBarGeometry bar, int index) + => frame.DesktopDrag is { } d ? DesktopBarReorder.Layout(bar, d, index, frame.BarExpansion).Thumb : bar.ThumbRect(index, frame.BarExpansion); + + private static double Distance(RectD a, RectD b) => Math.Max(Math.Abs(a.Left - b.Left), Math.Abs(a.Top - b.Top)); + + [Fact] + public void Fractional_slots_interpolate_between_neighbors_and_clamp_at_the_ends() + { + var bar = DesktopBarGeometry.Compute(Area, 3, 1.5); + Assert.Equal((bar.Thumbs[0].CenterX + bar.Thumbs[1].CenterX) / 2, bar.ThumbRectAt(0.5, 1).CenterX, 6); + Assert.Equal(bar.Thumbs[2], bar.ThumbRectAt(2, 1)); + Assert.Equal(bar.Thumbs[0], bar.ThumbRectAt(-0.4, 1)); + Assert.Equal(bar.Thumbs[2], bar.ThumbRectAt(7, 1)); + Assert.Equal(bar.Thumbs[0], bar.ThumbRectAt(double.NaN, 1)); + Assert.Equal((bar.Labels[1].Left + bar.Labels[2].Left) / 2, bar.LabelAt(1.5).Left, 6); + Assert.Equal((bar.Pills[1].Left + bar.Pills[2].Left) / 2, bar.PillAt(1.5).Left, 6); + + // 桌面数对不上(列表刚变过):按原位,不越界 + var drag = new DesktopBarDragFrame(0, 0, 0, 0.5, 0.5, -1, DesktopReorderMotion.Identity(3, 0).Retarget(0, 2, 0, snap: 0), 1_000_000); + Assert.Equal(2, drag.SlotOf(0, 3), 6); + Assert.Equal(0, drag.SlotOf(0, 4), 6); + Assert.Equal(3, drag.SlotOf(3, 4), 6); + } + + [Fact] + public void Neighbors_slide_out_of_the_way_instead_of_jumping() + { + var (c, _, t, bar) = OpenedBar(); + var thumb = bar.Thumbs[0]; + Assert.True(c.BeginDesktopDrag(0, thumb.CenterX, thumb.CenterY, thumb, t)); + c.DragDesktopTo(bar.Thumbs[2].CenterX + 1, thumb.CenterY + 20, bar, t + 10_000); + + var dragging = c.Sample(t + 20_000).DesktopDrag!.Value; + Assert.Equal(0, dragging.Source); + Assert.Equal(bar.Thumbs[2].CenterX + 1, dragging.X); + Assert.Equal(thumb.CenterY + 20, dragging.Y); + Assert.Equal(0.5, dragging.GrabX, 6); + Assert.Equal(-1, dragging.Fly); // 跟着指针 + var follow = DesktopBarReorder.Layout(bar, dragging, 0, 1); + Assert.Equal(bar.Thumbs[2].CenterX + 1, follow.Thumb.CenterX, 6); + Assert.Equal(follow.Thumb.Top + (bar.Labels[2].Top - bar.Thumbs[2].Top), follow.Label.Top, 6); // 桌面名紧跟在缩略图下面 + + // 让位途中:在新旧槽位之间 + var mid = c.Sample(t + 80_000).DesktopDrag!.Value; + Assert.InRange(mid.SlotOf(1, 3), 0.05, 0.95); + Assert.InRange(mid.SlotOf(2, 3), 1.05, 1.95); + // 落定:到新槽位 + var end = c.Sample(t + 2_000_000).DesktopDrag!.Value; + Assert.Equal(0, end.SlotOf(1, 3), 6); + Assert.Equal(1, end.SlotOf(2, 3), 6); + } + + [Fact] + public void Successful_drop_flies_into_the_slot_and_finishes_only_after_settling_once() + { + var (c, host, t, bar) = OpenedBar(); + var thumb = bar.Thumbs[0]; + Assert.True(c.BeginDesktopDrag(0, thumb.CenterX, thumb.CenterY, thumb, t)); + c.DragDesktopTo(bar.Thumbs[2].CenterX + 1, thumb.CenterY + 20, bar, t + 10_000); + c.Sample(t + 20_000); + + long drop = t + 30_000; + var move = c.DropDesktop(drop); + Assert.Equal(new DesktopBarMove(0, 2), move.GetValueOrDefault()); + Assert.Null(c.DropDesktop(drop)); // 重复松开不能再次调用系统排序 + Assert.False(c.CompleteDesktopMove(move!.Value, success: true, drop + 5_000)); // 系统很快返回,动画还在播 + + var flying = c.Sample(drop + 60_000); + Assert.InRange(flying.DesktopDrag!.Value.Fly, 0.01, 0.99); + var at = ThumbOf(flying, bar, 0); + Assert.InRange(at.Top, bar.Thumbs[2].Top + 0.5, bar.Thumbs[2].Top + 19.5); // 从松手处飞向槽位 + Assert.True(c.IsDesktopDragging); + Assert.DoesNotContain("DesktopReorderSettled", host.Calls); + + var settled = c.Sample(drop + 2_000_000); + Assert.Equal(bar.Thumbs[2], ThumbOf(settled, bar, 0)); + Assert.Single(host.Calls, x => x == "DesktopReorderSettled"); + Assert.NotNull(settled.DesktopDrag); // 新顺序要等宿主重建桌面条,不能自己结束 + c.Sample(drop + 2_010_000); + Assert.Single(host.Calls, x => x == "DesktopReorderSettled"); + + Assert.True(c.FinishSettledDesktopMove()); + Assert.False(c.FinishSettledDesktopMove()); + Assert.False(c.IsDesktopDragging); + Assert.Null(c.Sample(drop + 2_020_000).DesktopDrag); + } + + [Fact] + public void System_result_after_the_animation_settled_finishes_at_once() + { + var (c, host, t, bar) = OpenedBar(); + Assert.True(c.BeginDesktopDrag(0, bar.Thumbs[0].CenterX, bar.Thumbs[0].CenterY, bar.Thumbs[0], t)); + c.DragDesktopTo(bar.Thumbs[1].CenterX + 1, bar.Thumbs[0].CenterY, bar, t + 10_000); + var move = c.DropDesktop(t + 20_000)!.Value; + c.Sample(t + 2_000_000); // 等系统结果:不通知宿主 + Assert.DoesNotContain("DesktopReorderSettled", host.Calls); + Assert.True(c.CompleteDesktopMove(move, success: true, t + 2_000_000)); + Assert.False(c.IsDesktopDragging); + Assert.False(c.CompleteDesktopMove(move, success: true, t + 2_010_000)); // 过时的结果 + c.Sample(t + 2_020_000); + Assert.DoesNotContain("DesktopReorderSettled", host.Calls); + } + + [Theory] + [InlineData(false)] // Esc / 捕获丢失 + [InlineData(true)] // 放在桌面条外、或系统排序失败 + public void Cancelled_or_failed_drop_flies_back_and_ends_by_itself(bool dropped) + { + var (c, host, t, bar) = OpenedBar(); + var thumb = bar.Thumbs[1]; + Assert.True(c.BeginDesktopDrag(1, thumb.CenterX, thumb.CenterY, thumb, t)); + c.DragDesktopTo(bar.Thumbs[0].CenterX - 1, thumb.CenterY + 30, bar, t + 10_000); + c.Sample(t + 2_000_000); // 桌面 0 已经让到槽位 1 + long end = t + 2_000_000; + if (dropped) c.CompleteDesktopMove(c.DropDesktop(end)!.Value, success: false, end + 5_000); + else c.CancelDesktopDrag(end); + Assert.Null(c.DropDesktop(end)); + + var back = c.Sample(end + 60_000); + Assert.InRange(back.DesktopDrag!.Value.SlotOf(0, 3), 0.05, 0.95); // 弹回途中 + var at = ThumbOf(back, bar, 1); + Assert.InRange(at.Top, thumb.Top + 0.5, thumb.Top + 29.5); // 飞回途中 + + var done = c.Sample(end + 2_000_000); + Assert.Null(done.DesktopDrag); // 原顺序:自己结束,不用宿主重建 + Assert.False(c.IsDesktopDragging); + Assert.Single(host.Calls, x => x == "DesktopReorderSettled"); // 只让宿主补上拖动期间跳过的刷新 + Assert.False(c.FinishSettledDesktopMove()); + c.Sample(end + 2_010_000); + Assert.Single(host.Calls, x => x == "DesktopReorderSettled"); + } + + [Fact] + public void Desktop_drag_needs_an_expanded_bar() + { + var (c, _, t) = Opened(); + var bar = DesktopBarGeometry.Compute(Area, 3, 1.5); + Assert.False(c.BeginDesktopDrag(0, bar.Thumbs[0].CenterX, bar.Thumbs[0].CenterY, bar.Thumbs[0], t)); + Assert.Null(c.DropDesktop(t)); + Assert.Null(c.Sample(t + 20_000).DesktopDrag); + } + + [Fact] + public void A_press_skips_the_settle_animation_but_not_the_drag_or_the_system_call() + { + var (c, _, t, bar) = OpenedBar(); + Assert.True(c.SkipDesktopDragAnimation(out bool refresh)); // 没在拖:照常按下 + Assert.False(refresh); + + Assert.True(c.BeginDesktopDrag(0, bar.Thumbs[0].CenterX, bar.Thumbs[0].CenterY, bar.Thumbs[0], t)); + c.DragDesktopTo(bar.Thumbs[2].CenterX + 1, bar.Thumbs[0].CenterY, bar, t + 10_000); + Assert.False(c.SkipDesktopDragAnimation(out _)); // 拖着 + var move = c.DropDesktop(t + 20_000)!.Value; + Assert.False(c.SkipDesktopDragAnimation(out _)); // 等系统排序 + c.CompleteDesktopMove(move, success: true, t + 25_000); + Assert.True(c.SkipDesktopDragAnimation(out refresh)); // 排好了、动画还在播:跳到终点 + Assert.True(refresh); + Assert.False(c.IsDesktopDragging); + Assert.False(c.FinishSettledDesktopMove()); // 之后到的落定通知什么也不做 + + Assert.True(c.BeginDesktopDrag(1, bar.Thumbs[1].CenterX, bar.Thumbs[1].CenterY, bar.Thumbs[1], t + 100_000)); + c.CancelDesktopDrag(t + 110_000); + Assert.True(c.SkipDesktopDragAnimation(out refresh)); // 弹回原顺序途中:不用重建 + Assert.False(refresh); + Assert.Null(c.Sample(t + 120_000).DesktopDrag); + } + + [Fact] + public void Closing_the_overview_mid_animation_clears_the_drag_without_finishing_it() + { + var (c, host, t, bar) = OpenedBar(); + Assert.True(c.BeginDesktopDrag(0, bar.Thumbs[0].CenterX, bar.Thumbs[0].CenterY, bar.Thumbs[0], t)); + c.DragDesktopTo(bar.Thumbs[2].CenterX + 1, bar.Thumbs[0].CenterY, bar, t + 10_000); + var move = c.DropDesktop(t + 20_000)!.Value; + c.Close(null, t + 30_000); + for (long u = t + 36_000; u < t + 3_000_000; u += 6_000) c.Sample(u); + Assert.Contains("Hide:-", host.Calls); + Assert.False(c.IsDesktopDragging); + Assert.False(c.CompleteDesktopMove(move, success: true, t + 3_000_000)); // 撤层后才到的系统结果 + Assert.DoesNotContain("DesktopReorderSettled", host.Calls); + } + + /// 在展开好的桌面条上把桌面 0 拖到 2 松手,系统马上返回;返回落定动画播到一半的时刻。 + private static long DropAndSettleHalfway(OverviewController c, long t, DesktopBarGeometry bar, bool success) + { + Assert.True(c.BeginDesktopDrag(0, bar.Thumbs[0].CenterX, bar.Thumbs[0].CenterY, bar.Thumbs[0], t)); + c.DragDesktopTo(bar.Thumbs[2].CenterX + 1, bar.Thumbs[0].CenterY + 20, bar, t + 10_000); + var move = c.DropDesktop(t + 20_000)!.Value; + Assert.False(c.CompleteDesktopMove(move, success, t + 25_000)); + long mid = t + 120_000; + Assert.NotNull(c.Sample(mid).DesktopDrag); // 还在动 + return mid; + } + + private static GestureEvent Swipe3Down(GesturePhase phase, double downMm, long t) + => new(GestureKind.Swipe, phase, 3, GestureAxis.Vertical, GestureDirection.Down, 0, downMm, 0, downMm * 5, t, t); + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void A_gesture_mid_settle_skips_the_animation_instead_of_being_ignored(bool success) + { + var (c, host, t, bar) = OpenedBar(); + long u = DropAndSettleHalfway(c, t, bar, success); + c.Begin(Swipe3Down(GesturePhase.Began, 0, u)); + Assert.Single(host.Calls, x => x == "DesktopReorderSettled"); // 宿主收尾(排好了的按新顺序重建,没排成的补上跳过的刷新) + + var frame = c.Sample(u + 1_000); + if (success) + { + // 停在新顺序上、不闪回旧顺序,等宿主重建桌面条 + Assert.NotNull(frame.DesktopDrag); + for (int i = 0; i < 3; i++) + Assert.True(Distance(ThumbOf(frame, bar, i), bar.ThumbRect(DesktopBarReorder.Slot(i, 0, 2, 3), frame.BarExpansion)) < 0.5, $"桌面 {i} 没到终点"); + Assert.True(c.FinishSettledDesktopMove()); + } + else Assert.Null(frame.DesktopDrag); // 弹回原顺序:直接结束 + Assert.False(c.IsDesktopDragging); + + c.Update(Swipe3Down(GesturePhase.Changed, 15, u + 50_000)); + Assert.True(c.Sample(u + 60_000).Progress < 0.9); // 这次手势照常跟手 + c.Sample(u + 3_000_000); + Assert.Single(host.Calls, x => x == "DesktopReorderSettled"); // 不会再发一次 + } + + [Fact] + public void The_rebuild_can_land_while_the_gesture_is_already_tracking() + { + var (c, host, t, pages) = OpenedWithPages(); + c.SetBarHovered(true, t); + t += 2_000_000; + c.Sample(t); + var bar = DesktopBarGeometry.Compute(Area, 3, 1.5); + long u = DropAndSettleHalfway(c, t, bar, success: true); + + c.Begin(Swipe3Down(GesturePhase.Began, 0, u)); + c.Update(Swipe3Down(GesturePhase.Changed, 5, u + 20_000)); + double before = c.Sample(u + 30_000).Progress; + Assert.True(before < 1); + + // 宿主的收尾投递到 UI 线程,晚于手势开始才到:按新顺序重建(当前页的场景不变) + Assert.True(c.FinishSettledDesktopMove()); + c.SetDesktops(new DesktopBarState(["桌面 2", "桌面 3", "桌面 1"], 2)); + c.SetPages([pages[1], pages[2], pages[0]], 2); + + c.Update(Swipe3Down(GesturePhase.Changed, 15, u + 50_000)); + var frame = c.Sample(u + 60_000); + Assert.True(frame.Progress < before); // 照常跟手 + Assert.Null(frame.DesktopDrag); + c.End(Swipe3Down(GesturePhase.Ended, 40, u + 70_000)); + c.Sample(u + 3_000_000); + Assert.Contains(host.Calls, x => x.StartsWith("Closing", StringComparison.Ordinal)); + Assert.Single(host.Calls, x => x == "DesktopReorderSettled"); + } + + [Fact] + public void Gestures_still_wait_while_dragging_or_waiting_for_the_system() + { + var (c, host, t, bar) = OpenedBar(); + Assert.True(c.BeginDesktopDrag(0, bar.Thumbs[0].CenterX, bar.Thumbs[0].CenterY, bar.Thumbs[0], t)); + c.DragDesktopTo(bar.Thumbs[2].CenterX + 1, bar.Thumbs[0].CenterY, bar, t + 10_000); + for (int k = 0; k < 2; k++) + { + if (k == 1) c.DropDesktop(t + 20_000); // 松手了、系统排序还没返回 + long u = t + 30_000 + k * 200_000; + c.Begin(Swipe3Down(GesturePhase.Began, 0, u)); + c.Update(Swipe3Down(GesturePhase.Changed, 15, u + 50_000)); + c.End(Swipe3Down(GesturePhase.Ended, 15, u + 60_000)); + Assert.Equal(1, c.Sample(u + 70_000).Progress, 3); + Assert.True(c.IsDesktopDragging); + } + Assert.DoesNotContain(host.Calls, x => x.StartsWith("Closing", StringComparison.Ordinal)); + Assert.DoesNotContain("DesktopReorderSettled", host.Calls); + } + + [Theory] + [InlineData(true, false)] + [InlineData(true, true)] + [InlineData(false, false)] + [InlineData(false, true)] + public void Paging_mid_settle_waits_only_for_the_new_order(bool success, bool swipe) + { + var (c, host, t, pages) = OpenedWithPages(); + c.SetBarHovered(true, t); + t += 2_000_000; + c.Sample(t); + var bar = DesktopBarGeometry.Compute(Area, 3, 1.5); + long u = DropAndSettleHalfway(c, t, bar, success); + + // Ctrl+Win+←/→ 或四指横滑一次;返回是否翻了页 + bool Page(int direction, long at) + { + if (!swipe) return c.StepPage(direction, at); + int before = host.Calls.Count(x => x.StartsWith("PageChanged", StringComparison.Ordinal)); + var dir = direction > 0 ? GestureDirection.Left : GestureDirection.Right; + double dx = direction > 0 ? -40 : 40; + c.DesktopSwipe.Begin(Swipe4(GesturePhase.Began, 0, 0, at, dir)); + c.DesktopSwipe.Update(Swipe4(GesturePhase.Changed, dx, dx * 5, at + 10_000, dir)); + c.DesktopSwipe.End(Swipe4(GesturePhase.Ended, dx, dx * 5, at + 20_000, dir)); + return host.Calls.Count(x => x.StartsWith("PageChanged", StringComparison.Ordinal)) > before; + } + + if (success) + { + // 系统已按新顺序排好、各页还是旧顺序:这一下不翻,动画跳到终点,等宿主按新顺序重建 + Assert.False(Page(1, u)); + Assert.Single(host.Calls, x => x == "DesktopReorderSettled"); + Assert.NotNull(c.Sample(u + 30_000).DesktopDrag); + Assert.True(c.FinishSettledDesktopMove()); + c.SetDesktops(new DesktopBarState(["桌面 2", "桌面 3", "桌面 1"], 2)); // 宿主重建:原第 0 个桌面(当前)现在是第 2 个 + c.SetPages([pages[1], pages[2], pages[0]], 2); + Assert.True(Page(-1, u + 40_000)); + } + else + { + Assert.True(Page(1, u)); // 弹回原顺序:直接结束,照常翻页 + Assert.Single(host.Calls, x => x == "DesktopReorderSettled"); + } + Assert.Equal("PageChanged:1", host.Calls[^1]); + Assert.False(c.IsDesktopDragging); + } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void Every_frame_moves_a_little_from_pick_up_to_the_final_order(bool success) + { + var (c, host, t, bar) = OpenedBar(); + double pitch = bar.Thumbs[1].Left - bar.Thumbs[0].Left; + var thumb = bar.Thumbs[0]; + long u = t; + RectD[]? last = null; + int frames = 0; + + void Step(bool draggedFollows) + { + var frame = c.Sample(u); + var rects = Enumerable.Range(0, 3).Select(i => ThumbOf(frame, bar, i)).ToArray(); + if (last is not null) + for (int i = 0; i < 3; i++) + if (!(draggedFollows && i == 0)) // 跟着指针的那个由指针决定 + Assert.True(Distance(last[i], rects[i]) < 0.2 * pitch, $"第 {frames} 帧桌面 {i} 跳了 {Distance(last[i], rects[i]):F1}px"); + last = rects; + frames++; + u += 6_000; + } + + Assert.True(c.BeginDesktopDrag(0, thumb.CenterX, thumb.CenterY, thumb, u)); + // 指针往右拖过两个槽位,中途折回一个(让位动画中途改道) + for (int k = 0; k <= 40; k++) + { + double x = thumb.CenterX + (k <= 30 ? k / 30.0 * 2.2 * pitch : (2.2 - (k - 30) / 10.0 * 1.0) * pitch); + c.DragDesktopTo(x, thumb.CenterY + 15, bar, u); + Step(draggedFollows: true); + } + var move = c.DropDesktop(u)!.Value; + Assert.Equal(new DesktopBarMove(0, 1), move); + Step(draggedFollows: false); // 松手那一帧不能跳 + for (int k = 0; k < 3; k++) Step(draggedFollows: false); + Assert.False(c.CompleteDesktopMove(move, success, u)); + while (c.IsDesktopDragging && frames < 400) + { + Step(draggedFollows: false); + if (host.Calls.Contains("DesktopReorderSettled") && c.FinishSettledDesktopMove()) break; + } + Assert.False(c.IsDesktopDragging); + + // 结束预览后的第一帧(成功:宿主已按新顺序重建,原第 i 个桌面现在是第 Slot(i) 个)与最后一帧预览一致 + var after = c.Sample(u); + Assert.Null(after.DesktopDrag); + for (int i = 0; i < 3; i++) + { + int now = success ? DesktopBarReorder.Slot(i, move.From, move.To, 3) : i; + Assert.True(Distance(last![i], bar.ThumbRect(now, after.BarExpansion)) < 0.5, $"桌面 {i} 结束时跳了"); + } + } + + [Fact] + public void Desktop_drag_keeps_the_current_page_and_blocks_paging_clicks_and_swipes() + { + var (c, host, t, pages) = OpenedWithPages(); + c.SetBarHovered(true, t); + t += 2_000_000; + c.Sample(t); + var bar = DesktopBarGeometry.Compute(Area, 3, 1.5); + Assert.True(c.BeginDesktopDrag(0, bar.Thumbs[0].CenterX, bar.Thumbs[0].CenterY, bar.Thumbs[0], t)); + c.DragDesktopTo(bar.Thumbs[2].CenterX, bar.Thumbs[0].CenterY, bar, t + 10_000); + Assert.Same(pages[0], c.Scene); + Assert.Equal(0, c.Desktops.Current); + Assert.False(c.StepPage(1, t + 20_000)); + c.GoToDesktop(2, t + 20_000); + Assert.DoesNotContain(host.Calls, x => x.StartsWith("SwitchDesktop", StringComparison.Ordinal)); + c.DesktopSwipe.Begin(Swipe4(GesturePhase.Began, 0, 0, t + 20_000)); + c.DesktopSwipe.Update(Swipe4(GesturePhase.Changed, -40, -200, t + 30_000)); + c.DesktopSwipe.End(Swipe4(GesturePhase.Ended, -40, -200, t + 40_000)); + Assert.Equal(0, c.Sample(t + 50_000).PagePosition, 6); + Assert.DoesNotContain(host.Calls, x => x.StartsWith("PageChanged", StringComparison.Ordinal)); + } + + [Fact] + public void MoveDesktop_uses_the_checked_vtable_slot_for_each_supported_layout() + { + Assert.Equal(12, global::GlideShell.Platform.VirtualDesktops.VirtualDesktopLayout.Win11_24H2.MoveDesktop); + Assert.Equal(11, global::GlideShell.Platform.VirtualDesktops.VirtualDesktopLayout.Win11_23H2.MoveDesktop); + Assert.Equal(10, global::GlideShell.Platform.VirtualDesktops.VirtualDesktopLayout.Win11_23H2.CreateDesktop); + Assert.Equal(12, global::GlideShell.Platform.VirtualDesktops.VirtualDesktopLayout.Win11_23H2.RemoveDesktop); + } + [Fact] public void Windows_fade_out_while_zooming_into_another_desktop() { diff --git a/tests/GlideShell.Tests/Overview/OverviewControllerTests.cs b/tests/GlideShell.Tests/Overview/OverviewControllerTests.cs index ec9b4a8..9e6f862 100644 --- a/tests/GlideShell.Tests/Overview/OverviewControllerTests.cs +++ b/tests/GlideShell.Tests/Overview/OverviewControllerTests.cs @@ -19,6 +19,7 @@ private sealed class FakeHost : IOverviewHost public void Hide(long? focusWindowId) => Calls.Add($"Hide:{focusWindowId?.ToString() ?? "-"}"); public void SwitchDesktop(int desktopIndex) => Calls.Add($"SwitchDesktop:{desktopIndex}"); public void PageChanged(int desktopIndex) => Calls.Add($"PageChanged:{desktopIndex}"); + public void DesktopReorderSettled() => Calls.Add("DesktopReorderSettled"); } private const double Travel = 30; diff --git a/tests/GlideShell.Tests/Overview/ShowDesktopTests.cs b/tests/GlideShell.Tests/Overview/ShowDesktopTests.cs index f506e1e..9060522 100644 --- a/tests/GlideShell.Tests/Overview/ShowDesktopTests.cs +++ b/tests/GlideShell.Tests/Overview/ShowDesktopTests.cs @@ -22,6 +22,7 @@ private sealed class FakeHost : IOverviewHost public void Hide(long? focusWindowId) => Calls.Add($"Hide:{focusWindowId?.ToString() ?? "-"}"); public void SwitchDesktop(int desktopIndex) => Calls.Add($"SwitchDesktop:{desktopIndex}"); public void PageChanged(int desktopIndex) => Calls.Add($"PageChanged:{desktopIndex}"); + public void DesktopReorderSettled() => Calls.Add("DesktopReorderSettled"); } private static GestureEvent Ev(GesturePhase phase, GestureDirection dir, double downMm, double downVelocity, long t) diff --git a/tests/GlideShell.Tests/Overview/WindowDragTests.cs b/tests/GlideShell.Tests/Overview/WindowDragTests.cs index 14d875c..aad5b93 100644 --- a/tests/GlideShell.Tests/Overview/WindowDragTests.cs +++ b/tests/GlideShell.Tests/Overview/WindowDragTests.cs @@ -22,6 +22,7 @@ public void Show() { } public void Hide(long? focusWindowId) => Calls.Add($"Hide:{focusWindowId?.ToString() ?? "-"}"); public void SwitchDesktop(int desktopIndex) => Calls.Add($"SwitchDesktop:{desktopIndex}"); public void PageChanged(int desktopIndex) => Calls.Add($"PageChanged:{desktopIndex}"); + public void DesktopReorderSettled() => Calls.Add("DesktopReorderSettled"); } private static OverviewScene ThreeWindows() => OverviewScene.Build( diff --git a/tests/GlideShell.Tests/Performance/HotPathAllocationTests.cs b/tests/GlideShell.Tests/Performance/HotPathAllocationTests.cs index 2ed1afb..acfeaca 100644 --- a/tests/GlideShell.Tests/Performance/HotPathAllocationTests.cs +++ b/tests/GlideShell.Tests/Performance/HotPathAllocationTests.cs @@ -12,6 +12,7 @@ namespace GlideShell.Tests.Performance; /// 组帧 → 识别 → 路由 → 控制器,以及渲染线程每帧的取样与窗口矩形,都不得分配托管对象。 /// 手势开始 / 结束时的少量分配(预热、选目标)不在约束内,所以只测中段的 Changed 报文。 /// +[Collection(nameof(HotPathAllocationTests))] // 计数按线程,但并行跑别的测试时偶尔会多出几 KB:单独跑,免得误报 public class HotPathAllocationTests { private sealed class NoopOverviewHost : IOverviewHost @@ -24,6 +25,7 @@ public void Closing(long? focusWindowId) { } public void Hide(long? focusWindowId) { } public void SwitchDesktop(int desktopIndex) { } public void PageChanged(int desktopIndex) { } + public void DesktopReorderSettled() { } } private sealed class NoopSwitcherHost : ISwitcherHost @@ -129,4 +131,59 @@ void Frame(long us) Assert.Equal(0, allocated); } + + [Fact] + public void Render_sampling_does_not_allocate_while_reordering_desktops() + { + var overviewConfig = new OverviewConfig(); + var overview = new OverviewController(new NoopOverviewHost(), () => overviewConfig); + var area = new RectD(0, 0, 2560, 1528); + var bar = DesktopBarGeometry.Compute(area, 5, 1.5); + overview.SetDesktops(new DesktopBarState(["1", "2", "3", "4", "5"], 0)); + overview.Open(0); + overview.OnShown(0); + overview.SetBarHovered(true, 0); + overview.Sample(2_000_000); + double sum = 0; + + void Frame(long us) + { + var frame = overview.Sample(us); + if (frame.DesktopDrag is not { } drag) return; + for (int i = 0; i < bar.Count; i++) + { + var (thumb, pill, label) = DesktopBarReorder.Layout(bar, drag, i, frame.BarExpansion); + sum += thumb.Left + pill.Left + label.Left; + } + } + + long t = 2_000_000; + void Session(bool measure, ref long result) + { + Assert.True(overview.BeginDesktopDrag(0, bar.Thumbs[0].CenterX, bar.Thumbs[0].CenterY, bar.Thumbs[0], t)); + overview.DragDesktopTo(bar.Thumbs[3].CenterX + 1, bar.Thumbs[0].CenterY, bar, t); // UI 线程(换目标时分配新的动画状态) + long before = GC.GetAllocatedBytesForCurrentThread(); + for (int i = 1; i < 60; i++) Frame(t + i * 6_000); // 让位途中 + long during = GC.GetAllocatedBytesForCurrentThread() - before; + t += 400_000; + var move = overview.DropDesktop(t)!.Value; + overview.CompleteDesktopMove(move, success: true, t); + before = GC.GetAllocatedBytesForCurrentThread(); + for (int i = 1; i < 150; i++) Frame(t + i * 6_000); // 飞到槽位、落定、通知宿主 + during += GC.GetAllocatedBytesForCurrentThread() - before; + t += 1_000_000; + Assert.True(overview.FinishSettledDesktopMove()); + if (measure) result = during; + } + + long allocated = 0; + Session(measure: false, ref allocated); // JIT + Session(measure: true, ref allocated); + + Assert.Equal(0, allocated); + Assert.True(sum != 0); + } } + +[CollectionDefinition(nameof(HotPathAllocationTests), DisableParallelization = true)] +public sealed class HotPathAllocationCollection;