Merge pull request #735 from deathbeam/notifier-global-options

Move the notification options to Notifier
This commit is contained in:
Adam
2018-03-01 19:14:58 -05:00
committed by GitHub
9 changed files with 99 additions and 151 deletions

View File

@@ -26,39 +26,40 @@ package net.runelite.client;
import com.google.common.escape.Escaper; import com.google.common.escape.Escaper;
import com.google.common.escape.Escapers; import com.google.common.escape.Escapers;
import com.google.inject.Inject;
import java.awt.Toolkit; import java.awt.Toolkit;
import java.awt.TrayIcon; import java.awt.TrayIcon;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.inject.Provider;
import javax.inject.Singleton;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.client.config.RuneLiteConfig; import net.runelite.client.config.RuneLiteConfig;
import net.runelite.client.util.OSType; import net.runelite.client.util.OSType;
import net.runelite.client.ui.ClientUI;
@Singleton
@Slf4j @Slf4j
public class Notifier public class Notifier
{ {
// Default timeout of notification in milliseconds // Default timeout of notification in milliseconds
private static final int DEFAULT_TIMEOUT = 10000; private static final int DEFAULT_TIMEOUT = 10000;
private static final String DOUBLE_QUOTE = "\""; private static final String DOUBLE_QUOTE = "\"";
private static final Escaper SHELL_ESCAPE; private static final Escaper SHELL_ESCAPE = Escapers.builder()
.addEscape('"', "'")
static .build();
{
final Escapers.Builder builder = Escapers.builder();
builder.addEscape('"', "'");
SHELL_ESCAPE = builder.build();
}
private final String appName; private final String appName;
private final TrayIcon trayIcon;
private final RuneLiteConfig runeLiteConfig; private final RuneLiteConfig runeLiteConfig;
private final Provider<ClientUI> clientUI;
Notifier(final String appName, final TrayIcon trayIcon, final RuneLiteConfig runeliteConfig) @Inject
private Notifier(final Provider<ClientUI> clientUI, final RuneLiteConfig runeliteConfig, final RuneLiteProperties
runeLiteProperties)
{ {
this.appName = appName; this.appName = runeLiteProperties.getTitle();
this.trayIcon = trayIcon; this.clientUI = clientUI;
this.runeLiteConfig = runeliteConfig; this.runeLiteConfig = runeliteConfig;
} }
@@ -69,6 +70,28 @@ public class Notifier
public void notify(String message, TrayIcon.MessageType type) public void notify(String message, TrayIcon.MessageType type)
{ {
if (!runeLiteConfig.enableTrayNotifications())
{
return;
}
final ClientUI clientUI = this.clientUI.get();
if (clientUI == null)
{
return;
}
if (!runeLiteConfig.sendNotificationsWhenFocused() && clientUI.isFocused())
{
return;
}
if (runeLiteConfig.requestFocusOnNotification())
{
clientUI.requestFocus();
}
sendNotification(appName, message, type, null); sendNotification(appName, message, type, null);
if (runeLiteConfig.enableNotificationSound()) if (runeLiteConfig.enableNotificationSound())
@@ -105,9 +128,16 @@ public class Notifier
final String message, final String message,
final TrayIcon.MessageType type) final TrayIcon.MessageType type)
{ {
if (trayIcon != null) final ClientUI clientUI = this.clientUI.get();
if (clientUI == null)
{ {
trayIcon.displayMessage(title, message, type); return;
}
if (clientUI.getTrayIcon() != null)
{
clientUI.getTrayIcon().displayMessage(title, message, type);
} }
} }

View File

@@ -98,7 +98,6 @@ public class RuneLite
Client client; Client client;
ClientUI gui; ClientUI gui;
Notifier notifier;
public static void main(String[] args) throws Exception public static void main(String[] args) throws Exception
{ {
@@ -161,9 +160,6 @@ public class RuneLite
eventBus.register(gui); eventBus.register(gui);
eventBus.register(pluginManager); eventBus.register(pluginManager);
// Setup the notifier
notifier = new Notifier(properties.getTitle(), gui.getTrayIcon(), runeliteConfig);
// Tell the plugin manager if client is outdated or not // Tell the plugin manager if client is outdated or not
pluginManager.setOutdated(isOutdated); pluginManager.setOutdated(isOutdated);
@@ -219,12 +215,6 @@ public class RuneLite
this.client = client; this.client = client;
} }
@VisibleForTesting
public void setNotifier(Notifier notifier)
{
this.notifier = notifier;
}
public static Injector getInjector() public static Injector getInjector()
{ {
return injector; return injector;

View File

@@ -73,12 +73,6 @@ public class RuneLiteModule extends AbstractModule
return runelite.gui; return runelite.gui;
} }
@Provides
Notifier provideNotifier(RuneLite runeLite)
{
return runeLite.notifier;
}
@Provides @Provides
@Singleton @Singleton
RuneLiteConfig provideConfig(ConfigManager configManager) RuneLiteConfig provideConfig(ConfigManager configManager)

View File

@@ -76,6 +76,16 @@ public interface RuneLiteConfig extends Config
return false; return false;
} }
@ConfigItem(
keyName = "notificationTray",
name = "Enable tray notifications",
description = "Enables tray notifications"
)
default boolean enableTrayNotifications()
{
return true;
}
@ConfigItem( @ConfigItem(
keyName = "notificationSound", keyName = "notificationSound",
name = "Enable sound on notifications", name = "Enable sound on notifications",
@@ -85,4 +95,24 @@ public interface RuneLiteConfig extends Config
{ {
return true; return true;
} }
@ConfigItem(
keyName = "notificationFocused",
name = "Send notifications when focused",
description = "Toggles idle notifications for when the client is focused"
)
default boolean sendNotificationsWhenFocused()
{
return false;
}
@ConfigItem(
keyName = "notificationRequestFocus",
name = "Request focus on notification",
description = "Toggles window focus request"
)
default boolean requestFocusOnNotification()
{
return true;
}
} }

View File

@@ -35,39 +35,6 @@ import net.runelite.client.config.ConfigItem;
) )
public interface IdleNotifierConfig extends Config public interface IdleNotifierConfig extends Config
{ {
@ConfigItem(
keyName = "tray",
name = "Send Tray Notification",
description = "Toggles tray notifications",
position = 2
)
default boolean sendTrayNotification()
{
return true;
}
@ConfigItem(
keyName = "focused",
name = "Notify When Focused",
description = "Toggles idle notifications for when the client is focused",
position = 3
)
default boolean alertWhenFocused()
{
return false;
}
@ConfigItem(
keyName = "request",
name = "Request Window Focus",
description = "Toggles window focus request",
position = 4
)
default boolean requestFocus()
{
return true;
}
@ConfigItem( @ConfigItem(
keyName = "timeout", keyName = "timeout",
name = "Idle Timeout (ms)", name = "Idle Timeout (ms)",

View File

@@ -25,6 +25,12 @@
*/ */
package net.runelite.client.plugins.idlenotifier; package net.runelite.client.plugins.idlenotifier;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Provides;
import java.time.Duration;
import java.time.Instant;
import javax.inject.Inject;
import net.runelite.api.Actor;
import static net.runelite.api.AnimationID.COOKING_FIRE; import static net.runelite.api.AnimationID.COOKING_FIRE;
import static net.runelite.api.AnimationID.COOKING_RANGE; import static net.runelite.api.AnimationID.COOKING_RANGE;
import static net.runelite.api.AnimationID.CRAFTING_GLASSBLOWING; import static net.runelite.api.AnimationID.CRAFTING_GLASSBLOWING;
@@ -91,12 +97,6 @@ import static net.runelite.api.AnimationID.WOODCUTTING_IRON;
import static net.runelite.api.AnimationID.WOODCUTTING_MITHRIL; import static net.runelite.api.AnimationID.WOODCUTTING_MITHRIL;
import static net.runelite.api.AnimationID.WOODCUTTING_RUNE; import static net.runelite.api.AnimationID.WOODCUTTING_RUNE;
import static net.runelite.api.AnimationID.WOODCUTTING_STEEL; import static net.runelite.api.AnimationID.WOODCUTTING_STEEL;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Provides;
import java.time.Duration;
import java.time.Instant;
import javax.inject.Inject;
import net.runelite.api.Actor;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.api.GameState; import net.runelite.api.GameState;
import net.runelite.api.Player; import net.runelite.api.Player;
@@ -108,7 +108,6 @@ import net.runelite.client.Notifier;
import net.runelite.client.config.ConfigManager; import net.runelite.client.config.ConfigManager;
import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.ui.ClientUI;
@PluginDescriptor( @PluginDescriptor(
name = "Idle notifier" name = "Idle notifier"
@@ -121,9 +120,6 @@ public class IdleNotifierPlugin extends Plugin
@Inject @Inject
private Notifier notifier; private Notifier notifier;
@Inject
private ClientUI gui;
@Inject @Inject
private Client client; private Client client;
@@ -285,32 +281,32 @@ public class IdleNotifierPlugin extends Plugin
if (checkIdleLogout()) if (checkIdleLogout())
{ {
sendNotification("[" + local.getName() + "] is about to log out from idling too long!"); notifier.notify("[" + local.getName() + "] is about to log out from idling too long!");
} }
if (check6hrLogout()) if (check6hrLogout())
{ {
sendNotification("[" + local.getName() + "] is about to log out from being online for 6 hours!"); notifier.notify("[" + local.getName() + "] is about to log out from being online for 6 hours!");
} }
if (checkAnimationIdle(waitDuration, local)) if (checkAnimationIdle(waitDuration, local))
{ {
sendNotification("[" + local.getName() + "] is now idle!"); notifier.notify("[" + local.getName() + "] is now idle!");
} }
if (checkOutOfCombat(waitDuration, local)) if (checkOutOfCombat(waitDuration, local))
{ {
sendNotification("[" + local.getName() + "] is now out of combat!"); notifier.notify("[" + local.getName() + "] is now out of combat!");
} }
if (checkLowHitpoints(waitDuration)) if (checkLowHitpoints(waitDuration))
{ {
sendNotification("[" + local.getName() + "] has low hitpoints!"); notifier.notify("[" + local.getName() + "] has low hitpoints!");
} }
if (checkLowPrayer(waitDuration)) if (checkLowPrayer(waitDuration))
{ {
sendNotification("[" + local.getName() + "] has low prayer!"); notifier.notify("[" + local.getName() + "] has low prayer!");
} }
} }
@@ -454,22 +450,6 @@ public class IdleNotifierPlugin extends Plugin
return false; return false;
} }
private void sendNotification(String message)
{
if (!config.alertWhenFocused() && gui.isFocused())
{
return;
}
if (config.requestFocus())
{
gui.requestFocus();
}
if (config.sendTrayNotification())
{
notifier.notify(message);
}
}
private void resetTimers() private void resetTimers()
{ {
// Reset animation idle timer // Reset animation idle timer

View File

@@ -37,23 +37,12 @@ import net.runelite.client.config.ConfigItem;
public interface NightmareZoneConfig extends Config public interface NightmareZoneConfig extends Config
{ {
@ConfigItem( @ConfigItem(
keyName = "tray", keyName = "moveoverlay",
name = "Send Tray Notification", name = "Override NMZ overlay",
description = "Toggles tray notifications", description = "Overrides the overlay so it doesn't conflict with other RuneLite plugins",
position = 1 position = 1
) )
default boolean sendTrayNotification() default boolean moveOverlay()
{
return true;
}
@ConfigItem(
keyName = "request",
name = "Request Window Focus",
description = "Toggles window focus request",
position = 2
)
default boolean requestFocus()
{ {
return true; return true;
} }
@@ -62,29 +51,18 @@ public interface NightmareZoneConfig extends Config
keyName = "overloadnotification", keyName = "overloadnotification",
name = "Overload notification", name = "Overload notification",
description = "Toggles notifications when your overload runs out", description = "Toggles notifications when your overload runs out",
position = 3 position = 2
) )
default boolean overloadNotification() default boolean overloadNotification()
{ {
return true; return true;
} }
@ConfigItem(
keyName = "moveoverlay",
name = "Override NMZ overlay",
description = "Overrides the overlay so it doesn't conflict with other RuneLite plugins",
position = 4
)
default boolean moveOverlay()
{
return true;
}
@ConfigItem( @ConfigItem(
keyName = "absorptionnotification", keyName = "absorptionnotification",
name = "Absorption notification", name = "Absorption notification",
description = "Toggles notifications when your absorption points gets below your threshold", description = "Toggles notifications when your absorption points gets below your threshold",
position = 5 position = 3
) )
default boolean absorptionNotification() default boolean absorptionNotification()
{ {
@@ -95,7 +73,7 @@ public interface NightmareZoneConfig extends Config
keyName = "absorptionthreshold", keyName = "absorptionthreshold",
name = "Absorption Threshold", name = "Absorption Threshold",
description = "The amount of absorption points to send a notification at", description = "The amount of absorption points to send a notification at",
position = 6 position = 4
) )
default int absorptionThreshold() default int absorptionThreshold()
{ {
@@ -106,7 +84,7 @@ public interface NightmareZoneConfig extends Config
keyName = "absorptioncoloroverthreshold", keyName = "absorptioncoloroverthreshold",
name = "Color above threshold", name = "Color above threshold",
description = "Configures the color for the absorption widget when above the threshold", description = "Configures the color for the absorption widget when above the threshold",
position = 7 position = 5
) )
default Color absorptionColorAboveThreshold() default Color absorptionColorAboveThreshold()
{ {
@@ -117,7 +95,7 @@ public interface NightmareZoneConfig extends Config
keyName = "absorptioncolorbelowthreshold", keyName = "absorptioncolorbelowthreshold",
name = "Color below threshold", name = "Color below threshold",
description = "Configures the color for the absorption widget when below the threshold", description = "Configures the color for the absorption widget when below the threshold",
position = 8 position = 6
) )
default Color absorptionColorBelowThreshold() default Color absorptionColorBelowThreshold()
{ {

View File

@@ -38,7 +38,6 @@ import net.runelite.client.Notifier;
import net.runelite.client.config.ConfigManager; import net.runelite.client.config.ConfigManager;
import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.ui.ClientUI;
import net.runelite.client.ui.overlay.Overlay; import net.runelite.client.ui.overlay.Overlay;
@PluginDescriptor( @PluginDescriptor(
@@ -51,9 +50,6 @@ public class NightmareZonePlugin extends Plugin
@Inject @Inject
private Notifier notifier; private Notifier notifier;
@Inject
private ClientUI gui;
@Inject @Inject
private Client client; private Client client;
@@ -117,7 +113,7 @@ public class NightmareZonePlugin extends Plugin
String msg = event.getMessage().replaceAll("<[^>]*>", " "); //remove color and linebreaks String msg = event.getMessage().replaceAll("<[^>]*>", " "); //remove color and linebreaks
if (msg.contains("The effects of overload have worn off, and you feel normal again.")) if (msg.contains("The effects of overload have worn off, and you feel normal again."))
{ {
sendNotification("Your overload has worn off"); notifier.notify("Your overload has worn off");
} }
} }
@@ -129,7 +125,7 @@ public class NightmareZonePlugin extends Plugin
{ {
if (absorptionPoints < config.absorptionThreshold()) if (absorptionPoints < config.absorptionThreshold())
{ {
sendNotification("Absorption points below: " + config.absorptionThreshold()); notifier.notify("Absorption points below: " + config.absorptionThreshold());
absorptionNotificationSend = true; absorptionNotificationSend = true;
} }
} }
@@ -146,16 +142,4 @@ public class NightmareZonePlugin extends Plugin
{ {
return Arrays.equals(client.getMapRegions(), NMZ_MAP_REGION); return Arrays.equals(client.getMapRegions(), NMZ_MAP_REGION);
} }
private void sendNotification(String message)
{
if (!gui.isFocused() && config.requestFocus())
{
gui.requestFocus();
}
if (config.sendTrayNotification())
{
notifier.notify(message);
}
}
} }

View File

@@ -43,7 +43,6 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import joptsimple.OptionSet; import joptsimple.OptionSet;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.client.Notifier;
import net.runelite.client.RuneLite; import net.runelite.client.RuneLite;
import net.runelite.client.RuneLiteModule; import net.runelite.client.RuneLiteModule;
import net.runelite.client.ui.ClientUI; import net.runelite.client.ui.ClientUI;
@@ -74,9 +73,6 @@ public class PluginManagerTest
@Mock @Mock
Client client; Client client;
@Mock
Notifier notifier;
@Before @Before
public void before() throws IOException public void before() throws IOException
{ {
@@ -88,7 +84,6 @@ public class PluginManagerTest
runelite = injector.getInstance(RuneLite.class); runelite = injector.getInstance(RuneLite.class);
runelite.setGui(clientUi); runelite.setGui(clientUi);
runelite.setNotifier(notifier);
// Find plugins we expect to have // Find plugins we expect to have
pluginClasses = new HashSet<>(); pluginClasses = new HashSet<>();