Commit ca71119a authored by Jonas Ådahl's avatar Jonas Ådahl
Browse files

gdk/surface: Replace move_to_rect() with GdkPopupLayout based API

Replace the gdk_surface_move_to_rect() API with a new GdkSurface
method called gdk_surface_present_popup() taking a new GdkPopupLayout
object describing how they should be laid out on screen.

The layout properties provided are the same as the ones used with
gdk_surface_move_to_rect(), except they are now set up using
GdkPopupLayout.

Calling gdk_surface_present_popup() will either show the popup at the
position described using the popup layout object and a new unconstrained
size, or reposition it accordingly.

In some situations, such as when a popup is set to autohide, presenting
may immediately fail, in case the grab was not granted by the display
server.

After a successful present, the result of the layout can be queried
using the following methods:

 * gdk_surface_get_position() - to get the position relative to its
   parent
 * gdk_surface_get_width() - to get the current width
 * gdk_surface_get_height() - to get the current height
 * gdk_surface_get_rect_anchor() - to get the anchor point on the anchor
   rectangle the popup was effectively positioned against given
   constraints defined by the environment and the layout rules provided
   via GdkPopupLayout.
 * gdk_surface_get_surface_anchor() - the same as the one above but for
   the surface anchor.

A new signal replaces the old "moved-to-rect" one -
"popup-layout-changed". However, it is only intended to be emitted when
the layout changes implicitly by the windowing system, for example if
the monitor resolution changed, or the parent window moved.
parent 37f4c644
......@@ -205,7 +205,9 @@ gdk_surface_set_keep_above
gdk_surface_set_keep_below
gdk_surface_set_opacity
gdk_surface_resize
gdk_surface_move_to_rect
gdk_surface_present_popup
gdk_surface_get_popup_rect_anchor
gdk_surface_get_popup_surface_anchor
gdk_surface_raise
gdk_surface_lower
gdk_surface_restack
......
......@@ -455,18 +455,21 @@ gdk_broadway_surface_move (GdkSurface *surface,
}
static void
gdk_broadway_surface_moved_to_rect (GdkSurface *surface,
GdkRectangle final_rect)
gdk_broadway_surface_layout_popup (GdkSurface *surface,
int width,
int height,
GdkPopupLayout *layout)
{
GdkSurface *toplevel;
GdkRectangle final_rect;
int x, y;
if (surface->surface_type == GDK_SURFACE_POPUP)
toplevel = surface->parent;
else
toplevel = surface->transient_for;
gdk_surface_layout_popup_helper (surface,
width,
height,
layout,
&final_rect);
gdk_surface_get_origin (toplevel, &x, &y);
gdk_surface_get_origin (surface->parent, &x, &y);
x += final_rect.x;
y += final_rect.y;
......@@ -474,8 +477,10 @@ gdk_broadway_surface_moved_to_rect (GdkSurface *surface,
final_rect.height != surface->height)
{
gdk_broadway_surface_move_resize (surface,
x, y,
final_rect.width, final_rect.height);
x,
y,
final_rect.width,
final_rect.height);
}
else
{
......@@ -484,22 +489,49 @@ gdk_broadway_surface_moved_to_rect (GdkSurface *surface,
}
static void
gdk_broadway_surface_move_to_rect (GdkSurface *surface,
const GdkRectangle *rect,
GdkGravity rect_anchor,
GdkGravity surface_anchor,
GdkAnchorHints anchor_hints,
gint rect_anchor_dx,
gint rect_anchor_dy)
{
gdk_surface_move_to_rect_helper (surface,
rect,
rect_anchor,
surface_anchor,
anchor_hints,
rect_anchor_dx,
rect_anchor_dy,
gdk_broadway_surface_moved_to_rect);
show_popup (GdkSurface *surface)
{
gdk_surface_raise (surface);
gdk_synthesize_surface_state (surface, GDK_SURFACE_STATE_WITHDRAWN, 0);
_gdk_surface_update_viewable (surface);
gdk_broadway_surface_show (surface, FALSE);
gdk_surface_invalidate_rect (surface, NULL);
}
static void
show_grabbing_popup (GdkSeat *seat,
GdkSurface *surface,
gpointer user_data)
{
show_popup (surface);
}
static gboolean
gdk_broadway_surface_present_popup (GdkSurface *surface,
int width,
int height,
GdkPopupLayout *layout)
{
gdk_broadway_surface_layout_popup (surface, width, height, layout);
if (GDK_SURFACE_IS_MAPPED (surface))
return TRUE;
if (surface->autohide)
{
gdk_seat_grab (gdk_display_get_default_seat (surface->display),
surface,
GDK_SEAT_CAPABILITY_ALL,
TRUE,
NULL, NULL,
show_grabbing_popup, NULL);
}
else
{
show_popup (surface);
}
return GDK_SURFACE_IS_MAPPED (surface);
}
static void
......@@ -1393,7 +1425,7 @@ gdk_broadway_surface_class_init (GdkBroadwaySurfaceClass *klass)
impl_class->lower = gdk_broadway_surface_lower;
impl_class->restack_toplevel = gdk_broadway_surface_restack_toplevel;
impl_class->toplevel_resize = gdk_broadway_surface_toplevel_resize;
impl_class->move_to_rect = gdk_broadway_surface_move_to_rect;
impl_class->present_popup = gdk_broadway_surface_present_popup;
impl_class->get_geometry = gdk_broadway_surface_get_geometry;
impl_class->get_root_coords = gdk_broadway_surface_get_root_coords;
impl_class->get_device_state = gdk_broadway_surface_get_device_state;
......
......@@ -35,6 +35,7 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(GdkGLContext, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GdkKeymap, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GdkMonitor, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GdkSeat, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GdkPopupLayout, gdk_popup_layout_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GdkVulkanContext, g_object_unref)
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GdkSurface, g_object_unref)
......
/* GDK - The GIMP Drawing Kit
* Copyright (C) 2020 Red Hat
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include "gdkpopuplayout.h"
#include "gdksurface.h"
struct _GdkPopupLayout
{
/* < private >*/
grefcount ref_count;
GdkRectangle anchor_rect;
GdkGravity rect_anchor;
GdkGravity surface_anchor;
GdkAnchorHints anchor_hints;
int dx;
int dy;
gboolean is_sealed;
};
G_DEFINE_BOXED_TYPE (GdkPopupLayout, gdk_popup_layout,
gdk_popup_layout_ref,
gdk_popup_layout_unref)
/**
* gdk_popup_layout_new: (constructor)
* @anchor_rect: (not nullable): the anchor #GdkRectangle to align @surface with
* @rect_anchor: the point on @anchor_rect to align with @surface's anchor point
* @surface_anchor: the point on @surface to align with @rect's anchor point
*
* Create a popup layout description. Used together with
* gdk_surface_present_popup() to describe how a popup surface should be placed
* and behave on-screen.
*
* @anchor_rect is relative to the top-left corner of the surface's parent.
* @rect_anchor and @surface_anchor determine anchor points on @anchor_rect and
* surface to pin together.
*
* The position of @anchor_rect's anchor point can optionally be offset using
* gdk_popup_layout_set_offset(), which is equivalent to offsetting the
* position of surface.
*
* Returns: (transfer full): newly created instance of #GdkPopupLayout
*/
GdkPopupLayout *
gdk_popup_layout_new (const GdkRectangle *anchor_rect,
GdkGravity rect_anchor,
GdkGravity surface_anchor)
{
GdkPopupLayout *layout;
layout = g_new0 (GdkPopupLayout, 1);
g_ref_count_init (&layout->ref_count);
layout->anchor_rect = *anchor_rect;
layout->rect_anchor = rect_anchor;
layout->surface_anchor = surface_anchor;
return layout;
}
/**
* gdk_popup_layout_ref:
* @layout: a #GdkPopupLayout
*
* Increases the reference count of @value.
*
* Returns: the same @layout
*/
GdkPopupLayout *
gdk_popup_layout_ref (GdkPopupLayout *layout)
{
g_ref_count_inc (&layout->ref_count);
return layout;
}
/**
* gdk_popup_layout_unref:
* @layout: a #GdkPopupLayout
*
* Decreases the reference count of @value.
*/
void
gdk_popup_layout_unref (GdkPopupLayout *layout)
{
if (g_ref_count_dec (&layout->ref_count))
g_free (layout);
}
/**
* gdk_popup_layout_copy:
* @layout: a #GdkPopupLayout
*
* Create a new #GdkPopupLayout and copy the contents of @layout into it.
*
* Returns: (transfer full): a copy of @layout.
*/
GdkPopupLayout *
gdk_popup_layout_copy (GdkPopupLayout *layout)
{
GdkPopupLayout *new_layout;
new_layout = g_new0 (GdkPopupLayout, 1);
g_ref_count_init (&new_layout->ref_count);
new_layout->anchor_rect = layout->anchor_rect;
new_layout->rect_anchor = layout->rect_anchor;
new_layout->surface_anchor = layout->surface_anchor;
new_layout->anchor_hints = layout->anchor_hints;
new_layout->dx = layout->dx;
new_layout->dy = layout->dy;
return new_layout;
}
/**
* gdk_popup_layout_set_anchor_rect:
* @layout: a #GdkPopupLayout
* @anchor_rect: the new anchor rectangle
*
* Set the anchor rectangle.
*/
void
gdk_popup_layout_set_anchor_rect (GdkPopupLayout *layout,
const GdkRectangle *anchor_rect)
{
layout->anchor_rect = *anchor_rect;
}
/**
* gdk_popup_layout_get_anchor_rect:
* @layout: a #GdkPopupLayout
*
* Get the anchor rectangle.
*
* Returns: The anchor rectangle.
*/
const GdkRectangle *
gdk_popup_layout_get_anchor_rect (GdkPopupLayout *layout)
{
return &layout->anchor_rect;
}
/**
* gdk_popup_layout_set_rect_anchor:
* @layout: a #GdkPopupLayout
* @anchor: the new rect anchor
*
* Set the anchor on the anchor rectangle.
*/
void
gdk_popup_layout_set_rect_anchor (GdkPopupLayout *layout,
GdkGravity anchor)
{
layout->rect_anchor = anchor;
}
/**
* gdk_popup_layout_get_rect_anchor:
* @layout: a #GdkPopupLayout
*
* Returns: the anchor on the anchor rectangle.
*/
GdkGravity
gdk_popup_layout_get_rect_anchor (GdkPopupLayout *layout)
{
return layout->rect_anchor;
}
/**
* gdk_popup_layout_set_surface_anchor:
* @layout: a #GdkPopupLayout
* @anchor: the new popup surface anchor
*
* Set the anchor on the popup surface.
*/
void
gdk_popup_layout_set_surface_anchor (GdkPopupLayout *layout,
GdkGravity anchor)
{
layout->surface_anchor = anchor;
}
/**
* gdk_popup_layout_get_surface_anchor:
* @layout: a #GdkPopupLayout
*
* Returns: the anchor on the popup surface.
*/
GdkGravity
gdk_popup_layout_get_surface_anchor (GdkPopupLayout *layout)
{
return layout->surface_anchor;
}
/**
* gdk_popup_layout_set_anchor_hints:
* @layout: a #GdkPopupLayout
* @anchor_hints: the new #GdkAnchorHints
*
* Set new anchor hints.
*
* The set @anchor_hints determines how @surface will be moved if the anchor
* points cause it to move off-screen. For example, %GDK_ANCHOR_FLIP_X will
* replace %GDK_GRAVITY_NORTH_WEST with %GDK_GRAVITY_NORTH_EAST and vice versa
* if @surface extends beyond the left or right edges of the monitor.
*/
void
gdk_popup_layout_set_anchor_hints (GdkPopupLayout *layout,
GdkAnchorHints anchor_hints)
{
layout->anchor_hints = anchor_hints;
}
/**
* gdk_popup_layout_get_anchor_hints:
* @layout: a #GdkPopupLayout
*
* Get the #GdkAnchorHints.
*
* Returns: the #GdkAnchorHints.
*/
GdkAnchorHints
gdk_popup_layout_get_anchor_hints (GdkPopupLayout *layout)
{
return layout->anchor_hints;
}
/**
* gdk_popup_layout_set_offset:
* @layout: a #GdkPopupLayout
* @dx: x delta to offset the anchor rectangle with
* @dy: y delta to offset the anchor rectangle with
*
* Offset the position of the anchor rectangle with the given delta.
*/
void
gdk_popup_layout_set_offset (GdkPopupLayout *layout,
int dx,
int dy)
{
layout->dx = dx;
layout->dy = dy;
}
/**
* gdk_popup_layout_get_offset:
* @layout: a #GdkPopupLayout
* @dx: a pointer to where to store the delta x coordinate
* @dy: a pointer to where to store the delta y coordinate
*
* Get the delta the anchor rectangle is offset with
*/
void
gdk_popup_layout_get_offset (GdkPopupLayout *layout,
int *dx,
int *dy)
{
if (dx)
*dx = layout->dx;
if (dy)
*dy = layout->dy;
}
/* GDK - The GIMP Drawing Kit
* Copyright (C) 2020 Red Hat
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __GDK_POPUP_LAYOUT_H__
#define __GDK_POPUP_LAYOUT_H__
#if !defined (__GDK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gdk/gdk.h> can be included directly."
#endif
#include <gdk/gdkversionmacros.h>
#include <gdk/gdktypes.h>
G_BEGIN_DECLS
/**
* GdkAnchorHints:
* @GDK_ANCHOR_FLIP_X: allow flipping anchors horizontally
* @GDK_ANCHOR_FLIP_Y: allow flipping anchors vertically
* @GDK_ANCHOR_SLIDE_X: allow sliding surface horizontally
* @GDK_ANCHOR_SLIDE_Y: allow sliding surface vertically
* @GDK_ANCHOR_RESIZE_X: allow resizing surface horizontally
* @GDK_ANCHOR_RESIZE_Y: allow resizing surface vertically
* @GDK_ANCHOR_FLIP: allow flipping anchors on both axes
* @GDK_ANCHOR_SLIDE: allow sliding surface on both axes
* @GDK_ANCHOR_RESIZE: allow resizing surface on both axes
*
* Positioning hints for aligning a surface relative to a rectangle.
*
* These hints determine how the surface should be positioned in the case that
* the surface would fall off-screen if placed in its ideal position.
*
* For example, %GDK_ANCHOR_FLIP_X will replace %GDK_GRAVITY_NORTH_WEST with
* %GDK_GRAVITY_NORTH_EAST and vice versa if the surface extends beyond the left
* or right edges of the monitor.
*
* If %GDK_ANCHOR_SLIDE_X is set, the surface can be shifted horizontally to fit
* on-screen. If %GDK_ANCHOR_RESIZE_X is set, the surface can be shrunken
* horizontally to fit.
*
* In general, when multiple flags are set, flipping should take precedence over
* sliding, which should take precedence over resizing.
*/
typedef enum
{
GDK_ANCHOR_FLIP_X = 1 << 0,
GDK_ANCHOR_FLIP_Y = 1 << 1,
GDK_ANCHOR_SLIDE_X = 1 << 2,
GDK_ANCHOR_SLIDE_Y = 1 << 3,
GDK_ANCHOR_RESIZE_X = 1 << 4,
GDK_ANCHOR_RESIZE_Y = 1 << 5,
GDK_ANCHOR_FLIP = GDK_ANCHOR_FLIP_X | GDK_ANCHOR_FLIP_Y,
GDK_ANCHOR_SLIDE = GDK_ANCHOR_SLIDE_X | GDK_ANCHOR_SLIDE_Y,
GDK_ANCHOR_RESIZE = GDK_ANCHOR_RESIZE_X | GDK_ANCHOR_RESIZE_Y,
} GdkAnchorHints;
/**
* GdkPopupLayout:
*/
typedef struct _GdkPopupLayout GdkPopupLayout;
#define GDK_TYPE_POPUP_LAYOUT (gdk_popup_layout_get_type ())
GDK_AVAILABLE_IN_ALL
GType gdk_popup_layout_get_type (void);
GDK_AVAILABLE_IN_ALL
GdkPopupLayout * gdk_popup_layout_new (const GdkRectangle *anchor_rect,
GdkGravity rect_anchor,
GdkGravity surface_anchor);
GDK_AVAILABLE_IN_ALL
GdkPopupLayout * gdk_popup_layout_ref (GdkPopupLayout *layout);
GDK_AVAILABLE_IN_ALL
void gdk_popup_layout_unref (GdkPopupLayout *layout);
GDK_AVAILABLE_IN_ALL
GdkPopupLayout * gdk_popup_layout_copy (GdkPopupLayout *layout);
GDK_AVAILABLE_IN_ALL
void gdk_popup_layout_set_anchor_rect (GdkPopupLayout *layout,
const GdkRectangle *anchor_rect);
GDK_AVAILABLE_IN_ALL
const GdkRectangle * gdk_popup_layout_get_anchor_rect (GdkPopupLayout *layout);
GDK_AVAILABLE_IN_ALL
void gdk_popup_layout_set_rect_anchor (GdkPopupLayout *layout,
GdkGravity anchor);
GDK_AVAILABLE_IN_ALL
GdkGravity gdk_popup_layout_get_rect_anchor (GdkPopupLayout *layout);
GDK_AVAILABLE_IN_ALL
void gdk_popup_layout_set_surface_anchor (GdkPopupLayout *layout,
GdkGravity anchor);
GDK_AVAILABLE_IN_ALL
GdkGravity gdk_popup_layout_get_surface_anchor (GdkPopupLayout *layout);
GDK_AVAILABLE_IN_ALL
void gdk_popup_layout_set_anchor_hints (GdkPopupLayout *layout,
GdkAnchorHints anchor_hints);
GDK_AVAILABLE_IN_ALL
GdkAnchorHints gdk_popup_layout_get_anchor_hints (GdkPopupLayout *layout);
GDK_AVAILABLE_IN_ALL
void gdk_popup_layout_set_offset (GdkPopupLayout *layout,
int dx,
int dy);
GDK_AVAILABLE_IN_ALL
void gdk_popup_layout_get_offset (GdkPopupLayout *layout,
int *dx,
int *dy);
G_END_DECLS
#endif /* __GDK_POPUP_LAYOUT_H__ */
......@@ -69,7 +69,7 @@
*/
enum {
MOVED_TO_RECT,
POPUP_LAYOUT_CHANGED,
SIZE_CHANGED,
RENDER,
EVENT,
......@@ -247,36 +247,30 @@ maybe_flip_position (gint bounds_pos,
}
void
gdk_surface_move_to_rect_helper (GdkSurface *surface,
const GdkRectangle *rect,
GdkGravity rect_anchor,
GdkGravity surface_anchor,
GdkAnchorHints anchor_hints,
gint rect_anchor_dx,
gint rect_anchor_dy,
GdkSurfaceMovedToRect moved_to_rect)
{
GdkSurface *toplevel;
gdk_surface_layout_popup_helper (GdkSurface *surface,
int width,
int height,
GdkPopupLayout *layout,
GdkRectangle *out_final_rect)
{
GdkDisplay *display;
GdkMonitor *monitor;
GdkRectangle bounds;
GdkRectangle root_rect = *rect;
GdkRectangle flipped_rect;
GdkRectangle root_rect;
GdkGravity rect_anchor;
GdkGravity surface_anchor;
int rect_anchor_dx;
int rect_anchor_dy;
GdkAnchorHints anchor_hints;
GdkRectangle final_rect;
gboolean flipped_x;
gboolean flipped_y;
int x, y;
/* This implementation only works for backends that
* can provide root coordinates via get_root_coords.
* Other backends need to implement move_to_rect.
*/
if (surface->surface_type == GDK_SURFACE_POPUP)
toplevel = surface->parent;
else
toplevel = surface->transient_for;
g_return_if_fail (surface->surface_type == GDK_SURFACE_POPUP);
gdk_surface_get_root_coords (toplevel,
root_rect = *gdk_popup_layout_get_anchor_rect (layout);
gdk_surface_get_root_coords (surface->parent,
root_rect.x,
root_rect.y,
&root_rect.x,
......@@ -286,30 +280,33 @@ gdk_surface_move_to_rect_helper (GdkSurface *surface,
monitor = get_monitor_for_rect (display, &root_rect);
gdk_monitor_get_workarea (monitor, &bounds);
flipped_rect.width = surface->width - surface->shadow_left - surface->shadow_right;
flipped_rect.height = surface->height - surface->shadow_top - surface->shadow_bottom;
flipped_rect.x = maybe_flip_position (bounds.x,
bounds.width,
root_rect.x,
root_rect.width,
flipped_rect.width,
get_anchor_x_sign (rect_anchor),
get_anchor_x_sign (surface_anchor),
rect_anchor_dx,
anchor_hints & GDK_ANCHOR_FLIP_X,
&flipped_x);
flipped_rect.y = maybe_flip_position (bounds.y,