import { Theme, ActionButton } from "theme.slint"; import { AudioSlider } from "controls.slint"; export component AvatarCropper inherits Rectangle { in property avatar-source; in property source-width: 1; in property source-height: 1; in property offset-x; in property offset-y; in-out property zoom: 1; in property error; callback crop-moved(float, float); callback zoom-changed(float); callback confirm(); callback cancel(); private property frame-size: Math.min(320px, Math.max(220px, root.height - 350px)); private property cover-scale: Math.max(root.frame-size / 1px / Math.max(root.source-width, 1), root.frame-size / 1px / Math.max(root.source-height, 1)) * root.zoom; background: Theme.overlay; TouchArea { } keyboard := FocusScope { init => { self.focus(); } key-pressed(event) => { if event.text == Key.Escape { root.cancel(); return accept; } if event.text == "+" || event.text == "=" { root.set-zoom(root.zoom + 0.1); return accept; } if event.text == "-" { root.set-zoom(root.zoom - 0.1); return accept; } return reject; } Rectangle { width: 440px; height: Math.min(620px, root.height - 24px); x: (root.width - self.width) / 2; y: (root.height - self.height) / 2; background: Theme.bg-surface; border-radius: 12px; border-width: 1px; border-color: Theme.border; VerticalLayout { padding: 18px; spacing: 8px; alignment: start; HorizontalLayout { Text { text: @tr("Crop photo"); color: Theme.text-primary; font-size: Theme.font-size + 5px; font-weight: 600; horizontal-stretch: 1; vertical-alignment: center; } ActionButton { text: @tr("Cancel"); icon: "x"; width: 30px; quiet: true; clicked => { root.cancel(); } } } Text { text: @tr("Drag the photo and adjust the zoom."); color: Theme.text-muted; font-size: Math.max(Theme.font-size - 3px, 11px); } Rectangle { height: 4px; } HorizontalLayout { Rectangle { horizontal-stretch: 1; } frame := Rectangle { width: root.frame-size; height: root.frame-size; clip: true; background: Theme.bg-surface; accessible-role: image; accessible-label: @tr("Photo crop area. Drag the image to adjust the crop."); Image { source: root.avatar-source; width: root.source-width * root.cover-scale * 1px; height: root.source-height * root.cover-scale * 1px; x: (frame.width - self.width) / 2 + root.offset-x * frame.width; y: (frame.height - self.height) / 2 + root.offset-y * frame.height; image-fit: fill; } // The mask makes the final circular avatar explicit, independently // of renderer support for rounded clipping of bitmap images. Path { width: parent.width; height: parent.height; viewbox-width: 320; viewbox-height: 320; fill: Theme.bg-surface; fill-rule: evenodd; commands: "M0 0H320V320H0Z M320 160 A160 160 0 1 0 0 160 A160 160 0 1 0 320 160Z"; } Rectangle { width: parent.width; height: parent.height; border-radius: parent.width / 2; border-width: 1px; border-color: Theme.border; background: transparent; } drag := TouchArea { property start-x; property start-y; pointer-event(event) => { if event.button == PointerEventButton.left && event.kind == PointerEventKind.down { self.start-x = root.offset-x; self.start-y = root.offset-y; keyboard.focus(); } } moved => { if self.pressed { root.crop-moved(self.start-x + (self.mouse-x - self.pressed-x) / frame.width, self.start-y + (self.mouse-y - self.pressed-y) / frame.height); } } } } Rectangle { horizontal-stretch: 1; } } Rectangle { height: 2px; } HorizontalLayout { spacing: 10px; ActionButton { text: "-"; width: 32px; clicked => { root.set-zoom(root.zoom - 0.1); } } AudioSlider { minimum: 1; maximum: 3; value <=> root.zoom; changed(value) => { root.zoom-changed(value); } } ActionButton { text: "+"; width: 32px; clicked => { root.set-zoom(root.zoom + 0.1); } } } HorizontalLayout { Text { text: @tr("Zoom {}%", Math.round(root.zoom * 100)); color: Theme.text-muted; font-size: Math.max(Theme.font-size - 4px, 11px); vertical-alignment: center; horizontal-stretch: 1; } ActionButton { text: @tr("Reset crop"); quiet: true; clicked => { root.crop-moved(0,0); root.set-zoom(1); } } } if root.error != "" : Text { text: root.error; color: Theme.danger; font-size: Math.max(Theme.font-size - 3px, 11px); wrap: word-wrap; } Rectangle { vertical-stretch: 1; } HorizontalLayout { spacing: 9px; ActionButton { text: @tr("Cancel"); quiet: true; clicked => { root.cancel(); } } Rectangle { horizontal-stretch: 1; } ActionButton { text: @tr("Use photo"); active: true; clicked => { root.confirm(); } } } } } } function set-zoom(value: float) { root.zoom = Math.clamp(value, 1, 3); root.zoom-changed(root.zoom); } }