add tray (windows & mac)

This commit is contained in:
life
2016-12-26 20:02:55 +08:00
parent f9151a771b
commit 3958e0076b
11 changed files with 108 additions and 23 deletions

75
main.js
View File

@@ -1,7 +1,11 @@
// var app = require('electron').app; // Module to control application life.
const {app, BrowserWindow, crashReporter} = require('electron');
var ipc = require('electron').ipcMain;
const electron = require('electron');
const Menu = electron.Menu
const Tray = electron.Tray
var pdfMain = require('pdf_main');
var appIcon;
// Report crashes to our server.
crashReporter.start({
@@ -11,6 +15,7 @@ crashReporter.start({
autoSubmit: true
});
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is GCed.
var mainWindow = null;
@@ -94,7 +99,6 @@ function openIt() {
var leanoteProtocol = require('leanote_protocol');
leanoteProtocol.init();
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1050,
@@ -129,13 +133,27 @@ function openIt() {
if(mainWindow && mainWindow.webContents)
mainWindow.webContents.send('blurWindow');
});
function close (e, force) {
console.log('close:', force);
mainWindow.hide();
e && e.preventDefault();
mainWindow.webContents.send('closeWindow');
}
// 以前的关闭是真关闭, 现是是假关闭了
// 关闭,先保存数据
mainWindow.on('close', function(e) {
console.log('close');
mainWindow.hide();
e.preventDefault();
mainWindow.webContents.send('closeWindow');
// windows支持tray, 点close就是隐藏
if (process.platform.toLowerCase().indexOf('win') === 0) { // win32
mainWindow.hide();
e.preventDefault();
return;
}
// mac 在docker下quit;
// linux直接点x linux不支持Tray
close(e, false);
});
// 前端发来可以关闭了
@@ -146,4 +164,51 @@ function openIt() {
});
pdfMain.init();
function show () {
mainWindow.show();
mainWindow.restore();
mainWindow.focus();
mainWindow.webContents.send('focusWindow');
}
var trayShowed = false;
ipc.on('show-tray', function(event, arg) {
if (trayShowed) {
return;
}
trayShowed = true;
if (process.platform == 'linux') {
return;
}
// 打开一次就自动关了
appIcon = new Tray(__dirname + '/public/images/tray/' + ( process.platform == 'darwin' ? 'trayTemplate.png' : 'tray.png'))
var contextMenu = Menu.buildFromTemplate([
{
label: arg.Open, click: function () {
show();
}
},
{
label: arg.Close, click: function () {
close(null, true);
}
},
]);
appIcon.setToolTip('Leanote');
// appIcon.setTitle('Leanote');
// appIcon.setContextMenu(contextMenu);
appIcon.on('click', function (e) {
show();
e.preventDefault();
});
appIcon.on('right-click', function () {
appIcon.popUpContextMenu(contextMenu);
});
});
}

View File

@@ -33,7 +33,7 @@ var Config = {
"name": "日本語"
}
],
"lang": "en-us",
"lang": "zh-cn",
"theme": "",
"view": "summary"
};

BIN
public/images/tray/tray.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -18,6 +18,7 @@ var Api = {
userService: UserService,
dbService: db,
ipc: nodeRequire('electron').ipcRenderer,
projectPath: projectPath,
// 打开本地目录
// mac和windows下不同

View File

@@ -21,28 +21,13 @@ $(function() {
var isMacP = isMac();
// mac上才会自己控制
$('.tool-close, .tool-close-blur').click(function() {
onClose(function() {
gui.win.hide();
});
});
// 从login.html -> note.html过来就没有reopen事件了?
// note.html -> login.html -> note.html, 使得两次bind
/*
if(gui.App._events) {
gui.App._events.reopen = function() {
win.show();
win.focus();
}
} else {
gui.App.on('reopen', function() {
win.show();
win.focus();
});
}
*/
$('.tool-min, .tool-min-blur').click(function() {
gui.win.minimize();
});
@@ -56,6 +41,29 @@ $(function() {
}
});
// Tray
/*
var electron = nodeRequire('electron');
var Menu = electron.remote.Menu;
var Tray = electron.remote.Tray;
var appIcon = new Tray(projectPath + '/public/images/logo/tray.png')
var contextMenu = Menu.buildFromTemplate([
{
label: '打开', click: function () {
alert(3);
}
},
{
label: '关闭', click: function () {
alert(3);
}
},
]);
appIcon.setToolTip('This is my application.')
appIcon.setContextMenu(contextMenu)
*/
});
// bind close event
@@ -73,6 +81,7 @@ function Menu() {
document.execCommand('cut');
} else {
/*
// 不知道什么原因, 可能是Chrome的原因
We don't execute document.execCommand() this time, because it is called recursively.
console.log('tinymce中没用');
setTimeout(function() {

View File

@@ -1450,6 +1450,11 @@ function initPage(initedCallback) {
});
});
ipc.send('show-tray', {
Open: getMsg('Open'),
Close: getMsg('Close')
});
// 注入前端变量#
WebService.set(Notebook, Note, Attach, Tag);

View File

@@ -39,3 +39,5 @@ var NodeFs = require('fs');
Service.dispatch = function() {};
var gui = require('gui');
// var remote = require('remote');
var projectPath = __dirname;

View File

@@ -310,5 +310,8 @@
"localAccountTips": "你好, 您使用的是本地离线帐户, 您的数据只存在本地, 不会同步到Leanote服务器, 如果您不小心删除了数据或硬盘损坏, 您的数据将永久丢失. <br>所以我们推荐您使用Leanote帐户来使用该客户端, 这样您的数据将会存储到云端, 以避免失误造成的数据丢失. <br>Leanote桌面端不再支持创建本地帐户, 但您之前创建的本地帐户还是可以继续使用.",
"Don't Show Anymore": "不再提示",
"ctrl/cmd+e Toggle Modify with Readonly": "ctrl/cmd+e 切换编辑与只读"
"ctrl/cmd+e Toggle Modify with Readonly": "ctrl/cmd+e 切换编辑与只读",
"Open": "打开",
"Close": "关闭"
}