File: DatePicker.vala

package info (click to toggle)
granite 6.2.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,748 kB
  • sloc: python: 10; makefile: 8
file content (148 lines) | stat: -rw-r--r-- 4,420 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2019 elementary, Inc. (https://elementary.io)
 * Copyright 2011–2013 Maxwell Barvian <maxwell@elementaryos.org>
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

namespace Granite.Widgets {

    /**
     * This class allows users to pick dates from a calendar.
     */
    public class DatePicker : Gtk.Entry, Gtk.Buildable {

        const int OFFSET = 15;
        const int MARGIN = 6;
        // Signals
        /**
         * Sent when the date got changed
         */
        public signal void date_changed ();

        /**
         * Desired format of DatePicker
         */
        public string format { get; construct; }

        /**
         * Dropdown of DatePicker
         */
        protected Gtk.EventBox dropdown;
        /**
         * The Calendar to create the DatePicker
         */
        protected Gtk.Calendar calendar;

        private Gtk.Popover popover;

        private GLib.DateTime _date;

        private bool proc_next_day_selected = true;

        /**
         * Current Date
         */
        public GLib.DateTime date {
            get { return _date; }
            set {
                _date = value;
                text = _date.format (format);
                proc_next_day_selected = false;
                calendar.select_month (value.get_month () - 1, value.get_year ());
                proc_next_day_selected = false;
                calendar.select_day (value.get_day_of_month ());
                date_changed ();
            }
        }

        /**
         * Makes new DatePicker
         */
        construct {
            if (format == null)
                format = Granite.DateTime.get_default_date_format (false, true, true);

            dropdown = new Gtk.EventBox ();
            dropdown.margin = MARGIN;
            popover = new Gtk.Popover (this);
            popover.add (dropdown);
            calendar = new Gtk.Calendar ();
            date = new GLib.DateTime.now_local ();

            // Entry properties
            can_focus = false;
            editable = false; // user can't edit the entry directly
            secondary_icon_gicon = new ThemedIcon.with_default_fallbacks ("office-calendar-symbolic");

            dropdown.add_events (Gdk.EventMask.FOCUS_CHANGE_MASK);
            dropdown.add (calendar);

            // Signals and callbacks
            icon_release.connect (on_icon_press);
            calendar.day_selected.connect (on_calendar_day_selected);

            /*
             * A next/prev month/year event
             * also triggers a day selected event,
             * so stop the next day selected event
             * from setting the date and closing
             * the calendar.
             */
            calendar.next_month.connect (() => {
                proc_next_day_selected = false;
            });

            calendar.next_year.connect (() => {
                proc_next_day_selected = false;
            });

            calendar.prev_month.connect (() => {
                proc_next_day_selected = false;
            });

            calendar.prev_year.connect (() => {
                proc_next_day_selected = false;
            });
        }

        /**
         * Makes a new DatePicker
         *
         * @param format desired format of new DatePicker
         */
        public DatePicker.with_format (string format) {
            Object (format: format);
        }

        private void on_icon_press (Gtk.EntryIconPosition position) {
            Gdk.Rectangle rect;
            position_dropdown (out rect);
            popover.pointing_to = rect;
            popover.position = Gtk.PositionType.BOTTOM;
            popover.show_all ();
            calendar.grab_focus ();
        }

        protected virtual void position_dropdown (out Gdk.Rectangle rect) {
            Gtk.Allocation size;
            get_allocation (out size);

            rect = Gdk.Rectangle ();
            rect.x = size.width - OFFSET;
            rect.y = size.height;
        }

        private void on_calendar_day_selected () {
            if (proc_next_day_selected) {
                date = new GLib.DateTime.local (calendar.year, calendar.month + 1, calendar.day, 0, 0, 0);
                hide_dropdown ();
            } else {
                proc_next_day_selected = true;
            }
        }

        private void hide_dropdown () {
            popover.hide ();
        }
    }
}