main.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
  4. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2021, Antonio Di Stefano <tonio9681@gmail.com>
  6. * Copyright (c) 2022, Filiph Sandström <filiph.sandstrom@filfatstudios.com>
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include "PreviewWidget.h"
  11. #include <Applications/ThemeEditor/ThemeEditorGML.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/ConfigFile.h>
  14. #include <LibCore/System.h>
  15. #include <LibFileSystemAccessClient/Client.h>
  16. #include <LibGUI/ActionGroup.h>
  17. #include <LibGUI/Application.h>
  18. #include <LibGUI/BoxLayout.h>
  19. #include <LibGUI/Button.h>
  20. #include <LibGUI/CheckBox.h>
  21. #include <LibGUI/ColorInput.h>
  22. #include <LibGUI/ComboBox.h>
  23. #include <LibGUI/FilePicker.h>
  24. #include <LibGUI/Icon.h>
  25. #include <LibGUI/ItemListModel.h>
  26. #include <LibGUI/Menu.h>
  27. #include <LibGUI/Menubar.h>
  28. #include <LibGUI/MessageBox.h>
  29. #include <LibGUI/SpinBox.h>
  30. #include <LibGUI/TextBox.h>
  31. #include <LibGUI/Window.h>
  32. #include <LibGfx/Filters/ColorBlindnessFilter.h>
  33. #include <LibMain/Main.h>
  34. #include <unistd.h>
  35. template<typename T>
  36. class RoleModel final : public GUI::ItemListModel<T> {
  37. public:
  38. static ErrorOr<NonnullRefPtr<RoleModel>> try_create(Vector<T> const& data)
  39. {
  40. return adopt_nonnull_ref_or_enomem(new (nothrow) RoleModel<T>(data));
  41. }
  42. virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
  43. {
  44. if (role == GUI::ModelRole::Display)
  45. return Gfx::to_string(this->m_data[index.row()]);
  46. if (role == GUI::ModelRole::Custom)
  47. return this->m_data[index.row()];
  48. return GUI::ItemListModel<T>::data(index, role);
  49. }
  50. private:
  51. explicit RoleModel(Vector<T> const& data)
  52. : GUI::ItemListModel<T>(data)
  53. {
  54. }
  55. };
  56. class AlignmentModel final : public GUI::Model {
  57. public:
  58. static ErrorOr<NonnullRefPtr<AlignmentModel>> try_create()
  59. {
  60. return adopt_nonnull_ref_or_enomem(new (nothrow) AlignmentModel());
  61. }
  62. virtual ~AlignmentModel() = default;
  63. virtual int row_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return 3; }
  64. virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return 2; }
  65. virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
  66. {
  67. if (role == GUI::ModelRole::Display)
  68. return m_alignments[index.row()].title;
  69. if (role == GUI::ModelRole::Custom)
  70. return m_alignments[index.row()].setting_value;
  71. return {};
  72. }
  73. private:
  74. AlignmentModel() = default;
  75. struct AlignmentValue {
  76. String title;
  77. Gfx::TextAlignment setting_value;
  78. };
  79. Vector<AlignmentValue> m_alignments {
  80. { "Center", Gfx::TextAlignment::Center },
  81. { "Left", Gfx::TextAlignment::CenterLeft },
  82. { "Right", Gfx::TextAlignment::CenterRight },
  83. };
  84. };
  85. ErrorOr<int> serenity_main(Main::Arguments arguments)
  86. {
  87. TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath unix"));
  88. auto app = TRY(GUI::Application::try_create(arguments));
  89. char const* file_to_edit = nullptr;
  90. Core::ArgsParser parser;
  91. parser.add_positional_argument(file_to_edit, "Theme file to edit", "file", Core::ArgsParser::Required::No);
  92. parser.parse(arguments);
  93. Optional<String> path = {};
  94. if (file_to_edit) {
  95. path = Core::File::absolute_path(file_to_edit);
  96. if (Core::File::exists(*path)) {
  97. dbgln("unveil for: {}", *path);
  98. if (unveil(path->characters(), "r") < 0) {
  99. perror("unveil");
  100. return 1;
  101. }
  102. }
  103. }
  104. TRY(Core::System::pledge("stdio recvfd sendfd thread rpath unix"));
  105. TRY(Core::System::unveil("/tmp/portal/filesystemaccess", "rw"));
  106. TRY(Core::System::unveil("/res", "r"));
  107. TRY(Core::System::unveil(nullptr, nullptr));
  108. auto app_icon = GUI::Icon::default_icon("app-theme-editor");
  109. Gfx::Palette startup_preview_palette = file_to_edit ? Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(Gfx::load_system_theme(*path))) : app->palette();
  110. auto window = GUI::Window::construct();
  111. Vector<Gfx::ColorRole> color_roles;
  112. #define __ENUMERATE_COLOR_ROLE(role) color_roles.append(Gfx::ColorRole::role);
  113. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  114. #undef __ENUMERATE_COLOR_ROLE
  115. Vector<Gfx::AlignmentRole> alignment_roles;
  116. #define __ENUMERATE_ALIGNMENT_ROLE(role) alignment_roles.append(Gfx::AlignmentRole::role);
  117. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  118. #undef __ENUMERATE_ALIGNMENT_ROLE
  119. Vector<Gfx::FlagRole> flag_roles;
  120. #define __ENUMERATE_FLAG_ROLE(role) flag_roles.append(Gfx::FlagRole::role);
  121. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  122. #undef __ENUMERATE_FLAG_ROLE
  123. Vector<Gfx::MetricRole> metric_roles;
  124. #define __ENUMERATE_METRIC_ROLE(role) metric_roles.append(Gfx::MetricRole::role);
  125. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  126. #undef __ENUMERATE_METRIC_ROLE
  127. Vector<Gfx::PathRole> path_roles;
  128. #define __ENUMERATE_PATH_ROLE(role) path_roles.append(Gfx::PathRole::role);
  129. ENUMERATE_PATH_ROLES(__ENUMERATE_PATH_ROLE)
  130. #undef __ENUMERATE_PATH_ROLE
  131. auto main_widget = TRY(window->try_set_main_widget<GUI::Widget>());
  132. main_widget->load_from_gml(theme_editor_gml);
  133. auto& preview_widget = main_widget->find_descendant_of_type_named<GUI::Frame>("preview_frame")
  134. ->add<ThemeEditor::PreviewWidget>(startup_preview_palette);
  135. auto& color_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("color_combo_box");
  136. auto& color_input = *main_widget->find_descendant_of_type_named<GUI::ColorInput>("color_input");
  137. auto& alignment_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("alignment_combo_box");
  138. auto& alignment_input = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("alignment_input");
  139. auto& flag_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("flag_combo_box");
  140. auto& flag_input = *main_widget->find_descendant_of_type_named<GUI::CheckBox>("flag_input");
  141. auto& metric_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("metric_combo_box");
  142. auto& metric_input = *main_widget->find_descendant_of_type_named<GUI::SpinBox>("metric_input");
  143. auto& path_combo_box = *main_widget->find_descendant_of_type_named<GUI::ComboBox>("path_combo_box");
  144. auto& path_input = *main_widget->find_descendant_of_type_named<GUI::TextBox>("path_input");
  145. auto& path_picker_button = *main_widget->find_descendant_of_type_named<GUI::Button>("path_picker_button");
  146. color_combo_box.set_model(TRY(RoleModel<Gfx::ColorRole>::try_create(color_roles)));
  147. color_combo_box.on_change = [&](auto&, auto& index) {
  148. auto role = index.model()->data(index, GUI::ModelRole::Custom).to_color_role();
  149. color_input.set_color(preview_widget.preview_palette().color(role));
  150. };
  151. color_combo_box.set_selected_index((size_t)Gfx::ColorRole::Window - 1);
  152. color_input.on_change = [&] {
  153. auto role = color_combo_box.model()->index(color_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_color_role();
  154. auto preview_palette = preview_widget.preview_palette();
  155. preview_palette.set_color(role, color_input.color());
  156. preview_widget.set_preview_palette(preview_palette);
  157. };
  158. color_input.set_color(startup_preview_palette.color(Gfx::ColorRole::Window));
  159. alignment_combo_box.set_model(TRY(RoleModel<Gfx::AlignmentRole>::try_create(alignment_roles)));
  160. alignment_combo_box.on_change = [&](auto&, auto& index) {
  161. auto role = index.model()->data(index, GUI::ModelRole::Custom).to_alignment_role();
  162. alignment_input.set_selected_index((size_t)preview_widget.preview_palette().alignment(role), GUI::AllowCallback::No);
  163. };
  164. alignment_combo_box.set_selected_index((size_t)Gfx::AlignmentRole::TitleAlignment - 1);
  165. alignment_input.set_only_allow_values_from_model(true);
  166. alignment_input.set_model(TRY(AlignmentModel::try_create()));
  167. alignment_input.set_selected_index((size_t)startup_preview_palette.alignment(Gfx::AlignmentRole::TitleAlignment));
  168. alignment_input.on_change = [&](auto&, auto& index) {
  169. auto role = alignment_combo_box.model()->index(alignment_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_alignment_role();
  170. auto preview_palette = preview_widget.preview_palette();
  171. preview_palette.set_alignment(role, index.data(GUI::ModelRole::Custom).to_text_alignment(Gfx::TextAlignment::CenterLeft));
  172. preview_widget.set_preview_palette(preview_palette);
  173. };
  174. flag_combo_box.set_model(TRY(RoleModel<Gfx::FlagRole>::try_create(flag_roles)));
  175. flag_combo_box.on_change = [&](auto&, auto& index) {
  176. auto role = index.model()->data(index, GUI::ModelRole::Custom).to_flag_role();
  177. flag_input.set_checked(preview_widget.preview_palette().flag(role), GUI::AllowCallback::No);
  178. };
  179. flag_combo_box.set_selected_index((size_t)Gfx::FlagRole::IsDark - 1);
  180. flag_input.on_checked = [&](bool checked) {
  181. auto role = flag_combo_box.model()->index(flag_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_flag_role();
  182. auto preview_palette = preview_widget.preview_palette();
  183. preview_palette.set_flag(role, checked);
  184. preview_widget.set_preview_palette(preview_palette);
  185. };
  186. flag_input.set_checked(startup_preview_palette.flag(Gfx::FlagRole::IsDark), GUI::AllowCallback::No);
  187. metric_combo_box.set_model(TRY(RoleModel<Gfx::MetricRole>::try_create(metric_roles)));
  188. metric_combo_box.on_change = [&](auto&, auto& index) {
  189. auto role = index.model()->data(index, GUI::ModelRole::Custom).to_metric_role();
  190. metric_input.set_value(preview_widget.preview_palette().metric(role), GUI::AllowCallback::No);
  191. };
  192. metric_combo_box.set_selected_index((size_t)Gfx::MetricRole::TitleButtonHeight - 1);
  193. metric_input.on_change = [&](int value) {
  194. auto role = metric_combo_box.model()->index(metric_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_metric_role();
  195. auto preview_palette = preview_widget.preview_palette();
  196. preview_palette.set_metric(role, value);
  197. preview_widget.set_preview_palette(preview_palette);
  198. };
  199. metric_input.set_value(startup_preview_palette.metric(Gfx::MetricRole::TitleButtonHeight), GUI::AllowCallback::No);
  200. path_combo_box.set_model(TRY(RoleModel<Gfx::PathRole>::try_create(path_roles)));
  201. path_combo_box.on_change = [&](auto&, auto& index) {
  202. auto role = index.model()->data(index, GUI::ModelRole::Custom).to_path_role();
  203. path_input.set_text(preview_widget.preview_palette().path(role));
  204. };
  205. path_combo_box.set_selected_index((size_t)Gfx::PathRole::TitleButtonIcons - 1);
  206. path_input.on_change = [&] {
  207. auto role = path_combo_box.model()->index(path_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_path_role();
  208. auto preview_palette = preview_widget.preview_palette();
  209. preview_palette.set_path(role, path_input.text());
  210. preview_widget.set_preview_palette(preview_palette);
  211. };
  212. path_input.set_text(startup_preview_palette.path(Gfx::PathRole::TitleButtonIcons));
  213. path_picker_button.on_click = [&](auto) {
  214. auto role = path_combo_box.model()->index(path_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_path_role();
  215. bool open_folder = (role == Gfx::PathRole::TitleButtonIcons);
  216. auto window_title = String::formatted(open_folder ? "Select {} folder" : "Select {} file", path_combo_box.text());
  217. auto target_path = path_input.text();
  218. if (Core::File::exists(target_path)) {
  219. if (!Core::File::is_directory(target_path))
  220. target_path = LexicalPath::dirname(target_path);
  221. } else {
  222. target_path = "/res/icons";
  223. }
  224. auto result = GUI::FilePicker::get_open_filepath(window, window_title, target_path, open_folder);
  225. if (!result.has_value())
  226. return;
  227. path_input.set_text(*result);
  228. };
  229. auto file_menu = TRY(window->try_add_menu("&File"));
  230. auto update_window_title = [&] {
  231. window->set_title(String::formatted("{} - Theme Editor", path.value_or("Untitled")));
  232. };
  233. preview_widget.on_theme_load_from_file = [&](String const& new_path) {
  234. path = new_path;
  235. update_window_title();
  236. auto selected_color_role = color_combo_box.model()->index(color_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_color_role();
  237. color_input.set_color(preview_widget.preview_palette().color(selected_color_role));
  238. auto selected_flag_role = flag_combo_box.model()->index(flag_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_flag_role();
  239. flag_input.set_checked(preview_widget.preview_palette().flag(selected_flag_role), GUI::AllowCallback::No);
  240. auto selected_metric_role = metric_combo_box.model()->index(metric_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_metric_role();
  241. metric_input.set_value(preview_widget.preview_palette().metric(selected_metric_role), GUI::AllowCallback::No);
  242. auto selected_path_role = path_combo_box.model()->index(path_combo_box.selected_index()).data(GUI::ModelRole::Custom).to_path_role();
  243. path_input.set_text(preview_widget.preview_palette().path(selected_path_role));
  244. };
  245. auto save_to_result = [&](auto const& response) {
  246. if (response.is_error())
  247. return;
  248. update_window_title();
  249. auto file = response.value();
  250. auto theme = Core::ConfigFile::open(file->filename(), file->leak_fd()).release_value_but_fixme_should_propagate_errors();
  251. for (auto role : color_roles) {
  252. theme->write_entry("Colors", to_string(role), preview_widget.preview_palette().color(role).to_string());
  253. }
  254. for (auto role : flag_roles) {
  255. theme->write_bool_entry("Flags", to_string(role), preview_widget.preview_palette().flag(role));
  256. }
  257. for (auto role : metric_roles) {
  258. theme->write_num_entry("Metrics", to_string(role), preview_widget.preview_palette().metric(role));
  259. }
  260. for (auto role : path_roles) {
  261. theme->write_entry("Paths", to_string(role), preview_widget.preview_palette().path(role));
  262. }
  263. if (auto sync_result = theme->sync(); sync_result.is_error()) {
  264. GUI::MessageBox::show_error(window, String::formatted("Failed to save theme file: {}", sync_result.error()));
  265. }
  266. };
  267. TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) {
  268. auto response = FileSystemAccessClient::Client::the().try_open_file(window, "Select theme file", "/res/themes");
  269. if (response.is_error())
  270. return;
  271. preview_widget.set_theme_from_file(*response.value());
  272. })));
  273. TRY(file_menu->try_add_action(GUI::CommonActions::make_save_action([&](auto&) {
  274. if (path.has_value()) {
  275. save_to_result(FileSystemAccessClient::Client::the().try_request_file(window, *path, Core::OpenMode::ReadWrite | Core::OpenMode::Truncate));
  276. } else {
  277. save_to_result(FileSystemAccessClient::Client::the().try_save_file(window, "Theme", "ini", Core::OpenMode::ReadWrite | Core::OpenMode::Truncate));
  278. }
  279. })));
  280. TRY(file_menu->try_add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
  281. save_to_result(FileSystemAccessClient::Client::the().try_save_file(window, "Theme", "ini", Core::OpenMode::ReadWrite | Core::OpenMode::Truncate));
  282. })));
  283. TRY(file_menu->try_add_separator());
  284. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); })));
  285. auto accessibility_menu = TRY(window->try_add_menu("&Accessibility"));
  286. auto default_accessibility_action = GUI::Action::create_checkable("Default - non-impaired", { Mod_AltGr, Key_1 }, [&](auto&) {
  287. preview_widget.set_color_filter(nullptr);
  288. });
  289. default_accessibility_action->set_checked(true);
  290. auto pratanopia_accessibility_action = GUI::Action::create_checkable("Protanopia", { Mod_AltGr, Key_2 }, [&](auto&) {
  291. preview_widget.set_color_filter(Gfx::ColorBlindnessFilter::create_protanopia());
  292. });
  293. auto pratanomaly_accessibility_action = GUI::Action::create_checkable("Protanomaly", { Mod_AltGr, Key_3 }, [&](auto&) {
  294. preview_widget.set_color_filter(Gfx::ColorBlindnessFilter::create_protanomaly());
  295. });
  296. auto tritanopia_accessibility_action = GUI::Action::create_checkable("Tritanopia", { Mod_AltGr, Key_4 }, [&](auto&) {
  297. preview_widget.set_color_filter(Gfx::ColorBlindnessFilter::create_tritanopia());
  298. });
  299. auto tritanomaly_accessibility_action = GUI::Action::create_checkable("Tritanomaly", { Mod_AltGr, Key_5 }, [&](auto&) {
  300. preview_widget.set_color_filter(Gfx::ColorBlindnessFilter::create_tritanomaly());
  301. });
  302. auto deuteranopia_accessibility_action = GUI::Action::create_checkable("Deuteranopia", { Mod_AltGr, Key_6 }, [&](auto&) {
  303. preview_widget.set_color_filter(Gfx::ColorBlindnessFilter::create_deuteranopia());
  304. });
  305. auto deuteranomaly_accessibility_action = GUI::Action::create_checkable("Deuteranomaly", { Mod_AltGr, Key_7 }, [&](auto&) {
  306. preview_widget.set_color_filter(Gfx::ColorBlindnessFilter::create_deuteranomaly());
  307. });
  308. auto achromatopsia_accessibility_action = GUI::Action::create_checkable("Achromatopsia", { Mod_AltGr, Key_8 }, [&](auto&) {
  309. preview_widget.set_color_filter(Gfx::ColorBlindnessFilter::create_achromatopsia());
  310. });
  311. auto achromatomaly_accessibility_action = GUI::Action::create_checkable("Achromatomaly", { Mod_AltGr, Key_9 }, [&](auto&) {
  312. preview_widget.set_color_filter(Gfx::ColorBlindnessFilter::create_achromatomaly());
  313. });
  314. auto preview_type_action_group = make<GUI::ActionGroup>();
  315. preview_type_action_group->set_exclusive(true);
  316. preview_type_action_group->add_action(*default_accessibility_action);
  317. preview_type_action_group->add_action(*pratanopia_accessibility_action);
  318. preview_type_action_group->add_action(*pratanomaly_accessibility_action);
  319. preview_type_action_group->add_action(*tritanopia_accessibility_action);
  320. preview_type_action_group->add_action(*tritanomaly_accessibility_action);
  321. preview_type_action_group->add_action(*deuteranopia_accessibility_action);
  322. preview_type_action_group->add_action(*deuteranomaly_accessibility_action);
  323. preview_type_action_group->add_action(*achromatopsia_accessibility_action);
  324. preview_type_action_group->add_action(*achromatomaly_accessibility_action);
  325. TRY(accessibility_menu->try_add_action(default_accessibility_action));
  326. TRY(accessibility_menu->try_add_action(pratanopia_accessibility_action));
  327. TRY(accessibility_menu->try_add_action(pratanomaly_accessibility_action));
  328. TRY(accessibility_menu->try_add_action(tritanopia_accessibility_action));
  329. TRY(accessibility_menu->try_add_action(tritanomaly_accessibility_action));
  330. TRY(accessibility_menu->try_add_action(deuteranopia_accessibility_action));
  331. TRY(accessibility_menu->try_add_action(deuteranomaly_accessibility_action));
  332. TRY(accessibility_menu->try_add_action(achromatopsia_accessibility_action));
  333. TRY(accessibility_menu->try_add_action(achromatomaly_accessibility_action));
  334. auto help_menu = TRY(window->try_add_menu("&Help"));
  335. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Theme Editor", app_icon, window)));
  336. update_window_title();
  337. window->resize(480, 520);
  338. window->set_resizable(false);
  339. window->show();
  340. window->set_icon(app_icon.bitmap_for_size(16));
  341. return app->exec();
  342. }