Skip to content

Commit 5d0310f

Browse files
authored
[web] Separate the height ruler from the other rulers (flutter#22964)
1 parent 2bc94c4 commit 5d0310f

File tree

3 files changed

+180
-86
lines changed

3 files changed

+180
-86
lines changed

lib/web_ui/lib/src/engine/text/measurement.dart

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ bool _newlinePredicate(int char) {
2323
prop == LineCharProperty.CR;
2424
}
2525

26-
/// Manages [ParagraphRuler] instances and caches them per unique
27-
/// [ParagraphGeometricStyle].
28-
///
29-
/// All instances of [ParagraphRuler] should be created through this class.
30-
class RulerManager {
31-
RulerManager({required this.rulerCacheCapacity}) {
26+
/// Hosts ruler DOM elements in a hidden container.
27+
class RulerHost {
28+
RulerHost() {
3229
_rulerHost.style
3330
..position = 'fixed'
3431
..visibility = 'hidden'
@@ -41,8 +38,6 @@ class RulerManager {
4138
registerHotRestartListener(dispose);
4239
}
4340

44-
final int rulerCacheCapacity;
45-
4641
/// Hosts a cache of rulers that measure text.
4742
///
4843
/// This element exists purely for organizational purposes. Otherwise the
@@ -51,6 +46,28 @@ class RulerManager {
5146
/// purpose.
5247
final html.Element _rulerHost = html.Element.tag('flt-ruler-host');
5348

49+
/// Releases the resources used by this [RulerHost].
50+
///
51+
/// After this is called, this object is no longer usable.
52+
void dispose() {
53+
_rulerHost.remove();
54+
}
55+
56+
/// Adds an element used for measuring text as a child of [_rulerHost].
57+
void addElement(html.HtmlElement element) {
58+
_rulerHost.append(element);
59+
}
60+
}
61+
62+
/// Manages [ParagraphRuler] instances and caches them per unique
63+
/// [ParagraphGeometricStyle].
64+
///
65+
/// All instances of [ParagraphRuler] should be created through this class.
66+
class RulerManager extends RulerHost {
67+
RulerManager({required this.rulerCacheCapacity}): super();
68+
69+
final int rulerCacheCapacity;
70+
5471
/// The cache of rulers used to measure text.
5572
///
5673
/// Each ruler is keyed by paragraph style. This allows us to setup the
@@ -78,13 +95,6 @@ class RulerManager {
7895
}
7996
}
8097

81-
/// Releases the resources used by this [RulerManager].
82-
///
83-
/// After this is called, this object is no longer usable.
84-
void dispose() {
85-
_rulerHost.remove();
86-
}
87-
8898
// Evicts all rulers from the cache.
8999
void _evictAllRulers() {
90100
_rulers.forEach((ParagraphGeometricStyle style, ParagraphRuler ruler) {
@@ -129,11 +139,6 @@ class RulerManager {
129139
}
130140
}
131141

132-
/// Adds an element used for measuring text as a child of [_rulerHost].
133-
void addHostElement(html.DivElement element) {
134-
_rulerHost.append(element);
135-
}
136-
137142
/// Performs a cache lookup to find an existing [ParagraphRuler] for the given
138143
/// [style] and if it can't find one in the cache, it would create one.
139144
///
@@ -479,7 +484,7 @@ class DomTextMeasurementService extends TextMeasurementService {
479484
height = naturalHeight;
480485
} else {
481486
// Lazily compute [lineHeight] when [maxLines] is not null.
482-
lineHeight = ruler.lineHeightDimensions!.height;
487+
lineHeight = ruler.lineHeight;
483488
height = math.min(naturalHeight, maxLines * lineHeight);
484489
}
485490

@@ -587,8 +592,9 @@ class CanvasTextMeasurementService extends TextMeasurementService {
587592
}
588593
}
589594

595+
final double alphabeticBaseline = ruler.alphabeticBaseline;
590596
final int lineCount = linesCalculator.lines.length;
591-
final double lineHeight = ruler.lineHeightDimensions!.height;
597+
final double lineHeight = ruler.lineHeight;
592598
final double naturalHeight = lineCount * lineHeight;
593599
final int? maxLines = style.maxLines;
594600
final double height = maxLines == null
@@ -598,8 +604,8 @@ class CanvasTextMeasurementService extends TextMeasurementService {
598604
final MeasurementResult result = MeasurementResult(
599605
constraints.width,
600606
isSingleLine: lineCount == 1,
601-
alphabeticBaseline: ruler.alphabeticBaseline,
602-
ideographicBaseline: ruler.alphabeticBaseline * _baselineRatioHack,
607+
alphabeticBaseline: alphabeticBaseline,
608+
ideographicBaseline: alphabeticBaseline * _baselineRatioHack,
603609
height: height,
604610
naturalHeight: naturalHeight,
605611
lineHeight: lineHeight,

0 commit comments

Comments
 (0)