[gtk/wip/matthiasc/popup5: 108/109] Redo autohide as surface property



commit 474c9c52fedab9415d6765e52b099512644ffee5
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun May 19 22:03:43 2019 +0000

    Redo autohide as surface property

 gdk/gdksurface.c        | 85 ++++++++++++++++++++++++++-----------------------
 gdk/gdksurface.h        |  6 ++--
 gdk/gdksurfaceprivate.h |  2 +-
 3 files changed, 49 insertions(+), 44 deletions(-)
---
diff --git a/gdk/gdksurface.c b/gdk/gdksurface.c
index e999d57d9f..29e977a520 100644
--- a/gdk/gdksurface.c
+++ b/gdk/gdksurface.c
@@ -83,6 +83,7 @@ enum {
   PROP_FRAME_CLOCK,
   PROP_STATE,
   PROP_MAPPED,
+  PROP_AUTOHIDE,
   LAST_PROP
 };
 
@@ -465,6 +466,13 @@ gdk_surface_class_init (GdkSurfaceClass *klass)
                             FALSE,
                             G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
 
+  properties[PROP_AUTOHIDE] =
+      g_param_spec_boolean ("autohide",
+                            P_("Autohide"),
+                            P_("Whether to dismiss the surface on outside clicks"),
+                            FALSE,
+                            G_PARAM_READABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+
   g_object_class_install_properties (object_class, LAST_PROP, properties);
 
   /**
@@ -641,6 +649,10 @@ gdk_surface_set_property (GObject      *object,
       gdk_surface_set_frame_clock (surface, GDK_FRAME_CLOCK (g_value_get_object (value)));
       break;
 
+    case PROP_AUTOHIDE:
+      surface->autohide = g_value_get_boolean (value);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -681,6 +693,10 @@ gdk_surface_get_property (GObject    *object,
       g_value_set_boolean (value, GDK_SURFACE_IS_MAPPED (surface));
       break;
 
+    case PROP_AUTOHIDE:
+      g_value_set_boolean (value, surface->autohide);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -766,8 +782,10 @@ gdk_surface_new_temp (GdkDisplay         *display,
  * gdk_surface_new_popup: (constructor)
  * @display: the display to create the surface on
  * @parent: the parent surface to attach the surface to
+ * @autohide: whether to hide the surface on outside clicks
  *
  * Create a new popup surface.
+ *
  * The surface will be attached to @parent and can
  * be positioned relative to it using
  * gdk_surface_move_to_rect().
@@ -776,7 +794,8 @@ gdk_surface_new_temp (GdkDisplay         *display,
  */
 GdkSurface *
 gdk_surface_new_popup (GdkDisplay *display,
-                       GdkSurface *parent)
+                       GdkSurface *parent,
+                       gboolean    autohide)
 {
   GdkSurface *surface;
 
@@ -786,6 +805,8 @@ gdk_surface_new_popup (GdkDisplay *display,
   surface = gdk_surface_new (display, GDK_SURFACE_POPUP,
                              parent, 0, 0, 100, 100);
 
+  surface->autohide = autohide;
+
   return surface;
 }
 
@@ -1757,10 +1778,7 @@ gdk_surface_show_internal (GdkSurface *surface, gboolean raise)
     gdk_surface_raise_internal (surface);
 
   if (!was_mapped)
-    {
-      gdk_synthesize_surface_state (surface, GDK_SURFACE_STATE_WITHDRAWN, 0);
-      surface->auto_dismissal = FALSE;
-    }
+    gdk_synthesize_surface_state (surface, GDK_SURFACE_STATE_WITHDRAWN, 0);
 
   did_show = _gdk_surface_update_viewable (surface);
 
@@ -1887,6 +1905,13 @@ gdk_surface_restack (GdkSurface     *surface,
   GDK_SURFACE_GET_CLASS (surface)->restack_toplevel (surface, sibling, above);
 }
 
+static void
+grab_prepare_func (GdkSeat    *seat,
+                   GdkSurface *surface,
+                   gpointer    data)
+{
+  gdk_surface_show_internal (surface, TRUE);
+}
 
 /**
  * gdk_surface_show:
@@ -1905,7 +1930,19 @@ gdk_surface_restack (GdkSurface     *surface,
 void
 gdk_surface_show (GdkSurface *surface)
 {
-  gdk_surface_show_internal (surface, TRUE);
+  if (surface->autohide)
+    {
+      gdk_seat_grab (gdk_display_get_default_seat (surface->display),
+                     surface,
+                     GDK_SEAT_CAPABILITY_ALL,
+                     TRUE,
+                     NULL, NULL,
+                     grab_prepare_func, NULL);
+    }
+  else
+    {
+      gdk_surface_show_internal (surface, TRUE);
+    }
 }
 
 /**
@@ -4050,7 +4087,7 @@ gdk_synthesize_surface_state (GdkSurface     *surface,
 }
 
 static gboolean
-check_auto_dismissal (GdkEvent *event)
+check_autohide (GdkEvent *event)
 {
   GdkDisplay *display;
   GdkDevice *device;
@@ -4076,7 +4113,7 @@ check_auto_dismissal (GdkEvent *event)
       if (gdk_device_grab_info (display, device, &grab_surface, NULL))
         {
           if (grab_surface != gdk_event_get_surface (event) &&
-              grab_surface->auto_dismissal)
+              grab_surface->autohide)
             {
               gdk_surface_hide (grab_surface);
               return TRUE;
@@ -4094,7 +4131,7 @@ gdk_surface_handle_event (GdkEvent *event)
 {
   gboolean handled = FALSE;
 
-  if (check_auto_dismissal (event))
+  if (check_autohide (event))
     return TRUE;
 
   if (gdk_event_get_event_type (event) == GDK_CONFIGURE)
@@ -4110,33 +4147,3 @@ gdk_surface_handle_event (GdkEvent *event)
 
   return handled;
 }
-
-static void
-grab_prepare_func (GdkSeat    *seat,
-                   GdkSurface *surface,
-                   gpointer    data)
-{
-  gdk_surface_show (surface);
-  surface->auto_dismissal = TRUE;
-}
-
-/**
- * gdk_surface_show_with_auto_dismissal:
- * @surface: a #GdkSurface
- * @seat: the seat to grab on
- *
- * Show the surface, like gdk_surface_show(), but also
- * take a grab and hide the surface when we encounter
- * button presses or touch events outside the surface.
- */
-void
-gdk_surface_show_with_auto_dismissal (GdkSurface *surface,
-                                      GdkSeat    *seat)
-{
-  gdk_seat_grab (seat,
-                 surface,
-                 GDK_SEAT_CAPABILITY_ALL,
-                 TRUE,
-                 NULL, NULL,
-                 grab_prepare_func, NULL);
-}
diff --git a/gdk/gdksurface.h b/gdk/gdksurface.h
index 06dc12ad93..4bc98b6193 100644
--- a/gdk/gdksurface.h
+++ b/gdk/gdksurface.h
@@ -426,7 +426,8 @@ GdkSurface *   gdk_surface_new_temp             (GdkDisplay    *display,
                                                  const GdkRectangle *position);
 GDK_AVAILABLE_IN_ALL
 GdkSurface *   gdk_surface_new_popup            (GdkDisplay    *display,
-                                                 GdkSurface    *parent);
+                                                 GdkSurface    *parent,
+                                                 gboolean       autohide);
 
 GDK_AVAILABLE_IN_ALL
 void          gdk_surface_destroy               (GdkSurface     *surface);
@@ -446,9 +447,6 @@ GDK_AVAILABLE_IN_ALL
 void          gdk_surface_hide                  (GdkSurface     *surface);
 GDK_AVAILABLE_IN_ALL
 void          gdk_surface_show_unraised         (GdkSurface     *surface);
-GDK_AVAILABLE_IN_ALL
-void          gdk_surface_show_with_auto_dismissal (GdkSurface *surface,
-                                                    GdkSeat    *seat);
 
 GDK_AVAILABLE_IN_ALL
 void          gdk_surface_move                  (GdkSurface     *surface,
diff --git a/gdk/gdksurfaceprivate.h b/gdk/gdksurfaceprivate.h
index b03cc5ccd1..c7933fe0f2 100644
--- a/gdk/gdksurfaceprivate.h
+++ b/gdk/gdksurfaceprivate.h
@@ -69,7 +69,7 @@ struct _GdkSurface
   guint viewable : 1; /* mapped and all parents mapped */
   guint in_update : 1;
   guint frame_clock_events_paused : 1;
-  guint auto_dismissal : 1;
+  guint autohide : 1;
 
   guint update_and_descendants_freeze_count;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]