소스 검색

Support text scaling from os

Lucas Aschenbach 3 년 전
부모
커밋
54a5830383
4개의 변경된 파일29개의 추가작업 그리고 7개의 파일을 삭제
  1. 6 0
      lib/src/terminal_view.dart
  2. 2 2
      lib/src/ui/char_metrics.dart
  3. 2 2
      lib/src/ui/paragraph_cache.dart
  4. 19 3
      lib/src/ui/render.dart

+ 6 - 0
lib/src/terminal_view.dart

@@ -212,6 +212,7 @@ class TerminalViewState extends State<TerminalView> {
           padding: MediaQuery.of(context).padding,
           autoResize: widget.autoResize,
           textStyle: widget.textStyle,
+          textScaleFactor: MediaQuery.textScaleFactorOf(context),
           theme: widget.theme,
           focusNode: _focusNode,
           cursorType: widget.cursorType,
@@ -408,6 +409,7 @@ class _TerminalView extends LeafRenderObjectWidget {
     required this.padding,
     required this.autoResize,
     required this.textStyle,
+    required this.textScaleFactor,
     required this.theme,
     required this.focusNode,
     required this.cursorType,
@@ -428,6 +430,8 @@ class _TerminalView extends LeafRenderObjectWidget {
 
   final TerminalStyle textStyle;
 
+  final double textScaleFactor;
+
   final TerminalTheme theme;
 
   final FocusNode focusNode;
@@ -449,6 +453,7 @@ class _TerminalView extends LeafRenderObjectWidget {
       padding: padding,
       autoResize: autoResize,
       textStyle: textStyle,
+      textScaleFactor: textScaleFactor,
       theme: theme,
       focusNode: focusNode,
       cursorType: cursorType,
@@ -467,6 +472,7 @@ class _TerminalView extends LeafRenderObjectWidget {
       ..padding = padding
       ..autoResize = autoResize
       ..textStyle = textStyle
+      ..textScaleFactor = textScaleFactor
       ..theme = theme
       ..focusNode = focusNode
       ..cursorType = cursorType

+ 2 - 2
lib/src/ui/char_metrics.dart

@@ -2,12 +2,12 @@ import 'dart:ui';
 
 import 'package:xterm/src/ui/terminal_text_style.dart';
 
-Size calcCharSize(TerminalStyle style) {
+Size calcCharSize(TerminalStyle style, double textScaleFactor) {
   const test = 'mmmmmmmmmm';
 
   final textStyle = style.toTextStyle();
   final builder = ParagraphBuilder(textStyle.getParagraphStyle());
-  builder.pushStyle(textStyle.getTextStyle());
+  builder.pushStyle(textStyle.getTextStyle(textScaleFactor: textScaleFactor));
   builder.addText(test);
 
   final paragraph = builder.build();

+ 2 - 2
lib/src/ui/paragraph_cache.dart

@@ -13,9 +13,9 @@ class ParagraphCache {
     return _cache[key];
   }
 
-  Paragraph performAndCacheLayout(String text, TextStyle style, int key) {
+  Paragraph performAndCacheLayout(String text, TextStyle style, double textScaleFactor, int key) {
     final builder = ParagraphBuilder(style.getParagraphStyle());
-    builder.pushStyle(style.getTextStyle());
+    builder.pushStyle(style.getTextStyle(textScaleFactor: textScaleFactor));
     builder.addText(text);
 
     final paragraph = builder.build();

+ 19 - 3
lib/src/ui/render.dart

@@ -32,6 +32,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
     required EdgeInsets padding,
     required bool autoResize,
     required TerminalStyle textStyle,
+    required double textScaleFactor,
     required TerminalTheme theme,
     required FocusNode focusNode,
     required TerminalCursorType cursorType,
@@ -44,6 +45,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
         _padding = padding,
         _autoResize = autoResize,
         _textStyle = textStyle,
+        _textScaleFactor = textScaleFactor,
         _theme = theme,
         _focusNode = focusNode,
         _cursorType = cursorType,
@@ -51,7 +53,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
         _onEditableRect = onEditableRect,
         _composingText = composingText {
     _updateColorPalette();
-    _updateCharSize();
+    _charSize = calcCharSize(_textStyle, _textScaleFactor);
   }
 
   Terminal _terminal;
@@ -104,6 +106,14 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
     markNeedsLayout();
   }
 
+  double _textScaleFactor;
+  set textScaleFactor(double value) {
+    if (value == _textScaleFactor) return;
+    _textScaleFactor = value;
+    _updateCharSize();
+    markNeedsLayout();
+  }
+
   TerminalTheme _theme;
   set theme(TerminalTheme value) {
     if (value == _theme) return;
@@ -168,7 +178,12 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
   /// Updates [_charSize] based on the current [_textStyle]. This should be
   /// called whenever the [_textStyle] changes or the system font changes.
   void _updateCharSize() {
-    _charSize = calcCharSize(_textStyle);
+    final charSize = calcCharSize(_textStyle, _textScaleFactor);
+    // rebuild on changed char size
+    if (charSize != _charSize) {
+      _paragraphCache.clear();
+    }
+    _charSize = charSize;
   }
 
   var _stickToBottom = true;
@@ -493,7 +508,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
       _charSize.height,
       PlaceholderAlignment.middle,
     );
-    builder.pushStyle(style.getTextStyle());
+    builder.pushStyle(style.getTextStyle(textScaleFactor: _textScaleFactor));
     builder.addText(composingText);
 
     final paragraph = builder.build();
@@ -611,6 +626,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
       paragraph = _paragraphCache.performAndCacheLayout(
         char,
         style,
+        _textScaleFactor,
         hash,
       );
     }