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.

WindowFlashingManager.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2006-2014 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.DMDircMBassador;
  25. import com.dmdirc.addons.ui_swing.MainFrame;
  26. import com.dmdirc.config.ConfigBinder;
  27. import com.dmdirc.config.ConfigBinding;
  28. import com.dmdirc.events.ClientFocusGainedEvent;
  29. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  30. import javax.inject.Inject;
  31. import javax.inject.Singleton;
  32. import com.sun.jna.Native;
  33. import com.sun.jna.NativeLibrary;
  34. import com.sun.jna.Pointer;
  35. import com.sun.jna.platform.win32.User32;
  36. import com.sun.jna.platform.win32.WinDef;
  37. import com.sun.jna.platform.win32.WinUser;
  38. import net.engio.mbassy.listener.Handler;
  39. @Singleton
  40. public class WindowFlashingManager {
  41. /** Swing main frame. */
  42. private final MainFrame mainFrame;
  43. /** Event bus. */
  44. private final DMDircMBassador eventBus;
  45. /** Config binder. */
  46. private final ConfigBinder binder;
  47. /** Cached blink rate setting. */
  48. @ConfigBinding(domain = "plugin-windowflashing", key = "blinkrate",
  49. fallbacks = {"plugin-windowflashing", "blinkratefallback"})
  50. private int blinkrate;
  51. /** Cached count setting. */
  52. @ConfigBinding(domain = "plugin-windowflashing", key = "flashcount",
  53. fallbacks = {"plugin-windowflashing", "flashcountfallback"})
  54. private int flashcount;
  55. /** Cached flash taskbar setting. */
  56. @ConfigBinding(domain = "plugin-windowflashing", key = "flashtaskbar")
  57. private boolean flashtaskbar;
  58. /** Cached flash caption setting. */
  59. @ConfigBinding(domain = "plugin-windowflashing", key = "flashcaption")
  60. private boolean flashcaption;
  61. /** Library instance. */
  62. private User32 user32;
  63. @Inject
  64. public WindowFlashingManager(
  65. @GlobalConfig final AggregateConfigProvider config,
  66. final MainFrame mainFrame,
  67. final DMDircMBassador eventBus) {
  68. this.mainFrame = mainFrame;
  69. this.eventBus = eventBus;
  70. binder = config.getBinder();
  71. }
  72. public void onLoad() {
  73. user32 = (User32) Native.loadLibrary("user32", User32.class);
  74. binder.bind(this, WindowFlashing.class);
  75. eventBus.subscribe(this);
  76. }
  77. public void onUnload() {
  78. eventBus.unsubscribe(this);
  79. binder.unbind(this);
  80. user32 = null;
  81. NativeLibrary.getInstance("user32").dispose();
  82. }
  83. /**
  84. * Flashes an inactive window under windows.
  85. */
  86. public void flashWindow() {
  87. if (!mainFrame.isFocused()) {
  88. user32.FlashWindowEx(setupFlashObject());
  89. }
  90. }
  91. /**
  92. * Creates a new flash info object that starts flashing with the configured settings.
  93. */
  94. private WinUser.FLASHWINFO setupFlashObject() {
  95. final WinUser.FLASHWINFO flashInfo = new WinUser.FLASHWINFO();
  96. flashInfo.dwFlags = getFlags();
  97. flashInfo.dwTimeout = blinkrate;
  98. flashInfo.uCount = flashcount;
  99. flashInfo.hWnd = getHWND();
  100. flashInfo.cbSize = flashInfo.size();
  101. return flashInfo;
  102. }
  103. /**
  104. * Creates a new flash object that stops the flashing.
  105. */
  106. private WinUser.FLASHWINFO stopFlashObject() {
  107. final WinUser.FLASHWINFO flashInfo = new WinUser.FLASHWINFO();
  108. flashInfo.dwFlags = WinUser.FLASHW_STOP;
  109. flashInfo.dwTimeout = 0;
  110. flashInfo.uCount = 0;
  111. flashInfo.hWnd = getHWND();
  112. flashInfo.cbSize = flashInfo.size();
  113. return flashInfo;
  114. }
  115. /**
  116. * Returns the native handle object for the main frame.
  117. *
  118. * @return
  119. */
  120. private WinDef.HWND getHWND() {
  121. final WinDef.HWND hwnd = new WinDef.HWND();
  122. final Pointer pointer = Native.getWindowPointer(mainFrame);
  123. hwnd.setPointer(pointer);
  124. return hwnd;
  125. }
  126. /**
  127. * Calculates the flags for the flash object based on config settings.
  128. *
  129. * @return Flash info flags
  130. */
  131. private int getFlags() {
  132. int returnValue = 0;
  133. if (flashtaskbar) {
  134. returnValue |= WinUser.FLASHW_TRAY;
  135. }
  136. if (flashcaption) {
  137. returnValue |= WinUser.FLASHW_CAPTION;
  138. }
  139. if (flashcount >= 0) {
  140. returnValue |= WinUser.FLASHW_TIMER;
  141. } else {
  142. returnValue |= WinUser.FLASHW_TIMERNOFG;
  143. }
  144. return returnValue;
  145. }
  146. @Handler
  147. public void handleFocusGained(final ClientFocusGainedEvent event) {
  148. if (mainFrame != null) {
  149. user32.FlashWindowEx(stopFlashObject());
  150. }
  151. }
  152. }