Polish thumbar code.

* Fix a memory leak in thumbar initialization.
* Check the number of thumbar buttons, should be <= 7.
* Correct to check thumbar button click event.
This commit is contained in:
Haojian Wu 2015-08-05 13:47:59 +08:00
parent dfd076a3e5
commit 78eac4116c
6 changed files with 64 additions and 38 deletions

View 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

View 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;
}
}

View 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) {

View 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;