750071abe1
Terminals are marked as typing surfaces. While one has focus, the chat's "/ jumps to the composer", the Search tab's Escape, Ctrl+/, Ctrl+1…9 and Ctrl+Shift+arrow pane moves leave the key to the terminal, like they do for text fields.
374 lines
13 KiB
Dart
374 lines
13 KiB
Dart
import 'package:flutter/semantics.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import '../../../ui/motion/quick_switch_animation.dart';
|
|
import 'chat_workspace_controller.dart';
|
|
import 'chat_workspace_geometry.dart';
|
|
import 'chat_mobile_panes.dart';
|
|
import 'chat_pane_switch_motion.dart';
|
|
import 'chat_pane_backdrop.dart';
|
|
import 'chat_workspace_surface.dart';
|
|
import '../../../ui/input/keyboard_owner.dart';
|
|
|
|
export 'chat_workspace_controller.dart';
|
|
export 'chat_workspace_geometry.dart' show ChatPaneDirection;
|
|
|
|
typedef ChatWorkspacePaneBuilder =
|
|
Widget Function(BuildContext context, ChatWorkspacePane pane, bool active);
|
|
|
|
/// Describes the edge ownership of the pane currently being built.
|
|
class ChatWorkspacePaneScope extends InheritedWidget {
|
|
const ChatWorkspacePaneScope({
|
|
super.key,
|
|
required this.topEdgeShared,
|
|
required this.bottomEdgeShared,
|
|
required this.leftEdgeShared,
|
|
required this.rightEdgeShared,
|
|
required super.child,
|
|
});
|
|
|
|
final bool topEdgeShared;
|
|
final bool bottomEdgeShared;
|
|
final bool leftEdgeShared;
|
|
final bool rightEdgeShared;
|
|
|
|
static ChatWorkspacePaneScope? maybeOf(BuildContext context) =>
|
|
context.dependOnInheritedWidgetOfExactType<ChatWorkspacePaneScope>();
|
|
|
|
@override
|
|
bool updateShouldNotify(ChatWorkspacePaneScope oldWidget) =>
|
|
topEdgeShared != oldWidget.topEdgeShared ||
|
|
bottomEdgeShared != oldWidget.bottomEdgeShared ||
|
|
leftEdgeShared != oldWidget.leftEdgeShared ||
|
|
rightEdgeShared != oldWidget.rightEdgeShared;
|
|
}
|
|
|
|
/// All chat views are stable siblings, independent of the split tree. Moving a
|
|
/// divider or adding/removing a neighbouring split never reparents a chat view.
|
|
class ChatWorkspace extends StatefulWidget {
|
|
const ChatWorkspace({
|
|
super.key,
|
|
required this.controller,
|
|
required this.paneBuilder,
|
|
this.onFocusPane,
|
|
this.shortcutsEnabled = true,
|
|
this.singlePane = false,
|
|
this.mobilePeeks = false,
|
|
this.animationStyle,
|
|
this.paneTitle,
|
|
this.onVisibleSessionIdsChanged,
|
|
});
|
|
final ChatWorkspaceController controller;
|
|
final ChatWorkspacePaneBuilder paneBuilder;
|
|
final bool shortcutsEnabled;
|
|
|
|
/// Keeps the retained layout while presenting only the active chat.
|
|
final bool singlePane;
|
|
final bool mobilePeeks;
|
|
final QuickSwitchAnimation? animationStyle;
|
|
final String Function(ChatWorkspacePane)? paneTitle;
|
|
final ValueChanged<Set<String>>? onVisibleSessionIdsChanged;
|
|
|
|
/// Keyboard navigation only: the owner can focus the target composer.
|
|
/// Pointer activation leaves text-selection and pointer focus alone.
|
|
final ValueChanged<ChatWorkspacePane>? onFocusPane;
|
|
|
|
@override
|
|
State<ChatWorkspace> createState() => _ChatWorkspaceState();
|
|
}
|
|
|
|
class _ChatWorkspaceState extends State<ChatWorkspace>
|
|
with SingleTickerProviderStateMixin {
|
|
final _focus = FocusNode(debugLabel: 'Chat workspace');
|
|
final Map<String, FocusScopeNode> _panes = {};
|
|
late final ChatPaneSwitchMotion _motion;
|
|
ChatWorkspaceGeometry? _geometry;
|
|
String? _lastActiveId;
|
|
Set<String> _visibleSessionIds = const {};
|
|
late final ChatPaneBackdrop _backdrop;
|
|
void _backdropChanged() {
|
|
if (mounted) setState(() {});
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_motion = ChatPaneSwitchMotion(vsync: this);
|
|
_motion.animation.addStatusListener(_motionSettled);
|
|
_backdrop = ChatPaneBackdrop(_motion.animation)
|
|
..addListener(_backdropChanged);
|
|
HardwareKeyboard.instance.addHandler(_key);
|
|
}
|
|
|
|
void _reportVisible(Set<String> ids) {
|
|
if (setEquals(ids, _visibleSessionIds)) return;
|
|
_visibleSessionIds = Set.unmodifiable(ids);
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted && setEquals(ids, _visibleSessionIds)) {
|
|
widget.onVisibleSessionIdsChanged?.call(_visibleSessionIds);
|
|
}
|
|
});
|
|
}
|
|
|
|
void _motionSettled(AnimationStatus status) {
|
|
if (status != AnimationStatus.completed) return;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) setState(() {});
|
|
});
|
|
}
|
|
|
|
bool _key(KeyEvent event) {
|
|
if (!widget.shortcutsEnabled ||
|
|
widget.controller.paneCount <= 1 ||
|
|
!_focus.hasFocus ||
|
|
event is! KeyDownEvent ||
|
|
primaryFocusIsTypingSurface()) {
|
|
return false;
|
|
}
|
|
final keyboard = HardwareKeyboard.instance;
|
|
final modifier =
|
|
keyboard.isControlPressed && !keyboard.isMetaPressed ||
|
|
defaultTargetPlatform == TargetPlatform.macOS &&
|
|
keyboard.isMetaPressed &&
|
|
!keyboard.isControlPressed;
|
|
if (!modifier || !keyboard.isShiftPressed || keyboard.isAltPressed) {
|
|
return false;
|
|
}
|
|
final direction = switch (event.logicalKey) {
|
|
LogicalKeyboardKey.arrowLeft => ChatPaneDirection.left,
|
|
LogicalKeyboardKey.arrowRight => ChatPaneDirection.right,
|
|
LogicalKeyboardKey.arrowUp => ChatPaneDirection.up,
|
|
LogicalKeyboardKey.arrowDown => ChatPaneDirection.down,
|
|
_ => null,
|
|
};
|
|
if (direction == null) return false;
|
|
final id = widget.controller.activePaneId;
|
|
final target = id == null ? null : _geometry?.neighbor(id, direction);
|
|
if (target != null) {
|
|
widget.controller.activate(target);
|
|
final pane = widget.controller.activePane!;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted || widget.controller.activePaneId != target) return;
|
|
if (widget.onFocusPane != null) {
|
|
widget.onFocusPane!(pane);
|
|
} else {
|
|
_panes[target]?.requestFocus();
|
|
}
|
|
});
|
|
}
|
|
// Consume a workspace navigation chord at an edge too: it must not select
|
|
// words in the current composer when there is no neighbouring pane.
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
HardwareKeyboard.instance.removeHandler(_key);
|
|
_backdrop.dispose();
|
|
_motion.animation.removeStatusListener(_motionSettled);
|
|
_motion.dispose();
|
|
_focus.dispose();
|
|
for (final node in _panes.values) {
|
|
node.dispose();
|
|
}
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => Focus(
|
|
focusNode: _focus,
|
|
child: ListenableBuilder(
|
|
listenable: Listenable.merge([
|
|
widget.controller,
|
|
QuickSwitchAnimationController.instance,
|
|
]),
|
|
builder: (context, _) => LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final root = widget.controller.root;
|
|
if (root == null) {
|
|
_reportVisible({});
|
|
return const SizedBox.shrink();
|
|
}
|
|
final geometry = ChatWorkspaceGeometry(root, constraints.biggest);
|
|
_geometry = geometry;
|
|
final collapsed = widget.singlePane || geometry.collapsed;
|
|
final panes = widget.controller.panes;
|
|
// Finish one joined loop before taking the latest requested role.
|
|
// Repeated taps/chords never tear the tape or duplicate a live chat.
|
|
final activeId =
|
|
_motion.isAnimating &&
|
|
panes.any((pane) => pane.id == _lastActiveId)
|
|
? _lastActiveId
|
|
: widget.controller.activePaneId;
|
|
final switching = _lastActiveId != activeId;
|
|
if (switching) {
|
|
_lastActiveId = activeId;
|
|
}
|
|
final mobile = widget.mobilePeeks && collapsed && activeId != null
|
|
? ChatMobilePanes(
|
|
panes: panes,
|
|
activeId: activeId,
|
|
size: constraints.biggest,
|
|
peekFraction: widget.controller.mobilePeekFraction,
|
|
)
|
|
: null;
|
|
var animate = !MediaQuery.disableAnimationsOf(context);
|
|
if (mobile != null &&
|
|
switching &&
|
|
animate &&
|
|
(widget.animationStyle ??
|
|
QuickSwitchAnimationController.instance.value) ==
|
|
QuickSwitchAnimation.tape) {
|
|
// The top exit requires a fresh picture. If painting is not ready,
|
|
// settle safely instead of moving the only live tree offscreen.
|
|
animate = _backdrop.capture(context);
|
|
}
|
|
if (mobile == null) {
|
|
_motion.rest();
|
|
} else {
|
|
_motion.layout(
|
|
rects: mobile.rects,
|
|
size: constraints.biggest,
|
|
activeId: activeId!,
|
|
switching: switching,
|
|
animate: animate,
|
|
style:
|
|
widget.animationStyle ??
|
|
QuickSwitchAnimationController.instance.value,
|
|
);
|
|
}
|
|
_reportVisible({
|
|
for (final pane in panes)
|
|
if (mobile != null
|
|
? mobile.rects.containsKey(pane.id)
|
|
: !collapsed || pane.id == activeId)
|
|
pane.sessionId,
|
|
});
|
|
final existing = panes.map((p) => p.id).toSet();
|
|
for (final id
|
|
in _panes.keys.where((id) => !existing.contains(id)).toList()) {
|
|
final node = _panes.remove(id)!;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => node.dispose());
|
|
}
|
|
bool sharesEdge(Rect pane, String edge) => geometry.dividers.any((
|
|
divider,
|
|
) {
|
|
final horizontalOverlap =
|
|
pane.left < divider.rect.right &&
|
|
pane.right > divider.rect.left;
|
|
final verticalOverlap =
|
|
pane.top < divider.rect.bottom &&
|
|
pane.bottom > divider.rect.top;
|
|
return switch (edge) {
|
|
'top' =>
|
|
(pane.top - divider.rect.bottom).abs() < .5 &&
|
|
horizontalOverlap,
|
|
'bottom' =>
|
|
(pane.bottom - divider.rect.top).abs() < .5 &&
|
|
horizontalOverlap,
|
|
'left' =>
|
|
(pane.left - divider.rect.right).abs() < .5 && verticalOverlap,
|
|
'right' =>
|
|
(pane.right - divider.rect.left).abs() < .5 && verticalOverlap,
|
|
_ => false,
|
|
};
|
|
});
|
|
final sharedEdges = <String, Set<String>>{
|
|
for (final entry in geometry.panes.entries)
|
|
entry.key: {
|
|
for (final edge in const ['top', 'bottom', 'left', 'right'])
|
|
if (!collapsed &&
|
|
mobile == null &&
|
|
sharesEdge(entry.value, edge))
|
|
edge,
|
|
},
|
|
};
|
|
return chatWorkspaceSurface(
|
|
context: context,
|
|
backdrop: _backdrop,
|
|
motion: _motion,
|
|
controller: widget.controller,
|
|
geometry: geometry,
|
|
mobile: mobile,
|
|
collapsed: collapsed,
|
|
size: constraints.biggest,
|
|
panes: panes,
|
|
paneView: (pane) =>
|
|
_paneView(pane, mobile, collapsed, activeId, sharedEdges),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
|
|
Widget _paneView(
|
|
ChatWorkspacePane pane,
|
|
ChatMobilePanes? mobile,
|
|
bool collapsed,
|
|
String? activeId,
|
|
Map<String, Set<String>> sharedEdges,
|
|
) {
|
|
final outgoing = _motion.isRetainedOutgoing(pane.id);
|
|
final peek = mobile?.peekId == pane.id || outgoing;
|
|
final active = pane.id == activeId;
|
|
return Offstage(
|
|
offstage: mobile != null
|
|
? !mobile.rects.containsKey(pane.id) && !outgoing
|
|
: collapsed && !active,
|
|
child: ExcludeFocus(
|
|
excluding: collapsed && !active,
|
|
child: ChatMobilePaneViewport(
|
|
peek: peek,
|
|
active: active,
|
|
barReveal: _motion.revealFor(pane.id),
|
|
fixedChromeLayout: _motion.isAnimating && !_motion.isTape,
|
|
label: widget.paneTitle?.call(pane) ?? pane.sessionId,
|
|
paneId: pane.id,
|
|
onActivate: () {
|
|
FocusManager.instance.primaryFocus?.unfocus();
|
|
widget.controller.activate(pane.id);
|
|
},
|
|
child: FocusScope(
|
|
node: _panes.putIfAbsent(
|
|
pane.id,
|
|
() => FocusScopeNode(debugLabel: pane.id),
|
|
),
|
|
onFocusChange: (focused) {
|
|
if (focused && mobile == null) {
|
|
widget.controller.activate(pane.id);
|
|
}
|
|
},
|
|
child: Listener(
|
|
behavior: HitTestBehavior.translucent,
|
|
// A press in the peek scrolls or taps; the tap alone activates.
|
|
onPointerDown: peek
|
|
? null
|
|
: (_) => widget.controller.activate(pane.id),
|
|
child: Semantics(
|
|
identifier: 'chat-pane-${pane.id}',
|
|
container: true,
|
|
sortKey: mobile == null ? null : const OrdinalSortKey(0),
|
|
child: ChatWorkspacePaneScope(
|
|
topEdgeShared: sharedEdges[pane.id]?.contains('top') ?? false,
|
|
bottomEdgeShared:
|
|
sharedEdges[pane.id]?.contains('bottom') ?? false,
|
|
leftEdgeShared:
|
|
sharedEdges[pane.id]?.contains('left') ?? false,
|
|
rightEdgeShared:
|
|
sharedEdges[pane.id]?.contains('right') ?? false,
|
|
child: Builder(
|
|
builder: (paneContext) =>
|
|
widget.paneBuilder(paneContext, pane, active),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|