Window.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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/window-minimize.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/window-maximize.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)
  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_window_id(window_id)
  94. , m_client_id(client.client_id())
  95. , m_icon(default_window_icon())
  96. , m_frame(*this)
  97. {
  98. // FIXME: This should not be hard-coded here.
  99. if (m_type == WindowType::Taskbar) {
  100. m_wm_event_mask = WMEventMask::WindowStateChanges | WMEventMask::WindowRemovals | WMEventMask::WindowIconChanges;
  101. m_listens_to_wm_events = true;
  102. }
  103. WindowManager::the().add_window(*this);
  104. }
  105. Window::~Window()
  106. {
  107. // Detach from client at the start of teardown since we don't want
  108. // to confuse things by trying to send messages to it.
  109. m_client = nullptr;
  110. WindowManager::the().remove_window(*this);
  111. }
  112. void Window::set_title(const String& title)
  113. {
  114. if (m_title == title)
  115. return;
  116. m_title = title;
  117. WindowManager::the().notify_title_changed(*this);
  118. }
  119. void Window::set_rect(const Gfx::Rect& rect)
  120. {
  121. ASSERT(!rect.is_empty());
  122. Gfx::Rect old_rect;
  123. if (m_rect == rect)
  124. return;
  125. old_rect = m_rect;
  126. m_rect = rect;
  127. if (!m_client && (!m_backing_store || old_rect.size() != rect.size())) {
  128. m_backing_store = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, m_rect.size());
  129. }
  130. m_frame.notify_window_rect_changed(old_rect, rect);
  131. }
  132. void Window::set_rect_without_repaint(const Gfx::Rect& rect)
  133. {
  134. ASSERT(!rect.is_empty());
  135. if (m_rect == rect)
  136. return;
  137. auto old_rect = m_rect;
  138. m_rect = rect;
  139. if (old_rect.size() == m_rect.size()) {
  140. auto delta = m_rect.location() - old_rect.location();
  141. for (auto& child_window : m_child_windows) {
  142. if (child_window)
  143. child_window->move_by(delta);
  144. }
  145. }
  146. m_frame.notify_window_rect_changed(old_rect, rect);
  147. }
  148. void Window::handle_mouse_event(const MouseEvent& event)
  149. {
  150. set_automatic_cursor_tracking_enabled(event.buttons() != 0);
  151. switch (event.type()) {
  152. case Event::MouseMove:
  153. 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.drag_data_type()));
  154. break;
  155. case Event::MouseDown:
  156. m_client->post_message(Messages::WindowClient::MouseDown(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  157. break;
  158. case Event::MouseDoubleClick:
  159. m_client->post_message(Messages::WindowClient::MouseDoubleClick(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  160. break;
  161. case Event::MouseUp:
  162. m_client->post_message(Messages::WindowClient::MouseUp(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  163. break;
  164. case Event::MouseWheel:
  165. m_client->post_message(Messages::WindowClient::MouseWheel(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  166. break;
  167. default:
  168. ASSERT_NOT_REACHED();
  169. }
  170. }
  171. void Window::update_menu_item_text(PopupMenuItem item)
  172. {
  173. if (m_window_menu) {
  174. m_window_menu->item((int)item).set_text(item == PopupMenuItem::Minimize ? (m_minimized ? "Unminimize" : "Minimize") : (m_maximized ? "Restore" : "Maximize"));
  175. m_window_menu->redraw();
  176. }
  177. }
  178. void Window::update_menu_item_enabled(PopupMenuItem item)
  179. {
  180. if (m_window_menu) {
  181. m_window_menu->item((int)item).set_enabled(item == PopupMenuItem::Minimize ? m_minimizable : m_resizable);
  182. m_window_menu->redraw();
  183. }
  184. }
  185. void Window::set_minimized(bool minimized)
  186. {
  187. if (m_minimized == minimized)
  188. return;
  189. if (minimized && !m_minimizable)
  190. return;
  191. if (is_blocked_by_modal_window())
  192. return;
  193. m_minimized = minimized;
  194. update_menu_item_text(PopupMenuItem::Minimize);
  195. start_minimize_animation();
  196. if (!minimized)
  197. request_update({ {}, size() });
  198. invalidate();
  199. WindowManager::the().notify_minimization_state_changed(*this);
  200. }
  201. void Window::set_minimizable(bool minimizable)
  202. {
  203. if (m_minimizable == minimizable)
  204. return;
  205. m_minimizable = minimizable;
  206. update_menu_item_enabled(PopupMenuItem::Minimize);
  207. // TODO: Hide/show (or alternatively change enabled state of) window minimize button dynamically depending on value of m_minimizable
  208. }
  209. void Window::set_opacity(float opacity)
  210. {
  211. if (m_opacity == opacity)
  212. return;
  213. m_opacity = opacity;
  214. WindowManager::the().notify_opacity_changed(*this);
  215. }
  216. void Window::set_occluded(bool occluded)
  217. {
  218. if (m_occluded == occluded)
  219. return;
  220. m_occluded = occluded;
  221. WindowManager::the().notify_occlusion_state_changed(*this);
  222. }
  223. void Window::set_maximized(bool maximized)
  224. {
  225. if (m_maximized == maximized)
  226. return;
  227. if (maximized && !is_resizable())
  228. return;
  229. if (is_blocked_by_modal_window())
  230. return;
  231. set_tiled(WindowTileType::None);
  232. m_maximized = maximized;
  233. update_menu_item_text(PopupMenuItem::Maximize);
  234. auto old_rect = m_rect;
  235. if (maximized) {
  236. m_unmaximized_rect = m_rect;
  237. set_rect(WindowManager::the().maximized_window_rect(*this));
  238. } else {
  239. set_rect(m_unmaximized_rect);
  240. }
  241. m_frame.did_set_maximized({}, maximized);
  242. Core::EventLoop::current().post_event(*this, make<ResizeEvent>(old_rect, m_rect));
  243. }
  244. void Window::set_resizable(bool resizable)
  245. {
  246. if (m_resizable == resizable)
  247. return;
  248. m_resizable = resizable;
  249. update_menu_item_enabled(PopupMenuItem::Maximize);
  250. // TODO: Hide/show (or alternatively change enabled state of) window maximize button dynamically depending on value of is_resizable()
  251. }
  252. void Window::event(Core::Event& event)
  253. {
  254. if (!m_client) {
  255. ASSERT(parent());
  256. event.ignore();
  257. return;
  258. }
  259. if (is_blocked_by_modal_window())
  260. return;
  261. if (static_cast<Event&>(event).is_mouse_event())
  262. return handle_mouse_event(static_cast<const MouseEvent&>(event));
  263. switch (event.type()) {
  264. case Event::WindowEntered:
  265. m_client->post_message(Messages::WindowClient::WindowEntered(m_window_id));
  266. break;
  267. case Event::WindowLeft:
  268. m_client->post_message(Messages::WindowClient::WindowLeft(m_window_id));
  269. break;
  270. case Event::KeyDown:
  271. m_client->post_message(
  272. Messages::WindowClient::KeyDown(m_window_id,
  273. (u8) static_cast<const KeyEvent&>(event).character(),
  274. (u32) static_cast<const KeyEvent&>(event).key(),
  275. static_cast<const KeyEvent&>(event).modifiers(),
  276. (u32) static_cast<const KeyEvent&>(event).scancode()));
  277. break;
  278. case Event::KeyUp:
  279. m_client->post_message(
  280. Messages::WindowClient::KeyUp(m_window_id,
  281. (u8) static_cast<const KeyEvent&>(event).character(),
  282. (u32) static_cast<const KeyEvent&>(event).key(),
  283. static_cast<const KeyEvent&>(event).modifiers(),
  284. (u32) static_cast<const KeyEvent&>(event).scancode()));
  285. break;
  286. case Event::WindowActivated:
  287. m_client->post_message(Messages::WindowClient::WindowActivated(m_window_id));
  288. break;
  289. case Event::WindowDeactivated:
  290. m_client->post_message(Messages::WindowClient::WindowDeactivated(m_window_id));
  291. break;
  292. case Event::WindowCloseRequest:
  293. m_client->post_message(Messages::WindowClient::WindowCloseRequest(m_window_id));
  294. break;
  295. case Event::WindowResized:
  296. m_client->post_message(
  297. Messages::WindowClient::WindowResized(
  298. m_window_id,
  299. static_cast<const ResizeEvent&>(event).old_rect(),
  300. static_cast<const ResizeEvent&>(event).rect()));
  301. break;
  302. default:
  303. break;
  304. }
  305. }
  306. void Window::set_global_cursor_tracking_enabled(bool enabled)
  307. {
  308. m_global_cursor_tracking_enabled = enabled;
  309. }
  310. void Window::set_visible(bool b)
  311. {
  312. if (m_visible == b)
  313. return;
  314. m_visible = b;
  315. invalidate();
  316. }
  317. void Window::invalidate()
  318. {
  319. Compositor::the().invalidate(frame().rect());
  320. }
  321. void Window::invalidate(const Gfx::Rect& rect)
  322. {
  323. if (type() == WindowType::MenuApplet) {
  324. AppletManager::the().invalidate_applet(*this, rect);
  325. return;
  326. }
  327. if (rect.is_empty()) {
  328. invalidate();
  329. return;
  330. }
  331. auto outer_rect = frame().rect();
  332. auto inner_rect = rect;
  333. inner_rect.move_by(position());
  334. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  335. inner_rect.intersect(outer_rect);
  336. Compositor::the().invalidate(inner_rect);
  337. }
  338. bool Window::is_active() const
  339. {
  340. return WindowManager::the().active_window() == this;
  341. }
  342. bool Window::is_blocked_by_modal_window() const
  343. {
  344. bool is_any_modal = false;
  345. const Window* next = this;
  346. while (!is_any_modal && next) {
  347. is_any_modal = next->is_modal();
  348. next = next->parent_window();
  349. }
  350. return !is_any_modal && client() && client()->is_showing_modal_window();
  351. }
  352. void Window::set_default_icon()
  353. {
  354. m_icon = default_window_icon();
  355. }
  356. void Window::request_update(const Gfx::Rect& rect, bool ignore_occlusion)
  357. {
  358. if (m_pending_paint_rects.is_empty()) {
  359. deferred_invoke([this, ignore_occlusion](auto&) {
  360. client()->post_paint_message(*this, ignore_occlusion);
  361. });
  362. }
  363. m_pending_paint_rects.add(rect);
  364. }
  365. void Window::popup_window_menu(const Gfx::Point& position)
  366. {
  367. if (!m_window_menu) {
  368. m_window_menu = Menu::construct(nullptr, -1, "(Window Menu)");
  369. m_window_menu->set_window_menu_of(*this);
  370. auto minimize_item = make<MenuItem>(*m_window_menu, 1, m_minimized ? "Unminimize" : "Minimize");
  371. m_window_menu_minimize_item = minimize_item.ptr();
  372. m_window_menu->add_item(move(minimize_item));
  373. auto maximize_item = make<MenuItem>(*m_window_menu, 2, m_maximized ? "Restore" : "Maximize");
  374. m_window_menu_maximize_item = maximize_item.ptr();
  375. m_window_menu->add_item(move(maximize_item));
  376. m_window_menu->add_item(make<MenuItem>(*m_window_menu, MenuItem::Type::Separator));
  377. auto close_item = make<MenuItem>(*m_window_menu, 3, "Close");
  378. close_item->set_icon(&close_icon());
  379. m_window_menu->add_item(move(close_item));
  380. m_window_menu->item((int)PopupMenuItem::Minimize).set_enabled(m_minimizable);
  381. m_window_menu->item((int)PopupMenuItem::Maximize).set_enabled(m_resizable);
  382. m_window_menu->on_item_activation = [&](auto& item) {
  383. switch (item.identifier()) {
  384. case 1:
  385. set_minimized(!m_minimized);
  386. if (!m_minimized)
  387. WindowManager::the().move_to_front_and_make_active(*this);
  388. break;
  389. case 2:
  390. set_maximized(!m_maximized);
  391. if (m_minimized)
  392. set_minimized(false);
  393. WindowManager::the().move_to_front_and_make_active(*this);
  394. break;
  395. case 3:
  396. request_close();
  397. break;
  398. }
  399. };
  400. }
  401. m_window_menu_minimize_item->set_icon(m_minimized ? nullptr : &minimize_icon());
  402. m_window_menu_maximize_item->set_icon(m_maximized ? &restore_icon() : &maximize_icon());
  403. m_window_menu->popup(position);
  404. }
  405. void Window::request_close()
  406. {
  407. Event close_request(Event::WindowCloseRequest);
  408. event(close_request);
  409. }
  410. void Window::set_fullscreen(bool fullscreen)
  411. {
  412. if (m_fullscreen == fullscreen)
  413. return;
  414. m_fullscreen = fullscreen;
  415. Gfx::Rect new_window_rect = m_rect;
  416. if (m_fullscreen) {
  417. m_saved_nonfullscreen_rect = m_rect;
  418. new_window_rect = Screen::the().rect();
  419. } else if (!m_saved_nonfullscreen_rect.is_empty()) {
  420. new_window_rect = m_saved_nonfullscreen_rect;
  421. }
  422. Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect, new_window_rect));
  423. set_rect(new_window_rect);
  424. }
  425. Gfx::Rect Window::tiled_rect(WindowTileType tiled) const
  426. {
  427. int frame_width = (m_frame.rect().width() - m_rect.width()) / 2;
  428. switch (tiled) {
  429. case WindowTileType::None:
  430. return m_untiled_rect;
  431. case WindowTileType::Left:
  432. return Gfx::Rect(0,
  433. WindowManager::the().maximized_window_rect(*this).y(),
  434. Screen::the().width() / 2 - frame_width,
  435. WindowManager::the().maximized_window_rect(*this).height());
  436. case WindowTileType::Right:
  437. return Gfx::Rect(Screen::the().width() / 2 + frame_width,
  438. WindowManager::the().maximized_window_rect(*this).y(),
  439. Screen::the().width() / 2 - frame_width,
  440. WindowManager::the().maximized_window_rect(*this).height());
  441. default:
  442. ASSERT_NOT_REACHED();
  443. }
  444. }
  445. void Window::set_tiled(WindowTileType tiled)
  446. {
  447. if (m_tiled == tiled)
  448. return;
  449. m_tiled = tiled;
  450. auto old_rect = m_rect;
  451. if (tiled != WindowTileType::None)
  452. m_untiled_rect = m_rect;
  453. set_rect(tiled_rect(tiled));
  454. Core::EventLoop::current().post_event(*this, make<ResizeEvent>(old_rect, m_rect));
  455. }
  456. void Window::detach_client(Badge<ClientConnection>)
  457. {
  458. m_client = nullptr;
  459. }
  460. void Window::recalculate_rect()
  461. {
  462. if (!is_resizable())
  463. return;
  464. auto old_rect = m_rect;
  465. if (m_tiled != WindowTileType::None)
  466. set_rect(tiled_rect(m_tiled));
  467. else if (is_maximized())
  468. set_rect(WindowManager::the().maximized_window_rect(*this));
  469. Core::EventLoop::current().post_event(*this, make<ResizeEvent>(old_rect, m_rect));
  470. }
  471. void Window::add_child_window(Window& child_window)
  472. {
  473. m_child_windows.append(child_window.make_weak_ptr());
  474. }
  475. void Window::set_parent_window(Window& parent_window)
  476. {
  477. ASSERT(!m_parent_window);
  478. m_parent_window = parent_window.make_weak_ptr();
  479. parent_window.add_child_window(*this);
  480. }
  481. void Window::set_progress(int progress)
  482. {
  483. if (m_progress == progress)
  484. return;
  485. m_progress = progress;
  486. WindowManager::the().notify_progress_changed(*this);
  487. }
  488. }