1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/views/win/scoped_fullscreen_visibility.h"
6
7#include "base/logging.h"
8
9namespace views {
10
11// static
12std::map<HWND, int>* ScopedFullscreenVisibility::full_screen_windows_ = NULL;
13
14ScopedFullscreenVisibility::ScopedFullscreenVisibility(HWND hwnd)
15    : hwnd_(hwnd) {
16  if (!full_screen_windows_)
17    full_screen_windows_ = new FullscreenHWNDs;
18  FullscreenHWNDs::iterator it = full_screen_windows_->find(hwnd_);
19  if (it != full_screen_windows_->end()) {
20    it->second++;
21  } else {
22    full_screen_windows_->insert(std::make_pair(hwnd_, 1));
23    // NOTE: Be careful not to activate any windows here (for example, calling
24    // ShowWindow(SW_HIDE) will automatically activate another window).  This
25    // code can be called while a window is being deactivated, and activating
26    // another window will screw up the activation that is already in progress.
27    SetWindowPos(hwnd_, NULL, 0, 0, 0, 0,
28                 SWP_HIDEWINDOW | SWP_NOACTIVATE | SWP_NOMOVE |
29                 SWP_NOREPOSITION | SWP_NOSIZE | SWP_NOZORDER);
30  }
31}
32
33ScopedFullscreenVisibility::~ScopedFullscreenVisibility() {
34  FullscreenHWNDs::iterator it = full_screen_windows_->find(hwnd_);
35  DCHECK(it != full_screen_windows_->end());
36  if (--it->second == 0) {
37    full_screen_windows_->erase(it);
38    ShowWindow(hwnd_, SW_SHOW);
39  }
40  if (full_screen_windows_->empty()) {
41    delete full_screen_windows_;
42    full_screen_windows_ = NULL;
43  }
44}
45
46// static
47bool ScopedFullscreenVisibility::IsHiddenForFullscreen(HWND hwnd) {
48  if (!full_screen_windows_)
49    return false;
50  return full_screen_windows_->find(hwnd) != full_screen_windows_->end();
51}
52
53}  // namespace views
54