Skip to content

Commit

Permalink
Merge pull request #476 from sanao1006/feature/search_screenshots
Browse files Browse the repository at this point in the history
Filtering Images by Text Field
  • Loading branch information
takahirom authored Sep 7, 2024
2 parents 91c5e45 + 5fccf2c commit b7a8bd9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class PreviewViewModel {

var coroutineScope = MainScope()
val imagesStateFlow = MutableStateFlow<List<Pair<String, Long>>>(listOf())
private val searchText = MutableStateFlow("")
private val lastEditingFileName = MutableStateFlow<String?>(null)
val statusText = MutableStateFlow("No images found")
private val _dropDownUiState = MutableStateFlow(ActionToolbarUiState())
Expand Down Expand Up @@ -70,6 +71,16 @@ class PreviewViewModel {
}
}

fun onSearchTextChanged(project: Project, text: String) {
searchText.value = text
coroutineScope.launch {
updateListJob?.cancel()
refreshListProcess(project)
selectListIndexByCaret(project)
fetchTasks(project)
}
}

fun onRefreshButtonClicked(project: Project, selectedTaskName: String) {
roborazziLog("Executing task '$selectedTaskName'...")
_dropDownUiState.update { currentUiState ->
Expand Down Expand Up @@ -194,8 +205,14 @@ class PreviewViewModel {
}
}

allPreviewImageFiles.addAll(findImages(classes, files))
allPreviewImageFiles.addAll(findImages(functions, files))
allPreviewImageFiles.addAll(
findImages(classes, files)
.filter { it.name.contains(searchText.value, ignoreCase = true) }
)
allPreviewImageFiles.addAll(
findImages(functions, files)
.filter { it.name.contains(searchText.value, ignoreCase = true) }
)

if (allPreviewImageFiles.isEmpty()) {
statusText.value = "No images found"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.intellij.ui.components.JBBox
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBList
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.components.JBTextField
import com.intellij.ui.content.ContentFactory
import com.intellij.util.containers.SLRUMap
import com.intellij.util.messages.MessageBusConnection
Expand All @@ -49,13 +50,17 @@ import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.ListCellRenderer
import javax.swing.ListSelectionModel
import javax.swing.event.DocumentEvent
import javax.swing.event.DocumentListener


class RoborazziPreviewToolWindowFactory : ToolWindowFactory {
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
val contentFactory = ContentFactory.getInstance()
val panel = RoborazziPreviewPanel(project)

val content = contentFactory.createContent(panel, "", false)

toolWindow.contentManager.addContent(content)

if (toolWindow is ToolWindowEx) {
Expand Down Expand Up @@ -160,6 +165,22 @@ class RoborazziPreviewPanel(project: Project) : JPanel(BorderLayout()) {
anchor = GridBagConstraints.WEST
insets = JBUI.insets(4)
})
add((JBTextField().apply {
emptyText.text = "Enter screenshot name..."
document.addDocumentListener(object : DocumentListener {
override fun insertUpdate(e: DocumentEvent) {
viewModel?.onSearchTextChanged(project, text)
}

override fun removeUpdate(e: DocumentEvent) {
viewModel?.onSearchTextChanged(project, text)
}

override fun changedUpdate(e: DocumentEvent) {
viewModel?.onSearchTextChanged(project, text)
}
})
}))
}, BorderLayout.SOUTH)
viewModel?.onInit(project)
imageList.addListSelectionListener { event ->
Expand Down

0 comments on commit b7a8bd9

Please sign in to comment.