Search in sources :

Example 11 with ID

use of com.intellij.ui.mac.foundation.ID in project intellij-community by JetBrains.

the class MacColorPipette method captureScreen.

@Nullable
static BufferedImage captureScreen(@Nullable Window belowWindow, @NotNull Rectangle rect) {
    ID pool = Foundation.invoke("NSAutoreleasePool", "new");
    try {
        ID windowId = belowWindow != null ? MacUtil.findWindowFromJavaWindow(belowWindow) : null;
        Foundation.NSRect nsRect = new Foundation.NSRect(rect.x, rect.y, rect.width, rect.height);
        ID cgWindowId = windowId != null ? Foundation.invoke(windowId, "windowNumber") : ID.NIL;
        int windowListOptions = cgWindowId != null ? FoundationLibrary.kCGWindowListOptionOnScreenBelowWindow : FoundationLibrary.kCGWindowListOptionAll;
        int windowImageOptions = FoundationLibrary.kCGWindowImageNominalResolution;
        ID cgImageRef = Foundation.cgWindowListCreateImage(nsRect, windowListOptions, cgWindowId, windowImageOptions);
        ID bitmapRep = Foundation.invoke(Foundation.invoke("NSBitmapImageRep", "alloc"), "initWithCGImage:", cgImageRef);
        ID nsImage = Foundation.invoke(Foundation.invoke("NSImage", "alloc"), "init");
        Foundation.invoke(nsImage, "addRepresentation:", bitmapRep);
        ID data = Foundation.invoke(nsImage, "TIFFRepresentation");
        ID bytes = Foundation.invoke(data, "bytes");
        ID length = Foundation.invoke(data, "length");
        ByteBuffer byteBuffer = Native.getDirectByteBuffer(bytes.longValue(), length.longValue());
        Foundation.invoke(nsImage, "release");
        byte[] b = new byte[byteBuffer.remaining()];
        byteBuffer.get(b);
        BufferedImage result = ImageIO.read(new ByteArrayInputStream(b));
        if (result != null) {
            ColorSpace ics = ColorSpace.getInstance(ColorSpace.CS_sRGB);
            ColorConvertOp cco = new ColorConvertOp(ics, null);
            return cco.filter(result, null);
        }
        return null;
    } catch (Throwable t) {
        LOG.error(t);
        return null;
    } finally {
        Foundation.invoke(pool, "release");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) Foundation(com.intellij.ui.mac.foundation.Foundation) ID(com.intellij.ui.mac.foundation.ID) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with ID

use of com.intellij.ui.mac.foundation.ID in project intellij-community by JetBrains.

the class NSScrollerHelper method initNotificationObserver.

private static void initNotificationObserver() {
    Foundation.NSAutoreleasePool pool = new Foundation.NSAutoreleasePool();
    ID delegateClass = Foundation.allocateObjcClassPair(Foundation.getObjcClass("NSObject"), "NSScrollerChangesObserver");
    if (!ID.NIL.equals(delegateClass)) {
        // already exists.
        if (!Foundation.addMethod(delegateClass, Foundation.createSelector("handleScrollerStyleChanged:"), APPEARANCE_CALLBACK, "v@")) {
            throw new RuntimeException("Cannot add observer method");
        }
        if (!Foundation.addMethod(delegateClass, Foundation.createSelector("handleBehaviorChanged:"), BEHAVIOR_CALLBACK, "v@")) {
            throw new RuntimeException("Cannot add observer method");
        }
        Foundation.registerObjcClassPair(delegateClass);
    }
    ID delegate = invoke("NSScrollerChangesObserver", "new");
    try {
        ID center;
        center = invoke("NSNotificationCenter", "defaultCenter");
        invoke(center, "addObserver:selector:name:object:", delegate, Foundation.createSelector("handleScrollerStyleChanged:"), Foundation.nsString("NSPreferredScrollerStyleDidChangeNotification"), ID.NIL);
        center = invoke("NSDistributedNotificationCenter", "defaultCenter");
        invoke(center, "addObserver:selector:name:object:", delegate, Foundation.createSelector("handleBehaviorChanged:"), Foundation.nsString("AppleNoRedisplayAppearancePreferenceChanged"), ID.NIL, // NSNotificationSuspensionBehaviorCoalesce
        2);
    } finally {
        pool.drain();
    }
}
Also used : Foundation(com.intellij.ui.mac.foundation.Foundation) ID(com.intellij.ui.mac.foundation.ID)

Example 13 with ID

use of com.intellij.ui.mac.foundation.ID in project intellij-community by JetBrains.

the class DateFormatUtil method getMacFormats.

private static boolean getMacFormats(DateFormat[] formats) {
    final int MacFormatterNoStyle = 0;
    final int MacFormatterShortStyle = 1;
    final int MacFormatterMediumStyle = 2;
    final int MacFormatterBehavior_10_4 = 1040;
    ID autoReleasePool = Foundation.invoke("NSAutoreleasePool", "new");
    try {
        ID dateFormatter = Foundation.invoke("NSDateFormatter", "new");
        Foundation.invoke(dateFormatter, Foundation.createSelector("setFormatterBehavior:"), MacFormatterBehavior_10_4);
        // short date
        formats[0] = invokeFormatter(dateFormatter, MacFormatterNoStyle, MacFormatterShortStyle);
        // short time
        formats[1] = invokeFormatter(dateFormatter, MacFormatterShortStyle, MacFormatterNoStyle);
        // medium time
        formats[2] = invokeFormatter(dateFormatter, MacFormatterMediumStyle, MacFormatterNoStyle);
        // short date/time
        formats[3] = invokeFormatter(dateFormatter, MacFormatterShortStyle, MacFormatterShortStyle);
        return true;
    } finally {
        Foundation.invoke(autoReleasePool, Foundation.createSelector("release"));
    }
}
Also used : ID(com.intellij.ui.mac.foundation.ID)

Aggregations

ID (com.intellij.ui.mac.foundation.ID)13 Foundation (com.intellij.ui.mac.foundation.Foundation)3 ApplicationInfoEx (com.intellij.openapi.application.ex.ApplicationInfoEx)1 BuildNumber (com.intellij.openapi.util.BuildNumber)1 CustomProtocolHandler (com.intellij.ui.CustomProtocolHandler)1 Pointer (com.sun.jna.Pointer)1 ColorSpace (java.awt.color.ColorSpace)1 BufferedImage (java.awt.image.BufferedImage)1 ColorConvertOp (java.awt.image.ColorConvertOp)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteBuffer (java.nio.ByteBuffer)1 Nullable (org.jetbrains.annotations.Nullable)1