gtkimcontext: Make gtk_im_context_activate_osk() public

Make this API public so that foreign "text" widgets (e.g. VteTerminal)
have a chance to integrate this logic into their own event controllers,
without having to craft the behavior of their own gestures around the
built-in IM gesture.

In order to make it most useful for other backends, a GdkEvent argument
and a boolean return value were added. This might be useful information
for other platforms than Wayland, e.g. all investigation seems to hint
that on Windows only the a11y keyboard is available programmatically
via app launching, so the IM method implementation would need to set
up its own policies for showing up the OSK (e.g. on touch events).
This commit is contained in:
Carlos Garnacho 2024-03-11 14:10:45 +01:00
parent fe4cd7cf0f
commit ce6aa73c64
7 changed files with 51 additions and 43 deletions

View File

@ -18,7 +18,6 @@
#include "config.h"
#include <string.h>
#include "gtkimcontext.h"
#include "gtkimcontextprivate.h"
#include "gtkprivate.h"
#include "gtktypebuiltins.h"
#include "gtkmarshalers.h"
@ -1007,11 +1006,30 @@ gtk_im_context_set_property (GObject *obj,
}
}
void
gtk_im_context_activate_osk (GtkIMContext *context)
/**
* gtk_im_context_activate_osk:
* @context: a `GtkIMContext`
* @event: (nullable): a [class@Gdk.Event]
*
* Requests the platform to show an on-screen keyboard for user input.
*
* This method will return %TRUE if this request was actually performed
* to the platform, other environmental factors may result in an on-screen
* keyboard effectively not showing up.
*
* Returns: %TRUE if an on-screen keyboard could be requested to the platform.
*
* Since: 4.14
**/
gboolean
gtk_im_context_activate_osk (GtkIMContext *context,
GdkEvent *event)
{
g_return_if_fail (GTK_IS_IM_CONTEXT (context));
g_return_val_if_fail (GTK_IS_IM_CONTEXT (context), FALSE);
g_return_val_if_fail (!event || GDK_IS_EVENT (event), FALSE);
if (GTK_IM_CONTEXT_GET_CLASS (context)->activate_osk)
GTK_IM_CONTEXT_GET_CLASS (context)->activate_osk (context);
if (!GTK_IM_CONTEXT_GET_CLASS (context)->activate_osk_with_event)
return FALSE;
return GTK_IM_CONTEXT_GET_CLASS (context)->activate_osk_with_event (context, event);
}

View File

@ -96,6 +96,8 @@ struct _GtkIMContextClass
/*< private >*/
void (* activate_osk) (GtkIMContext *context);
gboolean (* activate_osk_with_event) (GtkIMContext *context,
GdkEvent *event);
/* Padding for future expansion */
void (*_gtk_reserved1) (void);
@ -168,6 +170,10 @@ gboolean gtk_im_context_delete_surrounding (GtkIMContext *context,
int offset,
int n_chars);
GDK_AVAILABLE_IN_4_14
gboolean gtk_im_context_activate_osk (GtkIMContext *context,
GdkEvent *event);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkIMContext, g_object_unref)
G_END_DECLS

View File

@ -1,23 +0,0 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2022 Red Hat Inc.
*
* 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/>.
*/
#pragma once
#include "gtkimcontext.h"
void gtk_im_context_activate_osk (GtkIMContext *context);

View File

@ -974,18 +974,20 @@ gtk_im_context_wayland_commit (GtkIMContext *context,
ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD);
}
static void
gtk_im_context_wayland_activate_osk (GtkIMContext *context)
static gboolean
gtk_im_context_wayland_activate_osk_with_event (GtkIMContext *context,
GdkEvent *event)
{
GtkIMContextWaylandGlobal *global;
global = gtk_im_context_wayland_get_global (GTK_IM_CONTEXT_WAYLAND (context));
if (global == NULL)
return;
return FALSE;
zwp_text_input_v3_enable (global->text_input);
notify_im_change (GTK_IM_CONTEXT_WAYLAND (context),
ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_OTHER);
return TRUE;
}
static void
@ -1007,7 +1009,7 @@ gtk_im_context_wayland_class_init (GtkIMContextWaylandClass *klass)
im_context_class->set_surrounding_with_selection = gtk_im_context_wayland_set_surrounding;
im_context_class->get_surrounding_with_selection = gtk_im_context_wayland_get_surrounding;
im_context_class->commit = gtk_im_context_wayland_commit;
im_context_class->activate_osk = gtk_im_context_wayland_activate_osk;
im_context_class->activate_osk_with_event = gtk_im_context_wayland_activate_osk_with_event;
}
static void

View File

@ -20,7 +20,6 @@
#include <string.h>
#include <locale.h>
#include "gtkimcontextprivate.h"
#include "gtkimmulticontext.h"
#include "gtkimmoduleprivate.h"
#include "gtklabel.h"
@ -106,7 +105,8 @@ static gboolean gtk_im_multicontext_delete_surrounding_cb (GtkIMContext *
int offset,
int n_chars,
GtkIMMulticontext *multicontext);
static void gtk_im_multicontext_activate_osk (GtkIMContext *context);
static gboolean gtk_im_multicontext_activate_osk_with_event (GtkIMContext *context,
GdkEvent *event);
static void propagate_purpose (GtkIMMulticontext *context);
@ -130,7 +130,7 @@ gtk_im_multicontext_class_init (GtkIMMulticontextClass *class)
im_context_class->set_use_preedit = gtk_im_multicontext_set_use_preedit;
im_context_class->set_surrounding_with_selection = gtk_im_multicontext_set_surrounding_with_selection;
im_context_class->get_surrounding_with_selection = gtk_im_multicontext_get_surrounding_with_selection;
im_context_class->activate_osk = gtk_im_multicontext_activate_osk;
im_context_class->activate_osk_with_event = gtk_im_multicontext_activate_osk_with_event;
gobject_class->finalize = gtk_im_multicontext_finalize;
}
@ -516,14 +516,17 @@ gtk_im_multicontext_set_surrounding_with_selection (GtkIMContext *context,
gtk_im_context_set_surrounding_with_selection (delegate, text, len, cursor_index, anchor_index);
}
static void
gtk_im_multicontext_activate_osk (GtkIMContext *context)
static gboolean
gtk_im_multicontext_activate_osk_with_event (GtkIMContext *context,
GdkEvent *event)
{
GtkIMMulticontext *multicontext = GTK_IM_MULTICONTEXT (context);
GtkIMContext *delegate = gtk_im_multicontext_get_delegate (multicontext);
if (delegate)
gtk_im_context_activate_osk (delegate);
return gtk_im_context_activate_osk (delegate, event);
else
return FALSE;
}
static void

View File

@ -44,7 +44,6 @@
#include "gtkgestureclick.h"
#include "gtkgesturesingle.h"
#include "gtkimageprivate.h"
#include "gtkimcontextprivate.h"
#include "gtkimcontextsimple.h"
#include "gtkimmulticontext.h"
#include "gtkjoinedmenuprivate.h"
@ -2927,11 +2926,13 @@ gtk_text_click_gesture_released (GtkGestureClick *gesture,
GtkText *self)
{
GtkTextPrivate *priv = gtk_text_get_instance_private (self);
GdkEvent *event =
gtk_event_controller_get_current_event (GTK_EVENT_CONTROLLER (gesture));
if (n_press == 1 &&
!priv->in_drag &&
priv->current_pos == priv->selection_bound)
gtk_im_context_activate_osk (priv->im_context);
gtk_im_context_activate_osk (priv->im_context, event);
}
static char *

View File

@ -40,7 +40,6 @@
#include "gtkdragsourceprivate.h"
#include "gtkdropcontrollermotion.h"
#include "gtkemojichooser.h"
#include "gtkimcontextprivate.h"
#include "gtkimmulticontext.h"
#include "gtkjoinedmenuprivate.h"
#include "gtkmagnifierprivate.h"
@ -5764,6 +5763,8 @@ gtk_text_view_click_gesture_released (GtkGestureClick *gesture,
double y,
GtkTextView *text_view)
{
GdkEvent *event =
gtk_event_controller_get_current_event (GTK_EVENT_CONTROLLER (gesture));
GtkTextViewPrivate *priv = text_view->priv;
GtkTextBuffer *buffer;
GtkTextIter start, end;
@ -5773,7 +5774,7 @@ gtk_text_view_click_gesture_released (GtkGestureClick *gesture,
if (gtk_text_iter_compare (&start, &end) == 0 &&
gtk_text_iter_can_insert (&start, priv->editable))
gtk_im_context_activate_osk (priv->im_context);
gtk_im_context_activate_osk (priv->im_context, event);
}
static void