taskbar_host.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/ui/win/taskbar_host.h"
  5. #include <objbase.h>
  6. #include <string>
  7. #include "atom/browser/native_window.h"
  8. #include "base/stl_util.h"
  9. #include "base/strings/utf_string_conversions.h"
  10. #include "base/win/scoped_gdi_object.h"
  11. #include "third_party/skia/include/core/SkBitmap.h"
  12. #include "ui/display/win/screen_win.h"
  13. #include "ui/gfx/icon_util.h"
  14. namespace atom {
  15. namespace {
  16. // From MSDN:
  17. // https://msdn.microsoft.com/en-us/library/windows/desktop/dd378460(v=vs.85).aspx#thumbbars
  18. // The thumbnail toolbar has a maximum of seven buttons due to the limited room.
  19. const size_t kMaxButtonsCount = 7;
  20. // The base id of Thumbar button.
  21. const int kButtonIdBase = 40001;
  22. bool GetThumbarButtonFlags(const std::vector<std::string>& flags,
  23. THUMBBUTTONFLAGS* out) {
  24. THUMBBUTTONFLAGS result = THBF_ENABLED; // THBF_ENABLED == 0
  25. for (const auto& flag : flags) {
  26. if (flag == "disabled")
  27. result |= THBF_DISABLED;
  28. else if (flag == "dismissonclick")
  29. result |= THBF_DISMISSONCLICK;
  30. else if (flag == "nobackground")
  31. result |= THBF_NOBACKGROUND;
  32. else if (flag == "hidden")
  33. result |= THBF_HIDDEN;
  34. else if (flag == "noninteractive")
  35. result |= THBF_NONINTERACTIVE;
  36. else
  37. return false;
  38. }
  39. *out = result;
  40. return true;
  41. }
  42. } // namespace
  43. TaskbarHost::TaskbarHost() {}
  44. TaskbarHost::~TaskbarHost() {}
  45. bool TaskbarHost::SetThumbarButtons(HWND window,
  46. const std::vector<ThumbarButton>& buttons) {
  47. if (buttons.size() > kMaxButtonsCount || !InitializeTaskbar())
  48. return false;
  49. callback_map_.clear();
  50. // The number of buttons in thumbar can not be changed once it is created,
  51. // so we have to claim kMaxButtonsCount buttons initialy in case users add
  52. // more buttons later.
  53. base::win::ScopedHICON icons[kMaxButtonsCount] = {};
  54. THUMBBUTTON thumb_buttons[kMaxButtonsCount] = {};
  55. for (size_t i = 0; i < kMaxButtonsCount; ++i) {
  56. THUMBBUTTON& thumb_button = thumb_buttons[i];
  57. // Set ID.
  58. thumb_button.iId = kButtonIdBase + i;
  59. thumb_button.dwMask = THB_FLAGS;
  60. if (i >= buttons.size()) {
  61. // This button is used to occupy the place in toolbar, and it does not
  62. // show.
  63. thumb_button.dwFlags = THBF_HIDDEN;
  64. continue;
  65. }
  66. // This button is user's button.
  67. const ThumbarButton& button = buttons[i];
  68. // Generate flags.
  69. thumb_button.dwFlags = THBF_ENABLED;
  70. if (!GetThumbarButtonFlags(button.flags, &thumb_button.dwFlags))
  71. return false;
  72. // Set icon.
  73. if (!button.icon.IsEmpty()) {
  74. thumb_button.dwMask |= THB_ICON;
  75. icons[i] = IconUtil::CreateHICONFromSkBitmap(button.icon.AsBitmap());
  76. thumb_button.hIcon = icons[i].get();
  77. }
  78. // Set tooltip.
  79. if (!button.tooltip.empty()) {
  80. thumb_button.dwMask |= THB_TOOLTIP;
  81. wcsncpy_s(thumb_button.szTip, base::UTF8ToUTF16(button.tooltip).c_str(),
  82. _TRUNCATE);
  83. }
  84. // Save callback.
  85. callback_map_[thumb_button.iId] = button.clicked_callback;
  86. }
  87. // Finally add them to taskbar.
  88. HRESULT r;
  89. if (thumbar_buttons_added_)
  90. r = taskbar_->ThumbBarUpdateButtons(window, kMaxButtonsCount,
  91. thumb_buttons);
  92. else
  93. r = taskbar_->ThumbBarAddButtons(window, kMaxButtonsCount, thumb_buttons);
  94. thumbar_buttons_added_ = true;
  95. last_buttons_ = buttons;
  96. return SUCCEEDED(r);
  97. }
  98. void TaskbarHost::RestoreThumbarButtons(HWND window) {
  99. if (thumbar_buttons_added_) {
  100. thumbar_buttons_added_ = false;
  101. SetThumbarButtons(window, last_buttons_);
  102. }
  103. }
  104. bool TaskbarHost::SetProgressBar(HWND window,
  105. double value,
  106. const NativeWindow::ProgressState state) {
  107. if (!InitializeTaskbar())
  108. return false;
  109. bool success;
  110. if (value > 1.0 || state == NativeWindow::PROGRESS_INDETERMINATE) {
  111. success = SUCCEEDED(taskbar_->SetProgressState(window, TBPF_INDETERMINATE));
  112. } else if (value < 0 || state == NativeWindow::PROGRESS_NONE) {
  113. success = SUCCEEDED(taskbar_->SetProgressState(window, TBPF_NOPROGRESS));
  114. } else {
  115. // Unless SetProgressState set a blocking state (TBPF_ERROR, TBPF_PAUSED)
  116. // for the window, a call to SetProgressValue assumes the TBPF_NORMAL
  117. // state even if it is not explicitly set.
  118. // SetProgressValue overrides and clears the TBPF_INDETERMINATE state.
  119. if (state == NativeWindow::PROGRESS_ERROR) {
  120. success = SUCCEEDED(taskbar_->SetProgressState(window, TBPF_ERROR));
  121. } else if (state == NativeWindow::PROGRESS_PAUSED) {
  122. success = SUCCEEDED(taskbar_->SetProgressState(window, TBPF_PAUSED));
  123. } else {
  124. success = SUCCEEDED(taskbar_->SetProgressState(window, TBPF_NORMAL));
  125. }
  126. if (success) {
  127. int val = static_cast<int>(value * 100);
  128. success = SUCCEEDED(taskbar_->SetProgressValue(window, val, 100));
  129. }
  130. }
  131. return success;
  132. }
  133. bool TaskbarHost::SetOverlayIcon(HWND window,
  134. const gfx::Image& overlay,
  135. const std::string& text) {
  136. if (!InitializeTaskbar())
  137. return false;
  138. base::win::ScopedHICON icon(
  139. IconUtil::CreateHICONFromSkBitmap(overlay.AsBitmap()));
  140. return SUCCEEDED(taskbar_->SetOverlayIcon(window, icon.get(),
  141. base::UTF8ToUTF16(text).c_str()));
  142. }
  143. bool TaskbarHost::SetThumbnailClip(HWND window, const gfx::Rect& region) {
  144. if (!InitializeTaskbar())
  145. return false;
  146. if (region.IsEmpty()) {
  147. return SUCCEEDED(taskbar_->SetThumbnailClip(window, NULL));
  148. } else {
  149. RECT rect =
  150. display::win::ScreenWin::DIPToScreenRect(window, region).ToRECT();
  151. return SUCCEEDED(taskbar_->SetThumbnailClip(window, &rect));
  152. }
  153. }
  154. bool TaskbarHost::SetThumbnailToolTip(HWND window, const std::string& tooltip) {
  155. if (!InitializeTaskbar())
  156. return false;
  157. return SUCCEEDED(taskbar_->SetThumbnailTooltip(
  158. window, base::UTF8ToUTF16(tooltip).c_str()));
  159. }
  160. bool TaskbarHost::HandleThumbarButtonEvent(int button_id) {
  161. if (ContainsKey(callback_map_, button_id)) {
  162. auto callback = callback_map_[button_id];
  163. if (!callback.is_null())
  164. callback.Run();
  165. return true;
  166. }
  167. return false;
  168. }
  169. bool TaskbarHost::InitializeTaskbar() {
  170. if (taskbar_)
  171. return true;
  172. if (FAILED(::CoCreateInstance(CLSID_TaskbarList, nullptr,
  173. CLSCTX_INPROC_SERVER,
  174. IID_PPV_ARGS(&taskbar_))) ||
  175. FAILED(taskbar_->HrInit())) {
  176. taskbar_.Reset();
  177. return false;
  178. } else {
  179. return true;
  180. }
  181. }
  182. } // namespace atom