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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "nsWindowTaskbarConcealer.h"
#include "nsIWinTaskbar.h"
#define NS_TASKBAR_CONTRACTID "@mozilla.org/windows-taskbar;1"
#include "mozilla/Logging.h"
/**
* TaskbarConcealerImpl
*
* Implement Windows-fullscreen marking.
*
* nsWindow::TaskbarConcealer implements logic determining _whether_ to tell
* Windows that a given window is fullscreen. TaskbarConcealerImpl performs the
* platform-specific work of actually communicating that fact to Windows.
*
* (This object is not persistent; it's constructed on the stack when needed.)
*/
struct TaskbarConcealerImpl {
void MarkAsHidingTaskbar(HWND aWnd, bool aMark);
private:
nsCOMPtr<nsIWinTaskbar> mTaskbarInfo;
};
/**
* nsWindow::TaskbarConcealer
*
* Issue taskbar-hide requests to the OS as needed.
*/
/*
Per MSDN [0], one should mark and unmark fullscreen windows via the
ITaskbarList2::MarkFullscreenWindow method. Unfortunately, Windows pays less
attention to this than one might prefer -- in particular, it typically fails
to show the taskbar when switching focus from a window marked as fullscreen to
one not thus marked. [1]
Experimentation has (so far) suggested that its behavior is reasonable when
switching between multiple monitors, or between a set of windows which are all
from different processes [2]. This leaves us to handle the same-monitor, same-
process case.
Rather than do anything subtle here, we take the blanket approach of simply
listening for every potentially-relevant state change, and then explicitly
marking or unmarking every potentially-visible toplevel window.
----
[0] Relevant link:
https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-itaskbarlist2-markfullscreenwindow
The "NonRudeHWND" property described therein doesn't help with anything
in this comment, unfortunately. (See its use in MarkAsHidingTaskbar for
more details.)
[1] This is an oversimplification; Windows' actual behavior here is...
complicated. See bug 1732517 comment 6 for some examples.
[2] A comment in Chromium asserts that this is actually different threads. For
us, of course, that makes no difference.
https://github.com/chromium/chromium/blob/2b822268bd3/ui/views/win/hwnd_message_handler.cc#L1342
*/
/**************************************************************
*
* SECTION: TaskbarConcealer utilities
*
**************************************************************/
static mozilla::LazyLogModule sTaskbarConcealerLog("TaskbarConcealer");
// Map of all relevant Gecko windows, along with the monitor on which each
// window was last known to be located.
/* static */
nsTHashMap<HWND, HMONITOR> nsWindow::TaskbarConcealer::sKnownWindows;
// Preference for changes associated with bug 1732517. When false, revert to the
// previous simple behavior of "Firefox fullscreen == Windows fullscreen".
//
// For simplicity-of-implementation's sake, changes to this pref require a
// restart of Firefox to take effect.
static bool UseAlternateFullscreenHeuristics() {
static const bool val =
StaticPrefs::widget_windows_alternate_fullscreen_heuristics();
return val;
}
// Returns Nothing if the window in question is irrelevant (for any reason),
// or Some(the window's current state) otherwise.
/* static */
Maybe<nsWindow::TaskbarConcealer::WindowState>
nsWindow::TaskbarConcealer::GetWindowState(HWND aWnd) {
// Classical Win32 visibility conditions.
if (!::IsWindowVisible(aWnd)) {
return Nothing();
}
if (::IsIconic(aWnd)) {
return Nothing();
}
// Non-nsWindow windows associated with this thread may include file dialogs
// and IME input popups.
nsWindow* pWin = WinUtils::GetNSWindowPtr(aWnd);
if (!pWin) {
return Nothing();
}
// nsWindows of other window-classes include tooltips and drop-shadow-bearing
// menus.
if (pWin->mWindowType != eWindowType_toplevel) {
return Nothing();
}
// Cloaked windows are (presumably) on a different virtual desktop.
// https://devblogs.microsoft.com/oldnewthing/20200302-00/?p=103507
if (pWin->mIsCloaked) {
return Nothing();
}
return Some(
WindowState{::MonitorFromWindow(aWnd, MONITOR_DEFAULTTONULL),
pWin->mFrameState->GetSizeMode() == nsSizeMode_Fullscreen});
}
/**************************************************************
*
* SECTION: TaskbarConcealer::UpdateAllState
*
**************************************************************/
// Update all Windows-fullscreen-marking state and internal caches to represent
// the current state of the system.
/* static */
void nsWindow::TaskbarConcealer::UpdateAllState(
HWND destroyedHwnd /* = nullptr */
) {
// sKnownWindows is otherwise-unprotected shared state
MOZ_ASSERT(NS_IsMainThread(),
"TaskbarConcealer can only be used from the main thread!");
if (MOZ_LOG_TEST(sTaskbarConcealerLog, LogLevel::Info)) {
static size_t sLogCounter = 0;
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Info,
("Calling UpdateAllState() for the %zuth time", sLogCounter++));
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Info, ("Last known state:"));
if (sKnownWindows.IsEmpty()) {
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Info,
(" none (no windows known)"));
} else {
for (const auto& entry : sKnownWindows) {
MOZ_LOG(
sTaskbarConcealerLog, LogLevel::Info,
(" window %p was on monitor %p", entry.GetKey(), entry.GetData()));
}
}
}
// Array of all our potentially-relevant HWNDs, in Z-order (topmost first),
// along with their associated relevant state.
struct Item {
HWND hwnd;
HMONITOR monitor;
bool isGkFullscreen;
};
const nsTArray<Item> windows = [&] {
nsTArray<Item> windows;
// USE OF UNDOCUMENTED BEHAVIOR: The EnumWindows family of functions
// enumerates windows in Z-order, topmost first. (This has been true since
// at least Windows 2000, and possibly since Windows 3.0.)
//
// It's necessarily unreliable if windows are reordered while being
// enumerated; but in that case we'll get a message informing us of that
// fact, and can redo our state-calculations then.
//
// There exists no documented interface to acquire this information (other
// than ::GetWindow(), which is racy).
mozilla::EnumerateThreadWindows([&](HWND hwnd) {
// Depending on details of window-destruction that probably shouldn't be
// relied on, this HWND may or may not still be in the window list.
// Pretend it's not.
if (hwnd == destroyedHwnd) {
return;
}
const auto maybeState = GetWindowState(hwnd);
if (!maybeState) {
return;
}
const WindowState& state = *maybeState;
windows.AppendElement(Item{.hwnd = hwnd,
.monitor = state.monitor,
.isGkFullscreen = state.isGkFullscreen});
});
return windows;
}();
// Relevant monitors are exactly those with relevant windows.
const nsTHashSet<HMONITOR> relevantMonitors = [&]() {
nsTHashSet<HMONITOR> relevantMonitors;
for (const Item& item : windows) {
relevantMonitors.Insert(item.monitor);
}
return relevantMonitors;
}();
// Update the cached mapping from windows to monitors. (This is only used as
// an optimization in TaskbarConcealer::OnWindowPosChanged().)
sKnownWindows.Clear();
for (const Item& item : windows) {
MOZ_LOG(
sTaskbarConcealerLog, LogLevel::Debug,
("Found relevant window %p on monitor %p", item.hwnd, item.monitor));
sKnownWindows.InsertOrUpdate(item.hwnd, item.monitor);
}
// Auxiliary function. Does what it says on the tin.
const auto FindUppermostWindowOn = [&windows](HMONITOR aMonitor) -> HWND {
for (const Item& item : windows) {
if (item.monitor == aMonitor) {
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Info,
("on monitor %p, uppermost relevant HWND is %p", aMonitor,
item.hwnd));
return item.hwnd;
}
}
// This should never happen, since we're drawing our monitor-set from the
// set of relevant windows.
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Warning,
("on monitor %p, no relevant windows were found", aMonitor));
return nullptr;
};
TaskbarConcealerImpl impl;
// Mark all relevant windows as not hiding the taskbar, unless they're both
// Gecko-fullscreen and the uppermost relevant window on their monitor.
for (HMONITOR monitor : relevantMonitors) {
const HWND topmost = FindUppermostWindowOn(monitor);
for (const Item& item : windows) {
if (item.monitor != monitor) continue;
impl.MarkAsHidingTaskbar(item.hwnd,
item.isGkFullscreen && item.hwnd == topmost);
}
}
} // nsWindow::TaskbarConcealer::UpdateAllState()
// Mark this window as requesting to occlude the taskbar. (The caller is
// responsible for keeping any local state up-to-date.)
void TaskbarConcealerImpl::MarkAsHidingTaskbar(HWND aWnd, bool aMark) {
// USE OF UNDOCUMENTED BEHAVIOR:
//
// `MarkFullscreenWindow` is documented not to be sufficient. It will indeed
// cause a window to be treated as fullscreen; but, in its absence, Windows
// will also use explicitly undocumented heuristics to determine whether or
// not to treat a given window as full-screen.
//
// In Windows 8.1 and later, these heuristics don't seem to apply to us.
// However, in Windows 7, they do -- they determine that our fullscreen
// windows are, indeed, fullscreen. (That this is technically correct is of
// little importance, given that Windows then goes on to do the wrong thing
// with that knowledge.)
//
// Fortunately, `MarkFullscreenWindow` does have a converse: the `NonRudeHWND`
// window property. A window with this property set will not be treated as
// fullscreen.
//
// ===
//
// DIFFERENCE FROM DOCUMENTED BEHAVIOR:
//
// The documentation, as it was at the time of writing, is archived at:
// https://web.archive.org/web/20211223073250/https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-itaskbarlist2-markfullscreenwindow
//
// The most relevant paragraph follows:
//
// > **Since Windows 7**, call `SetProp(hwnd, L”NonRudeHWND”,
// > reinterpret_cast<HANDLE>(TRUE))` before showing a window to indicate to
// > the Shell that the window should not be treated as full-screen.
//
// The key words in that paragraph are "before showing a window". On Windows 7
// this has no particular effect, but it completely changes the behavior on
// Windows 8.1 and Windows 10 -- if `NonRudeHWND` is set on a window before it
// is shown, that window will not be treated as fullscreen **even if the
// property is later removed!**
//
// `NonRudeHWND` isn't actually documented to do anything at all if it's set
// after the window has already been shown. That it seems to do exactly what
// we need on Windows 7 -- prevent a window from being detected as fullscreen
// while it's set, and only then -- is a stroke of fortune.
static const bool kUseWin7MarkingHack = [&] {
switch (StaticPrefs::widget_windows_fullscreen_marking_workaround()) {
case -1:
return false;
case 1:
return true;
default:
// The behavior on Windows 8 is not known. Hopefully there are no
// side effects there.
return !mozilla::IsWin8Point1OrLater();
}
}();
if (kUseWin7MarkingHack) {
constexpr static LPCWSTR kPropName = L"NonRudeHWND";
if (aMark) {
::RemovePropW(aWnd, kPropName);
} else {
::SetPropW(aWnd, kPropName, reinterpret_cast<HANDLE>(TRUE));
}
}
const char* const sMark = aMark ? "true" : "false";
if (!mTaskbarInfo) {
mTaskbarInfo = do_GetService(NS_TASKBAR_CONTRACTID);
if (!mTaskbarInfo) {
MOZ_LOG(
sTaskbarConcealerLog, LogLevel::Warning,
("could not acquire IWinTaskbar (aWnd %p, aMark %s)", aWnd, sMark));
return;
}
}
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Info,
("Calling PrepareFullScreenHWND(%p, %s)", aWnd, sMark));
const nsresult hr = mTaskbarInfo->PrepareFullScreenHWND(aWnd, aMark);
if (FAILED(hr)) {
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Error,
("Call to PrepareFullScreenHWND(%p, %s) failed with nsresult %x",
aWnd, sMark, hr));
}
};
/**************************************************************
*
* SECTION: TaskbarConcealer event callbacks
*
**************************************************************/
void nsWindow::TaskbarConcealer::OnWindowDestroyed(HWND aWnd) {
if (!UseAlternateFullscreenHeuristics()) {
return;
}
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Info,
("==> OnWindowDestroyed() for HWND %p", aWnd));
UpdateAllState(aWnd);
}
void nsWindow::TaskbarConcealer::OnFocusAcquired(nsWindow* aWin) {
if (!UseAlternateFullscreenHeuristics()) {
return;
}
// Update state unconditionally.
//
// This is partially because focus-acquisition only updates the z-order, which
// we don't cache and therefore can't notice changes to -- but also because
// it's probably a good idea to give the user a natural way to refresh the
// current fullscreen-marking state if it's somehow gone bad.
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Info,
("==> OnFocusAcquired() for HWND %p on HMONITOR %p", aWin->mWnd,
::MonitorFromWindow(aWin->mWnd, MONITOR_DEFAULTTONULL)));
UpdateAllState();
}
void nsWindow::TaskbarConcealer::OnFullscreenChanged(nsWindow* aWin,
bool enteredFullscreen) {
if (!UseAlternateFullscreenHeuristics()) {
TaskbarConcealerImpl().MarkAsHidingTaskbar(aWin->mWnd, enteredFullscreen);
return;
}
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Info,
("==> OnFullscreenChanged() for HWND %p on HMONITOR %p", aWin->mWnd,
::MonitorFromWindow(aWin->mWnd, MONITOR_DEFAULTTONULL)));
UpdateAllState();
}
void nsWindow::TaskbarConcealer::OnWindowPosChanged(nsWindow* aWin) {
if (!UseAlternateFullscreenHeuristics()) {
return;
}
// Optimization: don't bother updating the state if the window hasn't moved
// (including appearances and disappearances).
const HWND myHwnd = aWin->mWnd;
const HMONITOR oldMonitor = sKnownWindows.Get(myHwnd); // or nullptr
const HMONITOR newMonitor = GetWindowState(myHwnd)
.map([](auto state) { return state.monitor; })
.valueOr(nullptr);
if (oldMonitor == newMonitor) {
return;
}
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Info,
("==> OnWindowPosChanged() for HWND %p (HMONITOR %p -> %p)", myHwnd,
oldMonitor, newMonitor));
UpdateAllState();
}
void nsWindow::TaskbarConcealer::OnCloakChanged() {
if (!UseAlternateFullscreenHeuristics()) {
return;
}
MOZ_LOG(sTaskbarConcealerLog, LogLevel::Info, ("==> OnCloakChanged()"));
UpdateAllState();
}
|