Add automatic help overlay support to GtkApplication

When the $(resource_prefix)/gtk/help-overlay.ui resource exists,
load a GtkShortcutsWindow from it for each GtkApplicationWindow,
and set up a win.show-help-overlay action with accels <Primary>F1
and <Primary>? to show it.
This commit is contained in:
Matthias Clasen
2015-10-21 00:11:59 -04:00
parent 310781ecdd
commit f6d9f9f93d
4 changed files with 134 additions and 10 deletions

View File

@ -228,6 +228,8 @@ struct _GtkApplicationWindowPrivate
GMenu *menubar_section;
guint id;
GtkShortcutsWindow *help_overlay;
};
static void
@ -788,9 +790,9 @@ gtk_application_window_dispose (GObject *object)
g_clear_object (&window->priv->app_menu_section);
g_clear_object (&window->priv->menubar_section);
g_clear_object (&window->priv->help_overlay);
G_OBJECT_CLASS (gtk_application_window_parent_class)
->dispose (object);
G_OBJECT_CLASS (gtk_application_window_parent_class)->dispose (object);
/* We do this below the chain-up above to give us a chance to be
* removed from the GtkApplication (which is done in the dispose
@ -959,3 +961,75 @@ gtk_application_window_set_id (GtkApplicationWindow *window,
g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
window->priv->id = id;
}
static void
show_help_overlay (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GtkApplicationWindow *window = user_data;
if (window->priv->help_overlay)
gtk_widget_show (GTK_WIDGET (window->priv->help_overlay));
}
/**
* gtk_application_window_set_help_overlay:
* @window: a #GtkApplicationWindow
* @help_overlay: a #GtkShortcutsWindow
*
* Associates a shortcuts window with the application window, and
* sets up a action with the name win.show-help-overay to present
* it.
*
* Since: 3.20
*/
void
gtk_application_window_set_help_overlay (GtkApplicationWindow *window,
GtkShortcutsWindow *help_overlay)
{
g_return_if_fail (GTK_IS_APPLICATION_WINDOW (window));
g_return_if_fail (help_overlay == NULL || GTK_IS_SHORTCUTS_WINDOW (help_overlay));
if (window->priv->help_overlay)
g_signal_handlers_disconnect_by_func (window->priv->help_overlay,
G_CALLBACK (gtk_widget_hide_on_delete), NULL);
g_set_object (&window->priv->help_overlay, help_overlay);
if (!window->priv->help_overlay)
return;
gtk_window_set_modal (GTK_WINDOW (help_overlay), TRUE);
gtk_window_set_transient_for (GTK_WINDOW (help_overlay), GTK_WINDOW (window));
g_signal_connect (help_overlay, "delete-event",
G_CALLBACK (gtk_widget_hide_on_delete), NULL);
if (!g_action_map_lookup_action (G_ACTION_MAP (window->priv->actions), "show-help-overlay"))
{
GSimpleAction *action;
action = g_simple_action_new ("show-help-overlay", NULL);
g_signal_connect (action, "activate", G_CALLBACK (show_help_overlay), window);
g_action_map_add_action (G_ACTION_MAP (window->priv->actions), G_ACTION (action));
}
}
/**
* gtk_application_window_get_help_overlay:
* @window: a #GtkApplicationWindow
*
* Gets the #GtkShortcutsWindow that has been set up with
* a prior call to gtk_application_window_set_help_overlay().
*
* Returns: the help overlay associated with @window, or %NULL
*
* Since: 3.20
*/
GtkShortcutsWindow *
gtk_application_window_get_help_overlay (GtkApplicationWindow *window)
{
g_return_val_if_fail (GTK_IS_APPLICATION_WINDOW (window), NULL);
return window->priv->help_overlay;
}