import 'dart:async'; import 'dart:ui'; import 'package:flutter/foundation.dart'; import 'chat_float_store.dart'; import 'chat_workspace_controller.dart'; /// The floating chat windows' frames, by pane id. A drag moves a window every /// pointer event without saving; [commit] marks the end of the drag. A pane /// keeps its frame while it is docked, so floating it again puts the window /// back where it was. class ChatFloatFrames extends ChangeNotifier { ChatFloatFrames([Map frames = const {}]) : _frames = {...frames}; final Map _frames; int _commits = 0; Map get frames => Map.unmodifiable(_frames); /// Advances when a move or resize ends (the binding saves then). int get commits => _commits; /// [paneId]'s frame; a window without one opens at the default frame, /// stepped down and right by [index] so stacked windows stay visible. Rect rectFor(String paneId, {int index = 0}) => _frames[paneId] ?? chatFloatDefaultRect.shift(Offset(32, 32) * index.toDouble()); /// Gives [paneId] its opening frame ([rectFor] at [index]) unless it has /// one, so the window stays put when other windows come and go. void place(String paneId, {required int index}) { if (_frames.containsKey(paneId)) return; move(paneId, rectFor(paneId, index: index)); } void move(String paneId, Rect rect) { if (_frames[paneId] == rect) return; _frames[paneId] = rect; notifyListeners(); } /// Replaces every frame (a restore). void restore(Map frames) { if (mapEquals(frames, _frames)) return; _frames ..clear() ..addAll(frames); notifyListeners(); } void commit() { _commits++; notifyListeners(); } } /// Saves which chats float, whether they show and their windows' frames, and /// restores them once the workspace layout itself has been restored. class ChatFloatBinding { ChatFloatBinding({ required this.controller, required this.frames, required this.store, this.debounce = const Duration(milliseconds: 250), }) { controller.addListener(_changed); frames.addListener(_framesChanged); } final ChatWorkspaceController controller; final ChatFloatFrames frames; final ChatFloatStore store; final Duration debounce; ChatFloatRecord? _saved; bool _loading = true; bool _disposed = false; int _commits = 0; Timer? _timer; Future _writes = Future.value(); /// Frames of panes still open somewhere (tiled or floating); a closed /// pane's frame is dropped. ChatFloatRecord get _current { final open = { for (final pane in controller.panes) pane.id, for (final pane in controller.floatingPanes) pane.id, }; return ChatFloatRecord( panes: controller.floatingPanes, shown: controller.floatShown, frames: { for (final MapEntry(:key, :value) in frames.frames.entries) if (open.contains(key)) key: value, }, ); } /// Call after the workspace layout restored, so a saved float can tell /// whether its chat is already open in the tiling. Future start() async { ChatFloatRecord? record; try { record = await store.read(); } on Object { // Unavailable preferences leave the default frames and no float. } if (_disposed) return; if (record != null) { _saved = record; _commits = frames.commits; frames.restore(record.frames); if (record.panes.isNotEmpty && controller.floatingPanes.isEmpty) { controller.restoreFloats(record.panes, shown: record.shown); } } _loading = false; _changed(); } void _framesChanged() { if (frames.commits == _commits) return; _commits = frames.commits; _changed(); } void _changed() { if (_disposed || _loading || _current == _saved) return; _timer?.cancel(); _timer = Timer(debounce, flush); } Future flush() { _timer?.cancel(); _timer = null; final record = _current; if (_loading || record == _saved) return _writes; _saved = record; _writes = _writes.then((_) async { try { await store.write(record); } on Object { // A storage failure must not interrupt the chat. } }); return _writes; } void dispose() { if (_disposed) return; controller.removeListener(_changed); frames.removeListener(_framesChanged); unawaited(flush()); _disposed = true; } }