mirror of
https://github.com/tauri-apps/web-view.git
synced 2026-02-04 02:11:18 +01:00
* add todo example using vuejs * refactor by using vue component * fix wrapper class name * switch vue library in production and runtime-only lib * remove arrow function expressions that breaks to work with IEs * adD SAMple index.html for todo-vue * use bundler (parcel) * update * support addTask * update * update * use scoped style * refactor
105 lines
2.6 KiB
Rust
105 lines
2.6 KiB
Rust
//#![windows_subsystem = "windows"]
|
|
|
|
#[macro_use]
|
|
extern crate serde_derive;
|
|
extern crate serde_json;
|
|
extern crate web_view;
|
|
|
|
use web_view::*;
|
|
|
|
fn main() {
|
|
let html = format!(
|
|
r#"
|
|
<!doctype html>
|
|
<html>
|
|
<head>
|
|
{styles}
|
|
</head>
|
|
<body>
|
|
<!--[if lt IE 9]>
|
|
<div class="ie-upgrade-container">
|
|
<p class="ie-upgrade-message">Please, upgrade Internet Explorer to continue using this software.</p>
|
|
<a class="ie-upgrade-link" target="_blank" href="https://www.microsoft.com/en-us/download/internet-explorer.aspx">Upgrade</a>
|
|
</div>
|
|
<![endif]-->
|
|
<!--[if gte IE 9 | !IE ]> <!-->
|
|
<div id="app"></div>
|
|
{scripts}
|
|
<![endif]-->
|
|
</body>
|
|
</html>
|
|
"#,
|
|
styles = inline_style(include_str!("todo-vue/build/app.css")),
|
|
scripts = inline_script(include_str!("todo-vue/build/app.js"))
|
|
);
|
|
|
|
let mut webview = web_view::builder()
|
|
.title("Rust Todo App")
|
|
.content(Content::Html(html))
|
|
.size(320, 480)
|
|
.resizable(false)
|
|
.debug(true)
|
|
.user_data(vec![])
|
|
.invoke_handler(|webview, arg| {
|
|
use Cmd::*;
|
|
|
|
let tasks_len = {
|
|
let tasks = webview.user_data_mut();
|
|
|
|
match serde_json::from_str(arg).unwrap() {
|
|
Init => (),
|
|
Log { text } => println!("{}", text),
|
|
AddTask { name } => tasks.push(Task { name, done: false }),
|
|
MarkTask { index, done } => tasks[index].done = done,
|
|
ClearDoneTasks => tasks.retain(|t| !t.done),
|
|
}
|
|
|
|
tasks.len()
|
|
};
|
|
|
|
webview.set_title(&format!("Rust Todo App ({} Tasks)", tasks_len))?;
|
|
render(webview)
|
|
})
|
|
.build()
|
|
.unwrap();
|
|
|
|
webview.set_color((156, 39, 176));
|
|
|
|
let res = webview.run().unwrap();
|
|
|
|
println!("final state: {:?}", res);
|
|
}
|
|
|
|
fn render(webview: &mut WebView<Vec<Task>>) -> WVResult {
|
|
let render_tasks = {
|
|
let tasks = webview.user_data();
|
|
println!("{:#?}", tasks);
|
|
format!("app.fromRust({})", serde_json::to_string(tasks).unwrap())
|
|
};
|
|
webview.eval(&render_tasks)
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
struct Task {
|
|
name: String,
|
|
done: bool,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(tag = "cmd", rename_all = "camelCase")]
|
|
pub enum Cmd {
|
|
Init,
|
|
Log { text: String },
|
|
AddTask { name: String },
|
|
MarkTask { index: usize, done: bool },
|
|
ClearDoneTasks,
|
|
}
|
|
|
|
fn inline_style(s: &str) -> String {
|
|
format!(r#"<style type="text/css">{}</style>"#, s)
|
|
}
|
|
|
|
fn inline_script(s: &str) -> String {
|
|
format!(r#"<script type="text/javascript">{}</script>"#, s)
|
|
}
|