Skip to content

Commit

Permalink
Add disposable for when tool window is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
Katherine Liu committed Jul 7, 2023
1 parent c153bb4 commit 89e8992
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class SkateProjectServiceImpl(private val project: Project) : SkateProjectServic
val changeLogFile = VfsUtil.findRelativeFile(projectDir, settings.whatsNewFilePath) ?: return
val changeLogString = VfsUtil.loadText(changeLogFile)
val toolWindowManager = ToolWindowManager.getInstance(project)

toolWindowManager.invokeLater {
val toolWindow =
toolWindowManager.registerToolWindow("skate-whats-new") {
Expand All @@ -58,6 +59,7 @@ class SkateProjectServiceImpl(private val project: Project) : SkateProjectServic

WhatsNewPanelFactory()
.createToolWindowContent(toolWindow, project, changeLogString, parentDisposable)

toolWindow.show()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import com.intellij.openapi.util.Disposer
import com.intellij.openapi.wm.ToolWindow
import com.intellij.testFramework.LightVirtualFile
import com.intellij.ui.content.ContentFactory
import com.intellij.ui.content.ContentManagerEvent
import com.intellij.ui.content.ContentManagerListener
import java.awt.BorderLayout
import javax.swing.JComponent
import javax.swing.JPanel
Expand All @@ -49,6 +51,16 @@ class WhatsNewPanelFactory : DumbAware {
val content =
ContentFactory.getInstance().createContent(toolWindowContent.contentPanel, "", false)
toolWindow.contentManager.addContent(content)
toolWindow.contentManager.addContentManagerListener(
object : ContentManagerListener {
override fun contentRemoved(event: ContentManagerEvent) {
if (event.content.component == toolWindowContent.contentPanel) {
Disposer.dispose(parentDisposable)
toolWindow.contentManager.removeContentManagerListener(this)
}
}
}
)
}

private class WhatsNewPanelContent(
Expand All @@ -70,7 +82,12 @@ class WhatsNewPanelFactory : DumbAware {
@Language("Markdown") markdownFileString: String,
parentDisposable: Disposable
): JComponent {
// to take in the parsed Changelog:
// val parsedChangelog = ChangelogParser.readFile(markdownFileString)
// then, pass in parsedChangelog instead of markdownFileString

val file = LightVirtualFile("changelog.md", markdownFileString)

val panel = MarkdownJCEFHtmlPanel(project, file)
Disposer.register(parentDisposable, panel)
val html = runReadAction {
Expand Down

0 comments on commit 89e8992

Please sign in to comment.