Skip to content

Commit 1b0157e

Browse files
committed
[macos] Implement isVisibleOnAllWorkspaces & setVisibleOnAllWorkspaces method
1 parent 92944c1 commit 1b0157e

File tree

9 files changed

+185
-78
lines changed

9 files changed

+185
-78
lines changed

README-ZH.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ class HomePage extends StatefulWidget {
193193
class _HomePageState extends State<HomePage> with WindowListener {
194194
@override
195195
void initState() {
196-
windowManager.addListener(this);
197196
super.initState();
197+
windowManager.addListener(this);
198198
}
199199
200200
@override
@@ -305,9 +305,9 @@ class HomePage extends StatefulWidget {
305305
class _HomePageState extends State<HomePage> with WindowListener {
306306
@override
307307
void initState() {
308+
super.initState();
308309
windowManager.addListener(this);
309310
_init();
310-
super.initState();
311311
}
312312
313313
@override
@@ -441,7 +441,7 @@ bool Win32Window::CreateAndShow(const std::wstring& title,
441441
nullptr, nullptr, GetModuleHandle(nullptr), this);
442442
```
443443

444-
使用 flutter 3.7 创建的Windows项目
444+
使用 flutter 3.7 创建的 Windows 项目
445445
更改 `windows/runner/flutter_window.cpp` 如下:
446446

447447
```diff
@@ -739,6 +739,26 @@ Sets progress value in progress bar. Valid range is [0, 1.0].
739739
Sets window/taskbar icon.
740740

741741

742+
##### isVisibleOnAllWorkspaces `macos`
743+
744+
Returns `bool` - Whether the window is visible on all workspaces.
745+
746+
747+
##### setVisibleOnAllWorkspaces `macos`
748+
749+
Sets whether the window should be visible on all workspaces.
750+
751+
Note: If you need to support dragging a window on top of a fullscreen
752+
window on another screen, you need to modify MainFlutterWindow
753+
to inherit from NSPanel
754+
755+
```swift
756+
class MainFlutterWindow: NSPanel {
757+
// ...
758+
}
759+
```
760+
761+
742762
##### setBadgeLabel `macos`
743763

744764
Set/unset label on taskbar(dock) app icon

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ bool Win32Window::CreateAndShow(const std::wstring& title,
443443

444444
Since flutter 3.7 new windows project
445445
Change the file `windows/runner/flutter_window.cpp` as follows:
446+
446447
```diff
447448
bool FlutterWindow::OnCreate() {
448449
...
@@ -738,6 +739,26 @@ Sets progress value in progress bar. Valid range is [0, 1.0].
738739
Sets window/taskbar icon.
739740

740741

742+
##### isVisibleOnAllWorkspaces `macos`
743+
744+
Returns `bool` - Whether the window is visible on all workspaces.
745+
746+
747+
##### setVisibleOnAllWorkspaces `macos`
748+
749+
Sets whether the window should be visible on all workspaces.
750+
751+
Note: If you need to support dragging a window on top of a fullscreen
752+
window on another screen, you need to modify MainFlutterWindow
753+
to inherit from NSPanel
754+
755+
```swift
756+
class MainFlutterWindow: NSPanel {
757+
// ...
758+
}
759+
```
760+
761+
741762
##### setBadgeLabel `macos`
742763

743764
Set/unset label on taskbar(dock) app icon

example/lib/pages/home.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
5656
double _opacity = 1;
5757
bool _isIgnoreMouseEvents = false;
5858
String _iconType = _kIconTypeOriginal;
59+
bool _isVisibleOnAllWorkspaces = false;
5960

6061
@override
6162
void initState() {
@@ -754,6 +755,27 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
754755
),
755756
onTap: () => _handleSetIcon(_kIconTypeDefault),
756757
),
758+
PreferenceListSwitchItem(
759+
title: const Text(
760+
'isVisibleOnAllWorkspaces / setVisibleOnAllWorkspaces',
761+
),
762+
onTap: () async {
763+
bool isVisibleOnAllWorkspaces =
764+
await windowManager.isVisibleOnAllWorkspaces();
765+
BotToast.showText(
766+
text: 'isVisibleOnAllWorkspaces: $isVisibleOnAllWorkspaces',
767+
);
768+
},
769+
value: _isVisibleOnAllWorkspaces,
770+
onChanged: (newValue) {
771+
_isVisibleOnAllWorkspaces = newValue;
772+
windowManager.setVisibleOnAllWorkspaces(
773+
_isVisibleOnAllWorkspaces,
774+
visibleOnFullScreen: _isVisibleOnAllWorkspaces,
775+
);
776+
setState(() {});
777+
},
778+
),
757779
PreferenceListItem(
758780
title: const Text('setBadgeLabel'),
759781
accessoryView: Row(

example/macos/Runner/MainFlutterWindow.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Cocoa
22
import FlutterMacOS
33
import window_manager
44

5-
class MainFlutterWindow: NSWindow {
5+
class MainFlutterWindow: NSPanel {
66
override func awakeFromNib() {
77
let flutterViewController = FlutterViewController.init()
88
let windowFrame = self.frame
@@ -15,7 +15,7 @@ class MainFlutterWindow: NSWindow {
1515

1616
super.awakeFromNib()
1717
}
18-
18+
1919
override public func order(_ place: NSWindow.OrderingMode, relativeTo otherWin: Int) {
2020
super.order(place, relativeTo: otherWin)
2121
hiddenWindowAtLaunch()

example/pubspec.lock

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
# Generated by pub
22
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
4-
archive:
5-
dependency: transitive
6-
description:
7-
name: archive
8-
sha256: "80e5141fafcb3361653ce308776cfd7d45e6e9fbb429e14eec571382c0c5fecb"
9-
url: "https://pub.dev"
10-
source: hosted
11-
version: "3.3.2"
124
async:
135
dependency: transitive
146
description:
157
name: async
16-
sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0
8+
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
179
url: "https://pub.dev"
1810
source: hosted
19-
version: "2.10.0"
11+
version: "2.11.0"
2012
boolean_selector:
2113
dependency: transitive
2214
description:
@@ -37,10 +29,10 @@ packages:
3729
dependency: transitive
3830
description:
3931
name: characters
40-
sha256: e6a326c8af69605aec75ed6c187d06b349707a27fbff8222ca9cc2cff167975c
32+
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
4133
url: "https://pub.dev"
4234
source: hosted
43-
version: "1.2.1"
35+
version: "1.3.0"
4436
clock:
4537
dependency: transitive
4638
description:
@@ -53,18 +45,10 @@ packages:
5345
dependency: transitive
5446
description:
5547
name: collection
56-
sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0
57-
url: "https://pub.dev"
58-
source: hosted
59-
version: "1.17.0"
60-
crypto:
61-
dependency: transitive
62-
description:
63-
name: crypto
64-
sha256: aa274aa7774f8964e4f4f38cc994db7b6158dd36e9187aaceaddc994b35c6c67
48+
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
6549
url: "https://pub.dev"
6650
source: hosted
67-
version: "3.0.2"
51+
version: "1.17.1"
6852
cupertino_icons:
6953
dependency: "direct main"
7054
description:
@@ -126,10 +110,10 @@ packages:
126110
dependency: transitive
127111
description:
128112
name: js
129-
sha256: "5528c2f391ededb7775ec1daa69e65a2d61276f7552de2b5f7b8d34ee9fd4ab7"
113+
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
130114
url: "https://pub.dev"
131115
source: hosted
132-
version: "0.6.5"
116+
version: "0.6.7"
133117
lints:
134118
dependency: transitive
135119
description:
@@ -142,10 +126,10 @@ packages:
142126
dependency: transitive
143127
description:
144128
name: matcher
145-
sha256: "16db949ceee371e9b99d22f88fa3a73c4e59fd0afed0bd25fc336eb76c198b72"
129+
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
146130
url: "https://pub.dev"
147131
source: hosted
148-
version: "0.12.13"
132+
version: "0.12.15"
149133
material_color_utilities:
150134
dependency: transitive
151135
description:
@@ -166,18 +150,18 @@ packages:
166150
dependency: transitive
167151
description:
168152
name: meta
169-
sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42"
153+
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
170154
url: "https://pub.dev"
171155
source: hosted
172-
version: "1.8.0"
156+
version: "1.9.1"
173157
path:
174158
dependency: transitive
175159
description:
176160
name: path
177-
sha256: db9d4f58c908a4ba5953fcee2ae317c94889433e5024c27ce74a37f94267945b
161+
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
178162
url: "https://pub.dev"
179163
source: hosted
180-
version: "1.8.2"
164+
version: "1.8.3"
181165
platform:
182166
dependency: transitive
183167
description:
@@ -275,10 +259,10 @@ packages:
275259
dependency: transitive
276260
description:
277261
name: test_api
278-
sha256: ad540f65f92caa91bf21dfc8ffb8c589d6e4dc0c2267818b4cc2792857706206
262+
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
279263
url: "https://pub.dev"
280264
source: hosted
281-
version: "0.4.16"
265+
version: "0.5.1"
282266
tray_manager:
283267
dependency: "direct main"
284268
description:
@@ -287,14 +271,6 @@ packages:
287271
url: "https://pub.dev"
288272
source: hosted
289273
version: "0.2.0"
290-
typed_data:
291-
dependency: transitive
292-
description:
293-
name: typed_data
294-
sha256: "26f87ade979c47a150c9eaab93ccd2bebe70a27dc0b4b29517f2904f04eb11a5"
295-
url: "https://pub.dev"
296-
source: hosted
297-
version: "1.3.1"
298274
vector_math:
299275
dependency: transitive
300276
description:
@@ -307,18 +283,18 @@ packages:
307283
dependency: transitive
308284
description:
309285
name: vm_service
310-
sha256: e7fb6c2282f7631712b69c19d1bff82f3767eea33a2321c14fa59ad67ea391c7
286+
sha256: f6deed8ed625c52864792459709183da231ebf66ff0cf09e69b573227c377efe
311287
url: "https://pub.dev"
312288
source: hosted
313-
version: "9.4.0"
289+
version: "11.3.0"
314290
webdriver:
315291
dependency: transitive
316292
description:
317293
name: webdriver
318-
sha256: ef67178f0cc7e32c1494645b11639dd1335f1d18814aa8435113a92e9ef9d841
294+
sha256: "3c923e918918feeb90c4c9fdf1fe39220fa4c0e8e2c0fffaded174498ef86c49"
319295
url: "https://pub.dev"
320296
source: hosted
321-
version: "3.0.1"
297+
version: "3.0.2"
322298
window_manager:
323299
dependency: "direct main"
324300
description:
@@ -327,5 +303,5 @@ packages:
327303
source: path
328304
version: "0.3.2"
329305
sdks:
330-
dart: ">=2.18.0 <3.0.0"
306+
dart: ">=3.0.0-0 <4.0.0"
331307
flutter: ">=2.5.0"

lib/src/window_manager.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,37 @@ class WindowManager {
547547
await _channel.invokeMethod('setIcon', arguments);
548548
}
549549

550+
/// Returns `bool` - Whether the window is visible on all workspaces.
551+
///
552+
/// @platforms macos
553+
Future<bool> isVisibleOnAllWorkspaces() async {
554+
return await _channel.invokeMethod('isVisibleOnAllWorkspaces');
555+
}
556+
557+
/// Sets whether the window should be visible on all workspaces.
558+
///
559+
/// Note: If you need to support dragging a window on top of a fullscreen
560+
/// window on another screen, you need to modify MainFlutterWindow
561+
/// to inherit from NSPanel
562+
///
563+
/// ```swift
564+
/// class MainFlutterWindow: NSPanel {
565+
/// // ...
566+
/// }
567+
/// ```
568+
///
569+
/// @platforms macos
570+
Future<void> setVisibleOnAllWorkspaces(
571+
bool visible, {
572+
bool? visibleOnFullScreen,
573+
}) async {
574+
final Map<String, dynamic> arguments = {
575+
'visible': visible,
576+
'visibleOnFullScreen': visibleOnFullScreen ?? false,
577+
};
578+
await _channel.invokeMethod('setVisibleOnAllWorkspaces', arguments);
579+
}
580+
550581
/// Set/unset label on taskbar(dock) app icon
551582
///
552583
/// Note that it's required to request access at your AppDelegate.swift like this:

0 commit comments

Comments
 (0)