Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kenta #27

Open
wants to merge 20 commits into
base: bugfix
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $ npm start
```

### Option 2. pkg
[Download pkg](https://drive.google.com/open?id=1bz2Zs7MnAzgajKxF039KFqGFI02hVcJ3)
[Download pkg](https://drive.google.com/drive/folders/1yGCQRAMzCLOV0-r4-fp4iaba9AvK9jDc?usp=sharing)

## Usage
### Click tray, enter time and start
Expand Down
15 changes: 15 additions & 0 deletions block-window-renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const {ipcRenderer} = require('electron')


function sendRetrospect(event){
let retrospect = document.getElementById('retrospect').value;
ipcRenderer.send('retrospect-message', retrospect)
}

ipcRenderer.on('block-time-update', (event, arg) =>{ // Todo: Refactoring dupicated get element and consider using Jquery
document.getElementById('time').value = arg
document.getElementById('time').disabled = true
document.getElementById('submit_btn').disabled = true
})
274 changes: 144 additions & 130 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,103 +9,112 @@ const updateTray = require('./updateTray');
const calcTrayWindowXy = require('./calcTrayWindowXy');
const AutoLaunch = require('auto-launch');
const moment = require('moment');
const getPrettyTime = require('./getPrettyTime');

const ONE_MILLISEC = 1000;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.

let mainWindow, tray, trayWindow = null
let intervalObj
let min
let blockwindow, tray, trayWindow = null;
let intervalObj;
let min;
let startedTime, stopedTime;

var AutoLauncher = new AutoLaunch({
name: 'powerdoro',
path: '/Applications/powerdoro.app',
const AutoLauncher = new AutoLaunch({
name: 'powerdoro',
path: '/Applications/powerdoro.app',
})
AutoLauncher.enable();


function getExternalDisplayThreashold(){
var electronScreen = electron.screen
var displays = electronScreen.getAllDisplays()
var externalDisplay = null
for (var i in displays) {
if (displays[i].bounds.x != 0 || displays[i].bounds.y != 0) {
externalDisplay = displays[i]
break
}
const electronScreen = electron.screen;
const displays = electronScreen.getAllDisplays();
let externalDisplay = null;
for (var i in displays) {
if (displays[i].bounds.x != 0 || displays[i].bounds.y != 0) {
externalDisplay = displays[i]
break
}

return externalDisplay? {x: externalDisplay.bounds.x, y: externalDisplay.bounds.y} : {x: 0, y:0}
}
return externalDisplay? {x: externalDisplay.bounds.x, y: externalDisplay.bounds.y} : {x: 0, y:0}
}


function createBlockConcentrationWindow () {
let displayThreashold = getExternalDisplayThreashold()

let xThreshold = displayThreashold.x
let yThreshold = displayThreashold.y

let setting = {
x: xThreshold,
y: yThreshold,
fullscreen: true,
frame:false,
alwaysOnTop: true,
movable: false,
}
mainWindow = new BrowserWindow(setting)
let mainWindowPath = path.join(__dirname, 'index.html')
mainWindow.loadFile(mainWindowPath)
const displayThreashold = getExternalDisplayThreashold();

let xThreshold = displayThreashold.x;
let yThreshold = displayThreashold.y;

let setting = {
x: xThreshold,
y: yThreshold,
fullscreen: true,
frame:false,
alwaysOnTop: true,
movable: false,
}
blockwindow = new BrowserWindow(setting);
let blockwindowPath = path.join(__dirname, 'view/block-window.html');
blockwindow.loadFile(blockwindowPath);

blockwindow.setClosable(false);

// Emitted when the window is closed.
blockwindow.on('closed', function () {
blockwindow = null;
})
}

mainWindow.setClosable(false);

// Emitted when the window is closed.
mainWindow.on('closed', function () {
mainWindow = null
})
function stopTimer(){
stopedTime = moment().format('HH:mm');
trayWindow.webContents.send('stoped-timer', 'stop');
clearTimeout(intervalObj);
createBlockConcentrationWindow();
}


function stopTimer(){
trayWindow.webContents.send('stoped-timer', 'stop')
clearTimeout(intervalObj)
createBlockConcentrationWindow()
function getMilliSecFor(min, sec){
let ms = ((min * 60) + sec) * ONE_MILLISEC;
ms = Math.ceil(ms / ONE_MILLISEC) * ONE_MILLISEC; // Round up by one millisecond
return ms;
}


function startTimer(min, sec){
let ms = ((min * 60) + sec) * ONE_MILLISEC
ms = Math.ceil(ms / ONE_MILLISEC) * ONE_MILLISEC; // Round up by one millisecond
startedTime = moment().format('HH:mm');
let ms = getMilliSecFor(min, sec);
updateTray(tray, trayWindow.webContents, ms);
intervalObj = setInterval(()=>{
ms -= ONE_MILLISEC
updateTray(tray, trayWindow.webContents, ms);
intervalObj = setInterval(()=>{
ms -= ONE_MILLISEC
updateTray(tray, trayWindow.webContents, ms);
if(ms <= 0){ // Todo: Refactoring duplicated stop timer action
stopTimer()
}

}, ONE_MILLISEC)
}
if(ms <= 0){ // Todo: Refactoring duplicated stop timer action
stopTimer()
}

}, ONE_MILLISEC)
}


const createTray = () => {
let iconPath = path.join(__dirname, 'appicon.png')
tray = new Tray(iconPath)
tray.on('click', function (event) {
toggleWindow()
})
let iconPath = path.join(__dirname, 'appicon.png')
tray = new Tray(iconPath)
tray.on('click', function (event) {
toggleWindow()
})
}


const platforms = {
darwin: {
calcRelativeY: (trayBounds) => Math.round(trayBounds.y + trayBounds.height + 3),
hide: (app) => app.dock.hide(),
quit: (app) => app.quit(),
},
darwin: {
calcRelativeY: (trayBounds) => Math.round(trayBounds.y + trayBounds.height + 3),
hide: (app) => app.dock.hide(),
quit: (app) => app.quit(),
},
win32: {
calcRelativeY: (trayBounds) => trayBounds.y - (3 + 120), //Todo: Extract constant and replace to trayWindow's height
calcRelativeY: (trayBounds) => trayBounds.y - (3 + 120), //Todo: Extract constant and replace to trayWindow's height
hide: (app) => {},
quit: (app) => {},
}
Expand All @@ -114,77 +123,82 @@ const platforms = {

// Creates window & specifies its values
const createTrayWindow = () => {
trayWindow = new BrowserWindow({
width: 220,
height: 160,
show: false,
frame: false,
fullscreenable: false,
resizable: false,
transparent: true,
movable: false,
closable: false,
'node-integration': false
})
// This is where the index.html file is loaded into the window
trayWindow.loadURL('file://' + __dirname + '/menu.html');

// Hide the window when it loses focus
trayWindow.on('blur', () => {
if (!trayWindow.webContents.isDevToolsOpened()) {
trayWindow.hide()
}
})
trayWindow = new BrowserWindow({
width: 220,
height: 160,
show: false,
frame: false,
fullscreenable: false,
resizable: false,
transparent: true,
movable: false,
closable: false,
'node-integration': false
})

trayWindow.loadURL('file://' + __dirname + '/view/tray-window.html');

// Hide the window when it loses focus
trayWindow.on('blur', () => {
if (!trayWindow.webContents.isDevToolsOpened()) {
trayWindow.hide()
}
})
}


const toggleWindow = () => {
if (trayWindow.isVisible()) {
trayWindow.hide()
} else {
showTrayWindow()
}
if (trayWindow.isVisible()) {
trayWindow.hide()
} else {
showTrayWindow()
}
}


const showTrayWindow = () => {
const position = calcTrayWindowXy(
platforms[process.platform].calcRelativeY(tray.getBounds()),
tray.getBounds(),
trayWindow.getBounds().width,
getExternalDisplayThreashold().y
);

trayWindow.setPosition(position.x, position.y, false)
trayWindow.show()
trayWindow.focus()
const position = calcTrayWindowXy(
platforms[process.platform].calcRelativeY(tray.getBounds()),
tray.getBounds(),
trayWindow.getBounds().width,
getExternalDisplayThreashold().y
);

trayWindow.setPosition(position.x, position.y, false)
trayWindow.show()
trayWindow.focus()
}


ipcMain.on('asynchronous-message', (event, arg) => {
min = arg
startTimer(arg, 0)
trayWindow.hide();
})


var appendRetrospect = function(retrospect) {
let retroDirPath = path.join(homedir + '/Desktop/retrospect/')
if(!fs.existsSync(retroDirPath)){
fs.mkdir(retroDirPath)
let retroDirPath = path.join(homedir + '/Desktop/retrospect/')
if(!fs.existsSync(retroDirPath)){
fs.mkdir(retroDirPath)
}
let retroPath = path.join(retroDirPath + moment().format('YYYY_MM_DD') + '.txt') //Todo: Refacor with es5 syntax

let ms = getMilliSecFor(min, 0)
let prettyTime = getPrettyTime(ms)

let history = `[${startedTime}-${stopedTime}] [${prettyTime}] : ${retrospect}`
fs.appendFile(retroPath, history + '\n', (err)=>{
if(err){
console.log(err)
throw err
}
let retroPath = path.join(retroDirPath + moment().format('YYYY_MM_DD') + '.txt') //Todo: Refacor with es5 syntax
let history = min + ' : ' + retrospect
fs.appendFile(retroPath, history + '\n', (err)=>{
if(err){
console.log(err)
throw err
}
})
mainWindow.setClosable(true)
mainWindow.close()
})
blockwindow.setClosable(true)
blockwindow.close()
}


ipcMain.on('asynchronous-message', (event, arg) => {
min = arg
startTimer(arg, 0)
trayWindow.hide();
})


ipcMain.on('retrospect-message', (event, arg) => {
appendRetrospect(arg)
})
Expand All @@ -197,31 +211,31 @@ ipcMain.on('stop-message', (event, arg) => {


ipcMain.on('exit-app', (event, arg) =>{
app.exit()
app.exit()
})


platforms[process.platform].hide(app);


app.on('ready', ()=>{
createTray()
createTrayWindow()
createTray()
createTrayWindow()
})


// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
platforms[process.platform].quit(app);
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
platforms[process.platform].quit(app);
})


app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createBlockConcentrationWindow()
}
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (blockwindow === null) {
createBlockConcentrationWindow()
}
})
Loading