Skip to content

Commit

Permalink
Merge pull request #117 from liudongmiao/jcef-viewer
Browse files Browse the repository at this point in the history
context menu to open in jcef
  • Loading branch information
PHPirates authored Sep 23, 2024
2 parents 7d25164 + 1792987 commit 2f02b30
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.jcef.JCEFHtmlPanel
import com.intellij.util.ui.UIUtil
import io.netty.handler.codec.http.QueryStringDecoder
import kotlinx.serialization.json.JsonElement
import org.cef.browser.CefBrowser
import org.cef.browser.CefFrame
import org.cef.callback.CefContextMenuParams
import org.cef.callback.CefMenuModel
import org.cef.handler.CefContextMenuHandlerAdapter
import java.awt.Color
import java.net.URL
import java.util.concurrent.atomic.AtomicBoolean

class PdfJcefPreviewController(val project: Project, val virtualFile: VirtualFile) :
Expand Down Expand Up @@ -78,6 +85,29 @@ class PdfJcefPreviewController(val project: Project, val virtualFile: VirtualFil
browser.addConsoleMessageListener(createDefaultConsoleMessageListener(logger))
}

browser.jbCefClient.addContextMenuHandler(object : CefContextMenuHandlerAdapter() {
private fun getPdfUrl(cefBrowser: CefBrowser?): String? {
if (!viewLoaded || cefBrowser == null) return null
val urlDecoder = QueryStringDecoder(cefBrowser.url)
val file = urlDecoder.parameters()?.get("file")?.get(0) ?: return null
return URL(URL(cefBrowser.url), file).toString()
}

override fun onBeforeContextMenu(cefBrowser: CefBrowser?, frame: CefFrame?, params: CefContextMenuParams?, model: CefMenuModel?) {
getPdfUrl(cefBrowser) ?: return
model?.addItem(CefMenuModel.MenuId.MENU_ID_USER_FIRST, "Open in Embedded Browser")
}

override fun onContextMenuCommand(cefBrowser: CefBrowser?, frame: CefFrame?, params: CefContextMenuParams?, commandId: Int, eventFlags: Int): Boolean {
if (commandId != CefMenuModel.MenuId.MENU_ID_USER_FIRST) return false
val url = getPdfUrl(cefBrowser) ?: return true
logger.debug("would load pdf, base url: ${cefBrowser?.url}, pdf url: $url")
viewLoaded = false
browser.loadURL(url)
return true
}
}, browser.cefBrowser)

pipe.subscribe<BrowserMessages.InitialViewProperties> {
logger.debug(it.toString())
viewProperties = it.properties
Expand Down

0 comments on commit 2f02b30

Please sign in to comment.