Struct Receiver

Source
pub struct Receiver<T> { /* private fields */ }
Expand description

Creates a receiver struct

Implementations§

Source§

impl<T: 'static + Send + Sync> Receiver<T>

Source

pub fn recv(&self) -> Option<T>

Receives a message

Examples found in repository?
examples/messages.rs (line 33)
14fn main() {
15    let app = app::App::default();
16    let mut wind = Window::default().with_size(400, 300);
17    let mut frame = Frame::default().size_of(&wind).with_label("0");
18
19    let mut val = 0;
20
21    wind.show();
22
23    let (s, r) = app::channel::<Message>();
24
25    std::thread::spawn(move || {
26        loop {
27            app::sleep(1.);
28            s.send(Message::Increment(2));
29        }
30    });
31
32    while app.wait() {
33        if let Some(Message::Increment(step)) = r.recv() {
34            inc_frame(&mut frame, &mut val, step)
35        }
36    }
37}
More examples
Hide additional examples
examples/temp_converter2.rs (line 85)
83    pub fn run(&mut self) {
84        while self.a.wait() {
85            if let Some(msg) = self.r.recv() {
86                match msg {
87                    Message::CelciusChanged => {
88                        self.inp2.set_value(&format!(
89                            "{:.4}",
90                            c_to_f(self.inp1.value().parse().unwrap_or(0.0))
91                        ));
92                    }
93                    Message::FahrenheitChanged => {
94                        self.inp1.set_value(&format!(
95                            "{:.4}",
96                            f_to_c(self.inp2.value().parse().unwrap_or(0.0))
97                        ));
98                    }
99                }
100            }
101        }
102    }
examples/counter2.rs (line 30)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let app = app::App::default();
11    let mut wind = Window::default().with_size(160, 200).with_label("Counter");
12    let mut flex = Flex::default_fill().column();
13    flex.set_margins(30, 40, 30, 40);
14    flex.set_pad(10);
15    let mut but_inc = Button::default().with_label("+");
16    let mut frame = Frame::default().with_label("0");
17    let mut but_dec = Button::default().with_label("-");
18    flex.end();
19    wind.end();
20    wind.show();
21
22    let (s, r) = app::channel::<Message>();
23
24    but_inc.emit(s, Message::Increment);
25    but_dec.emit(s, Message::Decrement);
26
27    while app.wait() {
28        let label: i32 = frame.label().unwrap().parse()?;
29
30        if let Some(msg) = r.recv() {
31            match msg {
32                Message::Increment => frame.set_label(&(label + 1).to_string()),
33                Message::Decrement => frame.set_label(&(label - 1).to_string()),
34            }
35        }
36    }
37    Ok(())
38}
examples/closable_tabs2.rs (line 184)
152fn main() {
153    use fltk::{prelude::*, *};
154    // Create groups to be used as content for tabs
155    pub fn create_tab(from: i32, to: i32) -> group::Group {
156        let grp = group::Group::new(0, 0, 800, 600, None);
157        for idx in from..to {
158            button::Button::new(
159                idx * 10 + (idx - from) * 42,
160                idx * 10 + (idx - from) * 42,
161                80,
162                40,
163                None,
164            )
165            .with_label(&format!("button {idx}"));
166        }
167        grp.end();
168        grp
169    }
170    let app = app::App::default();
171    let mut win = window::Window::default().with_size(800, 600);
172    let (s, r) = app::channel::<closable_tab::Message>();
173    let mut tabs = closable_tab::ClosableTab::new(0, 0, 800, 600, &s);
174    win.end();
175    win.show();
176    tabs.add(&mut create_tab(1, 3), "tab 1");
177    tabs.add(&mut create_tab(4, 7), "tab 2");
178    tabs.add(&mut create_tab(8, 11), "tab 3");
179    tabs.add(&mut create_tab(12, 15), "tab 4");
180    tabs.add(&mut create_tab(16, 22), "tab 5");
181    tabs.set_foreground(2);
182    while app.wait() {
183        use closable_tab::Message::*;
184        if let Some(msg) = r.recv() {
185            match msg {
186                Foreground(idx) => {
187                    tabs.set_foreground(idx);
188                }
189                Delete(idx) => {
190                    tabs.remove(idx);
191                }
192                InsertNew(_) => {}
193            }
194        }
195    }
196}
examples/threads_windows.rs (line 63)
17fn main() -> Result<(), Box<dyn std::error::Error>> {
18    let app = app::App::default();
19    let mut wind = Window::default().with_size(160, 200).with_label("Counter");
20    let mut col = Flex::default()
21        .with_size(120, 140)
22        .center_of(&wind)
23        .column();
24    col.set_spacing(10);
25    let mut but_inc = Button::default().with_label("+");
26    let mut frame = Frame::default().with_label("0");
27    let mut but_dec = Button::default().with_label("-");
28    col.end();
29    wind.end();
30    wind.show();
31
32    let mut msg_wind = Window::default().with_size(120, 100).with_label("Message");
33    let mut msgview = HelpView::default().with_size(120, 100);
34    msgview.set_align(Align::Center);
35    msg_wind.end();
36
37    let (s, r) = app::channel::<Message>();
38
39    but_inc.set_callback({
40        move |_| {
41            s.send(Message::Deactivate);
42            thread::spawn(move || {
43                thread::sleep(Duration::from_secs(1));
44                s.send(Message::Increment);
45                s.send(Message::Message("Incremented"));
46                s.send(Message::Activate);
47            });
48        }
49    });
50    but_dec.set_callback({
51        move |_| {
52            s.send(Message::Deactivate);
53            thread::spawn(move || {
54                thread::sleep(Duration::from_secs(1));
55                s.send(Message::Decrement);
56                s.send(Message::Message("Decremented"));
57                s.send(Message::Activate);
58            });
59        }
60    });
61
62    while app.wait() {
63        if let Some(msg) = r.recv() {
64            let label: i32 = frame.label().unwrap().parse()?;
65            match msg {
66                Message::Increment => frame.set_label(&(label + 1).to_string()),
67                Message::Decrement => frame.set_label(&(label - 1).to_string()),
68                Message::Activate => {
69                    but_inc.activate();
70                    but_dec.activate();
71                }
72                Message::Deactivate => {
73                    but_inc.deactivate();
74                    but_dec.deactivate();
75                }
76                Message::Message(s) => {
77                    msgview.set_value(s);
78                    msg_wind.show();
79                }
80            }
81        }
82    }
83    Ok(())
84}
examples/editor2.rs (line 365)
362    pub fn launch(&mut self) {
363        while self.app.wait() {
364            use Message::*;
365            if let Some(msg) = self.r.recv() {
366                match msg {
367                    Changed => {
368                        if !self.modified {
369                            self.modified = true;
370                            self.menu
371                                .menu
372                                .find_item("&File/&Save\t")
373                                .unwrap()
374                                .activate();
375                            self.menu
376                                .menu
377                                .find_item("&File/&Quit\t")
378                                .unwrap()
379                                .set_label_color(Color::Red);
380                            let name = match &self.filename {
381                                Some(f) => f.to_string_lossy().to_string(),
382                                None => "(Untitled)".to_string(),
383                            };
384                            self.main_win.set_label(&format!("* {name} - RustyEd"));
385                        }
386                    }
387                    New => {
388                        if self.buf.text() != "" {
389                            let clear = if let Some(x) = dialog::choice(
390                                "File unsaved, Do you wish to continue?",
391                                "&Yes",
392                                "&No!",
393                                "",
394                            ) {
395                                x == 0
396                            } else {
397                                false
398                            };
399                            if clear {
400                                self.buf.set_text("");
401                            }
402                        }
403                    }
404                    Open => {
405                        if let Some(c) = nfc_get_file(dialog::NativeFileChooserType::BrowseFile) {
406                            if c.exists() {
407                                match self.buf.load_file(&c) {
408                                    Ok(_) => self.filename = Some(c),
409                                    Err(e) => dialog::alert(&format!(
410                                        "An issue occured while loading the file: {e}"
411                                    )),
412                                }
413                            } else {
414                                dialog::alert("File does not exist!")
415                            }
416                        }
417                    }
418                    Save => {
419                        self.save_file().unwrap();
420                    }
421                    SaveAs => {
422                        self.save_file_as().unwrap();
423                    }
424                    Print => {
425                        let mut printer = printer::Printer::default();
426                        if printer.begin_job(0).is_ok() {
427                            let (w, h) = printer.printable_rect();
428                            self.printable.resize(
429                                self.printable.x(),
430                                self.printable.y(),
431                                w - 40,
432                                h - 40,
433                            );
434                            // Needs cleanup
435                            let line_count = self.printable.count_lines(
436                                0,
437                                self.printable.buffer().unwrap().length(),
438                                true,
439                            ) / 45;
440                            for i in 0..=line_count {
441                                self.printable.scroll(45 * i, 0);
442                                printer.begin_page().ok();
443                                printer.print_widget(&self.printable, 20, 20);
444                                printer.end_page().ok();
445                            }
446                            printer.end_job();
447                        }
448                    }
449                    Quit => {
450                        if self.modified {
451                            match dialog::choice(
452                                "Would you like to save your work?",
453                                "&Yes",
454                                "&No",
455                                "",
456                            ) {
457                                Some(0) => {
458                                    if self.save_file().unwrap() {
459                                        self.app.quit();
460                                    }
461                                }
462                                Some(1) => self.app.quit(),
463                                Some(_) | None => (),
464                            }
465                        } else {
466                            self.app.quit();
467                        }
468                    }
469                    Cut => self.editor.cut(),
470                    Copy => self.editor.copy(),
471                    Paste => self.editor.paste(),
472                    About => dialog::message(
473                        "This is an example application written in Rust and using the FLTK Gui library.",
474                    ),
475                }
476            }
477        }
478    }
Source

pub fn get() -> Self

Get the global receiver

Trait Implementations§

Source§

impl<T: Send + Sync> Clone for Receiver<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Receiver<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Send + Sync> Copy for Receiver<T>

Source§

impl<T: Send + Sync> Send for Receiver<T>

Source§

impl<T: Send + Sync> Sync for Receiver<T>

Auto Trait Implementations§

§

impl<T> Freeze for Receiver<T>

§

impl<T> RefUnwindSafe for Receiver<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for Receiver<T>
where T: Unpin,

§

impl<T> UnwindSafe for Receiver<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.