This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/pending'
- Loading branch information
Showing
31 changed files
with
339 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
target | ||
Cargo.lock | ||
.vscode/settings.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ language: rust | |
rust: | ||
- nightly | ||
- beta | ||
- 1.39.0 # stable | ||
- 1.41.0 # stable | ||
- stable | ||
env: | ||
- GTK=3.14 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//! Example on how to use a communication thread alongside with the GUI thread. | ||
//! | ||
//! Tricks used here: | ||
//! - Use a channel to show data on the GUI. | ||
//! - Run an async function on the GUI event loop. | ||
//! - Use a separate thread to handle incoming data and put it into a channel. | ||
|
||
use futures::{channel::mpsc, StreamExt}; | ||
use gio::prelude::*; | ||
use gtk::prelude::*; | ||
use gtk::{ApplicationWindow, Label}; | ||
use std::env::args; | ||
use std::thread; | ||
|
||
fn main() { | ||
let application = gtk::Application::new( | ||
Some("com.github.gtk-rs.examples.communication_thread"), | ||
Default::default(), | ||
) | ||
.expect("Initialization failed..."); | ||
application.connect_activate(build_ui); | ||
application.run(&args().collect::<Vec<_>>()); | ||
} | ||
|
||
fn build_ui(application: >k::Application) { | ||
let window = ApplicationWindow::new(application); | ||
let label = Label::new(None); | ||
window.add(&label); | ||
|
||
// Create a channel between communication thread and main event loop: | ||
let (sender, receiver) = mpsc::channel(1000); | ||
|
||
spawn_local_handler(label, receiver); | ||
start_communication_thread(sender); | ||
window.show_all(); | ||
} | ||
|
||
/// Spawn channel receive task on the main event loop. | ||
fn spawn_local_handler(label: gtk::Label, mut receiver: mpsc::Receiver<String>) { | ||
let main_context = glib::MainContext::default(); | ||
let future = async move { | ||
while let Some(item) = receiver.next().await { | ||
label.set_text(&item); | ||
} | ||
}; | ||
main_context.spawn_local(future); | ||
} | ||
|
||
/// Spawn separate thread to handle communication. | ||
fn start_communication_thread(mut sender: mpsc::Sender<String>) { | ||
// Note that blocking I/O with threads can be prevented | ||
// by using asynchronous code, which is often a better | ||
// choice. For the sake of this example, we showcase the | ||
// way to use a thread when there is no other option. | ||
|
||
thread::spawn(move || { | ||
let mut counter = 0; | ||
loop { | ||
// Instead of a counter, your application code will | ||
// block here on TCP or serial communications. | ||
let data = format!("Counter = {}!", counter); | ||
println!("Thread received data: {}", data); | ||
match sender.try_send(data) { | ||
Ok(_) => {} | ||
Err(err) => { | ||
if err.is_full() { | ||
println!("Data is produced too fast for GUI"); | ||
} else if err.is_disconnected() { | ||
println!("GUI stopped, stopping thread."); | ||
break; | ||
} | ||
} | ||
} | ||
counter += 1; | ||
thread::sleep(std::time::Duration::from_millis(100)); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.