Serenity Operating System
at master 286 lines 10 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibGfx/Bitmap.h> 8#include <LibGfx/Font/Font.h> 9#include <LibGfx/StylePainter.h> 10#include <WindowServer/Compositor.h> 11#include <WindowServer/Event.h> 12#include <WindowServer/Screen.h> 13#include <WindowServer/WindowManager.h> 14#include <WindowServer/WindowSwitcher.h> 15 16namespace WindowServer { 17 18static WindowSwitcher* s_the; 19 20WindowSwitcher& WindowSwitcher::the() 21{ 22 VERIFY(s_the); 23 return *s_the; 24} 25 26WindowSwitcher::WindowSwitcher() 27{ 28 s_the = this; 29} 30 31void WindowSwitcher::set_visible(bool visible) 32{ 33 if (m_visible == visible) 34 return; 35 m_visible = visible; 36 Compositor::the().invalidate_occlusions(); 37 if (m_switcher_window) 38 m_switcher_window->set_visible(visible); 39 if (!m_visible) 40 return; 41 clear_hovered_index(); 42 refresh(); 43} 44 45Window* WindowSwitcher::selected_window() 46{ 47 if (m_selected_index < 0 || m_selected_index >= static_cast<int>(m_windows.size())) 48 return nullptr; 49 return m_windows[m_selected_index].ptr(); 50} 51 52void WindowSwitcher::event(Core::Event& event) 53{ 54 if (event.type() == Event::WindowLeft) { 55 clear_hovered_index(); 56 return; 57 } 58 59 if (!static_cast<Event&>(event).is_mouse_event()) 60 return; 61 62 auto& mouse_event = static_cast<MouseEvent&>(event); 63 int new_hovered_index = -1; 64 for (size_t i = 0; i < m_windows.size(); ++i) { 65 auto item_rect = this->item_rect(i); 66 if (item_rect.contains(mouse_event.position())) { 67 new_hovered_index = i; 68 break; 69 } 70 } 71 72 if (mouse_event.type() == Event::MouseMove) { 73 if (m_hovered_index != new_hovered_index) { 74 m_hovered_index = new_hovered_index; 75 redraw(); 76 } 77 } 78 79 if (new_hovered_index == -1) 80 return; 81 82 if (mouse_event.type() == Event::MouseDown) 83 select_window_at_index(new_hovered_index); 84 85 event.accept(); 86} 87 88void WindowSwitcher::on_key_event(KeyEvent const& event) 89{ 90 if (event.type() == Event::KeyUp) { 91 if (event.key() == (m_mode == Mode::ShowAllWindows ? Key_Super : Key_Alt)) { 92 if (auto* window = selected_window()) { 93 WindowManager::the().move_to_front_and_make_active(*window); 94 } 95 WindowManager::the().set_highlight_window(nullptr); 96 hide(); 97 } 98 return; 99 } 100 101 if (event.key() == Key_LeftShift || event.key() == Key_RightShift) 102 return; 103 if (event.key() != Key_Tab) { 104 WindowManager::the().set_highlight_window(nullptr); 105 hide(); 106 return; 107 } 108 VERIFY(!m_windows.is_empty()); 109 110 int new_selected_index; 111 112 if (!event.shift()) { 113 new_selected_index = (m_selected_index + 1) % static_cast<int>(m_windows.size()); 114 } else { 115 new_selected_index = (m_selected_index - 1) % static_cast<int>(m_windows.size()); 116 if (new_selected_index < 0) 117 new_selected_index = static_cast<int>(m_windows.size()) - 1; 118 } 119 VERIFY(new_selected_index < static_cast<int>(m_windows.size())); 120 121 select_window_at_index(new_selected_index); 122} 123 124void WindowSwitcher::select_window(Window& window) 125{ 126 for (size_t i = 0; i < m_windows.size(); ++i) { 127 if (m_windows.at(i) == &window) { 128 select_window_at_index(i); 129 return; 130 } 131 } 132} 133 134void WindowSwitcher::select_window_at_index(int index) 135{ 136 m_selected_index = index; 137 auto* highlight_window = m_windows.at(index).ptr(); 138 VERIFY(highlight_window); 139 auto& wm = WindowManager::the(); 140 if (m_mode == Mode::ShowAllWindows) { 141 if (auto& window_stack = highlight_window->window_stack(); &window_stack != &wm.current_window_stack()) 142 wm.switch_to_window_stack(window_stack, nullptr, false); 143 } 144 wm.set_highlight_window(highlight_window); 145 redraw(); 146} 147 148void WindowSwitcher::redraw() 149{ 150 draw(); 151 Compositor::the().invalidate_screen(m_rect); 152} 153 154Gfx::IntRect WindowSwitcher::item_rect(int index) const 155{ 156 return { 157 padding(), 158 padding() + index * item_height(), 159 m_rect.width() - padding() * 2, 160 item_height() 161 }; 162} 163 164void WindowSwitcher::draw() 165{ 166 auto palette = WindowManager::the().palette(); 167 168 Gfx::IntRect rect = { {}, m_rect.size() }; 169 Gfx::Painter painter(*m_switcher_window->backing_store()); 170 painter.clear_rect(rect, Color::Transparent); 171 172 // FIXME: Perhaps the WindowSwitcher could render as an overlay instead. 173 // That would require adding support for event handling to overlays. 174 if (auto* shadow_bitmap = WindowManager::the().overlay_rect_shadow()) { 175 // FIXME: Support other scale factors. 176 int scale_factor = 1; 177 Gfx::StylePainter::paint_simple_rect_shadow(painter, rect, shadow_bitmap->bitmap(scale_factor), true, true); 178 } 179 180 for (size_t index = 0; index < m_windows.size(); ++index) { 181 // FIXME: Ideally we wouldn't be in draw() without having pruned destroyed windows from the list already. 182 if (m_windows.at(index) == nullptr) 183 continue; 184 auto& window = *m_windows.at(index); 185 auto item_rect = this->item_rect(index); 186 Color text_color; 187 Color rect_text_color; 188 if (static_cast<int>(index) == m_selected_index) { 189 painter.fill_rect(item_rect, palette.selection()); 190 text_color = palette.selection_text(); 191 rect_text_color = palette.selection_text().with_alpha(0xcc); 192 } else { 193 if (static_cast<int>(index) == m_hovered_index) 194 Gfx::StylePainter::paint_frame(painter, item_rect, palette, Gfx::FrameShape::Panel, Gfx::FrameShadow::Raised, 2); 195 text_color = Color::White; 196 rect_text_color = Color(Color::White).with_alpha(0xcc); 197 } 198 item_rect.shrink(item_padding(), 0); 199 Gfx::IntRect thumbnail_rect = { item_rect.location().translated(0, 5), { thumbnail_width(), thumbnail_height() } }; 200 if (window.backing_store()) 201 painter.draw_scaled_bitmap(thumbnail_rect, *window.backing_store(), window.backing_store()->rect(), 1.0f, Gfx::Painter::ScalingMode::BilinearBlend); 202 Gfx::IntRect icon_rect = { thumbnail_rect.bottom_right().translated(-window.icon().width(), -window.icon().height()), { window.icon().width(), window.icon().height() } }; 203 painter.blit(icon_rect.location(), window.icon(), window.icon().rect()); 204 painter.draw_text(item_rect.translated(thumbnail_width() + 12, 0).translated(1, 1), window.computed_title(), WindowManager::the().window_title_font(), Gfx::TextAlignment::CenterLeft, text_color.inverted()); 205 painter.draw_text(item_rect.translated(thumbnail_width() + 12, 0), window.computed_title(), WindowManager::the().window_title_font(), Gfx::TextAlignment::CenterLeft, text_color); 206 auto window_details = m_windows_on_multiple_stacks ? DeprecatedString::formatted("{} on {}:{}", window.rect().to_deprecated_string(), window.window_stack().row() + 1, window.window_stack().column() + 1) : window.rect().to_deprecated_string(); 207 painter.draw_text(item_rect, window_details, Gfx::TextAlignment::CenterRight, rect_text_color); 208 } 209} 210 211void WindowSwitcher::refresh() 212{ 213 auto& wm = WindowManager::the(); 214 Window const* selected_window = nullptr; 215 if (m_selected_index > 0 && m_windows[m_selected_index]) 216 selected_window = m_windows[m_selected_index].ptr(); 217 if (!selected_window) 218 selected_window = wm.highlight_window() ? wm.highlight_window() : wm.active_window(); 219 m_windows.clear(); 220 m_windows_on_multiple_stacks = false; 221 m_selected_index = 0; 222 int window_count = 0; 223 int longest_title_width = 0; 224 225 WindowStack* last_added_on_window_stack = nullptr; 226 auto add_window_stack_windows = [&](WindowStack& window_stack) { 227 window_stack.for_each_window_of_type_from_front_to_back( 228 WindowType::Normal, [&](Window& window) { 229 if (window.is_frameless() || window.is_modal()) 230 return IterationDecision::Continue; 231 ++window_count; 232 longest_title_width = max(longest_title_width, wm.font().width(window.computed_title())); 233 if (selected_window == &window) 234 m_selected_index = m_windows.size(); 235 m_windows.append(window); 236 auto& window_stack = window.window_stack(); 237 if (!last_added_on_window_stack) { 238 last_added_on_window_stack = &window_stack; 239 } else if (last_added_on_window_stack != &window_stack) { 240 last_added_on_window_stack = &window_stack; 241 m_windows_on_multiple_stacks = true; 242 } 243 return IterationDecision::Continue; 244 }, 245 true); 246 }; 247 if (m_mode == Mode::ShowAllWindows) { 248 wm.for_each_window_stack([&](auto& window_stack) { 249 add_window_stack_windows(window_stack); 250 return IterationDecision::Continue; 251 }); 252 } else { 253 add_window_stack_windows(wm.current_window_stack()); 254 } 255 256 if (m_windows.is_empty()) { 257 hide(); 258 return; 259 } 260 int space_for_window_details = 200; 261 m_rect.set_width(thumbnail_width() + longest_title_width + space_for_window_details + padding() * 2 + item_padding() * 2); 262 m_rect.set_height(window_count * item_height() + padding() * 2); 263 m_rect.center_within(Screen::main().rect()); 264 if (!m_switcher_window) { 265 m_switcher_window = Window::construct(*this, WindowType::WindowSwitcher); 266 m_switcher_window->set_has_alpha_channel(true); 267 } 268 m_switcher_window->set_rect(m_rect); 269 redraw(); 270} 271 272void WindowSwitcher::refresh_if_needed() 273{ 274 if (m_visible) 275 refresh(); 276} 277 278void WindowSwitcher::clear_hovered_index() 279{ 280 if (m_hovered_index == -1) 281 return; 282 m_hovered_index = -1; 283 redraw(); 284} 285 286}