From d3987ad710f14ec9bfda07d2d68cb68f9c65e4a6 Mon Sep 17 00:00:00 2001 From: nikitatg <96043303+nikitatg@users.noreply.github.com> Date: Sat, 8 Apr 2023 12:13:32 +0300 Subject: [PATCH] [macos] Feature to set labeled badge on taskbar aka dock (#305) * [Linux] implementation of methods: setIcon, isFocused * [macos] feature to set badge with text on app icon at taskbar --- lib/src/window_manager.dart | 13 +++++++++++++ macos/Classes/WindowManager.swift | 5 +++++ macos/Classes/WindowManagerPlugin.swift | 4 ++++ 3 files changed, 22 insertions(+) diff --git a/lib/src/window_manager.dart b/lib/src/window_manager.dart index 4b2b1330..19cf5cfd 100644 --- a/lib/src/window_manager.dart +++ b/lib/src/window_manager.dart @@ -544,6 +544,19 @@ class WindowManager { await _channel.invokeMethod('setIcon', arguments); } + /// Set/unset label on taskbar(dock) app icon + /// + /// Note that it's required to request access at your AppDelegate.swift like this: + /// UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge]) <...> + /// ^--- + /// @platforms macos + Future setBadgeLabel([String? label]) async { + final Map arguments = { + 'label': label ?? '', + }; + await _channel.invokeMethod('setBadgeLabel', arguments); + } + /// Returns `bool` - Whether the window has a shadow. On Windows, always returns true unless window is frameless. /// /// @platforms macos,windows diff --git a/macos/Classes/WindowManager.swift b/macos/Classes/WindowManager.swift index 6bd5e641..c4a9bea3 100644 --- a/macos/Classes/WindowManager.swift +++ b/macos/Classes/WindowManager.swift @@ -370,6 +370,11 @@ public class WindowManager: NSObject, NSWindowDelegate { let isSkipTaskbar: Bool = args["isSkipTaskbar"] as! Bool NSApplication.shared.setActivationPolicy(isSkipTaskbar ? .accessory : .regular) } + + public func setBadgeLabel(_ args: [String: Any]) { + let label: String = args["label"] as! String + NSApplication.shared.dockTile.badgeLabel = label + } public func setProgressBar(_ args: [String: Any]) { let progress: CGFloat = CGFloat(truncating: args["progress"] as! NSNumber) diff --git a/macos/Classes/WindowManagerPlugin.swift b/macos/Classes/WindowManagerPlugin.swift index eb40549e..133e062a 100644 --- a/macos/Classes/WindowManagerPlugin.swift +++ b/macos/Classes/WindowManagerPlugin.swift @@ -208,6 +208,10 @@ public class WindowManagerPlugin: NSObject, FlutterPlugin { windowManager.setSkipTaskbar(args) result(true) break + case "setBadgeLabel": + windowManager.setBadgeLabel(args) + result(true) + break case "setProgressBar": windowManager.setProgressBar(args) result(true)