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; } } }