-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathscale.cpp
1536 lines (1312 loc) · 46.4 KB
/
scale.cpp
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
/**
* Original code by: Scott Moreau, Daniel Kondor
*/
#include <map>
#include <memory>
#include <wayfire/workarea.hpp>
#include <wayfire/seat.hpp>
#include <wayfire/per-output-plugin.hpp>
#include <wayfire/output.hpp>
#include <wayfire/util/duration.hpp>
#include <wayfire/view-transform.hpp>
#include <wayfire/render-manager.hpp>
#include <wayfire/workspace-set.hpp>
#include <wayfire/signal-definitions.hpp>
#include <wayfire/plugins/vswitch.hpp>
#include <wayfire/touch/touch.hpp>
#include <wayfire/plugins/scale-signal.hpp>
#include <wayfire/plugins/wobbly/wobbly-signal.hpp>
#include <wayfire/window-manager.hpp>
#include <wayfire/plugins/common/move-drag-interface.hpp>
#include <wayfire/plugins/common/shared-core-data.hpp>
#include <wayfire/plugins/common/input-grab.hpp>
#include <linux/input-event-codes.h>
#include "plugins/ipc/ipc-activator.hpp"
#include "scale.hpp"
#include "scale-title-overlay.hpp"
#include "wayfire/core.hpp"
#include "wayfire/debug.hpp"
#include "wayfire/plugin.hpp"
#include "wayfire/plugins/common/util.hpp"
#include "wayfire/scene-input.hpp"
#include "wayfire/scene.hpp"
#include "wayfire/signal-provider.hpp"
#include "wayfire/toplevel-view.hpp"
#include "wayfire/view.hpp"
using namespace wf::animation;
class scale_animation_t : public duration_t
{
public:
using duration_t::duration_t;
timed_transition_t scale_x{*this};
timed_transition_t scale_y{*this};
timed_transition_t translation_x{*this};
timed_transition_t translation_y{*this};
};
struct wf_scale_animation_attribs
{
wf::option_wrapper_t<wf::animation_description_t> duration{"scale/duration"};
scale_animation_t scale_animation{duration};
};
struct view_scale_data
{
int row, col;
std::shared_ptr<wf::scene::view_2d_transformer_t> transformer;
wf::animation::simple_animation_t fade_animation;
wf_scale_animation_attribs animation;
enum class view_visibility_t
{
VISIBLE, /* view is shown in position determined by layout_slots() */
HIDING, /* view is in the process of hiding (due to filters) */
HIDDEN, /* view is hidden by a filter (with set_visible(false)) */
};
view_visibility_t visibility = view_visibility_t::VISIBLE;
bool was_minimized = false; /* flag to indicate if this view was originally minimized */
};
/**
* Scale has the following hard coded bindings are as follows:
* KEY_ENTER:
* - Ends scale, switching to the workspace of the focused view
* KEY_ESC:
* - Ends scale, switching to the workspace where scale was started,
* and focuses the initially active view
* KEY_UP:
* KEY_DOWN:
* KEY_LEFT:
* KEY_RIGHT:
* - When scale is active, change focus of the views
*
* BTN_LEFT:
* - Ends scale, switching to the workspace of the surface clicked
* BTN_MIDDLE:
* - If middle_click_close is true, closes the view clicked
*/
class wayfire_scale : public wf::per_output_plugin_instance_t,
public wf::keyboard_interaction_t,
public wf::pointer_interaction_t,
public wf::touch_interaction_t
{
/* helper class for optionally showing title overlays */
scale_show_title_t show_title;
std::vector<int> current_row_sizes;
wf::point_t initial_workspace;
bool hook_set;
/* View that was active before scale began. */
std::weak_ptr<wf::view_interface_t> initial_focus_view;
/* View that has active focus. */
wayfire_toplevel_view current_focus_view;
// View over which the last input press happened
wayfire_toplevel_view last_selected_view;
std::map<wayfire_toplevel_view, view_scale_data> scale_data;
wf::option_wrapper_t<int> spacing{"scale/spacing"};
wf::option_wrapper_t<int> outer_margin{"scale/outer_margin"};
wf::option_wrapper_t<bool> middle_click_close{"scale/middle_click_close"};
wf::option_wrapper_t<double> inactive_alpha{"scale/inactive_alpha"};
wf::option_wrapper_t<double> minimized_alpha{"scale/minimized_alpha"};
wf::option_wrapper_t<bool> allow_scale_zoom{"scale/allow_zoom"};
wf::option_wrapper_t<bool> include_minimized{"scale/include_minimized"};
wf::option_wrapper_t<bool> close_on_new_view{"scale/close_on_new_view"};
/* maximum scale -- 1.0 means we will not "zoom in" on a view */
const double max_scale_factor = 1.0;
/* maximum scale for child views (relative to their parents)
* zero means unconstrained, 1.0 means child cannot be scaled
* "larger" than the parent */
const double max_scale_child = 1.0;
/* true if the currently running scale should include views from
* all workspaces */
bool all_workspaces;
std::unique_ptr<wf::vswitch::control_bindings_t> workspace_bindings;
wf::shared_data::ref_ptr_t<wf::move_drag::core_drag_t> drag_helper;
std::unique_ptr<wf::input_grab_t> grab;
wf::plugin_activation_data_t grab_interface{
.name = "scale",
.capabilities = wf::CAPABILITY_MANAGE_DESKTOP | wf::CAPABILITY_GRAB_INPUT,
.cancel = [=] () { finalize(); },
};
public:
bool active = false;
void init() override
{
hook_set = false;
grab = std::make_unique<wf::input_grab_t>("scale", output, this, this, this);
allow_scale_zoom.set_callback(allow_scale_zoom_option_changed);
setup_workspace_switching();
drag_helper->connect(&on_drag_output_focus);
drag_helper->connect(&on_drag_done);
drag_helper->connect(&on_drag_snap_off);
show_title.init(output);
output->connect(&update_cb);
}
void setup_workspace_switching()
{
workspace_bindings = std::make_unique<wf::vswitch::control_bindings_t>(output);
workspace_bindings->setup([&] (wf::point_t delta, wayfire_toplevel_view view, bool only_view)
{
if (!output->is_plugin_active(grab_interface.name))
{
return false;
}
if (delta == wf::point_t{0, 0})
{
// Consume input event
return true;
}
if (only_view)
{
// For now, scale does not let you move views between workspaces
return false;
}
auto ws = output->wset()->get_current_workspace() + delta;
// vswitch picks the top view, we want the focused one
std::vector<wayfire_toplevel_view> fixed_views;
if (view && current_focus_view && !all_workspaces)
{
fixed_views.push_back(current_focus_view);
}
output->wset()->request_workspace(ws, fixed_views);
return true;
});
}
/* Add a transformer that will be used to scale the view */
bool add_transformer(wayfire_toplevel_view view)
{
if (view->get_transformed_node()->get_transformer("scale"))
{
return false;
}
auto tr = std::make_shared<wf::scene::view_2d_transformer_t>(view);
scale_data[view].transformer = tr;
view->get_transformed_node()->add_transformer(tr, wf::TRANSFORMER_2D,
"scale");
/* Handle potentially minimized views by making them visible,
* however, they start out as fully transparent. */
if (view->minimized)
{
tr->alpha = 0.0;
wf::scene::set_node_enabled(view->get_root_node(), true);
scale_data[view].was_minimized = true;
}
/* Transformers are added only once when scale is activated so
* this is a good place to connect the geometry-changed handler */
view->connect(&view_geometry_changed);
view->connect(&view_unmapped);
set_tiled_wobbly(view, true);
/* signal that a transformer was added to this view */
scale_transformer_added_signal data;
data.view = view;
output->emit(&data);
return true;
}
/* Remove the scale transformer from the view */
void pop_transformer(wayfire_toplevel_view view)
{
/* signal that a transformer was added to this view */
scale_transformer_removed_signal data;
data.view = view;
output->emit(&data);
view->get_transformed_node()->rem_transformer("scale");
view->disconnect(&view_unmapped);
set_tiled_wobbly(view, false);
}
/* Remove scale transformers from all views */
void remove_transformers()
{
for (auto& e : scale_data)
{
for (auto& toplevel : e.first->enumerate_views(false))
{
pop_transformer(toplevel);
}
if (e.second.was_minimized)
{
wf::scene::set_node_enabled(e.first->get_root_node(), false);
}
if (e.second.visibility == view_scale_data::view_visibility_t::HIDDEN)
{
wf::scene::set_node_enabled(e.first->get_transformed_node(), true);
}
e.second.visibility = view_scale_data::view_visibility_t::VISIBLE;
}
}
/* Check whether views exist on other workspaces */
bool all_same_as_current_workspace_views()
{
return get_all_workspace_views().size() ==
get_current_workspace_views().size();
}
/* Activate scale, switch activator modes and deactivate */
bool handle_toggle(bool want_all_workspaces)
{
if (active && (all_same_as_current_workspace_views() ||
(want_all_workspaces == this->all_workspaces)))
{
deactivate();
return true;
}
this->all_workspaces = want_all_workspaces;
if (active)
{
switch_scale_modes();
return true;
} else
{
return activate();
}
}
wf::signal::connection_t<scale_update_signal> update_cb = [=] (scale_update_signal *ev)
{
if (active)
{
layout_slots(get_views());
output->render->schedule_redraw();
}
};
void handle_pointer_button(
const wlr_pointer_button_event& event) override
{
process_input(event.button, event.state,
wf::get_core().get_cursor_position());
}
void handle_touch_down(uint32_t, int finger_id, wf::pointf_t pos) override
{
if (finger_id == 0)
{
process_input(BTN_LEFT, WLR_BUTTON_PRESSED, pos);
}
}
void handle_touch_up(uint32_t, int finger_id,
wf::pointf_t lift_off_position) override
{
if (finger_id == 0)
{
process_input(BTN_LEFT, WLR_BUTTON_RELEASED, lift_off_position);
}
}
void handle_touch_motion(uint32_t time, int finger_id,
wf::pointf_t position) override
{
if (finger_id == 0)
{
handle_pointer_motion(position, time);
}
}
/* Fade all views' alpha to inactive alpha except the
* view argument */
void fade_out_all_except(wayfire_toplevel_view view)
{
for (auto& e : scale_data)
{
auto v = e.first;
if (wf::find_topmost_parent(v) == wf::find_topmost_parent(view))
{
continue;
}
if (e.second.visibility != view_scale_data::view_visibility_t::VISIBLE)
{
continue;
}
fade_out(v);
}
}
/* Fade in view alpha */
void fade_in(wayfire_toplevel_view view)
{
if (!view || !scale_data.count(view))
{
return;
}
set_hook();
auto alpha = scale_data[view].transformer->alpha;
scale_data[view].fade_animation.animate(alpha, 1);
if (view->children.size())
{
fade_in(view->children.front());
}
}
/* Fade out view alpha */
void fade_out(wayfire_toplevel_view view)
{
if (!view)
{
return;
}
set_hook();
for (auto v : view->enumerate_views(false))
{
// Could happen if we have a never-mapped child view
if (!scale_data.count(v))
{
continue;
}
auto alpha = scale_data[v].transformer->alpha;
double target_alpha = (v->minimized) ? minimized_alpha : inactive_alpha;
scale_data[v].fade_animation.animate(alpha, target_alpha);
}
}
/* Switch to the workspace for the untransformed view geometry */
void select_view(wayfire_toplevel_view view)
{
if (!view)
{
return;
}
auto ws = get_view_main_workspace(view);
output->wset()->request_workspace(ws);
}
/* Updates current and initial view focus variables accordingly */
void check_focus_view(wayfire_toplevel_view view)
{
if (view == current_focus_view)
{
current_focus_view = nullptr;
}
if (view == last_selected_view)
{
last_selected_view = nullptr;
}
}
/* Remove transformer from view and remove view from the scale_data map */
void remove_view(wayfire_toplevel_view view)
{
if (!view || !scale_data.count(view))
{
return;
}
if (scale_data.at(view).was_minimized)
{
wf::scene::set_node_enabled(view->get_root_node(), false);
}
for (auto v : view->enumerate_views(false))
{
check_focus_view(v);
pop_transformer(v);
scale_data.erase(v);
}
}
/* Process button event */
void process_input(uint32_t button, uint32_t state, wf::pointf_t input_position)
{
if (!active)
{
return;
}
if (state == WLR_BUTTON_PRESSED)
{
auto view = scale_find_view_at(input_position, output);
if (view && should_scale_view(view))
{
// Mark the view as the target of the next input release operation
last_selected_view = view;
} else
{
last_selected_view = nullptr;
}
drag_helper->set_pending_drag(input_position);
return;
}
drag_helper->handle_input_released();
auto view = scale_find_view_at(input_position, output);
if (!view || (last_selected_view != view))
{
last_selected_view = nullptr;
// Operation was cancelled, for ex. dragged outside of the view
return;
}
// Reset last_selected_view, because it is no longer held
last_selected_view = nullptr;
switch (button)
{
case BTN_LEFT:
// Focus the view under the mouse
current_focus_view = view;
fade_out_all_except(view);
fade_in(wf::find_topmost_parent(view));
// End scale
initial_focus_view.reset();
deactivate();
break;
case BTN_MIDDLE:
// Check kill the view
if (middle_click_close)
{
view->close();
}
break;
default:
break;
}
}
void handle_pointer_motion(wf::pointf_t to_f, uint32_t time) override
{
wf::point_t to{(int)std::round(to_f.x), (int)std::round(to_f.y)};
if (!drag_helper->view && last_selected_view && drag_helper->should_start_pending_drag(to))
{
wf::move_drag::drag_options_t opts;
opts.join_views = true;
opts.enable_snap_off = true;
opts.snap_off_threshold = 200;
// We want to receive raw inputs (e.g. no fake pointer releases) in case the view is moved to
// another output.
grab->set_wants_raw_input(true);
drag_helper->start_drag(last_selected_view, opts);
drag_helper->handle_motion(to);
} else if (drag_helper->view)
{
drag_helper->handle_motion(to);
if (last_selected_view)
{
const double threshold = 20.0;
if (drag_helper->distance_to_grab_origin(to) > threshold)
{
last_selected_view = nullptr;
}
}
}
}
/* Get the workspace for the center point of the untransformed view geometry */
wf::point_t get_view_main_workspace(wayfire_toplevel_view view)
{
view = wf::find_topmost_parent(view);
auto ws = output->wset()->get_current_workspace();
auto og = output->get_layout_geometry();
auto vg = view->get_geometry();
auto center = wf::point_t{vg.x + vg.width / 2, vg.y + vg.height / 2};
return wf::point_t{
ws.x + (int)std::floor((double)center.x / og.width),
ws.y + (int)std::floor((double)center.y / og.height)};
}
/* Given row and column, return a view at this position in the scale grid,
* or the first scaled view if none is found */
wayfire_toplevel_view find_view_in_grid(int row, int col)
{
for (auto& view : scale_data)
{
if ((view.first->parent == nullptr) &&
(view.second.visibility ==
view_scale_data::view_visibility_t::VISIBLE) &&
((view.second.row == row) &&
(view.second.col == col)))
{
return view.first;
}
}
return get_views().front();
}
/* Process key event */
void handle_keyboard_key(wf::seat_t*, wlr_keyboard_key_event ev) override
{
auto view = toplevel_cast(wf::get_active_view_for_output(output));
if (!view)
{
view = current_focus_view;
if (view)
{
fade_out_all_except(view);
fade_in(view);
wf::get_core().default_wm->focus_raise_view(view);
return;
}
} else if (!scale_data.count(view))
{
return;
}
int cur_row = view ? scale_data[view].row : 0;
int cur_col = view ? scale_data[view].col : 0;
int next_row = cur_row;
int next_col = cur_col;
if ((ev.state != WLR_KEY_PRESSED) ||
wf::get_core().seat->get_keyboard_modifiers())
{
return;
}
switch (ev.keycode)
{
case KEY_UP:
next_row--;
break;
case KEY_DOWN:
next_row++;
break;
case KEY_LEFT:
next_col--;
break;
case KEY_RIGHT:
next_col++;
break;
case KEY_ENTER:
deactivate();
select_view(current_focus_view);
wf::get_core().default_wm->focus_raise_view(view);
return;
case KEY_ESC:
deactivate();
output->wset()->request_workspace(initial_workspace);
if (auto focus = initial_focus_view.lock())
{
wf::get_core().default_wm->focus_raise_view(focus);
}
initial_focus_view.reset();
return;
default:
return;
}
if (!view)
{
return;
}
if (!current_row_sizes.empty())
{
next_row = (next_row + current_row_sizes.size()) %
current_row_sizes.size();
if (cur_row != next_row)
{
/* when moving to and from the last row, the number of columns
* may be different, so this bit figures out which view we
* should switch focus to */
float p = 1.0 * cur_col / current_row_sizes[cur_row];
next_col = p * current_row_sizes[next_row];
} else
{
next_col = (next_col + current_row_sizes[cur_row]) %
current_row_sizes[cur_row];
}
} else
{
next_row = cur_row;
next_col = cur_col;
}
view = find_view_in_grid(next_row, next_col);
if (view && (current_focus_view != view))
{
fade_out_all_except(view);
fade_in(view);
current_focus_view = view;
// Update activated state
wf::get_core().seat->focus_view(view);
}
}
/* Assign the transformer values to the view transformers */
void transform_views()
{
for (auto& e : scale_data)
{
auto view = e.first;
auto& view_data = e.second;
if (!view || !view_data.transformer)
{
continue;
}
if (view_data.fade_animation.running() ||
view_data.animation.scale_animation.running())
{
view->get_transformed_node()->begin_transform_update();
view_data.transformer->scale_x =
view_data.animation.scale_animation.scale_x;
view_data.transformer->scale_y =
view_data.animation.scale_animation.scale_y;
view_data.transformer->translation_x =
view_data.animation.scale_animation.translation_x;
view_data.transformer->translation_y =
view_data.animation.scale_animation.translation_y;
view_data.transformer->alpha = view_data.fade_animation;
if ((view_data.visibility ==
view_scale_data::view_visibility_t::HIDING) &&
!view_data.fade_animation.running())
{
view_data.visibility =
view_scale_data::view_visibility_t::HIDDEN;
wf::scene::set_node_enabled(view->get_transformed_node(), false);
}
view->get_transformed_node()->end_transform_update();
}
}
}
/* Returns a list of views for all workspaces */
std::vector<wayfire_toplevel_view> get_all_workspace_views()
{
return output->wset()->get_views(
(include_minimized ? 0 : wf::WSET_EXCLUDE_MINIMIZED) | wf::WSET_MAPPED_ONLY);
}
/* Returns a list of views for the current workspace */
std::vector<wayfire_toplevel_view> get_current_workspace_views()
{
std::vector<wayfire_toplevel_view> views;
for (auto& view : get_all_workspace_views())
{
auto vg = view->get_geometry();
auto og = output->get_relative_geometry();
wf::region_t wr{og};
wf::point_t center{vg.x + vg.width / 2, vg.y + vg.height / 2};
if (wr.contains_point(center))
{
views.push_back(view);
}
}
return views;
}
/* Returns a list of views to be scaled */
std::vector<wayfire_toplevel_view> get_views()
{
std::vector<wayfire_toplevel_view> views;
if (all_workspaces)
{
views = get_all_workspace_views();
} else
{
views = get_current_workspace_views();
}
return views;
}
/**
* @return true if the view is to be scaled.
*/
bool should_scale_view(wayfire_toplevel_view view)
{
auto views = get_views();
return std::find(
views.begin(), views.end(), wf::find_topmost_parent(view)) != views.end();
}
/* Convenience assignment function */
void setup_view_transform(view_scale_data& view_data,
double scale_x,
double scale_y,
double translation_x,
double translation_y,
double target_alpha)
{
view_data.animation.scale_animation.scale_x.set(
view_data.transformer->scale_x, scale_x);
view_data.animation.scale_animation.scale_y.set(
view_data.transformer->scale_y, scale_y);
view_data.animation.scale_animation.translation_x.set(
view_data.transformer->translation_x, translation_x);
view_data.animation.scale_animation.translation_y.set(
view_data.transformer->translation_y, translation_y);
view_data.animation.scale_animation.start();
view_data.fade_animation = wf::animation::simple_animation_t(
wf::option_wrapper_t<wf::animation_description_t>{"scale/duration"});
view_data.fade_animation.animate(view_data.transformer->alpha,
target_alpha);
}
static bool view_compare_x(const wayfire_toplevel_view& a, const wayfire_toplevel_view& b)
{
auto vg_a = a->get_geometry();
std::vector<int> a_coords = {vg_a.x, vg_a.width, vg_a.y, vg_a.height};
auto vg_b = b->get_geometry();
std::vector<int> b_coords = {vg_b.x, vg_b.width, vg_b.y, vg_b.height};
return a_coords < b_coords;
}
static bool view_compare_y(const wayfire_toplevel_view& a, const wayfire_toplevel_view& b)
{
auto vg_a = a->get_geometry();
std::vector<int> a_coords = {vg_a.y, vg_a.height, vg_a.x, vg_a.width};
auto vg_b = b->get_geometry();
std::vector<int> b_coords = {vg_b.y, vg_b.height, vg_b.x, vg_b.width};
return a_coords < b_coords;
}
std::vector<std::vector<wayfire_toplevel_view>> view_sort(
std::vector<wayfire_toplevel_view>& views)
{
std::vector<std::vector<wayfire_toplevel_view>> view_grid;
// First ensure a consistent sorting of all views using a persistent
// identifier before sorting by geometry.
// This is so that if two views have exactly the same geometry,
// they will always appear in the same order in the output list.
std::sort(views.begin(), views.end(), [] (auto a, auto b)
{
return a.get() < b.get();
});
std::stable_sort(views.begin(), views.end(), view_compare_y);
int rows = sqrt(views.size() + 1);
int views_per_row = (int)std::ceil((double)views.size() / rows);
size_t n = views.size();
for (size_t i = 0; i < n; i += views_per_row)
{
size_t j = std::min(i + views_per_row, n);
view_grid.emplace_back(views.begin() + i, views.begin() + j);
std::stable_sort(view_grid.back().begin(), view_grid.back().end(),
view_compare_x);
}
return view_grid;
}
/* Filter the views to be arranged by layout_slots() */
void filter_views(std::vector<wayfire_toplevel_view>& views)
{
std::vector<wayfire_toplevel_view> filtered_views;
scale_filter_signal signal(views, filtered_views);
output->emit(&signal);
/* update hidden views -- ensure that they and their children have a
* transformer and are in scale_data */
for (auto view : filtered_views)
{
for (auto v : view->enumerate_views(true))
{
add_transformer(v);
auto& view_data = scale_data[v];
if (view_data.visibility ==
view_scale_data::view_visibility_t::VISIBLE)
{
view_data.visibility =
view_scale_data::view_visibility_t::HIDING;
setup_view_transform(view_data, 1, 1, 0, 0, 0);
}
if (v == current_focus_view)
{
current_focus_view = nullptr;
}
}
}
if (!current_focus_view)
{
std::sort(views.begin(), views.end(), [=] (wayfire_toplevel_view a, wayfire_toplevel_view b)
{
if (a->minimized != b->minimized)
{
/* avoid focusing minimized views if possible, so
* sort them after non-minimized ones */
return b->minimized;
}
return wf::get_focus_timestamp(a) > wf::get_focus_timestamp(b);
});
current_focus_view = views.empty() ? nullptr : views.front();
wf::get_core().default_wm->focus_raise_view(current_focus_view);
}
}
/* Compute target scale layout geometry for all the view transformers
* and start animating. Initial code borrowed from the compiz scale
* plugin algorithm */
void layout_slots(std::vector<wayfire_toplevel_view> views)
{
wf::dassert(active || hook_set, "Scale is not active");
if (!views.size())
{
if (!all_workspaces && active)
{
deactivate();
}
return;
}
filter_views(views);
auto workarea = output->workarea->get_workarea();
workarea.x += outer_margin;
workarea.y += outer_margin;
workarea.width -= outer_margin * 2;
workarea.height -= outer_margin * 2;
auto sorted_rows = view_sort(views);
size_t cnt_rows = sorted_rows.size();
const double scaled_height = std::max((double)
(workarea.height - (cnt_rows + 1) * spacing) / cnt_rows, 1.0);
current_row_sizes.clear();
for (size_t i = 0; i < cnt_rows; i++)
{
size_t cnt_cols = sorted_rows[i].size();
current_row_sizes.push_back(cnt_cols);
const double scaled_width = std::max((double)
(workarea.width - (cnt_cols + 1) * spacing) / cnt_cols, 1.0);
for (size_t j = 0; j < cnt_cols; j++)
{
double x = workarea.x + spacing + (spacing + scaled_width) * j;
double y = workarea.y + spacing + (spacing + scaled_height) * i;
auto view = sorted_rows[i][j];
// Calculate current transformation of the view, in order to
// ensure that new views in the view tree start directly at the
// correct position
double main_view_dx = 0;
double main_view_dy = 0;
double main_view_scale = 1.0;
if (scale_data.count(view))
{
main_view_dx = scale_data[view].transformer->translation_x;
main_view_dy = scale_data[view].transformer->translation_y;
main_view_scale = scale_data[view].transformer->scale_x;
}
// Calculate target alpha for this view and its children
double target_alpha = 1.0;
if (view != current_focus_view)
{
target_alpha = (view->minimized) ? minimized_alpha : inactive_alpha;
}
// Helper function to calculate the desired scale for a view
const auto& calculate_scale = [=] (wf::dimensions_t vg)
{
double w = std::max(1.0, scaled_width);
double h = std::max(1.0, scaled_height);
const double scale = std::min(w / vg.width, h / vg.height);
if (!allow_scale_zoom)
{
return std::min(scale, max_scale_factor);
}
return scale;
};
add_transformer(view);
auto geom = view->get_geometry();
double view_scale = calculate_scale({geom.width, geom.height});
for (auto& child : view->enumerate_views(true))
{
// Ensure a transformer for the view, and make sure that
// new views in the view tree start off with the correct
// attributes set.
auto new_child = add_transformer(child);
auto& child_data = scale_data[child];
if (new_child)
{
child_data.transformer->translation_x = main_view_dx;
child_data.transformer->translation_y = main_view_dy;
child_data.transformer->scale_x = main_view_scale;
child_data.transformer->scale_y = main_view_scale;
}
if (child_data.visibility ==
view_scale_data::view_visibility_t::HIDDEN)
{
wf::scene::set_node_enabled(
child->get_transformed_node(), true);
}