WindowSwitcher.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.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. WindowSwitcher::~WindowSwitcher()
  26. {
  27. }
  28. void WindowSwitcher::set_visible(bool visible)
  29. {
  30. if (m_visible == visible)
  31. return;
  32. m_visible = visible;
  33. Compositor::the().invalidate_occlusions();
  34. if (m_switcher_window)
  35. m_switcher_window->set_visible(visible);
  36. if (!m_visible)
  37. return;
  38. refresh();
  39. }
  40. Window* WindowSwitcher::selected_window()
  41. {
  42. if (m_selected_index < 0 || m_selected_index >= static_cast<int>(m_windows.size()))
  43. return nullptr;
  44. return m_windows[m_selected_index].ptr();
  45. }
  46. void WindowSwitcher::event(Core::Event& event)
  47. {
  48. if (!static_cast<Event&>(event).is_mouse_event())
  49. return;
  50. auto& mouse_event = static_cast<MouseEvent&>(event);
  51. int new_hovered_index = -1;
  52. for (size_t i = 0; i < m_windows.size(); ++i) {
  53. auto item_rect = this->item_rect(i);
  54. if (item_rect.contains(mouse_event.position())) {
  55. new_hovered_index = i;
  56. break;
  57. }
  58. }
  59. if (mouse_event.type() == Event::MouseMove) {
  60. if (m_hovered_index != new_hovered_index) {
  61. m_hovered_index = new_hovered_index;
  62. redraw();
  63. }
  64. }
  65. if (new_hovered_index == -1)
  66. return;
  67. if (mouse_event.type() == Event::MouseDown)
  68. select_window_at_index(new_hovered_index);
  69. event.accept();
  70. }
  71. void WindowSwitcher::on_key_event(const KeyEvent& event)
  72. {
  73. if (event.type() == Event::KeyUp) {
  74. if (event.key() == Key_Super) {
  75. if (auto* window = selected_window()) {
  76. window->set_minimized(false);
  77. WindowManager::the().move_to_front_and_make_active(*window);
  78. }
  79. WindowManager::the().set_highlight_window(nullptr);
  80. hide();
  81. }
  82. return;
  83. }
  84. if (event.key() == Key_LeftShift || event.key() == Key_RightShift)
  85. return;
  86. if (event.key() != Key_Tab) {
  87. WindowManager::the().set_highlight_window(nullptr);
  88. hide();
  89. return;
  90. }
  91. VERIFY(!m_windows.is_empty());
  92. int new_selected_index;
  93. if (!event.shift()) {
  94. new_selected_index = (m_selected_index + 1) % static_cast<int>(m_windows.size());
  95. } else {
  96. new_selected_index = (m_selected_index - 1) % static_cast<int>(m_windows.size());
  97. if (new_selected_index < 0)
  98. new_selected_index = static_cast<int>(m_windows.size()) - 1;
  99. }
  100. VERIFY(new_selected_index < static_cast<int>(m_windows.size()));
  101. select_window_at_index(new_selected_index);
  102. }
  103. void WindowSwitcher::select_window(Window& window)
  104. {
  105. for (size_t i = 0; i < m_windows.size(); ++i) {
  106. if (m_windows.at(i) == &window) {
  107. select_window_at_index(i);
  108. return;
  109. }
  110. }
  111. }
  112. void WindowSwitcher::select_window_at_index(int index)
  113. {
  114. m_selected_index = index;
  115. auto* highlight_window = m_windows.at(index).ptr();
  116. VERIFY(highlight_window);
  117. WindowManager::the().set_highlight_window(highlight_window);
  118. redraw();
  119. }
  120. void WindowSwitcher::redraw()
  121. {
  122. draw();
  123. Compositor::the().invalidate_screen(m_rect);
  124. }
  125. Gfx::IntRect WindowSwitcher::item_rect(int index) const
  126. {
  127. return {
  128. padding(),
  129. padding() + index * item_height(),
  130. m_rect.width() - padding() * 2,
  131. item_height()
  132. };
  133. }
  134. void WindowSwitcher::draw()
  135. {
  136. auto palette = WindowManager::the().palette();
  137. Gfx::Painter painter(*m_switcher_window->backing_store());
  138. painter.fill_rect({ {}, m_rect.size() }, palette.window());
  139. painter.draw_rect({ {}, m_rect.size() }, palette.threed_shadow2());
  140. for (size_t index = 0; index < m_windows.size(); ++index) {
  141. auto& window = *m_windows.at(index);
  142. auto item_rect = this->item_rect(index);
  143. Color text_color;
  144. Color rect_text_color;
  145. if (static_cast<int>(index) == m_selected_index) {
  146. painter.fill_rect(item_rect, palette.selection());
  147. text_color = palette.selection_text();
  148. rect_text_color = palette.threed_shadow1();
  149. } else {
  150. if (static_cast<int>(index) == m_hovered_index)
  151. Gfx::StylePainter::paint_button(painter, item_rect, palette, Gfx::ButtonStyle::Coolbar, false, true);
  152. text_color = palette.window_text();
  153. rect_text_color = palette.threed_shadow2();
  154. }
  155. item_rect.shrink(item_padding(), 0);
  156. Gfx::IntRect thumbnail_rect = { item_rect.location().translated(0, 5), { thumbnail_width(), thumbnail_height() } };
  157. if (window.backing_store()) {
  158. painter.draw_scaled_bitmap(thumbnail_rect, *window.backing_store(), window.backing_store()->rect());
  159. Gfx::StylePainter::paint_frame(painter, thumbnail_rect.inflated(4, 4), palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
  160. }
  161. Gfx::IntRect icon_rect = { thumbnail_rect.bottom_right().translated(-window.icon().width(), -window.icon().height()), { window.icon().width(), window.icon().height() } };
  162. painter.fill_rect(icon_rect, palette.window());
  163. painter.blit(icon_rect.location(), window.icon(), window.icon().rect());
  164. painter.draw_text(item_rect.translated(thumbnail_width() + 12, 0), window.computed_title(), WindowManager::the().window_title_font(), Gfx::TextAlignment::CenterLeft, text_color);
  165. painter.draw_text(item_rect, window.rect().to_string(), Gfx::TextAlignment::CenterRight, rect_text_color);
  166. }
  167. }
  168. void WindowSwitcher::refresh()
  169. {
  170. auto& wm = WindowManager::the();
  171. const Window* selected_window = nullptr;
  172. if (m_selected_index > 0 && m_windows[m_selected_index])
  173. selected_window = m_windows[m_selected_index].ptr();
  174. if (!selected_window)
  175. selected_window = wm.highlight_window() ? wm.highlight_window() : wm.active_window();
  176. m_windows.clear();
  177. m_selected_index = 0;
  178. int window_count = 0;
  179. int longest_title_width = 0;
  180. wm.window_stack().for_each_window_of_type_from_front_to_back(
  181. WindowType::Normal, [&](Window& window) {
  182. if (window.is_frameless())
  183. return IterationDecision::Continue;
  184. ++window_count;
  185. longest_title_width = max(longest_title_width, wm.font().width(window.computed_title()));
  186. if (selected_window == &window)
  187. m_selected_index = m_windows.size();
  188. m_windows.append(window);
  189. return IterationDecision::Continue;
  190. },
  191. true);
  192. if (m_windows.is_empty()) {
  193. hide();
  194. return;
  195. }
  196. int space_for_window_rect = 180;
  197. m_rect.set_width(thumbnail_width() + longest_title_width + space_for_window_rect + padding() * 2 + item_padding() * 2);
  198. m_rect.set_height(window_count * item_height() + padding() * 2);
  199. m_rect.center_within(Screen::the().rect());
  200. if (!m_switcher_window)
  201. m_switcher_window = Window::construct(*this, WindowType::WindowSwitcher);
  202. m_switcher_window->set_rect(m_rect);
  203. redraw();
  204. }
  205. void WindowSwitcher::refresh_if_needed()
  206. {
  207. if (m_visible)
  208. refresh();
  209. }
  210. }