Skip to content

Commit

Permalink
Color-scheme swapping works for VSCode
Browse files Browse the repository at this point in the history
  • Loading branch information
tmr232 committed Jan 16, 2025
1 parent 41532e3 commit 11d20a4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
11 changes: 10 additions & 1 deletion src/jetbrains/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
let highlight = true;
// Set initial background color
const colorList = (function () {
let colorList = (function () {
function isDarkTheme(): boolean {
return document.body.dataset.theme !== "light";
}
Expand All @@ -92,6 +92,7 @@
simplify?: boolean;
flatSwitch?: boolean;
highlight?: boolean;
colorList?: ColorList;
};
type State = {
code?: string;
Expand Down Expand Up @@ -147,6 +148,14 @@
});
break;
}
case "updateSettings":
flatSwitch = message.flatSwitch;
simplify = message.simplify;
highlight = message.highlightCurrentNode;
colorList = message.colorList;
document.body.style.backgroundColor = colorList.find(
({ name }) => name === "graph.background",
).hex;
}
});
Expand Down
29 changes: 20 additions & 9 deletions src/vscode/extension.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as vscode from "vscode";
import type { Language } from "../control-flow/cfg";
import {
type ColorScheme,
type ColorList,
deserializeColorList,
getDarkColorList,
getLightColorList,
listToScheme,
} from "../control-flow/colors";
import type { UpdateCode } from "./messages.ts";
import type { UpdateCode, UpdateSettings } from "./messages.ts";
import { OverviewViewProvider } from "./overview-view";

// ADD-LANGUAGES-HERE
Expand Down Expand Up @@ -58,7 +57,7 @@ type Settings = {
flatSwitch: boolean;
simplify: boolean;
highlightCurrentNode: boolean;
colorScheme: ColorScheme;
colorList: ColorList;
};
type ColorSchemeOptions = "Light" | "Dark" | "Custom" | "System";
function loadSettings(): Settings {
Expand Down Expand Up @@ -91,7 +90,7 @@ function loadSettings(): Settings {
flatSwitch: config.get("flatSwitch") ?? false,
simplify: config.get("simplify") ?? false,
highlightCurrentNode: config.get("highlightCurrentNode") ?? true,
colorScheme: listToScheme(colorList),
colorList: colorList,
};
}

Expand Down Expand Up @@ -159,17 +158,29 @@ export async function activate(context: vscode.ExtensionContext) {
// TODO: This currently only changes the color-scheme.
// TODO: Make this react to all the CFG settings.
if (e.affectsConfiguration("functionGraphOverview")) {
const _settings = loadSettings();
// TODO: Trigger a redraw
const settings = loadSettings();
provider.postMessage<UpdateSettings>({
tag: "updateSettings",
flatSwitch: settings.flatSwitch,
simplify: settings.simplify,
highlightCurrentNode: settings.highlightCurrentNode,
colorList: settings.colorList,
});
}
},
),
);

context.subscriptions.push(
vscode.window.onDidChangeActiveColorTheme(() => {
const _settings = loadSettings();
// TODO: Trigger a redraw
const settings = loadSettings();
provider.postMessage<UpdateSettings>({
tag: "updateSettings",
flatSwitch: settings.flatSwitch,
simplify: settings.simplify,
highlightCurrentNode: settings.highlightCurrentNode,
colorList: settings.colorList,
});
}),
);

Expand Down
11 changes: 10 additions & 1 deletion src/vscode/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Language } from "../control-flow/cfg.ts";
import type { ColorList } from "../control-flow/colors.ts";

export type NavigateTo = {
tag: "navigateTo";
Expand All @@ -12,7 +13,15 @@ export type UpdateCode = {
code: string;
};

export type MessageToWebview = UpdateCode;
export type UpdateSettings = {
tag: "updateSettings";
flatSwitch: boolean;
simplify: boolean;
highlightCurrentNode: boolean;
colorList: ColorList;
};

export type MessageToWebview = UpdateCode | UpdateSettings;
export type MessageToVscode = NavigateTo;

type Message = MessageToVscode | MessageToWebview;
Expand Down
10 changes: 10 additions & 0 deletions src/vscode/overview-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export class OverviewViewProvider implements vscode.WebviewViewProvider {
private _view?: vscode.WebviewView;
private messageHandler: MessageHandler<MessageToVscode>;

/**
* Initialize the WebView
* @param _extensionUri
* @param isDark theme to use for the initial graph
* @param messageHandlers handlers for messages from the webview
*/
constructor(
private readonly _extensionUri: vscode.Uri,
private readonly isDark: boolean,
Expand All @@ -20,6 +26,10 @@ export class OverviewViewProvider implements vscode.WebviewViewProvider {
this.messageHandler = new MessageHandler(messageHandlers);
}

/**
* Post a message to the WebView, to be handled there.
* @param message The message to post
*/
public postMessage<T extends MessageToWebview>(message: T) {
if (this._view) {
this._view.webview.postMessage(message);
Expand Down

0 comments on commit 11d20a4

Please sign in to comment.