-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
MainWindow.vala
552 lines (442 loc) · 16.3 KB
/
MainWindow.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
* Copyright (c) 2018 Cassidy James Blaede (https://cassidyjames.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 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: Cassidy James Blaede <c@ssidyjam.es>
*/
public class MainWindow : Gtk.Window {
private const int DEFAULT_ASPECT_WIDTH = 16;
private const int DEFAULT_ASPECT_HEIGHT = 9;
private const int INTERNAL_IDEAL_DPI = 140;
private const int INTERNAL_IDEAL_RANGE = 20;
private const int INTERNAL_UNCLEAR_RANGE = 10;
private const int EXTERNAL_IDEAL_DPI = 120;
private const int EXTERNAL_IDEAL_RANGE = 30;
private const int EXTERNAL_UNCLEAR_RANGE = 20;
private const double INCHES_INFER_EXTERNAL = 18;
private const int DPI_INFER_HIDPI = 192; // According to GNOME
private enum Range {
LOW,
LODPI_LOW,
LODPI_IDEAL,
LODPI_HIGH,
HIDPI_LOW,
HIDPI_IDEAL,
HIDPI_HIGH,
HIGH,
UNCLEAR,
INVALID;
public string title () {
switch (this) {
case LOW:
return _("Very Low DPI");
case LODPI_LOW:
return _("Fairly Low DPI");
case LODPI_IDEAL:
return _("Ideal for LoDPI");
case LODPI_HIGH:
return _("Potentially Problematic");
case HIDPI_LOW:
return _("Potentially Problematic");
case HIDPI_IDEAL:
return _("Ideal for HiDPI");
case HIDPI_HIGH:
return _("Fairly High for HiDPI");
case HIGH:
return _("Too High DPI");
case UNCLEAR:
return _("Potentially Problematic");
case INVALID:
return _("Analyze a Display");
default:
assert_not_reached();
}
}
public string description () {
switch (this) {
case LOW:
return _("Text and UI are likely to be too big for typical viewing distances. Avoid if possible.");
case LODPI_LOW:
return _("Text and UI might be too big for typical viewing distances, but it's largely up to user preference and physical distance from the display.");
case LODPI_IDEAL:
return _("Not HiDPI, but a nice sweet spot. Text and UI should be legible at typical viewing distances.");
case LODPI_HIGH:
return _("Relatively high resolution, but not quite HiDPI. Text and UI may be too small by default, but forcing HiDPI would make them appear too large. The experience may be slightly improved by increasing the text size.");
case HIDPI_LOW:
return _("HiDPI by default, but text and UI may appear too large. Turning off HiDPI and increasing the text size might help.");
case HIDPI_IDEAL:
return _("Crisp HiDPI text and UI along with a readable size at typical viewing distances. This is the jackpot.");
case HIDPI_HIGH:
return _("Text and UI are likely to appear too small for typical viewing distances. Increasing the text size may help.");
case HIGH:
return _("Text and UI will appear too small for typical viewing distances.");
case UNCLEAR:
return _("This display is in a very tricky range and is not likely to work well with integer scaling out of the box.");
case INVALID:
return _("Enter details about a display to analyze it.");
default:
assert_not_reached();
}
}
public string icon () {
switch (this) {
case LOW:
case HIGH:
return "dialog-error";
case LODPI_LOW:
case LODPI_HIGH:
case HIDPI_LOW:
case HIDPI_HIGH:
case UNCLEAR:
return "dialog-warning";
case LODPI_IDEAL:
case HIDPI_IDEAL:
return "process-completed";
case INVALID:
return "dialog-information";
default:
assert_not_reached();
}
}
}
private enum DisplayType {
INTERNAL,
EXTERNAL;
public string to_string () {
switch (this) {
case INTERNAL:
return _("Laptop");
case EXTERNAL:
return _("Desktop");
default:
assert_not_reached();
}
}
public string icon_suffix () {
switch (this) {
case INTERNAL:
return "-notebook";
case EXTERNAL:
return "";
default:
assert_not_reached();
}
}
}
private int aspect_width = DEFAULT_ASPECT_WIDTH;
private int aspect_height = DEFAULT_ASPECT_HEIGHT;
private double inches = 0.0;
private int width = 0;
private int height = 0;
private bool is_default_display_type = true;
private bool is_default_width = true;
private bool is_default_height = true;
private bool is_hidpi = false;
private string direction = "";
private Gtk.Image diagram;
private Gtk.Entry diag_entry;
private Gtk.Entry width_entry;
private Gtk.Entry height_entry;
private Gtk.Label dpi_result_label;
private Gtk.Label aspect_result_label;
private Granite.Widgets.ModeButton type_modebutton;
private Gtk.Label range_title_label;
private Gtk.Label range_description_label;
private Gtk.Image range_icon;
private Range range;
private DisplayType display_type;
public MainWindow (Gtk.Application application) {
Object (
application: application,
border_width: 0,
icon_name: "com.github.cassidyjames.dippi",
resizable: false,
title: _("Dippi"),
window_position: Gtk.WindowPosition.CENTER
);
}
construct {
weak Gtk.IconTheme default_theme = Gtk.IconTheme.get_default ();
default_theme.add_resource_path ("/com/github/cassidyjames/dippi");
var header = new Gtk.HeaderBar ();
header.show_close_button = true;
var header_context = header.get_style_context ();
header_context.add_class ("titlebar");
header_context.add_class ("default-decoration");
header_context.add_class (Gtk.STYLE_CLASS_FLAT);
diagram = new Gtk.Image.from_icon_name ("com.github.cassidyjames.dippi", Gtk.IconSize.INVALID);
diagram.pixel_size = 128;
diagram.margin_bottom = 12;
var diag_label = new Gtk.Label (_("Diagonal size:"));
diag_label.halign = Gtk.Align.END;
diag_entry = new Gtk.Entry();
diag_entry.max_length = 5;
diag_entry.max_width_chars = 5;
diag_entry.width_chars = 5;
diag_entry.focus_in_event.connect ((event) => {
direction = "diagonal";
set_display_icon ();
return focus_in_event (event);
});
var res_label = new Gtk.Label (_("Resolution:"));
res_label.halign = Gtk.Align.END;
width_entry = new Gtk.Entry();
width_entry.max_length = 5;
width_entry.max_width_chars = 5;
width_entry.width_chars = 5;
width_entry.focus_in_event.connect ((event) => {
direction = "horizontal";
set_display_icon ();
return focus_in_event (event);
});
height_entry = new Gtk.Entry();
width_entry.max_length = 5;
height_entry.max_width_chars = 5;
height_entry.width_chars = 5;
height_entry.focus_in_event.connect ((event) => {
direction = "vertical";
set_display_icon ();
return focus_in_event (event);
});
var dpi_label = new Gtk.Label (_("DPI:"));
dpi_label.halign = Gtk.Align.END;
var aspect_label = new Gtk.Label (_("Aspect ratio:"));
aspect_label.halign = Gtk.Align.END;
var x_label = new Gtk.Label (_("×"));
var px_label = new Gtk.Label (_("px"));
var inches_label = new Gtk.Label (_("inches"));
inches_label.halign = Gtk.Align.START;
var type_label = new Gtk.Label (_("Type:"));
type_label.halign = Gtk.Align.END;
type_modebutton = new Granite.Widgets.ModeButton ();
type_modebutton.append_text (DisplayType.INTERNAL.to_string ());
type_modebutton.append_text (DisplayType.EXTERNAL.to_string ());
aspect_result_label = new Gtk.Label (null);
aspect_result_label.halign = Gtk.Align.START;
dpi_result_label = new Gtk.Label (null);
dpi_result_label.halign = Gtk.Align.START;
range_icon = new Gtk.Image.from_icon_name (Range.INVALID.icon (), Gtk.IconSize.DIALOG);
range_icon.margin_bottom = 12;
range_icon.valign = Gtk.Align.START;
range_title_label = new Gtk.Label (null);
range_title_label.get_style_context ().add_class ("h2");
range_title_label.wrap = true;
range_title_label.halign = Gtk.Align.START;
range_title_label.label = Range.INVALID.title ();
range_title_label.xalign = 0;
range_title_label.valign = Gtk.Align.END;
range_description_label = new Gtk.Label (null);
range_description_label.max_width_chars = 50;
range_description_label.wrap = true;
range_description_label.xalign = 0;
range_description_label.valign = Gtk.Align.START;
range_description_label.label = Range.INVALID.description ();
diag_entry.changed.connect (() => {
inches = double.parse (diag_entry.get_text ());
assess_dpi (
recalculate_dpi (inches, width, height),
infer_display_type (inches)
);
});
width_entry.changed.connect (() => {
width = int.parse (width_entry.get_text ());
is_default_width = false;
recalculate_aspect (width, height);
assess_dpi (
recalculate_dpi (inches, width, height),
display_type
);
if (!height_entry.has_focus && (is_default_height || height == 0)) {
double calculated_height = Math.round(width * DEFAULT_ASPECT_HEIGHT / DEFAULT_ASPECT_WIDTH);
height_entry.text = (calculated_height).to_string ();
is_default_height = true;
}
});
height_entry.changed.connect (() => {
height = int.parse (height_entry.get_text ());
is_default_height = false;
recalculate_aspect (width, height);
assess_dpi (
recalculate_dpi (inches, width, height),
display_type
);
if (!width_entry.has_focus && (is_default_width || width == 0)) {
double calculated_width = Math.round(height * DEFAULT_ASPECT_WIDTH / DEFAULT_ASPECT_HEIGHT);
width_entry.text = (calculated_width).to_string ();
is_default_width = true;
}
});
type_modebutton.mode_changed.connect (() => {
switch (type_modebutton.selected) {
case 0:
display_type = DisplayType.INTERNAL;
assess_dpi (dpi (inches, width, height), display_type);
set_display_icon ();
break;
case 1:
display_type = DisplayType.EXTERNAL;
assess_dpi (dpi (inches, width, height), display_type);
set_display_icon ();
break;
default:
assert_not_reached();
}
});
var data_grid = new Gtk.Grid ();
data_grid.column_spacing = 6;
data_grid.margin = 24;
data_grid.row_spacing = 6;
data_grid.get_style_context ().add_class ("data-grid");
data_grid.attach (diagram, 0, 0, 5, 1);
data_grid.attach (diag_label, 0, 1, 1, 1);
data_grid.attach (diag_entry, 1, 1, 1, 1);
data_grid.attach (inches_label, 2, 1, 2, 1);
data_grid.attach (res_label, 0, 2, 1, 1);
data_grid.attach (width_entry, 1, 2, 1, 1);
data_grid.attach (x_label, 2, 2, 1, 1);
data_grid.attach (height_entry, 3, 2, 1, 1);
data_grid.attach (px_label, 4, 2, 1, 1);
data_grid.attach (type_label, 0, 3, 1, 1);
data_grid.attach (type_modebutton, 1, 3, 4, 1);
data_grid.attach (aspect_label, 0, 4, 1, 1);
data_grid.attach (aspect_result_label, 1, 4, 4, 1);
data_grid.attach (dpi_label, 0, 5, 1, 1);
data_grid.attach (dpi_result_label, 1, 5, 4, 1);
var assessment_grid = new Gtk.Grid ();
assessment_grid.column_spacing = 12;
assessment_grid.halign = Gtk.Align.CENTER;
assessment_grid.margin = 12;
assessment_grid.margin_bottom = 48;
assessment_grid.row_spacing = 6;
assessment_grid.valign = Gtk.Align.CENTER;
assessment_grid.get_style_context ().add_class ("assessment-grid");
assessment_grid.attach (range_icon, 0, 0, 1, 2);
assessment_grid.attach (range_title_label, 1, 0, 1, 1);
assessment_grid.attach (range_description_label, 1, 1, 1, 1);
var main_layout = new Gtk.Grid ();
main_layout.column_spacing = 6;
main_layout.row_spacing = 6;
main_layout.attach (data_grid, 0, 0, 1, 1);
main_layout.attach (assessment_grid, 1, 0, 1, 1);
get_style_context ().add_class ("dippi");
get_style_context ().add_class ("rounded");
set_titlebar (header);
add (main_layout);
}
private int recalculate_dpi (double inches, int width, int height) {
if (inches > 0 && width > 0 && height > 0) {
int calculated_dpi = dpi (inches, width, height);
dpi_result_label.label = (calculated_dpi).to_string ();
if (calculated_dpi >= DPI_INFER_HIDPI) {
is_hidpi = true;
dpi_result_label.label = dpi_result_label.get_label () + _(" (HiDPI)");
} else {
is_hidpi = false;
}
return calculated_dpi;
}
return 0;
}
private void recalculate_aspect (int width, int height) {
if (width > 0 && height > 0) {
aspect_width = width / greatest_common_divisor (width, height);
aspect_height = height / greatest_common_divisor (width, height);
aspect_result_label.label = (aspect_width).to_string () + _(":") + (aspect_height).to_string ();
}
}
private int dpi (double inches, int width, int height) {
double unrounded_dpi = Math.sqrt( Math.pow (width, 2) + Math.pow (height, 2) ) / inches;
return (int)unrounded_dpi;
}
private Range assess_dpi (double calculated_dpi, DisplayType display_type) {
int ideal_dpi = INTERNAL_IDEAL_DPI;
int ideal_range = INTERNAL_IDEAL_RANGE;
int unclear_range = INTERNAL_UNCLEAR_RANGE;
if (display_type == DisplayType.EXTERNAL ) {
ideal_dpi = EXTERNAL_IDEAL_DPI;
ideal_range = EXTERNAL_IDEAL_RANGE;
unclear_range = EXTERNAL_UNCLEAR_RANGE;
}
if ( inches == 0 || width == 0 || height == 0 ) {
range = Range.INVALID;
}
else if (calculated_dpi < ideal_dpi - ideal_range - INTERNAL_UNCLEAR_RANGE) {
range = Range.LOW;
}
else if (calculated_dpi < ideal_dpi - ideal_range) {
range = Range.LODPI_LOW;
}
else if (calculated_dpi <= ideal_dpi + ideal_range) {
range = Range.LODPI_IDEAL;
}
else if (calculated_dpi <= ideal_dpi + ideal_range + unclear_range) {
range = Range.LODPI_HIGH;
}
else if (calculated_dpi < DPI_INFER_HIDPI) {
range = Range.UNCLEAR;
}
else if (calculated_dpi < (ideal_dpi - ideal_range - unclear_range) * 2) {
range = Range.UNCLEAR;
}
else if (calculated_dpi < (ideal_dpi - ideal_range) * 2) {
range = Range.HIDPI_LOW;
}
else if (calculated_dpi <= (ideal_dpi + ideal_range) * 2) {
range = Range.HIDPI_IDEAL;
}
else if (calculated_dpi <= (ideal_dpi + ideal_range + unclear_range) * 2) {
range = Range.HIDPI_HIGH;
}
else if (calculated_dpi > (ideal_dpi + ideal_range + unclear_range) * 2) {
range = Range.HIGH;
}
else {
range = Range.INVALID;
}
range_icon.icon_name = range.icon ();
range_title_label.label = range.title ();
range_description_label.label = range.description ();
return range;
}
private DisplayType infer_display_type (double inches) {
is_default_display_type = true;
if (inches < 18) {
display_type = DisplayType.INTERNAL;
type_modebutton.selected = 0;
} else {
display_type = DisplayType.EXTERNAL;
type_modebutton.selected = 1;
}
return display_type;
}
private void set_display_icon () {
diagram.icon_name = "display-measure-" + direction + display_type.icon_suffix ();
}
private int greatest_common_divisor (int a, int b) {
if (a == 0) {
return b;
}
if (b == 0) {
return a;
}
if (a > b) {
return greatest_common_divisor(a % b, b);
} else {
return greatest_common_divisor(a, b % a);
}
}
}