Skip to content

Commit

Permalink
refactor: add rust ffi wrapper for async js
Browse files Browse the repository at this point in the history
  • Loading branch information
ikey4u committed Feb 19, 2022
1 parent 973291e commit d1f9c53
Show file tree
Hide file tree
Showing 8 changed files with 4,641 additions and 9,966 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ To build wikit desktop
cd desktop
npm i
npm run build
cargo install tauri-cli --version "^1.0.0-beta"

# refer Developement section to see the installation of cargo-tauri
cargo tauri build

You can find the generated files in `target/release`.
Expand Down
14,486 changes: 4,573 additions & 9,913 deletions desktop/package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"@smui/dialog": "^6.0.0-beta.2",
"@smui/list": "^6.0.0-beta.2",
"@smui/radio": "^6.0.0-beta.2",
"@tauri-apps/api": "^1.0.0-beta.8",
"@tauri-apps/cli": "^1.0.0-beta.10",
"@tauri-apps/api": "^1.0.0-rc.1",
"@tauri-apps/cli": "^1.0.0-rc.5",
"rollup": "^2.3.4",
"rollup-plugin-css-only": "^3.1.0",
"rollup-plugin-livereload": "^2.0.0",
Expand All @@ -25,6 +25,7 @@
"svelte": "^3.0.0"
},
"dependencies": {
"latest": "^0.2.0",
"sirv-cli": "^1.0.0"
}
}
3 changes: 2 additions & 1 deletion desktop/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"distDir": "../public",
"devPath": "http://localhost:5000",
"beforeDevCommand": "",
"beforeBuildCommand": ""
"beforeBuildCommand": "",
"withGlobalTauri": true
},
"tauri": {
"bundle": {
Expand Down
53 changes: 24 additions & 29 deletions desktop/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script>
import { onMount, onDestroy } from 'svelte';
import { invoke, convertFileSrc } from '@tauri-apps/api/tauri';
import { listen, emit } from "@tauri-apps/api/event";
import Settings from './Settings.svelte';
import { dictSettings } from './store.js';
import ffi from './ffi.js';
let timer;
let input;
Expand All @@ -15,13 +15,13 @@
let content = default_content;
let isSettingsOpened = false;
let unlisten;
onMount(() => {
invoke('get_dict_list').then((r) => {
$dictSettings.dict.all = r;
if (r.length > 0) {
$dictSettings.dict.selected = [r[0]];
}
});
onMount(async () => {
let r = await ffi.get_dict_list();
$dictSettings.dict.all = r;
if (r.length > 0) {
$dictSettings.dict.selected = [r[0]];
}
unlisten = listen("rust-event", (e) => {
console.log("got rust event: " + e);
});
Expand All @@ -37,7 +37,7 @@
emit("js-event", "this is the payload string");
}
function lookup(input) {
async function lookup(input) {
if (!input || input.length <= 0 || input.trim().length <= 0) {
content = default_content;
return;
Expand All @@ -48,27 +48,22 @@
return;
}
input = input.trim();
invoke('lookup', {
dictid: $dictSettings.dict.selected[0],
word: input,
}).then((resp) => {
let meanings = resp["words"];
let meaning = meanings[input]
if (!meaning) {
let possible_words = [];
for (let key in meanings) {
possible_words.push(key);
}
if (possible_words.length > 0) {
meaning = `<p> not found <b>${input}</b>, would you mean <b>${possible_words}</b>? </p>`;
} else {
meaning = `<p> not found <b>${input}</b> and related words </p>`
}
input = input.trim().toLowerCase();
let resp = await ffi.lookup($dictSettings.dict.selected[0], input);
let meanings = resp["words"];
let meaning = meanings[input]
if (!meaning) {
let possible_words = [];
for (let key in meanings) {
possible_words.push(key);
}
updateContent(meaning, resp["script"], resp["style"]);
});
if (possible_words.length > 0) {
meaning = `<p> not found <b>${input}</b>, would you mean <b>${possible_words}</b>? </p>`;
} else {
meaning = `<p> not found <b>${input}</b> and related words </p>`
}
}
updateContent(meaning, resp["script"], resp["style"]);
}
function openSettings() {
Expand Down
46 changes: 28 additions & 18 deletions desktop/src/Settings.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
<script lang="ts">
import Dialog, { Title, Content, Actions, InitialFocus } from '@smui/dialog';
import Button, { Label } from '@smui/button';
import List, { Item, Graphic, Text } from '@smui/list';
import Radio from '@smui/radio';
import { dictSettings } from './store.js';
import ffi from './ffi.js';
let selection = '';
let open = false;
function closeHandler(e) {
if (e.detail.action === 'accept') {
if (selection.trim().length > 0) {
$dictSettings.dict.selected = [selection];
}
}
}
</script>

<Dialog
bind:open
selection
Expand Down Expand Up @@ -32,26 +52,16 @@
</Actions>
</Dialog>

<div class="button options-button" on:click={ () => (open = true) }>+</div>

<script lang="ts">
import Dialog, { Title, Content, Actions, InitialFocus } from '@smui/dialog';
import Button, { Label } from '@smui/button';
import List, { Item, Graphic, Text } from '@smui/list';
import Radio from '@smui/radio';
import { dictSettings } from './store.js';
let selection = '';
let open = false;
function closeHandler(e) {
if (e.detail.action === 'accept') {
if (selection.trim().length > 0) {
$dictSettings.dict.selected = [selection];
}
<div class="button options-button" on:click={
async () => {
let r = await ffi.get_dict_list();
$dictSettings.dict.all = r;
if (r.length > 0) {
$dictSettings.dict.selected = [r[0]];
}
open = true
}
</script>
}>+</div>

<style>
.button {
Expand Down
10 changes: 10 additions & 0 deletions desktop/src/ffi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { invoke, convertFileSrc } from '@tauri-apps/api/tauri';

export default {
get_dict_list: async function() {
return await invoke('get_dict_list')
},
lookup: async function(dictid, word) {
return await invoke('lookup', { dictid, word });
},
}
1 change: 0 additions & 1 deletion desktop/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ export const dictSettings = writable({
'selected': [],
},
});

0 comments on commit d1f9c53

Please sign in to comment.