WSWindowSwitcher.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <LibDraw/Font.h>
  2. #include <LibDraw/GraphicsBitmap.h>
  3. #include <LibDraw/StylePainter.h>
  4. #include <WindowServer/WSEvent.h>
  5. #include <WindowServer/WSScreen.h>
  6. #include <WindowServer/WSWindowManager.h>
  7. #include <WindowServer/WSWindowSwitcher.h>
  8. static WSWindowSwitcher* s_the;
  9. WSWindowSwitcher& WSWindowSwitcher::the()
  10. {
  11. ASSERT(s_the);
  12. return *s_the;
  13. }
  14. WSWindowSwitcher::WSWindowSwitcher()
  15. {
  16. s_the = this;
  17. }
  18. WSWindowSwitcher::~WSWindowSwitcher()
  19. {
  20. }
  21. void WSWindowSwitcher::set_visible(bool visible)
  22. {
  23. if (m_visible == visible)
  24. return;
  25. m_visible = visible;
  26. WSWindowManager::the().recompute_occlusions();
  27. if (m_switcher_window)
  28. m_switcher_window->set_visible(visible);
  29. if (!m_visible)
  30. return;
  31. refresh();
  32. }
  33. WSWindow* WSWindowSwitcher::selected_window()
  34. {
  35. if (m_selected_index < 0 || m_selected_index >= m_windows.size())
  36. return nullptr;
  37. return m_windows[m_selected_index].ptr();
  38. }
  39. void WSWindowSwitcher::on_key_event(const WSKeyEvent& event)
  40. {
  41. if (event.type() == WSEvent::KeyUp) {
  42. if (event.key() == Key_Logo) {
  43. if (auto* window = selected_window()) {
  44. window->set_minimized(false);
  45. WSWindowManager::the().move_to_front_and_make_active(*window);
  46. }
  47. WSWindowManager::the().set_highlight_window(nullptr);
  48. hide();
  49. }
  50. return;
  51. }
  52. if (event.key() == Key_LeftShift || event.key() == Key_RightShift)
  53. return;
  54. if (event.key() != Key_Tab) {
  55. WSWindowManager::the().set_highlight_window(nullptr);
  56. hide();
  57. return;
  58. }
  59. ASSERT(!m_windows.is_empty());
  60. if (!event.shift()) {
  61. m_selected_index = (m_selected_index + 1) % m_windows.size();
  62. } else {
  63. m_selected_index = (m_selected_index - 1) % m_windows.size();
  64. if (m_selected_index < 0)
  65. m_selected_index = m_windows.size() - 1;
  66. }
  67. ASSERT(m_selected_index < m_windows.size());
  68. auto* highlight_window = m_windows.at(m_selected_index).ptr();
  69. ASSERT(highlight_window);
  70. WSWindowManager::the().set_highlight_window(highlight_window);
  71. draw();
  72. WSWindowManager::the().invalidate(m_rect);
  73. }
  74. void WSWindowSwitcher::draw()
  75. {
  76. auto palette = WSWindowManager::the().palette();
  77. Painter painter(*m_switcher_window->backing_store());
  78. painter.fill_rect({ {}, m_rect.size() }, palette.window());
  79. painter.draw_rect({ {}, m_rect.size() }, palette.threed_shadow2());
  80. for (int index = 0; index < m_windows.size(); ++index) {
  81. auto& window = *m_windows.at(index);
  82. Rect item_rect {
  83. padding(),
  84. padding() + index * item_height(),
  85. m_rect.width() - padding() * 2,
  86. item_height()
  87. };
  88. Color text_color;
  89. Color rect_text_color;
  90. if (index == m_selected_index) {
  91. painter.fill_rect(item_rect, palette.selection());
  92. text_color = palette.selection_text();
  93. rect_text_color = palette.threed_shadow1();
  94. } else {
  95. text_color = palette.window_text();
  96. rect_text_color = palette.threed_shadow2();
  97. }
  98. item_rect.shrink(item_padding(), 0);
  99. Rect thumbnail_rect = { item_rect.location().translated(0, 5), { thumbnail_width(), thumbnail_height() } };
  100. if (window.backing_store()) {
  101. painter.draw_scaled_bitmap(thumbnail_rect, *window.backing_store(), window.backing_store()->rect());
  102. StylePainter::paint_frame(painter, thumbnail_rect.inflated(4, 4), palette, FrameShape::Container, FrameShadow::Sunken, 2);
  103. }
  104. Rect icon_rect = { thumbnail_rect.bottom_right().translated(-window.icon().width(), -window.icon().height()), { window.icon().width(), window.icon().height() } };
  105. painter.fill_rect(icon_rect, palette.window());
  106. painter.blit(icon_rect.location(), window.icon(), window.icon().rect());
  107. painter.draw_text(item_rect.translated(thumbnail_width() + 12, 0), window.title(), WSWindowManager::the().window_title_font(), TextAlignment::CenterLeft, text_color);
  108. painter.draw_text(item_rect, window.rect().to_string(), TextAlignment::CenterRight, rect_text_color);
  109. }
  110. }
  111. void WSWindowSwitcher::refresh()
  112. {
  113. auto& wm = WSWindowManager::the();
  114. WSWindow* selected_window = nullptr;
  115. if (m_selected_index > 0 && m_windows[m_selected_index])
  116. selected_window = m_windows[m_selected_index].ptr();
  117. if (!selected_window)
  118. selected_window = wm.highlight_window() ? wm.highlight_window() : wm.active_window();
  119. m_windows.clear();
  120. m_selected_index = 0;
  121. int window_count = 0;
  122. int longest_title_width = 0;
  123. wm.for_each_window_of_type_from_front_to_back(WSWindowType::Normal, [&](WSWindow& window) {
  124. ++window_count;
  125. longest_title_width = max(longest_title_width, wm.font().width(window.title()));
  126. if (selected_window == &window)
  127. m_selected_index = m_windows.size();
  128. m_windows.append(window.make_weak_ptr());
  129. return IterationDecision::Continue;
  130. },
  131. true);
  132. if (m_windows.is_empty()) {
  133. hide();
  134. return;
  135. }
  136. int space_for_window_rect = 180;
  137. m_rect.set_width(thumbnail_width() + longest_title_width + space_for_window_rect + padding() * 2 + item_padding() * 2);
  138. m_rect.set_height(window_count * item_height() + padding() * 2);
  139. m_rect.center_within(WSScreen::the().rect());
  140. if (!m_switcher_window)
  141. m_switcher_window = WSWindow::construct(*this, WSWindowType::WindowSwitcher);
  142. m_switcher_window->set_rect(m_rect);
  143. draw();
  144. }
  145. void WSWindowSwitcher::refresh_if_needed()
  146. {
  147. if (m_visible) {
  148. refresh();
  149. WSWindowManager::the().invalidate(m_rect);
  150. }
  151. }