Skip to content

Commit

Permalink
macOS: add macOS actions to address #22
Browse files Browse the repository at this point in the history
This adds the following actions:
* enableShadow
* disableShadow
* invalidateShadows
* addEmptyMaskImage
* removeMaskImage
  • Loading branch information
Adrian-Samoticha committed Dec 3, 2022
1 parent 29c7832 commit 2ad286b
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 7 deletions.
47 changes: 47 additions & 0 deletions lib/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ const _kRemoveToolbar = "RemoveToolbar";
/// (macOS only).
const _kSetToolbarStyle = "SetToolbarStyle";

/// (macOS only)
const _kEnableShadow = "EnableShadow";

/// (macOS only)
const _kDisableShadow = "DisableShadow";

/// (macOS only)
const _kInvalidateShadows = "InvalidateShadows";

/// (macOS only)
const _kAddEmptyMaskImage = "AddEmptyMaskImage";

/// (macOS only)
const _kRemoveMaskImage = "RemoveMaskImage";

final MethodChannel _kChannel = const MethodChannel(_kChannelName);
final Completer<void> _kCompleter = new Completer<void>();

Expand Down Expand Up @@ -626,4 +641,36 @@ class Window {
'toolbarStyle': toolbarStyle.name,
});
}

/// Enables the window's shadow (macOS only).
static Future<void> enableShadow() async {
await _kCompleter.future;
await _kChannel.invokeMethod(_kEnableShadow, {});
}

/// Disables the window's shadow (macOS only).
static Future<void> disableShadow() async {
await _kCompleter.future;
await _kChannel.invokeMethod(_kDisableShadow, {});
}

/// Invalidates the window's shadow (macOS only).
static Future<void> invalidateShadows() async {
await _kCompleter.future;
await _kChannel.invokeMethod(_kInvalidateShadows, {});
}

/// Adds an empty mask image to the window's view (macOS only).
///
/// This will effectively disable the `NSVisualEffectView`'s effect.
static Future<void> addEmptyMaskImage() async {
await _kCompleter.future;
await _kChannel.invokeMethod(_kAddEmptyMaskImage, {});
}

/// Removes the window's mask image (macOS only).
static Future<void> removeMaskImage() async {
await _kCompleter.future;
await _kChannel.invokeMethod(_kRemoveMaskImage, {});
}
}
30 changes: 30 additions & 0 deletions macos/Classes/FlutterAcrylicPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,36 @@ public class FlutterAcrylicPlugin: NSObject, FlutterPlugin {
result(true)
break

case "EnableShadow":
MainFlutterWindowManipulator.enableShadow()

result(true)
break

case "DisableShadow":
MainFlutterWindowManipulator.disableShadow()

result(true)
break

case "InvalidateShadows":
MainFlutterWindowManipulator.invalidateShadows()

result(true)
break

case "AddEmptyMaskImage":
MainFlutterWindowManipulator.addEmptyMaskImage()

result(true)
break

case "RemoveMaskImage":
MainFlutterWindowManipulator.removeMaskImage()

result(true)
break

default:
result(FlutterMethodNotImplemented)
break
Expand Down
76 changes: 69 additions & 7 deletions macos/Classes/MainFlutterWindowManipulator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ public class MainFlutterWindowManipulator {
return
}

self.mainFlutterWindow?.contentView?.superview?.appearance = NSAppearance(named: dark ? .darkAqua : .aqua)
self.mainFlutterWindow!.contentView?.superview?.appearance = NSAppearance(named: dark ? .darkAqua : .aqua)

self.mainFlutterWindow?.invalidateShadow()
self.mainFlutterWindow!.invalidateShadow()
}

@available(macOS 10.14, *)
Expand All @@ -354,10 +354,10 @@ public class MainFlutterWindowManipulator {
return
}

let blurryContainerViewController = self.mainFlutterWindow?.contentViewController as! BlurryContainerViewController;
let blurryContainerViewController = self.mainFlutterWindow!.contentViewController as! BlurryContainerViewController;
(blurryContainerViewController.view as! NSVisualEffectView).material = material

self.mainFlutterWindow?.invalidateShadow()
self.mainFlutterWindow!.invalidateShadow()
}

@available(macOS 10.14, *)
Expand Down Expand Up @@ -388,19 +388,81 @@ public class MainFlutterWindowManipulator {
}

public static func addToolbar() {
if (self.mainFlutterWindow == nil) {
printNotStartedWarning()
return
}

if #available(macOS 10.13, *) {
let newToolbar = NSToolbar()

self.mainFlutterWindow?.toolbar = newToolbar
self.mainFlutterWindow!.toolbar = newToolbar
}
}

public static func removeToolbar() {
self.mainFlutterWindow?.toolbar = nil
if (self.mainFlutterWindow == nil) {
printNotStartedWarning()
return
}

self.mainFlutterWindow!.toolbar = nil
}

@available(macOS 11.0, *)
public static func setToolbarStyle(toolbarStyle: NSWindow.ToolbarStyle) {
self.mainFlutterWindow?.toolbarStyle = toolbarStyle
if (self.mainFlutterWindow == nil) {
printNotStartedWarning()
return
}

self.mainFlutterWindow!.toolbarStyle = toolbarStyle
}

public static func enableShadow() {
if (self.mainFlutterWindow == nil) {
printNotStartedWarning()
return
}

self.mainFlutterWindow!.hasShadow = true
}

public static func disableShadow() {
if (self.mainFlutterWindow == nil) {
printNotStartedWarning()
return
}

self.mainFlutterWindow!.hasShadow = false
}

public static func invalidateShadows() {
if (self.mainFlutterWindow == nil) {
printNotStartedWarning()
return
}

self.mainFlutterWindow!.invalidateShadow()
}

public static func addEmptyMaskImage() {
if (self.mainFlutterWindow == nil) {
printNotStartedWarning()
return
}

let blurryContainerViewController = self.mainFlutterWindow!.contentViewController as! BlurryContainerViewController;
(blurryContainerViewController.view as! NSVisualEffectView).maskImage = NSImage()
}

public static func removeMaskImage() {
if (self.mainFlutterWindow == nil) {
printNotStartedWarning()
return
}

let blurryContainerViewController = self.mainFlutterWindow!.contentViewController as! BlurryContainerViewController;
(blurryContainerViewController.view as! NSVisualEffectView).maskImage = nil
}
}

0 comments on commit 2ad286b

Please sign in to comment.