forked from phase1geo/Minder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheme.vala
356 lines (305 loc) · 11.1 KB
/
Theme.vala
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (c) 2018 (https://github.com/phase1geo/Minder)
*
* 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 2 of the License, or (at your option) 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, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Trevor Williams <[email protected]>
*/
using Gtk;
using Gdk;
using Gee;
public class Theme : Object {
private HashMap<string,RGBA?> _colors;
public string name { set; get; }
public string label { set; get; }
public int index { set; get; default = -1; }
public bool prefer_dark { set; get; default = false; }
public bool custom { protected set; get; default = true; }
public bool temporary { set; get; default = false; }
public bool rotate { set; get; default = true; }
/* Default constructor */
public Theme() {
_colors = new HashMap<string,RGBA?>();
_colors.set( "background", null );
_colors.set( "foreground", null );
_colors.set( "root_background", null );
_colors.set( "root_foreground", null );
_colors.set( "nodesel_background", null );
_colors.set( "nodesel_foreground", null );
_colors.set( "textsel_background", null );
_colors.set( "textsel_foreground", null );
_colors.set( "text_cursor", null );
_colors.set( "attachable", null );
_colors.set( "connection_background", null );
_colors.set( "connection_foreground", null );
_colors.set( "url_background", null );
_colors.set( "url_foreground", null );
_colors.set( "tag", null );
_colors.set( "syntax", null );
_colors.set( "match_background", null );
_colors.set( "match_foreground", null );
_colors.set( "markdown_listitem", null );
_colors.set( "callout_background", null );
_colors.set( "link_color0", null );
_colors.set( "link_color1", null );
_colors.set( "link_color2", null );
_colors.set( "link_color3", null );
_colors.set( "link_color4", null );
_colors.set( "link_color5", null );
_colors.set( "link_color6", null );
_colors.set( "link_color7", null );
}
/* Copy constructor */
public Theme.from_theme( Theme theme ) {
copy( theme );
}
/* Copies the given theme to this theme */
public void copy( Theme theme ) {
name = theme.name;
label = theme.label;
index = theme.index;
prefer_dark = theme.prefer_dark;
temporary = theme.temporary;
rotate = theme.rotate;
_colors = new HashMap<string,RGBA?>();
var it = theme._colors.map_iterator();
while( it.next() ) {
_colors.set( it.get_key(), it.get_value() );
}
}
/* Returns the list of stored theme names */
public Array<string> colors() {
var cs = new Array<string>();
var it = _colors.map_iterator();
while( it.next() ) {
var name = (string)it.get_key();
cs.append_val( name );
}
return( cs );
}
/* Returns true if the given theme matches the current theme */
public bool matches( Theme theme ) {
if( (name == theme.name) || (label == theme.name) ) {
if( custom ) {
var it = _colors.map_iterator();
while( it.next() ) {
var key = it.get_key();
if( !theme._colors.has_key( key ) || !it.get_value().equal( theme._colors.get( key ) ) ) {
return( false );
}
}
return( prefer_dark == theme.prefer_dark );
}
return( true );
}
return( false );
}
/* Adds the given color to the list of link colors */
public bool set_color( string name, RGBA color ) {
if( _colors.has_key( name ) ) {
_colors.set( name, color );
return( true );
}
return( false );
}
/* Returns the given color */
public RGBA? get_color( string name ) {
if( _colors.has_key( name ) ) {
return( _colors.get( name ) );
}
return( null );
}
/* Returns the next available link color index */
public RGBA? next_color() {
if( index == -1 ) {
index = 0;
} else if( rotate ) {
index = (index + 1) % 8;
}
return( link_color( index ) );
}
/* Returns the number of link colors */
public static int num_link_colors() {
return( 8 );
}
/* Returns the color associated with the given index */
public RGBA link_color( int index ) {
return( _colors.get( "link_color%d".printf( index % 8 ) ) );
}
/* Returns a randomly selected link color */
public RGBA random_link_color() {
var rand = new Rand();
return( _colors.get( "link_color%d".printf( rand.int_range( 0, 8 ) ) ) );
}
/*
Searches the stored link colors for one that matches the given color.
If a match is found, returns the index of the stored color. If no match
was found, returns -1.
*/
public int get_color_index( RGBA color ) {
string color_str = color.to_string();
for( int i=0; i<8; i++ ) {
RGBA? lc = link_color( i );
if( (lc != null) && (lc.to_string() == color_str) ) {
return( i );
}
}
return( -1 );
}
/* Returns the RGBA color for the given color value */
protected RGBA color_from_string( string value ) {
return( Utils.color_from_string( value ) );
}
/* Returns the CSS provider for this theme */
public CssProvider get_css_provider( int text_size ) {
CssProvider provider = new CssProvider();
try {
var tv_size = (text_size == -1) ? ".textfield { font: 1em \"Sans\" } " :
".textfield { font: %dpx \"Sans\"; } ".printf( text_size );
var css_data = "@define-color colorPrimary #603461; " +
"@define-color textColorPrimary @SILVER_100; " +
"@define-color colorAccent #603461; " +
"@define-color tab_base_color " + get_color( "background" ).to_string() + ";" +
tv_size +
".theme-selected { background: #087DFF; } " +
".canvas { background: " + get_color( "background" ).to_string() + "; }" +
".highlighted { background: rgba(255, 255, 129, " + (prefer_dark ? "0.15" : "1.0") + "); }";
provider.load_from_data( css_data );
} catch( GLib.Error e ) {
stdout.printf( _( "Unable to load background color: %s" ), e.message );
}
return( provider );
}
/* Sets the context color based on the theme RGBA color */
private void set_context_color( Cairo.Context ctx, RGBA color ) {
ctx.set_source_rgba( color.red, color.green, color.blue, color.alpha );
}
/* Parses the specified XML node for theme coloring information */
public bool load( Xml.Node* n ) {
bool contains_data = false;
string? nn = n->get_prop( "name" );
if( nn != null ) {
name = nn;
}
string? ll = n->get_prop( "label" );
if( ll != null ) {
label = ll;
} else if( nn != null ) { /* This is for backwards compatibility */
label = nn;
}
string? idx = n->get_prop( "index" );
if( idx != null ) {
index = int.parse( idx );
}
var cs = colors();
for( int i=0; i<cs.length; i++ ) {
var name = cs.index( i );
string? s = n->get_prop( name );
if( s != null ) {
set_color( name, color_from_string( s ) );
contains_data = true;
}
}
string? d = n->get_prop( "prefer_dark" );
if( d != null ) {
prefer_dark = bool.parse( d );
contains_data = true;
}
return( contains_data );
}
/* Returns an XML node containing the contents of this theme color scheme */
public Xml.Node* save() {
Xml.Node* n = new Xml.Node( null, "theme" );
n->new_prop( "name", name );
n->new_prop( "label", label );
n->new_prop( "index", index.to_string() );
if( custom ) {
var cs = colors();
for( int i=0; i<cs.length; i++ ) {
var name = cs.index( i );
n->new_prop( name, Utils.color_from_rgba( get_color( name ) ) );
}
n->new_prop( "prefer_dark", prefer_dark.to_string() );
}
return( n );
}
/* Creates the icon representation based on the theme's colors */
public Cairo.Surface make_icon() {
int side = 140;
int nrad = 15;
Cairo.ImageSurface surface = new Cairo.ImageSurface( Cairo.Format.ARGB32, side, side );
Cairo.Context ctx = new Cairo.Context( surface );
int hside = side / 2;
double ypos[6];
int width, height;
var font_desc = new Pango.FontDescription();
font_desc.set_family( "Sans" );
font_desc.set_size( 11 * Pango.SCALE );
/* Draw the background */
set_context_color( ctx, get_color( "background" ) );
ctx.rectangle( 0, 0, side, side );
ctx.fill();
/* Create root node text */
var root_text = Pango.cairo_create_layout( ctx );
root_text.set_font_description( font_desc );
// root_text.set_width( 70 * Pango.SCALE );
root_text.set_text( _( "Root" ), -1 );
root_text.get_pixel_size( out width, out height );
var rrad = (width + 20) / 2;
var hspace = ((side - (rrad * 2) - (nrad * 4)) / 4) + nrad;
var vspace = ((side - (nrad * 6)) / 4) + nrad;
ypos[0] = ypos[3] = vspace;
ypos[1] = ypos[4] = (side / 2);
ypos[2] = ypos[5] = (side - vspace);
/* Draw root node */
set_context_color( ctx, get_color( "root_background" ) );
ctx.arc( hside, hside, rrad, 0, (2 * Math.PI) );
ctx.fill();
/* Add the text */
set_context_color( ctx, get_color( "root_foreground" ) );
ctx.move_to( (hside - (width / 2)), (hside - (height / 2)) );
Pango.cairo_show_layout( ctx, root_text );
/* Draw subnodes */
for( int i=0; i<6; i++ ) {
set_context_color( ctx, link_color( i ) );
ctx.arc( ((i < 3) ? hspace : (side - hspace)), ypos[i], nrad, 0, (2 * Math.PI) );
ctx.fill();
}
/* Draw connection */
set_context_color( ctx, get_color( "connection_background" ) );
double p[6];
p[0] = hspace + nrad + 2;
p[1] = vspace + 2;
p[2] = hside;
p[3] = p[1] - nrad;
p[4] = side - (hspace + nrad + 2);
p[5] = p[1];
ctx.set_line_width( 2 );
ctx.set_dash( {3, 5}, 0 );
ctx.move_to( p[0], p[1] );
ctx.curve_to(
(((2.0 / 3.0) * p[2]) + ((1.0 / 3.0) * p[0])),
(((2.0 / 3.0) * p[3]) + ((1.0 / 3.0) * p[1])),
(((2.0 / 3.0) * p[2]) + ((1.0 / 3.0) * p[4])),
(((2.0 / 3.0) * p[3]) + ((1.0 / 3.0) * p[5])),
p[4], p[5]
);
ctx.stroke();
Connection.draw_arrow( ctx, 2, p[0], p[1], p[2], p[3], 7 );
Connection.draw_arrow( ctx, 2, p[4], p[5], p[2], p[3], 7 );
return( surface );
}
}