diff --git a/src/Foundation/DictionaryContainer.cs b/src/Foundation/DictionaryContainer.cs index 5a7918052cb8..d8247bf5cb33 100644 --- a/src/Foundation/DictionaryContainer.cs +++ b/src/Foundation/DictionaryContainer.cs @@ -546,6 +546,7 @@ static public NativeHandle GetHandle (this DictionaryContainer? self) // helper to avoid the (common pattern) // var p = x is null ? null : x.Dictionary; + [return: NotNullIfNotNull (nameof (self))] static public NSDictionary? GetDictionary (this DictionaryContainer? self) { return self is null ? null : self.Dictionary; diff --git a/src/UIKit/UISegmentedControl.cs b/src/UIKit/UISegmentedControl.cs index f80fe2c8f0f9..d3756f66ca79 100644 --- a/src/UIKit/UISegmentedControl.cs +++ b/src/UIKit/UISegmentedControl.cs @@ -8,19 +8,14 @@ // Copyright 2011 Xamarin, Inc // -#if !WATCH - using System; using System.Collections; using Foundation; using ObjCRuntime; using CoreGraphics; +using UIKit; -#if XAMCORE_3_0 -using TextAttributes = UIKit.UIStringAttributes; -#else -using TextAttributes = UIKit.UITextAttributes; -#endif +#nullable enable namespace UIKit { public partial class UISegmentedControl { @@ -92,49 +87,5 @@ static NSArray FromStrings (string [] strings) return NSArray.FromStrings (strings); } - - public void SetTitleTextAttributes (TextAttributes attributes, UIControlState state) - { - if (attributes is null) - throw new ArgumentNullException ("attributes"); - -#if XAMCORE_3_0 - var dict = attributes.Dictionary; -#else - using var dict = attributes.ToDictionary (); -#endif - _SetTitleTextAttributes (dict, state); - } - - public TextAttributes GetTitleTextAttributes (UIControlState state) - { - using (var d = _GetTitleTextAttributes (state)) { - return new TextAttributes (d); - } - } - - public partial class UISegmentedControlAppearance { - public void SetTitleTextAttributes (TextAttributes attributes, UIControlState state) - { - if (attributes is null) - throw new ArgumentNullException ("attributes"); - -#if XAMCORE_3_0 - var dict = attributes.Dictionary; -#else - using var dict = attributes.ToDictionary (); -#endif - _SetTitleTextAttributes (dict, state); - } - - public TextAttributes GetTitleTextAttributes (UIControlState state) - { - using (var d = _GetTitleTextAttributes (state)) { - return new TextAttributes (d); - } - } - } } } - -#endif // !WATCH diff --git a/src/uikit.cs b/src/uikit.cs index c3559a3f2381..bdb2c99f5332 100644 --- a/src/uikit.cs +++ b/src/uikit.cs @@ -14087,13 +14087,22 @@ interface UISegmentedControl UIImage DividerImageForLeftSegmentStaterightSegmentStatebarMetrics (UIControlState leftState, UIControlState rightState, UIBarMetrics barMetrics); #endif - [Export ("setTitleTextAttributes:forState:"), Internal] [Appearance] - void _SetTitleTextAttributes (NSDictionary attributes, UIControlState state); + [Wrap ("SetTitleTextAttributes (attributes?.GetDictionary (), state)")] + void SetTitleTextAttributes ([NullAllowed] UIStringAttributes attributes, UIControlState state); - [Export ("titleTextAttributesForState:"), Internal] + [Export ("setTitleTextAttributes:forState:")] [Appearance] - NSDictionary _GetTitleTextAttributes (UIControlState state); + void SetTitleTextAttributes ([NullAllowed] NSDictionary attributes, UIControlState state); + + [Appearance] + [Wrap ("new UIStringAttributes (GetWeakTitleTextAttributes (state))")] + UIStringAttributes GetTitleTextAttributes (UIControlState state); + + [Appearance] + [Export ("titleTextAttributesForState:")] + [return: NullAllowed] + NSDictionary GetWeakTitleTextAttributes (UIControlState state); [Export ("setContentPositionAdjustment:forSegmentType:barMetrics:")] [Appearance] diff --git a/tests/cecil-tests/Documentation.KnownFailures.txt b/tests/cecil-tests/Documentation.KnownFailures.txt index 17d3e16881ca..900ecf8af7ed 100644 --- a/tests/cecil-tests/Documentation.KnownFailures.txt +++ b/tests/cecil-tests/Documentation.KnownFailures.txt @@ -51492,9 +51492,11 @@ M:UIKit.UISegmentedControl.UISegmentedControlAppearance.ContentPositionAdjustmen M:UIKit.UISegmentedControl.UISegmentedControlAppearance.GetBackgroundImage(UIKit.UIControlState,UIKit.UIBarMetrics) M:UIKit.UISegmentedControl.UISegmentedControlAppearance.GetDividerImage(UIKit.UIControlState,UIKit.UIControlState,UIKit.UIBarMetrics) M:UIKit.UISegmentedControl.UISegmentedControlAppearance.GetTitleTextAttributes(UIKit.UIControlState) +M:UIKit.UISegmentedControl.UISegmentedControlAppearance.GetWeakTitleTextAttributes(UIKit.UIControlState) M:UIKit.UISegmentedControl.UISegmentedControlAppearance.SetBackgroundImage(UIKit.UIImage,UIKit.UIControlState,UIKit.UIBarMetrics) M:UIKit.UISegmentedControl.UISegmentedControlAppearance.SetContentPositionAdjustment(UIKit.UIOffset,UIKit.UISegmentedControlSegment,UIKit.UIBarMetrics) M:UIKit.UISegmentedControl.UISegmentedControlAppearance.SetDividerImage(UIKit.UIImage,UIKit.UIControlState,UIKit.UIControlState,UIKit.UIBarMetrics) +M:UIKit.UISegmentedControl.UISegmentedControlAppearance.SetTitleTextAttributes(Foundation.NSDictionary,UIKit.UIControlState) M:UIKit.UISegmentedControl.UISegmentedControlAppearance.SetTitleTextAttributes(UIKit.UIStringAttributes,UIKit.UIControlState) M:UIKit.UISelectionFeedbackGenerator.GetFeedbackGenerator(UIKit.UIView) M:UIKit.UIShadowProperties.Copy(Foundation.NSZone) diff --git a/tests/monotouch-test/UIKit/SegmentedControlTest.cs b/tests/monotouch-test/UIKit/SegmentedControlTest.cs index 37435e0c216c..a567efd3b0e1 100644 --- a/tests/monotouch-test/UIKit/SegmentedControlTest.cs +++ b/tests/monotouch-test/UIKit/SegmentedControlTest.cs @@ -7,6 +7,7 @@ using System.IO; using CoreGraphics; using Foundation; +using ObjCRuntime; using UIKit; using NUnit.Framework; @@ -107,6 +108,17 @@ public void CtorUIImage () Assert.That (sc.NumberOfSegments, Is.EqualTo ((nint) 1), "NumberOfSegments"); } } + + [Test] + public void TitleTextAttributes () + { + using var sc = new UISegmentedControl ("one", "two"); + sc.SetTitleTextAttributes (new UIStringAttributes () { ForegroundColor = UIColor.Gray }, UIControlState.Selected); + var attrib = sc.GetTitleTextAttributes (UIControlState.Selected); + Assert.AreEqual (UIColor.Gray, attrib?.ForegroundColor, "ForegroundColor"); + Assert.IsNotNull (attrib?.Dictionary, "Dictionary"); + Assert.AreNotEqual (NativeHandle.Zero, attrib.Dictionary.Handle, "Dictionary.Handle"); + } } } diff --git a/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-UIKit.ignore b/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-UIKit.ignore index 8e3b0a4f0476..2739f5efab4c 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-UIKit.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/MacCatalyst-UIKit.ignore @@ -172,7 +172,6 @@ !missing-null-allowed! 'Foundation.NSDictionary UIKit.UIDocument::GetFileAttributesToWrite(Foundation.NSUrl,UIKit.UIDocumentSaveOperation,Foundation.NSError&)' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSDictionary UIKit.UIMotionEffect::ComputeKeyPathsAndRelativeValues(UIKit.UIOffset)' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSDictionary UIKit.UISearchBar::_GetScopeBarButtonTitleTextAttributes(UIKit.UIControlState)' is missing an [NullAllowed] on return type -!missing-null-allowed! 'Foundation.NSDictionary UIKit.UISegmentedControl::_GetTitleTextAttributes(UIKit.UIControlState)' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSIndexPath UIKit.UICollectionViewFocusUpdateContext::get_NextFocusedIndexPath()' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSIndexPath UIKit.UICollectionViewFocusUpdateContext::get_PreviouslyFocusedIndexPath()' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSIndexPath UIKit.UITableViewDelegate::WillDeselectRow(UIKit.UITableView,Foundation.NSIndexPath)' is missing an [NullAllowed] on return type @@ -284,7 +283,6 @@ !missing-null-allowed! 'System.Void UIKit.UISearchBar::_SetScopeBarButtonTitle(Foundation.NSDictionary,UIKit.UIControlState)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISearchTextField::set_TokenBackgroundColor(UIKit.UIColor)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::.ctor(Foundation.NSArray)' is missing an [NullAllowed] on parameter #0 -!missing-null-allowed! 'System.Void UIKit.UISegmentedControl::_SetTitleTextAttributes(Foundation.NSDictionary,UIKit.UIControlState)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::InsertSegment(System.String,System.IntPtr,System.Boolean)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::InsertSegment(UIKit.UIImage,System.IntPtr,System.Boolean)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::SetImage(UIKit.UIImage,System.IntPtr)' is missing an [NullAllowed] on parameter #0 diff --git a/tests/xtro-sharpie/api-annotations-dotnet/iOS-UIKit.ignore b/tests/xtro-sharpie/api-annotations-dotnet/iOS-UIKit.ignore index cc11a9f0362c..5539be8aa49c 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/iOS-UIKit.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/iOS-UIKit.ignore @@ -176,7 +176,6 @@ !missing-null-allowed! 'Foundation.NSDictionary UIKit.UIDocument::GetFileAttributesToWrite(Foundation.NSUrl,UIKit.UIDocumentSaveOperation,Foundation.NSError&)' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSDictionary UIKit.UIMotionEffect::ComputeKeyPathsAndRelativeValues(UIKit.UIOffset)' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSDictionary UIKit.UISearchBar::_GetScopeBarButtonTitleTextAttributes(UIKit.UIControlState)' is missing an [NullAllowed] on return type -!missing-null-allowed! 'Foundation.NSDictionary UIKit.UISegmentedControl::_GetTitleTextAttributes(UIKit.UIControlState)' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSIndexPath UIKit.UICollectionViewFocusUpdateContext::get_NextFocusedIndexPath()' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSIndexPath UIKit.UICollectionViewFocusUpdateContext::get_PreviouslyFocusedIndexPath()' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSIndexPath UIKit.UITableViewDelegate::WillDeselectRow(UIKit.UITableView,Foundation.NSIndexPath)' is missing an [NullAllowed] on return type @@ -288,7 +287,6 @@ !missing-null-allowed! 'System.Void UIKit.UISearchBar::_SetScopeBarButtonTitle(Foundation.NSDictionary,UIKit.UIControlState)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISearchTextField::set_TokenBackgroundColor(UIKit.UIColor)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::.ctor(Foundation.NSArray)' is missing an [NullAllowed] on parameter #0 -!missing-null-allowed! 'System.Void UIKit.UISegmentedControl::_SetTitleTextAttributes(Foundation.NSDictionary,UIKit.UIControlState)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::InsertSegment(System.String,System.IntPtr,System.Boolean)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::InsertSegment(UIKit.UIImage,System.IntPtr,System.Boolean)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::SetImage(UIKit.UIImage,System.IntPtr)' is missing an [NullAllowed] on parameter #0 diff --git a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-UIKit.ignore b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-UIKit.ignore index ddb894e0504e..dfbf47f14611 100644 --- a/tests/xtro-sharpie/api-annotations-dotnet/tvOS-UIKit.ignore +++ b/tests/xtro-sharpie/api-annotations-dotnet/tvOS-UIKit.ignore @@ -196,7 +196,6 @@ !missing-null-allowed! 'Foundation.NSDictionary UIKit.UICollectionViewLayoutInvalidationContext::get_InvalidatedSupplementaryIndexPaths()' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSDictionary UIKit.UIMotionEffect::ComputeKeyPathsAndRelativeValues(UIKit.UIOffset)' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSDictionary UIKit.UISearchBar::_GetScopeBarButtonTitleTextAttributes(UIKit.UIControlState)' is missing an [NullAllowed] on return type -!missing-null-allowed! 'Foundation.NSDictionary UIKit.UISegmentedControl::_GetTitleTextAttributes(UIKit.UIControlState)' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSIndexPath UIKit.UICollectionViewFocusUpdateContext::get_NextFocusedIndexPath()' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSIndexPath UIKit.UICollectionViewFocusUpdateContext::get_PreviouslyFocusedIndexPath()' is missing an [NullAllowed] on return type !missing-null-allowed! 'Foundation.NSIndexPath UIKit.UITableViewDelegate::WillDeselectRow(UIKit.UITableView,Foundation.NSIndexPath)' is missing an [NullAllowed] on return type @@ -265,7 +264,6 @@ !missing-null-allowed! 'System.Void UIKit.UIScrollViewDelegate::ZoomingStarted(UIKit.UIScrollView,UIKit.UIView)' is missing an [NullAllowed] on parameter #1 !missing-null-allowed! 'System.Void UIKit.UISearchBar::_SetScopeBarButtonTitle(Foundation.NSDictionary,UIKit.UIControlState)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::.ctor(Foundation.NSArray)' is missing an [NullAllowed] on parameter #0 -!missing-null-allowed! 'System.Void UIKit.UISegmentedControl::_SetTitleTextAttributes(Foundation.NSDictionary,UIKit.UIControlState)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::InsertSegment(System.String,System.IntPtr,System.Boolean)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::InsertSegment(UIKit.UIImage,System.IntPtr,System.Boolean)' is missing an [NullAllowed] on parameter #0 !missing-null-allowed! 'System.Void UIKit.UISegmentedControl::SetImage(UIKit.UIImage,System.IntPtr)' is missing an [NullAllowed] on parameter #0