1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sts=2 sw=2 et cin: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <strsafe.h>
#include "SystemStatusBar.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Element.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/LinkedList.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/widget/IconLoader.h"
#include "mozilla/widget/NativeMenu.h"
#include "mozilla/dom/XULButtonElement.h"
#include "nsComputedDOMStyle.h"
#include "nsIContentPolicy.h"
#include "nsISupports.h"
#include "nsMenuPopupFrame.h"
#include "nsXULPopupManager.h"
#include "nsIDocShell.h"
#include "nsDocShell.h"
#include "nsWindowGfx.h"
#include "shellapi.h"
namespace mozilla::widget {
using mozilla::LinkedListElement;
using mozilla::dom::Element;
class StatusBarEntry final : public LinkedListElement<RefPtr<StatusBarEntry>>,
public IconLoader::Listener,
public nsISupports {
public:
explicit StatusBarEntry(Element* aMenu);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(StatusBarEntry)
nsresult Init();
void Destroy();
MOZ_CAN_RUN_SCRIPT LRESULT OnMessage(HWND hWnd, UINT msg, WPARAM wp,
LPARAM lp);
const Element* GetMenu() { return mMenu; };
nsresult OnComplete(imgIContainer* aImage) override;
private:
~StatusBarEntry();
RefPtr<mozilla::widget::IconLoader> mIconLoader;
// Effectively const but is cycle collected
MOZ_KNOWN_LIVE RefPtr<Element> mMenu;
NOTIFYICONDATAW mIconData;
boolean mInitted;
};
NS_IMPL_CYCLE_COLLECTION_CLASS(StatusBarEntry)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(StatusBarEntry)
tmp->Destroy();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(StatusBarEntry)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIconLoader)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMenu)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StatusBarEntry)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(StatusBarEntry)
NS_IMPL_CYCLE_COLLECTING_RELEASE(StatusBarEntry)
StatusBarEntry::StatusBarEntry(Element* aMenu) : mMenu(aMenu), mInitted(false) {
mIconData = {/* cbSize */ sizeof(NOTIFYICONDATA),
/* hWnd */ 0,
/* uID */ 2,
/* uFlags */ NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP,
/* uCallbackMessage */ WM_USER,
/* hIcon */ 0,
/* szTip */ L"", // This is updated in Init()
/* dwState */ 0,
/* dwStateMask */ 0,
/* szInfo */ L"",
/* uVersion */ {NOTIFYICON_VERSION_4},
/* szInfoTitle */ L"",
/* dwInfoFlags */ 0};
MOZ_ASSERT(mMenu);
}
StatusBarEntry::~StatusBarEntry() {
if (!mInitted) {
return;
}
Destroy();
::Shell_NotifyIconW(NIM_DELETE, &mIconData);
VERIFY(::DestroyWindow(mIconData.hWnd));
}
void StatusBarEntry::Destroy() {
if (mIconLoader) {
mIconLoader->Destroy();
mIconLoader = nullptr;
}
}
nsresult StatusBarEntry::Init() {
MOZ_ASSERT(NS_IsMainThread());
mIconLoader = new IconLoader(this);
if (auto icon = NativeMenu::GetIcon(*mMenu)) {
mIconLoader->LoadIcon(icon.mURI, mMenu);
}
HWND iconWindow;
NS_ENSURE_TRUE(iconWindow = ::CreateWindowExW(
/* extended style */ 0,
/* className */ L"IconWindowClass",
/* title */ 0,
/* style */ WS_CAPTION,
/* x, y, cx, cy */ 0, 0, 0, 0,
/* parent */ 0,
/* menu */ 0,
/* instance */ 0,
/* create struct */ 0),
NS_ERROR_FAILURE);
::SetWindowLongPtr(iconWindow, GWLP_USERDATA, (LONG_PTR)this);
mIconData.hWnd = iconWindow;
mIconData.hIcon = ::LoadIcon(::GetModuleHandle(NULL), IDI_APPLICATION);
nsAutoString labelAttr;
mMenu->GetAttr(nsGkAtoms::label, labelAttr);
const nsString& label = PromiseFlatString(labelAttr);
size_t destLength = sizeof mIconData.szTip / (sizeof mIconData.szTip[0]);
wchar_t* tooltip = &(mIconData.szTip[0]);
::StringCchCopyNW(tooltip, destLength, label.get(), label.Length());
::Shell_NotifyIconW(NIM_ADD, &mIconData);
::Shell_NotifyIconW(NIM_SETVERSION, &mIconData);
mInitted = true;
return NS_OK;
}
nsresult StatusBarEntry::OnComplete(imgIContainer* aImage) {
NS_ENSURE_ARG_POINTER(aImage);
RefPtr<StatusBarEntry> kungFuDeathGrip = this;
nsresult rv = nsWindowGfx::CreateIcon(
aImage, nullptr, false, LayoutDeviceIntPoint(),
nsWindowGfx::GetIconMetrics(nsWindowGfx::kRegularIcon), &mIconData.hIcon);
NS_ENSURE_SUCCESS(rv, rv);
::Shell_NotifyIconW(NIM_MODIFY, &mIconData);
if (mIconData.hIcon) {
::DestroyIcon(mIconData.hIcon);
mIconData.hIcon = nullptr;
}
// To simplify things, we won't react to CSS changes to update the icon
// with this implementation. We can get rid of the IconLoader at this point.
mIconLoader->Destroy();
mIconLoader = nullptr;
return NS_OK;
}
LRESULT StatusBarEntry::OnMessage(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
if (msg == WM_USER &&
(LOWORD(lp) == NIN_SELECT || LOWORD(lp) == NIN_KEYSELECT ||
LOWORD(lp) == WM_CONTEXTMENU)) {
auto* menu = dom::XULButtonElement::FromNode(mMenu);
if (!menu) {
return TRUE;
}
nsMenuPopupFrame* popupFrame = menu->GetMenuPopup(FlushType::None);
if (NS_WARN_IF(!popupFrame)) {
return TRUE;
}
nsIWidget* widget = popupFrame->GetNearestWidget();
if (NS_WARN_IF(!widget)) {
return TRUE;
}
HWND win = static_cast<HWND>(widget->GetNativeData(NS_NATIVE_WINDOW));
MOZ_DIAGNOSTIC_ASSERT(win);
if (!win) {
return TRUE;
}
if (LOWORD(lp) == NIN_KEYSELECT && ::GetForegroundWindow() == win) {
// When enter is pressed on the icon, the shell sends two NIN_KEYSELECT
// notifications. This might cause us to open two windows. To work around
// this, if we're already the foreground window (which happens below),
// ignore this notification.
return TRUE;
}
if (LOWORD(lp) != WM_CONTEXTMENU &&
mMenu->HasAttr(nsGkAtoms::contextmenu)) {
::SetForegroundWindow(win);
nsEventStatus status = nsEventStatus_eIgnore;
WidgetMouseEvent event(true, eXULSystemStatusBarClick, nullptr,
WidgetMouseEvent::eReal);
RefPtr<nsPresContext> presContext = popupFrame->PresContext();
EventDispatcher::Dispatch(mMenu, presContext, &event, nullptr, &status);
return DefWindowProc(hWnd, msg, wp, lp);
}
nsPresContext* pc = popupFrame->PresContext();
const CSSIntPoint point = gfx::RoundedToInt(
LayoutDeviceIntPoint(GET_X_LPARAM(wp), GET_Y_LPARAM(wp)) /
pc->CSSToDevPixelScale());
// The menu that is being opened is a Gecko <xul:menu>, and the popup code
// that manages it expects that the window that the <xul:menu> belongs to
// will be in the foreground when it opens. If we don't do this, then if the
// icon is clicked when the window is _not_ in the foreground, then the
// opened menu will not be keyboard focusable, nor will it close on its own
// if the user clicks away from the menu (at least, not until the user
// focuses any window in the parent process).
::SetForegroundWindow(win);
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
pm->ShowPopupAtScreen(popupFrame->GetContent()->AsElement(), point.x,
point.y, false, nullptr);
}
return DefWindowProc(hWnd, msg, wp, lp);
}
NS_IMPL_ISUPPORTS(SystemStatusBar, nsISystemStatusBar)
MOZ_CAN_RUN_SCRIPT static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg,
WPARAM wp, LPARAM lp) {
if (RefPtr<StatusBarEntry> entry =
(StatusBarEntry*)GetWindowLongPtr(hWnd, GWLP_USERDATA)) {
return entry->OnMessage(hWnd, msg, wp, lp);
}
return TRUE;
}
static StaticRefPtr<SystemStatusBar> sSingleton;
SystemStatusBar& SystemStatusBar::GetSingleton() {
if (!sSingleton) {
sSingleton = new SystemStatusBar();
ClearOnShutdown(&sSingleton);
}
return *sSingleton;
}
already_AddRefed<SystemStatusBar> SystemStatusBar::GetAddRefedSingleton() {
RefPtr<SystemStatusBar> sm = &GetSingleton();
return sm.forget();
}
nsresult SystemStatusBar::Init() {
WNDCLASS classStruct = {/* style */ 0,
/* lpfnWndProc */ &WindowProc,
/* cbClsExtra */ 0,
/* cbWndExtra */ 0,
/* hInstance */ 0,
/* hIcon */ 0,
/* hCursor */ 0,
/* hbrBackground */ 0,
/* lpszMenuName */ 0,
/* lpszClassName */ L"IconWindowClass"};
NS_ENSURE_TRUE(::RegisterClass(&classStruct), NS_ERROR_FAILURE);
return NS_OK;
}
NS_IMETHODIMP
SystemStatusBar::AddItem(Element* aElement) {
RefPtr<StatusBarEntry> entry = new StatusBarEntry(aElement);
nsresult rv = entry->Init();
NS_ENSURE_SUCCESS(rv, rv);
mStatusBarEntries.insertBack(entry);
return NS_OK;
}
NS_IMETHODIMP
SystemStatusBar::RemoveItem(Element* aElement) {
for (StatusBarEntry* entry : mStatusBarEntries) {
if (entry->GetMenu() == aElement) {
entry->removeFrom(mStatusBarEntries);
return NS_OK;
}
}
return NS_ERROR_NOT_AVAILABLE;
}
} // namespace mozilla::widget
|