Skip to content

Commit

Permalink
refactor: improve tray-app (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlienard authored Sep 9, 2023
1 parent 42e6ba5 commit aae9d8a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ fn main() {
if !is_focused && window.label() == "tray" {
window.hide().unwrap();
}
commands::update_tray(window.app_handle(), None, Some(false))
}
_ => {}
})
Expand Down
3 changes: 3 additions & 0 deletions src/lib/components/dashboard/Main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
$: if (browser && window.__TAURI__) {
emit('loading', { loading: $loading });
}
$: if (browser && window.__TAURI__) {
emit('settings', { settings: $settings });
}
// Filter events
$: pinned = notifications.filter((item) => item.pinned && !item.done);
Expand Down
6 changes: 0 additions & 6 deletions src/routes/(app)/dashboard/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@
}, 60000);
}
function handleFocus() {
window.__TAURI__ && invoke('update_tray', { newIcon: false });
}
$: if (mounted && $githubNotifications.length) {
// Save events ids to storage
const toSave = $githubNotifications.map(
Expand Down Expand Up @@ -181,7 +177,6 @@
}
onMount(async () => {
window.addEventListener('focus', handleFocus);
window.addEventListener('refetch', refetch);
$savedNotifications = storage.get('github-notifications') || [];
Expand All @@ -194,7 +189,6 @@
onDestroy(() => {
if (mounted) {
window.removeEventListener('focus', handleFocus);
window.removeEventListener('refetch', refetch);
}
Expand Down
40 changes: 28 additions & 12 deletions src/routes/(tray)/tray/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,41 +1,57 @@
<script lang="ts">
import { listen, type UnlistenFn } from '@tauri-apps/api/event';
import { onDestroy, onMount } from 'svelte';
import { onDestroy, onMount, SvelteComponent } from 'svelte';
import { browser } from '$app/environment';
import { NotificationList, ScrollbarContainer } from '$lib/components';
import { loading } from '$lib/stores';
import type { NotificationData } from '$lib/types';
import { loading, settings } from '$lib/stores';
import type { NotificationData, Settings } from '$lib/types';
let notifications: NotificationData[] = [];
let scrollContainer: SvelteComponent;
let unlistenNotification: UnlistenFn = () => null;
let unlistenLoading: UnlistenFn = () => null;
let unlistenSettings: UnlistenFn = () => null;
$: isTrayApp = browser && window.__TAURI__ && window.location.pathname === '/tray';
function handleFocus() {
scrollContainer?.scrollTo({ top: 0 });
}
onMount(async () => {
if (isTrayApp) {
unlistenNotification = await listen<{ notifications: NotificationData[] }>(
'notifications',
(event) => {
window.addEventListener('focus', handleFocus);
const [a, b, c] = await Promise.all([
listen<{ notifications: NotificationData[] }>('notifications', (event) => {
notifications = event.payload.notifications;
}
);
unlistenLoading = await listen<{ loading: boolean }>('loading', (event) => {
$loading = event.payload.loading;
});
}),
listen<{ loading: boolean }>('loading', (event) => {
$loading = event.payload.loading;
}),
listen<{ settings: Settings }>('settings', (event) => {
$settings = event.payload.settings;
})
]);
unlistenNotification = a;
unlistenLoading = b;
unlistenSettings = c;
}
});
onDestroy(() => {
if (isTrayApp) {
window.removeEventListener('focus', handleFocus);
unlistenNotification();
unlistenLoading();
unlistenSettings();
}
});
</script>

<main class="main">
<ScrollbarContainer margin="2rem 1rem">
<ScrollbarContainer margin="2rem 1rem" bind:this={scrollContainer}>
<NotificationList {notifications} scrollShadow={false} />
</ScrollbarContainer>
</main>
Expand Down

0 comments on commit aae9d8a

Please sign in to comment.