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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
|
//
// fiv-browser.c: filesystem browsing widget
//
// Copyright (c) 2021 - 2022, Přemysl Eric Janouch <p@janouch.name>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
#include <math.h>
#include <pixman.h>
#include "config.h"
#include "fiv-browser.h"
#include "fiv-io.h"
#include "fiv-thumbnail.h"
#include "fiv-view.h"
// --- Widget ------------------------------------------------------------------
// _________________________________
// │ p a d d i n g
// │ p ╭───────────────────╮ s ╭┄┄┄┄┄
// │ a │ glow border ┊ │ p ┊
// │ d │ ┄ ╔═══════════╗ ┄ │ a ┊
// │ d │ ║ thumbnail ║ │ c ┊ ...
// │ i │ ┄ ╚═══════════╝ ┄ │ i ┊
// │ n │ ┊ glow border │ n ┊
// │ g ╰───────────────────╯ g ╰┄┄┄┄┄
// │ s p a c i n g
// │ ╭┄┄┄┄┄┄┄┄┄┄┄┄╮ ╭┄┄┄┄┄┄┄┄┄┄┄┄
//
// The glow is actually a glowing margin, the border is rendered in two parts.
//
typedef struct entry Entry;
typedef struct item Item;
typedef struct row Row;
typedef struct {
FivBrowser *self; ///< Parent browser
Entry *target; ///< Currently processed Entry pointer
GSubprocess *minion; ///< A slave for the current queue head
GCancellable *cancel; ///< Cancellable handle
} Thumbnailer;
struct _FivBrowser {
GtkWidget parent_instance;
GtkAdjustment *hadjustment; ///< GtkScrollable boilerplate
GtkAdjustment *vadjustment; ///< GtkScrollable boilerplate
GtkScrollablePolicy hscroll_policy; ///< GtkScrollable boilerplate
GtkScrollablePolicy vscroll_policy; ///< GtkScrollable boilerplate
FivThumbnailSize item_size; ///< Thumbnail size
int item_height; ///< Thumbnail height in pixels
int item_spacing; ///< Space between items in pixels
FivIoModel *model; ///< Filesystem model
GArray *entries; ///< []Entry
GArray *layouted_rows; ///< []Row
const Entry *selected; ///< Selected entry or NULL
Thumbnailer *thumbnailers; ///< Parallelized thumbnailers
size_t thumbnailers_len; ///< Thumbnailers array size
GList *thumbnailers_queue; ///< Queued up Entry pointers
GdkCursor *pointer; ///< Cached pointer cursor
cairo_surface_t *glow; ///< CAIRO_FORMAT_A8 mask
int item_border_x; ///< L/R .item margin + border
int item_border_y; ///< T/B .item margin + border
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct entry {
char *uri; ///< GIO URI
cairo_surface_t *thumbnail; ///< Prescaled thumbnail
GIcon *icon; ///< If no thumbnail, use this icon
};
static void
entry_free(Entry *self)
{
g_free(self->uri);
g_clear_pointer(&self->thumbnail, cairo_surface_destroy);
g_clear_object(&self->icon);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
struct item {
const Entry *entry;
int x_offset; ///< Offset within the row
};
struct row {
Item *items; ///< Ends with a NULL entry
gsize len; ///< Length of items
int x_offset; ///< Start position outside borders
int y_offset; ///< Start position inside borders
};
static void
row_free(Row *self)
{
g_free(self->items);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
append_row(FivBrowser *self, int *y, int x, GArray *items_array)
{
if (self->layouted_rows->len)
*y += self->item_spacing;
*y += self->item_border_y;
Row row = {.x_offset = x, .y_offset = *y};
row.items = g_array_steal(items_array, &row.len),
g_array_append_val(self->layouted_rows, row);
// Not trying to pack them vertically, but this would be the place to do it.
*y += self->item_height;
*y += self->item_border_y;
}
static int
relayout(FivBrowser *self, int width)
{
GtkWidget *widget = GTK_WIDGET(self);
GtkStyleContext *style = gtk_widget_get_style_context(widget);
GtkBorder padding = {};
gtk_style_context_get_padding(style, GTK_STATE_FLAG_NORMAL, &padding);
int available_width = width - padding.left - padding.right;
g_array_set_size(self->layouted_rows, 0);
GArray *items = g_array_new(TRUE, TRUE, sizeof(Item));
int x = 0, y = padding.top;
for (guint i = 0; i < self->entries->len; i++) {
const Entry *entry = &g_array_index(self->entries, Entry, i);
if (!entry->thumbnail)
continue;
int width = cairo_image_surface_get_width(entry->thumbnail) +
2 * self->item_border_x;
if (!items->len) {
// Just insert it, whether or not there's any space.
} else if (x + self->item_spacing + width <= available_width) {
x += self->item_spacing;
} else {
append_row(self, &y,
padding.left + MAX(0, available_width - x) / 2, items);
x = 0;
}
g_array_append_val(items,
((Item) {.entry = entry, .x_offset = x + self->item_border_x}));
x += width;
}
if (items->len) {
append_row(self, &y,
padding.left + MAX(0, available_width - x) / 2, items);
}
g_array_free(items, TRUE);
int total_height = y + padding.bottom;
if (self->hadjustment) {
// TODO(p): Set it to the width. Ideally, bump it to the minimum width.
}
if (self->vadjustment) {
gtk_adjustment_set_lower(self->vadjustment, 0);
gtk_adjustment_set_upper(self->vadjustment, total_height);
gtk_adjustment_set_page_size(
self->vadjustment, gtk_widget_get_allocated_height(widget));
gtk_adjustment_set_page_increment(
self->vadjustment, gtk_widget_get_allocated_height(widget));
gtk_adjustment_set_step_increment(self->vadjustment,
self->item_height + self->item_spacing + 2 * self->item_border_y);
}
return total_height;
}
static void
draw_outer_border(FivBrowser *self, cairo_t *cr, int width, int height)
{
int offset_x = cairo_image_surface_get_width(self->glow);
int offset_y = cairo_image_surface_get_height(self->glow);
cairo_pattern_t *mask = cairo_pattern_create_for_surface(self->glow);
cairo_matrix_t matrix;
cairo_pattern_set_extend(mask, CAIRO_EXTEND_PAD);
cairo_save(cr);
cairo_translate(cr, -offset_x, -offset_y);
cairo_rectangle(cr, 0, 0, offset_x + width, offset_y + height);
cairo_clip(cr);
cairo_mask(cr, mask);
cairo_restore(cr);
cairo_save(cr);
cairo_translate(cr, width + offset_x, height + offset_y);
cairo_rectangle(cr, 0, 0, -offset_x - width, -offset_y - height);
cairo_clip(cr);
cairo_scale(cr, -1, -1);
cairo_mask(cr, mask);
cairo_restore(cr);
cairo_pattern_set_extend(mask, CAIRO_EXTEND_NONE);
cairo_matrix_init_scale(&matrix, -1, 1);
cairo_matrix_translate(&matrix, -width - offset_x, offset_y);
cairo_pattern_set_matrix(mask, &matrix);
cairo_mask(cr, mask);
cairo_matrix_init_scale(&matrix, 1, -1);
cairo_matrix_translate(&matrix, offset_x, -height - offset_y);
cairo_pattern_set_matrix(mask, &matrix);
cairo_mask(cr, mask);
cairo_pattern_destroy(mask);
}
static GdkRectangle
item_extents(FivBrowser *self, const Item *item, const Row *row)
{
int width = cairo_image_surface_get_width(item->entry->thumbnail);
int height = cairo_image_surface_get_height(item->entry->thumbnail);
return (GdkRectangle) {
.x = row->x_offset + item->x_offset,
.y = row->y_offset + self->item_height - height,
.width = width,
.height = height,
};
}
static const Entry *
entry_at(FivBrowser *self, int x, int y)
{
if (self->vadjustment)
y += round(gtk_adjustment_get_value(self->vadjustment));
for (guint i = 0; i < self->layouted_rows->len; i++) {
const Row *row = &g_array_index(self->layouted_rows, Row, i);
for (Item *item = row->items; item->entry; item++) {
GdkRectangle extents = item_extents(self, item, row);
if (x >= extents.x &&
y >= extents.y &&
x <= extents.x + extents.width &&
y <= extents.y + extents.height)
return item->entry;
}
}
return NULL;
}
static GdkRectangle
entry_rect(FivBrowser *self, const Entry *entry)
{
GdkRectangle rect = {};
for (guint i = 0; i < self->layouted_rows->len; i++) {
const Row *row = &g_array_index(self->layouted_rows, Row, i);
for (Item *item = row->items; item->entry; item++) {
if (item->entry == entry) {
rect = item_extents(self, item, row);
break;
}
}
}
if (self->vadjustment)
rect.y -= round(gtk_adjustment_get_value(self->vadjustment));
return rect;
}
static void
draw_row(FivBrowser *self, cairo_t *cr, const Row *row)
{
GtkStyleContext *style = gtk_widget_get_style_context(GTK_WIDGET(self));
gtk_style_context_save(style);
gtk_style_context_add_class(style, "item");
GtkBorder border;
GtkStateFlags common_state = gtk_style_context_get_state(style);
gtk_style_context_get_border(style, common_state, &border);
for (Item *item = row->items; item->entry; item++) {
cairo_save(cr);
GdkRectangle extents = item_extents(self, item, row);
cairo_translate(cr, extents.x - border.left, extents.y - border.top);
GtkStateFlags state = common_state;
if (item->entry == self->selected)
state |= GTK_STATE_FLAG_SELECTED;
gtk_style_context_save(style);
gtk_style_context_set_state(style, state);
if (item->entry->icon) {
gtk_style_context_add_class(style, "symbolic");
} else {
GdkRGBA glow_color = {};
gtk_style_context_get_color(style, state, &glow_color);
gdk_cairo_set_source_rgba(cr, &glow_color);
draw_outer_border(self, cr,
border.left + extents.width + border.right,
border.top + extents.height + border.bottom);
}
// Performance optimization--specifically targeting the checkerboard.
if (cairo_image_surface_get_format(item->entry->thumbnail) !=
CAIRO_FORMAT_RGB24) {
gtk_render_background(style, cr, border.left, border.top,
extents.width, extents.height);
}
gtk_render_frame(style, cr, 0, 0,
border.left + extents.width + border.right,
border.top + extents.height + border.bottom);
if (item->entry->icon) {
GdkRGBA color = {};
gtk_style_context_get_color(style, state, &color);
gdk_cairo_set_source_rgba(cr, &color);
cairo_mask_surface(
cr, item->entry->thumbnail, border.left, border.top);
} else {
cairo_set_source_surface(
cr, item->entry->thumbnail, border.left, border.top);
cairo_paint(cr);
// Here, we could consider multiplying
// the whole rectangle with the selection color.
}
cairo_restore(cr);
gtk_style_context_restore(style);
}
gtk_style_context_restore(style);
}
// --- Thumbnails --------------------------------------------------------------
// NOTE: "It is important to note that when an image with an alpha channel is
// scaled, linear encoded, pre-multiplied component values must be used!"
static cairo_surface_t *
rescale_thumbnail(cairo_surface_t *thumbnail, double row_height)
{
if (!thumbnail)
return thumbnail;
int width = cairo_image_surface_get_width(thumbnail);
int height = cairo_image_surface_get_height(thumbnail);
double scale_x = 1;
double scale_y = 1;
if (width > FIV_THUMBNAIL_WIDE_COEFFICIENT * height) {
scale_x = FIV_THUMBNAIL_WIDE_COEFFICIENT * row_height / width;
scale_y = round(scale_x * height) / height;
} else {
scale_y = row_height / height;
scale_x = round(scale_y * width) / width;
}
if (scale_x == 1 && scale_y == 1)
return thumbnail;
int projected_width = round(scale_x * width);
int projected_height = round(scale_y * height);
cairo_format_t cairo_format = cairo_image_surface_get_format(thumbnail);
cairo_surface_t *scaled = cairo_image_surface_create(
cairo_format, projected_width, projected_height);
// pixman can take gamma into account when scaling, unlike Cairo.
struct pixman_f_transform xform_floating;
struct pixman_transform xform;
// PIXMAN_a8r8g8b8_sRGB can be used for gamma-correct results,
// but it's an incredibly slow transformation
pixman_format_code_t format =
cairo_format == CAIRO_FORMAT_RGB24 ? PIXMAN_x8r8g8b8 : PIXMAN_a8r8g8b8;
pixman_image_t *src = pixman_image_create_bits(format, width, height,
(uint32_t *) cairo_image_surface_get_data(thumbnail),
cairo_image_surface_get_stride(thumbnail));
pixman_image_t *dest = pixman_image_create_bits(format,
cairo_image_surface_get_width(scaled),
cairo_image_surface_get_height(scaled),
(uint32_t *) cairo_image_surface_get_data(scaled),
cairo_image_surface_get_stride(scaled));
pixman_f_transform_init_scale(&xform_floating, scale_x, scale_y);
pixman_f_transform_invert(&xform_floating, &xform_floating);
pixman_transform_from_pixman_f_transform(&xform, &xform_floating);
pixman_image_set_transform(src, &xform);
pixman_image_set_filter(src, PIXMAN_FILTER_BILINEAR, NULL, 0);
pixman_image_set_repeat(src, PIXMAN_REPEAT_PAD);
pixman_image_composite(PIXMAN_OP_SRC, src, NULL, dest, 0, 0, 0, 0, 0, 0,
projected_width, projected_height);
pixman_image_unref(src);
pixman_image_unref(dest);
cairo_surface_set_user_data(
scaled, &fiv_thumbnail_key_lq, (void *) (intptr_t) 1, NULL);
cairo_surface_destroy(thumbnail);
cairo_surface_mark_dirty(scaled);
return scaled;
}
static void
entry_add_thumbnail(gpointer data, gpointer user_data)
{
Entry *self = data;
g_clear_object(&self->icon);
g_clear_pointer(&self->thumbnail, cairo_surface_destroy);
FivBrowser *browser = FIV_BROWSER(user_data);
GFile *file = g_file_new_for_uri(self->uri);
self->thumbnail = rescale_thumbnail(
fiv_thumbnail_lookup(file, browser->item_size), browser->item_height);
if (self->thumbnail)
goto out;
// Fall back to symbolic icons, though there's only so much we can do
// in parallel--GTK+ isn't thread-safe.
GFileInfo *info = g_file_query_info(file,
G_FILE_ATTRIBUTE_STANDARD_NAME
"," G_FILE_ATTRIBUTE_STANDARD_SYMBOLIC_ICON,
G_FILE_QUERY_INFO_NONE, NULL, NULL);
if (info) {
GIcon *icon = g_file_info_get_symbolic_icon(info);
if (icon)
self->icon = g_object_ref(icon);
g_object_unref(info);
}
// The GVfs backend may not be friendly.
if (!self->icon)
self->icon = g_icon_new_for_string("text-x-generic-symbolic", NULL);
out:
g_object_unref(file);
}
static void
materialize_icon(FivBrowser *self, Entry *entry)
{
if (!entry->icon)
return;
// Fucker will still give us non-symbolic icons, no more playing nice.
// TODO(p): Investigate a bit closer. We may want to abandon the idea
// of using GLib to look up icons for us, derive a list from a guessed
// MIME type, with "-symbolic" prefixes and fallbacks,
// and use gtk_icon_theme_choose_icon() instead.
// TODO(p): Make sure we have /some/ icon for every entry.
// TODO(p): We might want to populate these on an as-needed basis.
GtkIconInfo *icon_info = gtk_icon_theme_lookup_by_gicon(
gtk_icon_theme_get_default(), entry->icon, self->item_height / 2,
GTK_ICON_LOOKUP_FORCE_SYMBOLIC);
if (!icon_info)
return;
// Bílá, bílá, bílá, bílá... komu by se nelíbí-lá...
// We do not want any highlights, nor do we want to remember the style.
const GdkRGBA white = {1, 1, 1, 1};
GdkPixbuf *pixbuf = gtk_icon_info_load_symbolic(
icon_info, &white, &white, &white, &white, NULL, NULL);
if (pixbuf) {
int outer_size = self->item_height;
entry->thumbnail =
cairo_image_surface_create(CAIRO_FORMAT_A8, outer_size, outer_size);
// "Note that the resulting pixbuf may not be exactly this size;"
// though GTK_ICON_LOOKUP_FORCE_SIZE is also an option.
int x = (outer_size - gdk_pixbuf_get_width(pixbuf)) / 2;
int y = (outer_size - gdk_pixbuf_get_height(pixbuf)) / 2;
cairo_t *cr = cairo_create(entry->thumbnail);
gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y);
cairo_paint(cr);
cairo_destroy(cr);
g_object_unref(pixbuf);
}
g_object_unref(icon_info);
}
static void
reload_thumbnails(FivBrowser *self)
{
GThreadPool *pool = g_thread_pool_new(
entry_add_thumbnail, self, g_get_num_processors(), FALSE, NULL);
for (guint i = 0; i < self->entries->len; i++)
g_thread_pool_push(pool, &g_array_index(self->entries, Entry, i), NULL);
g_thread_pool_free(pool, FALSE, TRUE);
for (guint i = 0; i < self->entries->len; i++)
materialize_icon(self, &g_array_index(self->entries, Entry, i));
gtk_widget_queue_resize(GTK_WIDGET(self));
}
// --- Minion management -------------------------------------------------------
static gboolean thumbnailer_next(Thumbnailer *thumbnailer);
static void
thumbnailer_reprocess_entry(FivBrowser *self, Entry *entry)
{
entry_add_thumbnail(entry, self);
materialize_icon(self, entry);
gtk_widget_queue_resize(GTK_WIDGET(self));
}
static void
on_thumbnailer_ready(GObject *object, GAsyncResult *res, gpointer user_data)
{
GSubprocess *subprocess = G_SUBPROCESS(object);
Thumbnailer *thumbnailer = user_data;
GError *error = NULL;
if (!g_subprocess_wait_check_finish(subprocess, res, &error)) {
if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
g_error_free(error);
return;
}
if (!g_subprocess_get_if_exited(subprocess) ||
g_subprocess_get_exit_status(subprocess) != EXIT_FAILURE)
g_warning("%s", error->message);
g_error_free(error);
}
g_return_if_fail(subprocess == thumbnailer->minion);
gboolean succeeded = g_subprocess_get_if_exited(subprocess) &&
g_subprocess_get_exit_status(subprocess) == EXIT_SUCCESS;
g_clear_object(&thumbnailer->minion);
if (!thumbnailer->target) {
g_warning("finished thumbnailing an unknown image");
return;
}
if (succeeded)
thumbnailer_reprocess_entry(thumbnailer->self, thumbnailer->target);
thumbnailer->target = NULL;
thumbnailer_next(thumbnailer);
}
static gboolean
thumbnailer_next(Thumbnailer *thumbnailer)
{
// TODO(p): Ideally, try to keep the minions alive.
FivBrowser *self = thumbnailer->self;
GList *link = self->thumbnailers_queue;
if (!link)
return FALSE;
thumbnailer->target = link->data;
self->thumbnailers_queue =
g_list_delete_link(self->thumbnailers_queue, self->thumbnailers_queue);
GError *error = NULL;
thumbnailer->minion = g_subprocess_new(G_SUBPROCESS_FLAGS_NONE, &error,
PROJECT_NAME, "--thumbnail",
fiv_thumbnail_sizes[self->item_size].thumbnail_spec_name, "--",
thumbnailer->target->uri, NULL);
if (error) {
g_warning("%s", error->message);
g_error_free(error);
return FALSE;
}
thumbnailer->cancel = g_cancellable_new();
g_subprocess_wait_check_async(thumbnailer->minion, thumbnailer->cancel,
on_thumbnailer_ready, thumbnailer);
return TRUE;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static void
thumbnailers_abort(FivBrowser *self)
{
g_list_free(self->thumbnailers_queue);
self->thumbnailers_queue = NULL;
for (size_t i = 0; i < self->thumbnailers_len; i++) {
Thumbnailer *thumbnailer = self->thumbnailers + i;
if (thumbnailer->cancel) {
g_cancellable_cancel(thumbnailer->cancel);
g_clear_object(&thumbnailer->cancel);
}
// Just let them exit on their own.
g_clear_object(&thumbnailer->minion);
thumbnailer->target = NULL;
}
}
static void
thumbnailers_start(FivBrowser *self)
{
thumbnailers_abort(self);
// TODO(p): Leave out all paths containing .cache/thumbnails altogether.
gchar *thumbnails_dir = fiv_thumbnail_get_root();
GFile *thumbnails = g_file_new_for_path(thumbnails_dir);
g_free(thumbnails_dir);
GFile *current = fiv_io_model_get_location(self->model);
gboolean is_a_thumbnail = current && g_file_has_prefix(current, thumbnails);
g_object_unref(thumbnails);
if (is_a_thumbnail)
return;
GList *missing = NULL, *lq = NULL;
for (guint i = self->entries->len; i--; ) {
Entry *entry = &g_array_index(self->entries, Entry, i);
if (entry->icon)
missing = g_list_prepend(missing, entry);
else if (cairo_surface_get_user_data(
entry->thumbnail, &fiv_thumbnail_key_lq))
lq = g_list_prepend(lq, entry);
}
self->thumbnailers_queue = g_list_concat(missing, lq);
for (size_t i = 0; i < self->thumbnailers_len; i++) {
if (!thumbnailer_next(self->thumbnailers + i))
break;
}
}
// --- Context menu-------------------------------------------------------------
typedef struct _OpenContext {
GWeakRef widget;
GFile *file;
char *content_type;
GAppInfo *app_info;
} OpenContext;
static void
open_context_notify(gpointer data, G_GNUC_UNUSED GClosure *closure)
{
OpenContext *self = data;
g_weak_ref_clear(&self->widget);
g_clear_object(&self->app_info);
g_clear_object(&self->file);
g_free(self->content_type);
g_free(self);
}
static void
open_context_launch(GtkWidget *widget, OpenContext *self)
{
GdkAppLaunchContext *context =
gdk_display_get_app_launch_context(gtk_widget_get_display(widget));
gdk_app_launch_context_set_screen(context, gtk_widget_get_screen(widget));
gdk_app_launch_context_set_timestamp(context, gtk_get_current_event_time());
// TODO(p): Display errors.
GList *files = g_list_append(NULL, self->file);
if (g_app_info_launch(
self->app_info, files, G_APP_LAUNCH_CONTEXT(context), NULL)) {
g_app_info_set_as_last_used_for_type(
self->app_info, self->content_type, NULL);
}
g_list_free(files);
g_object_unref(context);
}
static void
append_opener(GtkWidget *menu, GAppInfo *opener, const OpenContext *template)
{
OpenContext *ctx = g_malloc0(sizeof *ctx);
g_weak_ref_init(&ctx->widget, NULL);
ctx->file = g_object_ref(template->file);
ctx->content_type = g_strdup(template->content_type);
ctx->app_info = opener;
// It's documented that we can touch the child, if we want formatting:
// https://docs.gtk.org/gtk3/class.MenuItem.html
// XXX: Would g_app_info_get_display_name() be any better?
gchar *name = g_strdup_printf("Open With %s", g_app_info_get_name(opener));
GtkWidget *item = gtk_menu_item_new_with_label(name);
g_free(name);
g_signal_connect_data(item, "activate", G_CALLBACK(open_context_launch),
ctx, open_context_notify, 0);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
}
static void
on_chooser_activate(GtkMenuItem *item, gpointer user_data)
{
OpenContext *ctx = user_data;
GtkWindow *window = NULL;
GtkWidget *widget = g_weak_ref_get(&ctx->widget);
if (widget) {
if (GTK_IS_WINDOW((widget = gtk_widget_get_toplevel(widget))))
window = GTK_WINDOW(widget);
}
GtkWidget *dialog = gtk_app_chooser_dialog_new_for_content_type(window,
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL, ctx->content_type);
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
ctx->app_info = gtk_app_chooser_get_app_info(GTK_APP_CHOOSER(dialog));
open_context_launch(GTK_WIDGET(item), ctx);
}
gtk_widget_destroy(dialog);
}
static gboolean
destroy_widget_idle_source_func(GtkWidget *widget)
{
// The whole menu is deactivated /before/ any item is activated,
// and a destroyed child item will not activate.
gtk_widget_destroy(widget);
return FALSE;
}
static GtkMenu *
make_context_menu(GtkWidget *widget, GFile *file)
{
GFileInfo *info = g_file_query_info(file,
G_FILE_ATTRIBUTE_STANDARD_NAME
"," G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
G_FILE_QUERY_INFO_NONE, NULL, NULL);
if (!info)
return NULL;
// This will have no application pre-assigned, for use with GTK+'s dialog.
OpenContext *ctx = g_malloc0(sizeof *ctx);
g_weak_ref_init(&ctx->widget, widget);
ctx->file = g_object_ref(file);
ctx->content_type = g_strdup(g_file_info_get_content_type(info));
g_object_unref(info);
GAppInfo *default_ =
g_app_info_get_default_for_type(ctx->content_type, FALSE);
GList *recommended = g_app_info_get_recommended_for_type(ctx->content_type);
GList *fallback = g_app_info_get_fallback_for_type(ctx->content_type);
GtkWidget *menu = gtk_menu_new();
if (default_) {
append_opener(menu, default_, ctx);
gtk_menu_shell_append(
GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
}
for (GList *iter = recommended; iter; iter = iter->next) {
if (g_app_info_should_show(iter->data) &&
(!default_ || !g_app_info_equal(iter->data, default_)))
append_opener(menu, iter->data, ctx);
else
g_object_unref(iter->data);
}
if (recommended) {
g_list_free(recommended);
gtk_menu_shell_append(
GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
}
for (GList *iter = fallback; iter; iter = iter->next) {
if (g_app_info_should_show(iter->data) &&
(!default_ || !g_app_info_equal(iter->data, default_)))
append_opener(menu, iter->data, ctx);
else
g_object_unref(iter->data);
}
if (fallback) {
g_list_free(fallback);
gtk_menu_shell_append(
GTK_MENU_SHELL(menu), gtk_separator_menu_item_new());
}
GtkWidget *item = gtk_menu_item_new_with_label("Open With...");
g_signal_connect_data(item, "activate", G_CALLBACK(on_chooser_activate),
ctx, open_context_notify, 0);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
// As per GTK+ 3 Common Questions, 1.5.
g_object_ref_sink(menu);
g_signal_connect_swapped(menu, "deactivate",
G_CALLBACK(g_idle_add), destroy_widget_idle_source_func);
g_signal_connect(menu, "destroy", G_CALLBACK(g_object_unref), NULL);
gtk_widget_show_all(menu);
return GTK_MENU(menu);
}
static void
show_context_menu(GtkWidget *widget, GFile *file)
{
gtk_menu_popup_at_pointer(make_context_menu(widget, file), NULL);
}
// --- Boilerplate -------------------------------------------------------------
G_DEFINE_TYPE_EXTENDED(FivBrowser, fiv_browser, GTK_TYPE_WIDGET, 0,
G_IMPLEMENT_INTERFACE(GTK_TYPE_SCROLLABLE, NULL))
enum {
PROP_THUMBNAIL_SIZE = 1,
N_PROPERTIES,
// These are overriden, we do not register them.
PROP_HADJUSTMENT,
PROP_VADJUSTMENT,
PROP_HSCROLL_POLICY,
PROP_VSCROLL_POLICY,
};
static GParamSpec *browser_properties[N_PROPERTIES];
enum {
ITEM_ACTIVATED,
LAST_SIGNAL,
};
// Globals are, sadly, the canonical way of storing signal numbers.
static guint browser_signals[LAST_SIGNAL];
static void
on_adjustment_value_changed(
G_GNUC_UNUSED GtkAdjustment *adjustment, gpointer user_data)
{
FivBrowser *self = FIV_BROWSER(user_data);
gtk_widget_queue_draw(GTK_WIDGET(self));
}
static gboolean
replace_adjustment(
FivBrowser *self, GtkAdjustment **adjustment, GtkAdjustment *replacement)
{
if (*adjustment == replacement)
return FALSE;
if (*adjustment) {
g_signal_handlers_disconnect_by_func(
*adjustment, on_adjustment_value_changed, self);
g_clear_object(adjustment);
}
if (replacement) {
*adjustment = g_object_ref(replacement);
g_signal_connect(*adjustment, "value-changed",
G_CALLBACK(on_adjustment_value_changed), self);
// TODO(p): We should set it up, as it is done in relayout().
}
return TRUE;
}
static void
fiv_browser_finalize(GObject *gobject)
{
FivBrowser *self = FIV_BROWSER(gobject);
thumbnailers_abort(self);
g_array_free(self->entries, TRUE);
g_array_free(self->layouted_rows, TRUE);
if (self->model) {
g_signal_handlers_disconnect_by_data(self->model, self);
g_clear_object(&self->model);
}
cairo_surface_destroy(self->glow);
g_clear_object(&self->pointer);
replace_adjustment(self, &self->hadjustment, NULL);
replace_adjustment(self, &self->vadjustment, NULL);
G_OBJECT_CLASS(fiv_browser_parent_class)->finalize(gobject);
}
static void
fiv_browser_get_property(
GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
FivBrowser *self = FIV_BROWSER(object);
switch (property_id) {
case PROP_THUMBNAIL_SIZE:
g_value_set_enum(value, self->item_size);
break;
case PROP_HADJUSTMENT:
g_value_set_object(value, self->hadjustment);
break;
case PROP_VADJUSTMENT:
g_value_set_object(value, self->vadjustment);
break;
case PROP_HSCROLL_POLICY:
g_value_set_enum(value, self->hscroll_policy);
break;
case PROP_VSCROLL_POLICY:
g_value_set_enum(value, self->vscroll_policy);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}
}
static void
set_item_size(FivBrowser *self, FivThumbnailSize size)
{
if (size < FIV_THUMBNAIL_SIZE_MIN || size > FIV_THUMBNAIL_SIZE_MAX)
return;
if (size != self->item_size) {
self->item_size = size;
self->item_height = fiv_thumbnail_sizes[self->item_size].size;
reload_thumbnails(self);
g_object_notify_by_pspec(
G_OBJECT(self), browser_properties[PROP_THUMBNAIL_SIZE]);
}
}
static void
fiv_browser_set_property(
GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
FivBrowser *self = FIV_BROWSER(object);
switch (property_id) {
case PROP_THUMBNAIL_SIZE:
set_item_size(self, g_value_get_enum(value));
break;
case PROP_HADJUSTMENT:
if (replace_adjustment(
self, &self->hadjustment, g_value_get_object(value)))
g_object_notify_by_pspec(object, pspec);
break;
case PROP_VADJUSTMENT:
if (replace_adjustment(
self, &self->vadjustment, g_value_get_object(value)))
g_object_notify_by_pspec(object, pspec);
break;
case PROP_HSCROLL_POLICY:
if ((gint) self->hscroll_policy != g_value_get_enum(value)) {
self->hscroll_policy = g_value_get_enum(value);
gtk_widget_queue_resize(GTK_WIDGET(self));
g_object_notify_by_pspec(object, pspec);
}
break;
case PROP_VSCROLL_POLICY:
if ((gint) self->vscroll_policy != g_value_get_enum(value)) {
self->vscroll_policy = g_value_get_enum(value);
gtk_widget_queue_resize(GTK_WIDGET(self));
g_object_notify_by_pspec(object, pspec);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}
}
static GtkSizeRequestMode
fiv_browser_get_request_mode(G_GNUC_UNUSED GtkWidget *widget)
{
return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
}
static void
fiv_browser_get_preferred_width(GtkWidget *widget, gint *minimum, gint *natural)
{
FivBrowser *self = FIV_BROWSER(widget);
GtkStyleContext *style = gtk_widget_get_style_context(widget);
GtkBorder padding = {};
gtk_style_context_get_padding(style, GTK_STATE_FLAG_NORMAL, &padding);
*minimum = *natural =
FIV_THUMBNAIL_WIDE_COEFFICIENT * self->item_height + padding.left +
2 * self->item_border_x + padding.right;
}
static void
fiv_browser_get_preferred_height_for_width(
GtkWidget *widget, gint width, gint *minimum, gint *natural)
{
// XXX: This is rather ugly, the caller is only asking.
*minimum = *natural = relayout(FIV_BROWSER(widget), width);
}
static void
fiv_browser_realize(GtkWidget *widget)
{
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
GdkWindowAttr attributes = {
.window_type = GDK_WINDOW_CHILD,
.x = allocation.x,
.y = allocation.y,
.width = allocation.width,
.height = allocation.height,
// Input-only would presumably also work (as in GtkPathBar, e.g.),
// but it merely seems to involve more work.
.wclass = GDK_INPUT_OUTPUT,
.visual = gtk_widget_get_visual(widget),
.event_mask = gtk_widget_get_events(widget) | GDK_KEY_PRESS_MASK |
GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_SCROLL_MASK,
};
// We need this window to receive input events at all.
// TODO(p): See if input events bubble up to parents.
GdkWindow *window = gdk_window_new(gtk_widget_get_parent_window(widget),
&attributes, GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL);
gtk_widget_register_window(widget, window);
gtk_widget_set_window(widget, window);
gtk_widget_set_realized(widget, TRUE);
FivBrowser *self = FIV_BROWSER(widget);
g_clear_object(&self->pointer);
self->pointer =
gdk_cursor_new_from_name(gdk_window_get_display(window), "pointer");
}
static void
fiv_browser_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
FivBrowser *self = FIV_BROWSER(widget);
GTK_WIDGET_CLASS(fiv_browser_parent_class)
->size_allocate(widget, allocation);
int height = relayout(FIV_BROWSER(widget), allocation->width);
// Avoid fresh blank space.
if (self->vadjustment) {
double y1 = gtk_adjustment_get_value(self->vadjustment);
double ph = gtk_adjustment_get_page_size(self->vadjustment);
if (y1 + ph > height)
gtk_adjustment_set_value(self->vadjustment, height - ph);
}
}
static gboolean
fiv_browser_draw(GtkWidget *widget, cairo_t *cr)
{
FivBrowser *self = FIV_BROWSER(widget);
if (!gtk_cairo_should_draw_window(cr, gtk_widget_get_window(widget)))
return TRUE;
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
gtk_render_background(gtk_widget_get_style_context(widget), cr, 0, 0,
allocation.width, allocation.height);
// TODO(p): self->hadjustment as well, and test it.
if (self->vadjustment) {
gdouble y = round(gtk_adjustment_get_value(self->vadjustment));
cairo_translate(cr, 0, -y);
}
GdkRectangle clip = {};
gboolean have_clip = gdk_cairo_get_clip_rectangle(cr, &clip);
for (guint i = 0; i < self->layouted_rows->len; i++) {
const Row *row = &g_array_index(self->layouted_rows, Row, i);
GdkRectangle extents = {
.x = 0,
.y = row->y_offset - self->item_border_y,
.width = allocation.width,
.height = self->item_height + 2 * self->item_border_y,
};
if (!have_clip || gdk_rectangle_intersect(&clip, &extents, NULL))
draw_row(self, cr, row);
}
return TRUE;
}
static gboolean
open_entry(GtkWidget *self, const Entry *entry, gboolean new_window)
{
GFile *location = g_file_new_for_uri(entry->uri);
g_signal_emit(self, browser_signals[ITEM_ACTIVATED], 0, location,
new_window ? GTK_PLACES_OPEN_NEW_WINDOW : GTK_PLACES_OPEN_NORMAL);
g_object_unref(location);
return TRUE;
}
static gboolean
fiv_browser_button_press_event(GtkWidget *widget, GdkEventButton *event)
{
GTK_WIDGET_CLASS(fiv_browser_parent_class)
->button_press_event(widget, event);
FivBrowser *self = FIV_BROWSER(widget);
if (event->type != GDK_BUTTON_PRESS)
return FALSE;
guint state = event->state & gtk_accelerator_get_default_mod_mask();
if (event->button == GDK_BUTTON_PRIMARY && state == 0 &&
gtk_widget_get_focus_on_click(widget))
gtk_widget_grab_focus(widget);
const Entry *entry = entry_at(self, event->x, event->y);
if (!entry && state == 0) {
switch (event->button) {
case GDK_BUTTON_PRIMARY:
break;
case GDK_BUTTON_SECONDARY:
show_context_menu(widget, fiv_io_model_get_location(self->model));
break;
default:
return FALSE;
}
if (self->selected) {
self->selected = NULL;
gtk_widget_queue_draw(widget);
}
return TRUE;
}
if (!entry)
return FALSE;
switch (event->button) {
case GDK_BUTTON_PRIMARY:
if (state == 0)
return open_entry(widget, entry, FALSE);
if (state == GDK_CONTROL_MASK)
return open_entry(widget, entry, TRUE);
return FALSE;
case GDK_BUTTON_MIDDLE:
if (state == 0)
return open_entry(widget, entry, TRUE);
return FALSE;
case GDK_BUTTON_SECONDARY:
self->selected = entry;
gtk_widget_queue_draw(widget);
// On X11, after closing the menu, the pointer otherwise remains,
// no matter what its new location is.
gdk_window_set_cursor(gtk_widget_get_window(widget), NULL);
GFile *file = g_file_new_for_uri(entry->uri);
show_context_menu(widget, file);
g_object_unref(file);
return TRUE;
default:
return FALSE;
}
}
static gboolean
fiv_browser_motion_notify_event(GtkWidget *widget, GdkEventMotion *event)
{
GTK_WIDGET_CLASS(fiv_browser_parent_class)
->motion_notify_event(widget, event);
FivBrowser *self = FIV_BROWSER(widget);
if (event->state != 0)
return FALSE;
const Entry *entry = entry_at(self, event->x, event->y);
GdkWindow *window = gtk_widget_get_window(widget);
gdk_window_set_cursor(window, entry ? self->pointer : NULL);
return TRUE;
}
static gboolean
fiv_browser_scroll_event(GtkWidget *widget, GdkEventScroll *event)
{
FivBrowser *self = FIV_BROWSER(widget);
if ((event->state & gtk_accelerator_get_default_mod_mask()) !=
GDK_CONTROL_MASK)
return FALSE;
switch (event->direction) {
case GDK_SCROLL_UP:
set_item_size(self, self->item_size + 1);
return TRUE;
case GDK_SCROLL_DOWN:
set_item_size(self, self->item_size - 1);
return TRUE;
default:
// For some reason, we can also get GDK_SCROLL_SMOOTH.
// Left/right are good to steal from GtkScrolledWindow for consistency.
return TRUE;
}
}
static void
select_closest(FivBrowser *self, const Row *row, int target)
{
int closest = G_MAXINT;
for (guint i = 0; i < row->len; i++) {
GdkRectangle extents = item_extents(self, row->items + i, row);
int distance = ABS(extents.x + extents.width / 2 - target);
if (distance > closest)
break;
self->selected = row->items[i].entry;
closest = distance;
}
}
static void
scroll_to_row(FivBrowser *self, const Row *row)
{
if (!self->vadjustment)
return;
double y1 = gtk_adjustment_get_value(self->vadjustment);
double ph = gtk_adjustment_get_page_size(self->vadjustment);
if (row->y_offset < y1) {
gtk_adjustment_set_value(
self->vadjustment, row->y_offset - self->item_border_y);
} else if (row->y_offset + self->item_height > y1 + ph) {
gtk_adjustment_set_value(self->vadjustment,
row->y_offset - ph + self->item_height + self->item_border_y);
}
}
static void
move_selection(FivBrowser *self, GtkDirectionType dir)
{
GtkWidget *widget = GTK_WIDGET(self);
if (!self->layouted_rows->len)
return;
const Row *selected_row = NULL;
if (!self->selected) {
switch (dir) {
case GTK_DIR_RIGHT:
case GTK_DIR_DOWN:
selected_row = &g_array_index(self->layouted_rows, Row, 0);
self->selected = selected_row->items->entry;
goto adjust;
case GTK_DIR_LEFT:
case GTK_DIR_UP:
selected_row = &g_array_index(
self->layouted_rows, Row, self->layouted_rows->len - 1);
self->selected = selected_row->items[selected_row->len - 1].entry;
goto adjust;
default:
g_assert_not_reached();
}
}
gsize x = 0, y = 0;
int target_offset = 0;
for (y = 0; y < self->layouted_rows->len; y++) {
const Row *row = &g_array_index(self->layouted_rows, Row, y);
for (x = 0; x < row->len; x++) {
const Item *item = row->items + x;
if (item->entry == self->selected) {
GdkRectangle extents = item_extents(self, item, row);
target_offset = extents.x + extents.width / 2;
goto found;
}
}
}
found:
g_return_if_fail(y < self->layouted_rows->len);
selected_row = &g_array_index(self->layouted_rows, Row, y);
switch (dir) {
case GTK_DIR_LEFT:
if (x > 0) {
self->selected = selected_row->items[--x].entry;
} else if (y-- > 0) {
selected_row = &g_array_index(self->layouted_rows, Row, y);
self->selected = selected_row->items[selected_row->len - 1].entry;
}
break;
case GTK_DIR_RIGHT:
if (++x < selected_row->len) {
self->selected = selected_row->items[x].entry;
} else if (++y < self->layouted_rows->len) {
selected_row = &g_array_index(self->layouted_rows, Row, y);
self->selected = selected_row->items[0].entry;
}
break;
case GTK_DIR_UP:
if (y-- > 0) {
selected_row = &g_array_index(self->layouted_rows, Row, y);
select_closest(self, selected_row, target_offset);
}
break;
case GTK_DIR_DOWN:
if (++y < self->layouted_rows->len) {
selected_row = &g_array_index(self->layouted_rows, Row, y);
select_closest(self, selected_row, target_offset);
}
break;
default:
g_assert_not_reached();
}
adjust:
// TODO(p): We should also do it horizontally, although we don't use it.
scroll_to_row(self, selected_row);
gtk_widget_queue_draw(widget);
}
static void
move_selection_home(FivBrowser *self)
{
if (self->layouted_rows->len) {
const Row *row = &g_array_index(self->layouted_rows, Row, 0);
self->selected = row->items[0].entry;
scroll_to_row(self, row);
gtk_widget_queue_draw(GTK_WIDGET(self));
}
}
static void
move_selection_end(FivBrowser *self)
{
if (self->layouted_rows->len) {
const Row *row = &g_array_index(
self->layouted_rows, Row, self->layouted_rows->len - 1);
self->selected = row->items[row->len - 1].entry;
scroll_to_row(self, row);
gtk_widget_queue_draw(GTK_WIDGET(self));
}
}
static gboolean
fiv_browser_key_press_event(GtkWidget *widget, GdkEventKey *event)
{
FivBrowser *self = FIV_BROWSER(widget);
if (!(event->state & gtk_accelerator_get_default_mod_mask())) {
switch (event->keyval) {
case GDK_KEY_Return:
if (self->selected)
return open_entry(widget, self->selected, FALSE);
return TRUE;
case GDK_KEY_Left:
move_selection(self, GTK_DIR_LEFT);
return TRUE;
case GDK_KEY_Right:
move_selection(self, GTK_DIR_RIGHT);
return TRUE;
case GDK_KEY_Up:
move_selection(self, GTK_DIR_UP);
return TRUE;
case GDK_KEY_Down:
move_selection(self, GTK_DIR_DOWN);
return TRUE;
case GDK_KEY_Home:
move_selection_home(self);
return TRUE;
case GDK_KEY_End:
move_selection_end(self);
return TRUE;
}
}
return GTK_WIDGET_CLASS(fiv_browser_parent_class)
->key_press_event(widget, event);
}
static gboolean
fiv_browser_query_tooltip(GtkWidget *widget, gint x, gint y,
G_GNUC_UNUSED gboolean keyboard_tooltip, GtkTooltip *tooltip)
{
FivBrowser *self = FIV_BROWSER(widget);
const Entry *entry = entry_at(self, x, y);
if (!entry)
return FALSE;
GFile *file = g_file_new_for_uri(entry->uri);
GFileInfo *info = g_file_query_info(file,
G_FILE_ATTRIBUTE_STANDARD_NAME
"," G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
G_FILE_QUERY_INFO_NONE, NULL, NULL);
g_object_unref(file);
if (!info)
return FALSE;
gtk_tooltip_set_text(tooltip, g_file_info_get_display_name(info));
g_object_unref(info);
return TRUE;
}
static gboolean
fiv_browser_popup_menu(GtkWidget *widget)
{
FivBrowser *self = FIV_BROWSER(widget);
// This is what Windows Explorer does, and what you want to be done.
// Although invoking the menu outside the widget is questionable.
GFile *file = NULL;
GdkRectangle rect = {};
if (self->selected) {
file = g_file_new_for_uri(self->selected->uri);
rect = entry_rect(self, self->selected);
rect.x += rect.width / 2;
rect.y += rect.height / 2;
} else {
file = g_object_ref(fiv_io_model_get_location(self->model));
}
gtk_menu_popup_at_rect(make_context_menu(widget, file),
gtk_widget_get_window(widget), &rect, GDK_GRAVITY_NORTH_WEST,
GDK_GRAVITY_NORTH_WEST, NULL);
g_object_unref(file);
return TRUE;
}
static void
fiv_browser_style_updated(GtkWidget *widget)
{
GTK_WIDGET_CLASS(fiv_browser_parent_class)->style_updated(widget);
FivBrowser *self = FIV_BROWSER(widget);
GtkStyleContext *style = gtk_widget_get_style_context(widget);
GtkBorder border = {}, margin = {};
int item_spacing = self->item_spacing;
gtk_widget_style_get(widget, "spacing", &self->item_spacing, NULL);
if (item_spacing != self->item_spacing)
gtk_widget_queue_resize(widget);
// Using a pseudo-class, because GTK+ regions are deprecated.
gtk_style_context_save(style);
gtk_style_context_add_class(style, "item");
gtk_style_context_get_margin(style, GTK_STATE_FLAG_NORMAL, &margin);
gtk_style_context_get_border(style, GTK_STATE_FLAG_NORMAL, &border);
gtk_style_context_restore(style);
const int glow_w = (margin.left + margin.right) / 2;
const int glow_h = (margin.top + margin.bottom) / 2;
// Don't set different opposing sides, it will misrender, your problem.
// When the style of the class changes, this virtual method isn't invoked,
// so the update check is mildly pointless.
int item_border_x = glow_w + (border.left + border.right) / 2;
int item_border_y = glow_h + (border.top + border.bottom) / 2;
if (item_border_x != self->item_border_x ||
item_border_y != self->item_border_y) {
self->item_border_x = item_border_x;
self->item_border_y = item_border_y;
gtk_widget_queue_resize(widget);
}
if (self->glow)
cairo_surface_destroy(self->glow);
if (glow_w <= 0 || glow_h <= 0) {
self->glow = cairo_image_surface_create(CAIRO_FORMAT_A1, 0, 0);
return;
}
self->glow = cairo_image_surface_create(CAIRO_FORMAT_A8, glow_w, glow_h);
unsigned char *data = cairo_image_surface_get_data(self->glow);
int stride = cairo_image_surface_get_stride(self->glow);
// Smooth out the curve, so that the edge of the glow isn't too jarring.
const double fade_factor = 1.5;
const int x_max = glow_w - 1;
const int y_max = glow_h - 1;
const double x_scale = 1. / MAX(1, x_max);
const double y_scale = 1. / MAX(1, y_max);
for (int y = 0; y <= y_max; y++)
for (int x = 0; x <= x_max; x++) {
const double xn = x_scale * (x_max - x);
const double yn = y_scale * (y_max - y);
double v = MIN(sqrt(xn * xn + yn * yn), 1);
data[y * stride + x] = round(pow(1 - v, fade_factor) * 255);
}
cairo_surface_mark_dirty(self->glow);
}
static void
fiv_browser_class_init(FivBrowserClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = fiv_browser_finalize;
object_class->get_property = fiv_browser_get_property;
object_class->set_property = fiv_browser_set_property;
browser_properties[PROP_THUMBNAIL_SIZE] = g_param_spec_enum(
"thumbnail-size", "Thumbnail size", "The thumbnail height to use",
FIV_TYPE_THUMBNAIL_SIZE, FIV_THUMBNAIL_SIZE_NORMAL,
G_PARAM_READWRITE);
g_object_class_install_properties(
object_class, N_PROPERTIES, browser_properties);
g_object_class_override_property(
object_class, PROP_HADJUSTMENT, "hadjustment");
g_object_class_override_property(
object_class, PROP_VADJUSTMENT, "vadjustment");
g_object_class_override_property(
object_class, PROP_HSCROLL_POLICY, "hscroll-policy");
g_object_class_override_property(
object_class, PROP_VSCROLL_POLICY, "vscroll-policy");
browser_signals[ITEM_ACTIVATED] = g_signal_new("item-activated",
G_TYPE_FROM_CLASS(klass), 0, 0, NULL, NULL, NULL,
G_TYPE_NONE, 2, G_TYPE_FILE, GTK_TYPE_PLACES_OPEN_FLAGS);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
widget_class->get_request_mode = fiv_browser_get_request_mode;
widget_class->get_preferred_width = fiv_browser_get_preferred_width;
widget_class->get_preferred_height_for_width =
fiv_browser_get_preferred_height_for_width;
widget_class->realize = fiv_browser_realize;
widget_class->draw = fiv_browser_draw;
widget_class->size_allocate = fiv_browser_size_allocate;
widget_class->button_press_event = fiv_browser_button_press_event;
widget_class->motion_notify_event = fiv_browser_motion_notify_event;
widget_class->scroll_event = fiv_browser_scroll_event;
widget_class->key_press_event = fiv_browser_key_press_event;
widget_class->query_tooltip = fiv_browser_query_tooltip;
widget_class->popup_menu = fiv_browser_popup_menu;
widget_class->style_updated = fiv_browser_style_updated;
// Could be split to also-idiomatic row-spacing/column-spacing properties.
// The GParamSpec is sinked by this call.
gtk_widget_class_install_style_property(widget_class,
g_param_spec_int("spacing", "Spacing", "Space between items",
0, G_MAXINT, 1, G_PARAM_READWRITE));
// TODO(p): Later override "screen_changed", recreate Pango layouts there,
// if we get to have any, or otherwise reflect DPI changes.
gtk_widget_class_set_css_name(widget_class, "fiv-browser");
}
static void
fiv_browser_init(FivBrowser *self)
{
gtk_widget_set_can_focus(GTK_WIDGET(self), TRUE);
gtk_widget_set_has_tooltip(GTK_WIDGET(self), TRUE);
self->entries = g_array_new(FALSE, TRUE, sizeof(Entry));
g_array_set_clear_func(self->entries, (GDestroyNotify) entry_free);
self->layouted_rows = g_array_new(FALSE, TRUE, sizeof(Row));
g_array_set_clear_func(self->layouted_rows, (GDestroyNotify) row_free);
self->thumbnailers_len = g_get_num_processors();
self->thumbnailers =
g_malloc0_n(self->thumbnailers_len, sizeof *self->thumbnailers);
for (size_t i = 0; i < self->thumbnailers_len; i++)
self->thumbnailers[i].self = self;
set_item_size(self, FIV_THUMBNAIL_SIZE_NORMAL);
self->glow = cairo_image_surface_create(CAIRO_FORMAT_A1, 0, 0);
g_signal_connect_swapped(gtk_settings_get_default(),
"notify::gtk-icon-theme-name", G_CALLBACK(reload_thumbnails), self);
}
// --- Public interface --------------------------------------------------------
static void
on_model_files_changed(FivIoModel *model, FivBrowser *self)
{
g_return_if_fail(model == self->model);
gchar *selected_uri = NULL;
if (self->selected)
selected_uri = g_strdup(self->selected->uri);
// TODO(p): Later implement arguments.
thumbnailers_abort(self);
g_array_set_size(self->entries, 0);
g_array_set_size(self->layouted_rows, 0);
GPtrArray *files = fiv_io_model_get_files(self->model);
for (guint i = 0; i < files->len; i++) {
g_array_append_val(self->entries,
((Entry) {.thumbnail = NULL, .uri = files->pdata[i]}));
files->pdata[i] = NULL;
}
g_ptr_array_free(files, TRUE);
fiv_browser_select(self, selected_uri);
g_free(selected_uri);
reload_thumbnails(self);
thumbnailers_start(self);
}
GtkWidget *
fiv_browser_new(FivIoModel *model)
{
g_return_val_if_fail(FIV_IS_IO_MODEL(model), NULL);
FivBrowser *self = g_object_new(FIV_TYPE_BROWSER, NULL);
self->model = g_object_ref(model);
g_signal_connect(
self->model, "files-changed", G_CALLBACK(on_model_files_changed), self);
on_model_files_changed(self->model, self);
return GTK_WIDGET(self);
}
void
fiv_browser_select(FivBrowser *self, const char *uri)
{
g_return_if_fail(FIV_IS_BROWSER(self));
self->selected = NULL;
gtk_widget_queue_draw(GTK_WIDGET(self));
if (!uri)
return;
for (guint i = 0; i < self->entries->len; i++) {
const Entry *entry = &g_array_index(self->entries, Entry, i);
if (!g_strcmp0(entry->uri, uri)) {
self->selected = entry;
break;
}
}
// TODO(p): Scroll to selection.
}
|