controller.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import 'package:flutter/material.dart';
  2. import 'package:meta/meta.dart';
  3. import 'package:xterm/src/core/buffer/cell_offset.dart';
  4. import 'package:xterm/src/core/buffer/range.dart';
  5. import 'package:xterm/src/core/buffer/range_block.dart';
  6. import 'package:xterm/src/core/buffer/range_line.dart';
  7. import 'package:xterm/src/ui/pointer_input.dart';
  8. import 'package:xterm/src/ui/selection_mode.dart';
  9. class TerminalController with ChangeNotifier {
  10. TerminalController({
  11. SelectionMode selectionMode = SelectionMode.line,
  12. PointerInputs pointerInputs = const PointerInputs.none(),
  13. bool suspendPointerInput = false,
  14. }) : _selectionMode = selectionMode,
  15. _pointerInputs = pointerInputs,
  16. _suspendPointerInputs = suspendPointerInput;
  17. BufferRange? _selection;
  18. BufferRange? get selection => _selection;
  19. SelectionMode _selectionMode;
  20. SelectionMode get selectionMode => _selectionMode;
  21. /// Set selection on the terminal to [range]. For now [range] could be either
  22. /// a [BufferRangeLine] or a [BufferRangeBlock]. This is not effected by
  23. /// [selectionMode].
  24. PointerInputs _pointerInputs;
  25. /// The set of pointer events which will be used as mouse input for the terminal.
  26. PointerInputs get pointerInput => _pointerInputs;
  27. bool _suspendPointerInputs;
  28. /// True if sending pointer events to the terminal is suspended.
  29. bool get suspendedPointerInputs => _suspendPointerInputs;
  30. void setSelection(BufferRange? range) {
  31. range = range?.normalized;
  32. if (_selection != range) {
  33. _selection = range;
  34. notifyListeners();
  35. }
  36. }
  37. /// Set selection on the terminal to the minimum range that contains both
  38. /// [begin] and [end]. The type of range is determined by [selectionMode].
  39. void setSelectionRange(CellOffset begin, CellOffset end) {
  40. final range = _modeRange(begin, end);
  41. setSelection(range);
  42. }
  43. BufferRange _modeRange(CellOffset begin, CellOffset end) {
  44. switch (selectionMode) {
  45. case SelectionMode.line:
  46. return BufferRangeLine(begin, end);
  47. case SelectionMode.block:
  48. return BufferRangeBlock(begin, end);
  49. }
  50. }
  51. /// Controls how the terminal behaves when the user selects a range of text.
  52. /// The default is [SelectionMode.line]. Setting this to [SelectionMode.block]
  53. /// enables block selection mode.
  54. void setSelectionMode(SelectionMode newSelectionMode) {
  55. // If the new mode is the same as the old mode,
  56. // nothing has to be changed.
  57. if (_selectionMode == newSelectionMode) {
  58. return;
  59. }
  60. // Set the new mode.
  61. _selectionMode = newSelectionMode;
  62. // Check if an active selection exists.
  63. final selection = _selection;
  64. if (selection == null) {
  65. notifyListeners();
  66. return;
  67. }
  68. // Convert the selection into a selection corresponding to the new mode.
  69. setSelection(_modeRange(selection.begin, selection.end));
  70. }
  71. /// Clears the current selection.
  72. void clearSelection() {
  73. _selection = null;
  74. notifyListeners();
  75. }
  76. // Select which type of pointer events are send to the terminal.
  77. void setPointerInputs(PointerInputs pointerInput) {
  78. _pointerInputs = pointerInput;
  79. notifyListeners();
  80. }
  81. // Toggle sending pointer events to the terminal.
  82. void setSuspendPointerInput(bool suspend) {
  83. _suspendPointerInputs = suspend;
  84. notifyListeners();
  85. }
  86. // Returns true if this type of PointerInput should be send to the Terminal.
  87. @internal
  88. bool shouldSendPointerInput(PointerInput pointerInput) {
  89. // Always return false if pointer input is suspended.
  90. return _suspendPointerInputs
  91. ? false
  92. : _pointerInputs.inputs.contains(pointerInput);
  93. }
  94. void addHighlight(BufferRange? range) {
  95. // TODO: implement addHighlight
  96. }
  97. void clearHighlight() {
  98. // TODO: implement clearHighlight
  99. }
  100. }