Skip to content

Commit

Permalink
Update main window
Browse files Browse the repository at this point in the history
Themes now work for tabs. Separate themes for separate tabs. Implemented tab settings. Links clicked in whatsapp instance will open in default browser.
  • Loading branch information
amanharwara committed Feb 20, 2019
1 parent 3e1559b commit bbc80de
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 65 deletions.
195 changes: 130 additions & 65 deletions src/windows/main/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ let tabs = new Store({
}
});

let themes = new Store({
name: 'themes'
});

function loadTabsFromStorage() {
let storedTabs = tabs.get('instances');
if (storedTabs.length == 0) {
document.querySelector('.no-tabs').style.display = 'flex';
} else {
storedTabs.forEach(tab => {
let tabElementCode = `<a class="item" id="tab-${tab.id}" data-tab="${tab.id}">
let tabElementCode = `<a class="item tab-element" id="tab-${tab.id}" data-tab="${tab.id}">
<i class="whatsapp icon"></i> ${tab.name} <i class="cog icon ui"></i> <i class="close icon ui"></i>
</a>`;
let instanceElementCode = `<div class="ui bottom attached tab segment" id="${tab.id}" data-tab="${tab.id}">
let instanceElementCode = `<div class="ui bottom attached tab segment" id="tab-content-${tab.id}" data-tab="${tab.id}">
<webview preload="./js/whatsapp.js" id="whatsapp-${tab.id}" src="https://web.whatsapp.com/" useragent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" partition="persist:${tab.id}"></webview>
</div>`;
let settingsModalCode = `<div class="ui mini modal settings-modal-${tab.id}">
Expand All @@ -58,6 +62,14 @@ function loadTabsFromStorage() {
<input type="checkbox" tabindex="0" class="hidden">
</div>
</div>
<div class="ui field">
<label>Theme</label>
<div class="ui selection dropdown ${tab.id}-theme-value">
<input type="hidden" name="theme">
<i class="dropdown icon"></i>
<div class="default text">Theme</div>
</div>
</div>
</div>
</div>
<div class="actions">
Expand All @@ -74,66 +86,7 @@ function loadTabsFromStorage() {
document.querySelector('.tab-contents').appendChild(instanceElement);
document.querySelector('.modals-div').appendChild(settingsModal);

$('.menu .item')
.tab({
onFirstLoad: () => {
let webview = document.querySelector(`#${tab.id}`).querySelector('webview');
webview.addEventListener('dom-ready', () => {
if (tab.settings.sound == true) {
webview.setAudioMuted(false);
} else {
webview.setAudioMuted(true);
}

var notify;
if (tab.settings.notifications == true) {
notify = 'new _Notification(title, options)';
} else {
notify = 'undefined';
}
webview.executeJavaScript(`
var _Notification = window.Notification;
var ProxyNotification = function (title, options) {
var _notification = ${notify};
this.title = _notification.title;
this.dir = _notification.dir;
this.lang = _notification.lang;
this.body = _notification.body;
this.tag = _notification.tag;
this.icon = _notification.icon;
var that = this;
_notification.onclick = function (event) {
if (that.onclick != undefined) that.onclick(event);
};
_notification.onshow = function (event) {
if (that.onshow != undefined) that.onshow(event);
};
_notification.onerror = function (event) {
if (that.onerror != undefined) that.onerror(event);
};
_notification.onclose = function (event) {
if (that.onclose != undefined) that.onclose(event);
};
this.close = function () {
_notification.close();
};
this.addEventListener = function (type, listener, useCapture) {
_notification.addEventListener(type, listener, useCapture);
};
this.removeEventListener = function (type, listener, useCapture) {
_notification.removeEventListener(type, listener, useCapture);
};
this.dispatchEvent = function (event) {
_notification.dispatchEvent(event);
};
}
ProxyNotification.permission = _Notification.permission;
ProxyNotification.requestPermission = _Notification.requestPermission;
window.Notification = ProxyNotification;
`);
});
}
});
$(`#tab-${tab.id}`).tab();

$(`.settings-modal-${tab.id}`)
.modal({
Expand All @@ -143,7 +96,8 @@ window.Notification = ProxyNotification;
id: `${tab.id}`,
settings: {
notifications: $(`.${tab.id}-notifications-value`).checkbox('is checked'),
sound: $(`.${tab.id}-sound-value`).checkbox('is checked')
sound: $(`.${tab.id}-sound-value`).checkbox('is checked'),
theme: $(`.${tab.id}-theme-value`).dropdown('get value')
}
}
let tabsStore = tabs.get('instances');
Expand All @@ -167,6 +121,7 @@ window.Notification = ProxyNotification;
} else {
$(`.${tab.id}-sound-value`).checkbox('uncheck');
}

});

document.querySelector('.menu').firstElementChild.classList.add('active');
Expand All @@ -193,13 +148,15 @@ $('.ui.modal')
let instanceNameValue = document.querySelector('#new-instance-name').value;
let instanceNotificationsValue = $('.notifcheck').checkbox('is checked');
let instanceSoundValue = $('.soundcheck').checkbox('is checked');
let instanceThemeValue = $('.themecheck').dropdown('get value');

let newInstance = {
name: instanceNameValue,
id: generateId(),
settings: {
notifications: instanceNotificationsValue,
sound: instanceSoundValue
sound: instanceSoundValue,
theme: instanceThemeValue
}
};

Expand All @@ -218,7 +175,115 @@ $('.ui.checkbox')

loadTabsFromStorage();

ipcRenderer.on('settings-changed', e => window.location.reload(true));
function generateThemeNames() {
let nameList = [];

themes.get('themes').forEach(theme => {
let name = theme.name;
let value = theme.name;

let themeJSON = {
name: name,
value: value
}

nameList.push(themeJSON);
});

return nameList;
}

$('.dropdown').dropdown();
$('.dropdown').dropdown({
values: generateThemeNames()
});

function generateThemeCSS(name) {
let themesList = themes.get('themes');
let selectedTheme = themesList.find(x => x.name === name);
return selectedTheme.css;
}

function setWebViewSettings(webviewElement, tab) {
webviewElement.addEventListener('dom-ready', () => {
if (tab.settings.sound == true) {
webviewElement.setAudioMuted(false);
} else {
webviewElement.setAudioMuted(true);
}

webviewElement.executeJavaScript(`
var styleElem = document.querySelector('#whatsapp-style');
if (styleElem) {
styleElem.innerHTML = \`${generateThemeCSS(tab.settings.theme)}\`;
} else if (!styleElem) {
var styleElement = document.createElement('style');
styleElement.id = 'whatsapp-style';
styleElement.innerHTML = \`${generateThemeCSS(tab.settings.theme)}\`;
document.head.appendChild(styleElement);
}`);

var notify;
if (tab.settings.notifications == true) {
notify = 'new _Notification(title, options)';
} else {
notify = 'undefined';
}
webviewElement.executeJavaScript(`
var _Notification = window.Notification;
var ProxyNotification = function (title, options) {
var _notification = ${notify};
this.title = _notification.title;
this.dir = _notification.dir;
this.lang = _notification.lang;
this.body = _notification.body;
this.tag = _notification.tag;
this.icon = _notification.icon;
var that = this;
_notification.onclick = function (event) {
if (that.onclick != undefined) that.onclick(event);
};
_notification.onshow = function (event) {
if (that.onshow != undefined) that.onshow(event);
};
_notification.onerror = function (event) {
if (that.onerror != undefined) that.onerror(event);
};
_notification.onclose = function (event) {
if (that.onclose != undefined) that.onclose(event);
};
this.close = function () {
_notification.close();
};
this.addEventListener = function (type, listener, useCapture) {
_notification.addEventListener(type, listener, useCapture);
};
this.removeEventListener = function (type, listener, useCapture) {
_notification.removeEventListener(type, listener, useCapture);
};
this.dispatchEvent = function (event) {
_notification.dispatchEvent(event);
};
}
ProxyNotification.permission = _Notification.permission;
ProxyNotification.requestPermission = _Notification.requestPermission;
window.Notification = ProxyNotification;
`);
});
}

document.querySelectorAll('.tab').forEach(tabElement => {
let tabID = tabElement.id.replace('tab-content-', '');
let tabJSON = tabs.get('instances').find(x => x.id === tabID);
let webviewElement = document.querySelector(`#${tabElement.id}`).querySelector('webview');

$(`.${tabID}-theme-value`).dropdown('set selected', tabJSON.settings.theme);
$(`.${tabID}-theme-value`).dropdown();

setWebViewSettings(webviewElement, tabJSON);
});

ipcRenderer.on('new-themes-added', e => window.location.reload(true));

// Setting title explicitly
mainTitlebar.updateTitle(`Altus ${app.getVersion()}`);
9 changes: 9 additions & 0 deletions src/windows/main/js/whatsapp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const {
remote
} = require('electron');
const {
ipcRenderer
} = require('electron');
const Mousetrap = require('mousetrap');

// Fix for "WhatsApp works with Chrome 36+" issue . DO NOT REMOVE
Expand All @@ -24,6 +27,12 @@ window.onload = () => {
}
}

document.addEventListener('click', e => {
if (e.target.tagName == "A") {
ipcRenderer.send('link-open', e.target.href);
}
});

/* Mac Copy/Paste Fix */
Mousetrap.bind(['command+c', 'ctrl+c'], function(e) {
document.execCommand('copy');
Expand Down
8 changes: 8 additions & 0 deletions src/windows/main/window.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ <h1 class="huge inverted ui header">No Instances Found
<input id="new-sound-value" type="checkbox" tabindex="0" class="hidden">
</div>
</div>
<div class="ui field">
<label>Theme</label>
<div class="ui selection dropdown theme-dropdown themecheck">
<input type="hidden" name="theme">
<i class="dropdown icon"></i>
<div class="default text">Theme</div>
</div>
</div>
</div>
</div>
<div class="actions">
Expand Down

0 comments on commit bbc80de

Please sign in to comment.