d6f636a325
A chat opened from the keyboard asks its composer for focus; the composer takes it after the frame it is built in (new detail pane or phone page), or at once when that chat is already open. Taps and other opens cancel the request, so they keep focus where it was. The capture kit's press step accepts modifier chords such as Control+2.
91 lines
3.0 KiB
Dart
91 lines
3.0 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;
|
|
SchedulerBinding.instance
|
|
..addPostFrameCallback((_) {
|
|
if (!_disposed && canFocus()) focus.requestFocus();
|
|
})
|
|
..ensureVisualUpdate();
|
|
}
|
|
|
|
void dispose() {
|
|
_disposed = true;
|
|
_scope?.requests.removeListener(_onRequest);
|
|
_scope = null;
|
|
}
|
|
}
|