osk-button.c 3.72 KB
Newer Older
Guido Gunther's avatar
Guido Gunther committed
1
2
/*
 * Copyright (C) 2018 Purism SPC
Zander Brown's avatar
Zander Brown committed
3
4
5
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
Guido Gunther's avatar
Guido Gunther committed
6
7
8
9
10
11
 * Author: Guido Günther <agx@sigxcpu.org>
 */

#define G_LOG_DOMAIN "phosh-osk-button"

#include "osk-button.h"
12
13
#include "osk-manager.h"
#include "shell.h"
Guido Gunther's avatar
Guido Gunther committed
14
15
16
17
18

#include <gio/gio.h>
#include <gtk/gtk.h>

/**
19
 * SECTION:osk-button
Guido Gunther's avatar
Guido Gunther committed
20
21
22
23
24
 * @short_description: A button that toggles the OSK
 * @Title: PhoshOsk
 *
 * The #PhoshOskButton is responsible for toggling the on screen keyboard
 */
25
struct _PhoshOskButton
Guido Gunther's avatar
Guido Gunther committed
26
{
27
28
  GtkToggleButton parent;

29
  PhoshOskManager *osk;
Guido Gunther's avatar
Guido Gunther committed
30
  gboolean setting_visibility;
31
};
Guido Gunther's avatar
Guido Gunther committed
32

33
G_DEFINE_TYPE (PhoshOskButton, phosh_osk_button, GTK_TYPE_TOGGLE_BUTTON)
Guido Gunther's avatar
Guido Gunther committed
34
35
36
37
38


static void
toggled_cb (PhoshOskButton *self, gpointer data)
{
39
  gboolean visible, active;
Guido Gunther's avatar
Guido Gunther committed
40

41
42
  self->setting_visibility = TRUE;
  visible = phosh_osk_manager_get_visible (self->osk);
43
44
45

  active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (self));
  if (visible != active)
46
    phosh_osk_manager_set_visible (self->osk, active);
47

48
  self->setting_visibility = FALSE;
Guido Gunther's avatar
Guido Gunther committed
49
50
51
52
}


static void
53
on_osk_availability_changed (PhoshOskButton *self, GParamSpec *pspec, PhoshOskManager *osk)
Guido Gunther's avatar
Guido Gunther committed
54
{
55
  gboolean available;
Guido Gunther's avatar
Guido Gunther committed
56

57
58
  g_return_if_fail (PHOSH_IS_OSK_BUTTON (self));
  g_return_if_fail (PHOSH_IS_OSK_MANAGER (osk));
59
  g_return_if_fail (self->osk == osk);
Guido Gunther's avatar
Guido Gunther committed
60

61
62
  available = phosh_osk_manager_get_available (osk);
  gtk_widget_set_sensitive (GTK_WIDGET (self), available);
Guido Gunther's avatar
Guido Gunther committed
63
64
65
66
}


static void
67
on_osk_visibility_changed (PhoshOskButton *self, GParamSpec *pspec, PhoshOskManager *osk)
Guido Gunther's avatar
Guido Gunther committed
68
{
69
  gboolean visible;
Guido Gunther's avatar
Guido Gunther committed
70
71

  g_return_if_fail (PHOSH_IS_OSK_BUTTON (self));
72
  g_return_if_fail (PHOSH_IS_OSK_MANAGER (osk));
73
  g_return_if_fail (self->osk == osk);
74
75

  visible = phosh_osk_manager_get_visible (osk);
76
  if (!self->setting_visibility)
77
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self), visible);
Guido Gunther's avatar
Guido Gunther committed
78
79
80
}


81
82
83
84
85
86
87
88
89
90
91
92
93
static gboolean
on_idle (PhoshOskButton *self)
{
  g_autoptr (GSettings) settings = NULL;

  settings = g_settings_new ("org.gnome.desktop.a11y.applications");
  g_settings_bind (settings, "screen-keyboard-enabled",
                   self, "visible", G_SETTINGS_BIND_GET);

  return FALSE;
}


Guido Gunther's avatar
Guido Gunther committed
94
95
96
97
static void
phosh_osk_button_constructed (GObject *object)
{
  PhoshOskButton *self = PHOSH_OSK_BUTTON (object);
98
  PhoshShell *shell;
Guido Gunther's avatar
Guido Gunther committed
99
100
101
102
  GtkWidget *image;

  G_OBJECT_CLASS (phosh_osk_button_parent_class)->constructed (object);

103
  shell = phosh_shell_get_default ();
104
  self->osk = g_object_ref(phosh_shell_get_osk_manager (shell));
105
106

  g_signal_connect_swapped (
107
    self->osk,
108
109
    "notify::visible",
    G_CALLBACK (on_osk_visibility_changed),
Guido Gunther's avatar
Guido Gunther committed
110
111
112
    self);

  g_signal_connect_swapped (
113
    self->osk,
114
115
    "notify::available",
    G_CALLBACK (on_osk_availability_changed),
Guido Gunther's avatar
Guido Gunther committed
116
117
118
119
120
121
122
123
124
125
126
    self);

  g_signal_connect (self,
                    "toggled",
                    G_CALLBACK (toggled_cb),
                    NULL);

  image = gtk_image_new_from_icon_name ("input-keyboard-symbolic", GTK_ICON_SIZE_BUTTON);
  gtk_button_set_image (GTK_BUTTON (self), image);
  gtk_button_set_always_show_image (GTK_BUTTON (self), TRUE);

127
128
  on_osk_availability_changed (self, NULL, self->osk);
  on_osk_visibility_changed (self, NULL, self->osk);
129
130

  g_idle_add ((GSourceFunc) on_idle, self);
Guido Gunther's avatar
Guido Gunther committed
131
132
133
134
135
136
}


static void
phosh_osk_button_dispose (GObject *object)
{
137
  PhoshOskButton *self = PHOSH_OSK_BUTTON (object);
Guido Gunther's avatar
Guido Gunther committed
138

139
  g_clear_object (&self->osk);
Guido Gunther's avatar
Guido Gunther committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  G_OBJECT_CLASS (phosh_osk_button_parent_class)->dispose (object);
}


static void
phosh_osk_button_class_init (PhoshOskButtonClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = phosh_osk_button_constructed;
  object_class->dispose = phosh_osk_button_dispose;
}


static void
phosh_osk_button_init (PhoshOskButton *self)
{
}


GtkWidget *
phosh_osk_button_new (void)
{
  return g_object_new (PHOSH_TYPE_OSK_BUTTON, NULL);
}