Skip to content

Commit

Permalink
Merge pull request #470 from sanao1006/show-screenshot-filename-multi…
Browse files Browse the repository at this point in the history
…ple-lines

Show screenshot filename multiple lines
  • Loading branch information
takahirom authored Aug 20, 2024
2 parents 4e22977 + 60c4a2f commit 220f8bd
Showing 1 changed file with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,59 @@ class ImageListCellRenderer : ListCellRenderer<Pair<String, Long>> {
val isSelected: Boolean
)

private fun collapseFileNameToFitWidth(text: String, maxWidth: Int): String {
val label = JBLabel(text)
val fontMetrics = label.getFontMetrics(label.font)

// Adjust the maximum width to take into account the width of the scrollbar (about 20 pixels).
val bufferedWidth = maxOf(1, maxWidth - 20)

// Cache to store the width of previously computed substrings
val widthCache = mutableMapOf<String, Int>()

// Calculates the width of the given substring, using a cache for efficiency.
fun stringWidth(substring: String): Int {
return widthCache.getOrPut(substring) { fontMetrics.stringWidth(substring) }
}

/**
* Performs binary search to find the maximum number of characters that fit within the specified width.
* This helps to determine the end position of the substring that fits within the available width.
*/
fun findMaxCharsForWidth(start: Int): Int {
var low = start
var high = minOf(text.length, start + bufferedWidth)

while (low < high) {
val mid = (low + high + 1) / 2
val substringWidth = stringWidth(text.substring(start, mid))
if (substringWidth <= bufferedWidth) {
low = mid
} else {
high = mid - 1
}
}

return low
}

var start = 0
val lines = mutableListOf<String>()

// Loop to split the text into multiple lines
while (start < text.length) {
val end = findMaxCharsForWidth(start)
if (end == start) { // Prevent infinite loop
break
}
lines.add(text.substring(start, end))
start = end
}

// Return the result in HTML format with <br> tags separating lines
return "<html>${lines.joinToString("<br>")}</html>"
}

private val imageCache = SLRUMap<Pair<String, Long>, Image>(300, 50)
private val lruCache = SLRUMap<CacheKey, Box>(300, 50)
override fun getListCellRendererComponent(
Expand Down Expand Up @@ -282,7 +335,9 @@ class ImageListCellRenderer : ListCellRenderer<Pair<String, Long>> {
val box = JBBox.createVerticalBox().apply {
add(Box.createVerticalStrut(16))
add(imageLabel)
add(JBLabel(file.name))

val label = JBLabel(collapseFileNameToFitWidth(file.name, list.width))
add(label)
// Add space between items
add(Box.createVerticalStrut(16))
}
Expand Down

0 comments on commit 220f8bd

Please sign in to comment.