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 data; WindowEntry copyWith({ String? contentId, Rect? frame, bool? minimized, bool? maximized, Map? 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);