BetterDiscordAddons/Plugins/OldTitleBar/OldTitleBar.plugin.js

320 lines
12 KiB
JavaScript
Raw Normal View History

2020-10-20 23:25:34 +02:00
/**
* @name OldTitleBar
2021-03-05 13:26:41 +01:00
* @author DevilBro
2020-10-20 23:25:34 +02:00
* @authorId 278543574059057154
2022-10-29 12:13:39 +02:00
* @version 1.7.6
2021-04-23 12:01:44 +02:00
* @description Allows you to switch to Discord's old Titlebar
2020-10-20 23:25:34 +02:00
* @invite Jx3TjNS
* @donate https://www.paypal.me/MircoWittrien
* @patreon https://www.patreon.com/MircoWittrien
2021-03-09 15:10:55 +01:00
* @website https://mwittrien.github.io/
* @source https://github.com/mwittrien/BetterDiscordAddons/tree/master/Plugins/OldTitleBar/
2021-03-10 09:17:37 +01:00
* @updateUrl https://mwittrien.github.io/BetterDiscordAddons/Plugins/OldTitleBar/OldTitleBar.plugin.js
2020-10-20 23:25:34 +02:00
*/
2019-03-22 20:54:11 +01:00
2020-09-19 20:49:33 +02:00
module.exports = (_ => {
2022-09-01 14:40:11 +02:00
const changeLog = {
2022-09-02 12:37:10 +02:00
2020-09-19 20:49:33 +02:00
};
2020-11-13 19:47:44 +01:00
2022-02-05 21:14:17 +01:00
return !window.BDFDB_Global || (!window.BDFDB_Global.loaded && !window.BDFDB_Global.started) ? class {
2022-09-01 14:55:22 +02:00
constructor (meta) {for (let key in meta) this[key] = meta[key];}
getName () {return this.name;}
getAuthor () {return this.author;}
getVersion () {return this.version;}
getDescription () {return `The Library Plugin needed for ${this.name} is missing. Open the Plugin Settings to download it. \n\n${this.description}`;}
2021-02-01 17:13:13 +01:00
downloadLibrary () {
require("request").get("https://mwittrien.github.io/BetterDiscordAddons/Library/0BDFDB.plugin.js", (e, r, b) => {
2021-03-05 13:14:18 +01:00
if (!e && b && r.statusCode == 200) require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0BDFDB.plugin.js"), b, _ => BdApi.showToast("Finished downloading BDFDB Library", {type: "success"}));
2021-03-06 14:59:48 +01:00
else BdApi.alert("Error", "Could not download BDFDB Library Plugin. Try again later or download it manually from GitHub: https://mwittrien.github.io/downloader/?library");
2021-02-01 17:13:13 +01:00
});
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
load () {
2020-11-19 16:51:14 +01:00
if (!window.BDFDB_Global || !Array.isArray(window.BDFDB_Global.pluginQueue)) window.BDFDB_Global = Object.assign({}, window.BDFDB_Global, {pluginQueue: []});
2020-09-19 20:49:33 +02:00
if (!window.BDFDB_Global.downloadModal) {
window.BDFDB_Global.downloadModal = true;
2022-09-01 14:55:22 +02:00
BdApi.showConfirmationModal("Library Missing", `The Library Plugin needed for ${this.name} is missing. Please click "Download Now" to install it.`, {
2020-09-19 20:49:33 +02:00
confirmText: "Download Now",
cancelText: "Cancel",
onCancel: _ => {delete window.BDFDB_Global.downloadModal;},
2020-09-20 08:15:13 +02:00
onConfirm: _ => {
delete window.BDFDB_Global.downloadModal;
2021-02-01 17:13:13 +01:00
this.downloadLibrary();
2020-09-20 08:15:13 +02:00
}
2020-09-19 20:49:33 +02:00
});
}
2022-09-01 14:55:22 +02:00
if (!window.BDFDB_Global.pluginQueue.includes(this.name)) window.BDFDB_Global.pluginQueue.push(this.name);
2020-10-09 21:09:35 +02:00
}
2021-01-06 12:38:36 +01:00
start () {this.load();}
stop () {}
getSettingsPanel () {
2020-11-28 23:12:09 +01:00
let template = document.createElement("template");
2022-09-01 14:55:22 +02:00
template.innerHTML = `<div style="color: var(--header-primary); font-size: 16px; font-weight: 300; white-space: pre; line-height: 22px;">The Library Plugin needed for ${this.name} is missing.\nPlease click <a style="font-weight: 500;">Download Now</a> to install it.</div>`;
2021-02-01 17:13:13 +01:00
template.content.firstElementChild.querySelector("a").addEventListener("click", this.downloadLibrary);
2020-11-28 23:12:09 +01:00
return template.content.firstElementChild;
}
2020-10-09 21:09:35 +02:00
} : (([Plugin, BDFDB]) => {
2022-05-25 10:28:30 +02:00
var _this;
2021-02-03 12:27:13 +01:00
var patched, lastWindowRects;
2022-05-25 10:28:30 +02:00
var toolbars = [];
const OldTitleBarToolbarComponent = class OldTitleBarToolbar extends BdApi.React.Component {
componentDidMount() {
if (toolbars.indexOf(this) == -1) toolbars.push(this);
}
componentWillUnmount() {
BDFDB.ArrayUtils.remove(toolbars, this, true);
}
render() {
let children = [];
if (this.props.addFirstDivider) children.push(BDFDB.ReactUtils.createElement("div", {className: BDFDB.disCN.channelheaderdivider}))
if (_this.settings.general.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: _ => location.reload(),
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.channelheadericon,
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: _ => BDFDB.LibraryModules.WindowUtils.minimize(),
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.channelheadericon,
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: _ => _this.maximize(),
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.channelheadericon,
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: _ => BDFDB.LibraryModules.WindowUtils.close(),
children: BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SvgIcon, {
className: BDFDB.disCN.channelheadericon,
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>`
})
}));
2022-06-17 19:39:05 +02:00
return BDFDB.ReactUtils.createElement("div", {
className: BDFDB.disCN._oldtitlebartoolbar,
children: children
});
2022-05-25 10:28:30 +02:00
}
};
2020-09-19 20:49:33 +02:00
2020-10-09 21:09:35 +02:00
return class OldTitleBar extends Plugin {
2021-01-06 12:38:36 +01:00
onLoad () {
2022-05-25 10:28:30 +02:00
_this = this;
2020-09-19 20:49:33 +02:00
this.defaults = {
2021-04-21 20:20:36 +02:00
general: {
addToSettings: {value: true, description: "Adds a Titlebar to Settings Windows"},
2022-05-25 10:28:30 +02:00
reloadButton: {value: true, description: "Adds a Reload Button to the Titlebar"}
2020-09-19 20:49:33 +02:00
}
};
2019-11-12 15:42:23 +01:00
2022-10-29 12:13:39 +02:00
this.modulePatches = {
before: [
"HeaderBar"
],
after: [
"AuthWrapper",
"StandardSidebarView"
]
2020-09-19 20:49:33 +02:00
};
2020-02-11 12:43:04 +01:00
2020-09-19 20:49:33 +02:00
this.css = `
${BDFDB.dotCNS._oldtitlebarenabled + BDFDB.dotCN.titlebar},
${BDFDB.dotCNS._oldtitlebarenabled + BDFDB.dotCN.splashbackground}:before {
display: none !important;
}
2021-06-14 13:42:58 +02:00
${BDFDB.dotCNS._oldtitlebarenabled + BDFDB.dotCN.guildswrapper} {
2020-09-19 20:49:33 +02:00
margin-top: 0;
padding-top: 0;
}
2021-06-14 13:42:58 +02:00
${BDFDB.dotCNS._oldtitlebarenabled + BDFDB.dotCN.guildsscroller} {
2020-09-19 20:49:33 +02:00
padding-top: 4px;
}
2021-06-14 13:42:58 +02:00
${BDFDB.dotCNS._oldtitlebarenabled + BDFDB.dotCN.settingswindowstandardsidebarview}:before {
2020-09-19 20:49:33 +02:00
display: none;
}
${BDFDB.dotCN._oldtitlebartoolbar} {
2022-06-17 19:39:05 +02:00
display: flex;
flex: 1 0 auto;
2020-09-19 20:49:33 +02:00
justify-content: flex-end;
}
2022-06-17 19:39:05 +02:00
${BDFDB.dotCNS.chatthreadsidebaropen + BDFDB.dotCN._oldtitlebartoolbar},
${BDFDB.dotCNS.callcurrentchatsidebaropen + BDFDB.dotCN._oldtitlebartoolbar} {
display: none;
}
2020-09-19 20:49:33 +02:00
${BDFDB.dotCN._oldtitlebarsettingstoolbar} {
2021-04-30 12:03:42 +02:00
display: flex;
2020-09-19 20:49:33 +02:00
position: absolute;
top: 0;
right: 0;
padding: 10px;
z-index: 2;
-webkit-app-region: drag !important;
}
${BDFDB.dotCNS._oldtitlebarenabled + BDFDB.dotCNS.splashbackground + BDFDB.dotCN._oldtitlebarsettingstoolbar} {
background: rgba(0, 0, 0, 0.3);
border-radius: 0 0 0 5px;
}
2021-06-14 13:42:58 +02:00
${BDFDB.dotCN.channelheaderheaderbar},
${BDFDB.dotCNS.channelheaderheaderbar + BDFDB.dotCN.channelheaderchildren},
${BDFDB.dotCNS.channelheaderheaderbar + BDFDB.dotCN.channelheadertoolbar} {
2020-09-19 20:49:33 +02:00
-webkit-app-region: drag !important;
}
${BDFDB.dotCNS.stopanimations + BDFDB.dotCN.channelheaderheaderbar},
${BDFDB.dotCN.channelheaderheaderbar} * {
-webkit-app-region: no-drag !important;
}
`;
2020-02-11 12:43:04 +01:00
}
2021-01-06 12:38:36 +01:00
onStart () {
2020-02-11 12:43:04 +01:00
BDFDB.ListenerUtils.add(this, window, "resize", e => {
2022-05-25 10:28:30 +02:00
BDFDB.ReactUtils.forceUpdate(toolbars);
2020-02-11 12:43:04 +01:00
});
2019-01-26 22:45:19 +01:00
2020-02-11 12:43:04 +01:00
BDFDB.DOMUtils.addClass(document.body, BDFDB.disCN._oldtitlebarenabled);
2019-01-26 22:45:19 +01:00
2022-05-25 10:28:30 +02:00
BDFDB.DiscordUtils.rerenderAll();
2020-02-11 12:43:04 +01:00
}
2020-09-19 20:49:33 +02:00
2021-01-06 12:38:36 +01:00
onStop () {
2022-05-25 10:28:30 +02:00
BDFDB.DiscordUtils.rerenderAll();
2019-01-26 22:45:19 +01:00
2020-02-11 12:43:04 +01:00
BDFDB.DOMUtils.removeClassFromDOM(BDFDB.disCN._oldtitlebarenabled);
2020-09-19 20:49:33 +02:00
}
2019-01-26 22:45:19 +01:00
2020-09-19 20:49:33 +02:00
getSettingsPanel (collapseStates = {}) {
2021-04-21 20:20:36 +02:00
let settingsPanel;
return settingsPanel = BDFDB.PluginUtils.createSettingsPanel(this, {
collapseStates: collapseStates,
children: _ => {
let settingsItems = [];
for (let key in this.defaults.general) settingsItems.push(BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.SettingsSaveItem, {
type: "Switch",
plugin: this,
keys: ["general", key],
label: this.defaults.general[key].description,
value: this.settings.general[key],
}));
return settingsItems;
}
});
2020-02-11 12:43:04 +01:00
}
2018-10-11 10:21:26 +02:00
2021-01-06 12:38:36 +01:00
onSettingsClosed () {
2020-09-19 20:49:33 +02:00
if (this.SettingsUpdated) {
delete this.SettingsUpdated;
2019-01-26 22:45:19 +01:00
2022-05-25 10:28:30 +02:00
BDFDB.DiscordUtils.rerenderAll();
2020-09-19 20:49:33 +02:00
}
}
2022-06-17 19:39:05 +02:00
2022-05-25 10:28:30 +02:00
processHeaderBar (e) {
let wrapper = BDFDB.ReactUtils.findChild(e.instance, {props: ["toolbar", "children"]});
if (!wrapper) return;
2022-06-17 19:39:05 +02:00
let children = BDFDB.ArrayUtils.is(wrapper.props.toolbar) ? wrapper.props.toolbar : BDFDB.ObjectUtils.get(wrapper, "props.toolbar.props.children");
2020-09-19 20:49:33 +02:00
if (!children) {
2022-05-28 19:56:39 +02:00
children = [];
wrapper.props.toolbar = [
wrapper.props.toolbar,
BDFDB.ReactUtils.createElement(BDFDB.LibraryComponents.Flex, {children})
];
2020-09-19 20:49:33 +02:00
}
this.injectButtons(children, true);
}
2019-01-26 22:45:19 +01:00
2022-10-29 12:13:39 +02: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);
}
2020-09-19 20:49:33 +02:00
processStandardSidebarView (e) {
2022-08-20 20:11:44 +02:00
let sidebarView = BDFDB.ReactUtils.findChild(e.returnvalue, {props: [["className", BDFDB.disCN.settingswindowstandardsidebarview]]});
if (!sidebarView) return;
if (!BDFDB.ArrayUtils.is(sidebarView.props.children)) sidebarView.props.children = [sidebarView.props.children];
this.injectSettingsToolbar(sidebarView.props.children);
2020-09-19 20:49:33 +02:00
}
injectSettingsToolbar (children, fixed) {
2021-04-21 20:20:36 +02:00
if (!this.settings.general.addToSettings) return;
2020-02-11 12:43:04 +01:00
let toolbar = BDFDB.ReactUtils.createElement("div", {
2021-04-30 12:03:42 +02:00
className: BDFDB.disCN._oldtitlebarsettingstoolbar,
2020-09-19 20:49:33 +02:00
children: [],
style: fixed ? {position: "fixed"} : null
2020-02-11 12:43:04 +01:00
});
2020-09-19 20:49:33 +02:00
this.injectButtons(toolbar.props.children);
children.push(toolbar);
2020-02-11 12:43:04 +01:00
}
2020-09-19 20:49:33 +02:00
injectButtons (children, addFirstDivider) {
2022-05-25 10:28:30 +02:00
children.push(BDFDB.ReactUtils.createElement(OldTitleBarToolbarComponent, {addFirstDivider: addFirstDivider}));
2020-09-19 20:49:33 +02:00
}
2021-02-02 12:45:38 +01:00
isMaximized () {
2021-02-03 12:27:13 +01:00
let rects = this.getWindowRects();
return rects.x == 0 && rects.y == 0 && this.isScreenSize(rects);
}
isScreenSize (rects) {
return screen.availWidth - rects.width == 0 && screen.availHeight - rects.height == 0;
}
maximize () {
if (!this.isMaximized()) {
lastWindowRects = this.getWindowRects();
BDFDB.LibraryModules.WindowUtils.maximize();
}
else {
2021-06-21 22:00:21 +02:00
BDFDB.LibraryModules.WindowUtils.maximize();
BDFDB.TimeUtils.timeout(_ => {
if (!this.isMaximized()) return;
if (!lastWindowRects || this.isScreenSize(lastWindowRects)) {
let rects = this.getWindowRects();
window.resizeTo(rects.width/2, rects.height/2);
window.moveTo(rects.width/4, rects.height/4);
}
else {
window.resizeTo(lastWindowRects.width, lastWindowRects.height);
window.moveTo(lastWindowRects.x, lastWindowRects.y);
}
}, 100);
2021-02-03 12:27:13 +01:00
}
}
getWindowRects () {
return {x: window.screenX, y: window.screenY, width: window.outerWidth, height: window.outerHeight};
2021-02-02 12:45:38 +01:00
}
2020-09-19 20:49:33 +02:00
};
2022-09-01 14:40:11 +02:00
})(window.BDFDB_Global.PluginUtils.buildPlugin(changeLog));
2021-04-04 10:46:46 +02:00
})();