Files
pipelines/app/lib/features/chat/conversation/composer_focus_request.dart
T
alan 38cd92813b Chat: choosing a chat focuses its composer on desktop
A row click or tap, Next chat and a created chat put the keyboard in the
composer, as Ctrl+1-9 does, on desktop hosts (native or desktop browser, any
window width) and after a mouse or trackpad click anywhere. A finger tap on a
phone or tablet (Android, iOS, mobile browsers) leaves focus alone so the
on-screen keyboard does not cover the transcript. Closed chats are not
focused, and a request yields when another control took focus meanwhile.
2026-11-04 20:42:20 +00:00

105 lines
3.5 KiB
Dart

import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
/// One pending request to give a chat's composer keyboard focus, made when
/// the operator opens that chat from the keyboard (Ctrl+1…Ctrl+9) so they can
/// type at once. The composer takes it once it is built: at once when the
/// chat is already open, after the switch when the detail pane or page still
/// has to build it. A tap or any other way of opening a chat cancels it, so
/// only the keyboard path moves focus.
class ChatComposerFocusRequests extends ChangeNotifier {
String? _pending;
/// Asks the composer of [sessionId] to take focus once it is built.
void request(String sessionId) {
_pending = sessionId;
notifyListeners();
}
/// Drops a request no composer has taken yet.
void cancel() => _pending = null;
/// Consumes the request when it names [sessionId]; true when it did.
bool take(String sessionId) {
if (_pending != sessionId) return false;
_pending = null;
return true;
}
}
/// Tells the composer below which chat it belongs to and where its focus
/// requests come from. The Chat destination wraps each open conversation in
/// one; a composer outside it never takes focus on its own.
class ChatComposerFocusScope extends InheritedWidget {
const ChatComposerFocusScope({
super.key,
required this.sessionId,
required this.requests,
required super.child,
});
final String sessionId;
final ChatComposerFocusRequests requests;
static ChatComposerFocusScope? maybeOf(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<ChatComposerFocusScope>();
@override
bool updateShouldNotify(ChatComposerFocusScope oldWidget) =>
sessionId != oldWidget.sessionId || requests != oldWidget.requests;
}
/// Connects a composer's [focus] to the nearest [ChatComposerFocusScope]:
/// a request for its chat focuses it after the current frame, so the switch
/// (a new detail pane or page and its route focus) has settled first.
/// [canFocus] says whether the composer is still the operator's place then.
class ComposerFocusRequestListener {
ComposerFocusRequestListener({required this.focus, required this.canFocus});
final FocusNode focus;
final bool Function() canFocus;
ChatComposerFocusScope? _scope;
bool _disposed = false;
/// Call from the composer's `didChangeDependencies`.
void attach(BuildContext context) {
final scope = ChatComposerFocusScope.maybeOf(context);
if (scope?.requests != _scope?.requests) {
_scope?.requests.removeListener(_onRequest);
scope?.requests.addListener(_onRequest);
}
_scope = scope;
_onRequest();
}
void _onRequest() {
final scope = _scope;
if (scope == null || !scope.requests.take(scope.sessionId)) return;
final before = FocusManager.instance.primaryFocus;
SchedulerBinding.instance
..addPostFrameCallback((_) {
if (!_disposed && _focusIsFree(before) && canFocus()) {
focus.requestFocus();
}
})
..ensureVisualUpdate();
}
/// False when, since the request, another control took focus (the
/// operator moved on before the chat was built); the composer then leaves
/// it there.
bool _focusIsFree(FocusNode? before) {
final current = FocusManager.instance.primaryFocus;
return current == null ||
current == before ||
current == focus ||
current is FocusScopeNode;
}
void dispose() {
_disposed = true;
_scope?.requests.removeListener(_onRequest);
_scope = null;
}
}