scoped_overview_hide_windows.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2017 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. #include "ash/wm/overview/scoped_overview_hide_windows.h"
  5. #include "base/containers/contains.h"
  6. #include "base/notreached.h"
  7. #include "ui/aura/window.h"
  8. namespace ash {
  9. ScopedOverviewHideWindows::ScopedOverviewHideWindows(
  10. const std::vector<aura::Window*>& windows,
  11. bool force_hidden)
  12. : force_hidden_(force_hidden) {
  13. for (auto* window : windows)
  14. AddWindow(window);
  15. }
  16. ScopedOverviewHideWindows::~ScopedOverviewHideWindows() {
  17. for (const auto& element : window_visibility_) {
  18. element.first->RemoveObserver(this);
  19. if (element.second)
  20. element.first->Show();
  21. }
  22. }
  23. void ScopedOverviewHideWindows::AddWindow(aura::Window* window) {
  24. window->AddObserver(this);
  25. window_visibility_.emplace(window, window->IsVisible());
  26. window->Hide();
  27. }
  28. void ScopedOverviewHideWindows::RemoveWindow(aura::Window* window) {
  29. DCHECK(base::Contains(window_visibility_, window));
  30. window->RemoveObserver(this);
  31. if (window_visibility_[window])
  32. window->Show();
  33. window_visibility_.erase(window);
  34. }
  35. void ScopedOverviewHideWindows::OnWindowDestroying(aura::Window* window) {
  36. window_visibility_.erase(window);
  37. window->RemoveObserver(this);
  38. }
  39. void ScopedOverviewHideWindows::OnWindowVisibilityChanged(aura::Window* window,
  40. bool visible) {
  41. if (!visible)
  42. return;
  43. // It's expected that windows hidden in overview, unless they are forcefully
  44. // hidden should not be shown while in overview.
  45. if (!force_hidden_)
  46. NOTREACHED();
  47. // Do not let |window| change to visible during the lifetime of |this|. Also
  48. // update |window_visibility_| so that we can restore the window visibility
  49. // correctly.
  50. window->Hide();
  51. window_visibility_[window] = true;
  52. }
  53. } // namespace ash