Skip to content

Commit 11ecf53

Browse files
committed
[macos] Implement getOpacity & setOpacity methods #37
1 parent 829bd5c commit 11ecf53

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

example/lib/pages/home.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class _HomePageState extends State<HomePage> with WindowListener {
3939
bool _isAlwaysOnTop = false;
4040
bool _isSkipTaskbar = false;
4141
bool _hasShadow = true;
42+
double _opacity = 1;
4243

4344
@override
4445
void initState() {
@@ -447,6 +448,35 @@ class _HomePageState extends State<HomePage> with WindowListener {
447448
setState(() {});
448449
},
449450
),
451+
PreferenceListItem(
452+
title: Text('getOpacity / setOpacity'),
453+
onTap: () async {
454+
double opacity = await windowManager.getOpacity();
455+
BotToast.showText(
456+
text: 'opacity: $opacity',
457+
);
458+
},
459+
accessoryView: Row(
460+
children: [
461+
CupertinoButton(
462+
child: Text('1'),
463+
onPressed: () async {
464+
_opacity = 1;
465+
windowManager.setOpacity(_opacity);
466+
setState(() {});
467+
},
468+
),
469+
CupertinoButton(
470+
child: Text('0.5'),
471+
onPressed: () async {
472+
_opacity = 0.5;
473+
windowManager.setOpacity(_opacity);
474+
setState(() {});
475+
},
476+
),
477+
],
478+
),
479+
),
450480
PreferenceListItem(
451481
title: Text('createSubWindow'),
452482
onTap: () async {

lib/src/window_manager.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,19 @@ class WindowManager {
349349
await _channel.invokeMethod('setHasShadow', arguments);
350350
}
351351

352+
/// Returns double - between 0.0 (fully transparent) and 1.0 (fully opaque). On Linux, always returns 1.
353+
Future<double> getOpacity() async {
354+
return await _channel.invokeMethod('getOpacity');
355+
}
356+
357+
/// Sets the opacity of the window.
358+
Future<void> setOpacity(double opacity) async {
359+
final Map<String, dynamic> arguments = {
360+
'opacity': opacity,
361+
};
362+
await _channel.invokeMethod('setOpacity', arguments);
363+
}
364+
352365
Future<void> startDragging() async {
353366
await _channel.invokeMethod('startDragging');
354367
}

macos/Classes/WindowManager.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,15 @@ public class WindowManager: NSObject, NSWindowDelegate {
348348
mainWindow.invalidateShadow();
349349
}
350350

351+
public func getOpacity() -> CGFloat {
352+
return mainWindow.alphaValue
353+
}
354+
355+
public func setOpacity(_ args: [String: Any]) {
356+
let opacity: CGFloat = CGFloat(truncating: args["opacity"] as! NSNumber)
357+
mainWindow.alphaValue = opacity
358+
}
359+
351360
public func startDragging() {
352361
DispatchQueue.main.async {
353362
let window: NSWindow = self.mainWindow

macos/Classes/WindowManagerPlugin.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ public class WindowManagerPlugin: NSObject, FlutterPlugin {
187187
windowManager.setHasShadow(args)
188188
result(true)
189189
break
190+
case "getOpacity":
191+
result(windowManager.getOpacity())
192+
break
193+
case "setOpacity":
194+
windowManager.setOpacity(args)
195+
result(true)
196+
break
190197
case "startDragging":
191198
windowManager.startDragging()
192199
result(true)

0 commit comments

Comments
 (0)