Skip to content

Commit 9c0d6d2

Browse files
committed
fix RecentActivityToolWindowIconChanger
1 parent f852a03 commit 9c0d6d2

File tree

2 files changed

+79
-66
lines changed

2 files changed

+79
-66
lines changed

src/main/kotlin/org/digma/intellij/plugin/ui/jcef/pluginapi/RecentActivityBadgeCommand.kt

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@ package org.digma.intellij.plugin.ui.jcef.pluginapi
22

33
import com.fasterxml.jackson.annotation.JsonCreator
44
import com.fasterxml.jackson.annotation.JsonProperty
5-
import com.intellij.execution.runners.ExecutionUtil
65
import com.intellij.openapi.project.Project
7-
import com.intellij.openapi.wm.ToolWindow
8-
import com.intellij.openapi.wm.ToolWindowManager
9-
import org.digma.intellij.plugin.PluginId
10-
import org.digma.intellij.plugin.common.EDT
116
import org.digma.intellij.plugin.common.objectToJson
12-
import org.digma.intellij.plugin.icons.AppIcons
137
import org.digma.intellij.plugin.ui.jcef.jsonToObject
8+
import org.digma.intellij.plugin.ui.recentactivity.RecentActivityToolWindowIconChanger
149
import java.beans.ConstructorProperties
15-
import javax.swing.Icon
1610

1711
class RecentActivityBadgeCommand : Command() {
1812

@@ -43,7 +37,7 @@ class RecentActivityBadgeCommand : Command() {
4337
)
4438
}
4539

46-
val recentActivityToolWindowIconChanger = RecentActivityToolWindowIconChanger(project)
40+
val recentActivityToolWindowIconChanger = RecentActivityToolWindowIconChanger.getInstance(project)
4741
if (badgeRequest.status) {
4842
recentActivityToolWindowIconChanger.showBadge()
4943
} else {
@@ -84,61 +78,3 @@ constructor(
8478
@param:JsonProperty("status")
8579
val status: Boolean
8680
)
87-
88-
89-
private class RecentActivityToolWindowIconChanger(val project: Project) {
90-
91-
private val defaultIcon = AppIcons.TOOL_WINDOW_OBSERVABILITY
92-
private var actualIcon: Icon? = null
93-
private var badgeIcon: Icon? = null
94-
95-
96-
fun showBadge() {
97-
98-
val toolWindow = getToolWindow() ?: return
99-
100-
createBadgeIcon()
101-
102-
EDT.ensureEDT {
103-
badgeIcon?.let {
104-
toolWindow.setIcon(it)
105-
}
106-
}
107-
}
108-
109-
private fun createBadgeIcon() {
110-
111-
if (badgeIcon == null) {
112-
val icon = actualIcon ?: defaultIcon
113-
badgeIcon = ExecutionUtil.getLiveIndicator(icon)
114-
}
115-
}
116-
117-
118-
fun hideBadge() {
119-
120-
//if badgeIcon is null then it was never created and no need to do anything
121-
if (badgeIcon == null) {
122-
return
123-
}
124-
125-
val toolWindow = getToolWindow() ?: return
126-
127-
EDT.ensureEDT {
128-
toolWindow.setIcon(actualIcon ?: defaultIcon)
129-
}
130-
}
131-
132-
private fun getToolWindow(): ToolWindow? {
133-
val toolWindow = ToolWindowManager.getInstance(project).getToolWindow(PluginId.OBSERVABILITY_WINDOW_ID)
134-
135-
if (actualIcon == null) {
136-
//capture the actual icon the first time we got a non-null tool window.
137-
// and make sure it is initialized at least to the default icon
138-
actualIcon = toolWindow?.icon ?: defaultIcon
139-
}
140-
141-
return toolWindow
142-
}
143-
144-
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.digma.intellij.plugin.ui.recentactivity
2+
3+
import com.intellij.execution.runners.ExecutionUtil
4+
import com.intellij.openapi.components.Service
5+
import com.intellij.openapi.components.service
6+
import com.intellij.openapi.project.Project
7+
import com.intellij.openapi.wm.ToolWindow
8+
import com.intellij.openapi.wm.ToolWindowManager
9+
import org.digma.intellij.plugin.PluginId
10+
import org.digma.intellij.plugin.common.EDT
11+
import org.digma.intellij.plugin.icons.AppIcons
12+
import javax.swing.Icon
13+
14+
//must be a singleton because it relies on null and non-null properties
15+
@Service(Service.Level.PROJECT)
16+
class RecentActivityToolWindowIconChanger(val project: Project) {
17+
18+
private val defaultIcon = AppIcons.TOOL_WINDOW_OBSERVABILITY
19+
private var actualIcon: Icon? = null
20+
private var badgeIcon: Icon? = null
21+
22+
23+
companion object{
24+
fun getInstance(project: Project): RecentActivityToolWindowIconChanger {
25+
return project.service<RecentActivityToolWindowIconChanger>()
26+
}
27+
}
28+
29+
fun showBadge() {
30+
31+
val toolWindow = getToolWindow() ?: return
32+
33+
createBadgeIcon()
34+
35+
EDT.ensureEDT {
36+
badgeIcon?.let {
37+
toolWindow.setIcon(it)
38+
}
39+
}
40+
}
41+
42+
private fun createBadgeIcon() {
43+
44+
if (badgeIcon == null) {
45+
val icon = actualIcon ?: defaultIcon
46+
badgeIcon = ExecutionUtil.getLiveIndicator(icon)
47+
}
48+
}
49+
50+
51+
fun hideBadge() {
52+
53+
//if badgeIcon is null then it was never created and no need to do anything
54+
if (badgeIcon == null) {
55+
return
56+
}
57+
58+
val toolWindow = getToolWindow() ?: return
59+
60+
EDT.ensureEDT {
61+
toolWindow.setIcon(actualIcon ?: defaultIcon)
62+
}
63+
}
64+
65+
private fun getToolWindow(): ToolWindow? {
66+
val toolWindow = ToolWindowManager.getInstance(project).getToolWindow(PluginId.OBSERVABILITY_WINDOW_ID)
67+
68+
if (actualIcon == null) {
69+
//capture the actual icon the first time we got a non-null tool window.
70+
// and make sure it is initialized at least to the default icon
71+
actualIcon = toolWindow?.icon ?: defaultIcon
72+
}
73+
74+
return toolWindow
75+
}
76+
77+
}

0 commit comments

Comments
 (0)