WindowFrame.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 <LibGfx/WindowTheme.h>
  32. #include <WindowServer/Button.h>
  33. #include <WindowServer/Compositor.h>
  34. #include <WindowServer/Event.h>
  35. #include <WindowServer/Screen.h>
  36. #include <WindowServer/Window.h>
  37. #include <WindowServer/WindowFrame.h>
  38. #include <WindowServer/WindowManager.h>
  39. namespace WindowServer {
  40. static Gfx::WindowTheme::WindowType to_theme_window_type(WindowType type)
  41. {
  42. switch (type) {
  43. case WindowType::Normal:
  44. return Gfx::WindowTheme::WindowType::Normal;
  45. case WindowType::Notification:
  46. return Gfx::WindowTheme::WindowType::Notification;
  47. default:
  48. return Gfx::WindowTheme::WindowType::Other;
  49. }
  50. }
  51. static Gfx::Bitmap* s_minimize_icon;
  52. static Gfx::Bitmap* s_maximize_icon;
  53. static Gfx::Bitmap* s_restore_icon;
  54. static Gfx::Bitmap* s_close_icon;
  55. static String s_last_title_button_icons_path;
  56. static int s_last_title_button_icons_scale;
  57. static Gfx::Bitmap* s_window_shadow;
  58. static Gfx::Bitmap* s_menu_shadow;
  59. static Gfx::Bitmap* s_tooltip_shadow;
  60. static String s_last_window_shadow_path;
  61. static String s_last_menu_shadow_path;
  62. static String s_last_tooltip_shadow_path;
  63. static Gfx::IntRect frame_rect_for_window(Window& window, const Gfx::IntRect& rect)
  64. {
  65. if (window.is_frameless())
  66. return rect;
  67. return Gfx::WindowTheme::current().frame_rect_for_window(to_theme_window_type(window.type()), rect, WindowManager::the().palette());
  68. }
  69. WindowFrame::WindowFrame(Window& window)
  70. : m_window(window)
  71. {
  72. auto button = make<Button>(*this, [this](auto&) {
  73. m_window.request_close();
  74. });
  75. m_close_button = button.ptr();
  76. m_buttons.append(move(button));
  77. if (window.is_resizable()) {
  78. auto button = make<Button>(*this, [this](auto&) {
  79. WindowManager::the().maximize_windows(m_window, !m_window.is_maximized());
  80. });
  81. button->on_middle_click = [&](auto&) {
  82. m_window.set_vertically_maximized();
  83. };
  84. m_maximize_button = button.ptr();
  85. m_buttons.append(move(button));
  86. }
  87. if (window.is_minimizable()) {
  88. auto button = make<Button>(*this, [this](auto&) {
  89. WindowManager::the().minimize_windows(m_window, true);
  90. });
  91. m_minimize_button = button.ptr();
  92. m_buttons.append(move(button));
  93. }
  94. set_button_icons();
  95. }
  96. WindowFrame::~WindowFrame()
  97. {
  98. }
  99. void WindowFrame::set_button_icons()
  100. {
  101. m_dirty = true;
  102. if (m_window.is_frameless())
  103. return;
  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. }
  110. void WindowFrame::reload_config()
  111. {
  112. String icons_path = WindowManager::the().palette().title_button_icons_path();
  113. int icons_scale = WindowManager::the().compositor_icon_scale();
  114. StringBuilder full_path;
  115. if (!s_minimize_icon || s_last_title_button_icons_path != icons_path || s_last_title_button_icons_scale != icons_scale) {
  116. full_path.append(icons_path);
  117. full_path.append("window-minimize.png");
  118. if (s_minimize_icon)
  119. s_minimize_icon->unref();
  120. if (!(s_minimize_icon = Gfx::Bitmap::load_from_file(full_path.to_string(), icons_scale).leak_ref()))
  121. s_minimize_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png", icons_scale).leak_ref();
  122. full_path.clear();
  123. }
  124. if (!s_maximize_icon || s_last_title_button_icons_path != icons_path || s_last_title_button_icons_scale != icons_scale) {
  125. full_path.append(icons_path);
  126. full_path.append("window-maximize.png");
  127. if (s_maximize_icon)
  128. s_maximize_icon->unref();
  129. if (!(s_maximize_icon = Gfx::Bitmap::load_from_file(full_path.to_string(), icons_scale).leak_ref()))
  130. s_maximize_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/upward-triangle.png", icons_scale).leak_ref();
  131. full_path.clear();
  132. }
  133. if (!s_restore_icon || s_last_title_button_icons_path != icons_path || s_last_title_button_icons_scale != icons_scale) {
  134. full_path.append(icons_path);
  135. full_path.append("window-restore.png");
  136. if (s_restore_icon)
  137. s_restore_icon->unref();
  138. if (!(s_restore_icon = Gfx::Bitmap::load_from_file(full_path.to_string(), icons_scale).leak_ref()))
  139. s_restore_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-restore.png", icons_scale).leak_ref();
  140. full_path.clear();
  141. }
  142. if (!s_close_icon || s_last_title_button_icons_path != icons_path || s_last_title_button_icons_scale != icons_scale) {
  143. full_path.append(icons_path);
  144. full_path.append("window-close.png");
  145. if (s_close_icon)
  146. s_close_icon->unref();
  147. if (!(s_close_icon = Gfx::Bitmap::load_from_file(full_path.to_string(), icons_scale).leak_ref()))
  148. s_close_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-close.png", icons_scale).leak_ref();
  149. full_path.clear();
  150. }
  151. s_last_title_button_icons_path = icons_path;
  152. s_last_title_button_icons_scale = icons_scale;
  153. auto load_shadow = [](const String& path, String& last_path, Gfx::Bitmap*& shadow_bitmap) {
  154. if (path.is_empty()) {
  155. last_path = String::empty();
  156. if (shadow_bitmap) {
  157. shadow_bitmap->unref();
  158. shadow_bitmap = nullptr;
  159. }
  160. } else if (!shadow_bitmap || shadow_bitmap->scale() != s_last_title_button_icons_scale || last_path != path) {
  161. if (shadow_bitmap)
  162. shadow_bitmap->unref();
  163. shadow_bitmap = Gfx::Bitmap::load_from_file(path, s_last_title_button_icons_scale).leak_ref();
  164. if (shadow_bitmap)
  165. last_path = path;
  166. else
  167. last_path = String::empty();
  168. }
  169. };
  170. load_shadow(WindowManager::the().palette().window_shadow_path(), s_last_window_shadow_path, s_window_shadow);
  171. load_shadow(WindowManager::the().palette().menu_shadow_path(), s_last_menu_shadow_path, s_menu_shadow);
  172. load_shadow(WindowManager::the().palette().tooltip_shadow_path(), s_last_tooltip_shadow_path, s_tooltip_shadow);
  173. }
  174. Gfx::Bitmap* WindowFrame::window_shadow() const
  175. {
  176. if (m_window.is_frameless())
  177. return nullptr;
  178. switch (m_window.type()) {
  179. case WindowType::Desktop:
  180. return nullptr;
  181. case WindowType::Menu:
  182. return s_menu_shadow;
  183. case WindowType::Tooltip:
  184. return s_tooltip_shadow;
  185. default:
  186. return s_window_shadow;
  187. }
  188. }
  189. bool WindowFrame::frame_has_alpha() const
  190. {
  191. if (auto* shadow_bitmap = window_shadow(); shadow_bitmap && shadow_bitmap->format() == Gfx::BitmapFormat::RGBA32)
  192. return true;
  193. return false;
  194. }
  195. void WindowFrame::did_set_maximized(Badge<Window>, bool maximized)
  196. {
  197. ASSERT(m_maximize_button);
  198. m_maximize_button->set_icon(maximized ? *s_restore_icon : *s_maximize_icon);
  199. }
  200. Gfx::IntRect WindowFrame::title_bar_rect() const
  201. {
  202. return Gfx::WindowTheme::current().title_bar_rect(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette());
  203. }
  204. Gfx::IntRect WindowFrame::title_bar_icon_rect() const
  205. {
  206. return Gfx::WindowTheme::current().title_bar_icon_rect(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette());
  207. }
  208. Gfx::IntRect WindowFrame::title_bar_text_rect() const
  209. {
  210. return Gfx::WindowTheme::current().title_bar_text_rect(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette());
  211. }
  212. Gfx::WindowTheme::WindowState WindowFrame::window_state_for_theme() const
  213. {
  214. auto& wm = WindowManager::the();
  215. if (m_flash_timer && m_flash_timer->is_active())
  216. return m_flash_counter & 1 ? Gfx::WindowTheme::WindowState::Active : Gfx::WindowTheme::WindowState::Inactive;
  217. if (&m_window == wm.m_highlight_window)
  218. return Gfx::WindowTheme::WindowState::Highlighted;
  219. if (&m_window == wm.m_move_window)
  220. return Gfx::WindowTheme::WindowState::Moving;
  221. if (wm.is_active_window_or_accessory(m_window))
  222. return Gfx::WindowTheme::WindowState::Active;
  223. return Gfx::WindowTheme::WindowState::Inactive;
  224. }
  225. void WindowFrame::paint_notification_frame(Gfx::Painter& painter)
  226. {
  227. auto palette = WindowManager::the().palette();
  228. Gfx::WindowTheme::current().paint_notification_frame(painter, m_window.rect(), palette, m_buttons.last().relative_rect());
  229. }
  230. void WindowFrame::paint_normal_frame(Gfx::Painter& painter)
  231. {
  232. auto palette = WindowManager::the().palette();
  233. auto& window = m_window;
  234. String title_text;
  235. if (window.client() && window.client()->is_unresponsive()) {
  236. StringBuilder builder;
  237. builder.append(window.title());
  238. builder.append(" (Not responding)");
  239. title_text = builder.to_string();
  240. } else {
  241. title_text = window.title();
  242. }
  243. auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::IntRect() : m_buttons.last().relative_rect();
  244. Gfx::WindowTheme::current().paint_normal_frame(painter, window_state_for_theme(), m_window.rect(), title_text, m_window.icon(), palette, leftmost_button_rect);
  245. }
  246. void WindowFrame::paint(Gfx::Painter& painter, const Gfx::IntRect& rect)
  247. {
  248. render_to_cache();
  249. auto frame_rect = render_rect();
  250. auto window_rect = m_window.rect();
  251. if (m_top_bottom) {
  252. auto top_bottom_height = frame_rect.height() - window_rect.height();
  253. if (m_bottom_y > 0) {
  254. // We have a top piece
  255. auto src_rect = rect.intersected({ frame_rect.location(), { frame_rect.width(), m_bottom_y } });
  256. if (!src_rect.is_empty())
  257. painter.blit(src_rect.location(), *m_top_bottom, src_rect.translated(-frame_rect.location()), m_opacity);
  258. }
  259. if (m_bottom_y < top_bottom_height) {
  260. // We have a bottom piece
  261. Gfx::IntRect rect_in_frame { frame_rect.x(), window_rect.bottom() + 1, frame_rect.width(), top_bottom_height - m_bottom_y };
  262. auto src_rect = rect.intersected(rect_in_frame);
  263. if (!src_rect.is_empty())
  264. painter.blit(src_rect.location(), *m_top_bottom, src_rect.translated(-rect_in_frame.x(), -rect_in_frame.y() + m_bottom_y), m_opacity);
  265. }
  266. }
  267. if (m_left_right) {
  268. auto left_right_width = frame_rect.width() - window_rect.width();
  269. if (m_right_x > 0) {
  270. // We have a left piece
  271. Gfx::IntRect rect_in_frame { frame_rect.x(), window_rect.y(), m_right_x, window_rect.height() };
  272. auto src_rect = rect.intersected(rect_in_frame);
  273. if (!src_rect.is_empty())
  274. painter.blit(src_rect.location(), *m_left_right, src_rect.translated(-rect_in_frame.location()), m_opacity);
  275. }
  276. if (m_right_x < left_right_width) {
  277. // We have a right piece
  278. Gfx::IntRect rect_in_frame { window_rect.right() + 1, window_rect.y(), left_right_width - m_right_x, window_rect.height() };
  279. auto src_rect = rect.intersected(rect_in_frame);
  280. if (!src_rect.is_empty())
  281. painter.blit(src_rect.location(), *m_left_right, src_rect.translated(-rect_in_frame.x() + m_right_x, -rect_in_frame.y()), m_opacity);
  282. }
  283. }
  284. }
  285. void WindowFrame::render(Gfx::Painter& painter)
  286. {
  287. if (m_window.is_frameless())
  288. return;
  289. if (m_window.type() == WindowType::Notification)
  290. paint_notification_frame(painter);
  291. else if (m_window.type() == WindowType::Normal)
  292. paint_normal_frame(painter);
  293. else
  294. return;
  295. for (auto& button : m_buttons) {
  296. button.paint(painter);
  297. }
  298. }
  299. void WindowFrame::render_to_cache()
  300. {
  301. if (!m_dirty)
  302. return;
  303. m_dirty = false;
  304. static Gfx::Bitmap* s_tmp_bitmap;
  305. auto frame_rect = rect();
  306. auto total_frame_rect = frame_rect;
  307. Gfx::Bitmap* shadow_bitmap = inflate_for_shadow(total_frame_rect, m_shadow_offset);
  308. auto window_rect = m_window.rect();
  309. auto scale = Screen::the().scale_factor();
  310. if (!s_tmp_bitmap || !s_tmp_bitmap->size().contains(total_frame_rect.size()) || s_tmp_bitmap->scale() != scale) {
  311. if (s_tmp_bitmap)
  312. s_tmp_bitmap->unref();
  313. s_tmp_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, total_frame_rect.size(), scale).leak_ref();
  314. }
  315. auto top_bottom_height = total_frame_rect.height() - window_rect.height();
  316. auto left_right_width = total_frame_rect.width() - window_rect.width();
  317. if (!m_top_bottom || m_top_bottom->width() != total_frame_rect.width() || m_top_bottom->height() != top_bottom_height || m_top_bottom->scale() != scale) {
  318. if (top_bottom_height > 0)
  319. m_top_bottom = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, { total_frame_rect.width(), top_bottom_height }, scale);
  320. else
  321. m_top_bottom = nullptr;
  322. m_shadow_dirty = true;
  323. }
  324. if (!m_left_right || m_left_right->height() != total_frame_rect.height() || m_left_right->width() != left_right_width || m_left_right->scale() != scale) {
  325. if (left_right_width > 0)
  326. m_left_right = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, { left_right_width, total_frame_rect.height() }, scale);
  327. else
  328. m_left_right = nullptr;
  329. m_shadow_dirty = true;
  330. }
  331. auto& frame_rect_to_update = m_shadow_dirty ? total_frame_rect : frame_rect;
  332. Gfx::IntPoint update_location(m_shadow_dirty ? Gfx::IntPoint { 0, 0 } : m_shadow_offset);
  333. Gfx::Painter painter(*s_tmp_bitmap);
  334. // Clear the frame area, not including the window content area, which we don't care about
  335. for (auto& rect : frame_rect_to_update.shatter(window_rect))
  336. painter.clear_rect({ rect.location() - frame_rect_to_update.location(), rect.size() }, { 255, 255, 255, 0 });
  337. if (m_shadow_dirty && shadow_bitmap)
  338. paint_simple_rect_shadow(painter, { { 0, 0 }, total_frame_rect.size() }, *shadow_bitmap);
  339. {
  340. Gfx::PainterStateSaver save(painter);
  341. painter.translate(m_shadow_offset);
  342. render(painter);
  343. }
  344. if (m_top_bottom && top_bottom_height > 0) {
  345. m_bottom_y = window_rect.y() - total_frame_rect.y();
  346. ASSERT(m_bottom_y >= 0);
  347. Gfx::Painter top_bottom_painter(*m_top_bottom);
  348. top_bottom_painter.add_clip_rect({ update_location, { frame_rect_to_update.width(), top_bottom_height - update_location.y() - (total_frame_rect.bottom() - frame_rect_to_update.bottom()) } });
  349. if (m_bottom_y > 0)
  350. top_bottom_painter.blit({ 0, 0 }, *s_tmp_bitmap, { 0, 0, total_frame_rect.width(), m_bottom_y }, 1.0, false);
  351. if (m_bottom_y < top_bottom_height)
  352. top_bottom_painter.blit({ 0, m_bottom_y }, *s_tmp_bitmap, { 0, total_frame_rect.height() - (total_frame_rect.bottom() - window_rect.bottom()), total_frame_rect.width(), top_bottom_height - m_bottom_y }, 1.0, false);
  353. } else {
  354. m_bottom_y = 0;
  355. }
  356. if (left_right_width > 0) {
  357. m_right_x = window_rect.x() - total_frame_rect.x();
  358. ASSERT(m_right_x >= 0);
  359. Gfx::Painter left_right_painter(*m_left_right);
  360. left_right_painter.add_clip_rect({ update_location, { left_right_width - update_location.x() - (total_frame_rect.right() - frame_rect_to_update.right()), window_rect.height() } });
  361. if (m_right_x > 0)
  362. left_right_painter.blit({ 0, 0 }, *s_tmp_bitmap, { 0, m_bottom_y, m_right_x, window_rect.height() }, 1.0, false);
  363. if (m_right_x < left_right_width)
  364. left_right_painter.blit({ m_right_x, 0 }, *s_tmp_bitmap, { (window_rect.right() - total_frame_rect.x()) + 1, m_bottom_y, total_frame_rect.width() - (total_frame_rect.right() - window_rect.right()), window_rect.height() }, 1.0, false);
  365. } else {
  366. m_right_x = 0;
  367. }
  368. m_shadow_dirty = false;
  369. }
  370. void WindowFrame::set_opacity(float opacity)
  371. {
  372. if (m_opacity == opacity)
  373. return;
  374. bool was_opaque = is_opaque();
  375. m_opacity = opacity;
  376. if (was_opaque != is_opaque())
  377. Compositor::the().invalidate_occlusions();
  378. Compositor::the().invalidate_screen(render_rect());
  379. WindowManager::the().notify_opacity_changed(m_window);
  380. }
  381. Gfx::IntRect WindowFrame::inflated_for_shadow(const Gfx::IntRect& frame_rect) const
  382. {
  383. if (auto* shadow = window_shadow()) {
  384. auto total_shadow_size = shadow->height();
  385. return frame_rect.inflated(total_shadow_size, total_shadow_size);
  386. }
  387. return frame_rect;
  388. }
  389. Gfx::Bitmap* WindowFrame::inflate_for_shadow(Gfx::IntRect& frame_rect, Gfx::IntPoint& shadow_offset) const
  390. {
  391. auto* shadow = window_shadow();
  392. if (shadow) {
  393. auto total_shadow_size = shadow->height();
  394. frame_rect.inflate(total_shadow_size, total_shadow_size);
  395. auto offset = total_shadow_size / 2;
  396. shadow_offset = { offset, offset };
  397. } else {
  398. shadow_offset = {};
  399. }
  400. return shadow;
  401. }
  402. Gfx::IntRect WindowFrame::rect() const
  403. {
  404. return frame_rect_for_window(m_window, m_window.rect());
  405. }
  406. Gfx::IntRect WindowFrame::render_rect() const
  407. {
  408. return inflated_for_shadow(rect());
  409. }
  410. void WindowFrame::invalidate_title_bar()
  411. {
  412. invalidate(title_bar_rect());
  413. }
  414. void WindowFrame::invalidate(Gfx::IntRect relative_rect)
  415. {
  416. auto frame_rect = rect();
  417. auto window_rect = m_window.rect();
  418. relative_rect.move_by(frame_rect.x() - window_rect.x(), frame_rect.y() - window_rect.y());
  419. m_dirty = true;
  420. m_window.invalidate(relative_rect, true);
  421. }
  422. void WindowFrame::notify_window_rect_changed(const Gfx::IntRect& old_rect, const Gfx::IntRect& new_rect)
  423. {
  424. layout_buttons();
  425. auto old_frame_rect = inflated_for_shadow(frame_rect_for_window(m_window, old_rect));
  426. auto new_frame_rect = inflated_for_shadow(frame_rect_for_window(m_window, new_rect));
  427. if (old_frame_rect.size() != new_frame_rect.size())
  428. m_dirty = m_shadow_dirty = true;
  429. auto& compositor = Compositor::the();
  430. for (auto& dirty : old_frame_rect.shatter(new_frame_rect))
  431. compositor.invalidate_screen(dirty);
  432. if (!m_window.is_opaque())
  433. compositor.invalidate_screen(new_frame_rect);
  434. compositor.invalidate_occlusions();
  435. WindowManager::the().notify_rect_changed(m_window, old_rect, new_rect);
  436. }
  437. void WindowFrame::layout_buttons()
  438. {
  439. auto button_rects = Gfx::WindowTheme::current().layout_buttons(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette(), m_buttons.size());
  440. for (size_t i = 0; i < m_buttons.size(); i++)
  441. m_buttons[i].set_relative_rect(button_rects[i]);
  442. }
  443. void WindowFrame::on_mouse_event(const MouseEvent& event)
  444. {
  445. ASSERT(!m_window.is_fullscreen());
  446. auto& wm = WindowManager::the();
  447. if (m_window.type() != WindowType::Normal && m_window.type() != WindowType::Notification)
  448. return;
  449. if (m_window.type() == WindowType::Normal) {
  450. if (event.type() == Event::MouseDown)
  451. wm.move_to_front_and_make_active(m_window);
  452. if (m_window.blocking_modal_window())
  453. return;
  454. if (title_bar_icon_rect().contains(event.position())) {
  455. if (event.type() == Event::MouseDown && (event.button() == MouseButton::Left || event.button() == MouseButton::Right)) {
  456. // Manually start a potential double click. Since we're opening
  457. // a menu, we will only receive the MouseDown event, so we
  458. // need to record that fact. If the user subsequently clicks
  459. // on the same area, the menu will get closed, and we will
  460. // receive a MouseUp event, but because windows have changed
  461. // we don't get a MouseDoubleClick event. We can however record
  462. // this click, and when we receive the MouseUp event check if
  463. // it would have been considered a double click, if it weren't
  464. // for the fact that we opened and closed a window in the meanwhile
  465. auto& wm = WindowManager::the();
  466. wm.start_menu_doubleclick(m_window, event);
  467. m_window.popup_window_menu(title_bar_rect().bottom_left().translated(rect().location()), WindowMenuDefaultAction::Close);
  468. return;
  469. } else if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) {
  470. // Since the MouseDown event opened a menu, another MouseUp
  471. // from the second click outside the menu wouldn't be considered
  472. // a double click, so let's manually check if it would otherwise
  473. // have been be considered to be one
  474. auto& wm = WindowManager::the();
  475. if (wm.is_menu_doubleclick(m_window, event)) {
  476. // It is a double click, so perform activate the default item
  477. m_window.window_menu_activate_default();
  478. }
  479. return;
  480. }
  481. }
  482. }
  483. // This is slightly hackish, but expand the title bar rect by two pixels downwards,
  484. // so that mouse events between the title bar and window contents don't act like
  485. // mouse events on the border.
  486. auto adjusted_title_bar_rect = title_bar_rect();
  487. adjusted_title_bar_rect.set_height(adjusted_title_bar_rect.height() + 2);
  488. if (adjusted_title_bar_rect.contains(event.position())) {
  489. wm.clear_resize_candidate();
  490. if (event.type() == Event::MouseDown)
  491. wm.move_to_front_and_make_active(m_window);
  492. for (auto& button : m_buttons) {
  493. if (button.relative_rect().contains(event.position()))
  494. return button.on_mouse_event(event.translated(-button.relative_rect().location()));
  495. }
  496. if (event.type() == Event::MouseDown) {
  497. if (m_window.type() == WindowType::Normal && event.button() == MouseButton::Right) {
  498. auto default_action = m_window.is_maximized() ? WindowMenuDefaultAction::Restore : WindowMenuDefaultAction::Maximize;
  499. m_window.popup_window_menu(event.position().translated(rect().location()), default_action);
  500. return;
  501. }
  502. if (m_window.is_movable() && event.button() == MouseButton::Left)
  503. wm.start_window_move(m_window, event.translated(rect().location()));
  504. }
  505. return;
  506. }
  507. if (m_window.is_resizable() && event.type() == Event::MouseMove && event.buttons() == 0) {
  508. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  509. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  510. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  511. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  512. };
  513. Gfx::IntRect outer_rect = { {}, rect().size() };
  514. ASSERT(outer_rect.contains(event.position()));
  515. int window_relative_x = event.x() - outer_rect.x();
  516. int window_relative_y = event.y() - outer_rect.y();
  517. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  518. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  519. wm.set_resize_candidate(m_window, direction_for_hot_area[hot_area_row][hot_area_column]);
  520. Compositor::the().invalidate_cursor();
  521. return;
  522. }
  523. if (m_window.is_resizable() && event.type() == Event::MouseDown && event.button() == MouseButton::Left)
  524. wm.start_window_resize(m_window, event.translated(rect().location()));
  525. }
  526. void WindowFrame::start_flash_animation()
  527. {
  528. if (!m_flash_timer) {
  529. m_flash_timer = Core::Timer::construct(100, [this] {
  530. ASSERT(m_flash_counter);
  531. invalidate_title_bar();
  532. if (!--m_flash_counter)
  533. m_flash_timer->stop();
  534. });
  535. }
  536. m_flash_counter = 8;
  537. m_flash_timer->start();
  538. }
  539. void WindowFrame::paint_simple_rect_shadow(Gfx::Painter& painter, const Gfx::IntRect& containing_rect, const Gfx::Bitmap& shadow_bitmap) const
  540. {
  541. // The layout of the shadow_bitmap is defined like this:
  542. // +---------+----+---------+----+----+----+
  543. // | TL | T | TR | LT | L | LB |
  544. // +---------+----+---------+----+----+----+
  545. // | BL | B | BR | RT | R | RB |
  546. // +---------+----+---------+----+----+----+
  547. // Located strictly on the top or bottom of the rectangle, above or below of the content:
  548. // TL = top-left T = top TR = top-right
  549. // BL = bottom-left B = bottom BR = bottom-right
  550. // Located on the left or right of the rectangle, but not above or below of the content:
  551. // LT = left-top L = left LB = left-bottom
  552. // RT = right-top R = right RB = right-bottom
  553. // So, the bitmap has two rows and 6 column, two of which are twice as wide.
  554. // The height divided by two defines a cell size, and width of each
  555. // column must be the same as the height of the cell, except for the
  556. // first an third column, which are twice as wide.
  557. if (shadow_bitmap.height() % 2 != 0) {
  558. dbgln("Can't paint simple rect shadow, shadow bitmap height {} is not even", shadow_bitmap.height());
  559. return;
  560. }
  561. auto base_size = shadow_bitmap.height() / 2;
  562. if (shadow_bitmap.width() != base_size * (6 + 2)) {
  563. if (shadow_bitmap.width() % base_size != 0)
  564. dbgln("Can't paint simple rect shadow, shadow bitmap width {} is not a multiple of {}", shadow_bitmap.width(), base_size);
  565. else
  566. dbgln("Can't paint simple rect shadow, shadow bitmap width {} but expected {}", shadow_bitmap.width(), base_size * (6 + 2));
  567. return;
  568. }
  569. // The containing_rect should have been inflated appropriately
  570. ASSERT(containing_rect.size().contains(Gfx::IntSize { base_size, base_size }));
  571. auto half_width = containing_rect.width() / 2;
  572. auto paint_horizontal = [&](int y, int src_row) {
  573. Gfx::PainterStateSaver save(painter);
  574. painter.add_clip_rect({ containing_rect.left(), y, containing_rect.width(), base_size });
  575. int corner_piece_width = base_size * 2;
  576. int left_corners_right = min(half_width, corner_piece_width);
  577. int right_corners_left = max(half_width, containing_rect.width() - corner_piece_width);
  578. painter.blit({ containing_rect.left() + left_corners_right - corner_piece_width, y }, shadow_bitmap, { 0, src_row * base_size, corner_piece_width, base_size });
  579. painter.blit({ containing_rect.left() + right_corners_left, y }, shadow_bitmap, { corner_piece_width + base_size, src_row * base_size, corner_piece_width, base_size });
  580. for (int x = left_corners_right; x < right_corners_left; x += base_size) {
  581. auto width = min(right_corners_left - x, base_size);
  582. painter.blit({ containing_rect.left() + x, y }, shadow_bitmap, { corner_piece_width, src_row * base_size, width, base_size });
  583. }
  584. };
  585. paint_horizontal(containing_rect.top(), 0);
  586. paint_horizontal(containing_rect.bottom() - base_size + 1, 1);
  587. auto sides_height = containing_rect.height() - 2 * base_size;
  588. auto half_height = sides_height / 2;
  589. auto paint_vertical = [&](int x, int src_row) {
  590. Gfx::PainterStateSaver save(painter);
  591. painter.add_clip_rect({ x, containing_rect.y() + base_size, base_size, containing_rect.height() - 2 * base_size });
  592. int top_corners_bottom = base_size + min(half_height, base_size);
  593. int top_corner_height = top_corners_bottom - base_size;
  594. int bottom_corners_top = base_size + max(half_height, sides_height - base_size);
  595. int bottom_corner_height = sides_height + base_size - bottom_corners_top;
  596. painter.blit({ x, containing_rect.top() + top_corners_bottom - top_corner_height }, shadow_bitmap, { base_size * 5, src_row * base_size, base_size, top_corner_height });
  597. painter.blit({ x, containing_rect.top() + bottom_corners_top }, shadow_bitmap, { base_size * 7, src_row * base_size + base_size - bottom_corner_height, base_size, bottom_corner_height });
  598. if (sides_height > 2 * base_size) {
  599. for (int y = top_corners_bottom; y < bottom_corners_top; y += base_size) {
  600. auto height = min(bottom_corners_top - y, base_size);
  601. painter.blit({ x, containing_rect.top() + y }, shadow_bitmap, { base_size * 6, src_row * base_size, base_size, height });
  602. }
  603. }
  604. };
  605. paint_vertical(containing_rect.left(), 0);
  606. paint_vertical(containing_rect.right() - base_size + 1, 1);
  607. }
  608. }