1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibDesktop/AppFile.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Icon.h>
#include <LibGUI/IconView.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Model.h>
#include <LibGUI/Statusbar.h>
#include <LibGUI/Window.h>
#include <serenity.h>
#include <spawn.h>
#include <stdio.h>
#include <unistd.h>
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<Desktop::AppFile> 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<GUI::Widget>();
main_widget.set_fill_with_background_color(true);
main_widget.set_layout<GUI::VerticalBoxLayout>();
auto& icon_view = main_widget.add<GUI::IconView>();
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<char**>(argv), environ))) {
perror("posix_spawn");
return;
}
if (disown(child_pid) < 0)
perror("disown");
};
auto& statusbar = main_widget.add<GUI::Statusbar>();
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();
}
|