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
|
#include <LibDraw/Font.h>
#include <LibDraw/GraphicsBitmap.h>
#include <LibDraw/StylePainter.h>
#include <WindowServer/WSEvent.h>
#include <WindowServer/WSScreen.h>
#include <WindowServer/WSWindowManager.h>
#include <WindowServer/WSWindowSwitcher.h>
static WSWindowSwitcher* s_the;
WSWindowSwitcher& WSWindowSwitcher::the()
{
ASSERT(s_the);
return *s_the;
}
WSWindowSwitcher::WSWindowSwitcher()
{
s_the = this;
}
WSWindowSwitcher::~WSWindowSwitcher()
{
}
void WSWindowSwitcher::set_visible(bool visible)
{
if (m_visible == visible)
return;
m_visible = visible;
WSWindowManager::the().recompute_occlusions();
if (m_switcher_window)
m_switcher_window->set_visible(visible);
if (!m_visible)
return;
refresh();
}
WSWindow* WSWindowSwitcher::selected_window()
{
if (m_selected_index < 0 || m_selected_index >= m_windows.size())
return nullptr;
return m_windows[m_selected_index].ptr();
}
void WSWindowSwitcher::on_key_event(const WSKeyEvent& event)
{
if (event.type() == WSEvent::KeyUp) {
if (event.key() == Key_Logo) {
if (auto* window = selected_window())
WSWindowManager::the().move_to_front_and_make_active(*window);
WSWindowManager::the().set_highlight_window(nullptr);
hide();
}
return;
}
if (event.key() != Key_Tab) {
WSWindowManager::the().set_highlight_window(nullptr);
hide();
return;
}
ASSERT(!m_windows.is_empty());
m_selected_index = (m_selected_index + 1) % m_windows.size();
ASSERT(m_selected_index < m_windows.size());
auto* highlight_window = m_windows.at(m_selected_index).ptr();
ASSERT(highlight_window);
WSWindowManager::the().set_highlight_window(highlight_window);
draw();
WSWindowManager::the().invalidate(m_rect);
}
void WSWindowSwitcher::draw()
{
auto& palette = WSWindowManager::the().palette();
Painter painter(*m_switcher_window->backing_store());
painter.fill_rect({ {}, m_rect.size() }, palette.window());
painter.draw_rect({ {}, m_rect.size() }, palette.threed_shadow2());
for (int index = 0; index < m_windows.size(); ++index) {
auto& window = *m_windows.at(index);
Rect item_rect {
padding(),
padding() + index * item_height(),
m_rect.width() - padding() * 2,
item_height()
};
Color text_color;
Color rect_text_color;
if (index == m_selected_index) {
painter.fill_rect(item_rect, palette.selection());
text_color = palette.selection_text();
rect_text_color = palette.threed_shadow1();
} else {
text_color = palette.window_text();
rect_text_color = palette.threed_shadow2();
}
item_rect.shrink(item_padding(), 0);
Rect thumbnail_rect = { item_rect.location().translated(0, 5), { thumbnail_width(), thumbnail_height() } };
if (window.backing_store()) {
painter.draw_scaled_bitmap(thumbnail_rect, *window.backing_store(), window.backing_store()->rect());
StylePainter::paint_frame(painter, thumbnail_rect.inflated(4, 4), palette, FrameShape::Container, FrameShadow::Sunken, 2);
}
Rect icon_rect = { thumbnail_rect.bottom_right().translated(-window.icon().width(), -window.icon().height()), { window.icon().width(), window.icon().height() } };
painter.fill_rect(icon_rect, palette.window());
painter.blit(icon_rect.location(), window.icon(), window.icon().rect());
painter.draw_text(item_rect.translated(thumbnail_width() + 12, 0), window.title(), WSWindowManager::the().window_title_font(), TextAlignment::CenterLeft, text_color);
painter.draw_text(item_rect, window.rect().to_string(), TextAlignment::CenterRight, rect_text_color);
}
}
void WSWindowSwitcher::refresh()
{
auto& wm = WSWindowManager::the();
WSWindow* selected_window = nullptr;
if (m_selected_index > 0 && m_windows[m_selected_index])
selected_window = m_windows[m_selected_index].ptr();
if (!selected_window)
selected_window = wm.highlight_window() ? wm.highlight_window() : wm.active_window();
m_windows.clear();
m_selected_index = 0;
int window_count = 0;
int longest_title_width = 0;
wm.for_each_visible_window_of_type_from_front_to_back(WSWindowType::Normal, [&](WSWindow& window) {
++window_count;
longest_title_width = max(longest_title_width, wm.font().width(window.title()));
if (selected_window == &window)
m_selected_index = m_windows.size();
m_windows.append(window.make_weak_ptr());
return IterationDecision::Continue;
},
true);
if (m_windows.is_empty()) {
hide();
return;
}
int space_for_window_rect = 180;
m_rect.set_width(thumbnail_width() + longest_title_width + space_for_window_rect + padding() * 2 + item_padding() * 2);
m_rect.set_height(window_count * item_height() + padding() * 2);
m_rect.center_within(WSScreen::the().rect());
if (!m_switcher_window)
m_switcher_window = WSWindow::construct(*this, WSWindowType::WindowSwitcher);
m_switcher_window->set_rect(m_rect);
draw();
}
void WSWindowSwitcher::refresh_if_needed()
{
if (m_visible) {
refresh();
WSWindowManager::the().invalidate(m_rect);
}
}
|