d77b36f56f
# Conflicts: # app/lib/features/chat/workspace/chat_workspace.dart
521 lines
18 KiB
Dart
521 lines
18 KiB
Dart
import 'package:flutter/semantics.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import '../../../ui/floating/floating_layer.dart';
|
|
import '../../../ui/motion/motion.dart';
|
|
import '../../../ui/motion/quick_switch_animation.dart';
|
|
import 'chat_center_stage_layout.dart';
|
|
import 'chat_center_stage_motion.dart';
|
|
import 'chat_center_stage_strip.dart';
|
|
import 'chat_float_binding.dart';
|
|
import 'chat_float_host.dart';
|
|
import 'chat_workspace_controller.dart';
|
|
import 'chat_workspace_geometry.dart';
|
|
import 'chat_mobile_panes.dart';
|
|
import 'chat_pane_move_motion.dart';
|
|
import 'chat_pane_move_shortcuts.dart';
|
|
import 'chat_pane_switch_motion.dart';
|
|
import 'chat_pane_backdrop.dart';
|
|
import 'chat_workspace_pane_scope.dart';
|
|
import 'chat_workspace_surface.dart';
|
|
import '../../../ui/input/keyboard_owner.dart';
|
|
|
|
export 'chat_workspace_controller.dart';
|
|
export 'chat_workspace_geometry.dart' show ChatPaneDirection;
|
|
export 'chat_workspace_pane_scope.dart';
|
|
|
|
typedef ChatWorkspacePaneBuilder =
|
|
Widget Function(BuildContext context, ChatWorkspacePane pane, bool active);
|
|
|
|
/// 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,
|
|
this.centerStage,
|
|
this.paneColor,
|
|
this.floatFrames,
|
|
});
|
|
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;
|
|
|
|
/// While true on a wide window, the active chat takes center stage and the
|
|
/// others line up beside it (see [ChatCenterStageLayout]).
|
|
final ValueListenable<bool>? centerStage;
|
|
|
|
/// A pane's chat colour, for its strip beside the stage.
|
|
final Color? Function(ChatWorkspacePane)? paneColor;
|
|
|
|
/// Offers floating chats (see [ChatFloatHost]) in these frames; null for a
|
|
/// workspace without them. They show only while the panes are not
|
|
/// collapsed.
|
|
final ChatFloatFrames? floatFrames;
|
|
|
|
/// 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 TickerProviderStateMixin {
|
|
final _focus = FocusNode(debugLabel: 'Chat workspace');
|
|
final Map<String, FocusScopeNode> _panes = {};
|
|
|
|
/// Each pane's view keeps its State when it moves into or out of the
|
|
/// floating window.
|
|
final Map<String, GlobalKey> _views = {};
|
|
late final ChatPaneSwitchMotion _motion;
|
|
late final ChatCenterStageMotion _stage = ChatCenterStageMotion(vsync: this);
|
|
late final ChatPaneMoveMotion _move = ChatPaneMoveMotion(vsync: this);
|
|
int _moveSerial = 0;
|
|
bool _tiled = false;
|
|
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 chord = chatPaneChord(event, HardwareKeyboard.instance);
|
|
if (chord == null) return false;
|
|
final (kind, direction) = chord;
|
|
final id = widget.controller.activePaneId;
|
|
if (kind == ChatPaneChord.move) {
|
|
// Only tiled panes move; the phone's one-at-a-time layout keeps the keys.
|
|
if (!_tiled) return false;
|
|
final stage = _stage.stage;
|
|
final target = id == null
|
|
? null
|
|
: stage != null
|
|
? stage.neighbor(id, direction)
|
|
: _geometry?.adjacent(id, direction);
|
|
if (target != null) widget.controller.swap(id!, target);
|
|
// Consumed at an outer edge too, like focusing.
|
|
return true;
|
|
}
|
|
final stage = _stage.stage;
|
|
final target = id == null
|
|
? null
|
|
: stage != null
|
|
? stage.neighbor(id, direction)
|
|
: _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();
|
|
_stage.dispose();
|
|
_move.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,
|
|
?widget.centerStage,
|
|
]),
|
|
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);
|
|
final previous = _geometry;
|
|
_geometry = geometry;
|
|
final collapsed = widget.singlePane || geometry.collapsed;
|
|
final panes = widget.controller.panes;
|
|
final tiled = !collapsed && panes.length > 1;
|
|
if (_moveSerial != widget.controller.moveSerial) {
|
|
_moveSerial = widget.controller.moveSerial;
|
|
// Glide the swapped pair from their previous tiles.
|
|
_move.start(
|
|
tiled && _tiled && previous != null
|
|
? {
|
|
for (final id in widget.controller.movedPaneIds)
|
|
id: ?previous.panes[id],
|
|
}
|
|
: const {},
|
|
Motion.of(context, Motion.standard),
|
|
);
|
|
}
|
|
_tiled = tiled;
|
|
final moveTargets = <String, Map<ChatPaneDirection, String>>{
|
|
if (tiled)
|
|
for (final pane in panes)
|
|
pane.id: {
|
|
for (final direction in ChatPaneDirection.values)
|
|
direction: ?geometry.adjacent(pane.id, direction),
|
|
},
|
|
};
|
|
// 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,
|
|
);
|
|
}
|
|
final engaged = !collapsed && mobile == null;
|
|
_stage.layout(
|
|
engaged: engaged,
|
|
split: geometry.panes,
|
|
stage: engaged && widget.centerStage?.value == true
|
|
? ChatCenterStageLayout.of(constraints.biggest, [
|
|
for (final pane in panes) pane.id,
|
|
], activeId)
|
|
: null,
|
|
animate: !MediaQuery.disableAnimationsOf(context),
|
|
);
|
|
_reportVisible({
|
|
for (final pane in panes)
|
|
if (mobile != null
|
|
? mobile.rects.containsKey(pane.id)
|
|
: !collapsed || pane.id == activeId)
|
|
pane.sessionId,
|
|
});
|
|
final floating = widget.floatFrames == null
|
|
? const <ChatWorkspacePane>[]
|
|
: widget.controller.floatingPanes;
|
|
final existing = {
|
|
...panes.map((p) => p.id),
|
|
...floating.map((p) => p.id),
|
|
};
|
|
_views.removeWhere((id, _) => !existing.contains(id));
|
|
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 &&
|
|
_stage.stage == null &&
|
|
sharesEdge(entry.value, edge))
|
|
edge,
|
|
},
|
|
};
|
|
final surface = chatWorkspaceSurface(
|
|
context: context,
|
|
backdrop: _backdrop,
|
|
motion: _motion,
|
|
stage: _stage,
|
|
stageStrip: _strip,
|
|
move: _move,
|
|
controller: widget.controller,
|
|
geometry: geometry,
|
|
mobile: mobile,
|
|
collapsed: collapsed,
|
|
size: constraints.biggest,
|
|
panes: panes,
|
|
paneView: (pane) => _paneView(
|
|
pane,
|
|
mobile,
|
|
collapsed,
|
|
activeId,
|
|
sharedEdges,
|
|
moveTargets[pane.id],
|
|
),
|
|
);
|
|
final frames = widget.floatFrames;
|
|
if (frames == null) return surface;
|
|
return ChatFloatHost(
|
|
controller: widget.controller,
|
|
frames: frames,
|
|
enabled: !widget.singlePane,
|
|
title: (pane) => widget.paneTitle?.call(pane) ?? pane.sessionId,
|
|
content: _floatView,
|
|
scope: (pane) => _panes[pane.id],
|
|
focusedPane: () => widget.controller.panes
|
|
.where((pane) => _panes[pane.id]?.hasFocus ?? false)
|
|
.firstOrNull,
|
|
onFocusPane: widget.onFocusPane,
|
|
child: surface,
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
|
|
Widget _strip(ChatWorkspacePane pane) => ChatCenterStageStrip(
|
|
paneId: pane.id,
|
|
title: widget.paneTitle?.call(pane) ?? pane.sessionId,
|
|
color: widget.paneColor?.call(pane),
|
|
onSelected: () {
|
|
FocusManager.instance.primaryFocus?.unfocus();
|
|
widget.controller.activate(pane.id);
|
|
// Choosing a strip is choosing that chat: its composer takes the
|
|
// keyboard once it is on stage.
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted || widget.controller.activePaneId != pane.id) return;
|
|
widget.onFocusPane?.call(pane);
|
|
});
|
|
},
|
|
);
|
|
|
|
Widget _paneView(
|
|
ChatWorkspacePane pane,
|
|
ChatMobilePanes? mobile,
|
|
bool collapsed,
|
|
String? activeId,
|
|
Map<String, Set<String>> sharedEdges,
|
|
Map<ChatPaneDirection, String>? moveTargets,
|
|
) {
|
|
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: _paneContent(
|
|
pane,
|
|
active: active,
|
|
peek: peek,
|
|
mobile: mobile != null,
|
|
edges: sharedEdges[pane.id] ?? const {},
|
|
moveTargets: moveTargets,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// A floating chat's view: the same keyed content as in the tiling, so
|
|
/// floating and docking move it without rebuilding the chat. Its focus
|
|
/// hangs from the shell's [FloatingFocusRoot], so it keeps the keyboard
|
|
/// while another destination (which excludes this one) shows.
|
|
Widget _floatView(ChatWorkspacePane pane) => _paneContent(
|
|
pane,
|
|
active: true,
|
|
peek: false,
|
|
mobile: false,
|
|
edges: {},
|
|
focusParent: FloatingFocusRoot.maybeOf(context),
|
|
);
|
|
|
|
Widget _paneContent(
|
|
ChatWorkspacePane pane, {
|
|
required bool active,
|
|
required bool peek,
|
|
required bool mobile,
|
|
required Set<String> edges,
|
|
Map<ChatPaneDirection, String>? moveTargets,
|
|
FocusNode? focusParent,
|
|
}) {
|
|
var actionBarPointerDown = false;
|
|
bool preservesStageForActionBar() =>
|
|
_stage.stage != null && _stage.stage!.stageId != pane.id;
|
|
return KeyedSubtree(
|
|
key: _views.putIfAbsent(pane.id, () => GlobalKey(debugLabel: pane.id)),
|
|
child: FocusScope(
|
|
parentNode: focusParent,
|
|
node: _panes.putIfAbsent(
|
|
pane.id,
|
|
() => FocusScopeNode(debugLabel: pane.id),
|
|
),
|
|
onFocusChange: (focused) {
|
|
final inActionBar = ChatWorkspacePaneActionBar.contains(
|
|
primaryFocus?.context,
|
|
);
|
|
if (focused &&
|
|
!mobile &&
|
|
(!inActionBar || !preservesStageForActionBar())) {
|
|
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
|
|
: (_) {
|
|
if (!actionBarPointerDown || !preservesStageForActionBar()) {
|
|
widget.controller.activate(pane.id);
|
|
}
|
|
},
|
|
child: Semantics(
|
|
identifier: 'chat-pane-${pane.id}',
|
|
container: true,
|
|
sortKey: mobile ? const OrdinalSortKey(0) : null,
|
|
child: ChatWorkspacePaneScope(
|
|
topEdgeShared: edges.contains('top'),
|
|
bottomEdgeShared: edges.contains('bottom'),
|
|
leftEdgeShared: edges.contains('left'),
|
|
rightEdgeShared: edges.contains('right'),
|
|
movable: moveTargets != null,
|
|
moveTargets: moveTargets ?? const {},
|
|
onActionBarPointerDown: () => actionBarPointerDown = true,
|
|
onActionBarPointerUp: () => actionBarPointerDown = false,
|
|
child: Builder(
|
|
builder: (paneContext) =>
|
|
widget.paneBuilder(paneContext, pane, active),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|