MobileButton.xaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Globalization;
  3. using InABox.Core;
  4. using Xamarin.Forms;
  5. using Xamarin.Forms.Xaml;
  6. using XF.Material.Forms.Resources.Typography;
  7. namespace InABox.Mobile
  8. {
  9. public class MobileButtonClickEventArgs : EventArgs
  10. {
  11. public object Tag { get; private set; }
  12. public MobileButtonClickEventArgs(object tag)
  13. {
  14. Tag = tag;
  15. }
  16. }
  17. public delegate void MobileButtonClickEvent(object sender, MobileButtonClickEventArgs args);
  18. public class MobileButtonTextFormatter : IValueConverter
  19. {
  20. public object Prefix { get; set; }
  21. public String Prompt { get; set; }
  22. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  23. {
  24. if (String.IsNullOrWhiteSpace(value as String))
  25. return $"[{Prompt}]";
  26. return $"{Prefix} {value}".Trim();
  27. }
  28. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  29. {
  30. throw new NotImplementedException();
  31. }
  32. }
  33. public enum MobileButtonSizeDimension
  34. {
  35. Height,
  36. Width
  37. }
  38. public class MobileButtonSizeConverter : AbstractConverter<Size,double>
  39. {
  40. public MobileButtonSizeDimension Dimension { get; set; }
  41. protected override double Convert(Size value, object? parameter = null)
  42. {
  43. return Dimension == MobileButtonSizeDimension.Height
  44. ? value.Height
  45. : value.Width;
  46. }
  47. }
  48. [XamlCompilation(XamlCompilationOptions.Compile)]
  49. public partial class MobileButton
  50. {
  51. public event MobileButtonClickEvent Clicked;
  52. public object Prefix
  53. {
  54. get => _textformatter.Prefix;
  55. set => _textformatter.Prefix = value;
  56. }
  57. public String Prompt
  58. {
  59. get => _textformatter.Prompt;
  60. set => _textformatter.Prompt = value;
  61. }
  62. public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(
  63. nameof(CornerRadius),
  64. typeof(float),
  65. typeof(MobileButton),
  66. 5.0f);
  67. public float CornerRadius
  68. {
  69. get => (float)GetValue(CornerRadiusProperty);
  70. set => SetValue(CornerRadiusProperty, value);
  71. }
  72. public static readonly BindableProperty TagProperty = BindableProperty.Create(
  73. nameof(Tag),
  74. typeof(object),
  75. typeof(MobileButton));
  76. public object Tag
  77. {
  78. get => GetValue(TagProperty);
  79. set => SetValue(TagProperty, value);
  80. }
  81. private static readonly BindableProperty OrientationProperty = BindableProperty.Create(
  82. nameof(Orientation),
  83. typeof(StackOrientation),
  84. typeof(MobileButton),
  85. StackOrientation.Horizontal);
  86. public StackOrientation Orientation
  87. {
  88. get => (StackOrientation)GetValue(OrientationProperty);
  89. set => SetValue(OrientationProperty, value);
  90. }
  91. public static readonly BindableProperty ImageProperty = BindableProperty.Create(
  92. nameof(Image),
  93. typeof(ImageSource),
  94. typeof(MobileButton)
  95. );
  96. public ImageSource Image
  97. {
  98. get => GetValue(ImageProperty) as ImageSource;
  99. set => SetValue(ImageProperty, value);
  100. }
  101. public static readonly BindableProperty ImageSizeProperty = BindableProperty.Create(
  102. nameof(ImageSize),
  103. typeof(Size),
  104. typeof(MobileButton),
  105. new Size(24,24)
  106. );
  107. public Size ImageSize
  108. {
  109. get => (Size)GetValue(ImageSizeProperty);
  110. set => SetValue(ImageSizeProperty, value);
  111. }
  112. public static readonly BindableProperty TextProperty = BindableProperty.Create(
  113. nameof(Text),
  114. typeof(String),
  115. typeof(MobileButton)
  116. );
  117. public String Text
  118. {
  119. get => (String)GetValue(TextProperty);
  120. set => SetValue(TextProperty, value);
  121. }
  122. private static readonly BindableProperty AlertProperty = BindableProperty.Create(
  123. nameof(Alert),
  124. typeof(String),
  125. typeof(MobileButton)
  126. );
  127. public String Alert
  128. {
  129. get => (String)GetValue(AlertProperty);
  130. set => SetValue(AlertProperty, value);
  131. }
  132. private static readonly BindableProperty TextColorProperty = BindableProperty.Create(
  133. nameof(TextColor),
  134. typeof(Color),
  135. typeof(MobileButton),
  136. XF.Material.Forms.Material.Color.OnSecondary);
  137. public Color TextColor
  138. {
  139. get => (Color)GetValue(TextColorProperty);
  140. set => SetValue(TextColorProperty, value);
  141. }
  142. private static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
  143. nameof(TypeScale),
  144. typeof(MaterialTypeScale),
  145. typeof(MobileButton),
  146. MaterialTypeScale.Button);
  147. public MaterialTypeScale TypeScale
  148. {
  149. get => (MaterialTypeScale)GetValue(TypeScaleProperty);
  150. set => SetValue(TypeScaleProperty, value);
  151. }
  152. public new static readonly BindableProperty BackgroundColorProperty = BindableProperty.Create(
  153. nameof(BackgroundColor),
  154. typeof(Color),
  155. typeof(MobileButton),
  156. XF.Material.Forms.Material.Color.Secondary);
  157. public new Color BackgroundColor
  158. {
  159. get => (Color)GetValue(BackgroundColorProperty);
  160. set => SetValue(BackgroundColorProperty, value);
  161. }
  162. private static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
  163. nameof(BorderColor),
  164. typeof(Color),
  165. typeof(MobileButton),
  166. XF.Material.Forms.Material.Color.SecondaryVariant);
  167. public Color BorderColor
  168. {
  169. get => (Color)GetValue(BorderColorProperty);
  170. set => SetValue(BorderColorProperty, value);
  171. }
  172. public MobileButton()
  173. {
  174. InitializeComponent();
  175. Alert = "";
  176. HeightRequest = 40;
  177. }
  178. private async void _frame_OnClicked(object sender, EventArgs e)
  179. {
  180. Clicked?.Invoke(this, new MobileButtonClickEventArgs(Tag));
  181. }
  182. }
  183. }