IDEA-345694 Rework documentation popup styling - move magic size constants to DocumentationHtmlUtil; support scaling

GitOrigin-RevId: c519b8fa6acd4bb6ee3d3da45954c846189e2c87
This commit is contained in:
Piotr Tomiak
2024-02-12 17:56:02 +01:00
committed by intellij-monorepo-bot
parent ed56e22f06
commit b28149eb2a
8 changed files with 70 additions and 39 deletions

View File

@@ -91,8 +91,8 @@ public class DocumentationComponent extends JPanel implements Disposable, DataPr
public static final Color SECTION_COLOR = Gray.get(0x90);
private static final int PREFERRED_HEIGHT_MAX_EM = 10;
static final JBDimension MIN_DEFAULT = new JBDimension(300, 36);
static final JBDimension MAX_DEFAULT = new JBDimension(950, 500);
private static final JBDimension MIN_DEFAULT = new JBDimension(300, 36);
private static final JBDimension MAX_DEFAULT = new JBDimension(950, 500);
private final ExternalDocAction myExternalDocAction;

View File

@@ -33,7 +33,7 @@ import java.awt.event.KeyEvent;
import java.util.Map;
import java.util.function.Function;
import static com.intellij.codeInsight.documentation.DocumentationHtmlUtil.getDocumentationPaneDefaultCssRules;
import static com.intellij.codeInsight.documentation.DocumentationHtmlUtil.*;
import static com.intellij.lang.documentation.DocumentationMarkup.*;
import static com.intellij.util.ui.ExtendableHTMLViewFactory.Extensions;
import static com.intellij.util.ui.html.UtilsKt.getCssMargin;
@@ -204,15 +204,16 @@ public abstract class DocumentationEditorPane extends JEditorPane implements Dis
// These values were calculated based on experiments with varied content and manual resizing to comfortable width.
final int contentLengthPreferredSize;
if (textLength < 200) {
contentLengthPreferredSize = JBUIScale.scale(300);
contentLengthPreferredSize = getDocPopupPreferredMinWidth();
}
else if (textLength > 200 && textLength < 1000) {
contentLengthPreferredSize = JBUIScale.scale(300) + JBUIScale.scale(1) * (textLength - 200) * (500 - 300) / (1000 - 200);
contentLengthPreferredSize = getDocPopupPreferredMinWidth() +
(textLength - 200) * (getDocPopupPreferredMaxWidth() - getDocPopupPreferredMinWidth()) / (1000 - 200);
}
else {
contentLengthPreferredSize = JBUIScale.scale(500);
contentLengthPreferredSize = getDocPopupPreferredMaxWidth();
}
return contentLengthPreferredSize;
return JBUIScale.scale(contentLengthPreferredSize);
}
private static @Nullable View findSection(@NotNull View view, @NotNull String sectionClassName) {

View File

@@ -8,6 +8,7 @@ import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.module.ModuleTypeManager
import com.intellij.openapi.module.UnknownModuleType
import com.intellij.ui.ColorUtil
import com.intellij.ui.scale.JBUIScale
import com.intellij.util.containers.ContainerUtil
import com.intellij.util.ui.ExtendableHTMLViewFactory
import com.intellij.util.ui.ExtendableHTMLViewFactory.Extensions.icons
@@ -22,10 +23,36 @@ import javax.swing.Icon
@ApiStatus.Internal
object DocumentationHtmlUtil {
const val CONTENT_OUTER_PADDING = 10
@JvmStatic
val contentOuterPadding: Int get() = 10
// Should be minimum 2 to compensate mandatory table border width of 2
const val CONTENT_INNER_PADDING = 2
const val CONTENT_SPACING = 8
@JvmStatic
val contentInnerPadding: Int get() = 2
@JvmStatic
val contentSpacing: Int get() = 8
@JvmStatic
val docPopupPreferredMinWidth: Int get() = 300
@JvmStatic
val docPopupPreferredMaxWidth: Int get() = 500
@JvmStatic
val docPopupMinWidth: Int get() = 300
@JvmStatic
val docPopupMaxWidth: Int get() = 900
@JvmStatic
val docPopupMaxHeight: Int get() = 500
@JvmStatic
val lookupDocPopupWidth: Int get() = 550
@JvmStatic
val lookupDocPopupMinHeight: Int get() = 300
@JvmStatic
fun getIconsExtension(iconResolver: Function<in String?, out Icon?>): ExtendableHTMLViewFactory.Extension {
@@ -51,38 +78,41 @@ object DocumentationHtmlUtil {
@Language("CSS")
val result = ContainerUtil.newLinkedList(
"""
html { padding: 10px 10px 0 10px; margin: 0 }
html { padding: ${contentOuterPadding.scaled()}px ${contentOuterPadding.scaled()}px 0 ${contentOuterPadding.scaled()}px; margin: 0 }
body { padding: 0; margin: 0; }
pre { white-space: pre-wrap }
a { color: $linkColor; text-decoration: none;}
.$CLASS_DEFINITION, .$CLASS_DEFINITION_SEPARATED {
padding: 0 ${CONTENT_INNER_PADDING}px ${CONTENT_SPACING}px ${CONTENT_INNER_PADDING}px;
padding: 0 ${contentInnerPadding.scaled()}px ${contentSpacing.scaled()}px ${contentInnerPadding.scaled()}px;
}
.$CLASS_DEFINITION pre, .$CLASS_DEFINITION_SEPARATED pre {
margin: 0; padding: 0;
}
.$CLASS_CONTENT, .$CLASS_CONTENT_SEPARATED {
padding: 0 ${CONTENT_INNER_PADDING}px 0px ${CONTENT_INNER_PADDING}px;
padding: 0 ${contentInnerPadding.scaled()}px 0px ${contentInnerPadding.scaled()}px;
max-width: 100%;
}
.$CLASS_SEPARATED, .$CLASS_DEFINITION_SEPARATED, .$CLASS_CONTENT_SEPARATED {
margin-bottom: ${CONTENT_SPACING}px;
margin-bottom: ${contentSpacing.scaled()}px;
border-bottom: thin solid $borderColor;
}
.$CLASS_BOTTOM, .$CLASS_DOWNLOAD_DOCUMENTATION {
padding: 0 ${CONTENT_INNER_PADDING}px ${CONTENT_SPACING}px ${CONTENT_INNER_PADDING}px;
padding: 0 ${contentInnerPadding.scaled()}px ${contentSpacing.scaled()}px ${contentInnerPadding.scaled()}px;
}
.$CLASS_GRAYED { color: #909090; display: inline;}
.$CLASS_SECTIONS { padding: 0 ${CONTENT_INNER_PADDING - 2}px 0 ${CONTENT_INNER_PADDING - 2}px 0; border-spacing: 0; }
.$CLASS_SECTIONS { padding: 0 ${contentInnerPadding.scaled() - 2}px 0 ${contentInnerPadding.scaled() - 2}px 0; border-spacing: 0; }
.$CLASS_SECTION { color: $sectionColor; padding-right: 4px; white-space: nowrap; }
""".trimIndent()
)
// Styled code
val globalScheme = EditorColorsManager.getInstance().globalScheme
result.addAll(getDefaultDocCodeStyles(globalScheme, background, CONTENT_SPACING))
result.addAll(getDefaultFormattingStyles(CONTENT_SPACING))
result.addAll(getDefaultDocCodeStyles(globalScheme, background, contentSpacing.scaled()))
result.addAll(getDefaultFormattingStyles(contentSpacing.scaled()))
return result
}
private fun Int.scaled() =
JBUIScale.scale(this)
}

View File

@@ -17,9 +17,9 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static com.intellij.codeInsight.documentation.DocumentationComponent.MAX_DEFAULT;
import static com.intellij.codeInsight.documentation.DocumentationComponent.MIN_DEFAULT;
import static com.intellij.codeInsight.documentation.DocumentationHtmlUtil.*;
import static com.intellij.lang.documentation.ide.ui.UiKt.FORCED_WIDTH;
import static com.intellij.ui.scale.JBUIScale.scale;
@Internal
public final class DocumentationScrollPane extends JBScrollPane {
@@ -33,8 +33,8 @@ public final class DocumentationScrollPane extends JBScrollPane {
@Override
public Dimension getPreferredSize() {
Integer forcedWidth = UIUtil.getClientProperty(this, FORCED_WIDTH);
int minWidth = forcedWidth == null ? MIN_DEFAULT.width() : forcedWidth;
return getPreferredSize(minWidth, MAX_DEFAULT.width(), MAX_DEFAULT.height());
int minWidth = forcedWidth == null ? scale(getDocPopupMinWidth()) : forcedWidth;
return getPreferredSize(minWidth, scale(getDocPopupMaxWidth()), scale(getDocPopupMaxHeight()));
}
private @NotNull Dimension getPreferredSize(int minWidth, int maxWidth, int maxHeight) {

View File

@@ -5,7 +5,6 @@ import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.documentation.DocFontSizePopup;
import com.intellij.codeInsight.documentation.DocumentationActionProvider;
import com.intellij.codeInsight.documentation.DocumentationFontSize;
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil;
import com.intellij.icons.AllIcons;
import com.intellij.ide.ui.UISettings;
import com.intellij.lang.documentation.QuickDocHighlightingHelper;
@@ -56,6 +55,7 @@ import java.util.List;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import static com.intellij.codeInsight.documentation.DocumentationHtmlUtil.getContentSpacing;
import static com.intellij.codeInsight.documentation.render.InlineDocumentationImplKt.createAdditionalStylesForTips;
import static com.intellij.codeInsight.documentation.render.InlineDocumentationImplKt.unwrapTipsText;
import static com.intellij.lang.documentation.DocumentationMarkup.*;
@@ -374,11 +374,11 @@ public final class DocRenderer implements CustomFoldRegionRenderer {
"a {color: #" + ColorUtil.toHex(linkColor) + "; text-decoration: none}" +
"." + CLASS_SECTIONS + " {border-spacing: 0}" +
"." + CLASS_SECTION + " {padding-right: 5; white-space: nowrap}" +
"." + CLASS_CONTENT + " {padding: 0 2px " + DocumentationHtmlUtil.CONTENT_SPACING + "px 0}" +
"." + CLASS_CONTENT + " {padding: 0 2px " + getContentSpacing() + "px 0}" +
(useTipsKit ? createAdditionalStylesForTips(editor) : "") +
StringUtil.join(QuickDocHighlightingHelper.getDefaultDocCodeStyles(
colorsScheme, colorsScheme.getDefaultBackground(), DocumentationHtmlUtil.CONTENT_SPACING), "\n") +
StringUtil.join(QuickDocHighlightingHelper.getDefaultFormattingStyles(DocumentationHtmlUtil.CONTENT_SPACING), "\n");
colorsScheme, colorsScheme.getDefaultBackground(), getContentSpacing()), "\n") +
StringUtil.join(QuickDocHighlightingHelper.getDefaultFormattingStyles(getContentSpacing()), "\n");
StyleSheet result = StyleSheetUtil.loadStyleSheet(input);
if (!useTipsKit) {
ourCachedStyleSheet = result;

View File

@@ -1,6 +1,8 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.lang.documentation.ide.impl
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil.lookupDocPopupMinHeight
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil.lookupDocPopupWidth
import com.intellij.codeInsight.lookup.*
import com.intellij.codeInsight.lookup.impl.LookupImpl
import com.intellij.lang.documentation.ide.IdeDocumentationTargetProvider
@@ -14,6 +16,7 @@ import com.intellij.psi.util.PsiUtilBase
import com.intellij.ui.popup.AbstractPopup
import com.intellij.ui.popup.PopupPositionManager.Position
import com.intellij.ui.popup.PopupPositionManager.PositionAdjuster
import com.intellij.ui.scale.JBUIScale.scale
import com.intellij.util.applyIf
import com.intellij.util.ui.EDT
import kotlinx.coroutines.channels.BufferOverflow
@@ -28,9 +31,6 @@ import kotlin.coroutines.cancellation.CancellationException
import kotlin.math.max
import kotlin.math.min
const val LOOKUP_DOCUMENTATION_POPUP_WIDTH = 550
const val LOOKUP_DOCUMENTATION_POPUP_MIN_HEIGHT = 300
internal fun lookupPopupContext(editor: Editor?): PopupContext? {
val lookup = LookupManager.getActiveLookup(editor)
?: return null
@@ -115,8 +115,8 @@ private class LookupPopupBoundsHandler(
private val lookup: LookupEx,
) : BaseAdjustingPopupBoundsHandler(lookup.component) {
override fun popupBounds(anchor: Component, size: Dimension): Rectangle {
val preferredSize = Dimension(LOOKUP_DOCUMENTATION_POPUP_WIDTH,
min(size.height, max(LOOKUP_DOCUMENTATION_POPUP_MIN_HEIGHT, anchor.height)))
val preferredSize = Dimension(scale(lookupDocPopupWidth),
min(size.height, max(scale(lookupDocPopupMinHeight), anchor.height)))
val bounds = PositionAdjuster(anchor)
.adjustBounds(preferredSize, arrayOf(Position.RIGHT, Position.LEFT))
.applyIf(lookup.isPositionedAboveCaret) {

View File

@@ -2,8 +2,8 @@
package com.intellij.lang.documentation.ide.ui
import com.intellij.codeInsight.CodeInsightBundle
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil.CONTENT_INNER_PADDING
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil.CONTENT_OUTER_PADDING
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil.contentInnerPadding
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil.contentOuterPadding
import com.intellij.codeInsight.documentation.DocumentationManager.NEW_JAVADOC_LOCATION_AND_SIZE
import com.intellij.codeInsight.documentation.ToggleShowDocsOnHoverAction
import com.intellij.codeInsight.hint.HintManagerImpl.ActionToIgnore
@@ -85,8 +85,8 @@ internal class DocumentationPopupUI(
val bottomPanel = JPanel(BorderLayout()).apply {
add(ui.locationLabel, BorderLayout.CENTER)
add(toolbar, BorderLayout.EAST)
border = JBUI.Borders.empty(2, 2 + CONTENT_OUTER_PADDING + CONTENT_INNER_PADDING,
2 + CONTENT_OUTER_PADDING, CONTENT_OUTER_PADDING)
border = JBUI.Borders.empty(2, 2 + contentOuterPadding + contentInnerPadding,
2 + contentOuterPadding, contentOuterPadding)
}
ui.trackDocumentationBackgroundChange(this) {
bottomPanel.background = it

View File

@@ -2,8 +2,8 @@
package com.intellij.lang.documentation.ide.ui
import com.intellij.codeInsight.documentation.DocumentationEditorPane
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil.CONTENT_INNER_PADDING
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil.CONTENT_OUTER_PADDING
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil.contentInnerPadding
import com.intellij.codeInsight.documentation.DocumentationHtmlUtil.contentOuterPadding
import com.intellij.lang.documentation.ide.impl.DocumentationBrowser
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.EDT
@@ -33,8 +33,8 @@ internal class DocumentationToolWindowUI(
panel.add(ui.scrollPane, BorderLayout.CENTER)
ui.switcherToolbarComponent?.let { panel.add(it, BorderLayout.NORTH) }
panel.add(ui.locationLabel.also {
it.border = JBUI.Borders.empty(4, 2 + CONTENT_OUTER_PADDING + CONTENT_INNER_PADDING,
2 + CONTENT_OUTER_PADDING, CONTENT_OUTER_PADDING)
it.border = JBUI.Borders.empty(4, 2 + contentOuterPadding + contentInnerPadding,
2 + contentOuterPadding, contentOuterPadding)
}, BorderLayout.SOUTH)
}