Compare commits

...

6 commits

Author SHA1 Message Date
Leah
453c860ae2 merge: Feature/favicon notification dot (!474)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/474
2024-03-30 17:22:23 +00:00
KevinWh0
459e684117 fixed some of the issues with it 2024-03-19 22:00:28 +01:00
KevinWh0
ccf5659ac3 made methods private 2024-03-16 01:34:20 +01:00
KevinWh0
8f300cf460 added setting 2024-03-16 01:23:02 +01:00
KevinWh0
395ea9ab9f made notification dot appear if you load the page and there are already notifs 2024-03-16 01:08:08 +01:00
KevinWh0
a0c63c8cc4 added notification dot and it seems to work well 2024-03-16 00:27:26 +01:00
7 changed files with 87 additions and 1 deletions

View file

@ -688,6 +688,7 @@ channel: "Channels"
create: "Create"
notificationSetting: "Notification settings"
notificationSettingDesc: "Select the types of notification to display."
enableFaviconNotificationDot: "Enable favicon notification dot"
useGlobalSetting: "Use global settings"
useGlobalSettingDesc: "If turned on, your account's notification settings will be used. If turned off, individual configurations can be made."
other: "Other"

4
locales/index.d.ts vendored
View file

@ -2764,6 +2764,10 @@ export interface Locale extends ILocale {
*
*/
"notificationSettingDesc": string;
/**
*
*/
"enableFaviconNotificationDot": string;
/**
* 使
*/

View file

@ -687,6 +687,7 @@ channel: "チャンネル"
create: "作成"
notificationSetting: "通知設定"
notificationSettingDesc: "表示する通知の種別を選択してください。"
enableFaviconNotificationDot: "ファビコン通知ドットを有効にする"
useGlobalSetting: "グローバル設定を使う"
useGlobalSettingDesc: "オンにすると、アカウントの通知設定が使用されます。オフにすると、個別に設定できるようになります。"
other: "その他"

View file

@ -180,6 +180,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<div class="_gaps_m">
<div class="_gaps_s">
<MkSwitch v-model="enableFaviconNotificationDot">{{ i18n.ts.enableFaviconNotificationDot }}</MkSwitch>
<MkSwitch v-model="warnMissingAltText">{{ i18n.ts.warnForMissingAltText }}</MkSwitch>
<MkSwitch v-model="imageNewTab">{{ i18n.ts.openImageInNewTab }}</MkSwitch>
<MkSwitch v-model="useReactionPickerForContextMenu">{{ i18n.ts.useReactionPickerForContextMenu }}</MkSwitch>
@ -337,6 +338,7 @@ const oneko = computed(defaultStore.makeGetterSetter('oneko'));
const loadRawImages = computed(defaultStore.makeGetterSetter('loadRawImages'));
const highlightSensitiveMedia = computed(defaultStore.makeGetterSetter('highlightSensitiveMedia'));
const imageNewTab = computed(defaultStore.makeGetterSetter('imageNewTab'));
const enableFaviconNotificationDot = computed(defaultStore.makeGetterSetter('enableFaviconNotificationDot'));
const warnMissingAltText = computed(defaultStore.makeGetterSetter('warnMissingAltText'));
const nsfw = computed(defaultStore.makeGetterSetter('nsfw'));
const showFixedPostForm = computed(defaultStore.makeGetterSetter('showFixedPostForm'));

View file

@ -72,6 +72,7 @@ const defaultStoreSaveKeys: (keyof typeof defaultStore['state'])[] = [
'advancedMfm',
'loadRawImages',
'warnMissingAltText',
'enableFaviconNotificationDot',
'imageNewTab',
'dataSaver',
'disableShowingAnimatedImages',

View file

@ -268,6 +268,10 @@ export const defaultStore = markRaw(new Storage('base', {
where: 'device',
default: true,
},
enableFaviconNotificationDot: {
where: 'device',
default: true,
},
imageNewTab: {
where: 'device',
default: false,

View file

@ -47,7 +47,7 @@ SPDX-License-Identifier: AGPL-3.0-only
</template>
<script lang="ts" setup>
import { defineAsyncComponent, ref } from 'vue';
import { defineAsyncComponent, ref, watch } from 'vue';
import * as Misskey from 'misskey-js';
import { swInject } from './sw-inject.js';
import XNotification from './notification.vue';
@ -70,6 +70,74 @@ const dev = _DEV_;
const notifications = ref<Misskey.entities.Notification[]>([]);
class NotificationFavIconDot {
canvas : HTMLCanvasElement;
src : string | null = null;
ctx : CanvasRenderingContext2D | null = null;
favconImage : HTMLImageElement | null = null;
faviconEL : HTMLLinkElement;
hasLoaded : Promise;
constructor() {
this.canvas = document.createElement('canvas');
this.faviconEL = document.querySelector<HTMLLinkElement>('link[rel$=icon]') ?? this._createFaviconElem();
this.src = this.faviconEL.getAttribute('href');
this.ctx = this.canvas.getContext('2d');
this.favconImage = document.createElement('img');
this.favconImage.src = this.faviconEL.href;
this.hasLoaded = new Promise((resolve, reject) => {
this.favconImage.onload = () => {
this.canvas.width = this.favconImage.width;
this.canvas.height = this.favconImage.height;
// resolve();
setTimeout(() => resolve(), 500);
};
});
}
private _createFaviconElem() {
const newLink = document.createElement('link');
newLink.rel = 'icon';
newLink.href = '/favicon.ico';
document.head.appendChild(newLink);
return newLink;
}
private _drawIcon() {
if (!this.ctx || !this.favconImage) return;
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.drawImage(this.favconImage, 0, 0, this.favconImage.width, this.favconImage.height);
}
private _drawDot() {
if (!this.ctx || !this.favconImage) return;
this.ctx.beginPath();
this.ctx.arc(this.favconImage.width - 10, 10, 10, 0, 2 * Math.PI);
this.ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue('--navIndicator');
this.ctx.strokeStyle = 'white';
this.ctx.fill();
this.ctx.stroke();
}
private _setFavicon() {
this.faviconEL.href = this.canvas.toDataURL('image/png');
}
async setVisible(isVisible : boolean) {
//Wait for it to have loaded the icon
await this.hasLoaded;
console.log(this.hasLoaded);
this._drawIcon();
if (isVisible) this._drawDot();
this._setFavicon();
}
}
const notificationDot = new NotificationFavIconDot();
function onNotification(notification: Misskey.entities.Notification, isClient = false) {
if (document.visibilityState === 'visible') {
if (!isClient && notification.type !== 'test') {
@ -93,6 +161,11 @@ function onNotification(notification: Misskey.entities.Notification, isClient =
if ($i) {
const connection = useStream().useChannel('main', null, 'UI');
connection.on('notification', onNotification);
watch(() => $i?.hasUnreadNotification, (hasAny) => notificationDot.setVisible((defaultStore.state.enableFaviconNotificationDot ? hasAny : false) ?? false));
if ($i.hasUnreadNotification && defaultStore.state.enableFaviconNotificationDot) notificationDot.setVisible(true);
globalEvents.on('clientNotification', notification => onNotification(notification, true));
//#region Listen message from SW