Clean up whitespace

This commit is contained in:
Philip Kristoffersen
2022-04-28 23:39:23 +02:00
parent 29ad5ee2e7
commit 8e9cbd8652
4 changed files with 87 additions and 81 deletions
+5 -3
View File
@@ -79,9 +79,11 @@ pub async fn download_images_for_users<'b>(
println!("Finished getting images in: {:?}", duration); println!("Finished getting images in: {:?}", duration);
//Validate that the downloads where ok //Validate that the downloads where ok
for to_download in to_downloads{ for to_download in to_downloads {
let file_length = std::fs::metadata(&to_download.path).map(|m| m.len()).unwrap_or_default(); let file_length = std::fs::metadata(&to_download.path)
if file_length < 2{ .map(|m| m.len())
.unwrap_or_default();
if file_length < 2 {
// Image is too small, something went wrong // Image is too small, something went wrong
//Try to delete file again, don't care if it fails //Try to delete file again, don't care if it fails
let _ = std::fs::remove_file(&to_download.path); let _ = std::fs::remove_file(&to_download.path);
+62 -57
View File
@@ -87,7 +87,8 @@ impl MyEguiApp {
fn render_ui_image_action(&self, ui: &mut egui::Ui) -> UserAction { fn render_ui_image_action(&self, ui: &mut egui::Ui) -> UserAction {
let state = &self.image_selected_state; let state = &self.image_selected_state;
ui.heading("Images"); ui.heading("Images");
if (state.selected_shortcut.is_some() || (state.has_multiple_users() && state.steam_user.is_some())) if (state.selected_shortcut.is_some()
|| (state.has_multiple_users() && state.steam_user.is_some()))
&& ui.button("Back").clicked() && ui.button("Back").clicked()
{ {
return UserAction::BackButton; return UserAction::BackButton;
@@ -124,24 +125,27 @@ impl MyEguiApp {
Some(shortcuts) => { Some(shortcuts) => {
let user_info = &self.image_selected_state.steam_user.as_ref().unwrap(); let user_info = &self.image_selected_state.steam_user.as_ref().unwrap();
for shortcut in shortcuts { for shortcut in shortcuts {
let (_,key) = shortcut.key(&ImageType::Grid, Path::new(&user_info.steam_user_data_folder)); let (_, key) = shortcut.key(
&ImageType::Grid,
Path::new(&user_info.steam_user_data_folder),
);
let texture = self.image_selected_state.image_handles.get(&key); let texture = self.image_selected_state.image_handles.get(&key);
let mut clicked = false; let mut clicked = false;
if let Some(texture) = texture{ if let Some(texture) = texture {
match &texture.value() { match &texture.value() {
TextureState::Loaded(texture) => { TextureState::Loaded(texture) => {
let mut size = texture.size_vec2(); let mut size = texture.size_vec2();
clamp_to_width(&mut size, 100.); clamp_to_width(&mut size, 100.);
let image_button = ImageButton::new(texture,size); let image_button = ImageButton::new(texture, size);
clicked = clicked || ui.add(image_button).clicked(); clicked = clicked || ui.add(image_button).clicked();
}, }
_=> {} _ => {}
} }
} }
let button = ui.button(&shortcut.app_name); let button = ui.button(&shortcut.app_name);
clicked = clicked || button.clicked(); clicked = clicked || button.clicked();
if clicked{ if clicked {
return Some(UserAction::ShortcutSelected(shortcut.clone())); return Some(UserAction::ShortcutSelected(shortcut.clone()));
} }
} }
@@ -176,8 +180,8 @@ impl MyEguiApp {
} }
TextureState::Downloaded => { TextureState::Downloaded => {
//Need to load //Need to load
let image_data =load_image_from_path(&image.thumbnail_path); let image_data = load_image_from_path(&image.thumbnail_path);
if let Some(image_data) = image_data{ if let Some(image_data) = image_data {
let handle = ui.ctx().load_texture(&image_key, image_data); let handle = ui.ctx().load_texture(&image_key, image_data);
*state.value_mut() = TextureState::Loaded(handle); *state.value_mut() = TextureState::Loaded(handle);
} }
@@ -200,7 +204,9 @@ impl MyEguiApp {
let image_handles = &self.image_selected_state.image_handles; let image_handles = &self.image_selected_state.image_handles;
let path = &image.thumbnail_path; let path = &image.thumbnail_path;
//Redownload if file is too small //Redownload if file is too small
if !path.exists() || std::fs::metadata(path).map(|m| m.len()).unwrap_or_default() < 2{ if !path.exists()
|| std::fs::metadata(path).map(|m| m.len()).unwrap_or_default() < 2
{
image_handles.insert(image_key.clone(), TextureState::Downloading); image_handles.insert(image_key.clone(), TextureState::Downloading);
let to_download = ToDownload { let to_download = ToDownload {
path: path.clone(), path: path.clone(),
@@ -313,7 +319,7 @@ impl MyEguiApp {
fn handle_user_selected(&mut self, user: SteamUsersInfo, ui: &mut egui::Ui) { fn handle_user_selected(&mut self, user: SteamUsersInfo, ui: &mut egui::Ui) {
let state = &mut self.image_selected_state; let state = &mut self.image_selected_state;
let user_info = crate::steam::get_shortcuts_for_user(&user); let user_info = crate::steam::get_shortcuts_for_user(&user);
let mut user_folder =user_info.path.clone(); let mut user_folder = user_info.path.clone();
user_folder.pop(); user_folder.pop();
user_folder.pop(); user_folder.pop();
let mut shortcuts = user_info.shortcuts; let mut shortcuts = user_info.shortcuts;
@@ -321,13 +327,15 @@ impl MyEguiApp {
let image_type = &ImageType::Grid; let image_type = &ImageType::Grid;
for shortcut in &shortcuts { for shortcut in &shortcuts {
let (path,key) = shortcut.key(image_type, &user_folder); let (path, key) = shortcut.key(image_type, &user_folder);
let loaded = state.image_handles.contains_key(&key); let loaded = state.image_handles.contains_key(&key);
if !loaded && path.exists(){ if !loaded && path.exists() {
let image = load_image_from_path(&path); let image = load_image_from_path(&path);
if let Some(image) = image{ if let Some(image) = image {
let texture = ui.ctx().load_texture(&key, image); let texture = ui.ctx().load_texture(&key, image);
state.image_handles.insert(key, TextureState::Loaded(texture)); state
.image_handles
.insert(key, TextureState::Loaded(texture));
} }
} }
} }
@@ -389,23 +397,18 @@ impl MyEguiApp {
.join("grid") .join("grid")
.join(selected_image_type.file_name(selected_image.app_id)); .join(selected_image_type.file_name(selected_image.app_id));
if to.exists(){ if to.exists() {
let old_key = to.to_string_lossy().to_string(); let old_key = to.to_string_lossy().to_string();
let new_key = image.thumbnail_path.to_string_lossy().to_string(); let new_key = image.thumbnail_path.to_string_lossy().to_string();
let _ = self.image_selected_state.image_handles.remove(&old_key); let _ = self.image_selected_state.image_handles.remove(&old_key);
let swap = self.image_selected_state.image_handles.get(&new_key); let swap = self.image_selected_state.image_handles.get(&new_key);
if let Some(swp) = swap{ if let Some(swp) = swap {
self.image_selected_state.image_handles.insert(old_key, swp.value().clone()); self.image_selected_state
.image_handles
.insert(old_key, swp.value().clone());
} }
} }
let app_name = selected_image.app_name.clone(); let app_name = selected_image.app_name.clone();
let to_download = ToDownload { let to_download = ToDownload {
path: to, path: to,
@@ -425,14 +428,13 @@ impl MyEguiApp {
} }
fn clear_loaded_images(&mut self) { fn clear_loaded_images(&mut self) {
match &*self.image_selected_state.image_options.borrow() {
match &*self.image_selected_state.image_options.borrow(){
FetcStatus::Fetched(options) => { FetcStatus::Fetched(options) => {
for option in options{ for option in options {
let key = option.thumbnail_path.to_string_lossy().to_string(); let key = option.thumbnail_path.to_string_lossy().to_string();
self.image_selected_state.image_handles.remove(&key); self.image_selected_state.image_handles.remove(&key);
} }
}, }
_ => {} _ => {}
} }
} }
@@ -452,13 +454,14 @@ impl MyEguiApp {
} }
state.selected_shortcut = Some(shortcut.clone()); state.selected_shortcut = Some(shortcut.clone());
for image_type in ImageType::all() {
for image_type in ImageType::all(){ let (path, key) = shortcut.key(image_type, &Path::new(&user.steam_user_data_folder));
let (path,key) = shortcut.key(image_type, &Path::new(&user.steam_user_data_folder));
let image = load_image_from_path(&path); let image = load_image_from_path(&path);
if let Some(image) = image{ if let Some(image) = image {
let texture = ui.ctx().load_texture(&key, image); let texture = ui.ctx().load_texture(&key, image);
state.image_handles.insert(key,TextureState::Loaded(texture)); state
.image_handles
.insert(key, TextureState::Loaded(texture));
} }
} }
state.selected_shortcut = Some(shortcut); state.selected_shortcut = Some(shortcut);
@@ -510,19 +513,18 @@ fn render_shortcut_images(ui: &mut egui::Ui, state: &ImageSelectState) -> Option
let user_path = &state.steam_user.as_ref().unwrap().steam_user_data_folder; let user_path = &state.steam_user.as_ref().unwrap().steam_user_data_folder;
for image_type in ImageType::all() { for image_type in ImageType::all() {
ui.label(image_type.name()); ui.label(image_type.name());
let (_path,key) = shortcut.key(&image_type, Path::new(&user_path)); let (_path, key) = shortcut.key(&image_type, Path::new(&user_path));
let texture = state.image_handles.get(&key).map(|k| { let texture = state
match k.value(){ .image_handles
.get(&key)
.map(|k| match k.value() {
TextureState::Loaded(texture) => Some(texture.clone()), TextureState::Loaded(texture) => Some(texture.clone()),
_ => { _ => None,
None })
} .flatten();
}
}).flatten();
if render_thumbnail(ui, texture, &image_type) { if render_thumbnail(ui, texture, &image_type) {
return Some(UserAction::ImageTypeSelected(*image_type)); return Some(UserAction::ImageTypeSelected(*image_type));
} }
} }
None None
} }
@@ -542,24 +544,27 @@ fn render_user_select(state: &ImageSelectState, ui: &mut egui::Ui) -> UserAction
const MAX_WIDTH: f32 = 300.; const MAX_WIDTH: f32 = 300.;
fn render_thumbnail(ui: &mut egui::Ui, image: Option<egui::TextureHandle>, image_type:&ImageType) -> bool { fn render_thumbnail(
ui: &mut egui::Ui,
image: Option<egui::TextureHandle>,
image_type: &ImageType,
) -> bool {
if let Some(texture) = image { if let Some(texture) = image {
let mut size = texture.size_vec2(); let mut size = texture.size_vec2();
clamp_to_width(&mut size, MAX_WIDTH); clamp_to_width(&mut size, MAX_WIDTH);
let image_button = ImageButton::new(&texture, size); let image_button = ImageButton::new(&texture, size);
let added =ui.add(image_button); let added = ui.add(image_button);
match image_type{ match image_type {
ImageType::Icon => false, ImageType::Icon => false,
_=> added.on_hover_text("Click to change image").clicked() _ => added.on_hover_text("Click to change image").clicked(),
} }
} else { } else {
match image_type{ match image_type {
ImageType::Icon => { ImageType::Icon => {
ui.label("No icon found"); ui.label("No icon found");
false false
}, }
_=> _ => ui.button("Pick an image").clicked(),
ui.button("Pick an image").clicked(),
} }
} }
} }
@@ -591,15 +596,15 @@ fn get_image(
image image
} }
trait HasImageKey{ trait HasImageKey {
fn key(&self,image_type:&ImageType,user_path:&Path) -> (PathBuf,String); fn key(&self, image_type: &ImageType, user_path: &Path) -> (PathBuf, String);
} }
impl HasImageKey for ShortcutOwned{ impl HasImageKey for ShortcutOwned {
fn key(&self, image_type:&ImageType, user_path:&Path) -> (PathBuf,String) { fn key(&self, image_type: &ImageType, user_path: &Path) -> (PathBuf, String) {
let file_name = image_type.file_name(self.app_id); let file_name = image_type.file_name(self.app_id);
let path = user_path.join("config").join("grid").join(&file_name); let path = user_path.join("config").join("grid").join(&file_name);
let key = path.to_string_lossy().to_string(); let key = path.to_string_lossy().to_string();
(path,key) (path, key)
} }
} }
+8 -5
View File
@@ -184,12 +184,12 @@ impl MyEguiApp {
.unwrap_or(&mut empty_string); .unwrap_or(&mut empty_string);
let mut auth_key_clipboard = false; let mut auth_key_clipboard = false;
ui.label("Authentication key: "); ui.label("Authentication key: ");
if let Ok(mut clipboard_ctx) = copypasta::ClipboardContext::new(){ if let Ok(mut clipboard_ctx) = copypasta::ClipboardContext::new() {
if let Ok(content) = clipboard_ctx.get_contents(){ if let Ok(content) = clipboard_ctx.get_contents() {
if auth_key.is_empty() && looks_like_auth_key(&content) { if auth_key.is_empty() && looks_like_auth_key(&content) {
*auth_key = content.clone(); *auth_key = content.clone();
auth_key_clipboard = true; auth_key_clipboard = true;
}else if content.eq(auth_key){ } else if content.eq(auth_key) {
auth_key_clipboard = true; auth_key_clipboard = true;
} }
} }
@@ -197,7 +197,7 @@ impl MyEguiApp {
if ui.text_edit_singleline(auth_key).changed() { if ui.text_edit_singleline(auth_key).changed() {
self.settings.steamgrid_db.auth_key = Some(auth_key.to_string()); self.settings.steamgrid_db.auth_key = Some(auth_key.to_string());
} }
if auth_key_clipboard{ if auth_key_clipboard {
ui.label("Found API Key in clipboard"); ui.label("Found API Key in clipboard");
} }
}); });
@@ -292,5 +292,8 @@ impl MyEguiApp {
} }
fn looks_like_auth_key(content: &String) -> bool { fn looks_like_auth_key(content: &String) -> bool {
content.len() == 32 && content.is_ascii() && content.chars().all(char::is_alphanumeric) && content.chars().all(char::is_lowercase) content.len() == 32
&& content.is_ascii()
&& content.chars().all(char::is_alphanumeric)
&& content.chars().all(char::is_lowercase)
} }
-4
View File
@@ -83,10 +83,6 @@ impl epi::App for MyEguiApp {
let mut style: egui::Style = (*ctx.style()).clone(); let mut style: egui::Style = (*ctx.style()).clone();
create_style(&mut style); create_style(&mut style);
ctx.set_style(style); ctx.set_style(style);
//Clean up thumbnails
} }
fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) {