Skip to content

Commit

Permalink
new day new cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Dec 20, 2024
1 parent 1e2db9d commit e59d85e
Show file tree
Hide file tree
Showing 26 changed files with 361 additions and 183 deletions.
44 changes: 44 additions & 0 deletions src-tauri/common/src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::{constants::MAX_TEXT_PREVIEW, types::orm_query::ClipboardWithRelations};

pub fn trim_clipboard_data(
mut clipboards: Vec<ClipboardWithRelations>,
) -> Vec<ClipboardWithRelations> {
for clipboard in &mut clipboards {
// Trim text content
if let Some(text) = &mut clipboard.text {
text.data = truncate_text(&text.data, MAX_TEXT_PREVIEW);
}

// Trim HTML content
if let Some(html) = &mut clipboard.html {
html.data = truncate_text(&html.data, MAX_TEXT_PREVIEW);
}

// Trim RTF content
if let Some(rtf) = &mut clipboard.rtf {
rtf.data = truncate_text(&rtf.data, MAX_TEXT_PREVIEW);
}

// Remove image binary data but keep metadata
if let Some(image) = &mut clipboard.image {
image.data = Vec::new(); // Clear binary data
// Thumbnail, dimensions, size etc are preserved
}

// Clear file binary data but keep metadata
for file in &mut clipboard.files {
file.data = Vec::new(); // Clear binary data
// Name, extension, size etc are preserved
}
}

clipboards
}

fn truncate_text(text: &str, max_length: usize) -> String {
if text.len() <= max_length {
text.to_string()
} else {
format!("{}...", &text[..max_length])
}
}
1 change: 1 addition & 0 deletions src-tauri/common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub static SETTINGS_WINDOW_X: i32 = 500;
pub static SETTINGS_WINDOW_Y: i32 = 450;

pub static MAX_IMAGE_DIMENSIONS: u32 = 1280;
pub static MAX_TEXT_PREVIEW: usize = 500; // Adjust preview length as needed

pub static DISPLAY_SCALE: f32 = 1.0;
pub static DISPLAY_SCALE_MIN: f32 = 0.5;
Expand Down
1 change: 1 addition & 0 deletions src-tauri/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod keyboard;
pub mod language;
pub mod macros;
pub mod types;
pub mod clipboard;
pub mod constants;
6 changes: 4 additions & 2 deletions src-tauri/common/src/types/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ pub enum Language {
pub enum ListenEvent {
#[iden = "init"]
Init,
#[iden = "set_global_hotkey_event"]
SetGlobalHotkeyEvent,
#[iden = "enable_global_hotkey_event"]
EnableGlobalHotkeyEvent,
#[iden = "change_tab"]
ChangeTab,
#[iden = "scroll_to_top"]
ScrollToTop,
#[iden = "new_clipboard"]
NewClipboard,
}

#[derive(Iden, EnumIter, PartialEq, Serialize, Deserialize, Debug, Clone)]
Expand Down
7 changes: 7 additions & 0 deletions src-tauri/common/src/types/orm_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ pub struct ClipboardWithRelations {
pub files: Vec<clipboard_file::Model>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClipboardsResponse {
pub clipboards: Vec<ClipboardWithRelations>,
pub total: u64,
pub has_more: bool,
}

#[derive(Debug, Clone)]
pub struct ClipboardManager {
pub clipboard_model: entity::clipboard::ActiveModel,
Expand Down
32 changes: 27 additions & 5 deletions src-tauri/src/commands/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ extern crate alloc;
extern crate image;
use crate::{
service::clipboard::{
clear_clipboards_db, copy_clipboard_from_id, delete_clipboard_db, get_clipboard_db,
get_clipboards_db, star_clipboard_db,
clear_clipboards_db, copy_clipboard_from_id, delete_clipboard_db, get_clipboard_count_db,
get_clipboard_db, get_clipboards_db, star_clipboard_db,
},
tauri_config::config::APP,
utils::hotkey_manager::unregister_hotkeys,
};
use common::types::{enums::ClipboardType, orm_query::ClipboardWithRelations, types::CommandError};
use common::{
clipboard::trim_clipboard_data,
printlog,
types::{enums::ClipboardType, orm_query::ClipboardsResponse, types::CommandError},
};
use std::fs::File;
use tauri::Manager;

Expand All @@ -18,8 +22,26 @@ pub async fn get_clipboards(
search: Option<String>,
star: Option<bool>,
img: Option<bool>,
) -> Result<Vec<ClipboardWithRelations>, CommandError> {
Ok(get_clipboards_db(cursor, search, star, img).await?)
) -> Result<ClipboardsResponse, CommandError> {
let clipboards = get_clipboards_db(cursor, search, star, img).await?;
let total = get_clipboard_count_db().await?;

// Calculate if there are more items
let current_position = cursor.unwrap_or(0) + clipboards.len() as u64;
let has_more = current_position < total;

printlog!(
"Total: {}, Current Position: {}, Has More: {}",
total,
current_position,
has_more
);

Ok(ClipboardsResponse {
clipboards: trim_clipboard_data(clipboards),
total,
has_more,
})
}

#[tauri::command]
Expand Down
1 change: 0 additions & 1 deletion src-tauri/src/events/hotkey_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pub fn init_hotkey_listener() {
loop {
if let Ok(event) = receiver.try_recv() {
if event.state == HotKeyState::Pressed {
printlog!("hotkey caught {:?}", event);
let hotkey = get_hotkey_store().get(&event.id).cloned();
if let Some(hotkey) = hotkey {
parse_hotkey_event(&hotkey).await;
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/events/window_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn init_window_event_listener() {
unregister_hotkeys(false);
get_main_window()
.emit(
ListenEvent::SetGlobalHotkeyEvent.to_string().as_str(),
ListenEvent::EnableGlobalHotkeyEvent.to_string().as_str(),
false,
)
.expect("failed to emit event");
Expand Down
14 changes: 11 additions & 3 deletions src-tauri/src/service/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ pub async fn load_clipboards_with_relations(
.collect()
}

pub async fn get_clipboard_count_db() -> Result<u64, DbErr> {
let db = connection::db().await?;

let count = clipboard::Entity::find().count(&db).await?;

Ok(count)
}

pub async fn insert_clipboard_db(model: ClipboardManager) -> Result<ClipboardWithRelations, DbErr> {
let db = connection::db().await?;
let clipboard = model.clipboard_model.insert(&db).await?;
Expand Down Expand Up @@ -181,7 +189,7 @@ pub async fn get_clipboards_db(
q.filter(f)
})
.offset(cursor)
.limit(10)
.limit(25)
.order_by_desc(clipboard::Column::Id);

Ok(load_clipboards_with_relations(query.all(&db).await?).await)
Expand Down Expand Up @@ -249,7 +257,7 @@ pub async fn copy_clipboard_from_index(i: u64) -> Result<Option<Model>, DbErr> {
}

pub async fn copy_clipboard_from_id(id: i32, requested_type: ClipboardType) -> Result<bool, DbErr> {
printlog!("type {:?}", requested_type);
printlog!("copy clipboard type: {:?} id:{:?}", requested_type, id);
let clipboard_data = get_clipboard_db(id).await?;
let clipboard = get_app().state::<Clipboard>();

Expand Down Expand Up @@ -290,7 +298,7 @@ pub async fn copy_clipboard_from_id(id: i32, requested_type: ClipboardType) -> R
}
.is_some();

if success {
if success && !cfg!(debug_assertions) {
get_main_window().hide().ok();
}

Expand Down
7 changes: 2 additions & 5 deletions src-tauri/src/service/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ pub fn toggle_main_window() {
get_main_window().hide().expect("Failed to hide window");
unregister_hotkeys(false);
get_main_window()
.emit(
ListenEvent::SetGlobalHotkeyEvent.to_string().as_str(),
false,
)
.emit(ListenEvent::EnableGlobalHotkeyEvent.to_string().as_str(), false)
.expect("Failed to emit set global hotkey event");
} else {
position_window_near_cursor();
Expand All @@ -66,7 +63,7 @@ pub fn toggle_main_window() {

register_hotkeys(true);
get_main_window()
.emit(ListenEvent::SetGlobalHotkeyEvent.to_string().as_str(), true)
.emit(ListenEvent::EnableGlobalHotkeyEvent.to_string().as_str(), true)
.expect("Failed to emit set global hotkey event");

get_app()
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/utils/clipboard_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ impl ClipboardManagerExt for ClipboardManager {
.await;

if !manager.check_if_last_is_same().await {
insert_clipboard_db(manager)
let clipboard = insert_clipboard_db(manager)
.await
.expect("Failed to insert");
get_app_window(WebWindow::Main)
.emit(ListenEvent::Init.to_string().as_str(), ())
.emit(ListenEvent::NewClipboard.to_string().as_str(), clipboard)
.expect("Failed to emit");
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/pages/app/app.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BsHddFill } from "solid-icons/bs";
import { FiGlobe } from "solid-icons/fi";
import { Show } from "solid-js";
import { AppStore } from "../../../store/app-store";
import { SettingsStore } from "../../../store/settings-store";
import { AppSidebar } from "../../navigation/app-sidebar";
import { ClipboardHistory } from "./clipboard-history";
import { RecentClipboards } from "./recent-clipboards";
import { StarredClipboards } from "./starred-clipboards";
import { ViewMore } from "./view-more";
import { AppStore } from "../../../store/app-store";
import { SettingsStore } from "../../../store/settings-store";

function App() {
const { settings } = SettingsStore;
Expand All @@ -19,7 +19,7 @@ function App() {
<AppSidebar />
</div>
<div class="flex h-screen min-w-0 flex-1 flex-col">
<div class="flex w-full justify-between px-2 py-1">
<div class="z-10 flex w-full justify-between overflow-visible px-2 py-1">
<p class="select-none bg-gray-50 text-xs font-semibold text-gray-500 dark:bg-dark-dark dark:text-white">
{getCurrentTab()?.name?.toUpperCase()}
</p>
Expand Down
67 changes: 37 additions & 30 deletions src/components/pages/app/clipboard/base-clipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ import { TextClipboard } from "./text-clipboard";
interface BaseClipboardProps {
data: ClipboardWithRelations;
index: number;
isSelected: boolean;
}

export const BaseClipboard: Component<BaseClipboardProps> = (props) => {
const { setClipboards } = ClipboardStore;
const { setClipboards, resetClipboards } = ClipboardStore;
const { clipboard } = props.data;

const handleDelete = async (id: number) => {
if (await invokeCommand(InvokeCommand.DeleteClipboard, { id })) {
setClipboards((prev) => prev.filter((o) => o.clipboard.id !== id));
setClipboards((prev) => {
const updated = prev.filter((o) => o.clipboard.id !== id);
if (!updated.length) resetClipboards();
return updated;
});
}
};

Expand Down Expand Up @@ -64,47 +69,49 @@ export const BaseClipboard: Component<BaseClipboardProps> = (props) => {
};

return (
<div class="group relative">
<div class={`group relative ${props.isSelected ? "bg-zinc-100 dark:bg-neutral-600" : ""}`}>
{/* Actions overlay */}
<div class="absolute bottom-0 right-0 top-0 z-10 flex flex-col items-end justify-between">
<div class="flex flex-col justify-between">
<VsStarFull
onClick={(e) => {
e.stopPropagation();
handleStar(clipboard);
}}
class={`${
clipboard.star ? "text-yellow-400 dark:text-yellow-300" : "hidden text-zinc-700"
} text-lg hover:text-yellow-400 group-hover:block dark:text-white dark:hover:text-yellow-300`}
<div class="absolute bottom-0 right-0 top-0 z-10 my-1 mr-0.5 flex flex-col items-end justify-between">
<VsStarFull
onClick={(e) => {
e.stopPropagation();
handleStar(clipboard);
}}
title="Star"
class={`${
clipboard.star ? "text-yellow-400 dark:text-yellow-300" : "hidden text-zinc-700"
} cursor-pointer text-lg hover:text-yellow-400 group-hover:block dark:text-white dark:hover:text-yellow-300`}
/>
{props.data.rtf && (
<BsJournalRichtext
onClick={handleRtfCopy}
title="Copy as RTF"
class="hidden cursor-pointer text-lg text-zinc-700 hover:text-blue-600 group-hover:block dark:text-white dark:hover:text-blue-400"
/>
)}
{props.data.html && (
<TbSourceCode
onClick={handleHtmlCopy}
title="Copy as HTML"
class="hidden cursor-pointer text-lg text-zinc-700 hover:text-green-600 group-hover:block dark:text-white dark:hover:text-green-400"
/>
{props.data.rtf && (
<BsJournalRichtext
onClick={handleRtfCopy}
title="Copy as RTF"
class="hidden text-lg text-zinc-700 hover:text-blue-600 group-hover:block dark:text-white dark:hover:text-blue-400"
/>
)}
{props.data.html && (
<TbSourceCode
onClick={handleHtmlCopy}
title="Copy as HTML"
class="hidden text-lg text-zinc-700 hover:text-green-600 group-hover:block dark:text-white dark:hover:text-green-400"
/>
)}
</div>
)}
<IoTrashOutline
onClick={(e) => {
e.stopPropagation();
handleDelete(clipboard.id);
}}
class="hidden text-lg text-zinc-700 hover:text-red-600 group-hover:block dark:text-white dark:hover:text-red-600"
title="Delete"
class="hidden cursor-pointer text-lg text-zinc-700 hover:text-red-600 group-hover:block dark:text-white dark:hover:text-red-600"
/>
</div>

{/* Content rendered by specific clipboard type */}
{clipboard.types.includes(ClipboardType.Image) && <ImageClipboard {...props} />}
{clipboard.types.includes(ClipboardType.File) && <FileClipboard {...props} />}
{clipboard.types.includes(ClipboardType.Text) && <TextClipboard {...props} />}
{(clipboard.types.includes(ClipboardType.Text) ||
clipboard.types.includes(ClipboardType.Html) ||
clipboard.types.includes(ClipboardType.Rtf)) && <TextClipboard {...props} />}
</div>
);
};
Loading

0 comments on commit e59d85e

Please sign in to comment.