-
Couldn't load subscription status.
- Fork 6k
[web] Add support for textScaleFactor #29028
Changes from all commits
7cb6389
15ffe21
5c4689f
882228c
bd1cc67
f144ea9
fba505e
c58f1de
9fb0d25
46bb77b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| import 'dart:async'; | ||
| import 'dart:convert'; | ||
| import 'dart:html' as html; | ||
| import 'dart:js_util' as js_util; | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
|
|
@@ -53,8 +54,10 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher { | |
| /// The current platform configuration. | ||
| @override | ||
| ui.PlatformConfiguration get configuration => _configuration; | ||
| ui.PlatformConfiguration _configuration = | ||
| ui.PlatformConfiguration(locales: parseBrowserLanguages()); | ||
| ui.PlatformConfiguration _configuration = ui.PlatformConfiguration( | ||
| locales: parseBrowserLanguages(), | ||
| textScaleFactor: findBrowserTextScaleFactor(), | ||
| ); | ||
|
|
||
| /// Receives all events related to platform configuration changes. | ||
| @override | ||
|
|
@@ -1076,3 +1079,41 @@ void invoke3<A1, A2, A3>(void Function(A1 a1, A2 a2, A3 a3)? callback, | |
| }); | ||
| } | ||
| } | ||
|
|
||
| const double _defaultRootFontSize = 16.0; | ||
|
|
||
| /// Finds the text scale factor of the browser by looking at the computed style | ||
| /// of the browser's <html> element. | ||
| double findBrowserTextScaleFactor() { | ||
| final num fontSize = _parseFontSize(html.document.documentElement!) ?? _defaultRootFontSize; | ||
| return fontSize / _defaultRootFontSize; | ||
| } | ||
|
|
||
| /// Parses the font size of [element] and returns the value without a unit. | ||
| num? _parseFontSize(html.Element element) { | ||
| num? fontSize; | ||
|
|
||
| if (js_util.hasProperty(element, 'computedStyleMap')) { | ||
| // Use the newer `computedStyleMap` API available on some browsers. | ||
| final dynamic computedStyleMap = | ||
| // ignore: implicit_dynamic_function | ||
| js_util.callMethod(element, 'computedStyleMap', <Object?>[]); | ||
| if (computedStyleMap is Object) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a null check PLUS a type check/inference so when I pass it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strange that |
||
| final dynamic fontSizeObject = | ||
| // ignore: implicit_dynamic_function | ||
| js_util.callMethod(computedStyleMap, 'get', <Object?>['font-size']); | ||
| if (fontSizeObject is Object) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| // ignore: implicit_dynamic_function | ||
| fontSize = js_util.getProperty(fontSizeObject, 'value') as num; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (fontSize == null) { | ||
| // Fallback to `getComputedStyle`. | ||
| final String fontSizeString = element.getComputedStyle().fontSize; | ||
| fontSize = parseFloat(fontSizeString); | ||
| } | ||
|
|
||
| return fontSize; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered using https://api.flutter.dev/flutter/dart-html/Window/getComputedStyleMap.html?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried it. It throws an exception saying that
window.getComputedStyleMapis undefined. I also looked online and couldn't find this property onwindow. The only one that exists iswindow.getComputedStylewhich I'm already using below throughdart:html.