Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an unread counter in the favicon #267

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>{{ unreadItems > 0 ? '(' + unreadItems + ') ' : '' }}MailDev</title>
<title>MailDev{{ unreadItems > 0 ? ' (' + unreadItems + ')' : '' }}</title>
<meta name="description" content="SMTP Server + Web Interface for viewing and testing emails during development.">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="styles/style.css">
<link rel="icon" id="favicon" />
</head>
<body>

Expand Down
5 changes: 3 additions & 2 deletions app/scripts/controllers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

app.controller('MainCtrl', [
'$scope', '$rootScope', '$http', 'Email', '$route', '$location',
function ($scope, $rootScope, $http, Email, $route, $location) {
'$scope', '$rootScope', '$http', 'Email', '$route', '$location', 'Favicon',
function ($scope, $rootScope, $http, Email, $route, $location, Favicon) {
$scope.items = []
$scope.configOpen = false
$scope.currentItemId = null
Expand All @@ -17,6 +17,7 @@ app.controller('MainCtrl', [
$scope.unreadItems = $scope.items.filter(function (email) {
return !email.read
}).length
Favicon.setUnreadCount($scope.unreadItems)
}

// Load all emails
Expand Down
53 changes: 53 additions & 0 deletions app/scripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,57 @@ app.service('Email', ['$resource', function ($resource) {
params: {}
}
})
}]).service('Favicon', [function () {
const favicon = document.getElementById('favicon')
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
const bufferImage = new window.Image()

var lastUnreadCount = 0
const faviconSize = 16
const pos = {
x: faviconSize - faviconSize / 3,
y: faviconSize - faviconSize / 3
}

bufferImage.src = './favicon.ico'
bufferImage.onload = function () {
setUnreadCount(lastUnreadCount)
}

canvas.width = canvas.height = faviconSize

const drawCircle = function (ctx, pos) {
ctx.beginPath()
ctx.arc(pos.x, pos.y, faviconSize / 2.4, 0, 2 * Math.PI)
ctx.fillStyle = '#d00'
ctx.fill()
ctx.stroke()
}

const drawText = function (ctx, pos, text) {
ctx.font = 'bold 9px "helvetica", sans-serif'
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
ctx.fillStyle = '#FFFFFF'
ctx.fillText(text, pos.x, pos.y)
}

const setUnreadCount = function (unreadCount) {
lastUnreadCount = unreadCount
if (unreadCount === 0) {
favicon.href = bufferImage.src
return
}

context.drawImage(bufferImage, 0, 0, faviconSize, faviconSize)
drawCircle(context, pos)
drawText(context, pos, unreadCount)

favicon.href = canvas.toDataURL('image/png')
}

return {
setUnreadCount: setUnreadCount
}
}])