9b90806093
- 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>
143 lines
4.4 KiB
Dart
143 lines
4.4 KiB
Dart
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, 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,
|
|
required this.terminal,
|
|
required this.getCellOffset,
|
|
required this.getLineHeight,
|
|
this.simulateScroll = true,
|
|
required this.child,
|
|
});
|
|
|
|
final Terminal terminal;
|
|
|
|
/// 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;
|
|
|
|
/// Whether to simulate scroll events in the terminal when the application
|
|
/// doesn't declare it supports mouse wheel events. true by default as it
|
|
/// is the default behavior of most terminals.
|
|
final bool simulateScroll;
|
|
|
|
final Widget child;
|
|
|
|
@override
|
|
State<TerminalScrollGestureHandler> createState() =>
|
|
_TerminalScrollGestureHandlerState();
|
|
}
|
|
|
|
class _TerminalScrollGestureHandlerState
|
|
extends State<TerminalScrollGestureHandler> {
|
|
/// 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;
|
|
|
|
/// 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();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
widget.terminal.removeListener(_onTerminalUpdated);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant TerminalScrollGestureHandler oldWidget) {
|
|
if (oldWidget.terminal != widget.terminal) {
|
|
oldWidget.terminal.removeListener(_onTerminalUpdated);
|
|
widget.terminal.addListener(_onTerminalUpdated);
|
|
isAltBuffer = widget.terminal.isUsingAltBuffer;
|
|
reportsScroll = widget.terminal.mouseMode.reportScroll;
|
|
}
|
|
super.didUpdateWidget(oldWidget);
|
|
}
|
|
|
|
void _onTerminalUpdated() {
|
|
final terminal = widget.terminal;
|
|
if (isAltBuffer != terminal.isUsingAltBuffer ||
|
|
reportsScroll != terminal.mouseMode.reportScroll) {
|
|
setState(() {
|
|
isAltBuffer = terminal.isUsingAltBuffer;
|
|
reportsScroll = terminal.mouseMode.reportScroll;
|
|
});
|
|
}
|
|
}
|
|
|
|
/// Send a single scroll event to the terminal. If [simulateScroll] is true,
|
|
/// then if the application doesn't recognize mouse wheel events, this method
|
|
/// will simulate scroll events by sending up/down arrow keys.
|
|
void _sendScrollEvent(bool up) {
|
|
final position = widget.getCellOffset(lastPointerPosition);
|
|
|
|
final handled = widget.terminal.mouseInput(
|
|
up ? TerminalMouseButton.wheelUp : TerminalMouseButton.wheelDown,
|
|
TerminalMouseButtonState.down,
|
|
position,
|
|
);
|
|
|
|
if (!handled && widget.simulateScroll && isAltBuffer) {
|
|
widget.terminal.keyInput(
|
|
up ? TerminalKey.arrowUp : TerminalKey.arrowDown,
|
|
);
|
|
}
|
|
}
|
|
|
|
void _onScroll(double offset) {
|
|
final currentLineOffset = offset ~/ widget.getLineHeight();
|
|
|
|
final delta = currentLineOffset - lastLineOffset;
|
|
|
|
for (var i = 0; i < delta.abs(); i++) {
|
|
_sendScrollEvent(delta < 0);
|
|
}
|
|
|
|
lastLineOffset = currentLineOffset;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (!isAltBuffer && !reportsScroll) {
|
|
return widget.child;
|
|
}
|
|
|
|
return Listener(
|
|
onPointerSignal: (event) {
|
|
lastPointerPosition = event.position;
|
|
},
|
|
onPointerDown: (event) {
|
|
lastPointerPosition = event.position;
|
|
},
|
|
child: InfiniteScrollView(
|
|
onScroll: _onScroll,
|
|
child: widget.child,
|
|
),
|
|
);
|
|
}
|
|
}
|