labwc/src/xdg-popup.c

168 lines
4.9 KiB
C
Raw Normal View History

2021-09-24 21:45:48 +01:00
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2020 the sway authors
2021-03-03 20:51:19 +00:00
*
* This file is only needed in support of
2021-08-25 20:45:39 +01:00
* - unconstraining XDG popups
* - keeping non-layer-shell xdg-popups outside the layers.c code
*/
2024-10-06 19:51:59 +01:00
#include "common/macros.h"
#include "common/mem.h"
#include "labwc.h"
#include "node.h"
2025-07-26 15:34:45 -04:00
#include "output.h"
#include "view.h"
struct xdg_popup {
struct view *parent_view;
struct wlr_xdg_popup *wlr_popup;
struct wl_listener commit;
struct wl_listener destroy;
struct wl_listener new_popup;
struct wl_listener reposition;
};
static void
popup_unconstrain(struct xdg_popup *popup)
{
struct view *view = popup->parent_view;
2021-03-02 20:51:32 +00:00
struct server *server = view->server;
/* Get position of parent toplevel/popup */
int parent_lx, parent_ly;
struct wlr_scene_tree *parent_tree = popup->wlr_popup->parent->data;
wlr_scene_node_coords(&parent_tree->node, &parent_lx, &parent_ly);
2024-10-06 19:51:59 +01:00
/*
* Get usable area to constrain by
*
* The scheduled top-left corner (x, y) of the popup is sometimes less
* than zero, typically with Qt apps. We therefore clamp it to avoid for
* example the 'File' menu of a maximized window to end up on an another
* output.
*/
struct wlr_box *popup_box = &popup->wlr_popup->scheduled.geometry;
struct output *output = output_nearest_to(server,
2024-10-06 19:51:59 +01:00
parent_lx + MAX(popup_box->x, 0),
parent_ly + MAX(popup_box->y, 0));
struct wlr_box usable = output_usable_area_in_layout_coords(output);
2021-03-02 20:51:32 +00:00
/* Get offset of toplevel window from its surface */
int toplevel_dx = 0;
int toplevel_dy = 0;
struct wlr_xdg_surface *toplevel_surface = xdg_surface_from_view(view);
if (toplevel_surface) {
toplevel_dx = toplevel_surface->current.geometry.x;
toplevel_dy = toplevel_surface->current.geometry.y;
} else {
wlr_log(WLR_ERROR, "toplevel is not valid XDG surface");
}
/* Geometry of usable area relative to toplevel surface */
2021-03-02 20:51:32 +00:00
struct wlr_box output_toplevel_box = {
.x = usable.x - (view->current.x - toplevel_dx),
.y = usable.y - (view->current.y - toplevel_dy),
.width = usable.width,
.height = usable.height,
2021-03-02 20:51:32 +00:00
};
wlr_xdg_popup_unconstrain_from_box(popup->wlr_popup, &output_toplevel_box);
2021-03-02 20:51:32 +00:00
}
static void
2025-05-27 16:11:15 +09:00
handle_destroy(struct wl_listener *listener, void *data)
{
struct xdg_popup *popup = wl_container_of(listener, popup, destroy);
struct wlr_xdg_popup *_popup, *tmp;
wl_list_for_each_safe(_popup, tmp, &popup->wlr_popup->base->popups, link) {
wlr_xdg_popup_destroy(_popup);
}
wl_list_remove(&popup->destroy.link);
wl_list_remove(&popup->new_popup.link);
wl_list_remove(&popup->reposition.link);
/* Usually already removed unless there was no commit at all */
if (popup->commit.notify) {
wl_list_remove(&popup->commit.link);
}
cursor_update_focus(popup->parent_view->server);
free(popup);
}
static void
2025-05-27 16:11:15 +09:00
handle_commit(struct wl_listener *listener, void *data)
{
struct xdg_popup *popup = wl_container_of(listener, popup, commit);
if (popup->wlr_popup->base->initial_commit) {
popup_unconstrain(popup);
/* Prevent getting called over and over again */
wl_list_remove(&popup->commit.link);
popup->commit.notify = NULL;
}
}
static void
2025-05-27 16:11:15 +09:00
handle_reposition(struct wl_listener *listener, void *data)
{
struct xdg_popup *popup = wl_container_of(listener, popup, reposition);
popup_unconstrain(popup);
}
static void
2025-05-27 16:11:15 +09:00
handle_new_popup(struct wl_listener *listener, void *data)
{
struct xdg_popup *popup = wl_container_of(listener, popup, new_popup);
struct wlr_xdg_popup *wlr_popup = data;
xdg_popup_create(popup->parent_view, wlr_popup);
}
void
xdg_popup_create(struct view *view, struct wlr_xdg_popup *wlr_popup)
{
struct wlr_xdg_surface *parent =
wlr_xdg_surface_try_from_wlr_surface(wlr_popup->parent);
if (!parent) {
wlr_log(WLR_ERROR, "parent is not a valid XDG surface");
return;
}
2022-09-18 15:22:26 -04:00
struct xdg_popup *popup = znew(*popup);
popup->parent_view = view;
popup->wlr_popup = wlr_popup;
2025-05-27 16:11:15 +09:00
CONNECT_SIGNAL(wlr_popup, popup, destroy);
CONNECT_SIGNAL(wlr_popup->base, popup, new_popup);
CONNECT_SIGNAL(wlr_popup->base->surface, popup, commit);
CONNECT_SIGNAL(wlr_popup, popup, reposition);
/*
* We must add xdg popups to the scene graph so they get rendered. The
* wlroots scene graph provides a helper for this, but to use it we must
* provide the proper parent scene node of the xdg popup. To enable
* this, we always set the user data field of wlr_surfaces to the
* corresponding scene node.
2023-05-20 08:58:42 +01:00
*
* xdg-popups live in server->xdg_popup_tree so that they can be
* rendered above always-on-top windows
*/
2023-05-20 08:58:42 +01:00
struct wlr_scene_tree *parent_tree = NULL;
if (parent->role == WLR_XDG_SURFACE_ROLE_POPUP) {
parent_tree = parent->surface->data;
} else {
parent_tree = view->server->xdg_popup_tree;
wlr_scene_node_set_position(&view->server->xdg_popup_tree->node,
view->current.x, view->current.y);
}
wlr_popup->base->surface->data =
wlr_scene_xdg_surface_create(parent_tree, wlr_popup->base);
node_descriptor_create(wlr_popup->base->surface->data,
LAB_NODE_DESC_XDG_POPUP, view);
}