acdream/src/AcDream.App/UI/RetailWindowHandle.cs
Erik 4fada238e6 fix(plugin-ui): Slice A review fixes - real grip/toggle children, dialog z-order, persisted collapse/hide intent
Two independent Opus reviews of 01b98ca30 (Slice A - movable, collapsible
plugin shelf) found blocking design and behavior issues plus several
should-fix gaps. All addressed in this commit:

1. Grip/toggle are now REAL children instead of a drawn band + a
   cursor-position-dependent HandlesClick override + an OnEvent toggle
   hack: ShelfGripPanel (WindowMoveHandle=true) spans the top band minus
   the toggle width; a UiSimpleButton toggle sits beside it (HandlesClick
   already wins at UiRoot.OnMouseDown before the Draggable-window
   fallback). The shelf's own Draggable is now FALSE - verified against
   UiRoot.FindDragHandleWindow, which never reads a window's own
   Draggable flag at all (it walks for a WindowMoveHandle ancestor-or-self
   then climbs to the nearest child of UiRoot), so Draggable=true was
   never required for the grip to work and only armed the whole-window-
   drag fallback for clicks on the shelf's own padding - exactly the
   behavior the review asked NOT to have. The two Assert.Single(shelf.
   Children) test sites now filter by the (now internal) PluginShelfButton
   type instead of asserting child count.

2. Deleted the per-tick "always highest ZOrder" raise in OnTick. It fought
   RetailDialogFactory.Tick's own per-frame dialog re-raise, so a dialog
   opened while the shelf was visible could never end up on top of it.
   Registration's ordinary press-to-raise (a grip press calls
   BringToFront before the drag starts) remains.

3. Collapse and hide/show intent are now persisted through their own
   channel. RetainedWindowState gained a nullable RequestedVisible;
   RetailWindowHandle gained an internal StateChanged event that
   RetailWindowLayoutPersistence subscribes to (alongside Moved/Resized/
   Shown/Hidden) and that the shelf raises after a collapse toggle or
   Show/Hide. Capture() now persists state.RequestedVisible (the panel's
   own intent) instead of the derived IsVisible, so an availability hide
   (last plugin window unregistered) is never mistaken for a user hide.
   WindowNames.PluginShelf is now one of RetailUiRuntime's state-managed
   visibility windows, so Apply() restores the intent through
   RestoreWindowState directly rather than via Show()/Hide().

4. RetailWindowLayoutPersistence now subscribes to
   RetailWindowManager.WindowRegistered/WindowUnregistered so a window
   (a plugin window loaded after startup, or the shelf on any path that
   constructs persistence first) attaches even when it registers after
   persistence already exists.

5. Reflow()'s default height argument is now nullable and falls back to
   the last height OnTick actually measured (or unbounded if none yet),
   instead of always forcing a single-column layout - every call site
   OTHER than OnTick's own row-wrap (Add, unregister, Show/Hide, the
   collapse toggle, RestoreWindowState) used to collapse a wrapped
   multi-column layout to one column for a frame.

6. _userPositioned is now flipped only when the handle's position differs
   from the recorded dock placement, not on every RetailWindowHandle.
   Moved (which fires unconditionally on any completed window-drag
   release, including a zero-movement grip click, and on any
   ClampAllToScreen reachable-clamp).

7. New tests cover: a press+drag starting on an entry button does not
   move the shelf; the removed per-tick raise (a sibling with higher
   ZOrder keeps it after a tick); TogglePluginManager's hidden-and-
   collapsed -> shown-and-expanded / visible -> hidden transitions at the
   shelf API (no RetailUiRuntime construction harness exists in this test
   suite to exercise the action-routing switch itself - the "no plugin
   windows registered" message branch is therefore not covered here).

8. The hide branch of TogglePluginManager now displays "Plugin shelf
   hidden. Press Shift+Ctrl+F1 to show it again."; the show branch stays
   silent.

9. The collapse toggle now draws ASCII '<'/'>' instead of the DAT-font-
   dependent '«'/'»' glyphs (the only use of those code points in the App
   UI, silently dropped by UiDatFont when absent), and gets the same
   DatFont + bitmap fallback the shelf's entry buttons already have
   through UiSimpleButton. A new installed-DAT test pins that the default
   font actually carries both ASCII glyphs.

10. ResizeX/ResizeY are false on the shelf so a restored layout's saved
    dimensions can never stomp the derived Width/Height via ResizeTo.

11. WindowNames.PluginShelf replaces the "plugin-shelf" literal at every
    site (RetailUiRuntime, docs comments, tests).

12. The grip dims to half opacity while RetailWindowManager.IsLocked, the
    same visual cue every other retail window gets (the shelf's grip has
    no DatElementId, so RetailWindowLockPresentationController's authored-
    chrome dimming does not reach it on its own).

Every new test was verified to fail against the pre-fix source: reverting
src/AcDream.App/UI/PluginSidePanel.cs, IRetainedWindowStateController.cs,
RetailWindowHandle.cs, WindowNames.cs, and RetailUiRuntime.cs to their
01b98ca30 state makes the whole PluginSidePanelTests.cs file fail to even
compile (missing WindowNames.PluginShelf, the now-internal
PluginShelfButton type, and RetainedWindowState.RequestedVisible);
reverting RetailWindowLayoutPersistence.cs alone (fixed source elsewhere)
makes WindowRegisteredAfterConstruction_StillRoundTrips fail at runtime
with a null saved layout, confirming finding 4 in isolation.

Verified: dotnet build src/AcDream.App (Release) green; dotnet test
tests/AcDream.App.Tests (Release) targeted filter
(PluginSidePanel|RetailWindowLayout|Markup|UiRootInput) 105/105 green;
full suite 7303 passed / 97 skipped / 36 failed (identical failure set to
the pre-fix-round baseline - installed-DAT live-mount probes, Linux-only
pacing/credential tests, and alpha-flush COUNT-only conformance
divergences, none touching plugin UI; +9 tests, all passing, over the
prior 7294/97/36 baseline).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-06 13:51:25 +02:00

153 lines
5.6 KiB
C#

using System;
namespace AcDream.App.UI;
/// <summary>
/// Typed ownership handle for one named retained window. Geometry and opacity
/// mutations flow through this handle so lifecycle observers see the same state
/// regardless of whether a change came from input, restore, or controller logic.
/// </summary>
public sealed class RetailWindowHandle
{
private readonly RetailWindowManager _owner;
private bool _notifiedVisible;
private bool _closedSinceShown;
private bool _disposed;
internal RetailWindowHandle(
RetailWindowManager owner,
string name,
UiElement outerFrame,
UiElement contentRoot,
IRetainedPanelController? controller,
IRetainedWindowStateController? stateController,
int authoredGeometryRevision)
{
_owner = owner;
Name = name;
OuterFrame = outerFrame;
ContentRoot = contentRoot;
Controller = controller;
StateController = stateController;
AuthoredGeometryRevision = Math.Max(0, authoredGeometryRevision);
AuthoredWidth = outerFrame.Width;
AuthoredHeight = outerFrame.Height;
_notifiedVisible = outerFrame.Visible;
}
public string Name { get; }
public UiElement OuterFrame { get; }
public UiElement ContentRoot { get; }
public IRetainedPanelController? Controller { get; private set; }
public IRetainedWindowStateController? StateController { get; }
public int AuthoredGeometryRevision { get; }
public float AuthoredWidth { get; }
public float AuthoredHeight { get; }
public bool IsRegistered => !_disposed;
public bool IsVisible => OuterFrame.Visible;
public bool IsLocked => _owner.IsLocked;
public float Left => OuterFrame.Left;
public float Top => OuterFrame.Top;
public float Width => OuterFrame.Width;
public float Height => OuterFrame.Height;
public float Opacity => OuterFrame.Opacity;
public event Action<RetailWindowHandle>? Shown;
public event Action<RetailWindowHandle>? Hidden;
public event Action<RetailWindowHandle>? Moved;
public event Action<RetailWindowHandle>? Resized;
public event Action<RetailWindowHandle>? Closed;
/// <summary>
/// Review fix round (finding 3, docs/plans/2026-09-06-plugin-shelf-and-dat-icons.md
/// Slice A): raised by a controller-owned state change that Moved/Resized/
/// Shown/Hidden do not cover — e.g. <c>PluginSidePanel</c>'s collapse toggle
/// or its own <see cref="Show"/>/<see cref="Hide"/>, which route through
/// <see cref="IRetainedWindowStateController"/> instead of the outer frame's
/// <c>Visible</c> property. <see cref="RetailWindowLayoutPersistence"/>
/// subscribes here (alongside Moved/Resized/Shown/Hidden) to trigger a save.
/// Internal: no consumer outside this assembly needs it today.
/// </summary>
internal event Action<RetailWindowHandle>? StateChanged;
public event Action<RetailWindowHandle, bool>? LockChanged;
public event Action<RetailWindowHandle, UiElement?>? DescendantFocusChanged;
public event Action<RetailWindowHandle, UiElement?>? DescendantCaptureChanged;
public bool Show() => IsRegistered && _owner.Show(Name);
public bool Hide() => IsRegistered && _owner.Hide(Name);
public bool Close() => IsRegistered && _owner.Close(Name);
public bool MoveTo(float left, float top)
=> IsRegistered && _owner.MoveTo(Name, left, top);
public bool ResizeTo(float width, float height)
=> IsRegistered && _owner.ResizeTo(Name, width, height);
public bool SetOpacity(float opacity)
=> IsRegistered && _owner.SetOpacity(Name, opacity);
internal void NotifyInitialState()
{
if (_notifiedVisible)
Controller?.OnShown();
}
internal void AttachController(IRetainedPanelController controller)
{
ObjectDisposedException.ThrowIf(_disposed, this);
ArgumentNullException.ThrowIfNull(controller);
if (ReferenceEquals(Controller, controller)) return;
if (Controller is not null)
throw new InvalidOperationException($"Retained window '{Name}' already owns a controller.");
Controller = controller;
if (_notifiedVisible)
Controller.OnShown();
}
internal void NotifyVisibility(bool visible)
{
if (_notifiedVisible == visible) return;
_notifiedVisible = visible;
if (visible)
{
_closedSinceShown = false;
Controller?.OnShown();
Shown?.Invoke(this);
}
else
{
Controller?.OnHidden();
Hidden?.Invoke(this);
}
}
internal void NotifyMoved() => Moved?.Invoke(this);
internal void NotifyResized() => Resized?.Invoke(this);
/// <summary>Raises <see cref="StateChanged"/>. See that event's own doc.</summary>
internal void NotifyStateChanged() => StateChanged?.Invoke(this);
internal void NotifyClosed()
{
if (_closedSinceShown) return;
_closedSinceShown = true;
Closed?.Invoke(this);
}
internal void NotifyLockChanged(bool locked) => LockChanged?.Invoke(this, locked);
internal void NotifyDescendantFocusChanged(UiElement? focusedDescendant)
{
Controller?.OnDescendantFocusChanged(focusedDescendant);
DescendantFocusChanged?.Invoke(this, focusedDescendant);
}
internal void NotifyDescendantCaptureChanged(UiElement? capturedDescendant)
=> DescendantCaptureChanged?.Invoke(this, capturedDescendant);
internal void DisposeController()
{
if (_disposed) return;
_disposed = true;
Controller?.Dispose();
}
}