Skip to content

Commit

Permalink
Show system notifications, based on a setting
Browse files Browse the repository at this point in the history
  • Loading branch information
cbos committed Nov 23, 2022
1 parent 9bffa6c commit a8fe8f2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface PomoSettings {
ribbonIcon: boolean;
emoji: boolean;
notificationSound: boolean;
useSystemNotification: boolean;
backgroundNoiseFile: string;
logging: boolean;
logFile: string;
Expand All @@ -34,6 +35,7 @@ export const DEFAULT_SETTINGS: PomoSettings = {
ribbonIcon: true,
emoji: true,
notificationSound: true,
useSystemNotification: false,
backgroundNoiseFile: "",
logging: false,
logFile: "Pomodoro Log.md",
Expand Down Expand Up @@ -149,6 +151,15 @@ export class PomoSettingTab extends PluginSettingTab {
this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName("System notification")
.setDesc("Use system notifications at the end of each pomodoro and break")
.addToggle(toggle => toggle
.setValue(this.plugin.settings.useSystemNotification)
.onChange(value => {
this.plugin.settings.useSystemNotification = value;
this.plugin.saveSettings();
}));


/************** Sound settings **************/
Expand Down
39 changes: 39 additions & 0 deletions src/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { WhiteNoise } from './white_noise';
import { PomoSettings } from './settings';
import PomoTimerPlugin from './main';

const electron = require("electron");

const MILLISECS_IN_MINUTE = 60 * 1000;

export const enum Mode {
Expand Down Expand Up @@ -90,6 +92,9 @@ export class Timer {
if (this.settings.notificationSound === true) { //play sound end of timer
playNotification();
}
if (this.settings.useSystemNotification === true) { //show system notification end of timer
showSystemNotification(this.mode, this.settings.emoji);
}

if (this.settings.autostartTimer === false && this.settings.numAutoCycles <= this.cyclesSinceLastAutoStop) { //if autostart disabled, pause and allow user to start manually
this.setupTimer();
Expand Down Expand Up @@ -325,6 +330,40 @@ function playNotification(): void {
audio.play();
}

function showSystemNotification(mode:Mode, useEmoji:boolean): void {
let text = "";
switch (mode) {
case (Mode.Pomo): {
let emoji = useEmoji ? "🏖" : ""
text = `End of the pomodoro, time to take a break ${emoji}`;
break;
}
case (Mode.ShortBreak):
case (Mode.LongBreak): {
let emoji = useEmoji ? "🍅" : ""
text = `End of the break, time for the next pomodoro ${emoji}`;
break;
}
case (Mode.NoTimer): {
// no system notification needed
return;
}
}
let emoji = useEmoji ? "🍅" : ""
let title = `Obsidian Pomodoro ${emoji}`;

// Show system notification
const Notification = (electron as any).remote.Notification;
const n = new Notification({
title: title,
body: text
});
n.on("click", () => {
n.close();
});
n.show();
}

export async function getDailyNoteFile(): Promise<TFile> {
const file = getDailyNote(moment(), getAllDailyNotes());

Expand Down

0 comments on commit a8fe8f2

Please sign in to comment.