-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
window_caption.dart
139 lines (129 loc) · 3.75 KB
/
window_caption.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import 'package:flutter/material.dart';
import 'package:window_manager/src/widgets/drag_to_move_area.dart';
import 'package:window_manager/src/widgets/window_caption_button.dart';
import 'package:window_manager/src/window_listener.dart';
import 'package:window_manager/src/window_manager.dart';
const double kWindowCaptionHeight = 32;
/// A widget to simulate the title bar of windows 11.
///
/// {@tool snippet}
///
/// ```dart
/// Scaffold(
/// appBar: PreferredSize(
/// child: WindowCaption(
/// brightness: Theme.of(context).brightness,
/// title: Text('window_manager_example'),
/// ),
/// preferredSize: const Size.fromHeight(kWindowCaptionHeight),
/// ),
/// )
/// ```
/// {@end-tool}
class WindowCaption extends StatefulWidget {
const WindowCaption({
super.key,
this.title,
this.backgroundColor,
this.brightness,
});
final Widget? title;
final Color? backgroundColor;
final Brightness? brightness;
@override
State<WindowCaption> createState() => _WindowCaptionState();
}
class _WindowCaptionState extends State<WindowCaption> with WindowListener {
@override
void initState() {
windowManager.addListener(this);
super.initState();
}
@override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
return DecoratedBox(
decoration: BoxDecoration(
color: widget.backgroundColor ??
(widget.brightness == Brightness.dark
? const Color(0xff1C1C1C)
: Colors.transparent),
),
child: Row(
children: [
Expanded(
child: DragToMoveArea(
child: SizedBox(
height: double.infinity,
child: Row(
children: [
Container(
padding: const EdgeInsets.only(left: 16),
child: DefaultTextStyle(
style: TextStyle(
color: widget.brightness == Brightness.light
? Colors.black.withOpacity(0.8956)
: Colors.white,
fontSize: 14,
),
child: widget.title ?? Container(),
),
),
],
),
),
),
),
WindowCaptionButton.minimize(
brightness: widget.brightness,
onPressed: () async {
bool isMinimized = await windowManager.isMinimized();
if (isMinimized) {
windowManager.restore();
} else {
windowManager.minimize();
}
},
),
FutureBuilder<bool>(
future: windowManager.isMaximized(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (snapshot.data == true) {
return WindowCaptionButton.unmaximize(
brightness: widget.brightness,
onPressed: () {
windowManager.unmaximize();
},
);
}
return WindowCaptionButton.maximize(
brightness: widget.brightness,
onPressed: () {
windowManager.maximize();
},
);
},
),
WindowCaptionButton.close(
brightness: widget.brightness,
onPressed: () {
windowManager.close();
},
),
],
),
);
}
@override
void onWindowMaximize() {
setState(() {});
}
@override
void onWindowUnmaximize() {
setState(() {});
}
}