Skip to content

Commit

Permalink
[Foundation] Create a strong dictionary for NSAttributedStringDocumen…
Browse files Browse the repository at this point in the history
…tAttributeKey. (#19824)
  • Loading branch information
rolfbjarne committed Jan 29, 2024
1 parent ff516d1 commit 343aab8
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 168 deletions.
28 changes: 28 additions & 0 deletions src/Foundation/DictionaryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#endif // !WATCH
#endif

#if HAS_UIKIT
using UIKit;
#endif

#if !NET
using NativeHandle = System.IntPtr;
#endif
Expand Down Expand Up @@ -336,6 +340,23 @@ protected DictionaryContainer (NSDictionary? dictionary)
return value;
}
#endif // !WATCH

#if HAS_UIKIT
protected UIEdgeInsets? GetUIEdgeInsets (NSString key)
{
if (key is null)
throw new ArgumentNullException (nameof (key));

if (!Dictionary.TryGetValue (key, out var value))
return null;

if (value is NSValue size)
return size.UIEdgeInsetsValue;

return null;
}
#endif

bool NullCheckAndRemoveKey ([NotNullWhen (true)] NSString key, bool removeEntry)
{
if (key is null)
Expand Down Expand Up @@ -491,6 +512,13 @@ protected void SetCMTimeValue (NSString key, CMTime? value)
Dictionary [key] = value!.Value.ToDictionary ();
}
#endif //!WATCH

#if HAS_UIKIT
protected void SetUIEdgeInsets (NSString key, UIEdgeInsets? value)
{
SetNativeValue (key, value is null ? null : NSValue.FromUIEdgeInsets (value.Value));
}
#endif
#endregion
#endif
}
Expand Down
151 changes: 17 additions & 134 deletions src/Foundation/NSAttributedStringDocumentAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#nullable enable

using System;
using System.ComponentModel;

#if HAS_APPKIT
using AppKit;
Expand All @@ -34,71 +35,29 @@
namespace Foundation {
public partial class NSAttributedStringDocumentAttributes : DictionaryContainer {
#if !COREBUILD
public NSAttributedStringDocumentAttributes () { }
public NSAttributedStringDocumentAttributes (NSDictionary? dictionary) : base (dictionary) { }

public XColor? BackgroundColor {
get {
return GetNativeValue<XColor> (NSAttributedStringDocumentAttributeKey.NSBackgroundColorDocumentAttribute);
}
set {
SetNativeValue (NSAttributedStringDocumentAttributeKey.NSBackgroundColorDocumentAttribute, value);
}
}

public float? DefaultTabInterval {
get {
return GetFloatValue (NSAttributedStringDocumentAttributeKey.NSDefaultTabIntervalDocumentAttribute);
}
set {
if (value < 0 || value > 1.0f)
throw new ArgumentOutOfRangeException (nameof (value), value, "Value must be between 0 and 1");

SetNumberValue (NSAttributedStringDocumentAttributeKey.NSDefaultTabIntervalDocumentAttribute, value);
}
}

public float? HyphenationFactor {
public NSString? WeakDocumentType {
get {
return GetFloatValue (NSAttributedStringDocumentAttributeKey.NSHyphenationFactorDocumentAttribute);
return GetNSStringValue (NSAttributedStringDocumentAttributeKey.DocumentTypeDocumentAttribute);
}
set {
if (value < 0 || value > 1.0f)
throw new ArgumentOutOfRangeException (nameof (value), value, "Value must be between 0 and 1");

SetNumberValue (NSAttributedStringDocumentAttributeKey.NSHyphenationFactorDocumentAttribute, value);
SetStringValue (NSAttributedStringDocumentAttributeKey.DocumentTypeDocumentAttribute, value);
}
}

#if !XAMCORE_5_0
[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete ("Use 'CharacterEncoding' instead.")]
public NSStringEncoding? StringEncoding {
get {
return (NSStringEncoding?) (long?) GetNIntValue (NSAttributedStringDocumentAttributeKey.NSCharacterEncodingDocumentAttribute);
return CharacterEncoding;
}
set {
SetNumberValue (NSAttributedStringDocumentAttributeKey.NSCharacterEncodingDocumentAttribute, (nint?) (long?) value);
CharacterEncoding = value;
}
}
#endif // !XAMCORE_5_0

public NSString? WeakDocumentType {
get {
return GetNSStringValue (NSAttributedStringDocumentAttributeKey.NSDocumentTypeDocumentAttribute);
}
set {
SetStringValue (NSAttributedStringDocumentAttributeKey.NSDocumentTypeDocumentAttribute, value);
}
}

#if XAMCORE_5_0
public NSAttributedStringDocumentType DocumentType {
get {

return NSAttributedStringDocumentTypeExtensions.GetValue (WeakDocumentType);
}
set {
WeakDocumentType = value.GetConstant ();
}
}
#else
#if !XAMCORE_5_0
public NSDocumentType DocumentType {
get {

Expand All @@ -112,94 +71,35 @@ public NSDocumentType DocumentType {

public NSDictionary? WeakDefaultAttributes {
get {
return GetNativeValue<NSDictionary> (NSAttributedStringDocumentAttributeKey.NSDefaultAttributesDocumentAttribute);
}
set {
SetNativeValue (NSAttributedStringDocumentAttributeKey.NSDefaultAttributesDocumentAttribute, value);
}
}

public CGSize? PaperSize {
get {
return GetCGSizeValue (NSAttributedStringDocumentAttributeKey.NSPaperSizeDocumentAttribute);
}
set {
SetCGSizeValue (NSAttributedStringDocumentAttributeKey.NSPaperSizeDocumentAttribute, value);
}
}

#if !__MACOS__
#if NET
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("tvos")]
[SupportedOSPlatform ("maccatalyst")]
[UnsupportedOSPlatform ("macos")]
#endif // NET
public UIEdgeInsets? PaperMargin {
get {
if (!Dictionary.TryGetValue (NSAttributedStringDocumentAttributeKey.NSPaperMarginDocumentAttribute, out var value))
return null;

if (value is NSValue size)
return size.UIEdgeInsetsValue;

return null;
}
set {
SetNativeValue (NSAttributedStringDocumentAttributeKey.NSPaperMarginDocumentAttribute, value is null ? null : NSValue.FromUIEdgeInsets (value.Value));
}
}
#endif // !__MACOS__

public CGSize? ViewSize {
get {
return GetCGSizeValue (NSAttributedStringDocumentAttributeKey.NSViewSizeDocumentAttribute);
}
set {
SetCGSizeValue (NSAttributedStringDocumentAttributeKey.NSViewSizeDocumentAttribute, value);
}
}

public float? ViewZoom {
get {
return GetFloatValue (NSAttributedStringDocumentAttributeKey.NSViewZoomDocumentAttribute);
return GetNativeValue<NSDictionary> (NSAttributedStringDocumentAttributeKey.DefaultAttributesDocumentAttribute);
}
set {
SetNumberValue (NSAttributedStringDocumentAttributeKey.NSViewZoomDocumentAttribute, value);
}
}

public NSDocumentViewMode? ViewMode {
get {
return (NSDocumentViewMode?) GetInt32Value (NSAttributedStringDocumentAttributeKey.NSViewModeDocumentAttribute);
}
set {
SetNumberValue (NSAttributedStringDocumentAttributeKey.NSViewModeDocumentAttribute, value is null ? null : (int) value.Value);
SetNativeValue (NSAttributedStringDocumentAttributeKey.DefaultAttributesDocumentAttribute, value);
}
}

#if XAMCORE_5_0 || __MACOS__
public bool? ReadOnly {
get {
var value = GetInt32Value (NSAttributedStringDocumentAttributeKey.NSReadOnlyDocumentAttribute);
var value = GetInt32Value (NSAttributedStringDocumentAttributeKey.ReadOnlyDocumentAttribute);
if (value is null)
return null;
return value.Value == 1;
}
set {
SetNumberValue (NSAttributedStringDocumentAttributeKey.NSReadOnlyDocumentAttribute, value is null ? null : (value.Value ? 1 : 0));
SetNumberValue (NSAttributedStringDocumentAttributeKey.ReadOnlyDocumentAttribute, value is null ? null : (value.Value ? 1 : 0));
}
}
#else
public bool ReadOnly {
get {
var value = GetInt32Value (NSAttributedStringDocumentAttributeKey.NSReadOnlyDocumentAttribute);
var value = GetInt32Value (NSAttributedStringDocumentAttributeKey.ReadOnlyDocumentAttribute);
if (value is null || value.Value != 1)
return false;
return true;
}
set {
SetNumberValue (NSAttributedStringDocumentAttributeKey.NSReadOnlyDocumentAttribute, value ? 1 : 0);
SetNumberValue (NSAttributedStringDocumentAttributeKey.ReadOnlyDocumentAttribute, value ? 1 : 0);
}
}
#endif // XAMCORE_5_0 || __MACOS__
Expand Down Expand Up @@ -276,23 +176,6 @@ public NSUrl? BaseUrl {
}
#endif // !__MACOS__

#if __MACOS__
#if NET
[UnsupportedOSPlatform ("ios")]
[UnsupportedOSPlatform ("tvos")]
[UnsupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
#endif // NET
public string? TextEncodingName {
get {
return GetStringValue (NSAttributedStringDocumentReadingOptionKey.TextEncodingNameDocumentOption);
}
set {
SetStringValue (NSAttributedStringDocumentReadingOptionKey.TextEncodingNameDocumentOption, value);
}
}
#endif // !__MACOS__

#if __MACOS__
#if NET
[UnsupportedOSPlatform ("ios")]
Expand Down
5 changes: 5 additions & 0 deletions src/bgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1862,6 +1862,9 @@ void GenerateStrongDictionaryTypes ()
} else if (isNativeEnum && fetchType == TypeCache.System_UInt64) {
getter = "{1} (ulong?) GetNUIntValue ({0})";
setter = "SetNumberValue ({0}, {1}value)";
} else if (fetchType == TypeCache.UIEdgeInsets) {
getter = "{1} GetUIEdgeInsets ({0})";
setter = "SetUIEdgeInsets ({0}, {1}value)";
} else {
throw new BindingException (1033, true, pi.PropertyType, dictType, pi.Name);
}
Expand Down Expand Up @@ -1934,6 +1937,7 @@ void GenerateStrongDictionaryTypes ()
if (pi.CanRead) {
indent++;
print ("get {"); indent++;
Inject<PreSnippetAttribute> (pi.GetGetMethod ());
print ("return {0};", getter);
indent--; print ("}");
indent--;
Expand All @@ -1943,6 +1947,7 @@ void GenerateStrongDictionaryTypes ()
throw new BindingException (1032, true, pi.PropertyType, dictType, pi.Name);
indent++;
print ("set {"); indent++;
Inject<PreSnippetAttribute> (pi.GetSetMethod ());
print ("{0};", setter);
indent--; print ("}");
indent--;
Expand Down
Loading

6 comments on commit 343aab8

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.