xuty 5 жил өмнө
parent
commit
66af1b15b7

+ 65 - 25
lib/buffer/cell_attr.dart

@@ -60,31 +60,71 @@ class CellAttr {
     this.invisible = invisible;
   }
 
-  // CellAttr copyWith({
-  //   CellColor fgColour,
-  //   CellColor bgColour,
-  //   bool bold,
-  //   bool faint,
-  //   bool italic,
-  //   bool underline,
-  //   bool blink,
-  //   bool inverse,
-  //   bool invisible,
-  // }) {
-  //   return CellAttr(
-  //     fgColour: fgColour ?? this.fgColour,
-  //     bgColour: bgColour ?? this.bgColour,
-  //     bold: bold ?? this.bold,
-  //     faint: faint ?? this.faint,
-  //     italic: italic ?? this.italic,
-  //     underline: underline ?? this.underline,
-  //     blink: blink ?? this.blink,
-  //     inverse: inverse ?? this.inverse,
-  //     invisible: invisible ?? this.invisible,
-  //   );
-  // }
+  CellAttr copyWith({
+    CellColor fgColor,
+    CellColor bgColor,
+    bool bold,
+    bool faint,
+    bool italic,
+    bool underline,
+    bool blink,
+    bool inverse,
+    bool invisible,
+  }) {
+    return CellAttr(
+      fgColor: fgColor ?? this.fgColor,
+      bgColor: bgColor ?? this.bgColor,
+      bold: bold ?? this.bold,
+      faint: faint ?? this.faint,
+      italic: italic ?? this.italic,
+      underline: underline ?? this.underline,
+      blink: blink ?? this.blink,
+      inverse: inverse ?? this.inverse,
+      invisible: invisible ?? this.invisible,
+    );
+  }
 }
 
-// class CellAttrTemplate {
+class CellAttrTemplate {
+  CellAttrTemplate();
+  
+  CellAttr _attr;
+
+  set fgColor(CellColor value) {
+    _attr = _attr.copyWith(fgColor: value);
+  }
+
+  set bgColor(CellColor value) {
+    _attr = _attr.copyWith(bgColor: value);
+  }
+
+  set bold(bool value) {
+    _attr = _attr.copyWith(bold: value);
+  }
+
+  set faint(bool value) {
+    _attr = _attr.copyWith(faint: value);
+  }
 
-// }
+  set italic(bool value) {
+    _attr = _attr.copyWith(italic: value);
+  }
+
+  set underline(bool value) {
+    _attr = _attr.copyWith(underline: value);
+  }
+
+  set blink(bool value) {
+    _attr = _attr.copyWith(blink: value);
+  }
+
+  set inverse(bool value) {
+    _attr = _attr.copyWith(inverse: value);
+  }
+
+  set invisible(bool value) {
+    _attr = _attr.copyWith(invisible: value);
+  }
+
+  CellAttr get value {}
+}

+ 92 - 0
lib/frontend/cache.dart

@@ -0,0 +1,92 @@
+import 'package:flutter/painting.dart';
+import 'package:quiver/collection.dart';
+
+class TextLayoutCache {
+  TextLayoutCache(this.textDirection, int maximumSize)
+      : _cache = LruMap<TextSpan, TextPainter>(maximumSize: maximumSize);
+
+  final LruMap<TextSpan, TextPainter> _cache;
+  final TextDirection textDirection;
+
+  TextPainter getOrPerformLayout(TextSpan text) {
+    final cachedPainter = _cache[text];
+    if (cachedPainter != null) {
+      return cachedPainter;
+    } else {
+      return _performAndCacheLayout(text);
+    }
+  }
+
+  TextPainter _performAndCacheLayout(TextSpan text) {
+    final textPainter = TextPainter(text: text, textDirection: textDirection);
+    textPainter.layout();
+
+    _cache[text] = textPainter;
+
+    return textPainter;
+  }
+
+  int get length {
+    return _cache.length;
+  }
+}
+
+final textLayoutCache = TextLayoutCache(TextDirection.ltr, 1024);
+
+class TextLayoutCacheV2 {
+  TextLayoutCacheV2(this.textDirection, int maximumSize)
+      : _cache = LruMap<int, TextPainter>(maximumSize: maximumSize);
+
+  final LruMap<int, TextPainter> _cache;
+  final TextDirection textDirection;
+
+  TextPainter getOrPerformLayout(TextSpan text, int key) {
+    final cachedPainter = _cache[key];
+    if (cachedPainter != null) {
+      return cachedPainter;
+    } else {
+      return _performAndCacheLayout(text, key);
+    }
+  }
+
+  TextPainter _performAndCacheLayout(TextSpan text, int key) {
+    final textPainter = TextPainter(text: text, textDirection: textDirection);
+    textPainter.layout();
+
+    _cache[key] = textPainter;
+
+    return textPainter;
+  }
+
+  int get length {
+    return _cache.length;
+  }
+}
+
+final textLayoutCacheV2 = TextLayoutCacheV2(TextDirection.ltr, 1024);
+
+// class CodePointCache {
+//   CodePointCache(int maximumSize)
+//       : _cache = LruMap<int, String>(maximumSize: maximumSize);
+
+//   final LruMap<int, String> _cache;
+
+//   String getOrConstruct(int codePoint) {
+//     final cachedString = _cache[codePoint];
+//     if (cachedString != null) {
+//       return cachedString;
+//     } else {
+//       return _constructAndCacheString(codePoint);
+//     }
+//   }
+
+//   String _constructAndCacheString(int codePoint) {
+//     final string = String.fromCharCode(codePoint);
+
+//     _cache[codePoint] = string;
+
+//     return string;
+//   }
+// }
+
+// final codePointCache = CodePointCache(1024);

+ 5 - 4
lib/frontend/terminal_view.dart

@@ -14,7 +14,7 @@ import 'package:xterm/frontend/input_listener.dart';
 import 'package:xterm/frontend/input_map.dart';
 import 'package:xterm/frontend/mouse_listener.dart';
 import 'package:xterm/frontend/oscillator.dart';
-import 'package:xterm/frontend/text_layout_cache.dart';
+import 'package:xterm/frontend/cache.dart';
 import 'package:xterm/mouse/position.dart';
 import 'package:xterm/terminal/terminal.dart';
 
@@ -280,8 +280,6 @@ class _TerminalViewState extends State<TerminalView> {
   }
 }
 
-final textLayoutCache = TextLayoutCache(TextDirection.ltr, 1024);
-
 class TerminalPainter extends CustomPainter {
   TerminalPainter({
     this.terminal,
@@ -309,6 +307,7 @@ class TerminalPainter extends CustomPainter {
     }
 
     paintText(canvas);
+    // print(textLayoutCacheV2.length);
     // or paintTextFast(canvas);
 
     paintSelection(canvas);
@@ -480,10 +479,12 @@ class TerminalPainter extends CustomPainter {
 
     final span = TextSpan(
       text: String.fromCharCode(cell.codePoint),
+      // text: codePointCache.getOrConstruct(cell.codePoint),
       style: style,
     );
 
-    final tp = textLayoutCache.getOrPerformLayout(span);
+    // final tp = textLayoutCache.getOrPerformLayout(span);
+    final tp = textLayoutCacheV2.getOrPerformLayout(span, cell.codePoint);
 
     tp.paint(canvas, Offset(offsetX, offsetY));
   }

+ 0 - 27
lib/frontend/text_layout_cache.dart

@@ -1,27 +0,0 @@
-import 'package:flutter/painting.dart';
-import 'package:quiver/collection.dart';
-
-class TextLayoutCache {
-  final LruMap<TextSpan, TextPainter> _cache;
-  final TextDirection textDirection;
-
-  TextLayoutCache(this.textDirection, int maximumSize) : _cache = LruMap<TextSpan, TextPainter>(maximumSize: maximumSize);
-
-  TextPainter getOrPerformLayout(TextSpan text) {
-    final cachedPainter = _cache[text];
-    if (cachedPainter != null) {
-      return cachedPainter;
-    } else {
-      return _performAndCacheLayout(text);
-    }
-  }
-
-  TextPainter _performAndCacheLayout(TextSpan text) {
-    final textPainter = TextPainter(text: text, textDirection: textDirection);
-    textPainter.layout();
-
-    _cache[text] = textPainter;
-
-    return textPainter;
-  }
-}