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

Tweak idea plugin UI #443

Merged
merged 2 commits into from
Jul 27, 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
2 changes: 1 addition & 1 deletion roborazzi-idea-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "io.github.takahirom.roborazzi"
version = "1.4.0"
version = "1.5.0"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,20 @@ import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.content.ContentFactory
import com.intellij.util.containers.SLRUMap
import com.intellij.util.messages.MessageBusConnection
import com.intellij.util.ui.JBUI
import kotlinx.coroutines.launch
import org.jetbrains.kotlin.codegen.inline.getOrPut
import org.jetbrains.plugins.gradle.service.execution.GradleRunConfiguration
import java.awt.BorderLayout
import java.awt.Component
import java.awt.GridBagConstraints
import java.awt.Image
import java.awt.event.ComponentAdapter
import java.awt.event.ComponentEvent
import java.awt.event.ComponentListener
import java.io.File
import javax.imageio.ImageIO
import javax.swing.BorderFactory
import javax.swing.Box
import javax.swing.DefaultListModel
import javax.swing.ImageIcon
Expand Down Expand Up @@ -69,11 +72,20 @@ class RoborazziPreviewToolWindowFactory : ToolWindowFactory {

class RoborazziPreviewPanel(project: Project) : JPanel(BorderLayout()) {
private val listModel = DefaultListModel<Pair<String, Long>>()
private val statusGradleTaskPanel = StatusToolbarPanel(project) { taskName ->
private val statusGradleTaskPanel = TaskToolbarPanel(project) { taskName ->
viewModel?.onRefreshButtonClicked(project, taskName)
}
private val statusBar = JBBox.createHorizontalBox().apply {
statusGradleTaskPanel.statusLabel = "No images found"

private val _statusLabel = JBLabel().apply {
text = "No images found"
}
var statusLabel: String
get() = _statusLabel.text
set(value) {
_statusLabel.text = value
}

private val topBar = JBBox.createHorizontalBox().apply {
add(statusGradleTaskPanel)
}

Expand Down Expand Up @@ -121,24 +133,34 @@ class RoborazziPreviewPanel(project: Project) : JPanel(BorderLayout()) {
}
}
)
project.messageBus.connect().subscribe(ExecutionManager.EXECUTION_TOPIC, object : ExecutionListener {
override fun processTerminated(
executorId: String,
env: ExecutionEnvironment,
handler: ProcessHandler,
exitCode: Int
) {
super.processTerminated(executorId, env, handler, exitCode)
if (env.runProfile is GradleRunConfiguration) {
viewModel?.onTaskExecuted(project)
project.messageBus.connect()
.subscribe(ExecutionManager.EXECUTION_TOPIC, object : ExecutionListener {
override fun processTerminated(
executorId: String,
env: ExecutionEnvironment,
handler: ProcessHandler,
exitCode: Int
) {
super.processTerminated(executorId, env, handler, exitCode)
if (env.runProfile is GradleRunConfiguration) {
viewModel?.onTaskExecuted(project)
}
}
}
})
})

val scrollPane = JBScrollPane(imageList)
scrollPane.verticalScrollBarPolicy = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
add(statusBar, BorderLayout.NORTH)
add(topBar, BorderLayout.NORTH)
add(scrollPane, BorderLayout.CENTER)
add(JBBox.createHorizontalBox().apply {
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4))
add(_statusLabel, GridBagConstraints().apply {
gridx = 0
gridy = 0
anchor = GridBagConstraints.WEST
insets = JBUI.insets(4)
})
}, BorderLayout.SOUTH)
viewModel?.onInit(project)
imageList.addListSelectionListener { event ->
if (!event.valueIsAdjusting) {
Expand Down Expand Up @@ -183,15 +205,16 @@ class RoborazziPreviewPanel(project: Project) : JPanel(BorderLayout()) {
}
viewModel?.coroutineScope?.launch {
viewModel?.statusText?.collect {
statusGradleTaskPanel.statusLabel = it
statusLabel = it
}
}

viewModel?.coroutineScope?.launch {
viewModel?.dropDownUiState?.collect {
statusGradleTaskPanel.isExecuteGradleTaskActionEnabled = it.flag == PreviewViewModel.ActionToolbarUiState.Flag.IDLE
statusGradleTaskPanel.isExecuteGradleTaskActionEnabled =
it.flag == PreviewViewModel.ActionToolbarUiState.Flag.IDLE
statusGradleTaskPanel.setActions(
it.tasks.map { taskName -> StatusToolbarPanel.ToolbarAction(taskName, taskName)}
it.tasks.map { taskName -> TaskToolbarPanel.ToolbarAction(taskName, taskName) }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,26 @@ import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBBox
import com.intellij.ui.components.JBPanel
import com.intellij.util.ui.JBUI
import java.awt.Dimension
import java.awt.GridBagConstraints
import java.awt.GridBagLayout
import javax.swing.BorderFactory
import javax.swing.Box
import javax.swing.JComponent

class StatusToolbarPanel(
class TaskToolbarPanel(
project: Project,
onTaskExecuteButtonClicked: (taskName: String) -> Unit
) : JBPanel<JBPanel<*>>(GridBagLayout()){
private val _statusLabel = JBLabel()
) : JBPanel<JBPanel<*>>(GridBagLayout()) {
private val actionToolbar = RoborazziGradleTaskToolbar(
project = project,
place = "RoborazziGradleTaskToolbar",
horizontal = true,
onTaskExecuteButtonClicked = onTaskExecuteButtonClicked
)

var statusLabel: String = ""
get() = _statusLabel.text
set(value) {
_statusLabel.text = value
field = value
}

var isExecuteGradleTaskActionEnabled: Boolean
get() = actionToolbar.isExecuteGradleTaskActionEnabled
set(value) {
Expand All @@ -52,31 +43,20 @@ class StatusToolbarPanel(

init {
actionToolbar.setTargetComponent(this)
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5))
val gbc = GridBagConstraints().apply {
gridx = 0
gridy = 0
anchor = GridBagConstraints.WEST
insets = JBUI.insets(5)
setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4))
val actionToolbarConstraints = GridBagConstraints().apply {
insets = JBUI.insets(4)
gridx = 0
gridy = 0
anchor = GridBagConstraints.WEST
}
add(_statusLabel, gbc)
val hRGlueConstraints = gbc.apply {
add(actionToolbar.component, actionToolbarConstraints)
add(JBBox.createHorizontalGlue(), GridBagConstraints().apply {
Comment on lines +47 to +54
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe now that we have the status label moved to the bottom we could just use a BorderLayout so we don't have to set all these constraints and the horizontal glue.

Minor: Also I foresee the border still showing when the actionToolbar visibility is toggled to false. We could toggle the JBox instead that's if we wrap the actionToolbar into the JBBox's horizontal box. Something along the lines:

...
 private val jbBox = JBBox.createHorizontalBox()

  init {
    actionToolbar.setTargetComponent(this)
    add(jbBox.apply {
      isVisible = false
      setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4))
      add(actionToolbar.component, GridBagConstraints().apply {
        gridx = 0
        gridy = 0
        anchor = GridBagConstraints.WEST
        insets = JBUI.insets(4)
      })
    }, BorderLayout.WEST)
  }

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your advice. I released it a few days ago. I want to include these changes, but I'm a little busy. I'll do this the next time I modify the plugin, but if you could do it, it would be highly appreciated.

Copy link
Contributor

@eyedol eyedol Jul 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I'll look into it. And no problem

gridx = 1
gridy = 0
weightx = 1.0
anchor = GridBagConstraints.CENTER
fill = GridBagConstraints.HORIZONTAL
}
// Add a horizontal glue to push the label to the left
add(Box.createHorizontalGlue(), hRGlueConstraints)

val actionToolbarConstraints = gbc.apply {
gridx = 1
gridy = 0
fill = GridBagConstraints.NONE
anchor = GridBagConstraints.EAST
}
add(actionToolbar.component, actionToolbarConstraints)
})
}

fun setActions(actions: List<ToolbarAction>) {
Expand All @@ -102,7 +82,8 @@ class RoborazziGradleTaskToolbar(

private val propertiesComponent = PropertiesComponent.getInstance(project)
private val roborazziGradleTaskAction = GradleTaskComboBoxAction(propertiesComponent)
private val executeGradleTaskExecuteAction = ExecuteGradleTaskExecuteAction(propertiesComponent, onTaskExecuteButtonClicked)
private val executeGradleTaskExecuteAction =
ExecuteGradleTaskExecuteAction(propertiesComponent, onTaskExecuteButtonClicked)

var isExecuteGradleTaskActionEnabled: Boolean
get() = isEnabled
Expand Down Expand Up @@ -132,7 +113,7 @@ class RoborazziGradleTaskToolbar(
return toolbarButton
}

fun setActions(actions: List<StatusToolbarPanel.ToolbarAction>) {
fun setActions(actions: List<TaskToolbarPanel.ToolbarAction>) {
roborazziGradleTaskAction.setActions(actions)
}

Expand All @@ -142,33 +123,41 @@ class RoborazziGradleTaskToolbar(

private val popupActionGroup: DefaultActionGroup = DefaultActionGroup()

override fun createPopupActionGroup(button: JComponent, dataContext: DataContext) = popupActionGroup
override fun createPopupActionGroup(button: JComponent, dataContext: DataContext) =
popupActionGroup

override fun update(e: AnActionEvent) {

e.presentation.text = propertiesComponent.getValue(SELECTED_TASK_KEY) ?: if(popupActionGroup.childActionsOrStubs.isNotEmpty()) {
val defaultTask = popupActionGroup.childActionsOrStubs[0].templatePresentation.text
propertiesComponent.setSelectedTask(defaultTask)
defaultTask
} else {
"Select Task"
}
e.presentation.text = propertiesComponent.getValue(SELECTED_TASK_KEY)
?: if (popupActionGroup.childActionsOrStubs.isNotEmpty()) {
val defaultTask = popupActionGroup.childActionsOrStubs[0].templatePresentation.text
propertiesComponent.setSelectedTask(defaultTask)
defaultTask
} else {
"Select Task"
}
e.presentation.icon = AllIcons.General.Gear
}

override fun getActionUpdateThread() = ActionUpdateThread.BGT

fun setActions(actions: List<StatusToolbarPanel.ToolbarAction>) {
fun setActions(actions: List<TaskToolbarPanel.ToolbarAction>) {
popupActionGroup.removeAll()
actions.forEach { popupActionGroup.add(GradleTaskSelectAction(it.label, propertiesComponent)) }
actions.forEach {
popupActionGroup.add(
GradleTaskSelectAction(
it.label,
propertiesComponent
)
)
}
}

}

class ExecuteGradleTaskExecuteAction(
private val propertiesComponent: PropertiesComponent,
private val onActionClicked: (taskName: String) -> Unit
): DumbAwareAction("Execute Selected Task", "Execute selected task", AllIcons.Actions.Refresh) {
) : DumbAwareAction("Execute Selected Task", "Execute selected task", AllIcons.Actions.Refresh) {

var isActionEnabled = true
set(value) {
Expand All @@ -191,7 +180,7 @@ class RoborazziGradleTaskToolbar(
class GradleTaskSelectAction(
private val taskName: String,
private val propertiesComponent: PropertiesComponent,
): DumbAwareAction(taskName, taskName, AllIcons.General.Gear) {
) : DumbAwareAction(taskName, taskName, AllIcons.General.Gear) {

override fun actionPerformed(e: AnActionEvent) {
e.presentation.text = taskName
Expand Down
Loading