Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust] Ported Level Bars demo #197

Merged
merged 2 commits into from
Aug 16, 2024
Merged
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
60 changes: 60 additions & 0 deletions src/Level Bars/code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::workbench;
use gtk::glib;
use gtk::prelude::*;

pub fn main() {
let bar_continuous: gtk::LevelBar = workbench::builder().object("bar_continuous").unwrap();

bar_continuous.add_offset_value("full", 100.0);
bar_continuous.add_offset_value("half", 50.0);
bar_continuous.add_offset_value("low", 25.0);

let bar_discrete: gtk::LevelBar = workbench::builder().object("bar_discrete").unwrap();

bar_discrete.add_offset_value("very-weak", 1.0);
bar_discrete.add_offset_value("weak", 2.0);
bar_discrete.add_offset_value("moderate", 4.0);
bar_discrete.add_offset_value("strong", 6.0);

let entry: gtk::PasswordEntry = workbench::builder().object("entry").unwrap();
let label_strength: gtk::Label = workbench::builder().object("label_strength").unwrap();

// We're using the glib::clone! macro to
// provide access to the object from the handling closure
// https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/macro.clone.html

entry.connect_text_notify(glib::clone!(
@weak label_strength, @weak bar_discrete => move |entry| {
// This is not a secure way to estimate password strength
// Use appropriate solutions instead
// such as https://github.com/dropbox/zxcvbn
let level = std::cmp::min(entry.text().len() / 2, 6);

match level {
1 => {
label_strength.set_label("Very Weak");
label_strength.set_css_classes(&["very-weak-label"]);
}
2 => {
label_strength.set_label("Weak");
label_strength.set_css_classes(&["weak-label"]);
}
3 | 4 => {
label_strength.set_label("Moderate");
label_strength.set_css_classes(&["moderate-label"]);

}
5 | 6 => {
label_strength.set_label("Strong");
label_strength.set_css_classes(&["strong-label"]);
}
_ => {
label_strength.set_label("");
label_strength.set_css_classes(&[]);
}
}

bar_discrete.set_value(level as f64)
}
));
}
Loading