From d51072629d89a5c49b05533021969a0a1b96c0e5 Mon Sep 17 00:00:00 2001 From: networkException Date: Thu, 19 Aug 2021 00:07:39 +0200 Subject: [PATCH] ThemeEditor: Add Actions to save preview_palette to theme file This patch adds a save and save as Action to the file menu allowing to export all colors into an ini file. --- .../Applications/ThemeEditor/CMakeLists.txt | 2 +- Userland/Applications/ThemeEditor/main.cpp | 48 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Userland/Applications/ThemeEditor/CMakeLists.txt b/Userland/Applications/ThemeEditor/CMakeLists.txt index bb5a32087f3..64a7f1614b4 100644 --- a/Userland/Applications/ThemeEditor/CMakeLists.txt +++ b/Userland/Applications/ThemeEditor/CMakeLists.txt @@ -9,4 +9,4 @@ set(SOURCES ) serenity_app(ThemeEditor ICON app-theme-editor) -target_link_libraries(ThemeEditor LibGUI) +target_link_libraries(ThemeEditor LibGUI LibFileSystemAccessClient) diff --git a/Userland/Applications/ThemeEditor/main.cpp b/Userland/Applications/ThemeEditor/main.cpp index accce0e93a2..8e1dfd99af2 100644 --- a/Userland/Applications/ThemeEditor/main.cpp +++ b/Userland/Applications/ThemeEditor/main.cpp @@ -1,10 +1,13 @@ /* * Copyright (c) 2020, Andreas Kling + * Copyright (c) 2021, Jakob-Niklas See * * SPDX-License-Identifier: BSD-2-Clause */ #include "PreviewWidget.h" +#include +#include #include #include #include @@ -55,11 +58,16 @@ int main(int argc, char** argv) auto app = GUI::Application::construct(argc, argv); - if (pledge("stdio recvfd sendfd thread rpath", nullptr) < 0) { + if (pledge("stdio recvfd sendfd thread rpath unix", nullptr) < 0) { perror("pledge"); return 1; } + if (unveil("/tmp/portal/filesystemaccess", "rw") < 0) { + perror("unveil"); + return 1; + } + if (unveil("/res", "r") < 0) { perror("unveil"); return 1; @@ -76,9 +84,42 @@ int main(int argc, char** argv) auto window = GUI::Window::construct(); + Vector color_roles; +#define __ENUMERATE_COLOR_ROLE(role) color_roles.append(Gfx::ColorRole::role); + ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE) +#undef __ENUMERATE_COLOR_ROLE + auto& file_menu = window->add_menu("&File"); file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); })); + Optional path = {}; + + auto save_to_result = [&](FileSystemAccessClient::Result result) { + if (result.error != 0) + return; + + path = result.chosen_file; + + auto theme = Core::ConfigFile::open(*result.chosen_file, *result.fd); + for (auto role : color_roles) { + theme->write_entry("Colors", to_string(role), preview_palette.color(role).to_string()); + } + + theme->sync(); + }; + + file_menu.add_action(GUI::CommonActions::make_save_action([&](auto&) { + if (path.has_value()) { + save_to_result(FileSystemAccessClient::Client::the().request_file(window->window_id(), *path, Core::OpenMode::WriteOnly | Core::OpenMode::Truncate)); + } else { + save_to_result(FileSystemAccessClient::Client::the().save_file(window->window_id(), "Theme", "ini")); + } + })); + + file_menu.add_action(GUI::CommonActions::make_save_as_action([&](auto&) { + save_to_result(FileSystemAccessClient::Client::the().save_file(window->window_id(), "Theme", "ini")); + })); + auto& help_menu = window->add_menu("&Help"); help_menu.add_action(GUI::CommonActions::make_about_action("Theme Editor", app_icon, window)); @@ -96,11 +137,6 @@ int main(int argc, char** argv) auto& combo_box = horizontal_container.add(); auto& color_input = horizontal_container.add(); - Vector color_roles; -#define __ENUMERATE_COLOR_ROLE(role) color_roles.append(Gfx::ColorRole::role); - ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE) -#undef __ENUMERATE_COLOR_ROLE - combo_box.set_only_allow_values_from_model(true); combo_box.set_model(adopt_ref(*new ColorRoleModel(color_roles))); combo_box.on_change = [&](auto&, auto& index) {