summaryrefslogtreecommitdiff
path: root/src/ephy-action-bar.c
blob: c28ce6bacc34a2b7506a86101205be3e1df0cd8a (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 *  Copyright © 2018 Purism SPC
 *  Copyright © 2018 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 "ephy-action-bar.h"
#include "ephy-pages-button.h"
#include "ephy-pages-popover.h"
#include "ephy-settings.h"
#include "ephy-shell.h"

enum {
  PROP_0,
  PROP_WINDOW,
  N_PROPERTIES
};

static GParamSpec *object_properties[N_PROPERTIES] = { NULL, };

struct _EphyActionBar {
  GtkRevealer parent_instance;

  EphyWindow *window;
  EphyActionBarStart *action_bar_start;
  EphyActionBarEnd *action_bar_end;
  EphyPagesButton *pages_button;
  GtkNotebook *notebook;
};

G_DEFINE_TYPE (EphyActionBar, ephy_action_bar, GTK_TYPE_REVEALER)

static void
sync_chromes_visibility (EphyActionBar *action_bar)
{
  EphyWindowChrome chrome;

  chrome = ephy_window_get_chrome (action_bar->window);

  gtk_widget_set_visible (ephy_action_bar_start_get_navigation_box (action_bar->action_bar_start),
                          chrome & EPHY_WINDOW_CHROME_HEADER_BAR);
  ephy_action_bar_end_set_show_bookmarks_button (action_bar->action_bar_end,
                                                 chrome & EPHY_WINDOW_CHROME_BOOKMARKS);
}

static void
update_pages_button (EphyActionBar *action_bar)
{
  int n_pages;

  n_pages = gtk_notebook_get_n_pages (action_bar->notebook);
  ephy_pages_button_set_n_pages (action_bar->pages_button, n_pages);
}

static void
ephy_action_bar_set_property (GObject      *object,
                              guint         property_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  EphyActionBar *action_bar = EPHY_ACTION_BAR (object);

  switch (property_id) {
    case PROP_WINDOW:
      action_bar->window = EPHY_WINDOW (g_value_get_object (value));
      g_object_notify_by_pspec (object, object_properties[PROP_WINDOW]);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
ephy_action_bar_get_property (GObject    *object,
                              guint       property_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  EphyActionBar *action_bar = EPHY_ACTION_BAR (object);

  switch (property_id) {
    case PROP_WINDOW:
      g_value_set_object (value, action_bar->window);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  }
}

static void
titlebar_animation_changed (EphyActionBar *action_bar)
{
  switch (dzl_application_window_get_titlebar_animation (DZL_APPLICATION_WINDOW (action_bar->window))) {
    case DZL_TITLEBAR_ANIMATION_SHOWN:
      gtk_widget_set_visible (GTK_WIDGET (action_bar), TRUE);
      break;
    case DZL_TITLEBAR_ANIMATION_SHOWING:
      gtk_revealer_set_reveal_child (GTK_REVEALER (action_bar), TRUE);
      break;
    case DZL_TITLEBAR_ANIMATION_HIDING:
      gtk_revealer_set_reveal_child (GTK_REVEALER (action_bar), FALSE);
      break;
    case DZL_TITLEBAR_ANIMATION_HIDDEN:
      gtk_widget_set_visible (GTK_WIDGET (action_bar), FALSE);
      break;
  }
}

static void
ephy_action_bar_constructed (GObject *object)
{
  EphyActionBar *action_bar = EPHY_ACTION_BAR (object);

  G_OBJECT_CLASS (ephy_action_bar_parent_class)->constructed (object);

  action_bar->notebook = GTK_NOTEBOOK (ephy_window_get_notebook (action_bar->window));
  update_pages_button (action_bar);

  g_signal_connect_object (action_bar->window, "notify::chrome",
                           G_CALLBACK (sync_chromes_visibility), action_bar,
                           G_CONNECT_SWAPPED);
  g_signal_connect_object (action_bar->notebook, "page-added",
                           G_CALLBACK (update_pages_button), action_bar,
                           G_CONNECT_SWAPPED);
  g_signal_connect_object (action_bar->notebook, "page-removed",
                           G_CALLBACK (update_pages_button), action_bar,
                           G_CONNECT_SWAPPED);
  g_signal_connect_object (DZL_APPLICATION_WINDOW (action_bar->window), "notify::titlebar-animation",
                           G_CALLBACK (titlebar_animation_changed), action_bar,
                           G_CONNECT_SWAPPED);
}

static void
ephy_action_bar_class_init (EphyActionBarClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gobject_class->set_property = ephy_action_bar_set_property;
  gobject_class->get_property = ephy_action_bar_get_property;
  gobject_class->constructed = ephy_action_bar_constructed;

  object_properties[PROP_WINDOW] =
    g_param_spec_object ("window",
                         "Window",
                         "The action_bar's EphyWindow",
                         EPHY_TYPE_WINDOW,
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (gobject_class,
                                     N_PROPERTIES,
                                     object_properties);

  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/org/gnome/epiphany/gtk/action-bar.ui");

  gtk_widget_class_bind_template_child (widget_class,
                                        EphyActionBar,
                                        action_bar_start);
  gtk_widget_class_bind_template_child (widget_class,
                                        EphyActionBar,
                                        pages_button);
  gtk_widget_class_bind_template_child (widget_class,
                                        EphyActionBar,
                                        action_bar_end);
}

static void
ephy_action_bar_init (EphyActionBar *action_bar)
{
  EphyEmbedShellMode mode;

  /* Ensure the types used by the template have been initialized. */
  EPHY_TYPE_ACTION_BAR_END;
  EPHY_TYPE_ACTION_BAR_START;
  EPHY_TYPE_PAGES_BUTTON;

  gtk_widget_init_template (GTK_WIDGET (action_bar));

  mode = ephy_embed_shell_get_mode (EPHY_EMBED_SHELL (ephy_shell_get_default ()));
  gtk_widget_set_visible (GTK_WIDGET (action_bar->pages_button),
                          mode != EPHY_EMBED_SHELL_MODE_APPLICATION);

  ephy_action_bar_start_set_adaptive_mode (action_bar->action_bar_start,
                                           EPHY_ADAPTIVE_MODE_NARROW);
}

EphyActionBar *
ephy_action_bar_new (EphyWindow *window)
{
  return g_object_new (EPHY_TYPE_ACTION_BAR,
                       "window", window,
                       NULL);
}

EphyActionBarStart *
ephy_action_bar_get_action_bar_start (EphyActionBar *action_bar)
{
  return action_bar->action_bar_start;
}

EphyActionBarEnd *
ephy_action_bar_get_action_bar_end (EphyActionBar *action_bar)
{
  return action_bar->action_bar_end;
}

void
ephy_action_bar_set_adaptive_mode (EphyActionBar    *action_bar,
                                   EphyAdaptiveMode  adaptive_mode)
{
  switch (adaptive_mode) {
    case EPHY_ADAPTIVE_MODE_NORMAL:
      gtk_revealer_set_reveal_child (GTK_REVEALER (action_bar), FALSE);

      break;
    case EPHY_ADAPTIVE_MODE_NARROW:
      gtk_revealer_set_reveal_child (GTK_REVEALER (action_bar), TRUE);

      break;
  }
}