Skip to content

Conversation

@jonahwilliams
Copy link
Contributor

@jonahwilliams jonahwilliams commented Jun 16, 2020

Description

A frequent request from the last Flutter developer survey was for an easier method of testing light/dark mode changes. Currently, a user needs to manually change the theme settings or adjust phone settings to see the difference. Instead we should add a toggle from the CLI, and eventually devtools/Intellij/Vscode that allows developers to override the current setting.

Fixes #59495

Adds flutter.ext.brightnessOverride service protocol which either queries the current platform brightness, or overrides it to a new value. This accepts either Brightness.light or Brightness.dark as a value.

Adds a CLI toggle b which allows the setting to be toggled manually.

Requires an update to the MediaQuery, to conditionally use a debug override when not in release mode

@fluttergithubbot fluttergithubbot added framework flutter/packages/flutter repository. See also f: labels. tool Affects the "flutter" command-line tool. See also t: labels. work in progress; do not review labels Jun 16, 2020
/// window's metrics change. For example, see
/// [WidgetsBindingObserver.didChangeMetrics] or [Window.onMetricsChanged].
MediaQueryData.fromWindow(ui.Window window)
MediaQueryData.fromWindow(ui.Window window, { ui.Brightness platformBrightness })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@goderbauer thoughts on this API change? It seems likely to be non-breaking. I could instead create a mocked window ... but this seemed more straightforward

Copy link
Member

Choose a reason for hiding this comment

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

Instead of adding the extra slightly awkward one-off API, could you just do the following in app.dart?

MediaQueryData data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
if (!kReleaseMode) {
  data = data.copyWith(platformBrightness: debugBrightnessOverride);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, I missed the existing copyWith method - that seems more straightforward

@jonahwilliams jonahwilliams marked this pull request as ready for review June 16, 2020 17:52
@jonahwilliams jonahwilliams changed the title [flutter_tools][wip] add toggle b and service protocol to change platform brightness [flutter_tools] add toggle b and service extension to change platform brightness Jun 16, 2020
return value.toStringAsFixed(1);
}

/// A brightness setting that overrides [ui.Brightness] when used through the [BindingBase].
Copy link
Member

Choose a reason for hiding this comment

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

How do you use it "through the [BindingBase]"?

As implemented, it appears to be a debug setting in the widget layer, determining what brightness the MediaQuery of WidgetApp inserts into the tree, no?

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 guess it isn't really true - maybe "Used by MediaQuery to override the platform brightness?"

Copy link
Member

Choose a reason for hiding this comment

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

Sounds better, we should probably move it to widgets/debug.dart then since its more a debug setting of that layer?

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 wasn't sure, the window is exposed through BindingBase which is where the Brightness comes from. If one was hypothetically depending only on foundation, it would not be usable if the brightness was in the widget layer

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that's fair. If we add some more documentation to this property here I am OK with leaving it where it as.

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.

approach seems fine.

}

/// A setting that can be used to override the platform [Brightness] exposed
/// from [BindingBase.window]
Copy link
Member

Choose a reason for hiding this comment

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

nitty nit: missing a .

/// A setting that can be used to override the platform [Brightness] exposed
/// from [BindingBase.window]
///
/// For an example of using [debugBrightnessOverride], see
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just say "See also:" since that's our style. You could incorporate this half-sentence into the explanation after the [WidgetsApp], which below.

name: 'exit',
callback: _exitApplication,
);
registerServiceExtension(
Copy link
Member

Choose a reason for hiding this comment

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

Is this just no longer a thing - or why remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its not a thing

@jonahwilliams jonahwilliams requested a review from jmagman June 16, 2020 19:31
result = await binding.testExtension('brightnessOverride', <String, String>{});
final String brightnessValue = result['value'] as String;

expect(brightnessValue, 'Brightness.light');
Copy link
Member

Choose a reason for hiding this comment

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

Can dark be tested?

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 can't actually change the value in this test or I'll trigger reassemble and break the test framework. I will update the integration test

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 integration test now confirms both brightness settings and that a bogus value does not change anything

CommandHelpOption _b;
CommandHelpOption get b => _b ??= _makeOption(
'b',
'Toggle the platform brightness setting.',
Copy link
Member

Choose a reason for hiding this comment

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

I didn't know what "brightness" meant, maybe 'Toggle the platform brightness (dark and light mode).'?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, fixed

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

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

Impressed how quickly you did this based on survey feedback!

isolateId: isolate.id,
args: <String, String>{
'value': 'dark',
}
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a comment that this is purposely bogus? It looks like a bug in the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@jonahwilliams
Copy link
Contributor Author

Thanks! its not my first rodeo 🐮 🥍 😄

@jmagman
Copy link
Member

jmagman commented Jun 17, 2020

Thanks! its not my first rodeo 🐮 🥍 😄

I said "impressed" not "surprised". 😉

@jonahwilliams jonahwilliams requested a review from goderbauer June 17, 2020 01:16
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

await reassembleApplication();
}
return <String, dynamic>{
'value': (debugBrightnessOverride ?? window.platformBrightness)
Copy link
Member

Choose a reason for hiding this comment

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

nitty nit: just leave the .toString() on this line? Looks so lonely on the next.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gave it some friends

///
/// See also:
///
/// * [WidgetsApp], which uses the [debugBrightnessOverride] setting to construct a [MediaQueryData].
Copy link
Member

Choose a reason for hiding this comment

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

maybe add that it only uses it in debug mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@jonahwilliams jonahwilliams merged commit e1f4cfb into flutter:master Jun 18, 2020
@jonahwilliams jonahwilliams deleted the add_toggle_brightness branch June 18, 2020 17:32
zljj0818 pushed a commit to zljj0818/flutter that referenced this pull request Jun 22, 2020
…rm brightness (flutter#59571)

A frequent request from the last Flutter developer survey was for an easier method of testing light/dark mode changes. Currently, a user needs to manually change the theme settings or adjust phone settings to see the difference. Instead we should add a toggle from the CLI, and eventually devtools/Intellij/Vscode that allows developers to override the current setting.

Fixes flutter#59495

Adds flutter.ext.brightnessOverride service protocol which either queries the current platform brightness, or overrides it to a new value. This accepts either Brightness.light or Brightness.dark as a value.

Adds a CLI toggle b which allows the setting to be toggled manually.

Requires an update to the MediaQuery, to conditionally use a debug override when not in release mode
mingwandroid pushed a commit to mingwandroid/flutter that referenced this pull request Sep 6, 2020
…rm brightness (flutter#59571)

A frequent request from the last Flutter developer survey was for an easier method of testing light/dark mode changes. Currently, a user needs to manually change the theme settings or adjust phone settings to see the difference. Instead we should add a toggle from the CLI, and eventually devtools/Intellij/Vscode that allows developers to override the current setting.

Fixes flutter#59495

Adds flutter.ext.brightnessOverride service protocol which either queries the current platform brightness, or overrides it to a new value. This accepts either Brightness.light or Brightness.dark as a value.

Adds a CLI toggle b which allows the setting to be toggled manually.

Requires an update to the MediaQuery, to conditionally use a debug override when not in release mode
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

framework flutter/packages/flutter repository. See also f: labels. tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support a light/dark mode toggle (from the command line and IDEs)

5 participants