WindowFrame.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ConnectionFromClient.h"
  7. #include <AK/Badge.h>
  8. #include <LibGfx/Font/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/MultiScaleBitmaps.h>
  16. #include <WindowServer/Screen.h>
  17. #include <WindowServer/Window.h>
  18. #include <WindowServer/WindowFrame.h>
  19. #include <WindowServer/WindowManager.h>
  20. namespace WindowServer {
  21. static Gfx::WindowTheme::WindowType to_theme_window_type(WindowType type)
  22. {
  23. switch (type) {
  24. case WindowType::Normal:
  25. return Gfx::WindowTheme::WindowType::Normal;
  26. case WindowType::Notification:
  27. return Gfx::WindowTheme::WindowType::Notification;
  28. default:
  29. return Gfx::WindowTheme::WindowType::Other;
  30. }
  31. }
  32. static Gfx::WindowTheme::WindowMode to_theme_window_mode(WindowMode mode)
  33. {
  34. switch (mode) {
  35. case WindowMode::RenderAbove:
  36. return Gfx::WindowTheme::WindowMode::RenderAbove;
  37. default:
  38. return Gfx::WindowTheme::WindowMode::Other;
  39. }
  40. }
  41. static Button::Icon s_minimize_icon;
  42. static Button::Icon s_maximize_icon;
  43. static Button::Icon s_restore_icon;
  44. static Button::Icon s_close_icon;
  45. static Button::Icon s_close_modified_icon;
  46. static RefPtr<MultiScaleBitmaps> s_active_window_shadow;
  47. static RefPtr<MultiScaleBitmaps> s_inactive_window_shadow;
  48. static RefPtr<MultiScaleBitmaps> s_menu_shadow;
  49. static RefPtr<MultiScaleBitmaps> s_taskbar_shadow;
  50. static RefPtr<MultiScaleBitmaps> s_tooltip_shadow;
  51. static String s_last_active_window_shadow_path;
  52. static String s_last_inactive_window_shadow_path;
  53. static String s_last_menu_shadow_path;
  54. static String s_last_taskbar_shadow_path;
  55. static String s_last_tooltip_shadow_path;
  56. static Gfx::IntRect frame_rect_for_window(Window& window, Gfx::IntRect const& rect)
  57. {
  58. if (window.is_frameless())
  59. return rect;
  60. int menu_row_count = (window.menubar().has_menus() && window.should_show_menubar()) ? 1 : 0;
  61. return Gfx::WindowTheme::current().frame_rect_for_window(to_theme_window_type(window.type()), to_theme_window_mode(window.mode()), rect, WindowManager::the().palette(), menu_row_count);
  62. }
  63. WindowFrame::WindowFrame(Window& window)
  64. : m_window(window)
  65. {
  66. // Because Window constructs a WindowFrame during its construction, we need
  67. // to be careful and defer doing initialization that assumes a fully
  68. // constructed Window. It is fully constructed when Window notifies us with
  69. // a call to WindowFrame::window_was_constructed.
  70. }
  71. void WindowFrame::window_was_constructed(Badge<Window>)
  72. {
  73. if (m_window.is_closeable()) {
  74. auto button = make<Button>(*this, [this](auto&) {
  75. m_window.handle_window_menu_action(WindowMenuAction::Close);
  76. });
  77. m_close_button = button.ptr();
  78. m_buttons.append(move(button));
  79. }
  80. if (m_window.is_resizable()) {
  81. auto button = make<Button>(*this, [this](auto&) {
  82. m_window.handle_window_menu_action(WindowMenuAction::MaximizeOrRestore);
  83. });
  84. button->on_middle_click = [&](auto&) {
  85. if (m_window.tile_type() == WindowTileType::VerticallyMaximized)
  86. m_window.set_untiled();
  87. else
  88. m_window.set_tiled(WindowTileType::VerticallyMaximized);
  89. };
  90. button->on_secondary_click = [&](auto&) {
  91. if (m_window.tile_type() == WindowTileType::HorizontallyMaximized)
  92. m_window.set_untiled();
  93. else
  94. m_window.set_tiled(WindowTileType::HorizontallyMaximized);
  95. };
  96. m_maximize_button = button.ptr();
  97. m_buttons.append(move(button));
  98. }
  99. if (m_window.is_minimizable()) {
  100. auto button = make<Button>(*this, [this](auto&) {
  101. m_window.handle_window_menu_action(WindowMenuAction::MinimizeOrUnminimize);
  102. });
  103. m_minimize_button = button.ptr();
  104. m_buttons.append(move(button));
  105. }
  106. set_button_icons();
  107. m_has_alpha_channel = Gfx::WindowTheme::current().frame_uses_alpha(window_state_for_theme(), WindowManager::the().palette());
  108. }
  109. WindowFrame::~WindowFrame() = default;
  110. void WindowFrame::set_button_icons()
  111. {
  112. set_dirty();
  113. if (m_window.is_frameless())
  114. return;
  115. auto button_style = WindowManager::the().palette().title_buttons_icon_only()
  116. ? Button::Style::IconOnly
  117. : Button::Style::Normal;
  118. if (m_window.is_closeable()) {
  119. m_close_button->set_icon(m_window.is_modified() ? s_close_modified_icon : s_close_icon);
  120. m_close_button->set_style(button_style);
  121. }
  122. if (m_window.is_minimizable()) {
  123. m_minimize_button->set_icon(s_minimize_icon);
  124. m_minimize_button->set_style(button_style);
  125. }
  126. if (m_window.is_resizable()) {
  127. m_maximize_button->set_icon(m_window.is_maximized() ? s_restore_icon : s_maximize_icon);
  128. m_maximize_button->set_style(button_style);
  129. }
  130. }
  131. void WindowFrame::reload_config()
  132. {
  133. String icons_path = WindowManager::the().palette().title_button_icons_path();
  134. auto reload_bitmap = [&](RefPtr<MultiScaleBitmaps>& multiscale_bitmap, StringView path, StringView default_path = ""sv) {
  135. StringBuilder full_path;
  136. full_path.append(icons_path);
  137. full_path.append(path);
  138. if (multiscale_bitmap)
  139. multiscale_bitmap->load(full_path.string_view(), default_path);
  140. else
  141. multiscale_bitmap = MultiScaleBitmaps::create(full_path.string_view(), default_path);
  142. };
  143. auto reload_icon = [&](Button::Icon& icon, StringView name, StringView default_path) {
  144. StringBuilder full_name;
  145. full_name.append(name);
  146. full_name.append(".png"sv);
  147. reload_bitmap(icon.bitmap, full_name.string_view(), default_path);
  148. // Note: No default for hover bitmaps
  149. full_name.clear();
  150. full_name.append(name);
  151. full_name.append("-hover.png"sv);
  152. reload_bitmap(icon.hover_bitmap, full_name.string_view());
  153. };
  154. reload_icon(s_minimize_icon, "window-minimize"sv, "/res/icons/16x16/downward-triangle.png"sv);
  155. reload_icon(s_maximize_icon, "window-maximize"sv, "/res/icons/16x16/upward-triangle.png"sv);
  156. reload_icon(s_restore_icon, "window-restore"sv, "/res/icons/16x16/window-restore.png"sv);
  157. reload_icon(s_close_icon, "window-close"sv, "/res/icons/16x16/window-close.png"sv);
  158. reload_icon(s_close_modified_icon, "window-close-modified"sv, "/res/icons/16x16/window-close-modified.png"sv);
  159. auto load_shadow = [](String const& path, String& last_path, RefPtr<MultiScaleBitmaps>& shadow_bitmap) {
  160. if (path.is_empty()) {
  161. last_path = String::empty();
  162. shadow_bitmap = nullptr;
  163. } else if (!shadow_bitmap || last_path != path) {
  164. if (shadow_bitmap)
  165. shadow_bitmap->load(path);
  166. else
  167. shadow_bitmap = MultiScaleBitmaps::create(path);
  168. if (shadow_bitmap)
  169. last_path = path;
  170. else
  171. last_path = String::empty();
  172. }
  173. };
  174. load_shadow(WindowManager::the().palette().active_window_shadow_path(), s_last_active_window_shadow_path, s_active_window_shadow);
  175. load_shadow(WindowManager::the().palette().inactive_window_shadow_path(), s_last_inactive_window_shadow_path, s_inactive_window_shadow);
  176. load_shadow(WindowManager::the().palette().menu_shadow_path(), s_last_menu_shadow_path, s_menu_shadow);
  177. load_shadow(WindowManager::the().palette().taskbar_shadow_path(), s_last_taskbar_shadow_path, s_taskbar_shadow);
  178. load_shadow(WindowManager::the().palette().tooltip_shadow_path(), s_last_tooltip_shadow_path, s_tooltip_shadow);
  179. }
  180. MultiScaleBitmaps const* WindowFrame::shadow_bitmap() const
  181. {
  182. if (m_window.is_frameless() && !m_window.has_forced_shadow())
  183. return nullptr;
  184. switch (m_window.type()) {
  185. case WindowType::Desktop:
  186. return nullptr;
  187. case WindowType::Menu:
  188. if (!WindowManager::the().system_effects().menu_shadow())
  189. return nullptr;
  190. return s_menu_shadow;
  191. case WindowType::Tooltip:
  192. if (!WindowManager::the().system_effects().tooltip_shadow())
  193. return nullptr;
  194. return s_tooltip_shadow;
  195. case WindowType::Taskbar:
  196. return s_taskbar_shadow;
  197. case WindowType::AppletArea:
  198. return nullptr;
  199. case WindowType::WindowSwitcher:
  200. return nullptr;
  201. default:
  202. if (!WindowManager::the().system_effects().window_shadow())
  203. return nullptr;
  204. // FIXME: Support shadow for themes with border radius
  205. if (WindowManager::the().palette().window_border_radius() > 0)
  206. return nullptr;
  207. if (auto* highlight_window = WindowManager::the().highlight_window())
  208. return highlight_window == &m_window ? s_active_window_shadow : s_inactive_window_shadow;
  209. return m_window.is_active() ? s_active_window_shadow : s_inactive_window_shadow;
  210. }
  211. }
  212. bool WindowFrame::has_shadow() const
  213. {
  214. if (auto* shadow_bitmap = this->shadow_bitmap(); shadow_bitmap && shadow_bitmap->format() == Gfx::BitmapFormat::BGRA8888)
  215. return true;
  216. return false;
  217. }
  218. void WindowFrame::did_set_maximized(Badge<Window>, bool maximized)
  219. {
  220. VERIFY(m_maximize_button);
  221. m_maximize_button->set_icon(maximized ? s_restore_icon : s_maximize_icon);
  222. }
  223. Gfx::IntRect WindowFrame::menubar_rect() const
  224. {
  225. if (!m_window.menubar().has_menus() || !m_window.should_show_menubar())
  226. return {};
  227. return Gfx::WindowTheme::current().menubar_rect(to_theme_window_type(m_window.type()), to_theme_window_mode(m_window.mode()), m_window.rect(), WindowManager::the().palette(), menu_row_count());
  228. }
  229. Gfx::IntRect WindowFrame::titlebar_rect() const
  230. {
  231. return Gfx::WindowTheme::current().titlebar_rect(to_theme_window_type(m_window.type()), to_theme_window_mode(m_window.mode()), m_window.rect(), WindowManager::the().palette());
  232. }
  233. Gfx::IntRect WindowFrame::titlebar_icon_rect() const
  234. {
  235. return Gfx::WindowTheme::current().titlebar_icon_rect(to_theme_window_type(m_window.type()), to_theme_window_mode(m_window.mode()), m_window.rect(), WindowManager::the().palette());
  236. }
  237. Gfx::IntRect WindowFrame::titlebar_text_rect() const
  238. {
  239. return Gfx::WindowTheme::current().titlebar_text_rect(to_theme_window_type(m_window.type()), to_theme_window_mode(m_window.mode()), m_window.rect(), WindowManager::the().palette());
  240. }
  241. Gfx::WindowTheme::WindowState WindowFrame::window_state_for_theme() const
  242. {
  243. auto& wm = WindowManager::the();
  244. if (m_flash_timer && m_flash_timer->is_active())
  245. return m_flash_counter & 1 ? Gfx::WindowTheme::WindowState::Highlighted : Gfx::WindowTheme::WindowState::Inactive;
  246. if (&m_window == wm.highlight_window())
  247. return Gfx::WindowTheme::WindowState::Highlighted;
  248. if (&m_window == wm.m_move_window)
  249. return Gfx::WindowTheme::WindowState::Moving;
  250. if (wm.is_active_window_or_capturing_modal(m_window))
  251. return Gfx::WindowTheme::WindowState::Active;
  252. return Gfx::WindowTheme::WindowState::Inactive;
  253. }
  254. void WindowFrame::paint_notification_frame(Gfx::Painter& painter)
  255. {
  256. auto palette = WindowManager::the().palette();
  257. Gfx::WindowTheme::current().paint_notification_frame(painter, to_theme_window_mode(m_window.mode()), m_window.rect(), palette, m_buttons.last().relative_rect());
  258. }
  259. void WindowFrame::paint_menubar(Gfx::Painter& painter)
  260. {
  261. auto& wm = WindowManager::the();
  262. auto& font = wm.font();
  263. auto palette = wm.palette();
  264. auto menubar_rect = this->menubar_rect();
  265. painter.fill_rect(menubar_rect, palette.window());
  266. Gfx::PainterStateSaver saver(painter);
  267. painter.add_clip_rect(menubar_rect);
  268. painter.translate(menubar_rect.location());
  269. m_window.menubar().for_each_menu([&](Menu& menu) {
  270. bool paint_as_flashed = ((&menu) == m_window.menubar().flashed_menu());
  271. if (paint_as_flashed) {
  272. auto flashed_rect = menu.rect_in_window_menubar();
  273. flashed_rect.shrink(2, 2);
  274. painter.fill_rect(flashed_rect, palette.selection());
  275. }
  276. auto text_rect = menu.rect_in_window_menubar();
  277. Color text_color = (paint_as_flashed ? palette.selection_text() : palette.window_text());
  278. auto is_open = menu.is_open();
  279. if (is_open)
  280. text_rect.translate_by(1, 1);
  281. bool paint_as_pressed = is_open;
  282. bool paint_as_hovered = !paint_as_pressed && &menu == MenuManager::the().hovered_menu();
  283. if (paint_as_pressed || paint_as_hovered) {
  284. Gfx::StylePainter::paint_button(painter, menu.rect_in_window_menubar(), palette, Gfx::ButtonStyle::Coolbar, paint_as_pressed, paint_as_hovered);
  285. }
  286. painter.draw_ui_text(text_rect, menu.name(), font, Gfx::TextAlignment::Center, text_color);
  287. return IterationDecision::Continue;
  288. });
  289. }
  290. void WindowFrame::paint_normal_frame(Gfx::Painter& painter)
  291. {
  292. auto palette = WindowManager::the().palette();
  293. Gfx::WindowTheme::current().paint_normal_frame(painter, window_state_for_theme(), to_theme_window_mode(m_window.mode()), m_window.rect(), m_window.computed_title(), m_window.icon(), palette, leftmost_titlebar_button_rect(), menu_row_count(), m_window.is_modified());
  294. if (m_window.menubar().has_menus() && m_window.should_show_menubar())
  295. paint_menubar(painter);
  296. }
  297. void WindowFrame::paint(Screen& screen, Gfx::Painter& painter, Gfx::IntRect const& rect)
  298. {
  299. if (auto* cached = render_to_cache(screen))
  300. cached->paint(*this, painter, rect);
  301. }
  302. void WindowFrame::PerScaleRenderedCache::paint(WindowFrame& frame, Gfx::Painter& painter, Gfx::IntRect const& rect)
  303. {
  304. auto frame_rect = frame.unconstrained_render_rect();
  305. auto window_rect = frame.window().rect();
  306. if (m_top_bottom) {
  307. auto top_bottom_height = frame_rect.height() - window_rect.height();
  308. if (m_bottom_y > 0) {
  309. // We have a top piece
  310. auto src_rect = rect.intersected({ frame_rect.location(), { frame_rect.width(), m_bottom_y } });
  311. if (!src_rect.is_empty())
  312. painter.blit(src_rect.location(), *m_top_bottom, src_rect.translated(-frame_rect.location()), frame.opacity());
  313. }
  314. if (m_bottom_y < top_bottom_height) {
  315. // We have a bottom piece
  316. Gfx::IntRect rect_in_frame { frame_rect.x(), window_rect.bottom() + 1, frame_rect.width(), top_bottom_height - m_bottom_y };
  317. auto src_rect = rect.intersected(rect_in_frame);
  318. if (!src_rect.is_empty())
  319. painter.blit(src_rect.location(), *m_top_bottom, src_rect.translated(-rect_in_frame.x(), -rect_in_frame.y() + m_bottom_y), frame.opacity());
  320. }
  321. }
  322. if (m_left_right) {
  323. auto left_right_width = frame_rect.width() - window_rect.width();
  324. if (m_right_x > 0) {
  325. // We have a left piece
  326. Gfx::IntRect rect_in_frame { frame_rect.x(), window_rect.y(), m_right_x, window_rect.height() };
  327. auto src_rect = rect.intersected(rect_in_frame);
  328. if (!src_rect.is_empty())
  329. painter.blit(src_rect.location(), *m_left_right, src_rect.translated(-rect_in_frame.location()), frame.opacity());
  330. }
  331. if (m_right_x < left_right_width) {
  332. // We have a right piece
  333. Gfx::IntRect rect_in_frame { window_rect.right() + 1, window_rect.y(), left_right_width - m_right_x, window_rect.height() };
  334. auto src_rect = rect.intersected(rect_in_frame);
  335. if (!src_rect.is_empty())
  336. painter.blit(src_rect.location(), *m_left_right, src_rect.translated(-rect_in_frame.x() + m_right_x, -rect_in_frame.y()), frame.opacity());
  337. }
  338. }
  339. }
  340. void WindowFrame::render(Screen& screen, Gfx::Painter& painter)
  341. {
  342. if (m_window.is_frameless())
  343. return;
  344. if (m_window.type() == WindowType::Notification)
  345. paint_notification_frame(painter);
  346. else if (m_window.type() == WindowType::Normal)
  347. paint_normal_frame(painter);
  348. else
  349. return;
  350. for (auto& button : m_buttons)
  351. button.paint(screen, painter);
  352. }
  353. void WindowFrame::theme_changed()
  354. {
  355. m_rendered_cache = {};
  356. layout_buttons();
  357. set_button_icons();
  358. m_has_alpha_channel = Gfx::WindowTheme::current().frame_uses_alpha(window_state_for_theme(), WindowManager::the().palette());
  359. }
  360. auto WindowFrame::render_to_cache(Screen& screen) -> PerScaleRenderedCache*
  361. {
  362. auto scale = screen.scale_factor();
  363. PerScaleRenderedCache* rendered_cache;
  364. auto cached_it = m_rendered_cache.find(scale);
  365. if (cached_it == m_rendered_cache.end()) {
  366. auto new_rendered_cache = make<PerScaleRenderedCache>();
  367. rendered_cache = new_rendered_cache.ptr();
  368. m_rendered_cache.set(scale, move(new_rendered_cache));
  369. } else {
  370. rendered_cache = cached_it->value.ptr();
  371. }
  372. rendered_cache->render(*this, screen);
  373. return rendered_cache;
  374. }
  375. void WindowFrame::PerScaleRenderedCache::render(WindowFrame& frame, Screen& screen)
  376. {
  377. if (!m_dirty)
  378. return;
  379. m_dirty = false;
  380. auto scale = screen.scale_factor();
  381. auto frame_rect = frame.rect();
  382. auto frame_rect_including_shadow = frame_rect;
  383. auto* shadow_bitmap = frame.shadow_bitmap();
  384. Gfx::IntPoint shadow_offset;
  385. if (shadow_bitmap) {
  386. auto total_shadow_size = shadow_bitmap->bitmap(screen.scale_factor()).height();
  387. frame_rect_including_shadow.inflate(total_shadow_size, total_shadow_size);
  388. auto offset = total_shadow_size / 2;
  389. shadow_offset = { offset, offset };
  390. }
  391. auto window_rect = frame.window().rect();
  392. // TODO: if we stop using a scaling factor we should clear cached bitmaps from this map
  393. static HashMap<int, RefPtr<Gfx::Bitmap>> s_tmp_bitmap_cache;
  394. Gfx::Bitmap* tmp_bitmap;
  395. {
  396. auto tmp_it = s_tmp_bitmap_cache.find(scale);
  397. if (tmp_it == s_tmp_bitmap_cache.end() || !tmp_it->value->size().contains(frame_rect_including_shadow.size())) {
  398. // Explicitly clear the old bitmap first so this works on machines with very little memory
  399. if (tmp_it != s_tmp_bitmap_cache.end())
  400. tmp_it->value = nullptr;
  401. auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, frame_rect_including_shadow.size(), scale);
  402. if (bitmap_or_error.is_error()) {
  403. s_tmp_bitmap_cache.remove(scale);
  404. dbgln("Could not create bitmap of size {}: {}", frame_rect_including_shadow.size(), bitmap_or_error.error());
  405. return;
  406. }
  407. auto bitmap = bitmap_or_error.release_value();
  408. tmp_bitmap = bitmap.ptr();
  409. if (tmp_it != s_tmp_bitmap_cache.end())
  410. tmp_it->value = move(bitmap);
  411. else
  412. s_tmp_bitmap_cache.set(scale, move(bitmap));
  413. } else {
  414. tmp_bitmap = tmp_it->value.ptr();
  415. }
  416. }
  417. VERIFY(tmp_bitmap);
  418. auto top_bottom_height = frame_rect_including_shadow.height() - window_rect.height();
  419. auto left_right_width = frame_rect_including_shadow.width() - window_rect.width();
  420. if (!m_top_bottom || m_top_bottom->width() != frame_rect_including_shadow.width() || m_top_bottom->height() != top_bottom_height || m_top_bottom->scale() != scale) {
  421. if (top_bottom_height > 0)
  422. m_top_bottom = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { frame_rect_including_shadow.width(), top_bottom_height }, scale).release_value_but_fixme_should_propagate_errors();
  423. else
  424. m_top_bottom = nullptr;
  425. m_shadow_dirty = true;
  426. }
  427. if (!m_left_right || m_left_right->height() != frame_rect_including_shadow.height() || m_left_right->width() != left_right_width || m_left_right->scale() != scale) {
  428. if (left_right_width > 0)
  429. m_left_right = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { left_right_width, frame_rect_including_shadow.height() }, scale).release_value_but_fixme_should_propagate_errors();
  430. else
  431. m_left_right = nullptr;
  432. m_shadow_dirty = true;
  433. }
  434. auto& frame_rect_to_update = m_shadow_dirty ? frame_rect_including_shadow : frame_rect;
  435. Gfx::IntPoint update_location(m_shadow_dirty ? Gfx::IntPoint { 0, 0 } : shadow_offset);
  436. Gfx::Painter painter(*tmp_bitmap);
  437. // Clear the frame area, not including the window content area, which we don't care about
  438. for (auto& rect : frame_rect_to_update.shatter(window_rect))
  439. painter.clear_rect({ rect.location() - frame_rect_to_update.location(), rect.size() }, { 255, 255, 255, 0 });
  440. if (m_shadow_dirty && shadow_bitmap)
  441. Gfx::StylePainter::paint_simple_rect_shadow(painter, { { 0, 0 }, frame_rect_including_shadow.size() }, shadow_bitmap->bitmap(screen.scale_factor()));
  442. {
  443. Gfx::PainterStateSaver save(painter);
  444. painter.translate(shadow_offset);
  445. frame.render(screen, painter);
  446. }
  447. if (m_top_bottom && top_bottom_height > 0) {
  448. m_bottom_y = window_rect.y() - frame_rect_including_shadow.y();
  449. VERIFY(m_bottom_y >= 0);
  450. Gfx::Painter top_bottom_painter(*m_top_bottom);
  451. top_bottom_painter.add_clip_rect({ update_location, { frame_rect_to_update.width(), top_bottom_height - update_location.y() - (frame_rect_including_shadow.bottom() - frame_rect_to_update.bottom()) } });
  452. if (m_bottom_y > 0)
  453. top_bottom_painter.blit({ 0, 0 }, *tmp_bitmap, { 0, 0, frame_rect_including_shadow.width(), m_bottom_y }, 1.0, false);
  454. if (m_bottom_y < top_bottom_height)
  455. top_bottom_painter.blit({ 0, m_bottom_y }, *tmp_bitmap, { 0, frame_rect_including_shadow.height() - (frame_rect_including_shadow.bottom() - window_rect.bottom()), frame_rect_including_shadow.width(), top_bottom_height - m_bottom_y }, 1.0, false);
  456. } else {
  457. m_bottom_y = 0;
  458. }
  459. if (left_right_width > 0) {
  460. m_right_x = window_rect.x() - frame_rect_including_shadow.x();
  461. VERIFY(m_right_x >= 0);
  462. Gfx::Painter left_right_painter(*m_left_right);
  463. left_right_painter.add_clip_rect({ update_location, { left_right_width - update_location.x() - (frame_rect_including_shadow.right() - frame_rect_to_update.right()), window_rect.height() } });
  464. if (m_right_x > 0)
  465. left_right_painter.blit({ 0, 0 }, *tmp_bitmap, { 0, m_bottom_y, m_right_x, window_rect.height() }, 1.0, false);
  466. if (m_right_x < left_right_width)
  467. left_right_painter.blit({ m_right_x, 0 }, *tmp_bitmap, { (window_rect.right() - frame_rect_including_shadow.x()) + 1, m_bottom_y, frame_rect_including_shadow.width() - (frame_rect_including_shadow.right() - window_rect.right()), window_rect.height() }, 1.0, false);
  468. } else {
  469. m_right_x = 0;
  470. }
  471. m_shadow_dirty = false;
  472. }
  473. void WindowFrame::set_opacity(float opacity)
  474. {
  475. if (m_opacity == opacity)
  476. return;
  477. bool was_opaque = is_opaque();
  478. m_opacity = opacity;
  479. if (was_opaque != is_opaque())
  480. Compositor::the().invalidate_occlusions();
  481. Compositor::the().invalidate_screen(render_rect());
  482. WindowManager::the().notify_opacity_changed(m_window);
  483. }
  484. Gfx::IntRect WindowFrame::inflated_for_shadow(Gfx::IntRect const& frame_rect) const
  485. {
  486. if (auto* shadow = shadow_bitmap()) {
  487. auto total_shadow_size = shadow->default_bitmap().height();
  488. return frame_rect.inflated(total_shadow_size, total_shadow_size);
  489. }
  490. return frame_rect;
  491. }
  492. Gfx::IntRect WindowFrame::rect() const
  493. {
  494. return frame_rect_for_window(m_window, m_window.rect());
  495. }
  496. Gfx::IntRect WindowFrame::constrained_render_rect_to_screen(Gfx::IntRect const& render_rect) const
  497. {
  498. if (m_window.is_tiled())
  499. return render_rect.intersected(Screen::closest_to_rect(rect()).rect());
  500. return render_rect;
  501. }
  502. Gfx::IntRect WindowFrame::leftmost_titlebar_button_rect() const
  503. {
  504. if (!m_buttons.is_empty())
  505. return m_buttons.last().relative_rect();
  506. auto rect = titlebar_rect();
  507. rect.translate_by(rect.width(), 0);
  508. return rect;
  509. }
  510. Gfx::IntRect WindowFrame::render_rect() const
  511. {
  512. return constrained_render_rect_to_screen(inflated_for_shadow(rect()));
  513. }
  514. Gfx::IntRect WindowFrame::unconstrained_render_rect() const
  515. {
  516. return inflated_for_shadow(rect());
  517. }
  518. Gfx::DisjointRectSet WindowFrame::opaque_render_rects() const
  519. {
  520. auto border_radius = WindowManager::the().palette().window_border_radius();
  521. if (has_alpha_channel() || border_radius > 0) {
  522. if (m_window.is_opaque())
  523. return constrained_render_rect_to_screen(m_window.rect());
  524. return {};
  525. }
  526. if (m_window.is_opaque())
  527. return constrained_render_rect_to_screen(rect());
  528. Gfx::DisjointRectSet opaque_rects;
  529. opaque_rects.add_many(constrained_render_rect_to_screen(rect()).shatter(m_window.rect()));
  530. return opaque_rects;
  531. }
  532. Gfx::DisjointRectSet WindowFrame::transparent_render_rects() const
  533. {
  534. auto border_radius = WindowManager::the().palette().window_border_radius();
  535. if (has_alpha_channel() || border_radius > 0) {
  536. if (m_window.is_opaque()) {
  537. Gfx::DisjointRectSet transparent_rects;
  538. transparent_rects.add_many(render_rect().shatter(m_window.rect()));
  539. return transparent_rects;
  540. }
  541. return render_rect();
  542. }
  543. auto total_render_rect = render_rect();
  544. Gfx::DisjointRectSet transparent_rects;
  545. if (has_shadow())
  546. transparent_rects.add_many(total_render_rect.shatter(rect()));
  547. if (!m_window.is_opaque())
  548. transparent_rects.add(m_window.rect().intersected(total_render_rect));
  549. return transparent_rects;
  550. }
  551. void WindowFrame::invalidate_titlebar()
  552. {
  553. set_dirty();
  554. invalidate(titlebar_rect());
  555. }
  556. void WindowFrame::invalidate()
  557. {
  558. auto frame_rect = render_rect();
  559. invalidate(Gfx::IntRect { frame_rect.location() - m_window.position(), frame_rect.size() });
  560. m_window.invalidate(true, true);
  561. }
  562. void WindowFrame::invalidate_menubar()
  563. {
  564. invalidate(menubar_rect());
  565. }
  566. void WindowFrame::invalidate(Gfx::IntRect relative_rect)
  567. {
  568. auto frame_rect = rect();
  569. auto window_rect = m_window.rect();
  570. relative_rect.translate_by(frame_rect.x() - window_rect.x(), frame_rect.y() - window_rect.y());
  571. set_dirty();
  572. m_window.invalidate(relative_rect, true);
  573. }
  574. void WindowFrame::window_rect_changed(Gfx::IntRect const& old_rect, Gfx::IntRect const& new_rect)
  575. {
  576. layout_buttons();
  577. set_dirty(true);
  578. WindowManager::the().notify_rect_changed(m_window, old_rect, new_rect);
  579. }
  580. void WindowFrame::layout_buttons()
  581. {
  582. auto button_rects = Gfx::WindowTheme::current().layout_buttons(to_theme_window_type(m_window.type()), to_theme_window_mode(m_window.mode()), m_window.rect(), WindowManager::the().palette(), m_buttons.size());
  583. for (size_t i = 0; i < m_buttons.size(); i++)
  584. m_buttons[i].set_relative_rect(button_rects[i]);
  585. }
  586. Optional<HitTestResult> WindowFrame::hit_test(Gfx::IntPoint const& position)
  587. {
  588. if (m_window.is_frameless() || m_window.is_fullscreen())
  589. return {};
  590. if (!constrained_render_rect_to_screen(rect()).contains(position)) {
  591. // Checking just frame_rect is not enough. If we constrain rendering
  592. // a window to one screen (e.g. when it's maximized or tiled) so that
  593. // the frame doesn't bleed into the adjacent screen(s), then we need
  594. // to also check that we're within these bounds.
  595. return {};
  596. }
  597. auto window_rect = m_window.rect();
  598. if (window_rect.contains(position))
  599. return {};
  600. auto* screen = Screen::find_by_location(position);
  601. if (!screen)
  602. return {};
  603. auto* cached = render_to_cache(*screen);
  604. if (!cached)
  605. return {};
  606. auto window_relative_position = position.translated(-unconstrained_render_rect().location());
  607. return cached->hit_test(*this, position, window_relative_position);
  608. }
  609. Optional<HitTestResult> WindowFrame::PerScaleRenderedCache::hit_test(WindowFrame& frame, Gfx::IntPoint const& position, Gfx::IntPoint const& window_relative_position)
  610. {
  611. HitTestResult result {
  612. .window = frame.window(),
  613. .screen_position = position,
  614. .window_relative_position = window_relative_position,
  615. .is_frame_hit = true,
  616. };
  617. u8 alpha_threshold = Gfx::WindowTheme::current().frame_alpha_hit_threshold(frame.window_state_for_theme()) * 255;
  618. if (alpha_threshold == 0)
  619. return result;
  620. u8 alpha = 0xff;
  621. auto window_rect = frame.window().rect();
  622. if (position.y() < window_rect.y()) {
  623. if (m_top_bottom) {
  624. auto scaled_relative_point = window_relative_position * m_top_bottom->scale();
  625. if (m_top_bottom->rect().contains(scaled_relative_point))
  626. alpha = m_top_bottom->get_pixel(scaled_relative_point).alpha();
  627. }
  628. } else if (position.y() > window_rect.bottom()) {
  629. if (m_top_bottom) {
  630. Gfx::IntPoint scaled_relative_point { window_relative_position.x() * m_top_bottom->scale(), m_bottom_y * m_top_bottom->scale() + position.y() - window_rect.bottom() - 1 };
  631. if (m_top_bottom->rect().contains(scaled_relative_point))
  632. alpha = m_top_bottom->get_pixel(scaled_relative_point).alpha();
  633. }
  634. } else if (position.x() < window_rect.x()) {
  635. if (m_left_right) {
  636. Gfx::IntPoint scaled_relative_point { window_relative_position.x() * m_left_right->scale(), (window_relative_position.y() - m_bottom_y) * m_left_right->scale() };
  637. if (m_left_right->rect().contains(scaled_relative_point))
  638. alpha = m_left_right->get_pixel(scaled_relative_point).alpha();
  639. }
  640. } else if (position.x() > window_rect.right()) {
  641. if (m_left_right) {
  642. Gfx::IntPoint scaled_relative_point { m_right_x * m_left_right->scale() + position.x() - window_rect.right() - 1, (window_relative_position.y() - m_bottom_y) * m_left_right->scale() };
  643. if (m_left_right->rect().contains(scaled_relative_point))
  644. alpha = m_left_right->get_pixel(scaled_relative_point).alpha();
  645. }
  646. } else {
  647. return {};
  648. }
  649. if (alpha >= alpha_threshold)
  650. return result;
  651. return {};
  652. }
  653. bool WindowFrame::handle_titlebar_icon_mouse_event(MouseEvent const& event)
  654. {
  655. auto& wm = WindowManager::the();
  656. if (event.type() == Event::MouseDown && (event.button() == MouseButton::Primary || event.button() == MouseButton::Secondary)) {
  657. // Manually start a potential double click. Since we're opening
  658. // a menu, we will only receive the MouseDown event, so we
  659. // need to record that fact. If the user subsequently clicks
  660. // on the same area, the menu will get closed, and we will
  661. // receive a MouseUp event, but because windows have changed
  662. // we don't get a MouseDoubleClick event. We can however record
  663. // this click, and when we receive the MouseUp event check if
  664. // it would have been considered a double click, if it weren't
  665. // for the fact that we opened and closed a window in the meanwhile
  666. wm.start_menu_doubleclick(m_window, event);
  667. m_window.popup_window_menu(titlebar_rect().bottom_left().translated(rect().location()), WindowMenuDefaultAction::Close);
  668. return true;
  669. } else if (event.type() == Event::MouseUp && event.button() == MouseButton::Primary) {
  670. // Since the MouseDown event opened a menu, another MouseUp
  671. // from the second click outside the menu wouldn't be considered
  672. // a double click, so let's manually check if it would otherwise
  673. // have been be considered to be one
  674. if (wm.is_menu_doubleclick(m_window, event)) {
  675. // It is a double click, so perform activate the default item
  676. m_window.window_menu_activate_default();
  677. }
  678. return true;
  679. }
  680. return false;
  681. }
  682. void WindowFrame::handle_titlebar_mouse_event(MouseEvent const& event)
  683. {
  684. auto& wm = WindowManager::the();
  685. if (titlebar_icon_rect().contains(event.position())) {
  686. if (handle_titlebar_icon_mouse_event(event))
  687. return;
  688. }
  689. for (auto& button : m_buttons) {
  690. if (button.relative_rect().contains(event.position()))
  691. return button.on_mouse_event(event.translated(-button.relative_rect().location()));
  692. }
  693. if (event.type() == Event::MouseDown) {
  694. if (m_window.type() == WindowType::Normal && event.button() == MouseButton::Secondary) {
  695. auto default_action = m_window.is_maximized() ? WindowMenuDefaultAction::Restore : WindowMenuDefaultAction::Maximize;
  696. m_window.popup_window_menu(event.position().translated(rect().location()), default_action);
  697. return;
  698. }
  699. if (m_window.is_movable() && event.button() == MouseButton::Primary)
  700. wm.start_window_move(m_window, event.translated(rect().location()));
  701. }
  702. }
  703. void WindowFrame::handle_mouse_event(MouseEvent const& event)
  704. {
  705. VERIFY(!m_window.is_fullscreen());
  706. if (m_window.type() != WindowType::Normal && m_window.type() != WindowType::Notification)
  707. return;
  708. auto& wm = WindowManager::the();
  709. if (m_window.type() == WindowType::Normal) {
  710. if (event.type() == Event::MouseDown)
  711. wm.move_to_front_and_make_active(m_window);
  712. }
  713. if (m_window.blocking_modal_window())
  714. return;
  715. // This is slightly hackish, but expand the title bar rect by two pixels downwards,
  716. // so that mouse events between the title bar and window contents don't act like
  717. // mouse events on the border.
  718. auto adjusted_titlebar_rect = titlebar_rect();
  719. adjusted_titlebar_rect.set_height(adjusted_titlebar_rect.height() + 2);
  720. if (adjusted_titlebar_rect.contains(event.position())) {
  721. handle_titlebar_mouse_event(event);
  722. return;
  723. }
  724. if (menubar_rect().contains(event.position())) {
  725. handle_menubar_mouse_event(event);
  726. return;
  727. }
  728. handle_border_mouse_event(event);
  729. }
  730. void WindowFrame::handle_border_mouse_event(MouseEvent const& event)
  731. {
  732. if (!m_window.is_resizable())
  733. return;
  734. auto& wm = WindowManager::the();
  735. if (event.type() == Event::MouseMove && event.buttons() == 0) {
  736. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  737. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  738. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  739. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  740. };
  741. Gfx::IntRect outer_rect = { {}, rect().size() };
  742. VERIFY(outer_rect.contains(event.position()));
  743. int window_relative_x = event.x() - outer_rect.x();
  744. int window_relative_y = event.y() - outer_rect.y();
  745. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  746. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  747. wm.set_resize_candidate(m_window, direction_for_hot_area[hot_area_row][hot_area_column]);
  748. Compositor::the().invalidate_cursor();
  749. return;
  750. }
  751. if (event.type() == Event::MouseDown && event.button() == MouseButton::Primary)
  752. wm.start_window_resize(m_window, event.translated(rect().location()));
  753. }
  754. void WindowFrame::handle_menubar_mouse_event(MouseEvent const& event)
  755. {
  756. Menu* hovered_menu = nullptr;
  757. auto menubar_rect = this->menubar_rect();
  758. auto adjusted_position = event.position().translated(-menubar_rect.location());
  759. m_window.menubar().for_each_menu([&](Menu& menu) {
  760. if (menu.rect_in_window_menubar().contains(adjusted_position)) {
  761. hovered_menu = &menu;
  762. handle_menu_mouse_event(menu, event);
  763. return IterationDecision::Break;
  764. }
  765. return IterationDecision::Continue;
  766. });
  767. if (!hovered_menu && event.type() == Event::Type::MouseDown)
  768. MenuManager::the().close_everyone();
  769. if (hovered_menu != MenuManager::the().hovered_menu()) {
  770. MenuManager::the().set_hovered_menu(hovered_menu);
  771. invalidate(menubar_rect);
  772. }
  773. }
  774. void WindowFrame::open_menubar_menu(Menu& menu)
  775. {
  776. auto menubar_rect = this->menubar_rect();
  777. MenuManager::the().close_everyone();
  778. auto position = menu.rect_in_window_menubar().bottom_left().translated(rect().location()).translated(menubar_rect.location());
  779. auto& window = menu.ensure_menu_window(position);
  780. auto window_rect = window.rect();
  781. auto& screen = Screen::closest_to_rect(window_rect);
  782. auto window_border_thickness = 1;
  783. // If the menu is off the right edge of the screen align its right edge with the edge of the screen.
  784. if (window_rect.right() > screen.width()) {
  785. position = position.translated(((window_rect.right() - screen.width()) * -1) - window_border_thickness, 0);
  786. }
  787. // If the menu is below the bottom of the screen move it to appear above the menubar.
  788. if (window_rect.bottom() > screen.height()) {
  789. position = position.translated(0, (window_rect.height() * -1) - menubar_rect.height());
  790. }
  791. window.set_rect(position.x(), position.y(), window_rect.width(), window_rect.height());
  792. MenuManager::the().open_menu(menu);
  793. WindowManager::the().set_window_with_active_menu(&m_window);
  794. invalidate(menubar_rect);
  795. }
  796. void WindowFrame::handle_menu_mouse_event(Menu& menu, MouseEvent const& event)
  797. {
  798. auto menubar_rect = this->menubar_rect();
  799. bool is_hover_with_any_menu_open = event.type() == MouseEvent::MouseMove && &m_window == WindowManager::the().window_with_active_menu();
  800. bool is_mousedown_with_left_button = event.type() == MouseEvent::MouseDown && event.button() == MouseButton::Primary;
  801. bool should_open_menu = &menu != MenuManager::the().current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  802. bool should_close_menu = &menu == MenuManager::the().current_menu() && is_mousedown_with_left_button;
  803. if (should_open_menu) {
  804. open_menubar_menu(menu);
  805. return;
  806. }
  807. if (should_close_menu) {
  808. invalidate(menubar_rect);
  809. MenuManager::the().close_everyone();
  810. }
  811. }
  812. void WindowFrame::start_flash_animation()
  813. {
  814. if (!m_flash_timer) {
  815. m_flash_timer = Core::Timer::construct(100, [this] {
  816. VERIFY(m_flash_counter);
  817. invalidate_titlebar();
  818. if (!--m_flash_counter)
  819. m_flash_timer->stop();
  820. });
  821. }
  822. m_flash_counter = 8;
  823. m_flash_timer->start();
  824. }
  825. int WindowFrame::menu_row_count() const
  826. {
  827. if (!m_window.should_show_menubar())
  828. return 0;
  829. return m_window.menubar().has_menus() ? 1 : 0;
  830. }
  831. }