Return to site

Electron Mac Title Bar On Electron For Windows

broken image


Transcript from the 'Update Title Bar Solution' Lesson

[00:00:00]
>> Steve Kinney: All right, so our mission was to update the title bar based on whether or not the application was in an edited state. And then we're gonna take that one step further by enabling or disabling the buttons based on the editing state. And then we'll kind of do a bonus run for some MAC OS specific integration.

Which means many Electron apps will continue to be second-class apps. I do agree, the solution should not affect existing apps. How about introducing an Electron-only CSS property called -electron-app-title-bar? Alternatively, expose a method in the renderer that accepts a CSS selector or element to make the inset title bar. Custom Electron Titlebar. This project is a typescript library for electron that allows you to configure a fully customizable title bar. It is a library for electron, it cannot be used on a basic website. Dec 10, 2020 Send cross platform native notifications using Node.js. Notification Center for macOS, notify-osd/libnotify-bin for Linux, Toasters for Windows 8/10, or taskbar balloons for earlier Windows versions. Growl is used if none of these requirements are met. Works well with Electron. Input Example macOS Notification Center. Actions Example Windows.

[00:00:24] We have it in this update user interface, we just need to basically do something similar to what we have above. So we could say, if isEdited, we can further modify that title, and we'll say, again, I left it up to you on exactly how you wanted to do that.

[00:00:43] If you want to put an asterisk, if you want to put edited, colon, whatever, I'm gonna go with edited. If yours is slightly different, that is very okay. We'll go ahead and refresh this, and I can see that as soon as I start typing, I have that edited.

[00:00:59] If I delete it all the way back to its original state, and those strings match again, it's no longer edited. Let's go ahead and open a file.
>> Steve Kinney: Edited, not edited, it's oddly gratifying to change the window title. I understand that I'm doing that multiple times but it makes me feel good and sometimes this is about me, all right.

[00:01:23] So let's do a few other things, we wanted to enable those buttons so we have the saveMarkdownButton. And we have to do a little bit of reverse logic here, cuz the property is disabled.
>> Steve Kinney: And we'll do the same for the,
>> Steve Kinney: SaveHtmlButton. Go ahead and refresh that.

[00:01:52]
>> Steve Kinney: So save file and save html. Actually save html should always be enabled. We actually want a revert button.
>> Steve Kinney: All right, so now our user interface responds based on whether or not we edited the state. We can see the file name in there as well.
>> Steve Kinney: Cool, all right, next view, little tricks that we're gonna do are Mac OS specific.

[00:02:26] Now even if you're on a Windows computer or Linux computer, there is a nonzero chance that the person running the application could be on Mac OS but you won't be able to see anything on your system currently. That's okay, cuz later when we get to application menus, the roles will be reversed.

[00:02:42] And the Mac users will have to do extra work to have a very simple, easy functionality. So it's all even. It's all a game of give or take here. But right now, we're gonna do one or two things just to make it feel a little bit more like a native application.

[00:02:55] If I open up a true Mac app, this is TextMate. This is a text editor, and I save a file,
>> Steve Kinney: Yeah, show untitled. Save it to the desktop. You can see a few things going on here. One is I have this little file icon here, and if I actually hit Cmd+Click, this will work in any native Mac app.

Dmg download macos 9in1 usb recoveryinstall disk. [00:03:19] You can kind see the entire file tree and open that up in finder. And you can also see that if it's unsaved, I got a little dot here in the Close button. This signify I have unsaved changes. That is built in to native MacOS. It would be cool if our application had the same thing.

[00:03:42] So we can do that pretty easily, cuz Electron has access to those APIs in the same way that it had access to open up a file dialogue on Windows or Mac OS or Linux or anything along those lines. So what we can do is that little file icon here, it's called the represented file.

[00:04:01] And so what we can basically say is,
>> Steve Kinney: currentWindow.setRepresentedFilename(filePath), and we can do currentWindow.
>> Steve Kinney: SetDocumentEdited to isEdited, right? And these will just further modify that title bar to make it basically fit in a little bit more with the Mac OS operating system. So now we'll go ahead.

[00:04:37] Let's open a file.
>> Steve Kinney: You can see, I've got the little icon up here. And I can click it and I can see where it was in the file system just like if it was an application built on Cocoa. And if I change it, you can see that I've got the little dark dot here as well.

[00:04:54] So basically it fits in, right. Now we could listen for a Window close event and alert them about unsafe changes. It's very much the same that we would do it in the client as well. That'll work, right. You can have an event listener for that. We're not gonna do that right now, cuz we're going to do some more Electron specific stuff but I'll leave that as an exercise to the reader

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

Topics covered include:

  • 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:

Electron Mac Title Bar On Electron For Windows 10 Pro

  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.

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: Mission vision and objectives of apple inc.

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).

Windows

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

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:

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

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 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.

Electron Mac Title Bar On Electron For Windows 8.1

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.

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

Electron Mac Title Bar On Electron For Windows Configuration

Download the latest version of ComponentOne Studio Enterprise

Electron Mac Title Bar On Electron For Windows Key

Download Now!



broken image