Mailspring/internal_packages/system-tray/lib/tray-store.es6
Ben Gotow b2f8b34a32 feat(counts): Unread counts for all folders and labels across all accounts
Summary:
This diff replaces the UnreadCountStore with a better approach that is able to track unread counts for all folders/labels without continuous (and cripplingly slow) SELECT COUNT(*) queries.

When models are written to the database, we currently don't send out notifications with the "previous" state of those objects in the database. This makes it hard to determine how to update counters. (In the future, we may need to do this for live queries). Unfortunately, getting the "previous" state is going to be very hard, because multiple windows write to the database and the "previous" state we have might be outdated. We'd almost have to run a "SELECT" right before every "REPLACE INTO".

I created an API that allows you to register observers around persistModel and unpersistModel. With this API, you can run queries before and after the database changes are made and pluck just the "before" state you're interested in.

The `ThreadCountsStore` uses this API to determine the impact of persisting a set of threads on the unread counts of different labels. Before the threads are saved, it says "how much do these thread IDs contribute to unread counts currently?". After the write is complete it looks at the models and computes the difference between the old count impact and the new count impact, and updates the counters.

I decided not to attach the unread count to the Label objects themselves because 1) they update frequently and 2) most things observing the DatabaseStore for categories do not care about counts, so they would be updating unnecessarily.

The AccountSidebar now listens to the ThreadCountsStore as well as the CategoryStore, and there's a new preference in the General tab for turning off the counts.

Test Plan: Tests are a work in progress, want to get feedback first!

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D2232
2015-11-23 17:12:22 -08:00

105 lines
2.9 KiB
JavaScript

import path from 'path';
import remote from 'remote';
import ipc from 'ipc';
import NylasStore from 'nylas-store';
import {UnreadBadgeStore, CanvasUtils} from 'nylas-exports';
const NativeImage = remote.require('native-image');
const Menu = remote.require('menu');
const {canvasWithSystemTrayIconAndText} = CanvasUtils;
// Must be absolute real system path
// https://github.com/atom/electron/issues/1299
const BASE_ICON_PATH = path.join(__dirname, '..', 'assets', process.platform, 'ic-systemtray-nylas.png');
const UNREAD_ICON_PATH = path.join(__dirname, '..', 'assets', process.platform, 'ic-systemtray-nylas-unread.png');
const menuTemplate = [
{
label: 'New Message',
click: ()=> ipc.send('command', 'application:new-message'),
},
{
label: 'Preferences',
click: ()=> ipc.send('command', 'application:open-preferences'),
},
{
type: 'separator',
},
{
label: 'Quit N1',
click: ()=> ipc.send('command', 'application:quit'),
},
];
if (process.platform === 'darwin') {
menuTemplate.unshift({
label: 'Open Inbox',
click: ()=> ipc.send('command', 'application:show-main-window'),
});
}
const _buildMenu = ()=> {
return Menu.buildFromTemplate(menuTemplate);
};
class TrayStore extends NylasStore {
constructor(platform) {
super();
this._platform = platform;
this._unreadIcon = NativeImage.createFromPath(UNREAD_ICON_PATH);
this._baseIcon = NativeImage.createFromPath(BASE_ICON_PATH);
this._unreadCount = UnreadBadgeStore.count() || 0;
this._menu = _buildMenu(platform);
this._icon = this._getIconImg();
this.listenTo(UnreadBadgeStore, this._onUnreadCountChanged);
}
unreadCount() {
return this._unreadCount;
}
icon() {
return this._icon;
}
tooltip() {
return `${this._unreadCount} unread messages`;
}
menu() {
return this._menu;
}
_getIconImg(platform = this._platform, unreadCount = this._unreadCount) {
const imgHandlers = {
'darwin': ()=> {
const img = new Image();
// toDataUrl always returns the @1x image data, so the assets/darwin/
// contains an "@2x" image /without/ the @2x extension
img.src = this._baseIcon.toDataUrl();
const count = this._unreadCount || '';
const canvas = canvasWithSystemTrayIconAndText(img, count.toString());
const pngData = NativeImage.createFromDataUrl(canvas.toDataURL()).toPng();
// creating from a buffer allows us to specify that the image is @2x
const out2x = NativeImage.createFromBuffer(pngData, 2);
out2x.setTemplateImage(true);
return out2x;
},
'default': ()=> {
return unreadCount > 0 ? this._unreadIcon : this._baseIcon;
},
};
return imgHandlers[platform in imgHandlers ? platform : 'default']();
}
_onUnreadCountChanged() {
this._unreadCount = UnreadBadgeStore.count();
this._icon = this._getIconImg();
this.trigger();
}
}
export default TrayStore;