1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:17:35 +00:00

ThemeEditor: Port to Core::Stream

This commit is contained in:
Lucas CHOLLET 2023-01-07 12:18:16 -05:00 committed by Sam Atkins
parent a5d5b970ff
commit 8377adfde0
3 changed files with 19 additions and 19 deletions

View file

@ -238,36 +238,36 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) {
if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
return;
auto response = FileSystemAccessClient::Client::the().try_open_file_deprecated(&window, "Select theme file", "/res/themes"sv);
auto response = FileSystemAccessClient::Client::the().open_file(&window, "Select theme file", "/res/themes"sv);
if (response.is_error())
return;
auto load_from_file_result = load_from_file(*response.value());
auto load_from_file_result = load_from_file(response.value().filename(), response.value().release_stream());
if (load_from_file_result.is_error()) {
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Can't open file named {}: {}", response.value()->filename(), load_from_file_result.error()));
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Can't open file named {}: {}", response.value().filename(), load_from_file_result.error()));
return;
}
})));
m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
if (m_path.has_value()) {
auto result = FileSystemAccessClient::Client::the().try_request_file_deprecated(&window, *m_path, Core::OpenMode::ReadWrite | Core::OpenMode::Truncate);
auto result = FileSystemAccessClient::Client::the().request_file(&window, *m_path, Core::Stream::OpenMode::ReadWrite | Core::Stream::OpenMode::Truncate);
if (result.is_error())
return;
save_to_file(result.value());
save_to_file(result.value().filename(), result.value().release_stream());
} else {
auto result = FileSystemAccessClient::Client::the().try_save_file_deprecated(&window, "Theme", "ini", Core::OpenMode::ReadWrite | Core::OpenMode::Truncate);
auto result = FileSystemAccessClient::Client::the().save_file(&window, "Theme", "ini", Core::Stream::OpenMode::ReadWrite | Core::Stream::OpenMode::Truncate);
if (result.is_error())
return;
save_to_file(result.value());
save_to_file(result.value().filename(), result.value().release_stream());
}
});
TRY(file_menu->try_add_action(*m_save_action));
TRY(file_menu->try_add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
auto result = FileSystemAccessClient::Client::the().try_save_file_deprecated(&window, "Theme", "ini", Core::OpenMode::ReadWrite | Core::OpenMode::Truncate);
auto result = FileSystemAccessClient::Client::the().save_file(&window, "Theme", "ini", Core::Stream::OpenMode::ReadWrite | Core::Stream::OpenMode::Truncate);
if (result.is_error())
return;
save_to_file(result.value());
save_to_file(result.value().filename(), result.value().release_stream());
})));
TRY(file_menu->try_add_separator());
@ -315,9 +315,9 @@ void MainWidget::set_path(DeprecatedString path)
update_title();
}
void MainWidget::save_to_file(Core::File& file)
void MainWidget::save_to_file(String const& filename, NonnullOwnPtr<Core::Stream::File> file)
{
auto theme = Core::ConfigFile::open(file.filename(), file.leak_fd()).release_value_but_fixme_should_propagate_errors();
auto theme = Core::ConfigFile::open(filename.to_deprecated_string(), move(file)).release_value_but_fixme_should_propagate_errors();
#define __ENUMERATE_ALIGNMENT_ROLE(role) theme->write_entry("Alignments", to_string(Gfx::AlignmentRole::role), to_string(m_current_palette.alignment(Gfx::AlignmentRole::role)));
ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
@ -344,7 +344,7 @@ void MainWidget::save_to_file(Core::File& file)
GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Failed to save theme file: {}", sync_result.error()));
} else {
m_last_modified_time = Time::now_monotonic();
set_path(file.filename());
set_path(filename.to_deprecated_string());
window()->set_modified(false);
}
}
@ -601,15 +601,15 @@ void MainWidget::show_path_picker_dialog(StringView property_display_name, GUI::
path_input.set_text(*result);
}
ErrorOr<void> MainWidget::load_from_file(Core::File& file)
ErrorOr<void> MainWidget::load_from_file(String const& filename, NonnullOwnPtr<Core::Stream::File> file)
{
auto config_file = TRY(Core::ConfigFile::open(file.filename(), file.leak_fd()));
auto config_file = TRY(Core::ConfigFile::open(filename.to_deprecated_string(), move(file)));
auto theme = TRY(Gfx::load_system_theme(config_file));
VERIFY(theme.is_valid());
auto new_palette = Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(theme));
set_palette(move(new_palette));
set_path(file.filename());
set_path(filename.to_deprecated_string());
#define __ENUMERATE_ALIGNMENT_ROLE(role) \
if (auto alignment_input = m_alignment_inputs[to_underlying(Gfx::AlignmentRole::role)]) \