From 2e62263b697e4918b6a88cdc7c0a39e27b7ea474 Mon Sep 17 00:00:00 2001 From: Tig Date: Fri, 3 May 2024 12:58:56 -0600 Subject: [PATCH] Removed AutoSize from Slider --- Terminal.Gui/View/Layout/PosDim.cs | 14 +- Terminal.Gui/View/ViewContent.cs | 5 +- Terminal.Gui/Views/Slider.cs | 287 ++++++++---------------- UICatalog/Scenarios/ContentScrolling.cs | 1 - UICatalog/Scenarios/Sliders.cs | 128 ++++++----- UnitTests/Views/SliderTests.cs | 123 +++++++--- 6 files changed, 269 insertions(+), 289 deletions(-) diff --git a/Terminal.Gui/View/Layout/PosDim.cs b/Terminal.Gui/View/Layout/PosDim.cs index 38bd043c6..94fa1c6cf 100644 --- a/Terminal.Gui/View/Layout/PosDim.cs +++ b/Terminal.Gui/View/Layout/PosDim.cs @@ -960,8 +960,8 @@ public class Dim } else { - // BUGBUG: AnchorEnd needs work - // If _min > 0 we can SetRelativeLayout for the subviews? + // TODO: AnchorEnd needs work + // TODO: If _min > 0 we can SetRelativeLayout for the subviews? subviewsSize = 0; if (us.Subviews.Count > 0) { @@ -970,12 +970,12 @@ public class Dim var v = us.Subviews [i]; bool isNotPosAnchorEnd = dimension == Dim.Dimension.Width ? !(v.X is Pos.PosAnchorEnd) : !(v.Y is Pos.PosAnchorEnd); - if (!isNotPosAnchorEnd) - { - v.SetRelativeLayout(dimension == Dim.Dimension.Width ? (new Size (autoMin, 0)) : new Size (0, autoMin)); - } + //if (!isNotPosAnchorEnd) + //{ + // v.SetRelativeLayout(dimension == Dim.Dimension.Width ? (new Size (autoMin, 0)) : new Size (0, autoMin)); + //} - //if (isNotPosAnchorEnd) + if (isNotPosAnchorEnd) { int size = dimension == Dim.Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height; if (size > subviewsSize) diff --git a/Terminal.Gui/View/ViewContent.cs b/Terminal.Gui/View/ViewContent.cs index 966cb0c02..ba17287b6 100644 --- a/Terminal.Gui/View/ViewContent.cs +++ b/Terminal.Gui/View/ViewContent.cs @@ -170,8 +170,9 @@ public partial class View if (e.Cancel != true) { - SetNeedsLayout (); - SetNeedsDisplay (); + OnResizeNeeded (); + //SetNeedsLayout (); + //SetNeedsDisplay (); } return e.Cancel; diff --git a/Terminal.Gui/Views/Slider.cs b/Terminal.Gui/Views/Slider.cs index c6d9b4e34..992c72752 100644 --- a/Terminal.Gui/Views/Slider.cs +++ b/Terminal.Gui/Views/Slider.cs @@ -150,7 +150,6 @@ public class SliderStyle internal class SliderConfiguration { internal bool _allowEmpty; - internal bool _autoSize; internal int _endSpacing; internal int _innerSpacing; internal Orientation _legendsOrientation = Orientation.Horizontal; @@ -243,6 +242,8 @@ public class Slider : View Orientation orientation = Orientation.Horizontal ) { + Width = Dim.Auto (Dim.DimAutoStyle.Content); + Height = Dim.Auto (Dim.DimAutoStyle.Content); CanFocus = true; _options = options ?? new List> (); @@ -268,13 +269,11 @@ public class Slider : View // BUGBUG: This should not be needed - Need to ensure SetRelativeLayout gets called during EndInit Initialized += (s, e) => { - CalcSpacingConfig (); SetContentSizeBestFit (); }; LayoutStarted += (s, e) => { - CalcSpacingConfig (); SetContentSizeBestFit (); }; @@ -381,31 +380,6 @@ public class Slider : View } } - /// - /// If , and will be automatically set - /// such that the slider will be optimally sized to fit the options using the various slider settings. - /// - [ObsoleteAttribute ("Use Dim.Auto instead.", false)] - public override bool AutoSize - { - get => _config._autoSize; - set - { - _config._autoSize = value; - - if (value) - { - Width = Dim.Auto (Dim.DimAutoStyle.Content); - Height = Dim.Auto (Dim.DimAutoStyle.Content); - } - else - { - Width = ContentSize.GetValueOrDefault ().Width; - Height = ContentSize.GetValueOrDefault ().Height; - } - } - } - /// Gets or sets the number of rows/columns between public int InnerSpacing { @@ -414,11 +388,7 @@ public class Slider : View { _config._innerSpacing = value; - if (IsInitialized) - { - CalcSpacingConfig (); - SetContentSizeBestFit (); - } + SetContentSizeBestFit (); } } @@ -462,11 +432,7 @@ public class Slider : View _config._sliderOrientation = newOrientation; SetKeyBindings (); - if (IsInitialized) - { - CalcSpacingConfig (); - SetContentSizeBestFit (); - } + SetContentSizeBestFit (); } return args.Cancel; @@ -480,11 +446,7 @@ public class Slider : View { _config._legendsOrientation = value; - if (IsInitialized) - { - CalcSpacingConfig (); - SetContentSizeBestFit (); - } + SetContentSizeBestFit (); } } @@ -516,7 +478,6 @@ public class Slider : View return; } - CalcSpacingConfig (); SetContentSizeBestFit (); } } @@ -654,168 +615,110 @@ public class Slider : View // Last = '┤', } - /// - /// Calculates the spacing configuration (start, inner, end) as well as turning on/off legend abbreviation if - /// needed. Behaves differently based on and . - /// - internal void CalcSpacingConfig () - { - var size = 0; - - if (_options.Count == 0 || !IsInitialized) - { - return; - } - - _config._innerSpacing = 0; - _config._startSpacing = 0; - _config._endSpacing = 0; - - if (AutoSize) - { - // Max size is SuperView's Viewport. Min Size is size that will fit. - if (SuperView is { }) - { - // Calculate the size of the slider based on the size of the SuperView's Viewport. - if (_config._sliderOrientation == Orientation.Horizontal) - { - size = int.Min (SuperView.Viewport.Width, CalcBestLength ()); - } - else - { - size = int.Min (SuperView.Viewport.Height, CalcBestLength ()); - } - } - else - { - // Use the config values - size = CalcMinLength (); - - return; - } - } - else - { - // Fit Slider to the Viewport - if (_config._sliderOrientation == Orientation.Horizontal) - { - size = Viewport.Width; - } - else - { - size = Viewport.Height; - } - } - - int max_legend; // Because the legends are centered, the longest one determines inner spacing - - if (_config._sliderOrientation == _config._legendsOrientation) - { - max_legend = int.Max (_options.Max (s => s.Legend?.Length ?? 1), 1); - } - else - { - max_legend = 1; - } - - int min_size_that_fits_legends = _options.Count == 1 ? max_legend : max_legend / (_options.Count - 1); - - string first; - string last; - - if (max_legend >= size) - { - if (_config._sliderOrientation == _config._legendsOrientation) - { - _config._showLegendsAbbr = true; - - foreach (SliderOption o in _options.Where (op => op.LegendAbbr == default (Rune))) - { - o.LegendAbbr = (Rune)(o.Legend?.Length > 0 ? o.Legend [0] : ' '); - } - } - - first = "x"; - last = "x"; - } - else - { - _config._showLegendsAbbr = false; - first = _options.First ().Legend; - last = _options.Last ().Legend; - } - - // --o-- - // Hello - // Left = He - // Right = lo - int first_left = (first.Length - 1) / 2; // Chars count of the first option to the left. - int last_right = last.Length / 2; // Chars count of the last option to the right. - - if (_config._sliderOrientation != _config._legendsOrientation) - { - first_left = 0; - last_right = 0; - } - - // -1 because it's better to have an extra space at right than to clip - int width = size - first_left - last_right - 1; - - _config._startSpacing = first_left; - - if (_options.Count == 1) - { - _config._innerSpacing = max_legend; - } - else - { - _config._innerSpacing = Math.Max (0, (int)Math.Floor ((double)width / (_options.Count - 1)) - 1); - } - - _config._endSpacing = last_right; - } - /// Adjust the dimensions of the Slider to the best value if is true. public void SetContentSizeBestFit () { - if (!IsInitialized || !(Height is Dim.DimAuto && Width is Dim.DimAuto)) + if (!IsInitialized || /*!(Height is Dim.DimAuto && Width is Dim.DimAuto) || */_options.Count == 0) { return; } + CalcSpacingConfig (); + Thickness adornmentsThickness = GetAdornmentsThickness (); + var svWidth = SuperView?.ContentSize?.Width ?? 0; + var svHeight = SuperView?.ContentSize?.Height ?? 0; + if (_config._sliderOrientation == Orientation.Horizontal) { - // BUGBUG: For this View, ContentSize == Viewport.Size, so this works. But for correctness we should be setting ContentSize here - Viewport = new ( - Viewport.Location, - new ( - int.Min ( - SuperView.ContentSize.GetValueOrDefault ().Width - adornmentsThickness.Horizontal, - CalcBestLength () - ), - int.Min ( - SuperView.ContentSize.GetValueOrDefault ().Height - adornmentsThickness.Vertical, - CalcThickness () - ) - ) - ); + ContentSize = new (int.Min (svWidth, CalcBestLength ()), int.Min (svHeight, CalcThickness ())); } else { - ContentSize = new ( - new ( - int.Min ( - SuperView.ContentSize.GetValueOrDefault ().Width - adornmentsThickness.Horizontal, - CalcThickness () - ), - int.Min ( - SuperView.ContentSize.GetValueOrDefault ().Height - adornmentsThickness.Vertical, - CalcBestLength () - ) - ) - ); + ContentSize = new (int.Min (svWidth, CalcThickness ()), int.Min (svHeight, CalcBestLength ())); + } + + return; + + void CalcSpacingConfig () + { + _config._innerSpacing = 0; + _config._startSpacing = 0; + _config._endSpacing = 0; + + int size = 0; + if (ContentSize is { }) + { + size = _config._sliderOrientation == Orientation.Horizontal ? ContentSize.Value.Width : ContentSize.Value.Height; + } + + int max_legend; // Because the legends are centered, the longest one determines inner spacing + + if (_config._sliderOrientation == _config._legendsOrientation) + { + max_legend = int.Max (_options.Max (s => s.Legend?.Length ?? 1), 1); + } + else + { + max_legend = 1; + } + + int min_size_that_fits_legends = _options.Count == 1 ? max_legend : max_legend / (_options.Count - 1); + + string first; + string last; + + if (max_legend >= size) + { + if (_config._sliderOrientation == _config._legendsOrientation) + { + _config._showLegendsAbbr = true; + + foreach (SliderOption o in _options.Where (op => op.LegendAbbr == default (Rune))) + { + o.LegendAbbr = (Rune)(o.Legend?.Length > 0 ? o.Legend [0] : ' '); + } + } + + first = "x"; + last = "x"; + } + else + { + _config._showLegendsAbbr = false; + first = _options.First ().Legend; + last = _options.Last ().Legend; + } + + // --o-- + // Hello + // Left = He + // Right = lo + int first_left = (first.Length - 1) / 2; // Chars count of the first option to the left. + int last_right = last.Length / 2; // Chars count of the last option to the right. + + if (_config._sliderOrientation != _config._legendsOrientation) + { + first_left = 0; + last_right = 0; + } + + // -1 because it's better to have an extra space at right than to clip + int width = size - first_left - last_right - 1; + + _config._startSpacing = first_left; + + if (_options.Count == 1) + { + _config._innerSpacing = max_legend; + } + else + { + _config._innerSpacing = Math.Max (0, (int)Math.Floor ((double)width / (_options.Count - 1)) - 1); + } + + _config._endSpacing = last_right; } } diff --git a/UICatalog/Scenarios/ContentScrolling.cs b/UICatalog/Scenarios/ContentScrolling.cs index 79128e7ee..cbbde81b7 100644 --- a/UICatalog/Scenarios/ContentScrolling.cs +++ b/UICatalog/Scenarios/ContentScrolling.cs @@ -390,7 +390,6 @@ public class ContentScrolling : Scenario { X = 0, Y = Pos.Bottom (textField) + 1, - AutoSize = true, Orientation = Orientation.Vertical, Type = SliderType.Multiple, AllowEmpty = false, diff --git a/UICatalog/Scenarios/Sliders.cs b/UICatalog/Scenarios/Sliders.cs index 5811582a0..08b5d6fda 100644 --- a/UICatalog/Scenarios/Sliders.cs +++ b/UICatalog/Scenarios/Sliders.cs @@ -101,7 +101,10 @@ public class Sliders : Scenario v.Add (single); - single.OptionsChanged += (s, e) => { single.Title = $"Continuous {e.Options.FirstOrDefault ().Key}"; }; + single.OptionsChanged += (s, e) => + { + single.Title = $"Continuous {e.Options.FirstOrDefault ().Key}"; + }; List oneOption = new () { "The Only Option" }; @@ -150,41 +153,43 @@ public class Sliders : Scenario #region Config Slider - Slider slider = new () + Slider optionsSlider = new () { Title = "Options", X = 0, Y = 0, Type = SliderType.Multiple, - Width = Dim.Fill (), - Height = 4, AllowEmpty = true, BorderStyle = LineStyle.Single }; - slider.Style.SetChar.Attribute = new Attribute (Color.BrightGreen, Color.Black); - slider.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Black); + optionsSlider.Style.SetChar.Attribute = new Attribute (Color.BrightGreen, Color.Black); + optionsSlider.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Black); - slider.Options = new List> + optionsSlider.Options = new List> { new () { Legend = "Legends" }, new () { Legend = "RangeAllowSingle" }, new () { Legend = "EndSpacing" }, - new () { Legend = "AutoSize" } + new () { Legend = "DimAuto" } }; - configView.Add (slider); + configView.Add (optionsSlider); - slider.OptionsChanged += (sender, e) => + optionsSlider.OptionsChanged += (sender, e) => { foreach (Slider s in Win.Subviews.OfType ()) { s.ShowLegends = e.Options.ContainsKey (0); s.RangeAllowSingle = e.Options.ContainsKey (1); s.ShowEndSpacing = e.Options.ContainsKey (2); - s.AutoSize = e.Options.ContainsKey (3); - if (!s.AutoSize) + if (e.Options.ContainsKey (3)) + { + s.Width = Dim.Auto (Dim.DimAutoStyle.Content); + s.Height = Dim.Auto (Dim.DimAutoStyle.Content); + } + else { if (s.Orientation == Orientation.Horizontal) { @@ -209,28 +214,25 @@ public class Sliders : Scenario Win.LayoutSubviews (); } }; - slider.SetOption (0); // Legends - slider.SetOption (1); // RangeAllowSingle - - //slider.SetOption (3); // AutoSize + optionsSlider.SetOption (0); // Legends + optionsSlider.SetOption (1); // RangeAllowSingle + optionsSlider.SetOption (3); // DimAuto #region Slider Orientation Slider - Slider slider_orientation_slider = new (new List { "Horizontal", "Vertical" }) + Slider orientationSlider = new (new List { "Horizontal", "Vertical" }) { Title = "Slider Orientation", X = 0, - Y = Pos.Bottom (slider) + 1, - Width = Dim.Fill (), - Height = 4, + Y = Pos.Bottom (optionsSlider) + 1, BorderStyle = LineStyle.Single }; - slider_orientation_slider.SetOption (0); + orientationSlider.SetOption (0); - configView.Add (slider_orientation_slider); + configView.Add (orientationSlider); - slider_orientation_slider.OptionsChanged += (sender, e) => + orientationSlider.OptionsChanged += (sender, e) => { View prev = null; @@ -273,20 +275,28 @@ public class Sliders : Scenario prev = s; } - if (s.Orientation == Orientation.Horizontal) + if (optionsSlider.GetSetOptions ().Contains (3)) { - s.Width = Dim.Percent (50); - - int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical - ? s.Options.Max (o => o.Legend.Length) + 3 - : 4; - s.Height = h; + s.Width = Dim.Auto (Dim.DimAutoStyle.Content); + s.Height = Dim.Auto (Dim.DimAutoStyle.Content); } else { - int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3; - s.Width = w; - s.Height = Dim.Fill (); + if (s.Orientation == Orientation.Horizontal) + { + s.Width = Dim.Percent (50); + + int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical + ? s.Options.Max (o => o.Legend.Length) + 3 + : 4; + s.Height = h; + } + else + { + int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3; + s.Width = w; + s.Height = Dim.Fill (); + } } } @@ -297,21 +307,19 @@ public class Sliders : Scenario #region Legends Orientation Slider - Slider legends_orientation_slider = new (new List { "Horizontal", "Vertical" }) + Slider legendsOrientationSlider = new (new List { "Horizontal", "Vertical" }) { Title = "Legends Orientation", - X = Pos.Center (), - Y = Pos.Bottom (slider_orientation_slider) + 1, - Width = Dim.Fill (), - Height = 4, + X = 0, + Y = Pos.Bottom (orientationSlider) + 1, BorderStyle = LineStyle.Single }; - legends_orientation_slider.SetOption (0); + legendsOrientationSlider.SetOption (0); - configView.Add (legends_orientation_slider); + configView.Add (legendsOrientationSlider); - legends_orientation_slider.OptionsChanged += (sender, e) => + legendsOrientationSlider.OptionsChanged += (sender, e) => { foreach (Slider s in Win.Subviews.OfType ()) { @@ -324,20 +332,28 @@ public class Sliders : Scenario s.LegendsOrientation = Orientation.Vertical; } - if (s.Orientation == Orientation.Horizontal) + if (optionsSlider.GetSetOptions ().Contains (3)) { - s.Width = Dim.Percent (50); - - int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical - ? s.Options.Max (o => o.Legend.Length) + 3 - : 4; - s.Height = h; + s.Width = Dim.Auto (Dim.DimAutoStyle.Content); + s.Height = Dim.Auto (Dim.DimAutoStyle.Content); } else { - int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3; - s.Width = w; - s.Height = Dim.Fill (); + if (s.Orientation == Orientation.Horizontal) + { + s.Width = Dim.Percent (50); + + int h = s.ShowLegends && s.LegendsOrientation == Orientation.Vertical + ? s.Options.Max (o => o.Legend.Length) + 3 + : 4; + s.Height = h; + } + else + { + int w = s.ShowLegends ? s.Options.Max (o => o.Legend.Length) + 3 : 3; + s.Width = w; + s.Height = Dim.Fill (); + } } } @@ -361,16 +377,14 @@ public class Sliders : Scenario Title = "FG Color", X = 0, Y = Pos.Bottom ( - legends_orientation_slider + legendsOrientationSlider ) + 1, Type = SliderType.Single, BorderStyle = LineStyle.Single, AllowEmpty = false, Orientation = Orientation.Vertical, - LegendsOrientation = - Orientation.Horizontal, - AutoSize = true + LegendsOrientation = Orientation.Horizontal, }; sliderFGColor.Style.SetChar.Attribute = new Attribute (Color.BrightGreen, Color.Black); @@ -441,9 +455,7 @@ public class Sliders : Scenario BorderStyle = LineStyle.Single, AllowEmpty = false, Orientation = Orientation.Vertical, - LegendsOrientation = - Orientation.Horizontal, - AutoSize = true + LegendsOrientation = Orientation.Horizontal, }; sliderBGColor.Style.SetChar.Attribute = new Attribute (Color.BrightGreen, Color.Black); diff --git a/UnitTests/Views/SliderTests.cs b/UnitTests/Views/SliderTests.cs index c8f472b1e..04cc19191 100644 --- a/UnitTests/Views/SliderTests.cs +++ b/UnitTests/Views/SliderTests.cs @@ -61,9 +61,9 @@ public class SliderOptionTests [Fact] public void Slider_Option_Values_Constructor () { - SliderOption o = new ("1 thousand", new Rune ('y'), 1000); + SliderOption o = new ("1 thousand", new ('y'), 1000); Assert.Equal ("1 thousand", o.Legend); - Assert.Equal (new Rune ('y'), o.LegendAbbr); + Assert.Equal (new ('y'), o.LegendAbbr); Assert.Equal (1000, o.Data); } @@ -77,7 +77,7 @@ public class SliderOptionTests [Fact] public void SliderOption_ToString_WhenPopulated_WithInt () { - SliderOption sliderOption = new () { Legend = "Lord flibble", LegendAbbr = new Rune ('l'), Data = 1 }; + SliderOption sliderOption = new () { Legend = "Lord flibble", LegendAbbr = new ('l'), Data = 1 }; Assert.Equal ("{Legend=Lord flibble, LegendAbbr=l, Data=1}", sliderOption.ToString ()); } @@ -87,7 +87,7 @@ public class SliderOptionTests { SliderOption sliderOption = new () { - Legend = "Lord flibble", LegendAbbr = new Rune ('l'), Data = new SizeF (32, 11) + Legend = "Lord flibble", LegendAbbr = new ('l'), Data = new (32, 11) }; Assert.Equal ("{Legend=Lord flibble, LegendAbbr=l, Data={Width=32, Height=11}}", sliderOption.ToString ()); @@ -156,7 +156,8 @@ public class SliderTests Assert.False (slider.ShowEndSpacing); Assert.Equal (SliderType.Single, slider.Type); Assert.Equal (0, slider.InnerSpacing); - Assert.False (slider.AutoSize); + Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Content), slider.Width); + Assert.Equal (Dim.Auto (Dim.DimAutoStyle.Content), slider.Height); Assert.Equal (0, slider.FocusedOption); } @@ -179,7 +180,7 @@ public class SliderTests public void MovePlus_Should_MoveFocusRight_When_OptionIsAvailable () { // Arrange - Slider slider = new (new List { 1, 2, 3, 4 }); + Slider slider = new (new() { 1, 2, 3, 4 }); slider.AutoSize = true; // Act @@ -194,7 +195,7 @@ public class SliderTests public void MovePlus_Should_NotMoveFocusRight_When_AtEnd () { // Arrange - Slider slider = new (new List { 1, 2, 3, 4 }); + Slider slider = new (new() { 1, 2, 3, 4 }); slider.AutoSize = true; slider.FocusedOption = 3; @@ -210,7 +211,7 @@ public class SliderTests public void OnOptionFocused_Event_Cancelled () { // Arrange - Slider slider = new (new List { 1, 2, 3 }); + Slider slider = new (new() { 1, 2, 3 }); var eventRaised = false; var cancel = false; slider.OptionFocused += (sender, args) => eventRaised = true; @@ -220,7 +221,7 @@ public class SliderTests cancel = false; SliderEventArgs args = - new (new Dictionary> (), newFocusedOption) { Cancel = cancel }; + new (new (), newFocusedOption) { Cancel = cancel }; Assert.Equal (0, slider.FocusedOption); // Act @@ -233,7 +234,7 @@ public class SliderTests // Create args with cancel set to true cancel = true; - args = new SliderEventArgs (new Dictionary> (), newFocusedOption) + args = new (new (), newFocusedOption) { Cancel = cancel }; @@ -250,11 +251,11 @@ public class SliderTests public void OnOptionFocused_Event_Raised () { // Arrange - Slider slider = new (new List { 1, 2, 3 }); + Slider slider = new (new() { 1, 2, 3 }); var eventRaised = false; slider.OptionFocused += (sender, args) => eventRaised = true; var newFocusedOption = 1; - SliderEventArgs args = new (new Dictionary> (), newFocusedOption); + SliderEventArgs args = new (new (), newFocusedOption); // Act slider.OnOptionFocused (newFocusedOption, args); @@ -282,7 +283,7 @@ public class SliderTests public void Set_Should_Not_UnSetFocusedOption_When_EmptyNotAllowed () { // Arrange - Slider slider = new (new List { 1, 2, 3, 4 }) { AllowEmpty = false }; + Slider slider = new (new() { 1, 2, 3, 4 }) { AllowEmpty = false }; slider.AutoSize = true; Assert.NotEmpty (slider.GetSetOptions ()); @@ -301,7 +302,7 @@ public class SliderTests public void Set_Should_SetFocusedOption () { // Arrange - Slider slider = new (new List { 1, 2, 3, 4 }); + Slider slider = new (new() { 1, 2, 3, 4 }); slider.AutoSize = true; // Act @@ -318,7 +319,7 @@ public class SliderTests public void TryGetOptionByPosition_InvalidPosition_Failure () { // Arrange - Slider slider = new (new List { 1, 2, 3 }); + Slider slider = new (new() { 1, 2, 3 }); var x = 10; var y = 10; var threshold = 2; @@ -342,7 +343,7 @@ public class SliderTests public void TryGetOptionByPosition_ValidPositionHorizontal_Success (int x, int y, int threshold, int expectedData) { // Arrange - Slider slider = new (new List { 1, 2, 3, 4 }); + Slider slider = new (new() { 1, 2, 3, 4 }); slider.AutoSize = true; // Set auto size to true to enable testing slider.InnerSpacing = 2; @@ -369,7 +370,7 @@ public class SliderTests public void TryGetOptionByPosition_ValidPositionVertical_Success (int x, int y, int threshold, int expectedData) { // Arrange - Slider slider = new (new List { 1, 2, 3, 4 }); + Slider slider = new (new() { 1, 2, 3, 4 }); slider.Orientation = Orientation.Vertical; slider.AutoSize = true; // Set auto size to true to enable testing slider.InnerSpacing = 2; @@ -397,7 +398,7 @@ public class SliderTests public void TryGetPositionByOption_InvalidOption_Failure () { // Arrange - Slider slider = new (new List { 1, 2, 3 }); + Slider slider = new (new() { 1, 2, 3 }); int option = -1; (int, int) expectedPosition = (-1, -1); @@ -416,7 +417,7 @@ public class SliderTests public void TryGetPositionByOption_ValidOptionHorizontal_Success (int option, int expectedX, int expectedY) { // Arrange - Slider slider = new (new List { 1, 2, 3, 4 }); + Slider slider = new (new() { 1, 2, 3, 4 }); slider.AutoSize = true; // Set auto size to true to enable testing slider.InnerSpacing = 2; @@ -439,7 +440,7 @@ public class SliderTests public void TryGetPositionByOption_ValidOptionVertical_Success (int option, int expectedX, int expectedY) { // Arrange - Slider slider = new (new List { 1, 2, 3, 4 }); + Slider slider = new (new() { 1, 2, 3, 4 }); slider.Orientation = Orientation.Vertical; slider.AutoSize = true; // Set auto size to true to enable testing slider.InnerSpacing = 2; @@ -463,7 +464,7 @@ public class SliderTests slider.EndInit (); // Act/Assert - slider.Options = new List> { new () }; + slider.Options = new() { new () }; } [Fact] @@ -487,30 +488,27 @@ public class SliderTests } [Fact] - private void AutoSize_Respects_SuperView_ContentSize () + private void DimAuto_Both_Respects_SuperView_ContentSize () { View view = new () { Width = Dim.Fill (), - Height = Dim.Fill (), + Height = Dim.Fill () }; List options = new () { "01234", "01234" }; + Slider slider = new (options) { Orientation = Orientation.Vertical, Type = SliderType.Multiple, Width = Dim.Auto (Dim.DimAutoStyle.Content), - Height = Dim.Auto (Dim.DimAutoStyle.Content), + Height = Dim.Auto (Dim.DimAutoStyle.Content) }; view.Add (slider); view.BeginInit (); view.EndInit (); - // BUGBUG: This should not be needed. EndInit should have called LayoutSubviews - // BUGBUG: and LayoutSubviews should have - view.LayoutSubviews (); - Size expectedSize = slider.Frame.Size; Assert.Equal (new (6, 2), expectedSize); @@ -520,8 +518,75 @@ public class SliderTests view.LayoutSubviews (); slider.SetRelativeLayout (view.Viewport.Size); - Assert.Equal(new (1, 1), slider.Frame.Size); + Assert.Equal (new (1, 1), slider.Frame.Size); + } + [Fact] + private void DimAuto_Width_Respects_SuperView_ContentSize () + { + View view = new () + { + Width = Dim.Fill (), + Height = 10 + }; + + List options = new () { "01234", "01234" }; + + Slider slider = new (options) + { + Orientation = Orientation.Vertical, + Type = SliderType.Multiple, + Width = Dim.Auto (Dim.DimAutoStyle.Content), + Height = 10 + }; + view.Add (slider); + view.BeginInit (); + view.EndInit (); + + Size expectedSize = slider.Frame.Size; + + Assert.Equal (new (6, 10), expectedSize); + + view.ContentSize = new (1, 1); + + view.LayoutSubviews (); + slider.SetRelativeLayout (view.Viewport.Size); + + Assert.Equal (new (1, 10), slider.Frame.Size); + } + + [Fact] + private void DimAuto_Height_Respects_SuperView_ContentSize () + { + View view = new () + { + Width = 10, + Height = Dim.Fill () + }; + + List options = new () { "01234", "01234" }; + + Slider slider = new (options) + { + Orientation = Orientation.Vertical, + Type = SliderType.Multiple, + Width = 10, + Height = Dim.Auto (Dim.DimAutoStyle.Content) + }; + view.Add (slider); + view.BeginInit (); + view.EndInit (); + + Size expectedSize = slider.Frame.Size; + + Assert.Equal (new (10, 2), expectedSize); + + view.ContentSize = new (1, 1); + + view.LayoutSubviews (); + slider.SetRelativeLayout (view.Viewport.Size); + + Assert.Equal (new (10, 1), slider.Frame.Size); } // Add more tests for different scenarios and edge cases.