Struct Sender

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

Creates a sender struct

Implementations§

Source§

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

Source

pub fn send(&self, val: T)

Sends a message

Examples found in repository?
examples/messages.rs (line 28)
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/closable_tabs2.rs (line 79)
63        pub fn add(&mut self, child: &mut group::Group, label: &str) {
64            child.resize(
65                self.contents.x(),
66                self.contents.y(),
67                self.contents.w(),
68                self.contents.h(),
69            );
70            self.contents.add(child);
71            let but = create_tab_button(label);
72            self.tab_labels.add(&but);
73            but.child(1).unwrap().set_callback({
74                let curr_child = child.clone();
75                let contents = self.contents.clone();
76                let sndb = *self.snd;
77                move |_| {
78                    let idx = contents.find(&curr_child);
79                    sndb.send(Message::Delete(idx));
80                    app::redraw();
81                }
82            });
83            but.child(0).unwrap().set_callback({
84                let curr_child = child.clone();
85                let contents = self.contents.clone();
86                let sndb = *self.snd;
87                move |_| {
88                    let idx = contents.find(&curr_child);
89                    sndb.send(Message::Foreground(idx));
90                    app::redraw();
91                }
92            });
93        }
examples/threads_windows.rs (line 41)
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 221)
199    pub fn new(args: Vec<String>) -> Self {
200        let app = app::App::default().with_scheme(app::Scheme::Gtk);
201        app::background(211, 211, 211);
202        let (s, r) = app::channel::<Message>();
203        let mut buf = text::TextBuffer::default();
204        buf.set_tab_distance(4);
205        let mut main_win = window::Window::default()
206            .with_size(800, 600)
207            .with_label("RustyEd");
208        main_win.set_center_screen();
209        let menu = MyMenu::new(&s);
210        let modified = false;
211        menu.menu.find_item("&File/&Save\t").unwrap().deactivate();
212        let mut editor = MyEditor::new(buf.clone());
213        editor.emit(s, Message::Changed);
214        main_win.make_resizable(true);
215        // only resize editor, not the menu bar
216        main_win.resizable(&*editor);
217        main_win.end();
218        main_win.show();
219        main_win.set_callback(move |_| {
220            if app::event() == Event::Close {
221                s.send(Message::Quit);
222            }
223        });
224        let filename = if args.len() > 1 {
225            let file = path::Path::new(&args[1]);
226            assert!(
227                file.exists() && file.is_file(),
228                "An error occurred while opening the file!"
229            );
230            match buf.load_file(&args[1]) {
231                Ok(_) => Some(PathBuf::from(args[1].clone())),
232                Err(e) => {
233                    dialog::alert(&format!("An issue occured while loading the file: {e}"));
234                    None
235                }
236            }
237        } else {
238            None
239        };
240
241        // Handle drag and drop
242        editor.handle({
243            let mut dnd = false;
244            let mut released = false;
245            let buf = buf.clone();
246            move |_, ev| match ev {
247                Event::DndEnter => {
248                    dnd = true;
249                    true
250                }
251                Event::DndDrag => true,
252                Event::DndRelease => {
253                    released = true;
254                    true
255                }
256                Event::Paste => {
257                    if dnd && released {
258                        let path = app::event_text().unwrap();
259                        let path = path.trim();
260                        let path = path.replace("file://", "");
261                        let path = std::path::PathBuf::from(&path);
262                        if path.exists() {
263                            // we use a timeout to avoid pasting the path into the buffer
264                            app::add_timeout(0.0, {
265                                let mut buf = buf.clone();
266                                move |_| match buf.load_file(&path) {
267                                    Ok(_) => (),
268                                    Err(e) => dialog::alert(&format!(
269                                        "An issue occured while loading the file: {e}"
270                                    )),
271                                }
272                            });
273                        }
274                        dnd = false;
275                        released = false;
276                        true
277                    } else {
278                        false
279                    }
280                }
281                Event::DndLeave => {
282                    dnd = false;
283                    released = false;
284                    true
285                }
286                _ => false,
287            }
288        });
289
290        // What shows when we attempt to print
291        let mut printable = text::TextDisplay::default();
292        printable.set_frame(FrameType::NoBox);
293        printable.set_scrollbar_size(0);
294        printable.set_buffer(Some(buf.clone()));
295
296        Self {
297            app,
298            modified,
299            filename,
300            r,
301            main_win,
302            menu,
303            buf,
304            editor,
305            printable,
306        }
307    }
Source

pub fn get() -> Self

Get the global sender

Trait Implementations§

Source§

impl<T: Send + Sync> Clone for Sender<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 Sender<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 Sender<T>

Source§

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

Source§

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

Auto Trait Implementations§

§

impl<T> Freeze for Sender<T>

§

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

§

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

§

impl<T> UnwindSafe for Sender<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.