Skip to content

Commit

Permalink
Merge pull request #7 from devchat-ai/feat/style
Browse files Browse the repository at this point in the history
feat: add css to html
  • Loading branch information
pplam authored Nov 28, 2023
2 parents 701f6f8 + c4f9b80 commit 8f7fb17
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/main/java/ai/devchat/idea/DevChatToolWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import com.intellij.ui.jcef.JBCefBrowser;
import org.cef.browser.CefBrowser;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColors;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -67,7 +70,10 @@ public DevChatToolWindowContent(Project project) {
jsContent = "console.log('Error: main.js not found')";
}

String HtmlWithJsContent = insertJStoHTML(htmlContent, jsContent);

String HtmlWithCssContent = insertCSSToHTML(htmlContent);

String HtmlWithJsContent = insertJStoHTML(HtmlWithCssContent, jsContent);
Log.info("main.html and main.js are loaded.");

// enable dev tools
Expand Down Expand Up @@ -121,4 +127,31 @@ private String insertJStoHTML(String html, String js) {
}
return html;
}

private String insertCSSToHTML(String html){
int index = html.lastIndexOf("<head>");
int endIndex = html.lastIndexOf("</head>");
EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
Color editorBgColor = scheme.getColor(EditorColors.CARET_ROW_COLOR);
Color foregroundColor = scheme.getDefaultForeground();

String styleTag = "<style>" + ":root{" +
"--vscode-sideBar-background:"+colorToCssRgb(editorBgColor) + ";" +
"--vscode-menu-background:"+colorToCssRgb(editorBgColor) + ";" +
"--vscode-editor-foreground:"+colorToCssRgb(foregroundColor) + ";" +
"--vscode-menu-foreground:"+colorToCssRgb(foregroundColor) + ";" +
"--vscode-foreground:"+colorToCssRgb(foregroundColor) + ";" +
"}" + "</style>";
if(index != -1 && endIndex != -1){
html = html.substring(0,index + "<head>".length()) + "\n" + styleTag + html.substring(endIndex);
}
return html;
}

public String colorToCssRgb(Color color) {
if(color != null) return "rgb(" + color.getRed() + "," + color.getGreen() + "," + color.getBlue() + ")";
return "";
}


}

0 comments on commit 8f7fb17

Please sign in to comment.