import { UiSounds, UiCue } from "audio.slint";
import { Button } from "button.slint";
export component Lightbox inherits Rectangle {
in property <image> source;
in property <bool> software;
pure callback rasterize(image, float, length, length, length, length) -> image;
in-out property <float> zoom: root.fit;
in-out property <length> pan-x;
in-out property <length> pan-y;
callback closed();
out property <float> fit: min(1, min(max(1px, root.width - 40px) / max(1px, source.width * 1px), max(1px, root.height - 40px) / max(1px, source.height * 1px)));
property <length> image-width: root.source.width * 1px * root.zoom;
property <length> image-height: root.source.height * 1px * root.zoom;
property <length> image-x: (root.width - root.image-width) / 2 + root.pan-x;
property <length> image-y: (root.height - root.image-height) / 2 + root.pan-y;
property <length> limit-x: max(0px, (root.image-width - root.width + 40px) / 2);
property <length> limit-y: max(0px, (root.image-height - root.height + 40px) / 2);
background: #000000cc;
clip: true;
function constrain() {
root.pan-x = clamp(root.pan-x, -root.limit-x, root.limit-x);
root.pan-y = clamp(root.pan-y, -root.limit-y, root.limit-y);
}
changed width => { root.constrain(); }
changed height => { root.constrain(); }
changed fit => { root.zoom = clamp(root.zoom, root.fit, 8); root.constrain(); }
public function zoom-at(amount: float, x: length, y: length) {
let next = clamp(amount, root.fit, 8);
let ratio = next / root.zoom;
root.pan-x = (root.pan-x - x + root.width / 2) * ratio + x - root.width / 2;
root.pan-y = (root.pan-y - y + root.height / 2) * ratio + y - root.height / 2;
root.zoom = next;
root.constrain();
}
picture := Image {
x: root.software ? 0px : root.image-x;
y: root.software ? 0px : root.image-y;
width: root.software ? root.width : root.image-width;
height: root.software ? root.height : root.image-height;
source: root.software ? root.rasterize(root.source, root.zoom, root.pan-x, root.pan-y, root.width, root.height) : root.source;
image-fit: contain; image-rendering: smooth;
}
keyboard := FocusScope {
init => { root.zoom = clamp(root.zoom, root.fit, 8); self.focus(); }
key-pressed(event) => {
if (event.text == Key.Escape) { UiSounds.begin(); root.closed(); UiSounds.finish(UiCue.click); }
return accept;
}
key-released(event) => { return accept; }
}
property <length> last-x;
property <length> last-y;
property <bool> on-image;
property <bool> dragged;
touch := TouchArea {
mouse-cursor: root.zoom > root.fit ? grab : default;
pointer-event(event) => {
if (event.kind == PointerEventKind.down && event.button == PointerEventButton.left) {
root.on-image = self.mouse-x >= root.image-x && self.mouse-x <= root.image-x + root.image-width && self.mouse-y >= root.image-y && self.mouse-y <= root.image-y + root.image-height;
root.last-x = self.mouse-x; root.last-y = self.mouse-y; root.dragged = false;
}
}
moved => {
if (self.pressed && root.on-image && root.zoom > root.fit) {
let dx = self.mouse-x - root.last-x;
let dy = self.mouse-y - root.last-y;
root.pan-x += dx; root.pan-y += dy;
root.dragged = root.dragged || abs(dx) + abs(dy) > 1px;
root.last-x = self.mouse-x; root.last-y = self.mouse-y;
root.constrain();
}
}
clicked => { if (!root.on-image && !root.dragged) { root.closed(); } }
double-clicked => {
if (root.on-image) {
root.zoom-at(root.zoom > root.fit ? root.fit : 1, self.mouse-x, self.mouse-y);
}
}
scroll-event(event) => {
root.zoom-at(root.zoom * pow(1.002, event.delta-y / 1px), self.mouse-x, self.mouse-y);
return accept;
}
}
Button { sound: UiCue.tool; transparent-background: true; x: root.width - 48px; y: 56px; width: 32px; min-width: 0px; height: 28px; text: "X"; kind: "red"; clicked => { root.closed(); } }
}