WindowFrame.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/Font.h>
  29. #include <LibGfx/Painter.h>
  30. #include <LibGfx/StylePainter.h>
  31. #include <WindowServer/Button.h>
  32. #include <WindowServer/Compositor.h>
  33. #include <WindowServer/Event.h>
  34. #include <WindowServer/Window.h>
  35. #include <WindowServer/WindowFrame.h>
  36. #include <WindowServer/WindowManager.h>
  37. namespace WindowServer {
  38. static Gfx::Bitmap* s_minimize_icon;
  39. static Gfx::Bitmap* s_maximize_icon;
  40. static Gfx::Bitmap* s_restore_icon;
  41. static Gfx::Bitmap* s_close_icon;
  42. static String s_last_title_button_icons_path;
  43. WindowFrame::WindowFrame(Window& window)
  44. : m_window(window)
  45. {
  46. auto button = make<Button>(*this, [this](auto&) {
  47. m_window.request_close();
  48. });
  49. m_close_button = button.ptr();
  50. m_buttons.append(move(button));
  51. if (window.is_resizable()) {
  52. auto button = make<Button>(*this, [this](auto&) {
  53. m_window.set_maximized(!m_window.is_maximized());
  54. });
  55. m_maximize_button = button.ptr();
  56. m_buttons.append(move(button));
  57. }
  58. if (window.is_minimizable()) {
  59. auto button = make<Button>(*this, [this](auto&) {
  60. m_window.set_minimized(true);
  61. });
  62. m_minimize_button = button.ptr();
  63. m_buttons.append(move(button));
  64. }
  65. set_button_icons();
  66. }
  67. WindowFrame::~WindowFrame()
  68. {
  69. }
  70. void WindowFrame::set_button_icons()
  71. {
  72. if (m_window.is_frameless())
  73. return;
  74. String icons_path = WindowManager::the().palette().title_button_icons_path();
  75. StringBuilder full_path;
  76. if (!s_minimize_icon || s_last_title_button_icons_path != icons_path) {
  77. full_path.append(icons_path);
  78. full_path.append("window-minimize.png");
  79. if (!(s_minimize_icon = Gfx::Bitmap::load_from_file(full_path.to_string()).leak_ref()))
  80. s_minimize_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-minimize.png").leak_ref();
  81. full_path.clear();
  82. }
  83. if (!s_maximize_icon || s_last_title_button_icons_path != icons_path) {
  84. full_path.append(icons_path);
  85. full_path.append("window-maximize.png");
  86. if (!(s_maximize_icon = Gfx::Bitmap::load_from_file(full_path.to_string()).leak_ref()))
  87. s_maximize_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-maximize.png").leak_ref();
  88. full_path.clear();
  89. }
  90. if (!s_restore_icon || s_last_title_button_icons_path != icons_path) {
  91. full_path.append(icons_path);
  92. full_path.append("window-restore.png");
  93. if (!(s_restore_icon = Gfx::Bitmap::load_from_file(full_path.to_string()).leak_ref()))
  94. s_restore_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-restore.png").leak_ref();
  95. full_path.clear();
  96. }
  97. if (!s_close_icon || s_last_title_button_icons_path != icons_path) {
  98. full_path.append(icons_path);
  99. full_path.append("window-close.png");
  100. if (!(s_close_icon = Gfx::Bitmap::load_from_file(full_path.to_string()).leak_ref()))
  101. s_close_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-close.png").leak_ref();
  102. full_path.clear();
  103. }
  104. m_close_button->set_icon(*s_close_icon);
  105. if (m_window.is_minimizable())
  106. m_minimize_button->set_icon(*s_minimize_icon);
  107. if (m_window.is_resizable())
  108. m_maximize_button->set_icon(m_window.is_maximized() ? *s_restore_icon : *s_maximize_icon);
  109. s_last_title_button_icons_path = icons_path;
  110. }
  111. void WindowFrame::did_set_maximized(Badge<Window>, bool maximized)
  112. {
  113. ASSERT(m_maximize_button);
  114. m_maximize_button->set_icon(maximized ? *s_restore_icon : *s_maximize_icon);
  115. }
  116. Gfx::IntRect WindowFrame::title_bar_rect() const
  117. {
  118. auto window_titlebar_height = WindowManager::the().palette().window_title_height();
  119. int total_vertical_padding = window_titlebar_height - WindowManager::the().window_title_font().glyph_height();
  120. if (m_window.type() == WindowType::Notification)
  121. return { m_window.width() + 3, total_vertical_padding / 2 - 1, window_titlebar_height, m_window.height() };
  122. return { 4, total_vertical_padding / 2, m_window.width(), window_titlebar_height };
  123. }
  124. Gfx::IntRect WindowFrame::title_bar_icon_rect() const
  125. {
  126. auto titlebar_rect = title_bar_rect();
  127. Gfx::IntRect icon_rect {
  128. titlebar_rect.x() + 2,
  129. titlebar_rect.y(),
  130. 16,
  131. 16,
  132. };
  133. icon_rect.center_vertically_within(titlebar_rect);
  134. icon_rect.move_by(0, 1);
  135. return icon_rect;
  136. }
  137. Gfx::IntRect WindowFrame::title_bar_text_rect() const
  138. {
  139. auto titlebar_rect = title_bar_rect();
  140. auto titlebar_icon_rect = title_bar_icon_rect();
  141. return {
  142. titlebar_rect.x() + 3 + titlebar_icon_rect.width() + 2,
  143. titlebar_rect.y(),
  144. titlebar_rect.width() - 5 - titlebar_icon_rect.width() - 2,
  145. titlebar_rect.height()
  146. };
  147. }
  148. WindowFrame::FrameColors WindowFrame::compute_frame_colors() const
  149. {
  150. auto& wm = WindowManager::the();
  151. auto palette = wm.palette();
  152. if (&m_window == wm.m_highlight_window)
  153. return { palette.highlight_window_title(), palette.highlight_window_border1(), palette.highlight_window_border2(), palette.highlight_window_title_stripes(), palette.highlight_window_title_shadow() };
  154. if (&m_window == wm.m_move_window)
  155. return { palette.moving_window_title(), palette.moving_window_border1(), palette.moving_window_border2(), palette.moving_window_title_stripes(), palette.moving_window_title_shadow() };
  156. if (wm.is_active_window_or_accessory(m_window))
  157. return { palette.active_window_title(), palette.active_window_border1(), palette.active_window_border2(), palette.active_window_title_stripes(), palette.active_window_title_shadow() };
  158. return { palette.inactive_window_title(), palette.inactive_window_border1(), palette.inactive_window_border2(), palette.inactive_window_title_stripes(), palette.inactive_window_title_shadow() };
  159. }
  160. void WindowFrame::paint_notification_frame(Gfx::Painter& painter)
  161. {
  162. auto palette = WindowManager::the().palette();
  163. Gfx::IntRect outer_rect = { {}, rect().size() };
  164. Gfx::StylePainter::paint_window_frame(painter, outer_rect, palette);
  165. auto titlebar_rect = title_bar_rect();
  166. painter.fill_rect_with_gradient(Gfx::Orientation::Vertical, titlebar_rect, palette.active_window_border1(), palette.active_window_border2());
  167. int stripe_top = m_buttons.last().relative_rect().bottom() + 4;
  168. int stripe_bottom = m_window.height() - 3;
  169. if (stripe_top && stripe_bottom && stripe_top < stripe_bottom) {
  170. for (int i = 2; i <= palette.window_title_height() - 2; i += 2) {
  171. painter.draw_line({ titlebar_rect.x() + i, stripe_top }, { titlebar_rect.x() + i, stripe_bottom }, palette.active_window_title_stripes());
  172. }
  173. }
  174. }
  175. void WindowFrame::paint_normal_frame(Gfx::Painter& painter)
  176. {
  177. auto palette = WindowManager::the().palette();
  178. auto& window = m_window;
  179. Gfx::IntRect outer_rect = { {}, rect().size() };
  180. String title_text;
  181. if (window.client() && window.client()->is_unresponsive()) {
  182. StringBuilder builder;
  183. builder.append(window.title());
  184. builder.append(" (Not responding)");
  185. title_text = builder.to_string();
  186. } else {
  187. title_text = window.title();
  188. }
  189. Gfx::StylePainter::paint_window_frame(painter, outer_rect, palette);
  190. auto titlebar_rect = title_bar_rect();
  191. auto titlebar_icon_rect = title_bar_icon_rect();
  192. auto titlebar_inner_rect = title_bar_text_rect();
  193. auto titlebar_title_rect = titlebar_inner_rect;
  194. titlebar_title_rect.set_width(WindowManager::the().window_title_font().width(title_text));
  195. auto [title_color, border_color, border_color2, stripes_color, shadow_color] = compute_frame_colors();
  196. auto& wm = WindowManager::the();
  197. painter.draw_line(titlebar_rect.bottom_left().translated(0, 1), titlebar_rect.bottom_right().translated(0, 1), palette.button());
  198. painter.draw_line(titlebar_rect.bottom_left().translated(0, 2), titlebar_rect.bottom_right().translated(0, 2), palette.button());
  199. auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::IntRect() : m_buttons.last().relative_rect();
  200. painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  201. int stripe_left = titlebar_title_rect.right() + 4;
  202. int stripe_right = leftmost_button_rect.left() - 3;
  203. if (stripe_left && stripe_right && stripe_left < stripe_right) {
  204. for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) {
  205. painter.draw_line({ stripe_left, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, stripes_color);
  206. }
  207. }
  208. auto clipped_title_rect = titlebar_title_rect;
  209. clipped_title_rect.set_width(stripe_right - clipped_title_rect.x());
  210. if (!clipped_title_rect.is_empty()) {
  211. painter.draw_text(clipped_title_rect.translated(1, 2), title_text, wm.window_title_font(), Gfx::TextAlignment::CenterLeft, shadow_color, Gfx::TextElision::Right);
  212. // FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline.
  213. painter.draw_text(clipped_title_rect.translated(0, 1), title_text, wm.window_title_font(), Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right);
  214. }
  215. painter.blit(titlebar_icon_rect.location(), window.icon(), window.icon().rect());
  216. }
  217. void WindowFrame::paint(Gfx::Painter& painter)
  218. {
  219. if (m_window.is_frameless())
  220. return;
  221. Gfx::PainterStateSaver saver(painter);
  222. painter.translate(rect().location());
  223. if (m_window.type() == WindowType::Notification)
  224. paint_notification_frame(painter);
  225. else if (m_window.type() == WindowType::Normal)
  226. paint_normal_frame(painter);
  227. else
  228. return;
  229. for (auto& button : m_buttons) {
  230. button.paint(painter);
  231. }
  232. }
  233. static Gfx::IntRect frame_rect_for_window(Window& window, const Gfx::IntRect& rect)
  234. {
  235. if (window.is_frameless())
  236. return rect;
  237. auto type = window.type();
  238. auto window_titlebar_height = WindowManager::the().palette().window_title_height();
  239. switch (type) {
  240. case WindowType::Normal:
  241. return {
  242. rect.x() - 4,
  243. rect.y() - window_titlebar_height - 6,
  244. rect.width() + 8,
  245. rect.height() + 10 + window_titlebar_height
  246. };
  247. case WindowType::Notification:
  248. return {
  249. rect.x() - 3,
  250. rect.y() - 3,
  251. rect.width() + 6 + window_titlebar_height,
  252. rect.height() + 6
  253. };
  254. default:
  255. return rect;
  256. }
  257. }
  258. static Gfx::IntRect frame_rect_for_window(Window& window)
  259. {
  260. return frame_rect_for_window(window, window.rect());
  261. }
  262. Gfx::IntRect WindowFrame::rect() const
  263. {
  264. return frame_rect_for_window(m_window);
  265. }
  266. void WindowFrame::invalidate_title_bar()
  267. {
  268. Compositor::the().invalidate(title_bar_rect().translated(rect().location()));
  269. }
  270. void WindowFrame::notify_window_rect_changed(const Gfx::IntRect& old_rect, const Gfx::IntRect& new_rect)
  271. {
  272. layout_buttons();
  273. auto& wm = WindowManager::the();
  274. wm.invalidate(frame_rect_for_window(m_window, old_rect));
  275. wm.invalidate(frame_rect_for_window(m_window, new_rect));
  276. wm.notify_rect_changed(m_window, old_rect, new_rect);
  277. }
  278. void WindowFrame::layout_buttons()
  279. {
  280. auto palette = WindowManager::the().palette();
  281. int window_button_width = palette.window_title_button_width();
  282. int window_button_height = palette.window_title_button_height();
  283. int pos;
  284. if (m_window.type() == WindowType::Notification)
  285. pos = title_bar_rect().top() + 2;
  286. else
  287. pos = title_bar_text_rect().right() + 1;
  288. for (auto& button : m_buttons) {
  289. if (m_window.type() == WindowType::Notification) {
  290. // The button height & width have to be equal or it leaks out of its area
  291. Gfx::IntRect rect { 0, pos, window_button_height, window_button_height };
  292. rect.center_horizontally_within(title_bar_rect());
  293. button.set_relative_rect(rect);
  294. pos += window_button_height;
  295. } else {
  296. pos -= window_button_width;
  297. Gfx::IntRect rect { pos, 0, window_button_width, window_button_height };
  298. rect.center_vertically_within(title_bar_text_rect());
  299. button.set_relative_rect(rect);
  300. }
  301. }
  302. }
  303. void WindowFrame::on_mouse_event(const MouseEvent& event)
  304. {
  305. ASSERT(!m_window.is_fullscreen());
  306. if (m_window.is_blocked_by_modal_window())
  307. return;
  308. auto& wm = WindowManager::the();
  309. if (m_window.type() != WindowType::Normal && m_window.type() != WindowType::Notification)
  310. return;
  311. if (m_window.type() == WindowType::Normal && title_bar_icon_rect().contains(event.position())) {
  312. if (event.type() == Event::MouseDown)
  313. wm.move_to_front_and_make_active(m_window);
  314. if (event.type() == Event::MouseDown && (event.button() == MouseButton::Left || event.button() == MouseButton::Right)) {
  315. // Manually start a potential double click. Since we're opening
  316. // a menu, we will only receive the MouseDown event, so we
  317. // need to record that fact. If the user subsequently clicks
  318. // on the same area, the menu will get closed, and we will
  319. // receive a MouseUp event, but because windows have changed
  320. // we don't get a MouseDoubleClick event. We can however record
  321. // this click, and when we receive the MouseUp event check if
  322. // it would have been considered a double click, if it weren't
  323. // for the fact that we opened and closed a window in the meanwhile
  324. auto& wm = WindowManager::the();
  325. wm.start_menu_doubleclick(m_window, event);
  326. m_window.popup_window_menu(title_bar_rect().bottom_left().translated(rect().location()), WindowMenuDefaultAction::Close);
  327. return;
  328. } else if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) {
  329. // Since the MouseDown event opened a menu, another MouseUp
  330. // from the second click outside the menu wouldn't be considered
  331. // a double click, so let's manually check if it would otherwise
  332. // have been be considered to be one
  333. auto& wm = WindowManager::the();
  334. if (wm.is_menu_doubleclick(m_window, event)) {
  335. // It is a double click, so perform activate the default item
  336. m_window.window_menu_activate_default();
  337. }
  338. return;
  339. }
  340. }
  341. // This is slightly hackish, but expand the title bar rect by two pixels downwards,
  342. // so that mouse events between the title bar and window contents don't act like
  343. // mouse events on the border.
  344. auto adjusted_title_bar_rect = title_bar_rect();
  345. adjusted_title_bar_rect.set_height(adjusted_title_bar_rect.height() + 2);
  346. if (adjusted_title_bar_rect.contains(event.position())) {
  347. wm.clear_resize_candidate();
  348. if (event.type() == Event::MouseDown)
  349. wm.move_to_front_and_make_active(m_window);
  350. for (auto& button : m_buttons) {
  351. if (button.relative_rect().contains(event.position()))
  352. return button.on_mouse_event(event.translated(-button.relative_rect().location()));
  353. }
  354. if (event.type() == Event::MouseDown) {
  355. if (m_window.type() == WindowType::Normal && event.button() == MouseButton::Right) {
  356. auto default_action = m_window.is_maximized() ? WindowMenuDefaultAction::Restore : WindowMenuDefaultAction::Maximize;
  357. m_window.popup_window_menu(event.position().translated(rect().location()), default_action);
  358. return;
  359. }
  360. if (m_window.is_movable() && event.button() == MouseButton::Left)
  361. wm.start_window_move(m_window, event.translated(rect().location()));
  362. }
  363. return;
  364. }
  365. if (m_window.is_resizable() && event.type() == Event::MouseMove && event.buttons() == 0) {
  366. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  367. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  368. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  369. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  370. };
  371. Gfx::IntRect outer_rect = { {}, rect().size() };
  372. ASSERT(outer_rect.contains(event.position()));
  373. int window_relative_x = event.x() - outer_rect.x();
  374. int window_relative_y = event.y() - outer_rect.y();
  375. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  376. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  377. wm.set_resize_candidate(m_window, direction_for_hot_area[hot_area_row][hot_area_column]);
  378. Compositor::the().invalidate_cursor();
  379. return;
  380. }
  381. if (m_window.is_resizable() && event.type() == Event::MouseDown && event.button() == MouseButton::Left)
  382. wm.start_window_resize(m_window, event.translated(rect().location()));
  383. }
  384. }