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

➕ Add progress-tracker example #215

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: rust
rust:
- nightly
- beta
- stable
- 1.28.0 # stable
addons:
apt:
packages:
Expand Down
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Contributing
Contributors, you're welcome !

## How-to
For now, if you want to update or add an example, please create a pull request on the [pending branch](https://github.com/rust-gnome/examples/tree/pending). If your pull request targets the master branch, it won't be merged. Thanks !
47 changes: 16 additions & 31 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,17 @@ authors = ["The Gtk-rs Project Developers"]
chrono = "0.4"
url = "1.4"
futures-preview = { version = "0.2", optional = true }

[dependencies.glib]
git = "https://github.com/gtk-rs/glib"

[dependencies.gio]
git = "https://github.com/gtk-rs/gio"

[dependencies.cairo-rs]
git = "https://github.com/gtk-rs/cairo"
features = ["png"]

[dependencies.pango]
git = "https://github.com/gtk-rs/pango"

[dependencies.gdk-pixbuf]
git = "https://github.com/gtk-rs/gdk-pixbuf"

[dependencies.gdk]
git = "https://github.com/gtk-rs/gdk"

[dependencies.gtk]
git = "https://github.com/gtk-rs/gtk"

[dependencies.gobject-subclass]
git = "https://github.com/gtk-rs/gobject-subclass"

[dependencies.glib-sys]
git = "https://github.com/gtk-rs/sys"

[dependencies.gobject-sys]
git = "https://github.com/gtk-rs/sys"
gobject-subclass = "^0"
glib-sys = "^0"
gobject-sys = "^0"
glib = "^0"
gio = "^0"
gdk = "^0"
gdk-pixbuf = "^0"
gtk = "^0"
pango = "^0"
cairo-rs = { version = "^0", features = ["png"] }
cascade = "0.1.2"

[features]
#default = ["gtk_3_22_30", "futures-stable"]
Expand Down Expand Up @@ -113,6 +93,11 @@ name = "notebook"
[[bin]]
name = "pango_attributes"

[[bin]]
name = "progress-tracker"
path = "src/bin/progress_tracker.rs"
required-features = ["gtk/v3_22"]

[[bin]]
name = "simple_treeview"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# gtk-rs examples [![Build Status](https://travis-ci.org/gtk-rs/examples.png?branch=master)](https://travis-ci.org/gtk-rs/examples) [![Build status](https://ci.appveyor.com/api/projects/status/glyf8yir9lh8mrh5?svg=true)](https://ci.appveyor.com/project/GuillaumeGomez/examples)
# gtk-rs examples [![Build Status](https://travis-ci.org/gtk-rs/examples.png?branch=master)](https://travis-ci.org/gtk-rs/examples) [![Build status](https://ci.appveyor.com/api/projects/status/pi27a5xubp0ihl2d?svg=true)](https://ci.appveyor.com/project/GuillaumeGomez/examples)

A few gtk-rs examples. To build, just do:

Expand Down
50 changes: 0 additions & 50 deletions git_deps.sh

This file was deleted.

208 changes: 208 additions & 0 deletions src/bin/progress_tracker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
//! Track progress with a background thread and a channel.

#[macro_use]
extern crate cascade;
extern crate gio;
extern crate gtk;

use gio::prelude::*;
use gtk::prelude::*;

use std::env::args;
use std::rc::Rc;
use std::sync::mpsc::{self, TryRecvError};
use std::thread;
use std::time::Duration;

pub fn main() {
let application = gtk::Application::new(
"com.github.progress-tracker",
gio::ApplicationFlags::empty(),
).expect("initialization failed");

application.connect_startup(|app| {
Application::new(app);
});

application.connect_activate(|_| {});
application.run(&args().collect::<Vec<_>>());
}

pub struct Application {
pub widgets: Rc<Widgets>,
}

impl Application {
pub fn new(app: &gtk::Application) -> Self {
let app = Application {
widgets: Rc::new(Widgets::new(app)),
};

app.connect_progress();

app
}

fn connect_progress(&self) {
let widgets = self.widgets.clone();
self.widgets.main_view.button.connect_clicked(move |_| {
let (tx, rx) = mpsc::channel();

thread::spawn(move || {
for v in 1..=10 {
tx.send(v);
thread::sleep(Duration::from_millis(500));
}
});

let widgets = widgets.clone();
gtk::timeout_add(16, move || match rx.try_recv() {
Ok(value) => {
widgets
.main_view
.progress
.set_fraction(f64::from(value) / 10.0);

if value == 10 {
widgets
.view_stack
.set_visible_child(&widgets.complete_view.container);
}

gtk::Continue(true)
}
Err(TryRecvError::Empty) => gtk::Continue(true),
Err(TryRecvError::Disconnected) => gtk::Continue(false),
});
});
}
}

pub struct Widgets {
pub window: gtk::ApplicationWindow,
pub header: Header,
pub view_stack: gtk::Stack,
pub main_view: MainView,
pub complete_view: CompleteView,
}

impl Widgets {
pub fn new(application: &gtk::Application) -> Self {
let complete_view = CompleteView::new();
let main_view = MainView::new();

let view_stack = cascade! {
gtk::Stack::new();
..set_border_width(6);
..set_vexpand(true);
..set_hexpand(true);
..add(&main_view.container);
..add(&complete_view.container);
};

let header = Header::new();

let window = cascade! {
gtk::ApplicationWindow::new(application);
..set_icon_name("package-x-generic");
..set_property_window_position(gtk::WindowPosition::Center);
..set_titlebar(&header.container);
..add(&view_stack);
..show_all();
..set_default_size(500, 250);
..connect_delete_event(move |window, _| {
window.destroy();
Inhibit(false)
});
};

Widgets {
window,
header,
view_stack,
main_view,
complete_view,
}
}
}

pub struct Header {
container: gtk::HeaderBar,
}

impl Header {
pub fn new() -> Self {
let container = cascade! {
gtk::HeaderBar::new();
..set_title("Progress Tracker");
..set_show_close_button(true);
};

Header { container }
}
}

pub struct CompleteView {
pub container: gtk::Grid,
}

impl CompleteView {
pub fn new() -> Self {
let label = cascade! {
gtk::Label::new(None);
..set_markup("Task complete");
..set_halign(gtk::Align::Center);
..set_valign(gtk::Align::Center);
..set_vexpand(true);
..set_hexpand(true);
};

let container = cascade! {
gtk::Grid::new();
..set_vexpand(true);
..set_hexpand(true);
..add(&label);
};

CompleteView { container }
}
}

pub struct MainView {
pub container: gtk::Grid,
pub progress: gtk::ProgressBar,
pub button: gtk::Button,
}

impl MainView {
pub fn new() -> Self {
let progress = cascade! {
gtk::ProgressBar::new();
..set_text("Progress Bar");
..set_show_text(true);
..set_hexpand(true);
};

let button = cascade! {
gtk::Button::new();
..set_label("start");
..set_halign(gtk::Align::Center);
};

let container = cascade! {
gtk::Grid::new();
..attach(&progress, 0, 0, 1, 1);
..attach(&button, 0, 1, 1, 1);
..set_row_spacing(6);
..set_border_width(6);
..set_vexpand(true);
..set_hexpand(true);
};

MainView {
container,
progress,
button,
}
}
}