~tristan957/harvest-almanac

ref: 0f0fa7109d3fb40354c03f4ba0fcda63df8280e4 harvest-almanac/harvest-almanac/hal-window.c -rw-r--r-- 6.3 KiB
0f0fa710Tristan Partin harvest-glib: fix implicit fallthrough 1 year, 8 months ago
                                                                                
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
#include "config.h"

#include <glib-object.h>
#include <gtk/gtk.h>
#include <handy.h>

#include "hal-context.h"
#include "hal-profile.h"
#include "hal-time-tracker.h"
#include "hal-window.h"

extern HalContext *CONTEXT;

struct _HalWindow
{
	GtkApplicationWindow parent_instance;
};

typedef struct HalWindowPrivate
{
	HdyHeaderGroup *header_group;
	HdyLeaflet *content_leaflet;
	HdyLeaflet *header_leaflet;
	GtkHeaderBar *header_bar;
	GtkHeaderBar *sub_header_bar;
	GtkStackSidebar *stack_sidebar;
	GtkStack *stack;
	GtkButton *back_button;
	GtkStack *user_status_stack;
	GtkLabel *user_name_label;
	GtkLabel *company_label;
	HalTimeTracker *time_tracker;
	HalProfile *profile;
} HalWindowPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(HalWindow, hal_window, GTK_TYPE_APPLICATION_WINDOW)

static void
on_context_notify_user(
	G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GParamSpec *pspec, gpointer user_data)
{
	HalWindow *self		   = HAL_WINDOW(user_data);
	HalWindowPrivate *priv = hal_window_get_instance_private(self);
	HarvestUser *user	   = hal_context_get_user();

	if (user != NULL) {
		gtk_label_set_text(priv->user_name_label, g_strconcat(harvest_user_get_first_name(user),
													  " ", harvest_user_get_last_name(user), NULL));
		gtk_stack_set_visible_child_name(priv->user_status_stack, "logged-in");
	}
}

static void
on_contect_notify_computer(
	G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GParamSpec *pspec, gpointer user_data)
{
	HalWindow *self			= HAL_WINDOW(user_data);
	HalWindowPrivate *priv	= hal_window_get_instance_private(self);
	HarvestCompany *company = hal_context_get_company();

	if (company != NULL) {
		gtk_label_set_text(priv->company_label, harvest_company_get_name(company));
	}
}

static void
update_header_bar_title(HalWindow *self)
{
	HalWindowPrivate *priv = hal_window_get_instance_private(self);

	GtkWidget *child = gtk_stack_get_visible_child(priv->stack);
	GValue title	 = G_VALUE_INIT;
	g_value_init(&title, G_TYPE_STRING);
	gtk_container_child_get_property(GTK_CONTAINER(priv->stack), child, "title", &title);
	gtk_header_bar_set_title(priv->sub_header_bar, g_value_get_string(&title));
}

static void
update_focus(HalWindow *self)
{
	HalWindowPrivate *priv = hal_window_get_instance_private(self);

	GtkWidget *header_child = hdy_leaflet_get_visible_child(priv->header_leaflet);
	const HdyFold fold		= hdy_leaflet_get_fold(priv->header_leaflet);

	g_assert(header_child == NULL || GTK_IS_HEADER_BAR(header_child));

	hdy_header_group_set_focus(
		priv->header_group, fold == HDY_FOLD_FOLDED ? GTK_HEADER_BAR(header_child) : NULL);
}

static void
stack_notify_visible_child_cb(
	G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GParamSpec *pspec, gpointer user_data)
{
	HalWindow *self = HAL_WINDOW(user_data);

	update_header_bar_title(self);
}

static void
header_leaflet_notify_visible_child_cb(
	G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GParamSpec *pspec, gpointer user_data)
{
	HalWindow *self = HAL_WINDOW(user_data);

	update_focus(self);
}

static void
header_leaflet_notify_fold_cb(
	G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GParamSpec *pspec, gpointer user_data)
{
	HalWindow *self = HAL_WINDOW(user_data);

	update_focus(self);
}

static void
back_button_clicked_cb(G_GNUC_UNUSED GtkButton *widget, gpointer user_data)
{
	HalWindow *self		   = HAL_WINDOW(user_data);
	HalWindowPrivate *priv = hal_window_get_instance_private(self);

	hdy_leaflet_set_visible_child_name(priv->content_leaflet, "sidebar");
}

void
hal_window_show_content(HalWindow *self)
{
	HalWindowPrivate *priv = hal_window_get_instance_private(self);

	gtk_stack_set_visible_child_name(GTK_STACK(priv->profile), "profile");
}

void
hal_window_hide_content(HalWindow *self)
{
	HalWindowPrivate *priv = hal_window_get_instance_private(self);

	gtk_stack_set_visible_child_name(priv->stack, "profile");
	gtk_stack_set_visible_child_name(GTK_STACK(priv->profile), "harvest-api-access-token");
}

static void
hal_window_finalize(GObject *obj)
{
	G_OBJECT_CLASS(hal_window_parent_class)->finalize(obj);
}

static void
hal_window_class_init(HalWindowClass *klass)
{
	GObjectClass *obj_class	  = G_OBJECT_CLASS(klass);
	GtkWidgetClass *wid_class = GTK_WIDGET_CLASS(klass);

	obj_class->finalize = hal_window_finalize;

	gtk_widget_class_set_template_from_resource(
		wid_class, "/io/partin/tristan/HarvestAlmanac/ui/hal-window.ui");
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, header_group);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, content_leaflet);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, header_leaflet);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, header_bar);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, sub_header_bar);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, stack_sidebar);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, stack);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, back_button);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, user_status_stack);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, user_name_label);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, company_label);
	gtk_widget_class_bind_template_callback(wid_class, header_leaflet_notify_fold_cb);
	gtk_widget_class_bind_template_callback(wid_class, header_leaflet_notify_visible_child_cb);
	gtk_widget_class_bind_template_callback(wid_class, stack_notify_visible_child_cb);
	gtk_widget_class_bind_template_callback(wid_class, back_button_clicked_cb);
}

static void
hal_window_init(HalWindow *self)
{
	HalWindowPrivate *priv = hal_window_get_instance_private(self);

	gtk_widget_init_template(GTK_WIDGET(self));

	priv->profile	   = hal_profile_new();
	priv->time_tracker = hal_time_tracker_new();

	gtk_stack_add_titled(priv->stack, GTK_WIDGET(priv->profile), "profile", "Profile");
	gtk_stack_add_titled(priv->stack, GTK_WIDGET(priv->time_tracker), "time-tracker", "Time");

	update_header_bar_title(self);

	hdy_leaflet_set_visible_child_name(priv->content_leaflet, "content");

	g_signal_connect(CONTEXT, "notify::user", G_CALLBACK(on_context_notify_user), self);
	g_signal_connect(CONTEXT, "notify::company", G_CALLBACK(on_contect_notify_computer), self);
}

HalWindow *
hal_window_new(GApplication *app)
{
	return g_object_new(HAL_TYPE_WINDOW, "application", app, NULL);
}