FontEditor: Use LibFileSystemAccessClient

This commit is contained in:
Caoimhe 2023-03-24 19:49:14 +00:00 committed by Sam Atkins
parent f663e2dbd1
commit 5e4d835caf
Notes: sideshowbarker 2024-07-16 23:38:54 +09:00
4 changed files with 52 additions and 26 deletions

View file

@ -25,4 +25,4 @@ set(GENERATED_SOURCES
)
serenity_app(FontEditor ICON app-font-editor)
target_link_libraries(FontEditor PRIVATE LibConfig LibCore LibGUI LibDesktop LibGfx LibMain LibUnicode)
target_link_libraries(FontEditor PRIVATE LibConfig LibCore LibFileSystemAccessClient LibGUI LibDesktop LibGfx LibMain LibUnicode)

View file

@ -15,6 +15,7 @@
#include <Applications/FontEditor/FontPreviewWindowGML.h>
#include <LibConfig/Client.h>
#include <LibDesktop/Launcher.h>
#include <LibFileSystemAccessClient/Client.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/Button.h>
@ -110,26 +111,36 @@ ErrorOr<void> MainWidget::create_actions()
m_open_action = GUI::CommonActions::make_open_action([&](auto&) {
if (!request_close())
return;
Optional<DeprecatedString> open_path = GUI::FilePicker::get_open_filepath(window(), {}, "/res/fonts/"sv);
if (!open_path.has_value())
auto result = FileSystemAccessClient::Client::the().open_file(window(), "Open font file", "/res/fonts"sv);
if (result.is_error())
return;
if (auto result = open_file(open_path.value()); result.is_error())
show_error(result.release_error(), "Opening"sv, LexicalPath { open_path.value() }.basename());
auto file = result.release_value();
if (auto result = open_file(file.filename(), file.release_stream()); result.is_error())
show_error(result.release_error(), "Opening"sv, LexicalPath { file.filename().to_deprecated_string() }.basename());
});
m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
if (m_path.is_empty())
return m_save_as_action->activate();
if (auto result = save_file(m_path); result.is_error())
auto response = FileSystemAccessClient::Client::the().request_file(window(), m_path, Core::File::OpenMode::Truncate | Core::File::OpenMode::Write);
if (response.is_error())
return;
if (auto result = save_file(m_path, response.value().release_stream()); result.is_error())
show_error(result.release_error(), "Saving"sv, LexicalPath { m_path }.basename());
});
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
LexicalPath lexical_path(m_path.is_empty() ? "Untitled.font" : m_path);
Optional<DeprecatedString> save_path = GUI::FilePicker::get_save_filepath(window(), lexical_path.title(), lexical_path.extension());
if (!save_path.has_value())
auto response = FileSystemAccessClient::Client::the().save_file(window(), lexical_path.title(), lexical_path.extension());
if (response.is_error())
return;
if (auto result = save_file(save_path.value()); result.is_error())
if (auto result = save_file(lexical_path.string(), response.value().release_stream()); result.is_error())
show_error(result.release_error(), "Saving"sv, lexical_path.basename());
});
@ -724,10 +735,11 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
return {};
}
ErrorOr<void> MainWidget::save_file(DeprecatedString const& path)
ErrorOr<void> MainWidget::save_file(DeprecatedString const& path, NonnullOwnPtr<Core::File> file)
{
auto masked_font = TRY(m_edited_font->masked_character_set());
TRY(masked_font->write_to_file(path));
TRY(masked_font->write_to_file(move(file)));
m_path = path;
m_undo_stack->set_current_unmodified();
window()->set_modified(false);
@ -781,10 +793,11 @@ void MainWidget::set_show_system_emoji(bool show)
m_glyph_map_widget->set_show_system_emoji(show);
}
ErrorOr<void> MainWidget::open_file(DeprecatedString const& path)
ErrorOr<void> MainWidget::open_file(StringView path, NonnullOwnPtr<Core::File> file)
{
auto unmasked_font = TRY(TRY(Gfx::BitmapFont::try_load_from_file(path))->unmasked_character_set());
TRY(initialize(path, move(unmasked_font)));
auto mapped_file = TRY(Core::MappedFile::map_from_file(move(file), path));
auto unmasked_font = TRY(TRY(Gfx::BitmapFont::try_load_from_mapped_file(mapped_file))->unmasked_character_set());
TRY(initialize(path.to_deprecated_string(), move(unmasked_font)));
return {};
}
@ -976,8 +989,13 @@ void MainWidget::drop_event(GUI::DropEvent& event)
return;
auto file_path = urls.first().serialize_path();
if (auto result = open_file(file_path); result.is_error())
show_error(result.release_error(), "Opening"sv, LexicalPath { file_path }.basename());
auto result = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), file_path);
if (result.is_error())
return;
auto file = result.release_value();
if (auto result = open_file(file.filename(), file.release_stream()); result.is_error())
show_error(result.release_error(), "Opening"sv, LexicalPath { file.filename().to_deprecated_string() }.basename());
}
}

View file

@ -37,8 +37,8 @@ public:
ErrorOr<void> initialize(DeprecatedString const& path, RefPtr<Gfx::BitmapFont>&&);
ErrorOr<void> initialize_menubar(GUI::Window&);
ErrorOr<void> open_file(DeprecatedString const&);
ErrorOr<void> save_file(DeprecatedString const&);
ErrorOr<void> open_file(StringView, NonnullOwnPtr<Core::File>);
ErrorOr<void> save_file(DeprecatedString const&, NonnullOwnPtr<Core::File>);
bool request_close();
void update_title();

View file

@ -10,6 +10,7 @@
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
#include <LibDesktop/Launcher.h>
#include <LibFileSystemAccessClient/Client.h>
#include <LibGUI/Application.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Window.h>
@ -26,7 +27,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Desktop::Launcher::seal_allowlist());
Config::pledge_domain("FontEditor");
TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath"));
TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
TRY(Core::System::unveil("/res", "r"));
TRY(Core::System::unveil(nullptr, nullptr));
StringView path;
Core::ArgsParser args_parser;
@ -42,13 +46,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto font_editor = TRY(window->set_main_widget<FontEditor::MainWidget>());
TRY(font_editor->initialize_menubar(*window));
if (!path.is_empty()) {
TRY(font_editor->open_file(path));
} else {
auto mutable_font = TRY(TRY(Gfx::BitmapFont::try_load_from_file("/res/fonts/KaticaRegular10.font"))->unmasked_character_set());
TRY(font_editor->initialize({}, move(mutable_font)));
}
window->on_close_request = [&]() -> GUI::Window::CloseRequestDecision {
if (font_editor->request_close())
return GUI::Window::CloseRequestDecision::Close;
@ -57,5 +54,16 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->show();
auto path_to_load = path.is_empty() ? "/res/fonts/KaticaRegular10.font"sv : path;
auto file = TRY(FileSystemAccessClient::Client::the().request_file_read_only_approved(window, path_to_load));
if (!path.is_empty()) {
TRY(font_editor->open_file(file.filename(), file.release_stream()));
} else {
auto mapped_file = TRY(Core::MappedFile::map_from_file(file.release_stream(), path_to_load));
auto mutable_font = TRY(TRY(Gfx::BitmapFont::try_load_from_mapped_file(mapped_file))->unmasked_character_set());
TRY(font_editor->initialize({}, move(mutable_font)));
}
return app->exec();
}