Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

@mdebbar
Copy link
Contributor

@mdebbar mdebbar commented Oct 5, 2021

Read the computed font size of the <html> element and use that as a guide to figure out the textScaleFactor. The default value should be 16.

$$textScaleFactor = (font size of `<html>`) / 16.0$$

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

@mdebbar mdebbar added the platform-web Code specifically for the web engine label Oct 5, 2021
@mdebbar mdebbar requested a review from yjbanov October 5, 2021 20:23
@google-cla google-cla bot added the cla: yes label Oct 5, 2021
@mdebbar mdebbar requested a review from ditman October 7, 2021 13:59
@ditman
Copy link
Member

ditman commented Oct 8, 2021

I couldn't find a way to change the text scale factor in Safari.

@mdebbar I think you can set it in Safari -> Settings -> Advanced -> Never use font sizes smaller than...

Screen Shot 2021-10-08 at 9 35 43 AM

Copy link
Contributor

@yjbanov yjbanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nitpicks.

lgtm

// 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?;
Copy link
Contributor

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)

Copy link
Contributor Author

@mdebbar mdebbar Oct 15, 2021

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?>[]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@rileyporter
Copy link
Contributor

Several of the Dart js_util functions now have a generic return type which will affect this. It looks like the change to add the generic return type landed and rolled into Flutter. So far, there have been no analyzer errors, so I don't anticipate needing to roll this change back. You will probably get implicit_dynamic_function analyzer errors on this code now that you can fix by adding explicit types. Examples:

  • Explicit casts are no longer necessary and they will cause implicit dynamic casts, so they should be removed when the type can be inferred (i.e. from the variable type declaration) e.g.:
final num? result =
    js_util.callMethod(html.window, 'parseFloat', <Object>[source]); // remove the `as num?`
  • Declare the generic the type explicitly when the type cannot be inferred, or where the type is declared as dynamic, e.g:
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.

@mdebbar
Copy link
Contributor Author

mdebbar commented Oct 15, 2021

@mdebbar I think you can set it in Safari -> Settings -> Advanced -> Never use font sizes smaller than...

@ditman thanks for the tip! That worked and I was able to confirm that we read textScaleFactor correctly on Safari too.

@mdebbar
Copy link
Contributor Author

mdebbar commented Oct 15, 2021

@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.

@mdebbar mdebbar requested a review from yjbanov October 15, 2021 15:59
@rileyporter
Copy link
Contributor

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 js_util usage:

dart analyze --fatal-infos
Analyzing web_ui...                    4.7s

  error • lib/src/engine/platform_dispatcher.dart:1099:17 • Missing type arguments for generic function 'callMethod<T>'. Try adding an explicit type, or remove
          implicit-dynamic from your analysis options file. • implicit_dynamic_function
  error • lib/src/engine/platform_dispatcher.dart:1102:19 • Missing type arguments for generic function 'callMethod<T>'. Try adding an explicit type, or remove
          implicit-dynamic from your analysis options file. • implicit_dynamic_function
  error • lib/src/engine/platform_dispatcher.dart:1104:28 • Missing type arguments for generic function 'getProperty<T>'. Try adding an explicit type, or remove
          implicit-dynamic from your analysis options file. • implicit_dynamic_function

3 issues found.

Can you add // ignore: implicit_dynamic_function to these three places in this PR to keep it from breaking when I re-land?
image

Sorry for the inconvenience, I'll remove those comments and add explicit types in this PR after the generic types land.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla: yes platform-web Code specifically for the web engine

Projects

None yet

Development

Successfully merging this pull request may close these issues.

textScaleFactor not working on web when text scale change in Browser/System Level

4 participants