Debian Codegraph @

src/raven/widgets/calendar/calendar.vala

raw
1 /*
2 * This file is part of budgie-desktop
3 *
4 * Copyright Budgie Desktop Developers
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12 [DBus (name="org.budgie_desktop.Raven")]
13 public interface RavenToCalendarRemote : GLib.Object {
14 public signal void ExpansionChanged(bool is_expanded);
15 }
16
17 public const string RAVEN_DBUS_NAME = "org.budgie_desktop.Raven";
18 public const string RAVEN_DBUS_OBJECT_PATH = "/org/budgie_desktop/Raven";
19
20 public class CalendarRavenPlugin : Budgie.RavenPlugin, Peas.ExtensionBase {
21 public Budgie.RavenWidget new_widget_instance(string uuid, GLib.Settings? settings) {
22 return new CalendarRavenWidget(uuid, settings);
23 }
24
25 public bool supports_settings() {
26 return true;
27 }
28 }
29
30 public class CalendarRavenWidget : Budgie.RavenWidget {
31 private Gtk.Box? header = null;
32 private Gtk.Button? header_reveal_button = null;
33 private Gtk.Revealer? content_revealer = null;
34 private Gtk.Box? content = null;
35 private Gtk.Label? header_label = null;
36 private Gtk.Box? main_box = null;
37 private Gtk.Calendar? cal = null;
38 RavenToCalendarRemote? raven_proxy = null;
39
40 private const string date_format = "%e %b %Y";
41
42 public CalendarRavenWidget(string uuid, GLib.Settings? settings) {
43 initialize(uuid, settings);
44
45 main_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
46 add(main_box);
47
48 header = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
49 header.get_style_context().add_class("raven-header");
50 main_box.add(header);
51
52 var icon = new Gtk.Image.from_icon_name("x-office-calendar-symbolic", Gtk.IconSize.MENU);
53 icon.margin = 4;
54 icon.margin_start = 12;
55 icon.margin_end = 10;
56 header.add(icon);
57
58 var time = new DateTime.now_local();
59 header_label = new Gtk.Label(time.format(date_format));
60 header.add(header_label);
61
62 content = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
63 content.get_style_context().add_class("raven-background");
64
65 content_revealer = new Gtk.Revealer();
66 content_revealer.add(content);
67 content_revealer.reveal_child = true;
68 main_box.add(content_revealer);
69
70 header_reveal_button = new Gtk.Button.from_icon_name("pan-down-symbolic", Gtk.IconSize.MENU);
71 header_reveal_button.get_style_context().add_class("flat");
72 header_reveal_button.get_style_context().add_class("expander-button");
73 header_reveal_button.margin = 4;
74 header_reveal_button.valign = Gtk.Align.CENTER;
75 header_reveal_button.clicked.connect(() => {
76 content_revealer.reveal_child = !content_revealer.child_revealed;
77 var image = (Gtk.Image?) header_reveal_button.get_image();
78 if (content_revealer.reveal_child) {
79 image.set_from_icon_name("pan-down-symbolic", Gtk.IconSize.MENU);
80 } else {
81 image.set_from_icon_name("pan-end-symbolic", Gtk.IconSize.MENU);
82 }
83 });
84 header.pack_end(header_reveal_button, false, false, 0);
85
86 cal = new Gtk.Calendar();
87 cal.get_style_context().add_class("raven-calendar");
88 content.add(cal);
89
90 cal.month_changed.connect(() => {
91 update_selection();
92 });
93
94 settings.changed.connect(settings_updated);
95 settings_updated("show-week-numbers");
96 settings_updated("show-day-names");
97
98 show_all();
99 Bus.get_proxy.begin<RavenToCalendarRemote>(BusType.SESSION, RAVEN_DBUS_NAME, RAVEN_DBUS_OBJECT_PATH, 0, null, on_raven_get);
100 }
101
102 /* Hold onto our Raven proxy ref */
103 void on_raven_get(Object? o, AsyncResult? res) {
104 try {
105 raven_proxy = Bus.get_proxy.end(res);
106 raven_proxy.ExpansionChanged.connect((is_expanded) => on_visibility_changed(is_expanded));
107 } catch (Error e) {
108 warning("Failed to get Raven proxy: %s", e.message);
109 }
110 }
111
112 private void settings_updated(string key) {
113 if (key == "show-week-numbers") {
114 cal.show_week_numbers = get_instance_settings().get_boolean(key);
115 } else if (key == "show-day-names") {
116 cal.show_day_names = get_instance_settings().get_boolean(key);
117 }
118 }
119
120 private bool on_visibility_changed(bool is_expanded) {
121 if (!is_expanded) return true;
122 var time = new DateTime.now_local();
123 cal.select_month(time.get_month()-1, time.get_year());
124 cal.day = time.get_day_of_month();
125 return true;
126 }
127
128 private bool update_selection() {
129 var time = new DateTime.now_local();
130 var strf = time.format(date_format);
131 header_label.label = strf;
132 cal.day = (cal.month + 1) == time.get_month() && cal.year == time.get_year() ? time.get_day_of_month() : 0;
133 return true;
134 }
135
136 public override Gtk.Widget build_settings_ui() {
137 return new CalendarRavenWidgetSettings(get_instance_settings());
138 }
139 }
140
141 [GtkTemplate (ui="/org/buddiesofbudgie/budgie-desktop/raven/widget/Calendar/settings.ui")]
142 public class CalendarRavenWidgetSettings : Gtk.Grid {
143 [GtkChild]
144 private unowned Gtk.Switch? switch_show_day_names;
145 [GtkChild]
146 private unowned Gtk.Switch? switch_show_week_numbers;
147
148 public CalendarRavenWidgetSettings(Settings? settings) {
149 settings.bind("show-day-names", switch_show_day_names, "active", SettingsBindFlags.DEFAULT);
150 settings.bind("show-week-numbers", switch_show_week_numbers, "active", SettingsBindFlags.DEFAULT);
151 }
152 }
153
154 [ModuleInit]
155 public void peas_register_types(TypeModule module) {
156 // boilerplate - all modules need this
157 var objmodule = module as Peas.ObjectModule;
158 objmodule.register_extension_type(typeof(Budgie.RavenPlugin), typeof(CalendarRavenPlugin));
159 }