ef1c120f57
Phase 4 of docs/plans/floating-windows.md. A bar button lists the app's windows on every tab (show one, Show all, Minimise all, Close all). Alt+` and Alt+Shift+` bring the next or previous window to the front. Dropping a title at the left or right edge fills that half, at the top it maximises. A new window of a kind opens at the size that kind was last given.
135 lines
4.2 KiB
Dart
135 lines
4.2 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 its minimise button; 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.
|
|
Rect fitWindowFrame(Rect rect, Size bounds, Size minSize) {
|
|
final width = rect.width
|
|
.clamp(minSize.width, double.infinity)
|
|
.clamp(0.0, bounds.width);
|
|
final height = rect.height
|
|
.clamp(minSize.height, double.infinity)
|
|
.clamp(0.0, bounds.height);
|
|
return Rect.fromLTWH(
|
|
rect.left.clamp(0.0, bounds.width - width),
|
|
rect.top.clamp(0.0, bounds.height - height),
|
|
width,
|
|
height,
|
|
);
|
|
}
|
|
|
|
/// 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,
|
|
};
|
|
}
|