API: add abstract open_file method, implement open_file

This commit is contained in:
Johannes Kirschbauer
2024-06-08 17:04:51 +02:00
parent 2b82a966c7
commit 16a8ff208e
7 changed files with 1123 additions and 7 deletions

View File

@@ -12,11 +12,12 @@ export type SuccessData<T extends OperationNames> = Extract<
{ status: "success" }
>;
export type ClanOperations = {
[K in OperationNames]: (str: string) => void;
};
declare global {
interface Window {
clan: {
[K in OperationNames]: (str: string) => void;
};
clan: ClanOperations;
webkit: {
messageHandlers: {
gtk: {
@@ -29,6 +30,8 @@ declare global {
};
}
}
// Make sure window.webkit is defined although the type is not correctly filled yet.
window.clan = {} as ClanOperations;
function createFunctions<K extends OperationNames>(
operationName: K
@@ -84,4 +87,11 @@ operationNames.forEach((opName) => {
pyApi[name] = createFunctions(name);
});
pyApi.open_file.receive((r) => {
const { status } = r;
if (status === "error") return console.error(r.errors);
console.log(r.data);
alert(r.data);
});
export { pyApi };

View File

@@ -34,6 +34,6 @@ if (import.meta.env.DEV) {
},
};
}
postMessage;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
render(() => <App />, root!);

View File

@@ -48,7 +48,12 @@ export const MachineListView: Component = () => {
<button
class="btn btn-ghost"
onClick={() =>
pyApi.get_directory.dispatch({ current_path: "/home/" })
pyApi.open_file.dispatch({
file_request: {
title: "Open Clan",
mode: "select_folder",
},
})
}
>
<span class="material-icons ">folder_open</span>