WindowFrame.cpp 38 KB

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