File: theme_button.vala

package info (click to toggle)
deepin-terminal 3.2.1.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,764 kB
  • sloc: ansic: 128; exp: 25; sh: 18; makefile: 15
file content (219 lines) | stat: -rw-r--r-- 9,244 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
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
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2011 ~ 2018 Deepin, Inc.
 *               2011 ~ 2018 Wang Yong
 *
 * Author:     Wang Yong <wangyong@deepin.com>
 * Maintainer: Wang Yong <wangyong@deepin.com>
 *
 * This program 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 3 of the License, or
 * any later version.
 *
 * 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/>.
 */

using Gee;
using Gtk;
using Widgets;

namespace Widgets {
    public class ThemeButton : Gtk.EventBox {
        public Cairo.ImageSurface active_theme_border_surface;
        public Cairo.ImageSurface dark_theme_border_surface;
        public Cairo.ImageSurface light_theme_border_surface;
        public Gdk.RGBA background_color;
        public Gdk.RGBA prompt_color_host;
        public Gdk.RGBA prompt_color_path;
        public Gdk.RGBA foreground_color;
        public KeyFile theme_file;
        public bool is_active = false;
        public bool is_light_color;
        public int background_padding = 2;
        public int border_padding = 1;
        public int button_radius = 5;
        public int content_font_size = 11;
        public int content_padding_x = 14;
        public int content_padding_y = 25;
        public int title_font_size = 11;
        public int title_padding_x = 14;
        public int title_padding_y = 6;
        public string theme_name;

        public ThemeButton(string name) {
            theme_name = name;

            try {
                theme_file = new KeyFile();
                theme_file.load_from_file(Utils.get_theme_path(theme_name), KeyFileFlags.NONE);
                background_color = Utils.hex_to_rgba(theme_file.get_string("theme", "background").strip());
                is_light_color = Utils.is_light_color(theme_file.get_string("theme", "background").strip());

                background_color.alpha = 0.8;
                foreground_color = Utils.hex_to_rgba(theme_file.get_string("theme", "foreground").strip());
                prompt_color_host = Utils.hex_to_rgba(theme_file.get_string("theme", "color_11").strip());
                prompt_color_path = Utils.hex_to_rgba(theme_file.get_string("theme", "color_13").strip());

                dark_theme_border_surface = Utils.create_image_surface("dark_theme_border.svg");
                light_theme_border_surface = Utils.create_image_surface("light_theme_border.svg");
                active_theme_border_surface = Utils.create_image_surface("active_theme_border.svg");
            } catch (Error e) {
                print("ThemeButton: %s\n", e.message);
            }

            visible_window = false;

            set_size_request(Constant.THEME_BUTTON_WIDTH, Constant.THEME_BUTTON_HEIGHT);
            margin_start = (Constant.THEME_SLIDER_WIDTH - Constant.THEME_BUTTON_WIDTH) / 2;
            margin_end = (Constant.THEME_SLIDER_WIDTH - Constant.THEME_BUTTON_WIDTH) / 2;

            draw.connect(on_draw);
        }

        public void active() {
            is_active = true;

            queue_draw();
        }

        public void inactive() {
            is_active = false;

            queue_draw();
        }

        private bool on_draw(Gtk.Widget widget, Cairo.Context cr) {
            Gtk.Allocation rect;
            widget.get_allocation(out rect);

            int clip_width = rect.width - background_padding * 2;
            int clip_height = rect.height - background_padding * 2;

            cr.save();
            // Clip round rectangle when DPI > 1, for perfect radius effect.
            if (get_scale_factor() > 1) {
                Draw.clip_rounded_rectangle(
                    cr,
                    background_padding,
                    background_padding,
                    clip_width,
                    clip_height,
                    button_radius / get_scale_factor() + 1);
            }
            cr.set_source_rgba(background_color.red, background_color.green, background_color.blue, background_color.alpha);
            Draw.fill_rounded_rectangle(
                cr,
                background_padding,
                background_padding,
                clip_width,
                clip_height,
                button_radius / get_scale_factor());
            cr.restore();

            string prompt_host = "dde@linux:", prompt_path = "~/Theme";

            Draw.set_context_source_color(cr, prompt_color_host);
            Draw.draw_text(cr, prompt_host, title_padding_x, title_padding_y, rect.width, rect.height, title_font_size, Pango.Alignment.LEFT, "top");
            int prompt_host_width = Draw.get_text_render_width(cr, prompt_host, clip_width, clip_height, title_font_size);

            Draw.set_context_source_color(cr, prompt_color_path);
            Draw.draw_text(cr, prompt_path, title_padding_x + prompt_host_width, title_padding_y, rect.width, rect.height, title_font_size, Pango.Alignment.LEFT, "top");
            int prompt_host_n_path_width = Draw.get_text_render_width(cr, prompt_path, clip_width, clip_height, title_font_size) + prompt_host_width;

            Draw.set_context_source_color(cr, foreground_color);
            Draw.draw_text(cr, "$ _", title_padding_x + prompt_host_n_path_width, title_padding_y, rect.width, rect.height, title_font_size, Pango.Alignment.LEFT, "top");

            Draw.set_context_source_color(cr, foreground_color);
            Draw.draw_text(cr, theme_name, content_padding_x, content_padding_y, rect.width, rect.height, content_font_size, Pango.Alignment.LEFT, "top");

            if (is_active) {
                Draw.draw_surface(cr, active_theme_border_surface);
            } else if (is_light_color) {
                Draw.draw_surface(cr, light_theme_border_surface, border_padding, border_padding);
            } else {
                Draw.draw_surface(cr, dark_theme_border_surface, border_padding, border_padding);
            }

            return true;
        }
    }

    public class ThemeList : Gtk.VBox {
        public int theme_button_padding = Constant.THEME_BUTTON_PADDING;
        public HashMap<string, ThemeButton> theme_button_map;
        public ThemeButton? active_theme_button = null;

        public signal void active_theme(string theme_name);

        public ThemeList(string default_theme) {
            theme_button_map = new HashMap<string, ThemeButton>();

            var theme_names = Utils.list_files(Utils.get_theme_dir());
            var additional_theme_names = Utils.list_files(Utils.get_additional_theme_dir());
            theme_names.add_all(additional_theme_names);
            theme_names.sort((CompareDataFunc) compare_color_brightness);
            foreach (string theme_name in theme_names) {
                var button = new Widgets.ThemeButton(theme_name);
                pack_start(button, false, false, theme_button_padding);

                button.button_press_event.connect((w, e) => {
                        if (Utils.is_left_button(e)) {
                            active_button(theme_name);
                            active_theme(theme_name);
                        }

                        return false;
                    });

                theme_button_map.set(theme_name, button);
            }

            active_button(default_theme);
        }

        public static int compare_color_brightness(string a, string b) {
            try {
                var a_theme_file = new KeyFile();
                a_theme_file.load_from_file(Utils.get_theme_path((string) a), KeyFileFlags.NONE);
                var a_background_color = Utils.get_color_brightness(a_theme_file.get_string("theme", "background").strip());

                var b_theme_file = new KeyFile();
                b_theme_file.load_from_file(Utils.get_theme_path((string) b), KeyFileFlags.NONE);
                var b_background_color = Utils.get_color_brightness(b_theme_file.get_string("theme", "background").strip());

                if (a_background_color > b_background_color) {
                    return 1;
                }

                if (a_background_color == b_background_color) {
                    return 0;
                }
            } catch (Error e) {
                print("compare_color_brightness: %s\n", e.message);
            }

            return -1;
        }

        public void active_button(string theme_name) {
            if (active_theme_button != null) {
                active_theme_button.inactive();
            }

            if (theme_button_map.has_key(theme_name)) {
                active_theme_button = theme_button_map.get(theme_name);
            } else {
                active_theme_button = theme_button_map.get("deepin");
            }
            active_theme_button.active();
        }
    }
}