commit ad0c3a48ce95c8bedfb12a7911a25b77e6247f36
parent 65aa8374f3cbc3b37cb7086c7a57b281380b4cf8
Author: deurzen <max@deurzen.net>
Date: Wed, 1 Jun 2022 12:37:57 +0200
adds view {,auto-}centering
Diffstat:
7 files changed, 52 insertions(+), 26 deletions(-)
diff --git a/include/kranewl/tree/output.hh b/include/kranewl/tree/output.hh
@@ -45,6 +45,8 @@ public:
Region placeable_region() const;
void set_placeable_region(Region const&);
+ void place_at_center(Region&) const;
+
bool contains(Pos) const;
bool contains(Region) const;
diff --git a/include/kranewl/tree/view.hh b/include/kranewl/tree/view.hh
@@ -77,6 +77,7 @@ typedef struct View : public Node {
void map();
void unmap();
+ void center();
void tile(Toggle);
void relayer(SceneLayer);
void raise() const;
diff --git a/src/kranewl/model.cc b/src/kranewl/model.cc
@@ -1508,24 +1508,16 @@ Model::center_view(View_ptr view)
{
TRACE();
- if (!is_free(view))
+ if (!is_free(view) || !view->mp_output)
return;
- Region region = view->free_region();
- const Region screen_region
- = view->mp_context->output()->placeable_region();
-
- region.pos.x = screen_region.pos.x
- + (screen_region.dim.w - region.dim.w) / 2;
-
- region.pos.y = screen_region.pos.y
- + (screen_region.dim.h - region.dim.h) / 2;
+ view->center();
Placement placement = Placement {
Placement::PlacementMethod::Free,
view,
view->free_decoration(),
- region
+ view->free_region()
};
place_view(placement);
diff --git a/src/kranewl/tree/output.cc b/src/kranewl/tree/output.cc
@@ -192,6 +192,18 @@ Output::set_placeable_region(Region const& region)
m_placeable_region = region;
}
+void
+Output::place_at_center(Region& region) const
+{
+ TRACE();
+
+ region.pos.x = m_placeable_region.pos.x
+ + (m_placeable_region.dim.w - region.dim.w) / 2;
+
+ region.pos.y = m_placeable_region.pos.y
+ + (m_placeable_region.dim.h - region.dim.h) / 2;
+}
+
bool
Output::contains(Pos pos) const
{
diff --git a/src/kranewl/tree/view.cc b/src/kranewl/tree/view.cc
@@ -3,6 +3,7 @@
#include <kranewl/model.hh>
#include <kranewl/scene-layer.hh>
#include <kranewl/server.hh>
+#include <kranewl/tree/output.hh>
#include <kranewl/tree/view.hh>
#include <kranewl/tree/xdg-view.hh>
@@ -301,6 +302,13 @@ View::unmap()
}
void
+View::center()
+{
+ if (mp_output)
+ mp_output->place_at_center(m_free_region);
+}
+
+void
View::tile(Toggle toggle)
{
TRACE();
diff --git a/src/kranewl/tree/xdg-view.cc b/src/kranewl/tree/xdg-view.cc
@@ -337,12 +337,6 @@ XDGView::handle_map(struct wl_listener* listener, void* data)
.h = state->min_height
});
- view->set_free_region(Region{
- .pos = Pos{0, 0},
- .dim = preferred_dim
- });
- view->set_tile_region(view->free_region());
-
view->m_app_id = wlr_xdg_toplevel->app_id
? wlr_xdg_toplevel->app_id
: "N/a";
@@ -407,6 +401,18 @@ XDGView::handle_map(struct wl_listener* listener, void* data)
} else
workspace = model->mp_workspace;
+ Region region = Region{
+ .pos = Pos{0, 0},
+ .dim = preferred_dim
+ };
+
+ Output_ptr output = workspace->context()->output();
+ if (output)
+ output->place_at_center(region);
+
+ view->set_free_region(region);
+ view->set_tile_region(region);
+
view->set_mapped(true);
view->render_decoration();
model->register_view(view, workspace);
diff --git a/src/kranewl/tree/xwayland-view.cc b/src/kranewl/tree/xwayland-view.cc
@@ -314,15 +314,6 @@ XWaylandView::handle_map(struct wl_listener* listener, void* data)
preferred_dim.h += extents.top + extents.bottom;
view->set_preferred_dim(preferred_dim);
- view->set_free_region(Region{
- .pos = Pos{
- .x = xwayland_surface->x - extents.left,
- .y = xwayland_surface->y - extents.top
- },
- .dim = preferred_dim
- });
- view->set_tile_region(view->free_region());
-
view->m_app_id = view->m_class = xwayland_surface->class_
? xwayland_surface->class_
: "N/a";
@@ -364,7 +355,21 @@ XWaylandView::handle_map(struct wl_listener* listener, void* data)
wlr_scene_node_lower_to_bottom(&view->m_protrusions[i]->node);
}
+ Region region = Region{
+ .pos = Pos{
+ .x = xwayland_surface->x - extents.left,
+ .y = xwayland_surface->y - extents.top
+ },
+ .dim = preferred_dim
+ };
+
Workspace_ptr workspace = model->mp_workspace;
+ Output_ptr output = workspace->context()->output();
+ if (output)
+ output->place_at_center(region);
+
+ view->set_free_region(region);
+ view->set_tile_region(region);
view->set_mapped(true);
view->render_decoration();