Enable floating chat window resizing everywhere
Install directional hit regions in the shared window layer so floating chats can be moved and resized on every destination. Keep geometry updates in the shared manager and cover directional resizing with window host regressions.
This commit is contained in:
@@ -111,6 +111,59 @@ Rect fitWindowFrame(Rect rect, Size bounds, Size minSize) {
|
||||
);
|
||||
}
|
||||
|
||||
enum WindowResizeEdge { left, right, top, bottom }
|
||||
|
||||
/// Resizes while keeping the opposite edge anchored, then applies the same
|
||||
/// bounds and minimum-size rules as a moved window.
|
||||
Rect resizeWindowFrame(
|
||||
Rect start,
|
||||
Offset delta, {
|
||||
required bool left,
|
||||
required bool right,
|
||||
required bool top,
|
||||
required bool bottom,
|
||||
required Size bounds,
|
||||
required Size minSize,
|
||||
}) {
|
||||
var result = Rect.fromLTRB(
|
||||
left ? start.left + delta.dx : start.left,
|
||||
top ? start.top + delta.dy : start.top,
|
||||
right ? start.right + delta.dx : start.right,
|
||||
bottom ? start.bottom + delta.dy : start.bottom,
|
||||
);
|
||||
if (result.width < minSize.width) {
|
||||
result = left
|
||||
? Rect.fromLTRB(
|
||||
start.right - minSize.width,
|
||||
result.top,
|
||||
start.right,
|
||||
result.bottom,
|
||||
)
|
||||
: Rect.fromLTRB(
|
||||
start.left,
|
||||
result.top,
|
||||
start.left + minSize.width,
|
||||
result.bottom,
|
||||
);
|
||||
}
|
||||
if (result.height < minSize.height) {
|
||||
result = top
|
||||
? Rect.fromLTRB(
|
||||
result.left,
|
||||
start.bottom - minSize.height,
|
||||
result.right,
|
||||
start.bottom,
|
||||
)
|
||||
: Rect.fromLTRB(
|
||||
result.left,
|
||||
start.top,
|
||||
result.right,
|
||||
start.top + minSize.height,
|
||||
);
|
||||
}
|
||||
return fitWindowFrame(result, bounds, minSize);
|
||||
}
|
||||
|
||||
/// [value] rounded to a whole logical pixel and kept within
|
||||
/// `[minimum, limit]`.
|
||||
///
|
||||
|
||||
@@ -179,3 +179,32 @@ class WindowResizeCorner extends StatelessWidget {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// A transparent resize hit region. The visible lower-right affordance
|
||||
/// remains [WindowResizeCorner]; the other regions deliberately add no chrome.
|
||||
class WindowResizeHandle extends StatelessWidget {
|
||||
const WindowResizeHandle({
|
||||
super.key,
|
||||
required this.cursor,
|
||||
required this.onResize,
|
||||
required this.onResizeEnd,
|
||||
this.child,
|
||||
});
|
||||
|
||||
final MouseCursor cursor;
|
||||
final ValueChanged<Offset> onResize;
|
||||
final VoidCallback onResizeEnd;
|
||||
final Widget? child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => MouseRegion(
|
||||
cursor: cursor,
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
dragStartBehavior: DragStartBehavior.down,
|
||||
onPanUpdate: (details) => onResize(details.delta),
|
||||
onPanEnd: (_) => onResizeEnd(),
|
||||
child: child ?? const SizedBox.expand(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -159,25 +159,30 @@ class _WindowLayerState extends State<WindowLayer>
|
||||
widget.manager.commit(_id);
|
||||
}
|
||||
|
||||
void _resize(Offset delta) {
|
||||
void _resize(Offset delta, Set<WindowResizeEdge> edges) {
|
||||
final start = _dragStart ??= _current;
|
||||
_dragTotal += delta;
|
||||
final left = edges.contains(WindowResizeEdge.left);
|
||||
final top = edges.contains(WindowResizeEdge.top);
|
||||
widget.manager.setFrame(
|
||||
_id,
|
||||
fitWindowFrame(
|
||||
Rect.fromLTWH(
|
||||
start.left,
|
||||
start.top,
|
||||
start.width + _dragTotal.dx,
|
||||
start.height + _dragTotal.dy,
|
||||
),
|
||||
widget.bounds,
|
||||
widget.kind.minSize,
|
||||
resizeWindowFrame(
|
||||
start,
|
||||
_dragTotal,
|
||||
left: left,
|
||||
right: edges.contains(WindowResizeEdge.right),
|
||||
top: top,
|
||||
bottom: edges.contains(WindowResizeEdge.bottom),
|
||||
bounds: widget.bounds,
|
||||
minSize: widget.kind.minSize,
|
||||
),
|
||||
save: false,
|
||||
);
|
||||
}
|
||||
|
||||
void _resizeCorner(Offset delta, Set<WindowResizeEdge> edges) =>
|
||||
_resize(delta, edges);
|
||||
|
||||
void _resizeEnded() {
|
||||
_endDrag();
|
||||
widget.manager.commit(_id);
|
||||
@@ -290,15 +295,7 @@ class _WindowLayerState extends State<WindowLayer>
|
||||
),
|
||||
),
|
||||
),
|
||||
if (!entry.maximized)
|
||||
Positioned(
|
||||
right: widget.bounds.width - frame.right,
|
||||
bottom: widget.bounds.height - frame.bottom,
|
||||
child: WindowResizeCorner(
|
||||
onResize: _resize,
|
||||
onResizeEnd: _resizeEnded,
|
||||
),
|
||||
),
|
||||
if (!entry.maximized) ..._resizeHandles(frame),
|
||||
Positioned.fromRect(rect: frame, child: const WindowOutline()),
|
||||
],
|
||||
);
|
||||
@@ -352,6 +349,107 @@ class _WindowLayerState extends State<WindowLayer>
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _resizeHandles(Rect frame) {
|
||||
const corner = 16.0;
|
||||
const edge = 10.0;
|
||||
Widget handle({
|
||||
required Rect rect,
|
||||
required Set<WindowResizeEdge> edges,
|
||||
required MouseCursor cursor,
|
||||
Widget? child,
|
||||
Key? key,
|
||||
}) => Positioned.fromRect(
|
||||
rect: rect,
|
||||
child: WindowResizeHandle(
|
||||
key: key,
|
||||
cursor: cursor,
|
||||
onResize: (delta) => _resizeCorner(delta, edges),
|
||||
onResizeEnd: _resizeEnded,
|
||||
child: child,
|
||||
),
|
||||
);
|
||||
|
||||
return [
|
||||
handle(
|
||||
key: const ValueKey('window-resize-top-left'),
|
||||
rect: Rect.fromLTWH(frame.left, frame.top, corner, corner),
|
||||
edges: const {WindowResizeEdge.left, WindowResizeEdge.top},
|
||||
cursor: SystemMouseCursors.resizeUpLeft,
|
||||
),
|
||||
handle(
|
||||
key: const ValueKey('window-resize-top-right'),
|
||||
rect: Rect.fromLTWH(frame.right - corner, frame.top, corner, corner),
|
||||
edges: const {WindowResizeEdge.right, WindowResizeEdge.top},
|
||||
cursor: SystemMouseCursors.resizeUpRight,
|
||||
),
|
||||
handle(
|
||||
key: const ValueKey('window-resize-bottom-left'),
|
||||
rect: Rect.fromLTWH(frame.left, frame.bottom - corner, corner, corner),
|
||||
edges: const {WindowResizeEdge.left, WindowResizeEdge.bottom},
|
||||
cursor: SystemMouseCursors.resizeUpRight,
|
||||
),
|
||||
Positioned.fromRect(
|
||||
rect: Rect.fromLTWH(
|
||||
frame.right - corner,
|
||||
frame.bottom - corner,
|
||||
corner,
|
||||
corner,
|
||||
),
|
||||
child: WindowResizeCorner(
|
||||
onResize: (delta) => _resizeCorner(delta, const {
|
||||
WindowResizeEdge.right,
|
||||
WindowResizeEdge.bottom,
|
||||
}),
|
||||
onResizeEnd: _resizeEnded,
|
||||
),
|
||||
),
|
||||
handle(
|
||||
key: const ValueKey('window-resize-left'),
|
||||
rect: Rect.fromLTWH(
|
||||
frame.left,
|
||||
frame.top + corner,
|
||||
edge,
|
||||
frame.height - corner * 2,
|
||||
),
|
||||
edges: const {WindowResizeEdge.left},
|
||||
cursor: SystemMouseCursors.resizeLeftRight,
|
||||
),
|
||||
handle(
|
||||
key: const ValueKey('window-resize-right'),
|
||||
rect: Rect.fromLTWH(
|
||||
frame.right - edge,
|
||||
frame.top + corner,
|
||||
edge,
|
||||
frame.height - corner * 2,
|
||||
),
|
||||
edges: const {WindowResizeEdge.right},
|
||||
cursor: SystemMouseCursors.resizeLeftRight,
|
||||
),
|
||||
handle(
|
||||
key: const ValueKey('window-resize-top'),
|
||||
rect: Rect.fromLTWH(
|
||||
frame.left + corner,
|
||||
frame.top,
|
||||
frame.width - corner * 2,
|
||||
edge,
|
||||
),
|
||||
edges: const {WindowResizeEdge.top},
|
||||
cursor: SystemMouseCursors.resizeUpDown,
|
||||
),
|
||||
handle(
|
||||
key: const ValueKey('window-resize-bottom'),
|
||||
rect: Rect.fromLTWH(
|
||||
frame.left + corner,
|
||||
frame.bottom - edge,
|
||||
frame.width - corner * 2,
|
||||
edge,
|
||||
),
|
||||
edges: const {WindowResizeEdge.bottom},
|
||||
cursor: SystemMouseCursors.resizeUpDown,
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/// The kind's content, rebuilt with the window's current entry.
|
||||
|
||||
@@ -130,6 +130,72 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'all resize edges and corners change the rendered frame and manager entry',
|
||||
(tester) async {
|
||||
final entry = manager.open(
|
||||
'probe',
|
||||
'a',
|
||||
frame: const Rect.fromLTWH(300, 200, 360, 420),
|
||||
);
|
||||
await pumpProbe(tester, manager, destination: 1);
|
||||
|
||||
Future<void> drag(String key, Offset delta) async {
|
||||
await tester.drag(find.byKey(ValueKey(key)), delta);
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
windowRect(tester, 'Probe a'),
|
||||
manager.byId(entry.id)!.frame,
|
||||
reason: '$key did not rebuild from the manager frame',
|
||||
);
|
||||
}
|
||||
|
||||
await drag('window-resize-left', const Offset(20, 0));
|
||||
expect(windowRect(tester, 'Probe a').left, 320);
|
||||
await drag('window-resize-right', const Offset(20, 0));
|
||||
expect(windowRect(tester, 'Probe a').width, 360);
|
||||
await drag('window-resize-top', const Offset(0, 20));
|
||||
expect(windowRect(tester, 'Probe a').top, 220);
|
||||
await drag('window-resize-bottom', const Offset(0, 20));
|
||||
expect(windowRect(tester, 'Probe a').height, 420);
|
||||
|
||||
await drag('window-resize-top-left', const Offset(20, 20));
|
||||
expect(windowRect(tester, 'Probe a').topLeft, const Offset(340, 240));
|
||||
await drag('window-resize-top-right', const Offset(20, 20));
|
||||
expect(windowRect(tester, 'Probe a').top, 260);
|
||||
await drag('window-resize-bottom-left', const Offset(20, 20));
|
||||
expect(windowRect(tester, 'Probe a').left, 360);
|
||||
await drag('window-resize', const Offset(20, 20));
|
||||
expect(windowRect(tester, 'Probe a').bottomRight, const Offset(720, 680));
|
||||
|
||||
await drag('window-resize-left', const Offset(1000, 0));
|
||||
expect(windowRect(tester, 'Probe a').width, 240);
|
||||
expect(manager.byId(entry.id)!.frame.width, 240);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('geometry persists when switching away from and back to Chat', (
|
||||
tester,
|
||||
) async {
|
||||
final entry = manager.open('probe', 'a');
|
||||
await pumpProbe(tester, manager, destination: 1);
|
||||
await tester.drag(
|
||||
find.byKey(const ValueKey('window-title')),
|
||||
const Offset(48, 24),
|
||||
);
|
||||
await tester.drag(
|
||||
find.byKey(const ValueKey('window-resize-right')),
|
||||
const Offset(32, 0),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
final changed = manager.byId(entry.id)!.frame;
|
||||
|
||||
await pumpProbe(tester, manager, destination: 0);
|
||||
expect(windowRect(tester, 'Probe a'), changed);
|
||||
await pumpProbe(tester, manager, destination: 1);
|
||||
expect(windowRect(tester, 'Probe a'), changed);
|
||||
});
|
||||
|
||||
testWidgets('maximise fills the app and restores; double click toggles', (
|
||||
tester,
|
||||
) async {
|
||||
|
||||
Reference in New Issue
Block a user