Build App For Macos Electron



Electron-Firebase provides a quickstart framework for building cloud-connected applications on Windows, macOS, and Linux. By integrating Electron and Firebase, just a few configuration settings. Nov 05, 2019 And Electron apps include Chromium as a runtime library. Electron has a number of drawbacks – builds tend to be large and memory-hungry, for instance – but the advantage of being able to use familiar web technology to create a single code base that can generate builds for macOS, Linux, and Windows outweighs the downsides in many cases. The goal is to generate a.exe file for Windows, a.app for macOS, a.deb for Debian-based Linux distributions, and so on. Electron Packager is the official Electron tool to help us convert our source code to a bundle specific for macOS, Linux, or Windows. Design and Build electron applications that matters. Package and distribute your electron application for windows, linux and macOS. Secure an electron desktop application developer position. Use the electron documentation for any desktop app feature that isn’t covered in this course.

This article provides a walkthrough for developing and deploying an application with Electron.NET.

Topics covered include:

Macos
  • Modifying the default ASP. NET Core application to use Electron.NET
  • Using Visual Studio Code to debug ASP. NET Razor pages
  • Implementing native UI elements such as message boxes, system file dialogs, and custom application menus
  • Adding third-party MVC controls
  • Building deployment media for other platforms

Recently, I had the honor of presenting this topic at the NDC Oslo online event. Here is my presentation in blog form, with a source code link at the end of the article.

What is Electron?

Electron is a framework that supports the development of desktop applications using web technologies such as the Chromium rendering engine and the Node.js runtime. Supported operating systems include Windows, macOS, and Linux. Chances are you have used at least one of these applications, which were all developed using Electron:

  • Visual Studio Code
  • Slack
  • Discord
  • Skype
  • GitHub Desktop
  • Twitch

Electron leverages familiar standards such as HTML, CSS, and JavaScript. But what if you are a .NET developer accustomed to working in C#? That's where Electron.NET comes in.

What is Electron.NET?

Electron.NET is a wrapper around a 'normal' Electron application with an embedded ASP. NET Core application. It is an open-source project that allows .NET developers to invoke native Electron APIs using C#. Electron.NET consists of two components:

  1. A NuGet package that adds Electron APIs to an ASP. NET Core project
  2. A .NET Core command-line extension that builds and launches applications for Windows, macOS, and Linux platforms.

Electron.NET requires prior installation of the following software:

I relied on Electron.NET to develop the C1DataEngine Workbench, a cross-platform tool that supports the creation and visualization of data analysis workspaces managed by the ComponentOne DataEngine library for .NET Standard. I initially planned to make this a standard Electron application that communicated with the library via shell commands that called out to a .NET Core global tool. But once I discovered Electron.NET, I was able to eliminate the shell commands and call the library directly.

See the C1DataEngine Workbench Application in Action - included with ComponentOne Service Components

Download the latest version of ComponentOne Studio Enterprise

Download Now!

Create an ASP. NET Core Web Application

For this exercise, I'm using Visual Studio Code running on a Mac. First, open a terminal window and run the following commands to create a new project called Processes.

Build App For Macos Electron

When prompted by Visual Studio Code, say Yes to load the required assets for the project. Press F5 to build and run the application, opening a browser on the default ASP. NET Core welcome page, hosted on localhost:5001. Close the page, return to VS Code and stop debugging.

Electronize It!

Now let's turn our boilerplate ASP. NET Core project into an Electron application. This step involves adding a NuGet package to the project file, inserting some initialization code, and installing a command-line tool to perform builds. First, open the file Processes.csproj and insert a package reference for the Electron.NET API hosted on nuget.org:

Save the file, then restore packages when prompted to do so by VS Code. This action gives you immediate access to Intellisense for subsequent modifications to the code.

Next, edit Program.cs and insert a using statement for the newly added package:

Locate the static method CreateHostBuilder and insert the following two lines before the call to UseStartup:

The first line is necessary. The second is convenient during development, as it allows detailed error messages to be displayed.

Edit Startup.cs and insert the following using statements:

Locate the Configure method and add the following lines to the end of its body:

Finally, add the following method to the Startup class to create the main Electron window:

Since our application consists of a single window, we handle the OnClosed event to terminate the application if the user closes the window (instead of choosing Quit or Exit from the main menu).

Install the Command Line Tool

In addition to the runtime package that you previously referenced in the project file, Electron.NET provides a command-line tool to perform build and deployment tasks. In VS Code, create a new terminal window and type:

This one-time step will install a .NET Core global tool that implements a command named electronize. To see a list of tools/commands installed on your system, type the following:

Run the Electronized Application

After installing the command-line tool, type these lines in the VS Code terminal window:

The first line is a one-time step that creates a manifest file named electron.manifest.json and adds it to your project. The second line is used to launch the Electron application (don't use F5, as this will only open the ASP. NET Core application in the browser). Note that the content now appears in an application window, not a browser.

Also, note the default Electron application menu. On Macs, this menu is not part of the window itself but anchored to the top of the screen.

At the time of this writing, there is a script error in the Bootstrap module installed when you created the project.

To see it, open the View menu and click Toggle Developer Tools.

Fortunately, there is an easy fix. First, open the Electron menu and click Quit Electron to close Developer Tools and the application window. In VS Code, open Pages/Shared/_Layout.cshtml and insert the following line before the Bootstrap script tag:

Save this change, then type electronize start in the terminal window to rerun the application. Open Developer Tools and note that the error message is gone. Keep the application window open for the next step.

Debug ASP. NET Code

Since we launched our application with an external command instead of F5, we need to attach a debugger to the running ASP. NET process. With the application window open, go to VS Code, open Pages/Index.cshtml.cs, and set a breakpoint on the OnGet handler. Click Run on the activity bar, select .NET Core Attach from the dropdown control, then click the adjacent icon to reveal a list of processes. Type the name of the application (Processes) into the list and select the one remaining item. (If by some chance there are multiple processes still displayed, pick the one with the largest value of electronWebPort.)

In the application window, click Reload on the View menu, and the breakpoint will be hit. Continue execution, close the application window, and note that the debugger is automatically disconnected.

Customize the Home Page

To illustrate the cross-platform capabilities of Electron.NET, let's replace the default home page content with a list of active system processes. Later on, we'll build a Linux version and observe the differences on that platform.

First, open Pages/Index.cshtml.cs and add the following using statement for process APIs:

Next, add the following property declaration to the IndexModel class:

Now add the following lines to the (initially empty) body of the OnGet handler:

Now let's modify the corresponding Razor page markup to display the list of processes.

Open Pages/Index.cshtml and replace the entire original <div> tag with the following lines:

This modification will display a table of named processes with columns for the id number, process name, and the amount of physical memory allocated for the process. Note the use of the inline try/catch block. On some platforms, the process name may be truncated, so the module name is favored, with the process name serving as a fallback value.

Electron.NET supports a watch mode where it will monitor your changes and automatically rebuild and relaunch your application. To invoke the watch mode, run the following command:

Build App For Macos Electronics

Now save all of your changes to the project. After the application restarts, the modified home page should look something like this:

Add the Detail View

In a typical ASP. NET Core application, items in a list contain a link to a detail page where users can view the item in greater detail or modify it. Let's create a simple view for an individual process. First, add a new file to the Pages folder named View.cshtml and insert the following markup:

Next, add a corresponding code-behind file named View.cshtml.cs and insert the following code:

The string array PropertyList defines a list of Process object properties to be displayed in the detail view. Rather than hard coding these strings in the page markup, we use reflection to derive property names and values at run time.

To link the detail view to individual items on the home page, edit Pages/Index.cshtml and replace the expression:

with this anchor tag:

Run the application as before and note that the Id column contains hyperlinks that navigate to a page similar to this one:

You may have noticed that the markup contains a submit button for an HTTP POST request. Let's finish the implementation of the detail page by adding the following method to the ViewModel class:

While this will undoubtedly do the job, it would be better to give the user a chance to think it over and cancel the operation. Let's replace the last two lines with the following code, which uses the ShowMessageBoxAsync API of Electron.NET to display a platform-specific confirmation dialog box to the user:

This way, if the user cancels, the detail page remains current. Otherwise, the application redirects to the home page after killing the process.

Customize the Application Menu

Electron.NET provides a default application menu, as illustrated previously. This menu is the same default menu provided by the Electron framework itself. Unfortunately, there is no way to tweak the default menu (a limitation of Electron). If you want to add a new command or remove a submenu that you don't need, you have no choice but to specify the entire menu structure from scratch. This task is further complicated by the differences between macOS and other platforms. On macOS, applications have their own menu to the left of the standard File/Edit/View menus.

Note the use of predefined menu roles to specify the appropriate actions and shortcut keys for the target platform. Also, because of the way Electron.NET serializes the argument passed to SetApplicationMenu, you need to build the entire menu structure using an array initializer. You can't append MenuItem instances to an empty array and then pass that to the menu API.

Now when you run the application on a Mac, the menu bar will have three submenus named Electron, File, and View.

Electron.NET also supports native system file dialogs. Let's modify the File menu definition to add a Save As command that prompts the user for a filename and outputs the current process list to that file in comma-delimited format.

Instead of specifying one of the predefined menu roles, we set the menu type to normal and provide an asynchronous Click handler. The ShowSaveDialogAsync API opens a native file dialog using the specified options, in this case, a filter for files with the .csv extension. If the user does not cancel the dialog, the API returns the full path to the selected file. This path is used as an argument to the SaveAs Razor page, which contains an OnGetAsync handler that outputs comma-delimited text:

Add Third-Party Controls

Build App For Macos Electron Windows 10

As with any regular ASP. NET Core web site, you can add third-party controls to an Electron.NET application. Let's replace the default privacy page with a ComponentOne FlexChart control that plots the top ten processes in descending order of physical memory used.

Download ComponentOne FlexGrid for ASP. NET Core MVC from NuGet or the ASP. NET MVC Edition

Download the latest version of ComponentOne Studio Enterprise

Download Now!

First, add the following package reference to the .csproj file:

Edit Pages/_ViewImports.cshtml and append the corresponding tag helper:

Edit Pages/Shared/_Layout.cshtml and insert the following lines before the closing </head> tag:

Then, in the same file, replace the first two occurrences of Privacy with Chart.

Edit Startup.cs and replace the UseEndpoints call with the following:

The call to MapControllerRoute is needed for the MVC controls to load their resources properly.

Build App For Macos Electron Drive

Lastly, create a file named Chart.cshtml in the Pages folder and add the following markup:

Then add the corresponding code-behind file, Chart.cshtml.cs:

Save all of your changes. Note that the build process will create a 30-day trial license for ComponentOne Studio Enterprise. Click the Chart link in the menu bar, and you should see a bar chart similar to the following:

Build for Other Platforms

To build installation media for other platforms, run the following command in a terminal window:

Where xxx is one of win, linux, osx. Output goes to the bin/Desktop folder, for example:

  • Processes Setup 1.0.0.exe (Windows)
  • Processes-1.0.0.AppImage (Linux)
  • Processes-1.0.0.dmg (OSX)

Note that the Windows executable is a setup program, not the application itself. Also, OSX targets can only be built on a Mac, but Windows/Linux targets can be built on any platform. To change the version number, copyright notice, and other attributes, edit electron.manifest.json before creating the installation media.

Build App For Macos Electron Configuration

Here's what the application looks like running on Linux:

Conclusion and Sample Code

Electron.NET is an open-source tool that adds value to ASP. NET Core by providing C# developers with a vehicle for delivering cross-platform desktop applications for Windows, Linux, and macOS. It is also compatible with third-party components such as the MVC controls in ComponentOne Studio Enterprise.

The source code for the completed project described in this article is available on GitHub.

Try ComponentOne Controls Free for 30 Days

Download the latest version of ComponentOne Studio Enterprise

Download Now!