a rather hacky implementation of theme switching

This commit is contained in:
Matthew Hodgson 2017-01-17 19:13:23 +00:00
parent c2cdb626bd
commit 8288eb730c
2 changed files with 93 additions and 12 deletions

View file

@ -25,6 +25,7 @@ var SdkConfig = require("../../SdkConfig");
var ContextualMenu = require("./ContextualMenu"); var ContextualMenu = require("./ContextualMenu");
var RoomListSorter = require("../../RoomListSorter"); var RoomListSorter = require("../../RoomListSorter");
var UserActivity = require("../../UserActivity"); var UserActivity = require("../../UserActivity");
var UserSettingsStore = require('../../UserSettingsStore');
var Presence = require("../../Presence"); var Presence = require("../../Presence");
var dis = require("../../dispatcher"); var dis = require("../../dispatcher");
@ -456,6 +457,9 @@ module.exports = React.createClass({
middleOpacity: payload.middleOpacity, middleOpacity: payload.middleOpacity,
}); });
break; break;
case 'set_theme':
this._onSetTheme(payload.value);
break;
case 'on_logged_in': case 'on_logged_in':
this._onLoggedIn(); this._onLoggedIn();
break; break;
@ -584,6 +588,44 @@ module.exports = React.createClass({
_onLoadCompleted: function() { _onLoadCompleted: function() {
this.props.onLoadCompleted(); this.props.onLoadCompleted();
this.setState({loading: false}); this.setState({loading: false});
// set up the right theme.
// XXX: this will temporarily flicker the wrong CSS.
dis.dispatch({
action: 'set_theme',
value: UserSettingsStore.getSyncedSetting('theme')
});
},
/**
* Called whenever someone changes the theme
*/
_onSetTheme: function(theme) {
if (!theme) {
theme = 'light';
}
var i, a;
for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
var href = a.getAttribute("href");
if (href.startsWith("theme-")) {
if (href.startsWith("theme-" + theme + ".")) {
a.disabled = false;
}
else {
a.disabled = true;
}
}
}
if (theme === 'dark') {
// abuse the tinter to change all the SVG's #fff to #2d2d2d
// XXX: obviously this shouldn't be hardcoded here.
Tinter.tint(undefined, undefined, undefined, '#2d2d2d');
}
else {
Tinter.tint(undefined, undefined, undefined, '#ffffff');
}
}, },
/** /**

View file

@ -363,7 +363,23 @@ module.exports = React.createClass({
*/ */
]; ];
var themes = [
{
id: 'theme',
label: 'Light theme',
value: 'light',
},
{
id: 'theme',
label: 'Dark theme',
value: 'dark',
}
];
var syncedSettings = UserSettingsStore.getSyncedSettings(); var syncedSettings = UserSettingsStore.getSyncedSettings();
if (!syncedSettings.theme) {
syncedSettings.theme = 'light';
}
return ( return (
<div> <div>
@ -379,19 +395,42 @@ module.exports = React.createClass({
Disable inline URL previews by default Disable inline URL previews by default
</label> </label>
</div> </div>
</div> { settingsLabels.map( setting => {
{ settingsLabels.forEach( setting => { return <div className="mx_UserSettings_toggle" key={ setting.id }>
<div className="mx_UserSettings_toggle">
<input id={ setting.id } <input id={ setting.id }
type="checkbox" type="checkbox"
defaultChecked={ syncedSettings[setting.id] } defaultChecked={ syncedSettings[setting.id] }
onChange={ e => UserSettingsStore.setSyncedSetting(setting.id, e.target.checked) } onChange={ e => UserSettingsStore.setSyncedSetting(setting.id, e.target.checked) }
/> />
<label htmlFor={ setting.id }> <label htmlFor={ setting.id }>
{ settings.label } { setting.label }
</label> </label>
</div> </div>
})} })}
{ themes.map( setting => {
return <div className="mx_UserSettings_toggle" key={ setting.id + "_" + setting.value }>
<input id={ setting.id + "_" + setting.value }
type="radio"
name={ setting.id }
value={ setting.value }
defaultChecked={ syncedSettings[setting.id] === setting.value }
onChange={ e => {
if (e.target.checked) {
UserSettingsStore.setSyncedSetting(setting.id, setting.value)
}
dis.dispatch({
action: 'set_theme',
value: setting.value,
});
}
}
/>
<label htmlFor={ setting.id + "_" + setting.value }>
{ setting.label }
</label>
</div>
})}
</div>
</div> </div>
); );
}, },