Skip to main content

blit_widget/
lib.rs

1//! built-in widgets for interaction, layout, popovers, resizing, scrolling, splits and text input
2//!
3//! `no_std` with `alloc`
4
5#![no_std]
6
7extern crate alloc;
8#[cfg(test)]
9extern crate std;
10
11pub mod interact;
12pub mod layout;
13pub mod popover;
14pub mod resize;
15pub mod scroll;
16pub mod split;
17pub mod text_input;
18
19/// how a backend tunes the built-in widgets
20pub trait Context: blit::Context {
21    /// movement in frame coordinates before a touch becomes a drag
22    const SLOP: Self::Scalar;
23    /// movement in frame coordinates before a mouse press becomes a drag
24    const MOUSE_SLOP: Self::Scalar;
25}
26
27pub trait Clipboard {
28    fn copy(&mut self, text: &str);
29    fn request_paste(&mut self);
30    fn paste(&self) -> Option<&str>;
31}
32
33#[cfg(test)]
34mod test {
35    use std::time::Duration;
36
37    use blit::{
38        FrameInfo, Point, Size, Tree, Ui,
39        input::{Input, Modifiers, MouseButton, TouchPhase},
40    };
41
42    pub struct TestContext;
43
44    impl blit::Context for TestContext {
45        type Scalar = f32;
46    }
47
48    impl crate::Context for TestContext {
49        const SLOP: f32 = 6.0;
50        const MOUSE_SLOP: f32 = 2.0;
51    }
52
53    /// builds and lays out one 200 by 200 frame
54    pub fn step<O>(tree: &mut Tree<TestContext>, input: Input<f32>, build: impl FnOnce(Ui<'_, TestContext>) -> O) -> O {
55        let output = tree.build(
56            &mut TestContext,
57            FrameInfo::new(Size::uniform(200.0)),
58            Duration::ZERO,
59            input,
60            build,
61        );
62        tree.layout(&mut TestContext);
63        output
64    }
65
66    /// a touch with an id, otherwise the primary mouse button
67    pub fn pointer(id: Option<u64>, phase: TouchPhase, x: f32, y: f32) -> Input<f32> {
68        let position = Point::new(x, y);
69        match (id, phase) {
70            (Some(id), phase) => Input::Touch { id, position, phase },
71            (None, TouchPhase::Down) => Input::MouseDown {
72                position,
73                button: MouseButton::Primary,
74                modifiers: Modifiers::NONE,
75            },
76            (None, TouchPhase::Move) => Input::MouseMove {
77                position,
78                modifiers: Modifiers::NONE,
79            },
80            (None, TouchPhase::Up) => Input::MouseUp {
81                position,
82                button: MouseButton::Primary,
83                modifiers: Modifiers::NONE,
84                leave: false,
85            },
86            (None, TouchPhase::Cancel) => Input::Cancel,
87        }
88    }
89}