Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/AppKit/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public enum NSImageCacheMode : ulong {

[NoMacCatalyst]
[Mac (10,10)]
[Native]
[Native (ConvertToNative = "NSImageResizingModeExtensions.ToNative", ConvertToManaged = "NSImageResizingModeExtensions.ToManaged")]
public enum NSImageResizingMode : long {
Stretch,
Tile
Expand Down Expand Up @@ -1304,7 +1304,7 @@ public enum NSDragOperation : ulong {
}

[NoMacCatalyst]
[Native]
[Native (ConvertToNative = "NSTextAlignmentExtensions.ToNative", ConvertToManaged = "NSTextAlignmentExtensions.ToManaged")]
public enum NSTextAlignment : ulong {
Left = 0,
Right = 1,
Expand Down
87 changes: 87 additions & 0 deletions src/AppKit/NSEnumsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// NSEnumsExtensions.cs:
//
// Copyright 2021, Microsoft Corp
//

using System;
using Foundation;
using ObjCRuntime;

namespace AppKit {

#if !__MACCATALYST__
public static class NSImageResizingModeExtensions {
public static nint ToNative (NSImageResizingMode value)
{
// For backwards compat reasons, the values we have in managed code corresponds with the X64 values.
if (Runtime.IsARM64CallingConvention) {
// Stretch and Tile are switched on arm64 on macOS
switch (value) {
case (NSImageResizingMode) 0:
value = (NSImageResizingMode) 1;
break;
case (NSImageResizingMode) 1:
value = (NSImageResizingMode) 0;
break;
}
}
return (nint) (long) value;
}

public static NSImageResizingMode ToManaged (nint value)
{
// For backwards compat reasons, the values we have in managed code corresponds with the X64 values.
if (Runtime.IsARM64CallingConvention) {
// Stretch and Tile are switched on arm64 on macOS
switch (value) {
case 0:
value = (nint) 1;
break;
case 1:
value = (nint) 0;
break;
}
}
return (NSImageResizingMode) (long) value;
}
}

public static class NSTextAlignmentExtensions {
public static nuint ToNative (NSTextAlignment value)
{
// For backwards compat reasons, the values we have in managed code corresponds with the X64 values.
if (Runtime.IsARM64CallingConvention) {
// Center and Right are switched on arm64 on macOS
switch (value) {
case (NSTextAlignment) 2:
value = (NSTextAlignment) 1;
break;
case (NSTextAlignment) 1:
value = (NSTextAlignment) 2;
break;
}
}
return (nuint) (ulong) value;
}

public static NSTextAlignment ToManaged (nuint value)
{
// For backwards compat reasons, the values we have in managed code corresponds with the X64 values.
if (Runtime.IsARM64CallingConvention) {
// Center and Right are switched on arm64 on macOS
switch (value) {
case 1:
value = (nuint) 2;
break;
case 2:
value = (nuint) 1;
break;
}
}
return (NSTextAlignment) (ulong) value;
}
}
#endif // !__MACCATALYST__
}

10 changes: 10 additions & 0 deletions src/ObjCRuntime/NativeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,15 @@ public NativeAttribute (string name)
}

public string NativeName { get; set; }

// methods to use to convert a managed enum value to native (and vice versa)
// The methods should have the following signatures:
// public static NativeType ConvertToNative (MyEnum value) { }
// public static MyEnum ConvertToManaged (NativeType value) { }
// Where <integral type> is any of [S]Byte, [U]Int16, [U]Int32, [U]Int64, n[u]int (our own versions).
// <integral type> must be the same for both methods.
// If one of these methods is specified, the other one must be as well.
public string ConvertToNative { get; set; }
public string ConvertToManaged { get; set; }
}
}
34 changes: 34 additions & 0 deletions src/ObjCRuntime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,40 @@ public static void ThrowException (IntPtr gchandle)

throw exc;
}

[EditorBrowsable (EditorBrowsableState.Advanced)]
public static ulong ConvertNativeEnumValueToManaged (nuint value, bool targetTypeHasMaxValue)
{
#if ARCH_32
// Check if we got UInt32.MaxValue, which should probably be UInt64.MaxValue
if (targetTypeHasMaxValue && value == nuint.MaxValue)
return ulong.MaxValue;
#endif
return (ulong) value;
}

[EditorBrowsable (EditorBrowsableState.Advanced)]
public static long ConvertNativeEnumValueToManaged (nint value, bool targetTypeHasMaxValue)
{
#if ARCH_32
// Check if we got Int32.MaxValue, which should probably be Int64.MaxValue
if (targetTypeHasMaxValue && value == nint.MaxValue)
return long.MaxValue;
#endif
return (long) value;
}

[EditorBrowsable (EditorBrowsableState.Advanced)]
public static nint ConvertManagedEnumValueToNative (long value)
{
return (nint) value;
}

[EditorBrowsable (EditorBrowsableState.Advanced)]
public static nuint ConvertManagedEnumValueToNative (ulong value)
{
return (nuint) value;
}
}

internal class IntPtrEqualityComparer : IEqualityComparer<IntPtr>
Expand Down
8 changes: 8 additions & 0 deletions src/UIKit/UIEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,11 @@ public enum UITouchProperties : long
// note: __TVOS_PROHIBITED -> because it uses NSLineBreakMode (but we need this because we don't expose the later)
//
// NSInteger -> UIStringDrawing.h
#if __MACCATALYST__
[Native (ConvertToNative = "UITextAlignmentExtensions.ToNative", ConvertToManaged = "UITextAlignmentExtensions.ToManaged")]
#else
[Native]
#endif
public enum UITextAlignment : long {
Left,
Center,
Expand Down Expand Up @@ -1230,7 +1234,11 @@ public enum UILayoutConstraintAxis : long {
}

// NSInteger -> UIImage.h
#if __MACCATALYST__
[Native (ConvertToNative = "UIImageResizingModeExtensions.ToNative", ConvertToManaged = "UIImageResizingModeExtensions.ToManaged")]
#else
[Native]
#endif
public enum UIImageResizingMode : long {
Tile, Stretch
}
Expand Down
75 changes: 72 additions & 3 deletions src/UIKit/UIEnumsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
// Miguel de Icaza
//

#if IOS

using System;
using Foundation;
using ObjCRuntime;

namespace UIKit {

#if IOS
public static class UIDeviceOrientationExtensions {
public static bool IsPortrait (this UIDeviceOrientation orientation)
{
Expand Down Expand Up @@ -46,6 +45,76 @@ public static bool IsLandscape (this UIInterfaceOrientation orientation)
orientation == UIInterfaceOrientation.LandscapeLeft;
}
}
#endif // IOS

#if __MACCATALYST__
public static class UIImageResizingModeExtensions {
public static nint ToNative (UIImageResizingMode value)
{
// The values we have in managed code corresponds with the ARM64 values.
if (!Runtime.IsARM64CallingConvention) {
// Stretch and Tile are switched on x64 on Mac Catalyst
switch (value) {
case (UIImageResizingMode) 0:
return 1;
case (UIImageResizingMode) 1:
return 0;
}
}
return (nint) (long) value;
}

public static UIImageResizingMode ToManaged (nint value)
{
// The values we have in managed code corresponds with the ARM64 values.
if (!Runtime.IsARM64CallingConvention) {
// Stretch and Tile are switched on x64 on Mac Catalyst
switch (value) {
case 0:
return (UIImageResizingMode) 1;
case 1:
return (UIImageResizingMode) 0;
}
}
return (UIImageResizingMode) (long) value;
}
}

public static class UITextAlignmentExtensions {
public static nint ToNative (UITextAlignment value)
{
// The values we have in managed code corresponds with the ARM64 values.
if (!Runtime.IsARM64CallingConvention) {
// Center and Right are switched on x64 on Mac Catalyst
switch (value) {
case (UITextAlignment) 1:
value = (UITextAlignment) 2;
break;
case (UITextAlignment) 2:
value = (UITextAlignment) 1;
break;
}
}
return (nint) (long) value;
}

public static UITextAlignment ToManaged (nint value)
{
// The values we have in managed code corresponds with the ARM64 values.
if (!Runtime.IsARM64CallingConvention) {
// Center and Right are switched on x64 on Mac Catalyst
switch (value) {
case 1:
value = (nint) 2;
break;
case 2:
value = (nint) 1;
break;
}
}
return (UITextAlignment) (long) value;
}
}
#endif // __MACCATALYST__
}

#endif // IOS
1 change: 1 addition & 0 deletions src/frameworks.sources
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ APPKIT_SOURCES = \
AppKit/NSPasteboard.cs \
AppKit/NSSharingServiceDelegate.cs \
AppKit/NSDocument.cs \
AppKit/NSEnumsExtensions.cs \
AppKit/NSEvent.cs \
AppKit/NSFont.cs \
AppKit/NSGestureRecognizer.cs \
Expand Down
32 changes: 28 additions & 4 deletions src/generator-enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Foundation;
using ObjCRuntime;

Expand Down Expand Up @@ -50,10 +51,33 @@ void GenerateEnum (Type type)

var native = AttributeManager.GetCustomAttribute<NativeAttribute> (type);
if (native != null) {
if (String.IsNullOrEmpty (native.NativeName))
print ("[Native]");
else
print ("[Native (\"{0}\")]", native.NativeName);
var sb = new StringBuilder ();
sb.Append ("[Native");
var hasNativeName = !string.IsNullOrEmpty (native.NativeName);
var hasConvertToManaged = !string.IsNullOrEmpty (native.ConvertToManaged);
var hasConvertToNative = !string.IsNullOrEmpty (native.ConvertToNative);
if (hasNativeName || hasConvertToManaged || hasConvertToNative ) {
sb.Append (" (");
if (hasNativeName)
sb.Append ('"').Append (native.NativeName).Append ('"');
if (hasConvertToManaged) {
if (hasNativeName)
sb.Append (", ");
sb.Append ("ConvertToManaged = \"");
sb.Append (native.ConvertToManaged);
sb.Append ('"');
}
if (hasConvertToNative) {
if (hasNativeName || hasConvertToManaged)
sb.Append (", ");
sb.Append ("ConvertToNative = \"");
sb.Append (native.ConvertToNative);
sb.Append ('"');
}
sb.Append (")");
}
sb.Append ("]");
print (sb.ToString ());
}
CopyObsolete (type);

Expand Down
18 changes: 14 additions & 4 deletions src/generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,8 +2104,13 @@ bool GetNativeEnumToManagedExpression (Type enumType, out string preExpression,
throw new BindingException (1029, enumType);
}

preExpression = "(" + renderedEnumType + ") (" + RenderType (underlyingEnumType) + ") ";
postExpression = string.Empty;
if (!string.IsNullOrEmpty (attrib.ConvertToManaged)) {
preExpression = attrib.ConvertToManaged + " (";
postExpression = ")";
} else {
preExpression = "(" + renderedEnumType + ") (" + RenderType (underlyingEnumType) + ") ";
postExpression = string.Empty;
}

// Check if we got UInt32.MaxValue, which should probably be UInt64.MaxValue (if the enum
// in question actually has that value at least). Same goes for Int32.MinValue/Int64.MinValue.
Expand Down Expand Up @@ -2152,8 +2157,13 @@ bool GetNativeEnumToNativeExpression (Type enumType, out string preExpression, o
throw new BindingException (1029, enumType);
}

preExpression = "(" + nativeType + ") (" + RenderType (underlyingEnumType) + ") ";
postExpression = string.Empty;
if (!string.IsNullOrEmpty (attrib.ConvertToNative)) {
preExpression = attrib.ConvertToNative + " (";
postExpression = ")";
} else {
preExpression = "(" + nativeType + ") (" + RenderType (underlyingEnumType) + ") ";
postExpression = string.Empty;
}

return true;
}
Expand Down
1 change: 1 addition & 0 deletions tests/generator/BGenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ public void NativeEnum ()
bgen.Profile = Profile.iOS;
bgen.ProcessEnums = true;
bgen.Defines = BGenTool.GetDefaultDefines (bgen.Profile);
bgen.Sources = new string [] { Path.Combine (Configuration.SourceRoot, "tests", "generator", "tests", "nativeenum-extensions.cs") }.ToList ();
bgen.ApiDefinitions = new string [] { Path.Combine (Configuration.SourceRoot, "tests", "generator", "tests", "nativeenum.cs") }.ToList ();
bgen.CreateTemporaryBinding ();
bgen.AssertExecute ("build");
Expand Down
Loading