This repository was archived by the owner on May 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathradio.rs
More file actions
67 lines (58 loc) · 1.59 KB
/
Copy pathradio.rs
File metadata and controls
67 lines (58 loc) · 1.59 KB
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
use std::fmt::{Debug, Display};
use strum::IntoEnumIterator;
use vgtk::lib::gtk::prelude::*;
use vgtk::lib::gtk::*;
use vgtk::{gtk, Callback, Component, UpdateAction, VNode};
#[derive(Clone, Debug, Default)]
pub struct Radio<Enum: Unpin> {
pub active: Enum,
pub on_changed: Callback<Enum>,
}
#[derive(Clone, Debug)]
pub enum RadioMsg<Enum: Unpin> {
Selected(Enum),
}
impl<Enum, I> Component for Radio<Enum>
where
Enum: 'static
+ IntoEnumIterator<Iterator = I>
+ Display
+ PartialEq
+ Debug
+ Default
+ Copy
+ Send
+ Unpin,
I: Iterator<Item = Enum>,
{
type Message = RadioMsg<Enum>;
type Properties = Self;
fn create(props: Self::Properties) -> Self {
props
}
fn change(&mut self, props: Self::Properties) -> UpdateAction<Self> {
*self = props;
UpdateAction::Render
}
fn update(&mut self, msg: Self::Message) -> UpdateAction<Self> {
match msg {
RadioMsg::Selected(selected) => {
self.active = selected;
self.on_changed.send(self.active);
}
}
UpdateAction::Render
}
fn view(&self) -> VNode<Self> {
gtk! {
<Box orientation=Orientation::Horizontal spacing=10>
{ Enum::iter().map(|label| {
gtk!{
<ToggleButton label=label.to_string() active=label == self.active
on toggled=|_| RadioMsg::Selected(label)/>
}
}) }
</Box>
}
}
}