【问题标题】:Rich HTML tray menu in a desktop web application桌面 Web 应用程序中的丰富 HTML 托盘菜单
【发布时间】:2017-03-21 23:20:28
【问题描述】:

我想创建一个带有自定义按钮、滑块的托盘菜单应用程序,也许还有一些不错的过渡效果,像这样的页眉和页脚:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    您需要的基本文件是:

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

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

    main.js 文件是您放置主代码以驱动应用程序的位置。

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

    您应该关注的主要文件是main.js 文件。下面是上述应用程序的main.js 文件示例。我添加了 cmets 来帮助你理解:

    // 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 文件中,然后运行将从托盘运行的应用程序。

    如果你不知道如何使用 Electron,你需要先弄清楚 (Electron docs)。然后将清楚放置哪个文件以及如何运行应用程序。

    【讨论】:

    • 谢谢!因此,此代码在托盘图标旁边显示了一个无边框窗口。这很整洁!
    • 如果您希望内容(如标题/文本等)在窗口内,您需要编辑 index.html 文件,就像您编辑网站一样
    • 这适用于不同的面板位置吗? (左、右、上、下)
    • 哦,我以为你可以移动它。:)
    • 我不太确定,因为我不使用 Linux,虽然这似乎是一个不固定的问题 - bugs.chromium.org/p/chromium/issues/detail?id=309969
    猜你喜欢
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多