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

Conversation

@flar
Copy link
Contributor

@flar flar commented Dec 18, 2019

In a recent PR to add a new pushFooLayer() API to the SceneBuilder class in engine, the code format of the pushLayer methods was pointed out as not quite matching the flutter framework style. This PR is looking to find a suitable code style for these methods to embrace moving forward.

Most of the methods were formatted in this initial push to adopt the style suggested in the pushImageFilter PR, but it wasn't clear how to adapt the pushShaderMask() method to comply and I tried 2 variants here in the 2 different files for comparison and feedback.

dartfmt suggests that the fixed parameters be moved to separate lines as well, similar to pushPhysicalShape(). Note that the "one line per argument" style is not really embraced in the engine repo right now, so I hesitated on taking that suggestion to diverge even further.

The formatter built in to Android Studio with the Dart plugin also fights this style somewhat. If all of the arguments are moved to separate lines it insists on 4-space indentation which doesn't match the flutter style, but if only the named parameters (inside {}) are on their own lines then it seems perfectly happy with 2-space indentation.

Also, currently the main (non-web) SceneBuilder push...Layer() method code style seems to be universally "everything on one line regardless of how long the line is", which doesn't look very maintainable so that file should probably be consolidated with whatever format is deemed appropriate here for the web versions of that class.

@yjbanov
Copy link
Contributor

yjbanov commented Dec 18, 2019

The change LGTM. The new code does read nicer. Did you just add trailing commas and run dartfmt on the code? We currently use dartfmt in the Web implementation.

@flar
Copy link
Contributor Author

flar commented Dec 18, 2019

The change LGTM. The new code does read nicer. Did you just add trailing commas and run dartfmt on the code? We currently use dartfmt in the Web implementation.

Thoughts on the 2 different ways to format pushShaderMask()?

Actually, I just took the suggestion from @Hixie in this PR. If I run dartfmt on the new version of the files, though, it wants to move the fixed arguments into separate lines as well. If the web team is in the habit of running dartfmt on the dart source files then perhaps I should take its suggestion. I'm attaching diffs suggested by dartfmt.

compositing.dart diff
scene_builder.dart diff

@flar
Copy link
Contributor Author

flar commented Dec 18, 2019

To be clear, this is what the Dart plugin in Android Studio wants to do with pushOffsetLayer if I try to move the fixed arguments to their own lines as dartfmt suggests:

  OffsetEngineLayer pushOffset(
      double dx,
      double dy, {
        OffsetEngineLayer oldLayer,
      });

I can reduce the indentation to 2 spaces by changing the continuation indent, but then continuations are wrong. I don't see any way to get it to not do an extra indent on the named parameters, though.

@yjbanov
Copy link
Contributor

yjbanov commented Dec 19, 2019

Thoughts on the 2 different ways to format pushShaderMask()?

I'd like to abstain :)

If the web team is in the habit of running dartfmt on the dart source files then perhaps I should take its suggestion.

Yes, let's apply dartfmt. If you don't use it now, someone else will eventually anyway.

@flar
Copy link
Contributor Author

flar commented Dec 19, 2019

I added a few more trailing commas and ran dartfmt on all files that implement a SceneBuilder class.

I've now also adjusted the main version used for the native engine so I should get an approval from @liyuqian or @cbracken as well as @yjbanov

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 for the lib/web_ui parts

Copy link
Contributor

@liyuqian liyuqian left a comment

Choose a reason for hiding this comment

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

LGTM

@flar
Copy link
Contributor Author

flar commented Dec 19, 2019

It seems that the analyzer likes things like:

    if (isComplexHint)
      hints |= 1;

but dartfmt decides it should be a single line, as in:

    if (isComplexHint) hints |= 1;

which the analyzer then complains about because it always wants conditional bodies on separate lines. Rather than making it even more verbose by adding {} braces, I decided to use ?: to initialize the hints in a single line.

    final int hints = (isComplexHint ? 1 : 0) | ...;

Thoughts @liyuqian?

bool isComplexHint = false,
bool willChangeHint = false,
}) {
final int hints = (isComplexHint ? 1 : 0) | (willChangeHint ? 2 : 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@liyuqian this was, debatably, the best compromise I could find to make both dartfmt and the analyzer happy without expanding this code section even more with {} braces and such. Details in the PR comments.

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM

@liyuqian
Copy link
Contributor

It seems that the analyzer likes things like:

    if (isComplexHint)
      hints |= 1;

but dartfmt decides it should be a single line, as in:

    if (isComplexHint) hints |= 1;

which the analyzer then complains about because it always wants conditional bodies on separate lines. Rather than making it even more verbose by adding {} braces, I decided to use ?: to initialize the hints in a single line.

    final int hints = (isComplexHint ? 1 : 0) | ...;

Thoughts @liyuqian?

I like your new approach. In general, feel free to not strictly follow dartfmt if it conflicts with analyzer. I would also sometimes just reformat manually if I think dartfmt is not doing the right thing.

@flar flar merged commit 22413ef into flutter:master Dec 19, 2019
@yjbanov
Copy link
Contributor

yjbanov commented Dec 19, 2019

I believe over in the flutter/flutter side we're recommending to always use { }. This way both dartfmt and hand-written style do the same thing.

@liyuqian
Copy link
Contributor

I believe over in the flutter/flutter side we're recommending to always use { }. This way both dartfmt and hand-written style do the same thing.

There's a similar debate in C++ code too. I believe many prefer

if (x) {
 y;
}

over just if (x) y;.

But some prefer if (x) y;. I'm in the former case because Skia enforces the more verbose format, and I get used to it.

@flar
Copy link
Contributor Author

flar commented Dec 19, 2019

I like the use of {} in all cases because that minimizes diffs later if a second statement needs to be added to the body.

I decided to go with a style that was compatible with dartfmt in case, as @yjbanov said earlier, someone later decides to reformat the file.

I only like if (x) y; if the "y" is very simple, like a return. I was OK with the use here to set some hints, but not if it meant {}. Also, ?: works pretty well here.

engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 20, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 20, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 20, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 20, 2019
flar pushed a commit to flutter/flutter that referenced this pull request Dec 20, 2019
* bb118c6 Roll src/third_party/dart 8d11c1dce64a..642f8d052fd7 (1 commits) (flutter/engine#14574)

* 09c434d Use ELF for Dart AOT snapshots on Fuchsia. (flutter/engine#13896)

* e1e7851 Roll fuchsia/sdk/core/linux-amd64 from VdBKA... to uFFWW... (flutter/engine#14575)

* f317f8f Roll src/third_party/skia c76ac8e325c7..77742c350371 (1 commits) (flutter/engine#14576)

* 2ba5633 instructions for running firefox/safari tests (flutter/engine#14562)

* 9cf1e46 Roll src/third_party/dart 642f8d052fd7..7113fc79a83c (3 commits) (flutter/engine#14578)

* f5b877a [web] Run engine tests on Safari locally by launching safari installed on MacOS (flutter/engine#14555)

* 68d9196 Fix DOM-based ParagraphRuler.hitTest() (flutter/engine#14504)

* bb65df8 Roll src/third_party/skia 77742c350371..a8352ccaae37 (8 commits) (flutter/engine#14579)

* ad1ab56 Roll src/third_party/dart 7113fc79a83c..e50d98cd5651 (8 commits) (flutter/engine#14580)

* 22413ef Update formatting in web_ui scene bulder to match flutter style and dartfmt. (flutter/engine#14539)

* 6e825e7 Roll fuchsia/sdk/core/mac-amd64 from Ykb4b... to f51Q_... (flutter/engine#14584)

* 1d3bb8c Fix message_loop_fuchsia and thus enable fml_tests and flow_tests for Fuchsia (flutter/engine#14583)

* 1f7bb9d Wire up OpacityLayer to Scenic (flutter/engine#14577)

* 11db035 Roll src/third_party/skia a8352ccaae37..87e9ddb675b6 (11 commits) (flutter/engine#14585)

* bd58af7 Roll src/third_party/dart e50d98cd5651..141fcfa61092 (3 commits) (flutter/engine#14586)

* 929b1ed Engine support for ImageFiltered widget (flutter/engine#14491)

* 40b84fc Fix lint warnings across web_ui, add missing browserEngine case in text field. (flutter/engine#14535)

* ea1d330 Roll fuchsia/sdk/core/linux-amd64 from uFFWW... to 25LzW... (flutter/engine#14587)

* 854d5f8 Roll src/third_party/skia 87e9ddb675b6..7e2dea568299 (1 commits) (flutter/engine#14589)
filmil pushed a commit to filmil/engine that referenced this pull request Mar 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants