|
| 1 | +using Color = Microsoft.Maui.Graphics.Color; |
| 2 | +#if WINDOWS |
| 3 | +using WColor = Windows.UI.Color; |
| 4 | +using GradientBrush = Microsoft.UI.Xaml.Media.GradientBrush; |
| 5 | +#elif ANDROID |
| 6 | +using Android.Content; |
| 7 | +using Android.OS; |
| 8 | +using Android.Util; |
| 9 | +#endif |
| 10 | + |
| 11 | +namespace Syncfusion.Maui.Toolkit.EntryView |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// The SfEntryView is a entry component which is used to create input view entry for NumericEntry. |
| 15 | + /// </summary> |
| 16 | + internal partial class SfEntryView : Entry |
| 17 | + { |
| 18 | + #region Fields |
| 19 | + |
| 20 | + IDrawable? _drawable; |
| 21 | + |
| 22 | +#if MACCATALYST || IOS |
| 23 | + Color? _focusedColor = Color.FromArgb("#8EBDFF"); |
| 24 | +#else |
| 25 | + Color? _focusedColor = Colors.Gray; |
| 26 | +#endif |
| 27 | + |
| 28 | + #endregion |
| 29 | + |
| 30 | + #region Constructor |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Initializes a new instance of the <see cref="SfEntryView"/> class. |
| 34 | + /// </summary> |
| 35 | + public SfEntryView() |
| 36 | + { |
| 37 | + Initialize(); |
| 38 | + } |
| 39 | + |
| 40 | + void Initialize() |
| 41 | + { |
| 42 | + Style = new Style(typeof(SfEntryView)); |
| 43 | + BackgroundColor = Colors.Transparent; |
| 44 | + TextColor = Colors.Black; |
| 45 | + FontSize = 14d; |
| 46 | + } |
| 47 | + |
| 48 | + #endregion |
| 49 | + |
| 50 | + #region Properties |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Gets or sets the value for focused stroke. |
| 54 | + /// </summary> |
| 55 | + internal Color? FocusedStroke |
| 56 | + { |
| 57 | + get |
| 58 | + { |
| 59 | + return _focusedColor; |
| 60 | + } |
| 61 | + set |
| 62 | + { |
| 63 | + _focusedColor = value; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets or sets the drawable value. |
| 69 | + /// </summary> |
| 70 | + internal IDrawable? Drawable |
| 71 | + { |
| 72 | + get { return _drawable; } |
| 73 | + set { _drawable = value; } |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Gets or sets the button size value. |
| 79 | + /// </summary> |
| 80 | + internal double ButtonSize { get; set; } |
| 81 | + |
| 82 | + #endregion |
| 83 | + |
| 84 | + #region Override Methods |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Handler changed method. |
| 88 | + /// </summary> |
| 89 | + protected override void OnHandlerChanged() |
| 90 | + { |
| 91 | + base.OnHandlerChanged(); |
| 92 | + // Unsubscribe from events to avoid duplicate handlers |
| 93 | + Focused -= SfEntry_Focused; |
| 94 | + Unfocused -= SfEntry_Unfocused; |
| 95 | + |
| 96 | + // Check if the Handler is available for attaching event handlers |
| 97 | + if (Handler != null) |
| 98 | + { |
| 99 | + #if WINDOWS |
| 100 | + if (Handler.PlatformView is Microsoft.UI.Xaml.Controls.TextBox textbox) |
| 101 | + { |
| 102 | + AdjustWindowsTextBoxStyles(textbox); |
| 103 | + } |
| 104 | + #elif ANDROID |
| 105 | + if (Handler.PlatformView is AndroidX.AppCompat.Widget.AppCompatEditText textbox) |
| 106 | + { |
| 107 | + AdjustAndroidTextBoxStyles(textbox); |
| 108 | + } |
| 109 | + #endif |
| 110 | + // Subscribe to events when the handler is set |
| 111 | + Focused += SfEntry_Focused; |
| 112 | + Unfocused += SfEntry_Unfocused; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + #if WINDOWS |
| 117 | + private void AdjustWindowsTextBoxStyles(Microsoft.UI.Xaml.Controls.TextBox textbox) |
| 118 | + { |
| 119 | + textbox.BorderThickness = new Microsoft.UI.Xaml.Thickness(0); |
| 120 | + textbox.Resources["TextControlBorderThemeThicknessFocused"] = textbox.BorderThickness; |
| 121 | + |
| 122 | + if (textbox.Resources["TextControlBorderBrushFocused"] is GradientBrush brush && |
| 123 | + brush.GradientStops.FirstOrDefault()?.Color is WColor color) |
| 124 | + { |
| 125 | + FocusedStroke = new Color(color.R, color.G, color.B, color.A); |
| 126 | + } |
| 127 | + } |
| 128 | + #endif |
| 129 | + |
| 130 | + #if ANDROID |
| 131 | + private void AdjustAndroidTextBoxStyles(AndroidX.AppCompat.Widget.AppCompatEditText textbox) |
| 132 | + { |
| 133 | + textbox.SetBackgroundColor(Android.Graphics.Color.Transparent); |
| 134 | + var themeAccentColor = textbox.Context != null ? GetThemeAccentColor(textbox.Context) : Android.Graphics.Color.Transparent.ToArgb(); |
| 135 | + var color = new Android.Graphics.Color(themeAccentColor); |
| 136 | + FocusedStroke = new Color(color.R, color.G, color.B, color.A); |
| 137 | + } |
| 138 | + #endif |
| 139 | + |
| 140 | + #endregion |
| 141 | + |
| 142 | + #region Methods |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// It invoked when entry get unfocused. |
| 146 | + /// </summary> |
| 147 | + /// <param name="sender">The sender.</param> |
| 148 | + /// <param name="e">The focus event args.</param> |
| 149 | + void SfEntry_Unfocused(object? sender, FocusEventArgs e) |
| 150 | + { |
| 151 | + if (_drawable is SfView sfView) |
| 152 | + { |
| 153 | + sfView.InvalidateDrawable(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// It invoked when the entry get focus. |
| 159 | + /// </summary> |
| 160 | + /// <param name="sender">The sender.</param> |
| 161 | + /// <param name="e">The focus event args.</param> |
| 162 | + void SfEntry_Focused(object? sender, FocusEventArgs e) |
| 163 | + { |
| 164 | + if (_drawable is SfView sfView) |
| 165 | + { |
| 166 | + sfView.InvalidateDrawable(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | +#if ANDROID |
| 171 | + /// <summary> |
| 172 | + /// Get theme accent color method. |
| 173 | + /// </summary> |
| 174 | + /// <param name="context">The context.</param> |
| 175 | + static int GetThemeAccentColor(Context context) |
| 176 | + { |
| 177 | + int colorAttr = Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop |
| 178 | + ? Android.Resource.Attribute.ColorAccent |
| 179 | + : context.Resources?.GetIdentifier("colorAccent", "attr", context.PackageName) |
| 180 | + ?? Android.Resource.Color.BackgroundDark; |
| 181 | + |
| 182 | + TypedValue outValue = new(); |
| 183 | + bool resolved = context.Theme?.ResolveAttribute(colorAttr, outValue, true) ?? false; |
| 184 | + |
| 185 | + return resolved ? outValue.Data : Android.Resource.Color.BackgroundDark; |
| 186 | + } |
| 187 | +#endif |
| 188 | + #endregion |
| 189 | + } |
| 190 | +} |
0 commit comments