|
| 1 | +using System; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.AspNetCore.Components; |
| 5 | +using Microsoft.AspNetCore.Components.Web; |
| 6 | +using MudBlazor; |
| 7 | +using MudBlazor.Extensions; |
| 8 | +using MudBlazor.Services; |
| 9 | +using MudBlazor.Utilities; |
| 10 | + |
| 11 | +namespace MudExtensions |
| 12 | +{ |
| 13 | + public partial class MudSwitchM3<T> : MudBooleanInput<T> |
| 14 | + { |
| 15 | + protected string Classname => |
| 16 | + new CssBuilder("mud-switch-m3") |
| 17 | + .AddClass($"mud-disabled", Disabled) |
| 18 | + .AddClass($"mud-readonly", ReadOnly) |
| 19 | + .AddClass(LabelPosition == LabelPosition.End ? "mud-ltr" : "mud-rtl", true) |
| 20 | + .AddClass(Class) |
| 21 | + .Build(); |
| 22 | + |
| 23 | + protected string SwitchSpanClassname => |
| 24 | + new CssBuilder("mud-switch-span-m3 mud-flip-x-rtl") |
| 25 | + .AddClass("mud-switch-child-content-m3", ChildContent != null || !string.IsNullOrEmpty(Label)) |
| 26 | + .Build(); |
| 27 | + |
| 28 | + protected string SwitchClassname => |
| 29 | + new CssBuilder("mud-button-root mud-icon-button mud-switch-base-m3") |
| 30 | + .AddClass($"mud-ripple mud-ripple-switch", !DisableRipple && !ReadOnly && !Disabled) |
| 31 | + .AddClass($"mud-{Color.ToDescriptionString()}-text hover:mud-{Color.ToDescriptionString()}-hover", BoolValue == true) |
| 32 | + //.AddClass($"mud-{UnCheckedColor.ToDescriptionString()}-text hover:mud-{UnCheckedColor.ToDescriptionString()}-hover", BoolValue == false) |
| 33 | + .AddClass($"mud-switch-disabled", Disabled) |
| 34 | + .AddClass($"mud-readonly", ReadOnly) |
| 35 | + .AddClass($"mud-checked", BoolValue) |
| 36 | + .AddClass("mud-switch-base-dense-m3", !string.IsNullOrEmpty(ThumbOffIcon)) |
| 37 | + .Build(); |
| 38 | + |
| 39 | + protected string TrackClassname => |
| 40 | + new CssBuilder("mud-switch-track-m3") |
| 41 | + .AddClass($"mud-{Color.ToDescriptionString()}", BoolValue == true) |
| 42 | + .AddClass($"mud-switch-track-{Color.ToDescriptionString()}-m3") |
| 43 | + .Build(); |
| 44 | + |
| 45 | + protected string IconStylename => |
| 46 | + new StyleBuilder() |
| 47 | + .AddStyle("width: 16px; height: 16px;") |
| 48 | + .AddStyle("color", "var(--mud-palette-background)", !string.IsNullOrEmpty(ThumbOffIcon)) |
| 49 | + .Build(); |
| 50 | + |
| 51 | + private IKeyInterceptor _keyInterceptor; |
| 52 | + [Inject] private IKeyInterceptorFactory KeyInterceptorFactory { get; set; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// The color of the component. It supports the theme colors. |
| 56 | + /// </summary> |
| 57 | + [Parameter] |
| 58 | + [Category(CategoryTypes.FormComponent.Appearance)] |
| 59 | + public Color Color { get; set; } = Color.Default; |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// The text/label will be displayed next to the switch if set. |
| 63 | + /// </summary> |
| 64 | + [Parameter] |
| 65 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 66 | + public string Label { get; set; } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// The position of the text/label. |
| 70 | + /// </summary> |
| 71 | + [Parameter] |
| 72 | + [Category(CategoryTypes.FormComponent.Behavior)] |
| 73 | + public LabelPosition LabelPosition { get; set; } = LabelPosition.End; |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Shows an icon on Switch's thumb. |
| 77 | + /// </summary> |
| 78 | + [Parameter] |
| 79 | + [Category(CategoryTypes.FormComponent.Appearance)] |
| 80 | + public string ThumbIcon { get; set; } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Shows an icon on Switch's thumb (off state). |
| 84 | + /// </summary> |
| 85 | + [Parameter] |
| 86 | + [Category(CategoryTypes.FormComponent.Appearance)] |
| 87 | + public string ThumbOffIcon { get; set; } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// If true, disables ripple effect. |
| 91 | + /// </summary> |
| 92 | + [Parameter] |
| 93 | + [Category(CategoryTypes.FormComponent.Appearance)] |
| 94 | + public bool DisableRipple { get; set; } |
| 95 | + |
| 96 | + protected internal void HandleKeyDown(KeyboardEventArgs obj) |
| 97 | + { |
| 98 | + if (Disabled || ReadOnly) |
| 99 | + return; |
| 100 | + switch (obj.Key) |
| 101 | + { |
| 102 | + case "ArrowLeft": |
| 103 | + case "Delete": |
| 104 | + SetBoolValueAsync(false); |
| 105 | + break; |
| 106 | + case "ArrowRight": |
| 107 | + case "Enter": |
| 108 | + case "NumpadEnter": |
| 109 | + SetBoolValueAsync(true); |
| 110 | + break; |
| 111 | + case " ": |
| 112 | + if (BoolValue == true) |
| 113 | + { |
| 114 | + SetBoolValueAsync(false); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + SetBoolValueAsync(true); |
| 119 | + } |
| 120 | + break; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private string _elementId = "switchm3_" + Guid.NewGuid().ToString().Substring(0, 8); |
| 125 | + |
| 126 | + protected override void OnInitialized() |
| 127 | + { |
| 128 | + base.OnInitialized(); |
| 129 | + |
| 130 | + if (Label == null && For != null) |
| 131 | + Label = For.GetLabelString(); |
| 132 | + } |
| 133 | + |
| 134 | + protected override async Task OnAfterRenderAsync(bool firstRender) |
| 135 | + { |
| 136 | + if (firstRender) |
| 137 | + { |
| 138 | + _keyInterceptor = KeyInterceptorFactory.Create(); |
| 139 | + |
| 140 | + await _keyInterceptor.Connect(_elementId, new KeyInterceptorOptions() |
| 141 | + { |
| 142 | + //EnableLogging = true, |
| 143 | + TargetClass = "mud-switch-base-m3", |
| 144 | + Keys = { |
| 145 | + new KeyOptions { Key="ArrowUp", PreventDown = "key+none" }, // prevent scrolling page, instead increment |
| 146 | + new KeyOptions { Key="ArrowDown", PreventDown = "key+none" }, // prevent scrolling page, instead decrement |
| 147 | + new KeyOptions { Key=" ", PreventDown = "key+none", PreventUp = "key+none" }, |
| 148 | + }, |
| 149 | + }); |
| 150 | + |
| 151 | + _keyInterceptor.KeyDown += HandleKeyDown; |
| 152 | + } |
| 153 | + await base.OnAfterRenderAsync(firstRender); |
| 154 | + } |
| 155 | + |
| 156 | + protected override void Dispose(bool disposing) |
| 157 | + { |
| 158 | + base.Dispose(disposing); |
| 159 | + |
| 160 | + if (disposing == true) |
| 161 | + { |
| 162 | + if(_keyInterceptor != null) |
| 163 | + { |
| 164 | + _keyInterceptor.KeyDown -= HandleKeyDown; |
| 165 | + _keyInterceptor.Dispose(); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments