diff --git a/Xamarin.Forms.Platform.GTK/Controls/BoxView.cs b/Xamarin.Forms.Platform.GTK/Controls/BoxView.cs index 12a6843ee56..9b03f03d4d8 100644 --- a/Xamarin.Forms.Platform.GTK/Controls/BoxView.cs +++ b/Xamarin.Forms.Platform.GTK/Controls/BoxView.cs @@ -5,194 +5,194 @@ namespace Xamarin.Forms.Platform.GTK.Controls { - public class BoxView : VBox - { - private BoxViewDrawingArea _boxView; - - public BoxView() - { - // Custom control created with Cairo. It gives nice possibilities as rounded corners, etc. - _boxView = new BoxViewDrawingArea(); - - Add(_boxView); - } - - public void UpdateColor(Gdk.Color color) - { - if (_boxView != null) - { - _boxView.Color = color; - } - } - - public void ResetColor() - { - UpdateColor(Gdk.Color.Zero); - } - - public void UpdateBackgroundColor(Gdk.Color color) - { - if (_boxView != null) - { - _boxView.ModifyBg(StateType.Normal, color); - } - } - - public void UpdateSize(int height, int width) - { - if (_boxView != null) - { - _boxView.Height = height; - _boxView.Width = width; - } - } - - public void UpdateBorderRadius(int topLeftRadius = 5, int topRightRadius = 5, int bottomLeftRadius = 5, int bottomRightRadius = 5) - { - if (_boxView != null) - { - _boxView.TopLeftRadius = topLeftRadius > 0 ? topLeftRadius : 0; - _boxView.TopRightRadius = topRightRadius > 0 ? topRightRadius : 0; - _boxView.BottomLeftRadius = bottomLeftRadius > 0 ? bottomLeftRadius : 0; - _boxView.BottomRightRadius = bottomRightRadius > 0 ? bottomRightRadius : 0; - } - } - } - - public class BoxViewDrawingArea : DrawingArea - { - private Context _context; - private EventExpose _event; - private Gdk.Color _color; - private int _height; - private int _width; - private int _topLeftRadius, _topRightRadius, _bottomLeftRadius, _bottomRightRadius; - - public BoxViewDrawingArea() - { - QueueResize(); - } - - public Gdk.Color Color - { - get { return (_color); } - set - { - _color = value; - QueueDraw(); - } - } - - public int Height - { - get { return (_height); } - set - { - _height = value; - QueueDraw(); - } - } - public int Width - { - get { return (_width); } - set - { - _width = value; - QueueDraw(); - } - } - - public int TopLeftRadius - { - get { return (_topLeftRadius); } - set - { - _topLeftRadius = value; - QueueDraw(); - } - } - - public int TopRightRadius - { - get { return (_topRightRadius); } - set - { - _topRightRadius = value; - QueueDraw(); - } - } - - public int BottomLeftRadius - { - get { return (_bottomLeftRadius); } - set - { - _bottomLeftRadius = value; - QueueDraw(); - } - } - - public int BottomRightRadius - { - get { return (_bottomRightRadius); } - set - { - _bottomRightRadius = value; - QueueDraw(); - } - } - - protected override bool OnExposeEvent(EventExpose ev) - { - using (var cr = CairoHelper.Create(GdkWindow)) - { - Draw(cr, ev); - } - - return (true); - } - - private void Draw(Context cr, EventExpose ev) - { - _context = cr; - _event = ev; - - DrawBoxView(_context, _event); - } - - private void DrawBoxView(Context cr, EventExpose ev) - { - if (Color.Equal(Gdk.Color.Zero)) return; - - int clipHeight = ev.Area.Height > 0 ? Height : 0; - int clipWidth = ev.Area.Width > 0 ? Width : 0; - - double radius = TopLeftRadius; - int x = 0; - int y = 0; - int width = Width; - int height = Height; - - cr.Rectangle( - radius, - 0, - clipHeight, - clipWidth); - - cr.MoveTo(x, y); - cr.Arc(x + width - TopRightRadius, y + TopRightRadius, TopRightRadius, Math.PI * 1.5, Math.PI * 2); - cr.Arc(x + width - BottomRightRadius, y + height - BottomRightRadius, BottomRightRadius, 0, Math.PI * .5); - cr.Arc(x + BottomLeftRadius, y + height - BottomLeftRadius, BottomLeftRadius, Math.PI * .5, Math.PI); - cr.Arc(x + TopLeftRadius, y + TopLeftRadius, TopLeftRadius, Math.PI, Math.PI * 1.5); - - var cairoColor = new Cairo.Color( - (double)Color.Red / ushort.MaxValue, - (double)Color.Green / ushort.MaxValue, - (double)Color.Blue / ushort.MaxValue); - - cr.SetSourceRGB(cairoColor.R, cairoColor.G, cairoColor.B); - - cr.Fill(); - } - } -} \ No newline at end of file + public class BoxView : VBox + { + private BoxViewDrawingArea _boxView; + + public BoxView() + { + // Custom control created with Cairo. It gives nice possibilities as rounded corners, etc. + _boxView = new BoxViewDrawingArea(); + + Add(_boxView); + } + + public void UpdateColor(Gdk.Color color) + { + if (_boxView != null) + { + _boxView.Color = color; + } + } + + public void ResetColor() + { + UpdateColor(Gdk.Color.Zero); + } + + public void UpdateBackgroundColor(Gdk.Color color) + { + if (_boxView != null) + { + _boxView.ModifyBg(StateType.Normal, color); + } + } + + public void UpdateSize(int height, int width) + { + if (_boxView != null) + { + _boxView.Height = height; + _boxView.Width = width; + } + } + + public void UpdateBorderRadius(int topLeftRadius = 5, int topRightRadius = 5, int bottomLeftRadius = 5, int bottomRightRadius = 5) + { + if (_boxView != null) + { + _boxView.TopLeftRadius = topLeftRadius > 0 ? topLeftRadius : 0; + _boxView.TopRightRadius = topRightRadius > 0 ? topRightRadius : 0; + _boxView.BottomLeftRadius = bottomLeftRadius > 0 ? bottomLeftRadius : 0; + _boxView.BottomRightRadius = bottomRightRadius > 0 ? bottomRightRadius : 0; + } + } + } + + public class BoxViewDrawingArea : DrawingArea + { + private Context _context; + private EventExpose _event; + private Gdk.Color _color; + private int _height; + private int _width; + private int _topLeftRadius, _topRightRadius, _bottomLeftRadius, _bottomRightRadius; + + public BoxViewDrawingArea() + { + QueueResize(); + } + + public Gdk.Color Color + { + get { return (_color); } + set + { + _color = value; + QueueDraw(); + } + } + + public int Height + { + get { return (_height); } + set + { + _height = value; + QueueDraw(); + } + } + public int Width + { + get { return (_width); } + set + { + _width = value; + QueueDraw(); + } + } + + public int TopLeftRadius + { + get { return (_topLeftRadius); } + set + { + _topLeftRadius = value; + QueueDraw(); + } + } + + public int TopRightRadius + { + get { return (_topRightRadius); } + set + { + _topRightRadius = value; + QueueDraw(); + } + } + + public int BottomLeftRadius + { + get { return (_bottomLeftRadius); } + set + { + _bottomLeftRadius = value; + QueueDraw(); + } + } + + public int BottomRightRadius + { + get { return (_bottomRightRadius); } + set + { + _bottomRightRadius = value; + QueueDraw(); + } + } + + protected override bool OnExposeEvent(EventExpose ev) + { + using (var cr = CairoHelper.Create(GdkWindow)) + { + Draw(cr, ev); + } + + return (true); + } + + private void Draw(Context cr, EventExpose ev) + { + _context = cr; + _event = ev; + + DrawBoxView(_context, _event); + } + + private void DrawBoxView(Context cr, EventExpose ev) + { + if (Color.Equal(Gdk.Color.Zero)) return; + + int clipHeight = ev.Area.Height > 0 ? Height : 0; + int clipWidth = ev.Area.Width > 0 ? Width : 0; + + double radius = TopLeftRadius; + int x = 0; + int y = 0; + int width = Width; + int height = Height; + + cr.Rectangle( + radius, + 0, + clipHeight, + clipWidth); + + cr.MoveTo(x, y); + cr.Arc(x + width - TopRightRadius, y + TopRightRadius, TopRightRadius, Math.PI * 1.5, Math.PI * 2); + cr.Arc(x + width - BottomRightRadius, y + height - BottomRightRadius, BottomRightRadius, 0, Math.PI * .5); + cr.Arc(x + BottomLeftRadius, y + height - BottomLeftRadius, BottomLeftRadius, Math.PI * .5, Math.PI); + cr.Arc(x + TopLeftRadius, y + TopLeftRadius, TopLeftRadius, Math.PI, Math.PI * 1.5); + + var cairoColor = new Cairo.Color( + (double)Color.Red / ushort.MaxValue, + (double)Color.Green / ushort.MaxValue, + (double)Color.Blue / ushort.MaxValue); + + cr.SetSourceRGB(cairoColor.R, cairoColor.G, cairoColor.B); + + cr.Fill(); + } + } +} diff --git a/Xamarin.Forms.Platform.GTK/Controls/ScrolledTextView.cs b/Xamarin.Forms.Platform.GTK/Controls/ScrolledTextView.cs index 16773f78340..3a4afec510c 100644 --- a/Xamarin.Forms.Platform.GTK/Controls/ScrolledTextView.cs +++ b/Xamarin.Forms.Platform.GTK/Controls/ScrolledTextView.cs @@ -2,134 +2,134 @@ namespace Xamarin.Forms.Platform.GTK.Controls { - public class ScrolledTextView : EventBox - { - private Table _table; - private ScrolledWindow _scrolledWindow; - private Gtk.Label _placeholder; - private EventBox _placeholderContainer; - private int _maxLength; - - public ScrolledTextView() - { - _table = new Table(1, 1, true); - - TextView = new TextView - { - AcceptsTab = false, - WrapMode = WrapMode.WordChar - }; - - TextView.Buffer.InsertText += InsertText; - TextView.FocusOutEvent += FocusedOut; - - _scrolledWindow = new ScrolledWindow - { - ShadowType = ShadowType.In, - HscrollbarPolicy = PolicyType.Never, - VscrollbarPolicy = PolicyType.Automatic - }; - - _scrolledWindow.Add(TextView); - - _placeholder = new Gtk.Label(); - _placeholder.SetAlignment(0, 0); - - _placeholderContainer = new EventBox - { - BorderWidth = 2 - }; - - _placeholderContainer.Add(_placeholder); - - _placeholderContainer.ButtonPressEvent += PlaceHolderContainerPressed; - - SetBackgroundColor(TextView.Style.BaseColors[(int)StateType.Normal]); - - Add(_table); - - _table.Attach(_placeholderContainer, 0, 1, 0, 1, AttachOptions.Fill, AttachOptions.Fill, 0, 0); - _table.Attach(_scrolledWindow, 0, 1, 0, 1); - } - - public TextView TextView { get; } - - public string PlaceholderText - { - get - { - return _placeholder.Text; - } - set - { - _placeholder.Text = GLib.Markup.EscapeText(value ?? string.Empty); - } - } - - public void SetBackgroundColor(Gdk.Color color) - { - ModifyBg(StateType.Normal, color); - TextView.ModifyBase(StateType.Normal, color); - _placeholderContainer.ModifyBg(StateType.Normal, color); - } - - public void SetPlaceholderTextColor(Gdk.Color color) - { - _placeholder.ModifyFg(StateType.Normal, color); - } - - public void SetMaxLength(int maxLength) - { - _maxLength = maxLength; - - if (TextView.Buffer.CharCount > maxLength) - TextView.Buffer.Text = TextView.Buffer.Text.Substring(0, maxLength); - } - - protected override void OnFocusGrabbed() - { - TextView?.GrabFocus(); - } - - protected override void OnSizeAllocated(Gdk.Rectangle allocation) - { - base.OnSizeAllocated(allocation); - - TextView.SetSizeRequest(allocation.Width, allocation.Height); - - ShowPlaceholderIfNeeded(); - } - - private void InsertText(object o, InsertTextArgs args) - { - args.RetVal = args.Length <= _maxLength; - } - - private void FocusedOut(object o, FocusOutEventArgs args) - { - ShowPlaceholderIfNeeded(); - } - - private void ShowPlaceholderIfNeeded() - { - if (string.IsNullOrEmpty(TextView.Buffer.Text) && !string.IsNullOrEmpty(_placeholder.Text)) - { - _placeholderContainer.VisibleWindow = true; - } - else - { - _placeholderContainer.VisibleWindow = false; - } - } - - private void PlaceHolderContainerPressed(object o, ButtonPressEventArgs args) - { - if (Sensitive) - { - TextView.Sensitive = true; - TextView.HasFocus = true; - TextView.GdkWindow?.Raise(); - } - } - } -} \ No newline at end of file + public class ScrolledTextView : EventBox + { + private Table _table; + private ScrolledWindow _scrolledWindow; + private Gtk.Label _placeholder; + private EventBox _placeholderContainer; + private int _maxLength; + + public ScrolledTextView() + { + _table = new Table(1, 1, true); + + TextView = new TextView + { + AcceptsTab = false, + WrapMode = WrapMode.WordChar + }; + + TextView.Buffer.InsertText += InsertText; + TextView.FocusOutEvent += FocusedOut; + + _scrolledWindow = new ScrolledWindow + { + ShadowType = ShadowType.In, + HscrollbarPolicy = PolicyType.Never, + VscrollbarPolicy = PolicyType.Automatic + }; + + _scrolledWindow.Add(TextView); + + _placeholder = new Gtk.Label(); + _placeholder.SetAlignment(0, 0); + + _placeholderContainer = new EventBox + { + BorderWidth = 2 + }; + + _placeholderContainer.Add(_placeholder); + + _placeholderContainer.ButtonPressEvent += PlaceHolderContainerPressed; + + SetBackgroundColor(TextView.Style.BaseColors[(int)StateType.Normal]); + + Add(_table); + + _table.Attach(_placeholderContainer, 0, 1, 0, 1, AttachOptions.Fill, AttachOptions.Fill, 0, 0); + _table.Attach(_scrolledWindow, 0, 1, 0, 1); + } + + public TextView TextView { get; } + + public string PlaceholderText + { + get + { + return _placeholder.Text; + } + set + { + _placeholder.Text = GLib.Markup.EscapeText(value ?? string.Empty); + } + } + + public void SetBackgroundColor(Gdk.Color color) + { + ModifyBg(StateType.Normal, color); + TextView.ModifyBase(StateType.Normal, color); + _placeholderContainer.ModifyBg(StateType.Normal, color); + } + + public void SetPlaceholderTextColor(Gdk.Color color) + { + _placeholder.ModifyFg(StateType.Normal, color); + } + + public void SetMaxLength(int maxLength) + { + _maxLength = maxLength; + + if (TextView.Buffer.CharCount > maxLength) + TextView.Buffer.Text = TextView.Buffer.Text.Substring(0, maxLength); + } + + protected override void OnFocusGrabbed() + { + TextView?.GrabFocus(); + } + + protected override void OnSizeAllocated(Gdk.Rectangle allocation) + { + base.OnSizeAllocated(allocation); + + TextView.SetSizeRequest(allocation.Width, allocation.Height); + + ShowPlaceholderIfNeeded(); + } + + private void InsertText(object o, InsertTextArgs args) + { + args.RetVal = args.Length <= _maxLength; + } + + private void FocusedOut(object o, FocusOutEventArgs args) + { + ShowPlaceholderIfNeeded(); + } + + private void ShowPlaceholderIfNeeded() + { + if (string.IsNullOrEmpty(TextView.Buffer.Text) && !string.IsNullOrEmpty(_placeholder.Text)) + { + _placeholderContainer.VisibleWindow = true; + } + else + { + _placeholderContainer.VisibleWindow = false; + } + } + + private void PlaceHolderContainerPressed(object o, ButtonPressEventArgs args) + { + if (Sensitive) + { + TextView.Sensitive = true; + TextView.HasFocus = true; + TextView.GdkWindow?.Raise(); + } + } + } +} diff --git a/Xamarin.Forms.Platform.GTK/FormsWindow.cs b/Xamarin.Forms.Platform.GTK/FormsWindow.cs index ddc7b3ad988..32d5a65cc83 100644 --- a/Xamarin.Forms.Platform.GTK/FormsWindow.cs +++ b/Xamarin.Forms.Platform.GTK/FormsWindow.cs @@ -5,145 +5,145 @@ namespace Xamarin.Forms.Platform.GTK { - public class FormsWindow : Window - { - private Application _application; - private Gdk.Size _lastSize; + public class FormsWindow : Window + { + private Application _application; + private Gdk.Size _lastSize; - public FormsWindow () - : base (WindowType.Toplevel) - { - SetDefaultSize (800, 600); - SetSizeRequest (400, 400); + public FormsWindow () + : base (WindowType.Toplevel) + { + SetDefaultSize (800, 600); + SetSizeRequest (400, 400); - MainThreadID = Thread.CurrentThread.ManagedThreadId; - MainWindow = this; + MainThreadID = Thread.CurrentThread.ManagedThreadId; + MainWindow = this; - if (SynchronizationContext.Current == null) - SynchronizationContext.SetSynchronizationContext (new GtkSynchronizationContext ()); + if (SynchronizationContext.Current == null) + SynchronizationContext.SetSynchronizationContext (new GtkSynchronizationContext ()); - WindowStateEvent += OnWindowStateEvent; - } - - public static int MainThreadID { get; set; } - public static Window MainWindow { get; set; } + WindowStateEvent += OnWindowStateEvent; + } + + public static int MainThreadID { get; set; } + public static Window MainWindow { get; set; } - public void LoadApplication (Application application) - { - if (application == null) - throw new ArgumentNullException (nameof (application)); - - Application.SetCurrentApplication (application); - _application = application; - - application.PropertyChanged += ApplicationOnPropertyChanged; - UpdateMainPage (); - - _application.SendStart (); - } - - public void SetApplicationTitle (string title) - { - if (string.IsNullOrEmpty (title)) - return; - - Title = title; - } - - public void SetApplicationIcon (string icon) - { - if (string.IsNullOrEmpty (icon)) - return; - - var appliccationIconPixbuf = new Gdk.Pixbuf(icon); - Icon = appliccationIconPixbuf; - } - - public sealed override void Dispose () - { - base.Dispose (); - - Dispose (true); - } - - protected override bool OnDeleteEvent (Gdk.Event evnt) - { - base.OnDeleteEvent (evnt); - - Gtk.Application.Quit (); - - return true; - } - - private void ApplicationOnPropertyChanged (object sender, PropertyChangedEventArgs args) - { - if (args.PropertyName == nameof (Application.MainPage)) { - UpdateMainPage (); - } - } - - protected override bool OnConfigureEvent (Gdk.EventConfigure evnt) - { - Gdk.Size newSize = new Gdk.Size(evnt.Width, evnt.Height); - - if (_lastSize != newSize) { - _lastSize = newSize; - var pageRenderer = Platform.GetRenderer(_application.MainPage); - pageRenderer?.SetElementSize (new Size (newSize.Width, newSize.Height)); - } - - return base.OnConfigureEvent (evnt); - } - - private void UpdateMainPage () - { - if (_application.MainPage == null) - return; - - var platformRenderer = Child as PlatformRenderer; - - if (platformRenderer != null) { - RemoveChildIfExists (); - ((IDisposable)platformRenderer.Platform).Dispose (); - } - - var platform = new Platform(); - platform.PlatformRenderer.SetSizeRequest (WidthRequest, HeightRequest); - Add (platform.PlatformRenderer); - platform.SetPage (_application.MainPage); - - Child.ShowAll (); - } - - private void RemoveChildIfExists () - { - foreach (var child in Children) { - var widget = child as Widget; - - if (widget != null) { - Remove (widget); - } - } - } - - private void OnWindowStateEvent (object o, WindowStateEventArgs args) - { - if (args.Event.ChangedMask == Gdk.WindowState.Iconified) { - var windowState = args.Event.NewWindowState; - - if (windowState == Gdk.WindowState.Iconified) - _application.SendSleep(); - else - _application.SendResume (); - } - } - - private void Dispose (bool disposing) - { - if (disposing && _application != null) { - WindowStateEvent -= OnWindowStateEvent; - _application.PropertyChanged -= ApplicationOnPropertyChanged; - } - } - } -} \ No newline at end of file + public void LoadApplication (Application application) + { + if (application == null) + throw new ArgumentNullException (nameof (application)); + + Application.SetCurrentApplication (application); + _application = application; + + application.PropertyChanged += ApplicationOnPropertyChanged; + UpdateMainPage (); + + _application.SendStart (); + } + + public void SetApplicationTitle (string title) + { + if (string.IsNullOrEmpty (title)) + return; + + Title = title; + } + + public void SetApplicationIcon (string icon) + { + if (string.IsNullOrEmpty (icon)) + return; + + var appliccationIconPixbuf = new Gdk.Pixbuf(icon); + Icon = appliccationIconPixbuf; + } + + public sealed override void Dispose () + { + base.Dispose (); + + Dispose (true); + } + + protected override bool OnDeleteEvent (Gdk.Event evnt) + { + base.OnDeleteEvent (evnt); + + Gtk.Application.Quit (); + + return true; + } + + private void ApplicationOnPropertyChanged (object sender, PropertyChangedEventArgs args) + { + if (args.PropertyName == nameof (Application.MainPage)) { + UpdateMainPage (); + } + } + + protected override bool OnConfigureEvent (Gdk.EventConfigure evnt) + { + Gdk.Size newSize = new Gdk.Size(evnt.Width, evnt.Height); + + if (_lastSize != newSize) { + _lastSize = newSize; + var pageRenderer = Platform.GetRenderer(_application.MainPage); + pageRenderer?.SetElementSize (new Size (newSize.Width, newSize.Height)); + } + + return base.OnConfigureEvent (evnt); + } + + private void UpdateMainPage () + { + if (_application.MainPage == null) + return; + + var platformRenderer = Child as PlatformRenderer; + + if (platformRenderer != null) { + RemoveChildIfExists (); + ((IDisposable)platformRenderer.Platform).Dispose (); + } + + var platform = new Platform(); + platform.PlatformRenderer.SetSizeRequest (WidthRequest, HeightRequest); + Add (platform.PlatformRenderer); + platform.SetPage (_application.MainPage); + + Child.ShowAll (); + } + + private void RemoveChildIfExists () + { + foreach (var child in Children) { + var widget = child as Widget; + + if (widget != null) { + Remove (widget); + } + } + } + + private void OnWindowStateEvent (object o, WindowStateEventArgs args) + { + if (args.Event.ChangedMask == Gdk.WindowState.Iconified) { + var windowState = args.Event.NewWindowState; + + if (windowState == Gdk.WindowState.Iconified) + _application.SendSleep(); + else + _application.SendResume (); + } + } + + private void Dispose (bool disposing) + { + if (disposing && _application != null) { + WindowStateEvent -= OnWindowStateEvent; + _application.PropertyChanged -= ApplicationOnPropertyChanged; + } + } + } +} diff --git a/Xamarin.Forms.Platform.GTK/GtkPlatformServices.cs b/Xamarin.Forms.Platform.GTK/GtkPlatformServices.cs index b4cf7001681..1e91240c9bb 100644 --- a/Xamarin.Forms.Platform.GTK/GtkPlatformServices.cs +++ b/Xamarin.Forms.Platform.GTK/GtkPlatformServices.cs @@ -106,9 +106,9 @@ private static int Hex(int v) return 'a' + v - 10; } - public void QuitApplication() - { - Gtk.Application.Quit(); - } - } -} \ No newline at end of file + public void QuitApplication() + { + Gtk.Application.Quit(); + } + } +} diff --git a/Xamarin.Forms.Platform.GTK/GtkToolbarTracker.cs b/Xamarin.Forms.Platform.GTK/GtkToolbarTracker.cs index 22379691e00..03c50918562 100644 --- a/Xamarin.Forms.Platform.GTK/GtkToolbarTracker.cs +++ b/Xamarin.Forms.Platform.GTK/GtkToolbarTracker.cs @@ -236,14 +236,14 @@ private void UpdateItems(IList toolBarItems) ToolButton secondaryButton = ToolButtonHelper.CreateToolButton(Stock.Add); _toolbarSection.PackStart(secondaryButton, false, false, 0); - Gtk.Menu menu = new Gtk.Menu(); + Gtk.Menu menu = new Gtk.Menu(); foreach (var secondaryToolBarItem in secondaryToolBarItems) { - Gtk.MenuItem menuItem = new Gtk.MenuItem(secondaryToolBarItem.Text) - { - Sensitive = secondaryToolBarItem.IsEnabled - }; - menu.Add(menuItem); + Gtk.MenuItem menuItem = new Gtk.MenuItem(secondaryToolBarItem.Text) + { + Sensitive = secondaryToolBarItem.IsEnabled + }; + menu.Add(menuItem); menuItem.ButtonPressEvent += (sender, args) => { diff --git a/Xamarin.Forms.Platform.GTK/Renderers/AbstractPageRenderer.cs b/Xamarin.Forms.Platform.GTK/Renderers/AbstractPageRenderer.cs index ec1277d1c55..f67d7ea8b4d 100644 --- a/Xamarin.Forms.Platform.GTK/Renderers/AbstractPageRenderer.cs +++ b/Xamarin.Forms.Platform.GTK/Renderers/AbstractPageRenderer.cs @@ -83,33 +83,33 @@ public SizeRequest GetDesiredSize(double widthConstraint, double heightConstrain return Container.GetDesiredSize(widthConstraint, heightConstraint); } - public override void Dispose() - { - base.Dispose(); - - if (!_disposed) - { - if (_appeared) - { - ReadOnlyCollection children = ((IElementController)Element).LogicalChildren; - for (var i = 0; i < children.Count; i++) - { - var visualChild = children[i] as VisualElement; - visualChild?.Cleanup(); - } + public override void Dispose() + { + base.Dispose(); - Page.SendDisappearing(); - } + if (!_disposed) + { + if (_appeared) + { + ReadOnlyCollection children = ((IElementController)Element).LogicalChildren; + for (var i = 0; i < children.Count; i++) + { + var visualChild = children[i] as VisualElement; + visualChild?.Cleanup(); + } + + Page.SendDisappearing(); + } - _appeared = false; + _appeared = false; - Dispose(true); + Dispose(true); - _disposed = true; - } - } + _disposed = true; + } + } - protected override void OnShown() + protected override void OnShown() { base.OnShown(); @@ -133,7 +133,7 @@ protected override void OnDestroyed() _appeared = false; - PageController.SendDisappearing(); + PageController.SendDisappearing(); } protected override void OnSizeAllocated(Gdk.Rectangle allocation) @@ -256,9 +256,9 @@ private bool HasAncestorNavigationPage(TPage page) current = parent; } - var hasAncestorNavigationPage = hasParentNavigation && NavigationPage.GetHasNavigationBar(current); + var hasAncestorNavigationPage = hasParentNavigation && NavigationPage.GetHasNavigationBar(current); - return hasAncestorNavigationPage; - } - } -} \ No newline at end of file + return hasAncestorNavigationPage; + } + } +} diff --git a/Xamarin.Forms.Platform.GTK/Renderers/BoxViewRenderer.cs b/Xamarin.Forms.Platform.GTK/Renderers/BoxViewRenderer.cs index 552eb9cf0ed..3bd2f838e45 100644 --- a/Xamarin.Forms.Platform.GTK/Renderers/BoxViewRenderer.cs +++ b/Xamarin.Forms.Platform.GTK/Renderers/BoxViewRenderer.cs @@ -4,93 +4,93 @@ namespace Xamarin.Forms.Platform.GTK.Renderers { - public class BoxViewRenderer : ViewRenderer - { - protected override void OnElementChanged(ElementChangedEventArgs e) - { - if (e.NewElement != null) - { - if (Control == null) - { - SetNativeControl(new Controls.BoxView()); - } - - SetColor(Element.Color); - SetCornerRadius(Element.CornerRadius); - SetSize(); - } - - base.OnElementChanged(e); - } - - protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) - { - base.OnElementPropertyChanged(sender, e); - - if (e.PropertyName == BoxView.ColorProperty.PropertyName) - SetColor(Element.Color); - else if (e.PropertyName == BoxView.CornerRadiusProperty.PropertyName) - SetCornerRadius(Element.CornerRadius); - else if (e.PropertyName == - PlatformConfiguration.GTKSpecific.BoxView.HasCornerRadiusProperty.PropertyName) - SetHasCornerRadius(); - } - - protected override void OnSizeAllocated(Gdk.Rectangle allocation) - { - SetSize(); - - base.OnSizeAllocated(allocation); - } - - protected override void UpdateBackgroundColor() - { - base.UpdateBackgroundColor(); - - var backgroundColor = Element.BackgroundColor == Color.Default ? Color.Transparent.ToGtkColor() : Element.BackgroundColor.ToGtkColor(); - - Control.UpdateBackgroundColor(backgroundColor); - - Container.VisibleWindow = true; - } - - private void SetColor(Color color) - { - if (Element == null || Control == null) - return; - - if (color.IsDefaultOrTransparent()) - { - Control.ResetColor(); - } - else - { - var backgroundColor = color.ToGtkColor(); - Control.UpdateColor(backgroundColor); - } - } - - private void SetCornerRadius(CornerRadius cornerRadius) - { - if (Element == null || Control == null) - return; - - Control.UpdateBorderRadius((int)cornerRadius.TopLeft, (int)cornerRadius.TopRight, (int)cornerRadius.BottomLeft, (int)cornerRadius.BottomRight); - } - - private void SetSize() - { - int height = HeightRequest; - int width = WidthRequest; - - Control.UpdateSize(height, width); - } - - private void SetHasCornerRadius() - { - var hasCornerRadius = Element.OnThisPlatform().GetHasCornerRadius(); - - Control.UpdateBorderRadius(); - } - } -} \ No newline at end of file + public class BoxViewRenderer : ViewRenderer + { + protected override void OnElementChanged(ElementChangedEventArgs e) + { + if (e.NewElement != null) + { + if (Control == null) + { + SetNativeControl(new Controls.BoxView()); + } + + SetColor(Element.Color); + SetCornerRadius(Element.CornerRadius); + SetSize(); + } + + base.OnElementChanged(e); + } + + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + base.OnElementPropertyChanged(sender, e); + + if (e.PropertyName == BoxView.ColorProperty.PropertyName) + SetColor(Element.Color); + else if (e.PropertyName == BoxView.CornerRadiusProperty.PropertyName) + SetCornerRadius(Element.CornerRadius); + else if (e.PropertyName == + PlatformConfiguration.GTKSpecific.BoxView.HasCornerRadiusProperty.PropertyName) + SetHasCornerRadius(); + } + + protected override void OnSizeAllocated(Gdk.Rectangle allocation) + { + SetSize(); + + base.OnSizeAllocated(allocation); + } + + protected override void UpdateBackgroundColor() + { + base.UpdateBackgroundColor(); + + var backgroundColor = Element.BackgroundColor == Color.Default ? Color.Transparent.ToGtkColor() : Element.BackgroundColor.ToGtkColor(); + + Control.UpdateBackgroundColor(backgroundColor); + + Container.VisibleWindow = true; + } + + private void SetColor(Color color) + { + if (Element == null || Control == null) + return; + + if (color.IsDefaultOrTransparent()) + { + Control.ResetColor(); + } + else + { + var backgroundColor = color.ToGtkColor(); + Control.UpdateColor(backgroundColor); + } + } + + private void SetCornerRadius(CornerRadius cornerRadius) + { + if (Element == null || Control == null) + return; + + Control.UpdateBorderRadius((int)cornerRadius.TopLeft, (int)cornerRadius.TopRight, (int)cornerRadius.BottomLeft, (int)cornerRadius.BottomRight); + } + + private void SetSize() + { + int height = HeightRequest; + int width = WidthRequest; + + Control.UpdateSize(height, width); + } + + private void SetHasCornerRadius() + { + var hasCornerRadius = Element.OnThisPlatform().GetHasCornerRadius(); + + Control.UpdateBorderRadius(); + } + } +} diff --git a/Xamarin.Forms.Platform.GTK/Renderers/EditorRenderer.cs b/Xamarin.Forms.Platform.GTK/Renderers/EditorRenderer.cs index 6567c0f2fef..7541822b046 100644 --- a/Xamarin.Forms.Platform.GTK/Renderers/EditorRenderer.cs +++ b/Xamarin.Forms.Platform.GTK/Renderers/EditorRenderer.cs @@ -8,182 +8,182 @@ namespace Xamarin.Forms.Platform.GTK.Renderers { - public class EditorRenderer : ViewRenderer - { - private const string TextColorTagName = "text-color"; - - private bool _disposed; - - protected IEditorController EditorController => Element as IEditorController; - - protected override void UpdateBackgroundColor() - { - if (!Element.BackgroundColor.IsDefaultOrTransparent()) - { - var backgroundColor = Element.BackgroundColor.ToGtkColor(); - - Control.SetBackgroundColor(backgroundColor); - } - } - - protected override void OnElementChanged(ElementChangedEventArgs e) - { - if (Control == null) - { - var scrolled = new ScrolledTextView(); - - scrolled.TextView.Buffer.TagTable.Add(new TextTag(TextColorTagName)); - - scrolled.TextView.Buffer.Changed += TextViewBufferChanged; - scrolled.TextView.Focused += TextViewFocused; - scrolled.TextView.FocusOutEvent += TextViewFocusedOut; - - SetNativeControl(scrolled); - AdjustMinimumHeight(scrolled.TextView); - } - - if (e.NewElement != null) - { - UpdateText(); - UpdateFont(); - UpdateTextColor(); - UpdatePlaceholder(); - UpdatePlaceholderColor(); - UpdateMaxLength(); - } - - 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.PlaceholderProperty.PropertyName) - UpdatePlaceholder(); - else if (e.PropertyName == Editor.PlaceholderColorProperty.PropertyName) - UpdatePlaceholderColor(); - else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName) - UpdateFont(); - else if (e.PropertyName == Editor.FontFamilyProperty.PropertyName) - UpdateFont(); - else if (e.PropertyName == Editor.FontSizeProperty.PropertyName) - UpdateFont(); - else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName) - UpdateMaxLength(); - } - - protected override void Dispose(bool disposing) - { - if (disposing && !_disposed) - { - _disposed = true; - - if (Control != null) - { - Control.TextView.Buffer.Changed -= TextViewBufferChanged; - Control.TextView.Focused -= TextViewFocused; - Control.TextView.FocusOutEvent += TextViewFocusedOut; - } - } - - base.Dispose(disposing); - } - - private void UpdateText() - { - TextBuffer buffer = Control.TextView.Buffer; - - if (buffer.Text != Element.Text) - { - buffer.Text = Element.Text ?? string.Empty; - UpdateTextColor(); - } - } - - private void UpdateFont() - { - FontDescription fontDescription = FontDescriptionHelper.CreateFontDescription( - Element.FontSize, Element.FontFamily, Element.FontAttributes); - Control.TextView.ModifyFont(fontDescription); - - AdjustMinimumHeight(Control.TextView, fontDescription); - } - - private void UpdateTextColor() - { - if (!Element.TextColor.IsDefaultOrTransparent()) - { - var textColor = Element.TextColor.ToGtkColor(); - - TextBuffer buffer = Control.TextView.Buffer; - TextTag tag = buffer.TagTable.Lookup(TextColorTagName); - tag.ForegroundGdk = Element.IsEnabled ? textColor : Control.Style.Foregrounds[(int)StateType.Normal]; - Control.TextView.Buffer.ApplyTag(tag, buffer.StartIter, buffer.EndIter); - } - } - - private void TextViewBufferChanged(object sender, EventArgs e) - { - TextBuffer buffer = Control.TextView.Buffer; - - if (Element.Text != buffer.Text) - ElementController.SetValueFromRenderer(Editor.TextProperty, buffer.Text); - - UpdateTextColor(); - } - - private void TextViewFocused(object o, FocusedArgs args) - { - ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true); - } - - private void TextViewFocusedOut(object o, FocusOutEventArgs args) - { - ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false); - EditorController.SendCompleted(); - } - - private static void AdjustMinimumHeight(TextView textView, FontDescription font = null) - { - var fDescr = font != null ? font : textView.Style.FontDescription; - var metrics = textView.PangoContext.GetMetrics(font, Language.Default); - var pangoUnits = (metrics.Ascent + metrics.Descent) / Pango.Scale.PangoScale; - - var resolution = textView.Screen.Resolution; - var minHeight = (int)(pangoUnits * (resolution / 72.0)); - - if (textView.HeightRequest < minHeight) - { - textView.HeightRequest = minHeight; - } - } - - private void UpdateMaxLength() - { - Control.SetMaxLength(Element.MaxLength); - } - - private void UpdatePlaceholder() - { - if (Element.Placeholder != Control.PlaceholderText) - { - Control.PlaceholderText = Element.Placeholder; - } - } - - private void UpdatePlaceholderColor() - { - if (!Element.PlaceholderColor.IsDefaultOrTransparent()) - { - var placeholderColor = Element.PlaceholderColor.ToGtkColor(); - - Control.SetPlaceholderTextColor(placeholderColor); - } - } - } -} \ No newline at end of file + public class EditorRenderer : ViewRenderer + { + private const string TextColorTagName = "text-color"; + + private bool _disposed; + + protected IEditorController EditorController => Element as IEditorController; + + protected override void UpdateBackgroundColor() + { + if (!Element.BackgroundColor.IsDefaultOrTransparent()) + { + var backgroundColor = Element.BackgroundColor.ToGtkColor(); + + Control.SetBackgroundColor(backgroundColor); + } + } + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + if (Control == null) + { + var scrolled = new ScrolledTextView(); + + scrolled.TextView.Buffer.TagTable.Add(new TextTag(TextColorTagName)); + + scrolled.TextView.Buffer.Changed += TextViewBufferChanged; + scrolled.TextView.Focused += TextViewFocused; + scrolled.TextView.FocusOutEvent += TextViewFocusedOut; + + SetNativeControl(scrolled); + AdjustMinimumHeight(scrolled.TextView); + } + + if (e.NewElement != null) + { + UpdateText(); + UpdateFont(); + UpdateTextColor(); + UpdatePlaceholder(); + UpdatePlaceholderColor(); + UpdateMaxLength(); + } + + 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.PlaceholderProperty.PropertyName) + UpdatePlaceholder(); + else if (e.PropertyName == Editor.PlaceholderColorProperty.PropertyName) + UpdatePlaceholderColor(); + else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName) + UpdateFont(); + else if (e.PropertyName == Editor.FontFamilyProperty.PropertyName) + UpdateFont(); + else if (e.PropertyName == Editor.FontSizeProperty.PropertyName) + UpdateFont(); + else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName) + UpdateMaxLength(); + } + + protected override void Dispose(bool disposing) + { + if (disposing && !_disposed) + { + _disposed = true; + + if (Control != null) + { + Control.TextView.Buffer.Changed -= TextViewBufferChanged; + Control.TextView.Focused -= TextViewFocused; + Control.TextView.FocusOutEvent += TextViewFocusedOut; + } + } + + base.Dispose(disposing); + } + + private void UpdateText() + { + TextBuffer buffer = Control.TextView.Buffer; + + if (buffer.Text != Element.Text) + { + buffer.Text = Element.Text ?? string.Empty; + UpdateTextColor(); + } + } + + private void UpdateFont() + { + FontDescription fontDescription = FontDescriptionHelper.CreateFontDescription( + Element.FontSize, Element.FontFamily, Element.FontAttributes); + Control.TextView.ModifyFont(fontDescription); + + AdjustMinimumHeight(Control.TextView, fontDescription); + } + + private void UpdateTextColor() + { + if (!Element.TextColor.IsDefaultOrTransparent()) + { + var textColor = Element.TextColor.ToGtkColor(); + + TextBuffer buffer = Control.TextView.Buffer; + TextTag tag = buffer.TagTable.Lookup(TextColorTagName); + tag.ForegroundGdk = Element.IsEnabled ? textColor : Control.Style.Foregrounds[(int)StateType.Normal]; + Control.TextView.Buffer.ApplyTag(tag, buffer.StartIter, buffer.EndIter); + } + } + + private void TextViewBufferChanged(object sender, EventArgs e) + { + TextBuffer buffer = Control.TextView.Buffer; + + if (Element.Text != buffer.Text) + ElementController.SetValueFromRenderer(Editor.TextProperty, buffer.Text); + + UpdateTextColor(); + } + + private void TextViewFocused(object o, FocusedArgs args) + { + ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true); + } + + private void TextViewFocusedOut(object o, FocusOutEventArgs args) + { + ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false); + EditorController.SendCompleted(); + } + + private static void AdjustMinimumHeight(TextView textView, FontDescription font = null) + { + var fDescr = font != null ? font : textView.Style.FontDescription; + var metrics = textView.PangoContext.GetMetrics(font, Language.Default); + var pangoUnits = (metrics.Ascent + metrics.Descent) / Pango.Scale.PangoScale; + + var resolution = textView.Screen.Resolution; + var minHeight = (int)(pangoUnits * (resolution / 72.0)); + + if (textView.HeightRequest < minHeight) + { + textView.HeightRequest = minHeight; + } + } + + private void UpdateMaxLength() + { + Control.SetMaxLength(Element.MaxLength); + } + + private void UpdatePlaceholder() + { + if (Element.Placeholder != Control.PlaceholderText) + { + Control.PlaceholderText = Element.Placeholder; + } + } + + private void UpdatePlaceholderColor() + { + if (!Element.PlaceholderColor.IsDefaultOrTransparent()) + { + var placeholderColor = Element.PlaceholderColor.ToGtkColor(); + + Control.SetPlaceholderTextColor(placeholderColor); + } + } + } +} diff --git a/Xamarin.Forms.Platform.GTK/Renderers/EntryRenderer.cs b/Xamarin.Forms.Platform.GTK/Renderers/EntryRenderer.cs index 1a329483243..41a6fce4d77 100644 --- a/Xamarin.Forms.Platform.GTK/Renderers/EntryRenderer.cs +++ b/Xamarin.Forms.Platform.GTK/Renderers/EntryRenderer.cs @@ -7,158 +7,158 @@ namespace Xamarin.Forms.Platform.GTK.Renderers { - public class EntryRenderer : ViewRenderer - { - private bool _disposed; - - IEntryController EntryController => Element; - - protected override void OnElementChanged(ElementChangedEventArgs e) - { - if (Control == null) - { - // Custom control. The control is composed of a Gtk.Entry and a Gtk.Label to allow to display Placeholder. - var wrapper = new EntryWrapper(); - SetNativeControl(wrapper); - - wrapper.Entry.Changed += OnChanged; - wrapper.Entry.Focused += OnFocused; - wrapper.Entry.EditingDone += OnEditingDone; - wrapper.Entry.KeyReleaseEvent += OnKeyReleased; - } - - if (e.NewElement != null) - { - UpdateText(); - UpdateColor(); - UpdateAlignment(); - UpdateFont(); - UpdateTextVisibility(); - UpdatePlaceholder(); - UpdateMaxLength(); - } - - base.OnElementChanged(e); - } - - protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) - { - if (e.PropertyName == Entry.TextProperty.PropertyName) - UpdateText(); - else if (e.PropertyName == Entry.TextColorProperty.PropertyName) - UpdateColor(); - else if (e.PropertyName == Entry.HorizontalTextAlignmentProperty.PropertyName) - UpdateAlignment(); - else if (e.PropertyName == Entry.FontAttributesProperty.PropertyName) - UpdateFont(); - else if (e.PropertyName == Entry.FontFamilyProperty.PropertyName) - UpdateFont(); - else if (e.PropertyName == Entry.FontSizeProperty.PropertyName) - UpdateFont(); - else if (e.PropertyName == Entry.IsPasswordProperty.PropertyName) - UpdateTextVisibility(); - else if (e.PropertyName == Entry.PlaceholderProperty.PropertyName) - UpdatePlaceholder(); - else if (e.PropertyName == Entry.PlaceholderColorProperty.PropertyName) - UpdatePlaceholder(); - else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName) - UpdateMaxLength(); - - base.OnElementPropertyChanged(sender, e); - } - - protected override void Dispose(bool disposing) - { - if (disposing && !_disposed) - { - _disposed = true; - if (Control != null) - { - Control.Entry.Changed -= OnChanged; - Control.Entry.Focused -= OnFocused; - Control.Entry.EditingDone -= OnEditingDone; - Control.Entry.KeyReleaseEvent -= OnKeyReleased; - } - } - - base.Dispose(disposing); - } - - protected override void UpdateBackgroundColor() - { - if (!Element.BackgroundColor.IsDefaultOrTransparent()) - { - Control.SetBackgroundColor(Element.BackgroundColor.ToGtkColor()); - } - else - { - Control.SetBackgroundColor(Color.Transparent.ToGtkColor()); - } - } - - private void UpdateText() - { - if (Control.Entry.Text != Element.Text) - Control.Entry.Text = Element.Text ?? string.Empty; - } - - private void UpdateColor() - { - var textColor = Element.TextColor; - - Control.SetTextColor(textColor.ToGtkColor()); - } - - private void UpdateAlignment() - { - Control.SetAlignment(Element.HorizontalTextAlignment.ToNativeValue()); - } - - private void UpdateFont() - { - FontDescription fontDescription = FontDescriptionHelper.CreateFontDescription( - Element.FontSize, Element.FontFamily, Element.FontAttributes); - Control.SetFont(fontDescription); - } - - private void UpdateTextVisibility() - { - Control.Entry.Visibility = !Element.IsPassword; - } - - private void UpdatePlaceholder() - { - Control.PlaceholderText = Element.Placeholder ?? string.Empty; - Control.SetPlaceholderTextColor(Element.PlaceholderColor.ToGtkColor()); - } - - private void OnChanged(object sender, System.EventArgs e) - { - ElementController.SetValueFromRenderer(Entry.TextProperty, Control.Entry.Text); - } - - private void OnFocused(object o, FocusedArgs args) - { - ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true); - } - - private void OnEditingDone(object sender, System.EventArgs e) - { - ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false); - EntryController?.SendCompleted(); - } - - private void OnKeyReleased(object o, KeyReleaseEventArgs args) - { - if (args.Event.Key == Gdk.Key.Return) - { - EntryController.SendCompleted(); - } - } - - private void UpdateMaxLength() - { - Control.SetMaxLength(Element.MaxLength); - } - } -} \ No newline at end of file + public class EntryRenderer : ViewRenderer + { + private bool _disposed; + + IEntryController EntryController => Element; + + protected override void OnElementChanged(ElementChangedEventArgs e) + { + if (Control == null) + { + // Custom control. The control is composed of a Gtk.Entry and a Gtk.Label to allow to display Placeholder. + var wrapper = new EntryWrapper(); + SetNativeControl(wrapper); + + wrapper.Entry.Changed += OnChanged; + wrapper.Entry.Focused += OnFocused; + wrapper.Entry.EditingDone += OnEditingDone; + wrapper.Entry.KeyReleaseEvent += OnKeyReleased; + } + + if (e.NewElement != null) + { + UpdateText(); + UpdateColor(); + UpdateAlignment(); + UpdateFont(); + UpdateTextVisibility(); + UpdatePlaceholder(); + UpdateMaxLength(); + } + + base.OnElementChanged(e); + } + + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == Entry.TextProperty.PropertyName) + UpdateText(); + else if (e.PropertyName == Entry.TextColorProperty.PropertyName) + UpdateColor(); + else if (e.PropertyName == Entry.HorizontalTextAlignmentProperty.PropertyName) + UpdateAlignment(); + else if (e.PropertyName == Entry.FontAttributesProperty.PropertyName) + UpdateFont(); + else if (e.PropertyName == Entry.FontFamilyProperty.PropertyName) + UpdateFont(); + else if (e.PropertyName == Entry.FontSizeProperty.PropertyName) + UpdateFont(); + else if (e.PropertyName == Entry.IsPasswordProperty.PropertyName) + UpdateTextVisibility(); + else if (e.PropertyName == Entry.PlaceholderProperty.PropertyName) + UpdatePlaceholder(); + else if (e.PropertyName == Entry.PlaceholderColorProperty.PropertyName) + UpdatePlaceholder(); + else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName) + UpdateMaxLength(); + + base.OnElementPropertyChanged(sender, e); + } + + protected override void Dispose(bool disposing) + { + if (disposing && !_disposed) + { + _disposed = true; + if (Control != null) + { + Control.Entry.Changed -= OnChanged; + Control.Entry.Focused -= OnFocused; + Control.Entry.EditingDone -= OnEditingDone; + Control.Entry.KeyReleaseEvent -= OnKeyReleased; + } + } + + base.Dispose(disposing); + } + + protected override void UpdateBackgroundColor() + { + if (!Element.BackgroundColor.IsDefaultOrTransparent()) + { + Control.SetBackgroundColor(Element.BackgroundColor.ToGtkColor()); + } + else + { + Control.SetBackgroundColor(Color.Transparent.ToGtkColor()); + } + } + + private void UpdateText() + { + if (Control.Entry.Text != Element.Text) + Control.Entry.Text = Element.Text ?? string.Empty; + } + + private void UpdateColor() + { + var textColor = Element.TextColor; + + Control.SetTextColor(textColor.ToGtkColor()); + } + + private void UpdateAlignment() + { + Control.SetAlignment(Element.HorizontalTextAlignment.ToNativeValue()); + } + + private void UpdateFont() + { + FontDescription fontDescription = FontDescriptionHelper.CreateFontDescription( + Element.FontSize, Element.FontFamily, Element.FontAttributes); + Control.SetFont(fontDescription); + } + + private void UpdateTextVisibility() + { + Control.Entry.Visibility = !Element.IsPassword; + } + + private void UpdatePlaceholder() + { + Control.PlaceholderText = Element.Placeholder ?? string.Empty; + Control.SetPlaceholderTextColor(Element.PlaceholderColor.ToGtkColor()); + } + + private void OnChanged(object sender, System.EventArgs e) + { + ElementController.SetValueFromRenderer(Entry.TextProperty, Control.Entry.Text); + } + + private void OnFocused(object o, FocusedArgs args) + { + ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true); + } + + private void OnEditingDone(object sender, System.EventArgs e) + { + ElementController.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false); + EntryController?.SendCompleted(); + } + + private void OnKeyReleased(object o, KeyReleaseEventArgs args) + { + if (args.Event.Key == Gdk.Key.Return) + { + EntryController.SendCompleted(); + } + } + + private void UpdateMaxLength() + { + Control.SetMaxLength(Element.MaxLength); + } + } +} diff --git a/Xamarin.Forms.Platform.GTK/Renderers/ScrollViewRenderer.cs b/Xamarin.Forms.Platform.GTK/Renderers/ScrollViewRenderer.cs index 778de4a685d..7014628fb2f 100644 --- a/Xamarin.Forms.Platform.GTK/Renderers/ScrollViewRenderer.cs +++ b/Xamarin.Forms.Platform.GTK/Renderers/ScrollViewRenderer.cs @@ -210,7 +210,7 @@ void UpdateVerticalScrollBarVisibility() void UpdateHorizontalScrollBarVisibility() { - var orientation = Element.Orientation; + var orientation = Element.Orientation; if (orientation == ScrollOrientation.Horizontal || orientation == ScrollOrientation.Both) Control.HscrollbarPolicy = ScrollBarVisibilityToGtk(Element.HorizontalScrollBarVisibility); } @@ -220,14 +220,14 @@ PolicyType ScrollBarVisibilityToGtk(ScrollBarVisibility visibility) switch(visibility) { case ScrollBarVisibility.Default: - return PolicyType.Automatic; + return PolicyType.Automatic; case ScrollBarVisibility.Always: - return PolicyType.Always; + return PolicyType.Always; case ScrollBarVisibility.Never: - return PolicyType.Never; + return PolicyType.Never; default: - return PolicyType.Automatic; + return PolicyType.Automatic; } } } -} \ No newline at end of file +} diff --git a/Xamarin.Forms.Platform.GTK/Renderers/TabbedPageRenderer.cs b/Xamarin.Forms.Platform.GTK/Renderers/TabbedPageRenderer.cs index 0bdc8c8a853..a1f879d788f 100644 --- a/Xamarin.Forms.Platform.GTK/Renderers/TabbedPageRenderer.cs +++ b/Xamarin.Forms.Platform.GTK/Renderers/TabbedPageRenderer.cs @@ -9,284 +9,284 @@ namespace Xamarin.Forms.Platform.GTK.Renderers { - public class TabbedPageRenderer : AbstractPageRenderer - { - const int DefaultIconWidth = 24; - const int DefaultIconHeight = 24; - - protected override void OnElementChanged(VisualElementChangedEventArgs e) - { - base.OnElementChanged(e); - - if (e.OldElement != null) - { - Page.ChildAdded -= OnPageAdded; - Page.ChildRemoved -= OnPageRemoved; - Page.PagesChanged -= OnPagesChanged; - } - - if (e.NewElement != null) - { - var newPage = e.NewElement as TabbedPage; - - if (newPage == null) - throw new ArgumentException("Element must be a TabbedPage"); - - if (Widget == null) - { - // Custom control using a tabbed notebook container. - Widget = new NotebookWrapper(); - Control.Content = Widget; - } - - Init(); - } - } - - void Init() - { - OnPagesChanged(Page.Children, - new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); - - Page.ChildAdded += OnPageAdded; - Page.ChildRemoved += OnPageRemoved; - Page.PagesChanged += OnPagesChanged; - - UpdateCurrentPage(); - UpdateBarBackgroundColor(); - UpdateBarTextColor(); - UpdateTabPos(); - UpdateBackgroundImage(); - } - - protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) - { - base.OnElementPropertyChanged(sender, e); - - if (e.PropertyName == nameof(TabbedPage.CurrentPage)) - { - UpdateCurrentPage(); - UpdateBarTextColor(); - UpdateBarBackgroundColor(); - } - else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) - UpdateBarTextColor(); - else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) - UpdateBarBackgroundColor(); - else if (e.PropertyName == - PlatformConfiguration.GTKSpecific.TabbedPage.TabPositionProperty.PropertyName) - UpdateTabPos(); - } - - protected override void OnSizeAllocated(Gdk.Rectangle allocation) - { - base.OnSizeAllocated(allocation); - - Control?.Content?.SetSize(allocation.Width, allocation.Height); - } - - protected override void UpdateBackgroundImage() - { - Widget?.SetBackgroundImage(Page.BackgroundImage); - } - - protected override void Dispose(bool disposing) - { - Page.PagesChanged -= OnPagesChanged; - Page.ChildAdded -= OnPageAdded; - Page.ChildRemoved -= OnPageRemoved; - - if (Widget != null) - { - Widget.NoteBook.SwitchPage -= OnNotebookPageSwitched; - } - - base.Dispose(disposing); - } - - void OnPagesChanged(object sender, NotifyCollectionChangedEventArgs e) - { - Widget.NoteBook.SwitchPage -= OnNotebookPageSwitched; - - if (e.Action == NotifyCollectionChangedAction.Reset) - { - ResetPages(); - } - - UpdateChildrenOrderIndex(); - UpdateCurrentPage(); - - Widget.NoteBook.SwitchPage += OnNotebookPageSwitched; - } - - void OnPageAdded(object sender, ElementEventArgs e) - { - InsertPage(e.Element as Page, Page.Children.IndexOf(e.Element)); - } - - void OnPageRemoved(object sender, ElementEventArgs e) - { - RemovePage(e.Element as Page); - } - - void InsertPage(Page page, int index) - { - var pageRenderer = Platform.GetRenderer(page); - - if (pageRenderer == null) - { - pageRenderer = Platform.CreateRenderer(page); - Platform.SetRenderer(page, pageRenderer); - } - - Widget.InsertPage( - pageRenderer.Container, - page.Title, - page.Icon?.ToPixbuf(new Size(DefaultIconWidth, DefaultIconHeight)), - index); - - Widget.ShowAll(); - - page.PropertyChanged += OnPagePropertyChanged; - } - - void RemovePage(Page page) - { - page.PropertyChanged -= OnPagePropertyChanged; - - var pageRenderer = Platform.GetRenderer(page); - - if (pageRenderer != null) - { - Widget.RemovePage(pageRenderer.Container); - } - - Platform.SetRenderer(page, null); - } - - void ResetPages() - { - foreach (var page in Page.Children) - RemovePage(page); - - Widget.RemoveAllPages(); - - var i = 0; - foreach (var page in Page.Children) - InsertPage(page, i++); - } - - void OnPagePropertyChanged(object sender, PropertyChangedEventArgs e) - { - if (e.PropertyName == Xamarin.Forms.Page.TitleProperty.PropertyName) - { - var page = (Page)sender; - var index = TabbedPage.GetIndex(page); - - Widget.SetTabLabelText(index, page.Title); - } - else if (e.PropertyName == Xamarin.Forms.Page.IconProperty.PropertyName) - { - var page = (Page)sender; - var index = TabbedPage.GetIndex(page); - var icon = page.Icon; - - Widget.SetTabIcon(index, icon.ToPixbuf()); - } - else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) - UpdateBarBackgroundColor(); - else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) - UpdateBarTextColor(); - } - - void UpdateCurrentPage() - { - Page page = Page.CurrentPage; - - if (page == null) - return; - - int selectedIndex = 0; - - for (var i = 0; i < Page.Children.Count; i++) - { - if (Page.Children[i].Equals(page)) - { - break; - } - - selectedIndex++; - } - - Widget.NoteBook.CurrentPage = selectedIndex; - Widget.NoteBook.ShowAll(); - } - - void UpdateChildrenOrderIndex() - { - for (var i = 0; i < Page.Children.Count; i++) - { - var page = PageController.InternalChildren[i]; - - TabbedPage.SetIndex(page as Page, i); - } - } - - void UpdateBarBackgroundColor() - { - if (Element == null || Page.BarBackgroundColor.IsDefault) - return; - - var barBackgroundColor = Page.BarBackgroundColor.ToGtkColor(); - - for (var i = 0; i < Page.Children.Count; i++) - { - Widget.SetTabBackgroundColor(i, barBackgroundColor); - } - } - - void UpdateBarTextColor() - { - if (Element == null || Page.BarTextColor.IsDefault) - return; - - var barTextColor = Page.BarTextColor.ToGtkColor(); - - for (var i = 0; i < Page.Children.Count; i++) - { - Widget.SetTabTextColor(i, barTextColor); - } - } - - void UpdateTabPos() // Platform-Specific Functionality - { - var tabposition = Page.OnThisPlatform().GetTabPosition(); - - switch (tabposition) - { - case TabPosition.Top: - Widget.NoteBook.TabPos = PositionType.Top; - break; - case TabPosition.Bottom: - Widget.NoteBook.TabPos = PositionType.Bottom; - break; - } - } - - void OnNotebookPageSwitched(object o, SwitchPageArgs args) - { - var currentPageIndex = (int)args.PageNum; - VisualElement currentSelectedChild = Page.Children.Count > currentPageIndex - ? Page.Children[currentPageIndex] - : null; - - if (currentSelectedChild != null) - { - ElementController.SetValueFromRenderer(TabbedPage.SelectedItemProperty, currentSelectedChild.BindingContext); - - var pageRenderer = Platform.GetRenderer(currentSelectedChild); - pageRenderer?.Container.ShowAll(); - } - } - } -} \ No newline at end of file + public class TabbedPageRenderer : AbstractPageRenderer + { + const int DefaultIconWidth = 24; + const int DefaultIconHeight = 24; + + protected override void OnElementChanged(VisualElementChangedEventArgs e) + { + base.OnElementChanged(e); + + if (e.OldElement != null) + { + Page.ChildAdded -= OnPageAdded; + Page.ChildRemoved -= OnPageRemoved; + Page.PagesChanged -= OnPagesChanged; + } + + if (e.NewElement != null) + { + var newPage = e.NewElement as TabbedPage; + + if (newPage == null) + throw new ArgumentException("Element must be a TabbedPage"); + + if (Widget == null) + { + // Custom control using a tabbed notebook container. + Widget = new NotebookWrapper(); + Control.Content = Widget; + } + + Init(); + } + } + + void Init() + { + OnPagesChanged(Page.Children, + new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); + + Page.ChildAdded += OnPageAdded; + Page.ChildRemoved += OnPageRemoved; + Page.PagesChanged += OnPagesChanged; + + UpdateCurrentPage(); + UpdateBarBackgroundColor(); + UpdateBarTextColor(); + UpdateTabPos(); + UpdateBackgroundImage(); + } + + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + base.OnElementPropertyChanged(sender, e); + + if (e.PropertyName == nameof(TabbedPage.CurrentPage)) + { + UpdateCurrentPage(); + UpdateBarTextColor(); + UpdateBarBackgroundColor(); + } + else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) + UpdateBarTextColor(); + else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) + UpdateBarBackgroundColor(); + else if (e.PropertyName == + PlatformConfiguration.GTKSpecific.TabbedPage.TabPositionProperty.PropertyName) + UpdateTabPos(); + } + + protected override void OnSizeAllocated(Gdk.Rectangle allocation) + { + base.OnSizeAllocated(allocation); + + Control?.Content?.SetSize(allocation.Width, allocation.Height); + } + + protected override void UpdateBackgroundImage() + { + Widget?.SetBackgroundImage(Page.BackgroundImage); + } + + protected override void Dispose(bool disposing) + { + Page.PagesChanged -= OnPagesChanged; + Page.ChildAdded -= OnPageAdded; + Page.ChildRemoved -= OnPageRemoved; + + if (Widget != null) + { + Widget.NoteBook.SwitchPage -= OnNotebookPageSwitched; + } + + base.Dispose(disposing); + } + + void OnPagesChanged(object sender, NotifyCollectionChangedEventArgs e) + { + Widget.NoteBook.SwitchPage -= OnNotebookPageSwitched; + + if (e.Action == NotifyCollectionChangedAction.Reset) + { + ResetPages(); + } + + UpdateChildrenOrderIndex(); + UpdateCurrentPage(); + + Widget.NoteBook.SwitchPage += OnNotebookPageSwitched; + } + + void OnPageAdded(object sender, ElementEventArgs e) + { + InsertPage(e.Element as Page, Page.Children.IndexOf(e.Element)); + } + + void OnPageRemoved(object sender, ElementEventArgs e) + { + RemovePage(e.Element as Page); + } + + void InsertPage(Page page, int index) + { + var pageRenderer = Platform.GetRenderer(page); + + if (pageRenderer == null) + { + pageRenderer = Platform.CreateRenderer(page); + Platform.SetRenderer(page, pageRenderer); + } + + Widget.InsertPage( + pageRenderer.Container, + page.Title, + page.Icon?.ToPixbuf(new Size(DefaultIconWidth, DefaultIconHeight)), + index); + + Widget.ShowAll(); + + page.PropertyChanged += OnPagePropertyChanged; + } + + void RemovePage(Page page) + { + page.PropertyChanged -= OnPagePropertyChanged; + + var pageRenderer = Platform.GetRenderer(page); + + if (pageRenderer != null) + { + Widget.RemovePage(pageRenderer.Container); + } + + Platform.SetRenderer(page, null); + } + + void ResetPages() + { + foreach (var page in Page.Children) + RemovePage(page); + + Widget.RemoveAllPages(); + + var i = 0; + foreach (var page in Page.Children) + InsertPage(page, i++); + } + + void OnPagePropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == Xamarin.Forms.Page.TitleProperty.PropertyName) + { + var page = (Page)sender; + var index = TabbedPage.GetIndex(page); + + Widget.SetTabLabelText(index, page.Title); + } + else if (e.PropertyName == Xamarin.Forms.Page.IconProperty.PropertyName) + { + var page = (Page)sender; + var index = TabbedPage.GetIndex(page); + var icon = page.Icon; + + Widget.SetTabIcon(index, icon.ToPixbuf()); + } + else if (e.PropertyName == TabbedPage.BarBackgroundColorProperty.PropertyName) + UpdateBarBackgroundColor(); + else if (e.PropertyName == TabbedPage.BarTextColorProperty.PropertyName) + UpdateBarTextColor(); + } + + void UpdateCurrentPage() + { + Page page = Page.CurrentPage; + + if (page == null) + return; + + int selectedIndex = 0; + + for (var i = 0; i < Page.Children.Count; i++) + { + if (Page.Children[i].Equals(page)) + { + break; + } + + selectedIndex++; + } + + Widget.NoteBook.CurrentPage = selectedIndex; + Widget.NoteBook.ShowAll(); + } + + void UpdateChildrenOrderIndex() + { + for (var i = 0; i < Page.Children.Count; i++) + { + var page = PageController.InternalChildren[i]; + + TabbedPage.SetIndex(page as Page, i); + } + } + + void UpdateBarBackgroundColor() + { + if (Element == null || Page.BarBackgroundColor.IsDefault) + return; + + var barBackgroundColor = Page.BarBackgroundColor.ToGtkColor(); + + for (var i = 0; i < Page.Children.Count; i++) + { + Widget.SetTabBackgroundColor(i, barBackgroundColor); + } + } + + void UpdateBarTextColor() + { + if (Element == null || Page.BarTextColor.IsDefault) + return; + + var barTextColor = Page.BarTextColor.ToGtkColor(); + + for (var i = 0; i < Page.Children.Count; i++) + { + Widget.SetTabTextColor(i, barTextColor); + } + } + + void UpdateTabPos() // Platform-Specific Functionality + { + var tabposition = Page.OnThisPlatform().GetTabPosition(); + + switch (tabposition) + { + case TabPosition.Top: + Widget.NoteBook.TabPos = PositionType.Top; + break; + case TabPosition.Bottom: + Widget.NoteBook.TabPos = PositionType.Bottom; + break; + } + } + + void OnNotebookPageSwitched(object o, SwitchPageArgs args) + { + var currentPageIndex = (int)args.PageNum; + VisualElement currentSelectedChild = Page.Children.Count > currentPageIndex + ? Page.Children[currentPageIndex] + : null; + + if (currentSelectedChild != null) + { + ElementController.SetValueFromRenderer(TabbedPage.SelectedItemProperty, currentSelectedChild.BindingContext); + + var pageRenderer = Platform.GetRenderer(currentSelectedChild); + pageRenderer?.Container.ShowAll(); + } + } + } +} diff --git a/Xamarin.Forms.Platform.GTK/Renderers/WebViewRenderer.cs b/Xamarin.Forms.Platform.GTK/Renderers/WebViewRenderer.cs index b29b5212784..dc96d09ff40 100644 --- a/Xamarin.Forms.Platform.GTK/Renderers/WebViewRenderer.cs +++ b/Xamarin.Forms.Platform.GTK/Renderers/WebViewRenderer.cs @@ -208,7 +208,7 @@ private void OnEvalRequested(object sender, EvalRequested eventArg) Task OnEvaluateJavaScriptRequested(string script) { - Control?.ExecuteScript(script); + Control?.ExecuteScript(script); return null; }