Float one chat pane over the app and toggle it with Ctrl+Y / Cmd+Y
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
import 'dart:async';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'chat_float_store.dart';
|
||||
import 'chat_workspace_controller.dart';
|
||||
|
||||
/// The floating chat window's frame. A drag moves it every pointer event
|
||||
/// without saving; [commit] marks the end of the drag.
|
||||
class ChatFloatFrame extends ChangeNotifier {
|
||||
ChatFloatFrame([this._rect = chatFloatDefaultRect]);
|
||||
Rect _rect;
|
||||
Rect get rect => _rect;
|
||||
int _commits = 0;
|
||||
|
||||
/// Advances when a move or resize ends (the binding saves then).
|
||||
int get commits => _commits;
|
||||
|
||||
void move(Rect rect) {
|
||||
if (rect == _rect) return;
|
||||
_rect = rect;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void commit() {
|
||||
_commits++;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// Saves which chat floats, whether it shows and the window's frame, and
|
||||
/// restores them once the workspace layout itself has been restored.
|
||||
class ChatFloatBinding {
|
||||
ChatFloatBinding({
|
||||
required this.controller,
|
||||
required this.frame,
|
||||
required this.store,
|
||||
this.debounce = const Duration(milliseconds: 250),
|
||||
}) {
|
||||
controller.addListener(_changed);
|
||||
frame.addListener(_frameChanged);
|
||||
}
|
||||
|
||||
final ChatWorkspaceController controller;
|
||||
final ChatFloatFrame frame;
|
||||
final ChatFloatStore store;
|
||||
final Duration debounce;
|
||||
ChatFloatRecord? _saved;
|
||||
bool _loading = true;
|
||||
bool _disposed = false;
|
||||
int _commits = 0;
|
||||
Timer? _timer;
|
||||
Future<void> _writes = Future.value();
|
||||
|
||||
ChatFloatRecord get _current => ChatFloatRecord(
|
||||
pane: controller.floatingPane,
|
||||
shown: controller.floatShown,
|
||||
rect: frame.rect,
|
||||
);
|
||||
|
||||
/// 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 frame and no float.
|
||||
}
|
||||
if (_disposed) return;
|
||||
if (record != null) {
|
||||
_saved = record;
|
||||
_commits = frame.commits;
|
||||
frame.move(record.rect);
|
||||
final pane = record.pane;
|
||||
if (pane != null && controller.floatingPane == null) {
|
||||
controller.restoreFloat(pane, shown: record.shown);
|
||||
}
|
||||
}
|
||||
_loading = false;
|
||||
_changed();
|
||||
}
|
||||
|
||||
void _frameChanged() {
|
||||
if (frame.commits == _commits) return;
|
||||
_commits = frame.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);
|
||||
frame.removeListener(_frameChanged);
|
||||
unawaited(flush());
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user