aboutsummaryrefslogtreecommitdiff
path: root/src/dash-box.vala
blob: 4d907dbfa212127f99851fa2e035e44006565430 (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 4 -*-
 *
 * Copyright (C) 2011,2012 Canonical Ltd
 * Copyright (C) 2015 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
 * Copyright (C) 2023 Robert Tari
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Michael Terry <michael.terry@canonical.com>
 *          Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
 *          Robert Tari <robert@tari.in>
 */

public class DashBox : Gtk.Box
{
    public Background? background { get; construct; default = null; }

    public bool has_base { get; private set; default = false; }
    public double base_alpha { get; private set; default = 1.0; }

    private enum Mode
    {
        NORMAL,
        PUSH_FADE_OUT,
        PUSH_FADE_IN,
        POP_FADE_OUT,
        POP_FADE_IN,
    }

    private GreeterList pushed;
    private Gtk.Widget orig = null;
    private FadeTracker orig_tracker;
    private int orig_height = -1;
    private Mode mode;

    public DashBox (Background bg)
    {
        Object (background: bg);
    }

    construct
    {
        mode = Mode.NORMAL;
    }

    /* Does not actually add w to this widget, as doing so would potentially mess with w's placement. */
    public void set_base (Gtk.Widget? w)
    {
        var greeter = new ArcticaGreeter ();
        if (!greeter.test_mode) {
            return_if_fail (pushed == null);
            return_if_fail (mode == Mode.NORMAL);
        }

        if (orig != null)
            orig.size_allocate.disconnect (base_size_allocate_cb);
        orig = w;

        if (orig != null)
        {
            orig.size_allocate.connect (base_size_allocate_cb);
            orig_tracker = new FadeTracker (orig);
            orig_tracker.notify["alpha"].connect (() =>
            {
                base_alpha = orig_tracker.alpha;
                queue_draw ();
            });
            orig_tracker.done.connect (fade_done_cb);
            base_alpha = orig_tracker.alpha;
            has_base = true;
        }
        else
        {
            orig_height = -1;
            get_preferred_height (null, out orig_height); /* save height */

            orig_tracker = null;
            base_alpha = 1.0;
            has_base = false;
        }

        queue_resize ();
    }

    public void push (GreeterList l)
    {
        /* This isn't designed to push more than one widget at a time yet */
        return_if_fail (pushed == null);
        return_if_fail (orig != null);
        return_if_fail (mode == Mode.NORMAL);

        get_preferred_height (null, out orig_height);
        pushed = l;
        pushed.fade_done.connect (fade_done_cb);
        mode = Mode.PUSH_FADE_OUT;
        orig_tracker.reset (FadeTracker.Mode.FADE_OUT);
        queue_resize ();
    }

    public void pop ()
    {
        return_if_fail (pushed != null);
        return_if_fail (orig != null);
        return_if_fail (mode == Mode.NORMAL);

        mode = Mode.POP_FADE_OUT;
        pushed.fade_out ();
    }

    private void fade_done_cb ()
    {
        switch (mode)
        {
        case Mode.PUSH_FADE_OUT:
            mode = Mode.PUSH_FADE_IN;
            orig.hide ();
            pushed.fade_in ();
            break;
        case Mode.PUSH_FADE_IN:
            mode = Mode.NORMAL;
            pushed.grab_focus ();
            break;
        case Mode.POP_FADE_OUT:
            mode = Mode.POP_FADE_IN;
            orig_tracker.reset (FadeTracker.Mode.FADE_IN);
            orig.show ();
            break;
        case Mode.POP_FADE_IN:
            mode = Mode.NORMAL;
            pushed.fade_done.disconnect (fade_done_cb);
            pushed.destroy ();
            pushed = null;
            queue_resize ();
            orig.grab_focus ();
            break;
        default:
            return;
        }
    }

    private void base_size_allocate_cb ()
    {
        queue_resize ();
    }

    public override void get_preferred_height (out int min, out int nat)
    {
        if (orig == null)
        {
            /* Return cached height if we have it. This makes transitions between two base widgets smoother. */
            if (orig_height >= 0)
            {
                min = orig_height;
                nat = orig_height;
            }
            else
            {
                min = grid_size * GreeterList.DEFAULT_BOX_HEIGHT - GreeterList.BORDER * 2;
                nat = grid_size * GreeterList.DEFAULT_BOX_HEIGHT - GreeterList.BORDER * 2;
            }
        }
        else
        {
            if (pushed == null)
                orig.get_preferred_height (out min, out nat);
            else
            {
                pushed.selected_entry.get_preferred_height (out min, out nat);
                min = int.max (orig_height, min);
                nat = int.max (orig_height, nat);
            }
        }
    }

    public override void get_preferred_width (out int min, out int nat)
    {
        min = grid_size * GreeterList.BOX_WIDTH - GreeterList.BORDER * 2;
        nat = grid_size * GreeterList.BOX_WIDTH - GreeterList.BORDER * 2;
    }

    public override bool draw (Cairo.Context c)
    {
        if (background != null)
        {
            int x, y;
            background.translate_coordinates (this, 0, 0, out x, out y);
            c.save ();
            c.translate (x, y);
            background.draw_full (c, Background.DrawFlags.NONE);
            c.restore ();
        }

        /* Draw darker background with a rounded border */
        var box_r = 0.3 * grid_size;
        int box_y = 0;
        int box_w;
        int box_h;
        get_preferred_width (null, out box_w);
        get_preferred_height (null, out box_h);

        if (mode == Mode.PUSH_FADE_OUT)
        {
            /* Grow dark bg to fit new pushed object */
            var new_box_h = box_h - (int) ((box_h - orig_height) * base_alpha);
            box_h = new_box_h;
        }
        else if (mode == Mode.POP_FADE_IN)
        {
            /* Shrink dark bg to fit orig */
            var new_box_h = box_h - (int) ((box_h - orig_height) * base_alpha);
            box_h = new_box_h;
        }

        c.save ();

        CairoUtils.rounded_rectangle (c, 0, box_y, box_w, box_h, box_r);

        var agsettings = new AGSettings ();
        if (agsettings.high_contrast)
        {
            c.set_source_rgba (1.0, 1.0, 1.0, 1.0);
        }
        else
        {
            c.set_source_rgba (0.1, 0.1, 0.1, 0.4);
        }
        c.fill_preserve ();

        if (agsettings.high_contrast)
        {
            c.set_source_rgba (0.0, 0.0, 0.0, 1.0);
        }
        else
        {
            c.set_source_rgba (0.4, 0.4, 0.4, 0.4);
        }
        c.set_line_width (1);
        c.stroke ();

        c.restore ();

        return base.draw (c);
    }
}