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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import 'conversation/chat_scroll.dart';
|
||||
import 'conversation/chat_thread_view.dart';
|
||||
import 'conversation/chat_transcript_cache.dart';
|
||||
import 'conversation/composer_focus_request.dart';
|
||||
import 'conversation/composer_selection_focus.dart';
|
||||
import 'data/chat_repository.dart';
|
||||
import 'domain/chat_commands.dart';
|
||||
import 'domain/chat_cycle.dart';
|
||||
@@ -95,7 +96,8 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
|
||||
String? _standaloneSelection;
|
||||
|
||||
/// Ctrl+1…Ctrl+9 put the keyboard in the opened chat's composer.
|
||||
/// Ctrl+1…Ctrl+9 put the keyboard in the opened chat's composer; choosing a
|
||||
/// chat does too where [chatSelectionFocusesComposer] allows.
|
||||
final ChatComposerFocusRequests _composerFocus = ChatComposerFocusRequests();
|
||||
|
||||
/// The list's folded sections, kept here so Next chat counts the rows the
|
||||
@@ -109,6 +111,8 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Seen from now on, so the press that chooses the first chat counts.
|
||||
LastPointerKind.instance;
|
||||
_sessions.addListener(_nextChanged);
|
||||
_folds.addListener(_nextChanged);
|
||||
}
|
||||
@@ -124,7 +128,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
/// Next chat opens that row exactly as a tap on it would.
|
||||
void _openNext(String id) {
|
||||
final next = _nextAfter(id);
|
||||
if (next != null) _open(next);
|
||||
if (next != null) _openBySelection(next);
|
||||
}
|
||||
|
||||
void _nextChanged() {
|
||||
@@ -153,6 +157,12 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
/// also when it is the chat already open.
|
||||
void _openByShortcut(JsonMap session) => _open(session, focusComposer: true);
|
||||
|
||||
/// Opens [session] chosen by a tap, click or Next chat: its composer takes
|
||||
/// focus on a desktop host or after a mouse click, not from a finger on a
|
||||
/// phone or tablet (see [chatSelectionFocusesComposer]).
|
||||
void _openBySelection(JsonMap session) =>
|
||||
_open(session, focusComposer: chatSelectionFocusesComposer(session));
|
||||
|
||||
void _open(JsonMap session, {bool focusComposer = false}) {
|
||||
final id = '${session['id'] ?? ''}';
|
||||
if (id.isEmpty) return;
|
||||
@@ -370,7 +380,7 @@ class _ChatScreenState extends State<ChatScreen> {
|
||||
syncToken: widget.syncToken,
|
||||
controller: _sessions,
|
||||
folds: _folds,
|
||||
onOpen: _open,
|
||||
onOpen: _openBySelection,
|
||||
onShortcutOpen: _openByShortcut,
|
||||
onSessions: _adoptRows,
|
||||
activeSessionId: selected,
|
||||
|
||||
Reference in New Issue
Block a user