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.
This commit is contained in:
@@ -75,13 +75,27 @@ class ComposerFocusRequestListener {
|
||||
void _onRequest() {
|
||||
final scope = _scope;
|
||||
if (scope == null || !scope.requests.take(scope.sessionId)) return;
|
||||
final before = FocusManager.instance.primaryFocus;
|
||||
SchedulerBinding.instance
|
||||
..addPostFrameCallback((_) {
|
||||
if (!_disposed && canFocus()) focus.requestFocus();
|
||||
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);
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/gestures.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// Whether choosing a chat (a row tap or click, Next chat, a created chat)
|
||||
/// puts the keyboard in its composer, as Ctrl+1…Ctrl+9 always does.
|
||||
///
|
||||
/// Desktop hosts (native or a desktop browser, whatever the window width)
|
||||
/// focus it. Phones and tablets do not, since focusing would raise the
|
||||
/// on-screen keyboard over the transcript, unless the choice was made with a
|
||||
/// mouse or trackpad, which means hardware input is attached. On the web the
|
||||
/// platform comes from the browser, so a mobile browser counts as mobile.
|
||||
/// A closed chat is only read, so its composer is left alone.
|
||||
bool chatSelectionFocusesComposer(
|
||||
JsonMap session, {
|
||||
TargetPlatform? platform,
|
||||
PointerDeviceKind? pointer,
|
||||
}) {
|
||||
if (session['closed_at'] != null || session['closed'] == true) return false;
|
||||
final desktop = switch (platform ?? defaultTargetPlatform) {
|
||||
TargetPlatform.macOS ||
|
||||
TargetPlatform.windows ||
|
||||
TargetPlatform.linux => true,
|
||||
_ => false,
|
||||
};
|
||||
if (desktop) return true;
|
||||
final kind = pointer ?? LastPointerKind.instance.kind;
|
||||
return kind == PointerDeviceKind.mouse || kind == PointerDeviceKind.trackpad;
|
||||
}
|
||||
|
||||
/// The kind of the latest pointer press anywhere in the app, so a choice
|
||||
/// made through a plain callback can tell a mouse click from a finger tap.
|
||||
class LastPointerKind {
|
||||
LastPointerKind._() {
|
||||
GestureBinding.instance.pointerRouter.addGlobalRoute(_route);
|
||||
}
|
||||
|
||||
static LastPointerKind? _instance;
|
||||
|
||||
/// Starts listening on first use; call once early (the Chat destination
|
||||
/// does) so the press that makes the first choice is seen.
|
||||
static LastPointerKind get instance => _instance ??= LastPointerKind._();
|
||||
|
||||
PointerDeviceKind? kind;
|
||||
|
||||
void _route(PointerEvent event) {
|
||||
if (event is PointerDownEvent || event is PointerPanZoomStartEvent) {
|
||||
kind = event.kind;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user