Skip to content

Commit 2c45273

Browse files
committed
view: unconditionally center views in output layout
Whatever type or dimensions of a view, position it at center of the output layout. Then, if it is a primary view or if it extends the output layout, send request to maximize it. The client may or may not change the view dimensions after that. If not, e.g. weston-flower, the view will be kept displayed at center instead of to left. If so, the view will expand from center to fill the whole output layout. In order to update the view position, XDG shell surface commit is now handled to check for dimension change.
1 parent 8a00921 commit 2c45273

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

view.c

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,11 @@ view_activate(struct cg_view *view, bool activate)
5252
view->impl->activate(view, activate);
5353
}
5454

55-
static bool
55+
static inline bool
5656
view_extends_output_layout(struct cg_view *view, struct wlr_box *layout_box)
5757
{
58-
int width, height;
59-
view->impl->get_geometry(view, &width, &height);
60-
61-
return (layout_box->height < height || layout_box->width < width);
62-
}
63-
64-
static void
65-
view_maximize(struct cg_view *view, struct wlr_box *layout_box)
66-
{
67-
view->lx = layout_box->x;
68-
view->ly = layout_box->y;
69-
70-
wlr_scene_node_set_position(&view->scene_tree->node, view->lx, view->ly);
71-
72-
view->impl->maximize(view, layout_box->width, layout_box->height);
73-
}
74-
75-
static void
76-
view_center(struct cg_view *view, struct wlr_box *layout_box)
77-
{
78-
int width, height;
79-
view->impl->get_geometry(view, &width, &height);
80-
81-
view->lx = (layout_box->width - width) / 2;
82-
view->ly = (layout_box->height - height) / 2;
83-
84-
wlr_scene_node_set_position(&view->scene_tree->node, view->lx, view->ly);
58+
// View width and height are expected to be set before calling this function
59+
return (layout_box->height < view->height || layout_box->width < view->width);
8560
}
8661

8762
void
@@ -90,10 +65,17 @@ view_position(struct cg_view *view)
9065
struct wlr_box layout_box;
9166
wlr_output_layout_get_box(view->server->output_layout, NULL, &layout_box);
9267

68+
view->impl->get_geometry(view, &view->width, &view->height);
69+
70+
// If view dimensions are not the same as output layout ones,
71+
// it will be centered first
72+
view->lx = layout_box.x + (layout_box.width - view->width) / 2;
73+
view->ly = layout_box.y + (layout_box.height - view->height) / 2;
74+
75+
wlr_scene_node_set_position(&view->scene_tree->node, view->lx, view->ly);
76+
9377
if (view_is_primary(view) || view_extends_output_layout(view, &layout_box)) {
94-
view_maximize(view, &layout_box);
95-
} else {
96-
view_center(view, &layout_box);
78+
view->impl->maximize(view, layout_box.width, layout_box.height);
9779
}
9880
}
9981

view.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct cg_view {
2929

3030
/* The view has a position in layout coordinates. */
3131
int lx, ly;
32+
int width, height;
3233

3334
enum cg_view_type type;
3435
const struct cg_view_impl *impl;

xdg_shell.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,34 @@ handle_xdg_shell_surface_unmap(struct wl_listener *listener, void *data)
183183
struct cg_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, unmap);
184184
struct cg_view *view = &xdg_shell_view->view;
185185

186+
wl_list_remove(&xdg_shell_view->commit.link);
187+
186188
view_unmap(view);
187189
}
188190

191+
static void
192+
handle_xdg_shell_surface_commit(struct wl_listener *listener, void *data)
193+
{
194+
struct cg_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, commit);
195+
struct cg_view *view = &xdg_shell_view->view;
196+
197+
// Check if view dimensions have changed
198+
int width, height;
199+
get_geometry(view, &width, &height);
200+
if (width != view->width || height != view->height) {
201+
view_position(view);
202+
}
203+
}
204+
189205
static void
190206
handle_xdg_shell_surface_map(struct wl_listener *listener, void *data)
191207
{
192208
struct cg_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, map);
193209
struct cg_view *view = &xdg_shell_view->view;
194210

211+
xdg_shell_view->commit.notify = handle_xdg_shell_surface_commit;
212+
wl_signal_add(&xdg_shell_view->xdg_toplevel->base->surface->events.commit, &xdg_shell_view->commit);
213+
195214
view_map(view, xdg_shell_view->xdg_toplevel->base->surface);
196215
}
197216

xdg_shell.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct cg_xdg_shell_view {
1515
struct wl_listener unmap;
1616
struct wl_listener map;
1717
struct wl_listener request_fullscreen;
18+
struct wl_listener commit;
1819
};
1920

2021
struct cg_xdg_decoration {

0 commit comments

Comments
 (0)