MainWidget.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, networkException <networkexception@serenityos.org>
  4. * Copyright (c) 2021-2023, 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 <LibFileSystem/FileSystem.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 <LibGUI/Statusbar.h>
  34. namespace ThemeEditor {
  35. static PropertyTab const window_tab {
  36. "Windows"sv,
  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 PropertyTab const widgets_tab {
  84. "Widgets"sv,
  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 PropertyTab const syntax_highlighting_tab {
  142. "Syntax Highlighting"sv,
  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 PropertyTab const color_scheme_tab {
  165. "Color Scheme"sv,
  166. {
  167. { "General",
  168. { { Gfx::FlagRole::BoldTextAsBright },
  169. { Gfx::ColorRole::Black },
  170. { Gfx::ColorRole::Red },
  171. { Gfx::ColorRole::Green },
  172. { Gfx::ColorRole::Yellow },
  173. { Gfx::ColorRole::Blue },
  174. { Gfx::ColorRole::Magenta },
  175. { Gfx::ColorRole::ColorSchemeBackground },
  176. { Gfx::ColorRole::ColorSchemeForeground },
  177. { Gfx::ColorRole::Cyan },
  178. { Gfx::ColorRole::White },
  179. { Gfx::ColorRole::BrightBlack },
  180. { Gfx::ColorRole::BrightRed },
  181. { Gfx::ColorRole::BrightGreen },
  182. { Gfx::ColorRole::BrightYellow },
  183. { Gfx::ColorRole::BrightBlue },
  184. { Gfx::ColorRole::BrightMagenta },
  185. { Gfx::ColorRole::BrightCyan },
  186. { Gfx::ColorRole::BrightWhite } } },
  187. }
  188. };
  189. ErrorOr<NonnullRefPtr<MainWidget>> MainWidget::try_create()
  190. {
  191. auto alignment_model = TRY(AlignmentModel::try_create());
  192. auto main_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MainWidget(move(alignment_model))));
  193. TRY(main_widget->load_from_gml(theme_editor_gml));
  194. main_widget->m_preview_widget = main_widget->find_descendant_of_type_named<ThemeEditor::PreviewWidget>("preview_widget");
  195. main_widget->m_property_tabs = main_widget->find_descendant_of_type_named<GUI::TabWidget>("property_tabs");
  196. main_widget->m_statusbar = main_widget->find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  197. TRY(main_widget->add_property_tab(window_tab));
  198. TRY(main_widget->add_property_tab(widgets_tab));
  199. TRY(main_widget->add_property_tab(syntax_highlighting_tab));
  200. TRY(main_widget->add_property_tab(color_scheme_tab));
  201. main_widget->build_override_controls();
  202. return main_widget;
  203. }
  204. MainWidget::MainWidget(NonnullRefPtr<AlignmentModel> alignment_model)
  205. : m_current_palette(GUI::Application::the()->palette())
  206. , m_alignment_model(move(alignment_model))
  207. {
  208. GUI::Application::the()->on_action_enter = [this](GUI::Action& action) {
  209. m_statusbar->set_override_text(action.status_tip());
  210. };
  211. GUI::Application::the()->on_action_leave = [this](GUI::Action&) {
  212. m_statusbar->set_override_text({});
  213. };
  214. }
  215. ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
  216. {
  217. auto file_menu = window.add_menu("&File"_string);
  218. file_menu->add_action(GUI::CommonActions::make_open_action([&](auto&) {
  219. if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
  220. return;
  221. FileSystemAccessClient::OpenFileOptions options {
  222. .window_title = "Select Theme"sv,
  223. .path = "/res/themes"sv,
  224. .allowed_file_types = Vector { { "Theme Files", { { "ini" } } }, GUI::FileTypeFilter::all_files() },
  225. };
  226. auto response = FileSystemAccessClient::Client::the().open_file(&window, options);
  227. if (response.is_error())
  228. return;
  229. auto load_from_file_result = load_from_file(response.value().filename(), response.value().release_stream());
  230. if (load_from_file_result.is_error()) {
  231. GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Can't open file named {}: {}", response.value().filename(), load_from_file_result.error()));
  232. return;
  233. }
  234. }));
  235. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  236. if (m_path.has_value()) {
  237. auto result = FileSystemAccessClient::Client::the().request_file(&window, *m_path, Core::File::OpenMode::ReadWrite | Core::File::OpenMode::Truncate);
  238. if (result.is_error())
  239. return;
  240. save_to_file(result.value().filename(), result.value().release_stream());
  241. } else {
  242. auto result = FileSystemAccessClient::Client::the().save_file(&window, "Theme", "ini", Core::File::OpenMode::ReadWrite | Core::File::OpenMode::Truncate);
  243. if (result.is_error())
  244. return;
  245. save_to_file(result.value().filename(), result.value().release_stream());
  246. }
  247. });
  248. file_menu->add_action(*m_save_action);
  249. file_menu->add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
  250. auto result = FileSystemAccessClient::Client::the().save_file(&window, "Theme", "ini", Core::File::OpenMode::ReadWrite | Core::File::OpenMode::Truncate);
  251. if (result.is_error())
  252. return;
  253. save_to_file(result.value().filename(), result.value().release_stream());
  254. }));
  255. file_menu->add_separator();
  256. file_menu->add_recent_files_list([&](auto& action) {
  257. if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
  258. return;
  259. auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(&window, action.text());
  260. if (response.is_error())
  261. return;
  262. auto load_from_file_result = load_from_file(response.value().filename(), response.value().release_stream());
  263. if (load_from_file_result.is_error()) {
  264. GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Can't open file named {}: {}", response.value().filename(), load_from_file_result.error()));
  265. return;
  266. }
  267. });
  268. file_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) {
  269. if (request_close() == GUI::Window::CloseRequestDecision::Close)
  270. GUI::Application::the()->quit();
  271. }));
  272. window.add_menu(GUI::CommonMenus::make_accessibility_menu(*m_preview_widget));
  273. auto help_menu = window.add_menu("&Help"_string);
  274. help_menu->add_action(GUI::CommonActions::make_command_palette_action(&window));
  275. help_menu->add_action(GUI::CommonActions::make_about_action("Theme Editor"_string, GUI::Icon::default_icon("app-theme-editor"sv), &window));
  276. return {};
  277. }
  278. void MainWidget::update_title()
  279. {
  280. window()->set_title(DeprecatedString::formatted("{}[*] - Theme Editor", m_path.value_or("Untitled")));
  281. }
  282. GUI::Window::CloseRequestDecision MainWidget::request_close()
  283. {
  284. if (!window()->is_modified())
  285. return GUI::Window::CloseRequestDecision::Close;
  286. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_path.value_or(""), m_last_modified_time);
  287. if (result == GUI::MessageBox::ExecResult::Yes) {
  288. m_save_action->activate();
  289. if (window()->is_modified())
  290. return GUI::Window::CloseRequestDecision::StayOpen;
  291. return GUI::Window::CloseRequestDecision::Close;
  292. }
  293. if (result == GUI::MessageBox::ExecResult::No)
  294. return GUI::Window::CloseRequestDecision::Close;
  295. return GUI::Window::CloseRequestDecision::StayOpen;
  296. }
  297. void MainWidget::set_path(DeprecatedString path)
  298. {
  299. m_path = path;
  300. update_title();
  301. }
  302. void MainWidget::save_to_file(String const& filename, NonnullOwnPtr<Core::File> file)
  303. {
  304. auto theme = Core::ConfigFile::open(filename.to_deprecated_string(), move(file)).release_value_but_fixme_should_propagate_errors();
  305. #define __ENUMERATE_ALIGNMENT_ROLE(role) theme->write_entry("Alignments", to_string(Gfx::AlignmentRole::role), to_string(m_current_palette.alignment(Gfx::AlignmentRole::role)));
  306. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  307. #undef __ENUMERATE_ALIGNMENT_ROLE
  308. #define __ENUMERATE_COLOR_ROLE(role) theme->write_entry("Colors", to_string(Gfx::ColorRole::role), m_current_palette.color(Gfx::ColorRole::role).to_deprecated_string());
  309. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  310. #undef __ENUMERATE_COLOR_ROLE
  311. #define __ENUMERATE_FLAG_ROLE(role) theme->write_bool_entry("Flags", to_string(Gfx::FlagRole::role), m_current_palette.flag(Gfx::FlagRole::role));
  312. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  313. #undef __ENUMERATE_FLAG_ROLE
  314. #define __ENUMERATE_METRIC_ROLE(role) theme->write_num_entry("Metrics", to_string(Gfx::MetricRole::role), m_current_palette.metric(Gfx::MetricRole::role));
  315. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  316. #undef __ENUMERATE_METRIC_ROLE
  317. #define __ENUMERATE_PATH_ROLE(role) theme->write_entry("Paths", to_string(Gfx::PathRole::role), m_current_palette.path(Gfx::PathRole::role));
  318. ENUMERATE_PATH_ROLES(__ENUMERATE_PATH_ROLE)
  319. #undef __ENUMERATE_PATH_ROLE
  320. auto sync_result = theme->sync();
  321. if (sync_result.is_error()) {
  322. GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Failed to save theme file: {}", sync_result.error()));
  323. } else {
  324. m_last_modified_time = MonotonicTime::now();
  325. set_path(filename.to_deprecated_string());
  326. window()->set_modified(false);
  327. GUI::Application::the()->set_most_recently_open_file(filename);
  328. }
  329. }
  330. ErrorOr<Core::AnonymousBuffer> MainWidget::encode()
  331. {
  332. auto buffer = TRY(Core::AnonymousBuffer::create_with_size(sizeof(Gfx::SystemTheme)));
  333. auto* data = buffer.data<Gfx::SystemTheme>();
  334. #define __ENUMERATE_ALIGNMENT_ROLE(role) \
  335. data->alignment[(int)Gfx::AlignmentRole::role] = m_current_palette.alignment(Gfx::AlignmentRole::role);
  336. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  337. #undef __ENUMERATE_ALIGNMENT_ROLE
  338. #define __ENUMERATE_COLOR_ROLE(role) \
  339. data->color[(int)Gfx::ColorRole::role] = m_current_palette.color(Gfx::ColorRole::role).value();
  340. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  341. #undef __ENUMERATE_COLOR_ROLE
  342. #define __ENUMERATE_FLAG_ROLE(role) \
  343. data->flag[(int)Gfx::FlagRole::role] = m_current_palette.flag(Gfx::FlagRole::role);
  344. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  345. #undef __ENUMERATE_FLAG_ROLE
  346. #define __ENUMERATE_METRIC_ROLE(role) \
  347. data->metric[(int)Gfx::MetricRole::role] = m_current_palette.metric(Gfx::MetricRole::role);
  348. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  349. #undef __ENUMERATE_METRIC_ROLE
  350. #define ENCODE_PATH(role, allow_empty) \
  351. do { \
  352. auto path = m_current_palette.path(Gfx::PathRole::role); \
  353. char const* characters; \
  354. if (path.is_empty()) { \
  355. switch (Gfx::PathRole::role) { \
  356. case Gfx::PathRole::TitleButtonIcons: \
  357. characters = "/res/icons/16x16/"; \
  358. break; \
  359. default: \
  360. characters = allow_empty ? "" : "/res/"; \
  361. } \
  362. } \
  363. characters = path.characters(); \
  364. memcpy(data->path[(int)Gfx::PathRole::role], characters, min(strlen(characters) + 1, sizeof(data->path[(int)Gfx::PathRole::role]))); \
  365. data->path[(int)Gfx::PathRole::role][sizeof(data->path[(int)Gfx::PathRole::role]) - 1] = '\0'; \
  366. } while (0)
  367. ENCODE_PATH(TitleButtonIcons, false);
  368. ENCODE_PATH(ActiveWindowShadow, true);
  369. ENCODE_PATH(InactiveWindowShadow, true);
  370. ENCODE_PATH(TaskbarShadow, true);
  371. ENCODE_PATH(MenuShadow, true);
  372. ENCODE_PATH(TooltipShadow, true);
  373. return buffer;
  374. }
  375. void MainWidget::build_override_controls()
  376. {
  377. auto* theme_override_controls = find_descendant_of_type_named<GUI::Widget>("theme_override_controls");
  378. m_theme_override_apply = theme_override_controls->find_child_of_type_named<GUI::DialogButton>("apply_button");
  379. m_theme_override_reset = theme_override_controls->find_child_of_type_named<GUI::DialogButton>("reset_button");
  380. m_theme_override_apply->on_click = [&](auto) {
  381. auto encoded = encode();
  382. if (encoded.is_error())
  383. return;
  384. // Empty the color scheme path to signal that it exists only in memory.
  385. m_current_palette.path(Gfx::PathRole::ColorScheme) = "";
  386. GUI::ConnectionToWindowServer::the().async_set_system_theme_override(encoded.value());
  387. };
  388. m_theme_override_reset->on_click = [&](auto) {
  389. GUI::ConnectionToWindowServer::the().async_clear_system_theme_override();
  390. };
  391. GUI::Application::the()->on_theme_change = [&]() {
  392. auto override_active = GUI::ConnectionToWindowServer::the().is_system_theme_overridden();
  393. m_theme_override_apply->set_enabled(!override_active && window()->is_modified());
  394. m_theme_override_reset->set_enabled(override_active);
  395. };
  396. }
  397. ErrorOr<void> MainWidget::add_property_tab(PropertyTab const& property_tab)
  398. {
  399. auto scrollable_container = TRY(m_property_tabs->try_add_tab<GUI::ScrollableContainerWidget>(TRY(String::from_utf8(property_tab.title))));
  400. scrollable_container->set_should_hide_unnecessary_scrollbars(true);
  401. auto properties_list = TRY(GUI::Widget::try_create());
  402. scrollable_container->set_widget(properties_list);
  403. properties_list->set_layout<GUI::VerticalBoxLayout>(GUI::Margins { 8 }, 12);
  404. for (auto const& group : property_tab.property_groups) {
  405. NonnullRefPtr<GUI::GroupBox> group_box = TRY(properties_list->try_add<GUI::GroupBox>(group.title));
  406. // 1px less on the left makes the text line up with the group title.
  407. group_box->set_layout<GUI::VerticalBoxLayout>(GUI::Margins { 8, 8, 8, 7 }, 12);
  408. group_box->set_preferred_height(GUI::SpecialDimension::Fit);
  409. for (auto const& property : group.properties) {
  410. NonnullRefPtr<GUI::Widget> row_widget = TRY(group_box->try_add<GUI::Widget>());
  411. row_widget->set_fixed_height(22);
  412. TRY(property.role.visit(
  413. [&](Gfx::AlignmentRole role) -> ErrorOr<void> {
  414. TRY(row_widget->load_from_gml(alignment_property_gml));
  415. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  416. name_label.set_text(TRY(String::from_utf8(to_string(role))));
  417. auto& alignment_picker = *row_widget->find_descendant_of_type_named<GUI::ComboBox>("combo_box");
  418. alignment_picker.set_model(*m_alignment_model);
  419. alignment_picker.on_change = [&, role](auto&, auto& index) {
  420. set_alignment(role, index.data(GUI::ModelRole::Custom).to_text_alignment(Gfx::TextAlignment::CenterLeft));
  421. };
  422. alignment_picker.set_selected_index(m_alignment_model->index_of(m_current_palette.alignment(role)), GUI::AllowCallback::No);
  423. VERIFY(m_alignment_inputs[to_underlying(role)].is_null());
  424. m_alignment_inputs[to_underlying(role)] = alignment_picker;
  425. return {};
  426. },
  427. [&](Gfx::ColorRole role) -> ErrorOr<void> {
  428. TRY(row_widget->load_from_gml(color_property_gml));
  429. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  430. name_label.set_text(TRY(String::from_utf8(to_string(role))));
  431. auto& color_input = *row_widget->find_descendant_of_type_named<GUI::ColorInput>("color_input");
  432. color_input.on_change = [&, role] {
  433. set_color(role, color_input.color());
  434. };
  435. color_input.set_color(m_current_palette.color(role), GUI::AllowCallback::No);
  436. VERIFY(m_color_inputs[to_underlying(role)].is_null());
  437. m_color_inputs[to_underlying(role)] = color_input;
  438. return {};
  439. },
  440. [&](Gfx::FlagRole role) -> ErrorOr<void> {
  441. TRY(row_widget->load_from_gml(flag_property_gml));
  442. auto& checkbox = *row_widget->find_descendant_of_type_named<GUI::CheckBox>("checkbox");
  443. checkbox.set_text(TRY(String::from_utf8(to_string(role))));
  444. checkbox.on_checked = [&, role](bool checked) {
  445. set_flag(role, checked);
  446. };
  447. checkbox.set_checked(m_current_palette.flag(role), GUI::AllowCallback::No);
  448. VERIFY(m_flag_inputs[to_underlying(role)].is_null());
  449. m_flag_inputs[to_underlying(role)] = checkbox;
  450. return {};
  451. },
  452. [&](Gfx::MetricRole role) -> ErrorOr<void> {
  453. TRY(row_widget->load_from_gml(metric_property_gml));
  454. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  455. name_label.set_text(TRY(String::from_utf8(to_string(role))));
  456. auto& spin_box = *row_widget->find_descendant_of_type_named<GUI::SpinBox>("spin_box");
  457. spin_box.on_change = [&, role](int value) {
  458. set_metric(role, value);
  459. };
  460. spin_box.set_value(m_current_palette.metric(role), GUI::AllowCallback::No);
  461. VERIFY(m_metric_inputs[to_underlying(role)].is_null());
  462. m_metric_inputs[to_underlying(role)] = spin_box;
  463. return {};
  464. },
  465. [&](Gfx::PathRole role) -> ErrorOr<void> {
  466. TRY(row_widget->load_from_gml(path_property_gml));
  467. auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
  468. name_label.set_text(TRY(String::from_utf8(to_string(role))));
  469. auto& path_input = *row_widget->find_descendant_of_type_named<GUI::TextBox>("path_input");
  470. path_input.on_change = [&, role] {
  471. set_path(role, path_input.text());
  472. };
  473. path_input.set_text(m_current_palette.path(role), GUI::AllowCallback::No);
  474. auto& path_picker_button = *row_widget->find_descendant_of_type_named<GUI::Button>("path_picker_button");
  475. auto picker_target = (role == Gfx::PathRole::TitleButtonIcons) ? PathPickerTarget::Folder : PathPickerTarget::File;
  476. path_picker_button.on_click = [&, role, picker_target](auto) {
  477. show_path_picker_dialog(to_string(role), path_input, picker_target);
  478. };
  479. VERIFY(m_path_inputs[to_underlying(role)].is_null());
  480. m_path_inputs[to_underlying(role)] = path_input;
  481. return {};
  482. }));
  483. }
  484. }
  485. return {};
  486. }
  487. void MainWidget::set_alignment(Gfx::AlignmentRole role, Gfx::TextAlignment value)
  488. {
  489. auto preview_palette = m_current_palette;
  490. preview_palette.set_alignment(role, value);
  491. set_palette(preview_palette);
  492. }
  493. void MainWidget::set_color(Gfx::ColorRole role, Gfx::Color value)
  494. {
  495. auto preview_palette = m_current_palette;
  496. preview_palette.set_color(role, value);
  497. set_palette(preview_palette);
  498. }
  499. void MainWidget::set_flag(Gfx::FlagRole role, bool value)
  500. {
  501. auto preview_palette = m_current_palette;
  502. preview_palette.set_flag(role, value);
  503. set_palette(preview_palette);
  504. }
  505. void MainWidget::set_metric(Gfx::MetricRole role, int value)
  506. {
  507. auto preview_palette = m_current_palette;
  508. preview_palette.set_metric(role, value);
  509. set_palette(preview_palette);
  510. }
  511. void MainWidget::set_path(Gfx::PathRole role, DeprecatedString value)
  512. {
  513. auto preview_palette = m_current_palette;
  514. preview_palette.set_path(role, value);
  515. set_palette(preview_palette);
  516. }
  517. void MainWidget::set_palette(Gfx::Palette palette)
  518. {
  519. m_current_palette = move(palette);
  520. m_preview_widget->set_preview_palette(m_current_palette);
  521. m_theme_override_apply->set_enabled(true);
  522. window()->set_modified(true);
  523. }
  524. void MainWidget::show_path_picker_dialog(StringView property_display_name, GUI::TextBox& path_input, PathPickerTarget path_picker_target)
  525. {
  526. bool open_folder = path_picker_target == PathPickerTarget::Folder;
  527. auto window_title = DeprecatedString::formatted(open_folder ? "Select {} folder"sv : "Select {} file"sv, property_display_name);
  528. auto target_path = path_input.text();
  529. if (FileSystem::exists(target_path)) {
  530. if (!FileSystem::is_directory(target_path))
  531. target_path = LexicalPath::dirname(target_path);
  532. } else {
  533. target_path = "/res/icons";
  534. }
  535. auto result = GUI::FilePicker::get_open_filepath(window(), window_title, target_path, open_folder);
  536. if (!result.has_value())
  537. return;
  538. path_input.set_text(*result);
  539. }
  540. ErrorOr<void> MainWidget::load_from_file(String const& filename, NonnullOwnPtr<Core::File> file)
  541. {
  542. auto config_file = TRY(Core::ConfigFile::open(filename.to_deprecated_string(), move(file)));
  543. auto theme = TRY(Gfx::load_system_theme(config_file));
  544. VERIFY(theme.is_valid());
  545. auto new_palette = Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(theme));
  546. set_palette(move(new_palette));
  547. set_path(filename.to_deprecated_string());
  548. #define __ENUMERATE_ALIGNMENT_ROLE(role) \
  549. if (auto alignment_input = m_alignment_inputs[to_underlying(Gfx::AlignmentRole::role)]) \
  550. alignment_input->set_selected_index(m_alignment_model->index_of(m_current_palette.alignment(Gfx::AlignmentRole::role)), GUI::AllowCallback::No);
  551. ENUMERATE_ALIGNMENT_ROLES(__ENUMERATE_ALIGNMENT_ROLE)
  552. #undef __ENUMERATE_ALIGNMENT_ROLE
  553. #define __ENUMERATE_COLOR_ROLE(role) \
  554. if (auto color_input = m_color_inputs[to_underlying(Gfx::ColorRole::role)]) \
  555. color_input->set_color(m_current_palette.color(Gfx::ColorRole::role), GUI::AllowCallback::No);
  556. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  557. #undef __ENUMERATE_COLOR_ROLE
  558. #define __ENUMERATE_FLAG_ROLE(role) \
  559. if (auto flag_input = m_flag_inputs[to_underlying(Gfx::FlagRole::role)]) \
  560. flag_input->set_checked(m_current_palette.flag(Gfx::FlagRole::role), GUI::AllowCallback::No);
  561. ENUMERATE_FLAG_ROLES(__ENUMERATE_FLAG_ROLE)
  562. #undef __ENUMERATE_FLAG_ROLE
  563. #define __ENUMERATE_METRIC_ROLE(role) \
  564. if (auto metric_input = m_metric_inputs[to_underlying(Gfx::MetricRole::role)]) \
  565. metric_input->set_value(m_current_palette.metric(Gfx::MetricRole::role), GUI::AllowCallback::No);
  566. ENUMERATE_METRIC_ROLES(__ENUMERATE_METRIC_ROLE)
  567. #undef __ENUMERATE_METRIC_ROLE
  568. #define __ENUMERATE_PATH_ROLE(role) \
  569. if (auto path_input = m_path_inputs[to_underlying(Gfx::PathRole::role)]) \
  570. path_input->set_text(m_current_palette.path(Gfx::PathRole::role), GUI::AllowCallback::No);
  571. ENUMERATE_PATH_ROLES(__ENUMERATE_PATH_ROLE)
  572. #undef __ENUMERATE_PATH_ROLE
  573. m_last_modified_time = MonotonicTime::now();
  574. window()->set_modified(false);
  575. GUI::Application::the()->set_most_recently_open_file(filename);
  576. return {};
  577. }
  578. void MainWidget::drag_enter_event(GUI::DragEvent& event)
  579. {
  580. auto const& mime_types = event.mime_types();
  581. if (mime_types.contains_slow("text/uri-list"))
  582. event.accept();
  583. }
  584. void MainWidget::drop_event(GUI::DropEvent& event)
  585. {
  586. event.accept();
  587. window()->move_to_front();
  588. if (event.mime_data().has_urls()) {
  589. auto urls = event.mime_data().urls();
  590. if (urls.is_empty())
  591. return;
  592. if (urls.size() > 1) {
  593. GUI::MessageBox::show(window(), "ThemeEditor can only open one file at a time!"sv, "One at a time please!"sv, GUI::MessageBox::Type::Error);
  594. return;
  595. }
  596. if (request_close() == GUI::Window::CloseRequestDecision::StayOpen)
  597. return;
  598. auto response = FileSystemAccessClient::Client::the().request_file(window(), urls.first().serialize_path(), Core::File::OpenMode::Read);
  599. if (response.is_error())
  600. return;
  601. auto load_from_file_result = load_from_file(response.value().filename(), response.value().release_stream());
  602. if (load_from_file_result.is_error())
  603. GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Can't open file named {}: {}", response.value().filename(), load_from_file_result.error()));
  604. }
  605. }
  606. }