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 createState() => _TerminalScrollGestureHandlerState(); } class _TerminalScrollGestureHandlerState extends State { /// 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, ), ); } }