/*
 * The pages that go over the hub: a feed across the window, a thread, a
 * profile, the notifications, search, the saved feeds, the accounts (and
 * the sign-in form, which is also what shows with no account), and the
 * picture viewer.
 */
#include <math.h>
#include <string.h>
#include "app.h"

/* The back button and a title; the rest is the caller's */

static void go_back(struct c8ui_widget *w, void *data) {
	ui_back();
}

/* Metro's page header: the back button 40 in, the title in large light
 * type where the content starts (PAGE_LEFT), the hub's title's size */
#define BACK_X 40
#define BACK_W 40 // c8ui's round button

/* Narrow, the title is smaller and nearer the button */
static struct c8ui_widget *header_new(const char *title) {
	struct c8ui_widget *top = c8ui_box_new(C8UI_HORIZONTAL,
		PAGE_LEFT - BACK_X - BACK_W);
	c8ui_box_set_narrow_spacing(top, 16);
	top->fill = true;
	ui_pad(top, BACK_X, 30, PAGE_PAD, 14);
	struct c8ui_widget *back = c8ui_back_button_new(go_back, NULL);
	back->align = C8UI_ALIGN_CENTER;
	c8ui_widget_add(top, back);
	struct c8ui_widget *label = c8ui_label_new(title, C8UI_TEXT_HEADER);
	c8ui_label_set_narrow_style(label, C8UI_TEXT_SUBHEADER);
	c8ui_label_set_max_lines(label, 1);
	label->expand = true;
	label->align = C8UI_ALIGN_CENTER;
	c8ui_widget_add(top, label);
	return top;
}

struct c8ui_widget *page_new(const char *title, struct c8ui_widget **body) {
	struct c8ui_widget *page = c8ui_box_new(C8UI_VERTICAL, 0);
	page->fill = true;
	page->expand = true;
	c8ui_widget_set_background(page, BACKGROUND);
	c8ui_widget_add(page, header_new(title));
	*body = c8ui_box_new(C8UI_VERTICAL, 12);
	(*body)->fill = true;
	(*body)->expand = true;
	c8ui_widget_add(page, *body);
	return page;
}

void page_set_title(struct c8ui_widget *page, const char *title) {
	struct c8ui_widget *top = page->children[0];
	c8ui_label_set_text(top->children[1], title);
}

/* Something that sits in one place wide and another narrow (a feed's
 * words: their own column wide, over its posts narrow), moved as the
 * window crosses the line rather than built twice */

struct mover {
	struct c8ui_widget *wide, *narrow; // where it goes, at index 0
};

static void mover_measure(struct c8ui_widget *w, double avail, double *width,
		double *height) {
	c8ui_widget_measure(w->children[0], avail, width, height);
}

static void mover_layout(struct c8ui_widget *w) {
	c8ui_widget_layout(w->children[0], w->rect);
}

static void mover_narrow(struct c8ui_widget *w, bool narrow) {
	struct mover *m = w->data;
	struct c8ui_widget *to = narrow ? m->narrow : m->wide;
	if (to && w->parent != to) {
		c8ui_widget_insert(to, 0, w);
	}
}

static void mover_destroy(struct c8ui_widget *w) {
	g_free(w->data);
}

static const struct c8ui_widget_impl mover_impl = {
	.name = "sky-mover",
	.measure = mover_measure,
	.layout = mover_layout,
	.destroy = mover_destroy,
	.narrow = mover_narrow,
};

/* child goes first in wide while wide, first in narrow while narrow;
 * put it in wide to start with */
static struct c8ui_widget *mover_new(struct c8ui_widget *child,
		struct c8ui_widget *wide, struct c8ui_widget *narrow) {
	struct mover *m = g_new0(struct mover, 1);
	struct c8ui_widget *w = c8ui_widget_new(&mover_impl, m);
	w->fill = true;
	c8ui_widget_track(wide, &m->wide);
	c8ui_widget_track(narrow, &m->narrow);
	c8ui_widget_add(w, child);
	return w;
}

struct c8ui_widget *view_scroller(struct feedview *fv, enum view_shape shape) {
	struct c8ui_widget *inner = c8ui_box_new(C8UI_VERTICAL, 0);
	inner->fill = true;
	ui_pad(inner, PAGE_LEFT, 8, PAD_COLUMN, 40);
	c8ui_widget_add(inner, view_widget(fv, shape));
	struct c8ui_widget *scroll = c8ui_scroll_new(inner);
	scroll->fill = true;
	scroll->expand = true;
	return scroll;
}

/* Trails: Miller columns (c8ui_miller) across a page. A feed's page is
 * what the feed is, then its posts; a post opened from them is the next
 * column, an answer opened in that the next, and so on, so the way back
 * stays in sight and opening from a column takes away the ones after it.
 * A post opened from anywhere else starts a trail of its own. Narrow,
 * c8ui shows one column at a time (the feed's words move over its
 * posts), and Back steps out of the last before it leaves the page. */

#define TRAIL_INFO_W 340 // what the feed is
#define TRAIL_GAP 30 // and NARROW_PAD each side: a column's own

static void thread_loaded(struct feedview *fv, void *data);

struct trail {
	struct c8ui_widget *page, *miller;
};

/* A column: its name over it, and the posts in it if it is a list */
struct trail_col {
	struct feedview *fv;
	char *name;
};

static GPtrArray *trails;

static void trail_gone(struct c8ui_widget *w, void *data) {
	struct trail *t = data;
	g_ptr_array_remove(trails, t);
	g_free(t);
}

static void trail_col_gone(struct c8ui_widget *w, void *data) {
	struct trail_col *c = data;
	if (c->fv) {
		view_unref(c->fv);
	}
	g_free(c->name);
	g_free(c);
}

static struct trail_col *col_of(struct trail *t, int i) {
	struct c8ui_widget *w = c8ui_miller_get(t->miller, i);
	return w ? w->user_data : NULL;
}

static void trail_changed(struct c8ui_widget *miller, int count, void *data) {
	struct trail *t = data;
	/* The post that opened a column just closed is no longer the way on */
	struct trail_col *c = count > 0 ? col_of(t, count - 1) : NULL;
	if (c && c->fv && c->fv->list) {
		c8ui_list_select(c->fv->list, -1);
	}
}

static struct trail *trail_new(const char *title) {
	struct trail *t = g_new0(struct trail, 1);
	struct c8ui_widget *body;
	t->page = page_new(title, &body);
	t->miller = c8ui_miller_new();
	t->miller->expand = true;
	/* The first column's words start under the title */
	c8ui_miller_set_spacing(t->miller, PAGE_LEFT - NARROW_PAD, TRAIL_GAP, 0,
		16);
	c8ui_miller_on_change(t->miller, trail_changed, t);
	t->miller->user_data = t;
	t->miller->on_destroy = trail_gone;
	c8ui_widget_add(body, t->miller);
	if (trails == NULL) {
		trails = g_ptr_array_new();
	}
	g_ptr_array_add(trails, t);
	return t;
}

/* Opens a column after column after: fv's list, or content */
static void trail_add(struct trail *t, int after, const char *name,
		struct feedview *fv, struct c8ui_widget *content, double width) {
	struct trail_col *c = g_new0(struct trail_col, 1);
	c->fv = fv ? view_ref(fv) : NULL;
	c->name = g_strdup(name);
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 0);
	box->fill = true;
	box->user_data = c;
	box->on_destroy = trail_col_gone;
	c8ui_box_set_padding(box, NARROW_PAD, 0, NARROW_PAD, 0);
	struct c8ui_widget *sec = hub_section_new(name, NULL,
		content ? content : view_widget(fv, SHAPE_TRAIL), true);
	sec->fill = true;
	sec->expand = true;
	hub_section_keep_head(sec); // narrow too: it says what the column is
	c8ui_widget_add(box, sec);
	c8ui_miller_open(t->miller, after, box, width + 2 * NARROW_PAD);
}

static void thread_add(struct trail *t, int after, const char *uri) {
	struct feedview *fv = view_new(VIEW_THREAD, uri, NULL, "Post");
	fv->on_loaded = thread_loaded;
	trail_add(t, after, "Post", fv, NULL, TRAIL_W);
	view_reload(fv);
	view_unref(fv);
}

static struct trail *trail_find(struct feedview *fv, int *index) {
	for (guint i = 0; fv && fv->box && trails && i < trails->len; i++) {
		struct trail *t = trails->pdata[i];
		*index = c8ui_miller_index_of(t->miller, fv->box);
		if (*index >= 0) {
			return t;
		}
	}
	return NULL;
}

bool trail_has(struct feedview *fv) {
	int i;
	return trail_find(fv, &i) != NULL;
}

bool trail_open_thread(struct feedview *from, const char *uri) {
	int i;
	struct trail *t = trail_find(from, &i);
	if (t == NULL) {
		return false;
	}
	thread_add(t, i, uri);
	return true;
}

static struct trail *trail_on_top(void) {
	struct c8ui_widget *top = g.stack && g.stack->child_count > 1 ?
		g.stack->children[g.stack->child_count - 1] : NULL;
	for (guint i = 0; top && trails && i < trails->len; i++) {
		struct trail *t = trails->pdata[i];
		if (t->page == top) {
			return t;
		}
	}
	return NULL;
}

bool trail_back(void) {
	struct trail *t = trail_on_top();
	return t && c8ui_miller_back(t->miller);
}

/* What a feed or a list is: its picture, name, whose, its words, and
 * how liked or how many are in it (getFeedGenerator, getList) */

struct info {
	int refs;
	bool gone;
	struct trail *trail; // alive while it isn't gone: it is in its page
	bool list;
	char *avatar, *creator;
	struct c8ui_widget *face, *name, *by, *words, *counts;
};

static void info_unref(struct info *in) {
	if (--in->refs > 0) {
		return;
	}
	g_free(in->avatar);
	g_free(in->creator);
	g_free(in);
}

static void info_gone(struct c8ui_widget *w, void *data) {
	struct info *in = data;
	in->gone = true;
	info_unref(in);
}

static void info_face_draw(struct c8ui_widget *w, cairo_t *cr, void *data) {
	struct info *in = data;
	struct c8ui_rect r = w->rect;
	cairo_surface_t *s = in->avatar && sky_settings.images ?
		sky_image(in->avatar) : NULL;
	if (s) {
		c8ui_draw_surface_cover(cr, s, r.x, r.y, r.width, r.height);
	} else {
		c8ui_set_source(cr, in->list ? BAR_TILE : BLUE, 1);
		cairo_rectangle(cr, r.x, r.y, r.width, r.height);
		cairo_fill(cr);
	}
}

static void info_by_clicked(struct c8ui_widget *w, void *data) {
	struct info *in = data;
	if (in->creator) {
		open_profile(in->creator);
	}
}

static void got_info(int status, struct json *json, const char *error,
		void *data) {
	struct info *in = data;
	struct json *v = status != 200 ? NULL :
		json_get(json, in->list ? "list" : "view");
	if (in->gone) {
		info_unref(in);
		return;
	}
	if (v == NULL) {
		c8ui_label_set_text(in->words, error ? error :
			in->list ? "This list can't be shown." :
			"This feed can't be shown.");
		c8ui_widget_set_hidden(in->words, false);
		info_unref(in);
		return;
	}
	const char *name = json_str(json_get(v, in->list ? "name" :
		"displayName"), "");
	c8ui_label_set_text(in->name, name);
	if (name[0]) {
		/* Opened by its address alone, the page said only "Feed" */
		page_set_title(in->trail->page, name);
	}
	g_free(in->avatar);
	in->avatar = g_strdup(json_str(json_get(v, "avatar"), NULL));
	c8ui_widget_queue_draw(in->face);
	struct sky_profile *by = sky_profile_parse(json_get(v, "creator"));
	if (by) {
		g_free(in->creator);
		in->creator = g_strdup(by->did);
		char *text = g_strdup_printf("%s by @%s", in->list ? "A list" :
			"A feed", sky_shown_handle(by->did, by->handle));
		c8ui_button_set_label(in->by, text);
		c8ui_widget_set_hidden(in->by, false);
		g_free(text);
		sky_profile_free(by);
	}
	const char *words = json_str(json_get(v, "description"), "");
	c8ui_label_set_text(in->words, words);
	c8ui_widget_set_hidden(in->words, words[0] == '\0');
	char n[16], *counts = NULL;
	if (in->list) {
		int items = (int)json_num(json_get(v, "listItemCount"), -1);
		if (items >= 0) {
			card_count(items, n, sizeof(n));
			counts = g_strdup_printf("%s %s", n, items == 1 ? "person" :
				"people");
		}
	} else {
		int likes = (int)json_num(json_get(v, "likeCount"), 0);
		card_count(likes, n, sizeof(n));
		counts = g_strdup_printf("Liked by %s", n);
		if (json_get(json, "isOnline") && !json_bool(json_get(json,
				"isOnline"))) {
			char *off = g_strconcat(counts, "  ·  Seems to be offline",
				NULL);
			g_free(counts);
			counts = off;
		}
	}
	c8ui_label_set_text(in->counts, counts ? counts : "");
	c8ui_widget_set_hidden(in->counts, counts == NULL);
	g_free(counts);
	info_unref(in);
}

static struct c8ui_widget *info_new(struct trail *t, enum view_kind kind,
		const char *uri) {
	struct info *in = g_new0(struct info, 1);
	in->refs = 1;
	in->trail = t;
	in->list = kind == VIEW_LIST;
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 8);
	box->fill = true;
	box->user_data = in;
	box->on_destroy = info_gone;
	in->face = c8ui_canvas_new(96, 96, info_face_draw, NULL, in);
	c8ui_widget_add(box, in->face);
	/* The page's title says the name */
	in->name = c8ui_label_new("", C8UI_TEXT_SUBHEADER);
	c8ui_widget_set_hidden(in->name, true);
	c8ui_widget_add(box, in->name);
	in->by = c8ui_link_new("", info_by_clicked, in);
	c8ui_widget_set_hidden(in->by, true);
	c8ui_widget_add(box, in->by);
	in->words = c8ui_label_new("", C8UI_TEXT_BODY);
	c8ui_widget_set_hidden(in->words, true);
	c8ui_widget_add(box, in->words);
	in->counts = c8ui_label_new("", C8UI_TEXT_BODY);
	c8ui_label_set_color(in->counts, C8UI_COLOR_SECONDARY);
	c8ui_widget_set_hidden(in->counts, true);
	c8ui_widget_add(box, in->counts);
	GString *call = g_string_new(in->list ? "app.bsky.graph.getList?limit=1" :
		"app.bsky.feed.getFeedGenerator");
	sky_query(call, in->list ? "list" : "feed", uri);
	in->refs++;
	sky_get(call->str, got_info, in);
	g_string_free(call, TRUE);
	return box;
}

/* A feed, a list, a search for a tag, someone's followers: what it is
 * (a feed or a list), its posts, and the trail from them. What it is
 * has a column of its own wide; narrow, where only the last column
 * shows, it moves over the posts. */

static struct trail *feed_trail(enum view_kind kind, const char *param,
		const char *filter, const char *title) {
	struct trail *t = trail_new(title);
	struct feedview *fv = view_new(kind, param, filter, title);
	bool about = kind == VIEW_FEED || kind == VIEW_LIST;
	const char *name = kind == VIEW_FOLLOWERS || kind == VIEW_FOLLOWS ?
		"People" : "Posts";
	if (about) {
		struct c8ui_widget *wide = c8ui_box_new(C8UI_VERTICAL, 0);
		wide->fill = true;
		struct c8ui_widget *posts = c8ui_box_new(C8UI_VERTICAL, 8);
		posts->fill = true;
		c8ui_widget_add(posts, view_widget(fv, SHAPE_TRAIL));
		c8ui_widget_add(wide, mover_new(info_new(t, kind, param), wide,
			posts));
		trail_add(t, -1, "About", NULL, wide, TRAIL_INFO_W);
		trail_add(t, 0, name, fv, posts, TRAIL_W);
		c8ui_miller_set_base(t->miller, 2);
	} else {
		trail_add(t, -1, name, fv, NULL, TRAIL_W);
		c8ui_miller_set_base(t->miller, 1);
	}
	view_reload(fv);
	view_unref(fv); // its widgets keep it
	return t;
}

void open_feed(enum view_kind kind, const char *param, const char *filter,
		const char *title) {
	ui_open_page(feed_trail(kind, param, filter, title)->page);
}

void open_notifications(void) {
	struct c8ui_widget *body;
	struct c8ui_widget *page = page_new("Notifications", &body);
	struct feedview *fv = view_new(VIEW_NOTICES, NULL, NULL, "Notifications");
	fv->mark_seen = true;
	c8ui_widget_add(body, view_scroller(fv, SHAPE_PAGE));
	ui_open_page(page);
	view_reload(fv);
	view_unref(fv);
}

/* A thread: what the post answers, the post, the answers to it, each
 * answer opened beside it */

static void thread_loaded(struct feedview *fv, void *data) {
	if (fv->list == NULL) {
		return;
	}
	for (guint i = 0; i < fv->items->len; i++) {
		struct sky_item *it = fv->items->pdata[i];
		if (it->focus) {
			c8ui_list_reveal(fv->list, (int)i);
			ui_set_target(it->post);
			c8ui_widget_grab_focus(fv->list);
			break;
		}
	}
	fv->on_loaded = NULL; // only the first time: a reload stays where it is
}

void open_thread(const char *uri) {
	struct trail *t = trail_new("Post");
	thread_add(t, -1, uri);
	c8ui_miller_set_base(t->miller, 1);
	ui_open_page(t->page);
}

/* A profile */

struct profile {
	int refs;
	bool gone; // its page was closed
	char *actor;
	struct sky_profile *p;
	/* A panorama: about them, then Posts, Replies and Media each a
	 * column (narrow, c8ui's pivot of them) */
	struct feedview *fv[3];
	struct c8ui_widget *page;
	struct c8ui_widget *banner, *name, *handle, *bio, *counts, *follow, *more;
	struct c8ui_widget *followers, *follows;
};

static GPtrArray *profiles; // the pages that are open

static void profile_unref(struct profile *pr) {
	if (--pr->refs > 0) {
		return;
	}
	sky_profile_free(pr->p);
	for (int i = 0; i < 3; i++) {
		if (pr->fv[i]) {
			view_unref(pr->fv[i]);
		}
	}
	g_free(pr->actor);
	g_free(pr);
}

static void profile_gone(struct c8ui_widget *w, void *data) {
	struct profile *pr = data;
	pr->gone = true;
	g_ptr_array_remove(profiles, pr);
	profile_unref(pr);
}

static void banner_draw(struct c8ui_widget *w, cairo_t *cr, void *data) {
	struct profile *pr = data;
	struct c8ui_rect r = w->rect;
	double band = r.height - 44;
	/* Ours, in stealth mode: neither picture */
	bool mine = pr->p && sky_mine(pr->p->did);
	cairo_surface_t *banner = pr->p && pr->p->banner && sky_settings.images &&
		!mine ? sky_image_large(pr->p->banner) : NULL;
	if (banner) {
		c8ui_draw_surface_cover(cr, banner, r.x, r.y, r.width, band);
	} else {
		c8ui_set_source(cr, BAR_TILE, 1);
		cairo_rectangle(cr, r.x, r.y, r.width, band);
		cairo_fill(cr);
	}
	/* The face, over the band's edge, in a white frame */
	double side = 88, ax = r.x + 16, ay = r.y + r.height - side;
	c8ui_set_source(cr, CARD, 1);
	cairo_rectangle(cr, ax - 3, ay - 3, side + 6, side + 6);
	cairo_fill(cr);
	cairo_surface_t *face = pr->p && pr->p->avatar && !mine ?
		sky_image(pr->p->avatar) : NULL;
	if (face) {
		c8ui_draw_surface_cover(cr, face, ax, ay, side, side);
	} else {
		c8ui_set_source(cr, 0x3a8fd9, 1);
		cairo_rectangle(cr, ax, ay, side, side);
		cairo_fill(cr);
	}
}

static void profile_show(struct profile *pr) {
	struct sky_profile *p = pr->p;
	bool me = sky_me && strcmp(p->did, sky_me->did) == 0;
	const char *name = sky_shown_name(p->did, p->name && p->name[0] ? p->name :
		p->handle);
	c8ui_label_set_text(pr->name, name);
	page_set_title(pr->page, name);
	char *handle = g_strdup_printf("@%s%s%s", sky_shown_handle(p->did,
		p->handle), p->follows_us ? "  ·  Follows you" : "",
		p->muted ? "  ·  Muted" : "");
	c8ui_label_set_text(pr->handle, handle);
	g_free(handle);
	/* Stealth mode: our own words about ourselves aren't shown */
	const char *bio = sky_mine(p->did) ? NULL : p->bio;
	c8ui_label_set_text(pr->bio, bio ? bio : "");
	c8ui_widget_set_hidden(pr->bio, bio == NULL);
	char *counts = g_strdup_printf("%d %s", p->posts, p->posts == 1 ? "post" :
		"posts");
	c8ui_label_set_text(pr->counts, counts);
	g_free(counts);
	char *followers = g_strdup_printf("%d %s", p->followers,
		p->followers == 1 ? "follower" : "followers");
	c8ui_button_set_label(pr->followers, followers);
	g_free(followers);
	char *follows = g_strdup_printf("%d following", p->follows);
	c8ui_button_set_label(pr->follows, follows);
	g_free(follows);
	c8ui_button_set_label(pr->follow, p->block_uri ? "Blocked" :
		p->follow_uri ? "Following" : "Follow");
	c8ui_button_set_primary(pr->follow, p->follow_uri == NULL &&
		p->block_uri == NULL);
	c8ui_widget_set_hidden(pr->follow, me);
	c8ui_widget_set_disabled(pr->follow, p->block_uri != NULL);
	c8ui_widget_set_hidden(pr->more, false);
	c8ui_widget_queue_draw(pr->banner);
}

static void profile_reload(struct profile *pr);

static void got_profile(int status, struct json *json, const char *error,
		void *data) {
	struct profile *pr = data;
	if (!pr->gone && status == 200) {
		struct sky_profile *p = sky_profile_parse(json);
		if (p) {
			bool first = pr->p == NULL;
			sky_profile_free(pr->p);
			pr->p = p;
			profile_show(pr);
			if (first) {
				/* Its posts, by its DID now that it is known */
				for (int i = 0; i < 3; i++) {
					if (pr->fv[i]) {
						g_free(pr->fv[i]->param);
						pr->fv[i]->param = g_strdup(p->did);
						view_reload(pr->fv[i]);
					}
				}
			}
		}
	} else if (!pr->gone && (status != 0 || error)) {
		c8ui_label_set_text(pr->name, "This profile can't be shown");
		c8ui_widget_set_hidden(pr->name, false);
		c8ui_label_set_text(pr->handle, error ? error : "");
	}
	profile_unref(pr);
}

static void profile_reload(struct profile *pr) {
	GString *call = g_string_new("app.bsky.actor.getProfile");
	sky_query(call, "actor", pr->p ? pr->p->did : pr->actor);
	pr->refs++;
	sky_get(call->str, got_profile, pr);
	g_string_free(call, TRUE);
}

void profile_set_follow(const char *did, const char *uri) {
	for (guint i = 0; profiles && i < profiles->len; i++) {
		struct profile *pr = profiles->pdata[i];
		if (pr->p && strcmp(pr->p->did, did) == 0) {
			g_free(pr->p->follow_uri);
			pr->p->follow_uri = g_strdup(uri);
			pr->p->followers += uri ? 1 : pr->p->followers > 0 ? -1 : 0;
			profile_show(pr);
		}
	}
}

static void follow_clicked(struct c8ui_widget *w, void *data) {
	struct profile *pr = data;
	if (pr->p) {
		ui_follow(pr->p->did, pr->p->follow_uri);
	}
}

static void followers_clicked(struct c8ui_widget *w, void *data) {
	struct profile *pr = data;
	if (pr->p) {
		char *title = g_strdup_printf("Followers of %s",
			sky_shown_name(pr->p->did, pr->p->name));
		open_feed(VIEW_FOLLOWERS, pr->p->did, NULL, title);
		g_free(title);
	}
}

static void follows_clicked(struct c8ui_widget *w, void *data) {
	struct profile *pr = data;
	if (pr->p) {
		char *title = g_strdup_printf("%s follows",
			sky_shown_name(pr->p->did, pr->p->name));
		open_feed(VIEW_FOLLOWS, pr->p->did, NULL, title);
		g_free(title);
	}
}

/* Mute and block: said, then the profile read again */
static void relation_changed(int status, struct json *json, const char *error,
		void *data) {
	struct profile *pr = data;
	if (status != 200) {
		ui_error(error);
	} else if (!pr->gone) {
		profile_reload(pr);
	}
	profile_unref(pr);
}

static void block_answer(int choice, void *data) {
	struct profile *pr = data;
	if (choice == 0 && !pr->gone && pr->p && sky_me) {
		GString *fields = g_string_new(",\"subject\":");
		sky_json_str(fields, pr->p->did);
		pr->refs++;
		ui_create_record("app.bsky.graph.block", fields->str, relation_changed,
			pr);
		g_string_free(fields, TRUE);
	}
	profile_unref(pr);
}

static void more_chosen(int choice, void *data) {
	struct profile *pr = data;
	struct sky_profile *p = pr->p;
	bool me = p && sky_me && strcmp(p->did, sky_me->did) == 0;
	if (pr->gone || p == NULL || choice < 0 || g.win == NULL) {
		profile_unref(pr);
		return;
	}
	char *web = g_strdup_printf("https://bsky.app/profile/%s",
		sky_mine(p->did) ? p->did : p->handle); // stealth: not the handle
	if (choice == 0) {
		c8ui_root_set_clipboard(g.root, web);
	} else if (choice == 1) {
		c8ui_window_open_uri(g.win, web);
	} else if (choice == 2 && !me) {
		GString *body = g_string_new("{\"actor\":");
		sky_json_str(body, p->did);
		g_string_append_c(body, '}');
		pr->refs++;
		sky_post(p->muted ? "app.bsky.graph.unmuteActor" :
			"app.bsky.graph.muteActor", body->str, relation_changed, pr);
		g_string_free(body, TRUE);
	} else if (choice == 3 && !me && p->block_uri) {
		pr->refs++;
		ui_delete_record("app.bsky.graph.block", p->block_uri,
			relation_changed, pr);
	} else if (choice == 3 && !me) {
		static const char *const buttons[] = { "Block", "Cancel" };
		char *title = g_strdup_printf("Block %s?", p->name);
		pr->refs++;
		c8ui_dialog_show(g.root, title, "You won't see each other's posts, "
			"and they can't reply to you or follow you. Blocks are public.",
			buttons, 2, block_answer, pr);
		g_free(title);
	}
	g_free(web);
	profile_unref(pr);
}

static void more_clicked(struct c8ui_widget *w, void *data) {
	struct profile *pr = data;
	if (pr->p == NULL) {
		return;
	}
	bool me = sky_me && strcmp(pr->p->did, sky_me->did) == 0;
	const char *items[] = { "Copy link", "Open in browser",
		pr->p->muted ? "Unmute" : "Mute",
		pr->p->block_uri ? "Unblock" : "Block" };
	pr->refs++;
	c8ui_menu_show(g.root, w->rect.x, w->rect.y + w->rect.height, false, items,
		me ? 2 : 4, more_chosen, pr);
}

#define ABOUT_W 380 // the panorama's first section

/* Who they are: the banner and face, name, handle, words, buttons */
static struct c8ui_widget *about_new(struct profile *pr) {
	struct c8ui_widget *inner = c8ui_box_new(C8UI_VERTICAL, 8);
	inner->fill = true;
	pr->banner = c8ui_canvas_new(0, 190, banner_draw, NULL, pr);
	pr->banner->fill = true;
	c8ui_widget_add(inner, pr->banner);
	/* The page's title says the name; this only says it can't be shown */
	pr->name = c8ui_label_new("", C8UI_TEXT_SUBHEADER);
	c8ui_widget_set_hidden(pr->name, true);
	c8ui_widget_add(inner, pr->name);
	pr->handle = c8ui_label_new("", C8UI_TEXT_BODY);
	c8ui_label_set_color(pr->handle, C8UI_COLOR_SECONDARY);
	c8ui_widget_add(inner, pr->handle);
	pr->bio = c8ui_label_new("", C8UI_TEXT_BODY);
	pr->bio->max_width = 640;
	c8ui_widget_set_hidden(pr->bio, true);
	c8ui_widget_add(inner, pr->bio);
	struct c8ui_widget *row = c8ui_box_new(C8UI_HORIZONTAL, 10);
	c8ui_box_set_wrap(row, true);
	pr->follow = c8ui_button_new("Follow", follow_clicked, pr);
	c8ui_widget_set_hidden(pr->follow, true);
	c8ui_widget_add(row, pr->follow);
	pr->followers = c8ui_button_new("Followers", followers_clicked, pr);
	c8ui_widget_add(row, pr->followers);
	pr->follows = c8ui_button_new("Following", follows_clicked, pr);
	c8ui_widget_add(row, pr->follows);
	pr->more = c8ui_button_new("More…", more_clicked, pr);
	c8ui_widget_set_hidden(pr->more, true);
	c8ui_widget_add(row, pr->more);
	pr->counts = c8ui_label_new("", C8UI_TEXT_BODY);
	c8ui_label_set_color(pr->counts, C8UI_COLOR_SECONDARY);
	pr->counts->align = C8UI_ALIGN_CENTER;
	c8ui_widget_add(row, pr->counts);
	c8ui_widget_add(inner, row);
	return inner;
}

static struct c8ui_widget *profile_page(const char *actor) {
	static const char *const names[] = { "Posts", "Replies", "Media" };
	static const char *const filters[] = { NULL, "posts_with_replies",
		"posts_with_media" };
	struct profile *pr = g_new0(struct profile, 1);
	pr->refs = 1;
	pr->actor = g_strdup(actor);
	for (int i = 0; i < 3; i++) {
		pr->fv[i] = view_new(VIEW_AUTHOR, actor, filters[i], names[i]);
	}
	pr->fv[2]->tiles = true; // Media: the pictures, as tiles
	if (profiles == NULL) {
		profiles = g_ptr_array_new();
	}
	g_ptr_array_add(profiles, pr);

	struct c8ui_widget *body;
	pr->page = page_new("Profile", &body);
	body->user_data = pr;
	body->on_destroy = profile_gone;
	/* Metro's people hub: who they are, then what they posted, each
	 * kind its own column, panning sideways under the name; narrow, a
	 * pivot of the same four */
	struct c8ui_widget *hub = c8ui_hub_new(NULL);
	c8ui_hub_set_spacing(hub, PAGE_LEFT, 80, 6, 24);
	hub->fill = true;
	hub->expand = true;
	c8ui_hub_add(hub, NULL, hub_section_new("About", NULL, about_new(pr),
		true), ABOUT_W);
	c8ui_hub_set_name(hub, 0, "About");
	for (int i = 0; i < 3; i++) {
		c8ui_hub_add(hub, NULL, hub_section_new(names[i], NULL,
			view_widget(pr->fv[i], SHAPE_COLUMN), true), COLUMN_W);
		c8ui_hub_set_name(hub, i + 1, names[i]);
	}
	c8ui_widget_add(body, hub);
	profile_reload(pr);
	return pr->page;
}

void open_profile(const char *actor) {
	if (actor && actor[0]) {
		ui_open_page(profile_page(actor));
	}
}

/* Search: posts or people */

static struct {
	struct c8ui_widget *page, *entry, *kind, *holder;
} search;

static void search_run(void) {
	if (search.page == NULL) {
		return;
	}
	const char *q = c8ui_entry_get(search.entry);
	c8ui_widget_clear(search.holder);
	if (q == NULL || q[0] == '\0') {
		return;
	}
	int kind = c8ui_choice_get(search.kind);
	struct feedview *fv = view_new(kind == 2 ? VIEW_PEOPLE : VIEW_SEARCH, q,
		kind == 1 ? "latest" : "top", "Search");
	c8ui_widget_add(search.holder, view_scroller(fv, SHAPE_PAGE));
	view_reload(fv);
	view_unref(fv);
}

static void search_entered(struct c8ui_widget *w, void *data) {
	search_run();
}

void open_search(const char *query) {
	static const char *const kinds[] = { "Top", "Latest", "People" };
	struct c8ui_widget *body;
	struct c8ui_widget *page = page_new("Search", &body);
	c8ui_widget_track(page, &search.page);
	struct c8ui_widget *top = c8ui_box_new(C8UI_HORIZONTAL, 16);
	c8ui_box_set_wrap(top, true);
	top->fill = true;
	ui_pad(top, PAGE_LEFT, 0, PAGE_PAD, 0);
	search.entry = c8ui_entry_new(NULL, query ? query : "", search_entered,
		NULL);
	c8ui_entry_set_search(search.entry, true);
	c8ui_entry_set_enter_only(search.entry, true);
	c8ui_entry_set_placeholder(search.entry, "Posts, people, #tags");
	search.entry->fixed_width = 360;
	c8ui_widget_add(top, search.entry);
	search.kind = c8ui_choice_new(NULL, kinds, 3, 0, search_entered, NULL);
	c8ui_widget_add(top, search.kind);
	c8ui_widget_add(body, top);
	search.holder = c8ui_box_new(C8UI_VERTICAL, 0);
	search.holder->fill = true;
	search.holder->expand = true;
	c8ui_widget_add(body, search.holder);
	ui_open_page(page);
	if (query && query[0]) {
		search_run();
	} else {
		c8ui_widget_grab_focus(search.entry);
	}
}

/* Feeds: the saved ones, and the account's own lists */

struct feed_row {
	char *uri, *name, *avatar, *detail;
	bool list;
};

static struct {
	struct c8ui_widget *page, *list;
	GPtrArray *rows;
} feeds;

static void feed_row_free(gpointer data) {
	struct feed_row *r = data;
	g_free(r->uri);
	g_free(r->name);
	g_free(r->avatar);
	g_free(r->detail);
	g_free(r);
}

static void feed_row_add(const char *uri, const char *name,
		const char *avatar, char *detail, bool list) {
	for (guint i = 0; i < feeds.rows->len; i++) {
		struct feed_row *have = feeds.rows->pdata[i];
		if (strcmp(have->uri, uri) == 0) {
			g_free(detail);
			return;
		}
	}
	struct feed_row *r = g_new0(struct feed_row, 1);
	r->uri = g_strdup(uri);
	r->name = g_strdup(name);
	r->avatar = g_strdup(avatar);
	r->detail = detail;
	r->list = list;
	g_ptr_array_add(feeds.rows, r);
}

static void feed_row_draw(struct c8ui_widget *list, cairo_t *cr,
		const struct c8ui_list_item *item, void *data) {
	if (item->index >= (int)feeds.rows->len) {
		return;
	}
	struct feed_row *r = feeds.rows->pdata[item->index];
	struct c8ui_rect b = item->rect;
	const char *family = c8ui_widget_font(list);
	cairo_surface_t *s = r->avatar ? sky_image(r->avatar) : NULL;
	if (s) {
		c8ui_draw_surface_cover(cr, s, b.x + 8, b.y + 8, 48, 48);
	} else {
		c8ui_set_source(cr, r->list ? BAR_TILE : BLUE, 1);
		cairo_rectangle(cr, b.x + 8, b.y + 8, 48, 48);
		cairo_fill(cr);
	}
	c8ui_set_source(cr, item->fg, 1);
	c8ui_text_draw(cr, family, c8ui_text_font(C8UI_TEXT_BODY_STRONG), r->name,
		b.x + 68, b.y + 10, b.width - 76, 1);
	c8ui_set_source(cr, item->fg, item->secondary);
	c8ui_text_draw(cr, family, c8ui_text_font(C8UI_TEXT_CAPTION),
		r->detail ? r->detail : "", b.x + 68, b.y + 34, b.width - 76, 1);
}

static void feed_row_opened(struct c8ui_widget *list, int index, void *data) {
	if (index < 0 || index >= (int)feeds.rows->len) {
		return;
	}
	struct feed_row *r = feeds.rows->pdata[index];
	if (strcmp(r->uri, "following") == 0) {
		open_feed(VIEW_TIMELINE, NULL, NULL, "Following");
	} else {
		open_feed(r->list ? VIEW_LIST : VIEW_FEED, r->uri, NULL, r->name);
	}
}

static void got_lists(int status, struct json *json, const char *error,
		void *data) {
	if (status != 200 || feeds.page == NULL) {
		return;
	}
	for (struct json *l = json_get(json, "lists") ?
			json_get(json, "lists")->child : NULL; l; l = l->next) {
		const char *purpose = json_str(json_get(l, "purpose"), "");
		if (!g_str_has_suffix(purpose, "#curatelist")) {
			continue; // lists to mute or block by have no feed
		}
		feed_row_add(json_str(json_get(l, "uri"), ""),
			json_str(json_get(l, "name"), "List"),
			json_str(json_get(l, "avatar"), NULL), g_strdup("Your list"), true);
	}
	c8ui_list_set_count(feeds.list, (int)feeds.rows->len);
	c8ui_list_redraw(feeds.list);
}

static void feeds_gone(struct c8ui_widget *w, void *data) {
	if (feeds.list == w) {
		feeds.list = NULL;
		g_clear_pointer(&feeds.rows, g_ptr_array_unref);
	}
}

void open_feeds(void) {
	struct c8ui_widget *body;
	struct c8ui_widget *page = page_new("Feeds", &body);
	c8ui_widget_track(page, &feeds.page);
	if (feeds.rows) {
		g_ptr_array_unref(feeds.rows);
	}
	feeds.rows = g_ptr_array_new_with_free_func(feed_row_free);
	feed_row_add("following", "Following", NULL,
		g_strdup("Posts from the people you follow"), false);
	for (guint i = 0; g.saved && i < g.saved->len; i++) {
		struct sky_saved *s = g.saved->pdata[i];
		char *detail = s->list ? g_strdup(s->pinned ? "List  ·  Pinned" : "List") :
			g_strdup_printf("%s%s%s", s->by ? "Feed by @" : "Feed",
			s->by ? s->by : "", s->pinned ? "  ·  Pinned" : "");
		/* A feed of ours: by the persona, in stealth mode */
		char *shown = g_strdup(sky_shown_text(detail));
		g_free(detail);
		feed_row_add(s->uri, s->name, s->avatar, shown, s->list);
	}
	struct c8ui_widget *inner = c8ui_box_new(C8UI_VERTICAL, 12);
	inner->fill = true;
	ui_pad(inner, PAGE_LEFT, 8, PAGE_PAD, 40);
	struct c8ui_widget *hint = c8ui_label_new("The feeds and lists saved in "
		"your account. The pinned ones are tiles at home.", C8UI_TEXT_BODY);
	c8ui_label_set_color(hint, C8UI_COLOR_SECONDARY);
	c8ui_widget_add(inner, hint);
	/* Cells across the window, not one thin column down its side */
	feeds.list = c8ui_list_new(C8UI_LIST_GRID, 290, 64, feed_row_draw, NULL);
	feeds.list->fill = true;
	feeds.list->on_destroy = feeds_gone;
	c8ui_list_set_click_opens(feeds.list, true);
	c8ui_list_on_open(feeds.list, feed_row_opened, NULL);
	c8ui_list_set_count(feeds.list, (int)feeds.rows->len);
	c8ui_widget_add(inner, feeds.list);
	struct c8ui_widget *scroll = c8ui_scroll_new(inner);
	scroll->fill = true;
	scroll->expand = true;
	c8ui_widget_add(body, scroll);
	ui_push(page);
	if (sky_me) {
		GString *call = g_string_new("app.bsky.graph.getLists?limit=50");
		sky_query(call, "actor", sky_me->did);
		sky_get(call->str, got_lists, NULL);
		g_string_free(call, TRUE);
	}
}

/* Signing in: the form, on the accounts page and as the whole window
 * when there is no account */

static struct {
	struct c8ui_widget *box, *handle, *password, *server, *button, *status;
	bool busy;
} form;

static void signed_in(const char *error, void *data) {
	form.busy = false;
	if (error) {
		if (form.box) {
			c8ui_label_set_text(form.status, error);
			c8ui_widget_set_hidden(form.status, false);
			c8ui_widget_set_disabled(form.button, false);
		}
		return;
	}
	ui_signed_in();
}

static void sign_in(struct c8ui_widget *w, void *data) {
	if (form.busy || form.box == NULL) {
		return;
	}
	form.busy = true;
	c8ui_label_set_text(form.status, "Signing in…");
	c8ui_widget_set_hidden(form.status, false);
	c8ui_widget_set_disabled(form.button, true);
	const char *handle = c8ui_entry_get(form.handle);
	const char *server = c8ui_entry_get(form.server);
	/* Back into the account whose session ended: it is where it was */
	if (server[0] == '\0' && sky_me && sky_me->expired &&
			g_ascii_strcasecmp(handle[0] == '@' ? handle + 1 : handle,
			sky_me->handle) == 0) {
		server = sky_me->pds;
	}
	sky_sign_in(handle, c8ui_entry_get(form.password), server, signed_in,
		NULL);
	c8ui_entry_set(form.password, "");
}

static void app_passwords(struct c8ui_widget *w, void *data) {
	if (g.win) {
		c8ui_window_open_uri(g.win, "https://bsky.app/settings/app-passwords");
	}
}

static struct c8ui_widget *form_new(const char *handle) {
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 14);
	/* As wide as a form wants, less in a narrow window */
	box->fixed_width = 420;
	c8ui_widget_track(box, &form.box);
	/* Enter in any of its boxes signs in */
	form.handle = c8ui_entry_new("Handle", handle ? handle : "", sign_in,
		NULL);
	c8ui_entry_set_enter_only(form.handle, true);
	c8ui_entry_set_placeholder(form.handle, "name.bsky.social");
	form.handle->fill = true;
	c8ui_widget_add(box, form.handle);
	form.password = c8ui_entry_new("App password", "", sign_in, NULL);
	c8ui_entry_set_password(form.password, true);
	c8ui_entry_set_enter_only(form.password, true);
	c8ui_entry_set_placeholder(form.password, "xxxx-xxxx-xxxx-xxxx");
	form.password->fill = true;
	c8ui_widget_add(box, form.password);
	struct c8ui_widget *hint = c8ui_label_new("Not your account's own "
		"password: one made for this app, which you can take back any time. "
		"It is kept in your wallet.", C8UI_TEXT_CAPTION);
	c8ui_label_set_color(hint, C8UI_COLOR_SECONDARY);
	c8ui_widget_add(box, hint);
	c8ui_widget_add(box, c8ui_link_new("Make an app password on bsky.app",
		app_passwords, NULL));
	form.server = c8ui_entry_new(NULL, "", sign_in, NULL);
	c8ui_entry_set_enter_only(form.server, true);
	c8ui_entry_set_placeholder(form.server, "Found from your handle");
	form.server->fill = true;
	struct c8ui_widget *advanced = c8ui_expander_new("Server", form.server,
		false, NULL, NULL);
	advanced->fill = true;
	c8ui_widget_add(box, advanced);
	form.button = c8ui_button_new("Sign in", sign_in, NULL);
	c8ui_button_set_primary(form.button, true);
	c8ui_widget_set_disabled(form.button, form.busy);
	c8ui_widget_add(box, form.button);
	form.status = c8ui_label_new("", C8UI_TEXT_BODY);
	c8ui_widget_set_hidden(form.status, true);
	c8ui_widget_add(box, form.status);
	return box;
}

struct c8ui_widget *welcome_new(void) {
	struct c8ui_widget *inner = c8ui_box_new(C8UI_VERTICAL, 16);
	inner->fill = true;
	ui_pad(inner, PAGE_LEFT, 60, PAGE_PAD, 40);
	c8ui_widget_add(inner, c8ui_label_new("Cirrus", C8UI_TEXT_HEADER));
	bool again = sky_me && sky_me->expired;
	struct c8ui_widget *say = c8ui_label_new(again ? "Your session has ended. "
		"Sign in again to carry on." : "Sign in to Bluesky to see what the people you "
		"follow are saying.", C8UI_TEXT_BODY);
	say->fixed_width = 420;
	c8ui_widget_add(inner, say);
	/* Not filled in while in stealth mode: it would show the handle */
	c8ui_widget_add(inner, form_new(again && !sky_mine(sky_me->did) ?
		sky_me->handle : NULL));
	struct c8ui_widget *scroll = c8ui_scroll_new(inner);
	scroll->fill = true;
	scroll->expand = true;
	return scroll;
}

/* Accounts */

static void account_use(struct c8ui_widget *w, void *data) {
	struct sky_account *a = sky_account_find(data);
	if (a && a != sky_me) {
		sky_use(a);
		ui_signed_in();
	}
}

static void account_out_answer(int choice, void *data) {
	char *did = data;
	struct sky_account *a = sky_account_find(did);
	if (choice == 0 && a) {
		sky_sign_out(a);
		ui_signed_in();
		if (sky_me) {
			open_accounts();
		}
	}
	g_free(did);
}

static void account_out(struct c8ui_widget *w, void *data) {
	static const char *const buttons[] = { "Sign out", "Cancel" };
	struct sky_account *a = sky_account_find(data);
	if (a) {
		char *title = g_strdup_printf("Sign out of @%s?",
			sky_shown_handle(a->did, a->handle));
		c8ui_dialog_show(g.root, title, "This PC forgets the account. Your "
			"posts and everything else stay on Bluesky.", buttons, 2,
			account_out_answer, g_strdup(a->did));
		g_free(title);
	}
}

/* A row of the Accounts page: the DID its buttons carry, and what
 * stealth mode changes */
struct account_row {
	char *did;
	struct c8ui_widget *face, *name, *sub;
};

static GPtrArray *account_rows;

static void account_row_gone(struct c8ui_widget *w, void *data) {
	struct account_row *r = data;
	g_ptr_array_remove(account_rows, r);
	g_free(r->did);
	g_free(r);
}

static void account_row_show(struct account_row *r) {
	struct sky_account *a = sky_account_find(r->did);
	if (a == NULL) {
		return;
	}
	c8ui_label_set_text(r->name, sky_shown_name(a->did, a->name ? a->name :
		a->handle));
	char *handle = g_strdup_printf("@%s%s", sky_shown_handle(a->did,
		a->handle), a == sky_me ? (a->expired ? "  ·  Signed out" :
		"  ·  In use") : "");
	c8ui_label_set_text(r->sub, handle);
	g_free(handle);
	c8ui_widget_queue_draw(r->face);
}

/* Stealth mode went on or off: what pages show of us, again */
void pages_stealth_changed(void) {
	for (guint i = 0; profiles && i < profiles->len; i++) {
		struct profile *pr = profiles->pdata[i];
		if (pr->p && !pr->gone) {
			profile_show(pr);
		}
	}
	for (guint i = 0; account_rows && i < account_rows->len; i++) {
		account_row_show(account_rows->pdata[i]);
	}
}

/* Asked for as it is drawn: it may still be on its way */
static void face_draw(struct c8ui_widget *w, cairo_t *cr, void *data) {
	struct sky_account *a = sky_account_find(data);
	const char *url = a ? sky_shown_avatar(a->did, a->avatar) : NULL;
	cairo_surface_t *face = url ? sky_image(url) : NULL;
	if (face) {
		c8ui_draw_surface_cover(cr, face, w->rect.x, w->rect.y, w->rect.width,
			w->rect.height);
	} else {
		c8ui_set_source(cr, 0x3a8fd9, 1);
		cairo_rectangle(cr, w->rect.x, w->rect.y, w->rect.width,
			w->rect.height);
		cairo_fill(cr);
	}
}

void open_accounts(void) {
	struct c8ui_widget *body;
	struct c8ui_widget *page = page_new("Accounts", &body);
	struct c8ui_widget *inner = c8ui_box_new(C8UI_VERTICAL, 18);
	inner->fill = true;
	ui_pad(inner, PAGE_LEFT, 8, PAGE_PAD, 40);
	for (guint i = 0; i < sky_accounts->len; i++) {
		struct sky_account *a = sky_accounts->pdata[i];
		struct c8ui_widget *row = c8ui_box_new(C8UI_HORIZONTAL, 14);
		c8ui_box_set_wrap(row, true);
		/* The row owns a copy of the DID its buttons carry */
		struct account_row *r = g_new0(struct account_row, 1);
		char *did = r->did = g_strdup(a->did);
		row->user_data = r;
		row->on_destroy = account_row_gone;
		if (account_rows == NULL) {
			account_rows = g_ptr_array_new();
		}
		g_ptr_array_add(account_rows, r);
		r->face = c8ui_canvas_new(48, 48, face_draw, NULL, did);
		c8ui_widget_add(row, r->face);
		struct c8ui_widget *who = c8ui_box_new(C8UI_VERTICAL, 2);
		who->min_width = 200;
		who->align = C8UI_ALIGN_CENTER;
		r->name = c8ui_label_new("", C8UI_TEXT_BODY_STRONG);
		c8ui_widget_add(who, r->name);
		r->sub = c8ui_label_new("", C8UI_TEXT_CAPTION);
		c8ui_label_set_color(r->sub, C8UI_COLOR_SECONDARY);
		c8ui_widget_add(who, r->sub);
		account_row_show(r);
		c8ui_widget_add(row, who);
		if (a != sky_me) {
			struct c8ui_widget *use = c8ui_button_new("Use", account_use, did);
			use->align = C8UI_ALIGN_CENTER;
			c8ui_widget_add(row, use);
		}
		struct c8ui_widget *out = c8ui_button_new("Sign out", account_out, did);
		out->align = C8UI_ALIGN_CENTER;
		c8ui_widget_add(row, out);
		c8ui_widget_add(inner, row);
	}
	if (sky_accounts->len) {
		c8ui_widget_add(inner, c8ui_separator_new(C8UI_HORIZONTAL));
	}
	c8ui_widget_add(inner, c8ui_label_new("Add an account", C8UI_TEXT_TITLE));
	c8ui_widget_add(inner, form_new(NULL));
	struct c8ui_widget *scroll = c8ui_scroll_new(inner);
	scroll->fill = true;
	scroll->expand = true;
	c8ui_widget_add(body, scroll);
	ui_push(page);
}

/* Pictures, one at a time, as big as the window */

struct shot {
	struct sky_post *post;
	int index;
};

static void shot_draw(struct c8ui_widget *w, cairo_t *cr, void *data) {
	struct shot *s = data;
	struct sky_image *im = &s->post->images[s->index];
	struct c8ui_rect r = w->rect;
	const char *family = c8ui_widget_font(w);
	double caption = 0;
	if (im->alt) {
		double tw, th;
		c8ui_text_size(family, c8ui_text_font(C8UI_TEXT_BODY), im->alt,
			fmin(r.width - 40, 720), 4, &tw, &th);
		caption = th + 24;
	}
	cairo_surface_t *big = sky_image_large(im->full ? im->full : im->thumb);
	if (big == NULL) {
		big = sky_image(im->thumb);
	}
	if (big) {
		c8ui_draw_surface_fit(cr, big, r.x + 8, r.y + 8, r.width - 16,
			r.height - 16 - caption, 2);
	} else {
		cairo_set_source_rgba(cr, 1, 1, 1, 0.6);
		c8ui_text_draw(cr, family, c8ui_text_font(C8UI_TEXT_BODY), "Loading…",
			r.x + 20, r.y + 20, 0, 1);
	}
	if (im->alt) {
		double tw = fmin(r.width - 40, 720);
		cairo_set_source_rgba(cr, 1, 1, 1, 0.85);
		c8ui_text_draw(cr, family, c8ui_text_font(C8UI_TEXT_BODY), im->alt,
			r.x + (r.width - tw) / 2, r.y + r.height - caption + 8, tw, 4);
	}
}

static void shot_gone(struct c8ui_widget *w, void *data) {
	struct shot *s = data;
	sky_post_unref(s->post);
	g_free(s);
}

void open_viewer(struct sky_post *post, int index) {
	if (post == NULL || post->n_images == 0) {
		return;
	}
	struct c8ui_widget *body;
	char *title = g_strdup_printf("%s's picture%s",
		sky_shown_name(post->did, post->name),
		post->n_images > 1 ? "s" : "");
	struct c8ui_widget *page = page_new(title, &body);
	g_free(title);
	c8ui_widget_set_scheme(page, C8UI_SCHEME_DARK);
	c8ui_widget_set_background(page, 0x101418);
	struct c8ui_widget *flip = c8ui_flip_new(NULL, NULL);
	flip->fill = true;
	flip->expand = true;
	for (int i = 0; i < post->n_images; i++) {
		struct shot *s = g_new0(struct shot, 1);
		s->post = sky_post_ref(post);
		s->index = i;
		struct c8ui_widget *shot = c8ui_canvas_new(0, 0, shot_draw, NULL, s);
		shot->fill = true;
		shot->expand = true;
		shot->user_data = s;
		shot->on_destroy = shot_gone;
		c8ui_widget_add(flip, shot);
	}
	c8ui_flip_set_dots(flip, post->n_images > 1);
	c8ui_flip_set(flip, CLAMP(index, 0, post->n_images - 1));
	c8ui_widget_add(body, flip);
	ui_push(page);
	c8ui_widget_grab_focus(flip);
}
