|
| 1 | +namespace SwayNotificationCenter.Widgets { |
| 2 | + public class Calendar : BaseWidget { |
| 3 | + public override string widget_name { |
| 4 | + get { |
| 5 | + return "calendar"; |
| 6 | + } |
| 7 | + } |
| 8 | + |
| 9 | + Gtk.Calendar calendar; |
| 10 | + Gtk.Label date_label; |
| 11 | + Gtk.Label day_label; |
| 12 | + |
| 13 | + string day_format = "%A"; |
| 14 | + string date_format = "%B %-d, %Y"; |
| 15 | + |
| 16 | + public Calendar (string suffix) { |
| 17 | + base (suffix); |
| 18 | + |
| 19 | + Json.Object ?config = get_config (this); |
| 20 | + |
| 21 | + var container = new Gtk.Box (Gtk.Orientation.VERTICAL, 6); |
| 22 | + container.add_css_class ("calendar-container"); |
| 23 | + container.set_hexpand (true); |
| 24 | + |
| 25 | + day_label = new Gtk.Label (null); |
| 26 | + day_label.add_css_class ("day-label"); |
| 27 | + day_label.set_halign (Gtk.Align.START); |
| 28 | + |
| 29 | + date_label = new Gtk.Label (null); |
| 30 | + date_label.add_css_class ("date-label"); |
| 31 | + date_label.set_halign (Gtk.Align.START); |
| 32 | + |
| 33 | + calendar = new Gtk.Calendar (); |
| 34 | + calendar.set_hexpand (true); |
| 35 | + |
| 36 | + if (config != null) { |
| 37 | + bool show_day_label_found; |
| 38 | + bool ?show_day_label = get_prop<bool> (config, "show-day-label", |
| 39 | + out show_day_label_found); |
| 40 | + if (show_day_label_found && show_day_label == false) { |
| 41 | + day_label.set_visible (false); |
| 42 | + } |
| 43 | + |
| 44 | + bool show_date_label_found; |
| 45 | + bool ?show_date_label = get_prop<bool> (config, "show-date-label", |
| 46 | + out show_date_label_found); |
| 47 | + if (show_date_label_found && show_date_label == false) { |
| 48 | + date_label.set_visible (false); |
| 49 | + } |
| 50 | + |
| 51 | + bool show_heading_found; |
| 52 | + bool ?show_heading = get_prop<bool> (config, "show-heading", |
| 53 | + out show_heading_found); |
| 54 | + if (show_heading_found) { |
| 55 | + calendar.show_heading = show_heading; |
| 56 | + } |
| 57 | + |
| 58 | + bool show_day_names_found; |
| 59 | + bool ?show_day_names = get_prop<bool> (config, "show-day-names", |
| 60 | + out show_day_names_found); |
| 61 | + if (show_day_names_found) { |
| 62 | + calendar.show_day_names = show_day_names; |
| 63 | + } |
| 64 | + |
| 65 | + bool show_week_numbers_found; |
| 66 | + bool ?show_week_numbers = get_prop<bool> (config, "show-week-numbers", |
| 67 | + out show_week_numbers_found); |
| 68 | + if (show_week_numbers_found) { |
| 69 | + calendar.show_week_numbers = show_week_numbers; |
| 70 | + } |
| 71 | + |
| 72 | + string ?df = get_prop<string> (config, "day-format"); |
| 73 | + if (df != null) { |
| 74 | + day_format = df; |
| 75 | + } |
| 76 | + |
| 77 | + string ?dtf = get_prop<string> (config, "date-format"); |
| 78 | + if (dtf != null) { |
| 79 | + date_format = dtf; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + mark_today (); |
| 84 | + |
| 85 | + container.append (day_label); |
| 86 | + container.append (date_label); |
| 87 | + container.append (calendar); |
| 88 | + append (container); |
| 89 | + |
| 90 | + calendar.day_selected.connect (update_labels); |
| 91 | + calendar.next_month.connect (update_labels); |
| 92 | + calendar.prev_month.connect (update_labels); |
| 93 | + update_labels (); |
| 94 | + } |
| 95 | + |
| 96 | + void mark_today () { |
| 97 | + calendar.clear_marks (); |
| 98 | + var now = new DateTime.now_local (); |
| 99 | + calendar.mark_day ((uint) now.get_day_of_month ()); |
| 100 | + } |
| 101 | + |
| 102 | + void update_labels () { |
| 103 | + var selected = calendar.get_date (); |
| 104 | + var dt = new DateTime.local ((int) selected.get_year (), |
| 105 | + (int) selected.get_month (), |
| 106 | + (int) selected.get_day_of_month (), |
| 107 | + 0, 0, 0.0); |
| 108 | + day_label.set_label (dt.format (day_format)); |
| 109 | + date_label.set_label (dt.format (date_format)); |
| 110 | + } |
| 111 | + |
| 112 | + public override void on_cc_visibility_change (bool value) { |
| 113 | + if (value) { |
| 114 | + mark_today (); |
| 115 | + update_labels (); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments