1use super::dom::{Control, Dom, Edit, El, Run};
42use super::style::{Sheet, StyleError};
43use crate::types::Rect;
44
45pub const KIT_CSS: &str = r#"
48:root { --k-accent: #3b82f6; --k-bg: #17181b; --k-panel: #1f2125; --k-fg: #e8e8ea; --k-muted: #9a9ca3; --k-line: #33363c;
49 --k-me: #2563eb; --k-them: #26282d; --k-warn: #f59e0b; --k-error: #ef4444; --k-info: #3b82f6; --k-ok: #22c55e;
50 --k-btn: #2b2e34; --k-btn-hover: #353840; --k-track: #4a4d55; --k-track-hover: #575b64; --k-accent-hover: #2f6fd8;
51 --k-field: #111214; --k-sunken: #141518; --k-hover: #2c2f36; --k-hi: #33363d; --k-sel: rgba(59,130,246,0.18);
52 --k-dis: #5d6068; --k-body: #c9cbd1; --k-on-accent: #ffffff; --k-scrim: rgba(0,0,0,0.55); --k-shadow: rgba(0,0,0,0.45) }
53div.k-root { color: var(--k-fg); font-size: 13px; font-family: Inter, sans-serif }
54button.k-btn { padding: 6px 14px; border-radius: 8px; background: var(--k-btn); color: var(--k-fg); font-weight: 500 }
55button.k-btn:hover { background: var(--k-btn-hover) }
56button.k-btn.primary { background: var(--k-accent); color: var(--k-on-accent) }
57button.k-btn.primary:hover { background: var(--k-accent-hover) }
58button.k-btn.ghost { background: transparent; border: 1px solid var(--k-line) }
59button.k-btn.ghost:hover { background: var(--k-hover) }
60button.k-btn.danger { background: transparent; border: 1px solid var(--k-error); color: var(--k-error) }
61button.k-btn:focus-visible { border: 2px solid var(--k-accent) }
62button.k-switch { width: 36px; height: 20px; padding: 2px; border-radius: 10px; background: var(--k-track); --knob: #ffffff }
63button.k-switch:checked { background: var(--k-accent) }
64button.k-switch:hover { background: var(--k-track-hover) }
65button.k-switch:checked:hover { background: var(--k-accent-hover) }
66div.k-check { display: flex; align-items: center; gap: 8px }
67span.k-box { width: 16px; height: 16px; padding: 1px; border-radius: 4px; border: 1.5px solid var(--k-track-hover); --check: #ffffff; --check-width: 2px }
68span.k-box:checked { background: var(--k-accent); border-color: var(--k-accent) }
69div.k-input { height: 32px; padding: 0 10px; border-radius: 8px; background: var(--k-field); border: 1px solid var(--k-line);
70 --placeholder: var(--k-muted); --selection: rgba(59,130,246,0.4); --caret: var(--k-fg) }
71div.k-input:hover { border-color: var(--k-track) }
72div.k-input:focus { border-color: var(--k-accent) }
73div.k-num { display: flex; align-items: stretch; height: 32px }
74div.k-num > div.k-input { flex-grow: 1; border-radius: 8px 0px 0px 8px }
75div.k-steps { display: flex; flex-direction: column; width: 22px; flex-shrink: 0; border: 1px solid var(--k-line); border-left: none;
76 border-radius: 0px 8px 8px 0px; background: var(--k-btn); overflow: hidden }
77button.k-step { flex-grow: 1; display: flex; align-items: center; justify-content: center; color: var(--k-muted); padding: 0px }
78button.k-step:hover { background: var(--k-btn-hover); color: var(--k-fg) }
79button.k-step > span { width: 12px; height: 12px }
80span.k-spinner { width: 18px; height: 18px; color: var(--k-accent); --spin-track: var(--k-line); --spin-speed: 1.2 }
81select.k-select { height: 32px; padding: 0 30px 0 12px; border-radius: 8px; background: var(--k-btn); color: var(--k-fg); --chevron: var(--k-muted) }
82select.k-select:hover { background: var(--k-btn-hover) }
83select.k-select.open { border: 1px solid var(--k-accent) }
84div.k-pop { position: fixed; z-index: 100; padding: 4px; border-radius: 10px; background: var(--k-panel); border: 1px solid var(--k-line);
85 box-shadow: 0 8px 24px var(--k-shadow); display: flex; flex-direction: column }
86div.k-opt { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-radius: 6px; color: var(--k-fg); flex-shrink: 0 }
87div.k-opt:hover { background: var(--k-hover) }
88div.k-opt.sel { background: var(--k-sel) }
89div.k-opt.hi { background: var(--k-hi) }
90span.k-tick { width: 14px; height: 14px; flex-shrink: 0; color: var(--k-accent) }
91div.k-optl { display: flex; flex-direction: column; flex-grow: 1; min-width: 0 }
92div.k-search { display: flex; align-items: center; gap: 8px; height: 28px; margin: 2px 2px 4px; padding: 0 8px; border-radius: 6px; background: var(--k-sunken); flex-shrink: 0 }
93span.k-mag { width: 14px; height: 14px; color: var(--k-muted) }
94div.k-ph { color: var(--k-muted) }
95div.k-empty { padding: 7px 14px; color: var(--k-muted) }
96div.k-thumb { position: absolute; right: 3px; width: 3px; border-radius: 2px; background: var(--k-track) }
97div.k-opt.dis { color: var(--k-dis) }
98div.k-opt .k-sub { color: var(--k-muted); font-size: 11px }
99div.k-opt.dis .k-sub { color: var(--k-dis) }
100div.k-msg { display: flex; flex-direction: column; max-width: 75%; padding: 8px 12px; border-radius: 14px; white-space: normal; gap: 2px }
101div.k-msg.me { align-self: flex-end; background: var(--k-me); color: #ffffff; border-radius: 14px 14px 4px 14px }
102div.k-msg.them { align-self: flex-start; background: var(--k-them); border-radius: 14px 14px 14px 4px }
103div.k-msg > div.k-who { font-weight: 600; font-size: 12px; color: var(--k-accent) }
104div.k-msg.me > div.k-who { color: #dbe7ff }
105div.k-msg div.k-time { font-size: 10px; color: var(--k-muted); text-align: right }
106div.k-msg.me div.k-time { color: #c7d7f7 }
107div.k-body code { font-family: monospace; background: rgba(0,0,0,0.3); padding: 0 5px; border-radius: 5px }
108div.k-body b { font-weight: 700 }
109div.k-body i { font-style: italic }
110div.k-body a.link { color: #93c5fd }
111div.k-body a.mention { color: #93c5fd; font-weight: 600 }
112div.k-msg.me div.k-body a.link { color: #ffffff }
113div.k-msg > div.k-actions { align-self: flex-end }
114div.k-actions { display: flex; gap: 2px; padding: 2px; border-radius: 8px; background: var(--k-panel); border: 1px solid var(--k-line) }
115button.k-act { width: 26px; height: 26px; padding: 5px; border-radius: 6px; color: var(--k-muted) }
116button.k-act:hover { background: var(--k-hover); color: var(--k-fg) }
117div.k-card { border-radius: 10px; border: 1px solid var(--k-line); background: var(--k-panel); display: flex; flex-direction: column }
118div.k-card-head { display: flex; align-items: center; gap: 6px; padding: 8px 10px; font-weight: 600 }
119div.k-card-head:hover { background: var(--k-hover); border-radius: 10px }
120div.k-card > div.k-card-head > span.k-chev { width: 14px; height: 14px; color: var(--k-muted) }
121div.k-card-body { padding: 4px 12px 10px; white-space: normal; color: var(--k-body) }
122button.k-chip { padding: 4px 10px; border-radius: 999px; border: 1px solid var(--k-line); color: var(--k-muted); font-size: 12px }
123button.k-chip:hover { color: var(--k-fg) }
124button.k-chip.on { background: var(--k-sel); border-color: var(--k-accent); color: var(--k-fg) }
125div.k-banner { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-radius: 8px; border-left: 3px solid var(--k-info); background: rgba(59,130,246,0.12) }
126div.k-banner.warn { border-left: 3px solid var(--k-warn); background: rgba(245,158,11,0.12) }
127div.k-banner.error { border-left: 3px solid var(--k-error); background: rgba(239,68,68,0.12) }
128div.k-banner.ok { border-left: 3px solid var(--k-ok); background: rgba(34,197,94,0.12) }
129div.k-banner > div.k-banner-text { flex-grow: 1; white-space: normal }
130div.k-banner button.k-act { width: 22px; height: 22px; padding: 4px }
131div.k-side { position: fixed; top: 0; right: 0; bottom: 0; width: 320px; z-index: 50; background: var(--k-panel); border-left: 1px solid var(--k-line);
132 display: flex; flex-direction: column }
133div.k-side-head { display: flex; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--k-line); font-weight: 600 }
134div.k-side-head > div.k-side-title { flex-grow: 1 }
135div.k-side-body { padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; flex-grow: 1 }
136div.k-slot { border: 1px dashed var(--k-line); border-radius: 8px }
137div.k-tabs { display: flex; gap: 2px; padding: 3px; border-radius: 9px; background: var(--k-sunken); border: 1px solid var(--k-line) }
138button.k-tab { flex-grow: 1; padding: 5px 12px; border-radius: 6px; color: var(--k-muted); font-weight: 500; text-align: center }
139button.k-tab:hover { color: var(--k-fg); background: var(--k-hover) }
140button.k-tab.on { background: var(--k-btn-hover); color: var(--k-fg) }
141div.k-list { display: flex; flex-direction: column; padding: 4px; border-radius: 10px; border: 1px solid var(--k-line); background: var(--k-panel); overflow: hidden }
142div.k-li { display: flex; align-items: center; gap: 8px; height: 30px; padding: 0 10px; border-radius: 6px; flex-shrink: 0 }
143div.k-li:hover { background: var(--k-hover) }
144div.k-li.sel { background: var(--k-sel); color: var(--k-fg) }
145div.k-li > div.k-li-main { flex-grow: 1; min-width: 0 }
146div.k-li > div.k-li-side { color: var(--k-muted); font-size: 12px }
147div.k-table { display: flex; flex-direction: column; border-radius: 10px; border: 1px solid var(--k-line); background: var(--k-panel); overflow: hidden }
148div.k-tr { display: grid; align-items: center; height: 30px; padding: 0 12px; column-gap: 12px; border-bottom: 1px solid var(--k-line); flex-shrink: 0 }
149div.k-tr:hover { background: var(--k-hover) }
150div.k-tr.sel { background: var(--k-sel) }
151div.k-tr.k-th { color: var(--k-muted); font-size: 11px; font-weight: 600; background: var(--k-sunken) }
152div.k-td.num { text-align: right }
153div.k-menu { position: fixed; z-index: 120; padding: 4px; border-radius: 10px; background: var(--k-panel); border: 1px solid var(--k-line);
154 box-shadow: 0 8px 24px var(--k-shadow); display: flex; flex-direction: column }
155div.k-menu-title { padding: 6px 10px 4px; color: var(--k-muted); font-size: 11px; font-weight: 600 }
156div.k-mi { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-radius: 6px; flex-shrink: 0 }
157div.k-mi:hover { background: var(--k-hover) }
158div.k-mi.danger { color: var(--k-error) }
159div.k-mi.dis { color: var(--k-dis) }
160div.k-mi > span.k-mi-icon { width: 14px; height: 14px; color: var(--k-muted) }
161div.k-mi > div.k-mi-label { flex-grow: 1 }
162div.k-sep { height: 1px; margin: 4px 6px; background: var(--k-line); flex-shrink: 0 }
163span.k-kbd { padding: 1px 6px; border-radius: 5px; border: 1px solid var(--k-line); background: var(--k-sunken); color: var(--k-muted); font-size: 11px; font-family: monospace }
164span.k-badge { padding: 1px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; background: var(--k-sel); color: var(--k-accent) }
165span.k-badge.ok { background: rgba(34,197,94,0.15); color: var(--k-ok) }
166span.k-badge.warn { background: rgba(245,158,11,0.15); color: var(--k-warn) }
167span.k-badge.error { background: rgba(239,68,68,0.15); color: var(--k-error) }
168span.k-badge.muted { background: var(--k-hover); color: var(--k-muted) }
169div.k-scrim { position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 140; background: var(--k-scrim) }
170div.k-dialog { position: fixed; z-index: 150; border-radius: 14px; background: var(--k-panel); border: 1px solid var(--k-line);
171 box-shadow: 0 16px 48px var(--k-shadow); display: flex; flex-direction: column }
172div.k-dialog-head { display: flex; align-items: center; padding: 14px 16px 6px; font-size: 15px; font-weight: 600 }
173div.k-dialog-head > div.k-dialog-title { flex-grow: 1 }
174div.k-dialog-body { padding: 6px 16px 12px; flex-grow: 1; white-space: normal; color: var(--k-body); display: flex; flex-direction: column; gap: 10px }
175div.k-dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--k-line) }
176div.k-tip { position: fixed; z-index: 160; padding: 5px 9px; border-radius: 6px; background: var(--k-fg); color: var(--k-bg); font-size: 12px }
177div.k-toasts { position: fixed; right: 16px; bottom: 16px; z-index: 170; width: 300px; display: flex; flex-direction: column; gap: 8px }
178div.k-toast { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px; background: var(--k-panel); border: 1px solid var(--k-line);
179 border-left: 3px solid var(--k-info); box-shadow: 0 8px 24px var(--k-shadow) }
180div.k-toast.ok { border-left: 3px solid var(--k-ok) }
181div.k-toast.warn { border-left: 3px solid var(--k-warn) }
182div.k-toast.error { border-left: 3px solid var(--k-error) }
183div.k-toast > div.k-toast-text { flex-grow: 1; white-space: normal }
184div.k-seg { display: flex; padding: 3px; gap: 2px; border-radius: 9px; background: var(--k-sunken); border: 1px solid var(--k-line) }
185button.k-seg-btn { flex-grow: 1; padding: 5px 10px; border-radius: 6px; color: var(--k-muted); text-align: center; font-weight: 500 }
186button.k-seg-btn:hover { color: var(--k-fg) }
187button.k-seg-btn.on { background: var(--k-accent); color: var(--k-on-accent) }
188div.k-stat { display: flex; flex-direction: column; gap: 4px; padding: 12px 14px; border-radius: 12px; background: var(--k-panel); border: 1px solid var(--k-line) }
189div.k-stat > div.k-stat-label { color: var(--k-muted); font-size: 11px; font-weight: 600 }
190div.k-stat > div.k-stat-value { font-size: 24px; font-weight: 700 }
191div.k-stat > div.k-stat-note { color: var(--k-muted); font-size: 11px }
192div.k-progress { height: 6px; border-radius: 3px; background: var(--k-line); overflow: hidden }
193div.k-progress > div.k-bar { height: 6px; border-radius: 3px; background: var(--k-accent) }
194div.k-field { display: flex; flex-direction: column; gap: 6px }
195div.k-field > div.k-field-label { color: var(--k-muted); font-size: 11px; font-weight: 600 }
196"#;
197
198pub const TOOLBAR_CSS: &str = r#"
202div.k-toolbar { display: flex; align-items: center; gap: 4px; padding: 4px; background: var(--k-panel) }
203div.k-toolbar.vert { flex-direction: column }
204button.k-tool { display: flex; align-items: center; justify-content: center; gap: 6px; min-width: 32px; height: 32px; padding: 0 6px;
205 border-radius: 6px; color: var(--k-muted); flex-shrink: 0 }
206button.k-tool:hover { background: var(--k-hover); color: var(--k-fg) }
207button.k-tool.on { background: var(--k-sel); color: var(--k-accent) }
208button.k-tool.dis { color: var(--k-dis) }
209span.k-tool-icon { width: 20px; height: 20px }
210div.k-tool-split { display: flex; align-items: center; flex-shrink: 0 }
211div.k-tool-split > button.k-tool { border-radius: 6px 0px 0px 6px }
212button.k-tool-chev { display: flex; align-items: center; justify-content: center; width: 14px; height: 32px; padding: 0px;
213 border-radius: 0px 6px 6px 0px; color: var(--k-muted) }
214button.k-tool-chev:hover { background: var(--k-hover); color: var(--k-fg) }
215button.k-tool-chev > span { width: 10px; height: 10px }
216div.k-tool-sep { width: 1px; height: 20px; background: var(--k-line); flex-shrink: 0 }
217div.k-toolbar.vert > div.k-tool-sep { width: 20px; height: 1px }
218div.k-tool-fill { flex-grow: 1 }
219"#;
220
221pub const KIT_TOUCH_CSS: &str = r#"
227@media (pointer: coarse) {
228div.k-root { font-size: 15px }
229button.k-btn { padding: 12px 18px; min-height: 44px; border-radius: 10px }
230button.k-switch { width: 44px; height: 26px; padding: 3px; border-radius: 13px }
231span.k-box { width: 22px; height: 22px; border-radius: 6px }
232div.k-check { gap: 12px; min-height: 44px }
233div.k-input { height: 44px; padding: 0 12px; border-radius: 10px }
234select.k-select { height: 44px; padding: 0 34px 0 14px; border-radius: 10px }
235div.k-opt { padding: 12px 12px; border-radius: 8px }
236div.k-mi { padding: 12px 12px; border-radius: 8px }
237div.k-search { height: 40px }
238div.k-li { height: 48px; padding: 0 14px; border-radius: 8px }
239div.k-tr { height: 44px }
240button.k-act { width: 40px; height: 40px; padding: 10px; border-radius: 8px }
241button.k-tab { padding: 10px 14px }
242button.k-seg-btn { padding: 9px 12px }
243button.k-chip { padding: 8px 14px }
244div.k-card-head { padding: 12px 14px }
245div.k-dialog-foot { padding: 12px 16px 16px }
246}
247"#;
248
249pub const LIGHT_VARS: &str = "--k-bg: #f6f7f9; --k-panel: #ffffff; --k-fg: #1d1f24; --k-muted: #6b6f78; --k-line: #dfe1e6; \
252--k-them: #eceef2; --k-btn: #eceef2; --k-btn-hover: #e2e5ea; --k-track: #c9ccd3; --k-track-hover: #b7bbc4; --k-accent-hover: #2563eb; \
253--k-field: #ffffff; --k-sunken: #f0f1f4; --k-hover: #eef0f4; --k-hi: #e6e9ef; --k-sel: rgba(59,130,246,0.14); --k-dis: #b0b4bc; \
254--k-body: #3a3d45; --k-scrim: rgba(15,17,21,0.35); --k-shadow: rgba(15,17,21,0.18)";
255
256pub fn theme_vars(dark: bool) -> &'static str {
258 if dark {
259 ""
260 } else {
261 LIGHT_VARS
262 }
263}
264
265pub fn sheet() -> Sheet {
267 Sheet::parse(&[KIT_CSS, KIT_TOUCH_CSS].concat()).expect("KIT_CSS parses")
268}
269
270pub fn sheet_with(app_css: &str) -> Result<Sheet, StyleError> {
272 Sheet::parse(&[KIT_CSS, KIT_TOUCH_CSS, app_css].concat())
273}
274
275pub fn icon(body: &str) -> String {
277 format!(r#"<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">{body}</svg>"#)
278}
279
280pub mod icons {
282 pub const CLOSE: &str = r#"<path d="M6 6l12 12M18 6L6 18"/>"#;
283 pub const RIGHT: &str = r#"<path d="M9 6l6 6-6 6"/>"#;
284 pub const DOWN: &str = r#"<path d="M6 9l6 6 6-6"/>"#;
285 pub const UP: &str = r#"<path d="M6 15l6-6 6 6"/>"#;
286 pub const REPLY: &str = r#"<path d="M9 14L4 9l5-5"/><path d="M4 9h10a6 6 0 0 1 6 6v3"/>"#;
287 pub const COPY: &str = r#"<rect x="9" y="9" width="11" height="11" rx="2"/><path d="M5 15V5a2 2 0 0 1 2-2h8"/>"#;
288 pub const SMILE: &str = r#"<circle cx="12" cy="12" r="9"/><path d="M8.5 14.5a4.5 4.5 0 0 0 7 0M9 9.5h.01M15 9.5h.01"/>"#;
289 pub const MORE: &str = r#"<path d="M5 12h.01M12 12h.01M19 12h.01"/>"#;
290 pub const CHECK: &str = r#"<path d="M5 12.5l4.5 4.5L19 7.5"/>"#;
291 pub const SEARCH: &str = r#"<circle cx="11" cy="11" r="6.5"/><path d="M16 16l4 4"/>"#;
292 pub const LEFT: &str = r#"<path d="M15 6l-6 6 6 6"/>"#;
293 pub const PLUS: &str = r#"<path d="M12 5v14M5 12h14"/>"#;
294 pub const SETTINGS: &str = r#"<circle cx="12" cy="12" r="3"/><path d="M12 2v3M12 19v3M2 12h3M19 12h3M4.9 4.9l2.1 2.1M17 17l2.1 2.1M4.9 19.1L7 17M17 7l2.1-2.1"/>"#;
295 pub const MENU: &str = r#"<path d="M4 6h16M4 12h16M4 18h16"/>"#;
296 pub const SUN: &str = r#"<circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M2 12h2M20 12h2M5 5l1.4 1.4M17.6 17.6L19 19M5 19l1.4-1.4M17.6 6.4L19 5"/>"#;
297 pub const MOON: &str = r#"<path d="M20 14.5A8 8 0 1 1 9.5 4a6.5 6.5 0 0 0 10.5 10.5z"/>"#;
298 pub const SAVE: &str = r#"<path d="M5 3h11l3 3v15H5z"/><path d="M8 3v5h7V3M8 21v-7h8v7"/>"#;
299 pub const UNDO: &str = r#"<path d="M4 9h11a5 5 0 0 1 0 10H8"/><path d="M8 5L4 9l4 4"/>"#;
300 pub const EXPAND: &str = r#"<path d="M15 3h6v6M9 21H3v-6M21 3l-7 7M3 21l7-7"/>"#;
301 pub const PLAY: &str = r#"<path d="M7 4l13 8-13 8z"/>"#;
302 pub const GRID: &str = r#"<rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/>"#;
303 pub const IMAGE: &str = r#"<rect x="3" y="4" width="18" height="16" rx="2"/><circle cx="9" cy="10" r="2"/><path d="M21 16l-5-5-9 9"/>"#;
304 pub const WINDOW: &str = r#"<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M3 9h18"/>"#;
305 pub const DOWNLOAD: &str = r#"<path d="M12 3v12M7 10l5 5 5-5M4 20h16"/>"#;
306 pub const CHART: &str = r#"<path d="M4 20V4M4 20h16M8 16v-5M12 16V8M16 16v-8"/>"#;
307 pub const LIST: &str = r#"<path d="M9 6h11M9 12h11M9 18h11M4 6h.01M4 12h.01M4 18h.01"/>"#;
308 pub const TERMINAL: &str = r#"<path d="M4 7l5 5-5 5M12 19h8"/>"#;
309 pub const TRASH: &str = r#"<path d="M4 7h16M10 11v6M14 11v6M6 7l1 13h10l1-13M9 7V4h6v3"/>"#;
310 pub const EDIT: &str = r#"<path d="M4 20h4L19 9l-4-4L4 16z"/>"#;
311 pub const PIN: &str = r#"<path d="M9 4h6l-1 6 4 4H6l4-4zM12 14v6"/>"#;
312 pub const BOX: &str = r#"<path d="M21 8l-9-5-9 5 9 5 9-5zM3 8v8l9 5 9-5V8M12 13v8"/>"#;
313 pub const BOLT: &str = r#"<path d="M13 2L4 14h7l-1 8 9-12h-7z"/>"#;
314}
315
316pub fn button(id: &str, label: &str) -> El {
318 El::new("button.k-btn").id(id).tab().text(label)
319}
320
321pub fn switch(id: &str, on: bool, name: &str) -> El {
323 El::new("button.k-switch").id(id).tab().name(name).control(Control::switch(on))
324}
325
326pub fn switch_at(id: &str, on: bool, pos: f64, name: &str) -> El {
328 El::new("button.k-switch").id(id).tab().name(name).control(Control::Switch { on, pos })
329}
330
331pub fn checkbox(id: &str, on: bool, label: &str) -> El {
333 El::new("div.k-check").id(id).tab().name(label).child(El::new("span.k-box").control(Control::checkbox(on))).child(El::new("span").text(label))
334}
335
336pub fn spinner(t: f64) -> El {
338 El::new("span.k-spinner").name("Loading").control(Control::spinner(t))
339}
340
341pub fn input(id: &str, edit: Edit) -> El {
343 El::new("div.k-input").id(id).tab().control(Control::input(edit))
344}
345
346#[derive(Clone, Copy, Debug, PartialEq)]
352#[non_exhaustive]
353pub struct NumberSpec {
354 pub min: Option<f64>,
356 pub max: Option<f64>,
358 pub step: f64,
360 pub decimals: Option<u32>,
362}
363
364impl Default for NumberSpec {
365 fn default() -> Self {
366 NumberSpec { min: None, max: None, step: 1.0, decimals: None }
367 }
368}
369
370impl NumberSpec {
371 pub fn new() -> Self {
373 Self::default()
374 }
375 pub fn integer() -> Self {
377 NumberSpec { decimals: Some(0), ..Self::default() }
378 }
379 pub fn range(mut self, min: f64, max: f64) -> Self {
381 self.min = Some(min.min(max));
382 self.max = Some(max.max(min));
383 self
384 }
385 pub fn min(mut self, v: f64) -> Self {
387 self.min = Some(v);
388 self
389 }
390 pub fn max(mut self, v: f64) -> Self {
392 self.max = Some(v);
393 self
394 }
395 pub fn step(mut self, v: f64) -> Self {
397 if v.is_finite() && v > 0.0 {
398 self.step = v;
399 }
400 self
401 }
402 pub fn decimals(mut self, n: u32) -> Self {
404 self.decimals = Some(n);
405 self
406 }
407
408 pub fn accepts(&self, c: char) -> bool {
411 c.is_ascii_digit() || matches!(c, '-' | '+' | 'e' | 'E') || (self.decimals != Some(0) && matches!(c, '.' | ','))
412 }
413
414 pub fn sanitize(&self, text: &str) -> String {
416 text.chars().filter(|c| self.accepts(*c)).collect()
417 }
418
419 pub fn parse(&self, text: &str) -> Option<f64> {
422 let t: String = self.sanitize(text).replace(',', ".");
423 let v: f64 = t.parse().ok().filter(|v: &f64| v.is_finite())?;
424 Some(self.clamp(v))
425 }
426
427 pub fn clamp(&self, v: f64) -> f64 {
429 let v = self.min.map_or(v, |m| v.max(m));
430 self.max.map_or(v, |m| v.min(m))
431 }
432
433 pub fn format(&self, v: f64) -> String {
435 match self.decimals {
436 Some(n) => format!("{v:.*}", n as usize),
437 None => {
438 let places = decimals_of(self.step).max(decimals_of(v)).min(10);
440 let s = format!("{v:.places$}");
441 if s.contains('.') { s.trim_end_matches('0').trim_end_matches('.').to_string() } else { s }
442 }
443 }
444 }
445
446 pub fn stepped(&self, text: &str, n: i32) -> String {
449 let from = self.parse(text).unwrap_or_else(|| self.min.unwrap_or(0.0).max(self.max.map_or(f64::MIN, |m| m.min(0.0))));
450 let base = self.min.unwrap_or(0.0);
451 let k = ((from - base) / self.step).round();
452 let at_grid = (base + k * self.step - from).abs() < self.step * 1e-9;
453 let k = if at_grid { k + n as f64 } else if n > 0 { ((from - base) / self.step).floor() + n as f64 } else { ((from - base) / self.step).ceil() + n as f64 };
454 self.format(self.clamp(base + k * self.step))
455 }
456
457 pub fn normalized(&self, text: &str) -> String {
460 match self.parse(text) {
461 Some(v) => self.format(v),
462 None => text.to_string(),
463 }
464 }
465}
466
467fn decimals_of(v: f64) -> usize {
468 let s = format!("{}", v.abs());
469 s.split_once('.').map_or(0, |(_, f)| f.len())
470}
471
472pub fn number_input(id: &str, edit: Edit, name: &str) -> El {
477 let step = |dir: &str, label: &str, body: &str| {
478 El::new("button.k-step").id(format!("{id}:{dir}")).name(format!("{name} {label}")).child(El::new("span").svg(icon(body)))
479 };
480 El::new("div.k-num")
481 .child(input(id, edit).name(name))
482 .child(El::new("div.k-steps").child(step("inc", "+", icons::UP)).child(step("dec", "−", icons::DOWN)))
483}
484
485#[derive(Clone, Debug, Default, PartialEq)]
487pub struct SelectOpt {
488 pub label: String,
489 pub sub: Option<String>,
491 pub disabled: bool,
493}
494
495impl SelectOpt {
496 pub fn new(label: impl Into<String>) -> Self {
497 SelectOpt { label: label.into(), ..Default::default() }
498 }
499 pub fn sub(mut self, s: impl Into<String>) -> Self {
500 self.sub = Some(s.into());
501 self
502 }
503 pub fn disabled(mut self, on: bool) -> Self {
504 self.disabled = on;
505 self
506 }
507}
508
509pub fn select_trigger(id: &str, label: &str, open: bool, name: &str) -> El {
511 El::new(if open { "select.k-select.open" } else { "select.k-select" }).id(id).tab().name(name).text(label)
512}
513
514pub fn popover_at(anchor: Rect, viewport: Rect, w: f64, h: f64) -> Rect {
517 let w = w.max(anchor.width);
518 let below = viewport.y + viewport.height - (anchor.y + anchor.height + 4.0);
519 let above = anchor.y - 4.0 - viewport.y;
520 let y = if h > below && above > below { (anchor.y - 4.0 - h).max(viewport.y) } else { anchor.y + anchor.height + 4.0 };
521 let x = anchor.x.min(viewport.x + viewport.width - w).max(viewport.x);
522 Rect::new(x, y, w, h.min(below.max(above)))
523}
524
525pub fn select_popover(id: &str, opts: &[SelectOpt], chosen: Option<usize>, at: Rect) -> El {
529 select_list(id, opts, chosen, &ListView::all(), at)
530}
531
532#[derive(Clone, Copy, Debug, PartialEq)]
535pub struct ListView<'a> {
536 pub hi: Option<usize>,
538 pub top: usize,
540 pub rows: usize,
542 pub query: Option<&'a str>,
544 pub row_h: f64,
546}
547
548impl ListView<'_> {
549 pub fn all() -> Self {
551 ListView { hi: None, top: 0, rows: usize::MAX, query: None, row_h: 0.0 }
552 }
553}
554
555pub fn select_list(id: &str, opts: &[SelectOpt], chosen: Option<usize>, view: &ListView<'_>, at: Rect) -> El {
562 let mut pop = El::new("div.k-pop").id(format!("{id}:pop")).css(format!("top: {}px; left: {}px; width: {}px", at.y, at.x, at.width));
563 if let Some(q) = view.query {
564 let line = El::new("div.k-search").child(El::new("span.k-mag").svg(icon(icons::SEARCH)));
565 pop = pop.child(if q.is_empty() { line.child(El::new("div.k-ph").text("Type to search")) } else { line.child(El::new("div.k-q").text(q)) });
566 }
567 if opts.is_empty() {
568 pop = pop.child(El::new("div.k-empty").text("No matches"));
569 }
570 let end = view.top.saturating_add(view.rows).min(opts.len());
571 for (i, o) in opts.iter().enumerate().take(end).skip(view.top) {
572 let mut spec = String::from("div.k-opt");
573 if chosen == Some(i) {
574 spec.push_str(".sel");
575 }
576 if view.hi == Some(i) {
577 spec.push_str(".hi");
578 }
579 if o.disabled {
580 spec.push_str(".dis");
581 }
582 let mut label = El::new("div.k-optl").child(El::new("div.k-label").text(&o.label));
583 if let Some(s) = &o.sub {
584 label = label.child(El::new("div.k-sub").text(s));
585 }
586 let tick = El::new("span.k-tick");
587 let tick = if chosen == Some(i) { tick.svg(icon(icons::CHECK)) } else { tick };
588 let mut row = El::new(spec).name(&o.label).child(tick).child(label);
589 if !o.disabled {
590 row = row.id(format!("{id}:opt:{i}")).tab();
591 }
592 pop = pop.child(row);
593 }
594 let n = opts.len();
595 if n > view.rows && view.row_h > 0.0 {
596 let track = view.row_h * view.rows as f64;
597 let th = (track * view.rows as f64 / n as f64).max(18.0);
598 let ty = (track - th) * view.top as f64 / (n - view.rows) as f64 + if view.query.is_some() { 34.0 } else { 0.0 } + 4.0;
599 pop = pop.child(El::new("div.k-thumb").css(format!("top: {ty}px; height: {th}px")));
600 }
601 pop
602}
603
604pub fn picked_opt(id: &str, hit: &str) -> Option<usize> {
606 hit.strip_prefix(id)?.strip_prefix(":opt:")?.parse().ok()
607}
608
609pub fn bubble(id: &str, mine: bool, who: Option<&str>, body: &str, time: Option<&str>) -> El {
611 let mut b = El::new(if mine { "div.k-msg.me" } else { "div.k-msg.them" }).id(id);
612 if let Some(w) = who {
613 b = b.child(El::new("div.k-who").text(w));
614 }
615 b = b.child(El::new("div.k-body").runs(inline_md(body)));
616 if let Some(t) = time {
617 b = b.child(El::new("div.k-time").text(t));
618 }
619 b
620}
621
622pub fn action_row(id: &str, actions: &[(&str, &str, &str)]) -> El {
625 El::new("div.k-actions").id(id).children(actions.iter().map(|(k, ic, n)| El::new("button.k-act").id(format!("{id}:{k}")).tab().name(*n).svg(icon(ic))))
626}
627
628pub fn card(id: &str, title: &str, open: bool, body: El) -> El {
630 let head = El::new("div.k-card-head")
631 .id(id)
632 .tab()
633 .name(title)
634 .child(El::new("span.k-chev").svg(icon(if open { icons::DOWN } else { icons::RIGHT })))
635 .child(El::new("span").text(title));
636 El::new(if open { "div.k-card.open" } else { "div.k-card" }).child(head).child_if(open, || El::new("div.k-card-body").child(body))
637}
638
639pub fn chip(id: &str, label: &str, on: bool) -> El {
641 El::new(if on { "button.k-chip.on" } else { "button.k-chip" }).id(id).tab().text(label)
642}
643
644pub fn banner(id: &str, kind: &str, text: &str, action: Option<&str>, closable: bool) -> El {
647 let mut b = El::new(format!("div.k-banner.{kind}")).id(id).name(text).child(El::new("div.k-banner-text").text(text));
648 if let Some(a) = action {
649 b = b.child(El::new("button.k-btn.ghost").id(format!("{id}:act")).tab().text(a));
650 }
651 if closable {
652 b = b.child(El::new("button.k-act").id(format!("{id}:close")).tab().name("Close").svg(icon(icons::CLOSE)));
653 }
654 b
655}
656
657pub fn side_panel(id: &str, title: &str, body: impl IntoIterator<Item = El>) -> El {
660 El::new("div.k-side")
661 .id(id)
662 .child(
663 El::new("div.k-side-head")
664 .child(El::new("div.k-side-title").text(title))
665 .child(El::new("button.k-act").id(format!("{id}:close")).tab().name("Close").svg(icon(icons::CLOSE))),
666 )
667 .child(El::new("div.k-side-body").children(body))
668}
669
670pub fn slot(key: &str, w: f64, h: f64) -> El {
673 El::new("div.k-slot").slot(key).css(format!("width: {w}px; height: {h}px"))
674}
675
676pub fn tabs(id: &str, labels: &[&str], active: usize) -> El {
678 El::new("div.k-tabs").id(id).children(labels.iter().enumerate().map(|(i, l)| {
679 El::new(if i == active { "button.k-tab.on" } else { "button.k-tab" }).id(format!("{id}:tab:{i}")).tab().text(*l)
680 }))
681}
682
683pub fn segmented(id: &str, labels: &[&str], active: usize) -> El {
685 El::new("div.k-seg").id(id).children(labels.iter().enumerate().map(|(i, l)| {
686 El::new(if i == active { "button.k-seg-btn.on" } else { "button.k-seg-btn" }).id(format!("{id}:{i}")).tab().text(*l)
687 }))
688}
689
690pub fn picked(id: &str, kind: &str, hit: &str) -> Option<usize> {
692 let rest = hit.strip_prefix(id)?.strip_prefix(':')?;
693 let rest = if kind.is_empty() { rest } else { rest.strip_prefix(kind)?.strip_prefix(':')? };
694 rest.parse().ok()
695}
696
697pub fn list(id: &str, rows: &[(&str, Option<&str>)], selected: Option<usize>) -> El {
700 El::new("div.k-list").id(id).children(rows.iter().enumerate().map(|(i, (main, side))| {
701 El::new(if selected == Some(i) { "div.k-li.sel" } else { "div.k-li" })
702 .id(format!("{id}:row:{i}"))
703 .tab()
704 .name(*main)
705 .child(El::new("div.k-li-main").text(*main))
706 .child_if(side.is_some(), || El::new("div.k-li-side").text(side.unwrap_or_default()))
707 }))
708}
709
710pub fn table(id: &str, head: &[&str], columns: &str, num: &[usize], rows: &[Vec<String>], selected: Option<usize>) -> El {
713 let grid = format!("grid-template-columns: {columns}");
714 let cell = |c: usize, t: &str| El::new(if num.contains(&c) { "div.k-td.num" } else { "div.k-td" }).text(t);
715 let mut t = El::new("div.k-table").id(id).child(
716 El::new("div.k-tr.k-th").css(grid.clone()).children(head.iter().enumerate().map(|(c, h)| cell(c, h))),
717 );
718 for (i, r) in rows.iter().enumerate() {
719 t = t.child(
720 El::new(if selected == Some(i) { "div.k-tr.sel" } else { "div.k-tr" })
721 .id(format!("{id}:row:{i}"))
722 .tab()
723 .name(r.first().map(String::as_str).unwrap_or_default())
724 .css(grid.clone())
725 .children(r.iter().enumerate().map(|(c, v)| cell(c, v))),
726 );
727 }
728 t
729}
730
731#[derive(Clone, Debug, Default, PartialEq)]
733pub struct MenuItem {
734 pub label: String,
735 pub icon: Option<&'static str>,
737 pub kbd: Option<String>,
739 pub danger: bool,
741 pub disabled: bool,
743 pub separator: bool,
745}
746
747impl MenuItem {
748 pub fn new(label: impl Into<String>) -> Self {
749 MenuItem { label: label.into(), ..Default::default() }
750 }
751 pub fn separator() -> Self {
752 MenuItem { separator: true, ..Default::default() }
753 }
754 pub fn icon(mut self, body: &'static str) -> Self {
755 self.icon = Some(body);
756 self
757 }
758 pub fn kbd(mut self, k: impl Into<String>) -> Self {
759 self.kbd = Some(k.into());
760 self
761 }
762 pub fn danger(mut self, on: bool) -> Self {
763 self.danger = on;
764 self
765 }
766 pub fn disabled(mut self, on: bool) -> Self {
767 self.disabled = on;
768 self
769 }
770}
771
772pub fn menu(id: &str, title: Option<&str>, items: &[MenuItem], at: Rect) -> El {
776 let mut m = El::new("div.k-menu").id(format!("{id}:menu")).css(format!("top: {}px; left: {}px; width: {}px", at.y, at.x, at.width));
777 if let Some(t) = title {
778 m = m.child(El::new("div.k-menu-title").text(t));
779 }
780 m.children(items.iter().enumerate().map(|(i, it)| {
781 if it.separator {
782 return El::new("div.k-sep");
783 }
784 let mut spec = String::from("div.k-mi");
785 if it.danger {
786 spec.push_str(".danger");
787 }
788 if it.disabled {
789 spec.push_str(".dis");
790 }
791 let mut row = El::new(spec).name(&it.label);
792 if let Some(ic) = it.icon {
793 row = row.child(El::new("span.k-mi-icon").svg(icon(ic)));
794 }
795 row = row.child(El::new("div.k-mi-label").text(&it.label));
796 if let Some(k) = &it.kbd {
797 row = row.child(kbd(k));
798 }
799 if !it.disabled {
800 row = row.id(format!("{id}:item:{i}")).tab();
801 }
802 row
803 }))
804}
805
806#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
808#[non_exhaustive]
809pub enum ToolKind {
810 #[default]
812 Button,
813 Split,
816 Separator,
818 Fill,
820}
821
822#[derive(Clone, Debug, Default)]
824#[non_exhaustive]
825pub struct ToolItem {
826 pub kind: ToolKind,
827 pub key: String,
829 pub icon: Option<String>,
831 pub label: Option<String>,
832 pub name: Option<String>,
834 pub on: bool,
836 pub disabled: bool,
838}
839
840impl ToolItem {
841 pub fn button(key: impl Into<String>, icon_svg: impl Into<String>) -> Self {
843 ToolItem { key: key.into(), icon: Some(icon_svg.into()), ..Self::default() }
844 }
845 pub fn split(key: impl Into<String>, icon_svg: impl Into<String>) -> Self {
847 ToolItem { kind: ToolKind::Split, ..Self::button(key, icon_svg) }
848 }
849 pub fn separator() -> Self {
851 ToolItem { kind: ToolKind::Separator, ..Self::default() }
852 }
853 pub fn fill() -> Self {
855 ToolItem { kind: ToolKind::Fill, ..Self::default() }
856 }
857 pub fn label(mut self, l: impl Into<String>) -> Self {
858 self.label = Some(l.into());
859 self
860 }
861 pub fn name(mut self, n: impl Into<String>) -> Self {
862 self.name = Some(n.into());
863 self
864 }
865 pub fn on(mut self, on: bool) -> Self {
866 self.on = on;
867 self
868 }
869 pub fn disabled(mut self, on: bool) -> Self {
870 self.disabled = on;
871 self
872 }
873}
874
875pub fn toolbar(id: &str, items: &[ToolItem], vertical: bool) -> El {
879 let bar = El::new(if vertical { "div.k-toolbar.vert" } else { "div.k-toolbar" }).id(format!("{id}:bar"));
880 bar.children(items.iter().map(|it| match it.kind {
881 ToolKind::Separator => El::new("div.k-tool-sep"),
882 ToolKind::Fill => El::new("div.k-tool-fill"),
883 ToolKind::Button | ToolKind::Split => {
884 let mut spec = String::from("button.k-tool");
885 if it.on {
886 spec.push_str(".on");
887 }
888 if it.disabled {
889 spec.push_str(".dis");
890 }
891 let name = it.name.as_deref().or(it.label.as_deref()).unwrap_or(&it.key);
892 let mut b = El::new(spec).name(name);
893 if !it.disabled {
894 b = b.id(format!("{id}:{}", it.key)).tab();
895 }
896 if let Some(svg) = &it.icon {
897 b = b.child(El::new("span.k-tool-icon").svg(svg.clone()));
898 }
899 if let Some(l) = &it.label {
900 b = b.child(El::new("span").text(l.clone()));
901 }
902 if it.kind == ToolKind::Split {
903 let mut chev = El::new("button.k-tool-chev").name(format!("{name}: more"));
904 if !it.disabled {
905 chev = chev.id(format!("{id}:{}:menu", it.key)).tab();
906 }
907 let chev = chev.child(El::new("span").svg(icon(if vertical { icons::RIGHT } else { icons::DOWN })));
908 El::new("div.k-tool-split").child(b).child(chev)
909 } else {
910 b
911 }
912 }
913 }))
914}
915
916pub fn picked_tool<'a>(id: &str, hit: &'a str) -> Option<(&'a str, bool)> {
919 let rest = hit.strip_prefix(id)?.strip_prefix(':')?;
920 if rest == "bar" {
921 return None;
922 }
923 Some(match rest.strip_suffix(":menu") {
924 Some(key) => (key, true),
925 None => (rest, false),
926 })
927}
928
929pub fn flyout_at(anchor: Rect, viewport: Rect, w: f64, h: f64, vertical: bool) -> Rect {
932 let (mut x, mut y) = if vertical { (anchor.x + anchor.width + 4.0, anchor.y) } else { (anchor.x, anchor.y + anchor.height + 4.0) };
933 if x + w > viewport.x + viewport.width {
934 x = if vertical { anchor.x - 4.0 - w } else { viewport.x + viewport.width - w };
935 }
936 if y + h > viewport.y + viewport.height {
937 y = if vertical { viewport.y + viewport.height - h } else { anchor.y - 4.0 - h };
938 }
939 Rect::new(x.max(viewport.x), y.max(viewport.y), w, h)
940}
941
942pub fn kbd(text: &str) -> El {
944 El::new("span.k-kbd").text(text)
945}
946
947pub fn badge(text: &str, kind: &str) -> El {
949 El::new(if kind.is_empty() { "span.k-badge".to_string() } else { format!("span.k-badge.{kind}") }).text(text)
950}
951
952pub fn centered(viewport: Rect, w: f64, h: f64) -> Rect {
954 let (w, h) = (w.min(viewport.width), h.min(viewport.height));
955 Rect::new(viewport.x + (viewport.width - w) / 2.0, viewport.y + (viewport.height - h) / 2.0, w, h)
956}
957
958pub fn dialog(id: &str, title: &str, body: El, footer: &[&str], at: Rect) -> El {
962 let foot = El::new("div.k-dialog-foot").children(footer.iter().enumerate().map(|(i, l)| {
963 El::new(if i == 0 { "button.k-btn.primary" } else { "button.k-btn" }).id(format!("{id}:foot:{i}")).tab().text(*l)
964 }));
965 El::new("div.k-modal")
966 .child(El::new("div.k-scrim").id(format!("{id}:scrim")))
967 .child(
968 El::new("div.k-dialog")
969 .id(id)
970 .name(title)
971 .css(format!("top: {}px; left: {}px; width: {}px; height: {}px", at.y, at.x, at.width, at.height))
972 .child(
973 El::new("div.k-dialog-head")
974 .child(El::new("div.k-dialog-title").text(title))
975 .child(El::new("button.k-act").id(format!("{id}:close")).tab().name("Close").svg(icon(icons::CLOSE))),
976 )
977 .child(El::new("div.k-dialog-body").child(body))
978 .child_if(!footer.is_empty(), || foot),
979 )
980}
981
982pub fn tooltip(text: &str, anchor: Rect) -> El {
984 El::new("div.k-tip").text(text).css(format!("top: {}px; left: {}px", anchor.y + anchor.height + 6.0, anchor.x))
985}
986
987pub fn toasts(id: &str, items: &[(&str, &str)]) -> El {
990 El::new("div.k-toasts").id(id).children(items.iter().enumerate().map(|(i, (kind, text))| {
991 El::new(if kind.is_empty() { "div.k-toast".to_string() } else { format!("div.k-toast.{kind}") })
992 .name(*text)
993 .child(El::new("div.k-toast-text").text(*text))
994 .child(El::new("button.k-act").id(format!("{id}:{i}:close")).tab().name("Close").svg(icon(icons::CLOSE)))
995 }))
996}
997
998pub fn stat(id: &str, label: &str, value: &str, note: &str) -> El {
1000 El::new("div.k-stat")
1001 .id(id)
1002 .name(label)
1003 .child(El::new("div.k-stat-label").text(label))
1004 .child(El::new("div.k-stat-value").text(value))
1005 .child_if(!note.is_empty(), || El::new("div.k-stat-note").text(note))
1006}
1007
1008pub fn progress(v: f64) -> El {
1010 let pct = (v.clamp(0.0, 1.0) * 100.0 * 10.0).round() / 10.0;
1011 El::new("div.k-progress").child(El::new("div.k-bar").css(format!("width: {pct}%")))
1012}
1013
1014pub fn field(label: &str, control: El) -> El {
1016 El::new("div.k-field").child(El::new("div.k-field-label").text(label)).child(control)
1017}
1018
1019pub fn inline_md(text: &str) -> Vec<Run> {
1024 let cs: Vec<char> = text.chars().collect();
1025 let mut out: Vec<Run> = Vec::new();
1026 let (mut bold, mut ital) = (false, false);
1027 let mut cur = String::new();
1028 let flush = |out: &mut Vec<Run>, cur: &mut String, bold: bool, ital: bool| {
1029 if cur.is_empty() {
1030 return;
1031 }
1032 let spec = match (bold, ital) {
1033 (true, true) => "b.i",
1034 (true, false) => "b",
1035 (false, true) => "i",
1036 _ => "span",
1037 };
1038 for sp in crate::ui::widgets::composite::chat_thread::linkify(cur, "link", 400) {
1039 let r = match crate::ui::widgets::composite::chat_thread::span_link(&sp) {
1040 Some(crate::ui::widgets::composite::chat_thread::Link::Mention(n)) => Run::new("a.mention", sp.text.as_str()).id(format!("nick:{n}")),
1041 Some(crate::ui::widgets::composite::chat_thread::Link::Url(u)) => Run::new("a.link", sp.text.as_str()).id(format!("url:{u}")),
1042 None => Run::new(spec, sp.text.as_str()),
1043 };
1044 let r = if (bold || ital) && r.spec.starts_with('a') {
1045 r.css(format!("{}{}", if bold { "font-weight: 700;" } else { "" }, if ital { "font-style: italic" } else { "" }))
1046 } else if spec == "b.i" {
1047 r.css("font-weight: 700; font-style: italic")
1048 } else if spec == "i" {
1049 r.css("font-style: italic")
1050 } else if spec == "b" {
1051 r.css("font-weight: 700")
1052 } else {
1053 r
1054 };
1055 out.push(r);
1056 }
1057 cur.clear();
1058 };
1059 let closes = |from: usize, pat: &[char]| -> bool {
1060 let n = pat.len();
1061 (from..cs.len().saturating_sub(n - 1)).any(|j| cs[j..j + n] == *pat && j > from)
1062 };
1063 let mut i = 0;
1064 while i < cs.len() {
1065 let c = cs[i];
1066 if c == '`' {
1067 if let Some(j) = (i + 1..cs.len()).find(|&j| cs[j] == '`') {
1068 flush(&mut out, &mut cur, bold, ital);
1069 let code: String = cs[i + 1..j].iter().collect();
1070 if !code.is_empty() {
1071 out.push(Run::new("code", code));
1072 }
1073 i = j + 1;
1074 continue;
1075 }
1076 }
1077 if c == '*' && i + 1 < cs.len() && cs[i + 1] == '*' && (bold || closes(i + 2, &['*', '*'])) {
1078 flush(&mut out, &mut cur, bold, ital);
1079 bold = !bold;
1080 i += 2;
1081 continue;
1082 }
1083 if c == '*' && (ital || (i + 1 < cs.len() && !cs[i + 1].is_whitespace() && closes(i + 1, &['*']))) {
1084 flush(&mut out, &mut cur, bold, ital);
1085 ital = !ital;
1086 i += 1;
1087 continue;
1088 }
1089 if c == '_' {
1090 let word_before = i > 0 && cs[i - 1].is_alphanumeric();
1091 let word_after = i + 1 < cs.len() && cs[i + 1].is_alphanumeric();
1092 let opens = !ital
1093 && !word_before
1094 && i + 1 < cs.len()
1095 && !cs[i + 1].is_whitespace()
1096 && (i + 2..cs.len()).any(|j| cs[j] == '_' && !cs[j - 1].is_whitespace() && (j + 1 == cs.len() || !cs[j + 1].is_alphanumeric()));
1097 if opens || (ital && !word_after) {
1098 flush(&mut out, &mut cur, bold, ital);
1099 ital = !ital;
1100 i += 1;
1101 continue;
1102 }
1103 }
1104 cur.push(c);
1105 i += 1;
1106 }
1107 flush(&mut out, &mut cur, bold, ital);
1108 out
1109}
1110
1111#[derive(Clone, Debug, Default, PartialEq)]
1115pub struct VirtualList {
1116 heights: Vec<Option<f64>>,
1117 pub estimate: f64,
1119 pub gap: f64,
1121}
1122
1123#[derive(Clone, Copy, Debug, Default, PartialEq)]
1125pub struct Window {
1126 pub start: usize,
1127 pub end: usize,
1129 pub top: f64,
1131 pub bottom: f64,
1133}
1134
1135impl VirtualList {
1136 pub fn new(len: usize, estimate: f64) -> Self {
1137 VirtualList { heights: vec![None; len], estimate, gap: 0.0 }
1138 }
1139 pub fn len(&self) -> usize {
1140 self.heights.len()
1141 }
1142 pub fn is_empty(&self) -> bool {
1143 self.heights.is_empty()
1144 }
1145 pub fn set_len(&mut self, n: usize) {
1147 self.heights.resize(n, None);
1148 }
1149 pub fn invalidate(&mut self, i: usize) {
1151 if let Some(h) = self.heights.get_mut(i) {
1152 *h = None;
1153 }
1154 }
1155 fn h(&self, i: usize) -> f64 {
1156 self.heights[i].unwrap_or(self.estimate)
1157 }
1158 pub fn set_height(&mut self, i: usize, h: f64) {
1161 if let Some(x) = self.heights.get_mut(i) {
1162 *x = Some(h);
1163 }
1164 }
1165 pub fn height(&self, i: usize) -> Option<f64> {
1167 self.heights.get(i).copied().flatten()
1168 }
1169 pub fn tops(&self) -> Vec<f64> {
1172 let mut out = Vec::with_capacity(self.len() + 1);
1173 let mut y = 0.0;
1174 for i in 0..self.len() {
1175 out.push(y);
1176 y += self.h(i) + self.gap;
1177 }
1178 out.push(self.total());
1179 out
1180 }
1181 pub fn total(&self) -> f64 {
1183 (0..self.len()).map(|i| self.h(i)).sum::<f64>() + self.gap * self.len().saturating_sub(1) as f64
1184 }
1185 pub fn offset_of(&self, i: usize) -> f64 {
1187 (0..i.min(self.len())).map(|k| self.h(k) + self.gap).sum()
1188 }
1189 pub fn index_at(&self, y: f64) -> usize {
1191 let mut acc = 0.0;
1192 for i in 0..self.len() {
1193 acc += self.h(i) + self.gap;
1194 if y < acc {
1195 return i;
1196 }
1197 }
1198 self.len().saturating_sub(1)
1199 }
1200 pub fn window(&self, scroll: f64, view: f64, overscan: f64) -> Window {
1202 if self.is_empty() {
1203 return Window::default();
1204 }
1205 let a = (scroll - overscan).max(0.0);
1206 let b = scroll + view + overscan;
1207 let start = self.index_at(a);
1208 let mut end = start;
1209 let mut y = self.offset_of(start);
1210 while end < self.len() && y < b {
1211 y += self.h(end) + self.gap;
1212 end += 1;
1213 }
1214 let top = self.offset_of(start);
1215 let bottom = (self.total() - self.offset_of(end)).max(0.0);
1216 Window { start, end, top, bottom }
1217 }
1218 pub fn el(&self, container: El, w: Window, mut row: impl FnMut(usize) -> El) -> El {
1222 let mut c = container;
1223 if w.start > 0 {
1224 c = c.child(El::new("div.k-vspace").css(format!("height: {}px", (w.top - self.gap).max(0.0))));
1225 }
1226 for i in w.start..w.end {
1227 c = c.child(row(i));
1228 }
1229 if w.end < self.len() {
1230 c = c.child(El::new("div.k-vspace").css(format!("height: {}px", w.bottom)));
1231 }
1232 c
1233 }
1234 pub fn measure(&mut self, dom: &Dom, w: Window, id_of: impl Fn(usize) -> String) -> bool {
1237 let mut changed = false;
1238 for i in w.start..w.end.min(self.len()) {
1239 if let Some(r) = dom.rect(&id_of(i)) {
1240 if self.heights[i].is_none_or(|h| (h - r.height).abs() > 0.25) {
1241 self.heights[i] = Some(r.height);
1242 changed = true;
1243 }
1244 }
1245 }
1246 changed
1247 }
1248}
1249
1250#[cfg(all(test, feature = "flow-flex"))]
1251mod tests {
1252 use super::*;
1253 use crate::testing::NullRenderContext;
1254 use crate::ui::dom::lay_out_in;
1255 use crate::ui::style::State;
1256
1257 #[test]
1258 fn number_spec_parses_steps_and_tidies() {
1259 let s = NumberSpec::new().range(0.0, 10.0).step(0.5);
1260 assert_eq!(s.parse("3,5"), Some(3.5), "a decimal comma reads as a point");
1261 assert_eq!(s.parse("42"), Some(10.0), "clamped");
1262 assert_eq!(s.parse("-"), None, "not a number yet");
1263 assert_eq!(s.sanitize("1 234,5 $"), "1234,5");
1264 assert_eq!(s.stepped("3.5", 1), "4");
1265 assert_eq!(s.stepped("3.2", 1), "3.5", "off the grid: up to the next step");
1266 assert_eq!(s.stepped("3.2", -1), "3", "and down to the previous one");
1267 assert_eq!(s.stepped("9.8", 3), "10", "clamped at the top");
1268 assert_eq!(s.stepped("", 1), "0.5", "an empty field steps from the start");
1269 assert_eq!(s.normalized("7,25"), "7.25");
1270 assert_eq!(s.normalized("abc"), "abc");
1271 let i = NumberSpec::integer().min(1.0);
1272 assert!(!i.accepts('.') && i.accepts('7'));
1273 assert_eq!(i.stepped("0", 1), "2", "from below the range: clamped, then a step");
1274 assert_eq!(NumberSpec::new().step(0.1).stepped("0.2", 1), "0.3", "no float noise");
1275 assert_eq!(NumberSpec::new().decimals(2).format(1.0), "1.00");
1276 }
1277
1278 #[test]
1279 fn toolbar_lays_out_groups_hits_and_an_end_group() {
1280 let mut ctx = NullRenderContext::default();
1281 let sheet = sheet_with(TOOLBAR_CSS).unwrap();
1282 let items = [
1283 ToolItem::button("cursor", icon(icons::PLUS)).on(true),
1284 ToolItem::split("line", icon(icons::EDIT)),
1285 ToolItem::separator(),
1286 ToolItem::button("lock", icon(icons::PIN)).disabled(true),
1287 ToolItem::fill(),
1288 ToolItem::button("trash", icon(icons::TRASH)),
1289 ];
1290 let row = El::new("div").css("width: 400px").child(toolbar("tb", &items, false));
1291 let dom = lay_out_in(&mut ctx, &sheet, &row, 0.0, 0.0, 400.0, None, &|_| State::NONE);
1292 let ids: Vec<&str> = dom.hits().map(|(id, _)| id).collect();
1293 assert_eq!(ids, ["tb:bar", "tb:cursor", "tb:line", "tb:line:menu", "tb:trash"], "no hit for the disabled tool");
1294 let (c, l, m, t) = (dom.rect("tb:cursor").unwrap(), dom.rect("tb:line").unwrap(), dom.rect("tb:line:menu").unwrap(), dom.rect("tb:trash").unwrap());
1295 assert_eq!((c.x, c.y, c.width, c.height), (4.0, 4.0, 32.0, 32.0));
1296 assert_eq!(l.x, 40.0, "one gap after the first tool");
1297 assert_eq!(m.x, l.x + l.width, "the chevron hugs its button");
1298 assert_eq!(t.x + t.width, 396.0, "the end group sits at the far edge, inside the padding");
1299 assert_eq!(dom.get("tb:cursor").and_then(|p| p.style.background.as_deref()), Some("rgba(59,130,246,0.18)"), "lit");
1300 assert_eq!(picked_tool("tb", "tb:line:menu"), Some(("line", true)));
1301 assert_eq!(picked_tool("tb", "tb:trash"), Some(("trash", false)));
1302 assert_eq!(picked_tool("tb", "tb:bar"), None);
1303
1304 let col = El::new("div").css("height: 300px; display: flex; flex-direction: column").child(toolbar("v", &items, true).css("flex-grow: 1"));
1305 let dom = lay_out_in(&mut ctx, &sheet, &col, 0.0, 0.0, 48.0, None, &|_| State::NONE);
1306 let (c, l, t) = (dom.rect("v:cursor").unwrap(), dom.rect("v:line").unwrap(), dom.rect("v:trash").unwrap());
1307 assert_eq!((c.x, c.y), (8.0, 4.0), "a column, tools centred across it");
1308 assert_eq!(l.y, 40.0);
1309 assert_eq!(t.y + t.height, 296.0, "the end group at the bottom");
1310 }
1311
1312 #[test]
1313 fn flyouts_open_beside_their_tool_and_stay_on_screen() {
1314 let vp = Rect::new(0.0, 0.0, 800.0, 600.0);
1315 let r = flyout_at(Rect::new(4.0, 100.0, 32.0, 32.0), vp, 200.0, 150.0, true);
1316 assert_eq!((r.x, r.y), (40.0, 100.0), "right of a vertical toolbar's tool");
1317 let r = flyout_at(Rect::new(700.0, 4.0, 32.0, 32.0), vp, 200.0, 150.0, false);
1318 assert_eq!((r.x, r.y), (600.0, 40.0), "below, pulled back inside");
1319 let r = flyout_at(Rect::new(4.0, 560.0, 32.0, 32.0), vp, 200.0, 150.0, true);
1320 assert_eq!(r.y, 450.0, "lifted to fit");
1321 }
1322
1323 #[test]
1324 fn number_input_has_the_field_and_steppers() {
1325 fn walk(e: &El, out: &mut Vec<String>) {
1326 if let Some(i) = &e.id {
1327 out.push(i.clone());
1328 }
1329 for c in &e.children {
1330 walk(c, out);
1331 }
1332 }
1333 let mut ids = Vec::new();
1334 walk(&number_input("qty", Edit::new("3"), "Quantity"), &mut ids);
1335 assert_eq!(ids, ["qty", "qty:inc", "qty:dec"]);
1336 }
1337
1338 #[test]
1339 fn markdown_bold_italic_code_links() {
1340 let r = inline_md("a **b** *i* _u_ snake_case ***bi*** `x*y` see https://e.x @bob");
1341 let specs: Vec<(&str, &str)> = r.iter().map(|r| (r.spec.as_str(), r.text.as_str())).collect();
1342 assert!(specs.contains(&("b", "b")));
1343 assert!(specs.contains(&("i", "i")));
1344 assert!(specs.contains(&("i", "u")));
1345 assert!(specs.contains(&("b.i", "bi")));
1346 assert!(specs.contains(&("code", "x*y")));
1347 assert!(specs.iter().any(|(s, t)| *s == "span" && t.contains("snake_case")));
1348 assert!(r.iter().any(|r| r.spec == "a.link" && r.id.as_deref() == Some("url:https://e.x")));
1349 assert!(r.iter().any(|r| r.spec == "a.mention" && r.id.as_deref() == Some("nick:bob")));
1350 let r = inline_md("2 * 3 and *open");
1352 assert_eq!(r.iter().map(|r| r.text.as_str()).collect::<String>(), "2 * 3 and *open");
1353 }
1354
1355 #[test]
1356 fn virtual_list_windows_and_learns() {
1357 let mut v = VirtualList::new(1000, 40.0);
1358 let w = v.window(4000.0, 400.0, 0.0);
1359 assert_eq!((w.start, w.end), (100, 110));
1360 assert_eq!(w.top, 4000.0);
1361 assert_eq!(w.top + (w.end - w.start) as f64 * 40.0 + w.bottom, v.total());
1362 let mut ctx = NullRenderContext::default();
1363 let el = v.el(El::new("div").css("display:flex;flex-direction:column"), w, |i| El::new("div").id(format!("r{i}")).css(if i % 2 == 0 { "height:60px" } else { "height:40px" }));
1364 let dom = crate::ui::dom::lay_out(&mut ctx, &sheet(), &el, 0.0, 0.0, 300.0, &|_| State::NONE);
1365 assert!(v.measure(&dom, w, |i| format!("r{i}")));
1366 assert_eq!(v.total(), 990.0 * 40.0 + 5.0 * 60.0 + 5.0 * 40.0);
1367 let mut v = VirtualList::new(3, 10.0);
1369 v.set_height(1, 30.0);
1370 assert_eq!((v.height(0), v.height(1)), (None, Some(30.0)));
1371 assert_eq!(v.tops(), vec![0.0, 10.0, 40.0, 50.0]);
1372 }
1373
1374 #[test]
1375 fn select_popover_is_fixed_and_skips_disabled() {
1376 let opts = [SelectOpt::new("Auto").sub("follow the system"), SelectOpt::new("Dark"), SelectOpt::new("Light").disabled(true)];
1377 let page = El::new("div.k-root")
1378 .css("height: 2000px")
1379 .child(select_trigger("theme", "Auto", true, "Theme"))
1380 .child(select_popover("theme", &opts, Some(0), Rect::new(10.0, 50.0, 200.0, 120.0)));
1381 let mut ctx = NullRenderContext::default();
1382 let vp = Rect::new(0.0, 0.0, 800.0, 600.0);
1383 let dom = lay_out_in(&mut ctx, &sheet(), &page, 0.0, 0.0, 800.0, Some(vp), &|_| State::NONE);
1384 assert!(dom.rect("theme:opt:0").is_some() && dom.rect("theme:opt:1").is_some());
1385 assert!(dom.rect("theme:opt:2").is_none());
1386 let pop = dom.rect("theme:pop").unwrap();
1387 assert_eq!((pop.x, pop.y, pop.width), (10.0, 50.0, 200.0));
1388 let moved = dom.clone().moved(0.0, -300.0);
1390 assert_eq!(moved.rect("theme:pop"), Some(pop));
1391 assert_eq!(moved.rect("theme").map(|r| r.y), dom.rect("theme").map(|r| r.y - 300.0));
1392 assert_eq!(moved.hit(20.0, 60.0), Some("theme:opt:0"));
1393 assert_eq!(picked_opt("theme", "theme:opt:1"), Some(1));
1394 }
1395
1396 #[test]
1397 fn select_list_windows_highlights_and_searches() {
1398 let opts: Vec<SelectOpt> = (0..20).map(|i| SelectOpt::new(format!("Zone {i}"))).collect();
1399 let view = ListView { hi: Some(6), top: 5, rows: 4, query: Some("zo"), row_h: 30.0 };
1400 let pop = select_list("tz", &opts, Some(7), &view, Rect::new(0.0, 0.0, 240.0, 200.0));
1401 let mut ctx = NullRenderContext::default();
1402 let vp = Rect::new(0.0, 0.0, 800.0, 600.0);
1403 let dom = lay_out_in(&mut ctx, &sheet(), &pop, 0.0, 0.0, 800.0, Some(vp), &|_| State::NONE);
1404 let shown: Vec<&str> = dom.hits().map(|(id, _)| id).filter(|id| id.starts_with("tz:opt:")).collect();
1405 assert_eq!(shown, ["tz:opt:5", "tz:opt:6", "tz:opt:7", "tz:opt:8"]);
1406 assert_eq!(dom.get("tz:opt:6").and_then(|p| p.style.background.as_deref()), Some("#33363d"));
1407 assert!(dom.get("tz:opt:5").is_some_and(|p| p.style.background.is_none()));
1408 let row = dom.rect("tz:opt:5").unwrap();
1409 assert!(row.y > 30.0, "below the search line");
1410 let none = select_list("tz", &[], None, &ListView { query: Some(""), ..ListView::all() }, Rect::new(0.0, 0.0, 240.0, 80.0));
1411 let dom = lay_out_in(&mut ctx, &sheet(), &none, 0.0, 0.0, 800.0, Some(vp), &|_| State::NONE);
1412 assert!(dom.hits().all(|(id, _)| !id.contains(":opt:")));
1413 }
1414
1415 #[test]
1416 fn controls_are_css_styled_and_named() {
1417 let page = El::new("div.k-root")
1418 .child(switch("wifi", true, "Wi-Fi"))
1419 .child(checkbox("agree", false, "I agree"))
1420 .child(input("q", Edit::new("hello").placeholder("Search").caret(5)))
1421 .child(spinner(0.3));
1422 let mut ctx = NullRenderContext::default();
1423 let dom = crate::ui::dom::lay_out(&mut ctx, &sheet(), &page, 0.0, 0.0, 400.0, &|_| State::NONE);
1424 let sw = dom.get("wifi").unwrap();
1425 assert_eq!((sw.rect.width, sw.rect.height), (36.0, 20.0));
1426 assert_eq!(sw.style.background.as_deref(), Some("#3b82f6"));
1428 assert_eq!(dom.get("q").unwrap().style.border_color.as_deref(), Some("#3b82f6"));
1430 let a = dom.a11y();
1431 assert!(a.iter().any(|n| n.id == "wifi" && n.role == "switch" && n.name == "Wi-Fi" && n.checked == Some(true)));
1432 assert!(a.iter().any(|n| n.id == "q" && n.role == "textbox" && n.name == "hello"));
1433 assert!(a.iter().any(|n| n.id == "agree" && n.name == "I agree"));
1434 assert!(dom.get("q").unwrap().caret_rect().is_some());
1435 }
1436
1437 #[test]
1438 fn light_vars_retheme_the_whole_kit() {
1439 let page = |vars: &str| {
1440 El::new("div.k-root")
1441 .css(vars)
1442 .child(button("b", "Save"))
1443 .child(input("q", Edit::new("x")))
1444 .child(tabs("t", &["A", "B"], 1))
1445 };
1446 let mut ctx = NullRenderContext::default();
1447 let dark = crate::ui::dom::lay_out(&mut ctx, &sheet(), &page(theme_vars(true)), 0.0, 0.0, 400.0, &|_| State::NONE);
1448 let light = crate::ui::dom::lay_out(&mut ctx, &sheet(), &page(theme_vars(false)), 0.0, 0.0, 400.0, &|_| State::NONE);
1449 assert_eq!(dark.get("b").and_then(|p| p.style.background.as_deref()), Some("#2b2e34"));
1450 assert_eq!(light.get("b").and_then(|p| p.style.background.as_deref()), Some("#eceef2"));
1451 assert_eq!(light.get("q").and_then(|p| p.style.background.as_deref()), Some("#ffffff"));
1452 assert_eq!(light.get("t:tab:1").and_then(|p| p.style.color.as_deref()), Some("#1d1f24"));
1453 for line in KIT_CSS.lines().filter(|l| !l.starts_with(":root") && !l.starts_with(" --")) {
1455 if line.contains("k-msg") || line.contains("k-body") || line.contains("k-banner") || line.contains("k-badge") || line.contains("k-toast") {
1456 continue;
1457 }
1458 let rest = line.replace("--knob: #ffffff", "").replace("--check: #ffffff", "");
1459 assert!(!rest.contains(": #") && !rest.contains(" #"), "hard-coded colour in {line}");
1460 }
1461 }
1462
1463 #[test]
1464 fn tabs_segmented_list_table_hits() {
1465 let rows = vec![vec!["alpha".to_string(), "7".to_string()], vec!["beta".to_string(), "14".to_string()]];
1466 let page = El::new("div.k-root")
1467 .child(tabs("tb", &["One", "Two", "Three"], 0))
1468 .child(segmented("theme", &["Dark", "Light"], 1))
1469 .child(list("ls", &[("alpha", Some("3")), ("beta", None)], Some(1)))
1470 .child(table("tt", &["Name", "Count"], "1fr 80px", &[1], &rows, Some(0)));
1471 let mut ctx = NullRenderContext::default();
1472 let dom = crate::ui::dom::lay_out(&mut ctx, &sheet(), &page, 0.0, 0.0, 400.0, &|_| State::NONE);
1473 let (a, b) = (dom.rect("tb:tab:0").unwrap(), dom.rect("tb:tab:2").unwrap());
1474 assert!(b.x > a.x && (a.y - b.y).abs() < 0.01, "tabs share a row");
1475 assert!(dom.get("theme:1").is_some_and(|p| p.style.background.as_deref() == Some("#3b82f6")));
1476 assert_eq!(picked("theme", "", "theme:1"), Some(1));
1477 assert_eq!(picked("tb", "tab", "tb:tab:2"), Some(2));
1478 assert_eq!(picked("tt", "row", "tb:tab:2"), None);
1479 assert!(dom.get("ls:row:1").is_some_and(|p| p.style.background.is_some()));
1480 let row = dom.rect("tt:row:0").unwrap();
1482 assert!(row.height >= 30.0);
1483 assert!(dom.get("tt:row:0").is_some_and(|p| p.style.background.is_some()), ".sel row tinted");
1484 }
1485
1486 #[test]
1487 fn menu_dialog_toasts_are_fixed_layers() {
1488 let vp = Rect::new(0.0, 0.0, 800.0, 600.0);
1489 let items = [MenuItem::new("Rename").kbd("F2"), MenuItem::separator(), MenuItem::new("Locked").disabled(true), MenuItem::new("Delete").danger(true)];
1490 let page = El::new("div.k-root")
1491 .css("height: 2000px")
1492 .child(menu("m", Some("Row"), &items, Rect::new(100.0, 100.0, 200.0, 0.0)))
1493 .child(dialog("d", "Details", El::new("div").text("Body"), &["OK", "Cancel"], centered(vp, 360.0, 200.0)))
1494 .child(toasts("ts", &[("ok", "Saved"), ("", "Synced")]))
1495 .child(tooltip("Saves the name", Rect::new(10.0, 10.0, 80.0, 30.0)));
1496 let mut ctx = NullRenderContext::default();
1497 let dom = lay_out_in(&mut ctx, &sheet(), &page, 0.0, 0.0, 800.0, Some(vp), &|_| State::NONE);
1498 assert!(dom.rect("m:item:0").is_some() && dom.rect("m:item:3").is_some());
1499 assert!(dom.rect("m:item:2").is_none(), "disabled item is not a hit");
1500 assert_eq!(dom.rect("m:menu").map(|r| (r.x, r.y)), Some((100.0, 100.0)));
1501 let d = dom.rect("d").unwrap();
1502 assert_eq!((d.x, d.y, d.width, d.height), (220.0, 200.0, 360.0, 200.0));
1503 assert_eq!(dom.rect("d:scrim"), Some(vp), "scrim covers the viewport");
1504 assert_eq!(dom.hit(400.0, 300.0).map(|s| s.starts_with("d")), Some(true));
1506 assert_eq!(dom.hit(10.0, 590.0), Some("d:scrim"));
1507 let t = dom.rect("ts").unwrap();
1508 assert!((t.x + t.width - 784.0).abs() < 0.5 && (t.y + t.height - 584.0).abs() < 0.5, "toasts sit 16px from the bottom-right: {t:?}");
1509 assert!(dom.rect("ts:1:close").is_some());
1510 let moved = dom.clone().moved(0.0, -500.0);
1511 assert_eq!(moved.rect("d"), Some(d), "fixed layers do not scroll");
1512 }
1513
1514 #[test]
1515 fn stat_progress_badge_field() {
1516 let page = El::new("div.k-root").css("width: 300px")
1517 .child(stat("s", "FRAMES", "1 204", "+12 / s"))
1518 .child(progress(0.25))
1519 .child(badge("live", "ok"))
1520 .child(field("Name", input("n", Edit::new(""))));
1521 let mut ctx = NullRenderContext::default();
1522 let dom = crate::ui::dom::lay_out(&mut ctx, &sheet(), &page, 0.0, 0.0, 300.0, &|_| State::NONE);
1523 assert!(dom.rect("s").is_some_and(|r| r.height > 40.0));
1524 let bar = dom.boxes.iter().find(|b| b.style.background.as_deref() == Some("#3b82f6") && b.rect.height == 6.0).expect("bar");
1525 assert!((bar.rect.width - 75.0).abs() < 0.5, "25% of 300: {:?}", bar.rect);
1526 assert!(dom.rect("n").is_some());
1527 }
1528
1529 #[test]
1530 fn coarse_pointer_grows_targets_to_touch_size() {
1531 use crate::ui::media::{Edges, MediaEnv};
1532 let css = [KIT_CSS, KIT_TOUCH_CSS].concat();
1533 let touch = Sheet::parse_with(&css, &MediaEnv::touch(390.0, 844.0, Edges::ZERO)).unwrap();
1534 let mouse = Sheet::parse_with(&css, &MediaEnv::default()).unwrap();
1535 assert_eq!(mouse.el("div.k-li").height, Some(30.0));
1536 assert_eq!(touch.el("div.k-li").height, Some(48.0));
1537 assert_eq!(touch.el("div.k-input").height, Some(44.0));
1538 assert_eq!(touch.el("select.k-select").height, Some(44.0));
1539 assert_eq!(touch.el("button.k-btn").min_height, Some(44.0));
1540 assert_eq!(mouse.el("button.k-btn").min_height, None);
1541 assert_eq!(touch.el("button.k-btn").background, mouse.el("button.k-btn").background);
1543 }
1544
1545}