-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
Feature/1457 add shorcuts for pause breaks #1468
Changes from 4 commits
8c81987
07ddfe2
5947189
ee9665f
e41c051
74acca6
ebf1e51
a6367fd
be9d708
eb111ae
8e144d8
227f8a4
cbbb4fe
ffd88e6
0102f91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const { UntilMorning } = require('./untilMorning') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please what do you think about registering Or even put all shortcuts here? (like skipToNextScheduledBreakShortcut, skipToNextMiniBreakShortcut and such). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
const intervals = { | ||
pauseBreaksFor30MinutesShortcut: 30 * 60 * 1000, | ||
pauseBreaksFor1HourShortcut: 3600 * 1000, | ||
pauseBreaksFor2HoursShortcut: 2 * 3600 * 1000, | ||
pauseBreaksFor5HoursShortcut: 5 * 3600 * 1000, | ||
pauseBreaksUntilMorningShortcut: null | ||
} | ||
|
||
function calculateInterval (name, settings) { | ||
if (name === 'pauseBreaksUntilMorningShortcut') { | ||
return new UntilMorning(settings).msToSunrise() | ||
} | ||
|
||
return intervals[name] | ||
} | ||
|
||
function setupBreak (name, shortcutText, settings, pauseBreaks, log, globalShortcut) { | ||
const shortcut = globalShortcut.register(shortcutText, () => { | ||
const interval = calculateInterval(name, settings) | ||
pauseBreaks(interval) | ||
}) | ||
|
||
if (shortcut) { | ||
log.info(`Stretchly: ${name} registration successful (${shortcutText})`) | ||
} else { | ||
log.warn(`Stretchly: ${name} registration failed`) | ||
} | ||
} | ||
|
||
function registerPauseBreaksShortcuts (settings, pauseBreaks, log, globalShortcut) { | ||
for (const name of Object.keys(intervals)) { | ||
const shortcutText = settings.get(name) | ||
if (shortcutText === '') continue | ||
setupBreak(name, shortcutText, settings, pauseBreaks, log, globalShortcut) | ||
} | ||
} | ||
|
||
module.exports = { | ||
calculateInterval, | ||
registerPauseBreaksShortcuts, | ||
setupBreak | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { vi } from 'vitest' | ||
import { expect } from 'chai' | ||
import { calculateInterval, registerPauseBreaksShortcuts, setupBreak } from '../app/utils/pauseBreaksShortcut' | ||
|
||
describe('pauseBreaksShortcut', () => { | ||
describe('calculateInterval', () => { | ||
it('should return correct interval for pauseBreaksFor30MinutesShortcut', () => { | ||
const interval = calculateInterval('pauseBreaksFor30MinutesShortcut') | ||
expect(interval).toBe(30 * 60 * 1000) | ||
}) | ||
|
||
it('should return correct interval for pauseBreaksFor1HourShortcut', () => { | ||
const interval = calculateInterval('pauseBreaksFor1HourShortcut') | ||
expect(interval).toBe(3600 * 1000) | ||
}) | ||
|
||
it('should return correct interval for pauseBreaksFor2HoursShortcut', () => { | ||
const interval = calculateInterval('pauseBreaksFor2HoursShortcut') | ||
expect(interval).toBe(2 * 3600 * 1000) | ||
}) | ||
|
||
it('should return correct interval for pauseBreaksFor5HoursShortcut', () => { | ||
const interval = calculateInterval('pauseBreaksFor5HoursShortcut') | ||
expect(interval).toBe(5 * 3600 * 1000) | ||
}) | ||
|
||
it('should return correct interval for pauseBreaksUntilMorningShortcut', () => { | ||
const settings = { get: () => 6 } | ||
const interval = calculateInterval('pauseBreaksUntilMorningShortcut', settings) | ||
expect(interval).toBeGreaterThan(0) | ||
expect(interval).toBeLessThan(24 * 60 * 60 * 1000) | ||
}) | ||
}) | ||
|
||
describe('setupBreak', () => { | ||
it('should register a shortcut and call pauseBreaks with the correct interval', () => { | ||
const globalShortcut = { register: vi.fn().mockReturnValue(true) } | ||
const log = { info: vi.fn(), warn: vi.fn() } | ||
const pauseBreaks = vi.fn() | ||
|
||
setupBreak('pauseBreaksFor30MinutesShortcut', 'Ctrl+Shift+P', null, pauseBreaks, log, globalShortcut) | ||
|
||
expect(globalShortcut.register).toHaveBeenCalledWith('Ctrl+Shift+P', expect.any(Function)) | ||
globalShortcut.register.mock.calls[0][1]() | ||
expect(pauseBreaks).toHaveBeenCalledWith(30 * 60 * 1000) | ||
expect(log.info).toHaveBeenCalledWith('Stretchly: pauseBreaksFor30MinutesShortcut registration successful (Ctrl+Shift+P)') | ||
}) | ||
|
||
it('should log a warning if shortcut registration fails', () => { | ||
const globalShortcut = { register: vi.fn().mockReturnValue(false) } | ||
const log = { info: vi.fn(), warn: vi.fn() } | ||
const pauseBreaks = vi.fn() | ||
|
||
setupBreak('pauseBreaksFor30MinutesShortcut', 'Ctrl+Shift+P', null, pauseBreaks, log, globalShortcut) | ||
|
||
expect(log.warn).toHaveBeenCalledWith('Stretchly: pauseBreaksFor30MinutesShortcut registration failed') | ||
}) | ||
}) | ||
|
||
describe('registerPauseBreaksShortcuts', () => { | ||
it('should register all shortcuts from settings', () => { | ||
const globalShortcut = { register: vi.fn().mockReturnValue(true) } | ||
const log = { info: vi.fn(), warn: vi.fn() } | ||
const pauseBreaks = vi.fn() | ||
const settings = { get: vi.fn((name) => name === 'pauseBreaksFor30MinutesShortcut' ? 'Ctrl+Shift+P' : '') } | ||
|
||
registerPauseBreaksShortcuts(settings, pauseBreaks, log, globalShortcut) | ||
|
||
expect(globalShortcut.register).toHaveBeenCalledWith('Ctrl+Shift+P', expect.any(Function)) | ||
expect(log.info).toHaveBeenCalledWith('Stretchly: pauseBreaksFor30MinutesShortcut registration successful (Ctrl+Shift+P)') | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess not that important but returns
[18960:0723/192752.999383:WARNING:accelerator_util.cc(65)] doesn't contain a valid key
when no value set.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well spotted. How do I reproduce this error? I tried to start the app (
npm run start
) with empty string""
values for the shortcuts but I don't see this error in the terminal.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try with
npm run dev
, it gives more debug info.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, the warning only appeared in Ubuntu for me but not in Windows or Mac.