Skip to content

Commit

Permalink
[web] Separate the height ruler from the other rulers (flutter#22964)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdebbar authored Dec 14, 2020
1 parent 2bc94c4 commit 5d0310f
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 86 deletions.
54 changes: 30 additions & 24 deletions lib/web_ui/lib/src/engine/text/measurement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ bool _newlinePredicate(int char) {
prop == LineCharProperty.CR;
}

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

final int rulerCacheCapacity;

/// Hosts a cache of rulers that measure text.
///
/// This element exists purely for organizational purposes. Otherwise the
Expand All @@ -51,6 +46,28 @@ class RulerManager {
/// purpose.
final html.Element _rulerHost = html.Element.tag('flt-ruler-host');

/// Releases the resources used by this [RulerHost].
///
/// After this is called, this object is no longer usable.
void dispose() {
_rulerHost.remove();
}

/// Adds an element used for measuring text as a child of [_rulerHost].
void addElement(html.HtmlElement element) {
_rulerHost.append(element);
}
}

/// Manages [ParagraphRuler] instances and caches them per unique
/// [ParagraphGeometricStyle].
///
/// All instances of [ParagraphRuler] should be created through this class.
class RulerManager extends RulerHost {
RulerManager({required this.rulerCacheCapacity}): super();

final int rulerCacheCapacity;

/// The cache of rulers used to measure text.
///
/// Each ruler is keyed by paragraph style. This allows us to setup the
Expand Down Expand Up @@ -78,13 +95,6 @@ class RulerManager {
}
}

/// Releases the resources used by this [RulerManager].
///
/// After this is called, this object is no longer usable.
void dispose() {
_rulerHost.remove();
}

// Evicts all rulers from the cache.
void _evictAllRulers() {
_rulers.forEach((ParagraphGeometricStyle style, ParagraphRuler ruler) {
Expand Down Expand Up @@ -129,11 +139,6 @@ class RulerManager {
}
}

/// Adds an element used for measuring text as a child of [_rulerHost].
void addHostElement(html.DivElement element) {
_rulerHost.append(element);
}

/// Performs a cache lookup to find an existing [ParagraphRuler] for the given
/// [style] and if it can't find one in the cache, it would create one.
///
Expand Down Expand Up @@ -479,7 +484,7 @@ class DomTextMeasurementService extends TextMeasurementService {
height = naturalHeight;
} else {
// Lazily compute [lineHeight] when [maxLines] is not null.
lineHeight = ruler.lineHeightDimensions!.height;
lineHeight = ruler.lineHeight;
height = math.min(naturalHeight, maxLines * lineHeight);
}

Expand Down Expand Up @@ -587,8 +592,9 @@ class CanvasTextMeasurementService extends TextMeasurementService {
}
}

final double alphabeticBaseline = ruler.alphabeticBaseline;
final int lineCount = linesCalculator.lines.length;
final double lineHeight = ruler.lineHeightDimensions!.height;
final double lineHeight = ruler.lineHeight;
final double naturalHeight = lineCount * lineHeight;
final int? maxLines = style.maxLines;
final double height = maxLines == null
Expand All @@ -598,8 +604,8 @@ class CanvasTextMeasurementService extends TextMeasurementService {
final MeasurementResult result = MeasurementResult(
constraints.width,
isSingleLine: lineCount == 1,
alphabeticBaseline: ruler.alphabeticBaseline,
ideographicBaseline: ruler.alphabeticBaseline * _baselineRatioHack,
alphabeticBaseline: alphabeticBaseline,
ideographicBaseline: alphabeticBaseline * _baselineRatioHack,
height: height,
naturalHeight: naturalHeight,
lineHeight: lineHeight,
Expand Down
Loading

0 comments on commit 5d0310f

Please sign in to comment.