Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checkboxes to graphic insertion wizard for relative width or height #3819

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

### Added
* Add checkboxes to graphic insertion wizard for relative width or height

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.components.JBTextField
import com.intellij.ui.components.fields.ExpandableTextField
import com.intellij.ui.components.panels.HorizontalLayout
import nl.hannahsten.texifyidea.lang.commands.LatexGenericRegularCommand
import nl.hannahsten.texifyidea.lang.graphic.CaptionLocation
import nl.hannahsten.texifyidea.lang.graphic.FigureLocation
import nl.hannahsten.texifyidea.util.*
import nl.hannahsten.texifyidea.util.magic.FileMagic
import java.awt.Dimension
import java.awt.event.ActionEvent
import java.io.File
import java.util.*
import javax.swing.Box
Expand Down Expand Up @@ -66,6 +68,31 @@ open class InsertGraphicWizardDialogWrapper(val initialFilePath: String = "") :
}
}

private fun addOrRemoveSizeCommand(field: JBTextField, event: ActionEvent, command: String) {
val isSelected = (event.source as JBCheckBox).isSelected
if (isSelected && command !in field.text) {
field.text += command
}
else if (!isSelected && command in field.text) {
field.text = field.text.remove(command)
}
}

private val txtWidthRelative = JBCheckBox("Relative to line width").apply {
addActionListener {
// linewidth seems to be a good default: https://tex.stackexchange.com/a/17085/98850
val command = LatexGenericRegularCommand.LINEWIDTH.commandWithSlash
addOrRemoveSizeCommand(txtWidth, it, command)
}
}

private val txtHeightRelative = JBCheckBox("Relative to text height").apply {
addActionListener {
val command = LatexGenericRegularCommand.TEXTHEIGHT.commandWithSlash
addOrRemoveSizeCommand(txtHeight, it, command)
}
}

/**
* The angle option for the graphic. When empty, no angle. Not necessarily a number.
*/
Expand All @@ -76,6 +103,25 @@ open class InsertGraphicWizardDialogWrapper(val initialFilePath: String = "") :
}
}

private val keepAspectRatio = JBCheckBox("Keep aspect ratio").apply {
addActionListener {
val option = "keepaspectratio"
val isSelected = (it.source as JBCheckBox).isSelected
if (isSelected && option !in txtCustomOptions.text) {
if (txtCustomOptions.text.isNotBlank()) txtCustomOptions.text += ","
txtCustomOptions.text += option
}
else if (!isSelected && option in txtCustomOptions.text) {
if (",$option" in txtCustomOptions.text) {
txtCustomOptions.text = txtCustomOptions.text.remove(",$option")
}
else {
txtCustomOptions.text = txtCustomOptions.text.remove(option)
}
}
}
}

/**
* The custom graphics options. This is basically the optional parameter of \includegraphics.
* Can get modified when the width/height/angle get modified.
Expand Down Expand Up @@ -211,7 +257,13 @@ open class InsertGraphicWizardDialogWrapper(val initialFilePath: String = "") :
addLabeledComponent(txtAngle, "Angle:", labelWidth)
}
add(optionsPanel)
addLabeledComponent(txtCustomOptions, "Custom:", labelWidth)
val relativeOptionsPanel = JPanel(HorizontalLayout(10)).apply {
add(txtWidthRelative)
add(txtHeightRelative)
add(keepAspectRatio)
}
add(relativeOptionsPanel)
addLabeledComponent(txtCustomOptions, "Custom:", 80)
}

private fun JPanel.addLayoutControls() {
Expand Down Expand Up @@ -258,19 +310,25 @@ open class InsertGraphicWizardDialogWrapper(val initialFilePath: String = "") :
}

private fun JTextField.updateGraphicsOptions(fieldName: String) {
val text = text.replace(",", "")
val newOptionValue = text.replace(",", "")
// Update
if (txtCustomOptions.text.contains("$fieldName=")) {
txtCustomOptions.text = txtCustomOptions.text
.replace(Regex("$fieldName=[^,]*"), Regex.escapeReplacement("$fieldName=$text"))
if (newOptionValue.isNotBlank()) {
txtCustomOptions.text = txtCustomOptions.text
.replace(Regex("$fieldName=[^,]*"), Regex.escapeReplacement("$fieldName=$newOptionValue"))
}
else {
// Remove
txtCustomOptions.text = txtCustomOptions.text.replace(Regex(",?$fieldName=[^,]*"), "")
}
}
// Nothing yet, set width property.
else if (txtCustomOptions.text.isBlank()) {
txtCustomOptions.text = "$fieldName=$text"
txtCustomOptions.text = "$fieldName=$newOptionValue"
}
// When there is something, append width property.
else {
txtCustomOptions.text += ",$fieldName=$text"
txtCustomOptions.text += ",$fieldName=$newOptionValue"
}
}

Expand Down
Loading