diff --git a/src/browser/index.jsx b/src/browser/index.jsx index 56761f16..5800ced9 100644 --- a/src/browser/index.jsx +++ b/src/browser/index.jsx @@ -9,6 +9,10 @@ const Tab = ReactBootstrap.Tab; const electron = require('electron'); const remote = electron.remote; +const osLocale = require('os-locale'); +const fs = require('fs'); +const url = require('url'); + const settings = require('../common/settings'); var MainPage = React.createClass({ @@ -57,13 +61,77 @@ var MainPage = React.createClass({ }); var MattermostView = React.createClass({ + getInitialState: function() { + return { + unreadCount: 0 + }; + }, + handleUnreadCountChange: function(count) { + this.setState({ + unreadCount: count + }); + if (this.props.onUnreadCountChange) { + this.props.onUnreadCountChange(count); + } + }, + componentDidMount: function() { + var thisObj = this; + var webview = ReactDOM.findDOMNode(this.refs.webview); + + // Open link in browserWindow. for exmaple, attached files. + webview.addEventListener('new-window', function(e) { + var currentURL = url.parse(webview.getURL()); + var destURL = url.parse(e.url); + if (currentURL.host === destURL.host) { + window.open(e.url, 'electron-mattermost'); + } else { + // if the link is external, use default browser. + require('shell').openExternal(e.url); + } + }); + + webview.addEventListener("dom-ready", function() { + // webview.openDevTools(); + + // Use 'Meiryo UI' and 'MS Gothic' to prevent CJK fonts on Windows(JP). + if (process.platform === 'win32') { + var applyCssFile = function(cssFile) { + fs.readFile(cssFile, 'utf8', function(err, data) { + if (err) { + console.log(err); + return; + } + webview.insertCSS(data); + }); + }; + + osLocale(function(err, locale) { + if (err) { + console.log(err); + return; + } + if (locale === 'ja_JP') { + applyCssFile(__dirname + '/css/jp_fonts.css'); + } + }); + } + }); + + webview.addEventListener('ipc-message', function(event) { + switch (event.channel) { + case 'onUnreadCountChange': + var unreadCount = event.args[0]; + thisObj.handleUnreadCountChange(unreadCount); + break; + } + }); + + }, render: function() { // 'disablewebsecurity' is necessary to display external images. // However, it allows also CSS/JavaScript. // So webview should use 'allowDisplayingInsecureContent' as same as BrowserWindow. - return ( - - ); + return (); } }); diff --git a/src/browser/webview/mattermost.js b/src/browser/webview/mattermost.js index d8c83986..80543b3a 100644 --- a/src/browser/webview/mattermost.js +++ b/src/browser/webview/mattermost.js @@ -4,10 +4,16 @@ const electron = require('electron'); const ipc = electron.ipcRenderer; const NativeNotification = Notification; -ipc.on('retrieveUnreadCount', function() { +var unreadCountTimer = setInterval(function() { + if (!this.count) { + this.count = 0; + } var unreadCount = document.getElementsByClassName('unread-title').length; - ipc.sendToHost('retrieveUnreadCount', unreadCount); -}); + if (this.count != unreadCount) { + ipc.sendToHost('onUnreadCountChange', unreadCount); + } + this.count = unreadCount; +}, 1000); // On Windows 8.1 and Windows 8, a shortcut with a Application User Model ID must be installed to the Start screen. // In current version, use tray balloon for notification