From a680b0fa705ef9a34db5bcf4f9649fb821ca9d27 Mon Sep 17 00:00:00 2001 From: Phil Hopper Date: Wed, 30 Sep 2020 11:38:40 -0400 Subject: [PATCH] Buttons not in last segment if segment less than 0.5 seconds Also fixed floating point errors that started happening after updating Windows and Visual Studio --- src/SayMore/Transcription/Model/TimeTier.cs | 2 +- .../SegmenterDlgBase.cs | 20 +++++++++++++++++-- .../Model/TierCollectionTests.cs | 6 +++--- .../Transcription/Model/TimeTierTests.cs | 12 +++++------ .../UI/SegmenterDlgBaseViewModelTests.cs | 6 ++---- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/SayMore/Transcription/Model/TimeTier.cs b/src/SayMore/Transcription/Model/TimeTier.cs index 60e29b1ae..bde32a916 100644 --- a/src/SayMore/Transcription/Model/TimeTier.cs +++ b/src/SayMore/Transcription/Model/TimeTier.cs @@ -296,7 +296,7 @@ public BoundaryModificationResult ChangeSegmentsEndBoundary(AnnotationSegment se /// ------------------------------------------------------------------------------------ public bool GetIsAcceptableSegmentLength(float start, float end) { - return end - start >= Settings.Default.MinimumSegmentLengthInMilliseconds / 1000f; + return Math.Round(end * 1000 - start * 1000) >= Settings.Default.MinimumSegmentLengthInMilliseconds; } /// ------------------------------------------------------------------------------------ diff --git a/src/SayMore/Transcription/UI/SegmentingAndRecording/SegmenterDlgBase.cs b/src/SayMore/Transcription/UI/SegmentingAndRecording/SegmenterDlgBase.cs index 8c34a65d4..8b163a3a4 100644 --- a/src/SayMore/Transcription/UI/SegmentingAndRecording/SegmenterDlgBase.cs +++ b/src/SayMore/Transcription/UI/SegmentingAndRecording/SegmenterDlgBase.cs @@ -307,11 +307,27 @@ public static Rectangle GetButtonRectangleForSegment(Rectangle rcSegment, int bo { Debug.Assert(buttonSizes.Count == 1 || indexOfFirstRightAlignedButton < buttonSizes.Count); + if (rcSegment.IsEmpty) + return Rectangle.Empty; + var btnWidth = buttonSizes[index].Width; var btnHeight = buttonSizes[index].Height; - if (rcSegment.IsEmpty || btnWidth + (kMinimalHorizontalMargin * 2) > rcSegment.Width) - return Rectangle.Empty; // No way to fit the button, even with minimal margins + var testWidth = btnWidth + (kMinimalHorizontalMargin * 2); + if (testWidth > rcSegment.Width) + { + // try shrinking the button to fit the space available + var diffX = testWidth - rcSegment.Width; + + // only allow 20% reduction in size + if (diffX / (float)btnWidth > 0.2) + return Rectangle.Empty; + + // reduce the height by the same percent + var diffY = (int)Math.Ceiling(diffX * (float)btnHeight / btnWidth); + btnWidth -= diffX; + btnHeight -= diffY; + } // Set the default top var buttonRectangleTop = rcSegment.Bottom - bottomMargin - btnHeight; diff --git a/src/SayMoreTests/Transcription/Model/TierCollectionTests.cs b/src/SayMoreTests/Transcription/Model/TierCollectionTests.cs index e280678a2..2a568ada3 100644 --- a/src/SayMoreTests/Transcription/Model/TierCollectionTests.cs +++ b/src/SayMoreTests/Transcription/Model/TierCollectionTests.cs @@ -168,7 +168,7 @@ public void InsertTierSegment_BoundaryIsSameAsExisting_ReturnsNotSuccess() [Test] public void InsertTierSegment_BoundaryIsTooCloseToPrevious_ReturnsNotSuccess() { - Assert.AreEqual(BoundaryModificationResult.SegmentWillBeTooShort, _collection.InsertTierSegment(10.49f)); + Assert.AreEqual(BoundaryModificationResult.SegmentWillBeTooShort, _collection.InsertTierSegment(10.459f)); Assert.AreEqual(BoundaryModificationResult.SegmentWillBeTooShort, _collection.InsertTierSegment(30.4f)); } @@ -176,8 +176,8 @@ public void InsertTierSegment_BoundaryIsTooCloseToPrevious_ReturnsNotSuccess() [Test] public void InsertTierSegment_BoundaryIsTooCloseToNext_ReturnsNotSuccess() { - Assert.AreEqual(BoundaryModificationResult.NextSegmentWillBeTooShort, _collection.InsertTierSegment(19.501f)); - Assert.AreEqual(BoundaryModificationResult.NextSegmentWillBeTooShort, _collection.InsertTierSegment(29.501f)); + Assert.AreEqual(BoundaryModificationResult.NextSegmentWillBeTooShort, _collection.InsertTierSegment(19.541f)); + Assert.AreEqual(BoundaryModificationResult.NextSegmentWillBeTooShort, _collection.InsertTierSegment(29.541f)); } /// ------------------------------------------------------------------------------------ diff --git a/src/SayMoreTests/Transcription/Model/TimeTierTests.cs b/src/SayMoreTests/Transcription/Model/TimeTierTests.cs index ba296ca5f..d89915110 100644 --- a/src/SayMoreTests/Transcription/Model/TimeTierTests.cs +++ b/src/SayMoreTests/Transcription/Model/TimeTierTests.cs @@ -787,15 +787,15 @@ public void ChangeSegmentsEndBoundary_WhenSegmentIsNotLast_RenamesSegAnnotationF public void InsertSegmentBoundary_NewBoundaryTooCloseToPrecedingBoundary_ReturnsNotSuccess() { Assert.AreEqual(BoundaryModificationResult.SegmentWillBeTooShort, _tier.InsertSegmentBoundary(20.4f)); - Assert.AreEqual(BoundaryModificationResult.SegmentWillBeTooShort, _tier.InsertSegmentBoundary(20.49f)); + Assert.AreEqual(BoundaryModificationResult.SegmentWillBeTooShort, _tier.InsertSegmentBoundary(20.45f)); } /// ------------------------------------------------------------------------------------ [Test] public void InsertSegmentBoundary_NewBoundaryTooCloseToFollowingBoundary_ReturnsNotSuccess() { - Assert.AreEqual(BoundaryModificationResult.NextSegmentWillBeTooShort, _tier.InsertSegmentBoundary(29.501f)); - Assert.AreEqual(BoundaryModificationResult.NextSegmentWillBeTooShort, _tier.InsertSegmentBoundary(29.51f)); + Assert.AreEqual(BoundaryModificationResult.NextSegmentWillBeTooShort, _tier.InsertSegmentBoundary(29.541f)); + Assert.AreEqual(BoundaryModificationResult.NextSegmentWillBeTooShort, _tier.InsertSegmentBoundary(29.55f)); } /// ------------------------------------------------------------------------------------ @@ -803,7 +803,7 @@ public void InsertSegmentBoundary_NewBoundaryTooCloseToFollowingBoundary_Returns public void InsertSegmentBoundary_WhenNoSegmentsAndNewBoundaryTooSmall_ReturnsNotSuccess() { _tier.Segments.Clear(); - Assert.AreEqual(BoundaryModificationResult.SegmentWillBeTooShort, _tier.InsertSegmentBoundary(0.4999f)); + Assert.AreEqual(BoundaryModificationResult.SegmentWillBeTooShort, _tier.InsertSegmentBoundary(0.4599f)); } /// ------------------------------------------------------------------------------------ @@ -902,7 +902,7 @@ public void CanBoundaryMoveLeft_MoveWillYieldNewNegativeBoundary_ReturnsFalse() public void CanBoundaryMoveLeft_MoveWillResultInTooShortSegment_ReturnsFalse() { Assert.IsFalse(_tier.CanBoundaryMoveLeft(12f, 2.0f)); - Assert.IsFalse(_tier.CanBoundaryMoveLeft(12f, 1.501f)); + Assert.IsFalse(_tier.CanBoundaryMoveLeft(12f, 1.541f)); } /// ------------------------------------------------------------------------------------ @@ -933,7 +933,7 @@ public void CanBoundaryMoveRight_MoveWillPutNewBoundaryOnLimit_ReturnsTrue() public void CanBoundaryMoveRight_MoveWillResultInTooShortSegment_ReturnsFalse() { Assert.IsFalse(_tier.CanBoundaryMoveRight(28f, 2.01f, 100f)); - Assert.IsFalse(_tier.CanBoundaryMoveRight(28f, 1.501f, 100f)); + Assert.IsFalse(_tier.CanBoundaryMoveRight(28f, 1.541f, 100f)); } /// ------------------------------------------------------------------------------------ diff --git a/src/SayMoreTests/Transcription/UI/SegmenterDlgBaseViewModelTests.cs b/src/SayMoreTests/Transcription/UI/SegmenterDlgBaseViewModelTests.cs index 064dc4d04..d0fdafbd0 100644 --- a/src/SayMoreTests/Transcription/UI/SegmenterDlgBaseViewModelTests.cs +++ b/src/SayMoreTests/Transcription/UI/SegmenterDlgBaseViewModelTests.cs @@ -1,9 +1,7 @@ using System; -using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; -using System.Runtime.InteropServices; using Moq; using NUnit.Framework; using SayMore.Model.Files; @@ -1061,7 +1059,7 @@ public void GetButtonRectangleForSegment_SingleButtonTooWide_EmptyRectangle( int buttonWidth, int buttonHeight, int bottomMargin) { // Note: Current design should prevent this. - var rect = new Rectangle(0, 0, buttonWidth + SegmenterDlgBase.kMinimalHorizontalMargin * 2 - 1, buttonHeight + bottomMargin); + var rect = new Rectangle(0, 0, buttonWidth + SegmenterDlgBase.kMinimalHorizontalMargin * 2 - (int)(buttonWidth * 0.2) - 1, buttonHeight + bottomMargin); Assert.AreEqual(Rectangle.Empty, SegmenterDlgBase.GetButtonRectangleForSegment(rect, bottomMargin, new[] {new Size(buttonWidth, buttonHeight)}, 1000, 0)); } @@ -1127,7 +1125,7 @@ public void GetButtonRectangleForSegment_SingleButtonFitsEasilyWithNormalMargins /// ------------------------------------------------------------------------------------ [TestCase(32, -8, 0, ExpectedResult = 1)] // 36 pixels visible in segment. Entire button visible. - [TestCase(10, -12, 2, ExpectedResult = 0)] // 12 pixels visible in segment. Entire button visible, no visible left margin + [TestCase(10, -12, 2, ExpectedResult = -1)] // 12 pixels visible in segment. Entire button visible, no visible left margin [TestCase(20, -28, 12, ExpectedResult = -10)] // 16 pixels visible in segment. 20-10 = 10 pixels of button visible. (Don't bother with reduced margins.) public int GetButtonRectangleForSegment_SingleButtonFitsInRectangleThatIsScrolledTooFarLeftToAllowFullLeftMargin_ReducedOrClippedLeftPosition( int buttonWidth, int boundingRectangleLeft, int extraWidth)