summaryrefslogtreecommitdiff
path: root/src/transitRouteLabel.js
blob: 680d18b014037aa209d33289fa641a1b2b59070d (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
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
/* vim: set et ts=4 sw=4: */
/*
 * Copyright (c) 2017 Marcus Lundblad.
 *
 * GNOME Maps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * GNOME Maps 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 GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Marcus Lundblad <ml@update.uu.se>
 */

import Cairo from 'cairo';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Graphene from 'gi://Graphene';
import Gtk from 'gi://Gtk';

import * as Color from './color.js';
import * as Gfx from './gfx.js';
import * as Utils from './utils.js';

/* threashhold for route color luminance when we consider it more or less
 * as white, and draw an outline around the label
 */
const OUTLINE_LUMINANCE_THREASHHOLD = 0.9;
/* the threashhold when using a dark theme, when the color is darker draw an
 * outline around the label
 */
const DARK_OUTLINE_LUMINANCE_THREASHHOLD = 0.1;

// fallback high contrast colors
const HIGH_CONTRAST_COLOR = '000000';
const HIGH_CONTRAST_TEXT_COLOR = 'ffffff';

export class TransitRouteLabel extends Gtk.Label {

    constructor({leg, compact, ...params}) {
        super(params);

        this._setLabel(leg, compact);
    }

    _setLabel(leg, compact) {
        let color = leg.color;
        let textColor = leg.textColor;
        let label = leg.route;
        let usingDarkTheme = Utils.isUsingDarkThemeVariant();
        let usingHighContrastTheme = Utils.isUsingHighContrastTheme();

        textColor = Color.getContrastingForegroundColor(color, textColor);

        /* if using the high contrast theme and a label is set, fallback to
         * hight-contrasting colors, if no label, assume the route color is
         * more relevant and keep it also for high contrast
         */
        if (usingHighContrastTheme && ((compact && leg.compactRoute) ||
                                       (!compact && label))) {
            color = HIGH_CONTRAST_COLOR;
            textColor = HIGH_CONTRAST_TEXT_COLOR;
        }

        this._color = color;
        this._textColor = textColor;

        if ((!usingDarkTheme &&
             Color.relativeLuminance(color) > OUTLINE_LUMINANCE_THREASHHOLD) ||
            (usingDarkTheme &&
             Color.relativeLuminance(color) < DARK_OUTLINE_LUMINANCE_THREASHHOLD)) {
            this._hasOutline = true;
        }

        /* for compact (overview) mode, try to shorten the label if the route
         * name was more than 6 characters
         */
        if (compact && label.length > 6)
            label = leg.compactRoute;

        if (compact) {
            /* restrict number of characters shown in the label when compact mode
             * is requested
             */
            this.max_width_chars = 6;
        } else if (leg && !leg.headsign) {
            // if there is no trip headsign to display, allow more space
            this.max_width_chars = 25;
        }

        this.label = '<span foreground="#%s">%s</span>'.format(
                                        textColor,
                                        GLib.markup_escape_text(label, -1));

        this.queue_draw();
    }

    /* I didn't find any easy/obvious way to override widget background color
     * and getting rounded corner just using CSS styles, so doing a custom
     * Cairo drawing of a "roundrect"
     */
    vfunc_snapshot(snapshot) {
        let {x, y, width, height} = this.get_allocation();
        let rect = new Graphene.Rect();

        rect.init(0, 0, width, height);

        let cr = snapshot.append_cairo(rect);

        // clip off the badge, this seems to avoid some extra space at right/bottom with the CSS
        Gfx.drawColoredBagde(cr, this._color,
                             this._hasOutline ? this._textColor : null,
                             0, 0, width - 3, height - 3);
        super.vfunc_snapshot(snapshot);
    }
}

GObject.registerClass({
    Template: 'resource:///org/gnome/Maps/ui/transit-route-label.ui',
}, TransitRouteLabel);