-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f38c8a
commit 8f9f1c1
Showing
5 changed files
with
154 additions
and
135 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
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,9 @@ | ||
/// Application id and name | ||
pub const APP_ID: &str = "password-generator"; | ||
pub const APP_NAME: &str = "Password Generator"; | ||
|
||
/// Maximum password length | ||
pub const MAX_PASSWORD_LENGTH: u8 = 50; | ||
|
||
/// Special characters for random password generation | ||
pub const SPECIAL_CHARACTERS: [char; 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use copypasta::{ClipboardContext, ClipboardProvider}; | ||
use eframe::{ | ||
egui::{CentralPanel, Context, RichText, Slider}, | ||
App, Frame, | ||
}; | ||
use serde_json::{from_str, json}; | ||
use std::time::Duration; | ||
|
||
use crate::{app::PasswordGenerator, constants::MAX_PASSWORD_LENGTH, types::RandomPassword}; | ||
|
||
impl App for PasswordGenerator { | ||
fn auto_save_interval(&self) -> Duration { | ||
Duration::from_secs(5) | ||
} | ||
|
||
fn save(&mut self, storage: &mut dyn eframe::Storage) { | ||
let mut passwords_to_add: Vec<String> = Vec::with_capacity(10); | ||
|
||
passwords_to_add.push(self.password.clone()); | ||
|
||
let mut recent_passwords = match storage.get_string("recent_passwords") { | ||
Some(passwords) => { | ||
let passwords: Vec<_> = from_str(passwords.as_str()).unwrap_or_default(); | ||
|
||
passwords_to_add.extend(passwords); | ||
|
||
passwords_to_add | ||
} | ||
None => passwords_to_add, | ||
}; | ||
|
||
recent_passwords.sort(); | ||
|
||
recent_passwords.dedup(); | ||
|
||
storage.set_string("recent_passwords", json!(recent_passwords).to_string()); | ||
} | ||
|
||
fn update(&mut self, ctx: &Context, _frame: &mut Frame) { | ||
CentralPanel::default().show(ctx, |ui| { | ||
ui.label(RichText::new(self.password.to_string()).size(18.0)); | ||
|
||
ui.add(Slider::new(&mut self.options.length, 1..=MAX_PASSWORD_LENGTH).text("Length")); | ||
|
||
ui.checkbox(&mut self.options.include_lowercase, "Include Lowercase"); | ||
ui.checkbox(&mut self.options.include_uppercase, "Include Uppercase"); | ||
ui.checkbox(&mut self.options.include_numbers, "Include Numbers"); | ||
ui.checkbox( | ||
&mut self.options.include_special_characters, | ||
"Include Special Characters", | ||
); | ||
|
||
let checkbox_options = [ | ||
self.options.include_lowercase, | ||
self.options.include_uppercase, | ||
self.options.include_numbers, | ||
self.options.include_special_characters, | ||
]; | ||
|
||
if ui.button(RichText::new("Generate")).clicked() { | ||
if !checkbox_options.iter().any(|&x| x) { | ||
return self.password = String::from("At least one of checkboxes should be checked"); | ||
} | ||
|
||
self.password = RandomPassword::new(self.options).password; | ||
|
||
if self.recent_passwords.len() >= 10 { | ||
self.recent_passwords.pop(); | ||
} | ||
|
||
self.recent_passwords.push(self.password.clone()); | ||
} | ||
|
||
if ui.button(RichText::new("Copy")).clicked() { | ||
let mut context = ClipboardContext::new().unwrap(); | ||
|
||
let _copied = context.set_contents(self.password.to_string()); | ||
} | ||
|
||
ui.separator(); | ||
|
||
ui.heading("Recent Passwords"); | ||
|
||
ui.vertical(|ui| { | ||
for password in self.recent_passwords.iter().rev() { | ||
ui.label(RichText::new(password.to_string()).size(14.0)); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
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,16 @@ | ||
use rand::distributions::Uniform; | ||
|
||
#[derive(Copy, Clone)] | ||
pub struct PasswordGeneratorOptions { | ||
pub include_lowercase: bool, | ||
pub include_numbers: bool, | ||
pub include_special_characters: bool, | ||
pub include_uppercase: bool, | ||
pub length: u8, | ||
} | ||
|
||
pub struct RandomPassword { | ||
pub characters: Vec<char>, | ||
pub password: String, | ||
pub range: Uniform<usize>, | ||
} |