Skip to content

Commit

Permalink
Add option to reveal in folder
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Jan 13, 2022
1 parent f4cdad8 commit a19be98
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 18 deletions.
12 changes: 12 additions & 0 deletions re.sonny.Junction.json
Expand Up @@ -107,6 +107,18 @@
}
]
},
{
"name": "libportal",
"buildsystem": "meson",
"config-opts": ["-Ddocs=false", "-Dvapi=false", "-Dbackends=gtk4"],
"sources": [
{
"type": "archive",
"url": "https://github.com/flatpak/libportal/releases/download/0.5/libportal-0.5.tar.xz",
"sha256": "d8c8cb18a34e5eeb26a39c94044c955995b01de0e139caac5e18c076cf821b3b"
}
]
},
{
"name": "Junction",
"buildsystem": "meson",
Expand Down
78 changes: 61 additions & 17 deletions src/AppButton.js
Expand Up @@ -3,15 +3,45 @@ import GLib from "gi://GLib";
import Gio from "gi://Gio";
import Gdk from "gi://Gdk";
import { gettext as _ } from "gettext";
import Xdp from "gi://Xdp";

import { relativePath, openWithApplication } from "./util.js";
import { relativePath, openWithApplication, promiseTask } from "./util.js";
import { settings } from "./common.js";

const portal = new Xdp.Portal();

const { byteArray } = imports;

const [, content] = GLib.file_get_contents(relativePath("./AppButton.ui"));
const template = byteArray.toString(content);

export function TileButton({
label,
tooltip = label,
icon_name,
icon_size,
onClicked,
}) {
const builder = Gtk.Builder.new_from_string(template, template.length);
const button = builder.get_object("button");

button.set_tooltip_text(tooltip);
const glabel = builder.get_object("label");
glabel.label = label;
glabel.visible = false;
settings.bind("show-app-names", glabel, "visible", Gio.SettingsBindFlags.GET);

const image = builder.get_object("image");
image.set_from_icon_name(icon_name);
if (icon_size) {
image.set_pixel_size(icon_size);
}

button.connect("clicked", onClicked);

return button;
}

export default function AppButton({ appInfo, content_type, entry, window }) {
const builder = Gtk.Builder.new_from_string(template, template.length);
const button = builder.get_object("button");
Expand Down Expand Up @@ -108,21 +138,32 @@ export default function AppButton({ appInfo, content_type, entry, window }) {
return button;
}

export function ViewAllButton({ file, content_type, entry, window }) {
const builder = Gtk.Builder.new_from_string(template, template.length);
const button = builder.get_object("button");

const name = _("View All");
button.set_tooltip_text(name);
const label = builder.get_object("label");
label.label = name;
label.visible = false;
settings.bind("show-app-names", label, "visible", Gio.SettingsBindFlags.GET);
export function RevealInFolderButton({ file, entry, window }) {
async function onClicked() {
const result = await promiseTask(
portal,
"open_directory",
"open_directory_finish",
imports.gi.XdpGtk4.parent_new_gtk(window),
file.get_uri(),
Xdp.OpenUriFlags.NONE,
null,
);
if (result) {
window.close();
}
}

const image = builder.get_object("image");
image.set_from_icon_name("view-more-horizontal-symbolic");
image.set_pixel_size(48);
return TileButton({
label: _("Reveal"),
tooltip: _("Reveal in Folder"),
icon_name: "folder-symbolic",
icon_size: 48,
onClicked,
});
}

export function ViewAllButton({ file, content_type, entry, window }) {
function onResponse(appChooserDialog, response_id) {
if (response_id !== Gtk.ResponseType.OK) {
appChooserDialog.destroy();
Expand Down Expand Up @@ -173,9 +214,12 @@ export function ViewAllButton({ file, content_type, entry, window }) {
appChooserDialog.show();
}

button.connect("clicked", onClicked);

return button;
return TileButton({
label: _("View All"),
icon_name: "view-more-horizontal-symbolic",
icon_size: 48,
onClicked,
});
}

function popupActionsMenu({ popoverMenu, appInfo, location }) {
Expand Down
12 changes: 12 additions & 0 deletions src/util.js
Expand Up @@ -14,6 +14,18 @@ export function logEnum(obj, value) {
);
}

export function promiseTask(object, method, finish, ...args) {
return new Promise((resolve, reject) => {
object[method](...args, (self, asyncResult) => {
try {
resolve(object[finish](asyncResult));
} catch (err) {
reject(err);
}
});
});
}

export function relativePath(path) {
const [filename] = GLib.filename_from_uri(import.meta.url);
const dirname = GLib.path_get_dirname(filename);
Expand Down
13 changes: 12 additions & 1 deletion src/window.js
Expand Up @@ -5,7 +5,7 @@ import GLib from "gi://GLib";

import { relativePath, readResource, openWithAction } from "./util.js";
import Entry from "./Entry.js";
import AppButton, { ViewAllButton } from "./AppButton.js";
import AppButton, { ViewAllButton, RevealInFolderButton } from "./AppButton.js";
import { settings } from "./common.js";

export default function Window({ application, file }) {
Expand Down Expand Up @@ -44,6 +44,17 @@ export default function Window({ application, file }) {
window,
}),
);

if (scheme === "file" && !['inode/directory', 'application/octet-stream'].includes(content_type)) {
list.append(
RevealInFolderButton({
file,
entry,
window,
}),
);
}

const buttons = [...list];

function getButtonForKeyval(keyval) {
Expand Down
7 changes: 7 additions & 0 deletions test/util.test.js
Expand Up @@ -85,6 +85,13 @@ test("readResource", () => {
scheme: "http",
content_type: "x-scheme-handler/http",
});

const not_found_path = `/tmp/I-DO-NOT-EXIST-${Math.random()}`
assert.equal(read(not_found_path), {
resource: not_found_path,
scheme: "file",
content_type: "application/octet-stream",
});
});

test("prefixCommandLineForHost", () => {
Expand Down

0 comments on commit a19be98

Please sign in to comment.