summaryrefslogtreecommitdiff
path: root/src/ephy-page-row.c
blob: 5616ee093aad4c30d4c3c6ad00c7458246bf473b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 *  Copyright © 2019 Purism SPC
 *  Copyright © 2019 Adrien Plazas <kekun.plazas@laposte.net>
 *
 *  This file is part of Epiphany.
 *
 *  Epiphany is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Epiphany 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Epiphany.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "ephy-desktop-utils.h"
#include "ephy-embed-utils.h"
#include "ephy-page-row.h"
#include "ephy-web-view.h"

enum {
  CLOSED,

  LAST_SIGNAL
};

struct _EphyPageRow {
  GtkPopover parent_instance;

  GtkBox *box;
  GtkImage *icon;
  GtkStack *icon_stack;
  GtkImage *speaker_icon;
  GtkSpinner *spinner;
  GtkLabel *title;
  GtkButton *close_button;
};

static guint signals[LAST_SIGNAL];

G_DEFINE_TYPE (EphyPageRow, ephy_page_row, GTK_TYPE_LIST_BOX_ROW)

static void
sync_load_status (EphyWebView *view,
                  GParamSpec  *pspec,
                  EphyPageRow *self)
{
  EphyEmbed *embed;

  g_assert (EPHY_IS_WEB_VIEW (view));
  g_assert (EPHY_IS_PAGE_ROW (self));

  embed = EPHY_GET_EMBED_FROM_EPHY_WEB_VIEW (view);

  g_assert (EPHY_IS_EMBED (embed));

  if (ephy_web_view_is_loading (view) && !ephy_embed_has_load_pending (embed)) {
    gtk_stack_set_visible_child (self->icon_stack, GTK_WIDGET (self->spinner));
    gtk_spinner_start (GTK_SPINNER (self->spinner));
  } else {
    gtk_stack_set_visible_child (self->icon_stack, GTK_WIDGET (self->icon));
    gtk_spinner_stop (GTK_SPINNER (self->spinner));
  }
}

static void
load_changed_cb (EphyWebView     *view,
                 WebKitLoadEvent  load_event,
                 EphyPageRow     *self)
{
  sync_load_status (view, NULL, self);
}

static void
close_clicked_cb (EphyPageRow *self)
{
  g_signal_emit (self, signals[CLOSED], 0);
}

static gboolean
button_release_event (GtkWidget   *widget,
                      GdkEvent    *event,
                      EphyPageRow *self)
{
  GdkEventButton *button_event = (GdkEventButton *)event;

  if (button_event->button == GDK_BUTTON_MIDDLE) {
    g_signal_emit (self, signals[CLOSED], 0);

    return GDK_EVENT_STOP;
  }

  return GDK_EVENT_PROPAGATE;
}

static void
ephy_page_row_class_init (EphyPageRowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  signals[CLOSED] =
    g_signal_new ("closed",
                  EPHY_TYPE_PAGE_ROW,
                  G_SIGNAL_RUN_LAST,
                  0, NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/epiphany/gtk/page-row.ui");
  gtk_widget_class_bind_template_child (widget_class, EphyPageRow, box);
  gtk_widget_class_bind_template_child (widget_class, EphyPageRow, icon);
  gtk_widget_class_bind_template_child (widget_class, EphyPageRow, icon_stack);
  gtk_widget_class_bind_template_child (widget_class, EphyPageRow, speaker_icon);
  gtk_widget_class_bind_template_child (widget_class, EphyPageRow, spinner);
  gtk_widget_class_bind_template_child (widget_class, EphyPageRow, title);
  gtk_widget_class_bind_template_child (widget_class, EphyPageRow, close_button);
  gtk_widget_class_bind_template_callback (widget_class, close_clicked_cb);
  gtk_widget_class_bind_template_callback (widget_class, button_release_event);
}

static void
ephy_page_row_init (EphyPageRow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
}

static void
sync_favicon (EphyWebView *view,
              GParamSpec  *pspec,
              EphyPageRow *self)
{
  GdkPixbuf *pixbuf = ephy_web_view_get_icon (view);

  if (pixbuf)
    gtk_image_set_from_gicon (GTK_IMAGE (self->icon), G_ICON (pixbuf), GTK_ICON_SIZE_MENU);
  else {
    const char *favicon_name = ephy_get_fallback_favicon_name (ephy_web_view_get_display_address (view), EPHY_FAVICON_TYPE_SHOW_MISSING_PLACEHOLDER);

    gtk_image_set_from_icon_name (GTK_IMAGE (self->icon), favicon_name, GTK_ICON_SIZE_MENU);
  }
}

EphyPageRow *
ephy_page_row_new (EphyNotebook *notebook,
                   gint          position)
{
  EphyPageRow *self;
  GtkWidget *embed;
  GtkWidget *tab_label;
  EphyWebView *view;

  g_assert (notebook != NULL);
  g_assert (position >= 0);

  self = g_object_new (EPHY_TYPE_PAGE_ROW, NULL);

  embed = gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), position);

  g_assert (EPHY_IS_EMBED (embed));

  tab_label = gtk_notebook_get_tab_label (GTK_NOTEBOOK (notebook), embed);
  view = ephy_embed_get_web_view (EPHY_EMBED (embed));

  sync_favicon (view, NULL, self);
  g_signal_connect_object (view, "notify::icon", G_CALLBACK (sync_favicon), self, 0);
  g_object_bind_property (embed, "title", self->title, "label", G_BINDING_SYNC_CREATE);
  g_object_bind_property (embed, "title", self->title, "tooltip-text", G_BINDING_SYNC_CREATE);
  g_object_bind_property (view, "is-playing-audio", self->speaker_icon, "visible", G_BINDING_SYNC_CREATE);
  g_object_bind_property (tab_label, "pinned", self->close_button, "visible", G_BINDING_SYNC_CREATE | G_BINDING_INVERT_BOOLEAN);
  sync_load_status (view, NULL, self);
  g_signal_connect_object (view, "load-changed",
                           G_CALLBACK (load_changed_cb), self, 0);

  return self;
}

void
ephy_page_row_set_adaptive_mode (EphyPageRow      *self,
                                 EphyAdaptiveMode  adaptive_mode)
{
  g_assert (EPHY_IS_PAGE_ROW (self));

  switch (adaptive_mode) {
    case EPHY_ADAPTIVE_MODE_NORMAL:
      gtk_widget_set_margin_start (GTK_WIDGET (self->box), 3);
      gtk_widget_set_margin_end (GTK_WIDGET (self->box), 1);
      gtk_box_set_spacing (self->box, 0);

      break;
    case EPHY_ADAPTIVE_MODE_NARROW:
      gtk_widget_set_margin_start (GTK_WIDGET (self->box), 8);
      gtk_widget_set_margin_end (GTK_WIDGET (self->box), 0);
      gtk_box_set_spacing (self->box, 4);

      break;
  }
}