@alulore/kumiko 0.15.1 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,102 @@
1
+ import { rubberBand } from "@ui-sdk/react/gesture";
2
+ export const FIT = { scale: 1, x: 0, y: 0 };
3
+ export const MIN_SCALE = 1;
4
+ export function clamp(value, low, high) {
5
+ return Math.min(high, Math.max(low, value));
6
+ }
7
+ /* The size the picture shows at fit: it never grows past its own size, and never leaves the frame. */
8
+ export function fitSize(natural, frame) {
9
+ if (natural.w <= 0 || natural.h <= 0 || frame.w <= 0 || frame.h <= 0) {
10
+ return { w: 0, h: 0 };
11
+ }
12
+ const ratio = Math.min(1, frame.w / natural.w, frame.h / natural.h);
13
+ return { w: natural.w * ratio, h: natural.h * ratio };
14
+ }
15
+ /* The zoom at which one picture pixel is one screen pixel (never below 1). */
16
+ export function actualScale(natural, fit) {
17
+ return fit.w > 0 ? Math.max(1, natural.w / fit.w) : 1;
18
+ }
19
+ /* The most a picture zooms: six times fit, or twice its actual size when that is more. */
20
+ export function maxScale(natural, fit) {
21
+ return Math.max(6, actualScale(natural, fit) * 2);
22
+ }
23
+ /* How far the picture may be moved from the centre on each axis: none while it is smaller than the frame. */
24
+ export function panLimits(fit, frame, scale) {
25
+ return {
26
+ x: Math.max(0, (fit.w * scale - frame.w) / 2),
27
+ y: Math.max(0, (fit.h * scale - frame.h) / 2)
28
+ };
29
+ }
30
+ export function clampPan(view, limits) {
31
+ return {
32
+ scale: view.scale,
33
+ x: clamp(view.x, -limits.x, limits.x),
34
+ y: clamp(view.y, -limits.y, limits.y)
35
+ };
36
+ }
37
+ /* A position resisting the further it goes past its limit, like the edge of a scroll view. */
38
+ export function softLimit(value, limit, dimension) {
39
+ const over = Math.abs(value) - limit;
40
+ if (over <= 0) {
41
+ return value;
42
+ }
43
+ return Math.sign(value) * (limit + rubberBand(over, dimension));
44
+ }
45
+ export function softPan(view, limits, frame) {
46
+ return {
47
+ scale: view.scale,
48
+ x: softLimit(view.x, limits.x, frame.w),
49
+ y: softLimit(view.y, limits.y, frame.h)
50
+ };
51
+ }
52
+ /* How far past the limit a position is, signed in the direction it went past. */
53
+ export function overshoot(value, limit) {
54
+ if (Math.abs(value) <= limit) {
55
+ return 0;
56
+ }
57
+ return value - Math.sign(value) * limit;
58
+ }
59
+ /*
60
+ * The view after zooming to a scale. With the point anchor, the picture point under the pointer
61
+ * stays under it. With the centre anchor, the picture grows from the middle of the frame.
62
+ */
63
+ export function zoomTo(view, scale, at, anchor) {
64
+ const ratio = scale / view.scale;
65
+ if (anchor === "center") {
66
+ return { scale, x: view.x * ratio, y: view.y * ratio };
67
+ }
68
+ return { scale, x: at.x - (at.x - view.x) * ratio, y: at.y - (at.y - view.y) * ratio };
69
+ }
70
+ /* One step of a wheel or a key: a factor on the scale, kept in the allowed range. */
71
+ export function factorScale(scale, factor, max) {
72
+ return clamp(scale * factor, MIN_SCALE, max);
73
+ }
74
+ /* The factor a wheel turn makes. A pinch on a trackpad arrives as a ctrl wheel with small deltas. */
75
+ export function wheelFactor(deltaY, pinch) {
76
+ return Math.exp(-deltaY * (pinch ? 0.01 : 0.0015));
77
+ }
78
+ /* Where a double tap goes: back to fit when zoomed, else to a target scale. */
79
+ export function doubleTapScale(scale, target) {
80
+ return scale > 1.02 ? 1 : target;
81
+ }
82
+ /* Pinch: the view after two fingers moved from where they started. */
83
+ export function pinchView(start, startCentre, startGap, centre, gap, anchor, max) {
84
+ const scale = clamp(start.scale * (gap / Math.max(1, startGap)), MIN_SCALE * 0.6, max * 1.3);
85
+ const grown = zoomTo(start, scale, startCentre, anchor);
86
+ if (anchor === "center") {
87
+ return grown;
88
+ }
89
+ return { scale, x: grown.x + centre.x - startCentre.x, y: grown.y + centre.y - startCentre.y };
90
+ }
91
+ /* One frame of coasting: the position and speed after dt ms, slowing by a rate per ms. */
92
+ export function coast(position, velocity, dt, rate) {
93
+ const decay = rate ** dt;
94
+ return {
95
+ position: position + (velocity * (decay - 1)) / Math.log(rate),
96
+ velocity: velocity * decay
97
+ };
98
+ }
99
+ /* Whether a release past an edge by this much, or a quick flick that way, turns the page. */
100
+ export function shouldPage(overshootPx, velocity, threshold) {
101
+ return Math.abs(overshootPx) > threshold || (overshootPx !== 0 && Math.abs(velocity) > 0.5);
102
+ }
@@ -0,0 +1,2 @@
1
+ export { Titlebar, TitlebarButton, TitlebarWindowControls, useWindowActive, type TitlebarButtonProps, type TitlebarProps, type WindowControls, type WindowControlsProps } from "./titlebar";
2
+ export { titlebarWords, type TitlebarButtonLook, type TitlebarDivider, type TitlebarEdge, type TitlebarHeight, type TitlebarLayout, type TitlebarLooks } from "./titlebar-looks";
@@ -0,0 +1,2 @@
1
+ export { Titlebar, TitlebarButton, TitlebarWindowControls, useWindowActive } from "./titlebar";
2
+ export { titlebarWords } from "./titlebar-looks";
@@ -0,0 +1,53 @@
1
+ /** Where the brand sits: only the mark at the start and the name centred in the bar, or the name next to the mark. */
2
+ export type TitlebarLayout = "center" | "start";
3
+ /** The bar's height through its token: compact, regular or tall. */
4
+ export type TitlebarHeight = "compact" | "regular" | "tall";
5
+ /** The bar's bottom edge: a soft shadow, a line, or nothing. */
6
+ export type TitlebarEdge = "shadow" | "line" | "none";
7
+ /** A tool button's look: a rounded square wash, a round wash, or ghost (no wash, only the colour changes). */
8
+ export type TitlebarButtonLook = "square" | "round" | "ghost";
9
+ /** The divider between the brand and the middle: a small diamond, a thin line, or none. */
10
+ export type TitlebarDivider = "diamond" | "line" | "none";
11
+ /** The looks of a bar. Each is a prop, or a word on any container such as `data-kumiko-titlebar="height-compact edge-none"`. */
12
+ export interface TitlebarLooks {
13
+ /**
14
+ * Word: `layout-center` or `layout-start`.
15
+ * @defaultValue "center"
16
+ */
17
+ layout?: TitlebarLayout;
18
+ /**
19
+ * Word: `height-compact`, `height-regular` or `height-tall`. An app that feeds the height to its window sets
20
+ * `--kumiko-titlebar-height` itself and leaves this out.
21
+ * @defaultValue "regular"
22
+ */
23
+ height?: TitlebarHeight;
24
+ /**
25
+ * Word: `edge-shadow`, `edge-line` or `edge-none`. The edge's room is kept either way, so the height never changes.
26
+ * @defaultValue "shadow"
27
+ */
28
+ edge?: TitlebarEdge;
29
+ /**
30
+ * The look of the tool buttons. Word: `look-square`, `look-round` or `look-ghost`.
31
+ * @defaultValue "square"
32
+ */
33
+ look?: TitlebarButtonLook;
34
+ /**
35
+ * Tool button icons tilt on hover (and a gear spins) as well as the button growing. Word: `tilt` or `no-tilt`.
36
+ * @defaultValue false
37
+ */
38
+ tilt?: boolean;
39
+ /**
40
+ * Word: `divider-diamond`, `divider-line` or `divider-none`.
41
+ * @defaultValue "diamond"
42
+ */
43
+ divider?: TitlebarDivider;
44
+ /**
45
+ * Fade the bar's contents while the window is not focused. Word: `dim-inactive` or `no-dim-inactive`.
46
+ * @defaultValue true
47
+ */
48
+ dimInactive?: boolean;
49
+ }
50
+ /** The words the looks become, or nothing when no look is set. A look left out sets no word, so a container's word can pick. */
51
+ export declare function titlebarWords(looks: TitlebarLooks): string | undefined;
52
+ /** A tool button's tilt as its data attribute: on, off, or nothing so the bar's word decides. */
53
+ export declare function tiltAttribute(tilt: boolean | undefined): "on" | "off" | undefined;
@@ -0,0 +1,26 @@
1
+ function onOff(value, word) {
2
+ if (value === undefined) {
3
+ return undefined;
4
+ }
5
+ return value ? word : `no-${word}`;
6
+ }
7
+ /** The words the looks become, or nothing when no look is set. A look left out sets no word, so a container's word can pick. */
8
+ export function titlebarWords(looks) {
9
+ const words = [
10
+ looks.layout && `layout-${looks.layout}`,
11
+ looks.height && `height-${looks.height}`,
12
+ looks.edge && `edge-${looks.edge}`,
13
+ looks.look && `look-${looks.look}`,
14
+ onOff(looks.tilt, "tilt"),
15
+ looks.divider && `divider-${looks.divider}`,
16
+ onOff(looks.dimInactive, "dim-inactive")
17
+ ].filter(Boolean);
18
+ return words.length > 0 ? words.join(" ") : undefined;
19
+ }
20
+ /** A tool button's tilt as its data attribute: on, off, or nothing so the bar's word decides. */
21
+ export function tiltAttribute(tilt) {
22
+ if (tilt === undefined) {
23
+ return undefined;
24
+ }
25
+ return tilt ? "on" : "off";
26
+ }