[ScriptingBridge] Improve API and add nullability. (#13787)

* Improve the API for both legacy Xamarin and .NET.
* Obsolete the old API in legacy Xamarin and point to the new API.
* Remove the old API in .NET.
* Add nullability.
This commit is contained in:
Rolf Bjarne Kvinge 2022-01-19 20:52:18 +01:00 коммит произвёл GitHub
Родитель c6ee74377a
Коммит 26d9d9172a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 33 добавлений и 26 удалений

Просмотреть файл

@ -1,3 +1,5 @@
#nullable enable
using System;
using System.Reflection;
using AppKit;
@ -7,30 +9,36 @@ using ObjCRuntime;
namespace ScriptingBridge {
public partial class SBApplication
{
#if XAMCORE_4_0
public static SBApplication GetApplication (string ident) => Runtime.GetNSObject<SBApplication> (_FromBundleIdentifier (ident));
public static SBApplication? GetApplication (string ident) => Runtime.GetNSObject<SBApplication> (_FromBundleIdentifier (ident));
public static T GetApplication<T> (string ident) where T : SBApplication => Runtime.GetINativeObject<T> (_FromBundleIdentifier (ident), forced_type: true, owns: false);
public static T? GetApplication<T> (string ident) where T : SBApplication => Runtime.GetINativeObject<T> (_FromBundleIdentifier (ident), forced_type: true, owns: false);
public static SBApplication GetApplication (NSUrl url) => Runtime.GetNSObject<SBApplication> (_FromURL (url) ;
public static SBApplication? GetApplication (NSUrl url) => Runtime.GetNSObject<SBApplication> (_FromURL (url));
public static T GetApplication<T> (NSUrl url) where T : SBApplication => Runtime.GetINativeObject<T> (_FromURL (url), forced_type: true, owns: false);
public static T? GetApplication<T> (NSUrl url) where T : SBApplication => Runtime.GetINativeObject<T> (_FromURL (url), forced_type: true, owns: false);
public static SBApplication GetApplication (int pid) => Runtime.GetNSObject<SBApplication> (_FromProcessIdentifier (pid));
public static SBApplication? GetApplication (int pid) => Runtime.GetNSObject<SBApplication> (_FromProcessIdentifier (pid));
public static T GetApplication<T> (int pid) where T : SBApplication => Runtime.GetINativeObject<T> (_FromProcessIdentifier (pid), forced_type: true, owns: false);
#else
public static SBApplication FromBundleIdentifier (string ident) => Runtime.GetNSObject<SBApplication> (_FromBundleIdentifier (ident));
public static T? GetApplication<T> (int pid) where T : SBApplication => Runtime.GetINativeObject<T> (_FromProcessIdentifier (pid), forced_type: true, owns: false);
public static T FromBundleIdentifier<T> (string ident) where T : SBApplication => Runtime.GetINativeObject<T> (_FromBundleIdentifier (ident), forced_type: true, owns: false);
#if !NET
[Obsolete ("Use 'GetApplication' instead.")]
public static SBApplication? FromBundleIdentifier (string ident) => Runtime.GetNSObject<SBApplication> (_FromBundleIdentifier (ident));
public static SBApplication FromURL (NSUrl url) => Runtime.GetNSObject<SBApplication> (_FromURL (url));
[Obsolete ("Use 'GetApplication' instead.")]
public static T? FromBundleIdentifier<T> (string ident) where T : SBApplication => Runtime.GetINativeObject<T> (_FromBundleIdentifier (ident), forced_type: true, owns: false);
public static T FromURL<T> (NSUrl url) where T : SBApplication => Runtime.GetINativeObject<T> (_FromURL (url), forced_type: true, owns: false);
[Obsolete ("Use 'GetApplication' instead.")]
public static SBApplication? FromURL (NSUrl url) => Runtime.GetNSObject<SBApplication> (_FromURL (url));
public static SBApplication FromProcessIdentifier (int pid) => Runtime.GetNSObject<SBApplication> (_FromProcessIdentifier (pid));
[Obsolete ("Use 'GetApplication' instead.")]
public static T? FromURL<T> (NSUrl url) where T : SBApplication => Runtime.GetINativeObject<T> (_FromURL (url), forced_type: true, owns: false);
public static T FromProcessIdentifier<T> (int pid) where T : SBApplication => Runtime.GetINativeObject<T> (_FromProcessIdentifier (pid), forced_type: true, owns: false);
[Obsolete ("Use 'GetApplication' instead.")]
public static SBApplication? FromProcessIdentifier (int pid) => Runtime.GetNSObject<SBApplication> (_FromProcessIdentifier (pid));
[Obsolete ("Use 'GetApplication' instead.")]
public static T? FromProcessIdentifier<T> (int pid) where T : SBApplication => Runtime.GetINativeObject<T> (_FromProcessIdentifier (pid), forced_type: true, owns: false);
#endif
}
}

Просмотреть файл

@ -161,7 +161,7 @@ namespace ScriptingBridge {
[Model]
[Protocol]
interface SBApplicationDelegate {
#if !XAMCORE_4_0
#if !NET
[Abstract]
[Export ("eventDidFail:withError:"), DelegateName ("SBApplicationError"), DefaultValue (null)]
//NSObject EventDidFailwithError (const AppleEvent event, NSError error);

Просмотреть файл

@ -38,16 +38,16 @@ namespace Xamarin.Mac.Tests {
{
const string knownBundle = "com.apple.finder";
const string unknownBundle = "com.unknown.bundle";
#if !XAMCORE_4_0
#if !NET
using (var app1 = SBApplication.FromBundleIdentifier (knownBundle))
using (var app2 = SBApplication.FromBundleIdentifier<MySBApp> (knownBundle))
using (var app3 = SBApplication.FromBundleIdentifier (unknownBundle))
using (var app4 = SBApplication.FromBundleIdentifier<MySBApp> (unknownBundle))
#else
var (app1 = SBApplication.GetApplication (knownBundle))
var (app2 = SBApplication.GetApplication<MySbApp> (knownBundle))
var (app3 = SBApplication.GetApplication (unknownBundle))
var (app4 = SBApplication.GetApplication<MySbApp> (unknownBundle))
using (var app1 = SBApplication.GetApplication (knownBundle))
using (var app2 = SBApplication.GetApplication<MySBApp> (knownBundle))
using (var app3 = SBApplication.GetApplication (unknownBundle))
using (var app4 = SBApplication.GetApplication<MySBApp> (unknownBundle))
#endif
{
Assert.IsNotNull (app1, "SBApplication from known bundle is null");
@ -61,12 +61,12 @@ namespace Xamarin.Mac.Tests {
public void TestGetApplicationFromUrl ()
{
using (NSUrl knownUrl = new NSUrl ("http://www.xamarin.com"))
#if !XAMCORE_4_0
#if !NET
using (var app1 = SBApplication.FromURL (knownUrl))
using (var app2 = SBApplication.FromURL<MySBApp> (knownUrl))
#else
using (var app1 = SBApplication.GetApplication (knownUrl))
using (var app2 = SBApplication.GetApplication<MySbApp> (knownUrl))
using (var app2 = SBApplication.GetApplication<MySBApp> (knownUrl))
#endif
{
Assert.IsNotNull (app1, "SBApplication from known URL is null");
@ -79,16 +79,16 @@ namespace Xamarin.Mac.Tests {
{
int knownPid = System.Diagnostics.Process.GetCurrentProcess ().Id;
int unknownPid = -1; // valid pid is > 0
#if !XAMCORE_4_0
#if !NET
using (var app1 = SBApplication.FromProcessIdentifier (knownPid))
using (var app2 = SBApplication.FromProcessIdentifier<MySBApp> (knownPid))
using (var app3 = SBApplication.FromProcessIdentifier (unknownPid))
using (var app4 = SBApplication.FromProcessIdentifier<MySBApp> (unknownPid))
#else
using (var app1 = SBApplication.GetApplication (knownPid))
using (var app2 = SBApplication.GetApplication<MySbApp> (knownPid))
using (var app2 = SBApplication.GetApplication<MySBApp> (knownPid))
using (var app3 = SBApplication.GetApplication (unknownPid))
using (var app4 = SBApplication.GetApplication<MySbApp> (unknownPid)
using (var app4 = SBApplication.GetApplication<MySBApp> (unknownPid))
#endif
{
Assert.IsNotNull (app1, "SBApplication from known pid is null");

Просмотреть файл

@ -1,7 +1,6 @@
# Initial result from new rule missing-null-allowed
!missing-null-allowed! 'Foundation.NSError ScriptingBridge.SBObject::get_LastError()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSObject ScriptingBridge.SBApplicationDelegate::EventDidFailwithError(System.IntPtr,Foundation.NSError)' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSObject ScriptingBridge.SBObject::get_Get()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'Foundation.NSObject[] ScriptingBridge.SBElementArray::Get()' is missing an [NullAllowed] on return type
!missing-null-allowed! 'ObjCRuntime.Class ScriptingBridge.SBApplication::ClassForScripting(System.String)' is missing an [NullAllowed] on return type