Java Code Examples for com.intellij.util.ui.JBUI#ScaleContext

The following examples show how to use com.intellij.util.ui.JBUI#ScaleContext . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageLoader.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Loads an image of available resolution (1x, 2x, ...) and scales to address the provided scale context.
 * Then wraps the image with {@link JBHiDPIScaledImage} if necessary.
 */
@Nullable
public static Image loadFromUrl(@Nonnull URL url, final boolean allowFloatScaling, boolean useCache, Supplier<ImageFilter>[] filters, final JBUI.ScaleContext ctx) {
  // We can't check all 3rd party plugins and convince the authors to add @2x icons.
  // In IDE-managed HiDPI mode with scale > 1.0 we scale images manually.

  return ImageDescList.create(url.toString(), null, UIUtil.isUnderDarcula(), allowFloatScaling, ctx).load(ImageConverterChain.create().
          withFilter(filters).
          with(new ImageConverter() {
            @Override
            public Image convert(Image source, ImageDesc desc) {
              if (source != null && desc.type != SVG) {
                double scale = adjustScaleFactor(allowFloatScaling, ctx.getScale(PIX_SCALE));
                if (desc.scale > 1) scale /= desc.scale; // compensate the image original scale
                source = scaleImage(source, scale);
              }
              return source;
            }
          }).
          withHiDPI(ctx), useCache);
}
 
Example 2
Source File: AWTIconLoaderFacade.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Image toImage(Icon icon, @Nullable JBUI.ScaleContext ctx) {
  if (icon instanceof RetrievableIcon) {
    icon = ((RetrievableIcon)icon).retrieveIcon();
  }
  if (icon instanceof CachedImageIcon) {
    icon = ((CachedImageIcon)icon).getRealIcon(ctx);
  }
  if (icon instanceof ImageIcon) {
    return ((ImageIcon)icon).getImage();
  }
  else {
    BufferedImage image;
    if (GraphicsEnvironment.isHeadless()) { // for testing purpose
      image = UIUtil.createImage(ctx, icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB, PaintUtil.RoundingMode.FLOOR);
    }
    else {
      // [tav] todo: match the screen with the provided ctx
      image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration()
              .createCompatibleImage(icon.getIconWidth(), icon.getIconHeight(), Transparency.TRANSLUCENT);
    }
    Graphics2D g = image.createGraphics();
    try {
      icon.paintIcon(null, g, 0, 0);
    }
    finally {
      g.dispose();
    }
    return image;
  }
}
 
Example 3
Source File: ImageLoader.java    From consulo with Apache License 2.0 5 votes vote down vote up
public ImageConverterChain withHiDPI(final JBUI.ScaleContext ctx) {
  if (ctx == null) return this;
  return with(new ImageConverter() {
    @Override
    public Image convert(Image source, ImageDesc desc) {
      return ImageUtil.ensureHiDPI(source, ctx);
    }
  });
}
 
Example 4
Source File: ImageLoader.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static ImageDescList create(@Nonnull String path, @Nullable Class cls, boolean dark, boolean allowFloatScaling, JBUI.ScaleContext ctx) {
  // Prefer retina images for HiDPI scale, because downscaling
  // retina images provides a better result than up-scaling non-retina images.
  boolean retina = JBUI.isHiDPI(ctx.getScale(PIX_SCALE));

  Builder list = new Builder(FileUtil.getNameWithoutExtension(path), FileUtilRt.getExtension(path), cls, true, adjustScaleFactor(allowFloatScaling, ctx.getScale(PIX_SCALE)));

  if (path.contains("://") && !path.startsWith("file:")) {
    list.add(StringUtil.endsWithIgnoreCase(path, ".svg") ? SVG : IMG);
  }
  else if (retina && dark) {
    list.add(true, true);
    list.add(true, false); // fallback to non-dark
  }
  else if (dark) {
    list.add(false, true);
    list.add(false, false); // fallback to non-dark
  }
  else if (retina) {
    list.add(true, false);
  }
  else {
    list.add(false, false);
  }

  return list.build();
}
 
Example 5
Source File: PaintUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static double getScale(JBUI.ScaleContext ctx) {
  // exclude the user scale, unless it's zero
  double scale = ctx.getScale(JBUI.ScaleType.USR_SCALE) == 0 ? 0 : ctx.getScale(JBUI.ScaleType.PIX_SCALE) / ctx.getScale(JBUI.ScaleType.USR_SCALE);
  if (scale <= 0) {
    //Logger.getInstance(PaintUtil.class).warn("bad scale in the context: " + ctx.toString(), new Throwable());
  }
  return scale;
}
 
Example 6
Source File: AWTIconLoaderFacade.java    From consulo with Apache License 2.0 4 votes vote down vote up
private Couple<Double> key(@Nonnull JBUI.ScaleContext ctx) {
  return new Couple<>(ctx.getScale(USR_SCALE) * ctx.getScale(OBJ_SCALE), ctx.getScale(SYS_SCALE));
}
 
Example 7
Source File: AWTIconLoaderFacade.java    From consulo with Apache License 2.0 4 votes vote down vote up
private Image loadFromUrl(@Nonnull JBUI.ScaleContext ctx) {
  return ImageLoader.loadFromUrl(myUrl, true, useCacheOnLoad, myFilters, ctx);
}
 
Example 8
Source File: ImageLoader.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static Image loadFromResource(@NonNls @Nonnull String path, @Nonnull Class aClass, boolean darculaState) {
  JBUI.ScaleContext ctx = JBUI.ScaleContext.create();
  return ImageDescList.create(path, aClass, darculaState, true, ctx).load(ImageConverterChain.create().withHiDPI(ctx));
}
 
Example 9
Source File: JBHiDPIScaledImage.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * @see #JBHiDPIScaledImage(GraphicsConfiguration, double, double, int)
 */
public JBHiDPIScaledImage(@Nullable JBUI.ScaleContext ctx, double width, double height, int type, @Nonnull RoundingMode rm) {
  this(JBUI.sysScale(ctx), width, height, type, rm);
}
 
Example 10
Source File: PaintUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * @see #devValue(double, Graphics2D)
 */
public static double devValue(double usrValue, @Nonnull JBUI.ScaleContext ctx) {
  return usrValue * getScale(ctx);
}
 
Example 11
Source File: PaintUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * @see #alignToInt(double, ScaleContext, RoundingMode, ParityMode)
 */
public static double alignToInt(double usrValue, @Nonnull JBUI.ScaleContext ctx) {
  return alignToInt(usrValue, ctx, null, null);
}
 
Example 12
Source File: WebIconLoaderFacade.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public Image toImage(Icon icon, @Nullable JBUI.ScaleContext ctx) {
  throw new UnsupportedOperationException();
}
 
Example 13
Source File: AbstractNavBarUI.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static BufferedImage drawToBuffer(NavBarItem item, JBUI.ScaleContext ctx, boolean floating, boolean toolbarVisible, boolean selected, NavBarPanel navbar) {
  int w = item.getWidth();
  int h = item.getHeight();
  int offset = (w - getDecorationOffset());
  int h2 = h / 2;

  BufferedImage result = UIUtil.createImage(ctx, w, h, BufferedImage.TYPE_INT_ARGB, PaintUtil.RoundingMode.FLOOR);

  Color defaultBg = UIUtil.isUnderDarcula() ? Gray._100 : JBColor.WHITE;
  final Paint bg = floating ? defaultBg : null;
  final Color selection = UIUtil.getListSelectionBackground(true);

  Graphics2D g2 = result.createGraphics();
  g2.setStroke(new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


  Path2D.Double shape = new Path2D.Double();
  shape.moveTo(0, 0);

  shape.lineTo(offset, 0);
  shape.lineTo(w, h2);
  shape.lineTo(offset, h);
  shape.lineTo(0, h);
  shape.closePath();

  Path2D.Double endShape = new Path2D.Double();
  endShape.moveTo(offset, 0);
  endShape.lineTo(w, 0);
  endShape.lineTo(w, h);
  endShape.lineTo(offset, h);
  endShape.lineTo(w, h2);
  endShape.closePath();

  if (bg != null && toolbarVisible) {
    g2.setPaint(bg);
    g2.fill(shape);
    g2.fill(endShape);
  }

  if (selected) {
    Path2D.Double focusShape = new Path2D.Double();
    if (toolbarVisible || floating) {
      focusShape.moveTo(offset, 0);
    }
    else {
      focusShape.moveTo(0, 0);
      focusShape.lineTo(offset, 0);
    }
    focusShape.lineTo(w - 1, h2);
    focusShape.lineTo(offset, h - 1);
    if (!toolbarVisible && !floating) {
      focusShape.lineTo(0, h - 1);

    }

    g2.setColor(selection);
    if (floating && item.isLastElement()) {
      g2.fillRect(0, 0, w, h);
    }
    else {
      g2.fill(shape);
    }
  }

  if (item.isNextSelected() && navbar.isFocused()) {
    g2.setColor(selection);
    g2.fill(endShape);
  }

  if (!item.isLastElement()) {
    if (!selected && (!navbar.isFocused() | !item.isNextSelected())) {
      Icon icon = AllIcons.Ide.NavBarSeparator;
      icon.paintIcon(item, g2, w - icon.getIconWidth() - JBUIScale.scale(1), h2 - icon.getIconHeight() / 2);
    }
  }

  g2.dispose();
  return result;
}
 
Example 14
Source File: IconLoader.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static Image toImage(@Nonnull Icon icon, @Nullable JBUI.ScaleContext ctx) {
  return ourIconLoaderFacade.toImage(icon, ctx);
}
 
Example 15
Source File: PaintUtil.java    From consulo with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the value in the user space aligned with the integer value in the device space, applying the rounding and parity modes.
 * If the rounding mode is null - {@code ROUND} is applied. If the parity mode is null - it's ignored, otherwise the value
 * converted to the device space is aligned with the nearest integer odd/even value by the provided rounding mode.
 * For instance, 2.1 would be floor'd to odd 1, ceil'd and round'ed to odd 3. Also, 1.9 would be floor'd to odd 1, ceil'd to
 * odd 3 and round'ed to odd 1.
 *
 * @param usrValue the value to align, given in the user space
 * @param ctx      the scale context, the user scale is ignored
 * @param rm       the rounding mode to apply ({@code ROUND} if null)
 * @param pm       the parity mode to apply (ignored if null)
 * @return the aligned value, in the user space
 */
public static double alignToInt(double usrValue, @Nonnull JBUI.ScaleContext ctx, @Nullable RoundingMode rm, @Nullable ParityMode pm) {
  if (rm == null) rm = ROUND;
  double scale = getScale(ctx);
  if (scale == 0) return 0;

  int devValue = devValue(usrValue, scale, pm != null && rm == ROUND ? FLOOR : rm);
  if (pm != null && ParityMode.of(devValue) != pm) {
    devValue += rm == FLOOR ? -1 : 1;
  }
  return devValue / scale;
}
 
Example 16
Source File: PaintUtil.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Returns parity of the value converted to the device space with the given rounding mode ({@code ROUND} if null).
 *
 * @param usrValue the value, given in the user space
 * @param ctx      the scale context (the user scale is ignored)
 * @param rm       the rounding mode to apply
 * @return the parity of the value in the device space
 */
public static ParityMode getParityMode(double usrValue, @Nonnull JBUI.ScaleContext ctx, @Nullable RoundingMode rm) {
  int devValue = devValue(usrValue, getScale(ctx), rm == null ? ROUND : rm);
  return ParityMode.of(devValue);
}
 
Example 17
Source File: IconLoaderFacade.java    From consulo with Apache License 2.0 votes vote down vote up
Image toImage(Icon icon, @Nullable JBUI.ScaleContext ctx);