Skip to content

Commit

Permalink
Merge pull request #4 from isaxk/dev
Browse files Browse the repository at this point in the history
Custom css theming support and force cinema mode
  • Loading branch information
isaxk authored Aug 31, 2024
2 parents 9ee788d + 6518e63 commit 947b53d
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 95 deletions.
1 change: 0 additions & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { discordClient } from "./intergrations/discord";
import { initPlayerEvents } from "./player";
import { createTabManager } from "./tabs";
import { createMainWindowManager, MainWindowManager } from "./windows";
import { downloadCss, loadCss } from "./utils/customcss";

const store = factory();

Expand Down
2 changes: 2 additions & 0 deletions src/main/player/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export function initPlayerEvents(mainWindowManager: MainWindowManager) {
}
});

mainWindowManager.window.webContents.send("video-data-changed", null);

ipcMain.on("ytmView:storeStateChanged", (_, _queue, _like, v) => {
volume = v;
mainWindowManager.window.webContents.send("volume-changed", v);
Expand Down
31 changes: 19 additions & 12 deletions src/main/tabs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ function windowOpenHandler(details): WindowOpenHandlerResponse {
};
}

async function updateCustomCss(
key: string,
oldCssKey: string | null,
view: WebContentsView,
) {
async function updateCustomCss(key: string, view: WebContentsView) {
let ytCss = JSON.parse(store.get(key, "")!);
return new Promise<string | null>(async (resolve) => {
if (ytCss.enabled===true) {
if (ytCss.enabled === true) {
if (ytCss.type === "url") {
await loadCss(ytCss.url).then(async (css) => {
resolve(await view.webContents.insertCSS(css));
Expand Down Expand Up @@ -92,7 +88,7 @@ export async function createTabManager(mainWindow: BrowserWindow) {
});

view.webContents.on("dom-ready", async () => {
await updateCustomCss("music-css", oldCssKey, view).then((key) => {
await updateCustomCss("music-css", view).then((key) => {
oldCssKey = key;
});
});
Expand Down Expand Up @@ -141,11 +137,12 @@ export async function createTabManager(mainWindow: BrowserWindow) {
},
updateCss: async () => {
if (oldCssKey !== null) {
await view.webContents.removeInsertedCSS(oldCssKey).then(()=>console.log("remove"));
await view.webContents
.removeInsertedCSS(oldCssKey)
.then(() => console.log("remove"));
}
await updateCustomCss("music-css", oldCssKey, view).then((key) => {
await updateCustomCss("music-css", view).then((key) => {
oldCssKey = key;

});
},
};
Expand Down Expand Up @@ -261,9 +258,10 @@ export async function createTabManager(mainWindow: BrowserWindow) {
let oldCssKey: string | null = null;

view.webContents.on("dom-ready", async () => {
await updateCustomCss("yt-css", oldCssKey, view).then((key) => {
await updateCustomCss("yt-css", view).then((key) => {
oldCssKey = key;
});
view.webContents.send("force-cinema", store.get("force-cinema", false));
});

return {
Expand All @@ -286,7 +284,7 @@ export async function createTabManager(mainWindow: BrowserWindow) {
if (oldCssKey !== null) {
await view.webContents.removeInsertedCSS(oldCssKey);
}
await updateCustomCss("yt-css", oldCssKey, view).then((key) => {
await updateCustomCss("yt-css", view).then((key) => {
oldCssKey = key;
});
},
Expand Down Expand Up @@ -399,6 +397,15 @@ export async function createTabManager(mainWindow: BrowserWindow) {
}
});

ipcMain.on("set-config", (_, e) => {
if (e.key === "force-cinema") {
tabs.forEach((tab) => {
tab.data().type === "yt" ??
tab.view.webContents.send("force-cinema", e.value);
});
}
});

return {
getTabs: () => {
return tabs;
Expand Down
37 changes: 18 additions & 19 deletions src/main/utils/customcss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,27 @@ export function downloadCss(url: string) {
// );
// });

export function loadCss(url:string) {
export function loadCss(url: string) {
return new Promise<string>((resolve) => {
if(url==="") {
if (url === "") {
resolve("");
return;
}
https.get(url, res => {
let data = ''
const headerDate = res.headers && res.headers.date ? res.headers.date : 'no response date';

// @ts-ignore
res.on('data', (chunk:any) => {
data += chunk;
});

res.on('end', () => {
resolve(data)
https
.get(url, (res) => {
let data = "";

// @ts-ignore
res.on("data", (chunk: any) => {
data += chunk;
});

res.on("end", () => {
resolve(data);
});
})
.on("error", (err) => {
console.log("Error: ", err.message);
});
}).on('error', err => {
console.log('Error: ', err.message);
});
}
)

});
}
10 changes: 5 additions & 5 deletions src/main/windows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ function updateTheme(mainWindow: BrowserWindow) {
}

switch (store.get("theme")) {
case "system":
bgColor = nativeTheme.shouldUseDarkColors ? darkBg : lightBg;
break;
case "light":
bgColor = lightBg;
break;
case "dark":
bgColor = darkBg;
break;
default:
bgColor = nativeTheme.shouldUseDarkColors ? darkBg : lightBg;
break;
}

export type MainWindowManager = {
Expand All @@ -57,7 +57,7 @@ app.on("before-quit", () => {
export function createMainWindowManager() {
const mainWindow = new BrowserWindow({
minWidth: 800,
minHeight: 450,
minHeight: 670,
width: 1200,
height: 700,
titleBarStyle: "hiddenInset",
Expand Down Expand Up @@ -114,7 +114,7 @@ export function createMainWindowManager() {
mainWindow.setMaximizable(true);
mainWindow.setFullScreenable(true);
mainWindow.setAlwaysOnTop(false);
mainWindow.setMinimumSize(800, 450);
mainWindow.setMinimumSize(800, 670);
});

mainWindow.on("close", (e) => {
Expand Down
2 changes: 1 addition & 1 deletion src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const api = {
ipcRenderer.send("close-miniplayer");
},
onVideoDataChange: (listener: Function) => {
ipcRenderer.on("video-data-changed", (_, data) => {
ipcRenderer.on("video-data-changed", (_, data: musicData) => {
listener(data);
});
},
Expand Down
13 changes: 6 additions & 7 deletions src/preload/music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,12 +800,12 @@ async function hookPlayerApiEvents() {
(await webFrame.executeJavaScript(hookPlayerApiEventsScript))();
}

function overrideHistoryButtonDisplay() {
// @ts-expect-error Style is reported as readonly but this still works
document.querySelector<HTMLElement>(
"#history-link tp-yt-paper-icon-button",
).style = "display: inline-block !important;";
}
// function overrideHistoryButtonDisplay() {
// // @ts-expect-error Style is reported as readonly but this still works
// document.querySelector<HTMLElement>(
// "#history-link tp-yt-paper-icon-button",
// ).style = "display: inline-block !important;";
// }

function getYTMTextRun(runs: { text: string }[]) {
let final = "";
Expand Down Expand Up @@ -932,7 +932,6 @@ window.addEventListener("load", async () => {
await createAdditionalPlayerBarControls();
await hideChromecastButton();
await hookPlayerApiEvents();
overrideHistoryButtonDisplay();

// const integrationScripts: { [integrationName: string]: { [scriptName: string]: string } } = await ipcRenderer.invoke("ytmView:getIntegrationScripts");

Expand Down
16 changes: 16 additions & 0 deletions src/preload/yt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,19 @@ ipcRenderer.on("picture-in-picture", async () => {
`)
)();
});

ipcRenderer.on("force-cinema", async (_, is: boolean) => {
if (is) {
(
await webFrame.executeJavaScript(`
document.cookie = 'wide=1; expires='+new Date('3099').toUTCString()+'; path=/';
`)
)();
} else {
(
await webFrame.executeJavaScript(`
document.cookie = 'wide=0; expires='+new Date('3099').toUTCString()+'; path=/';
`)
)();
}
});
14 changes: 7 additions & 7 deletions src/renderer/src/components/settings/SettingItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
key,
JSON.stringify({
...value,
enabled: e
enabled: e,
}),
);
}}
Expand Down Expand Up @@ -105,19 +105,19 @@
}, 400);
}}
>
<div class="mb-4 rounded-md border dark:border-neutral-600">
<div class="pb-4 rounded-md border dark:border-neutral-600">
<Tabs.List
class="flex w-full justify-stretch rounded bg-neutral-800 p-1 text-center"
class="flex w-full justify-stretch rounded bg-zinc-200 p-1 text-center dark:bg-neutral-800"
>
<Tabs.Trigger
value="url"
class="w-full rounded p-1 data-[state=active]:bg-neutral-700"
class="w-full rounded p-1 data-[state=active]:bg-white data-[state=active]:font-medium data-[state=active]:dark:bg-neutral-700"
>URL</Tabs.Trigger
>
<Tabs.Trigger
value="editor"
class="w-full rounded p-1 data-[state=active]:bg-neutral-700"
>Editor</Tabs.Trigger
class="w-full rounded p-1 data-[state=active]:bg-white data-[state=active]:font-medium data-[state=active]:dark:bg-neutral-700"
>Editor <span class="text-xs font-light">(advanced)</span></Tabs.Trigger
>
</Tabs.List>
<div
Expand Down Expand Up @@ -152,7 +152,7 @@
}),
);
}}
class="h-full w-full bg-transparent p-2 font-mono outline-none text-sm"
class="h-full w-full bg-transparent p-2 font-mono text-sm outline-none"
placeholder="Paste or type css code here..."
></textarea>
</Tabs.Content>
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/src/components/settings/SidebarTab.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

<Tabs.Trigger
{value}
class="text-left p-4 transition-[background-color] flex rounded items-center text-neutral-600 dark:text-zinc-300 gap-2 w-full hover:text-current data-[state=active]:bg-zinc-200 data-[state=active]:dark:bg-neutral-700 data-[state=active]:text-current"
class="flex w-full items-center gap-2 rounded p-4 text-left text-neutral-600 transition-[background-color] hover:text-current data-[state=active]:bg-zinc-200 data-[state=active]:text-current dark:text-zinc-300 data-[state=active]:dark:bg-neutral-700 {value ===
'about' ?? 'self-end mt-auto'}"
>
<div class="">
<svelte:component this={icon} size={18} />
Expand Down
Loading

0 comments on commit 947b53d

Please sign in to comment.