-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: data flow between renderer and main process
- Loading branch information
Showing
4 changed files
with
70 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,27 @@ | ||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] | ||
|
||
use serde_json::json; | ||
|
||
#[tauri::command] | ||
fn set_query(query: &str) -> Vec<serde_json::Value> { | ||
let mut result = vec![]; | ||
for i in 0..=10 { | ||
result.push(json!({ | ||
"title": format!("{} {}", query, i), | ||
"on_click": format!("(send {})", i), | ||
})) | ||
} | ||
result | ||
} | ||
|
||
#[tauri::command] | ||
fn eval(input: &str) -> String { | ||
format!("eval({}) = TODO", input) | ||
fn dispatch(form: &str) { | ||
println!("received {}", form); | ||
} | ||
|
||
fn main() { | ||
tauri::Builder::default() | ||
.invoke_handler(tauri::generate_handler![eval]) | ||
.invoke_handler(tauri::generate_handler![set_query, dispatch]) | ||
.run(tauri::generate_context!()) | ||
.expect("error while running tauri application"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
const { invoke } = window.__TAURI__.tauri; | ||
|
||
let inputEl; | ||
let outputEl; | ||
let outputListEl; | ||
|
||
async function runEval(input) { | ||
outputEl.textContent = await invoke("eval", { input: input }); | ||
async function dispatch(form) { | ||
await invoke("dispatch", { form: form }); | ||
} | ||
|
||
async function setQuery(query) { | ||
const items = await invoke("set_query", { query: query }); | ||
|
||
outputListEl.replaceChildren(); | ||
for (const item of items) { | ||
const itemButton = document.createElement('button') | ||
itemButton.classList = ['output-item']; | ||
itemButton.innerText = item['title']; | ||
itemButton.addEventListener('click', (e) => { | ||
dispatch(item['on_click']); | ||
}); | ||
outputListEl.appendChild(itemButton); | ||
} | ||
} | ||
|
||
window.addEventListener("DOMContentLoaded", () => { | ||
inputEl = document.querySelector("#input-field"); | ||
outputEl = document.querySelector("#output"); | ||
document.querySelector("#input-form").addEventListener("submit", (e) => { | ||
outputListEl = document.querySelector("#output-list"); | ||
inputEl.addEventListener("input", (e) => { | ||
e.preventDefault(); | ||
runEval(inputEl.value); | ||
setQuery(inputEl.value); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters