예제 #1
0
    async Task <int> CreateWindow(string __dirname)
    {
        // Create the browser window.
        mainWindow = await BrowserWindow.Create(
            new BrowserWindowOptions()
        {
            Width    = 800,
            Height   = 600,
            IconPath = $"{__dirname}/assets/icons/appicon.ico"
        }

            );

        // and load the index.html of the app.
        await mainWindow.LoadURL($"file://{__dirname}/index.html");

        // Open the DevTools
        //await mainWindow.GetWebContents().ContinueWith(
        //        (t) => { t.Result?.OpenDevTools(DevToolsMode.Bottom); }
        //);

        // Did finish load
        await mainWindow.GetWebContents().ContinueWith(
            (t) =>
        {
            var webcontents = t.Result;
            webcontents.On("did-finish-load",
                           new ScriptObjectCallback(
                               async(ar) =>
            {
                try
                {
                    var ready = await InitializeEngine();
                    if (!ready)
                    {
                        var errDialog = await Dialog.Instance();
                        await errDialog.ShowErrorBox("FFMPeg engine failure", "FFMPeg could not be initialized");
                    }
                }
                catch (Exception initExc)
                {
                    var errDialog = await Dialog.Instance();
                    await errDialog.ShowErrorBox("FFMPeg engine failure", initExc.Message);
                }
            }
                               )
                           );
        }
            );

        return(await mainWindow.GetId());
    }
예제 #2
0
    /// <summary>
    /// Default entry into managed code.
    /// </summary>
    /// <param name="__dirname">The directory name of the current module. This the same as the path.dirname() of the __filename.</param>
    /// <returns></returns>
    public async Task <object> Invoke(string __dirname)
    {
        if (console == null)
        {
            console = await WebSharpJs.NodeJS.Console.Instance();
        }

        try
        {
            app = await App.Instance();

            // We use app.IsReady instead of listening for the 'ready'event.
            // By the time we get here from the main.js module the 'ready' event has
            // already fired.
            if (await app.IsReady())
            {
                windowId = await CreateWindow(__dirname);

                var webContents = await mainWindow.GetWebContents();

                // Capture all external link clicks and open them in the
                // default browser instead of inline in our application.
                await webContents.On("will-navigate",
                                     new ScriptObjectCallback <Event, string>(
                                         async(navResult) =>
                {
                    var result = navResult.CallbackState as object[];
                    var ev = result[0] as Event;
                    var externalURL = result[1].ToString();
                    ev.PreventDefault();
                    System.Diagnostics.Process.Start(externalURL);
                }
                                         )

                                     );
            }


            await console.Log($"Loading: file://{__dirname}/index.html");
        }
        catch (Exception exc) { await console.Log($"extension exception:  {exc.Message}"); }

        return(windowId);
    }
예제 #3
0
    // Attaches a will-download listener to the session and initiates the download
    async Task DownloadFile(string fileURL)
    {
        // Get a reference to a Session object in this case
        // from the mainWindow.
        var webContents = await mainWindow.GetWebContents();

        var session = await webContents.GetSession();

        // Call the DownloadURL() method from the WebContents
        await webContents.DownloadURL(fileURL);

        await session.Once("will-download",
                           new ScriptObjectCallback <Event, DownloadItem, WebContents>(
                               async(callbackResult) =>
        {
            await HandleDownload(new WillDownloadResult(callbackResult), fileURL);
        }
                               )
                           );
    }
예제 #4
0
    /// <summary>
    /// Default entry into managed code.
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task <object> Invoke(string __dirname)
    {
        if (console == null)
        {
            console = await WebSharpJs.NodeJS.Console.Instance();
        }

        try
        {
            // Create the browser window.
            mainWindow = await BrowserWindow.Create(new BrowserWindowOptions()
            {
                Width = 600, Height = 400
            });

            // and load the index.html of the app.
            await mainWindow.LoadURL($"file://{__dirname}/index.html");

            // Open the DevTools
            await mainWindow.GetWebContents().ContinueWith(
                (t) => { t.Result?.OpenDevTools(DevToolsMode.Bottom); }
                );

            // Emitted when the window is closed.
            await mainWindow.On("closed", async (evt) =>
            {
                await console.Log("Received closed event");
                System.Console.WriteLine("Received closed event");
                mainWindow = null;
                return(null);
            });

            await console.Log($"Loading: file://{__dirname}/index.html");
        }
        catch (Exception exc) { await console.Log($"extension exception:  {exc.Message}"); }

        return(await mainWindow.GetId());
    }
예제 #5
0
    /// <summary>
    /// Default entry into managed code.
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task <object> Invoke(string __dirname)
    {
        if (console == null)
        {
            console = await WebSharpJs.NodeJS.Console.Instance();
        }

        try
        {
            // Create the browser window.
            mainWindow = await BrowserWindow.Create(new BrowserWindowOptions()
            {
                Width = 600, Height = 400
            });

            // and load the index.html of the app.
            await mainWindow.LoadURL($"file://{__dirname}/index.html");

            await mainWindow.GetWebContents().ContinueWith(
                (t) => {
                var webContents = t.Result;

                // Subscribe to the "context-menu" event on the WebContents
                webContents?.On("context-menu", async(cmevt) =>
                {
                    // if we have not created the menu then do it now
                    if (menu == null)
                    {
                        menu = await Menu.Create();
                        await menu.Append(await MenuItem.Create(new MenuItemOptions()
                        {
                            Label = "Hello", Click = MenuItemClicked
                        }));
                        await menu.Append(await MenuItem.Create(new MenuItemOptions()
                        {
                            Type = MenuItemType.Separator
                        }));
                        await menu.Append(await MenuItem.Create(
                                              new MenuItemOptions()
                        {
                            Label          = "View",
                            SubMenuOptions = new MenuItemOptions[]
                            {
                                new MenuItemOptions()
                                {
                                    Role = MenuItemRole.Reload
                                },
                                new MenuItemOptions()
                                {
                                    Role = MenuItemRole.ToggleDevTools
                                },
                                new MenuItemOptions()
                                {
                                    Type = MenuItemType.Separator
                                },
                                new MenuItemOptions()
                                {
                                    Role = MenuItemRole.ResetZoom
                                },
                                new MenuItemOptions()
                                {
                                    Role = MenuItemRole.ZoomIn
                                },
                                new MenuItemOptions()
                                {
                                    Role = MenuItemRole.ZoomOut
                                },
                                new MenuItemOptions()
                                {
                                    Type = MenuItemType.Separator
                                },
                                new MenuItemOptions()
                                {
                                    Role = MenuItemRole.ToggleFullScreen
                                },
                            }
                        }
                                              ));
                        await menu.Append(await MenuItem.Create(new MenuItemOptions()
                        {
                            Type = MenuItemType.Separator
                        }));
                        await menu.Append(await MenuItem.Create(new MenuItemOptions()
                        {
                            Label = "WebSharp", Type = MenuItemType.Checkbox, Checked = true, Click = MenuItemClicked
                        }));
                    }

                    // popup our menu here
                    menu.Popup();
                    return(null);
                });

                // Open the DevTools
                webContents?.OpenDevTools(DevToolsMode.Bottom);
            }
                );

            // Emitted when the window is closed.
            await mainWindow.On("closed", async (evt) =>
            {
                await console.Log("Received closed event");
                System.Console.WriteLine("Received closed event");
                mainWindow = null;
                return(null);
            });

            await console.Log($"Loading: file://{__dirname}/index.html");
        }
        catch (Exception exc) { await console.Log($"extension exception:  {exc.Message}"); }

        return(await mainWindow.GetId());
    }
예제 #6
0
    /// <summary>
    /// Default entry into managed code.
    /// </summary>
    /// <param name="__dirname">The directory name of the current module. This the same as the path.dirname() of the __filename.</param>
    /// <returns></returns>
    public async Task <object> Invoke(string __dirname)
    {
        if (console == null)
        {
            console = await WebSharpJs.NodeJS.Console.Instance();
        }

        try
        {
            app = await App.Instance();

            // We use app.IsReady instead of listening for the 'ready'event.
            // By the time we get here from the main.js module the 'ready' event has
            // already fired.
            if (await app.IsReady())
            {
                windowId = await CreateWindow(__dirname);

                // Get our session
                try
                {
                    session = await Session.FromPartition("partition:nameofpartition");
                }
                catch (Exception sexc)
                {
                    await console.Log($"logging: {sexc.Message}");

                    var web = await mainWindow.GetWebContents();

                    session = await web.GetSession();
                }
                if (session != null)
                {
                    await session.SetPermissionRequestHandler(
                        new ScriptObjectCallback <WebContents, string, Func <object, Task <object> > >(

                            async(callbackResult) =>
                    {
                        var permissionResult = new PermissionRequestResult(callbackResult);

                        var url = await permissionResult.WebContents.GetURL();
                        await console.Log($"Received permission request from {url} for access to \"{permissionResult.Permission}\".");
                        if (permissionResult.Permission == Permission.PointerLock)
                        {
                            permissionResult.Callback(await GrantAccess(permissionResult.Permission));
                        }
                        else
                        {
                            permissionResult.Callback(true);
                        }
                    }
                            )
                        );
                }
            }


            await console.Log($"Loading: file://{__dirname}/index.html");
        }
        catch (Exception exc) { await console.Log($"extension exception:  {exc.Message}"); }

        return(windowId);
    }
예제 #7
0
    async Task <int> CreateWindow(string __dirname)
    {
        // Create the browser window.
        mainWindow = await BrowserWindow.Create();

        // Get our session
        try
        {
            session = await Session.FromPartition("partition:nameofpartition");
        }
        catch (Exception sexc)
        {
            await console.Log($"logging: {sexc.Message}");

            var web = await mainWindow.GetWebContents();

            //.ContinueWith(
            //    async (t) =>
            //    {
            //        session = await t.Result?.GetSession();

            //    }

            //    );

            session = await web.GetSession();
        }
        if (session != null)
        {
            await console.Log(session);

            await session.SetPermissionRequestHandler(
                new ScriptObjectCallback <WebContents, string, Func <object, Task <object> > >(

                    async(callbackResult) =>
            {
                var permissionResult = new PermissionRequestResult(callbackResult);
                var url = await permissionResult.WebContents.GetURL();
                await console.Log($"Received permission request from {url} for access to \"{permissionResult.Permission}\".");
                if (url == "https://html5demos.com/geo/" && permissionResult.Permission == "geolocation")
                {
                    permissionResult.Callback(await GrantAccess(url, permissionResult.Permission));
                }
                else
                {
                    permissionResult.Callback(true);
                }
            }
                    )
                );
        }

        // and load the index.html of the app.
        await mainWindow.LoadURL("https://html5demos.com/geo/");

        // Open the DevTools
        //await mainWindow.GetWebContents().ContinueWith(
        //        (t) => { t.Result?.OpenDevTools(DevToolsMode.Bottom); }
        //);

        return(await mainWindow.GetId());
    }