Electron.NET icon indicating copy to clipboard operation
Electron.NET copied to clipboard

Arguments for Chromium in an Electron.NET app

Open ToniTurek opened this issue 3 years ago • 7 comments

I am running Windows 10 on an iMac with screen scaling. Chromium has issues with that scaling which results in low FPS (around 15). I also see this low FPS when I run my Electron.NET app. When I start chromium with the arguments "--high-dpi-support=1" and "--force-device-scale-factor=1" it runs much better.

How can I enable these chromium arguments in my Electron.NET app?

ToniTurek avatar Feb 18 '22 07:02 ToniTurek

This stackoverflow question might give you some way to do this.

https://stackoverflow.com/questions/57900207/how-to-avoid-windows-zoom-in-electron-app

Basically it's recommending that you append the switches to the command arguments in the main process. I imagine you could do this inside the host hook as well.

danatcofo avatar Feb 18 '22 13:02 danatcofo

Hi @danatcofo

Thanks for your reply!

Acctualy, I tried to set these switches via:

  Electron.App.CommandLine.AppendSwitch("high-dpi-support", "1");
  Electron.App.CommandLine.AppendSwitch("force-device-scale-factor", "1");

  var browserWindow = await Electron.WindowManager.CreateWindowAsync(
    new BrowserWindowOptions
    {
      Width = 1152,
      Height = 940,
      Show = false,
      WebPreferences = new WebPreferences
      {
        Webgl = true,
        AllowRunningInsecureContent = true,
        WebSecurity = false
      }
    }
  );

  await browserWindow.WebContents.Session.ClearCacheAsync();

  // For the gracefull showing of the Electron Window when ready
  browserWindow.OnReadyToShow += () =>
  {
    browserWindow.Show();
    browserWindow.Maximize();
  };

  browserWindow.OnReadyToShow += () => browserWindow.Show();

but this has unfortunatelly no effect.

Does anyone an idea how to pass these arguments to Chromium in Electron.NET?

ToniTurek avatar Feb 18 '22 14:02 ToniTurek

Do it in the host hook js, I believe the .net properties are read only their and won't affect electron.

Remember this is a combo of electron & .net, sometimes you need to get down to the electron.

danatcofo avatar Feb 18 '22 14:02 danatcofo

I created a ElectronHostHook using the electronize add hosthook command. This has created an ElectronHostHook folder with an index.js and a package.json file in it. The index.js looks like this:

// @ts-ignore
import * as Electron from "electron";
import { Connector } from "./connector";

export class HookService extends Connector {
    constructor(socket: SocketIO.Socket, public app: Electron.App) {
      super(socket, app);
    }

    onHostReady(): void {
      // execute your own JavaScript Host logic here
    }
}

but I don't know where to add the

  app.commandLine.appendSwitch("high-dpi-support", "1");
  app.commandLine.appendSwitch("force-device-scale-factor", "1");

lines. Any hint?

ToniTurek avatar Feb 18 '22 17:02 ToniTurek

https://www.electronjs.org/docs/latest/api/app

const { app } = require('electron');

Or something similar

Or in your case given the snippet posted

Const app = Electron.app

danatcofo avatar Feb 18 '22 20:02 danatcofo

This is how my index.ts in the ElectronHostHook folder now looks like:

  // @ts-ignore
  import * as Electron from "electron";
  import { Connector } from "./connector";

  export class HookService extends Connector {
      constructor(socket: SocketIO.Socket, public app: Electron.App) {
        super(socket, app);
      }

      onHostReady(): void {
        // execute your own JavaScript Host logic here
        const app = Electron.app;
        app.commandLine.appendSwitch("high-dpi-support", "true");
        app.commandLine.appendSwitch("force-device-scale-factor", "1");

        console.log("HostHook has been initialized");
      }
  }

But nothing has changed. Not even the console.log output can be seen in the terminal although I see that the ElectronHostHook is executed: grafik

I think its all about customizing the main.js which for Electron.NET is created automatically from the electron.manifest.json. I also tried to explicitely specify a main.js with the electron.manifest.json command: "main": "my_main.js", but this is not working as well.

I'm really lost how to solve this issue...

ToniTurek avatar Feb 19 '22 18:02 ToniTurek

Honestly I'm just googling ideas here.

https://www.geeksforgeeks.org/command-line-arguments-in-electronjs/amp/

You could do the same. But this is an electron issue at its heart. Sorry trying to help.

danatcofo avatar Feb 19 '22 18:02 danatcofo