From 7e673705c1961c5679d469489ee5fb5dc831c545 Mon Sep 17 00:00:00 2001 From: Dennis Ushakov Date: Thu, 26 Jul 2012 15:37:04 +0400 Subject: [PATCH] mountain lion notifications support --- .../com/intellij/ui/GrowlNotifications.java | 2 +- .../src/com/intellij/ui/MacNotifications.java | 27 +++++++ .../ui/MountainLionNotifications.java | 79 +++++++++++++++++++ .../intellij/ui/SystemNotificationsImpl.java | 17 ++-- .../src/misc/registry.properties | 1 + 5 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 platform/platform-impl/src/com/intellij/ui/MacNotifications.java create mode 100644 platform/platform-impl/src/com/intellij/ui/MountainLionNotifications.java diff --git a/platform/platform-impl/src/com/intellij/ui/GrowlNotifications.java b/platform/platform-impl/src/com/intellij/ui/GrowlNotifications.java index 69ee728ab931..9c681bcaf55e 100644 --- a/platform/platform-impl/src/com/intellij/ui/GrowlNotifications.java +++ b/platform/platform-impl/src/com/intellij/ui/GrowlNotifications.java @@ -27,7 +27,7 @@ import java.util.TreeSet; /** * @author mike */ -class GrowlNotifications { +class GrowlNotifications implements MacNotifications { private static final Logger LOG = Logger.getInstance("#com.intellij.ui.GrowlNotifications"); private static GrowlNotifications ourNotifications; diff --git a/platform/platform-impl/src/com/intellij/ui/MacNotifications.java b/platform/platform-impl/src/com/intellij/ui/MacNotifications.java new file mode 100644 index 000000000000..d8c4711e4f25 --- /dev/null +++ b/platform/platform-impl/src/com/intellij/ui/MacNotifications.java @@ -0,0 +1,27 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.ui; + +import org.jetbrains.annotations.NotNull; + +import java.util.Set; + +/** + * @author Dennis.Ushakov + */ +public interface MacNotifications { + void notify(Set allNotifications, @NotNull String notificationName, String title, String description); +} diff --git a/platform/platform-impl/src/com/intellij/ui/MountainLionNotifications.java b/platform/platform-impl/src/com/intellij/ui/MountainLionNotifications.java new file mode 100644 index 000000000000..20e5c1a094ba --- /dev/null +++ b/platform/platform-impl/src/com/intellij/ui/MountainLionNotifications.java @@ -0,0 +1,79 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.ui; + +import com.intellij.ide.AppLifecycleListener; +import com.intellij.openapi.application.ApplicationActivationListener; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.wm.IdeFrame; +import com.intellij.ui.mac.foundation.Foundation; +import com.intellij.ui.mac.foundation.ID; +import com.intellij.util.messages.MessageBusConnection; +import org.jetbrains.annotations.NotNull; + +import java.util.Set; + +import static com.intellij.ui.mac.foundation.Foundation.invoke; +import static com.intellij.ui.mac.foundation.Foundation.nsString; + +/** + * @author Dennis.Ushakov + */ +public class MountainLionNotifications implements MacNotifications { + private static MountainLionNotifications ourNotifications; + + public MountainLionNotifications() { + final MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect(); + connection.subscribe(ApplicationActivationListener.TOPIC, new ApplicationActivationListener() { + @Override + public void applicationActivated(IdeFrame ideFrame) { + cleanupDeliveredNotifications(); + } + + @Override + public void applicationDeactivated(IdeFrame ideFrame) {} + }); + connection.subscribe(AppLifecycleListener.TOPIC, new AppLifecycleListener.Adapter() { + @Override + public void appClosing() { + cleanupDeliveredNotifications(); + } + }); + } + + public static synchronized MacNotifications getNotifications() { + if (ourNotifications == null) { + ourNotifications = new MountainLionNotifications(); + } + + return ourNotifications; + } + + @Override + public void notify(Set allNotifications, @NotNull String notificationName, String title, String description) { + final ID notification = invoke(Foundation.getObjcClass("NSUserNotification"), "new"); + invoke(notification, "setTitle:", nsString(StringUtil.stripHtml(title == null ? "" : title, true).replace("%", "%%"))); + invoke(notification, "setInformativeText:", nsString(StringUtil.stripHtml(description == null ? "" : description, true).replace("%", "%%"))); + final ID center = invoke(Foundation.getObjcClass("NSUserNotificationCenter"), "defaultUserNotificationCenter"); + invoke(center, "deliverNotification:", notification); + } + + public static void cleanupDeliveredNotifications() { + final ID center = invoke(Foundation.getObjcClass("NSUserNotificationCenter"), "defaultUserNotificationCenter"); + invoke(center, "removeAllDeliveredNotifications"); + } +} diff --git a/platform/platform-impl/src/com/intellij/ui/SystemNotificationsImpl.java b/platform/platform-impl/src/com/intellij/ui/SystemNotificationsImpl.java index e5c9f756f1c3..63665c10c431 100644 --- a/platform/platform-impl/src/com/intellij/ui/SystemNotificationsImpl.java +++ b/platform/platform-impl/src/com/intellij/ui/SystemNotificationsImpl.java @@ -21,6 +21,7 @@ import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; import com.intellij.openapi.components.StoragePathMacros; import com.intellij.openapi.util.SystemInfo; +import com.intellij.openapi.util.registry.Registry; import org.jetbrains.annotations.NotNull; import java.util.HashSet; @@ -41,11 +42,11 @@ public class SystemNotificationsImpl extends SystemNotifications implements Pers private boolean myGrowlDisabled = false; public void notify(@NotNull String notificationName, @NotNull String title, @NotNull String text) { - if (!isGrowlEnabled() || ApplicationManager.getApplication().isActive()) return; + if (!areNotificationsEnabled() || ApplicationManager.getApplication().isActive()) return; - final GrowlNotifications nofications; + final MacNotifications notifications; try { - nofications = GrowlNotifications.getNotifications(); + notifications = getMacNotifications(); } catch (Throwable e) { myGrowlDisabled = true; @@ -53,11 +54,17 @@ public class SystemNotificationsImpl extends SystemNotifications implements Pers } myState.NOTIFICATIONS.add(notificationName); - nofications.notify(myState.NOTIFICATIONS, notificationName, title, text); + notifications.notify(myState.NOTIFICATIONS, notificationName, title, text); } - private boolean isGrowlEnabled() { + private static MacNotifications getMacNotifications() { + return SystemInfo.isMacOSMountainLion && Registry.is("ide.mac.mountain.lion.notifications.enabled") ? + MountainLionNotifications.getNotifications() : GrowlNotifications.getNotifications(); + } + + private boolean areNotificationsEnabled() { if (myGrowlDisabled || !SystemInfo.isMac) return false; + if (SystemInfo.isMacOSMountainLion && Registry.is("ide.mac.mountain.lion.notifications.enabled")) return true; if ("true".equalsIgnoreCase(System.getProperty("growl.disable"))) { myGrowlDisabled = true; diff --git a/platform/platform-resources-en/src/misc/registry.properties b/platform/platform-resources-en/src/misc/registry.properties index 1ea88a1e4072..8e4f70291c76 100644 --- a/platform/platform-resources-en/src/misc/registry.properties +++ b/platform/platform-resources-en/src/misc/registry.properties @@ -116,6 +116,7 @@ ide.tabbedPane.dragOutMultiplier=1.2 ide.mac.file.chooser.show.hidden.files=false ide.mac.file.chooser.native=true ide.mac.message.dialogs.as.sheets=true +ide.mac.mountain.lion.notifications.enabled=true ide.mac.inplaceDialogMnemonicsFix=true ide.mac.fix.dialog.showing=false ide.mac.hide.cursor.when.typing=true