Skip to content

Commit 7f795cb

Browse files
authored
fix envs panel initial size (#147)
Signed-off-by: shalom <[email protected]> Signed-off-by: shalom <[email protected]>
1 parent c8c61dd commit 7f795cb

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/ui/ToolWindowShower.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.intellij.openapi.wm.ToolWindow;
66
import com.intellij.openapi.wm.ToolWindowManager;
77
import org.digma.intellij.plugin.log.Log;
8+
import org.digma.intellij.plugin.ui.service.ToolWindowTabsHelper;
89

910
public class ToolWindowShower {
1011

@@ -15,8 +16,11 @@ public class ToolWindowShower {
1516

1617
private ToolWindow toolWindow;
1718

19+
private final ToolWindowTabsHelper toolWindowTabsHelper;
20+
1821
public ToolWindowShower(Project project) {
1922
this.project = project;
23+
toolWindowTabsHelper = project.getService(ToolWindowTabsHelper.class);
2024
}
2125

2226
public void setToolWindow(ToolWindow toolWindow) {
@@ -49,5 +53,6 @@ private void show(ToolWindow toolWindow) {
4953
Log.log(LOGGER::debug, "Calling toolWindow.show");
5054
toolWindow.show();
5155
}
56+
toolWindowTabsHelper.showInsightsTab();
5257
}
5358
}

ide-common/src/main/kotlin/org/digma/intellij/plugin/ui/service/ToolWindowTabsHelper.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ class ToolWindowTabsHelper(val project: Project) {
5454
errorsContent.displayName = "Errors"
5555
}
5656

57-
fun isErrorDetailsOn():Boolean{
57+
fun isErrorDetailsOn(): Boolean {
5858
return errorDetailsOn
5959
}
6060

61-
61+
fun showInsightsTab() {
62+
toolWindow.contentManager.setSelectedContent(insightsContent)
63+
}
6264

6365

6466
}

src/main/java/org/digma/intellij/plugin/toolwindow/DigmaToolWindowFactory.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
6666
insightsViewService.setContent(toolWindow,insightsContent);
6767
toolWindowTabsHelper.setInsightsContent(insightsContent);
6868
contentToSelect = insightsContent;
69-
insightsPanel.reset();
7069
}
7170

7271
{
@@ -80,7 +79,6 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
8079
toolWindow.getContentManager().addContent(errorsContent);
8180
errorsViewService.setContent(toolWindow,errorsContent);
8281
toolWindowTabsHelper.setErrorsContent(errorsContent);
83-
errorsPanel.reset();
8482
}
8583

8684
{
@@ -93,7 +91,6 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
9391
summaryContent.setPreferredFocusableComponent(summaryPanel.getPreferredFocusableComponent());
9492
toolWindow.getContentManager().addContent(summaryContent);
9593
summaryViewService.setContent(toolWindow, summaryContent);
96-
summaryPanel.reset();
9794
}
9895

9996
ErrorsActionsService errorsActionsService = project.getService(ErrorsActionsService.class);
@@ -117,10 +114,6 @@ public void run(@NotNull ProgressIndicator indicator) {
117114
}.queue();
118115

119116

120-
//sometimes the environment panel doesn't show on startup until some ui action occurs, can't say why.
121-
//calling repaint here forces it to repaint
122-
toolWindow.getComponent().repaint(toolWindow.getComponent().getVisibleRect());
123-
124117

125118
//todo: sometimes there is a race condition on startup, a contextChange is fired before method info is available.
126119
//calling environmentChanged will fix it

src/main/kotlin/org/digma/intellij/plugin/ui/common/EnvironmentsPanel.kt

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ import org.digma.intellij.plugin.ui.common.Laf.Icons.Environment.Companion.ENVIR
1616
import org.digma.intellij.plugin.ui.model.PanelModel
1717
import org.digma.intellij.plugin.ui.model.environment.EnvironmentsSupplier
1818
import org.digma.intellij.plugin.ui.panels.DigmaResettablePanel
19+
import org.digma.intellij.plugin.ui.panels.DigmaTabPanel
1920
import java.awt.Dimension
2021
import java.awt.FlowLayout
22+
import java.lang.Integer.max
2123
import java.util.*
2224
import java.util.function.Function
2325
import javax.swing.Icon
2426
import javax.swing.JComponent
2527
import javax.swing.SwingUtilities
2628

27-
//need to remember we have two instances of this panel , one for the insights tab and one for the errors tab.
28-
//both instances need to be in sync with the selected button and the environments list.
29+
//need to remember we have few instances of this panel , one on every main tab.
30+
//all instances need to be in sync with the selected button and the environments list.
2931
class EnvironmentsPanel(
3032
project: Project,
3133
private val model: PanelModel,
@@ -70,30 +72,49 @@ class EnvironmentsPanel(
7072

7173
/*
7274
usually this panel works fine.
73-
there is one issue: sometimes when the plugin window opens on startup this panel takes too much
74-
vertical space and the insights list is almost not shown. any hover over this panel with the mouse or any
75-
other action related to the plugin will fix it and the tab will re-layout. from there on the panel functions ok.
76-
It seems that super.getPreferredSize() sometimes returns a size with negative width, when that happens the flow
77-
layout computes a much too large vertical space and when the panel is first visible it's too large.
78-
I could not find any way to cause this panel to layout correctly on startup.
79-
this code in getPreferredSize will keep track on the last positive size and return that one if super returns
80-
a negative width. it seems to do the job.
81-
*/
82-
private var lastPositivePs: Dimension = Dimension(100, 300)
75+
there is one issue: sometimes when the plugin window opens on startup the WrapLayout computes a large
76+
vertical height and the panel takes too much vertical space.
77+
Its noticed that when the computed width is a negative number it means the calculation is not good. in that
78+
case this method will calculate a preferred size based on the buttons size. its usually only on first
79+
initialization of the tool window.
80+
*/
8381
override fun getPreferredSize(): Dimension {
8482
val ps = super.getPreferredSize()
8583
if (ps != null) {
86-
return if (ps.width >= 0 && ps.height >= 0) {
87-
this.lastPositivePs = ps
84+
return if (ps.width > 0 && ps.height > 0) {
8885
ps
8986
} else {
90-
this.lastPositivePs
87+
computePreferredSize()
9188
}
9289
}
9390

9491
return super.getPreferredSize()
9592
}
9693

94+
private fun computePreferredSize(): Dimension {
95+
96+
var tabPanel = parent
97+
while ((tabPanel != null) && (tabPanel !is DigmaTabPanel)) {
98+
tabPanel = tabPanel.parent
99+
}
100+
101+
if ((tabPanel != null) && (tabPanel.size.width > 0) && components.isNotEmpty()) {
102+
val width = tabPanel.size.width - (this.insets.left + this.insets.right)
103+
var componentsWidth = 0
104+
var componentsHeight = 0
105+
components.forEach {
106+
componentsWidth += it.preferredSize.width + (layout as WrapLayout).hgap
107+
componentsHeight = max(componentsHeight, it.preferredSize.height)
108+
}
109+
110+
val lines = (componentsWidth / width) + 1
111+
val height = lines * (componentsHeight + ((layout as WrapLayout).vgap) * 2)
112+
return Dimension(width, height)
113+
}
114+
115+
return super.getPreferredSize()
116+
}
117+
97118

98119
private fun select(newSelectedEnv: String?) {
99120
val currentSelected: EnvLink? = getSelected()
@@ -122,7 +143,6 @@ class EnvironmentsPanel(
122143
this.components.forEach {
123144
this.remove(it)
124145
}
125-
revalidate()
126146
}
127147

128148
val usageStatusResult = model.getUsageStatus()

0 commit comments

Comments
 (0)