Click here to Skip to main content
15,878,852 members
Articles / Web Development / HTML

Chromely - Lightweight Alternative to Electron

Rate me:
Please Sign up or sign in to vote.
4.91/5 (12 votes)
30 May 2018CPOL4 min read 45.5K   672   41   19
Build .NET/.NET Core HTML5 desktop apps using cross-platform native GUI API

Introduction

Chromely is a "lightweight" .NET/.NET Core HTML5 Chromium desktop framework alternative to Electron.NET, Electron for .NET/.NET Core developers.

Chromely builds HTML5 desktops without WinForms or WPF. Chromely uses Windows and Linux native GUI API as chromium hosts. It embeds chromium using CEF implementation of Xilium.CefGlue and CefSharp.

With Chromely, you can build Single Page Application (SPA) HTML5 desktop apps with or without Node/NPM. Building SPA apps using JavaScript frameworks like Angular, React, Vue or similar is easy. You can use Visual Studio Code or any IDE you are familiar with as long as Chromely knows the entry HTML file from the compiled/bundled files. For more information, please see - Chromely-Apps.

Background

While working on a WPF project, SugarDesk, I was looking for ways to include SugarCrm Reports in my project and the most appealing option was to use Electron. Then, I discovered CefSharp but was not happy with the limitation - you can only use bitmap rendering instead of pure HTML rendering. In short, I ended up creating one without WPF and decided to share it with the community.

Alternative to Electron?

Yes, sort of.

  • Electron is true cross-platform, while Chromely only supports Windows and Linux - for now.
  • Chromely is only for .NET/.NET Core development.

Some advantages over Electron are:

  • Chromely does not require Node or NPM. Final installation and usage does not require Node.
  • Chromely is lightweight. To use Electron in .NET or .NET Core, you will require libraries like Edge.js or host Electron in ASP.NET NET like what is used in Electron.NET. Chromely uses thin native GUI hosts.
    "Lightweight" here is referring to simplicity - simple & less to achieve same, if you may. Chromely still depends on Chromium and not necessarily lighter in that regard.
  • Since this is done in .NET/.NET Core, there are no new skills required for .NET/.NET Core developers.
  • Easy for WPF/WinForms developers to start HTML5 desktop development with little or no Web development experience.

Using the code

Download, build and run the attached source code or follow the steps below.

The steps described here will be for CefSharp Windows App. For CefGlue and SPA apps, please check Chromely GitHub.

  1. Create .NET Console Application
  2. Ensure that the build platform target is x64.
  3. From project property, change "Console Application"" to "Windows Application" [Project -> Properties -> Application -> Output type: "Windows Application"].
  4. Add Chromely.CefSharp.Winapi from Nuget. (A warning may show, if this happens, unload the project and reload.)
    • Chromely.CefSharp.Winapi has the following dependencies and should be verified:
      • Winapi from Nuget (Ensure minimum of 4.0 version is installed that was released for .NET Standard)
      • CefSharp.Common from Nuget.
      • Chromely.Core from Nuget
  5. Create index.html file in the project root. Copy the content of file to index.html. Ensure that index.html is set to "Copy always" or "Copy if newer".
  6. Add the following code snippet to the Main function in Program.cs.
    C#
      1.  class Program
      2.  {
      3.    static int Main(string[] args)
      4.    {
      5.        string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
      6.        string startUrl = $"file:///{appDirectory}index.html";
      7.
      8.        ChromelyConfiguration config = ChromelyConfiguration
                                              .Create()
                                              .WithAppArgs(args)
                                              .WithHostSize(1000, 600)
                                              .WithStartUrl(startUrl);
    
      9.        var factory = WinapiHostFactory.Init();
      10.       using (var window = factory.CreateWindow(() => new CefSharpBrowserHost(config),
                      "chromely", constructionParams: new FrameWindowConstructionParams()))
                {
      11.           window.SetSize(config.HostWidth, config.HostHeight);
      12.           window.CenterToScreen();
      13.           window.Show();
      14.           return new EventLoop().Run(window);
                }
            }
        }
    
    // Creates Chromely window of size 1000 x 600 pixels.
    // Sets the window title to "chromely"
    // Sets start URL to "index.html"
    // Centers the window 
  7. Build the project.
  8. Run the built EXE file.
  9. If successful, the following will be shown:

    Image 1

How does it work?

Thin window/host

Chromely is based on CefGlue/CefSharp. Both are based on CEF that implements Chromium. Chromely approach is to create an HTML5 desktop with a very thin native GUI host. To illustrate using Win32 GUI Api:

C#
10.   var window = factory.CreateWindow(() => new CefSharpBrowserHost(config),

This creates a window/host using WinApi

Configuration

Both host and CEF are configurable. Host properties like size, icon can be set in the configuration class. CEF command line arguments and settings are also configurable. Configuration details can be found @ Chromely Configuration.

C#
5.        string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
6.        string startUrl = $"file:///{appDirectory}index.html";
7.
8.        ChromelyConfiguration config = ChromelyConfiguration
                                    .Create()
                                    .WithAppArgs(args)
                                    .WithHostSize(1000, 600)
                                    .WithStartUrl(startUrl);

Running the window/host

C#
11.           window.SetSize(config.HostWidth, config.HostHeight);
12.           window.CenterToScreen();
13.           window.Show();
14.           return new EventLoop().Run(window);

This snippet sets the size of the window, centers the window, shows the window, and runs the window in an infinite loop until window is closes.

Conclusion

This is a brief introduction to Chromely. Chromely offers a complete simple/lightweight framework as a different approach to use of Chromium Embedded Framework as base for achieving a modern HTML5 based GUI without the usual use of WPF and WinForms. I intend to highlight features of Chromely in future articles, however, Chromely github site has more info.

 

Points of Interest

What problems does Chromely solve? Many, some or none depending on what angle you are looking at it. If you are looking at developing pure HTML5 desktop apps, Chromely is for you. If you want to add/integrate HTML5 to a project requiring WPF or WinForms controls, then alternatives like NeutroniumCore or NanUI might be better options.

How does it compare to other pure HTML5 desktop frameworks? As stated earlier, Electron is, perhaps, the biggest player for pure cross-platform desktop frameworks. Chromely provides lighter alternative to Electron ONLY for .NET/.NET Core development. Also Chromely is not pure cross-platform - only Windows and Linux supported for now.

How does it help developers? The premise of Chromely is that without use of Node/NPM UI developers moving to the html/web world will have it easy to transition and vise versa.

References

  1. WinApi - https://github.com/prasannavl/WinApi
  2. Cef - https://bitbucket.org/chromiumembedded/cef
  3. CefSharp - https://github.com/cefsharp/CefSharp

History

  1. May 13, 2018 - Article submitted.
  2. May 14, 2018 - Added Chromely Github links.
  3. May 23, 2018 - Typo correction.
  4. May 28, 2018 - Article updated.
  5. May 29, 2018 - Typo correction.
This article was originally posted at https://github.com/mattkol/Chromely

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionProblem running a simple html5 file with video tag. Pin
SilverSoler12327-Jan-21 8:11
SilverSoler12327-Jan-21 8:11 
AnswerRe: Problem running a simple html5 file with video tag. Pin
OriginalGriff27-Jan-21 8:14
mveOriginalGriff27-Jan-21 8:14 
GeneralRe: Problem running a simple html5 file with video tag. Pin
SilverSoler12327-Jan-21 9:59
SilverSoler12327-Jan-21 9:59 
QuestionHow has the community received this? Pin
Your Display Name Here31-Mar-20 2:30
Your Display Name Here31-Mar-20 2:30 
QuestionExtend/Add Message Router Handler for progress feedback Pin
alexyarmoshko4-Oct-18 7:20
alexyarmoshko4-Oct-18 7:20 
Hi,

Thanks for the nice project.

Just wondering how to implement in Chromely a JS callback function that can be called by a C# code few times to provide some feedback on progress? Spent quite a time on this to no avail.

I have tried to find out how to "extend" the existing default Message Router Handler (cefQuery) to have an additional optional callback parameter "OnProgress", but could not find a way to do that.

Also not clear at all how to register an alternative handler with the different parameters, lets say "onSuccess", "onProgress" and "onFailure".

Could you advise on a direction or an approach I should take to implement such functionality?

Thanks.

Alex.
AnswerRe: Extend/Add Message Router Handler for progress feedback Pin
mattkol19-Oct-18 16:20
mattkol19-Oct-18 16:20 
QuestionStep by step doesn't work on VS2017 Pin
Dong Xie4-Jun-18 3:02
Dong Xie4-Jun-18 3:02 
AnswerRe: Step by step doesn't work on VS2017 Pin
mattkol4-Jun-18 12:32
mattkol4-Jun-18 12:32 
GeneralRe: Step by step doesn't work on VS2017 Pin
JohnHolliday21-Dec-18 12:13
JohnHolliday21-Dec-18 12:13 
AnswerRe: Step by step doesn't work on VS2017 Pin
mattkol4-Jun-18 16:06
mattkol4-Jun-18 16:06 
GeneralRe: Step by step doesn't work on VS2017 Pin
JohnHolliday21-Dec-18 12:27
JohnHolliday21-Dec-18 12:27 
QuestionHow to hide the right click menu? Pin
jackking29-May-18 5:20
jackking29-May-18 5:20 
AnswerRe: How to hide the right click menu? Pin
mattkol29-May-18 13:12
mattkol29-May-18 13:12 
QuestionCan't install the required NuGet packages Pin
Member 225326515-May-18 3:52
Member 225326515-May-18 3:52 
AnswerRe: Can't install the required NuGet packages Pin
mattkol17-May-18 22:14
mattkol17-May-18 22:14 
GeneralRe: Can't install the required NuGet packages Pin
JohnHolliday21-Dec-18 12:28
JohnHolliday21-Dec-18 12:28 
AnswerRe: Can't install the required NuGet packages Pin
mattkol17-May-18 22:32
mattkol17-May-18 22:32 
QuestionCore? Pin
Member 244330614-May-18 6:10
Member 244330614-May-18 6:10 
AnswerRe: Core? Pin
mattkol17-May-18 22:06
mattkol17-May-18 22:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.