Skip to content

Commit

Permalink
Add "Open folder" item in torrent/file row menu
Browse files Browse the repository at this point in the history
Issue #92
  • Loading branch information
qu1ck committed Oct 13, 2023
1 parent 2149c75 commit 71a3859
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
23 changes: 23 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tokio = { version = "^1.28", features = ["net"] }
serde = { version = "^1.0", features = ["derive"] }
tauri = { version = "^1.4", features = [ "clipboard-write-text", "cli", "devtools", "dialog-all", "fs-read-file", "fs-write-file", "notification", "path-all", "shell-open", "system-tray", "window-center", "window-close", "window-create", "window-hide", "window-set-focus", "window-set-position", "window-set-size", "window-set-title", "window-show", "window-unminimize"] }
tauri-utils = "^1.4"
opener = "0.6"
opener = { version = "0.6", features = ["reveal"] }
rodio = { version = "0.17.1", features = ["mp3"], default-features = false }
hyper-tls = "0.5.0"
notify-rust = "4.8.0"
Expand Down
9 changes: 5 additions & 4 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ pub async fn read_file(path: String) -> Result<TorrentReadResult, String> {
return Err(format!("Failed to read file {:?}", path));
}


match Torrent::read_from_bytes(&read_result.as_ref().unwrap()[..]) {
Err(_) => Err(format!("Failed to parse torrent {:?}", path)),
Ok(torrent) => {
Expand Down Expand Up @@ -105,12 +104,14 @@ pub async fn remove_file(path: String) {
}

#[tauri::command]
pub async fn shell_open(path: String) -> Result<(), String> {
pub async fn shell_open(path: String, reveal: bool) -> Result<(), String> {
#[cfg(target_os = "windows")]
let path = path.replace('/', "\\");

if let Err(e) = opener::open(path) {
return Err(e.to_string());
if reveal {
opener::reveal(path).map_err(|e| e.to_string())?
} else {
opener::open(path).map_err(|e| e.to_string())?
}
Ok(())
}
Expand Down
18 changes: 12 additions & 6 deletions src/components/tables/filetreetable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export function FileTreeTable(props: FileTreeTableProps) {
selectedReducer({ verb: "set", ids: [], isReset: true });
}, [props.fileTree.torrenthash, selectedReducer]);

const onRowDoubleClick = useCallback((row: FileDirEntry) => {
const onRowDoubleClick = useCallback((row: FileDirEntry, reveal: boolean = false) => {
if (TAURI) {
if (props.downloadDir === undefined || props.downloadDir === "") return;
let path = props.downloadDir;
Expand All @@ -296,7 +296,7 @@ export function FileTreeTable(props: FileTreeTableProps) {
}
path = path + row.fullpath + (isDirEntry(row) ? "/" : "");
path = pathMapFromServer(path, serverConfig);
invoke("shell_open", { path }).catch((e) => {
invoke("shell_open", { path, reveal }).catch((e) => {
notifications.show({
title: "Error opening path",
message: path,
Expand Down Expand Up @@ -389,16 +389,16 @@ function FiletreeContextMenu(props: {
setContextMenuInfo: (i: ContextMenuInfo) => void,
fileTree: CachedFileTree,
selected: string[],
onRowDoubleClick: (row: FileDirEntry) => void,
onRowDoubleClick: (row: FileDirEntry, reveal: boolean) => void,
setExpanded?: (state: boolean) => void,
toggleFileSearchBox: () => void,
}) {
const { onRowDoubleClick } = props;
const onOpen = useCallback(() => {
const onOpen = useCallback((reveal: boolean) => {
const [path] = [...props.selected];
const entry = props.fileTree.findEntry(path);
if (entry === undefined) return;
onRowDoubleClick(entry);
onRowDoubleClick(entry, reveal);
}, [onRowDoubleClick, props.fileTree, props.selected]);

const mutation = useMutateTorrent();
Expand Down Expand Up @@ -459,11 +459,17 @@ function FiletreeContextMenu(props: {
<ContextMenu contextMenuInfo={props.contextMenuInfo} setContextMenuInfo={props.setContextMenuInfo}>
{TAURI && <>
<Menu.Item
onClick={onOpen}
onClick={() => { onOpen(false); }}
icon={<Icon.BoxArrowUpRight size="1.1rem" />}
disabled={props.selected.length !== 1}>
<Text weight="bold">Open</Text>
</Menu.Item>
<Menu.Item
onClick={() => { onOpen(true); }}
icon={<Icon.Folder2Open size="1.1rem" />}
disabled={props.selected.length !== 1}>
<Text>Open folder</Text>
</Menu.Item>
<Menu.Divider />
</>}
<Menu.Item
Expand Down
19 changes: 13 additions & 6 deletions src/components/tables/torrenttable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export function TorrentTable(props: {
[onColumnVisibilityChange],
);

const onRowDoubleClick = useCallback((torrent: Torrent) => {
const onRowDoubleClick = useCallback((torrent: Torrent, reveal: boolean = false) => {
if (TAURI) {
if (torrent.downloadDir === undefined || torrent.downloadDir === "") return;
let path = torrent.downloadDir as string;
Expand All @@ -410,7 +410,7 @@ export function TorrentTable(props: {
}
path = path + fileSystemSafeName(torrent.name);
path = pathMapFromServer(path, serverConfig);
invoke("shell_open", { path }).catch((e) => {
invoke("shell_open", { path, reveal }).catch((e) => {
notifications.show({
title: "Error opening path",
message: path,
Expand Down Expand Up @@ -454,17 +454,17 @@ function TorrentContextMenu(props: {
contextMenuInfo: ContextMenuInfo,
setContextMenuInfo: (i: ContextMenuInfo) => void,
modals: React.RefObject<ModalCallbacks>,
onRowDoubleClick: (t: Torrent) => void,
onRowDoubleClick: (t: Torrent, reveal: boolean) => void,
}) {
const serverData = useServerTorrentData();
const serverSelected = useServerSelectedTorrents();

const { onRowDoubleClick } = props;
const onOpen = useCallback(() => {
const onOpen = useCallback((reveal: boolean) => {
const [id] = [...serverSelected];
const torrent = serverData.torrents.find((t) => t.id === id);
if (torrent === undefined) return;
onRowDoubleClick(torrent);
onRowDoubleClick(torrent, reveal);
}, [onRowDoubleClick, serverData.torrents, serverSelected]);

const mutation = useTorrentAction();
Expand Down Expand Up @@ -587,12 +587,19 @@ function TorrentContextMenu(props: {
<Box miw="14rem">
{TAURI && <>
<Menu.Item
onClick={onOpen}
onClick={() => { onOpen(false); }}
onMouseEnter={closeQueueSubmenu}
icon={<Icon.BoxArrowUpRight size="1.1rem" />}
disabled={serverSelected.size !== 1}>
<Text weight="bold">Open</Text>
</Menu.Item>
<Menu.Item
onClick={() => { onOpen(true); }}
onMouseEnter={closeQueueSubmenu}
icon={<Icon.Folder2Open size="1.1rem" />}
disabled={serverSelected.size !== 1}>
<Text>Open folder</Text>
</Menu.Item>
<Menu.Divider />
</>}
<Menu.Item
Expand Down

0 comments on commit 71a3859

Please sign in to comment.