Skip to content

Commit

Permalink
Disable the execution action button when gradle is running
Browse files Browse the repository at this point in the history
  • Loading branch information
eyedol committed Jul 13, 2024
1 parent a353eb6 commit d272d8e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.yield
Expand All @@ -32,7 +33,7 @@ class PreviewViewModel {
val imagesStateFlow = MutableStateFlow<List<Pair<String, Long>>>(listOf())
private val lastEditingFileName = MutableStateFlow<String?>(null)
val statusText = MutableStateFlow("No images found")
private val _dropDownUiState = MutableStateFlow(emptyList<String>())
private val _dropDownUiState = MutableStateFlow(ActionToolbarUiState())
val dropDownUiState = _dropDownUiState.asStateFlow()
val shouldSeeIndex = MutableStateFlow(-1)
private var updateListJob: Job? = null
Expand Down Expand Up @@ -70,7 +71,14 @@ class PreviewViewModel {

fun executeTaskByName(project: Project, taskName: String) {
roborazziLog("Executing task '$taskName'...")
gradleTask.executeTaskByName(project, taskName)
_dropDownUiState.update { currentUiState ->
currentUiState.copy(flag = ActionToolbarUiState.Flag.LOADING)
}
gradleTask.executeTaskByName(project, taskName) {
_dropDownUiState.update { currentUiState ->
currentUiState.copy(flag = ActionToolbarUiState.Flag.IDLE)
}
}
}

private fun selectListIndexByCaret(project: Project) {
Expand Down Expand Up @@ -130,7 +138,9 @@ class PreviewViewModel {

private fun fetchTasks(project: Project) {
roborazziLog("fetchTasks...")
_dropDownUiState.value = gradleTask.fetchTasks(project)
_dropDownUiState.update { currentUiState ->
currentUiState.copy( tasks = gradleTask.fetchTasks(project))
}
}

private suspend fun refreshListProcess(project: Project) {
Expand Down Expand Up @@ -252,4 +262,14 @@ class PreviewViewModel {
fun onShouldSeeIndexHandled() {
shouldSeeIndex.value = -1
}

data class ActionToolbarUiState(
val tasks: List<String> = emptyList(),
val flag: Flag = Flag.IDLE
) {
enum class Flag {
LOADING,
IDLE
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ class RoborazziGradleTask {
.map { it.data.name }.sorted()
}

fun executeTaskByName(project: Project, taskName: String) {
fun executeTaskByName(
project: Project,
taskName: String,
onTaskExecuted:() -> Unit
) {
val settings = ExternalSystemTaskExecutionSettings().apply {
externalProjectPath = project.basePath!!
taskNames = listOf(taskName)
Expand All @@ -43,10 +47,12 @@ class RoborazziGradleTask {
object : TaskCallback {
override fun onSuccess() {
roborazziLog("Task '$taskName' executed successfully")
onTaskExecuted()
}

override fun onFailure() {
roborazziLog("Task '$taskName' execution failed")
onTaskExecuted()
}
},
ProgressExecutionMode.IN_BACKGROUND_ASYNC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ class RoborazziPreviewPanel(project: Project) : JPanel(BorderLayout()) {

viewModel?.coroutineScope?.launch {
viewModel?.dropDownUiState?.collect {
statusGradleTaskPanel.isExecuteGradleTaskActionEnabled = it.flag == PreviewViewModel.ActionToolbarUiState.Flag.IDLE
statusGradleTaskPanel.setActions(
it.map { taskName -> StatusToolbarPanel.ToolbarAction(taskName, taskName)}
it.tasks.map { taskName -> StatusToolbarPanel.ToolbarAction(taskName, taskName)}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class StatusToolbarPanel(
onActionClicked: (taskName: String) -> Unit
) : JBPanel<Nothing>(BorderLayout()){
private val _statusLabel = JBLabel()
private val actionToolBar = RoborazziGradleTaskToolbar(
private val actionToolbar = RoborazziGradleTaskToolbar(
project = project,
place = "RoborazziGradleTaskToolbar",
horizontal = true,
Expand All @@ -40,20 +40,26 @@ class StatusToolbarPanel(
field = value
}

var isExecuteGradleTaskActionEnabled: Boolean
get() = actionToolbar.isExecuteGradleTaskActionEnabled
set(value) {
actionToolbar.isExecuteGradleTaskActionEnabled = value
}

init {
actionToolBar.setTargetComponent(this)
actionToolbar.setTargetComponent(this)
add(_statusLabel, BorderLayout.WEST)
add(actionToolBar.component, BorderLayout.EAST)
add(actionToolbar.component, BorderLayout.EAST)
}

fun setActions(actions: List<ToolbarAction>) {
if (actions.isEmpty()) {
actionToolBar.isVisible = false
actionToolbar.isVisible = false
return
}
actionToolBar.isVisible = true
actionToolbar.isVisible = true
val actionList = listOf(*actions.toTypedArray())
actionToolBar.setActions(actionList)
actionToolbar.setActions(actionList)
}

data class ToolbarAction(val label: String, val id: String)
Expand All @@ -69,12 +75,20 @@ class RoborazziGradleTaskToolbar(

private val propertiesComponent = PropertiesComponent.getInstance(project)
private val roborazziGradleTaskAction = GradleTaskComboBoxAction(propertiesComponent)
private val executeGradleTaskAction = ExecuteGradleTaskAction(propertiesComponent, onActionClicked)

var isExecuteGradleTaskActionEnabled: Boolean
get() = isEnabled
set(value) {
executeGradleTaskAction.isActionEnabled = value
isEnabled = value
}

init {
actionGroup.addAll(
listOf(
roborazziGradleTaskAction,
ExecuteGradleTaskAction(propertiesComponent, onActionClicked)
executeGradleTaskAction
)
)
}
Expand Down Expand Up @@ -122,9 +136,22 @@ class RoborazziGradleTaskToolbar(
private val onActionClicked: (taskName: String) -> Unit
): DumbAwareAction("Execute Selected Task", "Execute selected task", AllIcons.Actions.Refresh) {

var isActionEnabled = true
set(value) {
field = value
updateAllToolbarsImmediately()
}

override fun actionPerformed(e: AnActionEvent) {
propertiesComponent.getValue(SELECTED_TASK_KEY)?.let { onActionClicked(it) }
}

override fun update(e: AnActionEvent) {
super.update(e)
e.presentation.isEnabled = isActionEnabled
}

override fun getActionUpdateThread() = ActionUpdateThread.EDT
}

class GradleTaskAction(
Expand Down

0 comments on commit d272d8e

Please sign in to comment.