custom_widget/
custom_widget.rs

1#![windows_subsystem = "windows"]
2
3use accesskit::{Action, Node, NodeId, Rect, Role};
4use fltk::{enums::*, prelude::*, *};
5use fltk_accesskit::{builder, Accessible, AccessibleApp};
6
7#[derive(Clone)]
8struct MyButton {
9    f: button::Button,
10}
11
12impl MyButton {
13    pub fn new(label: &str) -> Self {
14        let mut f = button::Button::default_fill().with_label(label);
15        f.set_frame(FrameType::FlatBox);
16        Self { f }
17    }
18}
19
20impl Accessible for MyButton {
21    fn make_node(&self, _children: &[NodeId]) -> (NodeId, Node) {
22        let node_id = NodeId(self.as_widget_ptr() as usize as u64);
23        let node = {
24            let mut builder = Node::new(Role::Button);
25            builder.set_bounds(Rect {
26                x0: self.x() as f64,
27                y0: self.y() as f64,
28                x1: (self.w() + self.x()) as f64,
29                y1: (self.h() + self.y()) as f64,
30            });
31            builder.set_label(&*self.label());
32            builder.add_action(Action::Focus);
33            builder.add_action(Action::Click);
34            builder
35        };
36        (node_id, node)
37    }
38}
39
40fltk::widget_extends!(MyButton, button::Button, f);
41
42fn main() {
43    let a = app::App::default();
44    let mut w = window::Window::default()
45        .with_size(400, 300)
46        .with_label("Hello Window");
47    let col = group::Flex::default_fill().column();
48    let mut b1 = MyButton::new("Click 1");
49    let mut b2 = MyButton::new("Click 2");
50    b2.set_callback(|_| println!("clicked 2"));
51    col.end();
52    w.end();
53    w.show();
54
55    let ac = builder(w).attach();
56
57    b1.set_callback(|_| println!("clicked 1"));
58
59    a.run_with_accessibility(ac).unwrap();
60}