Skip to content

Commit b6c2a70

Browse files
committed
Add native notificaiton support
1 parent 12a584a commit b6c2a70

File tree

13 files changed

+235
-33
lines changed

13 files changed

+235
-33
lines changed

src-tauri/Cargo.lock

Lines changed: 137 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ tauri-build = { version = "1.2", features = [] }
1616
[dependencies]
1717
serde_json = "1.0"
1818
serde = { version = "1.0", features = ["derive"] }
19-
tauri = { version = "1.2.4", features = ["http-request", "icon-ico", "icon-png", "macos-private-api", "process-exit", "shell-open", "system-tray"] }
19+
tauri = { version = "1.2.4", features = ["dialog-confirm", "http-request", "icon-ico", "icon-png", "macos-private-api", "notification-all", "process-exit", "shell-open", "system-tray"] }
2020
window-vibrancy = "0.3.2"
2121
tiny_http = "0.12.0"
2222
typed-html = { git = "https://github.com/bodil/typed-html", rev = "4c13ecca" }

src-tauri/src/commands.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,17 @@ pub fn stop_server(window: Window, state: State<'_, Mutex<AuthServer>>) {
4646
let mut server = state.lock().unwrap();
4747
server.stop();
4848
}
49+
50+
#[cfg(target_os = "macos")]
51+
#[tauri::command]
52+
pub fn go_to_notification_settings() {
53+
std::process::Command::new("open")
54+
.arg("x-apple.systempreferences:com.apple.preference.notifications")
55+
.spawn();
56+
}
57+
58+
#[cfg(target_os = "windows")]
59+
#[tauri::command]
60+
pub fn go_to_notification_settings() {
61+
std::process::Command::new("ms-settings:notifications").spawn();
62+
}

src-tauri/src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ mod server;
99

1010
use std::sync::Mutex;
1111

12-
use commands::{play_notification_sound, set_icon_template, start_server, stop_server};
12+
use commands::{
13+
go_to_notification_settings, play_notification_sound, set_icon_template, start_server,
14+
stop_server,
15+
};
1316
use server::AuthServer;
1417

1518
use tauri::{
@@ -92,7 +95,8 @@ fn main() {
9295
play_notification_sound,
9396
set_icon_template,
9497
start_server,
95-
stop_server
98+
stop_server,
99+
go_to_notification_settings
96100
])
97101
.plugin(tauri_plugin_autostart::init(
98102
MacosLauncher::LaunchAgent,

src-tauri/tauri.conf.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@
2424
"process": {
2525
"exit": true
2626
},
27+
"notification": {
28+
"all": true
29+
},
2730
"shell": {
2831
"open": true
32+
},
33+
"dialog": {
34+
"confirm": true
2935
}
3036
},
3137
"bundle": {

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export enum InvokeCommand {
1717
SetIconTemplate = 'set_icon_template',
1818
StartServer = 'start_server',
1919
StopServer = 'stop_server',
20+
GoToNotificationSettings = 'go_to_notification_settings',
2021
}
2122

2223
export enum NotificationSubject {

src/main.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createPinia } from 'pinia'
66
import { isEnabled as isAutostartEnabled } from 'tauri-plugin-autostart-api'
77
import dayjs from 'dayjs'
88
import relativeTime from 'dayjs/plugin/relativeTime'
9+
import { isPermissionGranted } from '@tauri-apps/api/notification'
910
import App from './App.vue'
1011
import { AppStorage, cacheStorageFromDisk } from './storage'
1112
import { useStore } from './stores/store'
@@ -15,7 +16,7 @@ import { Page } from './constants'
1516
import { getReleases } from './api/releases'
1617
import { getNewRelease } from './utils/getNewRelease'
1718

18-
(async () => {
19+
;(async () => {
1920
dayjs.extend(relativeTime)
2021
window.addEventListener('contextmenu', e => e.preventDefault())
2122

@@ -30,7 +31,12 @@ import { getNewRelease } from './utils/getNewRelease'
3031
const token = AppStorage.get('accessToken')
3132
const user = AppStorage.get('user')
3233

33-
AppStorage.set('openAtStartup', await isAutostartEnabled())
34+
const [autoStartEnabled, notificationsGranted] = await Promise.all([isAutostartEnabled(), isPermissionGranted()])
35+
36+
AppStorage.set('openAtStartup', autoStartEnabled)
37+
38+
if (!notificationsGranted)
39+
AppStorage.set('showSystemNotifications', false)
3440

3541
if (token && user) {
3642
store.setPage(Page.Home)

src/pages/SettingsPage.vue

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { exit } from '@tauri-apps/api/process'
44
import { invoke } from '@tauri-apps/api/tauri'
55
import { disable as disableAutostart, enable as enableAutostart } from 'tauri-plugin-autostart-api'
66
import { watchDebounced } from '@vueuse/core'
7+
import { requestPermission } from '@tauri-apps/api/notification'
8+
import { confirm } from '@tauri-apps/api/dialog'
79
import AppButton from '../components/AppButton.vue'
810
import PageHeader from '../components/PageHeader.vue'
911
import SettingsItem from '../components/SettingsItem.vue'
@@ -57,6 +59,29 @@ function handleBack() {
5759
}
5860
5961
useKey('esc', handleBack, { prevent: true })
62+
63+
const showSystemNotifications = AppStorage.asRef('showSystemNotifications')
64+
async function handleUpdateShowSystemNotifications(value: boolean) {
65+
if (!value) {
66+
showSystemNotifications.value = false
67+
return
68+
}
69+
70+
const permission = await requestPermission()
71+
72+
if (permission !== 'granted') {
73+
const confirmed = await confirm('Gitification has no permission to show notifications. Press Ok to go to Preferences/Notifications.', {
74+
type: 'error',
75+
})
76+
77+
if (confirmed)
78+
invoke(InvokeCommand.GoToNotificationSettings)
79+
80+
return
81+
}
82+
83+
showSystemNotifications.value = value
84+
}
6085
</script>
6186

6287
<template>
@@ -93,6 +118,11 @@ useKey('esc', handleBack, { prevent: true })
93118
v-model:enabled="showReadNotifications"
94119
title="Show read notifications"
95120
/>
121+
<SettingsItem
122+
:enabled="showSystemNotifications"
123+
title="Show system notifications"
124+
@update:enabled="handleUpdateShowSystemNotifications"
125+
/>
96126
</div>
97127

98128
<div class="settings-footer">

0 commit comments

Comments
 (0)