/*
 * The folder grid Photos opens on: Pictures (or the folder of the last
 * picture viewed), its folders as dark tiles and its pictures as
 * thumbnails from the shared cache, made in a sandbox when missing. A
 * folder opens in place, a picture in the viewer with the folder's
 * pictures to flip through. The folder is watched, so pictures saved or
 * deleted elsewhere come and go.
 *
 * Or by date, as Windows 8.1's collection: every picture in Pictures,
 * newest first, a grid for each month under its name. A picture opened
 * there flips through all of them in that order.
 */
#include <math.h>
#include <string.h>
#include <c8ui/c8ui-files.h>
#include "photos.h"
#include "stealth.h"

#define CELL 190
#define GRID_GAP 10 // c8ui's list puts this between cells
#define RELOAD_MS 300

static struct {
	struct c8ui_widget *box, *header, *back, *title, *scroll, *list, *note;
	struct c8ui_widget *ring;
	struct browse_hooks hooks;
	char *folder, *top, *select;
	GPtrArray *entries;
	GCancellable *cancel;
	GFileMonitor *monitor;
	guint reload_timer;
	bool narrow;

	/* By date */
	bool by_date;
	struct c8ui_widget *dscroll, *months;
	GPtrArray *dated; // every picture, newest first
	GArray *runs; // struct run: the months, in order
	GHashTable *dated_at; // ph_entry -> its index in dated, + 1
	GCancellable *dcancel;
	struct c8ui_widget *dlist; // the month list last used
} b;

/* A month: dated[first .. first + count), and its grid */
struct run {
	int first, count;
	struct c8ui_widget *list;
};

static void update_header(void);

static struct ph_entry *entry_of(void *data, int index) {
	if (data == NULL) {
		return b.entries && index < (int)b.entries->len ?
			b.entries->pdata[index] : NULL;
	}
	struct run *r = &g_array_index(b.runs, struct run,
		GPOINTER_TO_INT(data) - 1);
	return index < r->count ? b.dated->pdata[r->first + index] : NULL;
}

static int index_of(struct ph_entry *e) {
	for (guint i = 0; b.entries && i < b.entries->len; i++) {
		if (b.entries->pdata[i] == e) {
			return i;
		}
	}
	return -1;
}

static void got_thumb(cairo_surface_t *thumb, void *data) {
	struct ph_entry *e = data;
	e->thumb_load = NULL;
	e->thumb = thumb;
	e->thumb_failed = thumb == NULL;
	int i = index_of(e);
	if (i >= 0 && b.list) {
		c8ui_list_redraw_item(b.list, i);
	}
	/* Or in a month: the date view's entries are its own, found by
	 * their place in it */
	int at = b.dated_at ? GPOINTER_TO_INT(g_hash_table_lookup(b.dated_at,
		e)) - 1 : -1;
	for (guint m = 0; at >= 0 && b.runs && m < b.runs->len; m++) {
		struct run *r = &g_array_index(b.runs, struct run, m);
		if (at >= r->first && at < r->first + r->count) {
			if (r->list) {
				c8ui_list_redraw_item(r->list, at - r->first);
			}
			break;
		}
	}
}

static void draw_name(cairo_t *cr, struct c8ui_widget *list,
		const struct c8ui_list_item *item, const char *name) {
	const char *family = c8ui_widget_font(list);
	struct c8ui_font font = c8ui_text_font(C8UI_TEXT_BODY);
	c8ui_set_source(cr, 0xffffff, 1);
	c8ui_text_draw(cr, family, font, name, item->rect.x + 10,
		item->rect.y + item->rect.height - 30, item->rect.width - 20, 1);
}

static void draw_item(struct c8ui_widget *list, cairo_t *cr,
		const struct c8ui_list_item *item, void *data) {
	struct ph_entry *e = entry_of(data, item->index);
	if (e == NULL) {
		return;
	}
	struct c8ui_rect r = item->rect;
	if (e->folder) {
		c8ui_set_source(cr, 0x2b2b2b, 1);
		cairo_rectangle(cr, r.x, r.y, r.width, r.height);
		cairo_fill(cr);
		/* A folder, drawn in the app bar glyphs' thin white line */
		double fw = round(r.width * 0.34), fh = round(fw * 0.74);
		double fx = round(r.x + (r.width - fw) / 2);
		double fy = round(r.y + (r.height - fh) / 2 - 14);
		c8ui_set_source(cr, 0xffffff, 0.9);
		cairo_set_line_width(cr, 2);
		cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
		cairo_move_to(cr, fx, fy + fh);
		cairo_line_to(cr, fx, fy);
		cairo_line_to(cr, fx + fw * 0.38, fy);
		cairo_line_to(cr, fx + fw * 0.48, fy + fh * 0.16);
		cairo_line_to(cr, fx + fw, fy + fh * 0.16);
		cairo_line_to(cr, fx + fw, fy + fh);
		cairo_close_path(cr);
		cairo_stroke(cr);
		draw_name(cr, list, item, c8_stealth_show(e->name));
	} else if (e->thumb) {
		c8ui_draw_surface_cover(cr, e->thumb, r.x, r.y, r.width, r.height);
	} else {
		c8ui_set_source(cr, 0x222222, 1);
		cairo_rectangle(cr, r.x, r.y, r.width, r.height);
		cairo_fill(cr);
		if (e->thumb_failed) {
			draw_name(cr, list, item, c8_stealth_show(e->name));
		} else {
			if (e->thumb_load == NULL) {
				e->thumb_load = c8ui_thumbnail_load(e->uri,
					C8UI_THUMBNAIL_LARGE, got_thumb, e);
			}
			c8ui_list_not_final(list);
		}
	}
	/* The states, over the picture: white when hovered, the accent when
	 * picked, as Metro's grids */
	struct c8ui_colors c = c8ui_widget_colors(list);
	if (item->selected || item->cursor || item->hovered) {
		uint32_t rgb = item->selected ? c.accent : 0xffffff;
		c8ui_set_source(cr, rgb, item->selected ? 1 : 0.6);
		cairo_set_line_width(cr, 4);
		cairo_rectangle(cr, r.x + 2, r.y + 2, r.width - 4, r.height - 4);
		cairo_stroke(cr);
	}
	if (item->pressed) {
		c8ui_set_source(cr, 0x000000, 0.25);
		cairo_rectangle(cr, r.x, r.y, r.width, r.height);
		cairo_fill(cr);
	}
}

static void opened(struct c8ui_widget *list, int index, void *data) {
	if (b.entries == NULL || index < 0 || index >= (int)b.entries->len) {
		return;
	}
	struct ph_entry *e = b.entries->pdata[index];
	if (e->folder) {
		char *uri = g_strdup(e->uri);
		browse_set_folder(uri, NULL);
		g_free(uri);
		return;
	}
	GPtrArray *pics = ph_pictures_of(b.entries);
	int at = 0;
	for (guint i = 0; i < pics->len; i++) {
		if (pics->pdata[i] == e) {
			at = i;
		}
	}
	if (b.hooks.open_picture) {
		b.hooks.open_picture(pics, at);
	}
	g_ptr_array_unref(pics);
}

/* A picture of a month: all of them to flip through, in date order */
static void opened_dated(struct c8ui_widget *list, int index, void *data) {
	struct run *r = &g_array_index(b.runs, struct run,
		GPOINTER_TO_INT(data) - 1);
	if (index < 0 || index >= r->count) {
		return;
	}
	b.dlist = list;
	GPtrArray *pics = ph_pictures_of(b.dated);
	if (b.hooks.open_picture) {
		b.hooks.open_picture(pics, r->first + index);
	}
	g_ptr_array_unref(pics);
}

static void context(struct c8ui_widget *list, int index, double x, double y,
		void *data) {
	if (index >= 0) {
		c8ui_list_select(list, index);
	}
	if (b.hooks.context) {
		b.hooks.context();
	}
}

static void select_uri(const char *uri) {
	for (guint i = 0; uri && b.entries && i < b.entries->len; i++) {
		struct ph_entry *e = b.entries->pdata[i];
		if (strcmp(e->uri, uri) == 0) {
			c8ui_list_select(b.list, i);
			c8ui_list_reveal(b.list, i);
			return;
		}
	}
}

static void listed(GPtrArray *entries, const char *error, void *data) {
	g_clear_object(&b.cancel);
	if (b.list == NULL) {
		return;
	}
	if (!b.by_date) {
		c8ui_ring_set_active(b.ring, false);
		b.ring->hidden = true;
	}
	char *keep = NULL;
	int sel = c8ui_list_get_selected(b.list);
	if (b.select == NULL && sel >= 0 && b.entries &&
			sel < (int)b.entries->len) {
		keep = g_strdup(((struct ph_entry *)b.entries->pdata[sel])->uri);
	}
	if (b.entries) {
		g_ptr_array_unref(b.entries);
	}
	b.entries = entries ? g_ptr_array_ref(entries) : NULL;
	c8ui_list_set_count(b.list, b.entries ? b.entries->len : 0);
	c8ui_list_redraw(b.list);
	select_uri(b.select ? b.select : keep);
	g_clear_pointer(&b.select, g_free);
	g_free(keep);
	if (b.by_date) {
		return; // the note is the date view's
	}
	if (error) {
		char *msg = g_strdup_printf("This folder can't be opened. %s", error);
		c8ui_label_set_text(b.note, msg);
		g_free(msg);
	} else {
		c8ui_label_set_text(b.note, "There are no pictures here.");
	}
	c8ui_widget_set_hidden(b.note, b.entries && b.entries->len > 0);
	c8ui_widget_queue_layout(b.box);
}

static void load(void) {
	if (b.cancel) {
		g_cancellable_cancel(b.cancel);
		g_object_unref(b.cancel);
	}
	b.cancel = g_cancellable_new();
	ph_list_folder(b.folder, true, b.cancel, listed, NULL);
}

static gboolean reload_due(gpointer data) {
	b.reload_timer = 0;
	load();
	return G_SOURCE_REMOVE;
}

static void folder_changed(GFileMonitor *m, GFile *file, GFile *other,
		GFileMonitorEvent event, gpointer data) {
	if (event == G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT ||
			event == G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED) {
		return;
	}
	if (b.reload_timer) {
		g_source_remove(b.reload_timer);
	}
	b.reload_timer = g_timeout_add(RELOAD_MS, reload_due, NULL);
}

/* By date */

static void dated_select(const char *uri) {
	for (guint i = 0; uri && b.dated && i < b.dated->len; i++) {
		struct ph_entry *e = b.dated->pdata[i];
		if (strcmp(e->uri, uri) != 0) {
			continue;
		}
		for (guint m = 0; m < b.runs->len; m++) {
			struct run *r = &g_array_index(b.runs, struct run, m);
			if ((int)i >= r->first && (int)i < r->first + r->count) {
				/* One picked in the whole view, as in one grid */
				for (guint n = 0; n < b.runs->len; n++) {
					struct run *o = &g_array_index(b.runs, struct run, n);
					if (o != r) {
						c8ui_list_select(o->list, -1);
					}
				}
				c8ui_list_select(r->list, i - r->first);
				c8ui_list_reveal(r->list, i - r->first);
				b.dlist = r->list;
			}
		}
		return;
	}
}

static double cell_size(void);

static void month_select(struct c8ui_widget *list, void *data) {
	if (c8ui_list_get_selected(list) < 0) {
		return;
	}
	b.dlist = list;
	for (guint n = 0; b.runs && n < b.runs->len; n++) {
		struct run *o = &g_array_index(b.runs, struct run, n);
		if (o->list != list) {
			c8ui_list_select(o->list, -1);
		}
	}
}

static void dated_listed(GPtrArray *entries, const char *error, void *data) {
	g_clear_object(&b.dcancel);
	if (b.months == NULL) {
		return;
	}
	char *keep = NULL;
	if (b.dlist) {
		int sel = c8ui_list_get_selected(b.dlist);
		for (guint m = 0; sel >= 0 && b.runs && m < b.runs->len; m++) {
			struct run *r = &g_array_index(b.runs, struct run, m);
			if (r->list == b.dlist) {
				keep = g_strdup(((struct ph_entry *)
					b.dated->pdata[r->first + sel])->uri);
			}
		}
	}
	c8ui_widget_clear(b.months);
	b.dlist = NULL;
	g_clear_pointer(&b.runs, g_array_unref);
	g_clear_pointer(&b.dated_at, g_hash_table_unref);
	g_clear_pointer(&b.dated, g_ptr_array_unref);
	b.dated = entries ? g_ptr_array_ref(entries) : g_ptr_array_new();
	b.runs = g_array_new(FALSE, TRUE, sizeof(struct run));
	b.dated_at = g_hash_table_new(NULL, NULL);
	int last_month = -1;
	for (guint i = 0; i < b.dated->len; i++) {
		struct ph_entry *e = b.dated->pdata[i];
		g_hash_table_insert(b.dated_at, e, GINT_TO_POINTER(i + 1));
		GDateTime *t = g_date_time_new_from_unix_local(e->taken);
		int month = t ? g_date_time_get_year(t) * 12 +
			g_date_time_get_month(t) : 0;
		if (t) {
			g_date_time_unref(t);
		}
		if (month != last_month) {
			struct run r = { .first = i };
			g_array_append_val(b.runs, r);
			last_month = month;
		}
		g_array_index(b.runs, struct run, b.runs->len - 1).count++;
	}
	double cell = cell_size();
	for (guint m = 0; m < b.runs->len; m++) {
		struct run *r = &g_array_index(b.runs, struct run, m);
		struct ph_entry *e = b.dated->pdata[r->first];
		GDateTime *t = g_date_time_new_from_unix_local(e->taken);
		char *name = t ? g_date_time_format(t, "%B %Y") : g_strdup("");
		if (t) {
			g_date_time_unref(t);
		}
		struct c8ui_widget *label = c8ui_label_new(name, C8UI_TEXT_SUBHEADER);
		g_free(name);
		c8ui_widget_add(b.months, label);
		void *data = GINT_TO_POINTER(m + 1);
		r->list = c8ui_list_new(C8UI_LIST_GRID, cell, cell, draw_item, data);
		c8ui_list_set_keep(r->list, true);
		c8ui_list_set_click_opens(r->list, true);
		c8ui_list_on_open(r->list, opened_dated, data);
		c8ui_list_on_context(r->list, context, NULL);
		c8ui_list_on_select(r->list, month_select, NULL);
		r->list->fill = true;
		c8ui_list_set_count(r->list, r->count);
		c8ui_widget_add(b.months, r->list);
	}
	if (b.by_date) {
		c8ui_ring_set_active(b.ring, false);
		b.ring->hidden = true;
		if (error) {
			char *msg = g_strdup_printf("Pictures can't be read. %s", error);
			c8ui_label_set_text(b.note, msg);
			g_free(msg);
		} else {
			c8ui_label_set_text(b.note, "There are no pictures in "
				"Pictures yet.");
		}
		c8ui_widget_set_hidden(b.note, b.dated->len > 0);
	}
	dated_select(keep);
	g_free(keep);
	c8ui_widget_queue_layout(b.box);
	if (b.hooks.changed) {
		b.hooks.changed();
	}
}

static void load_dated(void) {
	if (b.dcancel) {
		g_cancellable_cancel(b.dcancel);
		g_object_unref(b.dcancel);
	}
	b.dcancel = g_cancellable_new();
	if (b.by_date && b.dated == NULL) {
		b.ring->hidden = false;
		c8ui_ring_set_active(b.ring, true);
	}
	ph_list_dates(b.top, b.dcancel, dated_listed, NULL);
}

/* Which of the two shows */
static void show_mode(void) {
	if (b.box == NULL) {
		return;
	}
	c8ui_widget_set_hidden(b.scroll, b.by_date);
	c8ui_widget_set_hidden(b.dscroll, !b.by_date);
	bool loading = b.by_date ? b.dcancel != NULL : b.cancel != NULL;
	b.ring->hidden = !loading;
	c8ui_ring_set_active(b.ring, loading);
	GPtrArray *shown = b.by_date ? b.dated : b.entries;
	c8ui_widget_set_hidden(b.note, loading || (shown && shown->len > 0));
	if (b.by_date) {
		c8ui_label_set_text(b.note, "There are no pictures in Pictures yet.");
	} else {
		c8ui_label_set_text(b.note, "There are no pictures here.");
	}
	update_header();
}

void browse_set_by_date(bool on) {
	if (on == b.by_date) {
		return;
	}
	b.by_date = on;
	if (on) {
		load_dated(); // again: pictures may have come since
	}
	show_mode();
	if (b.hooks.changed) {
		b.hooks.changed();
	}
}

bool browse_by_date(void) {
	return b.by_date;
}

static void update_header(void) {
	if (b.by_date) {
		c8ui_widget_set_hidden(b.back, true);
		c8ui_label_set_text(b.title, "By date");
		return;
	}
	bool top = b.top && b.folder && strcmp(b.top, b.folder) == 0;
	char *parent = b.folder ? ph_parent_uri(b.folder) : NULL;
	c8ui_widget_set_hidden(b.back, top || parent == NULL);
	g_free(parent);
	GFile *f = g_file_new_for_uri(b.folder);
	char *name = NULL;
	GFileInfo *info = g_file_query_info(f,
		G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, G_FILE_QUERY_INFO_NONE,
		NULL, NULL);
	if (info) {
		name = g_strdup(g_file_info_get_display_name(info));
		g_object_unref(info);
	}
	g_object_unref(f);
	c8ui_label_set_text(b.title, name ? c8_stealth_show(name) : "Pictures");
	g_free(name);
}

/* Stealth mode turned on or off: names drawn again */
void browse_stealth_changed(void) {
	if (b.box) {
		update_header();
		c8ui_list_redraw(b.list);
	}
}

void browse_set_folder(const char *uri, const char *select) {
	if (uri == NULL) {
		return;
	}
	bool same = b.folder && strcmp(b.folder, uri) == 0;
	g_free(b.select);
	b.select = g_strdup(select);
	if (same && b.entries) {
		/* Already shown: only pick the picture */
		select_uri(b.select);
		g_clear_pointer(&b.select, g_free);
		return;
	}
	g_free(b.folder);
	b.folder = g_strdup(uri);
	if (b.entries) {
		g_ptr_array_unref(b.entries);
		b.entries = NULL;
	}
	if (b.list) {
		c8ui_list_set_count(b.list, 0);
		c8ui_list_select(b.list, -1);
		c8ui_scroll_to(b.scroll, 0);
		if (!b.by_date) {
			c8ui_widget_set_hidden(b.note, true);
			b.ring->hidden = false;
			c8ui_ring_set_active(b.ring, true);
		}
		update_header();
	}
	if (b.monitor) {
		g_signal_handlers_disconnect_by_func(b.monitor, folder_changed, NULL);
		g_file_monitor_cancel(b.monitor);
		g_clear_object(&b.monitor);
	}
	GFile *f = g_file_new_for_uri(uri);
	b.monitor = g_file_monitor_directory(f, G_FILE_MONITOR_WATCH_MOVES, NULL,
		NULL);
	g_object_unref(f);
	if (b.monitor) {
		g_signal_connect(b.monitor, "changed", G_CALLBACK(folder_changed),
			NULL);
	}
	load();
	if (b.hooks.changed) {
		b.hooks.changed();
	}
}

void browse_select(const char *uri) {
	if (b.by_date) {
		dated_select(uri);
		return;
	}
	char *parent = ph_parent_uri(uri);
	if (parent) {
		browse_set_folder(parent, uri);
	}
	g_free(parent);
}

const char *browse_folder(void) {
	return b.folder;
}

GPtrArray *browse_entries(void) {
	return b.by_date ? b.dated : b.entries;
}

GPtrArray *browse_pictures(void) {
	return ph_pictures_of(browse_entries());
}

struct ph_entry *browse_selected(void) {
	struct c8ui_widget *list = browse_list();
	int sel = list ? c8ui_list_get_selected(list) : -1;
	if (sel < 0) {
		return NULL;
	}
	if (!b.by_date) {
		return entry_of(NULL, sel);
	}
	for (guint m = 0; b.runs && m < b.runs->len; m++) {
		struct run *r = &g_array_index(b.runs, struct run, m);
		if (r->list == list) {
			return entry_of(GINT_TO_POINTER(m + 1), sel);
		}
	}
	return NULL;
}

bool browse_up(void) {
	if (b.by_date) {
		return false;
	}
	if (b.folder == NULL || (b.top && strcmp(b.top, b.folder) == 0)) {
		return false;
	}
	char *parent = ph_parent_uri(b.folder);
	if (parent == NULL) {
		return false;
	}
	char *from = g_strdup(b.folder);
	browse_set_folder(parent, from);
	g_free(from);
	g_free(parent);
	return true;
}

void browse_refresh(void) {
	if (b.by_date) {
		load_dated();
		show_mode();
	} else {
		load();
	}
}

static void back_pressed(struct c8ui_widget *w, void *data) {
	browse_up();
}

/* The cells' size; the box's paddings are its own */
static double cell_size(void) {
	if (!b.narrow || b.box == NULL || b.box->root == NULL) {
		return CELL;
	}
	/* Two across, filling the width */
	double width = c8ui_root_width(b.box->root) - 40;
	return fmax(100, floor((width - GRID_GAP) / 2));
}

void browse_set_narrow(bool narrow) {
	b.narrow = narrow;
	if (b.box == NULL) {
		return;
	}
	double cell = cell_size();
	c8ui_list_set_mode(b.list, C8UI_LIST_GRID, cell, cell);
	for (guint m = 0; b.runs && m < b.runs->len; m++) {
		struct run *r = &g_array_index(b.runs, struct run, m);
		c8ui_list_set_mode(r->list, C8UI_LIST_GRID, cell, cell);
	}
}

static void box_gone(struct c8ui_widget *w, void *data) {
	if (b.box != w) {
		return;
	}
	if (b.cancel) {
		g_cancellable_cancel(b.cancel);
		g_clear_object(&b.cancel);
	}
	if (b.reload_timer) {
		g_source_remove(b.reload_timer);
		b.reload_timer = 0;
	}
	if (b.dcancel) {
		g_cancellable_cancel(b.dcancel);
		g_clear_object(&b.dcancel);
	}
	/* The months' lists went with the box; the pictures go too, to be
	 * read again for a new window */
	g_clear_pointer(&b.runs, g_array_unref);
	g_clear_pointer(&b.dated_at, g_hash_table_unref);
	g_clear_pointer(&b.dated, g_ptr_array_unref);
	b.box = b.header = b.back = b.title = b.scroll = b.list = b.note = NULL;
	b.ring = b.dscroll = b.months = b.dlist = NULL;
}

struct c8ui_widget *browse_new(const struct browse_hooks *hooks) {
	b.hooks = *hooks;
	if (b.top == NULL) {
		b.top = ph_pictures_uri();
	}
	b.box = c8ui_box_new(C8UI_VERTICAL, 24);
	b.box->fill = true;
	b.box->expand = true;
	b.box->on_destroy = box_gone;
	c8ui_box_set_padding(b.box, 120, 44, 40, 0);
	c8ui_box_set_narrow_padding(b.box, 20, 24, 20, 0);
	b.header = c8ui_box_new(C8UI_HORIZONTAL, 20);
	b.back = c8ui_back_button_new(back_pressed, NULL);
	b.back->align = C8UI_ALIGN_CENTER;
	c8ui_widget_add(b.header, b.back);
	b.title = c8ui_label_new("Pictures", C8UI_TEXT_HEADER);
	c8ui_label_set_max_lines(b.title, 1);
	b.title->expand = true;
	c8ui_widget_add(b.header, b.title);
	b.ring = c8ui_ring_new(0);
	b.ring->align = C8UI_ALIGN_CENTER;
	c8ui_widget_add(b.header, b.ring);
	c8ui_widget_add(b.box, b.header);
	b.note = c8ui_label_new("", C8UI_TEXT_BODY);
	c8ui_label_set_color(b.note, C8UI_COLOR_SECONDARY);
	b.note->hidden = true;
	c8ui_widget_add(b.box, b.note);
	b.list = c8ui_list_new(C8UI_LIST_GRID, CELL, CELL, draw_item, NULL);
	c8ui_list_set_keep(b.list, true);
	c8ui_list_set_click_opens(b.list, true);
	c8ui_list_on_open(b.list, opened, NULL);
	c8ui_list_on_context(b.list, context, NULL);
	b.list->fill = true;
	/* Room under the last row, as Metro pages end */
	struct c8ui_widget *inner = c8ui_box_new(C8UI_VERTICAL, 0);
	c8ui_box_set_padding(inner, 0, 0, 0, 40);
	inner->fill = true;
	c8ui_widget_add(inner, b.list);
	b.scroll = c8ui_scroll_new(inner);
	b.scroll->fill = true;
	b.scroll->expand = true;
	c8ui_widget_add(b.box, b.scroll);
	/* By date: the months, one under another */
	b.months = c8ui_box_new(C8UI_VERTICAL, 16);
	c8ui_box_set_padding(b.months, 0, 0, 0, 40);
	b.months->fill = true;
	b.dscroll = c8ui_scroll_new(b.months);
	b.dscroll->fill = true;
	b.dscroll->expand = true;
	b.dscroll->hidden = true;
	c8ui_widget_add(b.box, b.dscroll);
	browse_set_narrow(b.narrow);
	if (b.by_date) {
		load_dated();
		show_mode();
	}
	if (b.folder) {
		/* A new window (the old one closed): show the folder again */
		char *uri = b.folder;
		b.folder = NULL;
		browse_set_folder(uri, NULL);
		g_free(uri);
	}
	return b.box;
}

struct c8ui_widget *browse_list(void) {
	if (b.by_date) {
		if (b.dlist == NULL && b.runs && b.runs->len > 0) {
			return g_array_index(b.runs, struct run, 0).list;
		}
		return b.dlist;
	}
	return b.list;
}
