@web-canvas-ui/react 0.0.0-stage → 0.1.1

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.
Files changed (98) hide show
  1. package/Canvas.tsx +142 -0
  2. package/README.md +988 -2
  3. package/__tests__/pure-logic.test.ts +153 -0
  4. package/__tests__/scene-and-node.test.ts +131 -0
  5. package/__tests__/scratch-space.test.ts +30 -0
  6. package/__tests__/textarea-caret.test.ts +96 -0
  7. package/__tests__/utils.test.ts +114 -0
  8. package/canvas-handle.ts +114 -0
  9. package/components/Button/button.ts +57 -0
  10. package/components/Button/index.ts +3 -0
  11. package/components/Button/logic.ts +11 -0
  12. package/components/CanvasView/canvasView.ts +103 -0
  13. package/components/CanvasView/index.ts +7 -0
  14. package/components/Carousel/carousel.ts +107 -0
  15. package/components/Carousel/index.ts +3 -0
  16. package/components/Carousel/logic.ts +36 -0
  17. package/components/Checkbox/checkbox.ts +14 -0
  18. package/components/Checkbox/index.ts +2 -0
  19. package/components/ChoiceGroup/choiceGroup.ts +172 -0
  20. package/components/ChoiceGroup/index.ts +3 -0
  21. package/components/ChoiceGroup/logic.ts +14 -0
  22. package/components/Circle/circle.ts +31 -0
  23. package/components/Circle/index.ts +2 -0
  24. package/components/Collapse/collapse.ts +69 -0
  25. package/components/Collapse/index.ts +3 -0
  26. package/components/Collapse/logic.ts +14 -0
  27. package/components/CustomDraw/customDraw.ts +50 -0
  28. package/components/CustomDraw/index.ts +7 -0
  29. package/components/DatePicker/datePicker.ts +70 -0
  30. package/components/DatePicker/index.ts +2 -0
  31. package/components/Dialog/dialog.ts +102 -0
  32. package/components/Dialog/index.ts +2 -0
  33. package/components/Dropdown/dropdown.ts +125 -0
  34. package/components/Dropdown/index.ts +3 -0
  35. package/components/Dropdown/logic.ts +29 -0
  36. package/components/Grid/grid.ts +15 -0
  37. package/components/Grid/index.ts +2 -0
  38. package/components/GridItem/gridItem.ts +11 -0
  39. package/components/GridItem/index.ts +2 -0
  40. package/components/Group/group.ts +8 -0
  41. package/components/Group/index.ts +2 -0
  42. package/components/Icon/icon.ts +669 -0
  43. package/components/Icon/index.ts +2 -0
  44. package/components/Image/image.ts +69 -0
  45. package/components/Image/index.ts +2 -0
  46. package/components/Input/index.ts +2 -0
  47. package/components/Input/input.ts +139 -0
  48. package/components/Progress/index.ts +2 -0
  49. package/components/Progress/progress.ts +59 -0
  50. package/components/Radio/index.ts +2 -0
  51. package/components/Radio/radio.ts +14 -0
  52. package/components/Rect/index.ts +2 -0
  53. package/components/Rect/rect.ts +35 -0
  54. package/components/Slider/index.ts +3 -0
  55. package/components/Slider/logic.ts +19 -0
  56. package/components/Slider/slider.ts +58 -0
  57. package/components/Switch/index.ts +3 -0
  58. package/components/Switch/logic.ts +8 -0
  59. package/components/Switch/switch.ts +37 -0
  60. package/components/Tabs/index.ts +3 -0
  61. package/components/Tabs/logic.ts +14 -0
  62. package/components/Tabs/tabs.ts +78 -0
  63. package/components/Text/index.ts +2 -0
  64. package/components/Text/text.ts +95 -0
  65. package/components/Textarea/index.ts +2 -0
  66. package/components/Textarea/textarea.ts +16 -0
  67. package/components/Upload/index.ts +2 -0
  68. package/components/Upload/upload.ts +66 -0
  69. package/components/draw-utils.ts +230 -0
  70. package/components/shared.ts +15 -0
  71. package/components/size.ts +20 -0
  72. package/components/types.ts +37 -0
  73. package/components.ts +98 -0
  74. package/engine/__tests__/carousel.test.ts +218 -0
  75. package/engine/__tests__/hit-test.test.ts +282 -0
  76. package/engine/__tests__/layout.test.ts +474 -0
  77. package/engine/carousel.ts +150 -0
  78. package/engine/events.ts +73 -0
  79. package/engine/hit-test.ts +321 -0
  80. package/engine/images.ts +41 -0
  81. package/engine/interaction.ts +442 -0
  82. package/engine/layout.ts +355 -0
  83. package/engine/nodes.ts +166 -0
  84. package/engine/state.ts +75 -0
  85. package/engine/text-controls.ts +122 -0
  86. package/engine/transform.ts +61 -0
  87. package/engine/vnodes.ts +204 -0
  88. package/engine.ts +162 -0
  89. package/image-assets.ts +37 -0
  90. package/index.ts +78 -0
  91. package/input-bridge.ts +285 -0
  92. package/package.json +27 -4
  93. package/renderer.ts +303 -0
  94. package/scene.ts +122 -0
  95. package/theme.css +327 -0
  96. package/theme.scss +234 -0
  97. package/types.ts +256 -0
  98. package/web-canvas-ui-react-0.1.1.tgz +0 -0
@@ -0,0 +1,69 @@
1
+ import type { TransformProps } from '../../types';
2
+ import { createComponent } from '../shared';
3
+ import { getCanvasImage } from '../../image-assets';
4
+ import { number } from '../draw-utils';
5
+
6
+ export interface ImageProps extends TransformProps {
7
+ src: string;
8
+ width: number;
9
+ height: number;
10
+ objectFit?: 'contain' | 'cover' | 'fill' | 'none' | 'scale-down';
11
+ onError?: (error: Error) => void;
12
+ }
13
+
14
+ export const Image = createComponent<ImageProps>('image');
15
+
16
+ export function drawImage(context: CanvasRenderingContext2D, props: ImageProps): void {
17
+ const src = props.src;
18
+ if (typeof src !== 'string' || !src) return;
19
+ const image = getCanvasImage(src);
20
+ if (!image) return;
21
+ const width = number(props.width, image.naturalWidth);
22
+ const height = number(props.height, image.naturalHeight);
23
+ const fit = props.objectFit ?? 'contain';
24
+ context.save();
25
+ context.beginPath();
26
+ context.rect(0, 0, width, height);
27
+ context.clip();
28
+ if (fit === 'fill') {
29
+ context.drawImage(image, 0, 0, width, height);
30
+ context.restore();
31
+ return;
32
+ }
33
+
34
+ const scale =
35
+ fit === 'cover'
36
+ ? Math.max(width / image.naturalWidth, height / image.naturalHeight)
37
+ : fit === 'none'
38
+ ? 1
39
+ : fit === 'scale-down'
40
+ ? Math.min(1, width / image.naturalWidth, height / image.naturalHeight)
41
+ : Math.min(width / image.naturalWidth, height / image.naturalHeight);
42
+ const drawWidth = image.naturalWidth * scale;
43
+ const drawHeight = image.naturalHeight * scale;
44
+ if (fit === 'cover') {
45
+ const sourceWidth = width / scale;
46
+ const sourceHeight = height / scale;
47
+ context.drawImage(
48
+ image,
49
+ (image.naturalWidth - sourceWidth) / 2,
50
+ (image.naturalHeight - sourceHeight) / 2,
51
+ sourceWidth,
52
+ sourceHeight,
53
+ 0,
54
+ 0,
55
+ width,
56
+ height,
57
+ );
58
+ context.restore();
59
+ return;
60
+ }
61
+ context.drawImage(
62
+ image,
63
+ (width - drawWidth) / 2,
64
+ (height - drawHeight) / 2,
65
+ drawWidth,
66
+ drawHeight,
67
+ );
68
+ context.restore();
69
+ }
@@ -0,0 +1,2 @@
1
+ export { Image, drawImage } from './image';
2
+ export type { ImageProps } from './image';
@@ -0,0 +1,2 @@
1
+ export { Input, drawInput } from './input';
2
+ export type { InputProps, InputDrawProps } from './input';
@@ -0,0 +1,139 @@
1
+ import type { TransformProps } from '../../types';
2
+ import { number, string, themeColor, verticalScrollbarWidth, wrapText } from '../draw-utils';
3
+ import { createComponent } from '../shared';
4
+
5
+ export interface InputProps extends TransformProps {
6
+ width?: number;
7
+ height?: number;
8
+ value?: string;
9
+ defaultValue?: string;
10
+ placeholder?: string;
11
+ fontSize?: number;
12
+ fontFamily?: string;
13
+ color?: string;
14
+ bgColor?: string;
15
+ borderColor?: string;
16
+ focusBorderColor?: string;
17
+ borderRadius?: number;
18
+ padding?: number;
19
+ disabled?: boolean;
20
+ maxLength?: number;
21
+ type?: 'text' | 'password' | 'number';
22
+ onChange?: (value: string) => void;
23
+ onFocus?: () => void;
24
+ onBlur?: () => void;
25
+ }
26
+
27
+ export const Input = createComponent<InputProps>('input');
28
+
29
+ /** Public props plus the engine-injected caret/scroll state consumed by the draw layer. */
30
+ export type InputDrawProps = InputProps & {
31
+ focused?: boolean;
32
+ cursorPos?: number;
33
+ _scrollTop?: number;
34
+ _scrollHeight?: number;
35
+ };
36
+
37
+ export function drawInput(
38
+ context: CanvasRenderingContext2D,
39
+ props: InputDrawProps,
40
+ multiline = false,
41
+ ): void {
42
+ const width = number(props.width, 200);
43
+ const height = number(props.height, 36);
44
+ const radius = Math.max(0, Math.min(number(props.borderRadius, 4), width / 2, height / 2));
45
+ const padding = number(props.padding, 8);
46
+ const value = String(props.value ?? '');
47
+ const placeholder = String(props.placeholder ?? '');
48
+ const text = props.type === 'password' ? '•'.repeat(value.length) : value;
49
+
50
+ context.beginPath();
51
+ context.roundRect(0, 0, width, height, radius);
52
+ context.fillStyle = string(
53
+ props.bgColor,
54
+ themeColor(context, '--canvas-ui-input-background', '#fff'),
55
+ );
56
+ context.fill();
57
+ context.strokeStyle = props.focused
58
+ ? string(
59
+ props.focusBorderColor,
60
+ themeColor(context, '--canvas-ui-input-focus-border', '#1677ff'),
61
+ )
62
+ : string(props.borderColor, themeColor(context, '--canvas-ui-input-border', '#d9d9d9'));
63
+ context.lineWidth = 1;
64
+ context.stroke();
65
+
66
+ context.save();
67
+ context.beginPath();
68
+ context.rect(0, 0, width, height);
69
+ context.clip();
70
+ const fontSize = number(props.fontSize, 14);
71
+ context.font = `${fontSize}px ${string(props.fontFamily, 'sans-serif')}`;
72
+ context.textBaseline = multiline ? 'top' : 'middle';
73
+ context.fillStyle = value
74
+ ? string(props.color, themeColor(context, '--canvas-ui-input-color', '#333'))
75
+ : themeColor(context, '--canvas-ui-input-placeholder-color', '#999');
76
+ const lineHeight = fontSize * 1.35;
77
+ // The text being drawn is really the transparent DOM element's text, and that element wraps at its
78
+ // own content width: as soon as the value overflows, its vertical scrollbar takes room away from
79
+ // the text. Wrapping a few pixels wider here breaks the same paragraph at other words, and the
80
+ // caret - resolved from a character index the element reported - then lands on a line the reader
81
+ // never clicked. Reserve the gutter the element reserves to keep the two wrappings identical.
82
+ const gutter =
83
+ multiline && number(props._scrollHeight, 0) > height ? verticalScrollbarWidth() : 0;
84
+ const contentWidth = Math.max(1, width - padding * 2 - gutter);
85
+ const lines = multiline ? wrapText(context, text || placeholder, contentWidth) : [];
86
+ const contentHeight = multiline
87
+ ? Math.max(number(props._scrollHeight, 0), lines.length * lineHeight + padding * 2)
88
+ : 0;
89
+ const scrollTop = multiline
90
+ ? Math.max(0, Math.min(contentHeight - height, number(props._scrollTop, 0)))
91
+ : 0;
92
+ if (multiline) {
93
+ lines.forEach((line, index) => {
94
+ context.fillText(line, padding, padding + index * lineHeight - scrollTop);
95
+ });
96
+ if (contentHeight > height)
97
+ drawTextareaScrollbar(context, width, height, contentHeight, scrollTop);
98
+ } else {
99
+ context.fillText(text || placeholder, padding, height / 2);
100
+ }
101
+
102
+ if (props.focused && !props.disabled) {
103
+ const cursorPosition = number(props.cursorPos, value.length);
104
+ const cursorLines = multiline
105
+ ? wrapText(context, text.slice(0, cursorPosition), contentWidth)
106
+ : text.slice(0, cursorPosition).split('\n');
107
+ const cursorWidth = context.measureText(cursorLines[cursorLines.length - 1] ?? '').width;
108
+ const cursorY = multiline
109
+ ? padding + (cursorLines.length - 1) * lineHeight - scrollTop
110
+ : (height - fontSize) / 2;
111
+ context.fillStyle = string(
112
+ props.color,
113
+ themeColor(context, '--canvas-ui-input-color', '#333'),
114
+ );
115
+ if (!multiline || (cursorY + fontSize >= 0 && cursorY <= height)) {
116
+ context.fillRect(padding + cursorWidth, cursorY, 1, fontSize);
117
+ }
118
+ }
119
+ context.restore();
120
+ }
121
+
122
+ function drawTextareaScrollbar(
123
+ context: CanvasRenderingContext2D,
124
+ width: number,
125
+ height: number,
126
+ contentHeight: number,
127
+ scrollTop: number,
128
+ ): void {
129
+ const trackWidth = 5;
130
+ const thumbHeight = Math.min(height, Math.max(16, (height * height) / contentHeight));
131
+ const maxScroll = contentHeight - height;
132
+ const thumbY = maxScroll > 0 ? ((height - thumbHeight) * scrollTop) / maxScroll : 0;
133
+ context.fillStyle = 'rgba(0, 0, 0, 0.08)';
134
+ context.fillRect(width - trackWidth, 0, trackWidth, height);
135
+ context.fillStyle = 'rgba(0, 0, 0, 0.35)';
136
+ context.beginPath();
137
+ context.roundRect(width - trackWidth + 1, thumbY, trackWidth - 2, thumbHeight, 2);
138
+ context.fill();
139
+ }
@@ -0,0 +1,2 @@
1
+ export { Progress, drawProgress } from './progress';
2
+ export type { ProgressProps } from './progress';
@@ -0,0 +1,59 @@
1
+ import type { TransformProps } from '../../types';
2
+ import { number, string, themeColor } from '../draw-utils';
3
+ import { createComponent } from '../shared';
4
+
5
+ export interface ProgressProps extends TransformProps {
6
+ value: number;
7
+ min?: number;
8
+ max?: number;
9
+ width?: number;
10
+ height?: number;
11
+ color?: string;
12
+ trackColor?: string;
13
+ textColor?: string;
14
+ borderRadius?: number;
15
+ showText?: boolean;
16
+ }
17
+
18
+ export const Progress = createComponent<ProgressProps>('progress');
19
+
20
+ export function drawProgress(context: CanvasRenderingContext2D, props: ProgressProps): void {
21
+ const width = number(props.width, 200);
22
+ const height = number(props.height, 12);
23
+ const min = number(props.min, 0);
24
+ const max = number(props.max, 100);
25
+ const value = Math.max(min, Math.min(max, number(props.value, min)));
26
+ const ratio = max > min ? (value - min) / (max - min) : 0;
27
+ const radius = Math.max(
28
+ 0,
29
+ Math.min(number(props.borderRadius, height / 2), height / 2, width / 2),
30
+ );
31
+ context.beginPath();
32
+ context.roundRect(0, 0, width, height, radius);
33
+ context.fillStyle = string(
34
+ props.trackColor,
35
+ themeColor(context, '--canvas-ui-progress-track', '#f0f0f0'),
36
+ );
37
+ context.fill();
38
+ if (ratio > 0) {
39
+ context.save();
40
+ context.beginPath();
41
+ context.roundRect(0, 0, width, height, radius);
42
+ context.clip();
43
+ context.fillStyle = string(
44
+ props.color,
45
+ themeColor(context, '--canvas-ui-progress-active', '#1677ff'),
46
+ );
47
+ context.fillRect(0, 0, width * ratio, height);
48
+ context.restore();
49
+ }
50
+ if (props.showText) {
51
+ context.font = `${Math.max(10, height)}px sans-serif`;
52
+ context.textBaseline = 'middle';
53
+ context.fillStyle = string(
54
+ props.textColor,
55
+ themeColor(context, '--canvas-ui-progress-text', '#333'),
56
+ );
57
+ context.fillText(`${Math.round(ratio * 100)}%`, width + 8, height / 2);
58
+ }
59
+ }
@@ -0,0 +1,2 @@
1
+ export { Radio, drawRadio } from './radio';
2
+ export type { RadioProps } from './radio';
@@ -0,0 +1,14 @@
1
+ import { createComponent } from '../shared';
2
+ import { drawChoiceGroup, type ChoiceGroupProps } from '../ChoiceGroup';
3
+
4
+ export interface RadioProps extends ChoiceGroupProps {
5
+ value?: string;
6
+ defaultValue?: string;
7
+ onChange?: (value: string) => void;
8
+ }
9
+
10
+ export const Radio = createComponent<RadioProps>('radio');
11
+
12
+ export function drawRadio(context: CanvasRenderingContext2D, props: RadioProps): void {
13
+ drawChoiceGroup(context, props, 'radio');
14
+ }
@@ -0,0 +1,2 @@
1
+ export { Rect, drawRect } from './rect';
2
+ export type { RectProps } from './rect';
@@ -0,0 +1,35 @@
1
+ import type { TransformProps } from '../../types';
2
+ import { number, string, themeColor } from '../draw-utils';
3
+ import { createComponent } from '../shared';
4
+
5
+ export interface RectProps extends TransformProps {
6
+ width?: number;
7
+ height?: number;
8
+ color?: string;
9
+ radius?: number;
10
+ stroke?: string;
11
+ strokeWidth?: number;
12
+ }
13
+
14
+ export const Rect = createComponent<RectProps>('rect');
15
+
16
+ export function drawRect(context: CanvasRenderingContext2D, props: RectProps): void {
17
+ const width = number(props.width, 100);
18
+ const height = number(props.height, 40);
19
+ const radius = Math.max(0, Math.min(number(props.radius, 0), width / 2, height / 2));
20
+ context.beginPath();
21
+ context.roundRect(0, 0, width, height, radius);
22
+ context.fillStyle = string(
23
+ props.color,
24
+ themeColor(context, '--canvas-ui-rect-background', 'transparent'),
25
+ );
26
+ context.fill();
27
+ if (props.stroke) {
28
+ context.strokeStyle = string(
29
+ props.stroke,
30
+ themeColor(context, '--canvas-ui-rect-border', '#000'),
31
+ );
32
+ context.lineWidth = number(props.strokeWidth, 1);
33
+ context.stroke();
34
+ }
35
+ }
@@ -0,0 +1,3 @@
1
+ export { Slider, drawSlider } from './slider';
2
+ export type { SliderProps } from './slider';
3
+ export { sliderValueAt } from './logic';
@@ -0,0 +1,19 @@
1
+ import { number } from '../draw-utils';
2
+
3
+ export function sliderValueAt(
4
+ props: Record<string, unknown>,
5
+ width: number,
6
+ localX: number,
7
+ ): number | undefined {
8
+ const thumbRadius = Math.min(8, number(props.height, 32) / 3);
9
+ const min = number(props.min, 0);
10
+ const max = number(props.max, 100);
11
+ if (max <= min) return undefined;
12
+ const step = Math.max(Number.EPSILON, number(props.step, 1));
13
+ const ratio = Math.max(
14
+ 0,
15
+ Math.min(1, (localX - thumbRadius) / Math.max(1, width - thumbRadius * 2)),
16
+ );
17
+ const rawValue = min + ratio * (max - min);
18
+ return Math.max(min, Math.min(max, min + Math.round((rawValue - min) / step) * step));
19
+ }
@@ -0,0 +1,58 @@
1
+ import type { TransformProps } from '../../types';
2
+ import { number, string, themeColor } from '../draw-utils';
3
+ import { createComponent } from '../shared';
4
+
5
+ export interface SliderProps extends TransformProps {
6
+ width?: number;
7
+ height?: number;
8
+ min?: number;
9
+ max?: number;
10
+ step?: number;
11
+ value?: number;
12
+ defaultValue?: number;
13
+ disabled?: boolean;
14
+ trackColor?: string;
15
+ activeColor?: string;
16
+ thumbColor?: string;
17
+ onChange?: (value: number) => void;
18
+ }
19
+
20
+ export const Slider = createComponent<SliderProps>('slider');
21
+
22
+ export function drawSlider(context: CanvasRenderingContext2D, props: SliderProps): void {
23
+ const width = number(props.width, 160);
24
+ const height = number(props.height, 32);
25
+ const min = number(props.min, 0);
26
+ const max = number(props.max, 100);
27
+ const value = Math.max(min, Math.min(max, number(props.value, min)));
28
+ const ratio = max === min ? 0 : (value - min) / (max - min);
29
+ const centerY = height / 2;
30
+ const radius = Math.min(8, height / 3);
31
+ const thumbX = radius + ratio * Math.max(0, width - radius * 2);
32
+ context.beginPath();
33
+ context.roundRect(radius, centerY - 3, Math.max(0, width - radius * 2), 6, 3);
34
+ context.fillStyle = string(
35
+ props.trackColor,
36
+ themeColor(context, '--canvas-ui-slider-track', '#f0f0f0'),
37
+ );
38
+ context.fill();
39
+ context.beginPath();
40
+ context.roundRect(radius, centerY - 3, Math.max(0, thumbX - radius), 6, 3);
41
+ context.fillStyle = string(
42
+ props.activeColor,
43
+ themeColor(context, '--canvas-ui-slider-active', '#1677ff'),
44
+ );
45
+ context.fill();
46
+ context.beginPath();
47
+ context.arc(thumbX, centerY, radius, 0, Math.PI * 2);
48
+ context.fillStyle = string(
49
+ props.thumbColor,
50
+ themeColor(context, '--canvas-ui-slider-thumb', '#fff'),
51
+ );
52
+ context.fill();
53
+ context.strokeStyle = string(
54
+ props.activeColor,
55
+ themeColor(context, '--canvas-ui-slider-active', '#1677ff'),
56
+ );
57
+ context.stroke();
58
+ }
@@ -0,0 +1,3 @@
1
+ export { Switch, drawSwitch } from './switch';
2
+ export type { SwitchProps } from './switch';
3
+ export { toggleSwitch } from './logic';
@@ -0,0 +1,8 @@
1
+ import type { SceneNode } from '../../scene';
2
+
3
+ export function toggleSwitch(node: SceneNode): void {
4
+ const checked = !node.props.checked;
5
+ node.setProps({ checked });
6
+ const onChange = node.props.onChange;
7
+ if (typeof onChange === 'function') onChange(checked);
8
+ }
@@ -0,0 +1,37 @@
1
+ import type { TransformProps } from '../../types';
2
+ import { number, string, themeColor } from '../draw-utils';
3
+ import { createComponent } from '../shared';
4
+
5
+ export interface SwitchProps extends TransformProps {
6
+ checked?: boolean;
7
+ defaultChecked?: boolean;
8
+ disabled?: boolean;
9
+ width?: number;
10
+ height?: number;
11
+ onColor?: string;
12
+ offColor?: string;
13
+ thumbColor?: string;
14
+ onChange?: (checked: boolean) => void;
15
+ }
16
+
17
+ export const Switch = createComponent<SwitchProps>('switch');
18
+
19
+ export function drawSwitch(context: CanvasRenderingContext2D, props: SwitchProps): void {
20
+ const width = number(props.width, 44);
21
+ const height = number(props.height, 24);
22
+ const radius = height / 2;
23
+ const checked = Boolean(props.checked);
24
+ context.beginPath();
25
+ context.roundRect(0, 0, width, height, radius);
26
+ context.fillStyle = checked
27
+ ? string(props.onColor, themeColor(context, '--canvas-ui-switch-on', '#1677ff'))
28
+ : string(props.offColor, themeColor(context, '--canvas-ui-switch-off', '#d9d9d9'));
29
+ context.fill();
30
+ context.beginPath();
31
+ context.arc(checked ? width - radius : radius, radius, Math.max(1, radius - 3), 0, Math.PI * 2);
32
+ context.fillStyle = string(
33
+ props.thumbColor,
34
+ themeColor(context, '--canvas-ui-switch-thumb', '#fff'),
35
+ );
36
+ context.fill();
37
+ }
@@ -0,0 +1,3 @@
1
+ export { Tabs, TabItem, drawTabs } from './tabs';
2
+ export type { TabItemProps, TabsProps, TabsDrawProps } from './tabs';
3
+ export { tabKeyAt } from './logic';
@@ -0,0 +1,14 @@
1
+ import type { SceneNode } from '../../scene';
2
+
3
+ export function tabKeyAt(
4
+ items: readonly SceneNode[],
5
+ x: number,
6
+ y: number,
7
+ width: number,
8
+ headerHeight: number,
9
+ ): string | undefined {
10
+ if (y >= headerHeight || items.length === 0) return undefined;
11
+ const item = items[Math.floor(x / (width / items.length))];
12
+ if (!item || item.props.disabled || typeof item.props.id !== 'string') return undefined;
13
+ return item.props.id;
14
+ }
@@ -0,0 +1,78 @@
1
+ import type { TransformProps } from '../../types';
2
+ import { fitText, number, string, themeColor } from '../draw-utils';
3
+ import { createComponent } from '../shared';
4
+ import type { ComponentSize } from '../size';
5
+
6
+ export interface TabsProps extends TransformProps {
7
+ width: number;
8
+ height: number;
9
+ activeKey?: string;
10
+ defaultActiveKey?: string;
11
+ headerHeight?: number;
12
+ backgroundColor?: string;
13
+ textColor?: string;
14
+ activeColor?: string;
15
+ indicatorColor?: string;
16
+ children?: unknown;
17
+ onChange?: (key: string) => void;
18
+ }
19
+
20
+ export interface TabItemProps extends TransformProps {
21
+ id: string;
22
+ title: string;
23
+ disabled?: boolean;
24
+ size?: ComponentSize;
25
+ children?: unknown;
26
+ }
27
+
28
+ export const Tabs = createComponent<TabsProps>('tabs');
29
+ export const TabItem = createComponent<TabItemProps>('tabItem');
30
+
31
+ /** A tab header entry injected by the engine for the draw layer. */
32
+ export interface TabItemView {
33
+ title: string;
34
+ disabled?: boolean;
35
+ selected?: boolean;
36
+ }
37
+
38
+ /** Public props plus the engine-injected header model consumed by the draw layer. */
39
+ export type TabsDrawProps = TabsProps & { _tabItems?: TabItemView[] };
40
+
41
+ export function drawTabs(context: CanvasRenderingContext2D, props: TabsDrawProps): void {
42
+ const width = number(props.width, 300);
43
+ const headerHeight = number(props.headerHeight, 40);
44
+ const items = props._tabItems ?? [];
45
+ if (!items.length) return;
46
+ const itemWidth = width / items.length;
47
+ context.fillStyle = string(
48
+ props.backgroundColor,
49
+ themeColor(context, '--canvas-ui-tabs-background', '#fff'),
50
+ );
51
+ context.fillRect(0, 0, width, headerHeight);
52
+ context.font = '14px sans-serif';
53
+ context.textAlign = 'center';
54
+ context.textBaseline = 'middle';
55
+ items.forEach((item, index) => {
56
+ context.fillStyle = item.disabled
57
+ ? themeColor(context, '--canvas-ui-tabs-disabled-color', '#bfbfbf')
58
+ : item.selected
59
+ ? string(
60
+ props.activeColor,
61
+ themeColor(context, '--canvas-ui-tabs-active-color', '#1677ff'),
62
+ )
63
+ : string(props.textColor, themeColor(context, '--canvas-ui-tabs-color', '#595959'));
64
+ context.fillText(
65
+ fitText(context, item.title, Math.max(0, itemWidth - 16)),
66
+ itemWidth * (index + 0.5),
67
+ headerHeight / 2,
68
+ );
69
+ if (item.selected) {
70
+ context.fillStyle = string(
71
+ props.indicatorColor,
72
+ themeColor(context, '--canvas-ui-tabs-indicator', '#1677ff'),
73
+ );
74
+ context.fillRect(itemWidth * index, headerHeight - 2, itemWidth, 2);
75
+ }
76
+ });
77
+ context.textAlign = 'left';
78
+ }
@@ -0,0 +1,2 @@
1
+ export { Text, drawText, measureTextBox } from './text';
2
+ export type { TextBox, TextDrawProps, TextProps } from './text';
@@ -0,0 +1,95 @@
1
+ import type { TransformProps } from '../../types';
2
+ import {
3
+ TEXT_LINE_HEIGHT,
4
+ measureLines,
5
+ measureTextWidth,
6
+ number,
7
+ string,
8
+ themeColor,
9
+ type TextMeasurer,
10
+ } from '../draw-utils';
11
+ import { createComponent } from '../shared';
12
+
13
+ export interface TextProps extends TransformProps {
14
+ text?: string;
15
+ children?: string | number;
16
+ fontSize?: number;
17
+ /** Wrap width. Falls back to the box the parent container hands down when unset. */
18
+ width?: number;
19
+ /**
20
+ * Maximum number of wrapped lines; the last one is cut with an ellipsis. Falls back to the number
21
+ * of lines that fit the container's box, and to "no limit" when neither is declared.
22
+ */
23
+ maxLines?: number;
24
+ fontFamily?: string;
25
+ color?: string;
26
+ baseline?: CanvasTextBaseline;
27
+ textAlign?: CanvasTextAlign;
28
+ }
29
+
30
+ export interface TextBox {
31
+ lines: string[];
32
+ width: number;
33
+ height: number;
34
+ }
35
+
36
+ /** Public props plus the content box a parent container hands down to this node. */
37
+ export type TextDrawProps = TextProps & {
38
+ _availableWidth?: number;
39
+ _availableHeight?: number;
40
+ };
41
+
42
+ const DEFAULT_FONT_SIZE = 16;
43
+
44
+ export const Text = createComponent<TextProps>('text');
45
+
46
+ /**
47
+ * Whole lines that fit `height`, floored so a half-visible last line is never painted. Returns 0 when
48
+ * no height is known, which {@link measureLines} reads as "no limit".
49
+ */
50
+ function lineBudget(height: number, fontSize: number): number {
51
+ if (!(height > 0)) return 0;
52
+ return Math.max(1, Math.floor(height / (fontSize * TEXT_LINE_HEIGHT)));
53
+ }
54
+
55
+ /**
56
+ * Resolves `props` into the lines it renders and the box they occupy. {@link drawText} paints these
57
+ * lines and the layout pass sizes the node from the same result, so a Text node's reported height can
58
+ * never disagree with the glyphs it actually draws.
59
+ *
60
+ * A box handed down by the container is the fallback, not the override: declaring `width` or
61
+ * `maxLines` keeps the author in charge, so a node can stay narrower than its cell on purpose.
62
+ */
63
+ export function measureTextBox(props: TextDrawProps): TextBox {
64
+ const fontSize = number(props.fontSize, DEFAULT_FONT_SIZE);
65
+ const font = `${fontSize}px ${string(props.fontFamily, 'sans-serif')}`;
66
+ const measure: TextMeasurer = (text: string) => measureTextWidth(text, font);
67
+ const text = String(props.text ?? '');
68
+ const width = number(props.width, number(props._availableWidth, 0));
69
+ const maxLines = number(
70
+ props.maxLines,
71
+ lineBudget(number(props._availableHeight, 0), fontSize),
72
+ );
73
+ const lines = measureLines(measure, text, width, maxLines);
74
+ return {
75
+ lines,
76
+ width: width > 0 ? width : measure(text),
77
+ height: lines.length * fontSize * TEXT_LINE_HEIGHT,
78
+ };
79
+ }
80
+
81
+ export function drawText(context: CanvasRenderingContext2D, props: TextDrawProps): void {
82
+ const fontSize = number(props.fontSize, DEFAULT_FONT_SIZE);
83
+ context.font = `${fontSize}px ${string(props.fontFamily, 'sans-serif')}`;
84
+ context.textBaseline = props.baseline ?? 'top';
85
+ const textAlign =
86
+ props.textAlign === 'center' || props.textAlign === 'right' ? props.textAlign : 'left';
87
+ context.textAlign = textAlign;
88
+ context.fillStyle = string(props.color, themeColor(context, '--canvas-ui-text-color', '#333'));
89
+ const box = measureTextBox(props);
90
+ const anchorX = textAlign === 'center' ? box.width / 2 : textAlign === 'right' ? box.width : 0;
91
+ box.lines.forEach((line, index) => {
92
+ context.fillText(line, anchorX, index * fontSize * TEXT_LINE_HEIGHT);
93
+ });
94
+ context.textAlign = 'left';
95
+ }