-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from author-more/feat/theme-support
feat: add themes support
- Loading branch information
Showing
10 changed files
with
232 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,112 @@ | ||
function ThemeCheck() { | ||
if (localStorage.getItem('theme') === 'light') { | ||
document.body.classList.add('theme-is-light') | ||
/** | ||
* @typedef {Parameters<typeof window.api.setTheme>[0]} ThemeId | ||
* @typedef {ThemeId | "tab"} ThemeSetting | ||
* @typedef {import("electron").IpcMessageEvent} IpcMessageEvent | ||
*/ | ||
|
||
const THEME_STORE_KEY = "theme"; | ||
const THEME_TAB_EVENTS = Object.freeze({ | ||
REQUEST_UPDATE: "theme-request-update", | ||
UPDATE: "theme-update", | ||
}); | ||
|
||
/** @type {ThemeSetting | null} */ | ||
let currentThemeSetting = null; | ||
|
||
window.addEventListener("DOMContentLoaded", () => { | ||
currentThemeSetting = /** @type {ThemeSetting | null} */ ( | ||
localStorage.getItem(THEME_STORE_KEY) | ||
); | ||
|
||
if (currentThemeSetting) { | ||
setTheme(currentThemeSetting); | ||
} | ||
|
||
prepareForm(currentThemeSetting); | ||
}); | ||
|
||
/** | ||
* @param {ThemeSetting | null} themeSetting | ||
*/ | ||
async function prepareForm(themeSetting) { | ||
const { themeSelect } = await getThemeSettingsForm(); | ||
|
||
if (themeSelect && themeSetting) { | ||
themeSelect.value = themeSetting; | ||
} | ||
|
||
themeSelect?.addEventListener("change", (event) => { | ||
const { target } = event; | ||
const value = target instanceof HTMLSelectElement && target.value; | ||
|
||
if (isThemeSetting(value)) { | ||
const isTabTheme = value === "tab"; | ||
|
||
currentThemeSetting = value; | ||
localStorage.setItem(THEME_STORE_KEY, value); | ||
|
||
if (isTabTheme) { | ||
requestTabTheme(); | ||
return; | ||
} | ||
|
||
setTheme(value); | ||
} else { | ||
document.body.classList.add('theme-is-dark') | ||
currentThemeSetting = null; | ||
localStorage.removeItem(THEME_STORE_KEY); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @param {string} themeId | ||
*/ | ||
function setTheme(themeId) { | ||
if (isThemeId(themeId)) { | ||
window.api.setTheme(themeId); | ||
} | ||
} | ||
|
||
async function getThemeSettingsForm() { | ||
const themeSelect = await getIncludedElement( | ||
"#theme-select", | ||
"#include-settings", | ||
HTMLSelectElement | ||
); | ||
|
||
return { themeSelect }; | ||
} | ||
|
||
/** | ||
* @param {string} inTabTheme | ||
*/ | ||
function handleInTabThemeUpdate(inTabTheme) { | ||
const shouldUseInTabTheme = currentThemeSetting === "tab"; | ||
|
||
if (shouldUseInTabTheme) { | ||
setTheme(inTabTheme); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {unknown} value | ||
* @returns {value is ThemeId} | ||
*/ | ||
function isThemeId(value) { | ||
return ( | ||
typeof value === "string" && ["light", "dark", "system"].includes(value) | ||
); | ||
} | ||
|
||
/** | ||
* | ||
* @param {unknown} value | ||
* @returns {value is ThemeSetting} | ||
*/ | ||
function isThemeSetting(value) { | ||
return ( | ||
typeof value === "string" && | ||
["light", "dark", "system", "tab"].includes(value) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
type Electron = import("electron"); | ||
|
||
declare var api: { | ||
send: (channel: string, data: unknown) => void; | ||
setTheme: (themeId: Electron.NativeTheme["themeSource"]) => void; | ||
onOpenTab: (callback: (href: string) => void) => void; | ||
onTabMenuAction: ( | ||
callback: ({ command: string, tabId: number }) => void | ||
) => void; | ||
}; | ||
|
||
declare var mainWindow: import("electron").BrowserWindow; | ||
declare var mainWindow: Electron.BrowserWindow; | ||
|
||
declare var transparent: boolean; | ||
declare var AppIcon: string; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.