Skip to content

Commit

Permalink
refactor: use ctx.subscriptions, optimize types
Browse files Browse the repository at this point in the history
  • Loading branch information
nekowinston committed Sep 8, 2023
1 parent aea8f71 commit c3697da
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 76 deletions.
28 changes: 16 additions & 12 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { workspace, ConfigurationChangeEvent, window } from "vscode";
import {
workspace,
ConfigurationChangeEvent,
window,
ExtensionContext,
} from "vscode";

export const activate = () => {
// regenerate the theme files when the config changes
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration("catppuccin")) {
window.showInformationMessage(
"VSCode Web doesn't support advanced Catppuccin options at the moment.",
);
}
});
export const activate = (ctx: ExtensionContext) => {
ctx.subscriptions.push(
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration("catppuccin")) {
window.showErrorMessage(
"VSCode Web doesn't support advanced Catppuccin options at the moment.",
);
}
}),
);
};

export const deactivate = () => {};
66 changes: 35 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
workspace,
ConfigurationChangeEvent,
extensions,
ExtensionContext,
Uri,
extensions,
workspace,
} from "vscode";
import utils, { UpdateTrigger } from "./utils";
import type { ThemePaths } from "./types";
Expand All @@ -26,35 +26,39 @@ export const activate = async (ctx: ExtensionContext) => {
);
}

// regenerate the theme files when the config changes
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (
event.affectsConfiguration("workbench.colorTheme") &&
extensions.getExtension("catppuccin.catppuccin-vsc-icons")
) {
const theme = workspace
ctx.subscriptions.push(
// regenerate the theme files when the config changes
workspace.onDidChangeConfiguration((event) => handler(event, paths)),
);
};

const handler = (event: ConfigurationChangeEvent, paths: ThemePaths) => {
const id = "catppuccin.catppuccin-vsc-icons";
const iconsInstalled = extensions.getExtension(id).isActive;
const iconsAffected = event.affectsConfiguration("workbench.colorTheme");

if (iconsInstalled && iconsAffected) {
const theme = workspace
.getConfiguration("workbench")
.get<string>("colorTheme");
const ctp_themes = {
"Catppuccin Latte": "catppuccin-latte",
"Catppuccin Frappé": "catppuccin-frappe",
"Catppuccin Macchiato": "catppuccin-macchiato",
"Catppuccin Mocha": "catppuccin-mocha",
};
if (Object.keys(ctp_themes).includes(theme)) {
workspace
.getConfiguration("workbench")
.get<string>("colorTheme");
const ctp_themes = {
"Catppuccin Latte": "catppuccin-latte",
"Catppuccin Frappé": "catppuccin-frappe",
"Catppuccin Macchiato": "catppuccin-macchiato",
"Catppuccin Mocha": "catppuccin-mocha",
};
if (Object.keys(ctp_themes).includes(theme)) {
workspace
.getConfiguration("workbench")
.update("iconTheme", ctp_themes[theme], true);
}
}
if (event.affectsConfiguration("catppuccin")) {
utils.updateThemes(
utils.getConfiguration(),
paths,
UpdateTrigger.CONFIG_CHANGE,
);
.update("iconTheme", ctp_themes[theme], true);
}
});
};
}

export const deactivate = () => {};
if (event.affectsConfiguration("catppuccin")) {
utils.updateThemes(
utils.getConfiguration(),
paths,
UpdateTrigger.CONFIG_CHANGE,
);
}
};
34 changes: 5 additions & 29 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { variants } from "@catppuccin/palette";
import { labels, variants } from "@catppuccin/palette";

import type { Uri } from "vscode";

Expand Down Expand Up @@ -29,35 +29,11 @@ export type CatppuccinBracketMode =
| "monochromatic"
| "neovim";

export interface CatppuccinPalette {
export type CatppuccinPalette = {
name: CatppuccinFlavour;
rosewater: string;
flamingo: string;
pink: string;
mauve: string;
red: string;
maroon: string;
peach: string;
yellow: string;
green: string;
teal: string;
sky: string;
sapphire: string;
blue: string;
lavender: string;
text: string;
subtext1: string;
subtext0: string;
overlay2: string;
overlay1: string;
overlay0: string;
surface2: string;
surface1: string;
surface0: string;
base: string;
mantle: string;
crust: string;
}
} & {
[k in keyof typeof labels]: string;
};

export type ColorOverrides = {
all?: Partial<CatppuccinPalette>;
Expand Down
5 changes: 1 addition & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ class Utils {
});

Promise.all(promises)
.then((x) => {
window.showInformationMessage(
"Catppuccin: Themes updated. " + JSON.stringify(x),
);
.then(() => {
this.promptToReload(trigger);
})
.catch((err) => {
Expand Down

0 comments on commit c3697da

Please sign in to comment.