Window.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 "Window.h"
  27. #include "AppletManager.h"
  28. #include "ClientConnection.h"
  29. #include "Compositor.h"
  30. #include "Event.h"
  31. #include "EventLoop.h"
  32. #include "Screen.h"
  33. #include "WindowManager.h"
  34. #include <AK/Badge.h>
  35. #include <WindowServer/WindowClientEndpoint.h>
  36. namespace WindowServer {
  37. static String default_window_icon_path()
  38. {
  39. return "/res/icons/16x16/window.png";
  40. }
  41. static Gfx::Bitmap& default_window_icon()
  42. {
  43. static Gfx::Bitmap* s_icon;
  44. if (!s_icon)
  45. s_icon = Gfx::Bitmap::load_from_file(default_window_icon_path()).leak_ref();
  46. return *s_icon;
  47. }
  48. static Gfx::Bitmap& minimize_icon()
  49. {
  50. static Gfx::Bitmap* s_icon;
  51. if (!s_icon)
  52. s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png").leak_ref();
  53. return *s_icon;
  54. }
  55. static Gfx::Bitmap& maximize_icon()
  56. {
  57. static Gfx::Bitmap* s_icon;
  58. if (!s_icon)
  59. s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/upward-triangle.png").leak_ref();
  60. return *s_icon;
  61. }
  62. static Gfx::Bitmap& restore_icon()
  63. {
  64. static Gfx::Bitmap* s_icon;
  65. if (!s_icon)
  66. s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-restore.png").leak_ref();
  67. return *s_icon;
  68. }
  69. static Gfx::Bitmap& close_icon()
  70. {
  71. static Gfx::Bitmap* s_icon;
  72. if (!s_icon)
  73. s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-close.png").leak_ref();
  74. return *s_icon;
  75. }
  76. Window::Window(Core::Object& parent, WindowType type)
  77. : Core::Object(&parent)
  78. , m_type(type)
  79. , m_icon(default_window_icon())
  80. , m_frame(*this)
  81. {
  82. WindowManager::the().add_window(*this);
  83. }
  84. Window::Window(ClientConnection& client, WindowType window_type, int window_id, bool modal, bool minimizable, bool frameless, bool resizable, bool fullscreen, bool accessory, Window* parent_window)
  85. : Core::Object(&client)
  86. , m_client(&client)
  87. , m_type(window_type)
  88. , m_modal(modal)
  89. , m_minimizable(minimizable)
  90. , m_frameless(frameless)
  91. , m_resizable(resizable)
  92. , m_fullscreen(fullscreen)
  93. , m_accessory(accessory)
  94. , m_window_id(window_id)
  95. , m_client_id(client.client_id())
  96. , m_icon(default_window_icon())
  97. , m_frame(*this)
  98. {
  99. // FIXME: This should not be hard-coded here.
  100. if (m_type == WindowType::Taskbar) {
  101. m_wm_event_mask = WMEventMask::WindowStateChanges | WMEventMask::WindowRemovals | WMEventMask::WindowIconChanges;
  102. m_listens_to_wm_events = true;
  103. }
  104. if (parent_window)
  105. set_parent_window(*parent_window);
  106. WindowManager::the().add_window(*this);
  107. }
  108. Window::~Window()
  109. {
  110. // Detach from client at the start of teardown since we don't want
  111. // to confuse things by trying to send messages to it.
  112. m_client = nullptr;
  113. WindowManager::the().remove_window(*this);
  114. }
  115. void Window::destroy()
  116. {
  117. m_destroyed = true;
  118. set_visible(false);
  119. }
  120. void Window::set_title(const String& title)
  121. {
  122. if (m_title == title)
  123. return;
  124. m_title = title;
  125. frame().invalidate_title_bar();
  126. WindowManager::the().notify_title_changed(*this);
  127. }
  128. void Window::set_rect(const Gfx::IntRect& rect)
  129. {
  130. ASSERT(!rect.is_empty());
  131. if (m_rect == rect)
  132. return;
  133. auto old_rect = m_rect;
  134. m_rect = rect;
  135. if (!m_client && (!m_backing_store || old_rect.size() != rect.size())) {
  136. m_backing_store = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, m_rect.size());
  137. }
  138. invalidate(true);
  139. m_frame.notify_window_rect_changed(old_rect, rect); // recomputes occlusions
  140. }
  141. void Window::set_rect_without_repaint(const Gfx::IntRect& rect)
  142. {
  143. ASSERT(!rect.is_empty());
  144. if (m_rect == rect)
  145. return;
  146. auto old_rect = m_rect;
  147. m_rect = rect;
  148. if (old_rect.size() == m_rect.size()) {
  149. auto delta = m_rect.location() - old_rect.location();
  150. for (auto& child_window : m_child_windows) {
  151. if (child_window)
  152. child_window->move_by(delta);
  153. }
  154. }
  155. invalidate(true);
  156. m_frame.notify_window_rect_changed(old_rect, rect); // recomputes occlusions
  157. }
  158. void Window::apply_minimum_size(Gfx::IntRect& rect)
  159. {
  160. Gfx::IntSize minimum_size { 1, 1 };
  161. if (type() == WindowType::Normal)
  162. minimum_size = { 50, 50 };
  163. rect.set_width(max(minimum_size.width(), rect.width()));
  164. rect.set_height(max(minimum_size.height(), rect.height()));
  165. }
  166. void Window::nudge_into_desktop(bool force_titlebar_visible)
  167. {
  168. Gfx::IntRect arena = WindowManager::the().arena_rect_for_type(type());
  169. auto min_visible = 1;
  170. if (type() == WindowType::Normal)
  171. min_visible = 30;
  172. // Push the frame around such that at least `min_visible` pixels of the *frame* are in the desktop rect.
  173. auto old_frame_rect = frame().rect();
  174. Gfx::IntRect new_frame_rect = {
  175. clamp(old_frame_rect.x(), arena.left() + min_visible - width(), arena.right() - min_visible),
  176. clamp(old_frame_rect.y(), arena.top() + min_visible - height(), arena.bottom() - min_visible),
  177. old_frame_rect.width(),
  178. old_frame_rect.height(),
  179. };
  180. // Make sure that at least half of the titlebar is visible.
  181. auto min_frame_y = arena.top() - (y() - old_frame_rect.y()) / 2;
  182. if (force_titlebar_visible && new_frame_rect.y() < min_frame_y) {
  183. new_frame_rect.set_y(min_frame_y);
  184. }
  185. // Deduce new window rect:
  186. Gfx::IntRect new_window_rect = {
  187. x() + new_frame_rect.x() - old_frame_rect.x(),
  188. y() + new_frame_rect.y() - old_frame_rect.y(),
  189. width(),
  190. height(),
  191. };
  192. set_rect(new_window_rect);
  193. }
  194. void Window::handle_mouse_event(const MouseEvent& event)
  195. {
  196. set_automatic_cursor_tracking_enabled(event.buttons() != 0);
  197. switch (event.type()) {
  198. case Event::MouseMove:
  199. m_client->post_message(Messages::WindowClient::MouseMove(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta(), event.is_drag(), event.mime_types()));
  200. break;
  201. case Event::MouseDown:
  202. m_client->post_message(Messages::WindowClient::MouseDown(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  203. break;
  204. case Event::MouseDoubleClick:
  205. m_client->post_message(Messages::WindowClient::MouseDoubleClick(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  206. break;
  207. case Event::MouseUp:
  208. m_client->post_message(Messages::WindowClient::MouseUp(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  209. break;
  210. case Event::MouseWheel:
  211. m_client->post_message(Messages::WindowClient::MouseWheel(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  212. break;
  213. default:
  214. ASSERT_NOT_REACHED();
  215. }
  216. }
  217. void Window::update_menu_item_text(PopupMenuItem item)
  218. {
  219. if (m_window_menu) {
  220. m_window_menu->item((int)item).set_text(item == PopupMenuItem::Minimize ? (m_minimized ? "Unminimize" : "Minimize") : (m_maximized ? "Restore" : "Maximize"));
  221. m_window_menu->redraw();
  222. }
  223. }
  224. void Window::update_menu_item_enabled(PopupMenuItem item)
  225. {
  226. if (m_window_menu) {
  227. m_window_menu->item((int)item).set_enabled(item == PopupMenuItem::Minimize ? m_minimizable : m_resizable);
  228. m_window_menu->redraw();
  229. }
  230. }
  231. void Window::set_minimized(bool minimized)
  232. {
  233. if (m_minimized == minimized)
  234. return;
  235. if (minimized && !m_minimizable)
  236. return;
  237. m_minimized = minimized;
  238. update_menu_item_text(PopupMenuItem::Minimize);
  239. Compositor::the().invalidate_occlusions();
  240. Compositor::the().invalidate_screen(frame().render_rect());
  241. if (!blocking_modal_window())
  242. start_minimize_animation();
  243. if (!minimized)
  244. request_update({ {}, size() });
  245. WindowManager::the().notify_minimization_state_changed(*this);
  246. }
  247. void Window::set_minimizable(bool minimizable)
  248. {
  249. if (m_minimizable == minimizable)
  250. return;
  251. m_minimizable = minimizable;
  252. update_menu_item_enabled(PopupMenuItem::Minimize);
  253. // TODO: Hide/show (or alternatively change enabled state of) window minimize button dynamically depending on value of m_minimizable
  254. }
  255. void Window::set_taskbar_rect(const Gfx::IntRect& rect)
  256. {
  257. m_taskbar_rect = rect;
  258. m_have_taskbar_rect = !m_taskbar_rect.is_empty();
  259. }
  260. void Window::start_minimize_animation()
  261. {
  262. if (!m_have_taskbar_rect) {
  263. // If this is a modal window, it may not have its own taskbar
  264. // button, so there is no rectangle. In that case, walk the
  265. // modal stack until we find a window that may have one
  266. WindowManager::the().for_each_window_in_modal_stack(*this, [&](Window& w, bool) {
  267. if (w.has_taskbar_rect()) {
  268. // We purposely do NOT set m_have_taskbar_rect to true here
  269. // because we want to only copy the rectangle from the
  270. // window that has it, but since this window wouldn't receive
  271. // any updates down the road we want to query it again
  272. // next time we want to start the animation
  273. m_taskbar_rect = w.taskbar_rect();
  274. ASSERT(!m_have_taskbar_rect); // should remain unset!
  275. return IterationDecision::Break;
  276. };
  277. return IterationDecision::Continue;
  278. });
  279. }
  280. m_minimize_animation_step = 0;
  281. }
  282. void Window::set_opacity(float opacity)
  283. {
  284. if (m_opacity == opacity)
  285. return;
  286. bool was_opaque = is_opaque();
  287. m_opacity = opacity;
  288. if (was_opaque != is_opaque())
  289. Compositor::the().invalidate_occlusions();
  290. Compositor::the().invalidate_screen(frame().render_rect());
  291. WindowManager::the().notify_opacity_changed(*this);
  292. }
  293. void Window::set_occluded(bool occluded)
  294. {
  295. if (m_occluded == occluded)
  296. return;
  297. m_occluded = occluded;
  298. WindowManager::the().notify_occlusion_state_changed(*this);
  299. }
  300. void Window::set_maximized(bool maximized, Optional<Gfx::IntPoint> fixed_point)
  301. {
  302. if (m_maximized == maximized)
  303. return;
  304. if (maximized && (!is_resizable() || resize_aspect_ratio().has_value()))
  305. return;
  306. m_tiled = WindowTileType::None;
  307. m_maximized = maximized;
  308. update_menu_item_text(PopupMenuItem::Maximize);
  309. if (maximized) {
  310. m_unmaximized_rect = m_rect;
  311. set_rect(WindowManager::the().maximized_window_rect(*this));
  312. } else {
  313. if (fixed_point.has_value()) {
  314. auto new_rect = Gfx::IntRect(m_rect);
  315. new_rect.set_size_around(m_unmaximized_rect.size(), fixed_point.value());
  316. set_rect(new_rect);
  317. } else {
  318. set_rect(m_unmaximized_rect);
  319. }
  320. }
  321. m_frame.did_set_maximized({}, maximized);
  322. Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect));
  323. set_default_positioned(false);
  324. }
  325. void Window::set_resizable(bool resizable)
  326. {
  327. if (m_resizable == resizable)
  328. return;
  329. m_resizable = resizable;
  330. update_menu_item_enabled(PopupMenuItem::Maximize);
  331. // TODO: Hide/show (or alternatively change enabled state of) window maximize button dynamically depending on value of is_resizable()
  332. }
  333. void Window::event(Core::Event& event)
  334. {
  335. if (!m_client) {
  336. ASSERT(parent());
  337. event.ignore();
  338. return;
  339. }
  340. if (blocking_modal_window()) {
  341. // We still want to handle the WindowDeactivated event below when a new modal is
  342. // created to notify its parent window, despite it being "blocked by modal window".
  343. if (event.type() != Event::WindowDeactivated)
  344. return;
  345. }
  346. if (static_cast<Event&>(event).is_mouse_event())
  347. return handle_mouse_event(static_cast<const MouseEvent&>(event));
  348. switch (event.type()) {
  349. case Event::WindowEntered:
  350. m_client->post_message(Messages::WindowClient::WindowEntered(m_window_id));
  351. break;
  352. case Event::WindowLeft:
  353. m_client->post_message(Messages::WindowClient::WindowLeft(m_window_id));
  354. break;
  355. case Event::KeyDown:
  356. m_client->post_message(
  357. Messages::WindowClient::KeyDown(m_window_id,
  358. (u32) static_cast<const KeyEvent&>(event).code_point(),
  359. (u32) static_cast<const KeyEvent&>(event).key(),
  360. static_cast<const KeyEvent&>(event).modifiers(),
  361. (u32) static_cast<const KeyEvent&>(event).scancode()));
  362. break;
  363. case Event::KeyUp:
  364. m_client->post_message(
  365. Messages::WindowClient::KeyUp(m_window_id,
  366. (u32) static_cast<const KeyEvent&>(event).code_point(),
  367. (u32) static_cast<const KeyEvent&>(event).key(),
  368. static_cast<const KeyEvent&>(event).modifiers(),
  369. (u32) static_cast<const KeyEvent&>(event).scancode()));
  370. break;
  371. case Event::WindowActivated:
  372. m_client->post_message(Messages::WindowClient::WindowActivated(m_window_id));
  373. break;
  374. case Event::WindowDeactivated:
  375. m_client->post_message(Messages::WindowClient::WindowDeactivated(m_window_id));
  376. break;
  377. case Event::WindowInputEntered:
  378. m_client->post_message(Messages::WindowClient::WindowInputEntered(m_window_id));
  379. break;
  380. case Event::WindowInputLeft:
  381. m_client->post_message(Messages::WindowClient::WindowInputLeft(m_window_id));
  382. break;
  383. case Event::WindowCloseRequest:
  384. m_client->post_message(Messages::WindowClient::WindowCloseRequest(m_window_id));
  385. break;
  386. case Event::WindowResized:
  387. m_client->post_message(Messages::WindowClient::WindowResized(m_window_id, static_cast<const ResizeEvent&>(event).rect()));
  388. break;
  389. default:
  390. break;
  391. }
  392. }
  393. void Window::set_global_cursor_tracking_enabled(bool enabled)
  394. {
  395. m_global_cursor_tracking_enabled = enabled;
  396. }
  397. void Window::set_visible(bool b)
  398. {
  399. if (m_visible == b)
  400. return;
  401. m_visible = b;
  402. Compositor::the().invalidate_occlusions();
  403. if (m_visible)
  404. invalidate(true);
  405. else
  406. Compositor::the().invalidate_screen(frame().render_rect());
  407. }
  408. void Window::invalidate(bool invalidate_frame)
  409. {
  410. m_invalidated = true;
  411. m_invalidated_all = true;
  412. if (invalidate_frame && !m_invalidated_frame) {
  413. m_invalidated_frame = true;
  414. frame().set_dirty();
  415. }
  416. m_dirty_rects.clear();
  417. Compositor::the().invalidate_window();
  418. }
  419. void Window::invalidate(const Gfx::IntRect& rect, bool with_frame)
  420. {
  421. if (type() == WindowType::MenuApplet) {
  422. AppletManager::the().invalidate_applet(*this, rect);
  423. return;
  424. }
  425. if (invalidate_no_notify(rect, with_frame))
  426. Compositor::the().invalidate_window();
  427. }
  428. bool Window::invalidate_no_notify(const Gfx::IntRect& rect, bool with_frame)
  429. {
  430. if (rect.is_empty())
  431. return false;
  432. if (m_invalidated_all) {
  433. if (with_frame && !m_invalidated_frame) {
  434. m_invalidated_frame = true;
  435. frame().set_dirty();
  436. }
  437. return false;
  438. }
  439. auto outer_rect = frame().render_rect();
  440. auto inner_rect = rect;
  441. inner_rect.move_by(position());
  442. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  443. inner_rect.intersect(outer_rect);
  444. if (inner_rect.is_empty())
  445. return false;
  446. m_invalidated = true;
  447. if (with_frame && !m_invalidated_frame) {
  448. m_invalidated_frame = true;
  449. frame().set_dirty();
  450. }
  451. m_dirty_rects.add(inner_rect.translated(-outer_rect.location()));
  452. return true;
  453. }
  454. void Window::prepare_dirty_rects()
  455. {
  456. if (m_invalidated_all) {
  457. if (m_invalidated_frame)
  458. m_dirty_rects = frame().render_rect();
  459. else
  460. m_dirty_rects = rect();
  461. } else {
  462. m_dirty_rects.move_by(frame().render_rect().location());
  463. }
  464. }
  465. void Window::clear_dirty_rects()
  466. {
  467. m_invalidated_all = false;
  468. m_invalidated_frame = false;
  469. m_invalidated = false;
  470. m_dirty_rects.clear_with_capacity();
  471. }
  472. bool Window::is_active() const
  473. {
  474. return WindowManager::the().active_window() == this;
  475. }
  476. Window* Window::blocking_modal_window()
  477. {
  478. // A window is blocked if any immediate child, or any child further
  479. // down the chain is modal
  480. for (auto& window : m_child_windows) {
  481. if (window && !window->is_destroyed()) {
  482. if (window->is_modal())
  483. return window;
  484. if (auto* blocking_window = window->blocking_modal_window())
  485. return blocking_window;
  486. }
  487. }
  488. return nullptr;
  489. }
  490. void Window::set_default_icon()
  491. {
  492. m_icon = default_window_icon();
  493. }
  494. void Window::request_update(const Gfx::IntRect& rect, bool ignore_occlusion)
  495. {
  496. if (rect.is_empty())
  497. return;
  498. if (m_pending_paint_rects.is_empty()) {
  499. deferred_invoke([this, ignore_occlusion](auto&) {
  500. client()->post_paint_message(*this, ignore_occlusion);
  501. });
  502. }
  503. m_pending_paint_rects.add(rect);
  504. }
  505. void Window::ensure_window_menu()
  506. {
  507. if (!m_window_menu) {
  508. m_window_menu = Menu::construct(nullptr, -1, "(Window Menu)");
  509. m_window_menu->set_window_menu_of(*this);
  510. auto minimize_item = make<MenuItem>(*m_window_menu, 1, m_minimized ? "Unminimize" : "Minimize");
  511. m_window_menu_minimize_item = minimize_item.ptr();
  512. m_window_menu->add_item(move(minimize_item));
  513. auto maximize_item = make<MenuItem>(*m_window_menu, 2, m_maximized ? "Restore" : "Maximize");
  514. m_window_menu_maximize_item = maximize_item.ptr();
  515. m_window_menu->add_item(move(maximize_item));
  516. m_window_menu->add_item(make<MenuItem>(*m_window_menu, MenuItem::Type::Separator));
  517. auto close_item = make<MenuItem>(*m_window_menu, 3, "Close");
  518. m_window_menu_close_item = close_item.ptr();
  519. m_window_menu_close_item->set_icon(&close_icon());
  520. m_window_menu_close_item->set_default(true);
  521. m_window_menu->add_item(move(close_item));
  522. m_window_menu->item((int)PopupMenuItem::Minimize).set_enabled(m_minimizable);
  523. m_window_menu->item((int)PopupMenuItem::Maximize).set_enabled(m_resizable);
  524. m_window_menu->on_item_activation = [&](auto& item) {
  525. switch (item.identifier()) {
  526. case 1:
  527. WindowManager::the().minimize_windows(*this, !m_minimized);
  528. if (!m_minimized)
  529. WindowManager::the().move_to_front_and_make_active(*this);
  530. break;
  531. case 2:
  532. WindowManager::the().maximize_windows(*this, !m_maximized);
  533. WindowManager::the().move_to_front_and_make_active(*this);
  534. break;
  535. case 3:
  536. request_close();
  537. break;
  538. }
  539. };
  540. }
  541. }
  542. void Window::popup_window_menu(const Gfx::IntPoint& position, WindowMenuDefaultAction default_action)
  543. {
  544. ensure_window_menu();
  545. if (default_action == WindowMenuDefaultAction::BasedOnWindowState) {
  546. // When clicked on the task bar, determine the default action
  547. if (!is_active() && !is_minimized())
  548. default_action = WindowMenuDefaultAction::None;
  549. else if (is_minimized())
  550. default_action = WindowMenuDefaultAction::Unminimize;
  551. else
  552. default_action = WindowMenuDefaultAction::Minimize;
  553. }
  554. m_window_menu_minimize_item->set_default(default_action == WindowMenuDefaultAction::Minimize || default_action == WindowMenuDefaultAction::Unminimize);
  555. m_window_menu_minimize_item->set_icon(m_minimized ? nullptr : &minimize_icon());
  556. m_window_menu_maximize_item->set_default(default_action == WindowMenuDefaultAction::Maximize || default_action == WindowMenuDefaultAction::Restore);
  557. m_window_menu_maximize_item->set_icon(m_maximized ? &restore_icon() : &maximize_icon());
  558. m_window_menu_close_item->set_default(default_action == WindowMenuDefaultAction::Close);
  559. m_window_menu->popup(position);
  560. }
  561. void Window::window_menu_activate_default()
  562. {
  563. ensure_window_menu();
  564. m_window_menu->activate_default();
  565. }
  566. void Window::request_close()
  567. {
  568. Event close_request(Event::WindowCloseRequest);
  569. event(close_request);
  570. }
  571. void Window::set_fullscreen(bool fullscreen)
  572. {
  573. if (m_fullscreen == fullscreen)
  574. return;
  575. m_fullscreen = fullscreen;
  576. Gfx::IntRect new_window_rect = m_rect;
  577. if (m_fullscreen) {
  578. m_saved_nonfullscreen_rect = m_rect;
  579. new_window_rect = Screen::the().rect();
  580. } else if (!m_saved_nonfullscreen_rect.is_empty()) {
  581. new_window_rect = m_saved_nonfullscreen_rect;
  582. }
  583. Core::EventLoop::current().post_event(*this, make<ResizeEvent>(new_window_rect));
  584. set_rect(new_window_rect);
  585. }
  586. Gfx::IntRect Window::tiled_rect(WindowTileType tiled) const
  587. {
  588. ASSERT(tiled != WindowTileType::None);
  589. int frame_width = (m_frame.rect().width() - m_rect.width()) / 2;
  590. int title_bar_height = m_frame.title_bar_rect().height();
  591. int menu_height = WindowManager::the().maximized_window_rect(*this).y();
  592. int max_height = WindowManager::the().maximized_window_rect(*this).height();
  593. switch (tiled) {
  594. case WindowTileType::Left:
  595. return Gfx::IntRect(0,
  596. menu_height,
  597. Screen::the().width() / 2 - frame_width,
  598. max_height);
  599. case WindowTileType::Right:
  600. return Gfx::IntRect(Screen::the().width() / 2 + frame_width,
  601. menu_height,
  602. Screen::the().width() / 2 - frame_width,
  603. max_height);
  604. case WindowTileType::Top:
  605. return Gfx::IntRect(0,
  606. menu_height,
  607. Screen::the().width() - frame_width,
  608. (max_height - title_bar_height) / 2 - frame_width);
  609. case WindowTileType::Bottom:
  610. return Gfx::IntRect(0,
  611. menu_height + (title_bar_height + max_height) / 2 + frame_width,
  612. Screen::the().width() - frame_width,
  613. (max_height - title_bar_height) / 2 - frame_width);
  614. case WindowTileType::TopLeft:
  615. return Gfx::IntRect(0,
  616. menu_height,
  617. Screen::the().width() / 2 - frame_width,
  618. (max_height - title_bar_height) / 2 - frame_width);
  619. case WindowTileType::TopRight:
  620. return Gfx::IntRect(Screen::the().width() / 2 + frame_width,
  621. menu_height,
  622. Screen::the().width() / 2 - frame_width,
  623. (max_height - title_bar_height) / 2 - frame_width);
  624. case WindowTileType::BottomLeft:
  625. return Gfx::IntRect(0,
  626. menu_height + (title_bar_height + max_height) / 2 + frame_width,
  627. Screen::the().width() / 2 - frame_width,
  628. (max_height - title_bar_height) / 2);
  629. case WindowTileType::BottomRight:
  630. return Gfx::IntRect(Screen::the().width() / 2 + frame_width,
  631. menu_height + (title_bar_height + max_height) / 2 + frame_width,
  632. Screen::the().width() / 2 - frame_width,
  633. (max_height - title_bar_height) / 2);
  634. default:
  635. ASSERT_NOT_REACHED();
  636. }
  637. }
  638. bool Window::set_untiled(Optional<Gfx::IntPoint> fixed_point)
  639. {
  640. if (m_tiled == WindowTileType::None)
  641. return false;
  642. ASSERT(!resize_aspect_ratio().has_value());
  643. m_tiled = WindowTileType::None;
  644. if (fixed_point.has_value()) {
  645. auto new_rect = Gfx::IntRect(m_rect);
  646. new_rect.set_size_around(m_untiled_rect.size(), fixed_point.value());
  647. set_rect(new_rect);
  648. } else {
  649. set_rect(m_untiled_rect);
  650. }
  651. Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect));
  652. return true;
  653. }
  654. void Window::set_tiled(WindowTileType tiled)
  655. {
  656. ASSERT(tiled != WindowTileType::None);
  657. if (m_tiled == tiled)
  658. return;
  659. if (resize_aspect_ratio().has_value())
  660. return;
  661. if (m_tiled == WindowTileType::None)
  662. m_untiled_rect = m_rect;
  663. m_tiled = tiled;
  664. set_rect(tiled_rect(tiled));
  665. Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect));
  666. }
  667. void Window::detach_client(Badge<ClientConnection>)
  668. {
  669. m_client = nullptr;
  670. }
  671. void Window::recalculate_rect()
  672. {
  673. if (!is_resizable())
  674. return;
  675. bool send_event = true;
  676. if (m_tiled != WindowTileType::None) {
  677. set_rect(tiled_rect(m_tiled));
  678. } else if (is_maximized()) {
  679. set_rect(WindowManager::the().maximized_window_rect(*this));
  680. } else if (type() == WindowType::Desktop) {
  681. set_rect(WindowManager::the().desktop_rect());
  682. } else {
  683. send_event = false;
  684. }
  685. if (send_event) {
  686. Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect));
  687. }
  688. }
  689. void Window::add_child_window(Window& child_window)
  690. {
  691. m_child_windows.append(child_window);
  692. }
  693. void Window::add_accessory_window(Window& accessory_window)
  694. {
  695. m_accessory_windows.append(accessory_window);
  696. }
  697. void Window::set_parent_window(Window& parent_window)
  698. {
  699. ASSERT(!m_parent_window);
  700. m_parent_window = parent_window;
  701. if (m_accessory)
  702. parent_window.add_accessory_window(*this);
  703. else
  704. parent_window.add_child_window(*this);
  705. }
  706. bool Window::is_accessory() const
  707. {
  708. if (!m_accessory)
  709. return false;
  710. if (parent_window() != nullptr)
  711. return true;
  712. // If accessory window was unparented, convert to a regular window
  713. const_cast<Window*>(this)->set_accessory(false);
  714. return false;
  715. }
  716. bool Window::is_accessory_of(Window& window) const
  717. {
  718. if (!is_accessory())
  719. return false;
  720. return parent_window() == &window;
  721. }
  722. void Window::modal_unparented()
  723. {
  724. m_modal = false;
  725. WindowManager::the().notify_modal_unparented(*this);
  726. }
  727. bool Window::is_modal() const
  728. {
  729. if (!m_modal)
  730. return false;
  731. if (!m_parent_window) {
  732. const_cast<Window*>(this)->modal_unparented();
  733. return false;
  734. }
  735. return true;
  736. }
  737. void Window::set_progress(int progress)
  738. {
  739. if (m_progress == progress)
  740. return;
  741. m_progress = progress;
  742. WindowManager::the().notify_progress_changed(*this);
  743. }
  744. bool Window::is_descendant_of(Window& window) const
  745. {
  746. for (auto* parent = parent_window(); parent; parent = parent->parent_window()) {
  747. if (parent == &window)
  748. return true;
  749. for (auto& accessory : parent->accessory_windows()) {
  750. if (accessory == &window)
  751. return true;
  752. }
  753. }
  754. return false;
  755. }
  756. }