cdd54c113c
Floating chats were stuck on other destinations: the hidden Chat tab muted the window's hide animation, so the window stayed painted but took no input. The reveal now uses its own ticker, each floating pane's focus scope hangs from a FloatingFocusRoot above the destination stack, and the windows show over every destination like the terminal. Any number of chats can float; the shortcut, a window's X and the menu show or hide them together. Each pane keeps its own frame (also while docked), saved in record version 2; version 1 records still load. Drags read the live frame so fast pointer events no longer lose movement.
157 lines
4.4 KiB
Dart
157 lines
4.4 KiB
Dart
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<String, Rect> frames = const {}])
|
|
: _frames = {...frames};
|
|
final Map<String, Rect> _frames;
|
|
int _commits = 0;
|
|
|
|
Map<String, Rect> 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<String, Rect> 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<void> _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<void> 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<void> 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;
|
|
}
|
|
}
|