Files
pipelines/app/lib/features/chat/conversation/chat_composer_paste_focus.dart
T
alan 1a584e3432 Paste into a focused floating chat
The clipboard bridge gave every paste to the active tiled chat while
another composer had focus, so a floating chat (never the active pane)
could not receive pasted text or files. A floating chat's composer now
owns the paste while it has the keyboard, and the paste target yields to
it. A focused composer in an inactive tiled pane still leaves the paste
to the active pane, as before.
2026-11-10 16:02:35 +00:00

51 lines
2.0 KiB
Dart

import 'package:flutter/widgets.dart';
import '../../../ui/input/keyboard_owner.dart';
/// Flutter shares a single web editing element across fields. DOM identity
/// alone cannot tell a chat composer from search, rename or another editor.
abstract final class ChatComposerPasteFocus {
static final Set<FocusNode> _composers = {};
static final Set<FocusNode> _inputs = {};
static void register(FocusNode focus) => _composers.add(focus);
static void unregister(FocusNode focus) {
_composers.remove(focus);
_owners.remove(focus);
}
/// An input that is not an [EditableText] (a shell command's terminal)
/// registers its focus here: a paste while it has focus is its own.
static void registerInput(FocusNode focus) => _inputs.add(focus);
static void unregisterInput(FocusNode focus) => _inputs.remove(focus);
/// Composers that own a paste while they have the keyboard, even when they
/// are not the paste target (a floating chat's).
static final Set<FocusNode> _owners = {};
static void setOwnsFocusedPaste(FocusNode focus, bool owns) =>
owns ? _owners.add(focus) : _owners.remove(focus);
/// Whether a composer other than [mine] owns the paste now: it has the
/// keyboard and owns a focused paste, so [mine] must not take it.
static bool otherOwnerHasFocus(FocusNode mine) =>
_owners.any((focus) => !identical(focus, mine) && focus.hasFocus);
static bool get anotherEditableHasFocus {
final primary = FocusManager.instance.primaryFocus;
if (primary == null || _composers.any((focus) => focus.hasFocus)) {
return false;
}
if (_inputs.any((focus) => focus.hasFocus)) return true;
if (primaryFocusIsTypingSurface()) return true;
final context = primary.context;
if (context == null) return false;
var editable = context.widget is EditableText;
context.visitAncestorElements((element) {
if (element.widget is EditableText) editable = true;
return !editable;
});
return editable;
}
}