2. 桌面環(huán)境整合

原文:https://github.com/electron/electron/blob/master/docs/tutorial/desktop-environment-integration.md
譯者:Lin

不同的操作系統(tǒng)為集成桌面應(yīng)用程序到它們的桌面環(huán)境提供了不同的特性。例如冀膝,Windows系統(tǒng)中窝剖,應(yīng)用程序可以在任務(wù)欄的跳轉(zhuǎn)列表中放置它們的快捷方式,Mac系統(tǒng)中熬北,應(yīng)用程序可以在dock菜單中放置一個(gè)自定義的菜單疙描。

本篇指南將講解如何使用Electron接口集成你的應(yīng)用程序到這些桌面環(huán)境中。

通知(Windows, Linux, macOS)

三個(gè)操作系統(tǒng)都給提供了應(yīng)用程序發(fā)送給用戶通知的方法讶隐。Electron是非常方便的允許開發(fā)者使用HTML5的Notification接口發(fā)送通知起胰,然后使用當(dāng)前操作系統(tǒng)的原生通知接口展示通知。

注意:因?yàn)檫@是一個(gè)HTML5接口巫延,所以他只在渲染進(jìn)程中有效效五。

let myNotification = new Notification('Title', {
    body: 'Lorem Ipsum Dolor Sit Amet'
})

myNotification.onclick = () => {
    console.log('Notification clicked')
}

雖然代碼和跨操作系統(tǒng)的用戶體驗(yàn)是相同,但是它們有著細(xì)微的差別炉峰。

Windows
  • Windows 10操作系統(tǒng)中戒劫,notifications “just work”。
  • Windows 8.1和Windows 8操作系統(tǒng)中,你的應(yīng)用程序的一個(gè)包含一個(gè)Application User Model ID的快捷方式必須安裝到開始屏幕上枚尼。注意蜻直,它不一定要被固定在開始屏幕上。
  • Windows 7操作系統(tǒng)中王悍,通知是不被支持的源譬。然而你可以使用Tray API發(fā)送“balloon notifications”。

此外,通知主體的最大長(zhǎng)度是250個(gè)字符,Windows團(tuán)隊(duì)建議通知應(yīng)該保持在200個(gè)字符以內(nèi)霞溪。

Linux

通知使用libnotify發(fā)送,它可以將通知顯示在任何一個(gè)桌面環(huán)境中,遵循Desktop Notifications Specification,包括了Cinnamon孤页、Enlightenment悲龟、Unity皿渗、GNOME、KDE。

macOS

通知在macOS中是直截了當(dāng)?shù)模欢阈枰私?a target="_blank" rel="nofollow">Apple’s Human Interface guidelines regarding notifications。
請(qǐng)注意懦尝,通知長(zhǎng)度僅限于256字節(jié)大小——如果你超過了這個(gè)限制它將會(huì)被截?cái)唷?/p>

最近的文件

Windows和macOS提供簡(jiǎn)單方便訪問最近打開的文件的列表踊挠,應(yīng)用分別通過跳轉(zhuǎn)列表和dock菜單進(jìn)入扁凛。

JumpList
Application dock menu

添加一個(gè)文件到最近文件列表则披,你可以使用app.addRecentDocument接口:

const {app} = require('electron')
app.addRecentDocument('/Users/USERNAME/Desktop/work.type')

你可以使用app.clearRecentDocuments接口清空最近文件列表:

const {app} = require('electron')
app.clearRecentDocuments()
Windows中的注意事項(xiàng)

為了能夠在Windows使用使用這個(gè)特性阱洪,你的應(yīng)用程序需要被注冊(cè)為一個(gè)處理文件的程序蚌本,否則即使你添加文件轴猎,文件也不會(huì)被展示在跳轉(zhuǎn)列表中。你可以在Application Registration中找到任何關(guān)于注冊(cè)你的應(yīng)用程序的信息。

當(dāng)一個(gè)用戶從跳轉(zhuǎn)列表中點(diǎn)擊一個(gè)文件,一個(gè)你的應(yīng)用程序的新的實(shí)例將會(huì)被啟動(dòng),這個(gè)文件路徑將作為一個(gè)命令行參數(shù)添加到你的應(yīng)用程序中蛮位。

MacOS注意事項(xiàng)

當(dāng)一個(gè)文件從最近文件列表中請(qǐng)求控轿,app中的open-file事件接口將會(huì)被激活片部。

自定義Dock菜單(MacOS)

MacOS 允許開發(fā)人員在dock上指定一個(gè)包含了一些你的應(yīng)用程序常用的功能的自定義菜單:

Dock menu of Terminal.app

你可以使用app.dock.setMenu接口設(shè)置你的自定義dock菜單缘回,這只能在MacOS上顯示:

const {app, Menu} = require('electron')

const dockMenu = Menu.buildFromTemplate([
    {label: 'New Window', click () { console.log('New Window') }},
    {label: 'New Window with Settings',
        submenu: [
            {label: 'Basic'},
            {label: 'Pro'}
        ]
    },
    {label: 'New Command...'}
])
app.dock.setMenu(dockMenu)

用戶任務(wù)(Windows)

Windows中你可以在跳轉(zhuǎn)列表的Tasks部分中指定自定義事件琳水,引用MSDN:

Applications define tasks based on both the program’s features and the key things a user is expected to do with them. Tasks should be context-free, in that the application does not need to be running for them to work. They should also be the statistically most common actions that a normal user would perform in an application, such as compose an email message or open the calendar in a mail program, create a new document in a word processor, launch an application in a certain mode, or launch one of its subcommands. An application should not clutter the menu with advanced features that standard users won’t need or one-time actions such as registration. Do not use tasks for promotional items such as upgrades or special offers.
It is strongly recommended that the task list be static. It should remain the same regardless of the state or status of the application. While it is possible to vary the list dynamically, you should consider that this could confuse the user who does not expect that portion of the destination list to change.

Tasks of Internet Explorer

不像在macOS下dock菜單是一個(gè)真正的菜單,Windows中用戶任務(wù)更像一個(gè)應(yīng)用程序快捷方式仆百,當(dāng)用戶點(diǎn)擊一個(gè)任務(wù)建丧,一個(gè)程序?qū)⒔邮盏街付ǖ膮?shù)來執(zhí)行。

你可以使用app.setUserTasks接口來為你的應(yīng)用程序設(shè)置用戶任務(wù):

const {app} = require('electron')
app.setUserTasks([
    {
        program: process.execPath,
        arguments: '--new-window',
        iconPath: process.execPath,
        iconIndex: 0,
        title: 'New Window',
        description: 'Create a new window'
    }
])

清理你的任務(wù)列表只需要調(diào)用app.setUserTasks賦值一個(gè)空的數(shù)組就可以:

const {app} = require('electron')
app.setUserTasks([])

即使你的應(yīng)用程序關(guān)閉了叁熔,用戶任務(wù)也仍然會(huì)顯示革砸,所以任務(wù)中的圖標(biāo)和程序路徑在你的應(yīng)用程序被寫在之前一直存在。

縮略圖工具欄(Windows)

Windows中你可以在應(yīng)用窗口的任務(wù)欄布局中添加一個(gè)帶有指定按鈕的縮略圖工具欄阎肝。它提供給用戶在不用還原或激活窗口的情況下眼刃,一個(gè)進(jìn)入一個(gè)詳細(xì)的窗口命令的方法。

MSDN中是這樣說明的:

This toolbar is simply the familiar standard toolbar common control. It has a maximum of seven buttons. Each button’s ID, image, tooltip, and state are defined in a structure, which is then passed to the taskbar. The application can show, enable, disable, or hide buttons from the thumbnail toolbar as required by its current state.
For example, Windows Media Player might offer standard media transport controls such as play, pause, mute, and stop.

Thumbnail toolbar of Windows Media Player

你可以使用BrowserWindow.setThumbarButtons在你的應(yīng)用程序中設(shè)置縮略圖工具欄:

const {BrowserWindow} = require('electron')
const path = require('path')

let win = new BrowserWindow({
    width: 800,
    height: 600
})

win.setThumbarButtons([
    {
        tooltip: 'button1',
        icon: path.join(__dirname, 'button1.png'),
        click () { console.log('button1 clicked') }
    },
    {
        tooltip: 'button2',
        icon: path.join(__dirname, 'button2.png'),
        flags: ['enabled', 'dismissonclick'],
        click () { console.log('button2 clicked.') }
    }
])

清空縮略圖工具欄只需要調(diào)用BrowserWindow.setThumbarButtons接口并傳入一個(gè)空的數(shù)組:

const {BrowserWindow} = require('electron')
let win = new BrowserWindow()
win.setThumbarButtons([])

Unity Launcher Shortcuts (Linux)

Unity中钳垮,你可以通過修改.desktop文件添加自定義entries到它自己的啟動(dòng)器中,詳情請(qǐng)見Adding Shortcuts to a Launcher你稚。

Launcher shortcuts of Audacious

任務(wù)欄中的進(jìn)度條

Windows中一個(gè)任務(wù)欄上的按鈕可以被用來展示一個(gè)進(jìn)度條涯肩。這使得一個(gè)窗口可以提供進(jìn)度信息給用戶,而無需用戶切換到窗口本身瞒斩。
MacOS中將會(huì)被展示為dock圖標(biāo)的一部分抛丽。

Unity DE也同樣有一個(gè)類似允許你在啟動(dòng)器中設(shè)置進(jìn)度條的功能。

Progress bar in taskbar button

你可以使用BrowserWindow.setProgressBar接口來給一個(gè)窗口設(shè)置進(jìn)度條:

const {BrowserWindow} = require('electron')
let win = new BrowserWindow()
win.setProgressBar(0.5)

任務(wù)欄的圖標(biāo)覆蓋(Windows)

Windows中一個(gè)任務(wù)欄上的按鈕可以用一個(gè)小的重疊狀態(tài)來展示應(yīng)用狀態(tài)扔仓,MSDN中是這樣說明的:

Icon overlays serve as a contextual notification of status, and are intended to negate the need for a separate notification area status icon to communicate that information to the user. For instance, the new mail status in Microsoft Outlook, currently shown in the notification area, can now be indicated through an overlay on the taskbar button. Again, you must decide during your development cycle which method is best for your application. Overlay icons are intended to supply important, long-standing status or notifications such as network status, messenger status, or new mail. The user should not be presented with constantly changing overlays or animations.

Overlay on taskbar button

你可以使用BrowserWindow.setOverlayIcon接口來為一個(gè)窗口設(shè)置重疊圖標(biāo):

const {BrowserWindow} = require('electron')
let win = new BrowserWindow()
win.setOverlayIcon('path/to/overlay.png', 'Description for overlay')

閃爍框架(Windows)

Windows中你可以高亮任務(wù)欄按鈕來獲得用戶的關(guān)注驼鹅。這類似于在macOS中dock上圖標(biāo)的彈跳。MSDN的文檔參考:

Typically, a window is flashed to inform the user that the window requires attention but that it does not currently have the keyboard focus.

你可以使用BrowserWindow.flashFrame接口來設(shè)置任務(wù)欄按鈕的閃爍狀態(tài):

const {BrowserWindow} = require('electron')
let win = new BrowserWindow()
win.once('focus', () => win.flashFrame(false))
win.flashFrame(true)

不要忘記調(diào)用flashFrame方法設(shè)置為false來關(guān)閉閃爍。上面的示例中双谆,當(dāng)窗口獲得焦點(diǎn)時(shí)它會(huì)被調(diào)用,但是你可能會(huì)使用一個(gè)延時(shí)時(shí)間或其他事件來禁用它果善。

窗口的路徑文件 (macOS)

MacOS中一個(gè)窗口可以設(shè)置它的路徑文件,并且文件的圖片可以展示在標(biāo)題欄句喜,當(dāng)用戶按Command鍵或者Control鍵時(shí),一個(gè)快捷功能表將會(huì)展示在標(biāo)題上。

你也可以設(shè)置窗口的編輯狀態(tài)以便文件圖標(biāo)可以指示窗口中的文檔是否被修改隅居。

Represented file popup menu

你可以使用BrowserWindow.setRepresentedFilenameBrowserWindow.setDocumentEdited接口來設(shè)置窗口路徑文件:

const {BrowserWindow} = require('electron')
let win = new BrowserWindow()
win.setRepresentedFilename('/etc/passwd')
win.setDocumentEdited(true)

將文件拖拽出窗口

對(duì)于某些需要操作文件類型的應(yīng)用申钩,有很重要的功能室允許拖拽文件從Electron到其他應(yīng)用廉涕。在你的應(yīng)用中實(shí)現(xiàn)這個(gè)功能你需要在ondragstart事件中調(diào)用webContents.startDrag(item)接口廉白。

網(wǎng)頁中:

<a href="#" id="drag">item</a>
<script type="text/javascript" charset="utf-8">
    document.getElementById('drag').ondragstart = (event) => {
        event.preventDefault()
        ipcRenderer.send('ondragstart', '/path/to/item')
    }
</script>

主進(jìn)程中:

const {ipcMain} = require('electron')
ipcMain.on('ondragstart', (event, filePath) => {
    event.sender.startDrag({
        file: filePath,
        icon: '/path/to/icon.png'
    })
})
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末摹量,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子当凡,更是在濱河造成了極大的恐慌沿量,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件席镀,死亡現(xiàn)場(chǎng)離奇詭異永高,居然都是意外死亡曹傀,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門饲宛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來皆愉,“玉大人,你說我怎么就攤上這事艇抠『ダ玻” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵练链,是天一觀的道長(zhǎng)翔脱。 經(jīng)常有香客問我,道長(zhǎng)媒鼓,這世上最難降的妖魔是什么届吁? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮绿鸣,結(jié)果婚禮上疚沐,老公的妹妹穿的比我還像新娘。我一直安慰自己潮模,他們只是感情好亮蛔,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著擎厢,像睡著了一般究流。 火紅的嫁衣襯著肌膚如雪辣吃。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天芬探,我揣著相機(jī)與錄音神得,去河邊找鬼偷仿。 笑死别智,一個(gè)胖子當(dāng)著我的面吹牛蛇数,可吹牛的內(nèi)容都是我干的倚评。 我是一名探鬼主播冕香,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼惫霸,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼居触!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起责鳍,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤咒程,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后鳖宾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體渔肩,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年拇惋,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了周偎。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡撑帖,死狀恐怖蓉坎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情胡嘿,我是刑警寧澤蛉艾,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站衷敌,受9級(jí)特大地震影響勿侯,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜缴罗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一助琐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瞒爬,春花似錦弓柱、人聲如沸沟堡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽航罗。三九已至禀横,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間粥血,已是汗流浹背柏锄。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留复亏,地道東北人趾娃。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像缔御,于是被迫代替她去往敵國(guó)和親抬闷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 原文:https://github.com/electron/electron/blob/master/docs/...
    Shmily落墨閱讀 19,022評(píng)論 1 5
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)笤成,斷路器评架,智...
    卡卡羅2017閱讀 134,665評(píng)論 18 139
  • 原文:https://github.com/electron/electron/blob/master/docs/...
    Shmily落墨閱讀 2,645評(píng)論 0 1
  • Ubuntu的發(fā)音 Ubuntu培遵,源于非洲祖魯人和科薩人的語言浙芙,發(fā)作 oo-boon-too 的音。了解發(fā)音是有意...
    螢火蟲de夢(mèng)閱讀 99,282評(píng)論 9 467
  • 今天是驛站AACTP的第二次會(huì)議籽腕,我擔(dān)任時(shí)間官負(fù)責(zé)各環(huán)節(jié)的時(shí)間把控茁裙。整個(gè)會(huì)議下來,收獲頗多节仿,具體如下: 一晤锥、關(guān)于時(shí)...
    文言聞閱讀 531評(píng)論 1 1