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>
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:xterm/xterm.dart';
|
||||
|
||||
import 'xterm_fixture.dart';
|
||||
|
||||
void main() {
|
||||
const down = TerminalMouseButtonState.down;
|
||||
const up = TerminalMouseButtonState.up;
|
||||
const at = CellOffset(4, 2); // column 5, row 3 one-based
|
||||
|
||||
String report(
|
||||
String modes,
|
||||
TerminalMouseButton button,
|
||||
TerminalMouseButtonState state, {
|
||||
bool motion = false,
|
||||
bool ctrl = false,
|
||||
}) {
|
||||
final t = FixtureTerminal()..feed(modes);
|
||||
t.terminal.mouseInput(button, state, at, motion: motion, ctrl: ctrl);
|
||||
return t.takeReplies();
|
||||
}
|
||||
|
||||
test('SGR wheel is 64 up and 65 down', () {
|
||||
const sgr = '\x1b[?1000h\x1b[?1006h';
|
||||
expect(report(sgr, TerminalMouseButton.wheelUp, down), '\x1b[<64;5;3M');
|
||||
expect(report(sgr, TerminalMouseButton.wheelDown, down), '\x1b[<65;5;3M');
|
||||
expect(report(sgr, TerminalMouseButton.wheelUp, up), '');
|
||||
});
|
||||
|
||||
test('default encoding: 32 + code, 32 + column, 32 + row', () {
|
||||
const normal = '\x1b[?1000h';
|
||||
expect(
|
||||
report(normal, TerminalMouseButton.left, down),
|
||||
'\x1b[M\x20\x25\x23',
|
||||
);
|
||||
expect(report(normal, TerminalMouseButton.left, up), '\x1b[M\x23\x25\x23');
|
||||
expect(
|
||||
report(normal, TerminalMouseButton.wheelDown, down),
|
||||
'\x1b[M\x61\x25\x23',
|
||||
);
|
||||
});
|
||||
|
||||
test('SGR reports which button was released', () {
|
||||
const sgr = '\x1b[?1000h\x1b[?1006h';
|
||||
expect(report(sgr, TerminalMouseButton.middle, down), '\x1b[<1;5;3M');
|
||||
expect(report(sgr, TerminalMouseButton.middle, up), '\x1b[<1;5;3m');
|
||||
expect(report(sgr, TerminalMouseButton.right, up), '\x1b[<2;5;3m');
|
||||
});
|
||||
|
||||
test('modifiers add 4/8/16', () {
|
||||
const sgr = '\x1b[?1000h\x1b[?1006h';
|
||||
expect(
|
||||
report(sgr, TerminalMouseButton.left, down, ctrl: true),
|
||||
'\x1b[<16;5;3M',
|
||||
);
|
||||
});
|
||||
|
||||
group('motion', () {
|
||||
test('1000 reports no motion', () {
|
||||
expect(
|
||||
report(
|
||||
'\x1b[?1000h\x1b[?1006h',
|
||||
TerminalMouseButton.left,
|
||||
down,
|
||||
motion: true,
|
||||
),
|
||||
'',
|
||||
);
|
||||
});
|
||||
|
||||
test('1002 reports drags with the button held, plus 32', () {
|
||||
const drag = '\x1b[?1002h\x1b[?1006h';
|
||||
expect(
|
||||
report(drag, TerminalMouseButton.left, down, motion: true),
|
||||
'\x1b[<32;5;3M',
|
||||
);
|
||||
expect(report(drag, TerminalMouseButton.none, down, motion: true), '');
|
||||
});
|
||||
|
||||
test('1003 reports motion without a button as 35', () {
|
||||
expect(
|
||||
report(
|
||||
'\x1b[?1003h\x1b[?1006h',
|
||||
TerminalMouseButton.none,
|
||||
down,
|
||||
motion: true,
|
||||
),
|
||||
'\x1b[<35;5;3M',
|
||||
);
|
||||
expect(
|
||||
report('\x1b[?1003h', TerminalMouseButton.none, down, motion: true),
|
||||
'\x1b[M\x43\x25\x23',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test('X10 mode (9) reports presses only', () {
|
||||
expect(
|
||||
report('\x1b[?9h', TerminalMouseButton.left, down),
|
||||
'\x1b[M\x20\x25\x23',
|
||||
);
|
||||
expect(report('\x1b[?9h', TerminalMouseButton.left, up), '');
|
||||
expect(report('\x1b[?9h', TerminalMouseButton.wheelUp, down), '');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
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');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user