Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
feat(navigation): show notification badge on tab if it has news
Browse files Browse the repository at this point in the history
  • Loading branch information
herteleo committed Apr 20, 2019
1 parent 3f2b3bd commit 827760f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/components/TabMain.vue
Expand Up @@ -14,6 +14,7 @@
<tab-webview
ref="webview"
:item="item"
:is-active="isActive"
/>
</div>
</template>
Expand Down Expand Up @@ -44,6 +45,11 @@ export default {
watch: {
isActive(isActive) {
if (isActive) {
this.$store.commit('Pages/setState', {
tabId: this.item.id,
data: { hasNotificationBadge: false },
});
this.webview.focus();
}
},
Expand Down
33 changes: 33 additions & 0 deletions src/components/TabWebview.vue
Expand Up @@ -19,6 +19,15 @@ export default {
type: Object,
default: () => ({}),
},
isActive: {
type: Boolean,
required: true,
},
},
data() {
return {
isLoaded: false,
};
},
computed: {
muteOnWindowBlur() {
Expand All @@ -44,6 +53,14 @@ export default {
},
},
mounted() {
this.webview.addEventListener('did-stop-loading', () => {
this.isLoaded = true;
});
this.webview.addEventListener('did-start-loading', () => {
this.isLoaded = false;
});
this.webview.addEventListener('ipc-message', (event) => {
if (event.channel !== 'notification') return;
Expand All @@ -54,6 +71,8 @@ export default {
tabId: this.item.id,
});
this.evaluateIfHasNotifications();
if (!this.notificationsPreventOnBlur || this.windowHasFocus) {
new Notification(notification.title, notification.options); // eslint-disable-line no-new
}
Expand All @@ -64,6 +83,8 @@ export default {
tabId: this.item.id,
data: { title },
});
this.evaluateIfHasNotifications();
});
this.webview.addEventListener('page-favicon-updated', ({ favicons }) => {
Expand All @@ -73,6 +94,8 @@ export default {
tabId: this.item.id,
data: { favicon },
});
this.evaluateIfHasNotifications();
});
this.webview.addEventListener('dom-ready', (view) => {
Expand All @@ -92,6 +115,16 @@ export default {
}
});
},
methods: {
evaluateIfHasNotifications() {
this.$store.commit('Pages/setState', {
tabId: this.item.id,
data: {
hasNotificationBadge: this.isLoaded && !this.isActive,
},
});
},
},
};
</script>

Expand Down
9 changes: 9 additions & 0 deletions src/components/TabsNavItem.vue
Expand Up @@ -5,6 +5,10 @@
:active-class="$style.active"
tag="button"
>
<div
v-if="pageState.hasNotificationBadge"
:class="$style.badge"
/>
<div
:class="{
[$style.thumb]: true,
Expand Down Expand Up @@ -59,6 +63,11 @@ export default {
@apply text-grey-light bg-grey-darkest shadow;
}
.badge {
@apply absolute pin-l w-2 h-2 bg-grey-darker rounded-full;
transform: translateX(-50%);
}
.thumb {
@apply
flex flex-no-shrink justify-center items-center
Expand Down
8 changes: 7 additions & 1 deletion src/store/modules/Pages.js
Expand Up @@ -12,7 +12,13 @@ export default {
setState({ pages }, { tabId, data = {} }) {
const pageIndex = pages.findIndex(page => page.tabId === tabId);
const pageState = pages[pageIndex] || {};
const newState = { ...pageState, ...data };

const newState = {
title: '',
hasNotificationBadge: false,
...pageState,
...data,
};

if (pageIndex !== -1) {
this._vm.$set(pages, pageIndex, newState);
Expand Down

0 comments on commit 827760f

Please sign in to comment.