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

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

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

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

通知(Windows, Linux, macOS)

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

注意:因為這是一個HTML5接口,所以他只在渲染進程中有效。

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

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

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

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

此外,通知主體的最大長度是250個字符,Windows團隊建議通知應該保持在200個字符以內(nèi)。

Linux

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

macOS

通知在macOS中是直截了當?shù)模欢阈枰私?a target="_blank" rel="nofollow">Apple’s Human Interface guidelines regarding notifications。
請注意,通知長度僅限于256字節(jié)大小——如果你超過了這個限制它將會被截斷。

最近的文件

Windows和macOS提供簡單方便訪問最近打開的文件的列表,應用分別通過跳轉(zhuǎn)列表和dock菜單進入。

JumpList
Application dock menu

添加一個文件到最近文件列表,你可以使用app.addRecentDocument接口:

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

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

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

為了能夠在Windows使用使用這個特性,你的應用程序需要被注冊為一個處理文件的程序,否則即使你添加文件,文件也不會被展示在跳轉(zhuǎn)列表中。你可以在Application Registration中找到任何關于注冊你的應用程序的信息。

當一個用戶從跳轉(zhuǎn)列表中點擊一個文件,一個你的應用程序的新的實例將會被啟動,這個文件路徑將作為一個命令行參數(shù)添加到你的應用程序中。

MacOS注意事項

當一個文件從最近文件列表中請求,app中的open-file事件接口將會被激活。

自定義Dock菜單(MacOS)

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

Dock menu of Terminal.app

你可以使用app.dock.setMenu接口設置你的自定義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)

用戶任務(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菜單是一個真正的菜單,Windows中用戶任務更像一個應用程序快捷方式,當用戶點擊一個任務,一個程序?qū)⒔邮盏街付ǖ膮?shù)來執(zhí)行。

你可以使用app.setUserTasks接口來為你的應用程序設置用戶任務:

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'
    }
])

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

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

即使你的應用程序關閉了,用戶任務也仍然會顯示,所以任務中的圖標和程序路徑在你的應用程序被寫在之前一直存在。

縮略圖工具欄(Windows)

Windows中你可以在應用窗口的任務欄布局中添加一個帶有指定按鈕的縮略圖工具欄。它提供給用戶在不用還原或激活窗口的情況下,一個進入一個詳細的窗口命令的方法。

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在你的應用程序中設置縮略圖工具欄:

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接口并傳入一個空的數(shù)組:

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

Unity Launcher Shortcuts (Linux)

Unity中,你可以通過修改.desktop文件添加自定義entries到它自己的啟動器中,詳情請見Adding Shortcuts to a Launcher

Launcher shortcuts of Audacious

任務欄中的進度條

Windows中一個任務欄上的按鈕可以被用來展示一個進度條。這使得一個窗口可以提供進度信息給用戶,而無需用戶切換到窗口本身。
MacOS中將會被展示為dock圖標的一部分。

Unity DE也同樣有一個類似允許你在啟動器中設置進度條的功能。

Progress bar in taskbar button

你可以使用BrowserWindow.setProgressBar接口來給一個窗口設置進度條:

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

任務欄的圖標覆蓋(Windows)

Windows中一個任務欄上的按鈕可以用一個小的重疊狀態(tài)來展示應用狀態(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接口來為一個窗口設置重疊圖標:

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

閃爍框架(Windows)

Windows中你可以高亮任務欄按鈕來獲得用戶的關注。這類似于在macOS中dock上圖標的彈跳。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接口來設置任務欄按鈕的閃爍狀態(tài):

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

不要忘記調(diào)用flashFrame方法設置為false來關閉閃爍。上面的示例中,當窗口獲得焦點時它會被調(diào)用,但是你可能會使用一個延時時間或其他事件來禁用它。

窗口的路徑文件 (macOS)

MacOS中一個窗口可以設置它的路徑文件,并且文件的圖片可以展示在標題欄,當用戶按Command鍵或者Control鍵時,一個快捷功能表將會展示在標題上。

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

Represented file popup menu

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

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

將文件拖拽出窗口

對于某些需要操作文件類型的應用,有很重要的功能室允許拖拽文件從Electron到其他應用。在你的應用中實現(xiàn)這個功能你需要在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>

主進程中:

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)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,002評論 6 542
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,400評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,136評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,714評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,452評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,818評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,812評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,997評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,552評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,292評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,510評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,035評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,721評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,121評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,429評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,235評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,480評論 2 379

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

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