Add gdk_surface_get_scale

Add a scale property to GdkSurface and use the
fractional scale for it on Wayland.
This commit is contained in:
Matthias Clasen 2023-04-01 14:51:11 -04:00
parent d10e228ae0
commit 075bea788b
8 changed files with 78 additions and 48 deletions

View File

@ -407,17 +407,10 @@ gdk_broadway_surface_hide (GdkSurface *surface)
_gdk_surface_clear_update_area (surface);
}
static int
gdk_broadway_surface_get_scale_factor (GdkSurface *surface)
static double
gdk_broadway_surface_get_scale (GdkSurface *surface)
{
GdkBroadwayDisplay *broadway_display;
if (GDK_SURFACE_DESTROYED (surface))
return 1;
broadway_display = GDK_BROADWAY_DISPLAY (gdk_surface_get_display (surface));
return broadway_display->scale_factor;
return GDK_BROADWAY_DISPLAY (gdk_surface_get_display (surface))->scale_factor;
}
static void
@ -1271,7 +1264,7 @@ gdk_broadway_surface_class_init (GdkBroadwaySurfaceClass *klass)
impl_class->beep = gdk_broadway_surface_beep;
impl_class->destroy_notify = gdk_broadway_surface_destroy_notify;
impl_class->drag_begin = _gdk_broadway_surface_drag_begin;
impl_class->get_scale_factor = gdk_broadway_surface_get_scale_factor;
impl_class->get_scale = gdk_broadway_surface_get_scale;
}
#define LAST_PROP 1

View File

@ -95,6 +95,7 @@ enum {
PROP_WIDTH,
PROP_HEIGHT,
PROP_SCALE_FACTOR,
PROP_SCALE,
LAST_PROP
};
@ -489,6 +490,12 @@ gdk_surface_init (GdkSurface *surface)
NULL, g_object_unref);
}
static double
gdk_surface_real_get_scale (GdkSurface *surface)
{
return 1.0;
}
static void
gdk_surface_class_init (GdkSurfaceClass *klass)
{
@ -499,6 +506,7 @@ gdk_surface_class_init (GdkSurfaceClass *klass)
object_class->get_property = gdk_surface_get_property;
klass->beep = gdk_surface_real_beep;
klass->get_scale = gdk_surface_real_get_scale;
/**
* GdkSurface:cursor: (attributes org.gtk.Property.get=gdk_surface_get_cursor org.gtk.Property.set=gdk_surface_set_cursor)
@ -570,6 +578,18 @@ gdk_surface_class_init (GdkSurfaceClass *klass)
1, G_MAXINT, 1,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/**
* GdkSurface:scale: (attributes org.gtk.Property.get=gdk_surface_get_scale)
*
* The scale of the surface.
*
* Since: 4.12
*/
properties[PROP_SCALE] =
g_param_spec_double ("scale", NULL, NULL,
1., G_MAXDOUBLE, 1.,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, LAST_PROP, properties);
/**
@ -800,6 +820,10 @@ gdk_surface_get_property (GObject *object,
g_value_set_int (value, gdk_surface_get_scale_factor (surface));
break;
case PROP_SCALE:
g_value_set_double (value, gdk_surface_get_scale (surface));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -2360,10 +2384,10 @@ _gdk_windowing_got_event (GdkDisplay *display,
* with it.
*/
cairo_surface_t *
gdk_surface_create_similar_surface (GdkSurface * surface,
cairo_content_t content,
int width,
int height)
gdk_surface_create_similar_surface (GdkSurface *surface,
cairo_content_t content,
int width,
int height)
{
cairo_surface_t *similar_surface;
int scale;
@ -2596,25 +2620,40 @@ gdk_surface_get_frame_clock (GdkSurface *surface)
* pixel-based data the scale value can be used to determine whether to
* use a pixel resource with higher resolution data.
*
* The scale of a surface may change during runtime.
* The scale may change during the lifetime of the surface.
*
* Returns: the scale factor
*/
int
gdk_surface_get_scale_factor (GdkSurface *surface)
{
GdkSurfaceClass *class;
g_return_val_if_fail (GDK_IS_SURFACE (surface), 1);
return (int) ceil (gdk_surface_get_scale (surface));
}
/**
* gdk_surface_get_scale: (attributes org.gtk.Method.get_property=scale)
* @surface: surface to get scale for
*
* Returns the internal scale that maps from surface coordinates
* to the actual device pixels.
*
* The scale may change during the lifetime of the surface.
*
* Returns: the scale
*
* Since: 4.12
*/
double
gdk_surface_get_scale (GdkSurface *surface)
{
g_return_val_if_fail (GDK_IS_SURFACE (surface), 1.);
if (GDK_SURFACE_DESTROYED (surface))
return 1;
return 1.;
class = GDK_SURFACE_GET_CLASS (surface);
if (class->get_scale_factor)
return class->get_scale_factor (surface);
return 1;
return GDK_SURFACE_GET_CLASS (surface)->get_scale (surface);
}
/**

View File

@ -97,6 +97,9 @@ gboolean gdk_surface_translate_coordinates (GdkSurface *from,
GDK_AVAILABLE_IN_ALL
int gdk_surface_get_scale_factor (GdkSurface *surface);
GDK_AVAILABLE_IN_4_12
double gdk_surface_get_scale (GdkSurface *surface);
GDK_AVAILABLE_IN_ALL
gboolean gdk_surface_get_device_position (GdkSurface *surface,
GdkDevice *device,

View File

@ -154,7 +154,7 @@ struct _GdkSurfaceClass
double dx,
double dy);
int (* get_scale_factor) (GdkSurface *surface);
double (* get_scale) (GdkSurface *surface);
void (* set_opaque_region) (GdkSurface *surface,
cairo_region_t *region);

View File

@ -234,8 +234,8 @@ gdk_macos_surface_hide (GdkSurface *surface)
}
}
static int
gdk_macos_surface_get_scale_factor (GdkSurface *surface)
static double
gdk_macos_surface_get_scale (GdkSurface *surface)
{
GdkMacosSurface *self = (GdkMacosSurface *)surface;
@ -585,7 +585,7 @@ gdk_macos_surface_class_init (GdkMacosSurfaceClass *klass)
surface_class->get_device_state = gdk_macos_surface_get_device_state;
surface_class->get_geometry = gdk_macos_surface_get_geometry;
surface_class->get_root_coords = gdk_macos_surface_get_root_coords;
surface_class->get_scale_factor = gdk_macos_surface_get_scale_factor;
surface_class->get_scale = gdk_macos_surface_get_scale;
surface_class->hide = gdk_macos_surface_hide;
surface_class->set_input_region = gdk_macos_surface_set_input_region;
surface_class->set_opaque_region = gdk_macos_surface_set_opaque_region;

View File

@ -269,7 +269,10 @@ gdk_wayland_surface_update_size (GdkSurface *surface,
if (height_changed)
g_object_notify (G_OBJECT (surface), "height");
if (scale_changed)
g_object_notify (G_OBJECT (surface), "scale-factor");
{
g_object_notify (G_OBJECT (surface), "scale-factor");
g_object_notify (G_OBJECT (surface), "scale");
}
_gdk_surface_update_size (surface);
}
@ -1253,15 +1256,12 @@ gdk_wayland_surface_destroy_notify (GdkSurface *surface)
g_object_unref (surface);
}
static int
gdk_wayland_surface_get_scale_factor (GdkSurface *surface)
static double
gdk_wayland_surface_get_scale (GdkSurface *surface)
{
GdkWaylandSurface *impl = GDK_WAYLAND_SURFACE (surface);
if (GDK_SURFACE_DESTROYED (surface))
return 1;
return gdk_fractional_scale_to_int (&impl->scale);
return gdk_fractional_scale_to_double (&impl->scale);
}
static void
@ -1313,7 +1313,7 @@ gdk_wayland_surface_class_init (GdkWaylandSurfaceClass *klass)
surface_class->destroy_notify = gdk_wayland_surface_destroy_notify;
surface_class->drag_begin = _gdk_wayland_surface_drag_begin;
surface_class->get_scale_factor = gdk_wayland_surface_get_scale_factor;
surface_class->get_scale = gdk_wayland_surface_get_scale;
surface_class->set_opaque_region = gdk_wayland_surface_set_opaque_region;
surface_class->request_layout = gdk_wayland_surface_request_layout;

View File

@ -4445,16 +4445,13 @@ gdk_win32_surface_set_shadow_width (GdkSurface *window,
}
int
_gdk_win32_surface_get_scale_factor (GdkSurface *surface)
double
_gdk_win32_surface_get_scale (GdkSurface *surface)
{
GdkDisplay *display;
GdkWin32Surface *impl;
GdkWin32Display *win32_display;
if (GDK_SURFACE_DESTROYED (surface))
return 1;
g_return_val_if_fail (surface != NULL, 1);
display = gdk_surface_get_display (surface);
@ -4654,7 +4651,7 @@ gdk_win32_surface_class_init (GdkWin32SurfaceClass *klass)
impl_class->destroy_notify = gdk_win32_surface_destroy_notify;
impl_class->drag_begin = _gdk_win32_surface_drag_begin;
impl_class->get_scale_factor = _gdk_win32_surface_get_scale_factor;
impl_class->get_scale = _gdk_win32_surface_get_scale;
impl_class->request_layout = _gdk_win32_surface_request_layout;
impl_class->compute_size = _gdk_win32_surface_compute_size;
}

View File

@ -2092,6 +2092,7 @@ _gdk_x11_surface_set_surface_scale (GdkSurface *surface,
gdk_surface_invalidate_rect (surface, NULL);
g_object_notify (G_OBJECT (surface), "scale-factor");
g_object_notify (G_OBJECT (surface), "scale");
}
void
@ -4741,14 +4742,11 @@ gdk_x11_surface_get_xid (GdkSurface *surface)
return GDK_X11_SURFACE (surface)->xid;
}
static int
gdk_x11_surface_get_scale_factor (GdkSurface *surface)
static double
gdk_x11_surface_get_scale (GdkSurface *surface)
{
GdkX11Surface *impl = GDK_X11_SURFACE (surface);
if (GDK_SURFACE_DESTROYED (surface))
return 1;
return impl->surface_scale;
}
@ -4888,7 +4886,7 @@ gdk_x11_surface_class_init (GdkX11SurfaceClass *klass)
impl_class->destroy_notify = gdk_x11_surface_destroy_notify;
impl_class->drag_begin = _gdk_x11_surface_drag_begin;
impl_class->get_scale_factor = gdk_x11_surface_get_scale_factor;
impl_class->get_scale = gdk_x11_surface_get_scale;
impl_class->set_opaque_region = gdk_x11_surface_set_opaque_region;
impl_class->request_layout = gdk_x11_surface_request_layout;
impl_class->compute_size = gdk_x11_surface_compute_size;