Struct fltk::app::Sender

source ·
pub struct Sender<T: Send + Sync> { /* 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 27)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
fn main() {
    let app = app::App::default();
    let mut wind = Window::default().with_size(400, 300);
    let mut frame = Frame::default().size_of(&wind).with_label("0");

    let mut val = 0;

    wind.show();

    let (s, r) = app::channel::<Message>();

    std::thread::spawn(move || loop {
        app::sleep(1.);
        s.send(Message::Increment(2));
    });

    while app.wait() {
        if let Some(Message::Increment(step)) = r.recv() {
            inc_frame(&mut frame, &mut val, step)
        }
    }
}
More examples
Hide additional examples
examples/closable_tabs2.rs (line 79)
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
        pub fn add(&mut self, child: &mut group::Group, label: &str) {
            child.resize(
                self.contents.x(),
                self.contents.y(),
                self.contents.w(),
                self.contents.h(),
            );
            self.contents.add(child);
            let but = create_tab_button(label);
            self.tab_labels.add(&but);
            but.child(1).unwrap().set_callback({
                let curr_child = child.clone();
                let contents = self.contents.clone();
                let sndb = *self.snd;
                move |_| {
                    let idx = contents.find(&curr_child);
                    sndb.send(Message::Delete(idx));
                    app::redraw();
                }
            });
            but.child(0).unwrap().set_callback({
                let curr_child = child.clone();
                let contents = self.contents.clone();
                let sndb = *self.snd;
                move |_| {
                    let idx = contents.find(&curr_child);
                    sndb.send(Message::Foreground(idx));
                    app::redraw();
                }
            });
        }
examples/threads_windows.rs (line 41)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let app = app::App::default();
    let mut wind = Window::default().with_size(160, 200).with_label("Counter");
    let mut col = Flex::default()
        .with_size(120, 140)
        .center_of(&wind)
        .column();
    col.set_spacing(10);
    let mut but_inc = Button::default().with_label("+");
    let mut frame = Frame::default().with_label("0");
    let mut but_dec = Button::default().with_label("-");
    col.end();
    wind.end();
    wind.show();

    let mut msg_wind = Window::default().with_size(120, 100).with_label("Message");
    let mut msgview = HelpView::default().with_size(120, 100);
    msgview.set_align(Align::Center);
    msg_wind.end();

    let (s, r) = app::channel::<Message>();

    but_inc.set_callback({
        move |_| {
            s.send(Message::Deactivate);
            thread::spawn(move || {
                thread::sleep(Duration::from_secs(1));
                s.send(Message::Increment);
                s.send(Message::Message("Incremented"));
                s.send(Message::Activate);
            });
        }
    });
    but_dec.set_callback({
        move |_| {
            s.send(Message::Deactivate);
            thread::spawn(move || {
                thread::sleep(Duration::from_secs(1));
                s.send(Message::Decrement);
                s.send(Message::Message("Decremented"));
                s.send(Message::Activate);
            });
        }
    });

    while app.wait() {
        if let Some(msg) = r.recv() {
            let label: i32 = frame.label().parse()?;
            match msg {
                Message::Increment => frame.set_label(&(label + 1).to_string()),
                Message::Decrement => frame.set_label(&(label - 1).to_string()),
                Message::Activate => {
                    but_inc.activate();
                    but_dec.activate();
                }
                Message::Deactivate => {
                    but_inc.deactivate();
                    but_dec.deactivate();
                }
                Message::Message(s) => {
                    msgview.set_value(s);
                    msg_wind.show();
                }
            }
        }
    }
    Ok(())
}
examples/editor2.rs (line 228)
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    pub fn new(args: Vec<String>) -> Self {
        let app = app::App::default().with_scheme(app::Scheme::Gtk);
        app::background(211, 211, 211);
        let (s, r) = app::channel::<Message>();
        let mut buf = text::TextBuffer::default();
        buf.set_tab_distance(4);
        let mut main_win = window::Window::default()
            .with_size(800, 600)
            .center_screen()
            .with_label("RustyEd");
        let menu = MyMenu::new(&s);
        let modified = false;
        menu.menu.find_item("&File/Save\t").unwrap().deactivate();
        let mut editor = MyEditor::new(buf.clone());
        editor.emit(s, Message::Changed);
        main_win.make_resizable(true);
        // only resize editor, not the menu bar
        main_win.resizable(&*editor);
        main_win.end();
        main_win.show();
        main_win.set_callback(move |_| {
            if app::event() == Event::Close {
                s.send(Message::Quit);
            }
        });
        let filename = if args.len() > 1 {
            let file = path::Path::new(&args[1]);
            assert!(
                file.exists() && file.is_file(),
                "An error occurred while opening the file!"
            );
            match buf.load_file(&args[1]) {
                Ok(_) => Some(PathBuf::from(args[1].clone())),
                Err(e) => {
                    dialog::alert(
                        center().0 - 200,
                        center().1 - 100,
                        &format!("An issue occured while loading the file: {e}"),
                    );
                    None
                }
            }
        } else {
            None
        };

        // Handle drag and drop
        editor.handle({
            let mut dnd = false;
            let mut released = false;
            let buf = buf.clone();
            move |_, ev| match ev {
                Event::DndEnter => {
                    dnd = true;
                    true
                }
                Event::DndDrag => true,
                Event::DndRelease => {
                    released = true;
                    true
                }
                Event::Paste => {
                    if dnd && released {
                        let path = app::event_text();
                        let path = path.trim();
                        let path = path.replace("file://", "");
                        let path = std::path::PathBuf::from(&path);
                        if path.exists() {
                            // we use a timeout to avoid pasting the path into the buffer
                            app::add_timeout3(0.0, {
                                let mut buf = buf.clone();
                                move |_| match buf.load_file(&path) {
                                    Ok(_) => (),
                                    Err(e) => dialog::alert(
                                        center().0 - 200,
                                        center().1 - 100,
                                        &format!("An issue occured while loading the file: {e}"),
                                    ),
                                }
                            });
                        }
                        dnd = false;
                        released = false;
                        true
                    } else {
                        false
                    }
                }
                Event::DndLeave => {
                    dnd = false;
                    released = false;
                    true
                }
                _ => false,
            }
        });

        // What shows when we attempt to print
        let mut printable = text::TextDisplay::default();
        printable.set_frame(FrameType::NoBox);
        printable.set_scrollbar_size(0);
        printable.set_buffer(Some(buf.clone()));

        Self {
            app,
            modified,
            filename,
            r,
            main_win,
            menu,
            buf,
            editor,
            printable,
        }
    }
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 copy 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 + Send + Sync> Debug for Sender<T>

source§

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

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

impl<T: Copy + 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> 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,

§

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>,

§

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>,

§

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.