velox-engine 0.2.0

Velox core UI engine: arena, pod state, 1D fixed-point layout solver, damage tracker, SDF scanline rasterizer, and cross-platform GPU/software backends
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
//! 面向合成器嵌套调试后端的自定义 `Xrgb8888` 帧缓冲窗口宿主运行器。

#[cfg(feature = "winit")]
use core::num::NonZeroU32;
#[cfg(feature = "winit")]
use std::rc::Rc;
#[cfg(feature = "winit")]
use std::string::String;
#[cfg(feature = "winit")]
use std::time::{Duration, Instant};

#[cfg(feature = "winit")]
use winit::application::ApplicationHandler;
#[cfg(feature = "winit")]
use winit::dpi::LogicalSize;
#[cfg(feature = "winit")]
use winit::event::{ElementState, Ime, MouseButton, MouseScrollDelta, WindowEvent};
#[cfg(feature = "winit")]
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
#[cfg(feature = "winit")]
use winit::keyboard::{Key, NamedKey};
#[cfg(feature = "winit")]
use winit::window::{ImePurpose, Window, WindowId};

use crate::prelude::*;

/// 面向合成器嵌套调试后端的自定义 `Xrgb8888` 帧缓冲窗口宿主运行器。
///
/// 使上层合成器(如 `velox-wm`)无需直接依赖 `winit` 或 `softbuffer` crate,即可在嵌套窗口模式下驱动合成帧与输入派发。
pub fn run_nested_compositor_window<S, FInput, FStep>(
    title: &str,
    width: u32,
    height: u32,
    state: S,
    on_input: FInput,
    on_step_frame: FStep,
) where
    S: 'static,
    FInput: FnMut(&mut S, InputEvent) + 'static,
    FStep: FnMut(&mut S, u16) -> (&[Xrgb8888], bool) + 'static,
{
    #[cfg(not(feature = "winit"))]
    {
        let mut state = state;
        let mut on_input = on_input;
        let mut on_step_frame = on_step_frame;
        let _ = (title, width, height, &mut on_input);
        let _ = on_step_frame(&mut state, 16);
    }
    #[cfg(feature = "winit")]
    {
    struct NestedHost<S, FInput, FStep> {
        title: String,
        w: u32,
        h: u32,
        state: S,
        on_input: FInput,
        on_step_frame: FStep,
        window: Option<Rc<Window>>,
        surface: Option<softbuffer::Surface<Rc<Window>, Rc<Window>>>,
        cursor_pos: (i16, i16),
        mouse_down: bool,
        last_tick: Instant,
        needs_redraw: bool,
    }

    impl<S, FInput, FStep> NestedHost<S, FInput, FStep>
    where
        FInput: FnMut(&mut S, InputEvent),
        FStep: FnMut(&mut S, u16) -> (&[Xrgb8888], bool),
    {
        fn present_frame(&mut self, dt_ms: u16) {
            let (fb, keep_polling) = (self.on_step_frame)(&mut self.state, dt_ms.max(1));
            self.needs_redraw = keep_polling;
            let lw = self.w.max(1) as usize;
            let lh = self.h.max(1) as usize;
            if let Some(surf) = &mut self.surface {
                if let Ok(mut out_buf) = surf.buffer_mut() {
                    let pw = out_buf.width().get() as usize;
                    let ph = out_buf.height().get() as usize;
                    let raw_u32: &[u32] = unsafe {
                        core::slice::from_raw_parts(fb.as_ptr() as *const u32, fb.len())
                    };
                    if pw == lw && ph == lh && out_buf.len() >= raw_u32.len() {
                        let len = out_buf.len().min(raw_u32.len());
                        out_buf[..len].copy_from_slice(&raw_u32[..len]);
                    } else {
                        for dy in 0..ph {
                            let sy = (dy * lh / ph).min(lh.saturating_sub(1));
                            let dst_row = dy * pw;
                            let src_row = sy * lw;
                            for dx in 0..pw {
                                let sx = (dx * lw / pw).min(lw.saturating_sub(1));
                                if dst_row + dx < out_buf.len() && src_row + sx < raw_u32.len() {
                                    out_buf[dst_row + dx] = raw_u32[src_row + sx];
                                }
                            }
                        }
                    }
                    let _ = out_buf.present();
                }
            }
        }
    }

    impl<S, FInput, FStep> ApplicationHandler for NestedHost<S, FInput, FStep>
    where
        FInput: FnMut(&mut S, InputEvent),
        FStep: FnMut(&mut S, u16) -> (&[Xrgb8888], bool),
    {
        fn resumed(&mut self, event_loop: &ActiveEventLoop) {
            if self.window.is_some() {
                return;
            }
            let defer_visible = cfg!(any(target_os = "windows", target_os = "macos"))
                || std::env::var_os("WAYLAND_DISPLAY").is_none();
            let attrs = Window::default_attributes()
                .with_title(self.title.clone())
                .with_inner_size(LogicalSize::new(self.w as f64, self.h as f64))
                .with_visible(!defer_visible)
                .with_resizable(false);
            if let Ok(win) = event_loop.create_window(attrs) {
                win.set_cursor_visible(true);
                win.set_ime_allowed(true);
                win.set_ime_purpose(ImePurpose::Normal);
                let phys = win.inner_size();
                let pw = phys.width.max(self.w.max(1));
                let ph = phys.height.max(self.h.max(1));
                let win_rc = Rc::new(win);
                if let Ok(ctx) = softbuffer::Context::new(win_rc.clone()) {
                    if let Ok(mut surf) = softbuffer::Surface::new(&ctx, win_rc.clone()) {
                        let _ = surf.resize(
                            NonZeroU32::new(pw).unwrap(),
                            NonZeroU32::new(ph).unwrap(),
                        );
                        self.surface = Some(surf);
                    }
                }
                self.window = Some(win_rc.clone());
                // 隐身态先提交首帧像素再显窗,彻底避免首屏白屏闪现
                self.last_tick = Instant::now();
                self.present_frame(16);
                if defer_visible {
                    win_rc.set_visible(true);
                    win_rc.focus_window();
                }
                win_rc.request_redraw();
            }
        }

        fn window_event(
            &mut self,
            event_loop: &ActiveEventLoop,
            _id: WindowId,
            event: WindowEvent,
        ) {
            match event {
                WindowEvent::CloseRequested => event_loop.exit(),
                WindowEvent::Resized(size) => {
                    if let Some(surf) = &mut self.surface {
                        if let (Some(nw), Some(nh)) = (
                            NonZeroU32::new(size.width.max(1)),
                            NonZeroU32::new(size.height.max(1)),
                        ) {
                            let _ = surf.resize(nw, nh);
                        }
                    }
                    if let Some(win) = &self.window {
                        win.request_redraw();
                    }
                }
                WindowEvent::CursorMoved { position, .. } => {
                    let (pw, ph) = self
                        .window
                        .as_ref()
                        .map(|w| {
                            let s = w.inner_size();
                            (s.width.max(1) as f64, s.height.max(1) as f64)
                        })
                        .unwrap_or((self.w.max(1) as f64, self.h.max(1) as f64));
                    let px = ((position.x * (self.w as f64)) / pw)
                        .round()
                        .clamp(0.0, (self.w.saturating_sub(1)) as f64)
                        as i16;
                    let py = ((position.y * (self.h as f64)) / ph)
                        .round()
                        .clamp(0.0, (self.h.saturating_sub(1)) as f64)
                        as i16;
                    if (px, py) != self.cursor_pos {
                        self.cursor_pos = (px, py);
                        (self.on_input)(&mut self.state, InputEvent::PointerMove { x: px, y: py });
                        if let Some(win) = &self.window {
                            win.request_redraw();
                        }
                    }
                }
                WindowEvent::MouseInput {
                    state,
                    button: MouseButton::Left,
                    ..
                } => {
                    let (px, py) = self.cursor_pos;
                    match state {
                        ElementState::Pressed => {
                            self.mouse_down = true;
                            (self.on_input)(
                                &mut self.state,
                                InputEvent::PointerKindChanged(PointerKind::Mouse),
                            );
                            (self.on_input)(
                                &mut self.state,
                                InputEvent::PointerDown { x: px, y: py },
                            );
                        }
                        ElementState::Released => {
                            self.mouse_down = false;
                            (self.on_input)(
                                &mut self.state,
                                InputEvent::PointerUp { x: px, y: py },
                            );
                        }
                    }
                    if let Some(win) = &self.window {
                        win.request_redraw();
                    }
                }
                WindowEvent::MouseInput {
                    state,
                    button: MouseButton::Right,
                    ..
                } => {
                    let (px, py) = self.cursor_pos;
                    let ev = match state {
                        ElementState::Pressed => InputEvent::PointerSecondaryDown { x: px, y: py },
                        ElementState::Released => InputEvent::PointerSecondaryUp { x: px, y: py },
                    };
                    (self.on_input)(&mut self.state, ev);
                    if let Some(win) = &self.window {
                        win.request_redraw();
                    }
                }
                WindowEvent::MouseWheel { delta, .. } => {
                    let sy = match delta {
                        MouseScrollDelta::LineDelta(_x, y) => y,
                        MouseScrollDelta::PixelDelta(pos) => (pos.y as f32) / 20.0,
                    };
                    if sy.abs() > 0.01 {
                        let step = if sy > 0.0 { -20 } else { 20 };
                        let (px, py) = self.cursor_pos;
                        (self.on_input)(
                            &mut self.state,
                            InputEvent::Scroll {
                                x: px,
                                y: py,
                                delta_y: step,
                            },
                        );
                        if let Some(win) = &self.window {
                            win.request_redraw();
                        }
                    }
                }
                WindowEvent::ModifiersChanged(mods) => {
                    let state = mods.state();
                    let mut km = KeyModifiers::NONE;
                    if state.shift_key() {
                        km = km.with(KeyModifiers::SHIFT);
                    }
                    if state.control_key() {
                        km = km.with(KeyModifiers::CTRL);
                    }
                    if state.alt_key() {
                        km = km.with(KeyModifiers::ALT);
                    }
                    if state.super_key() {
                        km = km.with(KeyModifiers::LOGO);
                    }
                    (self.on_input)(&mut self.state, InputEvent::ModifiersChanged(km));
                }
                WindowEvent::KeyboardInput { event, .. } => {
                    let pressed = event.state == ElementState::Pressed;
                    let wrap = |kc: KeyCode| {
                        if pressed {
                            InputEvent::KeyDown(kc)
                        } else {
                            InputEvent::KeyUp(kc)
                        }
                    };
                    match &event.logical_key {
                        Key::Named(NamedKey::Escape) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Escape));
                        }
                        Key::Named(NamedKey::Tab) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Tab));
                        }
                        Key::Named(NamedKey::ArrowDown) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Down));
                        }
                        Key::Named(NamedKey::ArrowUp) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Up));
                        }
                        Key::Named(NamedKey::ArrowLeft) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Left));
                        }
                        Key::Named(NamedKey::ArrowRight) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Right));
                        }
                        Key::Named(NamedKey::Enter) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Enter));
                        }
                        Key::Named(NamedKey::Space) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Space));
                        }
                        Key::Named(NamedKey::Backspace) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Backspace));
                        }
                        Key::Named(NamedKey::Delete) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Delete));
                        }
                        Key::Named(NamedKey::Home) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::Home));
                        }
                        Key::Named(NamedKey::End) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::End));
                        }
                        Key::Named(NamedKey::PageUp) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::PageUp));
                        }
                        Key::Named(NamedKey::PageDown) => {
                            (self.on_input)(&mut self.state, wrap(KeyCode::PageDown));
                        }
                        Key::Character(s) => {
                            for ch in s.chars() {
                                (self.on_input)(&mut self.state, wrap(KeyCode::Char(ch)));
                            }
                        }
                        _ => {}
                    }
                    if let Some(win) = &self.window {
                        win.request_redraw();
                    }
                }
                WindowEvent::Ime(ime) => {
                    match ime {
                        Ime::Enabled => (self.on_input)(&mut self.state, InputEvent::ImeEnabled),
                        Ime::Disabled => (self.on_input)(&mut self.state, InputEvent::ImeDisabled),
                        Ime::Preedit(text, cursor) => {
                            let preedit = ImePreedit::from_str(&text, cursor);
                            (self.on_input)(&mut self.state, InputEvent::ImePreedit(preedit));
                        }
                        Ime::Commit(text) => {
                            (self.on_input)(
                                &mut self.state,
                                InputEvent::ImePreedit(ImePreedit::empty()),
                            );
                            for ch in text.chars() {
                                (self.on_input)(&mut self.state, InputEvent::TextCommit(ch));
                            }
                        }
                    }
                    if let Some(win) = &self.window {
                        win.request_redraw();
                    }
                }
                WindowEvent::RedrawRequested => {
                    let now = Instant::now();
                    let dt_ms = now.duration_since(self.last_tick).as_millis().min(100) as u16;
                    self.last_tick = now;
                    self.present_frame(dt_ms.max(1));
                }
                _ => {}
            }
        }

        fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
            if self.needs_redraw {
                event_loop.set_control_flow(ControlFlow::WaitUntil(
                    Instant::now() + Duration::from_millis(16),
                ));
                if let Some(win) = &self.window {
                    win.request_redraw();
                }
            } else {
                event_loop.set_control_flow(ControlFlow::Wait);
            }
        }
    }

    if let Ok(event_loop) = EventLoop::new() {
        event_loop.listen_device_events(winit::event_loop::DeviceEvents::Never);
        let mut host = NestedHost {
            title: String::from(title),
            w: width,
            h: height,
            state,
            on_input,
            on_step_frame,
            window: None,
            surface: None,
            cursor_pos: (0, 0),
            mouse_down: false,
            last_tick: Instant::now(),
            needs_redraw: true,
        };
        let _ = event_loop.run_app(&mut host);
    }
    }
}