Rajeev Rajeev - 1 month ago 24
Scala Question

import electron browser window in scala js

I want write below code in scala.js :

// In the main process.
const {BrowserWindow} = require('electron')

// Or use `remote` from the renderer process.
// const {BrowserWindow} = require('electron').remote

let win = new BrowserWindow({width: 800, height: 600})
win.on('closed', () => {
win = null
})

// Load a remote URL
win.loadURL('https://github.com')

// Or load a local HTML file
win.loadURL(`file://${__dirname}/app/index.html`)


How can we do import electron and instantiate BrowserWindow in scala.js

Answer

With Scala.js 0.6.13+, you can choose to emit CommonJS modules. When you do that, you can import BrowserWindow like this:

import scala.scalajs.js
import js.annotation._

@ScalaJSDefined
trait BrowserWindowOptions extends js.Object {
  def width: Double
  def height: Double
}

@js.native
@JSImport("electron", "BrowserWindow")
class BrowserWindow(options: BrowserWindowOptions) extends js.Object {
  ...
  def on(eventName: String, handler: js.Function0[Any]): Unit = js.native
  ...
}

var win = new BrowserWindow(new BrowserWindowOptions {
  val width = 800.0
  val height = 600.0
})
win.on("closed", { () =>
  win = null
})
Comments