Ejemplo n.º 1
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 ipcMain = await IpcMain.Create();

            await ipcMain.AddListener("submitform",
                                      new IpcMainEventListener(
                                          async(result) =>
            {
                var state = result.CallbackState as object[];
                var ipcMainEvent = (IpcMainEvent)state[0];
                var parms = state[1] as object[];

                var engineCompleteHandler = new EventHandler <MediaToolkit.ConversionCompleteEventArgs>(
                    async(s, e) =>
                {
                    await ipcMainEvent.Sender.Send("completed");
                    await mainWindow.SetProgressBar(-1,
                                                    new ProgressBarOptions()
                    {
                        Mode = ProgressBarMode.None
                    });
                }
                    );

                var engineProgressHandler = new EventHandler <MediaToolkit.ConvertProgressEventArgs>(
                    async(s, e) =>
                {
                    var progress = (float)((float)e.ProcessedDuration.Ticks / e.TotalDuration.Ticks);
                    await mainWindow.SetProgressBar(progress, new ProgressBarOptions()
                    {
                        Mode = ProgressBarMode.Normal
                    });
                }
                    );

                var outputFile = System.IO.Path.Combine(parms[1].ToString(), parms[2].ToString());

                try
                {
                    await Task.Run(() =>
                                   Convert.FFMPEGWrapper.Convert(parms[0].ToString(),
                                                                 outputFile,
                                                                 engineProgressHandler,
                                                                 engineCompleteHandler)
                                   );
                }
                catch (Exception convertExc)
                {
                    var errDialog = await Dialog.Instance();
                    await errDialog.ShowErrorBox("FFMPeg engine failure", convertExc.Message);
                    await ipcMainEvent.Sender.Send("completedWithErrors");
                }
            }
                                          )

                                      );

            await ipcMain.AddListener("getMetaData",
                                      new IpcMainEventListener(
                                          async(result) =>
            {
                var state = result.CallbackState as object[];
                var ipcMainEvent = (IpcMainEvent)state[0];
                var parms = state[1] as object[];

                foreach (var parm in parms)
                {
                    System.Console.WriteLine($"\tparm: {parm}");
                }
                try
                {
                    var meta = await Convert.FFMPEGWrapper.GetMetaData(parms[0].ToString());

                    await ipcMainEvent.Sender.Send("gotMetaData", meta.AudioData, meta.VideoData, meta.Duration.ToString());
                }
                catch (Exception convertExc)
                {
                    var errDialog = await Dialog.Instance();
                    await errDialog.ShowErrorBox("FFMPeg engine failure", convertExc.Message);
                }
            }
                                          )

                                      );

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

        return(windowId);
    }
Ejemplo n.º 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 ipcMain = await IpcMain.Create();

                ipcMain.On("print-to-pdf",
                           new IpcMainEventListener
                           (
                               async(result) =>
                {
                    var state        = result.CallbackState as object[];
                    var ipcMainEvent = (IpcMainEvent)state[0];
                    var parms        = state[1] as object[];

                    var win = await BrowserWindow.FromWebContents(ipcMainEvent.Sender);
                    System.Console.WriteLine($"Asynchronous message from: {await win.GetTitle()}");
                    // foreach (var parm in parms)
                    //     System.Console.WriteLine($"\tparm: {parm}");



                    var contents = await win.GetWebContents();
                    await contents.PrintToPDF(new PrintToPDFOptions(),
                                              new ScriptObjectCallback <Error, byte[]>(
                                                  async(cr) =>
                    {
                        await console.Log("callback in Print to Pdf");
                        var PDFState = cr.CallbackState as object[];
                        var error    = PDFState[0] as Error;
                        if (error != null)
                        {
                            throw new Exception(error.Message);
                        }
                        var buffer      = PDFState[1] as byte[];
                        string filename = Path.GetTempFileName() + ".pdf";
                        File.WriteAllBytes(filename, buffer);
                        var process = Process.Start(filename);
                        await ipcMainEvent.Sender.Send("wrote-pdf", filename);
                    }
                                                  )


                                              );
                }
                           )
                           );
            }

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

        return(windowId);
    }
Ejemplo n.º 3
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);
            }


            await console.Log($"Loading: file://{__dirname}/index.html");

            // Note: Sending a synchronous message from the render process will block
            // the whole renderer process, unless you know what you are doing you should never use it.
            //
            // We will define the synchronous function via a javascript function
            // There is a limitation on synchronous messages via the CLR because of the
            // event.returnValue will not be set via the CLR.
            // If synchronous messaging is desired then a CLR implementation is what
            // you want.
            var synchronousFunction = await WebSharpJs.WebSharp.CreateJavaScriptFunction(
                @"
                        return function (data, callback) {
                            const {ipcMain} = require('electron')
                            ipcMain.on('synchronous-message', (event, arg) => {
                                console.log(arg)  // prints 'ping'
                                event.returnValue = 'synchronous pong'
                            });
                            callback(null, null);

                        }");

            await synchronousFunction(null);     // attach our synchronous message listener

            IpcMain ipcMain = await IpcMain.Create();

            ipcMain.On("asynchronous-message",
                       new IpcMainEventListener
                       (
                           async(result) =>
            {
                var state        = result.CallbackState as object[];
                var ipcMainEvent = (IpcMainEvent)state[0];
                var parms        = state[1] as object[];

                System.Console.WriteLine($"Asynchronous message from: {await ipcMainEvent.Sender.GetTitle()}");
                foreach (var parm in parms)
                {
                    System.Console.WriteLine($"\tparm: {parm}");
                }

                await ipcMainEvent.Sender.Send("asynchronous-reply", "asynchronous-pong1", "asynchronous-pong2");
            }
                       )
                       );
        }
        catch (Exception exc) { await console.Log($"extension exception:  {exc.Message}"); }

        return(windowId);
    }
Ejemplo n.º 4
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 web = await mainWindow.GetWebContents();
                // var session = await web.GetSession();

                // There are functionality differences between mac and windows.  You will need to try
                // this to see if it works for you in all scenarios.  For instance this works on a Mac,
                // at least it does on the ones that were tested, but windows always displays the save dialog.
                // await session.On("will-download",
                //     new ScriptObjectCallback<Event, DownloadItem, WebContents>(
                //         async (callbackResult) =>
                //         {
                //             var cr = new WillDownloadResult(callbackResult);
                //             await cr.DownloadItem.SetSavePath($"downloads/");
                //             var fileURL = await cr.DownloadItem.GetURL();
                //             await console.Log($"Downloading: {fileURL}");
                //             await HandleDownload(cr, fileURL);
                //         }
                //     )
                // );

                // attach a listener to download the file.
                var ipcMain = await IpcMain.Create();

                ipcMain.On("download-file",
                           new IpcMainEventListener
                           (
                               async(result) =>
                {
                    var state        = result.CallbackState as object[];
                    var ipcMainEvent = (IpcMainEvent)state[0];
                    var parms        = state[1] as object[];

                    // foreach (var parm in parms)
                    //     System.Console.WriteLine($"\tparm: {parm}");

                    // Call the DownloadFile helper method
                    await DownloadFile(parms[0].ToString());
                }
                           )
                           );
            }


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

        return(windowId);
    }