WindowFrame.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ClientConnection.h"
  27. #include <AK/Badge.h>
  28. #include <LibGfx/CharacterBitmap.h>
  29. #include <LibGfx/Font.h>
  30. #include <LibGfx/Painter.h>
  31. #include <LibGfx/StylePainter.h>
  32. #include <WindowServer/Button.h>
  33. #include <WindowServer/Compositor.h>
  34. #include <WindowServer/Event.h>
  35. #include <WindowServer/Window.h>
  36. #include <WindowServer/WindowFrame.h>
  37. #include <WindowServer/WindowManager.h>
  38. namespace WindowServer {
  39. static const int window_titlebar_height = 19;
  40. static const char* s_close_button_bitmap_data = {
  41. "## ##"
  42. "### ###"
  43. " ###### "
  44. " #### "
  45. " #### "
  46. " ###### "
  47. "### ###"
  48. "## ##"
  49. " "
  50. };
  51. static Gfx::CharacterBitmap* s_close_button_bitmap;
  52. static const int s_close_button_bitmap_width = 8;
  53. static const int s_close_button_bitmap_height = 9;
  54. static const char* s_minimize_button_bitmap_data = {
  55. " "
  56. " "
  57. " "
  58. " ###### "
  59. " #### "
  60. " ## "
  61. " "
  62. " "
  63. " "
  64. };
  65. static Gfx::CharacterBitmap* s_minimize_button_bitmap;
  66. static const int s_minimize_button_bitmap_width = 8;
  67. static const int s_minimize_button_bitmap_height = 9;
  68. static const char* s_maximize_button_bitmap_data = {
  69. " "
  70. " "
  71. " "
  72. " ## "
  73. " #### "
  74. " ###### "
  75. " "
  76. " "
  77. " "
  78. };
  79. static Gfx::CharacterBitmap* s_maximize_button_bitmap;
  80. static const int s_maximize_button_bitmap_width = 8;
  81. static const int s_maximize_button_bitmap_height = 9;
  82. static const char* s_unmaximize_button_bitmap_data = {
  83. " "
  84. " ## "
  85. " #### "
  86. " ###### "
  87. " "
  88. " ###### "
  89. " #### "
  90. " ## "
  91. " "
  92. };
  93. static Gfx::CharacterBitmap* s_unmaximize_button_bitmap;
  94. static const int s_unmaximize_button_bitmap_width = 8;
  95. static const int s_unmaximize_button_bitmap_height = 9;
  96. WindowFrame::WindowFrame(Window& window)
  97. : m_window(window)
  98. {
  99. if (!s_close_button_bitmap)
  100. s_close_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
  101. if (!s_minimize_button_bitmap)
  102. s_minimize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_minimize_button_bitmap_data, s_minimize_button_bitmap_width, s_minimize_button_bitmap_height).leak_ref();
  103. if (!s_maximize_button_bitmap)
  104. s_maximize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_maximize_button_bitmap_data, s_maximize_button_bitmap_width, s_maximize_button_bitmap_height).leak_ref();
  105. if (!s_unmaximize_button_bitmap)
  106. s_unmaximize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_unmaximize_button_bitmap_data, s_unmaximize_button_bitmap_width, s_unmaximize_button_bitmap_height).leak_ref();
  107. m_buttons.append(make<Button>(*this, *s_close_button_bitmap, [this](auto&) {
  108. m_window.request_close();
  109. }));
  110. if (window.is_resizable()) {
  111. auto button = make<Button>(*this, *s_maximize_button_bitmap, [this](auto&) {
  112. m_window.set_maximized(!m_window.is_maximized());
  113. });
  114. m_maximize_button = button.ptr();
  115. m_buttons.append(move(button));
  116. }
  117. if (window.is_minimizable()) {
  118. auto button = make<Button>(*this, *s_minimize_button_bitmap, [this](auto&) {
  119. m_window.set_minimized(true);
  120. });
  121. m_minimize_button = button.ptr();
  122. m_buttons.append(move(button));
  123. }
  124. }
  125. WindowFrame::~WindowFrame()
  126. {
  127. }
  128. void WindowFrame::did_set_maximized(Badge<Window>, bool maximized)
  129. {
  130. ASSERT(m_maximize_button);
  131. m_maximize_button->set_bitmap(maximized ? *s_unmaximize_button_bitmap : *s_maximize_button_bitmap);
  132. }
  133. Gfx::IntRect WindowFrame::title_bar_rect() const
  134. {
  135. if (m_window.type() == WindowType::Notification)
  136. return { m_window.width() + 3, 3, window_titlebar_height, m_window.height() };
  137. return { 4, 4, m_window.width(), window_titlebar_height };
  138. }
  139. Gfx::IntRect WindowFrame::title_bar_icon_rect() const
  140. {
  141. auto titlebar_rect = title_bar_rect();
  142. return {
  143. titlebar_rect.x() + 1,
  144. titlebar_rect.y() + 2,
  145. 16,
  146. titlebar_rect.height(),
  147. };
  148. }
  149. Gfx::IntRect WindowFrame::title_bar_text_rect() const
  150. {
  151. auto titlebar_rect = title_bar_rect();
  152. auto titlebar_icon_rect = title_bar_icon_rect();
  153. return {
  154. titlebar_rect.x() + 2 + titlebar_icon_rect.width() + 2,
  155. titlebar_rect.y(),
  156. titlebar_rect.width() - 4 - titlebar_icon_rect.width() - 2,
  157. titlebar_rect.height()
  158. };
  159. }
  160. WindowFrame::FrameColors WindowFrame::compute_frame_colors() const
  161. {
  162. auto& wm = WindowManager::the();
  163. auto palette = wm.palette();
  164. if (&m_window == wm.m_highlight_window)
  165. return { palette.highlight_window_title(), palette.highlight_window_border1(), palette.highlight_window_border2() };
  166. if (&m_window == wm.m_move_window)
  167. return { palette.moving_window_title(), palette.moving_window_border1(), palette.moving_window_border2() };
  168. if (&m_window == wm.m_active_window)
  169. return { palette.active_window_title(), palette.active_window_border1(), palette.active_window_border2() };
  170. return { palette.inactive_window_title(), palette.inactive_window_border1(), palette.inactive_window_border2() };
  171. }
  172. void WindowFrame::paint_notification_frame(Gfx::Painter& painter)
  173. {
  174. auto palette = WindowManager::the().palette();
  175. Gfx::IntRect outer_rect = { {}, rect().size() };
  176. Gfx::StylePainter::paint_window_frame(painter, outer_rect, palette);
  177. auto titlebar_rect = title_bar_rect();
  178. painter.fill_rect_with_gradient(Gfx::Orientation::Vertical, titlebar_rect, palette.active_window_border1(), palette.active_window_border2());
  179. int stripe_top = m_buttons.last().relative_rect().bottom() + 4;
  180. int stripe_bottom = m_window.height() - 3;
  181. if (stripe_top && stripe_bottom && stripe_top < stripe_bottom) {
  182. for (int i = 2; i <= window_titlebar_height - 2; i += 2) {
  183. painter.draw_line({ titlebar_rect.x() + i, stripe_top }, { titlebar_rect.x() + i, stripe_bottom }, palette.active_window_border1());
  184. }
  185. }
  186. }
  187. void WindowFrame::paint_normal_frame(Gfx::Painter& painter)
  188. {
  189. auto palette = WindowManager::the().palette();
  190. auto& window = m_window;
  191. Gfx::IntRect outer_rect = { {}, rect().size() };
  192. Gfx::StylePainter::paint_window_frame(painter, outer_rect, palette);
  193. auto titlebar_rect = title_bar_rect();
  194. auto titlebar_icon_rect = title_bar_icon_rect();
  195. auto titlebar_inner_rect = title_bar_text_rect();
  196. auto titlebar_title_rect = titlebar_inner_rect;
  197. titlebar_title_rect.set_width(Gfx::Font::default_bold_font().width(window.title()));
  198. auto [title_color, border_color, border_color2] = compute_frame_colors();
  199. auto& wm = WindowManager::the();
  200. painter.draw_line(titlebar_rect.bottom_left().translated(0, 1), titlebar_rect.bottom_right().translated(0, 1), palette.button());
  201. painter.draw_line(titlebar_rect.bottom_left().translated(0, 2), titlebar_rect.bottom_right().translated(0, 2), palette.button());
  202. auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::IntRect() : m_buttons.last().relative_rect();
  203. painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  204. int stripe_left = titlebar_title_rect.right() + 4;
  205. int stripe_right = leftmost_button_rect.left() - 3;
  206. if (stripe_left && stripe_right && stripe_left < stripe_right) {
  207. for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) {
  208. painter.draw_line({ stripe_left, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, border_color);
  209. }
  210. }
  211. String title_text;
  212. if (window.client() && window.client()->is_unresponsive()) {
  213. StringBuilder builder;
  214. builder.append(window.title());
  215. builder.append(" (Not responding)");
  216. title_text = builder.to_string();
  217. } else {
  218. title_text = window.title();
  219. }
  220. auto clipped_title_rect = titlebar_title_rect;
  221. clipped_title_rect.set_width(stripe_right - clipped_title_rect.x());
  222. if (!clipped_title_rect.is_empty()) {
  223. painter.draw_text(clipped_title_rect.translated(1, 2), title_text, wm.window_title_font(), Gfx::TextAlignment::CenterLeft, border_color.darkened(0.4), Gfx::TextElision::Right);
  224. // FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline.
  225. painter.draw_text(clipped_title_rect.translated(0, 1), title_text, wm.window_title_font(), Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right);
  226. }
  227. painter.blit(titlebar_icon_rect.location(), window.icon(), window.icon().rect());
  228. }
  229. void WindowFrame::paint(Gfx::Painter& painter)
  230. {
  231. if (m_window.is_frameless())
  232. return;
  233. Gfx::PainterStateSaver saver(painter);
  234. painter.translate(rect().location());
  235. if (m_window.type() == WindowType::Notification)
  236. paint_notification_frame(painter);
  237. else if (m_window.type() == WindowType::Normal)
  238. paint_normal_frame(painter);
  239. else
  240. return;
  241. for (auto& button : m_buttons) {
  242. button.paint(painter);
  243. }
  244. }
  245. static Gfx::IntRect frame_rect_for_window(Window& window, const Gfx::IntRect& rect)
  246. {
  247. if (window.is_frameless())
  248. return rect;
  249. auto type = window.type();
  250. switch (type) {
  251. case WindowType::Normal:
  252. return {
  253. rect.x() - 4,
  254. rect.y() - window_titlebar_height - 6,
  255. rect.width() + 8,
  256. rect.height() + 10 + window_titlebar_height
  257. };
  258. case WindowType::Notification:
  259. return {
  260. rect.x() - 3,
  261. rect.y() - 3,
  262. rect.width() + 6 + window_titlebar_height,
  263. rect.height() + 6
  264. };
  265. default:
  266. return rect;
  267. }
  268. }
  269. static Gfx::IntRect frame_rect_for_window(Window& window)
  270. {
  271. return frame_rect_for_window(window, window.rect());
  272. }
  273. Gfx::IntRect WindowFrame::rect() const
  274. {
  275. return frame_rect_for_window(m_window);
  276. }
  277. void WindowFrame::invalidate_title_bar()
  278. {
  279. Compositor::the().invalidate(title_bar_rect().translated(rect().location()));
  280. }
  281. void WindowFrame::notify_window_rect_changed(const Gfx::IntRect& old_rect, const Gfx::IntRect& new_rect)
  282. {
  283. int window_button_width = 15;
  284. int window_button_height = 15;
  285. int pos;
  286. if (m_window.type() == WindowType::Notification)
  287. pos = title_bar_rect().top() + 2;
  288. else
  289. pos = title_bar_text_rect().right() + 1;
  290. for (auto& button : m_buttons) {
  291. if (m_window.type() == WindowType::Notification) {
  292. Gfx::IntRect rect { 0, pos, window_button_width, window_button_height };
  293. rect.center_horizontally_within(title_bar_rect());
  294. button.set_relative_rect(rect);
  295. pos += window_button_width;
  296. } else {
  297. pos -= window_button_width;
  298. Gfx::IntRect rect { pos, 0, window_button_width, window_button_height };
  299. rect.center_vertically_within(title_bar_text_rect());
  300. button.set_relative_rect(rect);
  301. }
  302. }
  303. auto& wm = WindowManager::the();
  304. wm.invalidate(frame_rect_for_window(m_window, old_rect));
  305. wm.invalidate(frame_rect_for_window(m_window, new_rect));
  306. wm.notify_rect_changed(m_window, old_rect, new_rect);
  307. }
  308. void WindowFrame::on_mouse_event(const MouseEvent& event)
  309. {
  310. ASSERT(!m_window.is_fullscreen());
  311. if (m_window.is_blocked_by_modal_window())
  312. return;
  313. auto& wm = WindowManager::the();
  314. if (m_window.type() != WindowType::Normal && m_window.type() != WindowType::Notification)
  315. return;
  316. if (m_window.type() == WindowType::Normal && event.type() == Event::MouseDown && (event.button() == MouseButton::Left || event.button() == MouseButton::Right) && title_bar_icon_rect().contains(event.position())) {
  317. wm.move_to_front_and_make_active(m_window);
  318. m_window.popup_window_menu(title_bar_rect().bottom_left().translated(rect().location()));
  319. return;
  320. }
  321. // This is slightly hackish, but expand the title bar rect by two pixels downwards,
  322. // so that mouse events between the title bar and window contents don't act like
  323. // mouse events on the border.
  324. auto adjusted_title_bar_rect = title_bar_rect();
  325. adjusted_title_bar_rect.set_height(adjusted_title_bar_rect.height() + 2);
  326. if (adjusted_title_bar_rect.contains(event.position())) {
  327. wm.clear_resize_candidate();
  328. if (event.type() == Event::MouseDown)
  329. wm.move_to_front_and_make_active(m_window);
  330. for (auto& button : m_buttons) {
  331. if (button.relative_rect().contains(event.position()))
  332. return button.on_mouse_event(event.translated(-button.relative_rect().location()));
  333. }
  334. if (event.type() == Event::MouseDown) {
  335. if (m_window.type() == WindowType::Normal && event.button() == MouseButton::Right) {
  336. m_window.popup_window_menu(event.position().translated(rect().location()));
  337. return;
  338. }
  339. if (m_window.is_movable() && event.button() == MouseButton::Left)
  340. wm.start_window_move(m_window, event.translated(rect().location()));
  341. }
  342. return;
  343. }
  344. if (m_window.is_resizable() && event.type() == Event::MouseMove && event.buttons() == 0) {
  345. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  346. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  347. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  348. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  349. };
  350. Gfx::IntRect outer_rect = { {}, rect().size() };
  351. ASSERT(outer_rect.contains(event.position()));
  352. int window_relative_x = event.x() - outer_rect.x();
  353. int window_relative_y = event.y() - outer_rect.y();
  354. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  355. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  356. wm.set_resize_candidate(m_window, direction_for_hot_area[hot_area_row][hot_area_column]);
  357. Compositor::the().invalidate_cursor();
  358. return;
  359. }
  360. if (m_window.is_resizable() && event.type() == Event::MouseDown && event.button() == MouseButton::Left)
  361. wm.start_window_resize(m_window, event.translated(rect().location()));
  362. }
  363. }