Explorar el Código

feat: switch between line and block selection

Georg Wechslberger hace 3 años
padre
commit
f199ddeb16
Se han modificado 3 ficheros con 49 adiciones y 0 borrados
  1. 43 0
      lib/src/ui/controller.dart
  2. 5 0
      lib/src/ui/selection_mode.dart
  3. 1 0
      lib/ui.dart

+ 43 - 0
lib/src/ui/controller.dart

@@ -1,11 +1,22 @@
 import 'package:flutter/material.dart';
+import 'package:xterm/src/core/buffer/cell_offset.dart';
 import 'package:xterm/src/core/buffer/range.dart';
+import 'package:xterm/src/core/buffer/range_block.dart';
+import 'package:xterm/src/core/buffer/range_line.dart';
+import 'package:xterm/src/ui/selection_mode.dart';
 
 class TerminalController with ChangeNotifier {
   BufferRange? _selection;
 
   BufferRange? get selection => _selection;
 
+  SelectionMode _selectionMode;
+
+  SelectionMode get selectionMode => _selectionMode;
+
+  TerminalController({SelectionMode selectionMode = SelectionMode.line})
+      : _selectionMode = selectionMode;
+
   void setSelection(BufferRange? range) {
     range = range?.normalized;
 
@@ -15,6 +26,38 @@ class TerminalController with ChangeNotifier {
     }
   }
 
+  void setSelectionRange(CellOffset begin, CellOffset end) {
+    final range = _modeRange(begin, end);
+    setSelection(range);
+  }
+
+  BufferRange _modeRange(CellOffset begin, CellOffset end) {
+    switch (selectionMode) {
+      case SelectionMode.line:
+        return BufferRangeLine(begin, end);
+      case SelectionMode.block:
+        return BufferRangeBlock(begin, end);
+    }
+  }
+
+  void setSelectionMode(SelectionMode newSelectionMode) {
+    // If the new mode is the same as the old mode,
+    // nothing has to be changed.
+    if (_selectionMode == newSelectionMode) {
+      return;
+    }
+    // Set the new mode.
+    _selectionMode = newSelectionMode;
+    // Check if an active selection exists.
+    final selection = _selection;
+    if (selection == null) {
+      notifyListeners();
+      return;
+    }
+    // Convert the selection into a selection corresponding to the new mode.
+    setSelection(_modeRange(selection.begin, selection.end));
+  }
+
   void clearSelection() {
     _selection = null;
     notifyListeners();

+ 5 - 0
lib/src/ui/selection_mode.dart

@@ -0,0 +1,5 @@
+enum SelectionMode {
+  line,
+
+  block,
+}

+ 1 - 0
lib/ui.dart

@@ -2,6 +2,7 @@ export 'src/terminal_view.dart';
 export 'src/ui/controller.dart';
 export 'src/ui/cursor_type.dart';
 export 'src/ui/keyboard_visibility.dart';
+export 'src/ui/selection_mode.dart';
 export 'src/ui/shortcut/shortcuts.dart';
 export 'src/ui/terminal_text_style.dart';
 export 'src/ui/terminal_theme.dart';