Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 22 additions & 2 deletions lib/src/widgets/window_caption_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ class WindowCaptionButton extends StatefulWidget {
super.key,
this.brightness,
this.icon,
this.hovered = false,
this.pressed = false,
this.iconName,
required this.onPressed,
});
Expand All @@ -285,27 +287,35 @@ class WindowCaptionButton extends StatefulWidget {
super.key,
this.brightness,
this.icon,
this.hovered = false,
this.pressed = false,
this.onPressed,
}) : iconName = _kIconChromeMinimize;

WindowCaptionButton.maximize({
super.key,
this.brightness,
this.icon,
this.hovered = false,
this.pressed = false,
this.onPressed,
}) : iconName = _kIconChromeMaximize;

WindowCaptionButton.unmaximize({
super.key,
this.brightness,
this.icon,
this.hovered = false,
this.pressed = false,
this.onPressed,
}) : iconName = _kIconChromeUnmaximize;

WindowCaptionButton.close({
super.key,
this.brightness,
this.icon,
this.hovered = false,
this.pressed = false,
this.onPressed,
}) : iconName = _kIconChromeClose,
_lightButtonBgColorScheme = _ButtonBgColorScheme(
Expand Down Expand Up @@ -336,6 +346,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),
Expand Down Expand Up @@ -389,11 +409,11 @@ class _WindowCaptionButtonState extends State<WindowCaptionButton> {
Color bgColor = widget.buttonBgColorScheme.normal;
Color iconColor = widget.buttonIconColorScheme.normal;

if (_isHovering) {
if (_isHovering || widget.hovered) {
bgColor = widget.buttonBgColorScheme.hovered;
iconColor = widget.buttonIconColorScheme.hovered;
}
if (_isPressed) {
if (_isPressed || widget.pressed) {
bgColor = widget.buttonBgColorScheme.pressed;
iconColor = widget.buttonIconColorScheme.pressed;
}
Expand Down
61 changes: 61 additions & 0 deletions test/window_caption_button_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:window_manager/window_manager.dart';

Color? _background(WidgetTester tester) {
final container = tester.widget<Container>(
find.descendant(
of: find.byType(WindowCaptionButton),
matching: find.byType(Container),
),
);
return (container.decoration as BoxDecoration?)?.color;
}

Future<void> _pump(WidgetTester tester, WindowCaptionButton button) {
return tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Center(child: button),
),
);
}

void main() {
testWidgets('idle button has the normal background', (tester) async {
await _pump(tester, WindowCaptionButton.maximize(onPressed: () {}));
expect(_background(tester), Colors.transparent);
});

testWidgets('hovered shows the hover look without a pointer', (tester) async {
await _pump(
tester,
WindowCaptionButton.maximize(onPressed: () {}, hovered: true),
);
expect(_background(tester), Colors.black.withValues(alpha: 0.0373));
});

testWidgets('pressed wins over hovered', (tester) async {
await _pump(
tester,
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 _pump(
tester,
WindowCaptionButton.close(
brightness: Brightness.dark,
onPressed: () {},
hovered: true,
),
);
expect(_background(tester), const Color(0xffC42B1C));
});
}