-
-
Notifications
You must be signed in to change notification settings - Fork 35
Feat: added rusqlite for managing clipboard items and persisting them across sessions #242
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
c98d92f
29ae44e
2459783
9e9cd5b
b3d586a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about the |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ use iced::widget::operation::AbsoluteOffset; | |
| use iced::window; | ||
| use iced::window::Id; | ||
| use log::info; | ||
| use rayon::iter::IntoParallelRefIterator; | ||
| use crate::clipboard::ClipBoardContentType; | ||
| use rayon::iter::ParallelIterator; | ||
| use rayon::slice::ParallelSliceMut; | ||
|
|
||
|
|
@@ -239,10 +239,9 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> { | |
|
|
||
| Message::SaveRanking => { | ||
| tile.ranking = tile.options.get_rankings(); | ||
| let string_rep = toml::to_string(&tile.ranking).unwrap_or("".to_string()); | ||
| let ranking_file_path = | ||
| std::env::var("HOME").unwrap_or("/".to_string()) + "/.config/rustcast/ranking.toml"; | ||
| fs::write(ranking_file_path, string_rep).ok(); | ||
| for (name, rank) in &tile.ranking { | ||
| let _ = tile.db.save_ranking(name, *rank); | ||
| } | ||
| Task::none() | ||
| } | ||
|
|
||
|
|
@@ -479,16 +478,31 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> { | |
| Message::EditClipboardHistory(action) => { | ||
| match action { | ||
| Editable::Create(content) => { | ||
| if !tile.clipboard_content.contains(&content) { | ||
| tile.clipboard_content.insert(0, content); | ||
| let old_item = tile.clipboard_content.iter().find(|x| { | ||
|
||
| if let (ClipBoardContentType::Files(f1, _), ClipBoardContentType::Files(f2, _)) = (x, &content) { | ||
| f1 == f2 | ||
| } else { | ||
| *x == &content | ||
| } | ||
| }).cloned(); | ||
|
|
||
| if old_item.is_none() { | ||
| tile.clipboard_content.insert(0, content.clone()); | ||
| let _ = tile.db.save_clipboard_item(&content); | ||
| return Task::none(); | ||
| } | ||
|
|
||
| let new_content_vec = tile | ||
| .clipboard_content | ||
| .par_iter() | ||
| .iter() | ||
| .filter_map(|x| { | ||
| if *x == content { | ||
| let is_match = if let (ClipBoardContentType::Files(f1, _), ClipBoardContentType::Files(f2, _)) = (x, &content) { | ||
| f1 == f2 | ||
| } else { | ||
| x == &content | ||
| }; | ||
|
|
||
| if is_match { | ||
| None | ||
| } else { | ||
| Some(x.to_owned()) | ||
|
|
@@ -497,7 +511,11 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> { | |
| .collect(); | ||
|
|
||
| tile.clipboard_content = new_content_vec; | ||
| tile.clipboard_content.insert(0, content); | ||
| tile.clipboard_content.insert(0, content.clone()); | ||
| if let Some(old) = old_item { | ||
| let _ = tile.db.delete_clipboard_item(&old); | ||
|
||
| } | ||
| let _ = tile.db.save_clipboard_item(&content); | ||
| } | ||
| Editable::Delete(content) => { | ||
| tile.clipboard_content = tile | ||
|
|
@@ -511,13 +529,16 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> { | |
| } | ||
| }) | ||
| .collect(); | ||
| let _ = tile.db.delete_clipboard_item(&content); | ||
| } | ||
| Editable::Update { old, new } => { | ||
| tile.clipboard_content = tile | ||
| .clipboard_content | ||
| .iter() | ||
| .map(|x| if x == &old { new.clone() } else { x.to_owned() }) | ||
| .collect(); | ||
| let _ = tile.db.delete_clipboard_item(&old); | ||
| let _ = tile.db.save_clipboard_item(&new); | ||
| } | ||
| } | ||
| Task::none() | ||
|
|
@@ -765,6 +786,7 @@ pub fn handle_update(tile: &mut Tile, message: Message) -> Task<Message> { | |
|
|
||
| Message::ClearClipboardHistory => { | ||
| tile.clipboard_content.clear(); | ||
| let _ = tile.db.clear_clipboard(); | ||
| Task::none() | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,15 +11,40 @@ use crate::{ | |
| pub enum ClipBoardContentType { | ||
| Text(String), | ||
| Image(ImageData<'static>), | ||
| Files(Vec<String>, Option<ImageData<'static>>), | ||
| } | ||
|
|
||
| impl ToApp for ClipBoardContentType { | ||
| /// Returns the iced element for rendering the clipboard item, and the entire content since the | ||
| /// display name is only the first line | ||
| fn to_app(&self) -> App { | ||
| let mut display_name = match self { | ||
| ClipBoardContentType::Image(_) => "Image".to_string(), | ||
| ClipBoardContentType::Text(a) => a.get(0..25).unwrap_or(a).to_string(), | ||
| let (mut display_name, desc) = match self { | ||
|
||
| ClipBoardContentType::Image(_) => ("Image".to_string(), "Clipboard Item".to_string()), | ||
| ClipBoardContentType::Text(a) => ( | ||
| a.get(0..25).unwrap_or(a).to_string(), | ||
| "Clipboard Item".to_string(), | ||
| ), | ||
| ClipBoardContentType::Files(f, _) => { | ||
| if f.len() == 1 { | ||
| let path = std::path::Path::new(&f[0]); | ||
| let name = path | ||
| .file_name() | ||
| .unwrap_or_default() | ||
| .to_string_lossy() | ||
| .to_string(); | ||
| // Fall back to the raw path string if the file name was entirely empty | ||
| let mut final_name = name; | ||
| if final_name.is_empty() { | ||
| final_name = f[0].clone(); | ||
| } | ||
| (final_name, f[0].clone()) | ||
| } else { | ||
| ( | ||
| format!("{} Files", f.len()), | ||
| "Multiple files copied".to_string(), | ||
| ) | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| let self_clone = self.clone(); | ||
|
|
@@ -33,7 +58,7 @@ impl ToApp for ClipBoardContentType { | |
| open_command: crate::app::apps::AppCommand::Function(Function::CopyToClipboard( | ||
| self_clone.to_owned(), | ||
| )), | ||
| desc: "Clipboard Item".to_string(), | ||
| desc, | ||
| icons: None, | ||
unsecretised marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| display_name, | ||
| search_name, | ||
|
|
@@ -52,6 +77,17 @@ impl PartialEq for ClipBoardContentType { | |
| && let Self::Image(other_image_data) = other | ||
| { | ||
|
||
| return image_data.bytes == other_image_data.bytes; | ||
| } else if let Self::Files(f1, img1) = self | ||
| && let Self::Files(f2, img2) = other | ||
| { | ||
| if f1 != f2 { | ||
| return false; | ||
| } | ||
| return match (img1, img2) { | ||
| (Some(a), Some(b)) => a.bytes == b.bytes, | ||
| (None, None) => true, | ||
| _ => false, | ||
| }; | ||
| } | ||
| false | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this styling to styles.rs