Files
pipelines/app/lib/features/chat/workspace/chat_pane_cycle.dart
T
2026-12-03 22:18:36 +00:00

77 lines
2.9 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
/// The step Alt+] (1) or Alt+[ (-1) moves the keyboard between the chat
/// panes on screen, or null for any other key. On Apple platforms Cmd+] and
/// Cmd+[ do the same.
///
/// Why these keys:
/// - Alt+Tab and Cmd+Tab are the system's window switchers, Ctrl+Tab cycles
/// the open chats (and the browser keeps it), and Alt+` already cycles the
/// floating windows.
/// - Browsers use Alt+Left/Right for back and forward; nothing on GNOME,
/// Hyprland, Windows or a browser binds Alt+[ or Alt+].
/// - Option+] and Option+[ type quotes on a Mac, so with Alt the key is
/// matched by its position (also catching layouts whose bracket keys type
/// something else). Cmd+] and Cmd+[ are the Mac's own next/previous pair
/// and leave Option free for typing.
/// - AltGr is how several layouts type brackets, so it never matches.
///
/// Shift never matches: Alt+Shift+[ types braces. A focused terminal keeps
/// these keys (Alt+[ is its escape prefix); the caller checks that.
int? chatPaneCycleShortcut(
KeyEvent event,
HardwareKeyboard keyboard, {
TargetPlatform? platform,
}) {
if (event is KeyUpEvent ||
keyboard.isShiftPressed ||
keyboard.isControlPressed ||
keyboard.isLogicalKeyPressed(LogicalKeyboardKey.altGraph)) {
return null;
}
final alt = keyboard.isAltPressed;
final meta = keyboard.isMetaPressed;
final int? step;
if (alt && !meta) {
step = switch (event.physicalKey) {
PhysicalKeyboardKey.bracketRight => 1,
PhysicalKeyboardKey.bracketLeft => -1,
_ => null,
};
} else if (meta && !alt && _apple(platform ?? defaultTargetPlatform)) {
step =
event.logicalKey == LogicalKeyboardKey.bracketRight ||
event.physicalKey == PhysicalKeyboardKey.bracketRight
? 1
: event.logicalKey == LogicalKeyboardKey.bracketLeft ||
event.physicalKey == PhysicalKeyboardKey.bracketLeft
? -1
: null;
} else {
step = null;
}
return step;
}
bool _apple(TargetPlatform platform) =>
platform == TargetPlatform.macOS || platform == TargetPlatform.iOS;
/// The forward and backward chords as this platform reads them, for the
/// shortcuts list.
String chatPaneCycleShortcutLabel([TargetPlatform? platform]) =>
_apple(platform ?? defaultTargetPlatform)
? 'Option+], Option+[, Cmd+], Cmd+['
: 'Alt+], Alt+[';
/// The entry [step] places from [current] in [order], wrapping around; null
/// when there is no other entry to move to. A [current] outside [order]
/// starts from the first entry going forward and the last going back.
T? chatPaneCycleTarget<T>(List<T> order, int current, int step) {
if (order.length < 2 || step == 0) return null;
if (current < 0 || current >= order.length) {
return step > 0 ? order.first : order.last;
}
return order[(current + step) % order.length];
}