This commit is contained in:
Jonathan GUILLOT 2024-09-07 22:53:07 +02:00 committed by GitHub
commit 2d535d80b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 49 additions and 43 deletions

47
view.c
View file

@ -52,36 +52,11 @@ view_activate(struct cg_view *view, bool activate)
view->impl->activate(view, activate); view->impl->activate(view, activate);
} }
static bool static inline bool
view_extends_output_layout(struct cg_view *view, struct wlr_box *layout_box) view_extends_output_layout(struct cg_view *view, struct wlr_box *layout_box)
{ {
int width, height; // View width and height are expected to be set before calling this function
view->impl->get_geometry(view, &width, &height); return (layout_box->height < view->height || layout_box->width < view->width);
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);
} }
void void
@ -90,10 +65,20 @@ view_position(struct cg_view *view)
struct wlr_box layout_box; struct wlr_box layout_box;
wlr_output_layout_get_box(view->server->output_layout, NULL, &layout_box); wlr_output_layout_get_box(view->server->output_layout, NULL, &layout_box);
struct wlr_box view_box;
view->impl->get_geometry(view, &view_box);
// If view dimensions are not the same as output layout ones, it will be centered first
// Do not forget to adjust position according to top left corner declared in view geometry
view->lx = layout_box.x + (layout_box.width - view_box.width) / 2 - view_box.x;
view->ly = layout_box.y + (layout_box.height - view_box.height) / 2 - view_box.y;
view->width = view_box.width;
view->height = view_box.height;
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)) { if (view_is_primary(view) || view_extends_output_layout(view, &layout_box)) {
view_maximize(view, &layout_box); view->impl->maximize(view, layout_box.width, layout_box.height);
} else {
view_center(view, &layout_box);
} }
} }

3
view.h
View file

@ -29,6 +29,7 @@ struct cg_view {
/* The view has a position in layout coordinates. */ /* The view has a position in layout coordinates. */
int lx, ly; int lx, ly;
int width, height;
enum cg_view_type type; enum cg_view_type type;
const struct cg_view_impl *impl; const struct cg_view_impl *impl;
@ -36,7 +37,7 @@ struct cg_view {
struct cg_view_impl { struct cg_view_impl {
char *(*get_title)(struct cg_view *view); char *(*get_title)(struct cg_view *view);
void (*get_geometry)(struct cg_view *view, int *width_out, int *height_out); void (*get_geometry)(struct cg_view *view, struct wlr_box *view_box);
bool (*is_primary)(struct cg_view *view); bool (*is_primary)(struct cg_view *view);
bool (*is_transient_for)(struct cg_view *child, struct cg_view *parent); bool (*is_transient_for)(struct cg_view *child, struct cg_view *parent);
void (*activate)(struct cg_view *view, bool activate); void (*activate)(struct cg_view *view, bool activate);

View file

@ -101,14 +101,10 @@ get_title(struct cg_view *view)
} }
static void static void
get_geometry(struct cg_view *view, int *width_out, int *height_out) get_geometry(struct cg_view *view, struct wlr_box *view_box)
{ {
struct cg_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view); struct cg_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view);
struct wlr_box geom; wlr_xdg_surface_get_geometry(xdg_shell_view->xdg_toplevel->base, view_box);
wlr_xdg_surface_get_geometry(xdg_shell_view->xdg_toplevel->base, &geom);
*width_out = geom.width;
*height_out = geom.height;
} }
static bool static bool
@ -183,15 +179,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_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, unmap);
struct cg_view *view = &xdg_shell_view->view; struct cg_view *view = &xdg_shell_view->view;
wl_list_remove(&xdg_shell_view->commit.link);
view_unmap(view); 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
struct wlr_box view_box;
get_geometry(view, &view_box);
if (view_box.width != view->width || view_box.height != view->height) {
view_position(view);
}
}
static void static void
handle_xdg_shell_surface_map(struct wl_listener *listener, void *data) 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_xdg_shell_view *xdg_shell_view = wl_container_of(listener, xdg_shell_view, map);
struct cg_view *view = &xdg_shell_view->view; 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); view_map(view, xdg_shell_view->xdg_toplevel->base->surface);
} }

View file

@ -15,6 +15,7 @@ struct cg_xdg_shell_view {
struct wl_listener unmap; struct wl_listener unmap;
struct wl_listener map; struct wl_listener map;
struct wl_listener request_fullscreen; struct wl_listener request_fullscreen;
struct wl_listener commit;
}; };
struct cg_xdg_decoration { struct cg_xdg_decoration {

View file

@ -38,18 +38,22 @@ get_title(struct cg_view *view)
} }
static void static void
get_geometry(struct cg_view *view, int *width_out, int *height_out) get_geometry(struct cg_view *view, struct wlr_box *view_box)
{ {
struct cg_xwayland_view *xwayland_view = xwayland_view_from_view(view); struct cg_xwayland_view *xwayland_view = xwayland_view_from_view(view);
struct wlr_xwayland_surface *xsurface = xwayland_view->xwayland_surface; struct wlr_xwayland_surface *xsurface = xwayland_view->xwayland_surface;
view_box->x = 0;
view_box->y = 0;
if (xsurface->surface == NULL) { if (xsurface->surface == NULL) {
*width_out = 0; view_box->width = 0;
*height_out = 0; view_box->height = 0;
return; return;
} }
*width_out = xsurface->surface->current.width; view_box->width = xwayland_view->xwayland_surface->surface->current.width;
*height_out = xsurface->surface->current.height; view_box->height = xwayland_view->xwayland_surface->surface->current.height;
} }
static bool static bool