Skip to content

Commit

Permalink
send theme colors
Browse files Browse the repository at this point in the history
implements #2
  • Loading branch information
DominikPeters committed May 9, 2024
1 parent 23fe843 commit a4a46b7
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Visual Studio Code extension for interacting with [TeXpresso](https://github.com

![ezgif-6-3b2ad402f4](https://github.com/DominikPeters/texpresso-vscode/assets/3543224/0ff5cf57-5a2e-48cd-9e5f-633a5ed44411)

To use this extension, open the `.tex` document you wish to edit, then open the command pallete (<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd>), and select `Texpresso: Start Document`. A separate window will open showing the compiled preview. The preview immediately updates when you edit the file in VS Code, and using SyncTeX the preview automatically jumps to the current code position (and vice versa for clicks in the preview window). Buttons at the top of the editor are provided to switch pages, and a compile log for seeing compilation errors can be found by using the Output panel.
To use this extension, open the `.tex` document you wish to edit, then open the command pallete (<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd>), and select `TeXpresso: Start Document`. A separate window will open showing the compiled preview. The preview immediately updates when you edit the file in VS Code, and using SyncTeX the preview automatically jumps to the current code position (and vice versa for clicks in the preview window). Buttons at the top of the editor are provided to switch pages, and a compile log for seeing compilation errors can be found by using the Output panel.

## Features

Expand All @@ -26,6 +26,7 @@ This extension contributes the following settings:

* `texpresso.command`: The path to the texpresso binary.
* `texpresso.syncTeXForwardOnSelection`: Controls whether the preview should be updated when the selection in the editor changes.
* `texpresso.useEditorTheme`: Controls whether the preview should use the same color theme as the editor.

## Architecture

Expand Down
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
"title": "TeXpresso SyncTeX",
"enablement": "texpresso.inActiveEditor"
},
{
"command": "texpresso.adoptTheme",
"title": "TeXpresso: Adopt Editor Color Theme",
"enablement": "texpresso.running"
},
{
"command": "texpresso.defaultTheme",
"title": "TeXpresso: Adopt Default Color Theme",
"enablement": "texpresso.running"
},
{
"command": "texpresso.stop",
"title": "TeXpresso: Stop",
Expand All @@ -79,6 +89,11 @@
"type": "boolean",
"default": true,
"description": "Controls whether the preview should be updated when the selection in the editor changes (SyncTeX forward)"
},
"texpresso.useEditorTheme": {
"type": "boolean",
"default": false,
"description": "Controls whether the preview should use the same color theme as the editor (if activated), or the default theme (otherwise)"
}
}
},
Expand Down
129 changes: 113 additions & 16 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ export function activate(context: vscode.ExtensionContext) {

let activeEditor: vscode.TextEditor | undefined;

// The command has been defined in the package.json file
context.subscriptions.push(vscode.commands.registerCommand('texpresso.startDocument', () => {
activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
// vscode.window.showInformationMessage('Starting Texpreso for this document');
function startDocumentFromEditor(editor: vscode.TextEditor | undefined) {
if (texpresso) {
texpresso.kill();
}

if (editor) {
activeEditor = editor;
// vscode.window.showInformationMessage('Starting TeXpresso for this document');
filePath = activeEditor.document.fileName;
const text = activeEditor.document.getText();
rope = new Rope(text);
Expand Down Expand Up @@ -98,7 +101,15 @@ export function activate(context: vscode.ExtensionContext) {
if (texpresso && texpresso.stdin) {
texpresso.stdin.write(JSON.stringify(message) + '\n');
}
// Send color theme to texpresso if setting is enabled
if (vscode.workspace.getConfiguration('texpresso').get('useEditorTheme') as boolean) {
adoptTheme();
}
}
}

context.subscriptions.push(vscode.commands.registerCommand('texpresso.startDocument', () => {
startDocumentFromEditor(vscode.window.activeTextEditor);
}));

vscode.workspace.onDidChangeTextDocument(event => {
Expand Down Expand Up @@ -128,8 +139,14 @@ export function activate(context: vscode.ExtensionContext) {
}
});

/***********************************
***********************************
**** SyncTeX and Page Choice ****
***********************************
**********************************/

let previouslySentLineNumber: number | undefined;
function doSyncTeXForward(editor : vscode.TextEditor | undefined = vscode.window.activeTextEditor) {
function doSyncTeXForward(editor: vscode.TextEditor | undefined = vscode.window.activeTextEditor) {
if (!editor || editor.document !== activeEditor?.document) {
return;
}
Expand Down Expand Up @@ -159,28 +176,108 @@ export function activate(context: vscode.ExtensionContext) {
doSyncTeXForward();
}));

context.subscriptions.push(vscode.commands.registerCommand('texpresso.freshCompile', () => {
context.subscriptions.push(vscode.commands.registerCommand('texpresso.nextPage', () => {
if (activeEditor) {
const text = activeEditor.document.getText();
rope = new Rope(text);
// resend "open" command
const message = ["open", filePath, text];
const message = ["next-page"];
texpresso?.stdin?.write(JSON.stringify(message) + '\n');
texpresso?.stdin?.write(JSON.stringify(["rescan"]) + '\n');
}
}));

context.subscriptions.push(vscode.commands.registerCommand('texpresso.nextPage', () => {
context.subscriptions.push(vscode.commands.registerCommand('texpresso.previousPage', () => {
if (activeEditor) {
const message = ["next-page"];
const message = ["previous-page"];
texpresso?.stdin?.write(JSON.stringify(message) + '\n');
}
}));

context.subscriptions.push(vscode.commands.registerCommand('texpresso.previousPage', () => {
/***********************************
***********************************
**** Theme Color Adaption ****
***********************************
**********************************/

function adoptTheme() {
if (!texpresso || !activeEditor) {
return;
}
// to get explicit colors of the theme (which are not available in the API), we need to open a webview
// see https://github.com/microsoft/vscode/issues/32813#issuecomment-798680103
const webviewPanel = vscode.window.createWebviewPanel(
'texpresso.colorTheme',
'Color Theme',
{ preserveFocus: true, viewColumn: vscode.ViewColumn.Beside },
{
enableScripts: true,
}
);
const webview = webviewPanel.webview;
webview.html = `<!DOCTYPE html><html><script>
const vscode = acquireVsCodeApi();
vscode.postMessage(Object.values(document.getElementsByTagName('html')[0].style).map(
(rv) => {
return {
[rv]: document
.getElementsByTagName('html')[0]
.style.getPropertyValue(rv),
}
}
));
</script></html>`;
webview.onDidReceiveMessage((cssVars) => {
webviewPanel.dispose();
const colors = {} as { [key: string]: number[] };
for (const cssVar of cssVars) {
const key = Object.keys(cssVar)[0];
const value = cssVar[key];
if (key === '--vscode-editor-background' || key === '--vscode-editor-foreground') {
// value is for example "#cccccc"
// convert it to rgb as three floats between 0 and 1
const rgb = value.match(/#(..)(..)(..)/);
if (rgb) {
colors[key] = [
parseInt(rgb[1], 16) / 255,
parseInt(rgb[2], 16) / 255,
parseInt(rgb[3], 16) / 255,
];
}
}
}
const message = ["theme", colors['--vscode-editor-background'], colors['--vscode-editor-foreground']];
texpresso?.stdin?.write(JSON.stringify(message) + '\n');
});
}

vscode.window.onDidChangeActiveColorTheme(() => {
if (vscode.workspace.getConfiguration('texpresso').get('useEditorTheme') as boolean) {
adoptTheme();
}
});

context.subscriptions.push(vscode.commands.registerCommand('texpresso.adoptTheme', () => {
adoptTheme();
}));

context.subscriptions.push(vscode.commands.registerCommand('texpresso.defaultTheme', () => {
if (texpresso) {
const message = ["theme", [1, 1, 1], [0, 0, 0]];
texpresso.stdin?.write(JSON.stringify(message) + '\n');
}
}));

/***********************************
***********************************
***** Refresh and Stop *****
***********************************
**********************************/

context.subscriptions.push(vscode.commands.registerCommand('texpresso.freshCompile', () => {
if (activeEditor) {
const message = ["previous-page"];
const text = activeEditor.document.getText();
rope = new Rope(text);
// resend "open" command
const message = ["open", filePath, text];
texpresso?.stdin?.write(JSON.stringify(message) + '\n');
texpresso?.stdin?.write(JSON.stringify(["rescan"]) + '\n');
}
}));

Expand Down

0 comments on commit a4a46b7

Please sign in to comment.