Last active
October 3, 2018 02:38
-
-
Save t9mike/aff2886c141a26dce1a669de6079df9c to your computer and use it in GitHub Desktop.
Some C#/Xamarin wrappers for watchOS textProviderWithFormatformat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using CoreGraphics; | |
using WatchKit; | |
namespace Common | |
{ | |
public static class WatchUtil | |
{ | |
public static CGSize Watch_Size_Points | |
{ | |
get { return WKInterfaceDevice.CurrentDevice.ScreenBounds.Size; } | |
} | |
public static CGSize Watch_Size_Pixels | |
{ | |
get | |
{ | |
nfloat scale = WKInterfaceDevice.CurrentDevice.ScreenScale; | |
return new CGSize(Watch_Size_Points.Width * scale, Watch_Size_Points.Height * scale); | |
} | |
} | |
public static bool Has_Rounded_Corners => Watch_Size == WatchSize.Size40 || Watch_Size == WatchSize.Size44; | |
public static WatchSize Watch_Size | |
{ | |
get | |
{ | |
switch ((int)Watch_Size_Points.Width) | |
{ | |
// 38mm 136x170 | |
case 136: | |
return WatchSize.Size38; | |
// 40mm 162x197 | |
case 162: | |
return WatchSize.Size40; | |
// 42mm 156x195 | |
case 156: | |
return WatchSize.Size42; | |
// 44mm 184x224 | |
case 184: | |
return WatchSize.Size44; | |
default: | |
throw new NotImplementedException(); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using ClockKit; | |
using Common; | |
using Foundation; | |
using ObjCRuntime; | |
namespace WatchExtension | |
{ | |
/// <summary> | |
/// Provides wrappers to create a CLKTextProvider using watchOS textProviderWithFormatformat, ...) | |
/// varargs function. | |
/// </summary> | |
/// <remarks> | |
/// Help from Rolf Bjarne Kvinge and Manuel de la Pena. | |
/// See: | |
/// https://developer.apple.com/documentation/clockkit/clktextprovider/1627899-textproviderwithformat | |
/// https://github.com/xamarin/xamarin-macios/issues/4864#issuecomment-426331883 | |
/// https://github.com/xamarin/xamarin-macios/blob/788b63d96f90ed173895d31be439ec23c67722a6/src/UIKit/UIAppearance.cs#L114-L118 | |
/// https://bugzilla.xamarin.com/show_bug.cgi?id=53271 | |
/// </remarks> | |
public static class TextProviderWithFormat | |
{ | |
[DllImport("/usr/lib/libobjc.dylib")] | |
static extern IntPtr objc_msgSend(IntPtr obj, IntPtr selector, IntPtr format, IntPtr provider0); | |
[DllImport("/usr/lib/libobjc.dylib")] | |
static extern IntPtr objc_msgSend(IntPtr obj, IntPtr selector, IntPtr format, IntPtr provider0, IntPtr provider1); | |
[DllImport("/usr/lib/libobjc.dylib")] | |
static extern IntPtr objc_msgSend(IntPtr handle, IntPtr selector, IntPtr format, IntPtr dummy1, IntPtr dummy2, IntPtr dummy3, System.IntPtr dummy4, System.IntPtr dummy5, IntPtr provider0); | |
[DllImport("/usr/lib/libobjc.dylib")] | |
static extern IntPtr objc_msgSend(IntPtr handle, IntPtr selector, IntPtr format, IntPtr dummy1, IntPtr dummy2, IntPtr dummy3, System.IntPtr dummy4, System.IntPtr dummy5, IntPtr provider0, IntPtr provider1); | |
// True for Series 4+ running on device | |
public static bool Use_Dummy_Interop => WatchUtil.Has_Rounded_Corners && Runtime.Arch == Arch.DEVICE; | |
public static CLKTextProvider Create(string format, | |
CLKRelativeDateTextProvider provider) | |
{ | |
IntPtr ptr; | |
if (Use_Dummy_Interop) | |
{ | |
ptr = objc_msgSend(new Class(typeof(CLKTextProvider)).Handle, | |
Selector.GetHandle("textProviderWithFormat:"), | |
new NSString(format).Handle, | |
IntPtr.Zero, // dummy1 | |
IntPtr.Zero, // dummy2 | |
IntPtr.Zero, // dummy3 | |
IntPtr.Zero, // dummy4 | |
IntPtr.Zero, // dummy5 | |
provider.Handle); | |
} | |
else | |
{ | |
ptr = objc_msgSend(new Class(typeof(CLKTextProvider)).Handle, | |
Selector.GetHandle("textProviderWithFormat:"), | |
new NSString(format).Handle, provider.Handle); | |
} | |
return Runtime.GetNSObject<CLKTextProvider>(ptr); | |
} | |
public static CLKTextProvider Create(string format, | |
CLKTimeTextProvider timeTextProvider) | |
{ | |
IntPtr ptr; | |
if (Use_Dummy_Interop) | |
{ | |
ptr = objc_msgSend(new Class(typeof(CLKTextProvider)).Handle, | |
Selector.GetHandle("textProviderWithFormat:"), | |
new NSString(format).Handle, | |
IntPtr.Zero, // dummy1 | |
IntPtr.Zero, // dummy2 | |
IntPtr.Zero, // dummy3 | |
IntPtr.Zero, // dummy4 | |
IntPtr.Zero, // dummy5 | |
timeTextProvider.Handle); | |
} | |
else | |
{ | |
ptr = objc_msgSend(new Class(typeof(CLKTextProvider)).Handle, | |
Selector.GetHandle("textProviderWithFormat:"), | |
new NSString(format).Handle, | |
timeTextProvider.Handle); | |
} | |
return Runtime.GetNSObject<CLKTextProvider>(ptr); | |
} | |
public static CLKTextProvider Create(string format, | |
CLKTextProvider textProvider, | |
CLKRelativeDateTextProvider relativeDateTextProvider) | |
{ | |
IntPtr ptr; | |
if (Use_Dummy_Interop) | |
{ | |
ptr = objc_msgSend(new Class(typeof(CLKTextProvider)).Handle, | |
Selector.GetHandle("textProviderWithFormat:"), | |
new NSString(format).Handle, | |
textProvider.Handle, | |
IntPtr.Zero, // dummy1 | |
IntPtr.Zero, // dummy2 | |
IntPtr.Zero, // dummy3 | |
IntPtr.Zero, // dummy4 | |
IntPtr.Zero, // dummy5 | |
relativeDateTextProvider.Handle); | |
} | |
else | |
{ | |
ptr = objc_msgSend(new Class(typeof(CLKTextProvider)).Handle, | |
Selector.GetHandle("textProviderWithFormat:"), | |
new NSString(format).Handle, | |
textProvider.Handle, | |
relativeDateTextProvider.Handle); | |
} | |
return Runtime.GetNSObject<CLKTextProvider>(ptr); | |
} | |
public static CLKTextProvider Create(string format, | |
CLKRelativeDateTextProvider relativeDateTextProvider, | |
CLKTimeTextProvider timeTextProvider) | |
{ | |
IntPtr ptr; | |
if (Use_Dummy_Interop) | |
{ | |
ptr = objc_msgSend(new Class(typeof(CLKTextProvider)).Handle, | |
Selector.GetHandle("textProviderWithFormat:"), | |
new NSString(format).Handle, | |
IntPtr.Zero, // dummy1 | |
IntPtr.Zero, // dummy2 | |
IntPtr.Zero, // dummy3 | |
IntPtr.Zero, // dummy4 | |
IntPtr.Zero, // dummy5 | |
relativeDateTextProvider.Handle, | |
timeTextProvider.Handle); | |
} | |
else | |
{ | |
ptr = objc_msgSend(new Class(typeof(CLKTextProvider)).Handle, | |
Selector.GetHandle("textProviderWithFormat:"), | |
new NSString(format).Handle, | |
relativeDateTextProvider.Handle, | |
timeTextProvider.Handle); | |
} | |
return Runtime.GetNSObject<CLKTextProvider>(ptr); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment