From 87a22a77e9e1dc728358192c68da2320078b40dd Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 25 Jul 2021 21:52:39 +0200 Subject: [PATCH] Settings: Add a very simple Settings application This is really just a launcher app that gathers all the installed apps in the "Settings" category and presents them in a single window. --- Base/res/icons/16x16/app-settings.png | Bin 0 -> 259 bytes Base/res/icons/32x32/app-settings.png | Bin 0 -> 309 bytes Userland/Applications/CMakeLists.txt | 1 + Userland/Applications/Settings/CMakeLists.txt | 12 ++ Userland/Applications/Settings/main.cpp | 141 ++++++++++++++++++ 5 files changed, 154 insertions(+) create mode 100644 Base/res/icons/16x16/app-settings.png create mode 100644 Base/res/icons/32x32/app-settings.png create mode 100644 Userland/Applications/Settings/CMakeLists.txt create mode 100644 Userland/Applications/Settings/main.cpp diff --git a/Base/res/icons/16x16/app-settings.png b/Base/res/icons/16x16/app-settings.png new file mode 100644 index 0000000000000000000000000000000000000000..69a9c99bf79b78fe63e584115c1f059c0ac1041e GIT binary patch literal 259 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s7PIe8d3#@qCbe4x9&?|h|7OQ*w9UY7ZDxW<& z!PZo~K-a_N$g4)vcE@kKv;O~Cn5=U)fz6_3y7jE~;J_<6hTX3l6hA*LbYCNHFy&Hq z80(3+ycex^CL}$(zhCa5TKL)Qj(qlvaLb~H!t6HFs*S$xopIubuIc}UpQT@#Jb!webG|`0rLn02}4fhsnQQ$HD zY%^mXkHyS8Yc_BEEZ3-I!M!>s+CFpAr6ehPqZ$2+AEfX4d1mnm*Ch7R&XI!{!x>A1HF{ zS?Msvcvj56756`H=uWz=b?$5H#qHc*R!EENR0>Q9Q*GFoe?OkF`?H(8M#?#vV)+eQ z4p04jh5d=Xz5O=+Gn%TuzHZpP;M3+UF$Y9l_VYei%|GXESIzFLb2q$UKL56*XU~1v z{JHEAS8e1o?#z>(XT={8ywUXc*LRF{%OCwan<2I9?Va~+J3gG*z3s7nhD)8t@92j& R7#J8BJYD@<);T3K0RW(~i-iCH literal 0 HcmV?d00001 diff --git a/Userland/Applications/CMakeLists.txt b/Userland/Applications/CMakeLists.txt index 63fc2fc093..8bed54391b 100644 --- a/Userland/Applications/CMakeLists.txt +++ b/Userland/Applications/CMakeLists.txt @@ -23,6 +23,7 @@ add_subdirectory(PDFViewer) add_subdirectory(Piano) add_subdirectory(PixelPaint) add_subdirectory(Run) +add_subdirectory(Settings) add_subdirectory(SoundPlayer) add_subdirectory(SpaceAnalyzer) add_subdirectory(Spreadsheet) diff --git a/Userland/Applications/Settings/CMakeLists.txt b/Userland/Applications/Settings/CMakeLists.txt new file mode 100644 index 0000000000..edbabd23b2 --- /dev/null +++ b/Userland/Applications/Settings/CMakeLists.txt @@ -0,0 +1,12 @@ +serenity_component( + Settings + REQUIRED + TARGETS Settings +) + +set(SOURCES + main.cpp +) + +serenity_app(Settings ICON app-settings) +target_link_libraries(Settings LibGUI LibDesktop) diff --git a/Userland/Applications/Settings/main.cpp b/Userland/Applications/Settings/main.cpp new file mode 100644 index 0000000000..20bb5a9f2a --- /dev/null +++ b/Userland/Applications/Settings/main.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2021, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class SettingsAppsModel final : public GUI::Model { +public: + SettingsAppsModel() + { + Desktop::AppFile::for_each([&](Desktop::AppFile& app_file) { + if (app_file.category() != "Settings") + return; + m_apps.append(app_file); + }); + } + virtual int row_count(GUI::ModelIndex const&) const override { return m_apps.size(); } + virtual int column_count(GUI::ModelIndex const&) const override { return 1; } + + virtual GUI::ModelIndex index(int row, int column, GUI::ModelIndex const&) const override + { + if (row < 0 || row >= (int)m_apps.size()) + return {}; + return create_index(row, column, &m_apps[row]); + } + + virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override + { + auto& app = m_apps[index.row()]; + if (role == GUI::ModelRole::Icon) { + return app.icon(); + } + if (role == GUI::ModelRole::Display) { + String name; + if (app.name().ends_with(" Settings"sv)) { + name = app.name().substring(0, app.name().length() - " Settings"sv.length()); + } else { + name = app.name(); + } + return name; + } + if (role == GUI::ModelRole::Custom) { + return app.executable(); + } + return {}; + } + + virtual void update() override { } + +private: + NonnullRefPtrVector m_apps; +}; + +int main(int argc, char** argv) +{ + if (pledge("stdio thread recvfd sendfd rpath cpath wpath unix proc exec", nullptr) < 0) { + perror("pledge"); + return 1; + } + + auto app = GUI::Application::construct(argc, argv); + + if (pledge("stdio thread recvfd sendfd rpath cpath wpath proc exec", nullptr) < 0) { + perror("pledge"); + return 1; + } + + auto app_icon = GUI::Icon::default_icon("app-settings"); + + auto window = GUI::Window::construct(); + window->set_title("Settings"); + window->resize(400, 300); + + auto& file_menu = window->add_menu("&File"); + file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { + app->quit(); + })); + + auto& help_menu = window->add_menu("&Help"); + help_menu.add_action(GUI::CommonActions::make_about_action("Settings", app_icon, window)); + + auto& main_widget = window->set_main_widget(); + main_widget.set_fill_with_background_color(true); + main_widget.set_layout(); + + auto& icon_view = main_widget.add(); + icon_view.set_should_hide_unnecessary_scrollbars(true); + auto model = adopt_ref(*new SettingsAppsModel); + icon_view.set_model(*model); + + icon_view.on_activation = [&](GUI::ModelIndex const& index) { + auto& app = *(Desktop::AppFile*)index.internal_data(); + auto executable = app.executable(); + + auto launch_origin_rect = icon_view.to_widget_rect(icon_view.content_rect(index)).translated(icon_view.screen_relative_rect().location()); + setenv("__libgui_launch_origin_rect", String::formatted("{},{},{},{}", launch_origin_rect.x(), launch_origin_rect.y(), launch_origin_rect.width(), launch_origin_rect.height()).characters(), 1); + + pid_t child_pid; + const char* argv[] = { executable.characters(), nullptr }; + + if ((errno = posix_spawn(&child_pid, executable.characters(), nullptr, nullptr, const_cast(argv), environ))) { + perror("posix_spawn"); + return; + } + + if (disown(child_pid) < 0) + perror("disown"); + }; + + auto& statusbar = main_widget.add(); + + icon_view.on_selection_change = [&] { + auto index = icon_view.selection().first(); + if (!index.is_valid()) { + statusbar.set_text({}); + return; + } + + auto& app = *(Desktop::AppFile*)index.internal_data(); + statusbar.set_text(app.description()); + }; + + window->set_icon(app_icon.bitmap_for_size(16)); + + window->show(); + return app->exec(); +}