terminal: extract code converting JediTerm text style to TextAttributes

GitOrigin-RevId: efc70e1d55e3a772f32c7a1ec54c6e3d04f2b43b
This commit is contained in:
Sergey Simonchik
2023-10-26 12:51:57 +02:00
committed by intellij-monorepo-bot
parent 5d6f4a3258
commit 2e0eec3551
3 changed files with 52 additions and 68 deletions

View File

@@ -9,7 +9,6 @@ import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.editor.markup.EffectType
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.openapi.util.Disposer
import com.intellij.terminal.JBTerminalSystemSettingsProviderBase
@@ -17,9 +16,7 @@ import com.jediterm.terminal.StyledTextConsumer
import com.jediterm.terminal.TextStyle
import com.jediterm.terminal.emulator.ColorPalette
import com.jediterm.terminal.model.CharBuffer
import com.jediterm.terminal.ui.AwtTransformers
import java.awt.Color
import java.awt.Font
import org.jetbrains.plugins.terminal.exp.TerminalUiUtils.toTextAttributes
class SimpleTerminalController(
private val settings: JBTerminalSystemSettingsProviderBase,
@@ -135,33 +132,7 @@ class SimpleTerminalController(
caretPainter.repaint()
}
private fun TextStyle.toTextAttributes(): TextAttributes {
return TextAttributes().also { attr ->
attr.backgroundColor = AwtTransformers.toAwtColor(palette.getBackground(terminalModel.styleState.getBackground(backgroundForRun)))
attr.foregroundColor = getStyleForeground(this)
if (hasOption(TextStyle.Option.BOLD)) {
attr.fontType = attr.fontType or Font.BOLD
}
if (hasOption(TextStyle.Option.ITALIC)) {
attr.fontType = attr.fontType or Font.ITALIC
}
if (hasOption(TextStyle.Option.UNDERLINED)) {
attr.withAdditionalEffect(EffectType.LINE_UNDERSCORE, attr.foregroundColor)
}
}
}
private fun getStyleForeground(style: TextStyle): Color {
val foreground = palette.getForeground(terminalModel.styleState.getForeground(style.foregroundForRun))
return if (style.hasOption(TextStyle.Option.DIM)) {
val background = palette.getBackground(terminalModel.styleState.getBackground(style.backgroundForRun))
Color((foreground.red + background.red) / 2,
(foreground.green + background.green) / 2,
(foreground.blue + background.blue) / 2,
foreground.alpha)
}
else AwtTransformers.toAwtColor(foreground)!!
}
private fun TextStyle.toTextAttributes(): TextAttributes = this.toTextAttributes(palette, terminalModel, true)
override fun dispose() {
Disposer.dispose(caretModel)

View File

@@ -9,7 +9,6 @@ import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.editor.markup.EffectType
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.TextRange
@@ -19,9 +18,8 @@ import com.intellij.util.concurrency.annotations.RequiresEdt
import com.jediterm.terminal.StyledTextConsumer
import com.jediterm.terminal.TextStyle
import com.jediterm.terminal.model.CharBuffer
import com.jediterm.terminal.ui.AwtTransformers
import org.jetbrains.plugins.terminal.exp.TerminalDataContextUtils.IS_OUTPUT_EDITOR_KEY
import java.awt.Color
import org.jetbrains.plugins.terminal.exp.TerminalUiUtils.toTextAttributes
import java.awt.Font
class TerminalOutputController(
@@ -247,37 +245,9 @@ class TerminalOutputController(
}
private fun TextStyle.toTextAttributes(): TextAttributes {
return TextAttributes().also { attr ->
val background = palette.getBackground(terminalModel.styleState.getBackground(backgroundForRun))
val defaultBackground = palette.defaultBackground
// todo: it is a hack to not set default background, because it is different from the block background.
// They should match to remove this hack.
if (background != defaultBackground) {
attr.backgroundColor = AwtTransformers.toAwtColor(background)
}
attr.foregroundColor = getStyleForeground(this)
if (hasOption(TextStyle.Option.BOLD)) {
attr.fontType = attr.fontType or Font.BOLD
}
if (hasOption(TextStyle.Option.ITALIC)) {
attr.fontType = attr.fontType or Font.ITALIC
}
if (hasOption(TextStyle.Option.UNDERLINED)) {
attr.withAdditionalEffect(EffectType.LINE_UNDERSCORE, attr.foregroundColor)
}
}
}
private fun getStyleForeground(style: TextStyle): Color {
val foreground = palette.getForeground(terminalModel.styleState.getForeground(style.foregroundForRun))
return if (style.hasOption(TextStyle.Option.DIM)) {
val background = palette.getBackground(terminalModel.styleState.getBackground(style.backgroundForRun))
Color((foreground.red + background.red) / 2,
(foreground.green + background.green) / 2,
(foreground.blue + background.blue) / 2,
foreground.alpha)
}
else AwtTransformers.toAwtColor(foreground)!!
// Do not apply default background to [TextAttributes],
// because block background is painted by [TerminalBlockBackgroundRenderer] with gradient.
return this.toTextAttributes(palette, terminalModel, false)
}
/** It is implied that the command is not null */

View File

@@ -14,6 +14,8 @@ import com.intellij.openapi.editor.event.EditorMouseEvent
import com.intellij.openapi.editor.ex.EditorGutterFreePainterAreaState
import com.intellij.openapi.editor.impl.ContextMenuPopupHandler
import com.intellij.openapi.editor.impl.EditorImpl
import com.intellij.openapi.editor.markup.EffectType
import com.intellij.openapi.editor.markup.TextAttributes
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.terminal.JBTerminalSystemSettingsProviderBase
@@ -24,9 +26,11 @@ import com.intellij.util.concurrency.annotations.RequiresEdt
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UIUtil
import com.jediterm.core.util.TermSize
import java.awt.Component
import java.awt.Cursor
import java.awt.Dimension
import com.jediterm.terminal.TerminalColor
import com.jediterm.terminal.TextStyle
import com.jediterm.terminal.emulator.ColorPalette
import com.jediterm.terminal.ui.AwtTransformers
import java.awt.*
import java.awt.event.ComponentAdapter
import java.awt.event.ComponentEvent
import java.util.concurrent.CompletableFuture
@@ -122,6 +126,45 @@ object TerminalUiUtils {
fun toFloatAndScale(value: Int): Float = JBUIScale.scale(value.toFloat())
internal fun TextStyle.toTextAttributes(palette: ColorPalette,
terminalModel: TerminalModel,
applyDefaultBackground: Boolean): TextAttributes {
return TextAttributes().also { attr ->
val background: TerminalColor? = if (applyDefaultBackground) {
terminalModel.styleState.getBackground(backgroundForRun)
}
else {
backgroundForRun
}
if (background != null) {
attr.backgroundColor = AwtTransformers.toAwtColor(palette.getBackground(background))
}
attr.foregroundColor = getForegroundColor(this, palette, terminalModel)
if (hasOption(TextStyle.Option.BOLD)) {
attr.fontType = attr.fontType or Font.BOLD
}
if (hasOption(TextStyle.Option.ITALIC)) {
attr.fontType = attr.fontType or Font.ITALIC
}
if (hasOption(TextStyle.Option.UNDERLINED)) {
attr.withAdditionalEffect(EffectType.LINE_UNDERSCORE, attr.foregroundColor)
}
}
}
private fun getForegroundColor(style: TextStyle, palette: ColorPalette, terminalModel: TerminalModel): Color {
val foreground = palette.getForeground(terminalModel.styleState.getForeground(style.foregroundForRun))
return if (style.hasOption(TextStyle.Option.DIM)) {
val background = palette.getBackground(terminalModel.styleState.getBackground(style.backgroundForRun))
@Suppress("UseJBColor")
Color((foreground.red + background.red) / 2,
(foreground.green + background.green) / 2,
(foreground.blue + background.blue) / 2,
foreground.alpha)
}
else AwtTransformers.toAwtColor(foreground)!!
}
private val LOG = logger<TerminalUiUtils>()
private const val TIMEOUT = 2000
}