diff --git a/lib/src/widgets/window_caption_button.dart b/lib/src/widgets/window_caption_button.dart index d2d6644..85d652e 100644 --- a/lib/src/widgets/window_caption_button.dart +++ b/lib/src/widgets/window_caption_button.dart @@ -289,6 +289,8 @@ class WindowCaptionButton extends StatefulWidget { super.key, this.brightness, this.icon, + this.hovered = false, + this.pressed = false, this.iconName, required this.onPressed, }); @@ -297,6 +299,8 @@ class WindowCaptionButton extends StatefulWidget { super.key, this.brightness, this.icon, + this.hovered = false, + this.pressed = false, this.onPressed, }) : iconName = _kIconChromeMinimize; @@ -304,6 +308,8 @@ class WindowCaptionButton extends StatefulWidget { super.key, this.brightness, this.icon, + this.hovered = false, + this.pressed = false, this.onPressed, }) : iconName = _kIconChromeMaximize; @@ -311,6 +317,8 @@ class WindowCaptionButton extends StatefulWidget { super.key, this.brightness, this.icon, + this.hovered = false, + this.pressed = false, this.onPressed, }) : iconName = _kIconChromeUnmaximize; @@ -318,6 +326,8 @@ class WindowCaptionButton extends StatefulWidget { super.key, this.brightness, this.icon, + this.hovered = false, + this.pressed = false, this.onPressed, }) : iconName = _kIconChromeClose, _lightButtonBgColorScheme = _ButtonBgColorScheme( @@ -348,6 +358,16 @@ class WindowCaptionButton extends StatefulWidget { final String? iconName; final VoidCallback? onPressed; + /// Shows the hovered look even when Flutter does not see the pointer. + /// + /// For input the platform handles over the button, such as a Windows 11 + /// maximize button that reports `HTMAXBUTTON` to open Snap Layouts. + final bool hovered; + + /// Shows the pressed look even when Flutter does not see the pointer. + /// See [hovered]. + final bool pressed; + _ButtonBgColorScheme _lightButtonBgColorScheme = _ButtonBgColorScheme( normal: Colors.transparent, hovered: Colors.black.withValues(alpha: 0.0373), @@ -447,10 +467,10 @@ class _WindowCaptionButtonState extends State { Color iconColor = iconScheme.normal; if (!enabled) { iconColor = iconScheme.disabled; - } else if (_isPressed) { + } else if (_isPressed || widget.pressed) { bgColor = bgScheme.pressed; iconColor = iconScheme.pressed; - } else if (_isHovering) { + } else if (_isHovering || widget.hovered) { bgColor = bgScheme.hovered; iconColor = iconScheme.hovered; } diff --git a/test/window_caption_button_test.dart b/test/window_caption_button_test.dart index 16be60b..b877c40 100644 --- a/test/window_caption_button_test.dart +++ b/test/window_caption_button_test.dart @@ -1,6 +1,6 @@ import 'package:flutter/gestures.dart'; -import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; import 'package:window_manager/window_manager.dart'; // Caption buttons are laid out in a Row; mirror that sizing here. @@ -15,7 +15,7 @@ Color? _background(WidgetTester tester) { matching: find.byType(Container), ), ); - return (container.decoration! as BoxDecoration).color; + return (container.decoration as BoxDecoration?)?.color; } CustomPainter _painter(WidgetTester tester) => tester @@ -215,4 +215,44 @@ void main() { ); handle.dispose(); }); + + testWidgets('idle button has the normal background', (tester) async { + await tester.pumpWidget( + _host(WindowCaptionButton.maximize(onPressed: () {})), + ); + expect(_background(tester), Colors.transparent); + }); + + testWidgets('hovered shows the hover look without a pointer', (tester) async { + await tester.pumpWidget( + _host(WindowCaptionButton.maximize(onPressed: () {}, hovered: true)), + ); + expect(_background(tester), Colors.black.withValues(alpha: 0.0373)); + }); + + testWidgets('pressed wins over hovered', (tester) async { + await tester.pumpWidget( + _host( + WindowCaptionButton.unmaximize( + onPressed: () {}, + hovered: true, + pressed: true, + ), + ), + ); + expect(_background(tester), Colors.black.withValues(alpha: 0.0241)); + }); + + testWidgets('dark close button uses its own hover color', (tester) async { + await tester.pumpWidget( + _host( + WindowCaptionButton.close( + brightness: Brightness.dark, + onPressed: () {}, + hovered: true, + ), + ), + ); + expect(_background(tester), const Color(0xffC42B1C)); + }); }