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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
|
/**
* 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 "wayfire/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"
static constexpr const char *SCALE_TRANSFORMER = "scale";
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_TRANSFORMER,
.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_TRANSFORMER, 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_TRANSFORMER))
{
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 + 1,
SCALE_TRANSFORMER);
/* 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_TRANSFORMER);
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);
}
child_data.visibility =
view_scale_data::view_visibility_t::VISIBLE;
child_data.row = i;
child_data.col = j;
if (!active)
{
// On exit, we just animate towards normal state
setup_view_transform(child_data, 1, 1, 0, 0, 1);
continue;
}
auto vg = child->get_geometry();
wf::pointf_t center = {vg.x + vg.width / 2.0, vg.y + vg.height / 2.0};
// Take padding into account
double scale = calculate_scale({vg.width, vg.height});
// Ensure child is not scaled more than parent
if (!allow_scale_zoom &&
(child != view) &&
(max_scale_child > 0.0))
{
scale = std::min(max_scale_child * view_scale, scale);
}
// Target geometry is centered around the center slot
const double dx = x - center.x + scaled_width / 2.0;
const double dy = y - center.y + scaled_height / 2.0;
setup_view_transform(child_data, scale, scale,
dx, dy, target_alpha);
}
}
}
set_hook();
transform_views();
}
/* Called when adding or removing a group of views to be scaled,
* in this case between views on all workspaces and views on the
* current workspace */
void switch_scale_modes()
{
if (!output->is_plugin_active(grab_interface.name))
{
return;
}
if (all_workspaces)
{
layout_slots(get_views());
return;
}
bool rearrange = false;
for (auto& e : scale_data)
{
if (!should_scale_view(e.first))
{
setup_view_transform(e.second, 1, 1, 0, 0, 1);
rearrange = true;
}
}
if (rearrange)
{
layout_slots(get_views());
}
}
/* Toggle between restricting maximum scale to 100% or allowing it
* to become the greater. This is particularly noticeable when
* scaling a single view or a view with child views. */
wf::config::option_base_t::updated_callback_t allow_scale_zoom_option_changed =
[=] ()
{
if (!output->is_plugin_active(grab_interface.name))
{
return;
}
layout_slots(get_views());
};
void handle_new_view(wayfire_toplevel_view view, bool close_scale)
{
if (!should_scale_view(view))
{
return;
}
if (close_scale)
{
deactivate();
return;
}
layout_slots(get_views());
}
wf::signal::connection_t<wf::view_mapped_signal> on_view_mapped = [=] (wf::view_mapped_signal *ev)
{
if (auto toplevel = wf::toplevel_cast(ev->view))
{
handle_new_view(toplevel, close_on_new_view);
}
};
void handle_view_unmapped(wayfire_toplevel_view view)
{
remove_view(view);
if (scale_data.empty())
{
finalize();
} else if (!view->parent)
{
layout_slots(get_views());
}
}
/* Workspace changed */
wf::signal::connection_t<wf::workspace_changed_signal> workspace_changed =
[=] (wf::workspace_changed_signal *ev)
{
if (current_focus_view)
{
wf::get_core().default_wm->focus_raise_view(current_focus_view);
}
layout_slots(get_views());
};
wf::signal::connection_t<wf::workarea_changed_signal> workarea_changed =
[=] (wf::workarea_changed_signal *ev)
{
layout_slots(get_views());
};
/* View geometry changed. Also called when workspace changes */
wf::signal::connection_t<wf::view_geometry_changed_signal> view_geometry_changed =
[=] (wf::view_geometry_changed_signal *ev)
{
auto views = get_views();
if (!views.size())
{
deactivate();
return;
}
layout_slots(std::move(views));
};
/* View minimized */
wf::signal::connection_t<wf::view_minimized_signal> view_minimized = [=] (wf::view_minimized_signal *ev)
{
// Handle view restoration, view minimization is handled by disappeared already.
if (!ev->view->minimized)
{
layout_slots(get_views());
} else if (include_minimized && scale_data.count(ev->view))
{
if (!scale_data.at(ev->view).was_minimized)
{
scale_data.at(ev->view).was_minimized = true;
wf::scene::set_node_enabled(ev->view->get_root_node(), true);
}
fade_out(ev->view);
}
};
/* View unmapped */
wf::signal::connection_t<wf::view_unmapped_signal> view_unmapped = [=] (wf::view_unmapped_signal *ev)
{
if (auto toplevel = wf::toplevel_cast(ev->view))
{
check_focus_view(toplevel);
handle_view_unmapped(toplevel);
}
};
/* Our own refocus that uses untransformed coordinates */
void refocus()
{
if (current_focus_view)
{
wf::get_core().default_wm->focus_raise_view(current_focus_view);
select_view(current_focus_view);
return;
}
wayfire_toplevel_view next_focus = nullptr;
auto views = get_current_workspace_views();
for (auto v : views)
{
if (v->is_mapped() &&
v->get_keyboard_focus_surface())
{
next_focus = v;
break;
}
}
wf::get_core().default_wm->focus_raise_view(next_focus);
}
/* Returns true if any scale animation is running */
bool animation_running()
{
for (auto& e : scale_data)
{
if (e.second.fade_animation.running() ||
e.second.animation.scale_animation.running())
{
return true;
}
}
return false;
}
/* Assign transform values to the actual transformer */
wf::effect_hook_t pre_hook = [=] ()
{
transform_views();
};
/* Keep rendering until all animation has finished */
wf::effect_hook_t post_hook = [=] ()
{
bool running = animation_running();
if (running)
{
output->render->schedule_redraw();
}
if (active || running)
{
return;
}
finalize();
};
bool can_handle_drag()
{
return output->is_plugin_active(this->grab_interface.name);
}
wf::signal::connection_t<wf::move_drag::drag_focus_output_signal> on_drag_output_focus =
[=] (wf::move_drag::drag_focus_output_signal *ev)
{
if ((ev->focus_output == output) && can_handle_drag())
{
grab->set_wants_raw_input(true);
drag_helper->set_scale(1.0);
}
};
wf::signal::connection_t<wf::move_drag::drag_done_signal> on_drag_done =
[=] (wf::move_drag::drag_done_signal *ev)
{
if ((ev->focused_output == output) && can_handle_drag() && !drag_helper->is_view_held_in_place())
{
if (ev->main_view->get_output() == ev->focused_output)
{
// View left on the same output, don't do anything
for (auto& v : ev->all_views)
{
set_tiled_wobbly(v.view, true);
}
layout_slots(get_views());
return;
}
wf::move_drag::adjust_view_on_output(ev);
}
grab->set_wants_raw_input(false);
};
wf::signal::connection_t<wf::move_drag::snap_off_signal> on_drag_snap_off = [=] (auto)
{
last_selected_view = nullptr;
};
/* Activate and start scale animation */
bool activate()
{
if (active)
{
return false;
}
if (!output->activate_plugin(&grab_interface))
{
return false;
}
auto views = get_views();
if (views.empty())
{
output->deactivate_plugin(&grab_interface);
return false;
}
initial_workspace = output->wset()->get_current_workspace();
auto active_view = wf::get_active_view_for_output(output);
if (active_view)
{
initial_focus_view = active_view->weak_from_this();
current_focus_view = toplevel_cast(active_view);
if (std::find(views.begin(), views.end(), current_focus_view) == views.end())
{
current_focus_view = nullptr;
}
} else
{
initial_focus_view.reset();
current_focus_view = nullptr;
}
// Make sure no leftover events from the activation binding
// trigger an action in scale
last_selected_view = nullptr;
grab->grab_input(wf::scene::layer::WORKSPACE);
if (current_focus_view != wf::get_core().seat->get_active_view())
{
wf::get_core().default_wm->focus_raise_view(current_focus_view);
}
active = true;
layout_slots(get_views());
output->connect(&on_view_mapped);
output->connect(&workspace_changed);
output->connect(&workarea_changed);
output->connect(&view_minimized);
fade_out_all_except(current_focus_view);
fade_in(current_focus_view);
return true;
}
/* Deactivate and start unscale animation */
void deactivate()
{
active = false;
set_hook();
on_view_mapped.disconnect();
view_minimized.disconnect();
workspace_changed.disconnect();
workarea_changed.disconnect();
view_geometry_changed.disconnect();
grab->ungrab_input();
output->deactivate_plugin(&grab_interface);
for (auto& e : scale_data)
{
if (e.first->minimized && (e.first != current_focus_view))
{
e.second.visibility =
view_scale_data::view_visibility_t::HIDING;
setup_view_transform(e.second, 1, 1, 0, 0, 0);
} else
{
fade_in(e.first);
setup_view_transform(e.second, 1, 1, 0, 0, 1);
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;
}
}
refocus();
scale_end_signal signal;
output->emit(&signal);
}
/* Completely end scale, including animation */
void finalize()
{
if (active)
{
/* only emit the signal if deactivate() was not called before */
scale_end_signal signal;
output->emit(&signal);
if (drag_helper->view)
{
drag_helper->handle_input_released();
}
}
active = false;
unset_hook();
remove_transformers();
scale_data.clear();
grab->ungrab_input();
on_view_mapped.disconnect();
view_minimized.disconnect();
workspace_changed.disconnect();
workarea_changed.disconnect();
view_geometry_changed.disconnect();
output->deactivate_plugin(&grab_interface);
wf::scene::update(wf::get_core().scene(),
wf::scene::update_flag::INPUT_STATE);
}
/* Utility hook setter */
void set_hook()
{
if (hook_set)
{
return;
}
output->render->add_effect(&post_hook, wf::OUTPUT_EFFECT_POST);
output->render->add_effect(&pre_hook, wf::OUTPUT_EFFECT_PRE);
output->render->schedule_redraw();
hook_set = true;
}
/* Utility hook unsetter */
void unset_hook()
{
if (!hook_set)
{
return;
}
output->render->rem_effect(&post_hook);
output->render->rem_effect(&pre_hook);
hook_set = false;
}
void fini() override
{
finalize();
show_title.fini();
}
};
class wayfire_scale_global : public wf::plugin_interface_t,
public wf::per_output_tracker_mixin_t<wayfire_scale>
{
wf::ipc_activator_t toggle_ws{"scale/toggle"};
wf::ipc_activator_t toggle_all{"scale/toggle_all"};
public:
void init() override
{
this->init_output_tracking();
toggle_ws.set_handler(toggle_cb);
toggle_all.set_handler(toggle_all_cb);
}
void fini() override
{
this->fini_output_tracking();
}
void handle_new_output(wf::output_t *output) override
{
per_output_tracker_mixin_t::handle_new_output(output);
output->connect(&on_view_set_output);
}
void handle_output_removed(wf::output_t *output) override
{
per_output_tracker_mixin_t::handle_output_removed(output);
output->disconnect(&on_view_set_output);
}
wf::signal::connection_t<wf::view_set_output_signal> on_view_set_output =
[=] (wf::view_set_output_signal *ev)
{
if (auto toplevel = wf::toplevel_cast(ev->view))
{
auto old_output = ev->output;
if (old_output && output_instance.count(old_output))
{
this->output_instance[old_output]->handle_view_unmapped(toplevel);
}
auto new_output = ev->view->get_output();
if (new_output && output_instance.count(new_output) && output_instance[new_output]->active)
{
this->output_instance[ev->view->get_output()]->handle_new_view(toplevel, false);
}
}
};
wf::ipc_activator_t::handler_t toggle_cb = [=] (wf::output_t *output, wayfire_view)
{
if (this->output_instance[output]->handle_toggle(false))
{
output->render->schedule_redraw();
return true;
}
return false;
};
wf::ipc_activator_t::handler_t toggle_all_cb = [=] (wf::output_t *output, wayfire_view)
{
if (this->output_instance[output]->handle_toggle(true))
{
output->render->schedule_redraw();
return true;
}
return false;
};
};
DECLARE_WAYFIRE_PLUGIN(wayfire_scale_global);
|