Skip to content

Commit

Permalink
jcef-based viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Dec 22, 2023
1 parent 7b23cd6 commit de73ce5
Show file tree
Hide file tree
Showing 19 changed files with 269 additions and 1,140 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dvd.intellij.d2.ide.action

import com.dvd.intellij.d2.components.D2Layout
import com.dvd.intellij.d2.ide.editor.images.D2FileEditorImpl.Companion.D2_FILE_LAYOUT
import com.dvd.intellij.d2.ide.editor.images.D2_FILE_LAYOUT
import com.dvd.intellij.d2.ide.service.D2Service
import com.dvd.intellij.d2.ide.utils.D2Bundle
import com.dvd.intellij.d2.ide.utils.d2FileEditor
Expand Down Expand Up @@ -39,12 +39,11 @@ private class D2LayoutEngineAction(private val layout: D2Layout) : ToggleAction(
},
null
), DumbAware {
override fun isSelected(e: AnActionEvent): Boolean =
(e.d2FileEditor.getUserData(D2_FILE_LAYOUT) ?: D2Layout.DEFAULT) == layout
override fun isSelected(e: AnActionEvent): Boolean = (e.d2FileEditor.getUserData(D2_FILE_LAYOUT) ?: D2Layout.DEFAULT) == layout

override fun setSelected(e: AnActionEvent, state: Boolean) {
e.d2FileEditor.putUserData(D2_FILE_LAYOUT, layout)
e.d2FileEditor.refreshD2()
service<D2Service>().compile(e.d2FileEditor)
}

override fun getActionUpdateThread() = ActionUpdateThread.BGT
Expand Down
44 changes: 40 additions & 4 deletions src/main/kotlin/com/dvd/intellij/d2/ide/action/D2MessagesAction.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
package com.dvd.intellij.d2.ide.action

import com.dvd.intellij.d2.ide.toolWindow.D2ToolWindowService
import com.dvd.intellij.d2.ide.utils.d2FileEditor
import com.dvd.intellij.d2.ide.service.D2Service
import com.dvd.intellij.d2.ide.utils.D2Bundle
import com.intellij.execution.impl.ConsoleViewImpl
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.wm.ToolWindowId
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.ui.content.ContentFactory
import com.intellij.ui.content.MessageView
import java.awt.BorderLayout
import javax.swing.JPanel

class D2MessagesAction : AnAction() {
private class D2MessagesAction : AnAction(), DumbAware {
override fun actionPerformed(e: AnActionEvent) {
e.project?.service<D2ToolWindowService>()?.show(e.d2FileEditor)
val project = e.project ?: return

val messageView = MessageView.getInstance(project)
val displayName = D2Bundle.message("d2")

var content = messageView.contentManager.findContent(displayName)
if (content == null) {
val panel = JPanel(BorderLayout())
val console = ConsoleViewImpl(project, true)
panel.add(console.component, BorderLayout.CENTER)

content = ContentFactory.getInstance().createContent(panel, displayName, true)
messageView.contentManager.addContent(content)
Disposer.register(content, console)
}

val console = content.component as ConsoleViewImpl
console.clear()
messageView.contentManager.setSelectedContent(content)
for (command in service<D2Service>().map.values) {
console.print(command.log, ConsoleViewContentType.LOG_INFO_OUTPUT)
}

val toolWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.MESSAGES_WINDOW)
if (toolWindow != null && !toolWindow.isActive) {
toolWindow.activate(null, false)
}
}
}
22 changes: 11 additions & 11 deletions src/main/kotlin/com/dvd/intellij/d2/ide/action/D2ThemeChooser.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package com.dvd.intellij.d2.ide.action

import com.dvd.intellij.d2.components.D2Theme
import com.dvd.intellij.d2.ide.editor.images.D2FileEditorImpl.Companion.D2_FILE_THEME
import com.dvd.intellij.d2.ide.editor.images.D2_FILE_THEME
import com.dvd.intellij.d2.ide.service.D2Service
import com.dvd.intellij.d2.ide.utils.D2Bundle
import com.dvd.intellij.d2.ide.utils.d2FileEditor
import com.intellij.icons.AllIcons
import com.intellij.ide.BrowserUtil
import com.intellij.openapi.actionSystem.*
import com.intellij.openapi.components.service
import com.intellij.openapi.project.DumbAware

class D2ThemesActionGroup : ActionGroup() {
private class D2ThemesActionGroup : ActionGroup() {
override fun getChildren(e: AnActionEvent?): Array<AnAction> = arrayOf(
*D2Theme.values().map(::D2ThemeAction).toTypedArray(),
Separator(),
OpenThemeOverviewAction()
)
}

class D2ThemeAction(private val theme: D2Theme) : ToggleAction(theme.tName) {

private class D2ThemeAction(private val theme: D2Theme) : ToggleAction(theme.tName), DumbAware {
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT

override fun isSelected(e: AnActionEvent): Boolean =
(e.d2FileEditor.getUserData(D2_FILE_THEME) ?: D2Theme.DEFAULT) == theme

override fun setSelected(e: AnActionEvent, state: Boolean) {
e.d2FileEditor.putUserData(D2_FILE_THEME, theme)
e.d2FileEditor.refreshD2()
service<D2Service>().compile(e.d2FileEditor)
}
}

class OpenThemeOverviewAction : AnAction(
private const val THEME_DOCS = "https://d2lang.com/tour/themes"

private class OpenThemeOverviewAction : AnAction(
D2Bundle.messagePointer("d2.open.theme.documentation"),
AllIcons.General.Web
) {
companion object {
private const val THEME_DOCS = "https://d2lang.com/tour/themes"
}

), DumbAware {
override fun actionPerformed(e: AnActionEvent) = BrowserUtil.browse(THEME_DOCS)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.dvd.intellij.d2.ide.editor

import com.dvd.intellij.d2.ide.editor.images.D2FileEditorImpl
import com.dvd.intellij.d2.ide.editor.images.D2SvgViewer
import com.dvd.intellij.d2.ide.service.D2Service
import com.dvd.intellij.d2.ide.utils.D2_EDITOR_NAME
import com.dvd.intellij.d2.ide.utils.isD2
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import com.intellij.openapi.fileEditor.*
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider
Expand All @@ -12,16 +13,23 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile

// https://github.com/JetBrains/intellij-community/blob/master/images/src/org/intellij/images/editor/impl/ImageFileEditorProvider.java
class D2FileEditorProvider : FileEditorProvider, DumbAware {
override fun accept(project: Project, file: VirtualFile): Boolean {
if (!file.isD2) return false
private class D2FileEditorProvider : FileEditorProvider, DumbAware {
override fun accept(project: Project, file: VirtualFile): Boolean = file.isD2

return service<D2Service>().isCompilerInstalled()
// 2023.3
@Suppress("unused")
fun acceptRequiresReadAction(): Boolean {
return false
}

override fun createEditor(project: Project, file: VirtualFile): FileEditor {
val view = D2FileEditorImpl(project, file).also { it.refreshD2() }
val view = D2SvgViewer(project, file)
val editor = TextEditorProvider.getInstance().createEditor(project, file) as TextEditor

ApplicationManager.getApplication().executeOnPooledThread {
service<D2Service>().compile(view)
}

return TextEditorWithPreview(editor, view, D2_EDITOR_NAME, TextEditorWithPreview.Layout.SHOW_EDITOR_AND_PREVIEW)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ import javax.swing.JComponent
private class D2MissingCompilerNotificationProvider : EditorNotificationProvider, DumbAware {
override fun collectNotificationData(project: Project, file: VirtualFile): Function<in FileEditor, out JComponent?>? {
val installed = service<D2Service>().isCompilerInstalled()
return if (file.isD2 && !installed) {
Function { fileEditor -> D2MissingCompilerNotificationPanel(fileEditor) }
} else null
if (file.isD2 && !installed) {
return Function { fileEditor -> D2MissingCompilerNotificationPanel(fileEditor) }
} else {
return null
}
}
}

private class D2MissingCompilerNotificationPanel(
fileEditor: FileEditor
) : EditorNotificationPanel(fileEditor, Status.Warning) {
init {
text = D2Bundle.message("d2.executable.not.found.notification")
}
private class D2MissingCompilerNotificationPanel(
fileEditor: FileEditor
) : EditorNotificationPanel(fileEditor, Status.Warning) {
init {
text = D2Bundle.message("d2.executable.not.found.notification")
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit de73ce5

Please sign in to comment.