| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <AK/HashMap.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-18 20:39:46 +02:00
										 |  |  | #include <AK/JsonObject.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-26 22:13:50 +01:00
										 |  |  | #include <AK/NeverDestroyed.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-01 18:53:34 +01:00
										 |  |  | #include <AK/SharedBuffer.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-16 09:17:49 +01:00
										 |  |  | #include <LibCore/EventLoop.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 13:18:34 +01:00
										 |  |  | #include <LibCore/MimeData.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Action.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Application.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Event.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Painter.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Widget.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Window.h>
 | 
					
						
							|  |  |  | #include <LibGUI/WindowServerConnection.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 13:18:34 +01:00
										 |  |  | #include <LibGfx/Bitmap.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-28 12:03:17 +01:00
										 |  |  | #include <serenity.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-30 19:29:03 -06:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 08:27:13 +01:00
										 |  |  | //#define UPDATE_COALESCING_DEBUG
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | static NeverDestroyed<HashTable<Window*>> all_windows; | 
					
						
							|  |  |  | static NeverDestroyed<HashMap<int, Window*>> reified_windows; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Window* Window::from_window_id(int window_id) | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-26 22:13:50 +01:00
										 |  |  |     auto it = reified_windows->find(window_id); | 
					
						
							|  |  |  |     if (it != reified_windows->end()) | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  |         return (*it).value; | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Window::Window(Core::Object* parent) | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     : Core::Object(parent) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-26 22:13:50 +01:00
										 |  |  |     all_windows->set(this); | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  |     m_rect_when_windowless = { 100, 400, 140, 140 }; | 
					
						
							| 
									
										
										
										
											2020-03-05 09:25:05 +01:00
										 |  |  |     m_title_when_windowless = "GUI::Window"; | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Window::~Window() | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-26 22:13:50 +01:00
										 |  |  |     all_windows->remove(this); | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  |     hide(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::close() | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-21 18:53:17 +02:00
										 |  |  |     hide(); | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::move_to_front() | 
					
						
							| 
									
										
										
										
											2019-06-01 20:10:37 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-06-01 20:10:37 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::MoveWindowToFront>(m_window_id); | 
					
						
							| 
									
										
										
										
											2019-06-01 20:10:37 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::show() | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (is_visible()) | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-03-22 01:07:07 +01:00
										 |  |  |     m_override_cursor = StandardCursor::None; | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::CreateWindow>( | 
					
						
							| 
									
										
										
										
											2019-12-02 09:33:37 +01:00
										 |  |  |         m_rect_when_windowless, | 
					
						
							|  |  |  |         m_has_alpha_channel, | 
					
						
							|  |  |  |         m_modal, | 
					
						
							| 
									
										
										
										
											2020-01-04 13:12:02 +02:00
										 |  |  |         m_minimizable, | 
					
						
							| 
									
										
										
										
											2019-12-02 09:33:37 +01:00
										 |  |  |         m_resizable, | 
					
						
							|  |  |  |         m_fullscreen, | 
					
						
							|  |  |  |         m_show_titlebar, | 
					
						
							|  |  |  |         m_opacity_when_windowless, | 
					
						
							|  |  |  |         m_base_size, | 
					
						
							|  |  |  |         m_size_increment, | 
					
						
							|  |  |  |         (i32)m_window_type, | 
					
						
							|  |  |  |         m_title_when_windowless); | 
					
						
							|  |  |  |     m_window_id = response->window_id(); | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     m_visible = true; | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-02 14:02:56 -05:00
										 |  |  |     apply_icon(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 22:13:50 +01:00
										 |  |  |     reified_windows->set(m_window_id, this); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Application::the().did_create_window({}); | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  |     update(); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::hide() | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-12-26 22:13:50 +01:00
										 |  |  |     reified_windows->remove(m_window_id); | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::DestroyWindow>(m_window_id); | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |     m_window_id = 0; | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     m_visible = false; | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |     m_pending_paint_event_rects.clear(); | 
					
						
							|  |  |  |     m_back_bitmap = nullptr; | 
					
						
							|  |  |  |     m_front_bitmap = nullptr; | 
					
						
							| 
									
										
										
										
											2020-03-22 01:07:07 +01:00
										 |  |  |     m_override_cursor = StandardCursor::None; | 
					
						
							| 
									
										
										
										
											2019-09-21 18:53:17 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool app_has_visible_windows = false; | 
					
						
							| 
									
										
										
										
											2019-12-26 22:13:50 +01:00
										 |  |  |     for (auto& window : *all_windows) { | 
					
						
							| 
									
										
										
										
											2019-09-21 18:53:17 +02:00
										 |  |  |         if (window->is_visible()) { | 
					
						
							|  |  |  |             app_has_visible_windows = true; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!app_has_visible_windows) | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         Application::the().did_delete_last_window({}); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_title(const StringView& title) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  |     m_title_when_windowless = title; | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-02-14 01:21:32 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowTitle>(m_window_id, title); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-01-20 06:02:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | String Window::title() const | 
					
						
							| 
									
										
										
										
											2019-01-26 05:20:32 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-02-14 01:21:32 +01:00
										 |  |  |         return m_title_when_windowless; | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     return WindowServerConnection::the().send_sync<Messages::WindowServer::GetWindowTitle>(m_window_id)->title(); | 
					
						
							| 
									
										
										
										
											2019-01-26 05:20:32 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | Gfx::Rect Window::rect() const | 
					
						
							| 
									
										
										
										
											2019-02-08 00:13:35 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-02-14 01:21:32 +01:00
										 |  |  |         return m_rect_when_windowless; | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     return WindowServerConnection::the().send_sync<Messages::WindowServer::GetWindowRect>(m_window_id)->rect(); | 
					
						
							| 
									
										
										
										
											2019-02-08 00:13:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | void Window::set_rect(const Gfx::Rect& a_rect) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  |     m_rect_when_windowless = a_rect; | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) { | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  |         if (m_main_widget) | 
					
						
							|  |  |  |             m_main_widget->resize(m_rect_when_windowless.size()); | 
					
						
							| 
									
										
										
										
											2019-02-14 01:21:32 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowRect>(m_window_id, a_rect); | 
					
						
							| 
									
										
										
										
											2019-04-26 20:08:30 +02:00
										 |  |  |     if (m_back_bitmap && m_back_bitmap->size() != a_rect.size()) | 
					
						
							| 
									
										
										
										
											2019-04-10 14:35:13 +02:00
										 |  |  |         m_back_bitmap = nullptr; | 
					
						
							| 
									
										
										
										
											2019-04-26 20:08:30 +02:00
										 |  |  |     if (m_front_bitmap && m_front_bitmap->size() != a_rect.size()) | 
					
						
							| 
									
										
										
										
											2019-04-10 14:35:13 +02:00
										 |  |  |         m_front_bitmap = nullptr; | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |     if (m_main_widget) | 
					
						
							|  |  |  |         m_main_widget->resize(a_rect.size()); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_window_type(WindowType window_type) | 
					
						
							| 
									
										
										
										
											2019-04-03 19:38:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_window_type = window_type; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_override_cursor(StandardCursor cursor) | 
					
						
							| 
									
										
										
										
											2019-03-31 23:52:02 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-03-31 23:52:02 +02:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-03-22 01:07:07 +01:00
										 |  |  |     if (m_override_cursor == cursor) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowOverrideCursor>(m_window_id, (u32)cursor); | 
					
						
							| 
									
										
										
										
											2020-03-22 01:07:07 +01:00
										 |  |  |     m_override_cursor = cursor; | 
					
						
							| 
									
										
										
										
											2019-03-31 23:52:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::event(Core::Event& event) | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.type() == Event::Drop) { | 
					
						
							|  |  |  |         auto& drop_event = static_cast<DropEvent&>(event); | 
					
						
							| 
									
										
										
										
											2019-12-08 16:50:23 +01:00
										 |  |  |         if (!m_main_widget) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         auto result = m_main_widget->hit_test(drop_event.position()); | 
					
						
							| 
									
										
										
										
											2020-02-14 13:18:34 +01:00
										 |  |  |         auto local_event = make<DropEvent>(result.local_position, drop_event.text(), drop_event.mime_data()); | 
					
						
							| 
									
										
										
										
											2019-12-08 16:50:23 +01:00
										 |  |  |         ASSERT(result.widget); | 
					
						
							|  |  |  |         return result.widget->dispatch_event(*local_event, this); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.type() == Event::MouseUp || event.type() == Event::MouseDown || event.type() == Event::MouseDoubleClick || event.type() == Event::MouseMove || event.type() == Event::MouseWheel) { | 
					
						
							|  |  |  |         auto& mouse_event = static_cast<MouseEvent&>(event); | 
					
						
							| 
									
										
										
										
											2019-01-27 08:48:34 +01:00
										 |  |  |         if (m_global_cursor_tracking_widget) { | 
					
						
							| 
									
										
										
										
											2019-02-26 10:34:05 +01:00
										 |  |  |             auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect(); | 
					
						
							| 
									
										
										
										
											2020-02-06 13:08:32 +01:00
										 |  |  |             Gfx::Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() }; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |             auto local_event = make<MouseEvent>((Event::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta()); | 
					
						
							| 
									
										
										
										
											2019-09-20 20:37:31 +02:00
										 |  |  |             m_global_cursor_tracking_widget->dispatch_event(*local_event, this); | 
					
						
							| 
									
										
										
										
											2019-03-24 15:01:56 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (m_automatic_cursor_tracking_widget) { | 
					
						
							|  |  |  |             auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect(); | 
					
						
							| 
									
										
										
										
											2020-02-06 13:08:32 +01:00
										 |  |  |             Gfx::Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() }; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |             auto local_event = make<MouseEvent>((Event::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta()); | 
					
						
							| 
									
										
										
										
											2019-09-20 20:37:31 +02:00
										 |  |  |             m_automatic_cursor_tracking_widget->dispatch_event(*local_event, this); | 
					
						
							| 
									
										
										
										
											2019-03-25 01:42:15 +01:00
										 |  |  |             if (mouse_event.buttons() == 0) | 
					
						
							| 
									
										
										
										
											2019-03-24 15:01:56 +01:00
										 |  |  |                 m_automatic_cursor_tracking_widget = nullptr; | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2019-01-27 08:48:34 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-01-20 07:03:38 +01:00
										 |  |  |         if (!m_main_widget) | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2019-05-13 19:52:57 +02:00
										 |  |  |         auto result = m_main_widget->hit_test(mouse_event.position()); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         auto local_event = make<MouseEvent>((Event::Type)event.type(), result.local_position, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta()); | 
					
						
							| 
									
										
										
										
											2019-05-13 19:52:57 +02:00
										 |  |  |         ASSERT(result.widget); | 
					
						
							|  |  |  |         set_hovered_widget(result.widget); | 
					
						
							|  |  |  |         if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget) | 
					
						
							|  |  |  |             m_automatic_cursor_tracking_widget = result.widget->make_weak_ptr(); | 
					
						
							|  |  |  |         if (result.widget != m_global_cursor_tracking_widget.ptr()) | 
					
						
							| 
									
										
										
										
											2019-09-20 20:37:31 +02:00
										 |  |  |             return result.widget->dispatch_event(*local_event, this); | 
					
						
							| 
									
										
										
										
											2019-01-26 21:58:43 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-01-20 07:03:38 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.type() == Event::MultiPaint) { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |         if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2019-01-20 07:03:38 +01:00
										 |  |  |         if (!m_main_widget) | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         auto& paint_event = static_cast<MultiPaintEvent&>(event); | 
					
						
							| 
									
										
										
										
											2019-04-20 17:37:28 +02:00
										 |  |  |         auto rects = paint_event.rects(); | 
					
						
							|  |  |  |         ASSERT(!rects.is_empty()); | 
					
						
							| 
									
										
										
										
											2019-04-10 15:39:28 +02:00
										 |  |  |         if (m_back_bitmap && m_back_bitmap->size() != paint_event.window_size()) { | 
					
						
							|  |  |  |             // Eagerly discard the backing store if we learn from this paint event that it needs to be bigger.
 | 
					
						
							|  |  |  |             // Otherwise we would have to wait for a resize event to tell us. This way we don't waste the
 | 
					
						
							|  |  |  |             // effort on painting into an undersized bitmap that will be thrown away anyway.
 | 
					
						
							|  |  |  |             m_back_bitmap = nullptr; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  |         bool created_new_backing_store = !m_back_bitmap; | 
					
						
							| 
									
										
										
										
											2019-12-09 20:08:44 +01:00
										 |  |  |         if (!m_back_bitmap) { | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  |             m_back_bitmap = create_backing_bitmap(paint_event.window_size()); | 
					
						
							| 
									
										
										
										
											2019-12-09 20:08:44 +01:00
										 |  |  |         } else if (m_double_buffering_enabled) { | 
					
						
							|  |  |  |             bool still_has_pixels = m_back_bitmap->shared_buffer()->set_nonvolatile(); | 
					
						
							|  |  |  |             if (!still_has_pixels) { | 
					
						
							|  |  |  |                 m_back_bitmap = create_backing_bitmap(paint_event.window_size()); | 
					
						
							|  |  |  |                 created_new_backing_store = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 17:37:28 +02:00
										 |  |  |         auto rect = rects.first(); | 
					
						
							|  |  |  |         if (rect.is_empty() || created_new_backing_store) { | 
					
						
							|  |  |  |             rects.clear(); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |             rects.append({ {}, paint_event.window_size() }); | 
					
						
							| 
									
										
										
										
											2019-04-20 17:37:28 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-21 21:05:10 +02:00
										 |  |  |         for (auto& rect : rects) | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |             m_main_widget->dispatch_event(*make<PaintEvent>(rect), this); | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-21 21:05:10 +02:00
										 |  |  |         if (m_double_buffering_enabled) | 
					
						
							|  |  |  |             flip(rects); | 
					
						
							|  |  |  |         else if (created_new_backing_store) | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  |             set_current_backing_bitmap(*m_back_bitmap, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |         if (is_visible()) { | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  |             Vector<Gfx::Rect> rects_to_send; | 
					
						
							| 
									
										
										
										
											2019-12-02 09:33:37 +01:00
										 |  |  |             for (auto& r : rects) | 
					
						
							|  |  |  |                 rects_to_send.append(r); | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |             WindowServerConnection::the().post_message(Messages::WindowServer::DidFinishPainting(m_window_id, rects_to_send)); | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-01-26 21:58:43 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-01-20 07:03:38 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.type() == Event::KeyUp || event.type() == Event::KeyDown) { | 
					
						
							| 
									
										
										
										
											2019-12-26 23:29:18 +01:00
										 |  |  |         if (m_focused_widget) | 
					
						
							|  |  |  |             return m_focused_widget->dispatch_event(event, this); | 
					
						
							|  |  |  |         if (m_main_widget) | 
					
						
							|  |  |  |             return m_main_widget->dispatch_event(event, this); | 
					
						
							| 
									
										
										
										
											2019-02-10 14:28:39 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-01-26 06:39:13 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.type() == Event::WindowBecameActive || event.type() == Event::WindowBecameInactive) { | 
					
						
							|  |  |  |         m_is_active = event.type() == Event::WindowBecameActive; | 
					
						
							| 
									
										
										
										
											2019-02-10 14:28:39 +01:00
										 |  |  |         if (m_main_widget) | 
					
						
							| 
									
										
										
										
											2019-09-20 20:37:31 +02:00
										 |  |  |             m_main_widget->dispatch_event(event, this); | 
					
						
							| 
									
										
										
										
											2019-01-26 21:58:43 +01:00
										 |  |  |         if (m_focused_widget) | 
					
						
							|  |  |  |             m_focused_widget->update(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.type() == Event::WindowCloseRequest) { | 
					
						
							| 
									
										
										
										
											2019-08-27 20:35:37 +02:00
										 |  |  |         if (on_close_request) { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |             if (on_close_request() == Window::CloseRequestDecision::StayOpen) | 
					
						
							| 
									
										
										
										
											2019-08-27 20:35:37 +02:00
										 |  |  |                 return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-02-05 10:31:37 +01:00
										 |  |  |         close(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.type() == Event::WindowLeft) { | 
					
						
							| 
									
										
										
										
											2019-02-20 10:12:19 +01:00
										 |  |  |         set_hovered_widget(nullptr); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.type() == Event::Resize) { | 
					
						
							|  |  |  |         auto new_size = static_cast<ResizeEvent&>(event).size(); | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  |         if (m_back_bitmap && m_back_bitmap->size() != new_size) | 
					
						
							|  |  |  |             m_back_bitmap = nullptr; | 
					
						
							| 
									
										
										
										
											2019-04-22 01:15:47 +02:00
										 |  |  |         if (!m_pending_paint_event_rects.is_empty()) { | 
					
						
							|  |  |  |             m_pending_paint_event_rects.clear_with_capacity(); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |             m_pending_paint_event_rects.append({ {}, new_size }); | 
					
						
							| 
									
										
										
										
											2019-04-22 01:15:47 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         m_rect_when_windowless = { {}, new_size }; | 
					
						
							|  |  |  |         m_main_widget->set_relative_rect({ {}, new_size }); | 
					
						
							| 
									
										
										
										
											2019-02-20 15:34:55 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     if (event.type() > Event::__Begin_WM_Events && event.type() < Event::__End_WM_Events) | 
					
						
							|  |  |  |         return wm_event(static_cast<WMEvent&>(event)); | 
					
						
							| 
									
										
										
										
											2019-04-03 21:03:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-13 21:43:32 +01:00
										 |  |  |     if (event.type() == Event::DragMove) { | 
					
						
							|  |  |  |         if (!m_main_widget) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         auto& drag_event = static_cast<DragEvent&>(event); | 
					
						
							|  |  |  |         auto result = m_main_widget->hit_test(drag_event.position()); | 
					
						
							|  |  |  |         auto local_event = make<DragEvent>(static_cast<Event::Type>(drag_event.type()), result.local_position, drag_event.data_type()); | 
					
						
							|  |  |  |         ASSERT(result.widget); | 
					
						
							|  |  |  |         return result.widget->dispatch_event(*local_event, this); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-16 13:36:21 +02:00
										 |  |  |     if (event.type() == Event::ThemeChange) { | 
					
						
							|  |  |  |         if (!m_main_widget) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         auto theme_event = static_cast<ThemeChangeEvent&>(event); | 
					
						
							|  |  |  |         auto dispatch_theme_change = [&](auto& widget, auto recursive) { | 
					
						
							|  |  |  |             widget.dispatch_event(theme_event, this); | 
					
						
							|  |  |  |             widget.for_each_child_widget([&](auto& widget) -> IterationDecision { | 
					
						
							|  |  |  |                 widget.dispatch_event(theme_event, this); | 
					
						
							|  |  |  |                 recursive(widget, recursive); | 
					
						
							|  |  |  |                 return IterationDecision::Continue; | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         dispatch_theme_change(*m_main_widget.ptr(), dispatch_theme_change); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     Core::Object::event(event); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | bool Window::is_visible() const | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     return m_visible; | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::update() | 
					
						
							| 
									
										
										
										
											2019-12-27 11:34:40 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-19 16:45:06 +01:00
										 |  |  |     auto rect = this->rect(); | 
					
						
							|  |  |  |     update({ 0, 0, rect.width(), rect.height() }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Window::force_update() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2020-03-11 20:28:19 +00:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-02-19 16:45:06 +01:00
										 |  |  |     auto rect = this->rect(); | 
					
						
							|  |  |  |     WindowServerConnection::the().post_message(Messages::WindowServer::InvalidateRect(m_window_id, { { 0, 0, rect.width(), rect.height() } }, true)); | 
					
						
							| 
									
										
										
										
											2019-12-27 11:34:40 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | void Window::update(const Gfx::Rect& a_rect) | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-01-30 20:03:52 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-06-03 16:02:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-10 14:46:43 +01:00
										 |  |  |     for (auto& pending_rect : m_pending_paint_event_rects) { | 
					
						
							|  |  |  |         if (pending_rect.contains(a_rect)) { | 
					
						
							| 
									
										
										
										
											2019-02-11 08:27:13 +01:00
										 |  |  | #ifdef UPDATE_COALESCING_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-02-10 14:46:43 +01:00
										 |  |  |             dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters()); | 
					
						
							| 
									
										
										
										
											2019-02-11 08:27:13 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-02-28 13:58:04 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2019-02-10 14:46:43 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 15:19:02 +02:00
										 |  |  |     if (m_pending_paint_event_rects.is_empty()) { | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         deferred_invoke([this](auto&) { | 
					
						
							| 
									
										
										
										
											2019-04-23 20:43:51 +02:00
										 |  |  |             auto rects = move(m_pending_paint_event_rects); | 
					
						
							|  |  |  |             if (rects.is_empty()) | 
					
						
							|  |  |  |                 return; | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  |             Vector<Gfx::Rect> rects_to_send; | 
					
						
							| 
									
										
										
										
											2019-12-02 09:33:37 +01:00
										 |  |  |             for (auto& r : rects) | 
					
						
							|  |  |  |                 rects_to_send.append(r); | 
					
						
							| 
									
										
										
										
											2020-02-19 16:45:06 +01:00
										 |  |  |             WindowServerConnection::the().post_message(Messages::WindowServer::InvalidateRect(m_window_id, rects_to_send, false)); | 
					
						
							| 
									
										
										
										
											2019-04-20 15:19:02 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     m_pending_paint_event_rects.append(a_rect); | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_main_widget(Widget* widget) | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_main_widget == widget) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-08-18 12:17:09 +02:00
										 |  |  |     if (m_main_widget) | 
					
						
							|  |  |  |         remove_child(*m_main_widget); | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  |     m_main_widget = widget; | 
					
						
							| 
									
										
										
										
											2019-02-10 14:28:39 +01:00
										 |  |  |     if (m_main_widget) { | 
					
						
							| 
									
										
										
										
											2019-08-18 12:17:09 +02:00
										 |  |  |         add_child(*widget); | 
					
						
							| 
									
										
										
										
											2019-02-10 14:28:39 +01:00
										 |  |  |         auto new_window_rect = rect(); | 
					
						
							|  |  |  |         if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed) | 
					
						
							|  |  |  |             new_window_rect.set_width(m_main_widget->preferred_size().width()); | 
					
						
							|  |  |  |         if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed) | 
					
						
							|  |  |  |             new_window_rect.set_height(m_main_widget->preferred_size().height()); | 
					
						
							|  |  |  |         set_rect(new_window_rect); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         m_main_widget->set_relative_rect({ {}, new_window_rect.size() }); | 
					
						
							| 
									
										
										
										
											2019-02-10 14:28:39 +01:00
										 |  |  |         m_main_widget->set_window(this); | 
					
						
							|  |  |  |         if (m_main_widget->accepts_focus()) | 
					
						
							|  |  |  |             m_main_widget->set_focus(true); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-20 05:48:43 +01:00
										 |  |  |     update(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-01-26 06:39:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_focused_widget(Widget* widget) | 
					
						
							| 
									
										
										
										
											2019-01-26 06:39:13 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_focused_widget == widget) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  |     if (m_focused_widget) { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         Core::EventLoop::current().post_event(*m_focused_widget, make<Event>(Event::FocusOut)); | 
					
						
							| 
									
										
										
										
											2019-01-26 06:39:13 +01:00
										 |  |  |         m_focused_widget->update(); | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-18 23:16:57 +02:00
										 |  |  |     m_focused_widget = widget ? widget->make_weak_ptr() : nullptr; | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  |     if (m_focused_widget) { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         Core::EventLoop::current().post_event(*m_focused_widget, make<Event>(Event::FocusIn)); | 
					
						
							| 
									
										
										
										
											2019-01-26 11:24:16 +01:00
										 |  |  |         m_focused_widget->update(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-26 06:39:13 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-01-27 08:48:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_global_cursor_tracking_widget(Widget* widget) | 
					
						
							| 
									
										
										
										
											2019-01-27 08:48:34 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-14 02:36:06 +02:00
										 |  |  |     if (widget == m_global_cursor_tracking_widget) | 
					
						
							| 
									
										
										
										
											2019-01-27 08:48:34 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-01-31 17:31:23 +01:00
										 |  |  |     m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr; | 
					
						
							| 
									
										
										
										
											2019-03-24 15:01:56 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-02-14 01:21:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_automatic_cursor_tracking_widget(Widget* widget) | 
					
						
							| 
									
										
										
										
											2019-03-24 15:01:56 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-14 02:36:06 +02:00
										 |  |  |     if (widget == m_automatic_cursor_tracking_widget) | 
					
						
							| 
									
										
										
										
											2019-03-24 15:01:56 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr; | 
					
						
							| 
									
										
										
										
											2019-01-27 08:48:34 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-02-19 01:42:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_has_alpha_channel(bool value) | 
					
						
							| 
									
										
										
										
											2019-02-19 01:42:53 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-03 21:07:16 +02:00
										 |  |  |     if (m_has_alpha_channel == value) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-02-19 01:42:53 +01:00
										 |  |  |     m_has_alpha_channel = value; | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-05-03 21:07:16 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_pending_paint_event_rects.clear(); | 
					
						
							|  |  |  |     m_back_bitmap = nullptr; | 
					
						
							|  |  |  |     m_front_bitmap = nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowHasAlphaChannel>(m_window_id, value); | 
					
						
							| 
									
										
										
										
											2019-05-03 21:07:16 +02:00
										 |  |  |     update(); | 
					
						
							| 
									
										
										
										
											2019-02-19 01:42:53 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_double_buffering_enabled(bool value) | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     ASSERT(!is_visible()); | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  |     m_double_buffering_enabled = value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_opacity(float opacity) | 
					
						
							| 
									
										
										
										
											2019-02-19 01:42:53 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     m_opacity_when_windowless = opacity; | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-02-19 01:42:53 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowOpacity>(m_window_id, opacity); | 
					
						
							| 
									
										
										
										
											2019-02-19 01:42:53 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-02-20 10:12:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_hovered_widget(Widget* widget) | 
					
						
							| 
									
										
										
										
											2019-02-20 10:12:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-14 02:36:06 +02:00
										 |  |  |     if (widget == m_hovered_widget) | 
					
						
							| 
									
										
										
										
											2019-02-20 10:12:19 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_hovered_widget) | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Leave)); | 
					
						
							| 
									
										
										
										
											2019-02-20 10:12:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_hovered_widget) | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Enter)); | 
					
						
							| 
									
										
										
										
											2019-02-20 10:12:19 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | void Window::set_current_backing_bitmap(Gfx::Bitmap& bitmap, bool flush_immediately) | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-28 11:45:19 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBackingStore>(m_window_id, 32, bitmap.pitch(), bitmap.shbuf_id(), bitmap.has_alpha_channel(), bitmap.size(), flush_immediately); | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 13:02:38 +01:00
										 |  |  | void Window::flip(const Vector<Gfx::Rect, 32>& dirty_rects) | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     swap(m_front_bitmap, m_back_bitmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     set_current_backing_bitmap(*m_front_bitmap); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) { | 
					
						
							|  |  |  |         m_back_bitmap = create_backing_bitmap(m_front_bitmap->size()); | 
					
						
							| 
									
										
										
										
											2019-05-08 03:33:44 +02:00
										 |  |  |         memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes()); | 
					
						
							| 
									
										
										
										
											2019-12-09 21:33:39 +01:00
										 |  |  |         m_back_bitmap->shared_buffer()->set_volatile(); | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Copy whatever was painted from the front to the back.
 | 
					
						
							|  |  |  |     Painter painter(*m_back_bitmap); | 
					
						
							| 
									
										
										
										
											2019-05-21 21:05:10 +02:00
										 |  |  |     for (auto& dirty_rect : dirty_rects) | 
					
						
							|  |  |  |         painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect); | 
					
						
							| 
									
										
										
										
											2019-12-09 20:08:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_back_bitmap->shared_buffer()->set_volatile(); | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  | NonnullRefPtr<Gfx::Bitmap> Window::create_shared_bitmap(Gfx::BitmapFormat format, const Gfx::Size& size) | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     ASSERT(WindowServerConnection::the().server_pid()); | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  |     ASSERT(!size.is_empty()); | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     size_t pitch = round_up_to_power_of_two(size.width() * sizeof(Gfx::RGBA32), 16); | 
					
						
							| 
									
										
										
										
											2019-05-06 14:04:54 +02:00
										 |  |  |     size_t size_in_bytes = size.height() * pitch; | 
					
						
							| 
									
										
										
										
											2019-07-18 09:52:22 +02:00
										 |  |  |     auto shared_buffer = SharedBuffer::create_with_size(size_in_bytes); | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  |     ASSERT(shared_buffer); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     shared_buffer->share_with(WindowServerConnection::the().server_pid()); | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     return Gfx::Bitmap::create_with_shared_buffer(format, *shared_buffer, size); | 
					
						
							| 
									
										
										
										
											2019-03-17 04:23:54 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | NonnullRefPtr<Gfx::Bitmap> Window::create_backing_bitmap(const Gfx::Size& size) | 
					
						
							| 
									
										
										
										
											2019-07-28 10:18:49 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  |     auto format = m_has_alpha_channel ? Gfx::BitmapFormat::RGBA32 : Gfx::BitmapFormat::RGB32; | 
					
						
							| 
									
										
										
										
											2019-07-28 10:18:49 +02:00
										 |  |  |     return create_shared_bitmap(format, size); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_modal(bool modal) | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     ASSERT(!is_visible()); | 
					
						
							| 
									
										
										
										
											2019-03-19 00:01:02 +01:00
										 |  |  |     m_modal = modal; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-03 21:03:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::wm_event(WMEvent&) | 
					
						
							| 
									
										
										
										
											2019-04-03 21:03:12 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-13 16:59:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  | void Window::set_icon(const Gfx::Bitmap* icon) | 
					
						
							| 
									
										
										
										
											2019-07-28 10:18:49 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_icon == icon) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:02:47 +01:00
										 |  |  |     m_icon = create_shared_bitmap(Gfx::BitmapFormat::RGBA32, icon->size()); | 
					
						
							| 
									
										
										
										
											2019-07-28 10:18:49 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         Painter painter(*m_icon); | 
					
						
							| 
									
										
										
										
											2019-07-28 10:18:49 +02:00
										 |  |  |         painter.blit({ 0, 0 }, *icon, icon->rect()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-02 14:02:56 -05:00
										 |  |  |     apply_icon(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::apply_icon() | 
					
						
							| 
									
										
										
										
											2019-08-02 14:02:56 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!m_icon) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-08-02 14:02:56 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-28 11:45:19 +01:00
										 |  |  |     int rc = shbuf_seal(m_icon->shbuf_id()); | 
					
						
							| 
									
										
										
										
											2019-07-29 07:26:01 +02:00
										 |  |  |     ASSERT(rc == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-28 11:45:19 +01:00
										 |  |  |     rc = shbuf_allow_all(m_icon->shbuf_id()); | 
					
						
							| 
									
										
										
										
											2019-07-29 07:26:01 +02:00
										 |  |  |     ASSERT(rc == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static bool has_set_process_icon; | 
					
						
							|  |  |  |     if (!has_set_process_icon) | 
					
						
							| 
									
										
										
										
											2020-02-28 11:45:19 +01:00
										 |  |  |         set_process_icon(m_icon->shbuf_id()); | 
					
						
							| 
									
										
										
										
											2019-07-29 07:26:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-28 11:45:19 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowIconBitmap>(m_window_id, m_icon->shbuf_id(), m_icon->size()); | 
					
						
							| 
									
										
										
										
											2019-07-28 10:18:49 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::start_wm_resize() | 
					
						
							| 
									
										
										
										
											2019-05-03 01:38:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     WindowServerConnection::the().post_message(Messages::WindowServer::WM_StartWindowResize(WindowServerConnection::the().my_client_id(), m_window_id)); | 
					
						
							| 
									
										
										
										
											2019-05-03 01:38:24 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-15 02:39:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Vector<Widget*> Window::focusable_widgets() const | 
					
						
							| 
									
										
										
										
											2019-05-15 02:39:58 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!m_main_widget) | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2019-05-15 02:39:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Vector<Widget*> collected_widgets; | 
					
						
							| 
									
										
										
										
											2019-05-15 02:39:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Function<void(Widget&)> collect_focusable_widgets = [&](auto& widget) { | 
					
						
							| 
									
										
										
										
											2019-05-15 02:39:58 +02:00
										 |  |  |         if (widget.accepts_focus()) | 
					
						
							|  |  |  |             collected_widgets.append(&widget); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         widget.for_each_child_widget([&](auto& child) { | 
					
						
							| 
									
										
										
										
											2019-05-27 03:52:33 +02:00
										 |  |  |             if (!child.is_visible()) | 
					
						
							|  |  |  |                 return IterationDecision::Continue; | 
					
						
							|  |  |  |             if (!child.is_enabled()) | 
					
						
							|  |  |  |                 return IterationDecision::Continue; | 
					
						
							|  |  |  |             collect_focusable_widgets(child); | 
					
						
							|  |  |  |             return IterationDecision::Continue; | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-05-15 02:39:58 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     collect_focusable_widgets(const_cast<Widget&>(*m_main_widget)); | 
					
						
							| 
									
										
										
										
											2019-05-15 02:39:58 +02:00
										 |  |  |     return collected_widgets; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-18 20:39:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::save_to(AK::JsonObject& json) | 
					
						
							| 
									
										
										
										
											2019-08-18 20:39:46 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     json.set("title", title()); | 
					
						
							|  |  |  |     json.set("visible", is_visible()); | 
					
						
							|  |  |  |     json.set("active", is_active()); | 
					
						
							| 
									
										
										
										
											2020-01-04 13:12:02 +02:00
										 |  |  |     json.set("minimizable", is_minimizable()); | 
					
						
							| 
									
										
										
										
											2019-08-18 20:39:46 +02:00
										 |  |  |     json.set("resizable", is_resizable()); | 
					
						
							|  |  |  |     json.set("fullscreen", is_fullscreen()); | 
					
						
							|  |  |  |     json.set("rect", rect().to_string()); | 
					
						
							|  |  |  |     json.set("base_size", base_size().to_string()); | 
					
						
							|  |  |  |     json.set("size_increment", size_increment().to_string()); | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     Core::Object::save_to(json); | 
					
						
							| 
									
										
										
										
											2019-08-18 20:39:46 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-09-16 18:38:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::set_fullscreen(bool fullscreen) | 
					
						
							| 
									
										
										
										
											2019-09-16 18:38:42 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_fullscreen == fullscreen) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_fullscreen = fullscreen; | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (!is_visible()) | 
					
						
							| 
									
										
										
										
											2019-09-16 18:38:42 +02:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::SetFullscreen>(m_window_id, fullscreen); | 
					
						
							| 
									
										
										
										
											2019-09-16 18:38:42 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-26 12:27:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::schedule_relayout() | 
					
						
							| 
									
										
										
										
											2019-10-26 12:27:01 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_layout_pending) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_layout_pending = true; | 
					
						
							|  |  |  |     deferred_invoke([this](auto&) { | 
					
						
							|  |  |  |         if (main_widget()) | 
					
						
							|  |  |  |             main_widget()->do_layout(); | 
					
						
							|  |  |  |         update(); | 
					
						
							|  |  |  |         m_layout_pending = false; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-23 20:24:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-16 13:36:21 +02:00
										 |  |  | void Window::for_each_window(Badge<WindowServerConnection>, Function<void(Window&)> callback) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (auto& e : *reified_windows) { | 
					
						
							|  |  |  |         ASSERT(e.value); | 
					
						
							|  |  |  |         callback(*e.value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::update_all_windows(Badge<WindowServerConnection>) | 
					
						
							| 
									
										
										
										
											2019-12-23 20:24:26 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-11 20:28:19 +00:00
										 |  |  |     for (auto& e : *reified_windows) { | 
					
						
							|  |  |  |         e.value->force_update(); | 
					
						
							| 
									
										
										
										
											2019-12-23 20:24:26 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-26 12:06:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Window::notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded) | 
					
						
							| 
									
										
										
										
											2019-12-26 12:06:07 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-29 15:58:07 +01:00
										 |  |  |     m_visible_for_timer_purposes = !minimized && !occluded; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-27 11:34:40 +01:00
										 |  |  |     // When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
 | 
					
						
							| 
									
										
										
										
											2019-12-26 12:06:07 +01:00
										 |  |  |     // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
 | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     RefPtr<Gfx::Bitmap>& bitmap = m_double_buffering_enabled ? m_front_bitmap : m_back_bitmap; | 
					
						
							| 
									
										
										
										
											2019-12-26 12:06:07 +01:00
										 |  |  |     if (!bitmap) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-12-27 11:34:40 +01:00
										 |  |  |     if (minimized || occluded) { | 
					
						
							| 
									
										
										
										
											2019-12-26 12:06:07 +01:00
										 |  |  |         bitmap->shared_buffer()->set_volatile(); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-12-27 11:34:40 +01:00
										 |  |  |         if (!bitmap->shared_buffer()->set_nonvolatile()) { | 
					
						
							| 
									
										
										
										
											2019-12-26 12:06:07 +01:00
										 |  |  |             bitmap = nullptr; | 
					
						
							| 
									
										
										
										
											2019-12-27 11:34:40 +01:00
										 |  |  |             update(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-12-26 12:06:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Action* Window::action_for_key_event(const KeyEvent& event) | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     Shortcut shortcut(event.modifiers(), (KeyCode)event.key()); | 
					
						
							|  |  |  |     Action* found_action = nullptr; | 
					
						
							|  |  |  |     for_each_child_of_type<Action>([&](auto& action) { | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  |         if (action.shortcut() == shortcut) { | 
					
						
							|  |  |  |             found_action = &action; | 
					
						
							|  |  |  |             return IterationDecision::Break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return IterationDecision::Continue; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return found_action; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-24 19:23:57 +01:00
										 |  |  | void Window::set_base_size(const Gfx::Size& base_size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_base_size == base_size) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_base_size = base_size; | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (is_visible()) | 
					
						
							| 
									
										
										
										
											2020-02-24 19:23:57 +01:00
										 |  |  |         WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement>(m_window_id, m_base_size, m_size_increment); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Window::set_size_increment(const Gfx::Size& size_increment) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_size_increment == size_increment) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_size_increment = size_increment; | 
					
						
							| 
									
										
										
										
											2020-03-12 11:32:28 +00:00
										 |  |  |     if (is_visible()) | 
					
						
							| 
									
										
										
										
											2020-02-24 19:23:57 +01:00
										 |  |  |         WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement>(m_window_id, m_base_size, m_size_increment); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-05 15:30:11 +01:00
										 |  |  | void Window::did_remove_widget(Badge<Widget>, const Widget& widget) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_focused_widget == &widget) | 
					
						
							|  |  |  |         m_focused_widget = nullptr; | 
					
						
							|  |  |  |     if (m_hovered_widget == &widget) | 
					
						
							|  |  |  |         m_hovered_widget = nullptr; | 
					
						
							| 
									
										
										
										
											2020-03-05 19:49:18 +01:00
										 |  |  |     if (m_global_cursor_tracking_widget == &widget) | 
					
						
							| 
									
										
										
										
											2020-03-05 15:30:11 +01:00
										 |  |  |         m_global_cursor_tracking_widget = nullptr; | 
					
						
							| 
									
										
										
										
											2020-03-05 19:49:18 +01:00
										 |  |  |     if (m_automatic_cursor_tracking_widget == &widget) | 
					
						
							| 
									
										
										
										
											2020-03-05 15:30:11 +01:00
										 |  |  |         m_automatic_cursor_tracking_widget = nullptr; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | } |