You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystrayManager.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.systray;
  18. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  19. import com.dmdirc.addons.ui_swing.MainFrame;
  20. import com.dmdirc.addons.ui_swing.components.IconManager;
  21. import com.dmdirc.addons.ui_swing.events.ClientMinimisedEvent;
  22. import com.dmdirc.config.GlobalConfig;
  23. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  24. import com.dmdirc.config.prefs.PreferencesCategory;
  25. import com.dmdirc.config.prefs.PreferencesDialogModel;
  26. import com.dmdirc.config.prefs.PreferencesSetting;
  27. import com.dmdirc.config.prefs.PreferencesType;
  28. import com.dmdirc.events.ClientPrefsOpenedEvent;
  29. import com.dmdirc.events.eventbus.EventBus;
  30. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  31. import com.dmdirc.plugins.PluginDomain;
  32. import com.dmdirc.plugins.PluginInfo;
  33. import com.dmdirc.ui.messages.StyledMessageUtils;
  34. import java.awt.AWTException;
  35. import java.awt.Frame;
  36. import java.awt.MenuItem;
  37. import java.awt.PopupMenu;
  38. import java.awt.SystemTray;
  39. import java.awt.TrayIcon;
  40. import java.awt.event.ActionEvent;
  41. import java.awt.event.ActionListener;
  42. import java.awt.event.MouseEvent;
  43. import java.awt.event.MouseListener;
  44. import javax.inject.Inject;
  45. import net.engio.mbassy.listener.Handler;
  46. public class SystrayManager implements ActionListener, MouseListener {
  47. private final PluginInfo pluginInfo;
  48. /** Main frame instance. */
  49. private final MainFrame mainFrame;
  50. /** This plugin's settings domain. */
  51. private final String domain;
  52. /** The config to read settings from. */
  53. private final AggregateConfigProvider globalConfig;
  54. /** Icon manager to get images from. */
  55. private final IconManager iconManager;
  56. private final StyledMessageUtils styleUtils;
  57. /** The event bus to listen to events on. */
  58. private final EventBus eventBus;
  59. /** The tray icon we're currently using. */
  60. private TrayIcon icon;
  61. @Inject
  62. public SystrayManager(
  63. @GlobalConfig final AggregateConfigProvider globalConfig,
  64. @PluginDomain(SystrayPlugin.class) final String domain,
  65. @PluginDomain(SystrayPlugin.class) final PluginInfo pluginInfo,
  66. final MainFrame mainFrame,
  67. final IconManager iconManager,
  68. final StyledMessageUtils styleUtils,
  69. final EventBus eventBus) {
  70. this.globalConfig = globalConfig;
  71. this.domain = domain;
  72. this.pluginInfo = pluginInfo;
  73. this.mainFrame = mainFrame;
  74. this.iconManager = iconManager;
  75. this.styleUtils = styleUtils;
  76. this.eventBus = eventBus;
  77. }
  78. public void load() {
  79. final MenuItem show = new MenuItem("Show/hide");
  80. final MenuItem quit = new MenuItem("Quit");
  81. final PopupMenu menu = new PopupMenu();
  82. menu.add(show);
  83. menu.add(quit);
  84. show.addActionListener(this);
  85. quit.addActionListener(this);
  86. icon = new TrayIcon(iconManager.getImage("logo"), "DMDirc", menu);
  87. icon.setImageAutoSize(true);
  88. icon.addMouseListener(this);
  89. try {
  90. SystemTray.getSystemTray().add(icon);
  91. eventBus.subscribe(this);
  92. } catch (AWTException ex) {
  93. throw new IllegalStateException("Unable to load plugin", ex);
  94. }
  95. }
  96. public void unload() {
  97. SystemTray.getSystemTray().remove(icon);
  98. eventBus.unsubscribe(this);
  99. icon = null;
  100. }
  101. /**
  102. * Sends a notification via the system tray icon.
  103. *
  104. * @param title The title of the notification
  105. * @param message The contents of the notification
  106. * @param type The type of notification
  107. */
  108. public void notify(final String title, final String message, final TrayIcon.MessageType type) {
  109. icon.displayMessage(title, styleUtils.stripControlCodes(message), type);
  110. }
  111. /**
  112. * Sends a notification via the system tray icon.
  113. *
  114. * @param title The title of the notification
  115. * @param message The contents of the notification
  116. */
  117. public void notify(final String title, final String message) {
  118. notify(title, message, TrayIcon.MessageType.NONE);
  119. }
  120. @Override
  121. public void actionPerformed(final ActionEvent e) {
  122. switch (e.getActionCommand()) {
  123. case "Show/hide":
  124. mainFrame.setVisible(!mainFrame.isVisible());
  125. break;
  126. case "Quit":
  127. mainFrame.quit();
  128. break;
  129. }
  130. }
  131. @Override
  132. public void mouseClicked(final MouseEvent e) {
  133. if (e.getButton() == MouseEvent.BUTTON1) {
  134. if (mainFrame.isVisible()) {
  135. mainFrame.setVisible(false);
  136. } else {
  137. mainFrame.setVisible(true);
  138. mainFrame.setState(Frame.NORMAL);
  139. mainFrame.toFront();
  140. }
  141. }
  142. }
  143. @Handler(invocation = EdtHandlerInvocation.class)
  144. public void handleClientMinimised(final ClientMinimisedEvent event) {
  145. if (globalConfig.getOptionBool(domain, "autominimise")) {
  146. mainFrame.setVisible(false);
  147. }
  148. }
  149. @Handler
  150. public void showConfig(final ClientPrefsOpenedEvent event) {
  151. final PreferencesDialogModel manager = event.getModel();
  152. final PreferencesCategory category = new PluginPreferencesCategory(
  153. pluginInfo, "System Tray",
  154. "General configuration settings");
  155. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  156. pluginInfo.getDomain(), "autominimise", "Auto-hide DMDirc when minimised",
  157. "If this option is enabled, the systray plugin will hide DMDirc"
  158. + " to the system tray whenever DMDirc is minimised",
  159. manager.getConfigManager(), manager.getIdentity()));
  160. manager.getCategory("Plugins").addSubCategory(category);
  161. }
  162. @Override
  163. public void mousePressed(final MouseEvent e) {
  164. //Ignore
  165. }
  166. @Override
  167. public void mouseReleased(final MouseEvent e) {
  168. //Ignore
  169. }
  170. @Override
  171. public void mouseEntered(final MouseEvent e) {
  172. //Ignore
  173. }
  174. @Override
  175. public void mouseExited(final MouseEvent e) {
  176. //Ignore
  177. }
  178. }