Skip to content

Commit

Permalink
Add merge methods to MacosThemeData and widget-specific ThemeData
Browse files Browse the repository at this point in the history
… classes (closes #183) (#186)

* fix: add missing merge methods for `MacosThemeData` and widget-level `ThemeData` classes

* chore: update changelog and pubspec
  • Loading branch information
whiplashoo committed Mar 21, 2022
1 parent 65b53fa commit 90b7020
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.12.2"
version: "0.12.2+1"
matcher:
dependency: transitive
description:
Expand Down
8 changes: 8 additions & 0 deletions lib/src/buttons/help_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
12 changes: 12 additions & 0 deletions lib/src/buttons/icon_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,16 @@ class MacosIconButtonThemeData with Diagnosticable {
DiagnosticsProperty<BoxConstraints?>('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,
);
}
}
9 changes: 9 additions & 0 deletions lib/src/buttons/popup_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
9 changes: 9 additions & 0 deletions lib/src/buttons/push_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
4 changes: 2 additions & 2 deletions lib/src/icon/macos_icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions lib/src/indicators/scrollbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ class ScrollbarThemeData with Diagnosticable {
Color? hoveringThumbColor,
Color? draggingThumbColor,
Color? trackColor,
Color? hoveringTrackColor,
Color? trackBorderColor,
Color? hoveringTrackBorderColor,
double? crossAxisMargin,
double? mainAxisMargin,
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 17 additions & 2 deletions lib/src/labels/tooltip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}) {
Expand Down Expand Up @@ -576,6 +576,21 @@ class TooltipThemeData with Diagnosticable {
properties.add(DiagnosticsProperty<Duration>('showDuration', showDuration));
properties.add(DiagnosticsProperty<TextStyle>('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
Expand Down
58 changes: 49 additions & 9 deletions lib/src/theme/macos_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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),
);
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 90b7020

Please sign in to comment.