From da3ce257d0e511408f4795ad92170a8be3c5f015 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 5 Apr 2016 13:01:06 +0200 Subject: [PATCH] Added context menu, set badge via remote. --- .../controllers/widget/electron_events.coffee | 82 ++++++++++++++++++- 1 file changed, 79 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/app/controllers/widget/electron_events.coffee b/app/assets/javascripts/app/controllers/widget/electron_events.coffee index 81ad4d641..2d49e4602 100644 --- a/app/assets/javascripts/app/controllers/widget/electron_events.coffee +++ b/app/assets/javascripts/app/controllers/widget/electron_events.coffee @@ -1,10 +1,86 @@ class Widget constructor: -> return if !window.require - ipcRenderer = window.require('electron').ipcRenderer - return if !ipcRenderer + electron = window.require('electron') + return if !electron + remote = electron.remote App.Event.bind('online_notification_counter', (e) -> - ipcRenderer.send('set-badge', e) + setBadge(e) ) + Menu = remote.Menu + MenuItem = remote.MenuItem + + createDefault = -> + menu = new Menu() + menu.append(new MenuItem({ + label: 'Cut', + role: 'cut' + })) + menu.append(new MenuItem({ + label: 'Copy', + role: 'copy' + })) + menu.append(new MenuItem({ + label: 'Paste', + role: 'paste' + })) + menu.append(new MenuItem({ + label: 'Select All', + role: 'selectall' + })) + menu + + menu = createDefault() + window.addEventListener('contextmenu', (e) -> + menu.popup(remote.getCurrentWindow()) + false + ) + + badgeDataURL = (text) -> + scale = 2 # should rely display dpi + size = 16 * scale + canvas = document.createElement('canvas') + canvas.setAttribute('width', size) + canvas.setAttribute('height', size) + ctx = canvas.getContext('2d') + + # circle + ctx.fillStyle = '#FF1744' # Material Red A400 + ctx.beginPath() + ctx.arc(size / 2, size / 2, size / 2, 0, Math.PI * 2) + ctx.fill() + + # text + ctx.fillStyle = '#ffffff' + ctx.textAlign = 'center' + ctx.textBaseline = 'middle' + ctx.font = (11 * scale) + 'px sans-serif' + ctx.fillText(text, size / 2, size / 2, size) + + canvas.toDataURL() + + setBadgeWindows = (content) -> + sendBadge = (dataURL, description) -> + electron.ipcRenderer.send('win32-overlay', { + overlayDataURL: dataURL, + description: description, + content: content, + }) + + if content isnt '' + dataURL = badgeDataURL.createDataURL(content.toString()) + sendBadge(dataURL, 'You have unread messages (' + content + ')') + else + sendBadge(null, 'You have no unread messages') + + setBadgeOSX = (content) -> + remote.app.dock.setBadge(content) + + setBadge = (content) -> + if process.platform is 'win32' + setBadgeWindows(content) + else if process.platform is 'darwin' + setBadgeOSX(content) + App.Config.set('aaa_electron_events', Widget, 'Navigations')