<commit_before>@file:Suppress("InvalidBundleOrProperty")

package cn.yiiguxing.plugin.translate.ui

import cn.yiiguxing.plugin.translate.message
import cn.yiiguxing.plugin.translate.ui.form.WordDialogForm
import cn.yiiguxing.plugin.translate.util.Settings
import cn.yiiguxing.plugin.translate.util.invokeLater
import cn.yiiguxing.plugin.translate.wordbook.WordBookItem
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.wm.IdeFocusManager
import com.intellij.util.ui.JBFont
import com.intellij.util.ui.JBUI
import java.awt.event.ActionEvent
import java.lang.Boolean.TRUE
import javax.swing.AbstractAction
import javax.swing.Action
import javax.swing.SwingConstants

/**
 * Word of the day dialog
 *
 * Created by Yii.Guxing on 2019/08/20.
 */
class WordDialog(project: Project?, words: List<WordBookItem>) : WordDialogForm(project) {

    private val focusManager: IdeFocusManager = IdeFocusManager.getInstance(project)
    private val layout = FixedSizeCardLayout()

    private var words: List<WordBookItem> = emptyList()
    private var currentWordIndex: Int = -1

    private val nextWordAction: Action = NextWordAction()
    private val previousWordAction: Action = PreviousWordAction()

    init {
        initViews()
        setWords(words)

        Disposer.register(disposable, ttsButton)
        init()
    }

    private fun initViews() {
        isModal = false
        title = message("word.of.the.day.title")
        horizontalStretch = 1.33f
        verticalStretch = 1.25f
        setCancelButtonText(message("word.of.the.day.close"))

        initFont()

        explainsCard.apply {
            removeAll()
            layout = this@WordDialog.layout
            add(maskPanel, CARD_MASK)
            add(explainsView, CARD_EXPLAINS_VIEW)
        }

        ttsButton.verticalTextPosition = SwingConstants.BOTTOM
        explainsView.border = JBUI.Borders.empty(10)
        showExplanationButton.addActionListener { layout.show(explainsCard, CARD_EXPLAINS_VIEW) }
    }

    private fun initFont() {
        var primaryFont: JBFont = UI.defaultFont.deriveScaledFont(15f)
        var phoneticFont: JBFont = UI.defaultFont.deriveScaledFont(14f)

        with(Settings) {
            if (isOverrideFont) {
                primaryFont = primaryFontFamily
                    ?.let { JBUI.Fonts.create(it, 15) }
                    ?: primaryFont
                phoneticFont = phoneticFontFamily
                    ?.let { JBUI.Fonts.create(it, 14) }
                    ?: phoneticFont
            }
        }

        ttsButton.font = phoneticFont
        explainsView.font = primaryFont
        wordView.font = primaryFont.biggerOn(1f).asBold()
    }

    override fun getStyle(): DialogStyle = DialogStyle.COMPACT

    override fun createActions(): Array<Action> = arrayOf(previousWordAction, nextWordAction, cancelAction)

    fun setWords(words: List<WordBookItem>) {
        require(words.isNotEmpty()) { "Word list is empty" }
        if (words === this.words) {
            return
        }

        this.words = words<commit_msg>Fix word dialog UI<commit_after>@file:Suppress("InvalidBundleOrProperty")

package cn.yiiguxing.plugin.translate.ui

import cn.yiiguxing.plugin.translate.message
import cn.yiiguxing.plugin.translate.ui.form.WordDialogForm
import cn.yiiguxing.plugin.translate.util.Settings
import cn.yiiguxing.plugin.translate.util.invokeLater
import cn.yiiguxing.plugin.translate.wordbook.WordBookItem
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.wm.IdeFocusManager
import com.intellij.util.ui.JBFont
import com.intellij.util.ui.JBUI
import java.awt.event.ActionEvent
import java.lang.Boolean.TRUE
import javax.swing.AbstractAction
import javax.swing.Action

/**
 * Word of the day dialog
 *
 * Created by Yii.Guxing on 2019/08/20.
 */
class WordDialog(project: Project?, words: List<WordBookItem>) : WordDialogForm(project) {

    private val focusManager: IdeFocusManager = IdeFocusManager.getInstance(project)
    private val layout = FixedSizeCardLayout()

    private var words: List<WordBookItem> = emptyList()
    private var currentWordIndex: Int = -1

    private val nextWordAction: Action = NextWordAction()
    private val previousWordAction: Action = PreviousWordAction()

    init {
        initViews()
        setWords(words)

        Disposer.register(disposable, ttsButton)
        init()
    }

    private fun initViews() {
        isModal = false
        title = message("word.of.the.day.title")
        horizontalStretch = 1.33f
        verticalStretch = 1.25f
        setCancelButtonText(message("word.of.the.day.close"))

        initFont()

        explainsCard.apply {
            removeAll()
            layout = this@WordDialog.layout
            add(maskPanel, CARD_MASK)
            add(explainsView, CARD_EXPLAINS_VIEW)
        }

        explainsView.border = JBUI.Borders.empty(10)
        showExplanationButton.addActionListener { layout.show(explainsCard, CARD_EXPLAINS_VIEW) }
    }

    private fun initFont() {
        var primaryFont: JBFont = UI.defaultFont.deriveScaledFont(15f)
        var phoneticFont: JBFont = UI.defaultFont.deriveScaledFont(14f)

        with(Settings) {
            if (isOverrideFont) {
                primaryFont = primaryFontFamily
                    ?.let { JBUI.Fonts.create(it, 15) }
                    ?: primaryFont
                phoneticFont = phoneticFontFamily
                    ?.let { JBUI.Fonts.create(it, 14) }
                    ?: phoneticFont
            }
        }

        ttsButton.font = phoneticFont
        explainsView.font = primaryFont
        wordView.font = primaryFont.biggerOn(3f).asBold()
    }

    override fun getStyle(): DialogStyle = DialogStyle.COMPACT

    override fun createActions(): Array<Action> = arrayOf(previousWordAction, nextWordAction, cancelAction)

    fun setWords(words: List<WordBookItem>) {
        require(words.isNotEmpty()) { "Word list is empty" }
        if (words === this.words) {
            return
        }

        this.words = words
{'fraction_non_alphanumeric': 0.09394334093129274, 'fraction_numerical': 0.008466297622924128, 'mean_word_length': 2.775660725261217, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '137146', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1799, 'n_words': 338}