forked from WayfireWM/wf-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoplevel-icon.cpp
More file actions
336 lines (284 loc) · 8.27 KB
/
toplevel-icon.cpp
File metadata and controls
336 lines (284 loc) · 8.27 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <giomm/desktopappinfo.h>
#include <gtkmm/button.h>
#include <gtkmm/hvbox.h>
#include <gtkmm/icontheme.h>
#include <gtkmm/image.h>
#include <gdkmm/display.h>
#include <gdkmm/seat.h>
#include <gdk/gdkwayland.h>
#include "dock.hpp"
#include "toplevel.hpp"
#include "toplevel-icon.hpp"
#include "gtk-utils.hpp"
#include <iostream>
#include <sstream>
#include <cassert>
#include "wf-option-wrap.hpp"
namespace IconProvider
{
void set_image_from_icon(Gtk::Image& image,
std::string app_id_list, int size, int scale);
}
class WfToplevelIcon::impl
{
zwlr_foreign_toplevel_handle_v1 *handle;
wl_output *output;
uint32_t state;
Gtk::Button button;
Gtk::Image image;
std::string app_id;
WfOption<int> icon_height{"dock/icon_height"};
public:
impl(zwlr_foreign_toplevel_handle_v1 *handle, wl_output *output)
{
this->handle = handle;
this->output = output;
button.add(image);
button.set_tooltip_text("none");
button.get_style_context()->add_class("flat");
button.show_all();
button.signal_clicked().connect_notify(
sigc::mem_fun(this, &WfToplevelIcon::impl::on_clicked));
button.signal_size_allocate().connect_notify(
sigc::mem_fun(this, &WfToplevelIcon::impl::on_allocation_changed));
button.property_scale_factor().signal_changed()
.connect(sigc::mem_fun(this, &WfToplevelIcon::impl::on_scale_update));
auto dock = WfDockApp::get().dock_for_wl_output(output);
assert(dock); // ToplevelIcon is created only for existing outputs
dock->add_child(button);
}
void on_clicked()
{
if (!(state & WF_TOPLEVEL_STATE_ACTIVATED))
{
auto gseat = Gdk::Display::get_default()->get_default_seat();
auto seat = gdk_wayland_seat_get_wl_seat(gseat->gobj());
zwlr_foreign_toplevel_handle_v1_activate(handle, seat);
} else
{
send_rectangle_hint();
if (state & WF_TOPLEVEL_STATE_MINIMIZED)
{
zwlr_foreign_toplevel_handle_v1_unset_minimized(handle);
} else
{
zwlr_foreign_toplevel_handle_v1_set_minimized(handle);
}
}
}
void on_allocation_changed(Gtk::Allocation& alloc)
{
send_rectangle_hint();
}
void on_scale_update()
{
set_app_id(app_id);
}
void set_app_id(std::string app_id)
{
this->app_id = app_id;
IconProvider::set_image_from_icon(image,
app_id,
icon_height,
button.get_scale_factor());
}
void send_rectangle_hint()
{
Gtk::Widget *widget = &this->button;
int x = 0, y = 0;
int width = image.get_allocated_width();
int height = image.get_allocated_height();
while (widget)
{
x += widget->get_allocation().get_x();
y += widget->get_allocation().get_y();
widget = widget->get_parent();
}
auto dock = WfDockApp::get().dock_for_wl_output(output);
if (!dock)
{
return;
}
zwlr_foreign_toplevel_handle_v1_set_rectangle(handle,
dock->get_wl_surface(), x, y, width, height);
}
void set_title(std::string title)
{
button.set_tooltip_text(title);
}
void set_state(uint32_t state)
{
bool was_activated = this->state & WF_TOPLEVEL_STATE_ACTIVATED;
this->state = state;
bool is_activated = this->state & WF_TOPLEVEL_STATE_ACTIVATED;
if (!was_activated && is_activated)
{
this->button.get_style_context()->remove_class("flat");
} else if (was_activated && !is_activated)
{
this->button.get_style_context()->add_class("flat");
}
}
~impl()
{
auto dock = WfDockApp::get().dock_for_wl_output(output);
if (dock)
{
dock->rem_child(button);
}
}
};
WfToplevelIcon::WfToplevelIcon(zwlr_foreign_toplevel_handle_v1 *handle,
wl_output *output) : pimpl(new impl(handle, output))
{}
WfToplevelIcon::~WfToplevelIcon() = default;
void WfToplevelIcon::set_title(std::string title)
{
return pimpl->set_title(title);
}
void WfToplevelIcon::set_app_id(std::string app_id)
{
return pimpl->set_app_id(app_id);
}
void WfToplevelIcon::set_state(uint32_t state)
{
return pimpl->set_state(state);
}
/* Icon loading functions */
namespace IconProvider
{
using Icon = Glib::RefPtr<Gio::Icon>;
namespace
{
std::string tolower(std::string str)
{
for (auto& c : str)
{
c = std::tolower(c);
}
return str;
}
std::map<std::string, std::string> custom_icons;
}
void load_custom_icons()
{
static const std::string prefix = "icon_mapping_";
auto section = WayfireShellApp::get().config.get_section("dock");
for (auto option : section->get_registered_options())
{
if (option->get_name().compare(0, prefix.length(), prefix) != 0)
{
continue;
}
auto app_id = option->get_name().substr(prefix.length());
custom_icons[app_id] = option->get_value_str();
}
}
bool set_custom_icon(Gtk::Image& image, std::string app_id, int size, int scale)
{
if (!custom_icons.count(app_id))
{
return false;
}
auto pb = load_icon_pixbuf_safe(custom_icons[app_id], size * scale);
if (!pb.get())
{
return false;
}
set_image_pixbuf(image, pb, scale);
return true;
}
/* Gio::DesktopAppInfo
*
* Usually knowing the app_id, we can get a desktop app info from Gio
* The filename is either the app_id + ".desktop" or lower_app_id + ".desktop" */
Icon get_from_desktop_app_info(std::string app_id)
{
Glib::RefPtr<Gio::DesktopAppInfo> app_info;
std::vector<std::string> prefixes = {
"",
"/usr/share/applications/",
"/usr/share/applications/kde/",
"/usr/share/applications/org.kde.",
"/usr/share/applications/org.gnome.",
"/usr/local/share/applications/",
"/usr/local/share/applications/org.kde.",
"/usr/local/share/applications/org.gnome.",
};
std::vector<std::string> app_id_variations = {
app_id,
tolower(app_id),
tolower(app_id),
};
// e.g. org.gnome.Evince.desktop
app_id_variations[2][0] = std::toupper(app_id_variations[2][0]);
std::vector<std::string> suffixes = {
"",
".desktop"
};
for (auto& prefix : prefixes)
{
for (auto& id : app_id_variations)
{
for (auto& suffix : suffixes)
{
if (!app_info)
{
app_info = Gio::DesktopAppInfo
::create_from_filename(prefix + id + suffix);
}
}
}
}
if (app_info) // success
{
return app_info->get_icon();
}
return Icon{};
}
void set_image_from_icon(Gtk::Image& image,
std::string app_id_list, int size, int scale)
{
std::string app_id;
std::istringstream stream(app_id_list);
bool found_icon = false;
/* Wayfire sends a list of app-id's in space separated format, other compositors
* send a single app-id, but in any case this works fine */
while (stream >> app_id)
{
/* Try first method: custom icon file provided by the user */
if (set_custom_icon(image, app_id, size, scale))
{
found_icon = true;
break;
}
/* Then try to load the DesktopAppInfo */
auto icon = get_from_desktop_app_info(app_id);
std::string icon_name = "unknown";
if (!icon)
{
/* Finally try directly looking up the icon, if it exists */
if (Gtk::IconTheme::get_default()->lookup_icon(app_id, 24))
{
icon_name = app_id;
}
} else
{
icon_name = icon->to_string();
}
WfIconLoadOptions options;
options.user_scale = scale;
set_image_icon(image, icon_name, size, options);
/* finally found some icon */
if (icon_name != "unknown")
{
found_icon = true;
break;
}
}
if (!found_icon)
{
std::cout << "Failed to load icon for any of " << app_id_list << std::endl;
}
}
}