WindowStack.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "WindowStack.h"
  7. namespace WindowServer {
  8. WindowStack::WindowStack()
  9. {
  10. }
  11. WindowStack::~WindowStack()
  12. {
  13. }
  14. void WindowStack::add(Window& window)
  15. {
  16. VERIFY(window.outer_stack() == nullptr);
  17. m_windows.append(window);
  18. window.set_outer_stack({}, this);
  19. }
  20. void WindowStack::remove(Window& window)
  21. {
  22. VERIFY(window.outer_stack() == this);
  23. m_windows.remove(window);
  24. window.set_outer_stack({}, nullptr);
  25. }
  26. void WindowStack::move_to_front(Window& window)
  27. {
  28. if (m_windows.last() != &window)
  29. window.invalidate();
  30. m_windows.remove(window);
  31. m_windows.append(window);
  32. }
  33. Window* WindowStack::window_at(Gfx::IntPoint const& position) const
  34. {
  35. auto result = hit_test(position);
  36. if (!result.has_value())
  37. return nullptr;
  38. return result->window;
  39. }
  40. void WindowStack::set_highlight_window(Window* window)
  41. {
  42. if (!window)
  43. m_highlight_window = nullptr;
  44. else
  45. m_highlight_window = window->make_weak_ptr<Window>();
  46. }
  47. void WindowStack::set_active_window(Window* window)
  48. {
  49. if (!window)
  50. m_active_window = nullptr;
  51. else
  52. m_active_window = window->make_weak_ptr<Window>();
  53. }
  54. Optional<HitTestResult> WindowStack::hit_test(Gfx::IntPoint const& position) const
  55. {
  56. Optional<HitTestResult> result;
  57. const_cast<WindowStack*>(this)->for_each_visible_window_from_front_to_back([&](Window& window) {
  58. result = window.hit_test(position);
  59. if (result.has_value())
  60. return IterationDecision::Break;
  61. return IterationDecision::Continue;
  62. });
  63. return result;
  64. }
  65. }