-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathtoplevel.cpp
More file actions
1317 lines (1125 loc) · 38.4 KB
/
toplevel.cpp
File metadata and controls
1317 lines (1125 loc) · 38.4 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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <gtkmm.h>
#include <giomm/desktopappinfo.h>
#include <gdkmm/seat.h>
#include <gdk/wayland/gdkwayland.h>
#include <cmath>
#include <glibmm.h>
#include <cassert>
#include <sys/mman.h>
#include "toplevel.hpp"
#include "window-list.hpp"
#include "gtk-utils.hpp"
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);
}
static int create_anon_file(off_t size)
{
int fd = memfd_create("wf-live-preview", MFD_CLOEXEC);
if (fd == -1)
{
perror("memfd_create");
return 1;
}
if (ftruncate(fd, size) == -1)
{
perror("ftruncate");
close(fd);
return 1;
}
return fd;
}
#ifdef HAVE_DMABUF
static void dmabuf_created(void *data, struct zwp_linux_buffer_params_v1*,
struct wl_buffer *wl_buffer)
{
TooltipMedia *tooltip_media = (TooltipMedia*)data;
tooltip_media->buffer = wl_buffer;
zwlr_screencopy_frame_v1_copy(tooltip_media->frame, tooltip_media->buffer);
}
static void dmabuf_failed(void *data, struct zwp_linux_buffer_params_v1*)
{
TooltipMedia *tooltip_media = (TooltipMedia*)data;
std::cerr << "Failed to create dmabuf, trying shm" << std::endl;
tooltip_media->window_list->live_previews_dmabuf = false;
}
static const struct zwp_linux_buffer_params_v1_listener params_listener =
{
.created = dmabuf_created,
.failed = dmabuf_failed,
};
#endif // HAVE_DMABUF
void handle_frame_buffer(void *data,
struct zwlr_screencopy_frame_v1 *zwlr_screencopy_frame_v1,
uint32_t format,
uint32_t width,
uint32_t height,
uint32_t stride)
{
TooltipMedia *tooltip_media = (TooltipMedia*)data;
if (tooltip_media->window_list->live_previews_dmabuf)
{
tooltip_media->shm_data = nullptr;
return;
}
size_t size = width * height * int(stride / width);
if (tooltip_media->size != size)
{
if (tooltip_media->shm_data && tooltip_media->size)
{
if (munmap(tooltip_media->shm_data, tooltip_media->size) < 0)
{
perror("munmap failed");
}
}
tooltip_media->size = size;
auto anon_file = create_anon_file(size);
if (anon_file < 0)
{
perror("anon_file < 0");
return;
}
void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, anon_file, 0);
if (data == MAP_FAILED)
{
perror("data == MAP_FAILED");
close(anon_file);
return;
}
wl_shm_pool *pool = wl_shm_create_pool(tooltip_media->window_list->shm, anon_file, size);
tooltip_media->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, format);
wl_shm_pool_destroy(pool);
close(anon_file);
tooltip_media->buffer_width = width;
tooltip_media->buffer_height = height;
tooltip_media->buffer_stride = stride;
tooltip_media->shm_data = data;
}
zwlr_screencopy_frame_v1_copy(tooltip_media->frame, tooltip_media->buffer);
}
void handle_frame_flags(void*,
struct zwlr_screencopy_frame_v1*,
uint32_t)
{}
void handle_frame_ready(void *data,
struct zwlr_screencopy_frame_v1 *zwlr_screencopy_frame_v1,
uint32_t tv_sec_hi,
uint32_t tv_sec_lo,
uint32_t tv_nsec)
{
TooltipMedia *tooltip_media = (TooltipMedia*)data;
tooltip_media->request_next_frame();
#ifdef HAVE_DMABUF
if (tooltip_media->window_list->live_previews_dmabuf && tooltip_media->bo)
{
uint32_t stride = 0;
tooltip_media->map_data = nullptr;
tooltip_media->dmabuf_data = gbm_bo_map(tooltip_media->bo,
0, 0, tooltip_media->buffer_width, tooltip_media->buffer_height,
GBM_BO_TRANSFER_READ, &stride, &tooltip_media->map_data);
if (!tooltip_media->dmabuf_data)
{
perror("Failed to map bo");
std::cerr << "Trying shm" << std::endl;
tooltip_media->window_list->live_previews_dmabuf = false;
return;
}
if (tooltip_media->bo && tooltip_media->map_data)
{
gbm_bo_unmap(tooltip_media->bo, tooltip_media->map_data);
tooltip_media->map_data = nullptr;
}
tooltip_media->buffer_stride = stride;
tooltip_media->size = tooltip_media->buffer_height * tooltip_media->buffer_stride;
}
#endif // HAVE_DMABUF
if ((!tooltip_media->shm_data
#ifdef HAVE_DMABUF
&& !tooltip_media->dmabuf_data
#endif // HAVE_DMABUF
) || !tooltip_media->size)
{
return;
}
std::shared_ptr<Glib::Bytes> bytes = 0;
size_t size = tooltip_media->buffer_height * tooltip_media->buffer_stride;
if (tooltip_media->shm_data)
{
bytes = Glib::Bytes::create(tooltip_media->shm_data, size);
}
#ifdef HAVE_DMABUF
else if (tooltip_media->dmabuf_data)
{
bytes = Glib::Bytes::create(tooltip_media->dmabuf_data, size);
tooltip_media->dmabuf_data = nullptr;
}
#endif // HAVE_DMABUF
if (!bytes)
{
return;
}
auto builder = Gdk::MemoryTextureBuilder::create();
builder->set_bytes(bytes);
builder->set_width(tooltip_media->buffer_width);
builder->set_height(tooltip_media->buffer_height);
builder->set_stride(tooltip_media->buffer_stride);
builder->set_format(Gdk::MemoryFormat::R8G8B8A8);
auto texture = builder->build();
tooltip_media->set_paintable(texture);
}
void handle_frame_failed(void*, struct zwlr_screencopy_frame_v1*)
{}
void handle_frame_damage(void*,
struct zwlr_screencopy_frame_v1*,
uint32_t,
uint32_t,
uint32_t,
uint32_t)
{}
void handle_frame_linux_dmabuf(void *data,
struct zwlr_screencopy_frame_v1 *frame,
uint32_t format,
uint32_t width,
uint32_t height)
{
#ifdef HAVE_DMABUF
TooltipMedia *tooltip_media = (TooltipMedia*)data;
if (!tooltip_media->window_list->live_previews_dmabuf)
{
return;
}
tooltip_media->buffer_width = width;
tooltip_media->buffer_height = height;
if (!tooltip_media->buffer)
{
if (tooltip_media->bo)
{
if (tooltip_media->buffer)
{
wl_buffer_destroy(tooltip_media->buffer);
tooltip_media->buffer = nullptr;
}
if (tooltip_media->params)
{
zwp_linux_buffer_params_v1_destroy(tooltip_media->params);
tooltip_media->params = nullptr;
}
if (tooltip_media->bo)
{
gbm_bo_destroy(tooltip_media->bo);
tooltip_media->bo = nullptr;
}
}
const uint64_t modifier = 0; // DRM_FORMAT_MOD_LINEAR
tooltip_media->bo = gbm_bo_create_with_modifiers(tooltip_media->window_list->dmabuf_device,
tooltip_media->buffer_width,
tooltip_media->buffer_height, format, &modifier, 1);
if (!tooltip_media->bo)
{
tooltip_media->bo = gbm_bo_create(tooltip_media->window_list->dmabuf_device,
tooltip_media->buffer_width,
tooltip_media->buffer_height, format, GBM_BO_USE_LINEAR | GBM_BO_USE_RENDERING);
}
if (!tooltip_media->bo)
{
perror("Failed to create gbm bo");
std::cerr << "Trying shm" << std::endl;
tooltip_media->window_list->live_previews_dmabuf = false;
return;
}
tooltip_media->buffer_stride = gbm_bo_get_stride(tooltip_media->bo);
tooltip_media->params = zwp_linux_dmabuf_v1_create_params(tooltip_media->window_list->dmabuf);
uint64_t mod = gbm_bo_get_modifier(tooltip_media->bo);
zwp_linux_buffer_params_v1_add(tooltip_media->params,
gbm_bo_get_fd(tooltip_media->bo), 0,
gbm_bo_get_offset(tooltip_media->bo, 0),
gbm_bo_get_stride(tooltip_media->bo),
mod >> 32, mod & 0xffffffff);
zwp_linux_buffer_params_v1_add_listener(tooltip_media->params, ¶ms_listener, tooltip_media);
zwp_linux_buffer_params_v1_create(tooltip_media->params, tooltip_media->buffer_width,
tooltip_media->buffer_height, format, 0);
} else
{
zwlr_screencopy_frame_v1_copy(frame, tooltip_media->buffer);
}
#endif // HAVE_DMABUF
}
void handle_frame_buffer_done(void*, struct zwlr_screencopy_frame_v1*)
{}
static struct zwlr_screencopy_frame_v1_listener screencopy_frame_listener =
{
handle_frame_buffer,
handle_frame_flags,
handle_frame_ready,
handle_frame_failed,
handle_frame_damage,
handle_frame_linux_dmabuf,
handle_frame_buffer_done,
};
bool TooltipMedia::request_next_frame()
{
if (this->frame)
{
zwlr_screencopy_frame_v1_destroy(this->frame);
this->frame = nullptr;
}
if (!WayfireShellApp::get().live_preview_output)
{
return false;
}
this->frame = zwlr_screencopy_manager_v1_capture_output(this->window_list->screencopy_manager, 0,
WayfireShellApp::get().live_preview_output);
zwlr_screencopy_frame_v1_add_listener(this->frame, &screencopy_frame_listener, this);
return true;
}
TooltipMedia::TooltipMedia(WayfireWindowList *window_list)
{
this->window_list = window_list;
this->shm = window_list->shm;
this->add_tick_callback([=] (const Glib::RefPtr<Gdk::FrameClock>& clock)
{
return !this->request_next_frame();
});
}
TooltipMedia::~TooltipMedia()
{
if (this->frame)
{
zwlr_screencopy_frame_v1_destroy(this->frame);
}
if (
#ifdef HAVE_DMABUF
!this->window_list->live_previews_dmabuf &&
#endif // HAVE_DMABUF
this->shm_data && this->size)
{
if (munmap(this->shm_data, this->size) < 0)
{
perror("munmap failed");
}
}
if (this->buffer)
{
wl_buffer_destroy(this->buffer);
}
#ifdef HAVE_DMABUF
if (this->params)
{
zwp_linux_buffer_params_v1_destroy(this->params);
}
if (this->bo && this->map_data)
{
gbm_bo_unmap(this->bo, this->map_data);
}
if (this->bo)
{
gbm_bo_destroy(this->bo);
}
#endif // HAVE_DMABUF
}
class WayfireToplevel::impl
{
zwlr_foreign_toplevel_handle_v1 *handle, *parent;
std::vector<zwlr_foreign_toplevel_handle_v1*> children;
uint32_t state;
uint64_t view_id;
Gtk::Button button;
Gtk::Box custom_tooltip_content;
TooltipMedia *tooltip_media;
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;
std::vector<sigc::connection> signals;
sigc::connection button_leave_signal;
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->window_list = window_list;
this->handle = handle;
this->parent = nullptr;
zwlr_foreign_toplevel_handle_v1_add_listener(handle,
&toplevel_handle_v1_impl, this);
button.add_css_class("window-button");
button.add_css_class("flat");
button.remove_css_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);
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);
signals.push_back(close_action->signal_activate().connect(sigc::mem_fun(*this,
&WayfireToplevel::impl::on_menu_close)));
signals.push_back(minimize_action->signal_change_state().connect(sigc::mem_fun(*this,
&WayfireToplevel::impl::on_menu_minimize)));
signals.push_back(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();
signals.push_back(drag_gesture->signal_drag_begin().connect(
sigc::mem_fun(*this, &WayfireToplevel::impl::on_drag_begin)));
signals.push_back(drag_gesture->signal_drag_update().connect(
sigc::mem_fun(*this, &WayfireToplevel::impl::on_drag_update)));
signals.push_back(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();
auto long_press = Gtk::GestureLongPress::create();
long_press->set_touch_only(true);
signals.push_back(long_press->signal_pressed().connect(
[=] (double x, double y)
{
popover.popup();
long_press->set_state(Gtk::EventSequenceState::CLAIMED);
click_gesture->set_state(Gtk::EventSequenceState::DENIED);
}));
click_gesture->set_button(0);
signals.push_back(click_gesture->signal_pressed().connect(
[=] (int count, double x, double y)
{
click_gesture->set_state(Gtk::EventSequenceState::CLAIMED);
}));
signals.push_back(click_gesture->signal_released().connect(
[=] (int count, double x, double y)
{
int butt = click_gesture->get_current_button();
if ((butt == 2) && middle_click_close.value())
{
zwlr_foreign_toplevel_handle_v1_close(handle);
} else if (butt == 3)
{
popover.popup();
}
}));
button.add_controller(long_press);
button.add_controller(click_gesture);
auto motion_controller = Gtk::EventControllerMotion::create();
button_leave_signal = motion_controller->signal_leave().connect([=] ()
{
wf::json_t live_window_release_output_request;
live_window_release_output_request["method"] = "live_previews/release_output";
this->window_list->ipc_client->send(live_window_release_output_request.serialize(),
[=] (wf::json_t data)
{
unset_tooltip_media();
this->window_list->live_window_preview_view_id = 0;
if (data.serialize().find("error") != std::string::npos)
{
if (this->window_list->live_window_preview_tooltips)
{
std::cerr <<
"Error releasing output for live preview stream! (is live-previews plugin disabled?)"
<<
std::endl;
}
this->window_list->enable_normal_tooltips_flag(true);
return;
}
});
});
button.add_controller(motion_controller);
motion_controller = Gtk::EventControllerMotion::create();
signals.push_back(motion_controller->signal_enter().connect([=] (double x, double y)
{
wf::json_t live_window_preview_stream_request;
live_window_preview_stream_request["method"] = "live_previews/request_stream";
wf::json_t view_id_int;
view_id_int["id"] = this->view_id;
live_window_preview_stream_request["data"] = view_id_int;
this->window_list->ipc_client->send(live_window_preview_stream_request.serialize(),
[=] (wf::json_t data)
{
if ((data.serialize().find("error") != std::string::npos) &&
this->window_list->live_window_preview_tooltips)
{
std::cerr << data.serialize() << std::endl;
std::cerr <<
"Error acquiring live preview stream. (is live-previews wayfire plugin enabled?)" <<
std::endl;
this->window_list->enable_normal_tooltips_flag(true);
button.set_tooltip_text(title);
return;
}
set_tooltip_media();
update_tooltip();
});
}));
button.add_controller(motion_controller);
button.set_tooltip_text("none");
this->tooltip_media = nullptr;
signals.push_back(button.signal_query_tooltip().connect([=] (int x, int y, bool keyboard_mode,
const Glib::RefPtr<Gtk::Tooltip>
& tooltip)
{
return query_tooltip(x, y, keyboard_mode, tooltip);
}, false));
button.set_has_tooltip(true);
update_tooltip();
send_rectangle_hints();
set_state(0); // will set the appropriate button style
}
void set_tooltip_media()
{
if (this->tooltip_media)
{
return;
}
this->tooltip_media = Gtk::make_managed<TooltipMedia>(this->window_list);
this->custom_tooltip_content.append(*this->tooltip_media);
}
void unset_tooltip_media()
{
if (!this->tooltip_media)
{
return;
}
this->tooltip_media->unparent();
this->tooltip_media = nullptr;
}
void update_tooltip()
{
wf::json_t ipc_methods_request;
ipc_methods_request["method"] = "list-methods";
this->window_list->ipc_client->send(ipc_methods_request.serialize(), [=] (wf::json_t data)
{
if (data.serialize().find("error") != std::string::npos)
{
std::cerr << "Error getting ipc methods list!" << std::endl;
this->window_list->enable_normal_tooltips_flag(true);
return;
}
if ((data.serialize().find("live_previews/request_stream") == std::string::npos) ||
(data.serialize().find("live_previews/release_output") == std::string::npos))
{
unset_tooltip_media();
if (this->window_list->live_window_preview_tooltips)
{
if (this->window_list->live_window_previews_opt)
{
std::cout << "wf-shell configuration [panel] option 'live_window_previews' is set to 'true' but live-previews wayfire plugin is disabled." << std::endl;
this->window_list->enable_normal_tooltips_flag(true);
}
for (const auto& toplevel_button : this->window_list->toplevels)
{
if (toplevel_button.second && toplevel_button.second->pimpl)
{
toplevel_button.second->unset_tooltip_media();
}
}
}
} else
{
set_tooltip_media();
if (!this->window_list->live_window_preview_tooltips)
{
if (!this->window_list->live_window_previews_opt)
{
std::cout << "Detected live-previews plugin is enabled but wf-shell configuration [panel] option 'live_window_previews' is set to 'false'." << std::endl;
} else
{
std::cout << "Enabling live window preview tooltips." << std::endl;
}
this->window_list->enable_normal_tooltips_flag(false);
for (const auto& toplevel_button : this->window_list->toplevels)
{
if (toplevel_button.second && toplevel_button.second->pimpl)
{
toplevel_button.second->set_tooltip_media();
}
}
}
}
});
if (!this->window_list->live_window_previews_enabled())
{
this->window_list->normal_title_tooltips = true;
button.set_tooltip_text(title);
}
}
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)
{
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)
{
window_list->set_top_widget(nullptr);
set_classes(state);
if (!drag_exceeds_threshold)
{
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);
}
void on_clicked()
{
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);
}
bool query_tooltip(int x, int y, bool keyboard_mode, const Glib::RefPtr<Gtk::Tooltip>& tooltip)
{
if (this->popover.is_visible())
{
return false;
}
if (!this->window_list->live_window_previews_enabled())
{
if (!this->window_list->normal_title_tooltips)
{
std::cerr <<
"Normal title tooltips enabled. To enable live window preview tooltips, make sure to set [panel] option 'live_window_previews = true' in wf-shell configuration and enable wayfire plugin live-previews"
<<
std::endl;
this->window_list->enable_normal_tooltips_flag(true);
}
tooltip->set_text(title);
return true;
}
this->window_list->live_window_preview_view_id = this->view_id;
tooltip->set_custom(this->custom_tooltip_content);
return true;
}
uint64_t get_view_id_from_full_app_id(const std::string& app_id)
{
const std::string sub_str = "wf-ipc-";
size_t pos = app_id.find(sub_str);
if (pos != std::string::npos)
{
size_t suffix_start_index = pos + sub_str.length();
if (suffix_start_index < app_id.length())
{
try {
uint64_t view_id = std::stoi(app_id.substr(suffix_start_index, std::string::npos));
return view_id;
} catch (...)
{
return 0;
}
} else
{
return 0;
}
} else
{
return 0;
}
}
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());
this->view_id = get_view_id_from_full_app_id(app_id);
if (this->view_id == 0)
{
std::cerr << "Failed to get view id from app_id. " <<
"(Ensure 'app_id_mode' set to 'full' in wayfire " <<
"[workarounds] and restart wf-panel or the applications " <<
"in the window list)" << std::endl;
}
}
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;
if (!this->window_list->live_window_previews_enabled())
{
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()
{
button_leave_signal.disconnect();
window_list->remove(button);
send_rectangle_hints();
}
void set_classes(uint32_t state)
{
if (state & WF_TOPLEVEL_STATE_ACTIVATED)
{
button.add_css_class("activated");
button.remove_css_class("flat");
} else
{
button.add_css_class("flat");
button.remove_css_class("activated");
}
if (state & WF_TOPLEVEL_STATE_MINIMIZED)
{
button.add_css_class("minimized");
minimize_action->set_state(Glib::wrap(g_variant_new_boolean(true)));
} else
{
button.remove_css_class("minimized");
minimize_action->set_state(Glib::wrap(g_variant_new_boolean(false)));
}
if (state & WF_TOPLEVEL_STATE_MAXIMIZED)