Skip to content

Commit

Permalink
Merge pull request #791 from HappyTobi/tray
Browse files Browse the repository at this point in the history
[FIX] Mac osx menubar color
  • Loading branch information
engelgabriel authored Aug 4, 2018
2 parents ffe9045 + 02ee8d5 commit b602c93
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 33 deletions.
40 changes: 37 additions & 3 deletions src/i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ import { app, remote } from 'electron';

const eApp = app || remote.app;

let loadedLanguage = [];

/**
* Load singular and plural translation based on count
* @param {string} phrase The key fore the translation string
* @param {number} chount Count to check for singular / plural (0-1,2-n)
* @returns {string} Translation in user language
*/
function loadTranslation (phrase = '', count) {
const loadedLanguageTranslation = loadedLanguage[phrase];
let translation = loadedLanguageTranslation;
if (loadedLanguageTranslation === undefined) {
translation = phrase;
} else if (loadedLanguageTranslation instanceof Object) {
translation = loadedLanguageTranslation['one'];
if (count > 1) {
translation = loadedLanguageTranslation['multi'];
}
}
return translation;
}

class I18n {
/**
* Load users language if available, and fallback to english for any missing strings
Expand All @@ -16,11 +38,11 @@ class I18n {
dir = path.join(__dirname, 'i18n/lang');
}
const defaultLocale = path.join(dir, 'en.i18n.json');
this.loadedLanguage = JSON.parse(fs.readFileSync(defaultLocale, 'utf8'));
loadedLanguage = JSON.parse(fs.readFileSync(defaultLocale, 'utf8'));
const locale = path.join(dir, `${eApp.getLocale()}.i18n.json`);
if (fs.existsSync(locale)) {
const lang = JSON.parse(fs.readFileSync(locale, 'utf8'));
this.loadedLanguage = Object.assign(this.loadedLanguage, lang);
loadedLanguage = Object.assign(loadedLanguage, lang);
}
}

Expand All @@ -31,7 +53,19 @@ class I18n {
* @return {string} Translation in users language
*/
__ (phrase, ...replacements) {
const translation = this.loadedLanguage[phrase] ? this.loadedLanguage[phrase] : phrase;
const translation = loadTranslation(phrase, 0);
return util.format(translation, ...replacements);
}

/**
* Get translation string
* @param {string} phrase The key for the translation string
* @param {number} count Count to check for singular / plural (0-1,2-n)
* @param {...string|number} replacements List of replacements in template strings
* @return {string} Translation in users language
*/
pluralize (phrase, count, ...replacements) {
const translation = loadTranslation(phrase, count);
return util.format(translation, ...replacements);
}
}
Expand Down
69 changes: 39 additions & 30 deletions src/scripts/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,40 @@ const icons = {
};

const statusBullet = {
online: '\u001B[32m•\u001B[0m',
away: '\u001B[33m•\u001B[0m',
busy: '\u001B[31m•\u001B[0m',
offline: '\u001B[30m•\u001B[0m'
online: '\u001B[32m•',
away: '\u001B[33m•',
busy: '\u001B[31m•',
offline: '\u001B[30m•'
};

const messageCountColor = {
white: '\u001B[37m',
black: '\u001B[0m'
};

function getTrayImagePath (badge) {
let iconFilename;
if (badge.title === '•') {
iconFilename = "icon-tray-dot";
} else if (!isNaN(parseInt(badge.title)) && badge.title > 0) {
if (badge.title > 9) {
iconFilename = "icon-tray-9plus";
} else {
iconFilename = `icon-tray-${badge.count}`;
}
} else if (badge.showAlert) {
iconFilename = "icon-tray-alert";
} else {
iconFilename = "icon-tray-Template";
}

if (process.platform === 'win32') {
iconFilename += ".ico";
} else {
iconFilename += ".png";
}

return path.join(__dirname, 'images', icons[process.platform].dir, iconFilename);
}

function createAppTray () {
Expand Down Expand Up @@ -96,31 +126,6 @@ function createAppTray () {
};
}

function getTrayImagePath (badge) {
let iconFilename;
if (badge.title === '•') {
iconFilename = "icon-tray-dot";
} else if (!isNaN(parseInt(badge.title)) && badge.title > 0) {
if (badge.title > 9) {
iconFilename = "icon-tray-9plus";
} else {
iconFilename = `icon-tray-${badge.count}`;
}
} else if (badge.showAlert) {
iconFilename = "icon-tray-alert";
} else {
iconFilename = "icon-tray-Template";
}

if (process.platform === 'win32') {
iconFilename += ".ico";
} else {
iconFilename += ".png";
}

return path.join(__dirname, 'images', icons[process.platform].dir, iconFilename);
}

function showTrayAlert (badge, status = 'online') {
if (mainWindow.tray === null || mainWindow.tray === undefined) {
return;
Expand All @@ -129,7 +134,11 @@ function showTrayAlert (badge, status = 'online') {
mainWindow.flashFrame(badge.showAlert);

if (process.platform === 'darwin') {
mainWindow.tray.setTitle(`${statusBullet[status]}${badge.count}`);
let countColor = messageCountColor['black'];
if (remote.systemPreferences.isDarkMode()) {
countColor = messageCountColor['white'];
}
mainWindow.tray.setTitle(`${statusBullet[status]} ${countColor}${badge.count}`);
}
}

Expand Down

0 comments on commit b602c93

Please sign in to comment.