aboutsummaryrefslogtreecommitdiff
path: root/fastiv-browser.c
blob: 5fdbb8d5eacfccbcb5e01570b12b5a152b334b71 (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
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
//
// fastiv-browser.c: fast image viewer - filesystem browser widget
//
// Copyright (c) 2021, 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 "fastiv-browser.h"
#include "fastiv-io.h"
#include "fastiv-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
//                    │   ╭┄┄┄┄┄┄┄┄┄┄┄┄╮   ╭┄┄┄┄┄┄┄┄┄┄┄┄
//

struct _FastivBrowser {
	GtkWidget parent_instance;

	GArray *entries;                    ///< [Entry]
	GArray *layouted_rows;              ///< [Row]
	int selected;

	cairo_surface_t *glow;              ///< CAIRO_FORMAT_A8 mask
};

typedef struct entry Entry;
typedef struct item Item;
typedef struct row Row;

static const double g_row_height = 256;
static const double g_permitted_width_multiplier = 2;

// Could be split out to also-idiomatic row-spacing/column-spacing properties.
// TODO(p): Make a property for this.
static const int g_item_spacing = 1;

// All around, from the primary colour to transparency.
static const int g_item_border = 10;

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

struct entry {
	char *filename;                     ///< Absolute path
	cairo_surface_t *thumbnail;         ///< Prescaled thumbnail
};

static void
entry_free(Entry *self)
{
	g_free(self->filename);
	if (self->thumbnail)
		cairo_surface_destroy(self->thumbnail);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

struct item {
	const Entry *entry;
	int x_offset;                       ///< Offset within the row
};

struct row {
	Item *items;                        ///< Ends with a NULL entry
	int x_offset;                       ///< Start position including padding
	int y_offset;                       ///< Start position including padding
};

static void
row_free(Row *self)
{
	g_free(self->items);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

static void
append_row(FastivBrowser *self, int *y, int x, GArray *items_array)
{
	if (self->layouted_rows->len)
		*y += g_item_spacing;

	*y += g_item_border;
	g_array_append_val(self->layouted_rows, ((Row) {
		.items = g_array_steal(items_array, NULL),
		.x_offset = x,
		.y_offset = *y,
	}));

	// Not trying to pack them vertically, but this would be the place to do it.
	*y += g_row_height;
	*y += g_item_border;
}

static int
relayout(FastivBrowser *self, int width)
{
	GtkWidget *widget = GTK_WIDGET(self);
	GtkStyleContext *context = gtk_widget_get_style_context(widget);

	GtkBorder padding = {};
	gtk_style_context_get_padding(context, 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 * g_item_border;
		if (!items->len) {
			// Just insert it, whether or not there's any space.
		} else if (x + g_item_spacing + width <= available_width) {
			x += g_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 + g_item_border}));
		x += width;
	}
	if (items->len) {
		append_row(self, &y,
			padding.left + MAX(0, available_width - x) / 2, items);
	}

	g_array_free(items, TRUE);
	return y + padding.bottom;
}

static void
draw_item_border(FastivBrowser *self, cairo_t *cr, int width, int height)
{
	cairo_set_source_rgb(cr, .75, .75, .75);
	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, -g_item_border, -g_item_border);
	cairo_rectangle(cr, 0, 0, g_item_border + width, g_item_border + height);
	cairo_clip(cr);
	cairo_mask(cr, mask);
	cairo_restore(cr);

	cairo_save(cr);
	cairo_translate(cr, width + g_item_border, height + g_item_border);
	cairo_rectangle(cr, 0, 0, -g_item_border - width, -g_item_border - height);
	cairo_clip(cr);
	cairo_matrix_init_scale(&matrix, -1, -1);
	cairo_pattern_set_matrix(mask, &matrix);
	cairo_mask(cr, mask);
	cairo_restore(cr);

	cairo_pattern_set_extend(mask, CAIRO_EXTEND_NONE);

	cairo_save(cr);
	cairo_translate(cr, width + g_item_border, -g_item_border);
	cairo_matrix_init_scale(&matrix, -1, 1);
	cairo_pattern_set_matrix(mask, &matrix);
	cairo_mask(cr, mask);
	cairo_restore(cr);

	cairo_save(cr);
	cairo_translate(cr, -g_item_border, height + g_item_border);
	cairo_matrix_init_scale(&matrix, 1, -1);
	cairo_pattern_set_matrix(mask, &matrix);
	cairo_mask(cr, mask);
	cairo_restore(cr);

	// TODO(p): Distinguish between an inner and outer border,
	// don't override part of the glow.
	cairo_rectangle(cr, -.5, -.5, width + 1, height + 1);
	cairo_set_source_rgba(cr, 1, 1, 1, 0.75);
	cairo_set_line_width(cr, 1);
	cairo_stroke(cr);

	cairo_pattern_destroy(mask);
}

// --- Boilerplate -------------------------------------------------------------

// TODO(p): For proper navigation, we need to implement GtkScrollable.
G_DEFINE_TYPE_EXTENDED(FastivBrowser, fastiv_browser, GTK_TYPE_WIDGET, 0,
	/* G_IMPLEMENT_INTERFACE(GTK_TYPE_SCROLLABLE,
		fastiv_browser_scrollable_init) */)

enum {
	ITEM_ACTIVATED,
	LAST_SIGNAL,
};

// Globals are, sadly, the canonical way of storing signal numbers.
static guint browser_signals[LAST_SIGNAL];

static void
fastiv_browser_finalize(GObject *gobject)
{
	FastivBrowser *self = FASTIV_BROWSER(gobject);
	g_array_free(self->entries, TRUE);
	g_array_free(self->layouted_rows, TRUE);
	cairo_surface_destroy(self->glow);

	G_OBJECT_CLASS(fastiv_browser_parent_class)->finalize(gobject);
}

static GtkSizeRequestMode
fastiv_browser_get_request_mode(G_GNUC_UNUSED GtkWidget *widget)
{
	return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
}

static void
fastiv_browser_get_preferred_width(
	GtkWidget *widget, gint *minimum, gint *natural)
{
	GtkBorder padding = {};
	GtkStyleContext *context = gtk_widget_get_style_context(widget);
	gtk_style_context_get_padding(context, GTK_STATE_FLAG_NORMAL, &padding);
	*minimum = *natural = g_permitted_width_multiplier * g_row_height +
		padding.left + 2 * g_item_border + padding.right;
}

static void
fastiv_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(FASTIV_BROWSER(widget), width);
}

static void
fastiv_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_BUTTON_PRESS_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);
}

static void
fastiv_browser_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
	GTK_WIDGET_CLASS(fastiv_browser_parent_class)
		->size_allocate(widget, allocation);

	relayout(FASTIV_BROWSER(widget), allocation->width);
}

static gboolean
fastiv_browser_draw(GtkWidget *widget, cairo_t *cr)
{
	FastivBrowser *self = FASTIV_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);

	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++) {
			cairo_surface_t *thumbnail = item->entry->thumbnail;
			int width = cairo_image_surface_get_width(thumbnail);
			int height = cairo_image_surface_get_height(thumbnail);
			int x = row->x_offset + item->x_offset;
			int y = row->y_offset + g_row_height - height;

			// TODO(p): Test whether we need to render this first.

			cairo_save(cr);
			cairo_translate (cr, x, y);
			draw_item_border(self, cr, width, height);

			cairo_rectangle(cr, 0, 0, width, height);
			cairo_set_source_rgb(cr, .25, .25, .25);
			cairo_fill(cr);

			cairo_set_source_surface(cr, thumbnail, 0, 0);
			cairo_paint(cr);
			cairo_restore(cr);
		}
	}
	return TRUE;
}

static void
fastiv_browser_class_init(FastivBrowserClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);
	object_class->finalize = fastiv_browser_finalize;

	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
	widget_class->get_request_mode = fastiv_browser_get_request_mode;
	widget_class->get_preferred_width = fastiv_browser_get_preferred_width;
	widget_class->get_preferred_height_for_width =
		fastiv_browser_get_preferred_height_for_width;
	widget_class->realize = fastiv_browser_realize;
	widget_class->draw = fastiv_browser_draw;
	widget_class->size_allocate = fastiv_browser_size_allocate;

	// TODO(p): Connect to this and emit it.
	browser_signals[ITEM_ACTIVATED] = g_signal_new("item-activated",
		G_TYPE_FROM_CLASS(klass), 0, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);

	// 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, "fastiv-browser");
}

static void
fastiv_browser_init(FastivBrowser *self)
{
	gtk_widget_set_can_focus(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->selected = -1;

	self->glow = cairo_image_surface_create(
		CAIRO_FORMAT_A8, g_item_border, g_item_border);
	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 doesn't stand out
	const double fade_factor = 1.5;

	const int max = g_item_border - 1;
	for (int y = 0; y <= max; y++)
		for (int x = 0; x <= max; x++) {
			int xn = max - x;
			int yn = max - y;
			double v = MIN(sqrt(xn * xn + yn * yn) / max, 1);
			data[y * stride + x] = round(pow(1 - v, fade_factor) * 255);
		}
	cairo_surface_mark_dirty(self->glow);
}

static cairo_surface_t *
rescale_thumbnail(cairo_surface_t *thumbnail)
{
	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 > g_permitted_width_multiplier * height) {
		scale_x = g_permitted_width_multiplier * g_row_height / width;
		scale_y = round(scale_x * height) / height;
	} else {
		scale_y = g_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_surface_t *scaled = cairo_image_surface_create(
		CAIRO_FORMAT_ARGB32, 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 = 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_destroy(thumbnail);
	cairo_surface_mark_dirty(scaled);
	return scaled;
}

void
fastiv_browser_load(FastivBrowser *self, const char *path)
{
	g_array_set_size(self->entries, 0);

	// TODO(p): Use opendir(), in order to get file type directly.
	GDir *dir = g_dir_open(path, 0, NULL);
	if (!dir)
		return;

	const char *filename;
	while ((filename = g_dir_read_name(dir))) {
		if (!strcmp(filename, ".") || !strcmp(filename, ".."))
			continue;

		gchar *subpath = g_build_filename(path, filename, NULL);
		g_array_append_val(self->entries,
			((Entry){
				.thumbnail =
					rescale_thumbnail(fastiv_io_lookup_thumbnail(subpath)),
				.filename = subpath,
			}));
	}
	g_dir_close(dir);

	// TODO(p): Sort the entries.
	gtk_widget_queue_draw(GTK_WIDGET(self));
}