feat: add unread notification badge to dock icon

This commit is contained in:
reya
2024-05-07 14:38:00 +07:00
parent 437cd71f7e
commit c8e1b8b8bd
6 changed files with 69 additions and 66 deletions

View File

@@ -78,7 +78,7 @@ export function Col({
}); });
} }
}; };
}, []); }, [webview]);
return ( return (
<div className="h-full w-[440px] shrink-0 p-2"> <div className="h-full w-[440px] shrink-0 p-2">

View File

@@ -120,29 +120,20 @@ function Screen() {
}, [columns]); }, [columns]);
useEffect(() => { useEffect(() => {
let unlistenColEvent: Awaited<ReturnType<typeof listen>> | undefined = const unlistenColEvent = listen<EventColumns>("columns", (data) => {
undefined; if (data.payload.type === "add") add(data.payload.column);
let unlistenWindowResize: Awaited<ReturnType<typeof listen>> | undefined = if (data.payload.type === "remove") remove(data.payload.label);
undefined; if (data.payload.type === "set_title")
updateName(data.payload.label, data.payload.title);
});
(async () => { const unlistenWindowResize = getCurrent().listen("tauri://resize", () => {
if (unlistenColEvent && unlistenWindowResize) return; startResize();
});
unlistenColEvent = await listen<EventColumns>("columns", (data) => {
if (data.payload.type === "add") add(data.payload.column);
if (data.payload.type === "remove") remove(data.payload.label);
if (data.payload.type === "set_title")
updateName(data.payload.label, data.payload.title);
});
unlistenWindowResize = await getCurrent().listen("tauri://resize", () => {
startResize();
});
})();
return () => { return () => {
if (unlistenColEvent) unlistenColEvent(); unlistenColEvent.then((f) => f());
if (unlistenWindowResize) unlistenWindowResize(); unlistenWindowResize.then((f) => f());
}; };
}, []); }, []);

View File

@@ -8,7 +8,7 @@ import {
sendNativeNotification, sendNativeNotification,
} from "@lume/utils"; } from "@lume/utils";
import { Outlet, createFileRoute } from "@tanstack/react-router"; import { Outlet, createFileRoute } from "@tanstack/react-router";
import { UnlistenFn } from "@tauri-apps/api/event"; import { invoke } from "@tauri-apps/api/core";
import { getCurrent } from "@tauri-apps/api/window"; import { getCurrent } from "@tauri-apps/api/window";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
@@ -118,50 +118,45 @@ function Bell() {
const { ark } = Route.useRouteContext(); const { ark } = Route.useRouteContext();
const { account } = Route.useParams(); const { account } = Route.useParams();
const [isRing, setIsRing] = useState(false); const [count, setCount] = useState(0);
useEffect(() => { useEffect(() => {
let unlisten: UnlistenFn = undefined; const unlisten = getCurrent().listen<string>(
"activity",
async (payload) => {
setCount((prevCount) => prevCount + 1);
await invoke("set_badge", { count });
async function listenNotify() { const event: Event = JSON.parse(payload.payload);
unlisten = await getCurrent().listen<string>( const user = await ark.get_profile(event.pubkey);
"activity", const userName =
async (payload) => { user.display_name || user.name || displayNpub(event.pubkey, 16);
setIsRing(true);
const event: Event = JSON.parse(payload.payload); switch (event.kind) {
const user = await ark.get_profile(event.pubkey); case Kind.Text: {
const userName = sendNativeNotification("Mentioned you in a note", userName);
user.display_name || user.name || displayNpub(event.pubkey, 16); break;
switch (event.kind) {
case Kind.Text: {
sendNativeNotification("Mentioned you in a note", userName);
break;
}
case Kind.Repost: {
sendNativeNotification("Reposted your note", userName);
break;
}
case Kind.ZapReceipt: {
const amount = decodeZapInvoice(event.tags);
sendNativeNotification(
`Zapped ₿ ${amount.bitcoinFormatted}`,
userName,
);
break;
}
default:
break;
} }
}, case Kind.Repost: {
); sendNativeNotification("Reposted your note", userName);
} break;
}
if (!unlisten) listenNotify(); case Kind.ZapReceipt: {
const amount = decodeZapInvoice(event.tags);
sendNativeNotification(
`Zapped ₿ ${amount.bitcoinFormatted}`,
userName,
);
break;
}
default:
break;
}
},
);
return () => { return () => {
if (unlisten) unlisten(); unlisten.then((f) => f());
}; };
}, []); }, []);
@@ -169,13 +164,13 @@ function Bell() {
<button <button
type="button" type="button"
onClick={() => { onClick={() => {
setIsRing(false); setCount(0);
ark.open_activity(account); ark.open_activity(account);
}} }}
className="relative inline-flex size-8 items-center justify-center rounded-full text-neutral-800 hover:bg-black/10 dark:text-neutral-200 dark:hover:bg-white/10" className="relative inline-flex size-8 items-center justify-center rounded-full text-neutral-800 hover:bg-black/10 dark:text-neutral-200 dark:hover:bg-white/10"
> >
<BellIcon className="size-5" /> <BellIcon className="size-5" />
{isRing ? ( {count > 0 ? (
<span className="absolute right-0 top-0 block size-2 rounded-full bg-teal-500 ring-1 ring-black/5" /> <span className="absolute right-0 top-0 block size-2 rounded-full bg-teal-500 ring-1 ring-black/5" />
) : null} ) : null}
</button> </button>

View File

@@ -18,13 +18,13 @@ export const Route = createFileRoute("/store")({
function Screen() { function Screen() {
return ( return (
<div className="flex flex-col h-full"> <div className="flex flex-col h-full">
<div className="px-3 my-2"> <div className="px-3 mt-2 mb-1">
<div className="p-1 shrink-0 inline-flex w-full rounded-lg items-center gap-1 bg-black/10 dark:bg-white/10"> <div className="p-1 shrink-0 inline-flex w-full rounded-lg items-center gap-1 bg-black/5 dark:bg-white/5">
<Link to="/store/official" className="flex-1"> <Link to="/store/official" className="flex-1">
{({ isActive }) => ( {({ isActive }) => (
<div <div
className={cn( className={cn(
"inline-flex h-9 w-full items-center justify-center gap-1.5 rounded-md text-sm font-medium leading-tight", "inline-flex h-8 w-full items-center justify-center gap-1.5 rounded-md text-sm font-medium leading-tight",
isActive ? "bg-neutral-50 dark:bg-white/10" : "opacity-50", isActive ? "bg-neutral-50 dark:bg-white/10" : "opacity-50",
)} )}
> >
@@ -37,7 +37,7 @@ function Screen() {
{({ isActive }) => ( {({ isActive }) => (
<div <div
className={cn( className={cn(
"inline-flex h-9 w-full items-center justify-center gap-1.5 rounded-md text-sm font-medium leading-tight", "inline-flex h-8 w-full items-center justify-center gap-1.5 rounded-md text-sm font-medium leading-tight",
isActive ? "bg-neutral-50 dark:bg-white/10" : "opacity-50", isActive ? "bg-neutral-50 dark:bg-white/10" : "opacity-50",
)} )}
> >

View File

@@ -1,3 +1,5 @@
#[cfg(target_os = "macos")]
use cocoa::{appkit::NSApp, base::nil, foundation::NSString};
use std::path::PathBuf; use std::path::PathBuf;
use tauri::utils::config::WindowEffectsConfig; use tauri::utils::config::WindowEffectsConfig;
use tauri::window::Effect; use tauri::window::Effect;
@@ -166,3 +168,17 @@ pub fn open_window(
Ok(()) Ok(())
} }
#[tauri::command]
pub fn set_badge(count: i32) {
#[cfg(target_os = "macos")]
unsafe {
let label = if count == 0 {
nil
} else {
NSString::alloc(nil).init_str(&format!("{}", count))
};
let dock_tile: cocoa::base::id = msg_send![NSApp(), dockTile];
let _: cocoa::base::id = msg_send![dock_tile, setBadgeLabel: label];
}
}

View File

@@ -138,7 +138,8 @@ fn main() {
commands::window::resize_column, commands::window::resize_column,
commands::window::open_window, commands::window::open_window,
commands::window::get_path, commands::window::get_path,
commands::window::navigate commands::window::navigate,
commands::window::set_badge
]) ])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application") .expect("error while running tauri application")