|
5 | 5 | import 'package:flutter/material.dart'; |
6 | 6 |
|
7 | 7 | void showRegularWindowEditDialog( |
8 | | - BuildContext context, { |
9 | | - double? initialWidth, |
10 | | - double? initialHeight, |
11 | | - String? initialTitle, |
12 | | - Function(double?, double?, String?)? onSave, |
13 | | -}) { |
14 | | - final TextEditingController widthController = |
15 | | - TextEditingController(text: initialWidth?.toString() ?? ''); |
16 | | - final TextEditingController heightController = |
17 | | - TextEditingController(text: initialHeight?.toString() ?? ''); |
18 | | - final TextEditingController titleController = TextEditingController(text: initialTitle ?? ''); |
19 | | - |
| 8 | + {required BuildContext context, |
| 9 | + required RegularWindowController controller}) { |
20 | 10 | showDialog( |
21 | | - context: context, |
22 | | - builder: (context) { |
23 | | - return AlertDialog( |
24 | | - title: Text("Edit Window Properties"), |
25 | | - content: StatefulBuilder( |
26 | | - builder: (context, setState) { |
27 | | - return Column( |
28 | | - mainAxisSize: MainAxisSize.min, |
29 | | - children: [ |
30 | | - TextField( |
31 | | - controller: widthController, |
32 | | - keyboardType: TextInputType.number, |
33 | | - decoration: InputDecoration(labelText: "Width"), |
34 | | - ), |
35 | | - TextField( |
36 | | - controller: heightController, |
37 | | - keyboardType: TextInputType.number, |
38 | | - decoration: InputDecoration(labelText: "Height"), |
39 | | - ), |
40 | | - TextField( |
41 | | - controller: titleController, |
42 | | - decoration: InputDecoration(labelText: "Title"), |
43 | | - ), |
44 | | - ], |
45 | | - ); |
46 | | - }, |
47 | | - ), |
48 | | - actions: [ |
49 | | - TextButton( |
50 | | - onPressed: () => Navigator.of(context).pop(), |
51 | | - child: Text("Cancel"), |
| 11 | + context: context, |
| 12 | + builder: (context) => _RegularWindowEditDialog( |
| 13 | + controller: controller, onClose: () => Navigator.pop(context))); |
| 14 | +} |
| 15 | + |
| 16 | +class _RegularWindowEditDialog extends StatefulWidget { |
| 17 | + const _RegularWindowEditDialog( |
| 18 | + {required this.controller, required this.onClose}); |
| 19 | + |
| 20 | + final RegularWindowController controller; |
| 21 | + final VoidCallback onClose; |
| 22 | + |
| 23 | + @override |
| 24 | + State<StatefulWidget> createState() => _RegularWindowEditDialogState(); |
| 25 | +} |
| 26 | + |
| 27 | +class _RegularWindowEditDialogState extends State<_RegularWindowEditDialog> { |
| 28 | + late Size initialSize; |
| 29 | + late String initialTitle; |
| 30 | + late bool initialFullscreen; |
| 31 | + late bool initialMaximized; |
| 32 | + late bool initialMinimized; |
| 33 | + |
| 34 | + late final TextEditingController widthController; |
| 35 | + late final TextEditingController heightController; |
| 36 | + late final TextEditingController titleController; |
| 37 | + |
| 38 | + bool? nextIsFullscreen; |
| 39 | + bool? nextIsMaximized; |
| 40 | + bool? nextIsMinized; |
| 41 | + |
| 42 | + @override |
| 43 | + void initState() { |
| 44 | + super.initState(); |
| 45 | + initialSize = widget.controller.contentSize; |
| 46 | + initialTitle = ""; // TODO: Get the title |
| 47 | + initialFullscreen = widget.controller.isFullscreen(); |
| 48 | + initialMaximized = widget.controller.isMaximized(); |
| 49 | + initialMinimized = widget.controller.isMinimized(); |
| 50 | + |
| 51 | + widthController = TextEditingController(text: initialSize.width.toString()); |
| 52 | + heightController = |
| 53 | + TextEditingController(text: initialSize.height.toString()); |
| 54 | + titleController = TextEditingController(text: initialTitle); |
| 55 | + |
| 56 | + widget.controller.addListener(_onNotification); |
| 57 | + } |
| 58 | + |
| 59 | + void _onNotification() { |
| 60 | + // We listen on the state of the controller. If a value that the user |
| 61 | + // can edit changes from what it was initially set to, we invalidate |
| 62 | + // their current change and store the new "initial" value. |
| 63 | + if (widget.controller.contentSize != initialSize) { |
| 64 | + initialSize = widget.controller.contentSize; |
| 65 | + widthController.text = widget.controller.contentSize.width.toString(); |
| 66 | + heightController.text = widget.controller.contentSize.height.toString(); |
| 67 | + } |
| 68 | + if (widget.controller.isFullscreen() != initialFullscreen) { |
| 69 | + setState(() { |
| 70 | + initialFullscreen = widget.controller.isFullscreen(); |
| 71 | + nextIsFullscreen = null; |
| 72 | + }); |
| 73 | + } |
| 74 | + if (widget.controller.isMaximized() != initialMaximized) { |
| 75 | + setState(() { |
| 76 | + initialMaximized = widget.controller.isMaximized(); |
| 77 | + nextIsMaximized = null; |
| 78 | + }); |
| 79 | + } |
| 80 | + if (widget.controller.isMinimized() != initialMinimized) { |
| 81 | + setState(() { |
| 82 | + initialMinimized = widget.controller.isMinimized(); |
| 83 | + nextIsMinized = null; |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @override |
| 89 | + void dispose() { |
| 90 | + super.dispose(); |
| 91 | + widget.controller.removeListener(_onNotification); |
| 92 | + } |
| 93 | + |
| 94 | + @override |
| 95 | + Widget build(BuildContext context) { |
| 96 | + return AlertDialog( |
| 97 | + title: Text("Edit Window Properties"), |
| 98 | + content: Column( |
| 99 | + mainAxisSize: MainAxisSize.min, |
| 100 | + children: [ |
| 101 | + TextField( |
| 102 | + controller: widthController, |
| 103 | + keyboardType: TextInputType.number, |
| 104 | + decoration: InputDecoration(labelText: "Width"), |
52 | 105 | ), |
53 | | - TextButton( |
54 | | - onPressed: () { |
55 | | - double? width = double.tryParse(widthController.text); |
56 | | - double? height = double.tryParse(heightController.text); |
57 | | - String? title = titleController.text.isEmpty ? null : titleController.text; |
58 | | - |
59 | | - onSave?.call(width, height, title); |
60 | | - Navigator.of(context).pop(); |
61 | | - }, |
62 | | - child: Text("Save"), |
| 106 | + TextField( |
| 107 | + controller: heightController, |
| 108 | + keyboardType: TextInputType.number, |
| 109 | + decoration: InputDecoration(labelText: "Height"), |
63 | 110 | ), |
| 111 | + TextField( |
| 112 | + controller: titleController, |
| 113 | + decoration: InputDecoration(labelText: "Title"), |
| 114 | + ), |
| 115 | + CheckboxListTile( |
| 116 | + title: const Text('Fullscreen'), |
| 117 | + value: nextIsFullscreen ?? initialFullscreen, |
| 118 | + onChanged: (bool? value) { |
| 119 | + if (value != null) { |
| 120 | + setState(() => nextIsFullscreen = value); |
| 121 | + } |
| 122 | + }), |
| 123 | + CheckboxListTile( |
| 124 | + title: const Text('Maximized'), |
| 125 | + value: nextIsMaximized ?? initialMaximized, |
| 126 | + onChanged: (bool? value) { |
| 127 | + if (value != null) { |
| 128 | + setState(() => nextIsMaximized = value); |
| 129 | + } |
| 130 | + }), |
| 131 | + CheckboxListTile( |
| 132 | + title: const Text('Minimized'), |
| 133 | + value: nextIsMinized ?? initialMinimized, |
| 134 | + onChanged: (bool? value) { |
| 135 | + if (value != null) { |
| 136 | + setState(() => nextIsMinized = value); |
| 137 | + } |
| 138 | + }) |
64 | 139 | ], |
| 140 | + ), |
| 141 | + actions: [ |
| 142 | + TextButton( |
| 143 | + onPressed: () => widget.onClose(), |
| 144 | + child: Text("Cancel"), |
| 145 | + ), |
| 146 | + TextButton( |
| 147 | + onPressed: () => _onSave(), |
| 148 | + child: Text("Save"), |
| 149 | + ), |
| 150 | + ], |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + void _onSave() { |
| 155 | + double? width = double.tryParse(widthController.text); |
| 156 | + double? height = double.tryParse(heightController.text); |
| 157 | + String? title = titleController.text.isEmpty ? null : titleController.text; |
| 158 | + if (width != null && height != null) { |
| 159 | + widget.controller.updateContentSize( |
| 160 | + WindowSizing(preferredSize: Size(width, height)), |
65 | 161 | ); |
66 | | - }, |
67 | | - ); |
| 162 | + } |
| 163 | + if (title != null) { |
| 164 | + widget.controller.setTitle(title); |
| 165 | + } |
| 166 | + if (nextIsFullscreen != null) { |
| 167 | + if (widget.controller.isFullscreen() != nextIsFullscreen) { |
| 168 | + widget.controller.setFullscreen(nextIsFullscreen!); |
| 169 | + } |
| 170 | + } |
| 171 | + if (nextIsMaximized != null) { |
| 172 | + if (widget.controller.isMaximized() != nextIsMaximized) { |
| 173 | + widget.controller.setMaximized(nextIsMaximized!); |
| 174 | + } |
| 175 | + } |
| 176 | + if (nextIsMinized != null) { |
| 177 | + if (widget.controller.isMinimized() != nextIsMinized) { |
| 178 | + widget.controller.setMinimized(nextIsMinized!); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + widget.onClose(); |
| 183 | + } |
68 | 184 | } |
0 commit comments