cfd9f22f4b
Install directional hit regions in the shared window layer so floating chats can be moved and resized on every destination. Keep geometry updates in the shared manager and cover directional resizing with window host regressions.
220 lines
6.6 KiB
Dart
220 lines
6.6 KiB
Dart
import 'dart:ui';
|
|
|
|
/// Margin a maximised window keeps from the edges of the window area.
|
|
const double windowMaximizedMargin = 8;
|
|
|
|
/// One floating window: what it shows and where. Immutable; the
|
|
/// `WindowManager` replaces an entry to change it.
|
|
class WindowEntry {
|
|
const WindowEntry({
|
|
required this.id,
|
|
required this.kind,
|
|
required this.contentId,
|
|
required this.frame,
|
|
this.serverKey,
|
|
String? groupId,
|
|
this.minimized = false,
|
|
this.maximized = false,
|
|
this.data = const {},
|
|
}) : groupId = groupId ?? kind;
|
|
|
|
/// Unique among all windows on this device.
|
|
final String id;
|
|
|
|
/// The registered `WindowKind.key` that builds this window.
|
|
final String kind;
|
|
|
|
/// What the kind shows: a chat session id, a run id, `terminal`.
|
|
final String contentId;
|
|
|
|
/// The server this window belongs to, or null for a device-wide window
|
|
/// (the terminal).
|
|
final String? serverKey;
|
|
|
|
/// Windows shown and hidden together (by a chord); the kind by default.
|
|
final String groupId;
|
|
|
|
/// The saved frame; it is drawn fitted inside the window area.
|
|
final Rect frame;
|
|
|
|
/// Hidden by the title bar's X; still listed and shown again with its
|
|
/// group.
|
|
final bool minimized;
|
|
final bool maximized;
|
|
|
|
/// The kind's own JSON state (see `WindowKind.decodeData`).
|
|
final Map<String, Object?> data;
|
|
|
|
WindowEntry copyWith({
|
|
String? contentId,
|
|
Rect? frame,
|
|
bool? minimized,
|
|
bool? maximized,
|
|
Map<String, Object?>? data,
|
|
}) => WindowEntry(
|
|
id: id,
|
|
kind: kind,
|
|
contentId: contentId ?? this.contentId,
|
|
serverKey: serverKey,
|
|
groupId: groupId,
|
|
frame: frame ?? this.frame,
|
|
minimized: minimized ?? this.minimized,
|
|
maximized: maximized ?? this.maximized,
|
|
data: data ?? this.data,
|
|
);
|
|
|
|
@override
|
|
String toString() =>
|
|
'WindowEntry($id, $kind:$contentId, server: $serverKey, $frame'
|
|
'${minimized ? ', minimized' : ''}${maximized ? ', maximized' : ''})';
|
|
}
|
|
|
|
/// The key a window's frame is remembered under after it closes, so the same
|
|
/// content re-opens where it was.
|
|
String windowFrameKey(String kind, String? serverKey, String contentId) =>
|
|
'$kind\n${serverKey ?? ''}\n$contentId';
|
|
|
|
/// [rect] moved and shrunk to fit inside [bounds], at least [minSize] where
|
|
/// the bounds allow it, on whole logical pixels.
|
|
///
|
|
/// Whole pixels keep a moved window exactly its size from frame to frame: a
|
|
/// `Rect` stores left, top, right and bottom, so shifting a frame by a
|
|
/// fractional delta makes its width differ in its last bits, and every widget
|
|
/// that reads the window's size (its `MediaQuery`) rebuilds and lays out
|
|
/// again on every pointer event.
|
|
Rect fitWindowFrame(Rect rect, Size bounds, Size minSize) {
|
|
final width = _wholePixel(
|
|
rect.width.clamp(minSize.width, double.infinity).clamp(0.0, bounds.width),
|
|
minSize.width,
|
|
bounds.width,
|
|
);
|
|
final height = _wholePixel(
|
|
rect.height
|
|
.clamp(minSize.height, double.infinity)
|
|
.clamp(0.0, bounds.height),
|
|
minSize.height,
|
|
bounds.height,
|
|
);
|
|
return Rect.fromLTWH(
|
|
_wholePixel(
|
|
rect.left.clamp(0.0, bounds.width - width),
|
|
0,
|
|
bounds.width - width,
|
|
),
|
|
_wholePixel(
|
|
rect.top.clamp(0.0, bounds.height - height),
|
|
0,
|
|
bounds.height - height,
|
|
),
|
|
width,
|
|
height,
|
|
);
|
|
}
|
|
|
|
enum WindowResizeEdge { left, right, top, bottom }
|
|
|
|
/// Resizes while keeping the opposite edge anchored, then applies the same
|
|
/// bounds and minimum-size rules as a moved window.
|
|
Rect resizeWindowFrame(
|
|
Rect start,
|
|
Offset delta, {
|
|
required bool left,
|
|
required bool right,
|
|
required bool top,
|
|
required bool bottom,
|
|
required Size bounds,
|
|
required Size minSize,
|
|
}) {
|
|
var result = Rect.fromLTRB(
|
|
left ? start.left + delta.dx : start.left,
|
|
top ? start.top + delta.dy : start.top,
|
|
right ? start.right + delta.dx : start.right,
|
|
bottom ? start.bottom + delta.dy : start.bottom,
|
|
);
|
|
if (result.width < minSize.width) {
|
|
result = left
|
|
? Rect.fromLTRB(
|
|
start.right - minSize.width,
|
|
result.top,
|
|
start.right,
|
|
result.bottom,
|
|
)
|
|
: Rect.fromLTRB(
|
|
start.left,
|
|
result.top,
|
|
start.left + minSize.width,
|
|
result.bottom,
|
|
);
|
|
}
|
|
if (result.height < minSize.height) {
|
|
result = top
|
|
? Rect.fromLTRB(
|
|
result.left,
|
|
start.bottom - minSize.height,
|
|
result.right,
|
|
start.bottom,
|
|
)
|
|
: Rect.fromLTRB(
|
|
result.left,
|
|
start.top,
|
|
result.right,
|
|
start.top + minSize.height,
|
|
);
|
|
}
|
|
return fitWindowFrame(result, bounds, minSize);
|
|
}
|
|
|
|
/// [value] rounded to a whole logical pixel and kept within
|
|
/// `[minimum, limit]`.
|
|
///
|
|
/// A fractional [minimum] rounds up, so the whole-pixel size never drops below
|
|
/// the minimum the caller asked for; when the limit is too small to hold that,
|
|
/// the limit wins.
|
|
double _wholePixel(double value, double minimum, double limit) {
|
|
final max = limit.floorToDouble();
|
|
final min = minimum.ceilToDouble().clamp(0.0, max);
|
|
return value.roundToDouble().clamp(min, max);
|
|
}
|
|
|
|
/// Where [entry] is drawn inside [bounds]: fitted, or filling the area less
|
|
/// a margin when maximised.
|
|
Rect drawnWindowFrame(WindowEntry entry, Size bounds, Size minSize) =>
|
|
entry.maximized
|
|
? (Offset.zero & bounds).deflate(
|
|
windowMaximizedMargin.clamp(0.0, bounds.shortestSide / 2),
|
|
)
|
|
: fitWindowFrame(entry.frame, bounds, minSize);
|
|
|
|
/// Where a dragged window snaps when its title is dropped at an edge.
|
|
enum WindowSnap { left, right, maximize }
|
|
|
|
/// How close to an edge of the window area the pointer must be to snap.
|
|
const double windowSnapDistance = 6;
|
|
|
|
/// The snap for a title dropped with the pointer at [pointer] inside
|
|
/// [bounds], or null away from the edges.
|
|
WindowSnap? windowSnapFor(Offset pointer, Size bounds) {
|
|
if (pointer.dy <= windowSnapDistance) return WindowSnap.maximize;
|
|
if (pointer.dx <= windowSnapDistance) return WindowSnap.left;
|
|
if (pointer.dx >= bounds.width - windowSnapDistance) return WindowSnap.right;
|
|
return null;
|
|
}
|
|
|
|
/// The frame of a window snapped to a half of [bounds].
|
|
Rect windowSnapRect(WindowSnap snap, Size bounds) {
|
|
final area = (Offset.zero & bounds).deflate(
|
|
windowMaximizedMargin.clamp(0.0, bounds.shortestSide / 2),
|
|
);
|
|
final half = (area.width - windowMaximizedMargin) / 2;
|
|
return switch (snap) {
|
|
WindowSnap.left => Rect.fromLTWH(area.left, area.top, half, area.height),
|
|
WindowSnap.right => Rect.fromLTWH(
|
|
area.right - half,
|
|
area.top,
|
|
half,
|
|
area.height,
|
|
),
|
|
WindowSnap.maximize => area,
|
|
};
|
|
}
|