소스 검색

Merge pull request #38 from devmil/feature/zoom

Feature/zoom
xuty 4 년 전
부모
커밋
75e3b01bb8
5개의 변경된 파일70개의 추가작업 그리고 18개의 파일을 삭제
  1. 5 0
      lib/frontend/cache.dart
  2. 41 14
      lib/frontend/terminal_view.dart
  3. 13 1
      lib/theme/terminal_style.dart
  4. 10 3
      pubspec.lock
  5. 1 0
      pubspec.yaml

+ 5 - 0
lib/frontend/cache.dart

@@ -8,6 +8,10 @@ class TextLayoutCache {
   final LruMap<int, TextPainter> _cache;
   final TextDirection textDirection;
 
+  void clear() {
+    _cache.clear();
+  }
+
   TextPainter? getLayoutFromCache(int key) {
     return _cache[key];
   }
@@ -36,6 +40,7 @@ class TextLayoutCache {
 }
 
 final textLayoutCache = TextLayoutCache(TextDirection.ltr, 10240);
+double textLayoutCacheFontSize = 0;
 
 // class CodePointCache {
 //   CodePointCache(int maximumSize)

+ 41 - 14
lib/frontend/terminal_view.dart

@@ -1,3 +1,4 @@
+import 'dart:io';
 import 'dart:math' as math;
 import 'dart:ui';
 
@@ -47,7 +48,7 @@ class TerminalView extends StatefulWidget {
   final InputBehavior inputBehavior;
 
   // get the dimensions of a rendered character
-  CellSize measureCellSize() {
+  CellSize measureCellSize(double fontSize) {
     final testString = 'xxxxxxxxxx' * 1000;
 
     final text = Text(
@@ -55,12 +56,12 @@ class TerminalView extends StatefulWidget {
       maxLines: 1,
       style: (style.textStyleProvider != null)
           ? style.textStyleProvider!(
-              fontSize: style.fontSize,
+              fontSize: fontSize,
             )
           : TextStyle(
               fontFamily: 'monospace',
               fontFamilyFallback: style.fontFamily,
-              fontSize: style.fontSize,
+              fontSize: fontSize,
             ),
     );
 
@@ -120,7 +121,7 @@ class _TerminalViewState extends State<TerminalView> {
     // oscillator.addListener(onTick);
 
     // measureCellSize is expensive so we cache the result.
-    _cellSize = widget.measureCellSize();
+    _cellSize = widget.measureCellSize(widget.style.fontSize);
 
     widget.terminal.addListener(onTerminalChange);
 
@@ -131,6 +132,12 @@ class _TerminalViewState extends State<TerminalView> {
   void didUpdateWidget(TerminalView oldWidget) {
     oldWidget.terminal.removeListener(onTerminalChange);
     widget.terminal.addListener(onTerminalChange);
+
+    if (oldWidget.style != widget.style) {
+      _cellSize = widget.measureCellSize(widget.style.fontSize);
+      updateTerminalSize();
+    }
+
     super.didUpdateWidget(oldWidget);
   }
 
@@ -157,7 +164,7 @@ class _TerminalViewState extends State<TerminalView> {
       child: MouseRegion(
         cursor: SystemMouseCursors.text,
         child: LayoutBuilder(builder: (context, constraints) {
-          onSize(constraints.maxWidth, constraints.maxHeight);
+          onWidgetSize(constraints.maxWidth, constraints.maxHeight);
           // use flutter's Scrollable to manage scrolling to better integrate
           // with widgets such as Scrollbar.
           return NotificationListener<ScrollNotification>(
@@ -250,8 +257,9 @@ class _TerminalViewState extends State<TerminalView> {
             charSize: _cellSize,
           ),
         ),
-        color:
-            Color(widget.terminal.backgroundColor).withOpacity(widget.opacity),
+        color: Color(widget.terminal.backgroundColor).withOpacity(
+          widget.opacity,
+        ),
       ),
     );
   }
@@ -268,15 +276,29 @@ class _TerminalViewState extends State<TerminalView> {
     return Position(x, y);
   }
 
-  int? _lastTerminalWidth;
-  int? _lastTerminalHeight;
+  double? _width;
+  double? _height;
 
-  void onSize(double width, double height) {
+  void onWidgetSize(double width, double height) {
     if (!widget.terminal.isReady) {
       return;
     }
-    final termWidth = (width / _cellSize.cellWidth).floor();
-    final termHeight = (height / _cellSize.cellHeight).floor();
+
+    _width = width;
+    _height = height;
+
+    updateTerminalSize();
+  }
+
+  int? _lastTerminalWidth;
+  int? _lastTerminalHeight;
+
+  void updateTerminalSize() {
+    assert(_width != null);
+    assert(_height != null);
+
+    final termWidth = (_width! / _cellSize.cellWidth).floor();
+    final termHeight = (_height! / _cellSize.cellHeight).floor();
 
     if (_lastTerminalWidth == termWidth && _lastTerminalHeight == termHeight) {
       return;
@@ -478,6 +500,11 @@ class TerminalPainter extends CustomPainter {
     }
 
     // final cellHash = line.cellGetHash(cell);
+    final fontSize = view.style.fontSize;
+    if (textLayoutCacheFontSize != fontSize) {
+      textLayoutCache.clear();
+      textLayoutCacheFontSize = fontSize;
+    }
     final cellHash = hashValues(codePoint, fgColor, bgColor, flags);
 
     var tp = textLayoutCache.getLayoutFromCache(cellHash);
@@ -497,7 +524,7 @@ class TerminalPainter extends CustomPainter {
     final style = (view.style.textStyleProvider != null)
         ? view.style.textStyleProvider!(
             color: color,
-            fontSize: view.style.fontSize,
+            fontSize: fontSize,
             fontWeight: flags.hasFlag(CellFlags.bold)
                 ? FontWeight.bold
                 : FontWeight.normal,
@@ -510,7 +537,7 @@ class TerminalPainter extends CustomPainter {
           )
         : TextStyle(
             color: color,
-            fontSize: view.style.fontSize,
+            fontSize: fontSize,
             fontWeight: flags.hasFlag(CellFlags.bold)
                 ? FontWeight.bold
                 : FontWeight.normal,

+ 13 - 1
lib/theme/terminal_style.dart

@@ -1,7 +1,8 @@
 import 'dart:ui' as ui;
+import 'package:equatable/equatable.dart';
 import 'package:flutter/material.dart';
 
-class TerminalStyle {
+class TerminalStyle with EquatableMixin {
   static const defaultFontFamily = [
     'Monaco',
     'Droid Sans Mono',
@@ -41,6 +42,17 @@ class TerminalStyle {
   final double fontWidthScaleFactor;
   final double fontHeightScaleFactor;
   final TextStyleProvider? textStyleProvider;
+
+  @override
+  List<Object?> get props {
+    return [
+      fontFamily,
+      fontSize,
+      fontWidthScaleFactor,
+      fontHeightScaleFactor,
+      textStyleProvider,
+    ];
+  }
 }
 
 typedef TextStyleProvider = Function({

+ 10 - 3
pubspec.lock

@@ -7,7 +7,7 @@ packages:
       name: async
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "2.6.1"
+    version: "2.7.0"
   boolean_selector:
     dependency: transitive
     description:
@@ -50,6 +50,13 @@ packages:
       url: "https://pub.dartlang.org"
     source: hosted
     version: "3.0.0"
+  equatable:
+    dependency: "direct main"
+    description:
+      name: equatable
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.0.2"
   fake_async:
     dependency: transitive
     description:
@@ -80,7 +87,7 @@ packages:
       name: meta
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "1.3.0"
+    version: "1.4.0"
   path:
     dependency: transitive
     description:
@@ -148,7 +155,7 @@ packages:
       name: test_api
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "0.3.0"
+    version: "0.4.0"
   typed_data:
     dependency: transitive
     description:

+ 1 - 0
pubspec.yaml

@@ -12,6 +12,7 @@ dependencies:
   meta: ^1.3.0
   quiver: ^3.0.0
   platform_info: ^3.0.0-nullsafety.1
+  equatable: ^2.0.2
   flutter:
     sdk: flutter