~hkdb/geary/disco

« back to all changes in this revision

Viewing changes to src/client/components/count-badge.vala

  • Committer: hkdb
  • Date: 2019-09-26 19:40:48 UTC
  • Revision ID: hkdb@3df.io-20190926194048-n0vggm3yfo8p1ubr
Tags: upstream-3.32.2-disco
ImportĀ upstreamĀ versionĀ 3.32.2-disco

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2016 Software Freedom Conservancy Inc.
 
2
 *
 
3
 * This software is licensed under the GNU Lesser General Public License
 
4
 * (version 2.1 or later).  See the COPYING file in this distribution.
 
5
 */
 
6
 
 
7
/**
 
8
 * Draws the count badge and calculates its dimensions.
 
9
 */
 
10
public class CountBadge : Geary.BaseObject {
 
11
    public const string UNREAD_BG_COLOR = "#888888";
 
12
 
 
13
    private const int FONT_SIZE_MESSAGE_COUNT = 8;
 
14
 
 
15
    public int count { get; set; default = 0; }
 
16
 
 
17
    private int min = 0;
 
18
 
 
19
    /**
 
20
     * Creates a count badge.
 
21
     * @param min Minimum count to draw.
 
22
     */
 
23
    public CountBadge(int min) {
 
24
        this.min = min;
 
25
    }
 
26
 
 
27
    public int get_width(Gtk.Widget widget) {
 
28
        int width = 0;
 
29
        render_internal(widget, null, 0, 0, false, out width, null);
 
30
 
 
31
        return width;
 
32
    }
 
33
 
 
34
    public int get_height(Gtk.Widget widget) {
 
35
        int height = 0;
 
36
        render_internal(widget, null, 0, 0, false, null, out height);
 
37
 
 
38
        return height;
 
39
    }
 
40
 
 
41
    public void render(Gtk.Widget widget, Cairo.Context? ctx, int x, int y, bool selected) {
 
42
        render_internal(widget, ctx, x, y, selected, null, null);
 
43
    }
 
44
 
 
45
    private void render_internal(Gtk.Widget widget, Cairo.Context? ctx, int x, int y, bool selected,
 
46
        out int? width, out int? height) {
 
47
        if (count < min) {
 
48
            width = 0;
 
49
            height = 0;
 
50
 
 
51
            return;
 
52
        }
 
53
 
 
54
        string mails =
 
55
            "<span foreground='white' font='%d' weight='bold'> %d </span>"
 
56
            .printf(FONT_SIZE_MESSAGE_COUNT, count);
 
57
 
 
58
        Pango.Layout layout_num = widget.create_pango_layout(null);
 
59
        layout_num.set_markup(mails, -1);
 
60
        layout_num.set_alignment(Pango.Alignment.RIGHT);
 
61
 
 
62
        Pango.Rectangle? ink_rect;
 
63
        Pango.Rectangle? logical_rect;
 
64
        layout_num.get_pixel_extents(out ink_rect, out logical_rect);
 
65
        if (ctx != null) {
 
66
            double bg_width = logical_rect.width + FormattedConversationData.LINE_SPACING;
 
67
            double bg_height = logical_rect.height;
 
68
            double radius = bg_height / 2.0;
 
69
            double degrees = Math.PI / 180.0;
 
70
 
 
71
            // Create rounded rect.
 
72
            ctx.new_sub_path();
 
73
            ctx.arc(x + bg_width - radius,  y + radius, radius, -90 * degrees, 0 * degrees);
 
74
            ctx.arc(x + bg_width - radius,  y + bg_height - radius, radius, 0 * degrees, 90 * degrees);
 
75
            ctx.arc(x + radius, y + bg_height - radius, radius, 90 * degrees, 180 * degrees);
 
76
            ctx.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
 
77
            ctx.close_path();
 
78
 
 
79
            // Colorize our shape.
 
80
            GtkUtil.set_source_color_from_string(ctx, UNREAD_BG_COLOR);
 
81
            ctx.fill_preserve();
 
82
            ctx.set_line_width(2.0);
 
83
            ctx.stroke();
 
84
 
 
85
            // Center the text.
 
86
            ctx.move_to(x + (bg_width / 2) - logical_rect.width / 2, y);
 
87
            Pango.cairo_show_layout(ctx, layout_num);
 
88
        }
 
89
 
 
90
        width = logical_rect.width + FormattedConversationData.LINE_SPACING;
 
91
        height = logical_rect.height;
 
92
    }
 
93
}