WindowSwitcher.cpp 9.7 KB

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