~tristan957/harvest-almanac

ref: 44f7d9406abcfc7592b91b50f836efb14553a9d1 harvest-almanac/harvest-almanac/hal-window.c -rw-r--r-- 5.1 KiB
44f7d940Tristan Partin harvest-almanac: updated version in about dialog 2 years 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
#include "config.h"

#define G_LOG_DOMAIN "HalWindow"

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

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

struct _HalWindow
{
	GtkApplicationWindow parent_instance;
};

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

G_DEFINE_TYPE_WITH_PRIVATE(HalWindow, hal_window, GTK_TYPE_APPLICATION_WINDOW)

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) // update
{
	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 *object_class = G_OBJECT_CLASS(klass);
	GtkWidgetClass *wid_class  = GTK_WIDGET_CLASS(klass);

	object_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);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, sidebar);
	gtk_widget_class_bind_template_child_private(wid_class, HalWindow, back_button);
	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");
	gtk_container_child_set(GTK_CONTAINER(priv->stack), GTK_WIDGET(priv->profile), "icon-name",
		"user-info-symbolic", NULL);
	gtk_container_child_set(GTK_CONTAINER(priv->stack), GTK_WIDGET(priv->time_tracker), "icon-name",
		"document-open-recent-symbolic", NULL);

	update_header_bar_title(self);

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

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