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 _composers = {}; static final Set _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 _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; } }