Skip to content

Commit 5e48d61

Browse files
committed
fix settings
1 parent 41c8a07 commit 5e48d61

File tree

1 file changed

+75
-19
lines changed

1 file changed

+75
-19
lines changed

open-in-mpv.plugin.js

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,41 +50,97 @@ const contextMenuPatch = (tree, context) => {
5050
}
5151
};
5252

53-
function buildSetting(text, key, type, value, callback = () => {
54-
}) {
55-
const setting = Object.assign(document.createElement("div"), { className: "setting" });
56-
const label = Object.assign(document.createElement("span"), { textContent: text, className: "title-2yADjX" });
57-
const input = Object.assign(document.createElement("input"), { type: type, name: key, value: value });
58-
if (type === "checkbox") {
59-
input.classList.add("bd-switch");
60-
if (value) input.checked = true;
61-
}
53+
document.createSvgElement = (name) => { return document.createElementNS("http://www.w3.org/2000/svg", name) };
54+
55+
function buildCheckboxSetting(text, key, callback = () => {}) {
56+
const setting = document.createElement("div");
57+
setting.className = "flex_f5fbb7 vertical__1e37a justifyStart__42744 alignStretch_e239ef noWrap__5c413";
58+
setting.style.flex = "1 1 auto";
59+
60+
const rowWrapper = document.createElement("div");
61+
rowWrapper.className = "flex_f5fbb7 horizontal__992f6 justifyStart__42744 alignStart__4fe1e noWrap__5c413";
62+
rowWrapper.style.flex = "1 1 auto";
63+
64+
const titleWrapper = document.createElement("div");
65+
titleWrapper.className = "flexChild__6e093";
66+
titleWrapper.style.flex = "1 1 auto";
67+
68+
const title = document.createElement("div");
69+
title.className = "title__28a65";
70+
title.textContent = text;
71+
72+
const switchWrapper = document.createElement("div");
73+
switchWrapper.className = "bd-switch";
74+
if (settings[key]) switchWrapper.classList.add("bd-switch-checked");
75+
76+
const input = document.createElement("input");
77+
input.type = "checkbox";
78+
input.checked = settings[key];
79+
6280
input.addEventListener("change", () => {
63-
const newValue = type === "checkbox" ? input.checked : input.value;
81+
const newValue = input.checked;
82+
if (newValue) switchWrapper.classList.add("bd-switch-checked"); else switchWrapper.classList.remove("bd-switch-checked");
6483
settings[key] = newValue;
6584
BdApi.Data.save("open-in-mpv", "settings", settings);
6685
callback(newValue);
6786
});
68-
setting.append(label, input);
87+
88+
const switchBody = document.createElement("div");
89+
switchBody.className = "bd-switch-body";
90+
91+
const switchSlider = document.createSvgElement("svg");
92+
switchSlider.setAttribute("class", "bd-switch-slider");
93+
switchSlider.setAttribute("viewBox", "0 0 28 20");
94+
switchSlider.setAttribute("preserveAspectRatio", "xMinYMid meet");
95+
96+
const handle = document.createSvgElement("rect");
97+
handle.setAttribute("class", "bd-switch-handle");
98+
handle.setAttribute("fill", "white");
99+
handle.setAttribute("x", "4");
100+
handle.setAttribute("y", "0");
101+
handle.setAttribute("height", "20");
102+
handle.setAttribute("width", "20");
103+
handle.setAttribute("rx", "10");
104+
105+
const symbol = document.createSvgElement("svg");
106+
symbol.setAttribute("class", "bd-switch-symbol");
107+
symbol.setAttribute("viewBox", "0 0 20 20");
108+
symbol.setAttribute("fill", "none");
109+
110+
symbol.append(document.createSvgElement("path"), document.createSvgElement("path"));
111+
switchSlider.append(handle, symbol);
112+
switchBody.append(switchSlider);
113+
switchWrapper.append(input, switchBody);
114+
titleWrapper.append(title);
115+
rowWrapper.append(titleWrapper, switchWrapper);
116+
setting.append(rowWrapper);
117+
69118
return setting;
70119
}
71120

72121
// noinspection JSUnusedGlobalSymbols
73122
module.exports = () => ({
74123
start() {
75-
console.log("Open in mpv plugin enabled");
76-
77-
Object.assign(settings, BdApi.Data.load("open-in-mpv", "settings"));
124+
const saved = BdApi.Data.load("open-in-mpv", "settings");
125+
if (saved && !isEmpty(saved)) Object.assign(settings, saved);
78126
BdApi.ContextMenu.patch("message", contextMenuPatch);
79127
}, stop() {
80128
BdApi.ContextMenu.unpatch("message", contextMenuPatch);
81129
}, getSettingsPanel: () => {
82-
const mySettingsPanel = document.createElement("div", { className: "bd-addon-settings-wrap" });
83-
mySettingsPanel.id = "my-settings";
130+
const mySettingsPanel = Object.assign(document.createElement("div"));
84131

85-
const showAgain = buildSetting("Show Dialog", "showAgain", "checkbox", settings.showAgain);
132+
mySettingsPanel.append(buildCheckboxSetting("Show Dialog", "showAgain"));
86133

87-
mySettingsPanel.append(showAgain);
88134
return mySettingsPanel;
89-
}
135+
},
90136
});
137+
138+
function isEmpty(obj) {
139+
for (const prop in obj) {
140+
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
141+
return false;
142+
}
143+
}
144+
145+
return true
146+
}

0 commit comments

Comments
 (0)