WSWindowFrame.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include <WindowServer/WSWindowFrame.h>
  2. #include <WindowServer/WSWindowManager.h>
  3. #include <WindowServer/WSWindow.h>
  4. #include <WindowServer/WSMessage.h>
  5. #include <SharedGraphics/CharacterBitmap.h>
  6. #include <SharedGraphics/Painter.h>
  7. #include <SharedGraphics/Font.h>
  8. #include <SharedGraphics/StylePainter.h>
  9. WSWindowFrame::WSWindowFrame(WSWindow& window)
  10. : m_window(window)
  11. {
  12. }
  13. WSWindowFrame::~WSWindowFrame()
  14. {
  15. }
  16. static const int window_titlebar_height = 18;
  17. static inline Rect menu_window_rect(const Rect& rect)
  18. {
  19. return rect.inflated(2, 2);
  20. }
  21. static inline Rect title_bar_rect(const Rect& window)
  22. {
  23. return {
  24. window.x() - 1,
  25. window.y() - window_titlebar_height,
  26. window.width() + 2,
  27. window_titlebar_height
  28. };
  29. }
  30. static inline Rect title_bar_icon_rect(const Rect& window)
  31. {
  32. auto titlebar_rect = title_bar_rect(window);
  33. return {
  34. titlebar_rect.x() + 2,
  35. titlebar_rect.y(),
  36. 16,
  37. titlebar_rect.height(),
  38. };
  39. }
  40. static inline Rect title_bar_text_rect(const Rect& window)
  41. {
  42. auto titlebar_rect = title_bar_rect(window);
  43. auto titlebar_icon_rect = title_bar_icon_rect(window);
  44. return {
  45. titlebar_rect.x() + 2 + titlebar_icon_rect.width() + 2,
  46. titlebar_rect.y(),
  47. titlebar_rect.width() - 4 - titlebar_icon_rect.width() - 2,
  48. titlebar_rect.height()
  49. };
  50. }
  51. static inline Rect close_button_rect_for_window(const Rect& window_rect)
  52. {
  53. auto titlebar_inner_rect = title_bar_text_rect(window_rect);
  54. int close_button_margin = 1;
  55. int close_button_size = titlebar_inner_rect.height() - close_button_margin * 2;
  56. return Rect {
  57. titlebar_inner_rect.right() - close_button_size + 1,
  58. titlebar_inner_rect.top() + close_button_margin,
  59. close_button_size,
  60. close_button_size - 1
  61. };
  62. }
  63. static inline Rect border_window_rect(const Rect& window)
  64. {
  65. auto titlebar_rect = title_bar_rect(window);
  66. return { titlebar_rect.x() - 1,
  67. titlebar_rect.y() - 1,
  68. titlebar_rect.width() + 2,
  69. window_titlebar_height + window.height() + 3
  70. };
  71. }
  72. static inline Rect outer_window_rect(const Rect& window)
  73. {
  74. auto rect = border_window_rect(window);
  75. rect.inflate(2, 2);
  76. return rect;
  77. }
  78. static inline Rect outer_window_rect(const WSWindow& window)
  79. {
  80. if (window.type() == WSWindowType::Menu)
  81. return menu_window_rect(window.rect());
  82. if (window.type() == WSWindowType::WindowSwitcher)
  83. return window.rect();
  84. if (window.type() == WSWindowType::Taskbar)
  85. return window.rect();
  86. ASSERT(window.type() == WSWindowType::Normal);
  87. return outer_window_rect(window.rect());
  88. }
  89. static const char* s_close_button_bitmap_data = {
  90. "## ##"
  91. "### ###"
  92. " ###### "
  93. " #### "
  94. " ## "
  95. " #### "
  96. " ###### "
  97. "### ###"
  98. "## ##"
  99. };
  100. static CharacterBitmap* s_close_button_bitmap;
  101. static const int s_close_button_bitmap_width = 8;
  102. static const int s_close_button_bitmap_height = 9;
  103. void WSWindowFrame::paint(Painter& painter)
  104. {
  105. //printf("[WM] paint_window_frame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
  106. if (m_window.type() == WSWindowType::Menu) {
  107. painter.draw_rect(menu_window_rect(m_window.rect()), Color::LightGray);
  108. return;
  109. }
  110. if (m_window.type() == WSWindowType::WindowSwitcher)
  111. return;
  112. if (m_window.type() == WSWindowType::Taskbar)
  113. return;
  114. auto& window = m_window;
  115. auto titlebar_rect = title_bar_rect(window.rect());
  116. auto titlebar_icon_rect = title_bar_icon_rect(window.rect());
  117. auto titlebar_inner_rect = title_bar_text_rect(window.rect());
  118. auto outer_rect = outer_window_rect(window);
  119. auto border_rect = border_window_rect(window.rect());
  120. auto close_button_rect = close_button_rect_for_window(window.rect());
  121. auto titlebar_title_rect = titlebar_inner_rect;
  122. titlebar_title_rect.set_width(Font::default_bold_font().width(window.title()));
  123. Rect inner_border_rect {
  124. window.x() - 1,
  125. window.y() - 1,
  126. window.width() + 2,
  127. window.height() + 2
  128. };
  129. Color title_color;
  130. Color border_color;
  131. Color border_color2;
  132. Color middle_border_color;
  133. auto& wm = WSWindowManager::the();
  134. if (&window == wm.m_highlight_window.ptr()) {
  135. border_color = wm.m_highlight_window_border_color;
  136. border_color2 = wm.m_highlight_window_border_color2;
  137. title_color = wm.m_highlight_window_title_color;
  138. middle_border_color = Color::White;
  139. } else if (&window == wm.m_drag_window.ptr()) {
  140. border_color = wm.m_dragging_window_border_color;
  141. border_color2 = wm.m_dragging_window_border_color2;
  142. title_color = wm.m_dragging_window_title_color;
  143. middle_border_color = Color::from_rgb(0xf9b36a);
  144. } else if (&window == wm.m_active_window.ptr()) {
  145. border_color = wm.m_active_window_border_color;
  146. border_color2 = wm.m_active_window_border_color2;
  147. title_color = wm.m_active_window_title_color;
  148. middle_border_color = Color::from_rgb(0x8f673d);
  149. } else {
  150. border_color = wm.m_inactive_window_border_color;
  151. border_color2 = wm.m_inactive_window_border_color2;
  152. title_color = wm.m_inactive_window_title_color;
  153. middle_border_color = Color::MidGray;
  154. }
  155. painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  156. for (int i = 2; i <= titlebar_inner_rect.height() - 4; i += 2) {
  157. painter.draw_line({ titlebar_title_rect.right() + 4, titlebar_inner_rect.y() + i }, { close_button_rect.left() - 3, titlebar_inner_rect.y() + i }, border_color);
  158. }
  159. painter.draw_rect(border_rect, middle_border_color);
  160. painter.draw_rect(outer_rect, border_color);
  161. painter.draw_rect(inner_border_rect, border_color);
  162. painter.draw_text(titlebar_title_rect, window.title(), wm.window_title_font(), TextAlignment::CenterLeft, title_color);
  163. painter.blit(titlebar_icon_rect.location(), window.icon(), window.icon().rect());
  164. if (!s_close_button_bitmap)
  165. s_close_button_bitmap = &CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
  166. StylePainter::paint_button(painter, close_button_rect, ButtonStyle::Normal, false, false);
  167. auto x_location = close_button_rect.center();
  168. x_location.move_by(-(s_close_button_bitmap_width / 2), -(s_close_button_bitmap_height / 2));
  169. painter.draw_bitmap(x_location, *s_close_button_bitmap, Color::Black);
  170. }
  171. Rect WSWindowFrame::rect() const
  172. {
  173. if (m_window.type() == WSWindowType::Menu)
  174. return menu_window_rect(m_window.rect());
  175. if (m_window.type() == WSWindowType::Normal)
  176. return outer_window_rect(m_window);
  177. if (m_window.type() == WSWindowType::WindowSwitcher)
  178. return m_window.rect();
  179. if (m_window.type() == WSWindowType::Taskbar)
  180. return m_window.rect();
  181. ASSERT_NOT_REACHED();
  182. }
  183. void WSWindowFrame::notify_window_rect_changed(const Rect& old_rect, const Rect& new_rect)
  184. {
  185. auto& wm = WSWindowManager::the();
  186. wm.invalidate(outer_window_rect(old_rect));
  187. wm.invalidate(outer_window_rect(new_rect));
  188. wm.notify_rect_changed(m_window, old_rect, new_rect);
  189. }
  190. void WSWindowFrame::on_mouse_event(const WSMouseEvent& event)
  191. {
  192. auto& wm = WSWindowManager::the();
  193. if (m_window.type() != WSWindowType::Normal)
  194. return;
  195. if (title_bar_rect(m_window.rect()).contains(event.position())) {
  196. if (event.type() == WSMessage::MouseDown)
  197. wm.move_to_front_and_make_active(m_window);
  198. if (close_button_rect_for_window(m_window.rect()).contains(event.position())) {
  199. handle_close_button_mouse_event(event);
  200. return;
  201. }
  202. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left)
  203. wm.start_window_drag(m_window, event);
  204. }
  205. }
  206. void WSWindowFrame::handle_close_button_mouse_event(const WSMouseEvent& event)
  207. {
  208. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  209. m_window.on_message(WSMessage(WSMessage::WindowCloseRequest));
  210. return;
  211. }
  212. }