summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Extensions
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.MacOS/Extensions')
-rw-r--r--Xamarin.Forms.Platform.MacOS/Extensions/AlignmentExtensions.cs21
-rw-r--r--Xamarin.Forms.Platform.MacOS/Extensions/ButtonExtensions.cs25
-rw-r--r--Xamarin.Forms.Platform.MacOS/Extensions/NSButtonExtensions.cs25
-rw-r--r--Xamarin.Forms.Platform.MacOS/Extensions/NSImageExtensions.cs22
-rw-r--r--Xamarin.Forms.Platform.MacOS/Extensions/NSScrollViewExtensions.cs36
-rw-r--r--Xamarin.Forms.Platform.MacOS/Extensions/NSTableViewExtensions.cs22
-rw-r--r--Xamarin.Forms.Platform.MacOS/Extensions/NSTextFieldExtensions.cs46
-rw-r--r--Xamarin.Forms.Platform.MacOS/Extensions/NSViewControllerExtensions.cs24
-rw-r--r--Xamarin.Forms.Platform.MacOS/Extensions/PageExtensions.cs28
9 files changed, 249 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.MacOS/Extensions/AlignmentExtensions.cs b/Xamarin.Forms.Platform.MacOS/Extensions/AlignmentExtensions.cs
new file mode 100644
index 0000000..35d0f26
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Extensions/AlignmentExtensions.cs
@@ -0,0 +1,21 @@
+using System;
+using AppKit;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ internal static class AlignmentExtensions
+ {
+ internal static NSTextAlignment ToNativeTextAlignment(this TextAlignment alignment)
+ {
+ switch (alignment)
+ {
+ case TextAlignment.Center:
+ return NSTextAlignment.Center;
+ case TextAlignment.End:
+ return NSTextAlignment.Right;
+ default:
+ return NSTextAlignment.Left;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.MacOS/Extensions/ButtonExtensions.cs b/Xamarin.Forms.Platform.MacOS/Extensions/ButtonExtensions.cs
new file mode 100644
index 0000000..1bdc62a
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Extensions/ButtonExtensions.cs
@@ -0,0 +1,25 @@
+using System;
+using AppKit;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ internal static class ButtonExtensions
+ {
+ public static NSCellImagePosition ToNSCellImagePosition(this Button control)
+ {
+ switch (control.ContentLayout.Position)
+ {
+ case Button.ButtonContentLayout.ImagePosition.Left:
+ return NSCellImagePosition.ImageLeft;
+ case Button.ButtonContentLayout.ImagePosition.Top:
+ return NSCellImagePosition.ImageAbove;
+ case Button.ButtonContentLayout.ImagePosition.Right:
+ return NSCellImagePosition.ImageRight;
+ case Button.ButtonContentLayout.ImagePosition.Bottom:
+ return NSCellImagePosition.ImageBelow;
+ default:
+ return NSCellImagePosition.ImageOnly;
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.MacOS/Extensions/NSButtonExtensions.cs b/Xamarin.Forms.Platform.MacOS/Extensions/NSButtonExtensions.cs
new file mode 100644
index 0000000..d37f1a3
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Extensions/NSButtonExtensions.cs
@@ -0,0 +1,25 @@
+using System;
+using AppKit;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ public static class NSButtonExtensions
+ {
+ public static NSButton CreateButton(string text, Action activate = null)
+ {
+ return CreateButton(text, null, activate);
+ }
+
+ public static NSButton CreateButton(string text, NSImage image = null, Action activate = null)
+ {
+ var btn = new NSButton { Title = text };
+ btn.BezelStyle = NSBezelStyle.TexturedRounded;
+
+ if (image != null)
+ btn.Image = image;
+ if (activate != null)
+ btn.Activated += (sender, e) => activate();
+ return btn;
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.MacOS/Extensions/NSImageExtensions.cs b/Xamarin.Forms.Platform.MacOS/Extensions/NSImageExtensions.cs
new file mode 100644
index 0000000..0952173
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Extensions/NSImageExtensions.cs
@@ -0,0 +1,22 @@
+using AppKit;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ public static class NSImageExtensions
+ {
+ public static NSImage ResizeTo(this NSImage self, CoreGraphics.CGSize newSize)
+ {
+ if (self == null)
+ return null;
+ self.ResizingMode = NSImageResizingMode.Stretch;
+ var resizedImage = new NSImage(newSize);
+ resizedImage.LockFocus();
+ self.Size = newSize;
+ NSGraphicsContext.CurrentContext.ImageInterpolation = NSImageInterpolation.High;
+ self.Draw(CoreGraphics.CGPoint.Empty, new CoreGraphics.CGRect(0, 0, newSize.Width, newSize.Height),
+ NSCompositingOperation.Copy, 1.0f);
+ resizedImage.UnlockFocus();
+ return resizedImage;
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.MacOS/Extensions/NSScrollViewExtensions.cs b/Xamarin.Forms.Platform.MacOS/Extensions/NSScrollViewExtensions.cs
new file mode 100644
index 0000000..f9104d4
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Extensions/NSScrollViewExtensions.cs
@@ -0,0 +1,36 @@
+using System.Threading.Tasks;
+using AppKit;
+using PointF = CoreGraphics.CGPoint;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ internal static class NSScrollViewExtensions
+ {
+ public static Task ScrollToPositionAsync(this NSScrollView scrollView, PointF point, bool animate,
+ double duration = 0.5)
+ {
+ if (!animate)
+ {
+ var nsView = scrollView.DocumentView as NSView;
+ nsView?.ScrollPoint(point);
+ return Task.FromResult(true);
+ }
+
+ TaskCompletionSource<bool> source = new TaskCompletionSource<bool>();
+
+ NSAnimationContext.BeginGrouping();
+
+ NSAnimationContext.CurrentContext.CompletionHandler += () => { source.TrySetResult(true); };
+
+ NSAnimationContext.CurrentContext.Duration = duration;
+
+ var animator = scrollView.ContentView.Animator as NSView;
+
+ animator?.SetBoundsOrigin(point);
+
+ NSAnimationContext.EndGrouping();
+
+ return source.Task;
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.MacOS/Extensions/NSTableViewExtensions.cs b/Xamarin.Forms.Platform.MacOS/Extensions/NSTableViewExtensions.cs
new file mode 100644
index 0000000..c81e31e
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Extensions/NSTableViewExtensions.cs
@@ -0,0 +1,22 @@
+using AppKit;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ internal static class NSTableViewExtensions
+ {
+ public static NSTableView AsListViewLook(this NSTableView self)
+ {
+ self.SelectionHighlightStyle = NSTableViewSelectionHighlightStyle.SourceList;
+
+ self.AllowsColumnReordering = false;
+ self.AllowsColumnResizing = false;
+ self.AllowsColumnSelection = false;
+
+ //this is needed .. can we go around it ?
+ self.AddColumn(new NSTableColumn("1"));
+ //this line hides the header by default
+ self.HeaderView = new CustomNSTableHeaderView();
+ return self;
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.MacOS/Extensions/NSTextFieldExtensions.cs b/Xamarin.Forms.Platform.MacOS/Extensions/NSTextFieldExtensions.cs
new file mode 100644
index 0000000..9905fcd
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Extensions/NSTextFieldExtensions.cs
@@ -0,0 +1,46 @@
+using AppKit;
+using CoreGraphics;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ internal static class NSTextFieldExtensions
+ {
+ public static NSTextField CreateLabel(string text)
+ {
+ var textField = new NSTextField();
+ textField.StringValue = text;
+ textField.DrawsBackground = false;
+ textField.Editable = false;
+ textField.Bezeled = false;
+ textField.Selectable = false;
+ textField.SizeToFit();
+ textField.CenterTextVertically();
+ return textField;
+ }
+
+ public static NSTextFieldCell CreateLabelCentered(string text)
+ {
+ var textField = new VerticallyCenteredTextFieldCell(0);
+ textField.StringValue = text;
+ textField.DrawsBackground = false;
+ textField.Editable = false;
+ textField.Bezeled = false;
+ textField.Selectable = false;
+ return textField;
+ }
+
+ public static void CenterTextVertically(this NSTextField self)
+ {
+ self.CenterTextVertically(self.Frame);
+ }
+
+ public static void CenterTextVertically(this NSTextField self, CGRect frame)
+ {
+ var stringHeight = self.Cell.AttributedStringValue.Size.Height;
+ var titleRect = self.Cell.TitleRectForBounds(frame);
+ var newTitleRect = new CGRect(titleRect.X, frame.Y + (frame.Height - stringHeight) / 2.0, titleRect.Width,
+ stringHeight);
+ self.Frame = newTitleRect;
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.MacOS/Extensions/NSViewControllerExtensions.cs b/Xamarin.Forms.Platform.MacOS/Extensions/NSViewControllerExtensions.cs
new file mode 100644
index 0000000..f556232
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Extensions/NSViewControllerExtensions.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Threading.Tasks;
+using AppKit;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ internal static class NSViewControllerExtensions
+ {
+ public static Task<T> HandleAsyncAnimation<T>(this NSViewController container, NSViewController fromViewController,
+ NSViewController toViewController, NSViewControllerTransitionOptions transitonOption,
+ Action animationFinishedCallback, T result)
+ {
+ var tcs = new TaskCompletionSource<T>();
+
+ container.TransitionFromViewController(fromViewController, toViewController, transitonOption, () =>
+ {
+ tcs.SetResult(result);
+ animationFinishedCallback?.Invoke();
+ });
+
+ return tcs.Task;
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Platform.MacOS/Extensions/PageExtensions.cs b/Xamarin.Forms.Platform.MacOS/Extensions/PageExtensions.cs
new file mode 100644
index 0000000..24c9a52
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Extensions/PageExtensions.cs
@@ -0,0 +1,28 @@
+using System;
+using AppKit;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ public static class PageExtensions
+ {
+ public static NSViewController CreateViewController(this Page view)
+ {
+ if (!Forms.IsInitialized)
+ throw new InvalidOperationException("call Forms.Init() before this");
+
+ if (!(view.RealParent is Application))
+ {
+ Application app = new DefaultApplication();
+ app.MainPage = view;
+ }
+
+ var result = new Platform();
+ result.SetPage(view);
+ return result.ViewController;
+ }
+
+ class DefaultApplication : Application
+ {
+ }
+ }
+} \ No newline at end of file