From b8e71c9c6612c900225f235bacbcac7d052834d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez=20Ruiz?= Date: Thu, 16 Nov 2017 08:43:01 +0100 Subject: [PATCH 1/2] Added logic to launch Browser from Windows --- Ooui/Platform.cs | 267 ++++++++++++++++++++++++++--------------------- 1 file changed, 147 insertions(+), 120 deletions(-) diff --git a/Ooui/Platform.cs b/Ooui/Platform.cs index 951bd7a..123f8cc 100644 --- a/Ooui/Platform.cs +++ b/Ooui/Platform.cs @@ -7,147 +7,174 @@ namespace Ooui { static class Platform { - static readonly Assembly iosAssembly; - static readonly Type iosUIViewControllerType; - static readonly Type iosUIApplicationType; - static readonly Type iosUIWebViewType; - static readonly Type iosNSUrl; - static readonly Type iosNSUrlRequest; + static readonly Assembly iosAssembly; + static readonly Type iosUIViewControllerType; + static readonly Type iosUIApplicationType; + static readonly Type iosUIWebViewType; + static readonly Type iosNSUrl; + static readonly Type iosNSUrlRequest; - static readonly Assembly androidAssembly; - static readonly Type androidActivityType; - static readonly Type androidWebViewType; + static readonly Assembly androidAssembly; + static readonly Type androidActivityType; + static readonly Type androidWebViewType; - static Platform () - { - var asms = AppDomain.CurrentDomain.GetAssemblies ().ToDictionary ( - x => x.GetName ().Name); + static Platform() + { + var asms = AppDomain.CurrentDomain.GetAssemblies().ToDictionary( + x => x.GetName().Name); - asms.TryGetValue ("Xamarin.iOS", out iosAssembly); - if (iosAssembly != null) { - iosUIViewControllerType = iosAssembly.GetType ("UIKit.UIViewController"); - iosUIApplicationType = iosAssembly.GetType ("UIKit.UIApplication"); - iosUIWebViewType = iosAssembly.GetType ("UIKit.UIWebView"); - iosNSUrl = iosAssembly.GetType ("Foundation.NSUrl"); - iosNSUrlRequest = iosAssembly.GetType ("Foundation.NSUrlRequest"); - } + asms.TryGetValue("Xamarin.iOS", out iosAssembly); + if (iosAssembly != null) + { + iosUIViewControllerType = iosAssembly.GetType("UIKit.UIViewController"); + iosUIApplicationType = iosAssembly.GetType("UIKit.UIApplication"); + iosUIWebViewType = iosAssembly.GetType("UIKit.UIWebView"); + iosNSUrl = iosAssembly.GetType("Foundation.NSUrl"); + iosNSUrlRequest = iosAssembly.GetType("Foundation.NSUrlRequest"); + } - asms.TryGetValue ("Mono.Android", out androidAssembly); - if (androidAssembly != null) { - androidActivityType = androidAssembly.GetType ("Android.App.Activity"); - androidWebViewType = androidAssembly.GetType ("Android.Webkit.WebView"); - } - } + asms.TryGetValue("Mono.Android", out androidAssembly); + if (androidAssembly != null) + { + androidActivityType = androidAssembly.GetType("Android.App.Activity"); + androidWebViewType = androidAssembly.GetType("Android.Webkit.WebView"); + } + } - public static void OpenBrowser (string url, object presenter) - { - if (iosAssembly != null) { - OpenBrowserOniOS (url, presenter); - } - else if (androidAssembly != null) { - OpenBrowserOnAndroid (url, presenter); - } - else { - StartBrowserProcess (url); - } - } + public static void OpenBrowser(string url, object presenter) + { + if (iosAssembly != null) + { + OpenBrowserOniOS(url, presenter); + } + else if (androidAssembly != null) + { + OpenBrowserOnAndroid(url, presenter); + } + else + { + StartBrowserProcess(url); + } + } - static void OpenBrowserOnAndroid (string url, object presenter) - { - var presenterType = GetObjectType (presenter); + static void OpenBrowserOnAndroid(string url, object presenter) + { + var presenterType = GetObjectType(presenter); - object presenterWebView = null; - if (presenter != null && androidWebViewType.IsAssignableFrom (presenterType)) { - presenterWebView = presenter; - } + object presenterWebView = null; + if (presenter != null && androidWebViewType.IsAssignableFrom(presenterType)) + { + presenterWebView = presenter; + } - if (presenterWebView == null) { - throw new ArgumentException ("Presenter must be a WebView", nameof(presenter)); - } + if (presenterWebView == null) + { + throw new ArgumentException("Presenter must be a WebView", nameof(presenter)); + } - var m = androidWebViewType.GetMethod ("LoadUrl", BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, new Type[] { typeof(string) }, null); - m.Invoke (presenterWebView, new object[] { url }); - } + var m = androidWebViewType.GetMethod("LoadUrl", BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Any, new Type[] { typeof(string) }, null); + m.Invoke(presenterWebView, new object[] { url }); + } - static void OpenBrowserOniOS (string url, object presenter) - { - var presenterType = GetObjectType (presenter); + static void OpenBrowserOniOS(string url, object presenter) + { + var presenterType = GetObjectType(presenter); - // - // Find a presenter view controller - // 1. Try the given presenter - // 2. Find the key window vc - // 3. Create a window? - // - object presenterViewController = null; - if (presenter != null && iosUIViewControllerType.IsAssignableFrom (presenterType)) { - presenterViewController = presenter; - } + // + // Find a presenter view controller + // 1. Try the given presenter + // 2. Find the key window vc + // 3. Create a window? + // + object presenterViewController = null; + if (presenter != null && iosUIViewControllerType.IsAssignableFrom(presenterType)) + { + presenterViewController = presenter; + } - if (presenterViewController == null) { - var app = iosUIApplicationType.GetProperty ("SharedApplication").GetValue (null, null); - var window = iosUIApplicationType.GetProperty ("KeyWindow").GetValue (app, null); - if (window != null) { - var rvc = window.GetType ().GetProperty ("RootViewController").GetValue (window, null); - if (rvc != null) { - var pvc = rvc.GetType ().GetProperty ("PresentedViewController").GetValue (rvc, null); - presenterViewController = pvc ?? rvc; - } - } - } + if (presenterViewController == null) + { + var app = iosUIApplicationType.GetProperty("SharedApplication").GetValue(null, null); + var window = iosUIApplicationType.GetProperty("KeyWindow").GetValue(app, null); + if (window != null) + { + var rvc = window.GetType().GetProperty("RootViewController").GetValue(window, null); + if (rvc != null) + { + var pvc = rvc.GetType().GetProperty("PresentedViewController").GetValue(rvc, null); + presenterViewController = pvc ?? rvc; + } + } + } - if (presenterViewController == null) { - throw new InvalidOperationException ("Cannot find a view controller from which to present"); - } + if (presenterViewController == null) + { + throw new InvalidOperationException("Cannot find a view controller from which to present"); + } - // - // Create the browser - // - var browserVC = Activator.CreateInstance (iosUIViewControllerType); - var browserV = Activator.CreateInstance (iosUIWebViewType); + // + // Create the browser + // + var browserVC = Activator.CreateInstance(iosUIViewControllerType); + var browserV = Activator.CreateInstance(iosUIWebViewType); - var nsUrl = iosNSUrl.GetMethod ("FromString").Invoke (null, new object[] { url }); - var nsUrlRequest = iosNSUrlRequest.GetMethod ("FromUrl").Invoke (null, new object[] { nsUrl }); - iosUIWebViewType.GetMethod ("LoadRequest").Invoke (browserV, new object[] { nsUrlRequest }); - iosUIViewControllerType.GetProperty ("View").SetValue (browserVC, browserV, null); + var nsUrl = iosNSUrl.GetMethod("FromString").Invoke(null, new object[] { url }); + var nsUrlRequest = iosNSUrlRequest.GetMethod("FromUrl").Invoke(null, new object[] { nsUrl }); + iosUIWebViewType.GetMethod("LoadRequest").Invoke(browserV, new object[] { nsUrlRequest }); + iosUIViewControllerType.GetProperty("View").SetValue(browserVC, browserV, null); - var m = iosUIViewControllerType.GetMethod ("PresentViewController"); + var m = iosUIViewControllerType.GetMethod("PresentViewController"); - // Console.WriteLine (presenterViewController); - // Console.WriteLine (browserVC); - m.Invoke (presenterViewController, new object[] { browserVC, false, null }); - } + // Console.WriteLine (presenterViewController); + // Console.WriteLine (browserVC); + m.Invoke(presenterViewController, new object[] { browserVC, false, null }); + } - static Type GetObjectType (object o) - { - var t = typeof (object); - if (o is IReflectableType rt) { - t = rt.GetTypeInfo ().AsType (); - } - else if (o != null) { - t = o.GetType (); - } - return t; - } + static Type GetObjectType(object o) + { + var t = typeof(object); + if (o is IReflectableType rt) + { + t = rt.GetTypeInfo().AsType(); + } + else if (o != null) + { + t = o.GetType(); + } + return t; + } - static Process StartBrowserProcess (string url) - { - var cmd = url; - var args = ""; + static Process StartBrowserProcess(string url) + { + var cmd = url; + var args = string.Empty; - var osv = Environment.OSVersion; - if (osv.Platform == PlatformID.Unix) { - cmd = "open"; - args = url; - } + var osv = Environment.OSVersion; - // var vs = Environment.GetEnvironmentVariables (); - // foreach (System.Collections.DictionaryEntry kv in vs) { - // System.Console.WriteLine($"K={kv.Key}, V={kv.Value}"); - // } + if (osv.Platform == PlatformID.Unix) + { + cmd = "open"; + args = url; + } - // Console.WriteLine ($"Process.Start {cmd} {args}"); - return Process.Start (cmd, args); - } + var platform = (int)Environment.OSVersion.Platform; + var isWindows = ((platform != 4) && (platform != 6) && (platform != 128)); + + if (isWindows) + { + cmd = "explorer.exe"; + args = url; + } + + // var vs = Environment.GetEnvironmentVariables (); + // foreach (System.Collections.DictionaryEntry kv in vs) { + // System.Console.WriteLine($"K={kv.Key}, V={kv.Value}"); + // } + + // Console.WriteLine ($"Process.Start {cmd} {args}"); + + return Process.Start(cmd, args); + + } } } From 98efd72b61e991f4498b9fcdc5d77f8d921d45da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez=20Ruiz?= Date: Sun, 26 Nov 2017 18:28:47 +0100 Subject: [PATCH 2/2] Added Forms EditorRenderer --- Ooui.Forms/Exports.cs | 1 + Ooui.Forms/Renderers/EditorRenderer.cs | 124 +++++++++++++++++++++++++ Samples/EditorSample.cs | 44 +++++++++ Samples/Program.cs | 1 + 4 files changed, 170 insertions(+) create mode 100644 Ooui.Forms/Renderers/EditorRenderer.cs create mode 100644 Samples/EditorSample.cs diff --git a/Ooui.Forms/Exports.cs b/Ooui.Forms/Exports.cs index c8f3f60..49e6edc 100644 --- a/Ooui.Forms/Exports.cs +++ b/Ooui.Forms/Exports.cs @@ -7,6 +7,7 @@ using Xamarin.Forms.Internals; [assembly: Dependency (typeof (ResourcesProvider))] [assembly: ExportRenderer (typeof (BoxView), typeof (BoxRenderer))] [assembly: ExportRenderer (typeof (Button), typeof (ButtonRenderer))] +[assembly: ExportRenderer(typeof(Editor), typeof(EditorRenderer))] [assembly: ExportRenderer (typeof (Entry), typeof (EntryRenderer))] [assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))] diff --git a/Ooui.Forms/Renderers/EditorRenderer.cs b/Ooui.Forms/Renderers/EditorRenderer.cs new file mode 100644 index 0000000..d5b9137 --- /dev/null +++ b/Ooui.Forms/Renderers/EditorRenderer.cs @@ -0,0 +1,124 @@ +using Ooui.Forms.Extensions; +using System.ComponentModel; +using System.Diagnostics; +using Xamarin.Forms; + +namespace Ooui.Forms.Renderers +{ + public class EditorRenderer : ViewRenderer + { + private bool _disposed; + private Ooui.Color _defaultTextColor; + + static Size initialSize = Size.Zero; + + protected IElementController ElementController => Element as IElementController; + + public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint) + { + var size = Element.Text.MeasureSize(Element.FontFamily, Element.FontSize, Element.FontAttributes); + size = new Size(size.Width, size.Height * 1.428 + 14); + + return new SizeRequest(size, size); + } + + protected override void Dispose(bool disposing) + { + if (_disposed) + return; + + _disposed = true; + + if (disposing) + { + if (Control != null) + { + Control.Inputted -= OnEditingChanged; + Control.Changed -= OnEditingEnded; + } + } + + base.Dispose(disposing); + } + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + if (e.NewElement == null) + return; + + if (Control == null) + { + SetNativeControl(new Ooui.TextArea()); + + _defaultTextColor = Colors.Black; + + Debug.Assert(Control != null, "Control != null"); + + Control.Inputted += OnEditingChanged; + Control.Changed += OnEditingEnded; + } + + UpdateText(); + UpdateTextColor(); + UpdateFont(); + + base.OnElementChanged(e); + } + + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + base.OnElementPropertyChanged(sender, e); + + if (e.PropertyName == Editor.TextProperty.PropertyName) + UpdateText(); + else if (e.PropertyName == Editor.TextColorProperty.PropertyName) + UpdateTextColor(); + else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName) + UpdateFont(); + else if (e.PropertyName == Editor.FontFamilyProperty.PropertyName) + UpdateFont(); + else if (e.PropertyName == Editor.FontSizeProperty.PropertyName) + UpdateFont(); + } + + void UpdateText() + { + if (Control.Value != Element.Text) + Control.Value = Element.Text; + } + + void UpdateTextColor() + { + var textColor = Element.TextColor; + + if (textColor.IsDefault || !Element.IsEnabled) + Control.Style.Color = _defaultTextColor; + else + Control.Style.Color = textColor.ToOouiColor(); + } + + void UpdateFont() + { + if (initialSize == Size.Zero) + { + var testString = "Tj"; + initialSize = testString.MeasureSize(Control.Style); + } + + Element.SetStyleFont(Element.FontFamily, Element.FontSize, Element.FontAttributes, Control.Style); + } + + private void OnEditingChanged(object sender, TargetEventArgs e) + { + ElementController.SetValueFromRenderer(Editor.TextProperty, Control.Value); + } + + private void OnEditingEnded(object sender, TargetEventArgs e) + { + if (Control.Text != Element.Text) + { + ElementController.SetValueFromRenderer(Editor.TextProperty, Control.Text); + } + } + } +} diff --git a/Samples/EditorSample.cs b/Samples/EditorSample.cs new file mode 100644 index 0000000..494b9dc --- /dev/null +++ b/Samples/EditorSample.cs @@ -0,0 +1,44 @@ +using Ooui; +using Xamarin.Forms; + +namespace Samples +{ + public class EditorSample : ISample + { + public string Title => "Editor Sample"; + + public Ooui.Element CreateElement() + { + var panel = new StackLayout(); + + var titleLabel = new Xamarin.Forms.Label + { + Text = "Editor" + }; + panel.Children.Add(titleLabel); + + var editor = new Editor(); + panel.Children.Add(editor); + + var labelEditor = new Xamarin.Forms.Label(); + panel.Children.Add(labelEditor); + + editor.TextChanged += (sender, args) => + { + labelEditor.Text = args.NewTextValue; + }; + + var page = new ContentPage + { + Content = panel + }; + + return page.GetOouiElement(); + } + + public void Publish() + { + UI.Publish("/editor", CreateElement); + } + } +} diff --git a/Samples/Program.cs b/Samples/Program.cs index afff463..7a3f706 100644 --- a/Samples/Program.cs +++ b/Samples/Program.cs @@ -30,6 +30,7 @@ namespace Samples new DrawSample ().Publish (); new FilesSample ().Publish (); new DisplayAlertSample ().Publish (); + new EditorSample().Publish(); UI.Present ("/display-alert");