<commit_before>package org.jetbrains.plugins.scala.compilationCharts.ui

import java.awt._
import java.awt.geom.Rectangle2D

import com.intellij.icons.AllIcons
import com.intellij.openapi.actionSystem._
import com.intellij.openapi.actionSystem.ex.CustomComponentAction
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.util.NlsActions
import com.intellij.ui.components.JBPanel
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.components.BorderLayoutPanel
import javax.swing.border.LineBorder
import javax.swing.{Icon, JComponent}
import org.jetbrains.annotations.NotNull
import org.jetbrains.plugins.scala.ScalaBundle

import scala.concurrent.duration.DurationInt

class ActionPanel(setZoom: Zoom => Unit)
  extends BorderLayoutPanel {

  import ActionPanel._
  import Common._

  private var currentZoomIndex: Int = DefaultZoomIndex

  private val debugBorder = false
  private val debugBorderThickness = 2
  private def maybeSetDebugBorder(c: JComponent, color: Color): Unit = {
    if (debugBorder) {
      c.setBorder(new LineBorder(color, debugBorderThickness))
    }
  }

  setBorder(JBUI.Borders.empty)
  maybeSetDebugBorder(this, Color.RED)
  addToRight(createActionToolbar())
  withMinimumHeight((2.5 * NormalFont.getSize).toInt)

  private def createActionToolbar(): JComponent = {
    val actionGroup = new DefaultActionGroup
    val actions = Seq(LegendAction, new Separator, ResetZoomAction, ZoomOutAction, ZoomInAction)
    actions.foreach(actionGroup.add)
    val place = classOf[CompilationChartsComponent].getSimpleName
    val toolbar = ActionManager.getInstance.createActionToolbar(place, actionGroup, true)
    val component = toolbar.getComponent
    component
  }

  private object LegendAction
    extends DumbAwareAction
      with CustomComponentAction {

    override def createCustomComponent(presentation: Presentation, place: String): JComponent = {
      val panel = new JBPanel(new FlowLayout(FlowLayout.RIGHT))
      val items = Seq(
        new LegendItem("Production", ProdModuleColor),
        new LegendItem("Test", TestModuleColor),
        new LegendItem("Memory", MemoryLineColor, isLine = true)
      )
      items.foreach(panel.add)
      maybeSetDebugBorder(panel, Color.BLUE)
      panel
    }

    override def actionPerformed(e: AnActionEvent): Unit = ()

    private class LegendItem(name: String,
                             color: Color,
                             isLine: Boolean = false)
      extends JBPanel {

      maybeSetDebugBorder(this, Color.RED)

      private var initialized = false

      override def paintComponent(g: Graphics): Unit = {
        val graphics = g.asInstanceOf[Graphics2D]

        val clipBounds = g.getClipBounds()
        // HACK: I don't know why, but sometimes clipBounds is larger by 1 then component width and this causes flickering
        // Looks like something is wrong with parents layout, but I didn't manage to find out what exactly.
        // So this is probably not the best solution, but looks like it works fine.
        clipBounds.width = clipBounds.width.min(this.getWidth)
        clipBounds.height = clipBounds.height.min(this.getHeight)

        val rendering = graphics.getTextRendering(clipBounds, name, NormalFont, HAlign.Right, VAlign.Center)
        val textRect = rendering.rect.getBounds
        val colorHeight = if (isLine) MemoryLineStroke.thickness else ColorWidth
        val colorY = textRect.y + (textRect.height - colorHeight) / 2.0
        val colorRect = new Rectangle2D.Double(BeforeColorMargin, colorY, ColorWidth, colorHeight)

        graphics.printText(rendering, TextColor)
        graphics.printRect(colorRect, DiagramBackgroundColor)
        graphics.printRect(colorRect, color)

        // we don't have dynamic text, so we can set calculated preferred size just once
        if (!initialized) {
          val width = textRect.width + ColorWidth + BeforeColorMargin + AfterColorMargin
          val height = graphics.getFontMetrics(NormalFont).getHeight
          setPreferredSize(new Dimension(width, height))<commit_msg>compiler charts: enable antialiasing for text in ActionPanel #SCL-17802<commit_after>package org.jetbrains.plugins.scala.compilationCharts.ui

import java.awt._
import java.awt.geom.Rectangle2D

import com.intellij.icons.AllIcons
import com.intellij.ide.ui.UISettings
import com.intellij.openapi.actionSystem._
import com.intellij.openapi.actionSystem.ex.CustomComponentAction
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.util.NlsActions
import com.intellij.ui.components.JBPanel
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.components.BorderLayoutPanel
import javax.swing.border.LineBorder
import javax.swing.{Icon, JComponent}
import org.jetbrains.annotations.NotNull
import org.jetbrains.plugins.scala.ScalaBundle

import scala.concurrent.duration.DurationInt

class ActionPanel(setZoom: Zoom => Unit)
  extends BorderLayoutPanel {

  import ActionPanel._
  import Common._

  private var currentZoomIndex: Int = DefaultZoomIndex

  private val debugBorder = false
  private val debugBorderThickness = 2
  private def maybeSetDebugBorder(c: JComponent, color: Color): Unit = {
    if (debugBorder) {
      c.setBorder(new LineBorder(color, debugBorderThickness))
    }
  }

  setBorder(JBUI.Borders.empty)
  maybeSetDebugBorder(this, Color.RED)
  addToRight(createActionToolbar())
  withMinimumHeight((2.5 * NormalFont.getSize).toInt)

  private def createActionToolbar(): JComponent = {
    val actionGroup = new DefaultActionGroup
    val actions = Seq(LegendAction, new Separator, ResetZoomAction, ZoomOutAction, ZoomInAction)
    actions.foreach(actionGroup.add)
    val place = classOf[CompilationChartsComponent].getSimpleName
    val toolbar = ActionManager.getInstance.createActionToolbar(place, actionGroup, true)
    val component = toolbar.getComponent
    component
  }

  private object LegendAction
    extends DumbAwareAction
      with CustomComponentAction {

    override def createCustomComponent(presentation: Presentation, place: String): JComponent = {
      val panel = new JBPanel(new FlowLayout(FlowLayout.RIGHT))
      val items = Seq(
        new LegendItem("Production", ProdModuleColor),
        new LegendItem("Test", TestModuleColor),
        new LegendItem("Memory", MemoryLineColor, isLine = true)
      )
      items.foreach(panel.add)
      maybeSetDebugBorder(panel, Color.BLUE)
      panel
    }

    override def actionPerformed(e: AnActionEvent): Unit = ()

    private class LegendItem(name: String,
                             color: Color,
                             isLine: Boolean = false)
      extends JBPanel {

      maybeSetDebugBorder(this, Color.RED)

      private var initialized = false

      override def paintComponent(g: Graphics): Unit = {
        val graphics = g.asInstanceOf[Graphics2D]

        val clipBounds = g.getClipBounds()
        // HACK: I don't know why, but sometimes clipBounds is larger by 1 then component width and this causes flickering
        // Looks like something is wrong with parents layout, but I didn't manage to find out what exactly.
        // So this is probably not the best solution, but looks like it works fine.
        clipBounds.width = clipBounds.width.min(this.getWidth)
        clipBounds.height = clipBounds.height.min(this.getHeight)

        val rendering = graphics.getTextRendering(clipBounds, name, NormalFont, HAlign.Right, VAlign.Center)
        val textRect = rendering.rect.getBounds
        val colorHeight = if (isLine) MemoryLineStroke.thickness else ColorWidth
        val colorY = textRect.y + (textRect.height - colorHeight) / 2.0
        val colorRect = new Rectangle2D.Double(BeforeColorMargin, colorY, ColorWidth, colorHeight)

        UISettings.setupAntialiasing(graphics)
        graphics.printText(rendering, TextColor)
        graphics.printRect(colorRect, DiagramBackgroundColor)
        graphics.printRect(colorRect, color)

        // we don't have dynamic text, so we can set calculated preferred size just once
        if (!initialized) {
          val width = textRect.width + ColorWidth + BeforeColorMargin + AfterColorMargin
          val height = graphics.getFontMetrics(NormalFont).getHeight
          setPreferredSize(new Dimension(width, height))
{'fraction_non_alphanumeric': 0.07868537843348926, 'fraction_numerical': 0.0027587861341009955, 'mean_word_length': 3.6270810210876805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3755374', 'n_tokens_mistral': 2347, 'n_tokens_neox': 2301, 'n_words': 592}