From 7323a54e59ccb23e82d9629c123f88d717bfe460 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Sat, 13 May 2023 05:07:41 -0400 Subject: LibGUI+FileSystemAccessServer: Avoid using dummy windows Creates two new gatekept helpers for FilePicker and MessageBox to be used by FSAS to replace the "dummy window" approach to centering Dialogs. There was a slight delay in creating two windows, one a transparent intermediary hidden behind the second, to display FSAS Dialogs. Now we only need to make the window we actually see. --- Userland/Libraries/LibGUI/FilePicker.cpp | 21 +++++++++++++ Userland/Libraries/LibGUI/FilePicker.h | 8 +++++ Userland/Libraries/LibGUI/MessageBox.cpp | 13 +++++++++ Userland/Libraries/LibGUI/MessageBox.h | 7 +++++ .../ConnectionFromClient.cpp | 34 ++++++---------------- .../FileSystemAccessServer/ConnectionFromClient.h | 1 - 6 files changed, 58 insertions(+), 26 deletions(-) diff --git a/Userland/Libraries/LibGUI/FilePicker.cpp b/Userland/Libraries/LibGUI/FilePicker.cpp index 59927b525d..3a099939e6 100644 --- a/Userland/Libraries/LibGUI/FilePicker.cpp +++ b/Userland/Libraries/LibGUI/FilePicker.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,26 @@ namespace GUI { +ErrorOr> FilePicker::get_filepath(Badge, i32 window_server_client_id, i32 parent_window_id, Mode mode, StringView window_title, StringView file_basename, StringView path, Optional> allowed_file_types) +{ + auto picker = TRY(FilePicker::try_create(nullptr, mode, file_basename, path, ScreenPosition::DoNotPosition, move(allowed_file_types))); + auto parent_rect = ConnectionToWindowServer::the().get_window_rect_from_client(window_server_client_id, parent_window_id); + picker->center_within(parent_rect); + picker->constrain_to_desktop(); + if (!window_title.is_empty()) + picker->set_title(window_title); + picker->show(); + ConnectionToWindowServer::the().set_window_parent_from_client(window_server_client_id, parent_window_id, picker->window_id()); + + if (picker->exec() == ExecResult::OK) { + auto file_path = TRY(String::from_deprecated_string(picker->selected_file())); + if (file_path.is_empty()) + return Optional {}; + return file_path; + } + return Optional {}; +} + Optional FilePicker::get_open_filepath(Window* parent_window, DeprecatedString const& window_title, StringView path, bool folder, ScreenPosition screen_position, Optional> allowed_file_types) { auto picker = FilePicker::construct(parent_window, folder ? Mode::OpenFolder : Mode::Open, ""sv, path, screen_position, move(allowed_file_types)); diff --git a/Userland/Libraries/LibGUI/FilePicker.h b/Userland/Libraries/LibGUI/FilePicker.h index 0096877844..7a958a4e7f 100644 --- a/Userland/Libraries/LibGUI/FilePicker.h +++ b/Userland/Libraries/LibGUI/FilePicker.h @@ -16,6 +16,12 @@ #include #include +namespace FileSystemAccessServer { + +class ConnectionFromClient; + +} + namespace GUI { class FilePicker final @@ -34,6 +40,8 @@ public: static Optional get_open_filepath(Window* parent_window, DeprecatedString const& window_title = {}, StringView path = Core::StandardPaths::home_directory(), bool folder = false, ScreenPosition screen_position = Dialog::ScreenPosition::CenterWithinParent, Optional> allowed_file_types = {}); static Optional get_save_filepath(Window* parent_window, DeprecatedString const& title, DeprecatedString const& extension, StringView path = Core::StandardPaths::home_directory(), ScreenPosition screen_position = Dialog::ScreenPosition::CenterWithinParent); + static ErrorOr> get_filepath(Badge, i32 window_server_client_id, i32 parent_window_id, Mode, StringView window_title, StringView file_basename = {}, StringView path = Core::StandardPaths::home_directory(), Optional> = {}); + virtual ~FilePicker() override; DeprecatedString const& selected_file() const { return m_selected_file; } diff --git a/Userland/Libraries/LibGUI/MessageBox.cpp b/Userland/Libraries/LibGUI/MessageBox.cpp index a19f2753b2..6b40dc14c1 100644 --- a/Userland/Libraries/LibGUI/MessageBox.cpp +++ b/Userland/Libraries/LibGUI/MessageBox.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,18 @@ ErrorOr MessageBox::try_show(Window* parent_window, StringVi return box->exec(); } +ErrorOr MessageBox::try_show(Badge, i32 window_server_client_id, i32 parent_window_id, StringView text, StringView title) +{ + auto box = TRY(MessageBox::create(nullptr, text, title, MessageBox::Type::Warning, MessageBox::InputType::YesNo)); + auto parent_rect = ConnectionToWindowServer::the().get_window_rect_from_client(window_server_client_id, parent_window_id); + box->center_within(parent_rect); + box->constrain_to_desktop(); + box->set_screen_position(ScreenPosition::DoNotPosition); + box->Dialog::show(); + ConnectionToWindowServer::the().set_window_parent_from_client(window_server_client_id, parent_window_id, box->window_id()); + return box->exec(); +} + Dialog::ExecResult MessageBox::show_error(Window* parent_window, StringView text) { return MUST(try_show_error(parent_window, text)); diff --git a/Userland/Libraries/LibGUI/MessageBox.h b/Userland/Libraries/LibGUI/MessageBox.h index 03a89eec9b..04e2f429db 100644 --- a/Userland/Libraries/LibGUI/MessageBox.h +++ b/Userland/Libraries/LibGUI/MessageBox.h @@ -10,6 +10,12 @@ #include #include +namespace FileSystemAccessServer { + +class ConnectionFromClient; + +} + namespace GUI { class MessageBox : public Dialog { @@ -36,6 +42,7 @@ public: static ExecResult show_error(Window* parent_window, StringView text); static ExecResult ask_about_unsaved_changes(Window* parent_window, StringView path, Optional