Merge pull request #209 from zumero/image-scaling
Image Display fixes for Ooui.Forms
This commit is contained in:
		
						commit
						e8b6fd4cfc
					
				| 
						 | 
					@ -9,6 +9,8 @@ namespace Ooui.Forms.Renderers
 | 
				
			||||||
    public class ImageRenderer : ViewRenderer<Xamarin.Forms.Image, Ooui.Image>
 | 
					    public class ImageRenderer : ViewRenderer<Xamarin.Forms.Image, Ooui.Image>
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        bool _isDisposed;
 | 
					        bool _isDisposed;
 | 
				
			||||||
 | 
					        double ClientHeight = -1;
 | 
				
			||||||
 | 
					        double ClientWidth = -1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        protected override void Dispose (bool disposing)
 | 
					        protected override void Dispose (bool disposing)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
| 
						 | 
					@ -29,6 +31,8 @@ namespace Ooui.Forms.Renderers
 | 
				
			||||||
                var imageView = new Ooui.Image ();
 | 
					                var imageView = new Ooui.Image ();
 | 
				
			||||||
                SetNativeControl (imageView);
 | 
					                SetNativeControl (imageView);
 | 
				
			||||||
                this.Style.Overflow = "hidden";
 | 
					                this.Style.Overflow = "hidden";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                Control.Loaded += OnLoad;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (e.NewElement != null) {
 | 
					            if (e.NewElement != null) {
 | 
				
			||||||
| 
						 | 
					@ -48,7 +52,44 @@ namespace Ooui.Forms.Renderers
 | 
				
			||||||
            else if (e.PropertyName == Xamarin.Forms.Image.IsOpaqueProperty.PropertyName)
 | 
					            else if (e.PropertyName == Xamarin.Forms.Image.IsOpaqueProperty.PropertyName)
 | 
				
			||||||
                SetOpacity ();
 | 
					                SetOpacity ();
 | 
				
			||||||
            else if (e.PropertyName == Xamarin.Forms.Image.AspectProperty.PropertyName)
 | 
					            else if (e.PropertyName == Xamarin.Forms.Image.AspectProperty.PropertyName)
 | 
				
			||||||
                SetAspect ();
 | 
					                SetAspect();
 | 
				
			||||||
 | 
					            else if (e.PropertyName == VisualElement.WidthProperty.PropertyName)
 | 
				
			||||||
 | 
					                SetDimensions ();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        void OnLoad(object sender, EventArgs eventArgs)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var args = (TargetEventArgs)eventArgs;
 | 
				
			||||||
 | 
					            ClientHeight = args.ClientHeight;
 | 
				
			||||||
 | 
					            ClientWidth = args.ClientWidth;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            SetDimensions();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        void SetDimensions()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var b = Element.Bounds;
 | 
				
			||||||
 | 
					            double scale = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (ClientWidth < 0 || ClientHeight < 0)
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (Math.Abs(b.Width) > 0)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                scale = b.Width / ClientWidth;
 | 
				
			||||||
 | 
					                Element.WidthRequest = b.Width;
 | 
				
			||||||
 | 
					                Element.HeightRequest = scale * ClientHeight;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else if (Math.Abs(b.Height) > 0)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                scale = b.Height / ClientHeight;
 | 
				
			||||||
 | 
					                Element.WidthRequest = scale * ClientWidth;
 | 
				
			||||||
 | 
					                Element.HeightRequest = b.Height;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                // We can't really know what to do in this case
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        void SetAspect ()
 | 
					        void SetAspect ()
 | 
				
			||||||
| 
						 | 
					@ -112,7 +153,7 @@ namespace Ooui.Forms.Renderers
 | 
				
			||||||
                    return;
 | 
					                    return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                var imageView = Control;
 | 
					                var imageView = Control;
 | 
				
			||||||
                if (imageView != null)
 | 
					                if (imageView != null && uiimage != null)
 | 
				
			||||||
                    imageView.Source = uiimage;
 | 
					                    imageView.Source = uiimage;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                ((IVisualElementController)Element).NativeSizeChanged ();
 | 
					                ((IVisualElementController)Element).NativeSizeChanged ();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -37,6 +37,10 @@ const inputEvents = {
 | 
				
			||||||
    keyup: true,
 | 
					    keyup: true,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const elementEvents = {
 | 
				
			||||||
 | 
					    load: true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getSize () {
 | 
					function getSize () {
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
        height: window.innerHeight,
 | 
					        height: window.innerHeight,
 | 
				
			||||||
| 
						 | 
					@ -304,6 +308,12 @@ function msgListen (m) {
 | 
				
			||||||
                offsetY: e.offsetY,
 | 
					                offsetY: e.offsetY,
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        else if (elementEvents[m.k]) {
 | 
				
			||||||
 | 
					            em.v = {
 | 
				
			||||||
 | 
					                clientHeight: node.clientHeight,
 | 
				
			||||||
 | 
					                clientWidth: node.clientWidth
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        const ems = JSON.stringify (em);
 | 
					        const ems = JSON.stringify (em);
 | 
				
			||||||
        send (ems);
 | 
					        send (ems);
 | 
				
			||||||
        if (debug) console.log ("Event", em);
 | 
					        if (debug) console.log ("Event", em);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,6 +53,11 @@ namespace Ooui
 | 
				
			||||||
            remove => RemoveEventListener ("keyup", value);
 | 
					            remove => RemoveEventListener ("keyup", value);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
 | 
					        public event TargetEventHandler Loaded {
 | 
				
			||||||
 | 
					            add => AddEventListener ("load", value);
 | 
				
			||||||
 | 
					            remove => RemoveEventListener ("load", value);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        public event TargetEventHandler MouseDown {
 | 
					        public event TargetEventHandler MouseDown {
 | 
				
			||||||
            add => AddEventListener ("mousedown", value);
 | 
					            add => AddEventListener ("mousedown", value);
 | 
				
			||||||
            remove => RemoveEventListener ("mousedown", value);
 | 
					            remove => RemoveEventListener ("mousedown", value);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -220,9 +220,17 @@ namespace Ooui
 | 
				
			||||||
            if (handlers != null) {
 | 
					            if (handlers != null) {
 | 
				
			||||||
                var args = new TargetEventArgs ();
 | 
					                var args = new TargetEventArgs ();
 | 
				
			||||||
                if (message.Value is Newtonsoft.Json.Linq.JObject o) {
 | 
					                if (message.Value is Newtonsoft.Json.Linq.JObject o) {
 | 
				
			||||||
 | 
					                    if (o["offsetX"] != null)
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
                        args.OffsetX = (double)o["offsetX"];
 | 
					                        args.OffsetX = (double)o["offsetX"];
 | 
				
			||||||
                        args.OffsetY = (double)o["offsetY"];
 | 
					                        args.OffsetY = (double)o["offsetY"];
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 | 
					                    if (o["clientHeight"] != null)
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        args.ClientHeight = (double)o.GetValue("clientHeight");
 | 
				
			||||||
 | 
					                        args.ClientWidth = (double)o.GetValue("clientWidth");
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                foreach (var h in handlers) {
 | 
					                foreach (var h in handlers) {
 | 
				
			||||||
                    h.Invoke (this, args);
 | 
					                    h.Invoke (this, args);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
| 
						 | 
					@ -257,5 +265,7 @@ namespace Ooui
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public double OffsetX { get; set; }
 | 
					        public double OffsetX { get; set; }
 | 
				
			||||||
        public double OffsetY { get; set; }
 | 
					        public double OffsetY { get; set; }
 | 
				
			||||||
 | 
					        public double ClientHeight { get; set; }
 | 
				
			||||||
 | 
					        public double ClientWidth { get; set; }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue