Skip to content

Conversation

@nate-thegrate
Copy link
Contributor

This PR is the 8ᵗʰ step in the journey to solve issue #136139 and make the entire Flutter repo more readable.

(previous pull requests: #139048, #139882, #141591, #142279, #142634, #142793, #143293)

I did a pass through all of packages/flutter/lib/src/ and found a whole bunch of switch statements to improve: most of them were really simple, but many involved some thorough refactoring.

This pull request is just the complicated stuff. 😎 I'll make comments to describe the changes, and then in the future there will be another PR (and it'll be much easier to review than this one).

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. f: scrolling Viewports, list views, slivers, etc. f: cupertino flutter/packages/flutter/cupertino repository labels Feb 14, 2024
@nate-thegrate nate-thegrate marked this pull request as ready for review February 15, 2024 01:29
Comment on lines 1926 to +1932
switch (textDirection!) {
case TextDirection.rtl:
return Offset(x - boxSize.width, (sizes.content - boxSize.height + sizes.densityAdjustment.dy) / 2.0);
x -= boxSize.width;
case TextDirection.ltr:
return Offset(x, (sizes.content - boxSize.height + sizes.densityAdjustment.dy) / 2.0);
break;
}
return Offset(x, (sizes.content - boxSize.height + sizes.densityAdjustment.dy) / 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.

I didn't implement a switch expression here, but I did notice that the only difference between the two Offsets was that the rtl has a - boxSize.width. The way it's formatted now hopefully makes that easier to see.

Comment on lines -1732 to -1740
bool rightWayUp;
switch (constraints.axisDirection) {
case AxisDirection.up:
case AxisDirection.left:
rightWayUp = false;
case AxisDirection.down:
case AxisDirection.right:
rightWayUp = true;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

rightWayUp was set up to be true for AxisDirection.down & AxisDirection.right, and false otherwise.

But axisDirectionIsReversed() already does the same thing, just swapped.

assert(child.parent == this);
assert(child is RenderSliver);
final RenderSliver sliver = child as RenderSliver;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code has a lot in common with the getOffsetToReveal() function that I'd refactored previously, so I made similar changes here.

Comment on lines +148 to 154
if (modifiers & (leftMask | rightMask | anyMask) == anyMask) {
// If only the "anyMask" bit is set, then we respond true for requests of
// whether either left or right is pressed. Handles the case where iOS
// supplies just the "either" modifier flag, but not the left/right flag.
// (e.g. modifierShift but not modifierLeftShift).
return true;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code was originally set up as follows:

switch (side) {
  case KeyboardSide.any:
    return true;
  case KeyboardSide.all:
    return [expression] || anyOnly;
  case KeyboardSide.left:
    return [other expression] || anyOnly;
  case KeyboardSide.right:
    return [other expression] || anyOnly;
}

So if anyOnly was true, the function would return true no matter what. I think it's easier to follow when the function just returns true right away instead of creating a variable.

Comment on lines +734 to +737
final AxisDirection direction = switch (stretchDirection) {
_StretchDirection.trailing => widget.axisDirection,
_StretchDirection.leading => flipAxisDirection(widget.axisDirection),
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can avoid all those conditional expressions by just flipping the direction beforehand based on the stretchDirection.

Comment on lines +429 to +433
if (axisDirectionToAxis(intent.direction) == axisDirectionToAxis(state.axisDirection)) {
final double increment = _calculateScrollIncrement(state, type: intent.type);
return intent.direction == state.axisDirection ? increment : -increment;
}
return 0.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.

Saved the best for last! This function body has been reduced from 43 lines down to 5, and _calculateScrollIncrement() will no longer be called if it isn't needed.

@nate-thegrate nate-thegrate changed the title switch expressions, episode 8: refactoring flutter/lib/src/ Implementing switch expressions [refactoring flutter/lib/src/] Feb 15, 2024
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact "@test-exemption-reviewer" in the #hackers channel in Chat (don't just cc them here, they won't see it! Use Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

(true, false, true) => darkHighContrastColor,
(true, true, false) => darkElevatedColor,
(true, true, true) => darkHighContrastElevatedColor,
};
Copy link
Member

Choose a reason for hiding this comment

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

I find this sea of false/true makes it harder to understand what is actually going on. Is there a better way to express this that preserve a little more context of what is false/true?

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'm not a huge fan of the long CupertinoUserInterfaceLevelData name, but you have a good point that 3 bools in a row is difficult to follow. Hopefully it looks better now!

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this looks better. I wish Dart would support something similar to Swift where these could just be:

(.light, .base, false) => color,

Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

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

LGTM

(true, false, true) => darkHighContrastColor,
(true, true, false) => darkElevatedColor,
(true, true, true) => darkHighContrastElevatedColor,
};
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this looks better. I wish Dart would support something similar to Swift where these could just be:

(.light, .base, false) => color,

(drawerIsStart ? padding.right : padding.left);
}
}
final double dragAreaWidth = widget.edgeDragWidth ?? _kEdgeDragWidth + switch ((widget.alignment, Directionality.of(context))) {
Copy link
Member

@loic-sharma loic-sharma Feb 16, 2024

Choose a reason for hiding this comment

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

Can we break this up into multiple steps? This is a bit too busy in my opinion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! I made whitespace changes for readability; we could break it into multiple lines (but it would no longer be final and non-nullable):

double? dragAreaWidth = widget.edgeDragWidth;
dragAreaWidth ??= _kEdgeDragWidth + switch ((widget.alignment, Directionality.of(context))) {
  (DrawerAlignment.start, TextDirection.ltr) => MediaQuery.paddingOf(context).left,
  (DrawerAlignment.start, TextDirection.rtl) => MediaQuery.paddingOf(context).right,
  (DrawerAlignment.end,   TextDirection.rtl) => MediaQuery.paddingOf(context).left,
  (DrawerAlignment.end,   TextDirection.ltr) => MediaQuery.paddingOf(context).right,
};

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha. Feel free to keep it as is then :)

@loic-sharma loic-sharma added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 16, 2024
@auto-submit auto-submit bot merged commit 944cd11 into flutter:master Feb 16, 2024
@nate-thegrate nate-thegrate deleted the switch-expressions-8 branch February 16, 2024 20:20
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 17, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 17, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 18, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 18, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 19, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 19, 2024
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Feb 19, 2024
Roll Flutter from d7867ca to 064c340 (33 revisions)

flutter/flutter@d7867ca...064c340

2024-02-19 [email protected] Roll Flutter Engine from f71b7eee2266 to 714215d42e57 (1 revision) (flutter/flutter#143708)
2024-02-19 [email protected] Roll Flutter Engine from 07c73b0c8413 to f71b7eee2266 (2 revisions) (flutter/flutter#143694)
2024-02-19 [email protected] Roll Flutter Engine from 80d6745c6fd6 to 07c73b0c8413 (1 revision) (flutter/flutter#143682)
2024-02-19 [email protected] Roll Flutter Engine from 67abf6eb36a1 to 80d6745c6fd6 (1 revision) (flutter/flutter#143676)
2024-02-18 [email protected] Roll Flutter Engine from 8ae232196fc9 to 67abf6eb36a1 (1 revision) (flutter/flutter#143673)
2024-02-18 [email protected] Roll Flutter Engine from f636f9635438 to 8ae232196fc9 (1 revision) (flutter/flutter#143659)
2024-02-18 [email protected] Fixed the issue of incorrect item position when prototypeItem is set in SliverReorderableList. (flutter/flutter#142880)
2024-02-18 [email protected] ShowCaretOnScreen is correctly scheduled within a SliverMainAxisGroup (flutter/flutter#141671)
2024-02-18 [email protected] Roll Flutter Engine from 0ebd580cb8a7 to f636f9635438 (1 revision) (flutter/flutter#143657)
2024-02-17 [email protected] Roll Flutter Engine from c807aeaab89c to 0ebd580cb8a7 (1 revision) (flutter/flutter#143655)
2024-02-17 [email protected] Roll Flutter Engine from e51d4f1e285a to c807aeaab89c (4 revisions) (flutter/flutter#143652)
2024-02-17 [email protected] Manual roll Packages from c56c12d to 0af905d (flutter/flutter#143651)
2024-02-17 [email protected] Add an override annotation to the lineTerminator setter in the MemoryStdout fake class (flutter/flutter#143646)
2024-02-17 [email protected] InputDecorator M3 tests migration - Step3 (flutter/flutter#143520)
2024-02-17 [email protected] Update InputDecoration.contentPadding documentation (flutter/flutter#143519)
2024-02-17 [email protected] [Impeller] skip selectable text goldens for instability. (flutter/flutter#143627)
2024-02-17 [email protected] Roll Flutter Engine from 2ed159a786ef to e51d4f1e285a (2 revisions) (flutter/flutter#143624)
2024-02-17 [email protected] [Impeller] skip perspective transformed text goldens. (flutter/flutter#143623)
2024-02-17 [email protected] [framework] Skip 5 failing framework tests. (flutter/flutter#143618)
2024-02-17 [email protected] Roll Flutter Engine from afb270929a6c to 2ed159a786ef (1 revision) (flutter/flutter#143622)
2024-02-17 [email protected] Roll Flutter Engine from c4fe6f01e0f5 to afb270929a6c (1 revision) (flutter/flutter#143619)
2024-02-16 [email protected] Implement `lineTerminator` in `MemoryStdout` Fake (flutter/flutter#143608)
2024-02-16 [email protected] Roll Flutter Engine from 2eed3fbb293a to c4fe6f01e0f5 (3 revisions) (flutter/flutter#143615)
2024-02-16 [email protected] Don't paint the cursor for an invalid selection (flutter/flutter#143533)
2024-02-16 [email protected] Roll Flutter Engine from 13dc857bf2ef to 2eed3fbb293a (2 revisions) (flutter/flutter#143609)
2024-02-16 [email protected] Fix implementation imports outside of lib (flutter/flutter#143594)
2024-02-16 [email protected] add parsing of assets transformer declarations in pubspec.yaml (flutter/flutter#143557)
2024-02-16 [email protected] Roll Flutter Engine from 5fd5ccf32d08 to 13dc857bf2ef (6 revisions) (flutter/flutter#143607)
2024-02-16 [email protected] Fix SemanticsFinder for multi-view (flutter/flutter#143485)
2024-02-16 [email protected] rebuild the asset bundle if a file has been modified between `flutter test` runs (flutter/flutter#143569)
2024-02-16 [email protected] Added Missing Field Name in Doc Comment in SnackBarThemeData (flutter/flutter#143588)
2024-02-16 [email protected] Implementing `switch` expressions [refactoring `flutter/lib/src/`] (flutter/flutter#143496)
2024-02-16 [email protected] Roll Flutter Engine from dd530f1556df to 5fd5ccf32d08 (3 revisions) (flutter/flutter#143593)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
...
auto-submit bot pushed a commit that referenced this pull request Feb 20, 2024
…143634)

This PR is the 9�ʰ step in the journey to solve issue #136139 and make the entire Flutter repo more readable.

(previous pull requests: #139048, #139882, #141591, #142279, #142634, #142793, #143293, #143496)

I did a pass through all of `packages/flutter/lib/src/` and found an abundance of `switch` statements to improve. Whereas #143496 focused on in-depth refactoring, this PR is full of simple, straightforward changes. (I ended up making some more complicated changes in `rendering/` and will file those separately after this PR is done.)
auto-submit bot pushed a commit that referenced this pull request Feb 26, 2024
This pull request is part of the effort to solve issue #136139.

The previous [`switch` expressions PR](#143496) was comprised of many simple changes throughout `flutter/lib/src/`, but due to some more in-depth refactoring in `flutter/lib/src/rendering/`, I decided to submit the changes to this directory as a separate pull request.

There was really just one function that I changed significantly; I'll add a comment for explanation.
auto-submit bot pushed a commit that referenced this pull request Mar 29, 2024
This pull request is step 12 in the journey to make this entire repository more readable.

(previous PRs: #139048, #139882, #141591, #142279, #142634, #142793, #143293, #143496, #143634, #143812, #144580)

We're getting close to the end! �
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 14, 2024
@nate-thegrate nate-thegrate added the refactor Improving readability/efficiency without behavioral changes label Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. f: scrolling Viewports, list views, slivers, etc. framework flutter/packages/flutter repository. See also f: labels. refactor Improving readability/efficiency without behavioral changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants