Skip to content

Commit

Permalink
[UIKit] Improve code for UISegmentedControl. Fixes #21289. (#21299)
Browse files Browse the repository at this point in the history
1. Enable nullability.
2. Move Get|SetTitleTextAttributes to generated code.
3. Remove dead code (pre-.NET code paths).

Point 2. fixes #21289, so add tests for this scenario.

Fixes #21289.
  • Loading branch information
rolfbjarne committed Oct 2, 2024
1 parent 5016fd7 commit 3e30d47
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 61 deletions.
1 change: 1 addition & 0 deletions src/Foundation/DictionaryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
53 changes: 2 additions & 51 deletions src/UIKit/UISegmentedControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
17 changes: 13 additions & 4 deletions src/uikit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14085,13 +14085,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]
Expand Down
2 changes: 2 additions & 0 deletions tests/cecil-tests/Documentation.KnownFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions tests/monotouch-test/UIKit/SegmentedControlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using CoreGraphics;
using Foundation;
using ObjCRuntime;
using UIKit;
using NUnit.Framework;

Expand Down Expand Up @@ -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");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions tests/xtro-sharpie/api-annotations-dotnet/iOS-UIKit.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions tests/xtro-sharpie/api-annotations-dotnet/tvOS-UIKit.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e30d47

Please sign in to comment.