summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/codeInsight/daemon/impl/HintRenderer.kt
blob: 1f7ad13167f2b1418a24e72d7f148c0246b42a66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl

import com.intellij.codeInsight.hints.HintWidthAdjustment
import com.intellij.ide.ui.AntialiasingType
import com.intellij.ide.ui.UISettings
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorCustomElementRenderer
import com.intellij.openapi.editor.Inlay
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.openapi.editor.colors.EditorFontType
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable
import com.intellij.openapi.editor.ex.util.EditorUtil
import com.intellij.openapi.editor.impl.EditorImpl
import com.intellij.openapi.editor.impl.FocusModeModel
import com.intellij.openapi.editor.impl.FontInfo
import com.intellij.openapi.editor.markup.EffectType
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.openapi.util.Key
import com.intellij.ui.paint.EffectPainter
import com.intellij.util.ui.GraphicsUtil
import com.intellij.util.ui.StartupUiUtil
import org.intellij.lang.annotations.JdkConstants
import java.awt.*
import java.awt.font.FontRenderContext
import javax.swing.UIManager
import kotlin.math.ceil
import kotlin.math.max
import kotlin.math.roundToInt

open class HintRenderer(var text: String?) : EditorCustomElementRenderer {
  var widthAdjustment: HintWidthAdjustment? = null

  override fun calcWidthInPixels(inlay: Inlay<*>): Int {
    return calcWidthInPixels(inlay.editor, text, widthAdjustment, useEditorFont())
  }

  protected open fun getTextAttributes(editor: Editor): TextAttributes? {
    return editor.colorsScheme.getAttributes(DefaultLanguageHighlighterColors.INLINE_PARAMETER_HINT)
  }

  override fun paint(inlay: Inlay<*>, g: Graphics, r: Rectangle, textAttributes: TextAttributes) {
    val editor = inlay.editor
    if (editor !is EditorImpl) return

    val focusModeRange = editor.focusModeRange
    val attributes = if (focusModeRange != null && (inlay.offset <= focusModeRange.startOffset || focusModeRange.endOffset <= inlay.offset)) {
      editor.getUserData(FocusModeModel.FOCUS_MODE_ATTRIBUTES) ?: getTextAttributes(editor)
    }
    else {
      getTextAttributes(editor)
    }

    paintHint(g, editor, r, text, attributes, attributes ?: textAttributes, widthAdjustment, useEditorFont())
  }

  /**
   * @deprecated
   * @see calcHintTextWidth
   */
  protected fun doCalcWidth(text: String?, fontMetrics: FontMetrics): Int {
    return calcHintTextWidth(text, fontMetrics)
  }

  protected open fun useEditorFont() = useEditorFontFromSettings()

  companion object {
    @JvmStatic
    fun calcWidthInPixels(editor: Editor, text: String?, widthAdjustment: HintWidthAdjustment?): Int {
      return calcWidthInPixels(editor, text, widthAdjustment, useEditorFontFromSettings())
    }

    @JvmStatic
    fun calcWidthInPixels(editor: Editor, text: String?, widthAdjustment: HintWidthAdjustment?, useEditorFont: Boolean): Int {
      val fontMetrics = getFontMetrics(editor, useEditorFont).metrics
      return calcHintTextWidth(text, fontMetrics) + calcWidthAdjustment(text, editor, fontMetrics, widthAdjustment)
    }

    @JvmStatic
    fun paintHint(g: Graphics,
                  editor: EditorImpl,
                  r: Rectangle,
                  text: String?,
                  attributes: TextAttributes?,
                  textAttributes: TextAttributes,
                  widthAdjustment: HintWidthAdjustment?) {
      paintHint(g, editor, r, text, attributes, textAttributes, widthAdjustment, useEditorFontFromSettings())
    }

    @JvmStatic
    fun paintHint(g: Graphics,
                  editor: EditorImpl,
                  r: Rectangle,
                  text: String?,
                  attributes: TextAttributes?,
                  textAttributes: TextAttributes,
                  widthAdjustment: HintWidthAdjustment?,
                  useEditorFont: Boolean) {
      val ascent = editor.ascent
      val descent = editor.descent
      val g2d = g as Graphics2D

      if (text != null && attributes != null) {
        val fontMetrics = getFontMetrics(editor, useEditorFont)
        val gap = if (r.height < fontMetrics.lineHeight + 2) 1 else 2
        val backgroundColor = attributes.backgroundColor
        if (backgroundColor != null) {
          val alpha = if (isInsufficientContrast(attributes, textAttributes)) 1.0f else BACKGROUND_ALPHA
          val config = GraphicsUtil.setupAAPainting(g)
          GraphicsUtil.paintWithAlpha(g, alpha)
          g.setColor(backgroundColor)
          g.fillRoundRect(r.x + 2, r.y + gap, r.width - 4, r.height - gap * 2, 8, 8)
          config.restore()
        }
        val foregroundColor = attributes.foregroundColor
        if (foregroundColor != null) {
          val savedHint = g2d.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING)
          val savedClip = g.getClip()

          g.setColor(foregroundColor)
          g.setFont(getFont(editor, useEditorFont))
          g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, AntialiasingType.getKeyForCurrentScope(false))
          g.clipRect(r.x + 3, r.y + 2, r.width - 6, r.height - 4)
          val metrics = fontMetrics.metrics
          val startX = r.x + 7
          val startY = r.y + max(ascent, (r.height + metrics.ascent - metrics.descent) / 2) - 1

          val adjustment = calcWidthAdjustment(text, editor, g.fontMetrics, widthAdjustment)
          if (adjustment == 0) {
            g.drawString(text, startX, startY)
          }
          else {
            val adjustmentPosition = widthAdjustment!!.adjustmentPosition
            val firstPart = text.substring(0, adjustmentPosition)
            val secondPart = text.substring(adjustmentPosition)
            g.drawString(firstPart, startX, startY)
            g.drawString(secondPart, startX + g.getFontMetrics().stringWidth(firstPart) + adjustment, startY)
          }

          g.setClip(savedClip)
          g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, savedHint)
        }
      }
      val effectColor = textAttributes.effectColor
      val effectType = textAttributes.effectType
      if (effectColor != null) {
        g.setColor(effectColor)
        val xStart = r.x
        val xEnd = r.x + r.width
        val y = r.y + ascent
        val font = editor.getColorsScheme().getFont(EditorFontType.PLAIN)
        @Suppress("NON_EXHAUSTIVE_WHEN")
        when (effectType) {
          EffectType.LINE_UNDERSCORE -> EffectPainter.LINE_UNDERSCORE.paint(g2d, xStart, y, xEnd - xStart, descent, font)
          EffectType.BOLD_LINE_UNDERSCORE -> EffectPainter.BOLD_LINE_UNDERSCORE.paint(g2d, xStart, y, xEnd - xStart, descent, font)
          EffectType.STRIKEOUT -> EffectPainter.STRIKE_THROUGH.paint(g2d, xStart, y, xEnd - xStart, editor.charHeight, font)
          EffectType.WAVE_UNDERSCORE -> EffectPainter.WAVE_UNDERSCORE.paint(g2d, xStart, y, xEnd - xStart, descent, font)
          EffectType.BOLD_DOTTED_LINE -> EffectPainter.BOLD_DOTTED_UNDERSCORE.paint(g2d, xStart, y, xEnd - xStart, descent, font)
        }
      }
    }

    private fun isInsufficientContrast(
      attributes: TextAttributes,
      surroundingAttributes: TextAttributes
    ): Boolean {
      val backgroundUnderHint = surroundingAttributes.backgroundColor
      if (backgroundUnderHint != null && attributes.foregroundColor != null) {
        val backgroundBlended = srcOverBlend(attributes.backgroundColor, backgroundUnderHint, BACKGROUND_ALPHA)

        val backgroundBlendedGrayed = backgroundBlended.toGray()
        val textGrayed = attributes.foregroundColor.toGray()
        val delta = Math.abs(backgroundBlendedGrayed - textGrayed)
        return delta < 10
      }
      return false
    }

    private fun Color.toGray(): Double {
      return (0.30 * red) + (0.59 * green) + (0.11 * blue)
    }

    private fun srcOverBlend(foreground: Color, background: Color, foregroundAlpha: Float): Color {
      val r = foreground.red * foregroundAlpha + background.red * (1.0f - foregroundAlpha)
      val g = foreground.green * foregroundAlpha + background.green * (1.0f - foregroundAlpha)
      val b = foreground.blue * foregroundAlpha + background.blue * (1.0f - foregroundAlpha)
      return Color(r.roundToInt(), g.roundToInt(), b.roundToInt())
    }

    private fun calcWidthAdjustment(text: String?, editor: Editor, fontMetrics: FontMetrics, widthAdjustment: HintWidthAdjustment?): Int {
      if (widthAdjustment == null || editor !is EditorImpl) return 0
      val editorTextWidth = editor.getFontMetrics(Font.PLAIN).stringWidth(widthAdjustment.editorTextToMatch)
      return max(0, editorTextWidth
                         + calcHintTextWidth(widthAdjustment.hintTextToMatch, fontMetrics)
                         - calcHintTextWidth(text, fontMetrics))
    }

    class MyFontMetrics internal constructor(editor: Editor, size: Float, @JdkConstants.FontStyle fontType: Int, useEditorFont: Boolean) {
      val metrics: FontMetrics
      val lineHeight: Int

      val font: Font
        get() = metrics.font

      init {
        val font = if (useEditorFont) {
          val editorFont = EditorUtil.getEditorFont()
          editorFont.deriveFont(fontType, size)
        } else {
          val familyName = UIManager.getFont("Label.font").family
          StartupUiUtil.getFontWithFallback(familyName, fontType, size)
        }
        val context = getCurrentContext(editor)
        metrics = FontInfo.getFontMetrics(font, context)
        // We assume this will be a better approximation to a real line height for a given font
        lineHeight = ceil(font.createGlyphVector(context, "Ap").visualBounds.height).toInt()
      }

      fun isActual(editor: Editor, size: Float, fontType: Int, familyName: String): Boolean {
        val font = metrics.font
        if (familyName != font.family || size != font.size2D || fontType != font.style) return false
        val currentContext = getCurrentContext(editor)
        return currentContext.equals(metrics.fontRenderContext)
      }

      private fun getCurrentContext(editor: Editor): FontRenderContext {
        val editorContext = FontInfo.getFontRenderContext(editor.contentComponent)
        return FontRenderContext(editorContext.transform,
                                 AntialiasingType.getKeyForCurrentScope(false),
                                 UISettings.editorFractionalMetricsHint)
      }
    }

    @JvmStatic
    protected fun getFontMetrics(editor: Editor, useEditorFont: Boolean): MyFontMetrics {
      val size = HintUtil.getSize(editor)
      var metrics = editor.getUserData(HINT_FONT_METRICS)
      val attributes = editor.colorsScheme.getAttributes(DefaultLanguageHighlighterColors.INLINE_PARAMETER_HINT)
      val fontType = attributes.fontType
      val familyName = if (useEditorFont) {
        EditorColorsManager.getInstance().globalScheme.editorFontName
      }
      else {
        StartupUiUtil.getLabelFont().family
      }
      if (metrics != null && !metrics.isActual(editor, size, fontType, familyName)) {
        metrics = null
      }
      if (metrics == null) {
        metrics = MyFontMetrics(editor, size, fontType, useEditorFont)
        editor.putUserData(HINT_FONT_METRICS, metrics)
      }
      return metrics
    }

    @JvmStatic
    fun useEditorFontFromSettings() = EditorSettingsExternalizable.getInstance().isUseEditorFontInInlays

    private fun getFont(editor: Editor, useEditorFont: Boolean): Font {
      return getFontMetrics(editor, useEditorFont).font
    }

    @JvmStatic
    protected fun calcHintTextWidth(text: String?, fontMetrics: FontMetrics): Int {
      return if (text == null) 0 else fontMetrics.stringWidth(text) + 14
    }

    private val HINT_FONT_METRICS = Key.create<MyFontMetrics>("ParameterHintFontMetrics")
    private const val BACKGROUND_ALPHA = 0.55f
  }

  // workaround for KT-12063 "IllegalAccessError when accessing @JvmStatic protected member of a companion object from a subclass"
  @JvmSynthetic
  @JvmName("getFontMetrics$")
  protected fun getFontMetrics(editor: Editor, useEditorFont: Boolean): MyFontMetrics = Companion.getFontMetrics(editor, useEditorFont)
}