ClassicWindowTheme.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibGfx/ClassicWindowTheme.h>
  8. #include <LibGfx/FontDatabase.h>
  9. #include <LibGfx/Painter.h>
  10. #include <LibGfx/Palette.h>
  11. #include <LibGfx/StylePainter.h>
  12. namespace Gfx {
  13. static constexpr int menubar_height = 20;
  14. ClassicWindowTheme::ClassicWindowTheme()
  15. {
  16. }
  17. ClassicWindowTheme::~ClassicWindowTheme()
  18. {
  19. }
  20. Gfx::IntRect ClassicWindowTheme::titlebar_icon_rect(WindowType window_type, const IntRect& window_rect, const Palette& palette) const
  21. {
  22. if (window_type == WindowType::ToolWindow)
  23. return {};
  24. auto titlebar_rect = this->titlebar_rect(window_type, window_rect, palette);
  25. Gfx::IntRect icon_rect {
  26. titlebar_rect.x() + 2,
  27. titlebar_rect.y(),
  28. 16,
  29. 16,
  30. };
  31. icon_rect.center_vertically_within(titlebar_rect);
  32. icon_rect.translate_by(0, 1);
  33. return icon_rect;
  34. }
  35. Gfx::IntRect ClassicWindowTheme::titlebar_text_rect(WindowType window_type, const IntRect& window_rect, const Palette& palette) const
  36. {
  37. auto titlebar_rect = this->titlebar_rect(window_type, window_rect, palette);
  38. auto titlebar_icon_rect = this->titlebar_icon_rect(window_type, window_rect, palette);
  39. return {
  40. titlebar_rect.x() + 3 + (titlebar_icon_rect.is_empty() ? 0 : (titlebar_icon_rect.width() + 2)),
  41. titlebar_rect.y(),
  42. titlebar_rect.width() - 5 - (titlebar_icon_rect.is_empty() ? 0 : (titlebar_icon_rect.width() + 2)),
  43. titlebar_rect.height()
  44. };
  45. }
  46. void ClassicWindowTheme::paint_normal_frame(Painter& painter, WindowState window_state, const IntRect& window_rect, const StringView& window_title, const Bitmap& icon, const Palette& palette, const IntRect& leftmost_button_rect, int menu_row_count, [[maybe_unused]] bool window_modified) const
  47. {
  48. auto frame_rect = frame_rect_for_window(WindowType::Normal, window_rect, palette, menu_row_count);
  49. frame_rect.set_location({ 0, 0 });
  50. Gfx::StylePainter::paint_window_frame(painter, frame_rect, palette);
  51. auto& title_font = FontDatabase::default_font().bold_variant();
  52. auto titlebar_rect = this->titlebar_rect(WindowType::Normal, window_rect, palette);
  53. auto titlebar_icon_rect = this->titlebar_icon_rect(WindowType::Normal, window_rect, palette);
  54. auto titlebar_inner_rect = titlebar_text_rect(WindowType::Normal, window_rect, palette);
  55. auto titlebar_title_rect = titlebar_inner_rect;
  56. titlebar_title_rect.set_width(title_font.width(window_title));
  57. auto [title_color, border_color, border_color2, stripes_color, shadow_color] = compute_frame_colors(window_state, palette);
  58. painter.draw_line(titlebar_rect.bottom_left().translated(0, 1), titlebar_rect.bottom_right().translated(0, 1), palette.button());
  59. painter.draw_line(titlebar_rect.bottom_left().translated(0, 2), titlebar_rect.bottom_right().translated(0, 2), palette.button());
  60. painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  61. int stripe_right = leftmost_button_rect.left() - 3;
  62. if (stripes_color.alpha() > 0) {
  63. int stripe_left = titlebar_title_rect.right() + 5;
  64. if (stripe_left && stripe_right && stripe_left < stripe_right) {
  65. for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) {
  66. painter.draw_line({ stripe_left, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, stripes_color);
  67. }
  68. }
  69. }
  70. auto clipped_title_rect = titlebar_title_rect;
  71. clipped_title_rect.set_width(stripe_right - clipped_title_rect.x());
  72. if (!clipped_title_rect.is_empty()) {
  73. painter.draw_text(clipped_title_rect.translated(1, 2), window_title, title_font, Gfx::TextAlignment::CenterLeft, shadow_color, Gfx::TextElision::Right);
  74. // FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline.
  75. painter.draw_text(clipped_title_rect.translated(0, 1), window_title, title_font, Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right);
  76. }
  77. painter.draw_scaled_bitmap(titlebar_icon_rect, icon, icon.rect());
  78. }
  79. void ClassicWindowTheme::paint_tool_window_frame(Painter& painter, WindowState window_state, const IntRect& window_rect, const StringView& title_text, const Palette& palette, const IntRect& leftmost_button_rect) const
  80. {
  81. auto frame_rect = frame_rect_for_window(WindowType::ToolWindow, window_rect, palette, 0);
  82. frame_rect.set_location({ 0, 0 });
  83. Gfx::StylePainter::paint_window_frame(painter, frame_rect, palette);
  84. auto& title_font = FontDatabase::default_font().bold_variant();
  85. auto titlebar_rect = this->titlebar_rect(WindowType::ToolWindow, window_rect, palette);
  86. auto titlebar_inner_rect = titlebar_text_rect(WindowType::ToolWindow, window_rect, palette);
  87. auto titlebar_title_rect = titlebar_inner_rect;
  88. titlebar_title_rect.set_width(title_font.width(title_text));
  89. auto [title_color, border_color, border_color2, stripes_color, shadow_color] = compute_frame_colors(window_state, palette);
  90. painter.draw_line(titlebar_rect.bottom_left().translated(0, 1), titlebar_rect.bottom_right().translated(0, 1), palette.button());
  91. painter.draw_line(titlebar_rect.bottom_left().translated(0, 2), titlebar_rect.bottom_right().translated(0, 2), palette.button());
  92. painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  93. int stripe_right = leftmost_button_rect.left() - 3;
  94. auto clipped_title_rect = titlebar_title_rect;
  95. clipped_title_rect.set_width(stripe_right - clipped_title_rect.x());
  96. if (!clipped_title_rect.is_empty()) {
  97. painter.draw_text(clipped_title_rect.translated(1, 2), title_text, title_font, Gfx::TextAlignment::CenterLeft, shadow_color, Gfx::TextElision::Right);
  98. // FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline.
  99. painter.draw_text(clipped_title_rect.translated(0, 1), title_text, title_font, Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right);
  100. }
  101. }
  102. IntRect ClassicWindowTheme::menubar_rect(WindowType window_type, const IntRect& window_rect, const Palette& palette, int menu_row_count) const
  103. {
  104. if (window_type != WindowType::Normal)
  105. return {};
  106. return { 4, 3 + titlebar_height(window_type, palette) + 2, window_rect.width(), menubar_height * menu_row_count };
  107. }
  108. IntRect ClassicWindowTheme::titlebar_rect(WindowType window_type, const IntRect& window_rect, const Palette& palette) const
  109. {
  110. auto& title_font = FontDatabase::default_font().bold_variant();
  111. auto window_titlebar_height = titlebar_height(window_type, palette);
  112. // FIXME: The top of the titlebar doesn't get redrawn properly if this padding is different
  113. int total_vertical_padding = title_font.glyph_height() - 1;
  114. if (window_type == WindowType::Notification)
  115. return { window_rect.width() + 3, total_vertical_padding / 2 - 1, window_titlebar_height, window_rect.height() };
  116. return { 4, 4, window_rect.width(), window_titlebar_height };
  117. }
  118. ClassicWindowTheme::FrameColors ClassicWindowTheme::compute_frame_colors(WindowState state, const Palette& palette) const
  119. {
  120. switch (state) {
  121. case WindowState::Highlighted:
  122. return { palette.highlight_window_title(), palette.highlight_window_border1(), palette.highlight_window_border2(), palette.highlight_window_title_stripes(), palette.highlight_window_title_shadow() };
  123. case WindowState::Moving:
  124. return { palette.moving_window_title(), palette.moving_window_border1(), palette.moving_window_border2(), palette.moving_window_title_stripes(), palette.moving_window_title_shadow() };
  125. case WindowState::Active:
  126. return { palette.active_window_title(), palette.active_window_border1(), palette.active_window_border2(), palette.active_window_title_stripes(), palette.active_window_title_shadow() };
  127. case WindowState::Inactive:
  128. return { palette.inactive_window_title(), palette.inactive_window_border1(), palette.inactive_window_border2(), palette.inactive_window_title_stripes(), palette.inactive_window_title_shadow() };
  129. default:
  130. VERIFY_NOT_REACHED();
  131. }
  132. }
  133. void ClassicWindowTheme::paint_notification_frame(Painter& painter, const IntRect& window_rect, const Palette& palette, const IntRect& close_button_rect) const
  134. {
  135. auto frame_rect = frame_rect_for_window(WindowType::Notification, window_rect, palette, 0);
  136. frame_rect.set_location({ 0, 0 });
  137. Gfx::StylePainter::paint_window_frame(painter, frame_rect, palette);
  138. auto titlebar_rect = this->titlebar_rect(WindowType::Notification, window_rect, palette);
  139. painter.fill_rect_with_gradient(Gfx::Orientation::Vertical, titlebar_rect, palette.active_window_border1(), palette.active_window_border2());
  140. if (palette.active_window_title_stripes().alpha() > 0) {
  141. int stripe_top = close_button_rect.bottom() + 4;
  142. int stripe_bottom = window_rect.height() - 3;
  143. if (stripe_top && stripe_bottom && stripe_top < stripe_bottom) {
  144. for (int i = 2; i <= palette.window_title_height() - 2; i += 2) {
  145. painter.draw_line({ titlebar_rect.x() + i, stripe_top }, { titlebar_rect.x() + i, stripe_bottom }, palette.active_window_title_stripes());
  146. }
  147. }
  148. }
  149. }
  150. IntRect ClassicWindowTheme::frame_rect_for_window(WindowType window_type, const IntRect& window_rect, const Gfx::Palette& palette, int menu_row_count) const
  151. {
  152. auto window_titlebar_height = titlebar_height(window_type, palette);
  153. switch (window_type) {
  154. case WindowType::Normal:
  155. case WindowType::ToolWindow:
  156. return {
  157. window_rect.x() - 4,
  158. window_rect.y() - window_titlebar_height - 5 - menu_row_count * menubar_height,
  159. window_rect.width() + 8,
  160. window_rect.height() + 9 + window_titlebar_height + menu_row_count * menubar_height
  161. };
  162. case WindowType::Notification:
  163. return {
  164. window_rect.x() - 3,
  165. window_rect.y() - 3,
  166. window_rect.width() + 6 + window_titlebar_height,
  167. window_rect.height() + 6
  168. };
  169. default:
  170. return window_rect;
  171. }
  172. }
  173. Vector<IntRect> ClassicWindowTheme::layout_buttons(WindowType window_type, const IntRect& window_rect, const Palette& palette, size_t buttons) const
  174. {
  175. int window_button_width = palette.window_title_button_width();
  176. int window_button_height = palette.window_title_button_height();
  177. int pos;
  178. Vector<IntRect> button_rects;
  179. if (window_type == WindowType::Notification)
  180. pos = titlebar_rect(window_type, window_rect, palette).top() + 2;
  181. else
  182. pos = titlebar_text_rect(window_type, window_rect, palette).right() + 1;
  183. for (size_t i = 0; i < buttons; i++) {
  184. if (window_type == WindowType::Notification) {
  185. // The button height & width have to be equal or it leaks out of its area
  186. Gfx::IntRect rect { 0, pos, window_button_height, window_button_height };
  187. rect.center_horizontally_within(titlebar_rect(window_type, window_rect, palette));
  188. button_rects.append(rect);
  189. pos += window_button_height;
  190. } else {
  191. pos -= window_button_width;
  192. Gfx::IntRect rect { pos, 0, window_button_width, window_button_height };
  193. rect.center_vertically_within(titlebar_text_rect(window_type, window_rect, palette));
  194. button_rects.append(rect);
  195. }
  196. }
  197. return button_rects;
  198. }
  199. int ClassicWindowTheme::titlebar_height(WindowType window_type, const Palette& palette) const
  200. {
  201. auto& title_font = FontDatabase::default_font().bold_variant();
  202. switch (window_type) {
  203. case WindowType::Normal:
  204. case WindowType::Notification:
  205. return max(palette.window_title_height(), title_font.glyph_height() + 8);
  206. case WindowType::ToolWindow:
  207. return max(palette.window_title_height() - 4, title_font.glyph_height() + 4);
  208. default:
  209. return 0;
  210. }
  211. }
  212. }