summaryrefslogtreecommitdiff
path: root/gui-wm/wayfire/files/wayfire-0.11.0-physical-tiling.patch
blob: 7d7bd25267521dd2e7694e02e83b0425475161c6 (plain)
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
--- a/metadata/simple-tile.xml
+++ b/metadata/simple-tile.xml
@@ -2,7 +2,7 @@
 <wayfire>
 	<plugin name="simple-tile">
 		<_short>Simple Tile</_short>
-		<_long>A plugin which provides some tiling features, inspired by Sway.	The plugin is meant to contain only the basics.</_long>
+		<_long>A plugin which provides some tiling features, inspired by Sway.	The plugin is meant to contain only the basics. On a combined desktop, physical window scopes have independent tiling layouts; VIRTUAL application exceptions use the combined layout.</_long>
 		<category>Window Management</category>
 		<option name="tile_by_default" type="string">
 			<_short>Tile enabled for specified window types</_short>
--- a/plugins/tile/tile-dragging.hpp
+++ b/plugins/tile/tile-dragging.hpp
@@ -105,12 +105,12 @@
     {
         auto input = get_global_input_coordinates(output);
         auto view  = check_drop_destination(output, input, dragged_view);
-        if (!view)
-        {
+        auto& root = drop_root(output, input, dragged_view);
+        if (!view && !root->children.empty()) return hide_preview();
+        if (!view && tile::get_root(view_node_t::get_node(dragged_view)).get() == root.get())
             return hide_preview();
-        }
-
-        auto split = calculate_insert_type(view, input);
+
+        auto split = view ? calculate_insert_type(view, input) : INSERT_NONE;
         if (preview && (preview->get_output() != output))
         {
             hide_preview();
@@ -123,7 +123,7 @@
                 start_coords, output, "simple-tile");
         }
 
-        auto preview_geometry = calculate_split_preview(view, split);
+        auto preview_geometry = view ? calculate_split_preview(view, split) : root->geometry;
         preview_geometry = get_wset_local_coordinates(output->wset(), preview_geometry);
         if (preview_geometry != preview->get_target_geometry())
         {
@@ -162,6 +162,8 @@
     {
         auto output_a = a->get_output();
         auto output_b = b->get_output();
+        auto region_a = tile_workspace_set_data_t::get(output_a).region_for(a);
+        auto region_b = tile_workspace_set_data_t::get(output_b).region_for(b);
 
         if (output_a != output_b)
         {
@@ -171,6 +173,8 @@
             move_tiled_view(b, output_a);
         }
 
+        if (wf::window_scope_policy(a) == "physical") wf::select_window_scope_monitor(a, region_b);
+        if (wf::window_scope_policy(b) == "physical") wf::select_window_scope_monitor(b, region_a);
         {
             autocommit_transaction_t tx;
 
@@ -229,6 +233,8 @@
             move_tiled_view(source, target_output);
         }
 
+        if (wf::window_scope_policy(source) == "physical")
+            wf::select_window_scope_monitor(source, tile_workspace_set_data_t::get(target_output).region_for(target->view));
         autocommit_transaction_t tx;
         auto split_type = (split == INSERT_LEFT || split == INSERT_RIGHT) ?
             SPLIT_VERTICAL : SPLIT_HORIZONTAL;
@@ -292,7 +298,28 @@
         auto dropped_at = check_drop_destination(output, input, view);
         if (!dropped_at)
         {
-            return false;
+            auto& destination = drop_root(output, input, view);
+            auto node = view_node_t::get_node(view);
+            if (!node || !destination->children.empty() || tile::get_root(node).get() == destination.get())
+                return false;
+            auto old_output = view->get_output();
+            if (old_output != output)
+            {
+                wf::emit_view_pre_moved_to_wset_pre(view, view->get_wset(), output->wset());
+                move_tiled_view(view, output);
+            }
+            auto& data = tile_workspace_set_data_t::get(output);
+            if (wf::window_scope_policy(view) == "physical")
+                wf::select_window_scope_monitor(view, data.region_at(output->get_cursor_position(), view));
+            {
+                autocommit_transaction_t tx;
+                auto moving = node->parent->remove_child(node, tx.tx);
+                destination->as_split_node()->add_child(std::move(moving), tx.tx);
+                tile_workspace_set_data_t::get(old_output).refresh(tx.tx);
+                data.refresh(tx.tx);
+            }
+            if (old_output != output) wf::emit_view_moved_to_wset(view, old_output->wset(), output->wset());
+            return true;
         }
 
         auto split = calculate_insert_type(dropped_at, input);
@@ -303,6 +330,11 @@
 
         if (split == INSERT_SWAP)
         {
+            auto& target_data = tile_workspace_set_data_t::get(output);
+            auto target_policy = wf::window_scope_policy(dropped_at->view);
+            if (target_policy != "physical" && target_policy != "virtual" &&
+                target_data.region_for(dropped_at->view) != tile_workspace_set_data_t::get(view->get_output()).region_for(view))
+                return false;
             handle_swap(view, dropped_at->view);
         } else
         {
@@ -407,15 +439,23 @@
         return preview;
     }
 
+    std::unique_ptr<tree_node_t>& drop_root(wf::output_t *output, wf::pointf_t global_coords,
+        wayfire_toplevel_view dragged_view)
+    {
+        auto ws = output->wset()->get_current_workspace();
+        auto size = output->get_screen_size();
+        auto local = global_coords - wf::pointf_t{double(ws.x * size.width), double(ws.y * size.height)};
+        auto& data = tile_workspace_set_data_t::get(output);
+        return data.root_at(ws, data.region_at(local, dragged_view));
+    }
+
     /**
      * Return the node under the input which is suitable for dropping on.
      */
     nonstd::observer_ptr<view_node_t> check_drop_destination(wf::output_t *output, wf::pointf_t global_coords,
         wayfire_toplevel_view dragged_view)
     {
-        auto ws = output->wset()->get_current_workspace();
-        auto dropped_at =
-            find_view_at(tile_workspace_set_data_t::get(output).roots[ws.x][ws.y], global_coords);
+        auto dropped_at = find_view_at(drop_root(output, global_coords, dragged_view), global_coords);
 
         if (!dropped_at || (dropped_at->view == dragged_view))
         {
--- a/plugins/tile/tile-ipc.hpp
+++ b/plugins/tile/tile-ipc.hpp
@@ -209,6 +209,7 @@
         // Handle cases with a single view.
         auto split_root = std::make_unique<tile::split_node_t>(tile_workspace_set_data_t::default_split);
         split_root->children.push_back(std::move(root));
+        split_root->children.front()->parent = nonstd::make_observer(split_root.get());
         return split_root;
     }
 
@@ -240,6 +241,32 @@
     tile_workspace_set_data_t::get(view->get_wset()).set_view_maximized(view, show_maximized);
 
     return wf::ipc::json_ok();
+}
+
+// A tiling layout belongs to one physical monitor, or to VIRTUAL for views
+// whose policy spans the combined desktop. With no selector use the focused
+// view's scope, falling back to the pointer and default policy.
+inline std::string ipc_monitor(tile_workspace_set_data_t& data, const json_t& params)
+{
+    auto output = data.wset.lock()->get_attached_output();
+    if (params.has_member("monitor"))
+    {
+        auto monitor = wf::ipc::json_get_string(params, "monitor");
+        if (monitor.empty() || monitor == "virtual" || monitor == "VIRTUAL-1") return "";
+        if (output)
+        {
+            if (auto layout = output->get_data<wf::output_stitch_layout_t>())
+                for (auto& zone : layout->zones) if (zone.name == monitor) return monitor;
+        } else if (data.physical_roots.count(monitor)) return monitor;
+        throw wf::ipc::ipc_method_exception_t("Unknown physical monitor: " + monitor);
+    }
+    if (output)
+    {
+        auto view = wf::toplevel_cast(wf::get_active_view_for_output(output));
+        if (view && view->get_wset() == data.wset.lock()) return data.region_for(view);
+        return data.region_at(output->get_cursor_position());
+    }
+    return "";
 }
 
 inline wf::json_t handle_ipc_get_layout(const json_t& params)
@@ -257,7 +284,7 @@
     if (ws)
     {
         auto grid_size = ws->get_workspace_grid_size();
-        if ((x >= grid_size.width) || (y >= grid_size.height))
+        if (x < 0 || y < 0 || (x >= grid_size.width) || (y >= grid_size.height))
         {
             return wf::ipc::json_error("invalid workspace coordinates");
         }
@@ -271,8 +298,16 @@
             (double)(cur_ws.y * resolution.height),
         };
 
-        response["layout"] =
-            tree_to_json(tile_workspace_set_data_t::get(ws->shared_from_this()).roots[x][y], offset);
+        auto& data = tile_workspace_set_data_t::get(ws->shared_from_this());
+        auto monitor = ipc_monitor(data, params);
+        response["monitor"] = monitor.empty() ? "VIRTUAL-1" : monitor;
+        response["layout"] = tree_to_json(data.root_at({int(x), int(y)}, monitor), offset);
+        wf::json_t monitors = wf::json_t::array();
+        monitors.append("VIRTUAL-1");
+        if (auto output = ws->get_attached_output())
+            if (auto layout = output->get_data<wf::output_stitch_layout_t>())
+                for (auto& zone : layout->zones) monitors.append(zone.name);
+        response["available-monitors"] = monitors;
         return response;
     }
 
@@ -302,7 +337,7 @@
     }
 
     auto grid_size = ws->get_workspace_grid_size();
-    if ((x >= grid_size.width) || (y >= grid_size.height))
+    if (x < 0 || y < 0 || (x >= grid_size.width) || (y >= grid_size.height))
     {
         return wf::ipc::json_error("invalid workspace coordinates");
     }
@@ -310,17 +345,29 @@
     auto& tile_ws = tile_workspace_set_data_t::get(ws->shared_from_this());
 
     tile::json_builder_data_t data;
-    data.gaps = tile_ws.get_gaps();
-    auto workarea = tile_ws.roots[x][y]->geometry;
+    auto monitor = ipc_monitor(tile_ws, params);
+    auto& root = tile_ws.root_at({int(x), int(y)}, monitor);
+    data.gaps = tile_ws.get_gaps(monitor);
+    auto workarea = root->geometry;
     if (auto err = tile::verify_json_tree(params["layout"], data, wf::dimensions(workarea)))
     {
         return wf::ipc::json_error(*err);
+    }
+
+    // Do not silently turn an application exception into a physical window,
+    // or let an ordinary window escape into a desktop-wide tiling layout.
+    for (auto view : data.touched_views)
+    {
+        auto source_region = tile_ws.region_for(view);
+        if (source_region != monitor &&
+            !(wf::window_scope_policy(view) == "physical" && !source_region.empty() && !monitor.empty()))
+            return wf::ipc::json_error("Layout monitor does not match a window's scope; set its scope first");
     }
 
     // Step 1: detach any views which are currently present in the layout, but should no longer be
     // in the layout
     std::vector<nonstd::observer_ptr<tile::view_node_t>> views_to_remove;
-    tile::for_each_view(tile_ws.roots[x][y], [&] (wayfire_toplevel_view view)
+    tile::for_each_view(root, [&] (wayfire_toplevel_view view)
     {
         if (!data.touched_views.count(view))
         {
@@ -337,6 +384,8 @@
         // Step 2: temporarily detach some of the nodes
         for (auto& touched_view : data.touched_views)
         {
+            if (!monitor.empty() && wf::window_scope_policy(touched_view) == "physical")
+                wf::select_window_scope_monitor(touched_view, monitor);
             auto tile = wf::tile::view_node_t::get_node(touched_view);
             if (tile)
             {
@@ -360,11 +409,11 @@
         }
 
         // Step 3: set up the new layout
-        tile_ws.roots[x][y] = build_tree_from_json(params["layout"], &tile_ws, {static_cast<int>(x),
+        root = build_tree_from_json(params["layout"], &tile_ws, {static_cast<int>(x),
             static_cast<int>(y)});
-        tile::flatten_tree(tile_ws.roots[x][y]);
-        tile_ws.roots[x][y]->set_gaps(tile_ws.get_gaps());
-        tile_ws.roots[x][y]->set_geometry(workarea, tx.tx);
+        tile::flatten_tree(root);
+        root->set_gaps(tile_ws.get_gaps(monitor));
+        root->set_geometry(workarea, tx.tx);
     }
 
     data.touched_wsets.insert(ws);
--- a/plugins/tile/tile-plugin.cpp
+++ b/plugins/tile/tile-plugin.cpp
@@ -80,7 +80,7 @@
     bool has_fullscreen_view()
     {
         int count_fullscreen = 0;
-        for_each_view(tile_workspace_set_data_t::get_current_root(output), [&] (wayfire_toplevel_view view)
+        for_each_view(tile_workspace_set_data_t::get_current_root(output, get_tiled_focus()), [&] (wayfire_toplevel_view view)
         {
             count_fullscreen += view->pending_fullscreen();
         });
@@ -179,8 +179,12 @@
             return;
         }
 
-        // we ignore those requests because we manage the tiled state manually
+        // Keep tiling ownership, but honor scope/layout changes from the core.
         ev->carried_out = true;
+        stop_controller(true);
+        auto& data = tile_workspace_set_data_t::get(ev->view->get_wset());
+        data.reconcile_view(ev->view, ev->workspace);
+        data.update_root_size();
     };
 
     wf::signal::connection_t<view_fullscreen_request_signal> on_fullscreen_request =
@@ -192,6 +196,7 @@
         }
 
         ev->carried_out = true;
+        tile_workspace_set_data_t::get(ev->view->get_wset()).reconcile_view(ev->view, ev->workspace);
         tile_workspace_set_data_t::get(ev->view->get_wset()).set_view_fullscreen(ev->view, ev->state);
     };
 
@@ -448,11 +453,34 @@
     shared_data::ref_ptr_t<ipc::method_repository_t> ipc_repo;
     shared_data::ref_ptr_t<wf::move_drag::core_drag_t> drag_helper;
     std::unique_ptr<tile::drag_manager_t> preview_manager;
+    wf::wl_idle_call reflow_idle;
+
+    void schedule_reflow()
+    {
+        reflow_idle.run_once([=] ()
+        {
+            for (auto wset : workspace_set_t::get_all())
+            {
+                if (auto data = wset->get_data<tile_workspace_set_data_t>())
+                {
+                    stop_controller(wset->shared_from_this());
+                    data->update_root_size();
+                }
+            }
+        });
+    }
+
+    wf::signal::connection_t<wf::output_layout_configuration_changed_signal> layout_changed =
+        [=] (auto) { schedule_reflow(); };
+    wf::signal::connection_t<wf::reload_config_signal> config_changed =
+        [=] (auto) { schedule_reflow(); };
 
   public:
     void init() override
     {
         init_output_tracking();
+        wf::get_core().connect(&layout_changed);
+        wf::get_core().connect(&config_changed);
         wf::get_core().connect(&on_view_pre_moved_to_wset);
         wf::get_core().connect(&on_view_moved_to_wset);
         wf::get_core().connect(&on_focus_changed);
@@ -465,6 +493,7 @@
 
     void fini() override
     {
+        reflow_idle.disconnect();
         for (auto view : wf::get_core().get_all_views())
         {
             clear_auto_floating_above(view);
@@ -589,10 +618,7 @@
     };
 };
 
-std::unique_ptr<tile::tree_node_t>& tile::get_root(wf::workspace_set_t *set, wf::point_t workspace)
-{
-    return tile_workspace_set_data_t::get(set->shared_from_this()).roots[workspace.x][workspace.y];
+
 }
-}
 
 DECLARE_WAYFIRE_PLUGIN(wf::tile_plugin_t);
--- a/plugins/tile/tile-wset.hpp
+++ b/plugins/tile/tile-wset.hpp
@@ -3,6 +3,9 @@
 #include "tree.hpp"
 #include "../wm-actions/wm-actions-signals.hpp"
 #include <wayfire/output.hpp>
+#include <wayfire/window-scope.hpp>
+#include <map>
+#include <tuple>
 #include "tree-controller.hpp"
 #include "wayfire/view-helpers.hpp"
 #include "wayfire/txn/transaction-manager.hpp"
@@ -68,7 +71,120 @@
 class tile_workspace_set_data_t : public wf::custom_data_t
 {
   public:
-    std::vector<std::vector<std::unique_ptr<wf::tile::tree_node_t>>> roots;
+    using root_grid_t = std::vector<std::vector<std::unique_ptr<wf::tile::tree_node_t>>>;
+    root_grid_t roots; // VIRTUAL (or an ordinary, uncombined output).
+    std::map<std::string, root_grid_t> physical_roots;
+    bool updating_roots = false;
+
+    template<class Callback> void for_each_root(Callback callback)
+    {
+        auto visit = [&] (root_grid_t& grid, const std::string& monitor)
+        {
+            for (size_t x = 0; x < grid.size(); ++x)
+                for (size_t y = 0; y < grid[x].size(); ++y)
+                    if (grid[x][y]) callback(grid[x][y], wf::point_t{int(x), int(y)}, monitor);
+        };
+        visit(roots, "");
+        for (auto& [monitor, grid] : physical_roots) visit(grid, monitor);
+    }
+
+    std::string region_for(wayfire_toplevel_view view)
+    {
+        if (view)
+        {
+            if (view->get_output())
+            {
+                if (auto zone = wf::window_scope_zone(view)) return zone->name;
+            } else if (auto node = tile::view_node_t::get_node(view))
+            {
+                std::string result;
+                auto root = tile::get_root(node);
+                for_each_root([&] (auto& candidate, auto, const auto& monitor)
+                { if (candidate.get() == root.get()) result = monitor; });
+                return result;
+            }
+        }
+        return "";
+    }
+
+    wf::geometry_t region_workarea(const std::string& monitor)
+    {
+        auto output = wset.lock()->get_attached_output();
+        if (!output) return tile::default_output_resolution;
+        if (auto layout = output->get_data<wf::output_stitch_layout_t>())
+            for (auto& zone : layout->zones)
+                if (zone.name == monitor) return wf::physical_zone_workarea(output, zone);
+        return output->workarea->get_workarea();
+    }
+
+    std::unique_ptr<tile::tree_node_t>& root_at(wf::point_t vp, const std::string& monitor = "")
+    {
+        if (monitor.empty()) return roots[vp.x][vp.y];
+        auto& grid = physical_roots[monitor];
+        auto size = wset.lock()->get_workspace_grid_size();
+        grid.resize(size.width);
+        for (auto& column : grid) column.resize(size.height);
+        auto& root = grid[vp.x][vp.y];
+        if (!root)
+        {
+            root = std::make_unique<tile::split_node_t>(default_split);
+            auto area = region_workarea(monitor);
+            auto geometry = wset.lock()->get_last_output_geometry().value_or(tile::default_output_resolution);
+            area.x += vp.x * geometry.width; area.y += vp.y * geometry.height;
+            autocommit_transaction_t tx;
+            root->set_gaps(get_gaps(monitor));
+            root->set_geometry(area, tx.tx);
+        }
+        return root;
+    }
+
+    // Used by drag-and-drop and layout IPC. Named-monitor policies stay pinned;
+    // ordinary physical policies choose the monitor under the pointer.
+    std::string region_at(wf::pointf_t local, wayfire_toplevel_view view = nullptr)
+    {
+        auto output = wset.lock()->get_attached_output();
+        if (!output) return region_for(view);
+        std::string policy = view ? wf::window_scope_policy(view) :
+            wf::option_wrapper_t<std::string>{"core/window_scope"}.value();
+        if (policy == "virtual") return "";
+        auto layout = output->get_data<wf::output_stitch_layout_t>();
+        if (!layout || layout->zones.empty()) return "";
+        if (policy != "physical")
+            for (auto& zone : layout->zones) if (zone.name == policy) return zone.name;
+        double best = INFINITY; std::string result;
+        for (auto& zone : layout->zones)
+        {
+            const auto& g = zone.geometry;
+            double dx = local.x - std::clamp(local.x, g.x, g.x + g.width);
+            double dy = local.y - std::clamp(local.y, g.y, g.y + g.height);
+            if (dx * dx + dy * dy < best) { best = dx * dx + dy * dy; result = zone.name; }
+        }
+        return result;
+    }
+
+    void reconcile_view(wayfire_toplevel_view view, wf::point_t vp)
+    {
+        auto node = tile::view_node_t::get_node(view);
+        if (!node) return;
+        auto& destination = root_at(vp, region_for(view));
+        if (tile::get_root(node).get() == destination.get()) return;
+        autocommit_transaction_t tx;
+        auto moving = node->parent->remove_child(node, tx.tx);
+        destination->as_split_node()->add_child(std::move(moving), tx.tx);
+        wf::scene::readd_front(tiled_sublayer[vp.x][vp.y], view->get_root_node());
+    }
+
+    void reconcile_scopes()
+    {
+        if (!wset.lock()->get_attached_output()) return;
+        std::vector<std::pair<wayfire_toplevel_view, wf::point_t>> views;
+        for_each_root([&] (auto& root, auto vp, const auto&)
+        {
+            tile::for_each_view(root, [&] (auto view) { views.emplace_back(view, vp); });
+        });
+        for (auto& [view, vp] : views) reconcile_view(view, vp);
+        flatten_roots();
+    }
     std::vector<std::vector<wf::scene::floating_inner_ptr>> tiled_sublayer;
     static constexpr wf::tile::split_direction_t default_split = wf::tile::SPLIT_VERTICAL;
 
@@ -94,16 +210,10 @@
 
     ~tile_workspace_set_data_t()
     {
-        for (auto& row : roots)
-        {
-            for (auto& root : row)
-            {
-                tile::for_each_view(root, [] (wayfire_toplevel_view view)
-                {
-                    view->set_allowed_actions(VIEW_ALLOW_ALL);
-                });
-            }
-        }
+        for_each_root([] (auto& root, auto, const auto&)
+        {
+            tile::for_each_view(root, [] (auto view) { view->set_allowed_actions(VIEW_ALLOW_ALL); });
+        });
     }
 
     wf::signal::connection_t<workarea_changed_signal> on_workarea_changed = [=] (auto)
@@ -129,17 +239,24 @@
 
     void resize_roots(wf::dimensions_t wsize)
     {
+        std::vector<std::tuple<wayfire_toplevel_view, wf::point_t, bool>> retained;
+        for_each_root([&] (auto& root, auto vp, const auto&)
+        {
+            tile::for_each_view(root, [&] (auto view)
+            {
+                retained.emplace_back(view, vp, tile::view_node_t::get_node(view)->show_maximized);
+                view->set_allowed_actions(VIEW_ALLOW_ALL);
+            });
+        });
         for (size_t i = 0; i < tiled_sublayer.size(); i++)
         {
             for (size_t j = 0; j < tiled_sublayer[i].size(); j++)
             {
-                if (wset.lock()->is_workspace_valid({(int)i, (int)j}))
-                {
-                    destroy_sublayer(tiled_sublayer[i][j]);
-                }
+                destroy_sublayer(tiled_sublayer[i][j]);
             }
         }
 
+        physical_roots.clear();
         roots.resize(wsize.width);
         tiled_sublayer.resize(wsize.width);
         for (int i = 0; i < wsize.width; i++)
@@ -156,30 +273,32 @@
 
         update_root_size();
         update_gaps();
+        for (auto& [view, vp, maximized] : retained)
+        {
+            if (!view->is_mapped()) continue;
+            vp.x = std::clamp(vp.x, 0, wsize.width - 1);
+            vp.y = std::clamp(vp.y, 0, wsize.height - 1);
+            attach_view(view, vp);
+            if (maximized) set_view_maximized(view, true);
+        }
     }
 
     void update_root_size()
     {
-        auto wo = wset.lock()->get_attached_output();
-        wf::geometry_t workarea = wo ? wo->workarea->get_workarea() : tile::default_output_resolution;
-
-        wf::geometry_t output_geometry =
-            wset.lock()->get_last_output_geometry().value_or(tile::default_output_resolution);
-
-        auto wsize = wset.lock()->get_workspace_grid_size();
-        for (int i = 0; i < wsize.width; i++)
-        {
-            for (int j = 0; j < wsize.height; j++)
-            {
-                /* Set size */
-                auto vp_geometry = workarea;
-                vp_geometry.x += i * output_geometry.width;
-                vp_geometry.y += j * output_geometry.height;
-
-                autocommit_transaction_t tx;
-                roots[i][j]->set_geometry(vp_geometry, tx.tx);
-            }
-        }
+        if (updating_roots) return;
+        updating_roots = true;
+        reconcile_scopes();
+        auto output_geometry = wset.lock()->get_last_output_geometry().value_or(tile::default_output_resolution);
+        autocommit_transaction_t tx;
+        for_each_root([&] (auto& root, auto vp, const auto& monitor)
+        {
+            auto area = region_workarea(monitor);
+            area.x += vp.x * output_geometry.width;
+            area.y += vp.y * output_geometry.height;
+            root->set_gaps(get_gaps(monitor));
+            root->set_geometry(area, tx.tx);
+        });
+        updating_roots = false;
     }
 
     void destroy_sublayer(wf::scene::floating_inner_ptr sublayer)
@@ -195,8 +314,10 @@
         wf::scene::remove_child(sublayer);
     }
 
-    tile::gap_size_t get_gaps() const
-    {
+    tile::gap_size_t get_gaps(const std::string& monitor = "") const
+    {
+        // Physical workareas already include the Displays monitor-zone gap.
+        if (!monitor.empty()) return {0, 0, 0, 0, inner_gaps};
         return {
             .left   = outer_horiz_gaps,
             .right  = outer_horiz_gaps,
@@ -208,14 +329,11 @@
 
     void update_gaps_with_tx(wf::txn::transaction_uptr& tx)
     {
-        for (auto& col : roots)
-        {
-            for (auto& root : col)
-            {
-                root->set_gaps(get_gaps());
-                root->set_geometry(root->geometry, tx);
-            }
-        }
+        for_each_root([&] (auto& root, auto, const auto& monitor)
+        {
+            root->set_gaps(get_gaps(monitor));
+            root->set_geometry(root->geometry, tx);
+        });
     }
 
     void refresh(wf::txn::transaction_uptr& tx)
@@ -232,13 +350,7 @@
 
     void flatten_roots()
     {
-        for (auto& col : roots)
-        {
-            for (auto& root : col)
-            {
-                tile::flatten_tree(root);
-            }
-        }
+        for_each_root([] (auto& root, auto, const auto&) { tile::flatten_tree(root); });
     }
 
     static tile_workspace_set_data_t& get(std::shared_ptr<workspace_set_t> set)
@@ -256,12 +368,13 @@
         return get(output->wset());
     }
 
-    static std::unique_ptr<tile::tree_node_t>& get_current_root(wf::output_t *output)
+    static std::unique_ptr<tile::tree_node_t>& get_current_root(wf::output_t *output,
+        wayfire_toplevel_view view = nullptr)
     {
         auto set   = output->wset();
         auto vp    = set->get_current_workspace();
         auto& data = get(output);
-        return data.roots[vp.x][vp.y];
+        return data.root_at(vp, data.region_for(view));
     }
 
     static scene::floating_inner_ptr get_current_sublayer(wf::output_t *output)
@@ -290,11 +403,11 @@
         auto view_node = setup_view_tiling(view, vp);
         {
             autocommit_transaction_t tx;
-            roots[vp.x][vp.y]->as_split_node()->add_child(std::move(view_node), tx.tx);
+            root_at(vp, region_for(view))->as_split_node()->add_child(std::move(view_node), tx.tx);
         }
 
         consider_exit_fullscreen(view);
-        unmaximize_all_views_on_workspace();
+        unmaximize_all_views_on_workspace(view);
     }
 
     /** Remove the given view from its tiling container */
@@ -336,7 +449,7 @@
         if (tile::view_node_t::get_node(view) && !view->pending_fullscreen())
         {
             auto vp = this->wset.lock()->get_current_workspace();
-            for_each_view(roots[vp.x][vp.y], [&] (wayfire_toplevel_view view)
+            for_each_view(root_at(vp, region_for(view)), [&] (wayfire_toplevel_view view)
             {
                 if (view->pending_fullscreen())
                 {
@@ -346,10 +459,10 @@
         }
     }
 
-    void unmaximize_all_views_on_workspace()
+    void unmaximize_all_views_on_workspace(wayfire_toplevel_view selected)
     {
         auto vp = this->wset.lock()->get_current_workspace();
-        for_each_view(roots[vp.x][vp.y], [&] (wayfire_toplevel_view view)
+        for_each_view(root_at(vp, region_for(selected)), [&] (wayfire_toplevel_view view)
         {
             auto node = tile::view_node_t::get_node(view);
             if (node && node->show_maximized)
--- a/plugins/tile/tree-controller.cpp
+++ b/plugins/tile/tree-controller.cpp
@@ -141,10 +141,10 @@
 }
 
 /* ----------------------- resize tile controller --------------------------- */
-resize_view_controller_t::resize_view_controller_t(wf::workspace_set_t *wset, wayfire_toplevel_view)
+resize_view_controller_t::resize_view_controller_t(wf::workspace_set_t *wset, wayfire_toplevel_view view)
 {
     this->last_point   = get_global_input_coordinates(wset->get_attached_output());
-    this->grabbed_view = find_view_at(get_root(wset, wset->get_current_workspace()), last_point);
+    this->grabbed_view = view_node_t::get_node(view);
     this->output = wset->get_attached_output();
 
     if (this->grabbed_view)
--- a/plugins/tile/tree-controller.hpp
+++ b/plugins/tile/tree-controller.hpp
@@ -66,7 +66,6 @@
     {}
 };
 
-std::unique_ptr<tree_node_t>& get_root(wf::workspace_set_t *set, wf::point_t workspace);
 
 /**
  * Represents the moving view action, i.e dragging a window to change its
--- a/plugins/tile/tree.cpp
+++ b/plugins/tile/tree.cpp
@@ -1,4 +1,5 @@
 #include "tree.hpp"
+#include <wayfire/window-scope.hpp>
 #include "wayfire/core.hpp"
 #include "wayfire/geometry.hpp"
 #include "wayfire/toplevel-view.hpp"
@@ -408,7 +409,8 @@
     /* If view is maximized, we want to use the full available geometry */
     if (view->pending_fullscreen())
     {
-        local_geometry = adjust_geometry_for_workspace(wset, size, local_geometry);
+        auto fullscreen = view->get_output() ? wf::window_scope_fullscreen(view) : size;
+        local_geometry = adjust_geometry_for_workspace(wset, fullscreen, local_geometry);
     } else if (this->show_maximized)
     {
         auto attached_output = wset->get_attached_output();
@@ -417,11 +419,12 @@
             return local_geometry;
         }
 
-        auto base_workarea   = attached_output->workarea->get_workarea();
+        auto base_workarea   = wf::window_scope_workarea(view);
         auto source_geometry = view->get_geometry();
 
-        int oh = outer_horiz_gaps;
-        int ov = outer_vert_gaps;
+        bool physical = wf::window_scope_zone(view).has_value();
+        int oh = physical ? 0 : int(outer_horiz_gaps);
+        int ov = physical ? 0 : int(outer_vert_gaps);
 
         local_geometry    = adjust_geometry_for_workspace(wset, base_workarea, source_geometry);
         local_geometry.x += oh;
--- a/src/core/window-scope.cpp
+++ b/src/core/window-scope.cpp
@@ -170,10 +170,15 @@
 {
     if (view->parent) return;
     auto policy = window_scope_policy(view);
+    auto remembered_monitor = view->get_data_safe<scope_data_t>()->monitor;
     for (auto output : get_core().output_layout->get_outputs())
     {
         auto layout = output->get_data<output_stitch_layout_t>();
         bool match = policy == "virtual" ? bool(layout) : policy == output->handle->name;
+        // When a combined desktop is separated, the remembered physical
+        // region becomes a real output. Keep the window bound to that head.
+        if (policy == "physical" && !layout && !remembered_monitor.empty())
+            match = remembered_monitor == output->handle->name;
         if (layout) for (auto& zone : layout->zones) match |= policy == zone.name;
         if (match)
         {