BetterDiscordAddons/Plugins/OldTitleBar/OldTitleBar.plugin.js

276 lines
11 KiB
JavaScript
Raw Normal View History

2019-09-20 22:32:52 +02:00
//META{"name":"OldTitleBar","website":"https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/OldTitleBar","source":"https://raw.githubusercontent.com/mwittrien/BetterDiscordAddons/master/Plugins/OldTitleBar/OldTitleBar.plugin.js"}*//
2019-03-22 20:54:11 +01:00
2018-10-11 10:21:26 +02:00
class OldTitleBar {
2019-01-17 13:46:11 +01:00
getName () {return "OldTitleBar";}
2019-11-06 12:52:14 +01:00
getVersion () {return "1.6.1";}
2019-01-17 13:46:11 +01:00
getAuthor () {return "DevilBro";}
getDescription () {return "Reverts the title bar back to its former self.";}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
constructor () {
2019-03-06 11:30:31 +01:00
this.changelog = {
2019-11-05 21:16:39 +01:00
"improved":[["New Library Structure & React","Restructured my Library and switched to React rendering instead of DOM manipulation"]]
2019-03-06 11:30:31 +01:00
};
2019-09-04 12:34:02 +02:00
this.patchModules = {
2019-11-05 21:16:39 +01:00
App: "render",
HeaderBarContainer: "render",
StandardSidebarView: "render",
AuthWrapper: "render"
};
2019-09-04 12:34:02 +02:00
}
2019-01-26 22:45:19 +01:00
2019-09-04 12:34:02 +02:00
initConstructor () {
2018-10-11 10:21:26 +02:00
this.patched = false;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this.css = `
2019-11-05 21:16:39 +01:00
.OTB-enabled ${BDFDB.dotCN.splashbackground}:before {
2018-11-02 21:01:04 +01:00
display: none !important;
2018-10-11 10:21:26 +02:00
}
2019-11-05 21:16:39 +01:00
.platform-osx.OTB-enabled ${BDFDB.dotCN.guildswrapper} {
2019-05-29 16:25:12 +02:00
margin-top: 0;
}
2019-09-04 12:34:02 +02:00
2019-11-05 21:16:39 +01:00
.platform-osx.OTB-enabled ${BDFDB.dotCN.guildsscroller} {
2019-05-29 16:25:12 +02:00
padding-top: 10px;
}
2019-11-05 21:16:39 +01:00
.OTB-toolbar {
flex: 1 1 auto;
justify-content: flex-end;
}
.OTB-settingsToolbar {
position: absolute;
top: 0;
right: 0;
padding: 10px;
z-index: 2;
-webkit-app-region: drag !important;
}
.OTB-enabled ${BDFDB.dotCN.splashbackground} .OTB-settingsToolbar {
background: rgba(0, 0, 0, 0.3);
border-radius: 0 0 0 5px;
}
2019-01-26 22:45:19 +01:00
2019-11-05 21:16:39 +01:00
${BDFDB.dotCN.channelheaderheaderbar} {
2018-10-11 10:21:26 +02:00
-webkit-app-region: drag !important;
}
2019-01-26 22:45:19 +01:00
2019-11-06 12:53:40 +01:00
${BDFDB.dotCNS.stopanimations + BDFDB.dotCN.channelheaderheaderbar},
2019-11-06 12:52:14 +01:00
${BDFDB.dotCN.channelheaderheaderbar} * {
2018-10-11 10:21:26 +02:00
-webkit-app-region: no-drag !important;
}`;
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
this.defaults = {
settings: {
2018-12-14 18:58:35 +01:00
displayNative: {value:!!document.querySelector(".platform-linux"), description:"Displays the native Title Bar."},
addOldBar: {value:true, description:"Displays the Title Bar in the old fashion."},
2018-11-08 09:49:46 +01:00
addToSettings: {value:true, description:"Adds a Title Bar to Settings Windows."},
reloadButton: {value:false, description:"Adds a Reload Button to the Title Bar."}
2018-10-11 10:21:26 +02:00
}
};
}
getSettingsPanel () {
2019-01-22 11:28:32 +01:00
if (!global.BDFDB || typeof BDFDB != "object" || !BDFDB.loaded || !this.started) return;
2019-10-22 23:04:35 +02:00
let settings = BDFDB.DataUtils.get(this, "settings");
2019-11-05 21:16:39 +01:00
let settingsitems = [];
for (let key in settings) settingsitems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
className: BDFDB.disCN.marginbottom8,
type: "Switch",
plugin: this,
keys: ["settings", key],
label: this.defaults.settings[key].description,
value: settings[key],
onChange: key == "displayNative" ? value => {
if (this.patchMainScreen(value)) {
this.patched = !this.patched;
let notifybar = document.querySelector("#OldTitleBarNotifyBar");
if (notifybar) notifybar.querySelector(BDFDB.dotCN.noticedismiss).click();
if (this.patched) {
notifybar = BDFDB.NotificationUtils.notice("Changed nativebar settings, relaunch to see changes:", {type:"danger",btn:"Relaunch",id:"OldTitleBarNotifyBar"});
notifybar.querySelector(BDFDB.dotCN.noticebutton).addEventListener("click", () => {
BDFDB.LibraryRequires.electron.remote.app.relaunch();
BDFDB.LibraryRequires.electron.remote.app.quit();
});
}
2019-01-17 23:48:29 +01:00
}
2019-11-05 21:16:39 +01:00
} : null
}));
return BDFDB.PluginUtils.createSettingsPanel(this, settingsitems);
2018-10-11 10:21:26 +02:00
}
//legacy
load () {}
start () {
2019-02-04 09:13:15 +01:00
if (!global.BDFDB) global.BDFDB = {myPlugins:{}};
if (global.BDFDB && global.BDFDB.myPlugins && typeof global.BDFDB.myPlugins == "object") global.BDFDB.myPlugins[this.getName()] = this;
2019-05-26 13:55:26 +02:00
var libraryScript = document.querySelector('head script#BDFDBLibraryScript');
if (!libraryScript || (performance.now() - libraryScript.getAttribute("date")) > 600000) {
2018-10-11 10:21:26 +02:00
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
2019-05-26 13:55:26 +02:00
libraryScript.setAttribute("id", "BDFDBLibraryScript");
2018-10-11 10:21:26 +02:00
libraryScript.setAttribute("type", "text/javascript");
2019-10-18 10:56:41 +02:00
libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.min.js");
2019-01-17 23:48:29 +01:00
libraryScript.setAttribute("date", performance.now());
2019-05-26 13:55:26 +02:00
libraryScript.addEventListener("load", () => {this.initialize();});
2018-10-11 10:21:26 +02:00
document.head.appendChild(libraryScript);
}
2019-01-17 23:48:29 +01:00
else if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) this.initialize();
2019-11-01 10:27:07 +01:00
this.startTimeout = setTimeout(() => {
try {return this.initialize();}
catch (err) {console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not initiate plugin! " + err);}
}, 30000);
2018-10-11 10:21:26 +02:00
}
initialize () {
2019-01-17 13:46:11 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-01-22 11:05:54 +01:00
if (this.started) return;
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.init(this);
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.ListenerUtils.add(this, window, "resize", e => {
2019-11-05 21:16:39 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this, ["HeaderBarContainer", "StandardSidebarView"]);
2019-03-06 11:30:31 +01:00
});
2019-01-26 22:45:19 +01:00
2019-09-11 12:14:43 +02:00
this.window = BDFDB.LibraryRequires.electron.remote.getCurrentWindow();
2019-01-26 22:45:19 +01:00
2019-10-22 23:04:35 +02:00
this.patchMainScreen(BDFDB.DataUtils.get(this, "settings", "displayNative"));
2019-01-26 22:45:19 +01:00
2019-11-05 21:16:39 +01:00
BDFDB.DOMUtils.addClass(document.documentElement, "OTB-enabled");
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2018-10-11 10:21:26 +02:00
}
2019-11-01 10:14:50 +01:00
else console.error(`%c[${this.getName()}]%c`, "color: #3a71c1; font-weight: 700;", "", "Fatal Error: Could not load BD functions!");
2018-10-11 10:21:26 +02:00
}
stop () {
2019-01-17 13:46:11 +01:00
if (global.BDFDB && typeof BDFDB === "object" && BDFDB.loaded) {
2019-10-22 11:37:23 +02:00
this.stopping = true;
2019-11-05 21:16:39 +01:00
BDFDB.ModuleUtils.forceAllUpdates(this);
2019-01-26 22:45:19 +01:00
2019-11-05 21:16:39 +01:00
BDFDB.DOMUtils.removeClassFromDOM("OTB-enabled");
2019-01-26 22:45:19 +01:00
2019-10-22 18:55:25 +02:00
BDFDB.PluginUtils.clear(this);
2018-10-11 10:21:26 +02:00
}
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
// begin of own functions
2019-01-26 22:45:19 +01:00
2019-11-05 21:16:39 +01:00
onSettingsClosed () {
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
BDFDB.ModuleUtils.forceAllUpdates(this, ["HeaderBarContainer", "StandardSidebarView"]);
}
2019-04-23 12:59:14 +02:00
}
2019-11-05 21:16:39 +01:00
processApp (e) {
let [children, index] = BDFDB.ReactUtils.findChildren(e.returnvalue, {props:[["type",["WINDOWS", "MACOS"]]]});
if (index > -1) children[index] = null;
2019-01-05 20:24:38 +01:00
}
2019-01-26 22:45:19 +01:00
2019-11-05 21:16:39 +01:00
processHeaderBarContainer (e) {
if (!BDFDB.DataUtils.get(this, "settings", "addOldBar")) return;
let children = BDFDB.ReactUtils.getValue(e.returnvalue, "props.toolbar.props.children");
if (!children) {
let [oldToolbarParent, oldToolbarIndex] = BDFDB.ReactUtils.findChildren(e.returnvalue, {props:[["className", "OTB-toolbar"]]});
if (oldToolbarIndex > -1) oldToolbarParent.splice(oldToolbarIndex, 1);
let toolbar = BDFDB.ReactUtils.createElement("div", {
className: BDFDB.disCNS.channelheadertoolbar + "OTB-toolbar",
children: []
});
e.returnvalue.props.children.push(toolbar);
children = toolbar.props.children;
}
2019-11-05 21:16:39 +01:00
this.injectButtons(children, true);
}
2019-01-26 22:45:19 +01:00
2019-11-05 21:16:39 +01:00
processStandardSidebarView (e) {
if (!BDFDB.ArrayUtils.is(e.returnvalue.props.children)) e.returnvalue.props.children = [e.returnvalue.props.children];
this.injectSettingsToolbar(e.returnvalue.props.children);
}
2019-01-26 22:45:19 +01:00
2019-11-05 21:16:39 +01:00
processAuthWrapper (e) {
if (!BDFDB.ArrayUtils.is(e.returnvalue.props.children)) e.returnvalue.props.children = [e.returnvalue.props.children];
this.injectSettingsToolbar(e.returnvalue.props.children, true);
}
2019-11-05 21:16:39 +01:00
injectSettingsToolbar (children, fixed) {
if (!BDFDB.DataUtils.get(this, "settings", "addToSettings")) return;
let toolbar = BDFDB.ReactUtils.createElement("div", {
className: BDFDB.disCNS.channelheadertoolbar + "OTB-settingsToolbar",
children: [],
style: fixed ? {position: "fixed"} : null
2019-01-17 13:46:11 +01:00
});
2019-11-05 21:16:39 +01:00
this.injectButtons(toolbar.props.children);
children.push(toolbar);
2018-10-11 10:21:26 +02:00
}
2019-11-05 21:16:39 +01:00
injectButtons (children, addFirstDivider) {
if (addFirstDivider) children.push(BDFDB.ReactUtils.createElement("div", {className: BDFDB.disCN.channelheaderdivider}))
if (BDFDB.DataUtils.get(this, "settings", "reloadButton")) {
children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.TooltipContainer, {
text: BDFDB.LanguageUtils.LanguageStrings.ERRORS_RELOAD,
tooltipConfig: {type: "bottom"},
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Clickable, {
className: BDFDB.disCNS.channelheadericonwrapper + BDFDB.disCN.channelheadericonclickable,
onClick: _ => {this.window.reload();},
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCNS.channelheadericon + "OTB-reloadButton",
iconSVG: `<svg><path fill="currentColor" stroke="none" transform="translate(3,4)" d="M 17.061, 7.467 V 0 l -2.507, 2.507 C 13.013, 0.96, 10.885, 0, 8.528, 0 C 3.813, 0, 0.005, 3.819, 0.005, 8.533 s 3.808, 8.533, 8.523, 8.533 c 3.973, 0, 7.301 -2.72, 8.245 -6.4 h -2.219 c -0.88, 2.485 -3.237, 4.267 -6.027, 4.267 c -3.536, 0 -6.4 -2.864 -6.4 -6.4 s 2.864 -6.4, 6.4 -6.4 c 1.765, 0, 3.349, 0.736, 4.507, 1.893 l -3.44, 3.44 H 17.061 z"/></svg>`
})
})
}));
children.push(BDFDB.ReactUtils.createElement("div", {className: BDFDB.disCN.channelheaderdivider}));
};
children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Clickable, {
className: BDFDB.disCNS.channelheadericonwrapper + BDFDB.disCN.channelheadericonclickable,
onClick: _ => {this.window.minimize();},
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCNS.channelheadericon + "OTB-minButton",
iconSVG: `<svg width="26" height="26"><path stroke-width="2" stroke="currentColor" fill="none" d="M6 18 l13 0"/></svg>`
})
}));
children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Clickable, {
className: BDFDB.disCNS.channelheadericonwrapper + BDFDB.disCN.channelheadericonclickable,
onClick: _ => {
if (this.isMaximized()) this.window.unmaximize();
else this.window.maximize();
},
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCNS.channelheadericon + "OTB-maxButton",
iconSVG: this.isMaximized() ? `<svg width="26" height="26"><path stroke-width="2" stroke="currentColor" fill="none" d="M6 9 l10 0 l0 10 l-10 0 l0 -10 m3 -3 l10 0 l0 10"/></svg>` : `<svg width="26" height="26"><path stroke-width="2" stroke="currentColor" fill="none" d="M6 6 l13 0 l0 13 l-13 0 l0 -13"/></svg>`
})
}));
children.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Clickable, {
className: BDFDB.disCNS.channelheadericonwrapper + BDFDB.disCN.channelheadericonclickable,
onClick: _ => {this.window.close();},
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCNS.channelheadericon + "OTB-closeButton",
iconSVG: `<svg width="26" height="26"><path stroke-width="2" stroke="currentColor" fill="none" d="M6 6 l13 13 m0 -13 l-13 13"/></svg>`
})
}));
2018-10-11 10:21:26 +02:00
}
2019-09-04 12:34:02 +02:00
2019-03-06 11:30:31 +01:00
isMaximized () {
return window.screen.availWidth == window.outerWidth && window.screen.availHeight == window.outerHeight && window.screenX == 0 && window.screenY == 0;
}
2019-01-26 22:45:19 +01:00
2018-10-11 10:21:26 +02:00
patchMainScreen (enable) {
2018-10-19 11:40:39 +02:00
if (BdApi.getWindowPreference("frame") != enable) {
BdApi.setWindowPreference("frame", enable);
2018-10-11 10:21:26 +02:00
return true;
}
return false;
}
}