Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/pending'
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jul 4, 2020
2 parents aa53d61 + 986989d commit 5d38aff
Show file tree
Hide file tree
Showing 31 changed files with 339 additions and 122 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
Cargo.lock
.vscode/settings.json
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ language: rust
rust:
- nightly
- beta
- 1.39.0 # stable
- 1.41.0 # stable
- stable
env:
- GTK=3.14
Expand Down
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ name = "clock"
[[bin]]
name = "clone_macro"

[[bin]]
name = "communication_thread"
edition = "2018"

[[bin]]
name = "css"

Expand All @@ -91,6 +95,9 @@ name = "grid"
[[bin]]
name = "gtktest"

[[bin]]
name = "iconview_example"

[[bin]]
name = "listbox_model"
required-features = ["gtk/v3_16", "gio/v2_44"]
Expand Down
2 changes: 1 addition & 1 deletion build_travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ fi
if [ -n "$OTHER_TARGET" ]; then
PKG_CONFIG_ALLOW_CROSS=1 cargo check $OTHER_TARGET --features "$FEATURES" --jobs 1 "$@"
else
RUSTFLAGS="-C link-dead-code" cargo build --features "$FEATURES" --jobs 1 "$@"
RUSTFLAGS="-C link-dead-code" cargo build -v --features "$FEATURES" --jobs 1 "$@"
fi
8 changes: 2 additions & 6 deletions src/bin/accessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn build_ui(application: &gtk::Application) {
window.set_title("Accessibility");
window.set_position(gtk::WindowPosition::Center);

let button = gtk::Button::new_with_label("Click me!");
let button = gtk::Button::with_label("Click me!");
let label = gtk::Label::new(Some("0"));
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);

Expand All @@ -43,11 +43,7 @@ fn build_ui(application: &gtk::Application) {
window.add(&vbox);

button.connect_clicked(move |_| {
let value = label
.get_text()
.and_then(|s| u32::from_str_radix(&s, 10).ok())
.unwrap_or(0)
+ 1;
let value = label.get_text().parse().unwrap_or(0) + 1;
label.set_text(&value.to_string());
});

Expand Down
2 changes: 1 addition & 1 deletion src/bin/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn build_ui(application: &gtk::Application) {
window.set_position(gtk::WindowPosition::Center);
window.set_default_size(350, 70);

let button = gtk::Button::new_with_label("Click me!");
let button = gtk::Button::with_label("Click me!");

window.add(&button);

Expand Down
4 changes: 2 additions & 2 deletions src/bin/basic_subclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern crate once_cell;
use gio::prelude::*;
use gtk::prelude::*;

use gio::subclass::application::ApplicationImplExt;
use gio::subclass::prelude::*;
use gio::ApplicationFlags;
use glib::subclass;
use glib::subclass::prelude::*;
Expand Down Expand Up @@ -69,7 +69,7 @@ impl ObjectImpl for SimpleWindowPrivate {
let self_ = obj.downcast_ref::<SimpleWindow>().unwrap();

let headerbar = gtk::HeaderBar::new();
let increment = gtk::Button::new_with_label("Increment!");
let increment = gtk::Button::with_label("Increment!");
let label = gtk::Label::new(Some("Press the Increment Button!"));

headerbar.set_title(Some("Hello World!"));
Expand Down
9 changes: 6 additions & 3 deletions src/bin/builder_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
//! This sample demonstrates how to use the builder with an imported glade file

extern crate gio;
extern crate glib;
extern crate gtk;

use gio::prelude::*;
use glib::clone;
use gtk::prelude::*;

use gtk::{ApplicationWindow, Builder, Button, MessageDialog};
Expand All @@ -14,7 +16,7 @@ use std::env::args;

fn build_ui(application: &gtk::Application) {
let glade_src = include_str!("builder_basics.glade");
let builder = Builder::new_from_string(glade_src);
let builder = Builder::from_string(glade_src);

let window: ApplicationWindow = builder.get_object("window1").expect("Couldn't get window1");
window.set_application(Some(application));
Expand All @@ -23,11 +25,12 @@ fn build_ui(application: &gtk::Application) {
.get_object("messagedialog1")
.expect("Couldn't get messagedialog1");

bigbutton.connect_clicked(move |_| {
dialog.run();
dialog.connect_delete_event(|dialog, _| {
dialog.hide();
gtk::Inhibit(true)
});

bigbutton.connect_clicked(clone!(@weak dialog => move |_| dialog.show_all()));
window.show_all();
}

Expand Down
17 changes: 10 additions & 7 deletions src/bin/builder_signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
//! This sample demonstrates how to handle signals in builder

extern crate gio;
extern crate glib;
extern crate gtk;

use gio::prelude::*;
use glib::clone;
use gtk::prelude::*;

use gtk::{ApplicationWindow, Builder, MessageDialog};
Expand All @@ -14,27 +16,28 @@ use std::env::args;

fn build_ui(application: &gtk::Application) {
let glade_src = include_str!("builder_signal.glade");
let builder = Builder::new_from_string(glade_src);
let builder = Builder::from_string(glade_src);

let window: ApplicationWindow = builder.get_object("window1").expect("Couldn't get window1");
window.set_application(Some(application));
let dialog: MessageDialog = builder
.get_object("messagedialog1")
.expect("Couldn't get messagedialog1");
dialog.connect_delete_event(|dialog, _| {
dialog.hide();
gtk::Inhibit(true)
});

builder.connect_signals(move |_, handler_name| {
// This is the one-time callback to register signals.
// Here we map each handler name to its handler.

if handler_name == "button1_clicked" {
let dialog = dialog.clone();

// Return the signal handler.
Box::new(move |_| {
dialog.run();
dialog.hide();
Box::new(clone!(@weak dialog => @default-return None, move |_| {
dialog.show_all();
None
})
}))
} else {
panic!("Unknown handler name {}", handler_name)
}
Expand Down
9 changes: 4 additions & 5 deletions src/bin/child-properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ use gtk::Orientation::Vertical;
use gtk::{ApplicationWindow, Button, Label, PackType};

use std::env::args;
use std::str::FromStr;

fn build_ui(application: &gtk::Application) {
let vbox = gtk::Box::new(Vertical, 0);

let plus_button = Button::new_with_label("+");
let plus_button = Button::with_label("+");
vbox.add(&plus_button);
// Set some child properties.
// These calls need to be added after the Widget is added to the Box.
Expand All @@ -32,20 +31,20 @@ fn build_ui(application: &gtk::Application) {
let counter_label = Label::new(Some("0"));
vbox.add(&counter_label);

let minus_button = Button::new_with_label("-");
let minus_button = Button::with_label("-");
vbox.add(&minus_button);

minus_button.connect_clicked(clone!(@weak counter_label => move |_| {
let nb = counter_label.get_text()
.and_then(|s| u32::from_str(&s).ok())
.parse()
.unwrap_or(0);
if nb > 0 {
counter_label.set_text(&format!("{}", nb - 1));
}
}));
plus_button.connect_clicked(clone!(@weak counter_label => move |_| {
let nb = counter_label.get_text()
.and_then(|s| u32::from_str(&s).ok())
.parse()
.unwrap_or(0);
counter_label.set_text(&format!("{}", nb + 1));
}));
Expand Down
14 changes: 7 additions & 7 deletions src/bin/clipboard_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ fn build_ui(application: &gtk::Application) {
// Create the whole window
window.set_title("gtk::Clipboard Simple Example");
window.connect_delete_event(|window, _| {
window.destroy();
window.close();
Inhibit(false)
});

// Create the button grid
let grid = gtk::Grid::new();
grid.set_row_homogeneous(true);
grid.set_column_homogeneous(true);
let button_a1 = gtk::ToggleButton::new_with_label("A1");
let button_a1 = gtk::ToggleButton::with_label("A1");
grid.attach(&button_a1, 0, 0, 1, 1);
let button_a2 = gtk::ToggleButton::new_with_label("A2");
let button_a2 = gtk::ToggleButton::with_label("A2");
grid.attach(&button_a2, 1, 0, 1, 1);
let button_b1 = gtk::ToggleButton::new_with_label("B1");
let button_b1 = gtk::ToggleButton::with_label("B1");
grid.attach(&button_b1, 0, 1, 1, 1);
let button_b2 = gtk::ToggleButton::new_with_label("B2");
let button_b2 = gtk::ToggleButton::with_label("B2");
grid.attach(&button_b2, 1, 1, 1, 1);

// Add in the action buttons
let copy_button = gtk::Button::new_with_mnemonic("_Copy");
let paste_button = gtk::Button::new_with_mnemonic("_Paste");
let copy_button = gtk::Button::with_mnemonic("_Copy");
let paste_button = gtk::Button::with_mnemonic("_Paste");
let button_box = gtk::ButtonBox::new(gtk::Orientation::Horizontal);
button_box.set_layout(gtk::ButtonBoxStyle::End);
button_box.pack_start(&copy_button, false, false, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/clone_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() {
window.set_title("First GTK+ Program");
window.set_default_size(350, 70);

let button = Button::new_with_label("Click me!");
let button = Button::with_label("Click me!");
button.connect_clicked(clone!(@weak state, @weak state2 => move |_| {
let mut state = state.borrow_mut();
let mut state2 = state2.borrow_mut();
Expand Down
78 changes: 78 additions & 0 deletions src/bin/communication_thread.rs
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: &gtk::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));
}
});
}
2 changes: 1 addition & 1 deletion src/bin/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn build_ui(application: &gtk::Application) {
// The container container.
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);

let label = gtk::Button::new_with_label("hover me!");
let label = gtk::Button::with_label("hover me!");
// We need to name it in order to be able to use its name as a CSS label to
// apply CSS on it.
gtk::WidgetExt::set_widget_name(&label, "label1");
Expand Down
2 changes: 1 addition & 1 deletion src/bin/drag_and_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::env::args;

fn build_ui(application: &gtk::Application) {
// Configure button as drag source for text
let button = gtk::Button::new_with_label("Drag here");
let button = gtk::Button::with_label("Drag here");
let targets = vec![
gtk::TargetEntry::new("STRING", gtk::TargetFlags::SAME_APP, 0),
gtk::TargetEntry::new("text/plain", gtk::TargetFlags::SAME_APP, 0),
Expand Down
2 changes: 1 addition & 1 deletion src/bin/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::env::args;

fn build_ui(application: &gtk::Application) {
let glade_src = include_str!("grid.glade");
let builder = Builder::new_from_string(glade_src);
let builder = Builder::from_string(glade_src);

let window: ApplicationWindow = builder.get_object("window").expect("Couldn't get window");
window.set_application(Some(application));
Expand Down
Loading

0 comments on commit 5d38aff

Please sign in to comment.