Java Code Examples for com.intellij.util.ui.JBUI#sysScale()

The following examples show how to use com.intellij.util.ui.JBUI#sysScale() . 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: DesktopCanvasImageImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
  float current = JBUI.sysScale((Graphics2D)g);
  float old = myScaleFactor;
  if(current != old) {
    myScaleFactor = current;
    myImage = null;
  }

  if (myImage == null) {
    BufferedImage image = UIUtil.createImage(g, myWidth, myHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics graphics = image.createGraphics();
    GraphicsUtil.setupAAPainting(graphics);

    DesktopCanvas2DImpl canvas2D = new DesktopCanvas2DImpl((Graphics2D)graphics);
    myCanvas2DConsumer.accept(canvas2D);

    myImage = image;
  }

  UIUtil.drawImage(g, myImage, x, y, c);
}
 
Example 2
Source File: ShadowPainter.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void fill(Graphics g, Icon pattern, int x, int y, int from, int to, boolean horizontally) {
  double scale = JBUI.sysScale((Graphics2D)g);
  if (UIUtil.isJreHiDPIEnabled() && Math.ceil(scale) > scale) {
    // Direct painting for fractional scale
    BufferedImage img = ImageUtil.toBufferedImage(IconUtil.toImage(pattern));
    int patternSize = horizontally ? img.getWidth() : img.getHeight();
    Graphics2D g2d = (Graphics2D)g.create();
    try {
      g2d.scale(1 / scale, 1 / scale);
      g2d.translate(x * scale, y * scale);
      for (int at = (int)Math.floor(from * scale); at < to * scale; at += patternSize) {
        if (horizontally) {
          g2d.drawImage(img, at, 0, null);
        }
        else {
          g2d.drawImage(img, 0, at, null);
        }
      }
    }
    finally {
      g2d.dispose();
    }
  }
  else {
    for (int at = from; at < to; at++) {
      if (horizontally) {
        pattern.paintIcon(null, g, x + at, y);
      }
      else {
        pattern.paintIcon(null, g, x, y + at);
      }
    }
  }
}
 
Example 3
Source File: RecentProjectPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean rectInListCoordinatesContains(Rectangle listCellBounds, Point p) {

    int realCloseButtonInset = UIUtil.isJreHiDPI(myRootPanel) ? (int)(closeButtonInset * JBUI.sysScale(myRootPanel)) : closeButtonInset;

    Rectangle closeButtonRect =
            new Rectangle(myCloseButtonForEditor.getX() - realCloseButtonInset, myCloseButtonForEditor.getY() - realCloseButtonInset, myCloseButtonForEditor.getWidth() + realCloseButtonInset * 2,
                          myCloseButtonForEditor.getHeight() + realCloseButtonInset * 2);

    Rectangle rectInListCoordinates = new Rectangle(new Point(closeButtonRect.x + listCellBounds.x, closeButtonRect.y + listCellBounds.y), closeButtonRect.getSize());
    return rectInListCoordinates.contains(p);
  }
 
Example 4
Source File: AWTIconLoaderFacade.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new icon with the filter applied.
 */
@Nullable
public static Icon filterIcon(@Nonnull Icon icon, RGBImageFilter filter, @Nullable Component ancestor) {
  if (icon instanceof DesktopLazyImageImpl) icon = ((DesktopLazyImageImpl)icon).getOrComputeIcon();
  if (icon == null) return null;

  if (!isGoodSize(icon)) {
    LOG.error(icon); // # 22481
    return EMPTY_ICON;
  }
  if (icon instanceof CachedImageIcon) {
    icon = ((CachedImageIcon)icon).asDisabledIcon();
  }
  else {
    final float scale;
    if (icon instanceof JBUI.ScaleContextAware) {
      scale = (float)((JBUI.ScaleContextAware)icon).getScale(SYS_SCALE);
    }
    else {
      scale = UIUtil.isJreHiDPI() ? JBUI.sysScale(ancestor) : 1f;
    }
    @SuppressWarnings("UndesirableClassUsage") BufferedImage image = new BufferedImage((int)(scale * icon.getIconWidth()), (int)(scale * icon.getIconHeight()), BufferedImage.TYPE_INT_ARGB);
    final Graphics2D graphics = image.createGraphics();

    graphics.setColor(UIUtil.TRANSPARENT_COLOR);
    graphics.fillRect(0, 0, icon.getIconWidth(), icon.getIconHeight());
    graphics.scale(scale, scale);
    icon.paintIcon(LabelHolder.ourFakeComponent, graphics, 0, 0);

    graphics.dispose();

    Image img = ImageUtil.filter(image, filter);
    if (UIUtil.isJreHiDPI()) img = RetinaImage.createFrom(img, scale, null);

    icon = new JBImageIcon(img);
  }
  return icon;
}
 
Example 5
Source File: TipUIUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void updateImages(StringBuilder text, ClassLoader tipLoader, JEditorPane browser) {
  final boolean dark = StyleManager.get().getCurrentStyle().isDark();

  IdeFrame af = IdeFrameUtil.findActiveRootIdeFrame();
  Component comp = af != null ? TargetAWT.to(af.getWindow()) : browser;
  int index = text.indexOf("<img", 0);
  while (index != -1) {
    final int end = text.indexOf(">", index + 1);
    if (end == -1) return;
    final String img = text.substring(index, end + 1).replace('\r', ' ').replace('\n', ' ');
    final int srcIndex = img.indexOf("src=");
    final int endIndex = img.indexOf(".png", srcIndex);
    if (endIndex != -1) {
      String path = img.substring(srcIndex + 5, endIndex);
      if (!path.endsWith("_dark") && !path.endsWith("@2x")) {
        boolean hidpi = JBUI.isPixHiDPI(comp);
        path += (hidpi ? "@2x" : "") + (dark ? "_dark" : "") + ".png";
        URL url = ResourceUtil.getResource(tipLoader, "/tips/", path);
        if (url != null) {
          String newImgTag = "<img src=\"" + path + "\" ";
          try {
            BufferedImage image = ImageIO.read(url.openStream());
            int w = image.getWidth();
            int h = image.getHeight();
            if (UIUtil.isJreHiDPI(comp)) {
              // compensate JRE scale
              float sysScale = JBUI.sysScale(comp);
              w = (int)(w / sysScale);
              h = (int)(h / sysScale);
            }
            else {
              // compensate image scale
              float imgScale = hidpi ? 2f : 1f;
              w = (int)(w / imgScale);
              h = (int)(h / imgScale);
            }
            // fit the user scale
            w = (int)(JBUI.scale((float)w));
            h = (int)(JBUI.scale((float)h));

            newImgTag += "width=\"" + w + "\" height=\"" + h + "\"";
          }
          catch (Exception ignore) {
            newImgTag += "width=\"400\" height=\"200\"";
          }
          newImgTag += "/>";
          text.replace(index, end + 1, newImgTag);
        }
      }
    }
    index = text.indexOf("<img", index + 1);
  }
}
 
Example 6
Source File: CachingPainter.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Performs painting of frequently used pattern, using image cache. {@code x}, {@code y}, {@code width}, {@code height} define the region
 * where painting is done, {@code painter} performs the actual drawing, it's called with graphics origin set to the origin or the painting
 * region. Painter logic shouldn't depend on anything except the size of the region and values of {@code parameters}. Result of painting
 * will be cached with {@code key} as a key, and used for subsequent painting requests with the same region size and parameter values.
 * <p>
 * Subpixel-antialiased text shouldn't be rendered using this procedure, as the result depends on the target surface's background color,
 * and it cannot be determined when cached image is produced.
 */
public static void paint(@Nonnull Graphics2D g, float x, float y, float width, float height, @Nonnull Consumer<Graphics2D> painter, @Nonnull Object key, @Nonnull Object... parameters) {
  GraphicsConfiguration config = g.getDeviceConfiguration();
  float scale = JBUI.sysScale(config);
  if ((int)scale != scale) {
    // fractional-scale setups are not supported currently
    paintAndDispose((Graphics2D)g.create(), _g -> {
      _g.setComposite(AlphaComposite.SrcOver);
      _g.translate(x, y);
      painter.accept(_g);
    });
    return;
  }
  int xInt = (int)Math.floor(x);
  int yInt = (int)Math.floor(y);
  int widthInt = (int)Math.ceil(x + width) - xInt;
  int heightInt = (int)Math.ceil(y + height) - yInt;
  CachedPainting painting = ourCache.get(key);
  if (painting != null && !painting.matches(config, width, height, parameters)) {
    painting = null;
  }
  int validationResult = painting == null ? VolatileImage.IMAGE_INCOMPATIBLE : painting.image.validate(config);
  if (validationResult == VolatileImage.IMAGE_INCOMPATIBLE) {
    ourCache.put(key, painting = new CachedPainting(config, width, height, widthInt, heightInt, parameters));
  }
  if (validationResult != VolatileImage.IMAGE_OK) {
    // We cannot perform antialiased rendering onto volatile image using Src composite, so we draw to a buffered image first.
    BufferedImage bi = new JBHiDPIScaledImage(config, widthInt, heightInt, BufferedImage.TYPE_INT_ARGB, PaintUtil.RoundingMode.ROUND);
    paintAndDispose(bi.createGraphics(), _g -> {
      _g.setComposite(AlphaComposite.Src);
      _g.translate(x - xInt, y - yInt);
      painter.accept(_g);
    });
    paintAndDispose(painting.image.createGraphics(), _g -> {
      _g.setComposite(AlphaComposite.Src);
      UIUtil.drawImage(_g, bi, 0, 0, null);
    });
  }
  Composite savedComposite = g.getComposite();
  g.setComposite(AlphaComposite.SrcOver);
  g.drawImage(painting.image, xInt, yInt, null);
  g.setComposite(savedComposite);
  // We don't check whether volatile image's content was lost at this point,
  // cause we cannot repeat painting over the initial graphics reliably anyway (without restoring its initial contents first).
}
 
Example 7
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 8
Source File: UISettings.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the default font scale, which depends on the HiDPI mode (see JBUI#ScaleType).
 * <p>
 * The font is represented:
 * - in relative (dpi-independent) points in the JRE-managed HiDPI mode, so the method returns 1.0f
 * - in absolute (dpi-dependent) points in the IDE-managed HiDPI mode, so the method returns the default screen scale
 *
 * @return the system font scale
 */
public static float getDefFontScale() {
  return UIUtil.isJreHiDPIEnabled() ? 1f : JBUI.sysScale();
}
 
Example 9
Source File: JBHiDPIScaledImage.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a scaled HiDPI-aware BufferedImage, targeting the graphics config.
 *
 * @param gc     the graphics config which provides the target scale
 * @param width  the width in user coordinate space
 * @param height the height in user coordinate space
 * @param rm     the rounding mode to apply when converting width/height to the device space
 * @param type   the type
 */
public JBHiDPIScaledImage(@Nullable GraphicsConfiguration gc, double width, double height, int type, @Nonnull RoundingMode rm) {
  this(JBUI.sysScale(gc), width, height, type, rm);
}