Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add banner message warnings and errors #1764

Merged
merged 16 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/devtools_app/lib/src/connected_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ConnectedApp {

bool get isFlutterWebAppNow => isFlutterAppNow && isDartWebAppNow;

bool get isDebugFlutterAppNow => isFlutterAppNow && !isProfileBuildNow;

bool get isRunningOnDartVM => serviceManager.vm.name != 'ChromeDebugProxy';

Future<bool> get isDartCliApp async =>
Expand Down
358 changes: 358 additions & 0 deletions packages/devtools_app/lib/src/flutter/banner_messages.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,358 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import '../globals.dart';
import 'auto_dispose_mixin.dart';
import 'common_widgets.dart';
import 'controllers.dart';
import 'screen.dart';
import 'theme.dart';
import 'utils.dart';

const _runInProfileModeDocsUrl =
'https://flutter.dev/docs/testing/ui-performance#run-in-profile-mode';

const _profileGranularityDocsUrl =
'https://flutter.dev/docs/development/tools/devtools/performance#profile-granularity';

class BannerMessagesController {
final _messages = <DevToolsScreenType, List<BannerMessage>>{};
Copy link
Contributor

Choose a reason for hiding this comment

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

make the values in this map
ValueNotifier<List>
and be sure to call
notifyListeners on the ValueNotifier every time you change the content of the list.
Then users of this controller can be notified when the controller state changes instead of having to hope they call setState at the right time.

Copy link
Member Author

@kenzieschmoll kenzieschmoll Mar 27, 2020

Choose a reason for hiding this comment

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

the reason I didn't do this originally is because notifyListeners is protected
Screen Shot 2020-03-27 at 7 28 22 AM

used notifyListeners anyway and suppressed the warnings

Copy link
Contributor

Choose a reason for hiding this comment

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

sounds good. We could add a TODO to create our own sublcass that has a method to add an element to an existing List and call notifyListeners.

final _dismissedMessageKeys = <Key>{};

void addMessage(BannerMessage message) {
assert(!isMessageVisible(message));
final messages = messagesForScreen(message.screenType);
messages.add(message);
}

void removeMessage(BannerMessage message, {bool dismiss = false}) {
final messages = messagesForScreen(message.screenType);
messages.remove(message);
if (dismiss) {
assert(!_dismissedMessageKeys.contains(message.key));
_dismissedMessageKeys.add(message.key);
}
}

bool isMessageDismissed(BannerMessage message) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you can probably make this message @Protected or private now.

Copy link
Member Author

Choose a reason for hiding this comment

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

made visible for testing - here and below

return _dismissedMessageKeys.contains(message.key);
}

bool isMessageVisible(BannerMessage message) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: you can probably make this message @Protected or private now.

return messagesForScreen(message.screenType)
.where((m) => m.key == message.key)
.isNotEmpty;
}

List<BannerMessage> messagesForScreen(DevToolsScreenType screenType) {
return _messages.putIfAbsent(screenType, () => []);
}
}

class BannerMessages extends StatelessWidget {
const BannerMessages({Key key, @required this.screen}) : super(key: key);

final Screen screen;

@override
Widget build(BuildContext context) {
return _BannerMessagesProvider(screen: screen);
}

static BannerMessagesState of(BuildContext context) {
final provider =
context.dependOnInheritedWidgetOfExactType<_InheritedBannerMessages>();
return provider?.data;
}
}

class _BannerMessagesProvider extends StatefulWidget {
const _BannerMessagesProvider({Key key, this.screen}) : super(key: key);

final Screen screen;

@override
BannerMessagesState createState() => BannerMessagesState();
}

class _InheritedBannerMessages extends InheritedWidget {
const _InheritedBannerMessages({this.data, Widget child})
: super(child: child);

final BannerMessagesState data;

@override
bool updateShouldNotify(_InheritedBannerMessages oldWidget) {
return oldWidget.data != data;
}
}

class BannerMessagesState extends State<_BannerMessagesProvider>
with AutoDisposeMixin {
BannerMessagesController controller;

@override
void didChangeDependencies() {
super.didChangeDependencies();
final newController = Controllers.of(context)?.bannerMessages;
if (newController == controller) return;
controller = newController;
}

void push(BannerMessage message) {
if (controller.isMessageDismissed(message) ||
controller.isMessageVisible(message)) return;
// We push the banner message in a post frame callback because otherwise,we'd be
// trying to call setState while the parent widget `BannerMessages` is in the middle
// of `build`.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
setState(() {
controller.addMessage(message);
});
});
}

void remove(BannerMessage message, {bool dismiss = false}) {
// We push the banner message in a post frame callback because otherwise,we'd be
// trying to call setState while the parent widget `BannerMessages` is in the middle
// of `build`.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
setState(() {
controller.removeMessage(message, dismiss: dismiss);
});
});
}

@override
Widget build(BuildContext context) {
return _InheritedBannerMessages(
data: this,
child: Column(
children: [
...controller?.messagesForScreen(widget.screen.type) ?? [],
Copy link
Contributor

Choose a reason for hiding this comment

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

refactoring this to

Column(
  children: [
    Column(
      children: ValueListenableBuilder(controller?.messagesForScreen(widget.screen.type),
    ),
    Expanded(...),
)

will fix your issue with having to do a post frame callback and will make things work correctly if someone else uses the banner controller.

Copy link
Member Author

Choose a reason for hiding this comment

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

doing this still results in the setState() or markNeedsBuild() called during build. error.

Copy link
Contributor

Choose a reason for hiding this comment

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

darn. I guess the right mental model is you can't change any ValueListenable objects during build either.

Expanded(
child: widget.screen.build(context),
)
],
),
);
}
}

class BannerMessage extends StatelessWidget {
const BannerMessage({
@required Key key,
@required this.textSpans,
@required this.backgroundColor,
@required this.foregroundColor,
@required this.screenType,
}) : super(key: key);

final List<TextSpan> textSpans;
final Color backgroundColor;
final Color foregroundColor;
final DevToolsScreenType screenType;

@override
Widget build(BuildContext context) {
return Card(
color: backgroundColor,
child: Padding(
padding: const EdgeInsets.all(defaultSpacing),
child: Row(
children: <Widget>[
Expanded(
child: RichText(
text: TextSpan(
children: textSpans,
),
),
),
const SizedBox(width: denseSpacing),
CircularIconButton(
Copy link
Contributor

Choose a reason for hiding this comment

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

this icon looks quite large. is that intentional?

Copy link
Member Author

Choose a reason for hiding this comment

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

@raison00 do you have a recommendation on how large this "dismiss" button should be? Right now it is set at 36px
Screen Shot 2020-03-25 at 2 58 34 PM

Copy link

Choose a reason for hiding this comment

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

Google's minimum recommended touch target size is around 48px. WCAG suggests 44px or larger.

If the dismiss button had width/height of 24px, a smaller image imprint, the dismiss button should have additional padding to ensure the target size is within the 48px area.

Going below the 24px size is not recommended for the dismiss button.

This is a 24px with a 48px touch target for reference:
24-close

icon: Icons.close,
backgroundColor: backgroundColor,
foregroundColor: foregroundColor,
// TODO(kenz): animate the removal of this message.
onPressed: () =>
BannerMessages.of(context).remove(this, dismiss: true),
),
],
),
),
);
}
}

class _BannerError extends BannerMessage {
const _BannerError({
@required Key key,
@required List<TextSpan> textSpans,
@required DevToolsScreenType screenType,
}) : super(
key: key,
textSpans: textSpans,
backgroundColor: devtoolsError,
foregroundColor: foreground,
screenType: screenType,
);

static const foreground = Colors.white;
static const linkColor = Color(0xFF54C1EF);
}

// TODO(kenz): add "Do not show this again" option to warnings.
class _BannerWarning extends BannerMessage {
const _BannerWarning({
@required Key key,
@required List<TextSpan> textSpans,
@required DevToolsScreenType screenType,
}) : super(
key: key,
textSpans: textSpans,
backgroundColor: devtoolsWarning,
foregroundColor: foreground,
screenType: screenType,
);

static const foreground = Colors.black87;
static const linkColor = Color(0xFF54C1EF);
}

class DebugModePerformanceMessage {
const DebugModePerformanceMessage(this.screenType);

final DevToolsScreenType screenType;

Widget build(BuildContext context) {
return _BannerError(
key: Key('DebugModePerformanceMessage - $screenType'),
textSpans: [
const TextSpan(
text:
'You are running your app in debug mode. Debug mode performance '
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this is probably cleaner as a ''' literal. You wouldn't need to escape anything.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

'is not indicative of release performance.\n\nRelaunch your '
'application with the \'--profile\' argument, or ',
style: TextStyle(color: _BannerError.foreground),
),
TextSpan(
text: 'relaunch in profile mode from VS Code or IntelliJ',
style: TextStyle(
decoration: TextDecoration.underline,
color: _BannerError.linkColor,
),
recognizer: TapGestureRecognizer()
..onTap = () async {
await launchUrl(_runInProfileModeDocsUrl, context);
},
),
const TextSpan(
text: '.',
style: TextStyle(color: _BannerError.foreground),
),
],
screenType: screenType,
);
}
}

class HighProfileGranularityMessage {
const HighProfileGranularityMessage(this.screenType);

static const keyPrefix = 'HighProfileGranularityMessage';

final DevToolsScreenType screenType;

Widget build(BuildContext context) {
return _BannerWarning(
key: Key('$keyPrefix - $screenType'),
textSpans: [
const TextSpan(
text:
'You are opting in to a high CPU sampling rate. This may affect '
'the performance of your application. Please read our ',
style: TextStyle(color: _BannerWarning.foreground),
),
TextSpan(
text: 'documentation',
style: TextStyle(
decoration: TextDecoration.underline,
color: _BannerWarning.linkColor,
),
recognizer: TapGestureRecognizer()
..onTap = () async {
await launchUrl(_profileGranularityDocsUrl, context);
},
),
const TextSpan(
text: ' to understand the trade-offs associated with this setting.',
style: TextStyle(color: _BannerWarning.foreground),
),
],
screenType: screenType,
);
}
}

class DebugModeMemoryMessage {
const DebugModeMemoryMessage(this.screenType);

final DevToolsScreenType screenType;

BannerMessage build(BuildContext context) {
return _BannerWarning(
key: Key('DebugModeMemoryMessage - $screenType'),
textSpans: [
const TextSpan(
text: 'You are running your app in debug mode. Absolute memory usage '
'may be higher in a debug build than in a release build.\n\n'
'For the most accurate absolute memory stats, relaunch your '
'application with the \'--profile\' argument, or ',
style: TextStyle(color: _BannerWarning.foreground),
),
TextSpan(
text: 'relaunch in profile mode from VS Code or IntelliJ',
style: TextStyle(
decoration: TextDecoration.underline,
color: _BannerWarning.linkColor,
),
recognizer: TapGestureRecognizer()
..onTap = () async {
await launchUrl(_runInProfileModeDocsUrl, context);
},
),
const TextSpan(
text: '.',
style: TextStyle(color: _BannerWarning.foreground),
),
],
screenType: screenType,
);
}
}

void maybePushDebugModePerformanceMessage(
BuildContext context,
DevToolsScreenType screenType,
) {
if (serviceManager.connectedApp.isDebugFlutterAppNow) {
BannerMessages.of(context)
.push(DebugModePerformanceMessage(screenType).build(context));
}
}

void maybePushDebugModeMemoryMessage(
BuildContext context,
DevToolsScreenType screenType,
) {
if (serviceManager.connectedApp.isDebugFlutterAppNow) {
BannerMessages.of(context)
.push(DebugModeMemoryMessage(screenType).build(context));
}
}
Loading