Float several chats and keep them working on every tab
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.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../../../ui/floating/floating_layer.dart';
|
||||
@@ -11,18 +12,18 @@ import 'chat_float_shortcut.dart';
|
||||
import 'chat_float_window.dart';
|
||||
import 'chat_workspace_controller.dart';
|
||||
|
||||
/// The split workspace ([child]) with its floating chat over the app: the
|
||||
/// window, its show and hide motion, its focus and its shortcut (see
|
||||
/// [chatFloatShortcut]). [content] is the floating pane's live view, moved
|
||||
/// here from the tiling rather than rebuilt.
|
||||
/// The split workspace ([child]) with its floating chats over the app: their
|
||||
/// windows, show and hide motion, focus and shortcut (see
|
||||
/// [chatFloatShortcut]). [content] is a floating pane's live view, moved here
|
||||
/// from the tiling rather than rebuilt.
|
||||
///
|
||||
/// The window shows only while [enabled] (a wide layout) and the Chat
|
||||
/// destination is current; otherwise the floating chat is kept, unpainted.
|
||||
/// The windows show while [enabled] (a wide layout), over every destination,
|
||||
/// like the floating terminal. They show and hide together.
|
||||
class ChatFloatHost extends StatefulWidget {
|
||||
const ChatFloatHost({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.frame,
|
||||
required this.frames,
|
||||
required this.enabled,
|
||||
required this.title,
|
||||
required this.content,
|
||||
@@ -33,13 +34,13 @@ class ChatFloatHost extends StatefulWidget {
|
||||
});
|
||||
|
||||
final ChatWorkspaceController controller;
|
||||
final ChatFloatFrame frame;
|
||||
final ChatFloatFrames frames;
|
||||
final bool enabled;
|
||||
final String title;
|
||||
final Widget? content;
|
||||
final String Function(ChatWorkspacePane pane) title;
|
||||
final Widget Function(ChatWorkspacePane pane) content;
|
||||
|
||||
/// The floating pane's focus scope.
|
||||
final FocusScopeNode? scope;
|
||||
/// A floating pane's focus scope.
|
||||
final FocusScopeNode? Function(ChatWorkspacePane pane) scope;
|
||||
|
||||
/// The tiled pane with the keyboard, which the shortcut floats when no
|
||||
/// chat floats yet.
|
||||
@@ -51,20 +52,9 @@ class ChatFloatHost extends StatefulWidget {
|
||||
State<ChatFloatHost> createState() => _ChatFloatHostState();
|
||||
}
|
||||
|
||||
class _ChatFloatHostState extends State<ChatFloatHost>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late final AnimationController _reveal = AnimationController(
|
||||
vsync: this,
|
||||
duration: Motion.standard,
|
||||
);
|
||||
late final CurvedAnimation _curve = CurvedAnimation(
|
||||
parent: _reveal,
|
||||
curve: Motion.curve,
|
||||
reverseCurve: Curves.easeInCubic,
|
||||
);
|
||||
bool _current = true;
|
||||
class _ChatFloatHostState extends State<ChatFloatHost> {
|
||||
bool _visible = false;
|
||||
String? _paneId;
|
||||
Set<String> _paneIds = const {};
|
||||
FocusNode? _previous;
|
||||
|
||||
@override
|
||||
@@ -72,13 +62,6 @@ class _ChatFloatHostState extends State<ChatFloatHost>
|
||||
super.initState();
|
||||
FocusManager.instance.addEarlyKeyEventHandler(_key);
|
||||
widget.controller.addListener(_sync);
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
// Another destination mutes this one's tickers and takes its focus.
|
||||
_current = TickerMode.valuesOf(context).enabled;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _sync());
|
||||
}
|
||||
|
||||
@@ -98,44 +81,41 @@ class _ChatFloatHostState extends State<ChatFloatHost>
|
||||
void dispose() {
|
||||
FocusManager.instance.removeEarlyKeyEventHandler(_key);
|
||||
widget.controller.removeListener(_sync);
|
||||
_curve.dispose();
|
||||
_reveal.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool get _presentable => widget.enabled && _current;
|
||||
|
||||
void _sync() {
|
||||
if (!mounted) return;
|
||||
final pane = widget.controller.floatingPane;
|
||||
final visible = _presentable && widget.controller.floatShown;
|
||||
_reveal.duration = Motion.of(context, Motion.standard);
|
||||
if (pane == null) {
|
||||
// Docked or closed: the view went back to the tiling, so no motion.
|
||||
final panes = widget.controller.floatingPanes;
|
||||
final ids = {for (final pane in panes) pane.id};
|
||||
final visible = widget.enabled && widget.controller.floatShown;
|
||||
for (final (index, pane) in panes.indexed) {
|
||||
widget.frames.place(pane.id, index: index);
|
||||
}
|
||||
if (panes.isEmpty) {
|
||||
// Docked or closed: the views went back to the tiling.
|
||||
_previous = null;
|
||||
_reveal.value = 0;
|
||||
} else if (visible != _visible) {
|
||||
if (visible) {
|
||||
_remember();
|
||||
unawaited(_reveal.forward());
|
||||
_focusSoon(pane);
|
||||
_focusSoon(panes.last);
|
||||
} else {
|
||||
_release();
|
||||
unawaited(_reveal.reverse());
|
||||
}
|
||||
} else if (visible && pane.id != _paneId) {
|
||||
// Another chat replaced the floating one.
|
||||
_focusSoon(pane);
|
||||
} else if (visible) {
|
||||
// A newly floated chat takes the keyboard.
|
||||
final added = panes.where((p) => !_paneIds.contains(p.id)).lastOrNull;
|
||||
if (added != null) _focusSoon(added);
|
||||
}
|
||||
_paneId = pane?.id;
|
||||
_paneIds = ids;
|
||||
if (visible != _visible) setState(() => _visible = visible);
|
||||
}
|
||||
|
||||
bool _inside(FocusNode node) {
|
||||
final scope = widget.scope;
|
||||
bool _inside(FocusNode node) => widget.controller.floatingPanes.any((pane) {
|
||||
final scope = widget.scope(pane);
|
||||
return scope != null &&
|
||||
(identical(node, scope) || node.ancestors.contains(scope));
|
||||
}
|
||||
});
|
||||
|
||||
void _remember() {
|
||||
final current = FocusManager.instance.primaryFocus;
|
||||
@@ -148,30 +128,30 @@ class _ChatFloatHostState extends State<ChatFloatHost>
|
||||
void _focusSoon(ChatWorkspacePane pane) {
|
||||
WidgetsBinding.instance
|
||||
..addPostFrameCallback((_) {
|
||||
if (!mounted || widget.controller.floatingPane?.id != pane.id) return;
|
||||
if (!mounted || !widget.controller.isFloating(pane.id)) return;
|
||||
final focus = widget.onFocusPane;
|
||||
if (focus != null) {
|
||||
focus(pane);
|
||||
} else {
|
||||
widget.scope?.requestFocus();
|
||||
widget.scope(pane)?.requestFocus();
|
||||
}
|
||||
})
|
||||
..ensureVisualUpdate();
|
||||
}
|
||||
|
||||
/// Gives the keyboard back to what had it before the chat showed, if the
|
||||
/// Gives the keyboard back to what had it before the chats showed, if a
|
||||
/// floating chat has it now.
|
||||
void _release() {
|
||||
final previous = _previous;
|
||||
_previous = null;
|
||||
final scope = widget.scope;
|
||||
if (scope == null || !scope.hasFocus) return;
|
||||
final current = FocusManager.instance.primaryFocus;
|
||||
if (current == null || !_inside(current)) return;
|
||||
if (previous != null &&
|
||||
previous.context != null &&
|
||||
previous.canRequestFocus) {
|
||||
previous.requestFocus();
|
||||
} else {
|
||||
scope.unfocus();
|
||||
current.unfocus();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,12 +159,12 @@ class _ChatFloatHostState extends State<ChatFloatHost>
|
||||
/// composer or the terminal; ignored (left to them) when it does nothing.
|
||||
KeyEventResult _key(KeyEvent event) {
|
||||
if (!mounted ||
|
||||
!_presentable ||
|
||||
!widget.enabled ||
|
||||
!chatFloatShortcut(event, HardwareKeyboard.instance)) {
|
||||
return KeyEventResult.ignored;
|
||||
}
|
||||
final controller = widget.controller;
|
||||
if (controller.floatingPane == null) {
|
||||
if (controller.floatingPanes.isEmpty) {
|
||||
final pane = widget.focusedPane();
|
||||
if (pane == null || !controller.canFloat(pane.id)) {
|
||||
return KeyEventResult.ignored;
|
||||
@@ -198,33 +178,37 @@ class _ChatFloatHostState extends State<ChatFloatHost>
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final content = widget.content;
|
||||
final panes = widget.controller.floatingPanes;
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
return Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
widget.child,
|
||||
if (content != null)
|
||||
for (final (index, pane) in panes.indexed)
|
||||
Positioned.fill(
|
||||
key: ValueKey(pane.id),
|
||||
// Shown over every destination; the window's motion keeps
|
||||
// running there (see [_ChatFloatSlot]).
|
||||
child: FloatingLayer(
|
||||
open: _visible,
|
||||
coverOverlay: true,
|
||||
builder: (context, bounds) => [
|
||||
ListenableBuilder(
|
||||
listenable: Listenable.merge([widget.frame, _reveal]),
|
||||
builder: (context, _) => ChatFloatWindow(
|
||||
frame: widget.frame,
|
||||
listenable: widget.frames,
|
||||
builder: (context, _) => _ChatFloatSlot(
|
||||
visible: _visible,
|
||||
rect: widget.frames.rectFor(pane.id, index: index),
|
||||
currentRect: () =>
|
||||
widget.frames.rectFor(pane.id, index: index),
|
||||
onMove: (rect) => widget.frames.move(pane.id, rect),
|
||||
onCommit: widget.frames.commit,
|
||||
bounds: bounds,
|
||||
title: widget.title,
|
||||
cupertino:
|
||||
chatControlsStyleOf(context) ==
|
||||
ChatControlsStyle.cupertino,
|
||||
reveal: _curve,
|
||||
hiding: !_visible,
|
||||
offstage: !_visible && _reveal.isDismissed,
|
||||
shortcut: chatFloatShortcutLabel(),
|
||||
onDock: widget.controller.dock,
|
||||
title: widget.title(pane),
|
||||
cupertino: cupertino,
|
||||
onDock: () => widget.controller.dock(pane.id),
|
||||
onHide: () => widget.controller.setFloatShown(false),
|
||||
child: content,
|
||||
child: widget.content(pane),
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -234,3 +218,112 @@ class _ChatFloatHostState extends State<ChatFloatHost>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// One floating chat's window and its own grow-and-fade motion, so a chat
|
||||
/// floated while the others show animates in alone.
|
||||
class _ChatFloatSlot extends StatefulWidget {
|
||||
const _ChatFloatSlot({
|
||||
required this.visible,
|
||||
required this.rect,
|
||||
required this.currentRect,
|
||||
required this.onMove,
|
||||
required this.onCommit,
|
||||
required this.bounds,
|
||||
required this.title,
|
||||
required this.cupertino,
|
||||
required this.onDock,
|
||||
required this.onHide,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final bool visible;
|
||||
final Rect rect;
|
||||
final Rect Function() currentRect;
|
||||
final ValueChanged<Rect> onMove;
|
||||
final VoidCallback onCommit;
|
||||
final Size bounds;
|
||||
final String title;
|
||||
final bool cupertino;
|
||||
final VoidCallback onDock;
|
||||
final VoidCallback onHide;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
State<_ChatFloatSlot> createState() => _ChatFloatSlotState();
|
||||
}
|
||||
|
||||
class _ChatFloatSlotState extends State<_ChatFloatSlot>
|
||||
implements TickerProvider {
|
||||
Ticker? _ticker;
|
||||
|
||||
/// A ticker the hidden destination's [TickerMode] does not mute: the
|
||||
/// window must still show and hide over another destination, and a
|
||||
/// TickerMode cannot re-enable what an ancestor turned off.
|
||||
@override
|
||||
Ticker createTicker(TickerCallback onTick) {
|
||||
assert(_ticker == null, 'one reveal ticker per window');
|
||||
return _ticker = Ticker(onTick, debugLabel: 'chat float reveal');
|
||||
}
|
||||
|
||||
late final AnimationController _reveal = AnimationController(
|
||||
vsync: this,
|
||||
duration: Motion.standard,
|
||||
);
|
||||
late final CurvedAnimation _curve = CurvedAnimation(
|
||||
parent: _reveal,
|
||||
curve: Motion.curve,
|
||||
reverseCurve: Curves.easeInCubic,
|
||||
);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.visible) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _run());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant _ChatFloatSlot oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.visible != widget.visible) _run();
|
||||
}
|
||||
|
||||
void _run() {
|
||||
if (!mounted) return;
|
||||
_reveal.duration = Motion.of(context, Motion.standard);
|
||||
unawaited(widget.visible ? _reveal.forward() : _reveal.reverse());
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_curve.dispose();
|
||||
_reveal.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => AnimatedBuilder(
|
||||
animation: _reveal,
|
||||
builder: (context, _) {
|
||||
final offstage = !widget.visible && _reveal.isDismissed;
|
||||
return ChatFloatWindow(
|
||||
rect: widget.rect,
|
||||
currentRect: widget.currentRect,
|
||||
onMove: widget.onMove,
|
||||
onCommit: widget.onCommit,
|
||||
bounds: widget.bounds,
|
||||
title: widget.title,
|
||||
cupertino: widget.cupertino,
|
||||
reveal: _curve,
|
||||
hiding: !widget.visible,
|
||||
offstage: offstage,
|
||||
shortcut: chatFloatShortcutLabel(),
|
||||
onDock: widget.onDock,
|
||||
onHide: widget.onHide,
|
||||
// A hidden window's chat pauses its tickers like a hidden tab.
|
||||
child: TickerMode(enabled: !offstage, child: widget.child),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user