From 9cc579cd4d8f0cfeb20752f9042ec36fc50bb829 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 12 Apr 2010 12:54:52 +0400 Subject: [PATCH] ensure that non-customized lookup items have identical icon height (16) so that the lookup height remains constant while typing --- .../lookup/DefaultLookupItemRenderer.java | 12 ++++++- .../lookup/impl/LookupCellRenderer.java | 16 +++++----- .../src/com/intellij/ui/TruncatingIcon.java | 31 +++++++++++++++++++ 3 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 platform/platform-api/src/com/intellij/ui/TruncatingIcon.java diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/DefaultLookupItemRenderer.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/DefaultLookupItemRenderer.java index 279e25aad808..ccd62152fca3 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/DefaultLookupItemRenderer.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/DefaultLookupItemRenderer.java @@ -19,6 +19,7 @@ import com.intellij.openapi.util.Iconable; import com.intellij.psi.PsiElement; import com.intellij.psi.meta.PsiMetaData; import com.intellij.psi.util.PsiUtilBase; +import com.intellij.ui.TruncatingIcon; import com.intellij.util.Icons; import com.intellij.util.ui.EmptyIcon; import org.jetbrains.annotations.Nullable; @@ -44,6 +45,15 @@ public class DefaultLookupItemRenderer extends LookupElementRenderer @Nullable public static Icon getRawIcon(final LookupItem item, boolean real) { + final Icon icon = _getRawIcon(item, real); + if (icon != null && icon.getIconHeight() > SAMPLE_ICON.getIconHeight()) { + return new TruncatingIcon(icon, icon.getIconWidth(), SAMPLE_ICON.getIconHeight()); + } + return icon; + } + + @Nullable + private static Icon _getRawIcon(LookupItem item, boolean real) { Icon icon = (Icon)item.getAttribute(LookupItem.ICON_ATTR); if (icon != null) return icon; @@ -73,7 +83,7 @@ public class DefaultLookupItemRenderer extends LookupElementRenderer } return null; } - + @Nullable private static String getText3(final LookupItem item) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupCellRenderer.java b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupCellRenderer.java index 155117680969..0eaffc26094f 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupCellRenderer.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/lookup/impl/LookupCellRenderer.java @@ -25,7 +25,7 @@ import com.intellij.openapi.editor.colors.EditorColorsManager; import com.intellij.openapi.editor.colors.EditorColorsScheme; import com.intellij.openapi.editor.colors.EditorFontType; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.ui.RowIcon; +import com.intellij.ui.LayeredIcon; import com.intellij.ui.SimpleColoredComponent; import com.intellij.ui.SimpleTextAttributes; import com.intellij.ui.popup.PopupIcons; @@ -280,11 +280,11 @@ public class LookupCellRenderer implements ListCellRenderer { return myEmptyIcon; } - if (icon.getIconWidth() < myEmptyIcon.getIconWidth()) { - final RowIcon rowIcon = new RowIcon(2); - rowIcon.setIcon(icon, 0); - rowIcon.setIcon(new EmptyIcon(myEmptyIcon.getIconWidth() - icon.getIconWidth()), 1); - return rowIcon; + if (icon.getIconHeight() < myEmptyIcon.getIconHeight() || icon.getIconWidth() < myEmptyIcon.getIconWidth()) { + final LayeredIcon layeredIcon = new LayeredIcon(2); + layeredIcon.setIcon(icon, 0, 0, (myEmptyIcon.getIconHeight() - icon.getIconHeight()) / 2); + layeredIcon.setIcon(myEmptyIcon, 1); + return layeredIcon; } return icon; @@ -294,8 +294,8 @@ public class LookupCellRenderer implements ListCellRenderer { final LookupElementPresentation p = new LookupElementPresentation(); item.renderElement(p); final Icon icon = p.getIcon(); - if (icon != null && icon.getIconWidth() > myEmptyIcon.getIconWidth()) { - myEmptyIcon = new EmptyIcon(icon.getIconWidth(), 2); + if (icon != null && (icon.getIconWidth() > myEmptyIcon.getIconWidth() || icon.getIconHeight() > myEmptyIcon.getIconHeight())) { + myEmptyIcon = new EmptyIcon(Math.max(icon.getIconWidth(), myEmptyIcon.getIconWidth()), Math.max(icon.getIconHeight(), myEmptyIcon.getIconHeight())); } int maxWidth = Math.min(RealLookupElementPresentation.calculateWidth(p, myNormalMetrics, myBoldMetrics), myMaxWidth); diff --git a/platform/platform-api/src/com/intellij/ui/TruncatingIcon.java b/platform/platform-api/src/com/intellij/ui/TruncatingIcon.java new file mode 100644 index 000000000000..f131ee82b51e --- /dev/null +++ b/platform/platform-api/src/com/intellij/ui/TruncatingIcon.java @@ -0,0 +1,31 @@ +package com.intellij.ui; + +import javax.swing.*; +import java.awt.*; + +/** + * @author peter + */ +public class TruncatingIcon implements Icon { + private final int myWidth; + private final int myHeight; + private final Icon myDelegate; + + public TruncatingIcon(Icon delegate, int width, int height) { + myDelegate = delegate; + myWidth = width; + myHeight = height; + } + + public void paintIcon(Component c, Graphics g, int x, int y) { + myDelegate.paintIcon(c, g, x, y); + } + + public int getIconWidth() { + return myWidth; + } + + public int getIconHeight() { + return myHeight; + } +}