summaryrefslogtreecommitdiff
path: root/layout-inspector/src
diff options
context:
space:
mode:
authorJens Ole Lauridsen <jlauridsen@google.com>2022-02-17 15:34:48 -0800
committerJens Ole Lauridsen <jlauridsen@google.com>2022-02-22 20:58:21 +0000
commitec44effafddeb6411eee182afb5896e61e69a3de (patch)
tree4f34d04794689501735ccadfee75574a2d5cc9da /layout-inspector/src
parent964d624fa40def43efc04a2dd8d0893bff79b2b9 (diff)
downloadidea-ec44effafddeb6411eee182afb5896e61e69a3de.tar.gz
Move the table header mouse listener to the header itself
And out of LayoutInspectorTreePanel. This is in preparation for supporting editable headers. Bug: 172894679 Test: Added unit tests Change-Id: I33c0435bcf469c3e7e28a62c5da4a960177d3783
Diffstat (limited to 'layout-inspector/src')
-rw-r--r--layout-inspector/src/com/android/tools/idea/layoutinspector/tree/LayoutInspectorTreePanel.kt84
1 files changed, 20 insertions, 64 deletions
diff --git a/layout-inspector/src/com/android/tools/idea/layoutinspector/tree/LayoutInspectorTreePanel.kt b/layout-inspector/src/com/android/tools/idea/layoutinspector/tree/LayoutInspectorTreePanel.kt
index a8abf37d403..7f03278cd67 100644
--- a/layout-inspector/src/com/android/tools/idea/layoutinspector/tree/LayoutInspectorTreePanel.kt
+++ b/layout-inspector/src/com/android/tools/idea/layoutinspector/tree/LayoutInspectorTreePanel.kt
@@ -51,7 +51,6 @@ import com.intellij.ui.TableActions
import com.intellij.ui.TreeActions
import com.intellij.ui.components.JBLabel
import com.intellij.ui.treeStructure.Tree
-import com.intellij.util.ui.EmptyIcon
import com.intellij.util.ui.JBUI
import com.intellij.util.ui.UIUtil
import icons.StudioIcons
@@ -67,8 +66,8 @@ import javax.swing.Action
import javax.swing.Icon
import javax.swing.JComponent
import javax.swing.JPanel
-import javax.swing.JTable
import javax.swing.JTree
+import javax.swing.SwingUtilities
import javax.swing.table.TableCellRenderer
import kotlin.math.max
@@ -125,9 +124,9 @@ class LayoutInspectorTreePanel(parentDisposable: Disposable) : ToolContent<Layou
.withoutTreeSearch()
.withHeaderRenderer(createTreeHeaderRenderer())
.withColumn(createIntColumn<TreeViewNode>("Counts", { (it.view as? ComposeViewNode)?.recomposeCount }, leftDivider = true,
- headerRenderer = createIconHeader(StudioIcons.Compose.Toolbar.RUN_CONFIGURATION)))
+ headerRenderer = createCountsHeader()))
.withColumn(createIntColumn<TreeViewNode>("Skips", { (it.view as? ComposeViewNode)?.recomposeSkips }, foreground = JBColor.lightGray,
- headerRenderer = createIconHeader(AllIcons.RunConfigurations.ToolbarSkipped)))
+ headerRenderer = createSkipsHeader()))
.withInvokeLaterOption { ApplicationManager.getApplication().invokeLater(it) }
.withHorizontalScrollBar()
.withComponentName("inspectorComponentTree")
@@ -141,7 +140,6 @@ class LayoutInspectorTreePanel(parentDisposable: Disposable) : ToolContent<Layou
componentTreeModel = result.model
componentTreeSelectionModel = result.selectionModel
interactions = result.interactions
- createHeaderMouseListener()
ActionManager.getInstance()?.getAction(IdeActions.ACTION_GOTO_DECLARATION)?.shortcutSet
?.let { GotoDeclarationAction.registerCustomShortcutSet(it, componentTreePanel, parentDisposable) }
componentTreeSelectionModel.addSelectionListener {
@@ -180,6 +178,15 @@ class LayoutInspectorTreePanel(parentDisposable: Disposable) : ToolContent<Layou
text.border = JBUI.Borders.empty(ICON_BORDER, TEXT_HORIZONTAL_BORDER)
text.font = UIUtil.getLabelFont(UIUtil.FontSize.SMALL)
reset.border = JBUI.Borders.empty(ICON_BORDER)
+ reset.toolTipText = "Click to reset recomposition counts"
+ reset.cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)
+ reset.addMouseListener(object : MouseAdapter() {
+ override fun mouseClicked(event: MouseEvent) {
+ if (SwingUtilities.isLeftMouseButton(event)) {
+ resetRecompositionCounts()
+ }
+ }
+ })
panel.background = UIUtil.TRANSPARENT_COLOR
panel.isOpaque = false
panel.add(text, BorderLayout.CENTER)
@@ -187,70 +194,19 @@ class LayoutInspectorTreePanel(parentDisposable: Disposable) : ToolContent<Layou
return TableCellRenderer { _, _, _, _, _, _ -> panel }
}
- private fun createIconHeader(icon: Icon) : TableCellRenderer {
+ private fun createCountsHeader() =
+ createIconHeader(StudioIcons.Compose.Toolbar.RUN_CONFIGURATION, "Number of times this composable has been recomposed")
+
+ private fun createSkipsHeader() =
+ createIconHeader(AllIcons.RunConfigurations.ToolbarSkipped, "Number of times recomposition for this component has been skipped")
+
+ private fun createIconHeader(icon: Icon, toolTipText: String? = null) : TableCellRenderer {
val label = JBLabel(icon)
label.border = JBUI.Borders.empty(ICON_BORDER, ICON_HORIZONTAL_PADDING)
+ label.toolTipText = toolTipText
return TableCellRenderer { _, _, _, _, _, _ -> label }
}
- private enum class HeaderLocation { NONE, TREE_HEADER, RESET_BUTTON, COUNTS, SKIPS}
-
- private fun createHeaderMouseListener() {
- val table = focusComponent as? JTable ?: return
- val tableHeader = table.tableHeader
- val handCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)
-
- val listener = object : MouseAdapter() {
- private var lastLocation = HeaderLocation.NONE
-
- override fun mouseExited(event: MouseEvent) {
- update(HeaderLocation.NONE)
- }
-
- override fun mouseEntered(event: MouseEvent) {
- update(event.location)
- }
-
- override fun mouseMoved(event: MouseEvent) {
- update(event.location)
- }
-
- override fun mouseClicked(event: MouseEvent) {
- if (event.location == HeaderLocation.RESET_BUTTON) {
- resetRecompositionCounts()
- }
- }
-
- private fun update(location: HeaderLocation) {
- if (location == lastLocation) {
- return
- }
- lastLocation = location
- tableHeader.cursor = if (location == HeaderLocation.RESET_BUTTON) handCursor else Cursor.getDefaultCursor()
- tableHeader.toolTipText = when (location) {
- HeaderLocation.RESET_BUTTON -> "Click to reset recomposition counts"
- HeaderLocation.COUNTS -> "The number of times this composable has been recomposed"
- HeaderLocation.SKIPS -> "The number of times recomposition for this component has been skipped"
- else -> null
- }
- }
-
- private val MouseEvent.location: HeaderLocation
- get() {
- val column = table.columnAtPoint(point)
- return when {
- column == 1 -> HeaderLocation.COUNTS
- column == 2 -> HeaderLocation.SKIPS
- column == 0 && x > table.columnModel.getColumn(0).width - EmptyIcon.ICON_16.iconWidth - 2 * ICON_BORDER
- -> HeaderLocation.RESET_BUTTON
- else -> HeaderLocation.TREE_HEADER
- }
- }
- }
- tableHeader.addMouseListener(listener)
- tableHeader.addMouseMotionListener(listener)
- }
-
private fun installKeyboardActions(focusedComponent: JComponent) {
val (down, up) =
if (focusedComponent is JTree) Pair(TreeActions.Down.ID, TreeActions.Up.ID) else Pair(TableActions.Down.ID, TableActions.Up.ID)