From 891922df17cfe5380f3780f4f35cf48222a8c208 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Thu, 10 Aug 2023 01:16:05 -0400 Subject: [PATCH] Add fallback Html panel support for markdown rendering (#522) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Android Studio frustratingly ships with a broken version of the embedded markdown plugin, which prevents use of the modern JCEF API we were using. This adds a fallback implementation for when that's not available and just uses a simpler html panel. Code blocks don't work and it's a little plain, but it's otherwise ok. Screenshot 2023-08-10 at 12 56 05 AM --- .../sgp/intellij/ui/WhatsNewPanelFactory.kt | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/skate-plugin/src/main/kotlin/com/slack/sgp/intellij/ui/WhatsNewPanelFactory.kt b/skate-plugin/src/main/kotlin/com/slack/sgp/intellij/ui/WhatsNewPanelFactory.kt index 661ff3fcb..1052c535c 100644 --- a/skate-plugin/src/main/kotlin/com/slack/sgp/intellij/ui/WhatsNewPanelFactory.kt +++ b/skate-plugin/src/main/kotlin/com/slack/sgp/intellij/ui/WhatsNewPanelFactory.kt @@ -18,6 +18,7 @@ package com.slack.sgp.intellij.ui import com.intellij.openapi.Disposable import com.intellij.openapi.application.runReadAction import com.intellij.openapi.components.service +import com.intellij.openapi.diagnostic.logger import com.intellij.openapi.project.DumbAware import com.intellij.openapi.project.Project import com.intellij.openapi.util.Disposer @@ -26,6 +27,12 @@ import com.intellij.testFramework.LightVirtualFile import com.intellij.ui.content.ContentFactory import com.intellij.ui.content.ContentManagerEvent import com.intellij.ui.content.ContentManagerListener +import com.intellij.ui.dsl.builder.panel +import com.intellij.ui.dsl.gridLayout.HorizontalAlign +import com.intellij.ui.dsl.gridLayout.VerticalAlign +import com.intellij.ui.jcef.JBCefApp +import com.intellij.util.ui.HtmlPanel +import com.intellij.util.ui.JBUI import com.slack.sgp.intellij.ChangelogJournal import com.slack.sgp.intellij.ChangelogParser import java.awt.BorderLayout @@ -71,6 +78,8 @@ class WhatsNewPanelFactory : DumbAware { parentDisposable: Disposable ) { + private val logger = logger() + // Actual panel box for "What's New in Slack!" val contentPanel: JPanel = JPanel().apply { @@ -91,15 +100,49 @@ class WhatsNewPanelFactory : DumbAware { val file = LightVirtualFile("changelog.md", changeLogContent.changeLogString ?: "") - val panel = MarkdownJCEFHtmlPanel(project, file) - Disposer.register(parentDisposable, panel) - val html = runReadAction { MarkdownUtil.generateMarkdownHtml(file, changeLogContent.changeLogString ?: "", project) } - panel.setHtml(html, 0) - return panel.component + // We have to support two different modes here: the modern JCEF-based one and the legacy + // Legacy is because Android Studio ships with a broken markdown plugin that doesn't work with + // JCEF + // https://issuetracker.google.com/issues/159933628#comment19 + val panel = + if (JBCefApp.isSupported()) { + logger.debug("Using JCEFHtmlPanelProvider") + MarkdownJCEFHtmlPanel(project, file) + .apply { + Disposer.register(parentDisposable, this) + setHtml(html, 0) + } + .component + } else { + logger.debug("Using HtmlPanel") + val htmlPanel = + object : HtmlPanel() { + init { + isVisible = true + // Padding + border = JBUI.Borders.empty(16) + update() + } + + override fun getBody(): String { + return html + } + } + panel { + row { + cell(htmlPanel) + .horizontalAlign(HorizontalAlign.FILL) + .verticalAlign(VerticalAlign.FILL) + } + .resizableRow() + } + } + + return panel } } }