openkit 0.1.3

A cross-platform CSS-styled UI framework for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! WGSL shaders for GPU-accelerated rendering.

/// Shader for rendering rectangles with rounded corners and gradients.
pub const RECT_SHADER: &str = r#"
// Vertex shader
struct VertexInput {
    @location(0) position: vec2<f32>,
    @location(1) uv: vec2<f32>,
    @location(2) color: vec4<f32>,
    @location(3) rect_bounds: vec4<f32>,  // x, y, width, height
    @location(4) corner_radii: vec4<f32>, // top_left, top_right, bottom_right, bottom_left
    @location(5) params: vec4<f32>,       // gradient_angle, border_width, flags, unused
}

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) uv: vec2<f32>,
    @location(1) color: vec4<f32>,
    @location(2) rect_bounds: vec4<f32>,
    @location(3) corner_radii: vec4<f32>,
    @location(4) local_pos: vec2<f32>,
    @location(5) params: vec4<f32>,
}

@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    out.clip_position = vec4<f32>(in.position, 0.0, 1.0);
    out.uv = in.uv;
    out.color = in.color;
    out.rect_bounds = in.rect_bounds;
    out.corner_radii = in.corner_radii;
    out.local_pos = in.uv * in.rect_bounds.zw;
    out.params = in.params;
    return out;
}

// Fragment shader
fn rounded_box_sdf(p: vec2<f32>, size: vec2<f32>, radii: vec4<f32>) -> f32 {
    // Select corner radius based on quadrant
    var r: f32;
    if (p.x > 0.0) {
        if (p.y > 0.0) {
            r = radii.z; // bottom-right
        } else {
            r = radii.y; // top-right
        }
    } else {
        if (p.y > 0.0) {
            r = radii.w; // bottom-left
        } else {
            r = radii.x; // top-left
        }
    }

    let q = abs(p) - size + vec2<f32>(r);
    return min(max(q.x, q.y), 0.0) + length(max(q, vec2<f32>(0.0))) - r;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let size = in.rect_bounds.zw;
    let center = size * 0.5;
    let p = in.local_pos - center;

    // Calculate SDF for rounded rectangle
    let d = rounded_box_sdf(p, center, in.corner_radii);

    // Anti-aliased edge
    let aa = 1.0;
    let alpha = 1.0 - smoothstep(-aa, aa, d);

    // Border rendering
    let border_width = in.params.y;
    if (border_width > 0.0) {
        let inner_d = rounded_box_sdf(p, center - vec2<f32>(border_width), in.corner_radii - vec4<f32>(border_width));
        let border_alpha = smoothstep(-aa, aa, inner_d);
        return vec4<f32>(in.color.rgb, in.color.a * alpha * border_alpha);
    }

    return vec4<f32>(in.color.rgb, in.color.a * alpha);
}
"#;

/// Shader for rendering gradients.
pub const GRADIENT_SHADER: &str = r#"
struct VertexInput {
    @location(0) position: vec2<f32>,
    @location(1) uv: vec2<f32>,
    @location(2) color_start: vec4<f32>,
    @location(3) color_end: vec4<f32>,
    @location(4) rect_bounds: vec4<f32>,
    @location(5) corner_radii: vec4<f32>,
    @location(6) gradient_params: vec4<f32>, // angle, type, unused, unused
}

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) uv: vec2<f32>,
    @location(1) color_start: vec4<f32>,
    @location(2) color_end: vec4<f32>,
    @location(3) rect_bounds: vec4<f32>,
    @location(4) corner_radii: vec4<f32>,
    @location(5) local_pos: vec2<f32>,
    @location(6) gradient_params: vec4<f32>,
}

@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    out.clip_position = vec4<f32>(in.position, 0.0, 1.0);
    out.uv = in.uv;
    out.color_start = in.color_start;
    out.color_end = in.color_end;
    out.rect_bounds = in.rect_bounds;
    out.corner_radii = in.corner_radii;
    out.local_pos = in.uv * in.rect_bounds.zw;
    out.gradient_params = in.gradient_params;
    return out;
}

fn rounded_box_sdf(p: vec2<f32>, size: vec2<f32>, radii: vec4<f32>) -> f32 {
    var r: f32;
    if (p.x > 0.0) {
        if (p.y > 0.0) { r = radii.z; } else { r = radii.y; }
    } else {
        if (p.y > 0.0) { r = radii.w; } else { r = radii.x; }
    }
    let q = abs(p) - size + vec2<f32>(r);
    return min(max(q.x, q.y), 0.0) + length(max(q, vec2<f32>(0.0))) - r;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let size = in.rect_bounds.zw;
    let center = size * 0.5;
    let p = in.local_pos - center;

    // Calculate gradient factor based on angle
    let angle = in.gradient_params.x;
    let dir = vec2<f32>(cos(angle), sin(angle));
    let gradient_t = dot(in.uv - 0.5, dir) + 0.5;

    // Interpolate colors
    let color = mix(in.color_start, in.color_end, clamp(gradient_t, 0.0, 1.0));

    // Apply rounded rect mask
    let d = rounded_box_sdf(p, center, in.corner_radii);
    let alpha = 1.0 - smoothstep(-1.0, 1.0, d);

    return vec4<f32>(color.rgb, color.a * alpha);
}
"#;

/// Shader for rendering text (glyph atlas).
pub const TEXT_SHADER: &str = r#"
struct VertexInput {
    @location(0) position: vec2<f32>,
    @location(1) uv: vec2<f32>,
    @location(2) color: vec4<f32>,
}

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) uv: vec2<f32>,
    @location(1) color: vec4<f32>,
}

@group(0) @binding(0) var t_glyph: texture_2d<f32>;
@group(0) @binding(1) var s_glyph: sampler;

@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    out.clip_position = vec4<f32>(in.position, 0.0, 1.0);
    out.uv = in.uv;
    out.color = in.color;
    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let alpha = textureSample(t_glyph, s_glyph, in.uv).r;
    return vec4<f32>(in.color.rgb, in.color.a * alpha);
}
"#;

/// Shader for rendering images/textures.
pub const IMAGE_SHADER: &str = r#"
struct VertexInput {
    @location(0) position: vec2<f32>,
    @location(1) uv: vec2<f32>,
    @location(2) tint: vec4<f32>,
    @location(3) rect_bounds: vec4<f32>,
    @location(4) corner_radii: vec4<f32>,
}

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) uv: vec2<f32>,
    @location(1) tint: vec4<f32>,
    @location(2) rect_bounds: vec4<f32>,
    @location(3) corner_radii: vec4<f32>,
    @location(4) local_pos: vec2<f32>,
}

@group(0) @binding(0) var t_image: texture_2d<f32>;
@group(0) @binding(1) var s_image: sampler;

@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    out.clip_position = vec4<f32>(in.position, 0.0, 1.0);
    out.uv = in.uv;
    out.tint = in.tint;
    out.rect_bounds = in.rect_bounds;
    out.corner_radii = in.corner_radii;
    out.local_pos = in.uv * in.rect_bounds.zw;
    return out;
}

fn rounded_box_sdf(p: vec2<f32>, size: vec2<f32>, radii: vec4<f32>) -> f32 {
    var r: f32;
    if (p.x > 0.0) {
        if (p.y > 0.0) { r = radii.z; } else { r = radii.y; }
    } else {
        if (p.y > 0.0) { r = radii.w; } else { r = radii.x; }
    }
    let q = abs(p) - size + vec2<f32>(r);
    return min(max(q.x, q.y), 0.0) + length(max(q, vec2<f32>(0.0))) - r;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let size = in.rect_bounds.zw;
    let center = size * 0.5;
    let p = in.local_pos - center;

    let tex_color = textureSample(t_image, s_image, in.uv);
    let color = tex_color * in.tint;

    // Apply rounded rect mask
    let d = rounded_box_sdf(p, center, in.corner_radii);
    let alpha = 1.0 - smoothstep(-1.0, 1.0, d);

    return vec4<f32>(color.rgb, color.a * alpha);
}
"#;

/// Gaussian blur shader (horizontal pass).
pub const BLUR_H_SHADER: &str = r#"
struct BlurParams {
    direction: vec2<f32>,
    radius: f32,
    _padding: f32,
}

@group(0) @binding(0) var t_input: texture_2d<f32>;
@group(0) @binding(1) var s_input: sampler;
@group(0) @binding(2) var<uniform> params: BlurParams;

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) uv: vec2<f32>,
}

@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
    var out: VertexOutput;
    // Full-screen triangle
    let x = f32((vertex_index & 1u) << 2u) - 1.0;
    let y = f32((vertex_index & 2u) << 1u) - 1.0;
    out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
    out.uv = vec2<f32>((x + 1.0) * 0.5, (1.0 - y) * 0.5);
    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let tex_size = vec2<f32>(textureDimensions(t_input));
    let pixel_size = 1.0 / tex_size;

    var color = vec4<f32>(0.0);
    var total_weight = 0.0;

    let radius = i32(params.radius);
    for (var i = -radius; i <= radius; i++) {
        let offset = vec2<f32>(f32(i)) * params.direction * pixel_size;
        let weight = exp(-f32(i * i) / (2.0 * params.radius * params.radius));
        color += textureSample(t_input, s_input, in.uv + offset) * weight;
        total_weight += weight;
    }

    return color / total_weight;
}
"#;

/// Shadow shader.
pub const SHADOW_SHADER: &str = r#"
struct ShadowParams {
    color: vec4<f32>,
    offset: vec2<f32>,
    blur: f32,
    spread: f32,
}

struct VertexInput {
    @location(0) position: vec2<f32>,
    @location(1) uv: vec2<f32>,
    @location(2) rect_bounds: vec4<f32>,
    @location(3) corner_radii: vec4<f32>,
}

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) local_pos: vec2<f32>,
    @location(1) rect_bounds: vec4<f32>,
    @location(2) corner_radii: vec4<f32>,
}

@group(0) @binding(0) var<uniform> params: ShadowParams;

@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    out.clip_position = vec4<f32>(in.position + params.offset / 100.0, 0.0, 1.0);
    out.local_pos = in.uv * in.rect_bounds.zw;
    out.rect_bounds = in.rect_bounds;
    out.corner_radii = in.corner_radii;
    return out;
}

fn rounded_box_sdf(p: vec2<f32>, size: vec2<f32>, radii: vec4<f32>) -> f32 {
    var r: f32;
    if (p.x > 0.0) {
        if (p.y > 0.0) { r = radii.z; } else { r = radii.y; }
    } else {
        if (p.y > 0.0) { r = radii.w; } else { r = radii.x; }
    }
    let q = abs(p) - size + vec2<f32>(r);
    return min(max(q.x, q.y), 0.0) + length(max(q, vec2<f32>(0.0))) - r;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let size = in.rect_bounds.zw + vec2<f32>(params.spread * 2.0);
    let center = size * 0.5;
    let p = in.local_pos - center + vec2<f32>(params.spread);

    let d = rounded_box_sdf(p, center, in.corner_radii);

    // Soft shadow falloff
    let shadow_alpha = 1.0 - smoothstep(-params.blur, params.blur, d);

    return vec4<f32>(params.color.rgb, params.color.a * shadow_alpha);
}
"#;

/// Glow effect shader.
pub const GLOW_SHADER: &str = r#"
struct GlowParams {
    color: vec4<f32>,
    intensity: f32,
    radius: f32,
    _padding: vec2<f32>,
}

@group(0) @binding(0) var t_input: texture_2d<f32>;
@group(0) @binding(1) var s_input: sampler;
@group(0) @binding(2) var<uniform> params: GlowParams;

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) uv: vec2<f32>,
}

@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
    var out: VertexOutput;
    let x = f32((vertex_index & 1u) << 2u) - 1.0;
    let y = f32((vertex_index & 2u) << 1u) - 1.0;
    out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
    out.uv = vec2<f32>((x + 1.0) * 0.5, (1.0 - y) * 0.5);
    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let tex_size = vec2<f32>(textureDimensions(t_input));
    let pixel_size = 1.0 / tex_size;

    var glow = vec4<f32>(0.0);
    var total_weight = 0.0;

    let radius = i32(params.radius);
    for (var y = -radius; y <= radius; y++) {
        for (var x = -radius; x <= radius; x++) {
            let offset = vec2<f32>(f32(x), f32(y)) * pixel_size;
            let dist = length(vec2<f32>(f32(x), f32(y)));
            let weight = exp(-dist * dist / (2.0 * params.radius * params.radius));
            glow += textureSample(t_input, s_input, in.uv + offset) * weight;
            total_weight += weight;
        }
    }

    glow = glow / total_weight;
    let original = textureSample(t_input, s_input, in.uv);

    // Add glow
    return original + glow * params.color * params.intensity;
}
"#;