Skip to content

Commit

Permalink
view: unconditionally center views in output layout
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joggee-fr committed Feb 8, 2024
1 parent 8a00921 commit 2c45273
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
44 changes: 13 additions & 31 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,11 @@ view_activate(struct cg_view *view, bool activate)
view->impl->activate(view, activate);
}

static bool
static inline bool
view_extends_output_layout(struct cg_view *view, struct wlr_box *layout_box)
{
int width, height;
view->impl->get_geometry(view, &width, &height);

return (layout_box->height < height || layout_box->width < width);
}

static void
view_maximize(struct cg_view *view, struct wlr_box *layout_box)
{
view->lx = layout_box->x;
view->ly = layout_box->y;

wlr_scene_node_set_position(&view->scene_tree->node, view->lx, view->ly);

view->impl->maximize(view, layout_box->width, layout_box->height);
}

static void
view_center(struct cg_view *view, struct wlr_box *layout_box)
{
int width, height;
view->impl->get_geometry(view, &width, &height);

view->lx = (layout_box->width - width) / 2;
view->ly = (layout_box->height - height) / 2;

wlr_scene_node_set_position(&view->scene_tree->node, view->lx, view->ly);
// View width and height are expected to be set before calling this function
return (layout_box->height < view->height || layout_box->width < view->width);
}

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

view->impl->get_geometry(view, &view->width, &view->height);

// If view dimensions are not the same as output layout ones,
// it will be centered first
view->lx = layout_box.x + (layout_box.width - view->width) / 2;
view->ly = layout_box.y + (layout_box.height - view->height) / 2;

wlr_scene_node_set_position(&view->scene_tree->node, view->lx, view->ly);

if (view_is_primary(view) || view_extends_output_layout(view, &layout_box)) {
view_maximize(view, &layout_box);
} else {
view_center(view, &layout_box);
view->impl->maximize(view, layout_box.width, layout_box.height);
}
}

Expand Down
1 change: 1 addition & 0 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct cg_view {

/* The view has a position in layout coordinates. */
int lx, ly;
int width, height;

enum cg_view_type type;
const struct cg_view_impl *impl;
Expand Down
19 changes: 19 additions & 0 deletions xdg_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,34 @@ handle_xdg_shell_surface_unmap(struct wl_listener *listener, void *data)
struct cg_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, unmap);
struct cg_view *view = &xdg_shell_view->view;

wl_list_remove(&xdg_shell_view->commit.link);

view_unmap(view);
}

static void
handle_xdg_shell_surface_commit(struct wl_listener *listener, void *data)
{
struct cg_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, commit);
struct cg_view *view = &xdg_shell_view->view;

// Check if view dimensions have changed
int width, height;
get_geometry(view, &width, &height);
if (width != view->width || height != view->height) {
view_position(view);
}
}

static void
handle_xdg_shell_surface_map(struct wl_listener *listener, void *data)
{
struct cg_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, map);
struct cg_view *view = &xdg_shell_view->view;

xdg_shell_view->commit.notify = handle_xdg_shell_surface_commit;
wl_signal_add(&xdg_shell_view->xdg_toplevel->base->surface->events.commit, &xdg_shell_view->commit);

view_map(view, xdg_shell_view->xdg_toplevel->base->surface);
}

Expand Down
1 change: 1 addition & 0 deletions xdg_shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct cg_xdg_shell_view {
struct wl_listener unmap;
struct wl_listener map;
struct wl_listener request_fullscreen;
struct wl_listener commit;
};

struct cg_xdg_decoration {
Expand Down

0 comments on commit 2c45273

Please sign in to comment.