[vcs-log] shorten reference name more gradually appcode/171.620 clion/171.622 dbe/171.623 idea/171.619 phpstorm/171.618 pycharm/171.617 pycharm/171.621 rubymine/171.616
authorJulia Beliaeva <Julia.Beliaeva@jetbrains.com>
Fri, 28 Oct 2016 15:03:30 +0000 (18:03 +0300)
committerJulia Beliaeva <Julia.Beliaeva@jetbrains.com>
Mon, 31 Oct 2016 21:31:26 +0000 (00:31 +0300)
platform/vcs-log/impl/src/com/intellij/vcs/log/ui/render/LabelPainter.java

index 965a344932c9d966a09f1edb0035eeb191889d2c..3004786317dc89d9ee3f766ae0d732cce2d0b83d 100644 (file)
@@ -99,10 +99,7 @@ public class LabelPainter implements ReferencePainter {
 
     myGreyBackground = calculateGreyBackground(refGroups, background, isSelected);
     Pair<List<Pair<String, LabelIcon>>, Integer> presentation =
-      calculatePresentation(refGroups, metrics, myHeight, myGreyBackground != null ? myGreyBackground : myBackground, false);
-    if (presentation.second > availableWidth) {
-      presentation = calculatePresentation(refGroups, metrics, myHeight, myGreyBackground != null ? myGreyBackground : myBackground, true);
-    }
+      calculatePresentation(refGroups, metrics, myHeight, myGreyBackground != null ? myGreyBackground : myBackground, availableWidth);
 
     myLabels = presentation.first;
     myWidth = presentation.second;
@@ -113,7 +110,7 @@ public class LabelPainter implements ReferencePainter {
                                                                                     @NotNull FontMetrics fontMetrics,
                                                                                     int height,
                                                                                     @NotNull Color background,
-                                                                                    boolean shorten) {
+                                                                                    int availableWidth) {
     int width = LEFT_PADDING + RIGHT_PADDING;
 
     List<Pair<String, LabelIcon>> labels = ContainerUtil.newArrayList();
@@ -123,18 +120,22 @@ public class LabelPainter implements ReferencePainter {
       if (group.isExpanded()) {
         for (VcsRef ref : group.getRefs()) {
           LabelIcon labelIcon = new LabelIcon(height, background, ref.getType().getBackgroundColor());
-          String text = shorten ? shortenRefName(ref.getName()) : ref.getName();
+          width += labelIcon.getIconWidth() + MIDDLE_PADDING;
+
+          String text = shortenRefName(ref.getName(), fontMetrics, availableWidth - width);
+          width += fontMetrics.stringWidth(text);
 
           labels.add(Pair.create(text, labelIcon));
-          width += labelIcon.getIconWidth() + fontMetrics.stringWidth(text) + MIDDLE_PADDING;
         }
       }
       else {
         LabelIcon labelIcon = new LabelIcon(height, background, getGroupColors(group));
-        String text = shorten ? shortenRefName(group.getName()) : group.getName();
+        width += labelIcon.getIconWidth() + MIDDLE_PADDING;
+
+        String text = shortenRefName(group.getName(), fontMetrics, availableWidth - width);
+        width += fontMetrics.stringWidth(text);
 
         labels.add(Pair.create(text, labelIcon));
-        width += labelIcon.getIconWidth() + fontMetrics.stringWidth(text) + MIDDLE_PADDING;
       }
     }
 
@@ -161,13 +162,23 @@ public class LabelPainter implements ReferencePainter {
   }
 
   @NotNull
-  private static String shortenRefName(@NotNull String refName) {
-    int textLength = refName.length();
-    if (textLength > MAX_LENGTH) {
+  private static String shortenRefName(@NotNull String refName, @NotNull FontMetrics fontMetrics, int availableWidth) {
+    if (fontMetrics.stringWidth(refName) > availableWidth && refName.length() > MAX_LENGTH) {
       int separatorIndex = refName.indexOf(SEPARATOR);
       if (separatorIndex > TWO_DOTS.length()) {
         refName = TWO_DOTS + refName.substring(separatorIndex);
       }
+
+      if (fontMetrics.stringWidth(refName) <= availableWidth) return refName;
+
+      if (availableWidth > 0) {
+        for (int i = refName.length(); i > MAX_LENGTH; i--) {
+          String result = StringUtil.shortenTextWithEllipsis(refName, i, 0, THREE_DOTS);
+          if (fontMetrics.stringWidth(result) <= availableWidth) {
+            return result;
+          }
+        }
+      }
       return StringUtil.shortenTextWithEllipsis(refName, MAX_LENGTH, 0, THREE_DOTS);
     }
     return refName;