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');
|
||||
});
|
||||
}
|
||||
+11
-9
@@ -5,21 +5,23 @@ enum TerminalMouseButton {
|
||||
|
||||
right(id: 2),
|
||||
|
||||
wheelUp(id: 64 + 4, isWheel: true),
|
||||
/// No button: the pointer moved with no button held (mode 1003 motion).
|
||||
none(id: 3),
|
||||
|
||||
wheelDown(id: 64 + 5, isWheel: true),
|
||||
wheelUp(id: 64 + 0, isWheel: true),
|
||||
|
||||
wheelLeft(id: 64 + 6, isWheel: true),
|
||||
wheelDown(id: 64 + 1, isWheel: true),
|
||||
|
||||
wheelRight(id: 64 + 7, isWheel: true),
|
||||
wheelLeft(id: 64 + 2, isWheel: true),
|
||||
|
||||
wheelRight(id: 64 + 3, isWheel: true),
|
||||
;
|
||||
|
||||
/// The id that is used to report a button press or release to the terminal.
|
||||
/// The button code reported to the program, before modifiers and the
|
||||
/// motion flag are added.
|
||||
///
|
||||
/// Mouse wheel up / down use button IDs 4 = 0100 (binary) and 5 = 0101 (binary).
|
||||
/// The bits three and four of the button are transposed by 64 and 128
|
||||
/// respectively, when reporting the id of the button and have have to be
|
||||
/// adjusted correspondingly.
|
||||
/// The wheel is reported as buttons 4-7, which the protocol encodes by
|
||||
/// setting bit 6 (64) on codes 0-3: wheel up is 64, wheel down 65.
|
||||
final int id;
|
||||
|
||||
/// Whether this button is a mouse wheel button.
|
||||
|
||||
+40
-4
@@ -13,7 +13,7 @@ class TerminalMouseEvent {
|
||||
/// The current state of the button.
|
||||
final TerminalMouseButtonState buttonState;
|
||||
|
||||
/// The position of button state change.
|
||||
/// The zero-based screen cell of the event.
|
||||
final CellOffset position;
|
||||
|
||||
/// The state of the terminal.
|
||||
@@ -22,13 +22,33 @@ class TerminalMouseEvent {
|
||||
/// The platform of the terminal.
|
||||
final TerminalTargetPlatform platform;
|
||||
|
||||
/// Whether the pointer moved (with [button] held, or [TerminalMouseButton.none])
|
||||
/// rather than a button changing state.
|
||||
final bool motion;
|
||||
|
||||
final bool shift;
|
||||
|
||||
final bool alt;
|
||||
|
||||
final bool ctrl;
|
||||
|
||||
TerminalMouseEvent({
|
||||
required this.button,
|
||||
required this.buttonState,
|
||||
required this.position,
|
||||
required this.state,
|
||||
required this.platform,
|
||||
this.motion = false,
|
||||
this.shift = false,
|
||||
this.alt = false,
|
||||
this.ctrl = false,
|
||||
});
|
||||
|
||||
/// The modifier bits of the report.
|
||||
int get modifiers =>
|
||||
(shift ? MouseReporter.shiftBit : 0) |
|
||||
(alt ? MouseReporter.altBit : 0) |
|
||||
(ctrl ? MouseReporter.ctrlBit : 0);
|
||||
}
|
||||
|
||||
const defaultMouseHandler = CascadeMouseHandler([
|
||||
@@ -59,6 +79,8 @@ class CascadeMouseHandler implements TerminalMouseHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/// X10 compatibility mode (`CSI ? 9 h`): button presses only, no
|
||||
/// modifiers, no wheel.
|
||||
class ClickMouseHandler implements TerminalMouseHandler {
|
||||
const ClickMouseHandler();
|
||||
|
||||
@@ -66,9 +88,9 @@ class ClickMouseHandler implements TerminalMouseHandler {
|
||||
String? call(TerminalMouseEvent event) {
|
||||
switch (event.state.mouseMode) {
|
||||
case MouseMode.clickOnly:
|
||||
// Only clicks and only the first 3 buttons are reported.
|
||||
if (event.buttonState == TerminalMouseButtonState.down &&
|
||||
(event.button.id < 3)) {
|
||||
!event.motion &&
|
||||
event.button.id < 3) {
|
||||
return MouseReporter.report(
|
||||
event.button,
|
||||
event.buttonState,
|
||||
@@ -86,18 +108,30 @@ class ClickMouseHandler implements TerminalMouseHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/// Normal (1000), button-event (1002) and any-event (1003) tracking:
|
||||
/// presses, releases and the wheel; 1002 adds motion with a button held,
|
||||
/// 1003 all motion.
|
||||
class UpDownMouseHandler implements TerminalMouseHandler {
|
||||
const UpDownMouseHandler();
|
||||
|
||||
@override
|
||||
String? call(TerminalMouseEvent event) {
|
||||
switch (event.state.mouseMode) {
|
||||
final mode = event.state.mouseMode;
|
||||
switch (mode) {
|
||||
case MouseMode.none:
|
||||
case MouseMode.clickOnly:
|
||||
return null;
|
||||
case MouseMode.upDownScroll:
|
||||
case MouseMode.upDownScrollDrag:
|
||||
case MouseMode.upDownScrollMove:
|
||||
if (event.motion) {
|
||||
final reported = mode == MouseMode.upDownScrollMove ||
|
||||
mode == MouseMode.upDownScrollDrag &&
|
||||
event.button != TerminalMouseButton.none;
|
||||
if (!reported) return null;
|
||||
} else if (event.button == TerminalMouseButton.none) {
|
||||
return null;
|
||||
}
|
||||
// Up events are never reported for mouse wheel buttons.
|
||||
if (event.button.isWheel &&
|
||||
event.buttonState == TerminalMouseButtonState.up) {
|
||||
@@ -108,6 +142,8 @@ class UpDownMouseHandler implements TerminalMouseHandler {
|
||||
event.buttonState,
|
||||
event.position,
|
||||
event.state.mouseReportMode,
|
||||
motion: event.motion,
|
||||
modifiers: event.modifiers,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+37
-26
@@ -4,45 +4,56 @@ import 'package:xterm/src/core/mouse/button.dart';
|
||||
import 'package:xterm/src/core/mouse/button_state.dart';
|
||||
|
||||
abstract class MouseReporter {
|
||||
/// Modifier bits added to the button code.
|
||||
static const shiftBit = 4;
|
||||
static const altBit = 8;
|
||||
static const ctrlBit = 16;
|
||||
|
||||
/// Added to the button code for a motion event (modes 1002 and 1003).
|
||||
static const motionBit = 32;
|
||||
|
||||
/// Encodes a mouse event at the zero-based screen cell [position].
|
||||
///
|
||||
/// [modifiers] is a combination of [shiftBit], [altBit] and [ctrlBit];
|
||||
/// [motion] marks a move rather than a press or release.
|
||||
static String report(
|
||||
TerminalMouseButton button,
|
||||
TerminalMouseButtonState state,
|
||||
CellOffset position,
|
||||
MouseReportMode reportMode,
|
||||
) {
|
||||
MouseReportMode reportMode, {
|
||||
bool motion = false,
|
||||
int modifiers = 0,
|
||||
}) {
|
||||
// x and y offsets have to be incremented by 1 as the offset if 0-based,
|
||||
// The position has to be reported using 1-based coordinates.
|
||||
final x = position.x + 1;
|
||||
final y = position.y + 1;
|
||||
final extra = modifiers | (motion ? motionBit : 0);
|
||||
switch (reportMode) {
|
||||
case MouseReportMode.normal:
|
||||
case MouseReportMode.utf:
|
||||
// Button ID 3 is used to signal a button release.
|
||||
final buttonID = state == TerminalMouseButtonState.up ? 3 : button.id;
|
||||
// The button ID is reported as shifted by 32 to produce a printable
|
||||
// character.
|
||||
final btn = String.fromCharCode(32 + buttonID);
|
||||
// Normal mode only supports a maximum position of 223, while utf
|
||||
// supports positions up to 2015. Both modes send a null byte if the
|
||||
// position exceeds that limit.
|
||||
final col = (reportMode == MouseReportMode.normal && x > 223) ||
|
||||
(reportMode == MouseReportMode.utf && x > 2015)
|
||||
? '\x00'
|
||||
: String.fromCharCode(32 + x);
|
||||
final row = (reportMode == MouseReportMode.normal && y > 223) ||
|
||||
(reportMode == MouseReportMode.utf && y > 2015)
|
||||
? '\x00'
|
||||
: String.fromCharCode(32 + y + 1);
|
||||
return "\x1b[M$btn$col$row";
|
||||
// Button code 3 signals a release; these encodings cannot say which
|
||||
// button was released.
|
||||
final code =
|
||||
(state == TerminalMouseButtonState.up && !motion ? 3 : button.id) |
|
||||
extra;
|
||||
// The code and coordinates are sent as 32 + value. Normal mode only
|
||||
// reaches 223, utf mode 2015; beyond that a null byte is sent.
|
||||
final limit = reportMode == MouseReportMode.normal ? 223 : 2015;
|
||||
String encode(int value) =>
|
||||
value > limit ? '\x00' : String.fromCharCode(32 + value);
|
||||
return '\x1b[M${String.fromCharCode(32 + code)}${encode(x)}${encode(y)}';
|
||||
case MouseReportMode.sgr:
|
||||
final buttonID = button.id;
|
||||
final upDown = state == TerminalMouseButtonState.down ? 'M' : 'm';
|
||||
return "\x1b[<$buttonID;$x;$y$upDown";
|
||||
// SGR reports the released button and marks a release with `m`.
|
||||
final code = button.id | extra;
|
||||
final upDown =
|
||||
state == TerminalMouseButtonState.up && !motion ? 'm' : 'M';
|
||||
return '\x1b[<$code;$x;$y$upDown';
|
||||
case MouseReportMode.urxvt:
|
||||
// The button ID uses the same id as to report it as in normal mode.
|
||||
final buttonID =
|
||||
32 + (state == TerminalMouseButtonState.up ? 3 : button.id);
|
||||
return "\x1b[$buttonID;$x;${y}M";
|
||||
final code =
|
||||
(state == TerminalMouseButtonState.up && !motion ? 3 : button.id) |
|
||||
extra;
|
||||
return '\x1b[${32 + code};$x;${y}M';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+14
-3
@@ -376,18 +376,29 @@ class Terminal with Observable implements TerminalState, EscapeHandler {
|
||||
}
|
||||
}
|
||||
|
||||
// Handle a mouse event and return true if it was handled.
|
||||
/// Reports a mouse event at the zero-based screen cell [position] to the
|
||||
/// program, as its mouse mode asks; returns true if it was reported.
|
||||
/// [motion] marks a pointer move (with [button] held, or
|
||||
/// [TerminalMouseButton.none] in mode 1003).
|
||||
bool mouseInput(
|
||||
TerminalMouseButton button,
|
||||
TerminalMouseButtonState buttonState,
|
||||
CellOffset position,
|
||||
) {
|
||||
CellOffset position, {
|
||||
bool motion = false,
|
||||
bool shift = false,
|
||||
bool alt = false,
|
||||
bool ctrl = false,
|
||||
}) {
|
||||
final output = mouseHandler?.call(TerminalMouseEvent(
|
||||
button: button,
|
||||
buttonState: buttonState,
|
||||
position: position,
|
||||
state: this,
|
||||
platform: platform,
|
||||
motion: motion,
|
||||
shift: shift,
|
||||
alt: alt,
|
||||
ctrl: ctrl,
|
||||
));
|
||||
if (output != null) {
|
||||
onOutput?.call(output);
|
||||
|
||||
+17
-1
@@ -13,6 +13,7 @@ import 'package:xterm/src/ui/gesture/gesture_handler.dart';
|
||||
import 'package:xterm/src/ui/input_map.dart';
|
||||
import 'package:xterm/src/ui/keyboard_listener.dart';
|
||||
import 'package:xterm/src/ui/keyboard_visibility.dart';
|
||||
import 'package:xterm/src/ui/mouse_listener.dart';
|
||||
import 'package:xterm/src/ui/render.dart';
|
||||
import 'package:xterm/src/ui/scroll_handler.dart';
|
||||
import 'package:xterm/src/ui/shortcut/actions.dart';
|
||||
@@ -166,6 +167,10 @@ class TerminalViewState extends State<TerminalView> {
|
||||
RenderTerminal get renderTerminal =>
|
||||
_viewportKey.currentContext!.findRenderObject() as RenderTerminal;
|
||||
|
||||
/// Whether the latest mouse press was reported to the program (mouse
|
||||
/// tracking on, Shift not held), so the gestures it starts do not select.
|
||||
bool mousePressReported = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
_focusNode = widget.focusNode ?? FocusNode();
|
||||
@@ -244,7 +249,9 @@ class TerminalViewState extends State<TerminalView> {
|
||||
child = TerminalScrollGestureHandler(
|
||||
terminal: widget.terminal,
|
||||
simulateScroll: widget.simulateScroll,
|
||||
getCellOffset: (offset) => renderTerminal.getCellOffset(offset),
|
||||
getCellOffset: (globalPosition) => renderTerminal.getScreenCellOffset(
|
||||
renderTerminal.globalToLocal(globalPosition),
|
||||
),
|
||||
getLineHeight: () => renderTerminal.lineHeight,
|
||||
child: child,
|
||||
);
|
||||
@@ -296,6 +303,15 @@ class TerminalViewState extends State<TerminalView> {
|
||||
child: child,
|
||||
);
|
||||
|
||||
child = TerminalMouseListener(
|
||||
terminal: widget.terminal,
|
||||
controller: _controller,
|
||||
renderTerminal: () => renderTerminal,
|
||||
readOnly: widget.readOnly,
|
||||
onPressReported: (reported) => mousePressReported = reported,
|
||||
child: child,
|
||||
);
|
||||
|
||||
child = TerminalGestureHandler(
|
||||
terminalView: this,
|
||||
terminalController: _controller,
|
||||
|
||||
+3
-1
@@ -12,7 +12,9 @@ import 'package:xterm/src/ui/selection_mode.dart';
|
||||
class TerminalController with ChangeNotifier {
|
||||
TerminalController({
|
||||
SelectionMode selectionMode = SelectionMode.line,
|
||||
PointerInputs pointerInputs = const PointerInputs({PointerInput.tap}),
|
||||
// Every kind by default: the program opts into each with its mouse
|
||||
// mode, so drags (1002) and motion (1003) are reported when asked for.
|
||||
PointerInputs pointerInputs = const PointerInputs.all(),
|
||||
bool suspendPointerInput = false,
|
||||
}) : _selectionMode = selectionMode,
|
||||
_pointerInputs = pointerInputs,
|
||||
|
||||
@@ -68,8 +68,8 @@ class _TerminalGestureHandlerState extends State<TerminalGestureHandler> {
|
||||
onTapDown: onTapDown,
|
||||
onSecondaryTapDown: onSecondaryTapDown,
|
||||
onSecondaryTapUp: onSecondaryTapUp,
|
||||
onTertiaryTapDown: onSecondaryTapDown,
|
||||
onTertiaryTapUp: onSecondaryTapUp,
|
||||
onTertiaryTapDown: onTertiaryTapDown,
|
||||
onTertiaryTapUp: onTertiaryTapUp,
|
||||
onLongPressStart: onLongPressStart,
|
||||
onLongPressMoveUpdate: onLongPressMoveUpdate,
|
||||
// onLongPressUp: onLongPressUp,
|
||||
@@ -89,9 +89,10 @@ class _TerminalGestureHandlerState extends State<TerminalGestureHandler> {
|
||||
TerminalMouseButton button, {
|
||||
bool forceCallback = false,
|
||||
}) {
|
||||
// Check if the terminal should and can handle the tap down event.
|
||||
var handled = false;
|
||||
if (_shouldSendTapEvent) {
|
||||
// Check if the terminal should and can handle the tap down event. A
|
||||
// mouse press was already reported by the TerminalMouseListener.
|
||||
var handled = _isReportedMouse(details.kind);
|
||||
if (!_isMouse(details.kind) && _shouldSendTapEvent) {
|
||||
handled = renderTerminal.mouseEvent(
|
||||
button,
|
||||
TerminalMouseButtonState.down,
|
||||
@@ -111,8 +112,8 @@ class _TerminalGestureHandlerState extends State<TerminalGestureHandler> {
|
||||
bool forceCallback = false,
|
||||
}) {
|
||||
// Check if the terminal should and can handle the tap up event.
|
||||
var handled = false;
|
||||
if (_shouldSendTapEvent) {
|
||||
var handled = _isReportedMouse(details.kind);
|
||||
if (!_isMouse(details.kind) && _shouldSendTapEvent) {
|
||||
handled = renderTerminal.mouseEvent(
|
||||
button,
|
||||
TerminalMouseButtonState.up,
|
||||
@@ -153,10 +154,21 @@ class _TerminalGestureHandlerState extends State<TerminalGestureHandler> {
|
||||
}
|
||||
|
||||
void onTertiaryTapUp(TapUpDetails details) {
|
||||
_tapUp(widget.onTertiaryTapUp, details, TerminalMouseButton.right);
|
||||
_tapUp(widget.onTertiaryTapUp, details, TerminalMouseButton.middle);
|
||||
}
|
||||
|
||||
/// Whether [kind] is a mouse, whose presses the TerminalMouseListener
|
||||
/// reports instead of this handler.
|
||||
static bool _isMouse(PointerDeviceKind? kind) =>
|
||||
kind == PointerDeviceKind.mouse;
|
||||
|
||||
/// Whether the current press is a mouse press reported to the program,
|
||||
/// which therefore must not select text.
|
||||
bool _isReportedMouse(PointerDeviceKind? kind) =>
|
||||
_isMouse(kind) && terminalView.mousePressReported;
|
||||
|
||||
void onDoubleTapDown(TapDownDetails details) {
|
||||
if (_isReportedMouse(details.kind)) return;
|
||||
renderTerminal.selectWord(details.localPosition);
|
||||
}
|
||||
|
||||
@@ -175,6 +187,11 @@ class _TerminalGestureHandlerState extends State<TerminalGestureHandler> {
|
||||
// void onLongPressUp() {}
|
||||
|
||||
void onDragStart(DragStartDetails details) {
|
||||
if (_isReportedMouse(details.kind)) {
|
||||
// The program follows the drag (mode 1002/1003), not the selection.
|
||||
_lastDragStartDetails = null;
|
||||
return;
|
||||
}
|
||||
_lastDragStartDetails = details;
|
||||
|
||||
details.kind == PointerDeviceKind.mouse
|
||||
@@ -183,8 +200,10 @@ class _TerminalGestureHandlerState extends State<TerminalGestureHandler> {
|
||||
}
|
||||
|
||||
void onDragUpdate(DragUpdateDetails details) {
|
||||
final start = _lastDragStartDetails;
|
||||
if (start == null) return;
|
||||
renderTerminal.selectCharacters(
|
||||
_lastDragStartDetails!.localPosition,
|
||||
start.localPosition,
|
||||
details.localPosition,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:xterm/src/core/buffer/cell_offset.dart';
|
||||
import 'package:xterm/src/core/mouse/button.dart';
|
||||
import 'package:xterm/src/core/mouse/button_state.dart';
|
||||
import 'package:xterm/src/core/mouse/mode.dart';
|
||||
import 'package:xterm/src/terminal.dart';
|
||||
import 'package:xterm/src/ui/controller.dart';
|
||||
import 'package:xterm/src/ui/pointer_input.dart';
|
||||
import 'package:xterm/src/ui/render.dart';
|
||||
|
||||
/// Reports mouse buttons, drags and motion to the program while it has
|
||||
/// enabled mouse tracking (`CSI ? 1000/1002/1003 h`), straight from pointer
|
||||
/// events so presses, drags and releases keep their buttons and order.
|
||||
///
|
||||
/// Holding Shift at the press leaves the pointer to text selection, as in
|
||||
/// other terminals. Touch input is not handled here: taps on a touch screen
|
||||
/// are reported by the gesture handler.
|
||||
class TerminalMouseListener extends StatefulWidget {
|
||||
const TerminalMouseListener({
|
||||
super.key,
|
||||
required this.terminal,
|
||||
required this.controller,
|
||||
required this.renderTerminal,
|
||||
required this.readOnly,
|
||||
required this.onPressReported,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final Terminal terminal;
|
||||
|
||||
final TerminalController controller;
|
||||
|
||||
final RenderTerminal Function() renderTerminal;
|
||||
|
||||
final bool readOnly;
|
||||
|
||||
/// Called at each mouse press with whether the press goes to the program
|
||||
/// (and so must not start a selection).
|
||||
final void Function(bool reported) onPressReported;
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
State<TerminalMouseListener> createState() => _TerminalMouseListenerState();
|
||||
}
|
||||
|
||||
class _TerminalMouseListenerState extends State<TerminalMouseListener> {
|
||||
/// The pointer whose press was reported, and its button.
|
||||
int? _pointer;
|
||||
TerminalMouseButton _button = TerminalMouseButton.left;
|
||||
|
||||
/// The last cell a motion was reported for, so a move within one cell is
|
||||
/// not reported again.
|
||||
CellOffset? _lastCell;
|
||||
|
||||
bool _wants(PointerInput input) =>
|
||||
!widget.readOnly &&
|
||||
widget.terminal.mouseMode != MouseMode.none &&
|
||||
widget.controller.shouldSendPointerInput(input);
|
||||
|
||||
static bool get _shift => HardwareKeyboard.instance.isShiftPressed;
|
||||
|
||||
bool _report(
|
||||
TerminalMouseButton button,
|
||||
TerminalMouseButtonState state,
|
||||
Offset globalPosition, {
|
||||
bool motion = false,
|
||||
}) {
|
||||
final keyboard = HardwareKeyboard.instance;
|
||||
final cell = _cellOf(globalPosition);
|
||||
_lastCell = cell;
|
||||
return widget.terminal.mouseInput(
|
||||
button,
|
||||
state,
|
||||
cell,
|
||||
motion: motion,
|
||||
alt: keyboard.isAltPressed,
|
||||
ctrl: keyboard.isControlPressed,
|
||||
);
|
||||
}
|
||||
|
||||
static TerminalMouseButton? _buttonOf(int buttons) {
|
||||
if (buttons & kPrimaryMouseButton != 0) return TerminalMouseButton.left;
|
||||
if (buttons & kMiddleMouseButton != 0) return TerminalMouseButton.middle;
|
||||
if (buttons & kSecondaryMouseButton != 0) return TerminalMouseButton.right;
|
||||
return null;
|
||||
}
|
||||
|
||||
void _down(PointerDownEvent event) {
|
||||
if (event.kind != PointerDeviceKind.mouse || _pointer != null) return;
|
||||
final button = _buttonOf(event.buttons);
|
||||
final take = button != null && !_shift && _wants(PointerInput.tap);
|
||||
widget.onPressReported(take);
|
||||
if (!take) return;
|
||||
_pointer = event.pointer;
|
||||
_button = button;
|
||||
_report(button, TerminalMouseButtonState.down, event.position);
|
||||
}
|
||||
|
||||
void _move(PointerMoveEvent event) {
|
||||
if (event.pointer != _pointer || !_wants(PointerInput.drag)) return;
|
||||
if (_cellOf(event.position) == _lastCell) return;
|
||||
_report(
|
||||
_button,
|
||||
TerminalMouseButtonState.down,
|
||||
event.position,
|
||||
motion: true,
|
||||
);
|
||||
}
|
||||
|
||||
void _up(PointerEvent event) {
|
||||
if (event.pointer != _pointer) return;
|
||||
_pointer = null;
|
||||
if (widget.terminal.mouseMode == MouseMode.none) return;
|
||||
_report(_button, TerminalMouseButtonState.up, event.position);
|
||||
}
|
||||
|
||||
void _hover(PointerHoverEvent event) {
|
||||
if (event.kind != PointerDeviceKind.mouse ||
|
||||
widget.terminal.mouseMode != MouseMode.upDownScrollMove ||
|
||||
_shift ||
|
||||
!_wants(PointerInput.move)) {
|
||||
return;
|
||||
}
|
||||
if (_cellOf(event.position) == _lastCell) return;
|
||||
_report(
|
||||
TerminalMouseButton.none,
|
||||
TerminalMouseButtonState.down,
|
||||
event.position,
|
||||
motion: true,
|
||||
);
|
||||
}
|
||||
|
||||
CellOffset _cellOf(Offset globalPosition) {
|
||||
final render = widget.renderTerminal();
|
||||
return render.getScreenCellOffset(render.globalToLocal(globalPosition));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Listener(
|
||||
onPointerDown: _down,
|
||||
onPointerMove: _move,
|
||||
onPointerUp: _up,
|
||||
onPointerCancel: _up,
|
||||
onPointerHover: _hover,
|
||||
child: widget.child,
|
||||
);
|
||||
}
|
||||
}
|
||||
+23
-4
@@ -316,10 +316,29 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
|
||||
bool mouseEvent(
|
||||
TerminalMouseButton button,
|
||||
TerminalMouseButtonState buttonState,
|
||||
Offset offset,
|
||||
) {
|
||||
final position = getCellOffset(offset);
|
||||
return _terminal.mouseInput(button, buttonState, position);
|
||||
Offset offset, {
|
||||
bool motion = false,
|
||||
bool shift = false,
|
||||
bool alt = false,
|
||||
bool ctrl = false,
|
||||
}) {
|
||||
return _terminal.mouseInput(
|
||||
button,
|
||||
buttonState,
|
||||
getScreenCellOffset(offset),
|
||||
motion: motion,
|
||||
shift: shift,
|
||||
alt: alt,
|
||||
ctrl: ctrl,
|
||||
);
|
||||
}
|
||||
|
||||
/// The screen cell (row 0 is the top of the screen, not of the
|
||||
/// scrollback) that the local [offset] is in, as mouse reports use.
|
||||
CellOffset getScreenCellOffset(Offset offset) {
|
||||
final cell = getCellOffset(offset);
|
||||
final row = cell.y - _terminal.buffer.scrollBack;
|
||||
return CellOffset(cell.x, row.clamp(0, _terminal.viewHeight - 1));
|
||||
}
|
||||
|
||||
void _notifyEditableRect() {
|
||||
|
||||
+24
-15
@@ -2,10 +2,10 @@ import 'package:flutter/widgets.dart';
|
||||
import 'package:xterm/core.dart';
|
||||
import 'package:xterm/src/ui/infinite_scroll_view.dart';
|
||||
|
||||
/// Handles scrolling gestures in the alternate screen buffer. In alternate
|
||||
/// screen buffer, the terminal don't have a scrollback buffer, instead, the
|
||||
/// scroll gestures are converted to escape sequences based on the current
|
||||
/// report mode declared by the application.
|
||||
/// Handles scrolling gestures in the alternate screen buffer, and on either
|
||||
/// screen while the program tracks the mouse. There the scroll gestures are
|
||||
/// reported as wheel events in the program's mouse report mode, or (in the
|
||||
/// alternate screen without mouse tracking) simulated with arrow keys.
|
||||
class TerminalScrollGestureHandler extends StatefulWidget {
|
||||
const TerminalScrollGestureHandler({
|
||||
super.key,
|
||||
@@ -18,8 +18,8 @@ class TerminalScrollGestureHandler extends StatefulWidget {
|
||||
|
||||
final Terminal terminal;
|
||||
|
||||
/// Returns the cell offset for the pixel offset.
|
||||
final CellOffset Function(Offset) getCellOffset;
|
||||
/// Returns the zero-based screen cell under a global pixel position.
|
||||
final CellOffset Function(Offset globalPosition) getCellOffset;
|
||||
|
||||
/// Returns the pixel height of lines in the terminal.
|
||||
final double Function() getLineHeight;
|
||||
@@ -38,22 +38,26 @@ class TerminalScrollGestureHandler extends StatefulWidget {
|
||||
|
||||
class _TerminalScrollGestureHandlerState
|
||||
extends State<TerminalScrollGestureHandler> {
|
||||
/// Whether the application is in alternate screen buffer. If false, then this
|
||||
/// widget does nothing.
|
||||
/// Whether the application is in alternate screen buffer.
|
||||
var isAltBuffer = false;
|
||||
|
||||
/// Whether the program asked for wheel events. With neither this nor
|
||||
/// [isAltBuffer] this widget does nothing and the scrollback scrolls.
|
||||
var reportsScroll = false;
|
||||
|
||||
/// The variable that tracks the line offset in last scroll event. Used to
|
||||
/// determine how many the scroll events should be sent to the terminal.
|
||||
var lastLineOffset = 0;
|
||||
|
||||
/// This variable tracks the last offset where the scroll gesture started.
|
||||
/// Used to calculate the cell offset of the terminal mouse event.
|
||||
/// The global position where the scroll gesture started. Used to
|
||||
/// calculate the cell offset of the terminal mouse event.
|
||||
var lastPointerPosition = Offset.zero;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
widget.terminal.addListener(_onTerminalUpdated);
|
||||
isAltBuffer = widget.terminal.isUsingAltBuffer;
|
||||
reportsScroll = widget.terminal.mouseMode.reportScroll;
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@@ -69,14 +73,19 @@ class _TerminalScrollGestureHandlerState
|
||||
oldWidget.terminal.removeListener(_onTerminalUpdated);
|
||||
widget.terminal.addListener(_onTerminalUpdated);
|
||||
isAltBuffer = widget.terminal.isUsingAltBuffer;
|
||||
reportsScroll = widget.terminal.mouseMode.reportScroll;
|
||||
}
|
||||
super.didUpdateWidget(oldWidget);
|
||||
}
|
||||
|
||||
void _onTerminalUpdated() {
|
||||
if (isAltBuffer != widget.terminal.isUsingAltBuffer) {
|
||||
isAltBuffer = widget.terminal.isUsingAltBuffer;
|
||||
setState(() {});
|
||||
final terminal = widget.terminal;
|
||||
if (isAltBuffer != terminal.isUsingAltBuffer ||
|
||||
reportsScroll != terminal.mouseMode.reportScroll) {
|
||||
setState(() {
|
||||
isAltBuffer = terminal.isUsingAltBuffer;
|
||||
reportsScroll = terminal.mouseMode.reportScroll;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +101,7 @@ class _TerminalScrollGestureHandlerState
|
||||
position,
|
||||
);
|
||||
|
||||
if (!handled && widget.simulateScroll) {
|
||||
if (!handled && widget.simulateScroll && isAltBuffer) {
|
||||
widget.terminal.keyInput(
|
||||
up ? TerminalKey.arrowUp : TerminalKey.arrowDown,
|
||||
);
|
||||
@@ -113,7 +122,7 @@ class _TerminalScrollGestureHandlerState
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!isAltBuffer) {
|
||||
if (!isAltBuffer && !reportsScroll) {
|
||||
return widget.child;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user