Skip to content

Commit

Permalink
added settings to control preview size
Browse files Browse the repository at this point in the history
  • Loading branch information
vlgrigorev committed Nov 13, 2018
1 parent fb97c47 commit 1be3d47
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/main/kotlin/com/mistamek/drawablepreview/Extensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mistamek.drawablepreview

fun String.getDigits() = try {
this.replace("\\D+", "").toInt()
} catch (exception: Exception) {
null
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.mistamek.drawablepreview.factories

import com.mistamek.drawablepreview.drawables.Utils
import com.mistamek.drawablepreview.settings.SettingsUtils
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO

object BitmapImageFactory {
fun createBitmapImage(path: String): BufferedImage? {
ImageIO.read(File(path)).let {
val output = BufferedImage(Constants.ICON_SIZE, Constants.ICON_SIZE, BufferedImage.TYPE_INT_ARGB)
val output = BufferedImage(SettingsUtils.getPreviewSize(), SettingsUtils.getPreviewSize(), BufferedImage.TYPE_INT_ARGB)
Utils.drawResizedIcon(it, output)
return output
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.mistamek.drawablepreview.factories

import com.intellij.util.ui.UIUtil

object Constants {
const val ICON_SIZE = 16
val ICON_SIZE = if (UIUtil.isRetina()) 36 else 16
const val XML_TYPE = ".xml"
const val DRAWABLES_FOLDER_TYPE = "drawable"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.mistamek.drawablepreview.drawables.DrawableInflater
import com.mistamek.drawablepreview.drawables.Utils
import com.mistamek.drawablepreview.drawables.dom.Drawable
import com.mistamek.drawablepreview.drawables.forEach
import com.mistamek.drawablepreview.settings.SettingsUtils
import org.w3c.dom.Document
import org.w3c.dom.Element
import org.w3c.dom.Node
Expand All @@ -24,7 +25,7 @@ object XmlImageFactory {
return parseDocument(path)?.let { document ->
getDrawableImage(document.documentElement)
?: StringBuilder(100).let { builder ->
val imageTargetSize = VdPreview.TargetSize.createSizeFromWidth(Constants.ICON_SIZE)
val imageTargetSize = VdPreview.TargetSize.createSizeFromWidth(SettingsUtils.getPreviewSize())
VdPreview.getPreviewFromVectorDocument(imageTargetSize, document, builder)
}
}
Expand All @@ -50,7 +51,7 @@ object XmlImageFactory {

private fun getDrawableImage(rootElement: Element): BufferedImage? {
return DrawableInflater.getDrawable(rootElement)?.let { drawable ->
return@let BufferedImage(Constants.ICON_SIZE, Constants.ICON_SIZE, BufferedImage.TYPE_INT_ARGB).also { image ->
return@let BufferedImage(SettingsUtils.getPreviewSize(), SettingsUtils.getPreviewSize(), BufferedImage.TYPE_INT_ARGB).also { image ->
drawable.draw(image)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package com.mistamek.drawablepreview.settings

import com.intellij.openapi.options.Configurable
import com.mistamek.drawablepreview.getDigits
import javax.swing.JComponent

class SettingsConfigurable : Configurable {

private var settingsUi: SettingsUi? = null

override fun isModified() = false
override fun isModified(): Boolean {
return settingsUi?.previewSizeTextField?.let {
SettingsUtils.isModified(it.text.getDigits() ?: 0)
} ?: false
}

override fun getDisplayName() = "Android drawable preview"

override fun apply() {
settingsUi?.previewSizeTextField?.let {
SettingsUtils.apply(it.text.getDigits() ?: 0)
}
}

override fun createComponent(): JComponent? {
settingsUi = SettingsUi()
settingsUi?.previewSizeTextField?.text = SettingsUtils.getPreviewSize().toString()
return settingsUi?.rootPanel
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mistamek.drawablepreview.settings

import com.intellij.ide.util.PropertiesComponent
import com.mistamek.drawablepreview.factories.Constants

object SettingsUtils {

private const val PROPERTIES_SIZE = "com.mistamek.drawablepreview.settings.PropertiesSize"

fun getPreviewSize() = PropertiesComponent.getInstance().getInt(PROPERTIES_SIZE, Constants.ICON_SIZE)

fun isModified(previewSize: Int): Boolean {
return previewSize != getPreviewSize()
}

fun apply(previewSize: Int) {
PropertiesComponent.getInstance().setValue(PROPERTIES_SIZE, previewSize, Constants.ICON_SIZE)
}
}

0 comments on commit 1be3d47

Please sign in to comment.