桌面Web应用程序中的丰富HTML托盘菜单

时间:2016-11-08 10:56:02

标签: desktop-application electron nw.js phonegap-desktop-app

我想创建一个托盘菜单应用程序,其中包含自定义按钮,滑块,可能是一些不错的过渡效果,页眉和页脚如下:

menu

该应用程序需要在Linux,Windows和Mac上运行。 我猜想应该可以使用桌面Web应用程序+一些HTML,但我找不到任何框架的任何有用示例。每个例子都使用OS'菜单,它没有我需要的元素。

任何人都可以指导我如何在任何网络应用框架中或多或少地实现这一点?

1 个答案:

答案 0 :(得分:10)

这可以很容易地在电子中完成,我实际上已经在下面的图像中创建了一些托盘应用程序:

Tray app Trap app 2

以下是一篇文章,概述了要做的事情:http://www.bytcode.com/articles/1

您需要的基本文件是:

  • 的index.html
  • main.js
  • 的package.json

index.html中,您可以按照自己想要的方式设计自己的应用。在上面的例子中,我只使用了几个输入框并用CSS设置了它。

main.js文件中,您可以使用主代码来启动应用。

package.json文件中,您可以放置​​有关应用程序,开发依赖项等的详细信息。

您应该关注的主要文件是main.js文件。以下是上述应用的main.js文件示例。我已添加评论以帮助您理解:

// Sets variables (const)
const {app, BrowserWindow, ipcMain, Tray} = require('electron')
const path = require('path')

const assetsDirectory = path.join(__dirname, 'img')

let tray = undefined
let window = undefined

// Don't show the app in the doc
app.dock.hide()

// Creates tray & window
app.on('ready', () => {
  createTray()
  createWindow()
})

// Quit the app when the window is closed
app.on('window-all-closed', () => {
  app.quit()
})

// Creates tray image & toggles window on click
const createTray = () => {
  tray = new Tray(path.join(assetsDirectory, 'icon.png'))
  tray.on('click', function (event) {
    toggleWindow()
  })
}

  const getWindowPosition = () => {
  const windowBounds = window.getBounds()
  const trayBounds = tray.getBounds()

  // Center window horizontally below the tray icon
  const x = Math.round(trayBounds.x + (trayBounds.width / 2) - (windowBounds.width / 2))

  // Position window 4 pixels vertically below the tray icon
  const y = Math.round(trayBounds.y + trayBounds.height + 3)

  return {x: x, y: y}
}

// Creates window & specifies its values
const createWindow = () => {
  window = new BrowserWindow({
        width: 250,
        height: 310,
        show: false,
        frame: false,
        fullscreenable: false,
        resizable: false,
        transparent: true,
        'node-integration': false
    })
    // This is where the index.html file is loaded into the window
    window.loadURL('file://' + __dirname + '/index.html');

  // Hide the window when it loses focus
  window.on('blur', () => {
    if (!window.webContents.isDevToolsOpened()) {
      window.hide()
    }
  })
}

const toggleWindow = () => {
  if (window.isVisible()) {
    window.hide()
  } else {
    showWindow()
  }
}

const showWindow = () => {
  const position = getWindowPosition()
  window.setPosition(position.x, position.y, false)
  window.show()
  window.focus()
}

ipcMain.on('show-window', () => {
  showWindow()
})

以下是package.json文件的示例:

{
  "name": "NAMEOFAPP",
  "description": "DESCRIPTION OF APP",
  "version": "0.1.0",
  "main": "main.js",
  "license": "MIT",
  "author": "NAME OF AUTHOR",
  "scripts": {
    "start": "electron ."
  },
  "devDependencies": {
    "electron-packager": "^8.2.0"
  }
}

因此,如果您创建一个简单的index.html文件,说明Hello World,请将上述代码分别放入main.js文件和package.json文件中并运行它将从托盘运行的应用程序

如果你不知道如何使用电子,你需要首先弄明白(不是很难掌握)。然后,将清楚地放置哪些文件以及如何运行应用程序

这可能看起来有点复杂,有关详细信息,请阅读docs

相关问题