initial commit
This commit is contained in:
commit
c3e5b84282
54 changed files with 2009 additions and 0 deletions
79
examples/api/src/main.rs
Normal file
79
examples/api/src/main.rs
Normal file
|
@ -0,0 +1,79 @@
|
|||
mod views;
|
||||
|
||||
use sycamore::prelude::*;
|
||||
#[cfg(not(feature = "ssg"))]
|
||||
use sycamore_router::{Router, HistoryIntegration};
|
||||
|
||||
#[component]
|
||||
fn Header<G: Html>(cx: Scope) -> View<G> {
|
||||
view! { cx,
|
||||
header(style="display: flex; gap: 1em; margin-bottom: 1em;") {
|
||||
a(href="/") {
|
||||
"Welcome"
|
||||
}
|
||||
a(href="/app") {
|
||||
"App"
|
||||
}
|
||||
a(href="/clipboard") {
|
||||
"Clipboard"
|
||||
}
|
||||
a(href="/communication") {
|
||||
"Communication"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(not(debug_assertions), not(feature = "ssg")))]
|
||||
fn main() {
|
||||
wasm_logger::init(wasm_logger::Config::default());
|
||||
|
||||
sycamore::hydrate(|cx| view! { cx,
|
||||
Header
|
||||
Router(
|
||||
integration=HistoryIntegration::new(),
|
||||
view=views::switch
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(all(debug_assertions, not(feature = "ssg")))]
|
||||
fn main() {
|
||||
use sycamore::view;
|
||||
|
||||
wasm_logger::init(wasm_logger::Config::default());
|
||||
|
||||
sycamore::render(|cx| view! { cx,
|
||||
Header
|
||||
Router(
|
||||
integration=HistoryIntegration::new(),
|
||||
view=views::switch
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(feature = "ssg")]
|
||||
fn main() {
|
||||
use sycamore_router::StaticRouter;
|
||||
|
||||
let out_dir = std::env::args().nth(1).unwrap();
|
||||
|
||||
println!("out_dir {}", out_dir);
|
||||
|
||||
let template = std::fs::read_to_string(format!("{}/index.html", out_dir)).unwrap();
|
||||
|
||||
let html = sycamore::render_to_string(|cx| view! { cx,
|
||||
Header
|
||||
StaticRouter(
|
||||
route=route.clone(),
|
||||
view=views::switch
|
||||
)
|
||||
});
|
||||
|
||||
let html = template.replace("<!--app-html-->\n", &html);
|
||||
|
||||
let path = format!("{}/index.html", out_dir);
|
||||
|
||||
println!("Writing html to file \"{}\"", path);
|
||||
std::fs::write(path, html).unwrap();
|
||||
}
|
78
examples/api/src/views/app.rs
Normal file
78
examples/api/src/views/app.rs
Normal file
|
@ -0,0 +1,78 @@
|
|||
use gloo_timers::callback::Timeout;
|
||||
use sycamore::prelude::*;
|
||||
use tauri_api::app;
|
||||
|
||||
#[component]
|
||||
pub fn App<G: Html>(cx: Scope) -> View<G> {
|
||||
let show_app = |_| {
|
||||
sycamore::futures::spawn_local(async move {
|
||||
let res = app::hide().await;
|
||||
|
||||
log::debug!("app hide res {:?}", res);
|
||||
|
||||
let timeout = Timeout::new(2_000, move || {
|
||||
sycamore::futures::spawn_local(async move {
|
||||
let res = app::show().await;
|
||||
|
||||
log::debug!("app show res {:?}", res);
|
||||
});
|
||||
});
|
||||
|
||||
timeout.forget();
|
||||
});
|
||||
};
|
||||
|
||||
let hide_app = |_| {
|
||||
sycamore::futures::spawn_local(async move {
|
||||
let res = app::hide().await;
|
||||
|
||||
log::debug!("app hide res {:?}", res);
|
||||
});
|
||||
};
|
||||
|
||||
let get_name = |_| {
|
||||
sycamore::futures::spawn_local(async move {
|
||||
let res = app::get_name().await;
|
||||
|
||||
log::debug!("app name {:?}", res);
|
||||
});
|
||||
};
|
||||
|
||||
let get_version = |_| {
|
||||
sycamore::futures::spawn_local(async move {
|
||||
let res = app::get_version().await;
|
||||
|
||||
log::debug!("app version {:?}", res);
|
||||
});
|
||||
};
|
||||
|
||||
let get_tauri_version = |_| {
|
||||
sycamore::futures::spawn_local(async move {
|
||||
let res = app::get_tauri_version().await;
|
||||
|
||||
log::debug!("tauri version {:?}", res);
|
||||
});
|
||||
};
|
||||
|
||||
view! { cx,
|
||||
div {
|
||||
button(class="btn",id="get_name",on:click=get_name) {
|
||||
"Get App Name"
|
||||
}
|
||||
button(class="btn",id="get_version",on:click=get_version) {
|
||||
"Get App Version"
|
||||
}
|
||||
button(class="btn",id="get_tauri_version",on:click=get_tauri_version) {
|
||||
"Get Tauri Version"
|
||||
}
|
||||
}
|
||||
div {
|
||||
button(class="btn",id="show",title="Hides and shows the app after 2 seconds",on:click=show_app) {
|
||||
"Show"
|
||||
}
|
||||
button(class="btn",id="hide",on:click=hide_app) {
|
||||
"Hide"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
42
examples/api/src/views/clipboard.rs
Normal file
42
examples/api/src/views/clipboard.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use sycamore::prelude::*;
|
||||
use tauri_api::clipboard::{read_text, write_text};
|
||||
|
||||
#[component]
|
||||
pub fn Clipboard<G: Html>(cx: Scope) -> View<G> {
|
||||
let text = create_signal(cx, "clipboard message".to_string());
|
||||
|
||||
let write = move |_| {
|
||||
sycamore::futures::spawn_local_scoped(cx, async move {
|
||||
write_text(&text.get()).await
|
||||
// .then(() => {
|
||||
// onMessage('Wrote to the clipboard')
|
||||
// })
|
||||
// .catch(onMessage)
|
||||
});
|
||||
};
|
||||
|
||||
let read = |_| {
|
||||
sycamore::futures::spawn_local(async move {
|
||||
let text = read_text().await;
|
||||
|
||||
log::info!("Read text from clipboard {:?}", text);
|
||||
// readText()
|
||||
// .then((contents) => {
|
||||
// onMessage(`Clipboard contents: ${contents}`)
|
||||
// })
|
||||
// .catch(onMessage)
|
||||
});
|
||||
};
|
||||
|
||||
view! { cx,
|
||||
div(class="flex gap-1") {
|
||||
input(class="grow input",placeholder="Text to write to the clipboard",bind:value=text)
|
||||
button(class="btn",type="button",on:click=write) {
|
||||
"Write"
|
||||
}
|
||||
button(class="btn",type="button",on:click=read) {
|
||||
"Read"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
90
examples/api/src/views/communication.rs
Normal file
90
examples/api/src/views/communication.rs
Normal file
|
@ -0,0 +1,90 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use sycamore::prelude::*;
|
||||
use tauri_api::event::{emit, listen};
|
||||
use tauri_api::tauri::invoke;
|
||||
use shared::RequestBody;
|
||||
|
||||
#[component]
|
||||
pub fn Communication<'a, G: Html>(cx: Scope<'a>) -> View<G> {
|
||||
let unlisten = create_signal::<Option<Box<&dyn FnOnce()>>>(cx, None);
|
||||
|
||||
// on_mount(cx, move || {
|
||||
|
||||
// sycamore::futures::spawn_local_scoped(cx, async move {
|
||||
// let unlisten_raw = listen::<Reply>("rust-event", &|reply| log::debug!("got reply {:?}", reply)).await;
|
||||
|
||||
// unlisten.set(Some(Box::new(&unlisten_raw)));
|
||||
// });
|
||||
// });
|
||||
|
||||
// on_cleanup(cx, || {
|
||||
// if let Some(unlisten) = unlisten .take().as_deref() {
|
||||
// (unlisten)()
|
||||
// }
|
||||
// });
|
||||
|
||||
let log = |_| {
|
||||
#[derive(Serialize)]
|
||||
struct Payload<'a> {
|
||||
event: &'a str,
|
||||
payload: &'a str,
|
||||
}
|
||||
|
||||
sycamore::futures::spawn_local(async move {
|
||||
let res = invoke::<_, ()>(
|
||||
"log_operation",
|
||||
&Payload {
|
||||
event: "tauri-click",
|
||||
payload: "this payload is optional because we used Option in Rust",
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
log::debug!("Emitted event, response {:?}", res);
|
||||
});
|
||||
};
|
||||
|
||||
let perform_request = |_| {
|
||||
sycamore::futures::spawn_local(async move {
|
||||
#[derive(Serialize)]
|
||||
struct Payload<'a> {
|
||||
endpoint: &'a str,
|
||||
body: RequestBody<'a>
|
||||
}
|
||||
|
||||
let res = invoke::<_, String>(
|
||||
"perform_request",
|
||||
&Payload {
|
||||
endpoint: "dummy endpoint arg",
|
||||
body: RequestBody {
|
||||
id: 5,
|
||||
name: "test",
|
||||
},
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
log::debug!("Got reply {:?}", res);
|
||||
});
|
||||
};
|
||||
|
||||
let emit_event = |_| {
|
||||
sycamore::futures::spawn_local(async move {
|
||||
emit("js-event", &"this is the payload string").await;
|
||||
});
|
||||
};
|
||||
|
||||
view! { cx,
|
||||
div {
|
||||
button(class="btn",id="log",on:click=log) {
|
||||
"Call Log API"
|
||||
}
|
||||
button(class="btn",mid="request",on:click=perform_request) {
|
||||
"Call Request (async) API"
|
||||
}
|
||||
button(class="btn",id="event",on:click=emit_event) {
|
||||
"Send event to Rust"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
29
examples/api/src/views/mod.rs
Normal file
29
examples/api/src/views/mod.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
mod app;
|
||||
mod clipboard;
|
||||
mod communication;
|
||||
mod welcome;
|
||||
|
||||
use sycamore::view::View;
|
||||
use sycamore_router::Route;
|
||||
use sycamore::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone, Route)]
|
||||
pub enum Page {
|
||||
#[to("/app")]
|
||||
App,
|
||||
#[to("/clipboard")]
|
||||
Clipboard,
|
||||
#[to("/communication")]
|
||||
Communication,
|
||||
#[not_found]
|
||||
NotFound
|
||||
}
|
||||
|
||||
pub fn switch<G: Html>(cx: Scope, route: &ReadSignal<Page>) -> View<G> {
|
||||
match route.get().as_ref() {
|
||||
Page::App => app::App(cx),
|
||||
Page::Clipboard => clipboard::Clipboard(cx),
|
||||
Page::Communication => communication::Communication(cx),
|
||||
Page::NotFound => welcome::Welcome(cx)
|
||||
}
|
||||
}
|
10
examples/api/src/views/welcome.rs
Normal file
10
examples/api/src/views/welcome.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
use sycamore::prelude::*;
|
||||
|
||||
#[component]
|
||||
pub fn Welcome<G: Html>(cx: Scope) -> View<G> {
|
||||
view! { cx,
|
||||
h1 {
|
||||
"Welcome"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue