import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../../../ui/floating/floating_layer.dart'; import '../../../ui/motion/motion.dart'; import '../ui/sessions/chat_adaptive.dart'; import 'chat_float_binding.dart'; 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 window shows only while [enabled] (a wide layout) and the Chat /// destination is current; otherwise the floating chat is kept, unpainted. class ChatFloatHost extends StatefulWidget { const ChatFloatHost({ super.key, required this.controller, required this.frame, required this.enabled, required this.title, required this.content, required this.scope, required this.focusedPane, required this.onFocusPane, required this.child, }); final ChatWorkspaceController controller; final ChatFloatFrame frame; final bool enabled; final String title; final Widget? content; /// The floating pane's focus scope. final FocusScopeNode? scope; /// The tiled pane with the keyboard, which the shortcut floats when no /// chat floats yet. final ChatWorkspacePane? Function() focusedPane; final ValueChanged? onFocusPane; final Widget child; @override State createState() => _ChatFloatHostState(); } class _ChatFloatHostState extends State 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; bool _visible = false; String? _paneId; FocusNode? _previous; @override void initState() { 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()); } @override void didUpdateWidget(covariant ChatFloatHost oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.controller != widget.controller) { oldWidget.controller.removeListener(_sync); widget.controller.addListener(_sync); } if (oldWidget.enabled != widget.enabled) { WidgetsBinding.instance.addPostFrameCallback((_) => _sync()); } } @override 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. _previous = null; _reveal.value = 0; } else if (visible != _visible) { if (visible) { _remember(); unawaited(_reveal.forward()); _focusSoon(pane); } else { _release(); unawaited(_reveal.reverse()); } } else if (visible && pane.id != _paneId) { // Another chat replaced the floating one. _focusSoon(pane); } _paneId = pane?.id; if (visible != _visible) setState(() => _visible = visible); } bool _inside(FocusNode node) { final scope = widget.scope; return scope != null && (identical(node, scope) || node.ancestors.contains(scope)); } void _remember() { final current = FocusManager.instance.primaryFocus; if (current == null || _inside(current)) return; _previous = identical(current, FocusManager.instance.rootScope) ? null : current; } void _focusSoon(ChatWorkspacePane pane) { WidgetsBinding.instance ..addPostFrameCallback((_) { if (!mounted || widget.controller.floatingPane?.id != pane.id) return; final focus = widget.onFocusPane; if (focus != null) { focus(pane); } else { widget.scope?.requestFocus(); } }) ..ensureVisualUpdate(); } /// Gives the keyboard back to what had it before the chat showed, if the /// floating chat has it now. void _release() { final previous = _previous; _previous = null; final scope = widget.scope; if (scope == null || !scope.hasFocus) return; if (previous != null && previous.context != null && previous.canRequestFocus) { previous.requestFocus(); } else { scope.unfocus(); } } /// Handled before any focused widget sees the key, so it works from a /// composer or the terminal; ignored (left to them) when it does nothing. KeyEventResult _key(KeyEvent event) { if (!mounted || !_presentable || !chatFloatShortcut(event, HardwareKeyboard.instance)) { return KeyEventResult.ignored; } final controller = widget.controller; if (controller.floatingPane == null) { final pane = widget.focusedPane(); if (pane == null || !controller.canFloat(pane.id)) { return KeyEventResult.ignored; } if (event is KeyDownEvent) controller.float(pane.id); return KeyEventResult.handled; } if (event is KeyDownEvent) controller.toggleFloatShown(); return KeyEventResult.handled; } @override Widget build(BuildContext context) { final content = widget.content; return Stack( fit: StackFit.expand, children: [ widget.child, if (content != null) Positioned.fill( child: FloatingLayer( open: _visible, coverOverlay: true, builder: (context, bounds) => [ ListenableBuilder( listenable: Listenable.merge([widget.frame, _reveal]), builder: (context, _) => ChatFloatWindow( frame: widget.frame, bounds: bounds, title: widget.title, cupertino: chatControlsStyleOf(context) == ChatControlsStyle.cupertino, reveal: _curve, hiding: !_visible, offstage: !_visible && _reveal.isDismissed, shortcut: chatFloatShortcutLabel(), onDock: widget.controller.dock, onHide: () => widget.controller.setFloatShown(false), child: content, ), ), ], ), ), ], ); } }