MainWidget.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, networkException <networkexception@serenityos.org>
  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 "MainWidget.h"
  11. #include <Applications/ThemeEditor/AlignmentPropertyGML.h>
  12. #include <Applications/ThemeEditor/ColorPropertyGML.h>
  13. #include <Applications/ThemeEditor/FlagPropertyGML.h>
  14. #include <Applications/ThemeEditor/MetricPropertyGML.h>
  15. #include <Applications/ThemeEditor/PathPropertyGML.h>
  16. #include <Applications/ThemeEditor/ThemeEditorGML.h>
  17. #include <LibCore/DeprecatedFile.h>
  18. #include <LibFileSystemAccessClient/Client.h>
  19. #include <LibGUI/ActionGroup.h>
  20. #include <LibGUI/BoxLayout.h>
  21. #include <LibGUI/Button.h>
  22. #include <LibGUI/ConnectionToWindowServer.h>
  23. #include <LibGUI/FilePicker.h>
  24. #include <LibGUI/Frame.h>
  25. #include <LibGUI/GroupBox.h>
  26. #include <LibGUI/Icon.h>
  27. #include <LibGUI/ItemListModel.h>
  28. #include <LibGUI/Label.h>
  29. #include <LibGUI/Menu.h>
  30. #include <LibGUI/Menubar.h>
  31. #include <LibGUI/MessageBox.h>
  32. #include <LibGUI/ScrollableContainerWidget.h>
  33. #include <LibGfx/Filters/ColorBlindnessFilter.h>
  34. namespace ThemeEditor {
  35. static const PropertyTab window_tab {
  36. "Windows",
  37. {
  38. { "General",
  39. { { Gfx::FlagRole::IsDark },
  40. { Gfx::AlignmentRole::TitleAlignment },
  41. { Gfx::MetricRole::TitleHeight },
  42. { Gfx::MetricRole::TitleButtonWidth },
  43. { Gfx::MetricRole::TitleButtonHeight },
  44. { Gfx::PathRole::TitleButtonIcons },
  45. { Gfx::FlagRole::TitleButtonsIconOnly } } },
  46. { "Border",
  47. { { Gfx::MetricRole::BorderThickness },
  48. { Gfx::MetricRole::BorderRadius } } },
  49. { "Active Window",
  50. { { Gfx::ColorRole::ActiveWindowBorder1 },
  51. { Gfx::ColorRole::ActiveWindowBorder2 },
  52. { Gfx::ColorRole::ActiveWindowTitle },
  53. { Gfx::ColorRole::ActiveWindowTitleShadow },
  54. { Gfx::ColorRole::ActiveWindowTitleStripes },
  55. { Gfx::PathRole::ActiveWindowShadow } } },
  56. { "Inactive Window",
  57. { { Gfx::ColorRole::InactiveWindowBorder1 },
  58. { Gfx::ColorRole::InactiveWindowBorder2 },
  59. { Gfx::ColorRole::InactiveWindowTitle },
  60. { Gfx::ColorRole::InactiveWindowTitleShadow },
  61. { Gfx::ColorRole::InactiveWindowTitleStripes },
  62. { Gfx::PathRole::InactiveWindowShadow } } },
  63. { "Highlighted Window",
  64. { { Gfx::ColorRole::HighlightWindowBorder1 },
  65. { Gfx::ColorRole::HighlightWindowBorder2 },
  66. { Gfx::ColorRole::HighlightWindowTitle },
  67. { Gfx::ColorRole::HighlightWindowTitleShadow },
  68. { Gfx::ColorRole::HighlightWindowTitleStripes } } },
  69. { "Moving Window",
  70. { { Gfx::ColorRole::MovingWindowBorder1 },
  71. { Gfx::ColorRole::MovingWindowBorder2 },
  72. { Gfx::ColorRole::MovingWindowTitle },
  73. { Gfx::ColorRole::MovingWindowTitleShadow },
  74. { Gfx::ColorRole::MovingWindowTitleStripes } } },
  75. { "Contents",
  76. { { Gfx::ColorRole::Window },
  77. { Gfx::ColorRole::WindowText } } },
  78. { "Desktop",
  79. { { Gfx::ColorRole::DesktopBackground },
  80. { Gfx::PathRole::TaskbarShadow } } },
  81. }
  82. };
  83. static const PropertyTab widgets_tab {
  84. "Widgets",
  85. {
  86. { "General",
  87. { { Gfx::ColorRole::Accent },
  88. { Gfx::ColorRole::Base },
  89. { Gfx::ColorRole::ThreedHighlight },
  90. { Gfx::ColorRole::ThreedShadow1 },
  91. { Gfx::ColorRole::ThreedShadow2 },
  92. { Gfx::ColorRole::HoverHighlight } } },
  93. { "Text",
  94. { { Gfx::ColorRole::BaseText },
  95. { Gfx::ColorRole::DisabledTextFront },
  96. { Gfx::ColorRole::DisabledTextBack },
  97. { Gfx::ColorRole::PlaceholderText } } },
  98. { "Links",
  99. { { Gfx::ColorRole::Link },
  100. { Gfx::ColorRole::ActiveLink },
  101. { Gfx::ColorRole::VisitedLink } } },
  102. { "Buttons",
  103. { { Gfx::ColorRole::Button },
  104. { Gfx::ColorRole::ButtonText } } },
  105. { "Tooltips",
  106. { { Gfx::ColorRole::Tooltip },
  107. { Gfx::ColorRole::TooltipText },
  108. { Gfx::PathRole::TooltipShadow } } },
  109. { "Trays",
  110. { { Gfx::ColorRole::Tray },
  111. { Gfx::ColorRole::TrayText } } },
  112. { "Ruler",
  113. { { Gfx::ColorRole::Ruler },
  114. { Gfx::ColorRole::RulerBorder },
  115. { Gfx::ColorRole::RulerActiveText },
  116. { Gfx::ColorRole::RulerInactiveText } } },
  117. { "Gutter",
  118. { { Gfx::ColorRole::Gutter },
  119. { Gfx::ColorRole::GutterBorder } } },
  120. { "Rubber Band",
  121. { { Gfx::ColorRole::RubberBandBorder },
  122. { Gfx::ColorRole::RubberBandFill } } },
  123. { "Menus",
  124. { { Gfx::ColorRole::MenuBase },
  125. { Gfx::ColorRole::MenuBaseText },
  126. { Gfx::ColorRole::MenuSelection },
  127. { Gfx::ColorRole::MenuSelectionText },
  128. { Gfx::ColorRole::MenuStripe },
  129. { Gfx::PathRole::MenuShadow } } },
  130. { "Selection",
  131. { { Gfx::ColorRole::FocusOutline },
  132. { Gfx::ColorRole::TextCursor },
  133. { Gfx::ColorRole::Selection },
  134. { Gfx::ColorRole::SelectionText },
  135. { Gfx::ColorRole::InactiveSelection },
  136. { Gfx::ColorRole::InactiveSelectionText },
  137. { Gfx::ColorRole::HighlightSearching },
  138. { Gfx::ColorRole::HighlightSearchingText } } },
  139. }
  140. };
  141. static const PropertyTab syntax_highlighting_tab {
  142. "Syntax Highlighting",
  143. {
  144. { "General",
  145. { { Gfx::ColorRole::SyntaxComment },
  146. { Gfx::ColorRole::SyntaxControlKeyword },
  147. { Gfx::ColorRole::SyntaxIdentifier },
  148. { Gfx::ColorRole::SyntaxKeyword },
  149. { Gfx::ColorRole::SyntaxNumber },
  150. { Gfx::ColorRole::SyntaxOperator },
  151. { Gfx::ColorRole::SyntaxPreprocessorStatement },
  152. { Gfx::ColorRole::SyntaxPreprocessorValue },
  153. { Gfx::ColorRole::SyntaxPunctuation },
  154. { Gfx::ColorRole::SyntaxString },
  155. { Gfx::ColorRole::SyntaxType },
  156. { Gfx::ColorRole::SyntaxFunction },
  157. { Gfx::ColorRole::SyntaxVariable },
  158. { Gfx::ColorRole::SyntaxCustomType },
  159. { Gfx::ColorRole::SyntaxNamespace },
  160. { Gfx::ColorRole::SyntaxMember },
  161. { Gfx::ColorRole::SyntaxParameter } } },
  162. }
  163. };
  164. static const PropertyTab color_scheme_tab {
  165. "Color Scheme",
  166. {
  167. { "General",
  168. { { Gfx::ColorRole::Black },
  169. { Gfx::ColorRole::Red },
  170. { Gfx::ColorRole::Green },
  171. { Gfx::ColorRole::Yellow },
  172. { Gfx::ColorRole::Blue },
  173. { Gfx::ColorRole::Magenta },
  174. { Gfx::ColorRole::Cyan },
  175. { Gfx::ColorRole::White },
  176. { Gfx::ColorRole::BrightBlack },
  177. { Gfx::ColorRole::BrightRed },
  178. { Gfx::ColorRole::BrightGreen },
  179. { Gfx::ColorRole::BrightYellow },
  180. { Gfx::ColorRole::BrightBlue },
  181. { Gfx::ColorRole::BrightMagenta },
  182. { Gfx::ColorRole::BrightCyan },
  183. { Gfx::ColorRole::BrightWhite } } },
  184. }
  185. };
  186. ErrorOr<NonnullRefPtr<MainWidget>> MainWidget::try_create()
  187. {
  188. auto alignment_model = TRY(AlignmentModel::try_create());
  189. auto main_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MainWidget(move(alignment_model))));
  190. TRY(main_widget->load_from_gml(theme_editor_gml));
  191. main_widget->m_preview_widget = main_widget->find_descendant_of_type_named<ThemeEditor::PreviewWidget>("preview_widget");
  192. main_widget->m_property_tabs = main_widget->find_descendant_of_type_named<GUI::TabWidget>("property_tabs");
  193. TRY(main_widget->add_property_tab(window_tab));
  194. TRY(main_widget->add_property_tab(widgets_tab));
  195. TRY(main_widget->add_property_tab(syntax_highlighting_tab));
  196. TRY(main_widget->add_property_tab(color_scheme_tab));
  197. main_widget->build_override_controls();
  198. return main_widget;
  199. }
  200. MainWidget::MainWidget(NonnullRefPtr<AlignmentModel> alignment_model)
  201. : m_current_palette(GUI::Application::the()->palette())
  202. , m_alignment_model(move(alignment_model))
  203. {
  204. }
  205. ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
  206. {
  207. auto file_menu = TRY(window.try_add_menu("&File"));
  208. TRY(file_menu->try_add_action(GUI::CommonActions::make_open_action([&](auto&) {
  209. if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
  210. return;
  211. auto response = FileSystemAccessClient::Client::the().open_file(&window, "Select theme file", "/res/themes"sv);
  212. if (response.is_error())
  213. return;
  214. auto load_from_file_result = load_from_file(response.value().filename(), response.value().release_stream());
  215. if (load_from_file_result.is_error()) {
  216. GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Can't open file named {}: {}", response.value().filename(), load_from_file_result.error()));
  217. return;
  218. }
  219. })));
  220. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  221. if (m_path.has_value()) {
  222. auto result = FileSystemAccessClient::Client::the().request_file(&window, *m_path, Core::File::OpenMode::ReadWrite | Core::File::OpenMode::Truncate);
  223. if (result.is_error())
  224. return;
  225. save_to_file(result.value().filename(), result.value().release_stream());
  226. } else {
  227. auto result = FileSystemAccessClient::Client::the().save_file(&window, "Theme", "ini", Core::File::OpenMode::ReadWrite | Core::File::OpenMode::Truncate);
  228. if (result.is_error())
  229. return;
  230. save_to_file(result.value().filename(), result.value().release_stream());
  231. }
  232. });
  233. TRY(file_menu->try_add_action(*m_save_action));
  234. TRY(file_menu->try_add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
  235. auto result = FileSystemAccessClient::Client::the().save_file(&window, "Theme", "ini", Core::File::OpenMode::ReadWrite | Core::File::OpenMode::Truncate);
  236. if (result.is_error())
  237. return;
  238. save_to_file(result.value().filename(), result.value().release_stream());
  239. })));
  240. TRY(file_menu->try_add_separator());
  241. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  242. if (request_close() == GUI::Window::CloseRequestDecision::Close)
  243. GUI::Application::the()->quit();
  244. })));
  245. TRY(window.try_add_menu(TRY(GUI::CommonMenus::make_accessibility_menu(*m_preview_widget))));
  246. auto help_menu = TRY(window.try_add_menu("&Help"));
  247. TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(&window)));
  248. TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Theme Editor", GUI::Icon::default_icon("app-theme-editor"sv), &window)));
  249. return {};
  250. }
  251. void MainWidget::update_title()
  252. {
  253. window()->set_title(DeprecatedString::formatted("{}[*] - Theme Editor", m_path.value_or("Untitled")));
  254. }
  255. GUI::Window::CloseRequestDecision MainWidget::request_close()
  256. {
  257. if (!window()->is_modified())
  258. return GUI::Window::CloseRequestDecision::Close;
  259. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path.value_or(""), m_last_modified_time);
  260. if (result == GUI::MessageBox::ExecResult::Yes) {
  261. m_save_action->activate();
  262. if (window()->is_modified())
  263. return GUI::Window::CloseRequestDecision::StayOpen;
  264. return GUI::Window::CloseRequestDecision::Close;
  265. }
  266. if (result == GUI::MessageBox::ExecResult::No)
  267. return GUI::Window::CloseRequestDecision::Close;
  268. return GUI::Window::CloseRequestDecision::StayOpen;
  269. }
  270. void MainWidget::set_path(DeprecatedString path)
  271. {
  272. m_path = path;
  273. update_title();
  274. }
  275. void MainWidget::save_to_file(String const& filename, NonnullOwnPtr<Core::File> file)
  276. {
  277. auto theme = Core::ConfigFile::open(filename.to_deprecated_string(), move(file)).release_value_but_fixme_should_propagate_errors();
  278. #define __ENUMERATE_ALIGNMENT_ROLE(role) theme->write_entry("Alignments", to_string(Gfx::AlignmentRole::role), to_string(m_current_palette.alignment(Gfx::AlignmentRole::role)));
  279. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  280. #undef __ENUMERATE_ALIGNMENT_ROLE
  281. #define __ENUMERATE_COLOR_ROLE(role) theme->write_entry("Colors", to_string(Gfx::ColorRole::role), m_current_palette.color(Gfx::ColorRole::role).to_deprecated_string());
  282. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  283. #undef __ENUMERATE_COLOR_ROLE
  284. #define __ENUMERATE_FLAG_ROLE(role) theme->write_bool_entry("Flags", to_string(Gfx::FlagRole::role), m_current_palette.flag(Gfx::FlagRole::role));
  285. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  286. #undef __ENUMERATE_FLAG_ROLE
  287. #define __ENUMERATE_METRIC_ROLE(role) theme->write_num_entry("Metrics", to_string(Gfx::MetricRole::role), m_current_palette.metric(Gfx::MetricRole::role));
  288. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  289. #undef __ENUMERATE_METRIC_ROLE
  290. #define __ENUMERATE_PATH_ROLE(role) theme->write_entry("Paths", to_string(Gfx::PathRole::role), m_current_palette.path(Gfx::PathRole::role));
  291. ENUMERATE_PATH_ROLES(__ENUMERATE_PATH_ROLE)
  292. #undef __ENUMERATE_PATH_ROLE
  293. auto sync_result = theme->sync();
  294. if (sync_result.is_error()) {
  295. GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Failed to save theme file: {}", sync_result.error()));
  296. } else {
  297. m_last_modified_time = Time::now_monotonic();
  298. set_path(filename.to_deprecated_string());
  299. window()->set_modified(false);
  300. }
  301. }
  302. ErrorOr<Core::AnonymousBuffer> MainWidget::encode()
  303. {
  304. auto buffer = TRY(Core::AnonymousBuffer::create_with_size(sizeof(Gfx::SystemTheme)));
  305. auto* data = buffer.data<Gfx::SystemTheme>();
  306. #define __ENUMERATE_ALIGNMENT_ROLE(role) \
  307. data->alignment[(int)Gfx::AlignmentRole::role] = m_current_palette.alignment(Gfx::AlignmentRole::role);
  308. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  309. #undef __ENUMERATE_ALIGNMENT_ROLE
  310. #define __ENUMERATE_COLOR_ROLE(role) \
  311. data->color[(int)Gfx::ColorRole::role] = m_current_palette.color(Gfx::ColorRole::role).value();
  312. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  313. #undef __ENUMERATE_COLOR_ROLE
  314. #define __ENUMERATE_FLAG_ROLE(role) \
  315. data->flag[(int)Gfx::FlagRole::role] = m_current_palette.flag(Gfx::FlagRole::role);
  316. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  317. #undef __ENUMERATE_FLAG_ROLE
  318. #define __ENUMERATE_METRIC_ROLE(role) \
  319. data->metric[(int)Gfx::MetricRole::role] = m_current_palette.metric(Gfx::MetricRole::role);
  320. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  321. #undef __ENUMERATE_METRIC_ROLE
  322. #define ENCODE_PATH(role, allow_empty) \
  323. do { \
  324. auto path = m_current_palette.path(Gfx::PathRole::role); \
  325. char const* characters; \
  326. if (path.is_empty()) { \
  327. switch (Gfx::PathRole::role) { \
  328. case Gfx::PathRole::TitleButtonIcons: \
  329. characters = "/res/icons/16x16/"; \
  330. break; \
  331. default: \
  332. characters = allow_empty ? "" : "/res/"; \
  333. } \
  334. } \
  335. characters = path.characters(); \
  336. memcpy(data->path[(int)Gfx::PathRole::role], characters, min(strlen(characters) + 1, sizeof(data->path[(int)Gfx::PathRole::role]))); \
  337. data->path[(int)Gfx::PathRole::role][sizeof(data->path[(int)Gfx::PathRole::role]) - 1] = '\0'; \
  338. } while (0)
  339. ENCODE_PATH(TitleButtonIcons, false);
  340. ENCODE_PATH(ActiveWindowShadow, true);
  341. ENCODE_PATH(InactiveWindowShadow, true);
  342. ENCODE_PATH(TaskbarShadow, true);
  343. ENCODE_PATH(MenuShadow, true);
  344. ENCODE_PATH(TooltipShadow, true);
  345. return buffer;
  346. }
  347. void MainWidget::build_override_controls()
  348. {
  349. auto* theme_override_controls = find_descendant_of_type_named<GUI::Widget>("theme_override_controls");
  350. m_theme_override_apply = theme_override_controls->find_child_of_type_named<GUI::DialogButton>("apply_button");
  351. m_theme_override_reset = theme_override_controls->find_child_of_type_named<GUI::DialogButton>("reset_button");
  352. m_theme_override_apply->on_click = [&](auto) {
  353. auto encoded = encode();
  354. if (encoded.is_error())
  355. return;
  356. GUI::ConnectionToWindowServer::the().async_set_system_theme_override(encoded.value());
  357. };
  358. m_theme_override_reset->on_click = [&](auto) {
  359. GUI::ConnectionToWindowServer::the().async_clear_system_theme_override();
  360. };
  361. GUI::Application::the()->on_theme_change = [&]() {
  362. auto override_active = GUI::ConnectionToWindowServer::the().is_system_theme_overridden();
  363. m_theme_override_apply->set_enabled(!override_active && window()->is_modified());
  364. m_theme_override_reset->set_enabled(override_active);
  365. };
  366. }
  367. ErrorOr<void> MainWidget::add_property_tab(PropertyTab const& property_tab)
  368. {
  369. auto scrollable_container = TRY(m_property_tabs->try_add_tab<GUI::ScrollableContainerWidget>(property_tab.title));
  370. scrollable_container->set_should_hide_unnecessary_scrollbars(true);
  371. auto properties_list = TRY(GUI::Widget::try_create());
  372. scrollable_container->set_widget(properties_list);
  373. (void)TRY(properties_list->try_set_layout<GUI::VerticalBoxLayout>(GUI::Margins { 8 }, 12));
  374. for (auto const& group : property_tab.property_groups) {
  375. NonnullRefPtr<GUI::GroupBox> group_box = TRY(properties_list->try_add<GUI::GroupBox>(group.title));
  376. // 1px less on the left makes the text line up with the group title.
  377. (void)TRY(group_box->try_set_layout<GUI::VerticalBoxLayout>(GUI::Margins { 8, 8, 8, 7 }, 12));
  378. group_box->set_preferred_height(GUI::SpecialDimension::Fit);
  379. for (auto const& property : group.properties) {
  380. NonnullRefPtr<GUI::Widget> row_widget = TRY(group_box->try_add<GUI::Widget>());
  381. row_widget->set_fixed_height(22);
  382. TRY(property.role.visit(
  383. [&](Gfx::AlignmentRole role) -> ErrorOr<void> {
  384. TRY(row_widget->load_from_gml(alignment_property_gml));
  385. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  386. name_label.set_text(to_string(role));
  387. auto& alignment_picker = *row_widget->find_descendant_of_type_named<GUI::ComboBox>("combo_box");
  388. alignment_picker.set_model(*m_alignment_model);
  389. alignment_picker.on_change = [&, role](auto&, auto& index) {
  390. set_alignment(role, index.data(GUI::ModelRole::Custom).to_text_alignment(Gfx::TextAlignment::CenterLeft));
  391. };
  392. alignment_picker.set_selected_index(m_alignment_model->index_of(m_current_palette.alignment(role)), GUI::AllowCallback::No);
  393. VERIFY(m_alignment_inputs[to_underlying(role)].is_null());
  394. m_alignment_inputs[to_underlying(role)] = alignment_picker;
  395. return {};
  396. },
  397. [&](Gfx::ColorRole role) -> ErrorOr<void> {
  398. TRY(row_widget->load_from_gml(color_property_gml));
  399. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  400. name_label.set_text(to_string(role));
  401. auto& color_input = *row_widget->find_descendant_of_type_named<GUI::ColorInput>("color_input");
  402. color_input.on_change = [&, role] {
  403. set_color(role, color_input.color());
  404. };
  405. color_input.set_color(m_current_palette.color(role), GUI::AllowCallback::No);
  406. VERIFY(m_color_inputs[to_underlying(role)].is_null());
  407. m_color_inputs[to_underlying(role)] = color_input;
  408. return {};
  409. },
  410. [&](Gfx::FlagRole role) -> ErrorOr<void> {
  411. TRY(row_widget->load_from_gml(flag_property_gml));
  412. auto& checkbox = *row_widget->find_descendant_of_type_named<GUI::CheckBox>("checkbox");
  413. checkbox.set_text(String::from_deprecated_string(DeprecatedString(to_string(role))).release_value_but_fixme_should_propagate_errors());
  414. checkbox.on_checked = [&, role](bool checked) {
  415. set_flag(role, checked);
  416. };
  417. checkbox.set_checked(m_current_palette.flag(role), GUI::AllowCallback::No);
  418. VERIFY(m_flag_inputs[to_underlying(role)].is_null());
  419. m_flag_inputs[to_underlying(role)] = checkbox;
  420. return {};
  421. },
  422. [&](Gfx::MetricRole role) -> ErrorOr<void> {
  423. TRY(row_widget->load_from_gml(metric_property_gml));
  424. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  425. name_label.set_text(to_string(role));
  426. auto& spin_box = *row_widget->find_descendant_of_type_named<GUI::SpinBox>("spin_box");
  427. spin_box.on_change = [&, role](int value) {
  428. set_metric(role, value);
  429. };
  430. spin_box.set_value(m_current_palette.metric(role), GUI::AllowCallback::No);
  431. VERIFY(m_metric_inputs[to_underlying(role)].is_null());
  432. m_metric_inputs[to_underlying(role)] = spin_box;
  433. return {};
  434. },
  435. [&](Gfx::PathRole role) -> ErrorOr<void> {
  436. TRY(row_widget->load_from_gml(path_property_gml));
  437. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  438. name_label.set_text(to_string(role));
  439. auto& path_input = *row_widget->find_descendant_of_type_named<GUI::TextBox>("path_input");
  440. path_input.on_change = [&, role] {
  441. set_path(role, path_input.text());
  442. };
  443. path_input.set_text(m_current_palette.path(role), GUI::AllowCallback::No);
  444. auto& path_picker_button = *row_widget->find_descendant_of_type_named<GUI::Button>("path_picker_button");
  445. auto picker_target = (role == Gfx::PathRole::TitleButtonIcons) ? PathPickerTarget::Folder : PathPickerTarget::File;
  446. path_picker_button.on_click = [&, role, picker_target](auto) {
  447. show_path_picker_dialog(to_string(role), path_input, picker_target);
  448. };
  449. VERIFY(m_path_inputs[to_underlying(role)].is_null());
  450. m_path_inputs[to_underlying(role)] = path_input;
  451. return {};
  452. }));
  453. }
  454. }
  455. return {};
  456. }
  457. void MainWidget::set_alignment(Gfx::AlignmentRole role, Gfx::TextAlignment value)
  458. {
  459. auto preview_palette = m_current_palette;
  460. preview_palette.set_alignment(role, value);
  461. set_palette(preview_palette);
  462. }
  463. void MainWidget::set_color(Gfx::ColorRole role, Gfx::Color value)
  464. {
  465. auto preview_palette = m_current_palette;
  466. preview_palette.set_color(role, value);
  467. set_palette(preview_palette);
  468. }
  469. void MainWidget::set_flag(Gfx::FlagRole role, bool value)
  470. {
  471. auto preview_palette = m_current_palette;
  472. preview_palette.set_flag(role, value);
  473. set_palette(preview_palette);
  474. }
  475. void MainWidget::set_metric(Gfx::MetricRole role, int value)
  476. {
  477. auto preview_palette = m_current_palette;
  478. preview_palette.set_metric(role, value);
  479. set_palette(preview_palette);
  480. }
  481. void MainWidget::set_path(Gfx::PathRole role, DeprecatedString value)
  482. {
  483. auto preview_palette = m_current_palette;
  484. preview_palette.set_path(role, value);
  485. set_palette(preview_palette);
  486. }
  487. void MainWidget::set_palette(Gfx::Palette palette)
  488. {
  489. m_current_palette = move(palette);
  490. m_preview_widget->set_preview_palette(m_current_palette);
  491. m_theme_override_apply->set_enabled(true);
  492. window()->set_modified(true);
  493. }
  494. void MainWidget::show_path_picker_dialog(StringView property_display_name, GUI::TextBox& path_input, PathPickerTarget path_picker_target)
  495. {
  496. bool open_folder = path_picker_target == PathPickerTarget::Folder;
  497. auto window_title = DeprecatedString::formatted(open_folder ? "Select {} folder"sv : "Select {} file"sv, property_display_name);
  498. auto target_path = path_input.text();
  499. if (Core::DeprecatedFile::exists(target_path)) {
  500. if (!Core::DeprecatedFile::is_directory(target_path))
  501. target_path = LexicalPath::dirname(target_path);
  502. } else {
  503. target_path = "/res/icons";
  504. }
  505. auto result = GUI::FilePicker::get_open_filepath(window(), window_title, target_path, open_folder);
  506. if (!result.has_value())
  507. return;
  508. path_input.set_text(*result);
  509. }
  510. ErrorOr<void> MainWidget::load_from_file(String const& filename, NonnullOwnPtr<Core::File> file)
  511. {
  512. auto config_file = TRY(Core::ConfigFile::open(filename.to_deprecated_string(), move(file)));
  513. auto theme = TRY(Gfx::load_system_theme(config_file));
  514. VERIFY(theme.is_valid());
  515. auto new_palette = Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(theme));
  516. set_palette(move(new_palette));
  517. set_path(filename.to_deprecated_string());
  518. #define __ENUMERATE_ALIGNMENT_ROLE(role) \
  519. if (auto alignment_input = m_alignment_inputs[to_underlying(Gfx::AlignmentRole::role)]) \
  520. alignment_input->set_selected_index(m_alignment_model->index_of(m_current_palette.alignment(Gfx::AlignmentRole::role)), GUI::AllowCallback::No);
  521. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  522. #undef __ENUMERATE_ALIGNMENT_ROLE
  523. #define __ENUMERATE_COLOR_ROLE(role) \
  524. if (auto color_input = m_color_inputs[to_underlying(Gfx::ColorRole::role)]) \
  525. color_input->set_color(m_current_palette.color(Gfx::ColorRole::role), GUI::AllowCallback::No);
  526. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  527. #undef __ENUMERATE_COLOR_ROLE
  528. #define __ENUMERATE_FLAG_ROLE(role) \
  529. if (auto flag_input = m_flag_inputs[to_underlying(Gfx::FlagRole::role)]) \
  530. flag_input->set_checked(m_current_palette.flag(Gfx::FlagRole::role), GUI::AllowCallback::No);
  531. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  532. #undef __ENUMERATE_FLAG_ROLE
  533. #define __ENUMERATE_METRIC_ROLE(role) \
  534. if (auto metric_input = m_metric_inputs[to_underlying(Gfx::MetricRole::role)]) \
  535. metric_input->set_value(m_current_palette.metric(Gfx::MetricRole::role), GUI::AllowCallback::No);
  536. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  537. #undef __ENUMERATE_METRIC_ROLE
  538. #define __ENUMERATE_PATH_ROLE(role) \
  539. if (auto path_input = m_path_inputs[to_underlying(Gfx::PathRole::role)]) \
  540. path_input->set_text(m_current_palette.path(Gfx::PathRole::role), GUI::AllowCallback::No);
  541. ENUMERATE_PATH_ROLES(__ENUMERATE_PATH_ROLE)
  542. #undef __ENUMERATE_PATH_ROLE
  543. m_last_modified_time = Time::now_monotonic();
  544. window()->set_modified(false);
  545. return {};
  546. }
  547. void MainWidget::drag_enter_event(GUI::DragEvent& event)
  548. {
  549. auto const& mime_types = event.mime_types();
  550. if (mime_types.contains_slow("text/uri-list"))
  551. event.accept();
  552. }
  553. void MainWidget::drop_event(GUI::DropEvent& event)
  554. {
  555. event.accept();
  556. window()->move_to_front();
  557. if (event.mime_data().has_urls()) {
  558. auto urls = event.mime_data().urls();
  559. if (urls.is_empty())
  560. return;
  561. if (urls.size() > 1) {
  562. GUI::MessageBox::show(window(), "ThemeEditor can only open one file at a time!"sv, "One at a time please!"sv, GUI::MessageBox::Type::Error);
  563. return;
  564. }
  565. if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
  566. return;
  567. auto response = FileSystemAccessClient::Client::the().request_file(window(), urls.first().path(), Core::File::OpenMode::Read);
  568. if (response.is_error())
  569. return;
  570. auto load_from_file_result = load_from_file(response.value().filename(), response.value().release_stream());
  571. if (load_from_file_result.is_error())
  572. GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Can't open file named {}: {}", response.value().filename(), load_from_file_result.error()));
  573. }
  574. }
  575. }