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
use crate::yew_agent::{Agent, AgentLink, Context, HandlerId};
use crate::*;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
pub struct AppDrawerOptions {
pub(crate) title: String,
pub(crate) display_ref: usize,
pub(crate) hide_header: bool,
pub(crate) hide_footer: bool,
pub(crate) hide_close_x: bool,
pub(crate) hide_cancel: bool,
pub(crate) on_confirm: Option<usize>,
pub(crate) confirm_display: String,
}
pub struct AppDrawerOptionsBuilder {
title: String,
display_ref: usize,
hide_header: bool,
hide_footer: bool,
hide_close_x: bool,
hide_cancel: bool,
on_confirm: Option<fn() -> bool>,
confirm_display: String,
}
impl AppDrawerOptionsBuilder {
pub fn build(self: Self) -> AppDrawerOptions {
AppDrawerOptions {
title: self.title,
display_ref: self.display_ref,
hide_header: self.hide_header,
hide_footer: self.hide_footer,
hide_close_x: self.hide_close_x,
hide_cancel: self.hide_cancel,
on_confirm: match self.on_confirm {
Some(method) => Some(method as usize),
None => None,
},
confirm_display: self.confirm_display.to_string(),
}
}
pub fn hide_close_x(self: &mut Self) -> &mut Self {
self.hide_close_x = true;
self
}
pub fn hide_header(self: &mut Self) -> &mut Self {
self.hide_header = true;
self
}
pub fn hide_footer(self: &mut Self) -> &mut Self {
self.hide_footer = true;
self
}
pub(crate) fn hide_cancel(self: &mut Self) -> &mut Self {
self.hide_cancel = true;
self
}
pub fn set_on_confirm(self: &mut Self, display: String, on_confirm: fn() -> bool) -> &mut Self {
self.on_confirm = Some(on_confirm);
self.confirm_display = display;
self
}
}
impl AppDrawerOptions {
pub fn new(title: String, display: fn() -> Html) -> AppDrawerOptionsBuilder {
AppDrawerOptionsBuilder {
title,
display_ref: display as usize,
hide_header: false,
hide_footer: false,
hide_close_x: false,
hide_cancel: false,
confirm_display: "Confirm".to_string(),
on_confirm: None,
}
}
pub(crate) fn get_display(self: &Self) -> fn() -> Html {
let content: fn() -> Html = if self.display_ref > 0 {
let fnptr = self.display_ref as *const ();
unsafe { std::mem::transmute(fnptr) }
} else {
|| html!("")
};
content
}
pub(crate) fn get_on_confirm(self: &Self) -> fn() -> bool {
match self.on_confirm {
Some(value) => {
let content: fn() -> bool = if value > 0 {
let fnptr = value as *const ();
unsafe { std::mem::transmute(fnptr) }
} else {
|| true
};
content
}
None => || true,
}
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub enum AppDrawerRequest {
ToggleTopDrawer(Option<AppDrawerOptions>),
ToggleRightDrawer(Option<AppDrawerOptions>),
ToggleBottomDrawer(Option<AppDrawerOptions>),
ToggleLeftDrawer(Option<AppDrawerOptions>),
}
pub enum AppDrawerReceiverMessage {
AppDrawerMessage(AppDrawerRequest),
None,
}
pub(crate) struct AppDrawerAgent {
link: AgentLink<AppDrawerAgent>,
subscribers: HashSet<HandlerId>,
}
impl Agent for AppDrawerAgent {
type Reach = Context<Self>;
type Message = ();
type Input = AppDrawerRequest;
type Output = AppDrawerRequest;
fn create(link: AgentLink<Self>) -> Self {
Self {
link,
subscribers: HashSet::new(),
}
}
fn update(&mut self, _msg: Self::Message) {}
fn handle_input(&mut self, msg: Self::Input, _id: HandlerId) {
for sub in self.subscribers.iter() {
self.link.respond(*sub, msg.clone());
}
}
fn connected(&mut self, id: HandlerId) {
self.subscribers.insert(id);
}
fn disconnected(&mut self, id: HandlerId) {
self.subscribers.remove(&id);
}
}