Browse Source

Implemented setIcon() in NSViewComponentPeer

tags/2021-05-28
ed 7 years ago
parent
commit
9027d1e5a1
5 changed files with 86 additions and 54 deletions
  1. +31
    -8
      modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm
  2. +7
    -3
      modules/juce_graphics/native/juce_mac_CoreGraphicsHelpers.h
  3. +1
    -23
      modules/juce_gui_basics/native/juce_mac_MouseCursor.mm
  4. +45
    -13
      modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm
  5. +2
    -7
      modules/juce_gui_extra/native/juce_mac_SystemTrayIcon.cpp

+ 31
- 8
modules/juce_graphics/native/juce_mac_CoreGraphicsContext.mm View File

@@ -912,17 +912,40 @@ CGContextRef juce_getImageContext (const Image& image)
} }
#if JUCE_IOS #if JUCE_IOS
Image juce_createImageFromUIImage (UIImage* img)
{
CGImageRef image = [img CGImage];
Image juce_createImageFromUIImage (UIImage* img)
{
CGImageRef image = [img CGImage];
Image retval (Image::ARGB, (int) CGImageGetWidth (image), (int) CGImageGetHeight (image), true);
CGContextRef ctx = juce_getImageContext (retval);
Image retval (Image::ARGB, (int) CGImageGetWidth (image), (int) CGImageGetHeight (image), true);
CGContextRef ctx = juce_getImageContext (retval);
CGContextDrawImage (ctx, CGRectMake (0.0f, 0.0f, CGImageGetWidth (image), CGImageGetHeight (image)), image);
CGContextDrawImage (ctx, CGRectMake (0.0f, 0.0f, CGImageGetWidth (image), CGImageGetHeight (image)), image);
return retval;
}
return retval;
}
#endif
#if JUCE_MAC
NSImage* imageToNSImage (const Image& image, float scaleFactor)
{
JUCE_AUTORELEASEPOOL
{
NSImage* im = [[NSImage alloc] init];
auto requiredSize = NSMakeSize (image.getWidth() / scaleFactor, image.getHeight() / scaleFactor);
[im setSize: requiredSize];
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef imageRef = juce_createCoreGraphicsImage (image, colourSpace, true);
CGColorSpaceRelease (colourSpace);
NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: imageRef];
[imageRep setSize: requiredSize];
[im addRepresentation: imageRep];
[imageRep release];
CGImageRelease (imageRef);
return im;
}
}
#endif #endif
} }

+ 7
- 3
modules/juce_graphics/native/juce_mac_CoreGraphicsHelpers.h View File

@@ -55,11 +55,15 @@ namespace
} }
} }
extern CGImageRef juce_createCoreGraphicsImage (const Image&, CGColorSpaceRef, bool mustOutliveSource);
extern CGContextRef juce_getImageContext (const Image&);
CGImageRef juce_createCoreGraphicsImage (const Image&, CGColorSpaceRef, bool mustOutliveSource);
CGContextRef juce_getImageContext (const Image&);
#if JUCE_IOS #if JUCE_IOS
extern Image juce_createImageFromUIImage (UIImage*);
Image juce_createImageFromUIImage (UIImage*);
#endif
#if JUCE_MAC
NSImage* imageToNSImage (const Image& image, float scaleFactor = 1.0f);
#endif #endif
} // namespace juce } // namespace juce

+ 1
- 23
modules/juce_gui_basics/native/juce_mac_MouseCursor.mm View File

@@ -32,28 +32,6 @@ namespace juce
//============================================================================== //==============================================================================
namespace MouseCursorHelpers namespace MouseCursorHelpers
{ {
NSImage* createNSImage (const Image&, float scaleFactor = 1.0f);
NSImage* createNSImage (const Image& image, float scaleFactor)
{
JUCE_AUTORELEASEPOOL
{
NSImage* im = [[NSImage alloc] init];
auto requiredSize = NSMakeSize (image.getWidth() / scaleFactor, image.getHeight() / scaleFactor);
[im setSize: requiredSize];
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef imageRef = juce_createCoreGraphicsImage (image, colourSpace, true);
CGColorSpaceRelease (colourSpace);
NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage: imageRef];
[imageRep setSize: requiredSize];
[im addRepresentation: imageRep];
[imageRep release];
CGImageRelease (imageRef);
return im;
}
}
static NSCursor* fromNSImage (NSImage* im, NSPoint hotspot) static NSCursor* fromNSImage (NSImage* im, NSPoint hotspot)
{ {
NSCursor* c = [[NSCursor alloc] initWithImage: im NSCursor* c = [[NSCursor alloc] initWithImage: im
@@ -110,7 +88,7 @@ namespace MouseCursorHelpers
void* CustomMouseCursorInfo::create() const void* CustomMouseCursorInfo::create() const
{ {
return MouseCursorHelpers::fromNSImage (MouseCursorHelpers::createNSImage (image, scaleFactor),
return MouseCursorHelpers::fromNSImage (imageToNSImage (image, scaleFactor),
NSMakePoint (hotspot.x, hotspot.y)); NSMakePoint (hotspot.x, hotspot.y));
} }


+ 45
- 13
modules/juce_gui_basics/native/juce_mac_NSViewComponentPeer.mm View File

@@ -240,9 +240,13 @@ public:
void setRepresentedFile (const File& file) override void setRepresentedFile (const File& file) override
{ {
if (! isSharedWindow) if (! isSharedWindow)
{
[window setRepresentedFilename: juceStringToNS (file != File() [window setRepresentedFilename: juceStringToNS (file != File()
? file.getFullPathName() ? file.getFullPathName()
: String())]; : String())];
windowRepresentsFile = (file != File());
}
} }
void setBounds (const Rectangle<int>& newBounds, bool isNowFullScreen) override void setBounds (const Rectangle<int>& newBounds, bool isNowFullScreen) override
@@ -514,9 +518,16 @@ public:
} }
} }
void setIcon (const Image&) override
void setIcon (const Image& newIcon) override
{ {
// to do..
if (! isSharedWindow)
{
// need to set a dummy represented file here to show the file icon (which we then set to the new icon)
if (! windowRepresentsFile)
[window setRepresentedFilename:juceStringToNS (" ")]; // can't just use an empty string for some reason...
[[window standardWindowButton:NSWindowDocumentIconButton] setImage:imageToNSImage (newIcon)];
}
} }
StringArray getAvailableRenderingEngines() override StringArray getAvailableRenderingEngines() override
@@ -1365,6 +1376,7 @@ public:
#endif #endif
bool isZooming = false, isFirstLiveResize = false, textWasInserted = false; bool isZooming = false, isFirstLiveResize = false, textWasInserted = false;
bool isStretchingTop = false, isStretchingLeft = false, isStretchingBottom = false, isStretchingRight = false; bool isStretchingTop = false, isStretchingLeft = false, isStretchingBottom = false, isStretchingRight = false;
bool windowRepresentsFile = false;
String stringBeingComposed; String stringBeingComposed;
NSNotificationCenter* notificationCenter = nil; NSNotificationCenter* notificationCenter = nil;
@@ -1866,17 +1878,21 @@ struct JuceNSWindowClass : public ObjCClass<NSWindow>
{ {
addIvar<NSViewComponentPeer*> ("owner"); addIvar<NSViewComponentPeer*> ("owner");
addMethod (@selector (canBecomeKeyWindow), canBecomeKeyWindow, "c@:");
addMethod (@selector (canBecomeMainWindow), canBecomeMainWindow, "c@:");
addMethod (@selector (becomeKeyWindow), becomeKeyWindow, "v@:");
addMethod (@selector (windowShouldClose:), windowShouldClose, "c@:@");
addMethod (@selector (constrainFrameRect:toScreen:), constrainFrameRect, @encode (NSRect), "@:", @encode (NSRect), "@");
addMethod (@selector (windowWillResize:toSize:), windowWillResize, @encode (NSSize), "@:@", @encode (NSSize));
addMethod (@selector (windowDidExitFullScreen:), windowDidExitFullScreen, "v@:@");
addMethod (@selector (zoom:), zoom, "v@:@");
addMethod (@selector (windowWillMove:), windowWillMove, "v@:@");
addMethod (@selector (windowWillStartLiveResize:), windowWillStartLiveResize, "v@:@");
addMethod (@selector (windowDidEndLiveResize:), windowDidEndLiveResize, "v@:@");
addMethod (@selector (canBecomeKeyWindow), canBecomeKeyWindow, "c@:");
addMethod (@selector (canBecomeMainWindow), canBecomeMainWindow, "c@:");
addMethod (@selector (becomeKeyWindow), becomeKeyWindow, "v@:");
addMethod (@selector (windowShouldClose:), windowShouldClose, "c@:@");
addMethod (@selector (constrainFrameRect:toScreen:), constrainFrameRect, @encode (NSRect), "@:", @encode (NSRect), "@");
addMethod (@selector (windowWillResize:toSize:), windowWillResize, @encode (NSSize), "@:@", @encode (NSSize));
addMethod (@selector (windowDidExitFullScreen:), windowDidExitFullScreen, "v@:@");
addMethod (@selector (zoom:), zoom, "v@:@");
addMethod (@selector (windowWillMove:), windowWillMove, "v@:@");
addMethod (@selector (windowWillStartLiveResize:), windowWillStartLiveResize, "v@:@");
addMethod (@selector (windowDidEndLiveResize:), windowDidEndLiveResize, "v@:@");
addMethod (@selector (window:shouldPopUpDocumentPathMenu:), shouldPopUpPathMenu, "B@:@", @encode (NSMenu*));
addMethod (@selector (window:shouldDragDocumentWithEvent:from:withPasteboard:),
shouldAllowIconDrag, "B@:@", @encode (NSEvent*), @encode (NSPoint), @encode (NSPasteboard*));
#if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
addProtocol (@protocol (NSWindowDelegate)); addProtocol (@protocol (NSWindowDelegate));
@@ -1989,6 +2005,22 @@ private:
if (auto* owner = getOwner (self)) if (auto* owner = getOwner (self))
owner->liveResizingEnd(); owner->liveResizingEnd();
} }
static bool shouldPopUpPathMenu (id self, SEL, id /*window*/, NSMenu*)
{
if (auto* owner = getOwner (self))
return owner->windowRepresentsFile;
return false;
}
static bool shouldAllowIconDrag (id self, SEL, id /*window*/, NSEvent*, NSPoint, NSPasteboard*)
{
if (auto* owner = getOwner (self))
return owner->windowRepresentsFile;
return false;
}
}; };
NSView* NSViewComponentPeer::createViewInstance() NSView* NSViewComponentPeer::createViewInstance()


+ 2
- 7
modules/juce_gui_extra/native/juce_mac_SystemTrayIcon.cpp View File

@@ -27,11 +27,6 @@
namespace juce namespace juce
{ {
namespace MouseCursorHelpers
{
extern NSImage* createNSImage (const Image&, float scaleFactor = 1.f);
}
extern NSMenu* createNSMenu (const PopupMenu&, const String& name, int topLevelMenuId, extern NSMenu* createNSMenu (const PopupMenu&, const String& name, int topLevelMenuId,
int topLevelIndex, bool addDelegate); int topLevelIndex, bool addDelegate);
@@ -39,7 +34,7 @@ class SystemTrayIconComponent::Pimpl : private Timer
{ {
public: public:
Pimpl (SystemTrayIconComponent& iconComp, const Image& im) Pimpl (SystemTrayIconComponent& iconComp, const Image& im)
: owner (iconComp), statusIcon (MouseCursorHelpers::createNSImage (im))
: owner (iconComp), statusIcon (imageToNSImage (im))
{ {
static SystemTrayViewClass cls; static SystemTrayViewClass cls;
view = [cls.createInstance() init]; view = [cls.createInstance() init];
@@ -73,7 +68,7 @@ public:
void updateIcon (const Image& newImage) void updateIcon (const Image& newImage)
{ {
[statusIcon release]; [statusIcon release];
statusIcon = MouseCursorHelpers::createNSImage (newImage);
statusIcon = imageToNSImage (newImage);
setIconSize(); setIconSize();
SystemTrayViewClass::setImage (view, statusIcon); SystemTrayViewClass::setImage (view, statusIcon);
[statusItem setView: view]; [statusItem setView: view];


Loading…
Cancel
Save