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 4 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
304 changes: 304 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,304 @@
// 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 'dart:async';

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

import '../auto_dispose.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 implements DisposableController {
final _dismissedMessageIds = <String>{};

final _refreshMessagesController = StreamController<Null>.broadcast();

Stream<Null> get onRefreshMessages => _refreshMessagesController.stream;

@override
void dispose() {
_refreshMessagesController.close();
}

void refreshMessages() {
_refreshMessagesController.add(null);
}

void dismissMessage(String messageId) {
assert(!isMessageDismissed(messageId));
_dismissedMessageIds.add(messageId);
refreshMessages();
}

bool isMessageDismissed(String messageId) {
return _dismissedMessageIds.contains(messageId);
}
}

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

final Screen screen;

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

class _BannerMessageContainerState extends State<BannerMessageContainer>
with AutoDisposeMixin {
BannerMessagesController controller;

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

autoDispose(controller.onRefreshMessages.listen((_) => setState(() {})));
}

@override
Widget build(BuildContext context) {
final messagesForScreen = widget.screen.messages(context);
if (messagesForScreen.isNotEmpty) {
final messagesToShow = <Widget>[];
for (Widget message in messagesForScreen) {
assert(message is UniqueMessage);
if (!controller.isMessageDismissed((message as UniqueMessage).id)) {
messagesToShow.add(message);
}
}
if (messagesToShow.isNotEmpty) {
return Column(
children: messagesToShow,
);
}
}
return const SizedBox();
}
}

class BannerMessage extends StatelessWidget implements UniqueMessage {
const BannerMessage({
@required this.messageId,
@required this.textSpans,
@required this.backgroundColor,
@required this.foregroundColor,
});

final String messageId;
final List<TextSpan> textSpans;
final Color backgroundColor;
final Color foregroundColor;

@override
String get id => messageId;

@override
Widget build(BuildContext context) {
final bannerMessagesController = Controllers.of(context).bannerMessages;
return Card(
color: backgroundColor,
child: Padding(
padding: const EdgeInsets.all(defaultSpacing),
child: Row(
children: <Widget>[
Expanded(
child: Container(
Copy link
Contributor

Choose a reason for hiding this comment

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

@pq a lint should be able to catch that this Container widget can be removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed! avoid_unnecessary_containers should do just the trick.

Copy link
Contributor

Choose a reason for hiding this comment

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

(PR enabling it on the way...)

Copy link
Contributor

Choose a reason for hiding this comment

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

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: () => bannerMessagesController.dismissMessage(id),
),
],
),
),
);
}
}

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

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

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

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

class DebugModePerformanceMessage extends StatelessWidget
implements UniqueMessage {
const DebugModePerformanceMessage(this.screenType);

final DevToolsScreenType screenType;

@override
String get id => 'DebugModePerformanceMessage - $screenType';

@override
Widget build(BuildContext context) {
return _BannerError(
id: id,
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 extends StatelessWidget
implements UniqueMessage {
const HighProfileGranularityMessage(this.screenType);

final DevToolsScreenType screenType;

@override
String get id => 'HighProfileGranularityMessage - $screenType';

@override
Widget build(BuildContext context) {
return _BannerWarning(
id: id,
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 extends StatelessWidget implements UniqueMessage {
const DebugModeMemoryMessage(this.screenType);

final DevToolsScreenType screenType;

@override
String get id => 'DebugModeMemoryMessage - $screenType';

@override
Widget build(BuildContext context) {
return _BannerWarning(
id: id,
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,
);
}
}

abstract class UniqueMessage {
String get id;
}
40 changes: 40 additions & 0 deletions packages/devtools_app/lib/src/flutter/common_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,43 @@ class CenteredMessage extends StatelessWidget {
);
}
}

class CircularIconButton extends StatelessWidget {
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't there an existing material design close button we can use instead? this button seems a bit large and not that consistent with material design.

Copy link
Member Author

Choose a reason for hiding this comment

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

open to suggestions. A button with text seemed too cluttered, a button with a rectangular outline seemed to clash with the rounded corners of the card., and a solo "X" icon seemed a little plain IMO. @raison00 thoughts?

const CircularIconButton({
@required this.icon,
@required this.onPressed,
@required this.backgroundColor,
@required this.foregroundColor,
});

final IconData icon;
final VoidCallback onPressed;
final Color backgroundColor;
final Color foregroundColor;

@override
Widget build(BuildContext context) {
return RawMaterialButton(
fillColor: backgroundColor,
hoverColor: Theme.of(context).hoverColor,
elevation: 0.0,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
constraints: const BoxConstraints.tightFor(
width: buttonMinWidth,
height: buttonMinWidth,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
side: BorderSide(
width: 2.0,
color: foregroundColor,
),
),
onPressed: onPressed,
child: Icon(
icon,
color: foregroundColor,
),
);
}
}
8 changes: 7 additions & 1 deletion packages/devtools_app/lib/src/flutter/controllers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '../logging/logging_controller.dart';
import '../memory/flutter/memory_controller.dart';
import '../performance/performance_controller.dart';
import '../timeline/flutter/timeline_controller.dart';
import 'banner_messages.dart';

/// Container for controllers that should outlive individual screens of the app.
///
Expand All @@ -25,10 +26,12 @@ class ProvidedControllers implements DisposableController {
@required this.timeline,
@required this.memory,
@required this.performance,
@required this.bannerMessages,
}) : assert(logging != null),
assert(timeline != null),
assert(memory != null),
assert(performance != null);
assert(performance != null),
assert(bannerMessages != null);

/// Builds the default providers for the app.
factory ProvidedControllers.defaults() {
Expand All @@ -44,20 +47,23 @@ class ProvidedControllers implements DisposableController {
timeline: TimelineController(),
memory: MemoryController(),
performance: PerformanceController(),
bannerMessages: BannerMessagesController(),
);
}

final LoggingController logging;
final TimelineController timeline;
final MemoryController memory;
final PerformanceController performance;
final BannerMessagesController bannerMessages;

@override
void dispose() {
logging.dispose();
timeline.dispose();
memory.dispose();
performance.dispose();
bannerMessages.dispose();
}
}

Expand Down
Loading