controller.dart 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import 'package:flutter/material.dart';
  2. import 'package:xterm/src/core/buffer/cell_offset.dart';
  3. import 'package:xterm/src/core/buffer/range.dart';
  4. import 'package:xterm/src/core/buffer/range_block.dart';
  5. import 'package:xterm/src/core/buffer/range_line.dart';
  6. import 'package:xterm/src/ui/selection_mode.dart';
  7. class TerminalController with ChangeNotifier {
  8. BufferRange? _selection;
  9. BufferRange? get selection => _selection;
  10. SelectionMode _selectionMode;
  11. SelectionMode get selectionMode => _selectionMode;
  12. TerminalController({SelectionMode selectionMode = SelectionMode.line})
  13. : _selectionMode = selectionMode;
  14. /// Set selection on the terminal to [range]. For now [range] could be either
  15. /// a [BufferRangeLine] or a [BufferRangeBlock]. This is not effected by
  16. /// [selectionMode].
  17. void setSelection(BufferRange? range) {
  18. range = range?.normalized;
  19. if (_selection != range) {
  20. _selection = range;
  21. notifyListeners();
  22. }
  23. }
  24. void setSelectionRange(CellOffset begin, CellOffset end) {
  25. final range = _modeRange(begin, end);
  26. setSelection(range);
  27. }
  28. BufferRange _modeRange(CellOffset begin, CellOffset end) {
  29. switch (selectionMode) {
  30. case SelectionMode.line:
  31. return BufferRangeLine(begin, end);
  32. case SelectionMode.block:
  33. return BufferRangeBlock(begin, end);
  34. }
  35. }
  36. /// Controls how the terminal behaves when the user selects a range of text.
  37. /// The default is [SelectionMode.line]. Setting this to [SelectionMode.block]
  38. /// enables block selection mode.
  39. void setSelectionMode(SelectionMode newSelectionMode) {
  40. // If the new mode is the same as the old mode,
  41. // nothing has to be changed.
  42. if (_selectionMode == newSelectionMode) {
  43. return;
  44. }
  45. // Set the new mode.
  46. _selectionMode = newSelectionMode;
  47. // Check if an active selection exists.
  48. final selection = _selection;
  49. if (selection == null) {
  50. notifyListeners();
  51. return;
  52. }
  53. // Convert the selection into a selection corresponding to the new mode.
  54. setSelection(_modeRange(selection.begin, selection.end));
  55. }
  56. /// Clears the current selection.
  57. void clearSelection() {
  58. _selection = null;
  59. notifyListeners();
  60. }
  61. void addHighlight(BufferRange? range) {
  62. // TODO: implement addHighlight
  63. }
  64. void clearHighlight() {
  65. // TODO: implement clearHighlight
  66. }
  67. }