Skip to content

Commit

Permalink
Allow usage without loading file
Browse files Browse the repository at this point in the history
  • Loading branch information
benediktwerner committed Nov 15, 2021
1 parent 293d89d commit 70ccd8a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
59 changes: 32 additions & 27 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anyhow::{bail, ensure};
use chrono::Utc;
use copypasta::ClipboardProvider;
use eframe::{
egui::{self, vec2, Button, DragValue, Grid, Key, Layout, ProgressBar, Slider, TextEdit, Ui},
egui::{self, vec2, Button, DragValue, Grid, Key, Layout, ProgressBar, TextEdit},
epi,
};
use num_format::{Locale, ToFormattedString};
Expand Down Expand Up @@ -66,18 +66,16 @@ pub struct App {
}

impl App {
fn render_pick_file(&mut self, ui: &mut Ui) {
if ui.button("Load Data").clicked() {
if let Some(path) = rfd::FileDialog::new()
.add_filter("Usernames", &["txt", "txt.gz", "txt.gpg", "txt.gz.gpg"])
.pick_file()
{
let ext = path.extension().unwrap_or_default();
if ext == "gpg" {
self.state = State::AskPassword(path);
} else {
self.load_plain(path);
}
fn load_file(&mut self) {
if let Some(path) = rfd::FileDialog::new()
.add_filter("Usernames", &["txt", "txt.gz", "txt.gpg", "txt.gz.gpg"])
.pick_file()
{
let ext = path.extension().unwrap_or_default();
if ext == "gpg" {
self.state = State::AskPassword(path);
} else {
self.load_plain(path);
}
}
}
Expand Down Expand Up @@ -303,9 +301,6 @@ impl epi::App for App {
use State::*;

egui::CentralPanel::default().show(ctx, |ui| match &mut self.state {
PickFile => {
ui.centered_and_justified(|ui| self.render_pick_file(ui));
}
AskPassword(path) => {
let mut decrypt = false;
ui.vertical_centered(|ui| {
Expand Down Expand Up @@ -337,19 +332,33 @@ impl epi::App for App {
}
Some(Err(msg)) => {
show_error(msg);
self.state = PickFile;
self.state = State::default();
}
None => (),
}
}
});
}
Loaded(s) => {
let mut do_load_file = false;
let mut do_load_clipboard = false;

// First taskbar (search controls)
ui.horizontal_wrapped(|ui| {
ui.add(Slider::new(&mut self.page_size, 10..=100).text("Results per page"));
do_load_file = ui.button("Load file").clicked();
do_load_clipboard = ui
.button("Load from clipboard")
.on_hover_text(
"One username per line with leading slash or Lichess URL.\n\
Non-conforming lines and text after the username will be removed.",
)
.clicked();
ui.add_space(20.0);

ui.label("Results per page:");
ui.add(DragValue::new(&mut self.page_size).clamp_range(10..=100));
ui.add_space(20.0);

ui.label("Search mode: ");
ui.radio_value(&mut self.search_mode, SearchMode::Plain, "Plain")
.on_hover_text("Search for the text as is");
Expand Down Expand Up @@ -406,13 +415,17 @@ impl epi::App for App {
}
});

if do_load_file {
self.load_file();
return;
}

ui.separator();

let results = s.results.clone();
let mut results = results.lock().unwrap();
let mut do_search = false;
let mut do_update = false;
let mut do_load_clipboard = false;
let mut do_close = false;

// Second taskbar (search input + save lists)
Expand Down Expand Up @@ -453,14 +466,6 @@ impl epi::App for App {
}
ui.add_space(20.0);
}
do_load_clipboard = ui
.button("Load from clipboard")
.on_hover_text(
"One username per line with leading slash or Lichess URL.\n\
Non-conforming lines and text after the username will be removed.",
)
.clicked();
ui.add_space(20.0);
do_update = ui
.button("Fetch additional info")
.on_hover_text(
Expand Down
3 changes: 1 addition & 2 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ pub struct LoadedState {
}

pub enum State {
PickFile,
AskPassword(PathBuf),
Loading(LoadingState),
Loaded(LoadedState),
Expand All @@ -112,7 +111,7 @@ impl State {

impl Default for State {
fn default() -> Self {
Self::PickFile
Self::loaded(Vec::new())
}
}

Expand Down

0 comments on commit 70ccd8a

Please # to comment.