// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using Microsoft.Maui.Graphics; using Microsoft.Maui.Handlers; namespace Microsoft.Maui.Platform.Linux.Handlers; /// /// Linux handler for Slider control. /// public class SliderHandler : ViewHandler { public static IPropertyMapper Mapper = new PropertyMapper(ViewHandler.ViewMapper) { ["Minimum"] = MapMinimum, ["Maximum"] = MapMaximum, ["Value"] = MapValue, ["MinimumTrackColor"] = MapMinimumTrackColor, ["MaximumTrackColor"] = MapMaximumTrackColor, ["ThumbColor"] = MapThumbColor, ["Background"] = MapBackground, ["IsEnabled"] = MapIsEnabled }; public static CommandMapper CommandMapper = new(ViewHandler.ViewCommandMapper); public SliderHandler() : base(Mapper, CommandMapper) { } public SliderHandler(IPropertyMapper? mapper, CommandMapper? commandMapper = null) : base(mapper ?? Mapper, commandMapper ?? CommandMapper) { } protected override SkiaSlider CreatePlatformView() { return new SkiaSlider(); } protected override void ConnectHandler(SkiaSlider platformView) { base.ConnectHandler(platformView); platformView.ValueChanged += OnValueChanged; platformView.DragStarted += OnDragStarted; platformView.DragCompleted += OnDragCompleted; if (VirtualView != null) { MapMinimum(this, VirtualView); MapMaximum(this, VirtualView); MapValue(this, VirtualView); MapIsEnabled(this, VirtualView); } } protected override void DisconnectHandler(SkiaSlider platformView) { platformView.ValueChanged -= OnValueChanged; platformView.DragStarted -= OnDragStarted; platformView.DragCompleted -= OnDragCompleted; base.DisconnectHandler(platformView); } private void OnValueChanged(object? sender, SliderValueChangedEventArgs e) { if (VirtualView != null && PlatformView != null && Math.Abs(VirtualView.Value - e.NewValue) > 0.0001) { VirtualView.Value = e.NewValue; } } private void OnDragStarted(object? sender, EventArgs e) { VirtualView?.DragStarted(); } private void OnDragCompleted(object? sender, EventArgs e) { VirtualView?.DragCompleted(); } public static void MapMinimum(SliderHandler handler, ISlider slider) { if (handler.PlatformView != null) { handler.PlatformView.Minimum = slider.Minimum; } } public static void MapMaximum(SliderHandler handler, ISlider slider) { if (handler.PlatformView != null) { handler.PlatformView.Maximum = slider.Maximum; } } public static void MapValue(SliderHandler handler, ISlider slider) { if (handler.PlatformView != null && Math.Abs(handler.PlatformView.Value - slider.Value) > 0.0001) { handler.PlatformView.Value = slider.Value; } } public static void MapMinimumTrackColor(SliderHandler handler, ISlider slider) { if (handler.PlatformView != null && slider.MinimumTrackColor != null) { handler.PlatformView.ActiveTrackColor = slider.MinimumTrackColor.ToSKColor(); } } public static void MapMaximumTrackColor(SliderHandler handler, ISlider slider) { if (handler.PlatformView != null && slider.MaximumTrackColor != null) { handler.PlatformView.TrackColor = slider.MaximumTrackColor.ToSKColor(); } } public static void MapThumbColor(SliderHandler handler, ISlider slider) { if (handler.PlatformView != null && slider.ThumbColor != null) { handler.PlatformView.ThumbColor = slider.ThumbColor.ToSKColor(); } } public static void MapBackground(SliderHandler handler, ISlider slider) { if (handler.PlatformView != null) { if (slider.Background is SolidPaint solidPaint && solidPaint.Color != null) { handler.PlatformView.BackgroundColor = solidPaint.Color; } } } public static void MapIsEnabled(SliderHandler handler, ISlider slider) { if (handler.PlatformView != null) { handler.PlatformView.IsEnabled = slider.IsEnabled; handler.PlatformView.Invalidate(); } } }