Skip to content

Commit da25760

Browse files
committed
Add opportunity to remove recent file
1 parent 7a310e3 commit da25760

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

bytesto4t/src-tauri/src/commands/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ pub fn get_config_recent_files(app_data: State<Storage>) -> Result<Vec<String>,
109109
Ok(recent_files.clone())
110110
}
111111

112+
#[tauri::command]
113+
pub fn remove_config_recent_file(file_path: &str, app_data: State<Storage>) -> Result<(), String> {
114+
let mut app_data = app_data.app_data.lock().map_err(|e| e.to_string())?;
115+
if let Some(recent_files) = &mut app_data.app_config.recent_files {
116+
recent_files.retain(|file| file != file_path);
117+
}
118+
Ok(())
119+
}
120+
112121
#[tauri::command]
113122
pub fn set_config_openrouter_key(key: &str, app_data: State<Storage>) -> Result<(), String> {
114123
let mut app_data = app_data.app_data.lock().map_err(|e| e.to_string())?;

bytesto4t/src-tauri/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ pub fn run() {
119119
commands::config::set_config_theme,
120120
commands::config::set_config_colorscheme,
121121
commands::config::add_config_recent_file,
122+
commands::config::remove_config_recent_file,
122123
commands::config::get_config_theme,
123124
commands::config::get_config_colorscheme,
124125
commands::config::get_config_recent_files,

bytesto4t/src/routes/+page.svelte

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
let chosenRecentFile: string = "";
77
let targetFilePath: string = "";
88
let recentFiles: string[] = [];
9+
let contextMenuVisible: boolean = false;
10+
let contextMenuX: number = 0;
11+
let contextMenuY: number = 0;
12+
let contextMenuFile: string = "";
913
1014
async function setTargetFilePath(filePath: string) {
1115
try {
@@ -84,9 +88,43 @@
8488
}
8589
}
8690
91+
async function removeRecentFile(filePath: string) {
92+
try {
93+
await invoke("remove_config_recent_file", { filePath: filePath });
94+
await invoke("save_config");
95+
await getRecentFiles();
96+
contextMenuVisible = false;
97+
} catch (error) {
98+
console.error("Error removing recent file:", error);
99+
}
100+
}
101+
102+
function showContextMenu(event: MouseEvent, filePath: string) {
103+
event.preventDefault();
104+
contextMenuFile = filePath;
105+
contextMenuX = event.clientX;
106+
contextMenuY = event.clientY;
107+
contextMenuVisible = true;
108+
}
109+
110+
function hideContextMenu() {
111+
contextMenuVisible = false;
112+
}
113+
114+
function handleGlobalClick(event: MouseEvent) {
115+
if (contextMenuVisible && !(event.target as Element)?.closest('.context-menu')) {
116+
hideContextMenu();
117+
}
118+
}
119+
87120
onMount(async () => {
88121
await invoke("init_config", {});
89122
await getRecentFiles();
123+
document.addEventListener('click', handleGlobalClick);
124+
125+
return () => {
126+
document.removeEventListener('click', handleGlobalClick);
127+
};
90128
});
91129
</script>
92130

@@ -133,9 +171,28 @@
133171
onchange={onChangeListBoxItemHandler}
134172
>
135173
{#each recentFiles as recentFile}
136-
<option value={recentFile}>{recentFile}</option>
174+
<option
175+
value={recentFile}
176+
oncontextmenu={(e) => showContextMenu(e, recentFile)}
177+
>
178+
{recentFile}
179+
</option>
137180
{/each}
138181
</select>
139182
</form>
140183
</div>
184+
185+
{#if contextMenuVisible}
186+
<div
187+
class="context-menu fixed z-50 bg-surface-800 border border-surface-600 rounded-lg shadow-lg py-1 min-w-48"
188+
style="left: {contextMenuX}px; top: {contextMenuY}px;"
189+
>
190+
<button
191+
class="w-full text-left px-4 py-2 hover:bg-surface-700 transition-colors text-surface-100"
192+
onclick={() => removeRecentFile(contextMenuFile)}
193+
>
194+
Remove from list
195+
</button>
196+
</div>
197+
{/if}
141198
</main>

0 commit comments

Comments
 (0)