tauri-apps / webkit2gtk-rs

WebKit2 bindings and wrappers for Rust
MIT License
135 stars 29 forks source link

How to use webkit2gtk with glade? #65

Open idlesong opened 4 years ago

idlesong commented 4 years ago

In glade, I added a WebKitWebView, then I want to get this object in my code:

        let builder = gtk::Builder::new_from_resource("/im/idlesong/handybox/window.ui");
        let widget: gtk::ApplicationWindow = builder.get_object("window").expect("Failed to find the window object");
        let webview: WebView = builder.get_object("webview").expect("Failed to find label");

It builded success, but when run:

failed to add UI: .:136:1 Invalid object type 'WebKitWebView'

I searched the web, it seems there are only two related answers, https://stackoverflow.com/questions/51010820/glade-invalid-object-type-webkitwebview-when-loading-from-file-works-on-gla

https://prognotes.net/2019/12/make-a-gtk-web-browser-with-glade/

But one is C, one is Vala. It's too hard to convert to rust for me. Is there any samples or documents about it? Thanks.

antoyo commented 4 years ago

Not sure. Try to call get_type() before you use it.

ghost commented 3 years ago

Heyo! I'm using Glade on my GTK-rs web browser, and I've got this working. Firstly, the get_object() function gets an object by its ID/unique instance name. Have you given your WebView widget a name in Glade? Additionally, if that doesn't work for whatever reason (though, it should), you can create your own WebView instance in Rust, like this:

let web_view_builder = webkit2gtk::WebViewBuilder::new()
        .is_ephemeral(false)
        .automation_presentation_type(webkit2gtk::AutomationBrowsingContextPresentation::Tab);
let web_view = web_view_builder.build();

The webkit2gtk-rs and gtk3-rs documentation books are really useful, so take a look at those on the regular.