Files
pipelines/app/test/third_party_xterm/mouse_view_test.dart
T
alan 9b90806093 xterm: fix mouse wheel, drag, motion and middle-button reporting
- Wheel buttons were encoded as 68/69 (64 + 4/5, i.e. Shift + wheel down/
  wheel right); they are 64 up and 65 down.
- Wheel, click and drag positions use the view's own screen cell: the
  wheel used the window position, and clicks used the buffer row
  (counting scrollback) instead of the screen row. Default encoding sent
  the row one too high.
- A new TerminalMouseListener reports mouse presses, releases, drags
  (1002) and motion (1003, also without a button) from pointer events,
  with Ctrl/Alt modifiers. Before, a drag became a text selection and
  motion was never sent. Holding Shift at the press leaves the pointer to
  selection while the program owns the mouse.
- The middle button's press and release were reported as the right
  button. SGR releases name the released button.
- With mouse tracking on, the wheel is reported on the main screen too.
- TerminalController reports every pointer input kind by default; the
  program's mouse mode decides what is sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:36:32 +00:00

160 lines
4.6 KiB
Dart

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
/// A TerminalView placed away from the window's origin, so a report that
/// used window coordinates instead of the view's own would be wrong.
class _Harness {
_Harness(this.tester);
final WidgetTester tester;
final terminal = Terminal();
final controller = TerminalController();
final replies = <String>[];
Future<void> pump() async {
terminal.onOutput = replies.add;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Padding(
padding: const EdgeInsets.only(left: 100, top: 60),
child: SizedBox(
width: 400,
height: 200,
child: TerminalView(
terminal,
controller: controller,
hardwareKeyboardOnly: true,
),
),
),
),
),
);
}
TerminalViewState get _view =>
tester.state<TerminalViewState>(find.byType(TerminalView));
/// The global centre of zero-based screen cell ([x], [y]).
Offset cell(int x, int y) {
final render = _view.renderTerminal;
final size = render.cellSize;
return render.localToGlobal(
Offset((x + 0.5) * size.width, (y + 0.5) * size.height),
);
}
double get lineHeight => _view.renderTerminal.lineHeight;
String take() {
final all = replies.join();
replies.clear();
return all;
}
}
void main() {
testWidgets('a left drag with 1002 is reported, not selected', (
tester,
) async {
final h = _Harness(tester);
await h.pump();
h.terminal.write('\x1b[?1002h\x1b[?1006h');
await tester.pump();
final gesture = await tester.startGesture(
h.cell(1, 1),
kind: PointerDeviceKind.mouse,
);
await gesture.moveTo(h.cell(4, 1));
await gesture.moveTo(h.cell(6, 2));
await gesture.up();
await tester.pumpAndSettle();
expect(h.take(), '\x1b[<0;2;2M\x1b[<32;5;2M\x1b[<32;7;3M\x1b[<0;7;3m');
expect(h.controller.selection, isNull);
});
testWidgets('1003 reports plain motion; 1000 does not', (tester) async {
final h = _Harness(tester);
await h.pump();
h.terminal.write('\x1b[?1003h\x1b[?1006h');
final gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer(location: h.cell(0, 0));
await gesture.moveTo(h.cell(3, 2));
expect(h.take(), '\x1b[<35;4;3M');
h.terminal.write('\x1b[?1003l\x1b[?1000h');
await gesture.moveTo(h.cell(5, 2));
expect(h.take(), '');
await gesture.removePointer();
});
testWidgets('a middle click is reported as middle press and release', (
tester,
) async {
final h = _Harness(tester);
await h.pump();
h.terminal.write('\x1b[?1000h\x1b[?1006h');
final gesture = await tester.startGesture(
h.cell(2, 0),
kind: PointerDeviceKind.mouse,
buttons: kMiddleMouseButton,
);
await gesture.up();
await tester.pumpAndSettle();
expect(h.take(), '\x1b[<1;3;1M\x1b[<1;3;1m');
});
testWidgets('Shift+drag selects text even while the program has the mouse', (
tester,
) async {
final h = _Harness(tester);
await h.pump();
h.terminal.write('hello world\x1b[?1002h\x1b[?1006h');
await tester.pump();
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
final gesture = await tester.startGesture(
h.cell(0, 0),
kind: PointerDeviceKind.mouse,
);
await gesture.moveTo(h.cell(2, 0));
await gesture.moveTo(h.cell(4, 0));
await gesture.up();
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
await tester.pumpAndSettle();
expect(h.take(), '');
final selection = h.controller.selection;
expect(selection, isNotNull);
expect(h.terminal.buffer.getText(selection), startsWith('hell'));
});
testWidgets('the wheel reports 65 down / 64 up at the view cell', (
tester,
) async {
final h = _Harness(tester);
await h.pump();
h.terminal.write('\x1b[?1049h\x1b[?1000h\x1b[?1006h');
await tester.pump();
final pointer = TestPointer(1, PointerDeviceKind.mouse);
final at = h.cell(3, 1);
await tester.sendEventToBinding(pointer.hover(at));
await tester.sendEventToBinding(
pointer.scroll(Offset(0, h.lineHeight * 2)),
);
await tester.pump();
expect(h.take(), '\x1b[<65;4;2M\x1b[<65;4;2M');
await tester.sendEventToBinding(pointer.scroll(Offset(0, -h.lineHeight)));
await tester.pump();
expect(h.take(), '\x1b[<64;4;2M');
});
}