Add resizable split chat workspace primitives
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'chat_workspace_controller.dart';
|
||||
import 'chat_workspace_divider.dart';
|
||||
import 'chat_workspace_geometry.dart';
|
||||
|
||||
export 'chat_workspace_controller.dart';
|
||||
export 'chat_workspace_geometry.dart' show ChatPaneDirection;
|
||||
|
||||
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,
|
||||
});
|
||||
final ChatWorkspaceController controller;
|
||||
final ChatWorkspacePaneBuilder paneBuilder;
|
||||
final bool shortcutsEnabled;
|
||||
|
||||
/// 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> {
|
||||
final _focus = FocusNode(debugLabel: 'Chat workspace');
|
||||
final Map<String, FocusScopeNode> _panes = {};
|
||||
ChatWorkspaceGeometry? _geometry;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
HardwareKeyboard.instance.addHandler(_key);
|
||||
}
|
||||
|
||||
bool _key(KeyEvent event) {
|
||||
if (!widget.shortcutsEnabled ||
|
||||
!_focus.hasFocus ||
|
||||
event is! KeyDownEvent) {
|
||||
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);
|
||||
_focus.dispose();
|
||||
for (final node in _panes.values) {
|
||||
node.dispose();
|
||||
}
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Focus(
|
||||
focusNode: _focus,
|
||||
child: ListenableBuilder(
|
||||
listenable: widget.controller,
|
||||
builder: (context, _) => LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final root = widget.controller.root;
|
||||
if (root == null) return const SizedBox.shrink();
|
||||
final geometry = ChatWorkspaceGeometry(root, constraints.biggest);
|
||||
_geometry = geometry;
|
||||
final panes = widget.controller.panes;
|
||||
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());
|
||||
}
|
||||
return Stack(
|
||||
fit: StackFit.expand,
|
||||
clipBehavior: Clip.hardEdge,
|
||||
children: [
|
||||
for (final pane in panes)
|
||||
Positioned.fromRect(
|
||||
key: ValueKey('chat-workspace-${pane.id}'),
|
||||
rect: geometry.collapsed
|
||||
? Offset.zero & constraints.biggest
|
||||
: geometry.panes[pane.id]!,
|
||||
child: Offstage(
|
||||
offstage:
|
||||
geometry.collapsed &&
|
||||
pane.id != widget.controller.activePaneId,
|
||||
child: ExcludeFocus(
|
||||
excluding:
|
||||
geometry.collapsed &&
|
||||
pane.id != widget.controller.activePaneId,
|
||||
child: FocusScope(
|
||||
node: _panes.putIfAbsent(
|
||||
pane.id,
|
||||
() => FocusScopeNode(debugLabel: pane.id),
|
||||
),
|
||||
onFocusChange: (focused) {
|
||||
if (focused) widget.controller.activate(pane.id);
|
||||
},
|
||||
child: Listener(
|
||||
behavior: HitTestBehavior.translucent,
|
||||
onPointerDown: (_) =>
|
||||
widget.controller.activate(pane.id),
|
||||
child: Semantics(
|
||||
identifier: 'chat-pane-${pane.id}',
|
||||
container: true,
|
||||
child: widget.paneBuilder(
|
||||
context,
|
||||
pane,
|
||||
pane.id == widget.controller.activePaneId,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (!geometry.collapsed)
|
||||
for (final divider in geometry.dividers)
|
||||
Positioned.fromRect(
|
||||
key: ValueKey('chat-workspace-${divider.split.id}'),
|
||||
rect: divider.rect,
|
||||
child: ChatWorkspaceDivider(
|
||||
geometry: divider,
|
||||
onChanged: (ratio) =>
|
||||
widget.controller.resize(divider.split.id, ratio),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user