diff --git a/lib/window.dart b/lib/window.dart index db7795f..7e42e11 100644 --- a/lib/window.dart +++ b/lib/window.dart @@ -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 _kCompleter = new Completer(); @@ -626,4 +641,36 @@ class Window { 'toolbarStyle': toolbarStyle.name, }); } + + /// Enables the window's shadow (macOS only). + static Future enableShadow() async { + await _kCompleter.future; + await _kChannel.invokeMethod(_kEnableShadow, {}); + } + + /// Disables the window's shadow (macOS only). + static Future disableShadow() async { + await _kCompleter.future; + await _kChannel.invokeMethod(_kDisableShadow, {}); + } + + /// Invalidates the window's shadow (macOS only). + static Future 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 addEmptyMaskImage() async { + await _kCompleter.future; + await _kChannel.invokeMethod(_kAddEmptyMaskImage, {}); + } + + /// Removes the window's mask image (macOS only). + static Future removeMaskImage() async { + await _kCompleter.future; + await _kChannel.invokeMethod(_kRemoveMaskImage, {}); + } } diff --git a/macos/Classes/FlutterAcrylicPlugin.swift b/macos/Classes/FlutterAcrylicPlugin.swift index 4095e2b..001e091 100644 --- a/macos/Classes/FlutterAcrylicPlugin.swift +++ b/macos/Classes/FlutterAcrylicPlugin.swift @@ -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 diff --git a/macos/Classes/MainFlutterWindowManipulator.swift b/macos/Classes/MainFlutterWindowManipulator.swift index df3b7b1..299e9f9 100644 --- a/macos/Classes/MainFlutterWindowManipulator.swift +++ b/macos/Classes/MainFlutterWindowManipulator.swift @@ -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, *) @@ -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, *) @@ -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 } }