mirror of
https://github.com/zulip/zulip-desktop.git
synced 2025-11-16 11:51:36 +00:00
electron-bridge: Implement electron bridge.
This PR adds a bridge to communicate with the webapp in real time. As of now, the bridge listens for following events - * When realm name changes * When realm icon changes * When the unread count changes Partially fixes #425.
This commit is contained in:
committed by
Akash Nimare
parent
7130103999
commit
124a842bbd
@@ -279,6 +279,10 @@ app.on('ready', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ipcMain.on('realm-icon-changed', (event, serverURL, iconURL) => {
|
||||||
|
page.send('update-realm-icon', serverURL, iconURL);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.on('before-quit', () => {
|
app.on('before-quit', () => {
|
||||||
|
|||||||
39
app/renderer/js/electron-bridge.js
Normal file
39
app/renderer/js/electron-bridge.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
const events = require('events');
|
||||||
|
const { ipcRenderer } = require('electron');
|
||||||
|
|
||||||
|
// we have and will have some non camelcase stuff
|
||||||
|
// while working with zulip so just turning the rule off
|
||||||
|
// for the whole file.
|
||||||
|
/* eslint-disable camelcase */
|
||||||
|
class ElectronBridge extends events {
|
||||||
|
send_event(...args) {
|
||||||
|
this.emit(...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
on_event(...args) {
|
||||||
|
this.on(...args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const electron_bridge = new ElectronBridge();
|
||||||
|
|
||||||
|
electron_bridge.on('total_unread_count', (...args) => {
|
||||||
|
ipcRenderer.send('unread-count', ...args);
|
||||||
|
});
|
||||||
|
|
||||||
|
electron_bridge.on('realm_name', (...args) => {
|
||||||
|
ipcRenderer.send('realm-name-changed', ...args);
|
||||||
|
});
|
||||||
|
|
||||||
|
electron_bridge.on('realm_icon_url', iconURL => {
|
||||||
|
const serverURL = location.origin;
|
||||||
|
iconURL = iconURL.includes('http') ? iconURL : `${serverURL}${iconURL}`;
|
||||||
|
ipcRenderer.send('realm-icon-changed', serverURL, iconURL);
|
||||||
|
});
|
||||||
|
|
||||||
|
// this follows node's idiomatic implementation of event
|
||||||
|
// emitters to make event handling more simpler instead of using
|
||||||
|
// functions zulip side will emit event using ElectronBrigde.send_event
|
||||||
|
// which is alias of .emit and on this side we can handle the data by adding
|
||||||
|
// a listener for the event.
|
||||||
|
module.exports = electron_bridge;
|
||||||
@@ -397,6 +397,22 @@ class ServerManagerView {
|
|||||||
const webContents = webview.getWebContents();
|
const webContents = webview.getWebContents();
|
||||||
webContents.send('toggle-dnd', state, newSettings);
|
webContents.send('toggle-dnd', state, newSettings);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ipcRenderer.on('update-realm-icon', (event, serverURL, iconURL) => {
|
||||||
|
DomainUtil.getDomains().forEach((domain, index) => {
|
||||||
|
if (domain.url.includes(serverURL)) {
|
||||||
|
DomainUtil.saveServerIcon(iconURL).then(localIconUrl => {
|
||||||
|
const serverImgsSelector = `.tab .server-icons`;
|
||||||
|
const serverImgs = document.querySelectorAll(serverImgsSelector);
|
||||||
|
serverImgs[index].src = localIconUrl;
|
||||||
|
|
||||||
|
domain.icon = localIconUrl;
|
||||||
|
DomainUtil.db.push(`/domains[${index}]`, domain, true);
|
||||||
|
DomainUtil.reloadDB();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
destroyTab(name, index) {
|
destroyTab(name, index) {
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ require('./notification');
|
|||||||
// Prevent drag and drop event in main process which prevents remote code executaion
|
// Prevent drag and drop event in main process which prevents remote code executaion
|
||||||
require(__dirname + '/shared/preventdrag.js');
|
require(__dirname + '/shared/preventdrag.js');
|
||||||
|
|
||||||
|
// eslint-disable-next-line camelcase
|
||||||
|
window.electron_bridge = require('./electron-bridge');
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
// Create the menu for the below
|
// Create the menu for the below
|
||||||
document.querySelector('.dropdown-toggle').click();
|
document.querySelector('.dropdown-toggle').click();
|
||||||
|
|||||||
Reference in New Issue
Block a user