WindowFrame.cpp 38 KB

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