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.
148 lines
4.4 KiB
Dart
148 lines
4.4 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
/// A layer of floating windows painted in the app's root overlay, above the
|
|
/// page, while it keeps its place in the tree (its inherited widgets, focus
|
|
/// and state stay where it is built). Every [FloatingLayer] shares that one
|
|
/// overlay, so windows from different features (the terminal, a floating
|
|
/// chat) stack by use: the layer last pressed, focused or opened is on top.
|
|
///
|
|
/// [builder] returns the windows as [Positioned] children of a [Stack] the
|
|
/// size of [bounds]: this widget's own box, or the whole overlay when
|
|
/// [coverOverlay] is set. Space between windows takes no input. Without an
|
|
/// overlay (a bare widget test) the layer paints in place.
|
|
class FloatingLayer extends StatefulWidget {
|
|
const FloatingLayer({
|
|
super.key,
|
|
required this.builder,
|
|
this.open = true,
|
|
this.coverOverlay = false,
|
|
});
|
|
|
|
final List<Widget> Function(BuildContext context, Size bounds) builder;
|
|
|
|
/// Turning true brings the layer to the top, as its window opens.
|
|
final bool open;
|
|
|
|
final bool coverOverlay;
|
|
|
|
@override
|
|
State<FloatingLayer> createState() => _FloatingLayerState();
|
|
}
|
|
|
|
class _FloatingLayerState extends State<FloatingLayer> {
|
|
final OverlayPortalController _portal = OverlayPortalController(
|
|
debugLabel: 'floating layer',
|
|
);
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_portal.show();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant FloatingLayer oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (widget.open && !oldWidget.open) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => _raise());
|
|
}
|
|
}
|
|
|
|
void _raise() {
|
|
if (mounted) _portal.show();
|
|
}
|
|
|
|
Widget _layer(BuildContext context, Size bounds) => Listener(
|
|
behavior: HitTestBehavior.deferToChild,
|
|
onPointerDown: (_) => _raise(),
|
|
child: Focus(
|
|
canRequestFocus: false,
|
|
skipTraversal: true,
|
|
onFocusChange: (focused) {
|
|
if (focused) _raise();
|
|
},
|
|
child: SizedBox.fromSize(
|
|
size: bounds,
|
|
child: Stack(
|
|
clipBehavior: Clip.none,
|
|
children: widget.builder(context, bounds),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (Overlay.maybeOf(context, rootOverlay: true) == null) {
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) => _layer(context, constraints.biggest),
|
|
);
|
|
}
|
|
return OverlayPortal.overlayChildLayoutBuilder(
|
|
controller: _portal,
|
|
overlayLocation: OverlayChildLocation.rootOverlay,
|
|
overlayChildBuilder: (context, info) => widget.coverOverlay
|
|
? Positioned.fill(child: _layer(context, info.overlaySize))
|
|
: Positioned(
|
|
left: 0,
|
|
top: 0,
|
|
width: info.childSize.width,
|
|
height: info.childSize.height,
|
|
child: Transform(
|
|
transform: info.childPaintTransform,
|
|
child: _layer(context, info.childSize),
|
|
),
|
|
),
|
|
child: const SizedBox.expand(),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// A focus parent for floating windows placed above the app's destinations,
|
|
/// so a window built inside one destination keeps the keyboard while another
|
|
/// destination shows (a hidden destination excludes its own subtree from
|
|
/// focus). Windows attach their focus scope here with [maybeOf].
|
|
class FloatingFocusRoot extends StatefulWidget {
|
|
const FloatingFocusRoot({super.key, required this.child});
|
|
|
|
final Widget child;
|
|
|
|
/// The nearest root's focus node, or null outside one.
|
|
static FocusNode? maybeOf(BuildContext context) =>
|
|
context.dependOnInheritedWidgetOfExactType<_FloatingFocusScope>()?.node;
|
|
|
|
@override
|
|
State<FloatingFocusRoot> createState() => _FloatingFocusRootState();
|
|
}
|
|
|
|
class _FloatingFocusRootState extends State<FloatingFocusRoot> {
|
|
final FocusNode _node = FocusNode(
|
|
debugLabel: 'floating windows',
|
|
canRequestFocus: false,
|
|
skipTraversal: true,
|
|
);
|
|
|
|
@override
|
|
void dispose() {
|
|
_node.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Focus(
|
|
focusNode: _node,
|
|
includeSemantics: false,
|
|
child: _FloatingFocusScope(node: _node, child: widget.child),
|
|
);
|
|
}
|
|
|
|
class _FloatingFocusScope extends InheritedWidget {
|
|
const _FloatingFocusScope({required this.node, required super.child});
|
|
|
|
final FocusNode node;
|
|
|
|
@override
|
|
bool updateShouldNotify(_FloatingFocusScope oldWidget) =>
|
|
oldWidget.node != node;
|
|
}
|