-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathlaunchers.cpp
More file actions
173 lines (151 loc) · 5.37 KB
/
Copy pathlaunchers.cpp
File metadata and controls
173 lines (151 loc) · 5.37 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "launchers.hpp"
#include <giomm/file.h>
#include <glibmm/spawn.h>
#include <glibmm/keyfile.h>
#include <gtkmm/icontheme.h>
#include <gdk/gdkcairo.h>
#include <cassert>
#include <iostream>
#include <gtk-utils.hpp>
#include <wf-shell-app.hpp>
bool WfLauncherButton::initialize(std::string name, std::string icon, std::string label)
{
app_info = Gio::DesktopAppInfo::create(name);
if (icon == "none")
{
if (!app_info)
{
// attempt to interpret it as a .desktop file
app_info = Gio::DesktopAppInfo::create_from_filename(name);
}
} else
{
// Generate a .desktop file in memory
auto keyfile = Glib::KeyFile();
keyfile.set_string("Desktop Entry", "Type", "Application");
keyfile.set_string("Desktop Entry", "Exec", "/bin/sh -c \"" + name + "\"");
keyfile.set_string("Desktop Entry", "Icon", icon);
if (label == "")
{
label = name;
}
keyfile.set_string("Desktop Entry", "Name", label);
/* needed for xdg-activation to work, see:
* https://gitlab.gnome.org/GNOME/glib/-/blob/main/gio/gdesktopappinfo.c?ref_type=heads#L1970
* https://gitlab.gnome.org/GNOME/glib/-/blob/main/gio/gdesktopappinfo.c?ref_type=heads#L2957
*/
keyfile.set_string("Desktop Entry", "StartupNotify", "true");
// Hand off to have a custom launcher
app_info = Gio::DesktopAppInfo::create_from_keyfile(keyfile);
}
/* Failed to get a launcher */
if (!app_info)
{
return false;
}
button.set_image(m_icon);
auto style = button.get_style_context();
style->add_class("flat");
style->add_class("launcher");
button.signal_clicked().connect([=] () { launch(); });
button.property_scale_factor().signal_changed()
.connect([=] () {update_icon(); });
icon_size.set_callback([=] () { update_icon(); });
update_icon();
button.set_tooltip_text(app_info->get_name());
return true;
}
void WfLauncherButton::update_icon()
{
set_image_icon(m_icon, app_info->get_icon()->to_string(), icon_size);
}
void WfLauncherButton::launch()
{
if (app_info)
{
auto ctx = Gdk::Display::get_default()->get_app_launch_context();
app_info->launch(std::vector<Glib::RefPtr<Gio::File>>(), ctx);
}
}
WfLauncherButton::WfLauncherButton()
{}
WfLauncherButton::~WfLauncherButton()
{}
static bool begins_with(const std::string& string, const std::string& prefix)
{
return string.size() >= prefix.size() &&
string.substr(0, prefix.size()) == prefix;
}
launcher_container WayfireLaunchers::get_launchers_from_config()
{
auto section = WayfireShellApp::get().config.get_section("panel");
const std::string desktop_prefix = "launcher_";
const std::string file_icon_prefix = "launcher_icon_";
const std::string file_cmd_prefix = "launcher_cmd_";
const std::string file_label_prefix = "launcher_label_";
launcher_container launchers;
auto try_push_launcher = [&launchers] (
const std::string cmd, const std::string icon, const std::string label = "")
{
auto launcher = new WfLauncherButton();
if (launcher->initialize(cmd, icon, label))
{
launchers.push_back(std::unique_ptr<WfLauncherButton>(launcher));
} else
{
delete launcher;
}
};
for (auto opt : section->get_registered_options())
{
/* we have a command */
if (begins_with(opt->get_name(), file_cmd_prefix))
{
/* extract launcher name, i.e the string after the prefix */
auto launcher_name = opt->get_name().substr(file_cmd_prefix.size());
/* look for the corresponding icon */
auto icon_option = section->get_option_or(file_icon_prefix + launcher_name);
if (icon_option)
{
/* bingo, found command + icon
* now look for the corresponding label */
auto label_option = section->get_option_or(file_label_prefix + launcher_name);
if (label_option)
{
/* found label */
try_push_launcher(opt->get_value_str(), icon_option->get_value_str(),
label_option->get_value_str());
} else
{
try_push_launcher(opt->get_value_str(), icon_option->get_value_str());
}
}
}
/* an entry is a deskop-file entry if the it has the desktop prefix
* but not the file_icon, file_cmd or file_label prefix */
if (begins_with(opt->get_name(), desktop_prefix) &&
!begins_with(opt->get_name(), file_icon_prefix) &&
!begins_with(opt->get_name(), file_cmd_prefix) &&
!begins_with(opt->get_name(), file_label_prefix))
{
try_push_launcher(opt->get_value_str(), "none");
}
}
return launchers;
}
void WayfireLaunchers::init(Gtk::HBox *container)
{
box.get_style_context()->add_class("launchers");
container->pack_start(box, false, false);
handle_config_reload();
}
void WayfireLaunchers::handle_config_reload()
{
box.set_spacing(WfOption<int>{"panel/launchers_spacing"});
launchers = get_launchers_from_config();
for (auto& l : launchers)
{
box.pack_start(l->button, false, false);
}
box.show_all();
}