summaryrefslogtreecommitdiff
path: root/Userland/Services/WindowServer/WindowStack.cpp
blob: 2ed107528d09548eacc7b52f0424ad3a72cd5fa2 (plain)
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
/*
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "WindowStack.h"
#include "WindowManager.h"

namespace WindowServer {

WindowStack::WindowStack(unsigned row, unsigned column)
    : m_row(row)
    , m_column(column)
{
}

WindowStack::~WindowStack()
{
}

void WindowStack::add(Window& window)
{
    VERIFY(!window.is_on_any_window_stack({}));
    m_windows.append(window);
    window.set_window_stack({}, this);

    move_pinned_windows_to_front();
}

void WindowStack::add_to_back(Window& window)
{
    VERIFY(!window.is_on_any_window_stack({}));
    m_windows.prepend(window);
    window.set_window_stack({}, this);
}

void WindowStack::remove(Window& window)
{
    VERIFY(&window.window_stack() == this);
    m_windows.remove(window);
    window.set_window_stack({}, nullptr);
    if (m_active_window == &window)
        m_active_window = nullptr;
    if (m_active_input_window == &window)
        m_active_input_window = nullptr;
    if (m_active_input_tracking_window == &window)
        m_active_input_tracking_window = nullptr;
}

void WindowStack::move_to_front(Window& window)
{
    if (m_windows.last() != &window)
        window.invalidate();

    m_windows.remove(window);
    m_windows.append(window);

    move_pinned_windows_to_front();

    if (window.is_pinned()) {
        m_windows.remove(window);
        m_windows.append(window);
        window.invalidate();
    }
}

void WindowStack::move_pinned_windows_to_front()
{
    Window::List pinned_list;
    for (auto iterator = m_windows.begin(); iterator != m_windows.end(); ++iterator) {
        auto& window = *iterator;
        if (window.is_pinned()) {
            m_windows.remove(window);
            pinned_list.append(window);
            iterator = m_windows.begin();
        }
    }

    while (!pinned_list.is_empty()) {
        auto& window = *pinned_list.begin();
        pinned_list.remove(window);
        m_windows.append(window);
        window.invalidate();
    }
}

void WindowStack::move_all_windows(WindowStack& new_window_stack, Vector<Window*, 32>& windows_moved, MoveAllWindowsTo move_to)
{
    VERIFY(this != &new_window_stack);

    move_pinned_windows_to_front();

    if (move_to == MoveAllWindowsTo::Front) {
        while (auto* window = m_windows.take_first()) {
            window->set_window_stack({}, nullptr);
            new_window_stack.add(*window);
            windows_moved.append(window);
        }
    } else {
        while (auto* window = m_windows.take_last()) {
            window->set_window_stack({}, nullptr);
            new_window_stack.add_to_back(*window);
            windows_moved.append(window);
        }
    }
    m_active_window = nullptr;
    m_active_input_window = nullptr;
    m_active_input_tracking_window = nullptr;
}

Window* WindowStack::window_at(Gfx::IntPoint const& position, IncludeWindowFrame include_window_frame) const
{
    auto result = hit_test(position);
    if (!result.has_value())
        return nullptr;
    if (include_window_frame == IncludeWindowFrame::No && result->is_frame_hit)
        return nullptr;
    return result->window;
}

Window* WindowStack::highlight_window() const
{
    if (auto* window = WindowManager::the().highlight_window(); window && &window->window_stack() == this)
        return window;
    return nullptr;
}

void WindowStack::set_active_window(Window* window)
{
    if (!window)
        m_active_window = nullptr;
    else
        m_active_window = window->make_weak_ptr<Window>();
}

void WindowStack::set_all_occluded(bool occluded)
{
    for (auto& window : m_windows) {
        if (!WindowManager::is_stationary_window_type(window.type()))
            window.set_occluded(occluded);
    }
}

Optional<HitTestResult> WindowStack::hit_test(Gfx::IntPoint const& position) const
{
    Optional<HitTestResult> result;
    WindowManager::the().for_each_visible_window_from_front_to_back([&](Window& window) {
        result = window.hit_test(position);
        if (result.has_value())
            return IterationDecision::Break;
        return IterationDecision::Continue;
    },
        const_cast<WindowStack*>(this));
    return result;
}

}