import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; /// The app-wide chat actions bound to a bare Alt chord (Option on a Mac). enum ShellChatAction { /// Alt+N: a new chat, with the Chat list's create dialog. newChat, /// Alt+J: the next chat waiting for the operator, unread or failed. nextAttention, } /// The chat action [event] asks for, or null: Alt+N or Alt+J with no other /// modifier. Option+N and Option+J yield a dead key or a symbol on a Mac, so /// the key is matched by position as well. Repeats of a held chord match too, /// so the caller can swallow them; only the first press acts. /// /// Neither chord is taken by Chromium, Firefox or Safari on Linux, Windows or /// macOS, nor by the app's other shortcuts (Alt+K, Alt+T, Alt+Y, Alt+E, /// Alt+`, Alt+/). The terminal keeps both for readline (Alt+N searches /// history), so the caller leaves them to it while it has the keyboard. ShellChatAction? shellChatActionShortcut( KeyEvent event, HardwareKeyboard keyboard, ) { if (event is KeyUpEvent || !keyboard.isAltPressed || keyboard.isControlPressed || keyboard.isMetaPressed || keyboard.isShiftPressed) { return null; } bool key(LogicalKeyboardKey logical, PhysicalKeyboardKey physical) => event.logicalKey == logical || event.physicalKey == physical; if (key(LogicalKeyboardKey.keyN, PhysicalKeyboardKey.keyN)) { return ShellChatAction.newChat; } if (key(LogicalKeyboardKey.keyJ, PhysicalKeyboardKey.keyJ)) { return ShellChatAction.nextAttention; } return null; } /// [action]'s chord as it reads on [platform], for the help page. String shellChatActionLabel( ShellChatAction action, [ TargetPlatform? platform, ]) { final target = platform ?? defaultTargetPlatform; final apple = target == TargetPlatform.macOS || target == TargetPlatform.iOS; final letter = switch (action) { ShellChatAction.newChat => 'N', ShellChatAction.nextAttention => 'J', }; return '${apple ? 'Option' : 'Alt'}+$letter'; }