Implement loading themes from disk.

This fixes https://github.com/cmcaine/tridactyl/issues/491.
This commit is contained in:
glacambre 2018-07-29 10:29:38 +02:00
parent 048349bd37
commit 113fd9ef80
No known key found for this signature in database
GPG key ID: B9625DB1767553AC
5 changed files with 78 additions and 1 deletions

View file

@ -413,6 +413,12 @@ def handleMessage(message):
else:
reply["code"] = "File not found"
elif cmd == "getconfigpath":
reply["content"] = findUserConfigFile()
reply["code"] = 0
if reply["content"] == None:
reply["code"] = "Path not found"
elif cmd == "run":
commands = message["command"]
stdin = message.get("content", "").encode("utf-8")

View file

@ -20,6 +20,7 @@ type MessageCommand =
| "move"
| "eval"
| "getconfig"
| "getconfigpath"
| "env"
| "win_firefox_restart"
interface MessageResp {
@ -53,6 +54,15 @@ async function sendNativeMsg(
}
}
export async function getrcpath(): Promise <string> {
const res = await sendNativeMsg("getconfigpath", {})
if (res.code != 0)
throw new Error("getrcpath error: " + res.code)
return res.content
}
export async function getrc(): Promise<string> {
const res = await sendNativeMsg("getconfig", {})

View file

@ -18,11 +18,24 @@ function prefixTheme(name) {
// At the moment elements are only ever `:root` and so this array and stuff is all a bit overdesigned.
const THEMED_ELEMENTS = []
let insertedCSS = false
let customCss = {
allFrames: true,
matchAboutBlank: true,
code: "",
}
export async function theme(element) {
// Remove any old theme
for (let theme of THEMES.map(prefixTheme)) {
element.classList.remove(theme)
}
if (insertedCSS) {
// Typescript doesn't seem to be aware than remove/insertCSS's tabid
// argument is optional
await (browser.tabs.removeCSS as any)(customCss)
insertedCSS = false
}
let newTheme = await config.getAsync("theme")
@ -31,6 +44,17 @@ export async function theme(element) {
element.classList.add(prefixTheme(newTheme))
}
// Insert custom css if needed
if (newTheme !== "default" && !THEMES.includes(newTheme)) {
customCss.code = await config.getAsync("customthemes", newTheme)
if (customCss.code) {
await (browser.tabs.insertCSS as any)(customCss)
insertedCSS = true
} else {
logger.error("Theme " + newTheme + " couldn't be found.")
}
}
// Record for re-theming
// considering only elements :root (page and cmdline_iframe)
// TODO:

View file

@ -751,6 +751,44 @@ export function cssparse(...css: string[]) {
console.log(CSS.parse(css.join(" ")))
}
/** @hidden */
//#background
export async function loadtheme(themename: string) {
if (!(await Native.nativegate("0.1.8")))
return
const separator = (await browserBg.runtime.getPlatformInfo().os) == "win" ? "\\" : "/"
// remove the "tridactylrc" bit so that we're left with the directory
const path = (await Native.getrcpath()).split(separator).slice(0, -1).join(separator)
+ separator + "themes" + separator + themename
const file = await Native.read(path)
if (file.code != 0)
throw new Error("Couldn't read theme " + path)
return set("customthemes." + themename, file.content)
}
/** @hidden */
//#background
export async function unloadtheme(themename: string) {
unset("customthemes." + themename)
}
/** Changes the current theme.
*
* If THEMENAME is "dark", "default", "greenamt", "quake" or "shydactyl", the theme will be loaded from Tridactyl's internal storage.
*
* If THEMENAME is set to any other value, Tridactyl will attempt to use its native binary (see [[native]]) in order to load a CSS file named THEMENAME from disk. The CSS file has to be in a directory named "themes" and this directory has to be in the same directory as your tridactylrc.
*
* Example: `:colourscheme mysupertheme.css`
*/
//#background
export async function colourscheme(themename: string) {
// If this is a builtin theme, no need to bother with native messaging stuff
if (["dark", "default", "greenmat", "quake", "shydactyl"].includes(themename))
return set("theme", themename)
await loadtheme(themename)
return set("theme", themename)
}
/**
* Like [[fixamo]] but quieter.
*/

View file

@ -396,7 +396,6 @@ class default_config {
"!": "exclaim",
"!s": "exclaim_quiet",
containerremove: "containerdelete",
colourscheme: "set theme",
colours: "colourscheme",
colorscheme: "colourscheme",
colors: "colourscheme",