WindowFrame.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ClientConnection.h"
  7. #include <AK/Badge.h>
  8. #include <LibGfx/Font.h>
  9. #include <LibGfx/Painter.h>
  10. #include <LibGfx/StylePainter.h>
  11. #include <LibGfx/WindowTheme.h>
  12. #include <WindowServer/Button.h>
  13. #include <WindowServer/Compositor.h>
  14. #include <WindowServer/Event.h>
  15. #include <WindowServer/Screen.h>
  16. #include <WindowServer/Window.h>
  17. #include <WindowServer/WindowFrame.h>
  18. #include <WindowServer/WindowManager.h>
  19. namespace WindowServer {
  20. static Gfx::WindowTheme::WindowType to_theme_window_type(WindowType type)
  21. {
  22. switch (type) {
  23. case WindowType::Normal:
  24. return Gfx::WindowTheme::WindowType::Normal;
  25. case WindowType::ToolWindow:
  26. return Gfx::WindowTheme::WindowType::ToolWindow;
  27. case WindowType::Notification:
  28. return Gfx::WindowTheme::WindowType::Notification;
  29. default:
  30. return Gfx::WindowTheme::WindowType::Other;
  31. }
  32. }
  33. static Gfx::Bitmap* s_minimize_icon;
  34. static Gfx::Bitmap* s_maximize_icon;
  35. static Gfx::Bitmap* s_restore_icon;
  36. static Gfx::Bitmap* s_close_icon;
  37. static String s_last_title_button_icons_path;
  38. static int s_last_title_button_icons_scale;
  39. static Gfx::Bitmap* s_active_window_shadow;
  40. static Gfx::Bitmap* s_inactive_window_shadow;
  41. static Gfx::Bitmap* s_menu_shadow;
  42. static Gfx::Bitmap* s_taskbar_shadow;
  43. static Gfx::Bitmap* s_tooltip_shadow;
  44. static String s_last_active_window_shadow_path;
  45. static String s_last_inactive_window_shadow_path;
  46. static String s_last_menu_shadow_path;
  47. static String s_last_taskbar_shadow_path;
  48. static String s_last_tooltip_shadow_path;
  49. static Gfx::IntRect frame_rect_for_window(Window& window, const Gfx::IntRect& rect)
  50. {
  51. if (window.is_frameless())
  52. return rect;
  53. int menu_row_count = (window.menubar() && window.should_show_menubar()) ? 1 : 0;
  54. return Gfx::WindowTheme::current().frame_rect_for_window(to_theme_window_type(window.type()), rect, WindowManager::the().palette(), menu_row_count);
  55. }
  56. WindowFrame::WindowFrame(Window& window)
  57. : m_window(window)
  58. {
  59. auto button = make<Button>(*this, [this](auto&) {
  60. m_window.handle_window_menu_action(WindowMenuAction::Close);
  61. });
  62. m_close_button = button.ptr();
  63. m_buttons.append(move(button));
  64. if (window.is_resizable()) {
  65. auto button = make<Button>(*this, [this](auto&) {
  66. m_window.handle_window_menu_action(WindowMenuAction::MaximizeOrRestore);
  67. });
  68. button->on_middle_click = [&](auto&) {
  69. m_window.set_vertically_maximized();
  70. };
  71. m_maximize_button = button.ptr();
  72. m_buttons.append(move(button));
  73. }
  74. if (window.is_minimizable()) {
  75. auto button = make<Button>(*this, [this](auto&) {
  76. m_window.handle_window_menu_action(WindowMenuAction::MinimizeOrUnminimize);
  77. });
  78. m_minimize_button = button.ptr();
  79. m_buttons.append(move(button));
  80. }
  81. set_button_icons();
  82. }
  83. WindowFrame::~WindowFrame()
  84. {
  85. }
  86. void WindowFrame::set_button_icons()
  87. {
  88. m_dirty = true;
  89. if (m_window.is_frameless())
  90. return;
  91. m_close_button->set_icon(*s_close_icon);
  92. if (m_window.is_minimizable())
  93. m_minimize_button->set_icon(*s_minimize_icon);
  94. if (m_window.is_resizable())
  95. m_maximize_button->set_icon(m_window.is_maximized() ? *s_restore_icon : *s_maximize_icon);
  96. }
  97. void WindowFrame::reload_config()
  98. {
  99. String icons_path = WindowManager::the().palette().title_button_icons_path();
  100. int icons_scale = WindowManager::the().compositor_icon_scale();
  101. StringBuilder full_path;
  102. if (!s_minimize_icon || s_last_title_button_icons_path != icons_path || s_last_title_button_icons_scale != icons_scale) {
  103. full_path.append(icons_path);
  104. full_path.append("window-minimize.png");
  105. if (s_minimize_icon)
  106. s_minimize_icon->unref();
  107. if (!(s_minimize_icon = Gfx::Bitmap::load_from_file(full_path.to_string(), icons_scale).leak_ref()))
  108. s_minimize_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png", icons_scale).leak_ref();
  109. full_path.clear();
  110. }
  111. if (!s_maximize_icon || s_last_title_button_icons_path != icons_path || s_last_title_button_icons_scale != icons_scale) {
  112. full_path.append(icons_path);
  113. full_path.append("window-maximize.png");
  114. if (s_maximize_icon)
  115. s_maximize_icon->unref();
  116. if (!(s_maximize_icon = Gfx::Bitmap::load_from_file(full_path.to_string(), icons_scale).leak_ref()))
  117. s_maximize_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/upward-triangle.png", icons_scale).leak_ref();
  118. full_path.clear();
  119. }
  120. if (!s_restore_icon || s_last_title_button_icons_path != icons_path || s_last_title_button_icons_scale != icons_scale) {
  121. full_path.append(icons_path);
  122. full_path.append("window-restore.png");
  123. if (s_restore_icon)
  124. s_restore_icon->unref();
  125. if (!(s_restore_icon = Gfx::Bitmap::load_from_file(full_path.to_string(), icons_scale).leak_ref()))
  126. s_restore_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-restore.png", icons_scale).leak_ref();
  127. full_path.clear();
  128. }
  129. if (!s_close_icon || s_last_title_button_icons_path != icons_path || s_last_title_button_icons_scale != icons_scale) {
  130. full_path.append(icons_path);
  131. full_path.append("window-close.png");
  132. if (s_close_icon)
  133. s_close_icon->unref();
  134. if (!(s_close_icon = Gfx::Bitmap::load_from_file(full_path.to_string(), icons_scale).leak_ref()))
  135. s_close_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-close.png", icons_scale).leak_ref();
  136. full_path.clear();
  137. }
  138. s_last_title_button_icons_path = icons_path;
  139. s_last_title_button_icons_scale = icons_scale;
  140. auto load_shadow = [](const String& path, String& last_path, Gfx::Bitmap*& shadow_bitmap) {
  141. if (path.is_empty()) {
  142. last_path = String::empty();
  143. if (shadow_bitmap) {
  144. shadow_bitmap->unref();
  145. shadow_bitmap = nullptr;
  146. }
  147. } else if (!shadow_bitmap || shadow_bitmap->scale() != s_last_title_button_icons_scale || last_path != path) {
  148. if (shadow_bitmap)
  149. shadow_bitmap->unref();
  150. shadow_bitmap = Gfx::Bitmap::load_from_file(path, s_last_title_button_icons_scale).leak_ref();
  151. if (shadow_bitmap)
  152. last_path = path;
  153. else
  154. last_path = String::empty();
  155. }
  156. };
  157. load_shadow(WindowManager::the().palette().active_window_shadow_path(), s_last_active_window_shadow_path, s_active_window_shadow);
  158. load_shadow(WindowManager::the().palette().inactive_window_shadow_path(), s_last_inactive_window_shadow_path, s_inactive_window_shadow);
  159. load_shadow(WindowManager::the().palette().menu_shadow_path(), s_last_menu_shadow_path, s_menu_shadow);
  160. load_shadow(WindowManager::the().palette().taskbar_shadow_path(), s_last_taskbar_shadow_path, s_taskbar_shadow);
  161. load_shadow(WindowManager::the().palette().tooltip_shadow_path(), s_last_tooltip_shadow_path, s_tooltip_shadow);
  162. }
  163. Gfx::Bitmap* WindowFrame::window_shadow() const
  164. {
  165. if (m_window.is_frameless())
  166. return nullptr;
  167. switch (m_window.type()) {
  168. case WindowType::Desktop:
  169. return nullptr;
  170. case WindowType::Menu:
  171. return s_menu_shadow;
  172. case WindowType::Tooltip:
  173. return s_tooltip_shadow;
  174. case WindowType::Taskbar:
  175. return s_taskbar_shadow;
  176. case WindowType::AppletArea:
  177. return nullptr;
  178. default:
  179. if (auto* highlight_window = WindowManager::the().highlight_window())
  180. return highlight_window == &m_window ? s_active_window_shadow : s_inactive_window_shadow;
  181. return m_window.is_active() ? s_active_window_shadow : s_inactive_window_shadow;
  182. }
  183. }
  184. bool WindowFrame::frame_has_alpha() const
  185. {
  186. if (m_has_alpha_channel)
  187. return true;
  188. if (auto* shadow_bitmap = window_shadow(); shadow_bitmap && shadow_bitmap->format() == Gfx::BitmapFormat::BGRA8888)
  189. return true;
  190. return false;
  191. }
  192. void WindowFrame::did_set_maximized(Badge<Window>, bool maximized)
  193. {
  194. VERIFY(m_maximize_button);
  195. m_maximize_button->set_icon(maximized ? *s_restore_icon : *s_maximize_icon);
  196. }
  197. Gfx::IntRect WindowFrame::menubar_rect() const
  198. {
  199. if (!m_window.menubar() || !m_window.should_show_menubar())
  200. return {};
  201. return Gfx::WindowTheme::current().menubar_rect(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette(), menu_row_count());
  202. }
  203. Gfx::IntRect WindowFrame::titlebar_rect() const
  204. {
  205. return Gfx::WindowTheme::current().titlebar_rect(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette());
  206. }
  207. Gfx::IntRect WindowFrame::titlebar_icon_rect() const
  208. {
  209. return Gfx::WindowTheme::current().titlebar_icon_rect(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette());
  210. }
  211. Gfx::IntRect WindowFrame::titlebar_text_rect() const
  212. {
  213. return Gfx::WindowTheme::current().titlebar_text_rect(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette());
  214. }
  215. Gfx::WindowTheme::WindowState WindowFrame::window_state_for_theme() const
  216. {
  217. auto& wm = WindowManager::the();
  218. if (m_flash_timer && m_flash_timer->is_active())
  219. return m_flash_counter & 1 ? Gfx::WindowTheme::WindowState::Active : Gfx::WindowTheme::WindowState::Inactive;
  220. if (&m_window == wm.m_highlight_window)
  221. return Gfx::WindowTheme::WindowState::Highlighted;
  222. if (&m_window == wm.m_move_window)
  223. return Gfx::WindowTheme::WindowState::Moving;
  224. if (wm.is_active_window_or_accessory(m_window))
  225. return Gfx::WindowTheme::WindowState::Active;
  226. return Gfx::WindowTheme::WindowState::Inactive;
  227. }
  228. void WindowFrame::paint_notification_frame(Gfx::Painter& painter)
  229. {
  230. auto palette = WindowManager::the().palette();
  231. Gfx::WindowTheme::current().paint_notification_frame(painter, m_window.rect(), palette, m_buttons.last().relative_rect());
  232. }
  233. String WindowFrame::compute_title_text() const
  234. {
  235. if (m_window.client() && m_window.client()->is_unresponsive())
  236. return String::formatted("{} (Not responding)", m_window.title());
  237. return m_window.title();
  238. }
  239. void WindowFrame::paint_tool_window_frame(Gfx::Painter& painter)
  240. {
  241. auto palette = WindowManager::the().palette();
  242. auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::IntRect() : m_buttons.last().relative_rect();
  243. Gfx::WindowTheme::current().paint_tool_window_frame(painter, window_state_for_theme(), m_window.rect(), compute_title_text(), palette, leftmost_button_rect);
  244. }
  245. void WindowFrame::paint_menubar(Gfx::Painter& painter)
  246. {
  247. auto& wm = WindowManager::the();
  248. auto& font = wm.font();
  249. auto palette = wm.palette();
  250. auto menubar_rect = this->menubar_rect();
  251. painter.fill_rect(menubar_rect, palette.window());
  252. Gfx::PainterStateSaver saver(painter);
  253. painter.add_clip_rect(menubar_rect);
  254. painter.translate(menubar_rect.location());
  255. m_window.menubar()->for_each_menu([&](Menu& menu) {
  256. auto text_rect = menu.rect_in_window_menubar();
  257. Color text_color = palette.window_text();
  258. if (MenuManager::the().is_open(menu))
  259. text_rect.move_by(1, 1);
  260. bool paint_as_pressed = MenuManager::the().is_open(menu);
  261. bool paint_as_hovered = !paint_as_pressed && &menu == MenuManager::the().hovered_menu();
  262. if (paint_as_pressed || paint_as_hovered) {
  263. Gfx::StylePainter::paint_button(painter, menu.rect_in_window_menubar(), palette, Gfx::ButtonStyle::Coolbar, paint_as_pressed, paint_as_hovered);
  264. }
  265. painter.draw_ui_text(text_rect, menu.name(), font, Gfx::TextAlignment::Center, text_color);
  266. return IterationDecision::Continue;
  267. });
  268. }
  269. void WindowFrame::paint_normal_frame(Gfx::Painter& painter)
  270. {
  271. auto palette = WindowManager::the().palette();
  272. auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::IntRect() : m_buttons.last().relative_rect();
  273. Gfx::WindowTheme::current().paint_normal_frame(painter, window_state_for_theme(), m_window.rect(), compute_title_text(), m_window.icon(), palette, leftmost_button_rect, menu_row_count());
  274. if (m_window.menubar() && m_window.should_show_menubar())
  275. paint_menubar(painter);
  276. }
  277. void WindowFrame::paint(Gfx::Painter& painter, const Gfx::IntRect& rect)
  278. {
  279. render_to_cache();
  280. auto frame_rect = render_rect();
  281. auto window_rect = m_window.rect();
  282. if (m_top_bottom) {
  283. auto top_bottom_height = frame_rect.height() - window_rect.height();
  284. if (m_bottom_y > 0) {
  285. // We have a top piece
  286. auto src_rect = rect.intersected({ frame_rect.location(), { frame_rect.width(), m_bottom_y } });
  287. if (!src_rect.is_empty())
  288. painter.blit(src_rect.location(), *m_top_bottom, src_rect.translated(-frame_rect.location()), m_opacity);
  289. }
  290. if (m_bottom_y < top_bottom_height) {
  291. // We have a bottom piece
  292. Gfx::IntRect rect_in_frame { frame_rect.x(), window_rect.bottom() + 1, frame_rect.width(), top_bottom_height - m_bottom_y };
  293. auto src_rect = rect.intersected(rect_in_frame);
  294. if (!src_rect.is_empty())
  295. painter.blit(src_rect.location(), *m_top_bottom, src_rect.translated(-rect_in_frame.x(), -rect_in_frame.y() + m_bottom_y), m_opacity);
  296. }
  297. }
  298. if (m_left_right) {
  299. auto left_right_width = frame_rect.width() - window_rect.width();
  300. if (m_right_x > 0) {
  301. // We have a left piece
  302. Gfx::IntRect rect_in_frame { frame_rect.x(), window_rect.y(), m_right_x, window_rect.height() };
  303. auto src_rect = rect.intersected(rect_in_frame);
  304. if (!src_rect.is_empty())
  305. painter.blit(src_rect.location(), *m_left_right, src_rect.translated(-rect_in_frame.location()), m_opacity);
  306. }
  307. if (m_right_x < left_right_width) {
  308. // We have a right piece
  309. Gfx::IntRect rect_in_frame { window_rect.right() + 1, window_rect.y(), left_right_width - m_right_x, window_rect.height() };
  310. auto src_rect = rect.intersected(rect_in_frame);
  311. if (!src_rect.is_empty())
  312. painter.blit(src_rect.location(), *m_left_right, src_rect.translated(-rect_in_frame.x() + m_right_x, -rect_in_frame.y()), m_opacity);
  313. }
  314. }
  315. }
  316. void WindowFrame::render(Gfx::Painter& painter)
  317. {
  318. if (m_window.is_frameless())
  319. return;
  320. if (m_window.type() == WindowType::Notification)
  321. paint_notification_frame(painter);
  322. else if (m_window.type() == WindowType::Normal)
  323. paint_normal_frame(painter);
  324. else if (m_window.type() == WindowType::ToolWindow)
  325. paint_tool_window_frame(painter);
  326. else
  327. return;
  328. for (auto& button : m_buttons) {
  329. button.paint(painter);
  330. }
  331. }
  332. void WindowFrame::theme_changed()
  333. {
  334. m_dirty = m_shadow_dirty = true;
  335. m_top_bottom = nullptr;
  336. m_left_right = nullptr;
  337. m_bottom_y = m_right_x = 0;
  338. layout_buttons();
  339. set_button_icons();
  340. m_has_alpha_channel = Gfx::WindowTheme::current().frame_uses_alpha(window_state_for_theme(), WindowManager::the().palette());
  341. }
  342. void WindowFrame::render_to_cache()
  343. {
  344. if (!m_dirty)
  345. return;
  346. m_dirty = false;
  347. m_has_alpha_channel = Gfx::WindowTheme::current().frame_uses_alpha(window_state_for_theme(), WindowManager::the().palette());
  348. static Gfx::Bitmap* s_tmp_bitmap;
  349. auto frame_rect = rect();
  350. auto total_frame_rect = frame_rect;
  351. Gfx::Bitmap* shadow_bitmap = inflate_for_shadow(total_frame_rect, m_shadow_offset);
  352. auto window_rect = m_window.rect();
  353. auto scale = Screen::the().scale_factor();
  354. if (!s_tmp_bitmap || !s_tmp_bitmap->size().contains(total_frame_rect.size()) || s_tmp_bitmap->scale() != scale) {
  355. if (s_tmp_bitmap)
  356. s_tmp_bitmap->unref();
  357. s_tmp_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, total_frame_rect.size(), scale).leak_ref();
  358. }
  359. auto top_bottom_height = total_frame_rect.height() - window_rect.height();
  360. auto left_right_width = total_frame_rect.width() - window_rect.width();
  361. if (!m_top_bottom || m_top_bottom->width() != total_frame_rect.width() || m_top_bottom->height() != top_bottom_height || m_top_bottom->scale() != scale) {
  362. if (top_bottom_height > 0)
  363. m_top_bottom = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { total_frame_rect.width(), top_bottom_height }, scale);
  364. else
  365. m_top_bottom = nullptr;
  366. m_shadow_dirty = true;
  367. }
  368. if (!m_left_right || m_left_right->height() != total_frame_rect.height() || m_left_right->width() != left_right_width || m_left_right->scale() != scale) {
  369. if (left_right_width > 0)
  370. m_left_right = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { left_right_width, total_frame_rect.height() }, scale);
  371. else
  372. m_left_right = nullptr;
  373. m_shadow_dirty = true;
  374. }
  375. auto& frame_rect_to_update = m_shadow_dirty ? total_frame_rect : frame_rect;
  376. Gfx::IntPoint update_location(m_shadow_dirty ? Gfx::IntPoint { 0, 0 } : m_shadow_offset);
  377. Gfx::Painter painter(*s_tmp_bitmap);
  378. // Clear the frame area, not including the window content area, which we don't care about
  379. for (auto& rect : frame_rect_to_update.shatter(window_rect))
  380. painter.clear_rect({ rect.location() - frame_rect_to_update.location(), rect.size() }, { 255, 255, 255, 0 });
  381. if (m_shadow_dirty && shadow_bitmap)
  382. paint_simple_rect_shadow(painter, { { 0, 0 }, total_frame_rect.size() }, *shadow_bitmap);
  383. {
  384. Gfx::PainterStateSaver save(painter);
  385. painter.translate(m_shadow_offset);
  386. render(painter);
  387. }
  388. if (m_top_bottom && top_bottom_height > 0) {
  389. m_bottom_y = window_rect.y() - total_frame_rect.y();
  390. VERIFY(m_bottom_y >= 0);
  391. Gfx::Painter top_bottom_painter(*m_top_bottom);
  392. 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()) } });
  393. if (m_bottom_y > 0)
  394. top_bottom_painter.blit({ 0, 0 }, *s_tmp_bitmap, { 0, 0, total_frame_rect.width(), m_bottom_y }, 1.0, false);
  395. if (m_bottom_y < top_bottom_height)
  396. 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);
  397. } else {
  398. m_bottom_y = 0;
  399. }
  400. if (left_right_width > 0) {
  401. m_right_x = window_rect.x() - total_frame_rect.x();
  402. VERIFY(m_right_x >= 0);
  403. Gfx::Painter left_right_painter(*m_left_right);
  404. 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() } });
  405. if (m_right_x > 0)
  406. left_right_painter.blit({ 0, 0 }, *s_tmp_bitmap, { 0, m_bottom_y, m_right_x, window_rect.height() }, 1.0, false);
  407. if (m_right_x < left_right_width)
  408. 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);
  409. } else {
  410. m_right_x = 0;
  411. }
  412. m_shadow_dirty = false;
  413. }
  414. void WindowFrame::set_opacity(float opacity)
  415. {
  416. if (m_opacity == opacity)
  417. return;
  418. bool was_opaque = is_opaque();
  419. m_opacity = opacity;
  420. if (was_opaque != is_opaque())
  421. Compositor::the().invalidate_occlusions();
  422. Compositor::the().invalidate_screen(render_rect());
  423. WindowManager::the().notify_opacity_changed(m_window);
  424. }
  425. Gfx::IntRect WindowFrame::inflated_for_shadow(const Gfx::IntRect& frame_rect) const
  426. {
  427. if (auto* shadow = window_shadow()) {
  428. auto total_shadow_size = shadow->height();
  429. return frame_rect.inflated(total_shadow_size, total_shadow_size);
  430. }
  431. return frame_rect;
  432. }
  433. Gfx::Bitmap* WindowFrame::inflate_for_shadow(Gfx::IntRect& frame_rect, Gfx::IntPoint& shadow_offset) const
  434. {
  435. auto* shadow = window_shadow();
  436. if (shadow) {
  437. auto total_shadow_size = shadow->height();
  438. frame_rect.inflate(total_shadow_size, total_shadow_size);
  439. auto offset = total_shadow_size / 2;
  440. shadow_offset = { offset, offset };
  441. } else {
  442. shadow_offset = {};
  443. }
  444. return shadow;
  445. }
  446. Gfx::IntRect WindowFrame::rect() const
  447. {
  448. return frame_rect_for_window(m_window, m_window.rect());
  449. }
  450. Gfx::IntRect WindowFrame::render_rect() const
  451. {
  452. return inflated_for_shadow(rect());
  453. }
  454. void WindowFrame::invalidate_titlebar()
  455. {
  456. m_dirty = true;
  457. invalidate(titlebar_rect());
  458. }
  459. void WindowFrame::invalidate()
  460. {
  461. auto frame_rect = render_rect();
  462. invalidate(Gfx::IntRect { frame_rect.location() - m_window.position(), frame_rect.size() });
  463. m_window.invalidate(true, true);
  464. }
  465. void WindowFrame::invalidate(Gfx::IntRect relative_rect)
  466. {
  467. auto frame_rect = rect();
  468. auto window_rect = m_window.rect();
  469. relative_rect.move_by(frame_rect.x() - window_rect.x(), frame_rect.y() - window_rect.y());
  470. m_dirty = true;
  471. m_window.invalidate(relative_rect, true);
  472. }
  473. void WindowFrame::notify_window_rect_changed(const Gfx::IntRect& old_rect, const Gfx::IntRect& new_rect)
  474. {
  475. layout_buttons();
  476. auto old_frame_rect = inflated_for_shadow(frame_rect_for_window(m_window, old_rect));
  477. auto new_frame_rect = inflated_for_shadow(frame_rect_for_window(m_window, new_rect));
  478. if (old_frame_rect.size() != new_frame_rect.size())
  479. m_dirty = m_shadow_dirty = true;
  480. auto& compositor = Compositor::the();
  481. for (auto& dirty : old_frame_rect.shatter(new_frame_rect))
  482. compositor.invalidate_screen(dirty);
  483. if (!m_window.is_opaque())
  484. compositor.invalidate_screen(new_frame_rect);
  485. compositor.invalidate_occlusions();
  486. WindowManager::the().notify_rect_changed(m_window, old_rect, new_rect);
  487. }
  488. void WindowFrame::layout_buttons()
  489. {
  490. auto button_rects = Gfx::WindowTheme::current().layout_buttons(to_theme_window_type(m_window.type()), m_window.rect(), WindowManager::the().palette(), m_buttons.size());
  491. for (size_t i = 0; i < m_buttons.size(); i++)
  492. m_buttons[i].set_relative_rect(button_rects[i]);
  493. }
  494. bool WindowFrame::hit_test(const Gfx::IntPoint& point) const
  495. {
  496. if (m_window.is_frameless())
  497. return false;
  498. auto frame_rect = rect();
  499. if (!frame_rect.contains(point))
  500. return false;
  501. auto window_rect = m_window.rect();
  502. if (window_rect.contains(point))
  503. return false;
  504. u8 alpha_threshold = Gfx::WindowTheme::current().frame_alpha_hit_threshold(window_state_for_theme()) * 255;
  505. if (alpha_threshold == 0)
  506. return true;
  507. u8 alpha = 0xff;
  508. auto relative_point = point.translated(-render_rect().location());
  509. if (point.y() < window_rect.y()) {
  510. if (m_top_bottom) {
  511. auto scaled_relative_point = relative_point * m_top_bottom->scale();
  512. if (m_top_bottom->rect().contains(scaled_relative_point))
  513. alpha = m_top_bottom->get_pixel(scaled_relative_point).alpha();
  514. }
  515. } else if (point.y() > window_rect.bottom()) {
  516. if (m_top_bottom) {
  517. Gfx::IntPoint scaled_relative_point { relative_point.x() * m_top_bottom->scale(), m_bottom_y * m_top_bottom->scale() + point.y() - window_rect.bottom() - 1 };
  518. if (m_top_bottom->rect().contains(scaled_relative_point))
  519. alpha = m_top_bottom->get_pixel(scaled_relative_point).alpha();
  520. }
  521. } else if (point.x() < window_rect.x()) {
  522. if (m_left_right) {
  523. Gfx::IntPoint scaled_relative_point { relative_point.x() * m_left_right->scale(), (relative_point.y() - m_bottom_y) * m_left_right->scale() };
  524. if (m_left_right->rect().contains(scaled_relative_point))
  525. alpha = m_left_right->get_pixel(scaled_relative_point).alpha();
  526. }
  527. } else if (point.x() > window_rect.right()) {
  528. if (m_left_right) {
  529. Gfx::IntPoint scaled_relative_point { m_right_x * m_left_right->scale() + point.x() - window_rect.right() - 1, (relative_point.y() - m_bottom_y) * m_left_right->scale() };
  530. if (m_left_right->rect().contains(scaled_relative_point))
  531. alpha = m_left_right->get_pixel(scaled_relative_point).alpha();
  532. }
  533. } else {
  534. return false;
  535. }
  536. return alpha >= alpha_threshold;
  537. }
  538. void WindowFrame::on_mouse_event(const MouseEvent& event)
  539. {
  540. VERIFY(!m_window.is_fullscreen());
  541. auto& wm = WindowManager::the();
  542. if (m_window.type() != WindowType::Normal && m_window.type() != WindowType::ToolWindow && m_window.type() != WindowType::Notification)
  543. return;
  544. if (m_window.type() == WindowType::Normal || m_window.type() == WindowType::ToolWindow) {
  545. if (event.type() == Event::MouseDown)
  546. wm.move_to_front_and_make_active(m_window);
  547. if (m_window.blocking_modal_window())
  548. return;
  549. if (titlebar_icon_rect().contains(event.position())) {
  550. if (event.type() == Event::MouseDown && (event.button() == MouseButton::Left || event.button() == MouseButton::Right)) {
  551. // Manually start a potential double click. Since we're opening
  552. // a menu, we will only receive the MouseDown event, so we
  553. // need to record that fact. If the user subsequently clicks
  554. // on the same area, the menu will get closed, and we will
  555. // receive a MouseUp event, but because windows have changed
  556. // we don't get a MouseDoubleClick event. We can however record
  557. // this click, and when we receive the MouseUp event check if
  558. // it would have been considered a double click, if it weren't
  559. // for the fact that we opened and closed a window in the meanwhile
  560. auto& wm = WindowManager::the();
  561. wm.start_menu_doubleclick(m_window, event);
  562. m_window.popup_window_menu(titlebar_rect().bottom_left().translated(rect().location()), WindowMenuDefaultAction::Close);
  563. return;
  564. } else if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) {
  565. // Since the MouseDown event opened a menu, another MouseUp
  566. // from the second click outside the menu wouldn't be considered
  567. // a double click, so let's manually check if it would otherwise
  568. // have been be considered to be one
  569. auto& wm = WindowManager::the();
  570. if (wm.is_menu_doubleclick(m_window, event)) {
  571. // It is a double click, so perform activate the default item
  572. m_window.window_menu_activate_default();
  573. }
  574. return;
  575. }
  576. }
  577. }
  578. // This is slightly hackish, but expand the title bar rect by two pixels downwards,
  579. // so that mouse events between the title bar and window contents don't act like
  580. // mouse events on the border.
  581. auto adjusted_titlebar_rect = titlebar_rect();
  582. adjusted_titlebar_rect.set_height(adjusted_titlebar_rect.height() + 2);
  583. if (adjusted_titlebar_rect.contains(event.position())) {
  584. wm.clear_resize_candidate();
  585. if (event.type() == Event::MouseDown)
  586. wm.move_to_front_and_make_active(m_window);
  587. for (auto& button : m_buttons) {
  588. if (button.relative_rect().contains(event.position()))
  589. return button.on_mouse_event(event.translated(-button.relative_rect().location()));
  590. }
  591. if (event.type() == Event::MouseDown) {
  592. if ((m_window.type() == WindowType::Normal || m_window.type() == WindowType::ToolWindow) && event.button() == MouseButton::Right) {
  593. auto default_action = m_window.is_maximized() ? WindowMenuDefaultAction::Restore : WindowMenuDefaultAction::Maximize;
  594. m_window.popup_window_menu(event.position().translated(rect().location()), default_action);
  595. return;
  596. }
  597. if (m_window.is_movable() && event.button() == MouseButton::Left)
  598. wm.start_window_move(m_window, event.translated(rect().location()));
  599. }
  600. return;
  601. }
  602. auto menubar_rect = this->menubar_rect();
  603. if (menubar_rect.contains(event.position())) {
  604. wm.clear_resize_candidate();
  605. handle_menubar_mouse_event(event);
  606. return;
  607. }
  608. if (m_window.is_resizable() && event.type() == Event::MouseMove && event.buttons() == 0) {
  609. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  610. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  611. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  612. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  613. };
  614. Gfx::IntRect outer_rect = { {}, rect().size() };
  615. VERIFY(outer_rect.contains(event.position()));
  616. int window_relative_x = event.x() - outer_rect.x();
  617. int window_relative_y = event.y() - outer_rect.y();
  618. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  619. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  620. wm.set_resize_candidate(m_window, direction_for_hot_area[hot_area_row][hot_area_column]);
  621. Compositor::the().invalidate_cursor();
  622. return;
  623. }
  624. if (m_window.is_resizable() && event.type() == Event::MouseDown && event.button() == MouseButton::Left)
  625. wm.start_window_resize(m_window, event.translated(rect().location()));
  626. }
  627. void WindowFrame::handle_menubar_mouse_event(const MouseEvent& event)
  628. {
  629. Menu* hovered_menu = nullptr;
  630. auto menubar_rect = this->menubar_rect();
  631. auto adjusted_position = event.position().translated(-menubar_rect.location());
  632. m_window.menubar()->for_each_menu([&](Menu& menu) {
  633. if (menu.rect_in_window_menubar().contains(adjusted_position)) {
  634. hovered_menu = &menu;
  635. handle_menu_mouse_event(menu, event);
  636. return IterationDecision::Break;
  637. }
  638. return IterationDecision::Continue;
  639. });
  640. if (!hovered_menu && event.type() == Event::Type::MouseDown)
  641. MenuManager::the().close_everyone();
  642. if (hovered_menu != MenuManager::the().hovered_menu()) {
  643. MenuManager::the().set_hovered_menu(hovered_menu);
  644. invalidate(menubar_rect);
  645. }
  646. }
  647. void WindowFrame::open_menubar_menu(Menu& menu)
  648. {
  649. auto menubar_rect = this->menubar_rect();
  650. MenuManager::the().close_everyone();
  651. menu.ensure_menu_window().move_to(menu.rect_in_window_menubar().bottom_left().translated(rect().location()).translated(menubar_rect.location()));
  652. MenuManager::the().open_menu(menu);
  653. WindowManager::the().set_window_with_active_menu(&m_window);
  654. invalidate(menubar_rect);
  655. }
  656. void WindowFrame::handle_menu_mouse_event(Menu& menu, const MouseEvent& event)
  657. {
  658. auto menubar_rect = this->menubar_rect();
  659. bool is_hover_with_any_menu_open = event.type() == MouseEvent::MouseMove && &m_window == WindowManager::the().window_with_active_menu();
  660. bool is_mousedown_with_left_button = event.type() == MouseEvent::MouseDown && event.button() == MouseButton::Left;
  661. bool should_open_menu = &menu != MenuManager::the().current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  662. bool should_close_menu = &menu == MenuManager::the().current_menu() && is_mousedown_with_left_button;
  663. if (should_open_menu) {
  664. open_menubar_menu(menu);
  665. return;
  666. }
  667. if (should_close_menu) {
  668. invalidate(menubar_rect);
  669. MenuManager::the().close_everyone();
  670. }
  671. }
  672. void WindowFrame::start_flash_animation()
  673. {
  674. if (!m_flash_timer) {
  675. m_flash_timer = Core::Timer::construct(100, [this] {
  676. VERIFY(m_flash_counter);
  677. invalidate_titlebar();
  678. if (!--m_flash_counter)
  679. m_flash_timer->stop();
  680. });
  681. }
  682. m_flash_counter = 8;
  683. m_flash_timer->start();
  684. }
  685. void WindowFrame::paint_simple_rect_shadow(Gfx::Painter& painter, const Gfx::IntRect& containing_rect, const Gfx::Bitmap& shadow_bitmap) const
  686. {
  687. // The layout of the shadow_bitmap is defined like this:
  688. // +---------+----+---------+----+----+----+
  689. // | TL | T | TR | LT | L | LB |
  690. // +---------+----+---------+----+----+----+
  691. // | BL | B | BR | RT | R | RB |
  692. // +---------+----+---------+----+----+----+
  693. // Located strictly on the top or bottom of the rectangle, above or below of the content:
  694. // TL = top-left T = top TR = top-right
  695. // BL = bottom-left B = bottom BR = bottom-right
  696. // Located on the left or right of the rectangle, but not above or below of the content:
  697. // LT = left-top L = left LB = left-bottom
  698. // RT = right-top R = right RB = right-bottom
  699. // So, the bitmap has two rows and 6 column, two of which are twice as wide.
  700. // The height divided by two defines a cell size, and width of each
  701. // column must be the same as the height of the cell, except for the
  702. // first an third column, which are twice as wide.
  703. if (shadow_bitmap.height() % 2 != 0) {
  704. dbgln("Can't paint simple rect shadow, shadow bitmap height {} is not even", shadow_bitmap.height());
  705. return;
  706. }
  707. auto base_size = shadow_bitmap.height() / 2;
  708. if (shadow_bitmap.width() != base_size * (6 + 2)) {
  709. if (shadow_bitmap.width() % base_size != 0)
  710. dbgln("Can't paint simple rect shadow, shadow bitmap width {} is not a multiple of {}", shadow_bitmap.width(), base_size);
  711. else
  712. dbgln("Can't paint simple rect shadow, shadow bitmap width {} but expected {}", shadow_bitmap.width(), base_size * (6 + 2));
  713. return;
  714. }
  715. // The containing_rect should have been inflated appropriately
  716. VERIFY(containing_rect.size().contains(Gfx::IntSize { base_size, base_size }));
  717. auto sides_height = containing_rect.height() - 2 * base_size;
  718. auto half_height = sides_height / 2;
  719. auto containing_horizontal_rect = containing_rect;
  720. int horizontal_shift = 0;
  721. if (half_height < base_size) {
  722. // If the height is too small we need to shift the left/right accordingly
  723. horizontal_shift = base_size - half_height;
  724. containing_horizontal_rect.set_left(containing_horizontal_rect.left() + horizontal_shift);
  725. containing_horizontal_rect.set_right(containing_horizontal_rect.right() - 2 * horizontal_shift);
  726. }
  727. auto half_width = containing_horizontal_rect.width() / 2;
  728. auto paint_horizontal = [&](int y, int src_row) {
  729. if (half_width <= 0)
  730. return;
  731. Gfx::PainterStateSaver save(painter);
  732. painter.add_clip_rect({ containing_horizontal_rect.left(), y, containing_horizontal_rect.width(), base_size });
  733. int corner_piece_width = min(containing_horizontal_rect.width() / 2, base_size * 2);
  734. int left_corners_right = containing_horizontal_rect.left() + corner_piece_width;
  735. int right_corners_left = max(containing_horizontal_rect.right() - corner_piece_width + 1, left_corners_right + 1);
  736. painter.blit({ containing_horizontal_rect.left(), y }, shadow_bitmap, { 0, src_row * base_size, corner_piece_width, base_size });
  737. painter.blit({ right_corners_left, y }, shadow_bitmap, { 5 * base_size - corner_piece_width, src_row * base_size, corner_piece_width, base_size });
  738. for (int x = left_corners_right; x < right_corners_left; x += base_size) {
  739. auto width = min(right_corners_left - x, base_size);
  740. painter.blit({ x, y }, shadow_bitmap, { corner_piece_width, src_row * base_size, width, base_size });
  741. }
  742. };
  743. paint_horizontal(containing_rect.top(), 0);
  744. paint_horizontal(containing_rect.bottom() - base_size + 1, 1);
  745. auto paint_vertical = [&](int x, int src_row, int hshift, int hsrcshift) {
  746. Gfx::PainterStateSaver save(painter);
  747. painter.add_clip_rect({ x, containing_rect.y() + base_size, base_size, containing_rect.height() - 2 * base_size });
  748. int corner_piece_height = min(half_height, base_size);
  749. int top_corners_bottom = base_size + corner_piece_height;
  750. int bottom_corners_top = base_size + max(half_height, sides_height - corner_piece_height);
  751. painter.blit({ x + hshift, containing_rect.top() + top_corners_bottom - corner_piece_height }, shadow_bitmap, { base_size * 5 + hsrcshift, src_row * base_size, base_size - hsrcshift, corner_piece_height });
  752. painter.blit({ x + hshift, containing_rect.top() + bottom_corners_top }, shadow_bitmap, { base_size * 7 + hsrcshift, src_row * base_size + base_size - corner_piece_height, base_size - hsrcshift, corner_piece_height });
  753. for (int y = top_corners_bottom; y < bottom_corners_top; y += base_size) {
  754. auto height = min(bottom_corners_top - y, base_size);
  755. painter.blit({ x, containing_rect.top() + y }, shadow_bitmap, { base_size * 6, src_row * base_size, base_size, height });
  756. }
  757. };
  758. paint_vertical(containing_rect.left(), 0, horizontal_shift, 0);
  759. paint_vertical(containing_rect.right() - base_size + 1, 1, 0, horizontal_shift);
  760. }
  761. int WindowFrame::menu_row_count() const
  762. {
  763. if (!m_window.should_show_menubar())
  764. return 0;
  765. return m_window.menubar() ? 1 : 0;
  766. }
  767. }