Function fltk::app::widget_from_id
source · pub fn widget_from_id<T: 'static + Clone>(id: &str) -> Option<T>
Expand description
Get back the widget thru its id
Examples found in repository?
examples/widget_id.rs (line 29)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
fn increment_by(step: i32) {
let mut frame: frame::Frame = app::widget_from_id("my_frame").unwrap();
let state = app::GlobalState::<Counter>::get();
let count = state.with(move |c| {
c.count += step;
c.count
});
frame.set_label(&count.to_string());
}
// To pass a function object directly!
fn increment(_w: &mut impl WidgetExt) {
let mut frame: frame::Frame = app::widget_from_id("my_frame").unwrap();
let state = app::GlobalState::<Counter>::get();
let count = state.with(|c| {
c.count += 1;
c.count
});
frame.set_label(&count.to_string());
}
More examples
examples/editor.rs (line 168)
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
fn menu_cb(m: &mut impl MenuExt) {
if let Ok(mpath) = m.item_pathname(None) {
let ed: text::TextEditor = app::widget_from_id("ed").unwrap();
match mpath.as_str() {
"&File/New...\t" => {
STATE.with(|s| {
if !s.buf.text().is_empty() {
let c = dialog::choice2_default(
"Are you sure you want to clear the buffer?",
"Yes",
"No",
"",
);
if c == Some(0) {
s.buf.set_text("");
s.saved = false;
}
}
});
}
"&File/Open...\t" => {
let c = nfc_get_file(dialog::NativeFileChooserType::BrowseFile);
if let Ok(text) = std::fs::read_to_string(&c) {
STATE.with(move |s| {
s.buf.set_text(&text);
s.saved = false;
s.current_file = c.clone();
});
}
}
"&File/Save\t" => {
STATE.with(|s| {
if !s.saved && s.current_file.exists() {
std::fs::write(&s.current_file, s.buf.text()).ok();
}
});
}
"&File/Save as...\t" => {
let c = nfc_get_file(dialog::NativeFileChooserType::BrowseSaveFile);
STATE.with(move |s| {
std::fs::write(&c, s.buf.text()).ok();
s.saved = true;
s.current_file = c.clone();
});
}
"&File/Quit\t" => quit_cb(),
"&Edit/Cut\t" => ed.cut(),
"&Edit/Copy\t" => ed.copy(),
"&Edit/Paste\t" => ed.paste(),
"&Help/About\t" => {
dialog::message_default("A minimal text editor written using fltk-rs!")
}
_ => unreachable!(),
}
}
}