From 90b70205168fc806f46a86b98b5e470033a62f8d Mon Sep 17 00:00:00 2001 From: Minas Giannekas Date: Mon, 21 Mar 2022 03:20:32 +0200 Subject: [PATCH] Add merge methods to `MacosThemeData` and widget-specific `ThemeData` classes (closes #183) (#186) * fix: add missing merge methods for `MacosThemeData` and widget-level `ThemeData` classes * chore: update changelog and pubspec --- CHANGELOG.md | 3 ++ example/pubspec.lock | 2 +- lib/src/buttons/help_button.dart | 8 +++++ lib/src/buttons/icon_button.dart | 12 +++++++ lib/src/buttons/popup_button.dart | 9 +++++ lib/src/buttons/push_button.dart | 9 +++++ lib/src/icon/macos_icon.dart | 4 +-- lib/src/indicators/scrollbar.dart | 24 +++++++++++++ lib/src/labels/tooltip.dart | 19 ++++++++-- lib/src/theme/macos_theme.dart | 58 ++++++++++++++++++++++++++----- pubspec.yaml | 2 +- 11 files changed, 135 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb580035..543ccf74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [0.12.2+1] +* Adds missing `merge` methods to `MacosThemeData` and widget `ThemeData` classes, making it possible to use them properly with any number of user-provided custom properties. + ## [0.12.2] * Fixes `MacosThemeData` to properly apply user-defined `pushButtonTheme`, `helpButtonTheme`, and `tooltipTheme` properties. diff --git a/example/pubspec.lock b/example/pubspec.lock index ec0422a5..4b54b5f9 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -73,7 +73,7 @@ packages: path: ".." relative: true source: path - version: "0.12.2" + version: "0.12.2+1" matcher: dependency: transitive description: diff --git a/lib/src/buttons/help_button.dart b/lib/src/buttons/help_button.dart index 00f823f5..ec62f819 100644 --- a/lib/src/buttons/help_button.dart +++ b/lib/src/buttons/help_button.dart @@ -344,4 +344,12 @@ class HelpButtonThemeData with Diagnosticable { properties.add(ColorProperty('color', color)); properties.add(ColorProperty('disabledColor', disabledColor)); } + + HelpButtonThemeData merge(HelpButtonThemeData? other) { + if (other == null) return this; + return copyWith( + color: other.color, + disabledColor: other.disabledColor, + ); + } } diff --git a/lib/src/buttons/icon_button.dart b/lib/src/buttons/icon_button.dart index 1306dda2..a167aaea 100644 --- a/lib/src/buttons/icon_button.dart +++ b/lib/src/buttons/icon_button.dart @@ -426,4 +426,16 @@ class MacosIconButtonThemeData with Diagnosticable { DiagnosticsProperty('boxConstraints', boxConstraints), ); } + + MacosIconButtonThemeData merge(MacosIconButtonThemeData? other) { + if (other == null) return this; + return copyWith( + backgroundColor: other.backgroundColor, + disabledColor: other.disabledColor, + hoverColor: other.hoverColor, + shape: other.shape, + borderRadius: other.borderRadius, + boxConstraints: other.boxConstraints, + ); + } } diff --git a/lib/src/buttons/popup_button.dart b/lib/src/buttons/popup_button.dart index 8c244932..adece94b 100644 --- a/lib/src/buttons/popup_button.dart +++ b/lib/src/buttons/popup_button.dart @@ -1611,4 +1611,13 @@ class MacosPopupButtonThemeData with Diagnosticable { properties.add(ColorProperty('backgroundColor', backgroundColor)); properties.add(ColorProperty('popupColor', popupColor)); } + + MacosPopupButtonThemeData merge(MacosPopupButtonThemeData? other) { + if (other == null) return this; + return copyWith( + highlightColor: other.highlightColor, + backgroundColor: other.backgroundColor, + popupColor: other.popupColor, + ); + } } diff --git a/lib/src/buttons/push_button.dart b/lib/src/buttons/push_button.dart index 60e51ee3..1da66ea1 100644 --- a/lib/src/buttons/push_button.dart +++ b/lib/src/buttons/push_button.dart @@ -413,4 +413,13 @@ class PushButtonThemeData with Diagnosticable { properties.add(ColorProperty('disabledColor', disabledColor)); properties.add(ColorProperty('secondaryColor', secondaryColor)); } + + PushButtonThemeData merge(PushButtonThemeData? other) { + if (other == null) return this; + return copyWith( + color: other.color, + disabledColor: other.disabledColor, + secondaryColor: other.secondaryColor, + ); + } } diff --git a/lib/src/icon/macos_icon.dart b/lib/src/icon/macos_icon.dart index 247be5ee..0592fb0b 100644 --- a/lib/src/icon/macos_icon.dart +++ b/lib/src/icon/macos_icon.dart @@ -177,7 +177,7 @@ class MacosIconTheme extends InheritedTheme { /// The [data] and [child] arguments must not be null. static Widget merge({ Key? key, - required IconThemeData data, + required MacosIconThemeData data, required Widget child, }) { return Builder( @@ -293,7 +293,7 @@ class MacosIconThemeData with Diagnosticable { /// Returns a new icon theme that matches this icon theme but with some values /// replaced by the non-null parameters of the given icon theme. If the given /// icon theme is null, simply returns this icon theme. - MacosIconThemeData merge(IconThemeData? other) { + MacosIconThemeData merge(MacosIconThemeData? other) { if (other == null) return this; return copyWith( color: other.color, diff --git a/lib/src/indicators/scrollbar.dart b/lib/src/indicators/scrollbar.dart index b9721ac3..7e9e6e90 100644 --- a/lib/src/indicators/scrollbar.dart +++ b/lib/src/indicators/scrollbar.dart @@ -286,6 +286,8 @@ class ScrollbarThemeData with Diagnosticable { Color? hoveringThumbColor, Color? draggingThumbColor, Color? trackColor, + Color? hoveringTrackColor, + Color? trackBorderColor, Color? hoveringTrackBorderColor, double? crossAxisMargin, double? mainAxisMargin, @@ -466,6 +468,28 @@ class ScrollbarThemeData with Diagnosticable { defaultValue: null, )); } + + ScrollbarThemeData merge(ScrollbarThemeData? other) { + if (other == null) return this; + return copyWith( + thickness: other.thickness, + hoveringThickness: other.hoveringThickness, + showTrackOnHover: other.showTrackOnHover, + isAlwaysShown: other.isAlwaysShown, + interactive: other.interactive, + radius: other.radius, + thumbColor: other.thumbColor, + hoveringThumbColor: other.hoveringThumbColor, + draggingThumbColor: other.draggingThumbColor, + trackColor: other.trackColor, + hoveringTrackColor: other.hoveringTrackColor, + trackBorderColor: other.trackBorderColor, + hoveringTrackBorderColor: other.hoveringTrackBorderColor, + crossAxisMargin: other.crossAxisMargin, + mainAxisMargin: other.mainAxisMargin, + minThumbLength: other.minThumbLength, + ); + } } /// Applies a scrollbar theme to descendant [MacosScrollbar] widgets. diff --git a/lib/src/labels/tooltip.dart b/lib/src/labels/tooltip.dart index ef5c8ad6..d17ba9a2 100644 --- a/lib/src/labels/tooltip.dart +++ b/lib/src/labels/tooltip.dart @@ -423,13 +423,13 @@ class TooltipThemeData with Diagnosticable { /// Copy this tooltip with [style] TooltipThemeData copyWith({ - BoxDecoration? decoration, + Decoration? decoration, double? height, EdgeInsetsGeometry? margin, EdgeInsetsGeometry? padding, bool? preferBelow, Duration? showDuration, - TextStyle? testStyle, + TextStyle? textStyle, double? verticalOffset, Duration? waitDuration, }) { @@ -576,6 +576,21 @@ class TooltipThemeData with Diagnosticable { properties.add(DiagnosticsProperty('showDuration', showDuration)); properties.add(DiagnosticsProperty('textStyle', textStyle)); } + + TooltipThemeData merge(TooltipThemeData? other) { + if (other == null) return this; + return copyWith( + decoration: other.decoration, + height: other.height, + margin: other.margin, + padding: other.padding, + preferBelow: other.preferBelow, + showDuration: other.showDuration, + textStyle: other.textStyle, + verticalOffset: other.verticalOffset, + waitDuration: other.waitDuration, + ); + } } /// A delegate for computing the layout of a tooltip to be displayed above or diff --git a/lib/src/theme/macos_theme.dart b/lib/src/theme/macos_theme.dart index 1edc051e..b52552bc 100644 --- a/lib/src/theme/macos_theme.dart +++ b/lib/src/theme/macos_theme.dart @@ -271,7 +271,7 @@ class MacosThemeData with Diagnosticable { : const Color.fromRGBO(242, 242, 247, 1), ); - return MacosThemeData.raw( + final defaultData = MacosThemeData.raw( brightness: _brightness, primaryColor: primaryColor, canvasColor: canvasColor, @@ -286,6 +286,24 @@ class MacosThemeData with Diagnosticable { iconTheme: iconTheme, macosPopupButtonTheme: macosPopupButtonTheme, ); + + final customizedData = defaultData.copyWith( + brightness: _brightness, + primaryColor: primaryColor, + canvasColor: canvasColor, + typography: typography, + pushButtonTheme: pushButtonTheme, + dividerColor: dividerColor, + helpButtonTheme: helpButtonTheme, + tooltipTheme: tooltipTheme, + visualDensity: visualDensity, + scrollbarTheme: scrollbarTheme, + macosIconButtonTheme: macosIconButtonThemeData, + iconTheme: iconTheme, + macosPopupButtonTheme: macosPopupButtonTheme, + ); + + return defaultData.merge(customizedData); } /// Create a [MacosThemeData] given a set of exact values. All the values must @@ -423,16 +441,38 @@ class MacosThemeData with Diagnosticable { primaryColor: primaryColor ?? this.primaryColor, canvasColor: canvasColor ?? this.canvasColor, dividerColor: dividerColor ?? this.dividerColor, - typography: typography ?? this.typography, - pushButtonTheme: pushButtonTheme ?? this.pushButtonTheme, - helpButtonTheme: helpButtonTheme ?? this.helpButtonTheme, - tooltipTheme: tooltipTheme ?? this.tooltipTheme, + typography: this.typography.merge(typography), + pushButtonTheme: this.pushButtonTheme.merge(pushButtonTheme), + helpButtonTheme: this.helpButtonTheme.merge(helpButtonTheme), + tooltipTheme: this.tooltipTheme.merge(tooltipTheme), visualDensity: visualDensity ?? this.visualDensity, - scrollbarTheme: scrollbarTheme ?? this.scrollbarTheme, - macosIconButtonTheme: macosIconButtonTheme ?? this.macosIconButtonTheme, - iconTheme: iconTheme ?? this.iconTheme, + scrollbarTheme: this.scrollbarTheme.merge(scrollbarTheme), + macosIconButtonTheme: + this.macosIconButtonTheme.merge(macosIconButtonTheme), + iconTheme: this.iconTheme.merge(iconTheme), + macosPopupButtonTheme: + this.macosPopupButtonTheme.merge(macosPopupButtonTheme), + ); + } + + MacosThemeData merge(MacosThemeData? other) { + if (other == null) return this; + return copyWith( + brightness: other.brightness, + primaryColor: other.primaryColor, + canvasColor: other.canvasColor, + dividerColor: other.dividerColor, + typography: typography.merge(other.typography), + pushButtonTheme: pushButtonTheme.merge(other.pushButtonTheme), + helpButtonTheme: helpButtonTheme.merge(other.helpButtonTheme), + tooltipTheme: tooltipTheme.merge(other.tooltipTheme), + visualDensity: other.visualDensity, + scrollbarTheme: scrollbarTheme.merge(other.scrollbarTheme), + macosIconButtonTheme: + macosIconButtonTheme.merge(other.macosIconButtonTheme), + iconTheme: iconTheme.merge(other.iconTheme), macosPopupButtonTheme: - macosPopupButtonTheme ?? this.macosPopupButtonTheme, + macosPopupButtonTheme.merge(other.macosPopupButtonTheme), ); } diff --git a/pubspec.yaml b/pubspec.yaml index e1f50af6..54b4614e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: macos_ui description: Flutter widgets and themes implementing the current macOS design language. -version: 0.12.2 +version: 0.12.2+1 homepage: "https://github.com/GroovinChip/macos_ui" environment: