Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

WindowFlashingManager.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.windowflashing;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.addons.ui_swing.MainFrame;
  25. import com.dmdirc.config.ConfigBinder;
  26. import com.dmdirc.config.ConfigBinding;
  27. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  28. import com.dmdirc.config.prefs.PreferencesCategory;
  29. import com.dmdirc.config.prefs.PreferencesDialogModel;
  30. import com.dmdirc.config.prefs.PreferencesSetting;
  31. import com.dmdirc.config.prefs.PreferencesType;
  32. import com.dmdirc.addons.ui_swing.events.ClientFocusGainedEvent;
  33. import com.dmdirc.events.ClientPrefsOpenedEvent;
  34. import com.dmdirc.interfaces.EventBus;
  35. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  36. import com.dmdirc.plugins.PluginDomain;
  37. import com.dmdirc.plugins.PluginInfo;
  38. import javax.inject.Inject;
  39. import javax.inject.Singleton;
  40. import com.sun.jna.Native;
  41. import com.sun.jna.NativeLibrary;
  42. import com.sun.jna.Pointer;
  43. import com.sun.jna.platform.win32.User32;
  44. import com.sun.jna.platform.win32.WinDef;
  45. import com.sun.jna.platform.win32.WinUser;
  46. import net.engio.mbassy.listener.Handler;
  47. @Singleton
  48. public class WindowFlashingManager {
  49. private final PluginInfo pluginInfo;
  50. /** Swing main frame. */
  51. private final MainFrame mainFrame;
  52. /** Event bus. */
  53. private final EventBus eventBus;
  54. /** Config binder. */
  55. private final ConfigBinder binder;
  56. /** Cached blink rate setting. */
  57. @ConfigBinding(domain = "plugin-windowflashing", key = "blinkrate",
  58. fallbacks = {"plugin-windowflashing", "blinkratefallback"})
  59. private int blinkrate;
  60. /** Cached count setting. */
  61. @ConfigBinding(domain = "plugin-windowflashing", key = "flashcount",
  62. fallbacks = {"plugin-windowflashing", "flashcountfallback"})
  63. private int flashcount;
  64. /** Cached flash taskbar setting. */
  65. @ConfigBinding(domain = "plugin-windowflashing", key = "flashtaskbar")
  66. private boolean flashtaskbar;
  67. /** Cached flash caption setting. */
  68. @ConfigBinding(domain = "plugin-windowflashing", key = "flashcaption")
  69. private boolean flashcaption;
  70. /** Library instance. */
  71. private User32 user32;
  72. @Inject
  73. public WindowFlashingManager(
  74. @GlobalConfig final AggregateConfigProvider config,
  75. @PluginDomain(WindowFlashing.class) final PluginInfo pluginInfo,
  76. final MainFrame mainFrame,
  77. final EventBus eventBus) {
  78. this.pluginInfo = pluginInfo;
  79. this.mainFrame = mainFrame;
  80. this.eventBus = eventBus;
  81. binder = config.getBinder();
  82. }
  83. public void onLoad() {
  84. user32 = (User32) Native.loadLibrary("user32", User32.class);
  85. binder.bind(this, WindowFlashing.class);
  86. eventBus.subscribe(this);
  87. }
  88. public void onUnload() {
  89. eventBus.unsubscribe(this);
  90. binder.unbind(this);
  91. user32 = null;
  92. NativeLibrary.getInstance("user32").dispose();
  93. }
  94. /**
  95. * Flashes an inactive window under windows.
  96. */
  97. public void flashWindow() {
  98. if (!mainFrame.isFocused()) {
  99. user32.FlashWindowEx(setupFlashObject());
  100. }
  101. }
  102. /**
  103. * Creates a new flash info object that starts flashing with the configured settings.
  104. */
  105. private WinUser.FLASHWINFO setupFlashObject() {
  106. final WinUser.FLASHWINFO flashInfo = new WinUser.FLASHWINFO();
  107. flashInfo.dwFlags = getFlags();
  108. flashInfo.dwTimeout = blinkrate;
  109. flashInfo.uCount = flashcount;
  110. flashInfo.hWnd = getHWND();
  111. flashInfo.cbSize = flashInfo.size();
  112. return flashInfo;
  113. }
  114. /**
  115. * Creates a new flash object that stops the flashing.
  116. */
  117. private WinUser.FLASHWINFO stopFlashObject() {
  118. final WinUser.FLASHWINFO flashInfo = new WinUser.FLASHWINFO();
  119. flashInfo.dwFlags = WinUser.FLASHW_STOP;
  120. flashInfo.dwTimeout = 0;
  121. flashInfo.uCount = 0;
  122. flashInfo.hWnd = getHWND();
  123. flashInfo.cbSize = flashInfo.size();
  124. return flashInfo;
  125. }
  126. /**
  127. * Returns the native handle object for the main frame.
  128. *
  129. * @return
  130. */
  131. private WinDef.HWND getHWND() {
  132. final WinDef.HWND hwnd = new WinDef.HWND();
  133. final Pointer pointer = Native.getWindowPointer(mainFrame);
  134. hwnd.setPointer(pointer);
  135. return hwnd;
  136. }
  137. /**
  138. * Calculates the flags for the flash object based on config settings.
  139. *
  140. * @return Flash info flags
  141. */
  142. private int getFlags() {
  143. int returnValue = 0;
  144. if (flashtaskbar) {
  145. returnValue |= WinUser.FLASHW_TRAY;
  146. }
  147. if (flashcaption) {
  148. returnValue |= WinUser.FLASHW_CAPTION;
  149. }
  150. if (flashcount >= 0) {
  151. returnValue |= WinUser.FLASHW_TIMER;
  152. } else {
  153. returnValue |= WinUser.FLASHW_TIMERNOFG;
  154. }
  155. return returnValue;
  156. }
  157. @Handler
  158. public void handleFocusGained(final ClientFocusGainedEvent event) {
  159. if (mainFrame != null) {
  160. user32.FlashWindowEx(stopFlashObject());
  161. }
  162. }
  163. @Handler
  164. public void showConfig(final ClientPrefsOpenedEvent event) {
  165. final PreferencesDialogModel manager = event.getModel();
  166. final PreferencesCategory category = new PluginPreferencesCategory(
  167. pluginInfo, "Window Flashing",
  168. "General configuration for window flashing plugin.");
  169. category.addSetting(new PreferencesSetting(
  170. PreferencesType.OPTIONALINTEGER, pluginInfo.getDomain(), "blinkrate",
  171. "Blink rate", "Specifies the rate at which the taskbar and or "
  172. + "caption will blink, if unspecified this will be your cursor "
  173. + "blink rate.",
  174. manager.getConfigManager(), manager.getIdentity()));
  175. category.addSetting(new PreferencesSetting(
  176. PreferencesType.OPTIONALINTEGER, pluginInfo.getDomain(), "flashcount",
  177. "Flash count", "Specifies the number of times to blink, if "
  178. + "unspecified this will blink indefinitely",
  179. manager.getConfigManager(), manager.getIdentity()));
  180. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  181. pluginInfo.getDomain(), "flashtaskbar", "Flash taskbar",
  182. "Should the taskbar entry flash?",
  183. manager.getConfigManager(), manager.getIdentity()));
  184. category.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  185. pluginInfo.getDomain(), "flashcaption", "Flash caption",
  186. "Should the window caption flash?",
  187. manager.getConfigManager(), manager.getIdentity()));
  188. manager.getCategory("Plugins").addSubCategory(category);
  189. }
  190. }