-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtoplevel.cpp
More file actions
766 lines (652 loc) · 22 KB
/
Copy pathtoplevel.cpp
File metadata and controls
766 lines (652 loc) · 22 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#include <gtkmm.h>
#include <giomm/desktopappinfo.h>
#include <iostream>
#include <gdkmm/seat.h>
#include <gdk/wayland/gdkwayland.h>
#include <cmath>
#include <glibmm.h>
#include "toplevel.hpp"
#include "gtk-utils.hpp"
#include "panel.hpp"
#include <cassert>
namespace
{
extern zwlr_foreign_toplevel_handle_v1_listener toplevel_handle_v1_impl;
}
namespace IconProvider
{
void set_image_from_icon(Gtk::Image& image,
std::string app_id_list, int size, int scale);
}
class WayfireToplevel::impl
{
zwlr_foreign_toplevel_handle_v1 *handle, *parent;
std::vector<zwlr_foreign_toplevel_handle_v1*> children;
uint32_t state;
Gtk::Button button;
Glib::RefPtr<Gio::SimpleActionGroup> actions;
Gtk::PopoverMenu popover;
Glib::RefPtr<Gio::Menu> menu;
Glib::RefPtr<Gio::MenuItem> minimize, maximize, close;
Glib::RefPtr<Gio::SimpleAction> minimize_action, maximize_action, close_action;
// Gtk::Box menu_box;
Gtk::Box button_contents;
Gtk::Image image;
Gtk::Label label;
// Gtk::PopoverMenu menu;
Glib::RefPtr<Gtk::GestureDrag> drag_gesture;
sigc::connection m_drag_timeout;
Glib::ustring app_id, title;
WfOption<bool> middle_click_close{"panel/middle_click_close"};
public:
WayfireWindowList *window_list;
impl(WayfireWindowList *window_list, zwlr_foreign_toplevel_handle_v1 *handle)
{
this->handle = handle;
this->parent = nullptr;
zwlr_foreign_toplevel_handle_v1_add_listener(handle,
&toplevel_handle_v1_impl, this);
button.get_style_context()->add_class("window-button");
button.get_style_context()->add_class("flat");
button.get_style_context()->remove_class("activated");
button_contents.append(image);
button_contents.append(label);
button_contents.set_halign(Gtk::Align::START);
button_contents.set_hexpand(true);
button_contents.set_spacing(5);
button.set_child(button_contents);
button.set_tooltip_text("none");
label.set_ellipsize(Pango::EllipsizeMode::END);
label.set_hexpand(true);
button.property_scale_factor().signal_changed()
.connect(sigc::mem_fun(*this, &WayfireToplevel::impl::on_scale_update));
actions = Gio::SimpleActionGroup::create();
close_action = Gio::SimpleAction::create("close");
minimize_action = Gio::SimpleAction::create_bool("minimize", false);
maximize_action = Gio::SimpleAction::create_bool("maximize", false);
close_action->signal_activate().connect(sigc::mem_fun(*this, &WayfireToplevel::impl::on_menu_close));
minimize_action->signal_change_state().connect(sigc::mem_fun(*this,
&WayfireToplevel::impl::on_menu_minimize));
maximize_action->signal_change_state().connect(sigc::mem_fun(*this,
&WayfireToplevel::impl::on_menu_maximize));
actions->add_action(close_action);
actions->add_action(minimize_action);
actions->add_action(maximize_action);
// Hey Kids, want to see a really stupid idea?
// Button can only have one child! But setting the parent of a popover still works fine...
gtk_widget_set_parent(GTK_WIDGET(popover.gobj()), GTK_WIDGET(button.gobj()));
popover.insert_action_group("windowaction", actions);
menu = Gio::Menu::create();
minimize = Gio::MenuItem::create("Minimize", "windowaction.minimize");
maximize = Gio::MenuItem::create("Maximize", "windowaction.maximize");
close = Gio::MenuItem::create("Close", "windowaction.close");
menu->append_item(minimize);
menu->append_item(maximize);
menu->append_item(close);
popover.set_menu_model(menu);
drag_gesture = Gtk::GestureDrag::create();
drag_gesture->signal_drag_begin().connect(
sigc::mem_fun(*this, &WayfireToplevel::impl::on_drag_begin));
drag_gesture->signal_drag_update().connect(
sigc::mem_fun(*this, &WayfireToplevel::impl::on_drag_update));
drag_gesture->signal_drag_end().connect(
sigc::mem_fun(*this, &WayfireToplevel::impl::on_drag_end));
button.add_controller(drag_gesture);
auto click_gesture = Gtk::GestureClick::create();
click_gesture->set_button(0);
click_gesture->signal_pressed().connect(
[=] (int count, double x, double y)
{
int butt = click_gesture->get_current_button();
if (butt == 3)
{
popover.popup();
click_gesture->set_state(Gtk::EventSequenceState::CLAIMED);
} else if (butt == 1)
{
// Don't action it now because the press might be a drag!
click_gesture->set_state(Gtk::EventSequenceState::CLAIMED);
} else if (butt = 2 && middle_click_close)
{
zwlr_foreign_toplevel_handle_v1_close(handle);
click_gesture->set_state(Gtk::EventSequenceState::CLAIMED);
}
});
click_gesture->signal_released().connect(
[=] (int count, double x, double y)
{
if (click_gesture->get_current_button() == 1)
{
// Ah, it was a press after all!
if (!ignore_next_click)
{
this->on_clicked();
}
ignore_next_click = false;
}
});
button.add_controller(click_gesture);
this->window_list = window_list;
send_rectangle_hints();
set_state(0); // will set the appropriate button style
}
int grab_off_x;
double grab_start_x, grab_start_y;
double grab_abs_start_x;
bool drag_exceeds_threshold;
bool drag_paused()
{
/*
* auto gseat = Gdk::Display::get_default()->get_default_seat()->get_wl_seat();
* //auto seat = gdk_wayland_seat_get_wl_seat(gseat->gobj());
* zwlr_foreign_toplevel_handle_v1_activate(handle, gseat);
*/
return false;
}
void on_drag_begin(double _x, double _y)
{
// Set grab start, before transforming it to absolute position
grab_start_x = _x;
grab_start_y = _y;
set_classes(state);
window_list->set_top_widget(&button);
// Find the distance between pointer X and button origin
int x = window_list->get_absolute_position(_x, button);
grab_abs_start_x = x;
// Find button corner in window-relative coords
int loc_x = window_list->get_absolute_position(0, button);
grab_off_x = x - loc_x;
drag_exceeds_threshold = false;
}
static constexpr int DRAG_THRESHOLD = 3;
void on_drag_update(double _x, double y)
{
/* Window was not just clicked, but also dragged. Ignore the next click,
* which is the one that happens when the drag gesture ends. */
set_ignore_next_click();
int x = _x + grab_start_x;
x = window_list->get_absolute_position(x, button);
if (std::abs(x - grab_abs_start_x) > DRAG_THRESHOLD)
{
drag_exceeds_threshold = true;
}
auto hovered_button = window_list->get_widget_at(x);
Gtk::Widget *before = window_list->get_widget_before(x);
if (hovered_button)
{
// Where are we in the button?
auto allocation = hovered_button->get_allocation();
int half_width = allocation.get_width() / 2;
int x_in_button = x - allocation.get_x();
if (x_in_button < half_width) // Left Half
{
if (before == nullptr)
{
gtk_box_reorder_child_after(window_list->gobj(), GTK_WIDGET(button.gobj()), nullptr);
} else
{
window_list->reorder_child_after(button, *before);
}
} else if (x_in_button > half_width) // Right Half
{
window_list->reorder_child_after(button, *hovered_button);
}
}
/* Make sure the grabbed button always stays at the same relative position
* to the DnD position */
int target_x = x - grab_off_x;
window_list->set_top_x(target_x);
}
void on_drag_end(double _x, double _y)
{
int x = _x + grab_start_x;
int y = _y + grab_start_y;
int width = button.get_allocated_width();
int height = button.get_allocated_height();
window_list->set_top_widget(nullptr);
set_classes(state);
/* When a button is dropped after dnd, we ignore the unclick
* event so action doesn't happen in addition to dropping.
* If the drag ends and the unclick event happens outside
* the button, unset ignore_next_click or else the next
* click on the button won't cause action. */
if ((x < 0) || (x > width) || (y < 0) || (y > height))
{
unset_ignore_next_click();
}
/* When dragging with touch or pen, we allow some small movement while
* still counting the action as button press as opposed to only dragging. */
if (!drag_exceeds_threshold)
{
unset_ignore_next_click();
this->on_clicked();
}
drag_gesture->set_state(Gtk::EventSequenceState::DENIED);
send_rectangle_hints();
}
void set_hide_text(bool hide_text)
{
if (hide_text)
{
label.hide();
} else
{
label.show();
}
}
void on_menu_minimize(Glib::VariantBase vb)
{
bool val = g_variant_get_boolean(vb.gobj());
send_rectangle_hint();
if (!val)
{
zwlr_foreign_toplevel_handle_v1_unset_minimized(handle);
return;
}
zwlr_foreign_toplevel_handle_v1_set_minimized(handle);
}
void on_menu_maximize(Glib::VariantBase vb)
{
bool val = g_variant_get_boolean(vb.gobj());
if (!val)
{
zwlr_foreign_toplevel_handle_v1_unset_maximized(handle);
return;
}
zwlr_foreign_toplevel_handle_v1_set_maximized(handle);
}
void on_menu_close(Glib::VariantBase vb)
{
zwlr_foreign_toplevel_handle_v1_close(handle);
}
bool ignore_next_click = false;
void set_ignore_next_click()
{
ignore_next_click = true;
/* Make sure that the view doesn't show clicked on animations while
* dragging (this happens only on some themes) */
button.set_state_flags(Gtk::StateFlags::SELECTED |
Gtk::StateFlags::DROP_ACTIVE | Gtk::StateFlags::PRELIGHT);
}
void unset_ignore_next_click()
{
ignore_next_click = false;
button.unset_state_flags(Gtk::StateFlags::SELECTED |
Gtk::StateFlags::DROP_ACTIVE | Gtk::StateFlags::PRELIGHT);
}
void on_clicked()
{
/* If the button was dragged, we don't want to register the click.
* Subsequent clicks should be handled though. */
if (ignore_next_click)
{
unset_ignore_next_click();
return;
}
bool child_activated = false;
for (auto c : get_children())
{
if (window_list->toplevels[c]->get_state() & WF_TOPLEVEL_STATE_ACTIVATED)
{
child_activated = true;
break;
}
}
if (!(state & WF_TOPLEVEL_STATE_ACTIVATED) && !child_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_scale_update()
{
set_app_id(app_id);
}
void set_app_id(std::string app_id)
{
WfOption<int> minimal_panel_height{"panel/minimal_height"};
this->app_id = app_id;
IconProvider::set_image_from_icon(image, app_id,
std::min(int(minimal_panel_height), 24), button.get_scale_factor());
}
void send_rectangle_hints()
{
for (const auto& toplevel_button : window_list->toplevels)
{
if (toplevel_button.second && toplevel_button.second->pimpl)
{
toplevel_button.second->pimpl->send_rectangle_hint();
}
}
}
void send_rectangle_hint()
{
auto panel = WayfirePanelApp::get().panel_for_wl_output(window_list->output->wo);
auto w = button.get_width();
auto h = button.get_height();
if (panel && (w > 0) && (h > 0))
{
double x, y;
button.translate_coordinates(panel->get_window(), 0, 0, x, y);
zwlr_foreign_toplevel_handle_v1_set_rectangle(handle, panel->get_wl_surface(),
x, y, w, h);
}
}
void set_title(std::string title)
{
this->title = title;
button.set_tooltip_text(title);
label.set_text(title);
}
uint32_t get_state()
{
return this->state;
}
zwlr_foreign_toplevel_handle_v1 *get_parent()
{
return this->parent;
}
void set_parent(zwlr_foreign_toplevel_handle_v1 *parent)
{
this->parent = parent;
}
std::vector<zwlr_foreign_toplevel_handle_v1*>& get_children()
{
return this->children;
}
void remove_button()
{
window_list->remove(button);
send_rectangle_hints();
}
void set_classes(uint32_t state)
{
if (state & WF_TOPLEVEL_STATE_ACTIVATED)
{
button.get_style_context()->add_class("activated");
button.get_style_context()->remove_class("flat");
} else
{
button.get_style_context()->add_class("flat");
button.get_style_context()->remove_class("activated");
}
if (state & WF_TOPLEVEL_STATE_MINIMIZED)
{
button.get_style_context()->add_class("minimized");
minimize_action->set_state(Glib::wrap(g_variant_new_boolean(true)));
} else
{
button.get_style_context()->remove_class("minimized");
minimize_action->set_state(Glib::wrap(g_variant_new_boolean(false)));
}
if (state & WF_TOPLEVEL_STATE_MAXIMIZED)
{
button.get_style_context()->add_class("maximized");
maximize_action->set_state(Glib::wrap(g_variant_new_boolean(true)));
} else
{
button.get_style_context()->remove_class("maximized");
maximize_action->set_state(Glib::wrap(g_variant_new_boolean(false)));
}
}
void set_state(uint32_t state)
{
this->state = state;
set_classes(state);
}
~impl()
{
gtk_widget_unparent(GTK_WIDGET(popover.gobj()));
if (m_drag_timeout)
{
m_drag_timeout.disconnect();
}
zwlr_foreign_toplevel_handle_v1_destroy(handle);
}
void handle_output_enter(wl_output *output)
{
if (this->parent)
{
return;
}
if (window_list->output->wo == output)
{
window_list->append(button);
send_rectangle_hints();
}
}
void handle_output_leave(wl_output *output)
{
if (window_list->output->wo == output)
{
window_list->remove(button);
send_rectangle_hints();
}
}
};
WayfireToplevel::WayfireToplevel(WayfireWindowList *window_list,
zwlr_foreign_toplevel_handle_v1 *handle) :
pimpl(new WayfireToplevel::impl(window_list, handle))
{}
std::vector<zwlr_foreign_toplevel_handle_v1*>& WayfireToplevel::get_children()
{
return pimpl->get_children();
}
uint32_t WayfireToplevel::get_state()
{
return pimpl->get_state();
}
void WayfireToplevel::send_rectangle_hint()
{
return pimpl->send_rectangle_hint();
}
WayfireToplevel::~WayfireToplevel() = default;
using toplevel_t = zwlr_foreign_toplevel_handle_v1*;
static void handle_toplevel_title(void *data, toplevel_t, const char *title)
{
auto impl = static_cast<WayfireToplevel::impl*>(data);
impl->set_title(title);
}
static void handle_toplevel_app_id(void *data, toplevel_t, const char *app_id)
{
auto impl = static_cast<WayfireToplevel::impl*>(data);
impl->set_app_id(app_id);
}
static void handle_toplevel_output_enter(void *data, toplevel_t, wl_output *output)
{
auto impl = static_cast<WayfireToplevel::impl*>(data);
impl->handle_output_enter(output);
}
static void handle_toplevel_output_leave(void *data, toplevel_t, wl_output *output)
{
auto impl = static_cast<WayfireToplevel::impl*>(data);
impl->handle_output_leave(output);
}
/* wl_array_for_each isn't supported in C++, so we have to manually
* get the data from wl_array, see:
*
* https://gitlab.freedesktop.org/wayland/wayland/issues/34 */
template<class T>
static void array_for_each(wl_array *array, std::function<void(T)> func)
{
assert(array->size % sizeof(T) == 0); // do not use malformed arrays
for (T *entry = (T*)array->data; (char*)entry < ((char*)array->data + array->size); entry++)
{
func(*entry);
}
}
static void handle_toplevel_state(void *data, toplevel_t, wl_array *state)
{
uint32_t flags = 0;
array_for_each<uint32_t>(state, [&flags] (uint32_t st)
{
if (st == ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_ACTIVATED)
{
flags |= WF_TOPLEVEL_STATE_ACTIVATED;
}
if (st == ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MAXIMIZED)
{
flags |= WF_TOPLEVEL_STATE_MAXIMIZED;
}
if (st == ZWLR_FOREIGN_TOPLEVEL_HANDLE_V1_STATE_MINIMIZED)
{
flags |= WF_TOPLEVEL_STATE_MINIMIZED;
}
});
auto impl = static_cast<WayfireToplevel::impl*>(data);
impl->set_state(flags);
}
static void handle_toplevel_done(void *data, toplevel_t)
{
// auto impl = static_cast<WayfireToplevel::impl*> (data);
}
static void remove_child_from_parent(WayfireToplevel::impl *impl, toplevel_t child)
{
auto parent = impl->get_parent();
auto& parent_toplevel = impl->window_list->toplevels[parent];
if (child && parent && parent_toplevel)
{
auto& children = parent_toplevel->get_children();
children.erase(std::find(children.begin(), children.end(), child));
}
}
static void handle_toplevel_closed(void *data, toplevel_t handle)
{
auto impl = static_cast<WayfireToplevel::impl*>(data);
impl->remove_button();
remove_child_from_parent(impl, handle);
impl->window_list->handle_toplevel_closed(handle);
}
static void handle_toplevel_parent(void *data, toplevel_t handle, toplevel_t parent)
{
auto impl = static_cast<WayfireToplevel::impl*>(data);
if (!parent)
{
if (impl->get_parent())
{
impl->handle_output_enter(impl->window_list->output->wo);
}
remove_child_from_parent(impl, handle);
impl->set_parent(parent);
return;
}
if (impl->window_list->toplevels[parent])
{
auto& children = impl->window_list->toplevels[parent]->get_children();
children.push_back(handle);
}
impl->set_parent(parent);
impl->remove_button();
}
namespace
{
struct zwlr_foreign_toplevel_handle_v1_listener toplevel_handle_v1_impl = {
.title = handle_toplevel_title,
.app_id = handle_toplevel_app_id,
.output_enter = handle_toplevel_output_enter,
.output_leave = handle_toplevel_output_leave,
.state = handle_toplevel_state,
.done = handle_toplevel_done,
.closed = handle_toplevel_closed,
.parent = handle_toplevel_parent
};
}
/* 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;
}
}
/* 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 = {
"",
"org.kde.",
};
std::vector<std::string> app_id_variations = {
app_id,
tolower(app_id),
};
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(prefix + id + suffix);
}
}
}
}
if (app_info) // success
{
return app_info->get_icon();
}
return Icon{};
}
/* Second method: Just look up the built-in icon theme,
* perhaps some icon can be found there */
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);
/* 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 */
auto display = image.get_display();
while (stream >> app_id)
{
auto icon = get_from_desktop_app_info(app_id);
std::string icon_name = "unknown";
if (!icon)
{
/* Perhaps no desktop app info, but we might still be able to
* get an icon directly from the icon theme */
if (Gtk::IconTheme::get_for_display(display)->lookup_icon(app_id, size))
{
icon_name = app_id;
}
} else
{
icon_name = icon->to_string();
}
image_set_icon(&image, icon_name);
/* finally found some icon */
if (icon_name != "unknown")
{
break;
}
}
}
}