start/stop logic
This commit is contained in:
parent
0b46e51694
commit
62e65282b7
55
src/main.rs
55
src/main.rs
@ -1,15 +1,25 @@
|
||||
// Based on example from https://crates.io/crates/gtk4
|
||||
// Based on examples
|
||||
// - https://crates.io/crates/gtk4
|
||||
// - https://github.com/gtk-rs/gtk4-rs/blob/main/examples/clock/main.rs
|
||||
// - https://github.com/gtk-rs/gtk4-rs/blob/main/examples/clipboard/main.rs
|
||||
// - https://github.com/kashifsoofi/blog-code-samples/blob/gtk4-c-counter-app/gtk4-rust-counter-app/src/main.rs
|
||||
|
||||
use chrono::Local;
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use gtk4 as gtk;
|
||||
use gtk::glib;
|
||||
use gtk::glib::clone;
|
||||
use gtk::prelude::*;
|
||||
use gtk::{glib};
|
||||
use gtk4 as gtk;
|
||||
|
||||
const APP_ID: &str = "de.privacy1st.pdt";
|
||||
|
||||
const DUR_MIN: f64 = 1.0;
|
||||
const INTERVAL_SEC: u32 = 1;
|
||||
|
||||
fn main() -> glib::ExitCode {
|
||||
let application = gtk::Application::builder()
|
||||
.application_id("de.privacy1st.pdt")
|
||||
.build();
|
||||
let application = gtk::Application::builder().application_id(APP_ID).build();
|
||||
application.connect_activate(build_ui);
|
||||
application.run()
|
||||
}
|
||||
@ -33,18 +43,28 @@ fn build_ui(app: >k::Application) {
|
||||
.spacing(24)
|
||||
.build();
|
||||
|
||||
// How to change variable using GTK4 Button? https://stackoverflow.com/a/76657798/6334421
|
||||
let running = Rc::new(Cell::new(false));
|
||||
|
||||
let button_start = gtk::Button::with_label("Start");
|
||||
button_start.connect_clicked(|_| {
|
||||
button_start.connect_clicked(clone!(@strong running => move |_| {
|
||||
running.set(true);
|
||||
eprintln!("Timer started.");
|
||||
});
|
||||
}));
|
||||
container.append(&button_start);
|
||||
|
||||
let button_stop = gtk::Button::with_label("Stop");
|
||||
button_stop.connect_clicked(|_| {
|
||||
eprintln!("Timer stopped.");
|
||||
});
|
||||
button_stop.connect_clicked(clone!(@strong running => move |_| {
|
||||
running.set(false);
|
||||
eprintln!("Timer stopped.");
|
||||
}));
|
||||
container.append(&button_stop);
|
||||
|
||||
let mut progress: f64 = 0.0;
|
||||
let progress_bar = gtk::ProgressBar::new();
|
||||
progress_bar.set_fraction(progress);
|
||||
container.append(&progress_bar);
|
||||
|
||||
let time = current_time();
|
||||
let label = gtk::Label::default();
|
||||
label.set_text(&time);
|
||||
@ -56,14 +76,23 @@ fn build_ui(app: >k::Application) {
|
||||
|
||||
// we are using a closure to capture the label (else we could also use a normal function)
|
||||
let tick = move || {
|
||||
if running.get() {
|
||||
progress += ((INTERVAL_SEC as f64) / 60.0) / DUR_MIN;
|
||||
progress_bar.set_fraction(progress);
|
||||
}
|
||||
|
||||
if progress > 1.0 {
|
||||
// todo
|
||||
}
|
||||
|
||||
let time = current_time();
|
||||
label.set_text(&time);
|
||||
// we could return glib::ControlFlow::Break to stop our clock after this tick
|
||||
glib::ControlFlow::Continue
|
||||
};
|
||||
|
||||
// executes the closure once every second
|
||||
glib::timeout_add_seconds_local(1, tick);
|
||||
// executes the closure once every `INTERVAL_SEC` seconds
|
||||
glib::timeout_add_seconds_local(INTERVAL_SEC, tick);
|
||||
}
|
||||
|
||||
fn current_time() -> String {
|
||||
|
Loading…
x
Reference in New Issue
Block a user