Skip to content

Commit ba564ea

Browse files
authored
Refactor regular windows (#20)
* refactor: the RegularWindowController now holds all initial values and is solely responsible for creating and destroying a window * Updated multi window reference app and window tests
1 parent 1bff803 commit ba564ea

File tree

7 files changed

+220
-277
lines changed

7 files changed

+220
-277
lines changed

examples/multi_window_ref_app/lib/app/main_window.dart

+15-11
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import 'window_manager_model.dart';
77

88
class MainWindow extends StatefulWidget {
99
MainWindow({super.key, required WindowController mainController}) {
10-
_windowManagerModel.add(
11-
KeyedWindowController(isMainWindow: true, controller: mainController));
10+
_windowManagerModel.add(KeyedWindowController(
11+
isMainWindow: true, key: UniqueKey(), controller: mainController));
1212
}
1313

1414
final WindowManagerModel _windowManagerModel = WindowManagerModel();
@@ -70,9 +70,9 @@ class _MainWindowState extends State<MainWindow> {
7070
windowSettings: widget._settings,
7171
windowManagerModel: widget._windowManagerModel,
7272
onDestroyed: () =>
73-
widget._windowManagerModel.remove(controller),
73+
widget._windowManagerModel.remove(controller.key),
7474
onError: () =>
75-
widget._windowManagerModel.remove(controller),
75+
widget._windowManagerModel.remove(controller.key),
7676
));
7777
}
7878
}
@@ -134,10 +134,7 @@ class _ActiveWindowsTable extends StatelessWidget {
134134
key: controller.key,
135135
color: WidgetStateColor.resolveWith((states) {
136136
if (states.contains(WidgetState.selected)) {
137-
return Theme.of(context)
138-
.colorScheme
139-
.primary
140-
.withAlpha(20);
137+
return Theme.of(context).colorScheme.primary.withAlpha(20);
141138
}
142139
return Colors.transparent;
143140
}),
@@ -153,8 +150,8 @@ class _ActiveWindowsTable extends StatelessWidget {
153150
ListenableBuilder(
154151
listenable: controller.controller,
155152
builder: (BuildContext context, Widget? _) => Text(
156-
controller.controller.view != null
157-
? '${controller.controller.view?.viewId}'
153+
controller.controller.isReady
154+
? '${controller.controller.view.viewId}'
158155
: 'Loading...')),
159156
),
160157
DataCell(
@@ -218,8 +215,15 @@ class _WindowCreatorCard extends StatelessWidget {
218215
children: [
219216
OutlinedButton(
220217
onPressed: () async {
218+
final UniqueKey key = UniqueKey();
221219
windowManagerModel.add(KeyedWindowController(
222-
controller: RegularWindowController()));
220+
key: key,
221+
controller: RegularWindowController(
222+
onDestroyed: () => windowManagerModel.remove(key),
223+
onError: (String error) =>
224+
windowManagerModel.remove(key),
225+
title: "Regular",
226+
size: windowSettings.regularSize)));
223227
},
224228
child: const Text('Regular'),
225229
),

examples/multi_window_ref_app/lib/app/regular_window_content.dart

+12-3
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,16 @@ class _RegularWindowContentState extends State<RegularWindowContent>
8484
children: [
8585
ElevatedButton(
8686
onPressed: () {
87+
final UniqueKey key = UniqueKey();
8788
widget.windowManagerModel.add(KeyedWindowController(
88-
controller: RegularWindowController()));
89+
key: key,
90+
controller: RegularWindowController(
91+
onDestroyed: () =>
92+
widget.windowManagerModel.remove(key),
93+
onError: (String error) =>
94+
widget.windowManagerModel.remove(key),
95+
title: "Regular",
96+
size: widget.windowSettings.regularSize)));
8997
},
9098
child: const Text('Create Regular Window'),
9199
),
@@ -121,8 +129,9 @@ class _RegularWindowContentState extends State<RegularWindowContent>
121129
windowSettings: widget.windowSettings,
122130
windowManagerModel: widget.windowManagerModel,
123131
onDestroyed: () =>
124-
widget.windowManagerModel.remove(controller),
125-
onError: () => widget.windowManagerModel.remove(controller),
132+
widget.windowManagerModel.remove(controller.key),
133+
onError: () =>
134+
widget.windowManagerModel.remove(controller.key),
126135
));
127136
}
128137
}

examples/multi_window_ref_app/lib/app/window_controller_render.dart

-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ class WindowControllerRender extends StatelessWidget {
2424
case WindowArchetype.regular:
2525
return RegularWindow(
2626
key: key,
27-
onDestroyed: onDestroyed,
28-
onError: (String? reason) => onError(),
29-
size: windowSettings.regularSize,
30-
title: "Regular",
3127
controller: controller as RegularWindowController,
3228
child: RegularWindowContent(
3329
window: controller as RegularWindowController,

examples/multi_window_ref_app/lib/app/window_manager_model.dart

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import 'package:flutter/widgets.dart';
22

33
class KeyedWindowController {
44
KeyedWindowController(
5-
{this.parent, this.isMainWindow = false, required this.controller});
5+
{this.parent,
6+
this.isMainWindow = false,
7+
required this.key,
8+
required this.controller});
69

710
final WindowController? parent;
8-
final WindowController controller;
911
final bool isMainWindow;
10-
final UniqueKey key = UniqueKey();
12+
final UniqueKey key;
13+
final WindowController controller;
1114
}
1215

1316
/// Manages a flat list of all of the [WindowController]s that have been
@@ -23,7 +26,7 @@ class WindowManagerModel extends ChangeNotifier {
2326
}
2427

2528
for (final KeyedWindowController controller in _windows) {
26-
if (controller.controller.view?.viewId == _selectedViewId) {
29+
if (controller.controller.view.viewId == _selectedViewId) {
2730
return controller.controller;
2831
}
2932
}
@@ -36,8 +39,8 @@ class WindowManagerModel extends ChangeNotifier {
3639
notifyListeners();
3740
}
3841

39-
void remove(KeyedWindowController window) {
40-
_windows.remove(window);
42+
void remove(UniqueKey key) {
43+
_windows.removeWhere((KeyedWindowController window) => window.key == key);
4144
notifyListeners();
4245
}
4346

examples/multi_window_ref_app/lib/main.dart

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import 'package:flutter/material.dart';
22
import 'app/main_window.dart';
33

44
void main() {
5-
final RegularWindowController controller = RegularWindowController();
5+
final RegularWindowController controller = RegularWindowController(
6+
size: const Size(800, 600),
7+
sizeConstraints: const BoxConstraints(minWidth: 640, minHeight: 480),
8+
title: "Multi-Window Reference Application",
9+
);
610
runWidget(WindowingApp(children: <Widget>[
711
RegularWindow(
812
controller: controller,
9-
size: const Size(800, 600),
10-
sizeConstraints: const BoxConstraints(minWidth: 640, minHeight: 480),
11-
title: "Multi-Window Reference Application",
1213
child: MaterialApp(home: MainWindow(mainController: controller)))
1314
]));
1415
}

0 commit comments

Comments
 (0)