home.c 15.2 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-home"

#include "config.h"
12
#include "arrow.h"
13
#include "overview.h"
Guido Gunther's avatar
Guido Gunther committed
14
#include "home.h"
15
#include "shell.h"
16
#include "splash.h"
17
#include "phosh-enums.h"
Guido Gunther's avatar
Guido Gunther committed
18
#include "osk/osk-button.h"
Guido Gunther's avatar
Guido Gunther committed
19

20 21
#include <handy.h>

22 23 24 25
#define KEYBINDINGS_SCHEMA_ID "org.gnome.shell.keybindings"
#define KEYBINDING_KEY_TOGGLE_OVERVIEW "toggle-overview"
#define KEYBINDING_KEY_TOGGLE_APPLICATION_VIEW "toggle-application-view"

Guido Gunther's avatar
Guido Gunther committed
26
/**
27
 * SECTION:home
Guido Gunther's avatar
Guido Gunther committed
28 29
 * @short_description: The home surface contains the overview and
 * the button to fold and unfold the overview.
Guido Gunther's avatar
Guido Gunther committed
30 31
 * @Title: PhoshHome
 *
Guido Gunther's avatar
Guido Gunther committed
32 33 34 35
 * #PhoshHome contains the #PhoshOverview that manages running
 * applications and the app grid. It also manages a button
 * at the bottom of the screen to fold and unfold the #PhoshOverview
 * and a button to toggle the OSK.
Guido Gunther's avatar
Guido Gunther committed
36 37
 */
enum {
Guido Gunther's avatar
Guido Gunther committed
38
  OSK_ACTIVATED,
Guido Gunther's avatar
Guido Gunther committed
39 40 41 42
  N_SIGNALS
};
static guint signals[N_SIGNALS] = { 0 };

43 44 45 46

enum {
  PROP_0,
  PROP_HOME_STATE,
47
  PROP_OSK_ENABLED,
48 49 50 51 52
  PROP_LAST_PROP,
};
static GParamSpec *props[PROP_LAST_PROP];


Guido Gunther's avatar
Guido Gunther committed
53 54 55 56
struct _PhoshHome
{
  PhoshLayerSurface parent;

Guido Gunther's avatar
Guido Gunther committed
57
  GtkWidget *btn_home;
58
  GtkWidget *arrow_home;
Guido Gunther's avatar
Guido Gunther committed
59
  GtkWidget *btn_osk;
60
  GtkWidget *rev_home;
61
  GtkWidget *overview;
62

63
  struct {
64
    double progress;
65 66 67
    gint64 last_frame;
  } animation;

68
  PhoshHomeState state;
69 70 71 72

  /* Keybinding */
  GArray         *actions;
  GSettings      *settings;
73 74 75

  /* osk button */
  gboolean        osk_enabled;
76 77 78 79

  /* launch splash: TODO: move to object */
  GHashTable     *launching_apps;
  GtkWidget      *splash;
Guido Gunther's avatar
Guido Gunther committed
80 81
};
G_DEFINE_TYPE(PhoshHome, phosh_home, PHOSH_TYPE_LAYER_SURFACE);
Guido Gunther's avatar
Guido Gunther committed
82 83


84 85 86 87 88 89 90 91 92 93
static void
phosh_home_update_osk_button (PhoshHome *self)
{
  if (self->osk_enabled && self->state == PHOSH_HOME_STATE_FOLDED)
    gtk_widget_show (self->btn_osk);
  else
    gtk_widget_hide (self->btn_osk);
}


94 95 96 97 98 99 100 101 102 103 104 105 106
static void
phosh_home_set_property (GObject *object,
                          guint property_id,
                          const GValue *value,
                          GParamSpec *pspec)
{
  PhoshHome *self = PHOSH_HOME (object);

  switch (property_id) {
    case PROP_HOME_STATE:
      self->state = g_value_get_enum (value);
      g_object_notify_by_pspec (G_OBJECT (self), props[PROP_HOME_STATE]);
      break;
107 108 109 110 111
    case PROP_OSK_ENABLED:
      self->osk_enabled = g_value_get_boolean (value);
      phosh_home_update_osk_button (self);
      g_object_notify_by_pspec (G_OBJECT (self), props[PROP_OSK_ENABLED]);
      break;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}


static void
phosh_home_get_property (GObject *object,
                          guint property_id,
                          GValue *value,
                          GParamSpec *pspec)
{
  PhoshHome *self = PHOSH_HOME (object);

  switch (property_id) {
    case PROP_HOME_STATE:
      g_value_set_enum (value, self->state);
      break;
131 132 133
    case PROP_OSK_ENABLED:
      g_value_set_boolean (value, self->osk_enabled);
      break;
134 135 136 137 138 139 140
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
  }
}


141 142 143
static void
phosh_home_resize (PhoshHome *self)
{
144 145 146
  int margin;
  int height;
  double progress = hdy_ease_out_cubic (self->animation.progress);
147 148 149 150

  if (self->state == PHOSH_HOME_STATE_UNFOLDED)
    progress = 1.0 - progress;

151 152
  phosh_arrow_set_progress (PHOSH_ARROW (self->arrow_home), 1 - progress);

153
  g_object_get (self, "configured-height", &height, NULL);
154 155 156 157 158 159 160 161 162
  margin = (-height + PHOSH_HOME_BUTTON_HEIGHT) * progress;

  phosh_layer_surface_set_margins (PHOSH_LAYER_SURFACE (self), 0, 0, margin, 0);
  /* Adjust the exclusive zone since exclusive zone includes margins.
     We don't want to change the effective exclusive zone at all to
     prevent all clients from being resized. */
  phosh_layer_surface_set_exclusive_zone (PHOSH_LAYER_SURFACE (self),
                                          -margin + PHOSH_HOME_BUTTON_HEIGHT);

163
  phosh_layer_surface_wl_surface_commit (PHOSH_LAYER_SURFACE (self));
164 165 166
}


Guido Gunther's avatar
Guido Gunther committed
167 168 169 170 171
static void
home_clicked_cb (PhoshHome *self, GtkButton *btn)
{
  g_return_if_fail (PHOSH_IS_HOME (self));
  g_return_if_fail (GTK_IS_BUTTON (btn));
172 173

  phosh_home_set_state (self, !self->state);
Guido Gunther's avatar
Guido Gunther committed
174 175 176
}


Guido Gunther's avatar
Guido Gunther committed
177 178 179 180 181 182 183 184 185
static void
osk_clicked_cb (PhoshHome *self, GtkButton *btn)
{
  g_return_if_fail (PHOSH_IS_HOME (self));
  g_return_if_fail (GTK_IS_BUTTON (btn));
  g_signal_emit(self, signals[OSK_ACTIVATED], 0);
}


186
static void
187
fold_cb (PhoshHome *self, PhoshOverview *overview)
188 189
{
  g_return_if_fail (PHOSH_IS_HOME (self));
190
  g_return_if_fail (PHOSH_IS_OVERVIEW (overview));
191 192 193 194

  phosh_home_set_state (self, PHOSH_HOME_STATE_FOLDED);
}

195

196 197 198 199 200 201 202 203 204 205 206 207 208
static void
on_has_activities_changed (PhoshHome *self)
{
  gboolean reveal;

  g_return_if_fail (PHOSH_IS_HOME (self));

  reveal = (phosh_overview_has_running_activities (PHOSH_OVERVIEW (self->overview)) ||
            self->state == PHOSH_HOME_STATE_FOLDED);
  gtk_revealer_set_reveal_child (GTK_REVEALER (self->rev_home), reveal);
}


209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
static gboolean
key_press_event_cb (PhoshHome *self, GdkEventKey *event, gpointer data)
{
  gboolean handled = FALSE;
  g_return_val_if_fail (PHOSH_IS_HOME (self), FALSE);

  switch (event->keyval) {
    case GDK_KEY_Escape:
      phosh_home_set_state (self, PHOSH_HOME_STATE_FOLDED);
      handled = TRUE;
      break;
    default:
      /* nothing to do */
      break;
  }

  return handled;
}
227 228


229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
static void
toggle_overview_action (GSimpleAction *action, GVariant *param, gpointer data)
{
  PhoshHome *self = PHOSH_HOME (data);
  PhoshHomeState state;

  g_return_if_fail (PHOSH_IS_HOME (self));

  state = self->state == PHOSH_HOME_STATE_UNFOLDED ?
    PHOSH_HOME_STATE_FOLDED : PHOSH_HOME_STATE_UNFOLDED;
  phosh_home_set_state (self, state);
}


static void
toggle_application_view_action (GSimpleAction *action, GVariant *param, gpointer data)
{
  PhoshHome *self = PHOSH_HOME (data);
  PhoshHomeState state;

  g_return_if_fail (PHOSH_IS_HOME (self));

  state = self->state == PHOSH_HOME_STATE_UNFOLDED ?
    PHOSH_HOME_STATE_FOLDED : PHOSH_HOME_STATE_UNFOLDED;
  phosh_home_set_state (self, state);
  phosh_overview_focus_app_search (PHOSH_OVERVIEW (self->overview));
}


static void
add_keybindings (PhoshHome *self)
{
  g_auto (GStrv) bindings = NULL;
  g_autoptr (GSettings) settings = g_settings_new (KEYBINDINGS_SCHEMA_ID);

  bindings = g_settings_get_strv (settings, KEYBINDING_KEY_TOGGLE_OVERVIEW);
  for (int i = 0; i < g_strv_length (bindings); i++) {
    GActionEntry entry = { bindings[i],
                           toggle_overview_action, };
    g_array_append_val (self->actions, entry);
  }

  bindings = g_settings_get_strv (settings, KEYBINDING_KEY_TOGGLE_APPLICATION_VIEW);
  for (int i = 0; i < g_strv_length (bindings); i++) {
    GActionEntry entry = { bindings[i],
                           toggle_application_view_action, };
    g_array_append_val (self->actions, entry);
  }

  phosh_shell_add_global_keyboard_action_entries (phosh_shell_get_default (),
                                                  (GActionEntry*)self->actions->data,
                                                  self->actions->len,
                                                  self);
}


static void
on_keybindings_changed (PhoshHome  *self,
                        gchar     *key,
                        GSettings *settings)
{
  /* For now just redo all keybindings */
  g_debug ("Updating keybindings");
  phosh_shell_remove_global_keyboard_action_entries (phosh_shell_get_default (),
                                                     (GActionEntry*)self->actions->data,
                                                     self->actions->len);
  g_array_set_size (self->actions, 0);
  add_keybindings (self);
}


300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
static void
on_activity_launched (PhoshHome *self,
                      GAppInfo  *info,
                      PhoshOverview *overview)
{
  g_return_if_fail (PHOSH_IS_HOME (self));
  g_return_if_fail (G_IS_APP_INFO (info));
  g_return_if_fail (PHOSH_IS_OVERVIEW (overview));

  self->splash = phosh_splash_new (info);
  gtk_widget_show_all (self->splash);

  fold_cb (self, overview);
}


static void
on_activity_up (PhoshHome *self)
{
  g_return_if_fail (PHOSH_IS_HOME (self));

  g_debug ("Activity is up");
  /* TODO: match on app-id */
  gtk_widget_hide (self->splash);
  gtk_widget_destroy (self->splash);
  self->splash = NULL;
}


329 330 331 332 333 334 335 336 337 338 339 340 341
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, "osk-enabled", G_SETTINGS_BIND_GET);

  return FALSE;
}


Guido Gunther's avatar
Guido Gunther committed
342 343 344 345 346
static void
phosh_home_constructed (GObject *object)
{
  PhoshHome *self = PHOSH_HOME (object);

347
  g_signal_connect (self,
348
                    "configured",
349 350 351
                    G_CALLBACK (phosh_home_resize),
                    NULL);

352 353 354 355 356 357
  gtk_widget_add_events (GTK_WIDGET (self), GDK_KEY_PRESS_MASK);
  g_signal_connect (G_OBJECT (self),
                    "key_press_event",
                    G_CALLBACK (key_press_event_cb),
                    NULL);

358 359 360 361 362 363
  g_object_connect (self->settings,
                    "swapped-signal::changed::" KEYBINDING_KEY_TOGGLE_OVERVIEW,
                    on_keybindings_changed, self,
                    "swapped-signal::changed::" KEYBINDING_KEY_TOGGLE_APPLICATION_VIEW,
                    on_keybindings_changed, self,
                    NULL);
364

365 366
  add_keybindings (self);

367
  phosh_connect_feedback (self->btn_home);
368

369
  g_idle_add ((GSourceFunc) on_idle, self);
370

Guido Gunther's avatar
Guido Gunther committed
371 372 373 374
  G_OBJECT_CLASS (phosh_home_parent_class)->constructed (object);
}


375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
static void
phosh_home_dispose (GObject *object)
{
  PhoshHome *self = PHOSH_HOME (object);

  g_clear_object (&self->settings);

  if (self->actions) {
    phosh_shell_remove_global_keyboard_action_entries (phosh_shell_get_default (),
                                                       (GActionEntry*)self->actions->data,
                                                       self->actions->len);
    g_clear_pointer (&self->actions, g_array_unref);
  }

  G_OBJECT_CLASS (phosh_home_parent_class)->dispose (object);
}


Guido Gunther's avatar
Guido Gunther committed
393 394 395 396 397 398 399
static void
phosh_home_class_init (PhoshHomeClass *klass)
{
  GObjectClass *object_class = (GObjectClass *)klass;
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->constructed = phosh_home_constructed;
400
  object_class->dispose = phosh_home_dispose;
Guido Gunther's avatar
Guido Gunther committed
401

402 403 404
  object_class->set_property = phosh_home_set_property;
  object_class->get_property = phosh_home_get_property;

Guido Gunther's avatar
Guido Gunther committed
405 406 407
  signals[OSK_ACTIVATED] = g_signal_new ("osk-activated",
      G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL,
      NULL, G_TYPE_NONE, 0);
Guido Gunther's avatar
Guido Gunther committed
408

409 410 411 412 413 414 415 416
  props[PROP_HOME_STATE] =
    g_param_spec_enum ("state",
                       "Home State",
                       "The state of the home screen",
                       PHOSH_TYPE_HOME_STATE,
                       PHOSH_HOME_STATE_FOLDED,
                       G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

417 418 419 420 421 422 423
  props[PROP_OSK_ENABLED] =
    g_param_spec_boolean ("osk-enabled",
                          "OSK enabled",
                          "Whether the on screen keyboard is enabled",
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

424 425
  g_object_class_install_properties (object_class, PROP_LAST_PROP, props);

426
  g_type_ensure (PHOSH_TYPE_ARROW);
427 428 429
  g_type_ensure (PHOSH_TYPE_OSK_BUTTON);
  g_type_ensure (PHOSH_TYPE_OVERVIEW);

Guido Gunther's avatar
Guido Gunther committed
430 431
  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/sm/puri/phosh/ui/home.ui");
432
  gtk_widget_class_bind_template_child (widget_class, PhoshHome, arrow_home);
433
  gtk_widget_class_bind_template_child (widget_class, PhoshHome, btn_home);
Guido Gunther's avatar
Guido Gunther committed
434
  gtk_widget_class_bind_template_child (widget_class, PhoshHome, btn_osk);
435
  gtk_widget_class_bind_template_child (widget_class, PhoshHome, overview);
436
  gtk_widget_class_bind_template_child (widget_class, PhoshHome, rev_home);
437 438
  gtk_widget_class_bind_template_callback (widget_class, on_activity_launched);
  gtk_widget_class_bind_template_callback (widget_class, on_activity_up);
439
  gtk_widget_class_bind_template_callback (widget_class, fold_cb);
440
  gtk_widget_class_bind_template_callback (widget_class, home_clicked_cb);
441
  gtk_widget_class_bind_template_callback (widget_class, on_has_activities_changed);
442
  gtk_widget_class_bind_template_callback (widget_class, osk_clicked_cb);
Zander Brown's avatar
Zander Brown committed
443 444

  gtk_widget_class_set_css_name (widget_class, "phosh-home");
Guido Gunther's avatar
Guido Gunther committed
445 446 447 448 449 450
}


static void
phosh_home_init (PhoshHome *self)
{
451
  self->state = PHOSH_HOME_STATE_FOLDED;
452
  self->animation.progress = 1.0;
453 454
  self->actions = g_array_new (FALSE, TRUE, sizeof (GActionEntry));
  self->settings = g_settings_new (KEYBINDINGS_SCHEMA_ID);
455

Guido Gunther's avatar
Guido Gunther committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
  gtk_widget_init_template (GTK_WIDGET (self));
}


GtkWidget *
phosh_home_new (struct zwlr_layer_shell_v1 *layer_shell,
                struct wl_output *wl_output)
{
  return g_object_new (PHOSH_TYPE_HOME,
                       "layer-shell", layer_shell,
                       "wl-output", wl_output,
                       "anchor", ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
                                 ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT |
                                 ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT,
                       "layer", ZWLR_LAYER_SHELL_V1_LAYER_TOP,
                       "kbd-interactivity", FALSE,
472
                       "exclusive-zone", PHOSH_HOME_BUTTON_HEIGHT,
473
                       "namespace", "phosh home",
Guido Gunther's avatar
Guido Gunther committed
474 475
                       NULL);
}
476

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

static gboolean
animate_cb(GtkWidget *widget,
           GdkFrameClock *frame_clock,
           gpointer user_data)
{
  gint64 time;
  gboolean finished = FALSE;
  PhoshHome *self = PHOSH_HOME (widget);

  time = gdk_frame_clock_get_frame_time (frame_clock) - self->animation.last_frame;
  if (self->animation.last_frame < 0) {
    time = 0;
  }

  self->animation.progress += 0.06666 * time / 16666.00;
  self->animation.last_frame = gdk_frame_clock_get_frame_time (frame_clock);

  if (self->animation.progress >= 1.0) {
    finished = TRUE;
    self->animation.progress = 1.0;
  }

  phosh_home_resize (self);

  return finished ? G_SOURCE_REMOVE : G_SOURCE_CONTINUE;
}

505

506 507
/**
 * phosh_home_set_state:
Guido Gunther's avatar
Guido Gunther committed
508 509
 * @self: The home surface
 * @state: The state to set
510 511 512 513 514 515
 *
 * Set the state of the home screen. See #PhoshHomeState.
 */
void
phosh_home_set_state (PhoshHome *self, PhoshHomeState state)
{
516
  g_autofree char *state_name = NULL;
517
  gboolean enable_animations;
518
  gboolean kbd_interactivity;
519

520 521 522 523 524
  g_return_if_fail (PHOSH_IS_HOME (self));

  if (self->state == state)
    return;

525
  enable_animations = hdy_get_enable_animations (GTK_WIDGET (self));
526

527
  self->state = state;
Guido Gunther's avatar
Guido Gunther committed
528 529 530

  state_name = g_enum_to_string (PHOSH_TYPE_HOME_STATE, state);
  g_debug ("Setting state to %s", state_name);
531

532 533 534
  self->animation.last_frame = -1;
  self->animation.progress = enable_animations ? (1.0 - self->animation.progress) : 1.0;
  gtk_widget_add_tick_callback (GTK_WIDGET (self), animate_cb, NULL, NULL);
535

536
  if (state == PHOSH_HOME_STATE_UNFOLDED) {
537
    kbd_interactivity = TRUE;
538
    phosh_overview_reset (PHOSH_OVERVIEW (self->overview));
539
  } else {
540
    kbd_interactivity = FALSE;
541
  }
542
  phosh_home_update_osk_button (self);
543
  phosh_layer_surface_set_kbd_interactivity (PHOSH_LAYER_SURFACE (self), kbd_interactivity);
544

545 546
  on_has_activities_changed (self);

547 548
  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_HOME_STATE]);
}