/*
 * The viewer: one picture on black, the rest of its folder a flip away,
 * as Windows 8's Photos showed them. Arrows at the sides for the mouse
 * (gone when it rests), the wheel or the arrow keys, a finger dragging
 * the next one in, three fingers on the touchpad or two sideways.
 * Ctrl+wheel, a pinch, + and - zoom about the pointer; a double click or
 * tap goes between fitting and actual size; zoomed in, dragging pans.
 *
 * The picture shown and its neighbours (and the slide show's next) are
 * decoded ahead, so flipping is instant. Shrunk pictures are drawn from
 * a copy at the size shown (c8ui_draw_surface), made again once a zoom
 * has settled; in between, and at actual size or more, cairo scales the
 * whole picture under a clip, which costs nothing like a good filter.
 */
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <xkbcommon/xkbcommon-keysyms.h>
#include "photos.h"

#define GAP 24 // between pictures while flipping
#define FLIP_MS 280
#define FADE_MS 700
#define ARROW_W 40
#define ARROW_H 60
#define ARROWS_MS 2000 // the mouse resting this long hides the arrows
#define MAX_ZOOM 16.0
#define TAP_PX 12
#define DOUBLE_MS 350
#define SETTLE_MS 160
#define SWIPE_PX 60 // sideways on the touchpad to flip
#define CROP_MARGIN 48 // round the picture while cropping, for the handles
#define CROP_ZOOM 4.0 // a small picture is shown bigger to crop
#define CROP_MIN 16 // px of the picture, the smallest box
#define HANDLE_R 7
#define GRAB_PX 18 // how near an edge a press takes it
#define GRAB_TOUCH_PX 32

enum {
	GRAB_L = 1,
	GRAB_R = 2,
	GRAB_T = 4,
	GRAB_B = 8,
	GRAB_MOVE = 16,
};

enum slot { PREV, CUR, NEXT, AHEAD, FADE, SLOTS };

static struct {
	struct c8ui_widget *w, *ring;
	struct viewer_hooks hooks;
	GPtrArray *items;
	int index, ahead; // ahead: the slide show's next, -1
	struct ph_picture *pics[SLOTS];
	bool told; // hooks.changed ran for the current one being ready

	/* Zoom: fitting, or scale with the picture's point (cx, cy) in the
	 * middle of the view */
	bool fit;
	double scale, cx, cy;
	int64_t zoomed_at;
	guint settle_timer;

	/* Flipping: where the current picture is, from its place */
	double slide, anim_from;
	int64_t anim_start;
	bool animating;
	/* The slide show crossfades from the one before */
	double fade;
	int64_t fade_start;
	bool fading;

	/* Mouse */
	double px, py;
	bool arrows;
	guint arrows_timer;
	int arrow_down; // -1, or the side: 0 left, 1 right
	bool panning;
	double pan_x, pan_y;
	int64_t last_click;
	double click_x, click_y;
	double hscroll;
	bool scroll_used;
	struct c8ui_tab_swipe swipe;

	/* A finger */
	bool touching, tmoved;
	int32_t touch;
	double t0x, t0y, tlx, tly, tvx;
	uint32_t tltime;
	int64_t last_tap;
	double tap_x, tap_y;
	guint tap_timer;

	/* Two fingers, or a touchpad pinch */
	bool pinching;
	double pinch_s0, pinch_x, pinch_y;

	bool show;
	guint show_timer;

	/* Cropping: what is kept, in the picture's pixels; ratio its width
	 * over its height, or 0 for any. grab says which edges a press (or
	 * finger) holds, from the box it had then (g*). */
	bool crop;
	double ratio;
	double kx, ky, kw, kh;
	int grab;
	double grab_x, grab_y, gx, gy, gw, gh;
	bool crop_touching;
	int32_t crop_touch;
} v = { .index = -1, .ahead = -1, .fit = true, .arrow_down = -1 };

static int64_t now_ms(void) {
	return g_get_monotonic_time() / 1000;
}

static double ease_out(double t) {
	return 1 - pow(1 - t, 3);
}

static int count(void) {
	return v.items ? (int)v.items->len : 0;
}

static struct ph_picture *current(void) {
	struct ph_picture *p = v.pics[CUR];
	return p && p->state == PH_READY ? p : NULL;
}

/* Geometry */

static double view_w(void) {
	return v.w->rect.width;
}

static double view_h(void) {
	return v.w->rect.height;
}

/* Small pictures are shown at their size, not blown up */
static double fit_scale(struct ph_picture *p) {
	if (p->width <= 0 || p->height <= 0 || view_w() <= 0) {
		return 1;
	}
	double s = fmin(view_w() / p->width, view_h() / p->height);
	return fmin(s, 1);
}

static double scale_of(struct ph_picture *p) {
	return v.fit ? fit_scale(p) : v.scale;
}

/* Where a picture is drawn, in root coordinates; only the current one
 * is zoomed */
static struct c8ui_rect place(struct ph_picture *p, bool cur) {
	double s = cur ? scale_of(p) : fit_scale(p);
	double w = p->width * s, h = p->height * s;
	double x = (view_w() - w) / 2, y = (view_h() - h) / 2;
	if (cur && !v.fit && w > view_w()) {
		x = view_w() / 2 - v.cx * s;
	}
	if (cur && !v.fit && h > view_h()) {
		y = view_h() / 2 - v.cy * s;
	}
	return (struct c8ui_rect){ v.w->rect.x + x, v.w->rect.y + y, w, h };
}

/* Bigger than the view: then dragging pans instead of flipping */
static bool pannable(void) {
	struct ph_picture *p = current();
	if (p == NULL || v.fit) {
		return false;
	}
	return p->width * v.scale > view_w() + 0.5 ||
		p->height * v.scale > view_h() + 0.5;
}

/* Keeps the picture over the view: no black beside a zoomed picture */
static void clamp_view(void) {
	struct ph_picture *p = current();
	if (p == NULL || v.fit) {
		return;
	}
	double hw = view_w() / 2 / v.scale, hh = view_h() / 2 / v.scale;
	if (p->width * v.scale > view_w()) {
		v.cx = fmax(hw, fmin(p->width - hw, v.cx));
	} else {
		v.cx = p->width / 2.0;
	}
	if (p->height * v.scale > view_h()) {
		v.cy = fmax(hh, fmin(p->height - hh, v.cy));
	} else {
		v.cy = p->height / 2.0;
	}
}

static gboolean settled(gpointer data) {
	v.settle_timer = 0;
	c8ui_widget_queue_draw(v.w);
	return G_SOURCE_REMOVE;
}

/* Zooms to s keeping the point (x, y) of the window where it is */
static void zoom_at(double x, double y, double s) {
	struct ph_picture *p = current();
	if (p == NULL) {
		return;
	}
	double fs = fit_scale(p);
	s = fmin(MAX_ZOOM, s);
	struct c8ui_rect r = place(p, true);
	double old = scale_of(p);
	double ix = (x - r.x) / old, iy = (y - r.y) / old;
	if (s <= fs * 1.0001) {
		v.fit = true;
	} else {
		v.fit = false;
		v.scale = s;
		v.cx = (view_w() / 2 - (x - v.w->rect.x) + ix * s) / s;
		v.cy = (view_h() / 2 - (y - v.w->rect.y) + iy * s) / s;
		clamp_view();
	}
	v.zoomed_at = now_ms();
	if (v.settle_timer) {
		g_source_remove(v.settle_timer);
	}
	v.settle_timer = g_timeout_add(SETTLE_MS, settled, NULL);
	c8ui_widget_queue_draw(v.w);
}

static void middle(double *x, double *y) {
	*x = v.w->rect.x + view_w() / 2;
	*y = v.w->rect.y + view_h() / 2;
}

/* factor > 1 zooms in, about the middle; 0 fits it, -1 is actual size */
static void viewer_zoom(double factor) {
	struct ph_picture *p = current();
	if (p == NULL) {
		return;
	}
	double x, y;
	middle(&x, &y);
	if (factor == 0) {
		zoom_at(x, y, 0);
	} else if (factor < 0) {
		zoom_at(x, y, 1);
	} else {
		zoom_at(x, y, scale_of(p) * factor);
	}
}

/* A double click or tap: in to actual size (or twice the size of a
 * small one) at the point, or back out */
static void toggle_zoom(double x, double y) {
	struct ph_picture *p = current();
	if (p == NULL) {
		return;
	}
	if (v.fit) {
		double fs = fit_scale(p);
		zoom_at(x, y, fs < 1 ? 1 : 2);
	} else {
		zoom_at(x, y, 0);
	}
}

static void pan_by(double dx, double dy) {
	struct ph_picture *p = current();
	if (p == NULL || v.fit) {
		return;
	}
	v.cx -= dx / v.scale;
	v.cy -= dy / v.scale;
	clamp_view();
	c8ui_widget_queue_draw(v.w);
}

/* The pictures kept decoded */

static void update_ring(void) {
	struct ph_picture *p = v.pics[CUR];
	c8ui_ring_set_active(v.ring, p && p->state == PH_LOADING);
}

static void pic_changed(struct ph_picture *pic, void *data) {
	if (pic == v.pics[CUR]) {
		update_ring();
		if (!v.told && pic->state != PH_LOADING) {
			v.told = true;
			if (v.hooks.changed) {
				v.hooks.changed();
			}
		}
	}
	c8ui_widget_queue_draw(v.w);
}

static const char *uri_at(int index) {
	if (index < 0 || index >= count()) {
		return NULL;
	}
	struct ph_entry *e = v.items->pdata[index];
	return e->uri;
}

/* Keeps the pictures wanted now, reusing those already decoded */
static void refresh_cache(void) {
	struct ph_picture *old[SLOTS];
	memcpy(old, v.pics, sizeof(old));
	struct ph_picture *keep[SLOTS] = { NULL };
	keep[FADE] = old[FADE];
	old[FADE] = NULL;
	const char *want[FADE];
	want[PREV] = uri_at(v.index - 1);
	want[CUR] = uri_at(v.index);
	want[NEXT] = uri_at(v.index + 1);
	want[AHEAD] = v.show ? uri_at(v.ahead) : NULL;
	/* The current one first: its decoder starts first */
	static const int order[FADE] = { CUR, NEXT, PREV, AHEAD };
	for (int k = 0; k < FADE; k++) {
		int i = order[k];
		if (want[i] == NULL) {
			continue;
		}
		for (int j = 0; j < FADE && keep[i] == NULL; j++) {
			if (old[j] && strcmp(old[j]->uri, want[i]) == 0) {
				keep[i] = old[j];
				old[j] = NULL;
			}
		}
		for (int j = 0; j < SLOTS && keep[i] == NULL; j++) {
			if (j != i && keep[j] && strcmp(keep[j]->uri, want[i]) == 0) {
				keep[i] = ph_picture_ref(keep[j]);
			}
		}
		if (keep[i] == NULL) {
			keep[i] = ph_picture_load(want[i], pic_changed, NULL);
		}
	}
	for (int j = 0; j < SLOTS; j++) {
		ph_picture_release(old[j]);
	}
	memcpy(v.pics, keep, sizeof(keep));
	for (int j = 0; j < SLOTS; j++) {
		ph_picture_set_playing(v.pics[j], false);
	}
	ph_picture_set_playing(v.pics[CUR], true);
	v.told = v.pics[CUR] && v.pics[CUR]->state != PH_LOADING;
	update_ring();
}

static void drop_all(void) {
	for (int j = 0; j < SLOTS; j++) {
		ph_picture_release(v.pics[j]);
		v.pics[j] = NULL;
	}
}

/* Cropping */

static double crop_margin(void) {
	return fmin(CROP_MARGIN, fmin(view_w(), view_h()) / 10);
}

/* The whole picture while cropping: fitted inside the margin, a small
 * one made bigger */
static struct c8ui_rect crop_place(struct ph_picture *p) {
	double m = crop_margin();
	double s = fmin((view_w() - 2 * m) / p->width,
		(view_h() - 2 * m) / p->height);
	s = fmax(0.01, fmin(s, CROP_ZOOM));
	double w = p->width * s, h = p->height * s;
	return (struct c8ui_rect){ v.w->rect.x + round((view_w() - w) / 2),
		v.w->rect.y + round((view_h() - h) / 2), w, h };
}

/* The kept box on screen */
static struct c8ui_rect crop_box(struct ph_picture *p) {
	struct c8ui_rect r = crop_place(p);
	double s = r.width / p->width;
	return (struct c8ui_rect){ r.x + v.kx * s, r.y + v.ky * s, v.kw * s,
		v.kh * s };
}

/* The biggest box of the ratio that fits, in the middle */
static void crop_fill(struct ph_picture *p) {
	double w = p->width, h = p->height;
	if (v.ratio > 0) {
		if (w / h > v.ratio) {
			w = h * v.ratio;
		} else {
			h = w / v.ratio;
		}
	}
	v.kw = w;
	v.kh = h;
	v.kx = (p->width - w) / 2;
	v.ky = (p->height - h) / 2;
}

static int grab_at(struct ph_picture *p, double x, double y, double tol) {
	struct c8ui_rect b = crop_box(p);
	double l = b.x, r = b.x + b.width, t = b.y, bo = b.y + b.height;
	if (x < l - tol || x > r + tol || y < t - tol || y > bo + tol) {
		return 0;
	}
	int g = 0;
	/* A small box: the nearer edge of the two */
	if (fabs(x - l) <= tol && fabs(x - l) <= fabs(x - r)) {
		g |= GRAB_L;
	} else if (fabs(x - r) <= tol) {
		g |= GRAB_R;
	}
	if (fabs(y - t) <= tol && fabs(y - t) <= fabs(y - bo)) {
		g |= GRAB_T;
	} else if (fabs(y - bo) <= tol) {
		g |= GRAB_B;
	}
	if (g == 0 && x > l && x < r && y > t && y < bo) {
		g = GRAB_MOVE;
	}
	return g;
}

static enum c8ui_cursor grab_cursor(int g) {
	if (g == GRAB_MOVE) {
		return C8UI_CURSOR_MOVE;
	}
	if (g == GRAB_L || g == GRAB_R) {
		return C8UI_CURSOR_RESIZE_H;
	}
	if (g == GRAB_T || g == GRAB_B) {
		return C8UI_CURSOR_RESIZE_V;
	}
	return g ? C8UI_CURSOR_POINTER : C8UI_CURSOR_DEFAULT;
}

/* The press moved (dx, dy) in the picture's pixels from where it took
 * the box */
static void crop_drag(struct ph_picture *p, double dx, double dy) {
	double pw = p->width, ph = p->height;
	double min = fmin(CROP_MIN, fmin(pw, ph));
	if (v.grab == GRAB_MOVE) {
		v.kx = fmax(0, fmin(pw - v.kw, v.gx + dx));
		v.ky = fmax(0, fmin(ph - v.kh, v.gy + dy));
		return;
	}
	/* The edges held follow the pointer; the others stay */
	double l = v.gx, t = v.gy, r = v.gx + v.gw, b = v.gy + v.gh;
	if (v.grab & GRAB_L) {
		l = fmax(0, fmin(r - min, l + dx));
	}
	if (v.grab & GRAB_R) {
		r = fmin(pw, fmax(l + min, r + dx));
	}
	if (v.grab & GRAB_T) {
		t = fmax(0, fmin(b - min, t + dy));
	}
	if (v.grab & GRAB_B) {
		b = fmin(ph, fmax(t + min, b + dy));
	}
	double w = r - l, h = b - t;
	if (v.ratio <= 0) {
		v.kx = l;
		v.ky = t;
		v.kw = w;
		v.kh = h;
		return;
	}
	bool horiz = v.grab & (GRAB_L | GRAB_R), vert = v.grab & (GRAB_T | GRAB_B);
	/* Held by a corner the box keeps the ratio from the opposite one; by
	 * a side, the other way grows from the middle */
	double ax = (v.grab & GRAB_L) ? r : l; // the side that stays
	double ay = (v.grab & GRAB_T) ? b : t;
	double cx = v.gx + v.gw / 2, cy = v.gy + v.gh / 2;
	double maxw = (v.grab & GRAB_L) ? ax : pw - ax;
	double maxh = (v.grab & GRAB_T) ? ay : ph - ay;
	if (!horiz) {
		maxw = 2 * fmin(cx, pw - cx);
	}
	if (!vert) {
		maxh = 2 * fmin(cy, ph - cy);
	}
	if (horiz && vert) {
		if (w / h > v.ratio) {
			w = h * v.ratio;
		} else {
			h = w / v.ratio;
		}
	} else if (horiz) {
		h = w / v.ratio;
	} else {
		w = h * v.ratio;
	}
	if (w > maxw) {
		w = maxw;
		h = w / v.ratio;
	}
	if (h > maxh) {
		h = maxh;
		w = h * v.ratio;
	}
	v.kw = w;
	v.kh = h;
	v.kx = horiz ? ((v.grab & GRAB_L) ? ax - w : ax) : cx - w / 2;
	v.ky = vert ? ((v.grab & GRAB_T) ? ay - h : ay) : cy - h / 2;
}

static void draw_handle(cairo_t *cr, double x, double y) {
	cairo_new_sub_path(cr);
	cairo_arc(cr, x, y, HANDLE_R, 0, 2 * M_PI);
	c8ui_set_source(cr, 0xffffff, 1);
	cairo_fill_preserve(cr);
	c8ui_set_source(cr, 0x000000, 0.6);
	cairo_set_line_width(cr, 1);
	cairo_stroke(cr);
}

static void draw_crop(cairo_t *cr, struct ph_picture *p) {
	struct c8ui_rect r = crop_place(p);
	if (r.width < p->width) {
		c8ui_draw_surface(cr, p->surface, r.x, r.y, r.width, r.height);
	} else {
		cairo_save(cr);
		cairo_translate(cr, r.x, r.y);
		cairo_scale(cr, r.width / p->width, r.height / p->height);
		cairo_set_source_surface(cr, p->surface, 0, 0);
		cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BILINEAR);
		cairo_paint(cr);
		cairo_restore(cr);
	}
	struct c8ui_rect b = crop_box(p);
	/* What goes, dimmed */
	cairo_save(cr);
	cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
	cairo_rectangle(cr, r.x, r.y, r.width, r.height);
	cairo_rectangle(cr, b.x, b.y, b.width, b.height);
	c8ui_set_source(cr, 0x000000, 0.6);
	cairo_fill(cr);
	cairo_restore(cr);
	/* Thirds, for placing things, and the edge */
	c8ui_set_source(cr, 0xffffff, v.grab ? 0.5 : 0.25);
	cairo_set_line_width(cr, 1);
	for (int i = 1; i < 3; i++) {
		double x = round(b.x + b.width * i / 3) + 0.5;
		double y = round(b.y + b.height * i / 3) + 0.5;
		cairo_move_to(cr, x, b.y);
		cairo_line_to(cr, x, b.y + b.height);
		cairo_move_to(cr, b.x, y);
		cairo_line_to(cr, b.x + b.width, y);
	}
	cairo_stroke(cr);
	c8ui_set_source(cr, 0xffffff, 1);
	cairo_set_line_width(cr, 2);
	cairo_rectangle(cr, b.x, b.y, b.width, b.height);
	cairo_stroke(cr);
	double mx = b.x + b.width / 2, my = b.y + b.height / 2;
	double xs[3] = { b.x, mx, b.x + b.width };
	double ys[3] = { b.y, my, b.y + b.height };
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			/* The sides' middles only with room between the corners;
			 * with a ratio a side drags too, so they're there too */
			bool middle = i == 1 || j == 1;
			if ((i == 1 && j == 1) || (middle && (i == 1 ? b.width :
					b.height) < 6 * HANDLE_R)) {
				continue;
			}
			draw_handle(cr, xs[i], ys[j]);
		}
	}
}

static void crop_press(double x, double y, double tol) {
	struct ph_picture *p = current();
	if (p == NULL) {
		return;
	}
	v.grab = grab_at(p, x, y, tol);
	v.grab_x = x;
	v.grab_y = y;
	v.gx = v.kx;
	v.gy = v.ky;
	v.gw = v.kw;
	v.gh = v.kh;
	c8ui_widget_queue_draw(v.w);
}

static void crop_motion(double x, double y) {
	struct ph_picture *p = current();
	if (p == NULL) {
		return;
	}
	if (v.grab) {
		double s = crop_place(p).width / p->width;
		crop_drag(p, (x - v.grab_x) / s, (y - v.grab_y) / s);
		c8ui_widget_queue_draw(v.w);
	}
	c8ui_widget_set_cursor(v.w, grab_cursor(v.grab ? v.grab :
		grab_at(p, x, y, GRAB_PX)));
}

static void crop_release(void) {
	v.grab = 0;
	c8ui_widget_queue_draw(v.w);
}

/* Arrows move the box a little; with Shift they move its bottom right
 * corner, to make it bigger or smaller */
static bool crop_key(const struct c8ui_event *ev) {
	struct ph_picture *p = current();
	if (p == NULL) {
		return false;
	}
	double step = fmax(1, fmax(p->width, p->height) / 100);
	double dx = 0, dy = 0;
	switch (ev->sym) {
	case XKB_KEY_Left:
	case XKB_KEY_KP_Left:
		dx = -step;
		break;
	case XKB_KEY_Right:
	case XKB_KEY_KP_Right:
		dx = step;
		break;
	case XKB_KEY_Up:
	case XKB_KEY_KP_Up:
		dy = -step;
		break;
	case XKB_KEY_Down:
	case XKB_KEY_KP_Down:
		dy = step;
		break;
	default:
		return false;
	}
	v.grab = (ev->mods & C8UI_MOD_SHIFT) ? GRAB_R | GRAB_B : GRAB_MOVE;
	v.gx = v.kx;
	v.gy = v.ky;
	v.gw = v.kw;
	v.gh = v.kh;
	if (v.grab != GRAB_MOVE && v.ratio > 0 && dx == 0) {
		dx = dy * v.ratio; // a corner with a ratio follows the width
	}
	crop_drag(p, dx, dy);
	v.grab = 0;
	c8ui_widget_queue_draw(v.w);
	return true;
}

/* For the root's keys: the ratio button or Save may have the focus */
bool viewer_crop_key(const struct c8ui_event *ev) {
	return v.crop && crop_key(ev);
}

static bool crop_event(const struct c8ui_event *ev) {
	switch (ev->type) {
	case C8UI_EVENT_ENTER:
	case C8UI_EVENT_MOTION:
		crop_motion(ev->x, ev->y);
		return true;
	case C8UI_EVENT_PRESS:
		if (ev->button == C8UI_BUTTON_LEFT) {
			crop_press(ev->x, ev->y, GRAB_PX);
		}
		return true;
	case C8UI_EVENT_RELEASE:
		crop_release();
		return true;
	case C8UI_EVENT_TOUCH_DOWN:
		if (v.crop_touching) {
			return false;
		}
		v.crop_touching = true;
		v.crop_touch = ev->touch_id;
		crop_press(ev->x, ev->y, GRAB_TOUCH_PX);
		return true;
	case C8UI_EVENT_TOUCH_MOTION:
		if (v.crop_touching && ev->touch_id == v.crop_touch) {
			crop_motion(ev->x, ev->y);
		}
		return true;
	case C8UI_EVENT_TOUCH_UP:
	case C8UI_EVENT_TOUCH_CANCEL:
		if (v.crop_touching && (ev->type == C8UI_EVENT_TOUCH_CANCEL ||
				ev->touch_id == v.crop_touch)) {
			v.crop_touching = false;
			crop_release();
		}
		return true;
	case C8UI_EVENT_KEY:
		return crop_key(ev);
	case C8UI_EVENT_LEAVE:
		return true;
	default:
		/* No flipping, zooming or swiping while cropping */
		return ev->type != C8UI_EVENT_SCROLL_STOP;
	}
}

bool viewer_crop_begin(double ratio) {
	struct ph_picture *p = current();
	if (p == NULL || p->animated || v.show) {
		return false;
	}
	v.crop = true;
	v.ratio = ratio;
	v.grab = 0;
	v.fit = true;
	v.slide = 0;
	v.animating = false;
	v.arrows = false;
	crop_fill(p);
	c8ui_widget_queue_draw(v.w);
	return true;
}

void viewer_crop_set_ratio(double ratio) {
	struct ph_picture *p = current();
	if (!v.crop || p == NULL) {
		return;
	}
	v.ratio = ratio;
	if (ratio > 0) {
		crop_fill(p);
	}
	c8ui_widget_queue_draw(v.w);
}

void viewer_crop_end(void) {
	if (!v.crop) {
		return;
	}
	v.crop = false;
	v.grab = 0;
	c8ui_widget_set_cursor(v.w, C8UI_CURSOR_DEFAULT);
	c8ui_widget_queue_draw(v.w);
}

bool viewer_cropping(void) {
	return v.crop;
}

bool viewer_crop_get(int *x, int *y, int *w, int *h) {
	struct ph_picture *p = current();
	if (!v.crop || p == NULL) {
		return false;
	}
	*x = CLAMP((int)lround(v.kx), 0, p->width - 1);
	*y = CLAMP((int)lround(v.ky), 0, p->height - 1);
	*w = CLAMP((int)lround(v.kw), 1, p->width - *x);
	*h = CLAMP((int)lround(v.kh), 1, p->height - *y);
	return true;
}

/* Drawing */

static void draw_failed(cairo_t *cr, struct ph_picture *p, double dx) {
	const char *family = c8ui_widget_font(v.w);
	struct c8ui_font font = c8ui_text_font(C8UI_TEXT_SUBHEADER);
	double maxw = fmin(600, view_w() - 48);
	double tw, th;
	const char *msg = p->error ? p->error : "This picture can't be shown.";
	c8ui_text_size(family, font, msg, maxw, 0, &tw, &th);
	double x = v.w->rect.x + dx + (view_w() - tw) / 2;
	double y = v.w->rect.y + (view_h() - th) / 2;
	c8ui_set_source(cr, 0xffffff, 0.85);
	c8ui_text_draw(cr, family, font, msg, x, y, maxw, 0);
}

static void draw_picture(cairo_t *cr, struct ph_picture *p, double dx,
		bool cur, double alpha) {
	if (p == NULL || alpha <= 0) {
		return;
	}
	if (p->state == PH_FAILED) {
		draw_failed(cr, p, dx);
		return;
	}
	if (p->state != PH_READY) {
		return;
	}
	struct c8ui_rect r = place(p, cur);
	r.x += dx;
	double s = r.width / p->width;
	bool still = !v.pinching && now_ms() - v.zoomed_at >= SETTLE_MS;
	if (alpha < 1) {
		cairo_push_group(cr);
	}
	if (s < 1 && (still || !cur)) {
		c8ui_draw_surface(cr, p->surface, r.x, r.y, r.width, r.height);
	} else {
		/* Only the part in view gets drawn */
		cairo_save(cr);
		cairo_rectangle(cr, r.x, r.y, r.width, r.height);
		cairo_clip(cr);
		cairo_translate(cr, r.x, r.y);
		cairo_scale(cr, s, s);
		cairo_set_source_surface(cr, p->surface, 0, 0);
		/* Zoomed well in, pixels show as squares, as in any editor */
		cairo_pattern_set_filter(cairo_get_source(cr),
			s >= 2 ? CAIRO_FILTER_NEAREST : CAIRO_FILTER_BILINEAR);
		cairo_paint(cr);
		cairo_restore(cr);
	}
	if (alpha < 1) {
		cairo_pop_group_to_source(cr);
		cairo_paint_with_alpha(cr, alpha);
	}
}

static bool arrows_shown(void) {
	return v.arrows && !v.show && !v.touching && count() > 1;
}

static struct c8ui_rect arrow_rect(int side) {
	double y = v.w->rect.y + round((view_h() - ARROW_H) / 2);
	double x = side == 0 ? v.w->rect.x : v.w->rect.x + view_w() - ARROW_W;
	return (struct c8ui_rect){ x, y, ARROW_W, ARROW_H };
}

static int arrow_at(double x, double y) {
	if (!arrows_shown()) {
		return -1;
	}
	for (int side = 0; side < 2; side++) {
		bool there = side == 0 ? v.index > 0 : v.index < count() - 1;
		struct c8ui_rect r = arrow_rect(side);
		if (there && c8ui_rect_contains(&r, x, y)) {
			return side;
		}
	}
	return -1;
}

/* Metro's flip view arrows, light on the dark */
static void draw_arrows(cairo_t *cr) {
	if (!arrows_shown()) {
		return;
	}
	for (int side = 0; side < 2; side++) {
		bool there = side == 0 ? v.index > 0 : v.index < count() - 1;
		if (!there) {
			continue;
		}
		struct c8ui_rect r = arrow_rect(side);
		bool over = c8ui_rect_contains(&r, v.px, v.py);
		bool down = over && v.arrow_down == side;
		c8ui_set_source(cr, down ? 0xffffff : 0x000000,
			down ? 0.85 : over ? 0.8 : 0.55);
		cairo_rectangle(cr, r.x, r.y, r.width, r.height);
		cairo_fill(cr);
		double cx = r.x + r.width / 2, cy = r.y + r.height / 2;
		double d = side == 0 ? 1 : -1;
		c8ui_set_source(cr, down ? 0x000000 : 0xffffff, 1);
		cairo_set_line_width(cr, 2);
		cairo_move_to(cr, cx + 3 * d, cy - 7);
		cairo_line_to(cr, cx - 4 * d, cy);
		cairo_line_to(cr, cx + 3 * d, cy + 7);
		cairo_stroke(cr);
	}
}

static void viewer_draw(struct c8ui_widget *w, cairo_t *cr) {
	cairo_save(cr);
	cairo_rectangle(cr, w->rect.x, w->rect.y, w->rect.width, w->rect.height);
	cairo_clip(cr);
	c8ui_set_source(cr, 0x000000, 1);
	cairo_paint(cr);
	if (v.crop && current()) {
		draw_crop(cr, current());
	} else if (v.fading) {
		draw_picture(cr, v.pics[FADE], 0, false, 1 - v.fade);
		draw_picture(cr, v.pics[CUR], 0, true, v.fade);
	} else {
		draw_picture(cr, v.pics[CUR], v.slide, true, 1);
		if (v.slide < 0) {
			draw_picture(cr, v.pics[NEXT], v.slide + view_w() + GAP, false,
				1);
		} else if (v.slide > 0) {
			draw_picture(cr, v.pics[PREV], v.slide - view_w() - GAP, false,
				1);
		}
	}
	draw_arrows(cr);
	cairo_restore(cr);
}

/* Moving between pictures */

static void slide_back(void) {
	if (v.slide == 0) {
		v.animating = false;
		return;
	}
	v.anim_from = v.slide;
	v.anim_start = -1;
	v.animating = true;
	c8ui_widget_animate(v.w);
}

static void go(int index, bool fade) {
	if (index < 0 || index >= count() || index == v.index) {
		return;
	}
	int d = index > v.index ? 1 : -1;
	bool adjacent = abs(index - v.index) == 1;
	if (fade && v.pics[CUR]) {
		ph_picture_release(v.pics[FADE]);
		v.pics[FADE] = ph_picture_ref(v.pics[CUR]);
		v.fade = 0;
		v.fade_start = -1;
		v.fading = true;
		v.slide = 0;
		v.animating = false;
		c8ui_widget_animate(v.w);
	}
	v.index = index;
	v.fit = true;
	v.crop = false;
	if (!fade) {
		if (adjacent && view_w() > 0) {
			/* The new one comes in from where it was, beside the old */
			v.slide += d * (view_w() + GAP);
			slide_back();
		} else {
			v.slide = 0;
			v.animating = false;
		}
	}
	refresh_cache();
	c8ui_widget_queue_draw(v.w);
	if (v.hooks.changed) {
		v.hooks.changed();
	}
}

static void restart_show(void);

void viewer_go(int index) {
	go(index, false);
	if (v.show) {
		restart_show();
	}
}

static void viewer_step(int delta) {
	viewer_go(v.index + delta);
}

/* Input */

static gboolean arrows_rest(gpointer data) {
	v.arrows_timer = 0;
	v.arrows = false;
	c8ui_widget_queue_draw(v.w);
	return G_SOURCE_REMOVE;
}

static void pointer_moved(double x, double y) {
	v.px = x;
	v.py = y;
	if (v.show) {
		return;
	}
	if (!v.arrows) {
		v.arrows = true;
		c8ui_widget_queue_draw(v.w);
	} else if (arrow_at(x, y) >= 0 || v.arrow_down >= 0) {
		c8ui_widget_queue_draw(v.w); // hover shading
	}
	if (v.arrows_timer) {
		g_source_remove(v.arrows_timer);
	}
	v.arrows_timer = g_timeout_add(ARROWS_MS, arrows_rest, NULL);
	v.w->cursor = arrow_at(x, y) >= 0 ? C8UI_CURSOR_POINTER :
		pannable() ? C8UI_CURSOR_MOVE : C8UI_CURSOR_DEFAULT;
}

static void stop_show(void) {
	if (!v.show) {
		return;
	}
	viewer_slideshow(false);
	if (v.hooks.slideshow_ended) {
		v.hooks.slideshow_ended();
	}
}

static gboolean tap_done(gpointer data) {
	v.tap_timer = 0;
	if (v.hooks.context) {
		v.hooks.context();
	}
	return G_SOURCE_REMOVE;
}

static bool press(const struct c8ui_event *ev) {
	if (v.show) {
		stop_show();
		return true;
	}
	if (ev->button == C8UI_BUTTON_BACK && v.hooks.back) {
		v.hooks.back();
		return true;
	}
	if (ev->button == C8UI_BUTTON_RIGHT) {
		if (v.hooks.context) {
			v.hooks.context();
		}
		return true;
	}
	if (ev->button != C8UI_BUTTON_LEFT) {
		return false;
	}
	pointer_moved(ev->x, ev->y);
	v.arrow_down = arrow_at(ev->x, ev->y);
	if (v.arrow_down >= 0) {
		c8ui_widget_queue_draw(v.w);
		return true;
	}
	int64_t now = now_ms();
	if (now - v.last_click < DOUBLE_MS && fabs(ev->x - v.click_x) < TAP_PX &&
			fabs(ev->y - v.click_y) < TAP_PX) {
		v.last_click = 0;
		toggle_zoom(ev->x, ev->y);
		return true;
	}
	v.last_click = now;
	v.click_x = ev->x;
	v.click_y = ev->y;
	if (pannable()) {
		v.panning = true;
		v.pan_x = ev->x;
		v.pan_y = ev->y;
	}
	return true;
}

static bool release(const struct c8ui_event *ev) {
	if (v.arrow_down >= 0) {
		int side = v.arrow_down;
		v.arrow_down = -1;
		c8ui_widget_queue_draw(v.w);
		if (arrow_at(ev->x, ev->y) == side) {
			viewer_step(side == 0 ? -1 : 1);
		}
	}
	v.panning = false;
	return true;
}

static bool scroll(const struct c8ui_event *ev) {
	struct ph_picture *p = current();
	bool ctrl = ev->mods & C8UI_MOD_CTRL;
	if (!ev->precise) {
		double d = ev->dy != 0 ? ev->dy : ev->dx;
		if (d == 0) {
			return true;
		}
		if (ctrl || prefs.wheel == PH_WHEEL_ZOOM) {
			if (p) {
				zoom_at(ev->x, ev->y, scale_of(p) * (d < 0 ? 1.25 : 0.8));
			}
		} else {
			viewer_step(d > 0 ? 1 : -1);
		}
		return true;
	}
	if (ctrl) {
		if (p) {
			zoom_at(ev->x, ev->y, scale_of(p) * exp(-ev->dy / 300));
		}
		return true;
	}
	if (pannable()) {
		pan_by(-ev->dx, -ev->dy);
		return true;
	}
	/* Two fingers sideways flip once per gesture */
	v.hscroll += ev->dx;
	if (!v.scroll_used && fabs(v.hscroll) > SWIPE_PX) {
		v.scroll_used = true;
		viewer_step(v.hscroll > 0 ? 1 : -1);
	}
	return true;
}

static bool touch_down(const struct c8ui_event *ev) {
	if (v.show) {
		stop_show();
		return true;
	}
	if (v.touching) {
		return false;
	}
	v.touching = true;
	v.tmoved = false;
	v.touch = ev->touch_id;
	v.t0x = v.tlx = ev->x;
	v.t0y = v.tly = ev->y;
	v.tltime = ev->time;
	v.tvx = 0;
	v.animating = false; // caught mid-flip: it stays under the finger
	v.arrows = false;
	return true;
}

static void touch_motion(const struct c8ui_event *ev) {
	double dx = ev->x - v.tlx, dy = ev->y - v.tly;
	uint32_t dt = ev->time - v.tltime;
	if (dt > 0) {
		v.tvx = 0.6 * (dx / dt) + 0.4 * v.tvx;
	}
	v.tlx = ev->x;
	v.tly = ev->y;
	v.tltime = ev->time;
	if (fabs(ev->x - v.t0x) > TAP_PX || fabs(ev->y - v.t0y) > TAP_PX) {
		v.tmoved = true;
	}
	if (!v.tmoved) {
		return;
	}
	if (pannable() && v.slide == 0) {
		pan_by(dx, dy);
		return;
	}
	/* Past the first or last one it gives, but only a little */
	bool none = v.slide + dx > 0 ? v.index <= 0 : v.index >= count() - 1;
	v.slide += none ? dx / 3 : dx;
	c8ui_widget_queue_draw(v.w);
}

static void touch_up(const struct c8ui_event *ev) {
	v.touching = false;
	if (!v.tmoved) {
		int64_t now = now_ms();
		if (now - v.last_tap < DOUBLE_MS && fabs(ev->x - v.tap_x) < 40 &&
				fabs(ev->y - v.tap_y) < 40) {
			v.last_tap = 0;
			if (v.tap_timer) {
				g_source_remove(v.tap_timer);
				v.tap_timer = 0;
			}
			toggle_zoom(ev->x, ev->y);
		} else {
			/* A single tap shows the app bar, once it's not a double */
			v.last_tap = now;
			v.tap_x = ev->x;
			v.tap_y = ev->y;
			if (v.tap_timer) {
				g_source_remove(v.tap_timer);
			}
			v.tap_timer = g_timeout_add(DOUBLE_MS, tap_done, NULL);
		}
		return;
	}
	double w = view_w();
	if ((v.slide < -w / 4 || (v.slide < 0 && v.tvx < -0.4)) &&
			v.index < count() - 1) {
		viewer_step(1);
	} else if ((v.slide > w / 4 || (v.slide > 0 && v.tvx > 0.4)) &&
			v.index > 0) {
		viewer_step(-1);
	} else {
		slide_back();
	}
}

static bool pinch(const struct c8ui_event *ev) {
	struct ph_picture *p = current();
	switch (ev->type) {
	case C8UI_EVENT_PINCH_BEGIN:
		if (p == NULL || v.show) {
			return false;
		}
		v.pinching = true;
		v.pinch_s0 = scale_of(p);
		v.pinch_x = ev->x ? ev->x : v.px;
		v.pinch_y = ev->y ? ev->y : v.py;
		v.slide = 0;
		v.animating = false;
		return true;
	case C8UI_EVENT_PINCH_UPDATE:
		if (!v.pinching || p == NULL) {
			return true;
		}
		/* The fingers' middle moving pans */
		pan_by(ev->dx, ev->dy);
		v.pinch_x += ev->dx;
		v.pinch_y += ev->dy;
		zoom_at(v.pinch_x, v.pinch_y, v.pinch_s0 * ev->scale);
		return true;
	default:
		v.pinching = false;
		c8ui_widget_queue_draw(v.w);
		return true;
	}
}

static bool key(const struct c8ui_event *ev) {
	if (ev->mods & (C8UI_MOD_CTRL | C8UI_MOD_ALT | C8UI_MOD_SUPER)) {
		return false;
	}
	double step = fmin(view_w(), view_h()) / 8;
	switch (ev->sym) {
	case XKB_KEY_Left:
	case XKB_KEY_KP_Left:
		if (pannable()) {
			pan_by(step, 0);
		} else {
			viewer_step(-1);
		}
		return true;
	case XKB_KEY_Right:
	case XKB_KEY_KP_Right:
		if (pannable()) {
			pan_by(-step, 0);
		} else {
			viewer_step(1);
		}
		return true;
	case XKB_KEY_Up:
	case XKB_KEY_KP_Up:
		pan_by(0, step);
		return true;
	case XKB_KEY_Down:
	case XKB_KEY_KP_Down:
		pan_by(0, -step);
		return true;
	case XKB_KEY_Page_Up:
	case XKB_KEY_KP_Page_Up:
	case XKB_KEY_BackSpace:
		viewer_step(-1);
		return true;
	case XKB_KEY_Page_Down:
	case XKB_KEY_KP_Page_Down:
	case XKB_KEY_space:
		viewer_step(1);
		return true;
	case XKB_KEY_Home:
		viewer_go(0);
		return true;
	case XKB_KEY_End:
		viewer_go(count() - 1);
		return true;
	case XKB_KEY_plus:
	case XKB_KEY_equal:
	case XKB_KEY_KP_Add:
		viewer_zoom(1.25);
		return true;
	case XKB_KEY_minus:
	case XKB_KEY_KP_Subtract:
		viewer_zoom(0.8);
		return true;
	case XKB_KEY_0:
	case XKB_KEY_KP_0:
		viewer_zoom(0);
		return true;
	case XKB_KEY_1:
	case XKB_KEY_KP_1:
		viewer_zoom(-1);
		return true;
	}
	return false;
}

static bool viewer_event(struct c8ui_widget *w, const struct c8ui_event *ev) {
	if (v.crop) {
		return crop_event(ev);
	}
	switch (ev->type) {
	case C8UI_EVENT_ENTER:
	case C8UI_EVENT_MOTION:
		if (v.panning) {
			pan_by(ev->x - v.pan_x, ev->y - v.pan_y);
			v.pan_x = ev->x;
			v.pan_y = ev->y;
			return true;
		}
		pointer_moved(ev->x, ev->y);
		return true;
	case C8UI_EVENT_LEAVE:
		v.arrows = false;
		c8ui_widget_queue_draw(w);
		return true;
	case C8UI_EVENT_PRESS:
		return press(ev);
	case C8UI_EVENT_RELEASE:
		return release(ev);
	case C8UI_EVENT_SCROLL:
		return scroll(ev);
	case C8UI_EVENT_SCROLL_STOP:
		v.hscroll = 0;
		v.scroll_used = false;
		return true;
	case C8UI_EVENT_TOUCH_DOWN:
		return touch_down(ev);
	case C8UI_EVENT_TOUCH_MOTION:
		if (v.touching && ev->touch_id == v.touch) {
			touch_motion(ev);
		}
		return true;
	case C8UI_EVENT_TOUCH_UP:
		if (v.touching && ev->touch_id == v.touch) {
			touch_up(ev);
		}
		return true;
	case C8UI_EVENT_TOUCH_CANCEL:
		v.touching = false;
		slide_back();
		return true;
	case C8UI_EVENT_SWIPE_BEGIN:
	case C8UI_EVENT_SWIPE_UPDATE:
	case C8UI_EVENT_SWIPE_END: {
		int step = 0;
		bool took = c8ui_tab_swipe(&v.swipe, ev, &step);
		if (step) {
			viewer_step(step);
		}
		return took;
	}
	case C8UI_EVENT_PINCH_BEGIN:
	case C8UI_EVENT_PINCH_UPDATE:
	case C8UI_EVENT_PINCH_END:
		return pinch(ev);
	case C8UI_EVENT_KEY:
		if (v.show) {
			if (ev->sym == XKB_KEY_Left || ev->sym == XKB_KEY_Right) {
				viewer_step(ev->sym == XKB_KEY_Left ? -1 : 1);
				return true;
			}
			if (ev->sym != XKB_KEY_Escape) {
				stop_show();
				return true;
			}
			return false;
		}
		return key(ev);
	default:
		return false;
	}
}

static bool viewer_tick(struct c8ui_widget *w, int64_t now) {
	if (v.animating) {
		if (v.anim_start < 0) {
			v.anim_start = now;
		}
		double t = (double)(now - v.anim_start) / FLIP_MS;
		if (t >= 1) {
			v.slide = 0;
			v.animating = false;
		} else {
			v.slide = v.anim_from * (1 - ease_out(t));
		}
	}
	if (v.fading) {
		if (v.fade_start < 0) {
			v.fade_start = now;
		}
		double t = (double)(now - v.fade_start) / FADE_MS;
		if (t >= 1) {
			v.fading = false;
			v.fade = 1;
			ph_picture_release(v.pics[FADE]);
			v.pics[FADE] = NULL;
		} else {
			v.fade = t;
		}
	}
	c8ui_widget_queue_draw(w);
	return v.animating || v.fading;
}

static void viewer_measure(struct c8ui_widget *w, double avail, double *width,
		double *height) {
	*width = avail;
	*height = 0;
}

static void viewer_layout(struct c8ui_widget *w) {
	double rw, rh;
	c8ui_widget_measure(v.ring, w->rect.width, &rw, &rh);
	c8ui_widget_layout(v.ring, (struct c8ui_rect){
		w->rect.x + (w->rect.width - rw) / 2,
		w->rect.y + (w->rect.height - rh) / 2, rw, rh });
	clamp_view();
}

static void viewer_destroy(struct c8ui_widget *w) {
	viewer_slideshow(false);
	drop_all();
	if (v.items) {
		g_ptr_array_unref(v.items);
		v.items = NULL;
	}
	if (v.settle_timer) {
		g_source_remove(v.settle_timer);
	}
	if (v.arrows_timer) {
		g_source_remove(v.arrows_timer);
	}
	if (v.tap_timer) {
		g_source_remove(v.tap_timer);
	}
	struct viewer_hooks hooks = v.hooks;
	memset(&v, 0, sizeof(v));
	v.hooks = hooks;
	v.index = v.ahead = v.arrow_down = -1;
	v.fit = true;
}

static const struct c8ui_widget_impl viewer_impl = {
	.name = "photos-viewer",
	.measure = viewer_measure,
	.layout = viewer_layout,
	.draw = viewer_draw,
	.event = viewer_event,
	.tick = viewer_tick,
	.destroy = viewer_destroy,
};

struct c8ui_widget *viewer_new(void) {
	v.w = c8ui_widget_new(&viewer_impl, NULL);
	v.w->focusable = true;
	v.w->fill = true;
	v.w->expand = true;
	v.ring = c8ui_ring_new(0);
	c8ui_widget_set_scheme(v.ring, C8UI_SCHEME_DARK);
	c8ui_widget_add(v.w, v.ring);
	return v.w;
}

void viewer_set_hooks(const struct viewer_hooks *hooks) {
	v.hooks = *hooks;
}

void viewer_set_items(GPtrArray *items, int index) {
	GPtrArray *old = v.items;
	v.items = items ? g_ptr_array_ref(items) : NULL;
	if (old) {
		g_ptr_array_unref(old);
	}
	v.index = count() ? CLAMP(index, 0, count() - 1) : -1;
	v.crop = false;
	v.grab = 0;
	v.fit = true;
	v.slide = 0;
	v.animating = false;
	if (v.show && count() < 2) {
		stop_show();
	}
	refresh_cache();
	c8ui_widget_queue_draw(v.w);
	if (v.hooks.changed) {
		v.hooks.changed();
	}
}

void viewer_show(GPtrArray *items, int index) {
	viewer_slideshow(false);
	viewer_set_items(items, index);
}

GPtrArray *viewer_items(void) {
	return v.items;
}

int viewer_index(void) {
	return v.index;
}

struct ph_entry *viewer_current(void) {
	return v.index >= 0 && v.index < count() ? v.items->pdata[v.index] :
		NULL;
}

struct ph_picture *viewer_picture(void) {
	return v.pics[CUR];
}

void viewer_reload(void) {
	ph_picture_release(v.pics[CUR]);
	v.pics[CUR] = NULL;
	v.fit = true;
	refresh_cache();
	c8ui_widget_queue_draw(v.w);
}

/* The slide show */

static int pick_ahead(void) {
	int n = count();
	if (n < 2) {
		return -1;
	}
	if (prefs.shuffle) {
		int i = g_random_int_range(0, n - 1);
		return i >= v.index ? i + 1 : i;
	}
	return (v.index + 1) % n;
}

static gboolean show_next(gpointer data) {
	int next = v.ahead >= 0 ? v.ahead : pick_ahead();
	if (next < 0) {
		v.show_timer = 0;
		stop_show();
		return G_SOURCE_REMOVE;
	}
	go(next, true);
	v.ahead = pick_ahead();
	refresh_cache(); // the new next one starts decoding
	return G_SOURCE_CONTINUE;
}

static void restart_show(void) {
	if (v.show_timer) {
		g_source_remove(v.show_timer);
	}
	v.ahead = pick_ahead();
	refresh_cache();
	v.show_timer = g_timeout_add(prefs.slide_seconds * 1000, show_next,
		NULL);
}

void viewer_slideshow(bool on) {
	if (on == v.show) {
		return;
	}
	v.show = on;
	if (v.show_timer) {
		g_source_remove(v.show_timer);
		v.show_timer = 0;
	}
	if (on) {
		v.fit = true;
		v.arrows = false;
		restart_show();
	} else {
		v.ahead = -1;
		if (v.w) {
			refresh_cache();
		}
	}
	if (v.w) {
		c8ui_widget_queue_draw(v.w);
	}
}

bool viewer_in_slideshow(void) {
	return v.show;
}
