Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace WebView JS #23

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.IconLoader
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.ui.components.JBPanel
import com.intellij.ui.jcef.JBCefApp
import com.intellij.ui.jcef.JBCefJSQuery.Response
import javax.swing.JButton
Expand Down Expand Up @@ -70,6 +71,8 @@ class CFGToolWindowFactory :
}

private val localBrowser: LocalBrowser = LocalBrowser("/webview")
private val namespaceToWebview: LocalBrowser.Path = LocalBrowser.Path("JetBrains", "ToWebview")
private val namespaceToExtension: LocalBrowser.Path = LocalBrowser.Path("JetBrains", "ToExtension")

private val navigateQuery = localBrowser.createJSQuery()

Expand Down Expand Up @@ -129,7 +132,12 @@ class CFGToolWindowFactory :
* Initializes the webview callbacks into the plugin code
*/
private fun initializeCallbacks() {
localBrowser.injectFunction("navigateTo", "position", code = navigateQuery.inject("position"))
localBrowser.injectFunction(
namespaceToExtension,
"navigateTo",
args = listOf("position"),
code = navigateQuery.inject("position"),
)
}

private fun setCode(
Expand All @@ -139,14 +147,14 @@ class CFGToolWindowFactory :
) {
val cfgLanguage = internalLanguageName(language)
loadSettings()
localBrowser.call("setCode", jsStr(code), jsNum(cursorOffset), jsStr(cfgLanguage))
localBrowser.call(namespaceToWebview, "setCode", jsStr(code), jsNum(cursorOffset), jsStr(cfgLanguage))
initializeCallbacks()
}

private fun loadSettings() {
localBrowser.call("setSimplify", jsBool(Settings.simplify))
localBrowser.call("setFlatSwitch", jsBool(Settings.flatSwitch))
localBrowser.call("setHighlight", jsBool(Settings.highlight))
localBrowser.call(namespaceToWebview, "setSimplify", jsBool(Settings.simplify))
localBrowser.call(namespaceToWebview, "setFlatSwitch", jsBool(Settings.flatSwitch))
localBrowser.call(namespaceToWebview, "setHighlight", jsBool(Settings.highlight))
setColors(Settings.colorScheme)
}

Expand All @@ -158,7 +166,7 @@ class CFGToolWindowFactory :
"light" -> """{"version":1,"scheme":[{"name":"node.default","hex":"#d3d3d3"},{"name":"node.entry","hex":"#48AB30"},{"name":"node.exit","hex":"#AB3030"},{"name":"node.throw","hex":"#ffdddd"},{"name":"node.yield","hex":"#00bfff"},{"name":"node.border","hex":"#000000"},{"name":"node.highlight","hex":"#000000"},{"name":"edge.regular","hex":"#0000ff"},{"name":"edge.consequence","hex":"#008000"},{"name":"edge.alternative","hex":"#ff0000"},{"name":"cluster.border","hex":"#ffffff"},{"name":"cluster.with","hex":"#ffddff"},{"name":"cluster.tryComplex","hex":"#ddddff"},{"name":"cluster.try","hex":"#ddffdd"},{"name":"cluster.finally","hex":"#ffffdd"},{"name":"cluster.except","hex":"#ffdddd"},{"name":"graph.background","hex":"#F7F8FA"}]}"""
else -> colors
}
localBrowser.call("setColors", jsStr(colorScheme))
localBrowser.call(namespaceToWebview, "setColors", jsStr(colorScheme))
}

private fun updateCaretPosition(editor: Editor?) {
Expand All @@ -183,11 +191,11 @@ class CFGToolWindowFactory :

private fun createWebViewContent(): JComponent {
// TODO: Find a way to enable debugging and keep the scaling!
return localBrowser.component
// return JBPanel<JBPanel<*>>().apply {
// add(createButton("Open Devtools") { localBrowser.openDevtools() })
// add(localBrowser.component)
// }
// return localBrowser.component
return JBPanel<JBPanel<*>>().apply {
add(createButton("Open Devtools") { localBrowser.openDevtools() })
add(localBrowser.component)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@
private const val VIEWER_URL = "$PROTOCOL://$HOST_NAME$VIEWER_PATH"
}

class Path(
val name: String,
vararg names: String,
) {
val names: List<String> = listOf(*names)

fun extend(vararg names: String): Path = Path(this.name, *this.names.toTypedArray(), *names)

override fun toString(): String = listOf(name, *names.toTypedArray()).joinToString(".")
}

private val myCefClient = JBCefApp.getInstance().createClient()
private val myBrowser: JBCefBrowser =
JBCefBrowserBuilder().setClient(myCefClient).setEnableOpenDevToolsMenuItem(Disposer.isDebugMode()).build()
Expand Down Expand Up @@ -100,16 +111,34 @@
myCefClient.removeRequestHandler(myRequestHandler, myBrowser.cefBrowser)
}

fun call(
namespace: Path,
name: String,
vararg args: JSArg,
) {
call(namespace.extend(name).toString(), *args)
}

fun call(

Check notice on line 122 in src/main/kotlin/com/github/tmr232/function_graph_overview/toolWindow/LocalBrowser.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class member can have 'private' visibility

Function 'call' could be private
name: String,
vararg args: JSArg,
) {
myBrowser.cefBrowser.executeJavaScript(formatJSCall(name, *args), "", 0)
}

fun injectFunction(
namespace: Path,
name: String,
args: List<String>,
code: String,
) {
injectEmptyObject(namespace)
injectFunction(namespace.extend(name).toString(), args, code)
}

fun injectFunction(

Check notice on line 139 in src/main/kotlin/com/github/tmr232/function_graph_overview/toolWindow/LocalBrowser.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Class member can have 'private' visibility

Function 'injectFunction' could be private
name: String,
vararg args: String,
args: List<String>,
code: String,
) {
myBrowser.cefBrowser.executeJavaScript(
Expand All @@ -119,6 +148,16 @@
)
}

private fun injectEmptyObject(path: Path) {
for (path in path.names.scan(path.name) { acc, string -> "$acc.$string" }) {
myBrowser.cefBrowser.executeJavaScript(
"window.$path ??= {}",
myBrowser.cefBrowser.url,
0,
)
}
}

fun createJSQuery() = JBCefJSQuery.create(myBrowser as JBCefBrowserBase)

val component get() = myBrowser.component
Expand Down
Loading