Skip to content

Commit

Permalink
[appkit] Add NSImage lazy initialization
Browse files Browse the repository at this point in the history
We were missing initByReferencingFile: which initializes
the image object lazily.

The non working use case was:

var iconFile = NSBundle.MainBundle.PathForResource ("AppIcons", "icns");
NSApplication.SharedApplication.ApplicationIconImage = new NSImage (iconFile);

In this example, the constructor is calling initWithContentsOfFile: which will
not load the right icon from the .icns file.

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=40349.
  • Loading branch information
Vincent Dondain committed Apr 27, 2016
1 parent 2edb2ae commit 8b40072
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/AppKit/NSImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public static NSImage FromStream (System.IO.Stream stream)
}
}

public NSImage (string fileName, bool lazy)
{
if (lazy)
Handle = InitByReferencingFile (fileName);
else
Handle = InitWithContentsOfFile (fileName);
}

public NSImage (NSData data, bool ignoresOrientation)
{
if (ignoresOrientation) {
Expand Down
8 changes: 6 additions & 2 deletions src/appkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8183,15 +8183,19 @@ public partial interface NSImage : NSCoding, NSCopying, NSSecureCoding, NSPasteb
[Export ("initWithContentsOfURL:")]
IntPtr Constructor (NSUrl url);

//[Export ("initByReferencingFile:")]
//IntPtr Constructor (string fileName);
//[Export ("initByReferencingURL:")]
//IntPtr Constructor (NSUrl url);

// FIXME: need IconRec
//[Export ("initWithIconRef:")]
//IntPtr Constructor (IconRef iconRef);

[Sealed, Export ("initWithContentsOfFile:"), Internal]
IntPtr InitWithContentsOfFile (string fileName);

[Export ("initByReferencingFile:"), Internal]
IntPtr InitByReferencingFile (string name);

[Export ("initWithPasteboard:")]
IntPtr Constructor (NSPasteboard pasteboard);

Expand Down

0 comments on commit 8b40072

Please sign in to comment.