]> khamidou — GitHub mirrors - electron.git/commitdiff
Polish thumbar code.
authorHaojian Wu <hokein.wu@gmail.com>
Wed, 5 Aug 2015 05:47:59 +0000 (13:47 +0800)
committerHaojian Wu <hokein.wu@gmail.com>
Wed, 5 Aug 2015 11:38:12 +0000 (19:38 +0800)
* Fix a memory leak in thumbar initialization.
* Check the number of thumbar buttons, should be <= 7.
* Correct to check thumbar button click event.

atom/browser/native_window_views.cc
atom/browser/ui/win/atom_desktop_window_tree_host_win.cc
atom/browser/ui/win/thumbar_host.cc
atom/browser/ui/win/thumbar_host.h
docs/api/browser-window.md
filenames.gypi

index 018f294b8df13d383f5285d6c5d58dbc8e16638c..eda98dc4f853b7a5d073e7bde2e26d3727d216bc 100644 (file)
@@ -27,7 +27,6 @@
 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
 #include "ui/views/controls/webview/webview.h"
 #include "ui/views/window/client_view.h"
-#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
 #include "ui/views/widget/native_widget_private.h"
 #include "ui/views/widget/widget.h"
 #include "ui/wm/core/shadow_types.h"
@@ -52,6 +51,7 @@
 #include "ui/base/win/shell.h"
 #include "ui/gfx/icon_util.h"
 #include "ui/gfx/win/dpi.h"
+#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
 #include "ui/views/win/hwnd_util.h"
 #endif
 
index 55912612ebba50f983f942c30535cfb579e2e77b..5c5833f64c093ed7c2e865f55cb71f94ede02a09 100644 (file)
@@ -3,6 +3,9 @@
 // found in the LICENSE file.
 
 #include "atom/browser/ui/win/atom_desktop_window_tree_host_win.h"
+
+#include <shobjidl.h>
+
 #include "atom/browser/ui/win/thumbar_host.h"
 
 namespace atom {
@@ -32,8 +35,11 @@ bool AtomDesktopWindowTreeHostWin::PreHandleMSG(UINT message,
                                                 LRESULT* result) {
   switch (message) {
     case WM_COMMAND: {
+       // Handle thumbar button click message.
        int id = LOWORD(w_param);
-       if (thumbar_host_ && thumbar_host_->HandleThumbarButtonEvent(id))
+       int thbn_message = HIWORD(w_param);
+       if (thbn_message == THBN_CLICKED && thumbar_host_ &&
+           thumbar_host_->HandleThumbarButtonEvent(id))
          return true;
     }
   }
index 22e9ba8c41690ad5287f0cbcfae93bb301a6442b..2d82cd4c6cdb222af8fcc4c61e217d17419862a7 100644 (file)
@@ -18,9 +18,14 @@ namespace atom {
 
 namespace {
 
+// From MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/
+// dd378460(v=vs.85).aspx#thumbbars
 // The thumbnail toolbar has a maximum of seven buttons due to the limited room.
 const int kMaxButtonsCount = 7;
 
+// The base id of Thumbar button.
+const int kButtonIdBase = 40001;
+
 bool GetThumbarButtonFlags(const std::vector<std::string>& flags,
                            THUMBBUTTONFLAGS* out) {
   if (flags.empty()) {
@@ -60,7 +65,18 @@ ThumbarHost::~ThumbarHost() {
 
 bool ThumbarHost::SetThumbarButtons(
     const std::vector<ThumbarHost::ThumbarButton>& buttons) {
-  THUMBBUTTON thumb_buttons[kMaxButtonsCount];
+  if (buttons.size() > kMaxButtonsCount)
+    return false;
+
+  base::win::ScopedComPtr<ITaskbarList3> taskbar;
+  if (FAILED(taskbar.CreateInstance(CLSID_TaskbarList,
+                                    nullptr,
+                                    CLSCTX_INPROC_SERVER)) ||
+      FAILED(taskbar->HrInit())) {
+    return false;
+  }
+
+  THUMBBUTTON thumb_buttons[kMaxButtonsCount] = {};
   thumbar_button_clicked_callback_map_.clear();
 
   // Once a toolbar with a set of buttons is added to thumbnail, there is no way
@@ -70,7 +86,8 @@ bool ThumbarHost::SetThumbarButtons(
   //
   // Initialize all thumb buttons with HIDDEN state.
   for (int i = 0; i < kMaxButtonsCount; ++i) {
-    thumb_buttons[i].iId = i;
+    thumb_buttons[i].iId = kButtonIdBase + i;
+    thumb_buttons[i].dwMask = THB_FLAGS;  // dwFlags is valid.
     thumb_buttons[i].dwFlags = THBF_HIDDEN;
   }
 
@@ -87,25 +104,25 @@ bool ThumbarHost::SetThumbarButtons(
                base::UTF8ToUTF16(buttons[i].tooltip).c_str());
     }
 
-    thumbar_button_clicked_callback_map_[i] =
+    thumbar_button_clicked_callback_map_[thumb_buttons[i].iId] =
         buttons[i].clicked_callback;
   }
 
-  base::win::ScopedComPtr<ITaskbarList3> taskbar;
-  if (FAILED(taskbar.CreateInstance(CLSID_TaskbarList,
-                                    nullptr,
-                                    CLSCTX_INPROC_SERVER)) ||
-      FAILED(taskbar->HrInit())) {
-    return false;
-  }
+  bool is_success = false;
   if (!is_initialized_) {
     is_initialized_ = true;
-    return taskbar->ThumbBarAddButtons(
+    is_success = taskbar->ThumbBarAddButtons(
+        window_, kMaxButtonsCount, thumb_buttons) == S_OK;
+  } else {
+    is_success = taskbar->ThumbBarUpdateButtons(
         window_, kMaxButtonsCount, thumb_buttons) == S_OK;
   }
 
-  return taskbar->ThumbBarUpdateButtons(
-      window_, kMaxButtonsCount, thumb_buttons) == S_OK;
+  // Release thumb_buttons' icons, the taskbar makes its own copy.
+  for (size_t i = 0; i < buttons.size(); ++i) {
+    ::DestroyIcon(thumb_buttons[i].hIcon);
+  }
+  return is_success;
 }
 
 bool ThumbarHost::HandleThumbarButtonEvent(int button_id) {
index 2ab3b06a381d19d715b0550d71e8ea1c2a6552ec..40d0ae7400e894a7e837cfbd12c102d90efef211 100644 (file)
@@ -18,7 +18,7 @@ namespace atom {
 
 class ThumbarHost {
  public:
-  using ThumbarButtonClickedCallback = base::Callback<void(void)>;
+  using ThumbarButtonClickedCallback = base::Closure;
 
   struct ThumbarButton {
     std::string tooltip;
index 73f9c97de58dbd64085ce1df5109e43071d4329b..60b9b84735d044125fa67eee88d1ededdad5df28 100644 (file)
@@ -648,29 +648,32 @@ __Note:__ This API is only available on Windows (Windows 7 and above)
 
 * `buttons` Array of `button` objects
   * `button` Object
-     * `icon` [NativeImage](native-image.md) - The icon showing in thumbnail toolbar.
-     * `tooltip` String - (Option) - The text of the button's tooltip.
-     * `flags` Array of Strings - (Option) Control specific states and behaviors
-          of the button. By default, it uses `enabled`.
-       *  `enabled` - The button is active and available to the user.
-       *  `disabled` - The button is disabled. It is present, but has a visual state
-          that indicates that it will not respond to user action.
-       *  `dismissonclick` - When the button is clicked, the taskbar button's flyout
-          closes immediately.
-       *  `nobackground` - Do not draw a button border, use only the image.
-       *  `hidden` - The button is not shown to the user.
-       *  `noninteractive` - The button is enabled but not interactive; no pressed
-          button state is drawn. This value is intended for instances where the button
-          is used in a notification.
-     * `click` - Function
-
-Add a thumbnail toolbar with a specified set of buttons to the thumbnail image of
-a window in a taskbar button layout.
+    * `icon` [NativeImage](native-image.md) - The icon showing in thumbnail
+      toolbar.
+    * `tooltip` String - (Option) - The text of the button's tooltip.
+    * `flags` Array of Strings - (Option) Control specific states and behaviors
+      of the button. By default, it uses `enabled`.
+      *  `enabled` - The button is active and available to the user.
+      *  `disabled` - The button is disabled. It is present, but has a visual
+         state that indicates that it will not respond to user action.
+      *  `dismissonclick` - When the button is clicked, the taskbar button's
+         flyout closes immediately.
+      *  `nobackground` - Do not draw a button border, use only the image.
+      *  `hidden` - The button is not shown to the user.
+      *  `noninteractive` - The button is enabled but not interactive; no
+         pressed button state is drawn. This value is intended for instances
+         where the button is used in a notification.
+    * `click` - Function
+
+Add a thumbnail toolbar with a specified set of buttons to the thumbnail image
+of a window in a taskbar button layout. Returns a `Boolean` object indicates
+whether the thumbnail has been added successfully.
 
 __Note:__ This API is only available on Windows (Windows 7 and above).
-Once you setup the thumbnail toolbar, the toolbar cannot be removed due to the
-platform's limitation. But you can call the API with an empty array to clean the
-buttons.
+The number of buttons in thumbnail toolbar should be no greater than 7 due to
+the limited room. Once you setup the thumbnail toolbar, the toolbar cannot be
+removed due to the platform's limitation. But you can call the API with an empty
+array to clean the buttons.
 
 ### BrowserWindow.showDefinitionForSelection()
 
index 3b3d3a4cc48f8b593a25aacb81e8b622fa407331..45aaac7c27785f16c4660769d1d093bfac8afe01 100644 (file)
       'atom/browser/atom_browser_main_parts.h',
       'atom/browser/atom_browser_main_parts_linux.cc',
       'atom/browser/atom_browser_main_parts_mac.mm',
-      "atom/browser/atom_desktop_window_tree_host_win.cc",
-      "atom/browser/atom_desktop_window_tree_host_win.h",
       'atom/browser/atom_javascript_dialog_manager.cc',
       'atom/browser/atom_javascript_dialog_manager.h',
       'atom/browser/atom_quota_permission_context.cc',
       'atom/browser/ui/views/submenu_button.h',
       'atom/browser/ui/views/win_frame_view.cc',
       'atom/browser/ui/views/win_frame_view.h',
+      "atom/browser/ui/win/atom_desktop_window_tree_host_win.cc",
+      "atom/browser/ui/win/atom_desktop_window_tree_host_win.h",
       'atom/browser/ui/win/notify_icon_host.cc',
       'atom/browser/ui/win/notify_icon_host.h',
       'atom/browser/ui/win/notify_icon.cc',