-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Add support for textScaleFactor #29028
Conversation
@mdebbar I think you can set it in |
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.
| // Using JavaScript's `window.parseFloat` here because it can parse values | ||
| // like "20px", while Dart's `double.tryParse` fails. | ||
| final num? result = | ||
| js_util.callMethod(html.window, 'parseFloat', <Object>[source]) as num?; |
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.
Is it possible to use package:js for this (see canvaskit_api.dart)?
(applies throughout the file)
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 was able to use package:js for parseFloat. But I'm not sure how to do that for others like:
js_util.callMethod(element, 'computedStyleMap', <Object?>[])
Is this possible to convert to package:js?
| if (js_util.hasProperty(element, 'computedStyleMap')) { | ||
| // Use the newer `computedStyleMap` API available on some browsers. | ||
| final dynamic computedStyleMap = | ||
| js_util.callMethod(element, 'computedStyleMap', <Object?>[]); |
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.getComputedStyleMap is undefined. I also looked online and couldn't find this property on window. The only one that exists is window.getComputedStyle which I'm already using below through dart:html.
| // Use the newer `computedStyleMap` API available on some browsers. | ||
| final dynamic computedStyleMap = | ||
| js_util.callMethod(element, 'computedStyleMap', <Object?>[]); | ||
| if (computedStyleMap is Object) { |
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.
Is is Object a null check? If so, why not != null?
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.
It is a null check PLUS a type check/inference so when I pass it to js_util.callMethod I don't need to do as Object.
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.
Strange that != null doesn't promote to Object automatically. Generics allow you to declare non-nullness by doing <T extends Object>, so I'd expect != null to also be equivalent to is Object. But oh well.
| if (computedStyleMap is Object) { | ||
| final dynamic fontSizeObject = | ||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
|
Several of the Dart
final num? result =
js_util.callMethod(html.window, 'parseFloat', <Object>[source]); // remove the `as num?`
js_util.callMethod<void>(o, 'foo', []);
final dynamic fontSizeObject =
js_util.callMethod<dynamic>(computedStyleMap, 'get', <Object?>['font-size']);Let me know if any of that is confusing or if you need any help fixing this code for the analyzer. Hopefully it will be a smooth change and make the js_util code easier to read in the future. |
|
@rileyporter thanks for the details! I just pulled the latest, and I can see this commit which rolls your dart-sdk change into the engine. But in my IDE (VS Code), I still don't see the analyzer errors that you are talking about. The PR has also passed all the checks (including the analyzer) without me doing any changes. |
|
Sorry, my change did end up getting reverted. There is a dart linter bug that my change exposes which triggers for a couple cases in internal google code with false positives. The linter bug has been fixed, but it hasn't been rolled internally yet. I'm disabling the lints in the google code temporarily so I can re-land my change to hopefully keep other folks from writing more Flutter code using js_util that will need to be migrated. I downloaded a patch of this PR to verify my change does cause analyzer issues with the Can you add Sorry for the inconvenience, I'll remove those comments and add explicit types in this PR after the generic types land. |



Read the computed font size of the
<html>element and use that as a guide to figure out thetextScaleFactor. The default value should be 16.I was able to try this on Chrome and Firefox. I couldn't find a way to change the text scale factor in Safari.
Fixes flutter/flutter#52061