JinShil / BlazorWebView

A WebKitGtk WebView for running Blazor Hybrid applications.
Apache License 2.0
70 stars 11 forks source link

Child windows #9

Closed pm64 closed 5 months ago

pm64 commented 8 months ago

Is there any guidance or pattern we should follow with respect to opening and managing child windows? Is this supported?

JinShil commented 8 months ago

It depends on what kind of child window you wish to use. Do you want to open a dialog window in the browser with the <dialog /> tag? Do you want to create a message box in the browser with alert() or confirm()? Or do you want to display a native window on the platform, like a file selection dialog window?

For the windows in the browser, you can use the <dialog /> tag, alert() function, confirm() function or even create your own HTML/CSS/Javascript window.

Native windows, like a file selection dialog window, should also work fine because this is a Blazor hybrid project that utilizes the local .NET framework on the device that the program is running on, as opposed to Blazer Server that utilizes the .NET framework on a web server, or Blazor WASM that utilizes the .NET framework loaded into a sandboxed browser.

So, for example, on Windows, you should be able to add a reference to any native Windows .NET library and utilize it. You can also P/Invoke into any native unmanaged libraries should you need to. However, any native libraries will not be portable across platforms, so you'll probably have to add an implementation for each platform.

I think Maui might make that more portable, but I don't have any experience with it.

pm64 commented 8 months ago

Hey @JinShil, thanks for your reply. The child windows I had in mind are in a 3rd category -- proper child windows that can move beyond the bounds of the main application window, which themselves are displaying Blazor content.

JinShil commented 8 months ago

This works, but it may not be what you were thinking:

@using Microsoft.Extensions.DependencyInjection

<button @onclick="OpenWindow">Test</button>

@code {
    void OpenWindow()
    {
        var window = Gtk.Window.New();
        window.SetDefaultSize(800, 600);

        // Add the BlazorWebView
        var serviceProvider = new ServiceCollection()
            .AddBlazorWebViewOptions(new BlazorWebViewOptions()
            {
                RootComponent = typeof(WebKitGtk.Test.App),
                HostPath = "wwwroot/index.html"
            })
            .BuildServiceProvider();
        var webView = new BlazorWebView(serviceProvider);
        window.SetChild(webView);
        window.Show();
    }
}

If you wanted to open a window with javascript's window.open(), I believe you will need to tap into the WebView.OnCreate event and create the window there.

JinShil commented 5 months ago

Closing as I'm assuming there is nothing more to discuss. Reopen if there is anything further.