cross platform GUIを rustで作るにあたり, web-viewを使ってみた
ちなみに M1 MacBookではビルドに失敗するので変更
webview-sys-0.6.2/webview_cocoa.c
内のobjc_msgSend(oge,SEL,obj1,obj2)
を全て((void(*)(id, SEL,id,id))objc_msgSend)(oge,SEL,obj1,obj2)
のようにキャストして書き換えると動作した- 参考: https://shu223.hatenablog.com/entry/20131206/1386322399
- float, CGRect, CGSizeはキャストの方を変更
- Windows版は VS Studio 2019 C++をインストールするのにかなり時間がかかった
Cargo.toml
のdependencies
セクションに追加
[dependencies] web-view = { version = "0.7" }
main.rs
const HTML: &str = r#"<!DOCTYPE html> <html> <body> <div id="display">0</div> <button onclick="external.invoke('count')">count</button> <script> function update(count) { document.getElementById('display').innerHTML = count; } </script> </body> </html>"#; struct UserData { count: u32, } fn main() { web_view::builder() .title("Hello world!") .content(web_view::Content::Html(HTML)) .size(320, 240) .user_data( UserData { count: 0 } ) .invoke_handler(|webview, arg| { if arg == "count" { webview.user_data_mut().count += 1; webview.eval(&format!("update({})", webview.user_data().count)) } else { Ok(()) } }) .run() .unwrap(); }