Browse Source

Simplify scroll widget (no functional changes)

Michael Ragazzon 1 year ago
parent
commit
51d34a5a0a
2 changed files with 44 additions and 61 deletions
  1. 42 52
      Source/Core/WidgetScroll.cpp
  2. 2 9
      Source/Core/WidgetScroll.h

+ 42 - 52
Source/Core/WidgetScroll.cpp

@@ -204,7 +204,7 @@ void WidgetScroll::GetDimensions(Vector2f& dimensions) const
 	}
 }
 
-void WidgetScroll::FormatElements(const Vector2f containing_block, bool resize_element, float slider_length, float bar_length)
+void WidgetScroll::FormatElements(const Vector2f containing_block, float slider_length)
 {
 	int length_axis = orientation == VERTICAL ? 1 : 0;
 
@@ -220,8 +220,7 @@ void WidgetScroll::FormatElements(const Vector2f containing_block, bool resize_e
 	content[length_axis] = slider_length;
 	parent_box.SetContent(content);
 	// And set it on the slider element!
-	if (resize_element)
-		parent->SetBox(parent_box);
+	parent->SetBox(parent_box);
 
 	// Generate the initial dimensions for the track. It'll need to be cut down to fit the arrows.
 	Box track_box;
@@ -287,10 +286,10 @@ void WidgetScroll::FormatElements(const Vector2f containing_block, bool resize_e
 		arrows[1]->SetOffset(offset, parent);
 	}
 
-	FormatBar(bar_length);
+	FormatBar();
 }
 
-void WidgetScroll::FormatBar(float bar_length)
+void WidgetScroll::FormatBar()
 {
 	Box bar_box;
 	LayoutDetails::BuildBox(bar_box, parent->GetBox().GetSize(), bar);
@@ -307,50 +306,55 @@ void WidgetScroll::FormatBar(float bar_length)
 			bar_box_content.y = parent->GetBox().GetSize().y;
 	}
 
-	if (bar_length >= 0)
-	{
-		Vector2f track_size = track->GetBox().GetSize();
+	float relative_bar_length;
+	if (track_length <= 0)
+		relative_bar_length = 1;
+	else if (bar_length <= 0)
+		relative_bar_length = 0;
+	else
+		relative_bar_length = bar_length / track_length;
 
-		if (orientation == VERTICAL)
-		{
-			float track_length = track_size.y - bar_box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin, BoxArea::Padding);
+	Vector2f track_size = track->GetBox().GetSize();
 
-			if (height.type == height.Auto)
-			{
-				bar_box_content.y = track_length * bar_length;
+	if (orientation == VERTICAL)
+	{
+		float track_length = track_size.y - bar_box.GetSizeAcross(BoxDirection::Vertical, BoxArea::Margin, BoxArea::Padding);
 
-				// Check for 'min-height' restrictions.
-				float min_track_length = ResolveValue(computed.min_height(), track_length);
-				bar_box_content.y = Math::Max(min_track_length, bar_box_content.y);
+		if (height.type == height.Auto)
+		{
+			bar_box_content.y = track_length * relative_bar_length;
 
-				// Check for 'max-height' restrictions.
-				float max_track_length = ResolveValue(computed.max_height(), track_length);
-				bar_box_content.y = Math::Min(max_track_length, bar_box_content.y);
-			}
+			// Check for 'min-height' restrictions.
+			float min_track_length = ResolveValue(computed.min_height(), track_length);
+			bar_box_content.y = Math::Max(min_track_length, bar_box_content.y);
 
-			// Make sure we haven't gone further than we're allowed to (min-height may have made us too big).
-			bar_box_content.y = Math::Min(bar_box_content.y, track_length);
+			// Check for 'max-height' restrictions.
+			float max_track_length = ResolveValue(computed.max_height(), track_length);
+			bar_box_content.y = Math::Min(max_track_length, bar_box_content.y);
 		}
-		else
-		{
-			float track_length = track_size.x - bar_box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin, BoxArea::Padding);
 
-			if (width.type == width.Auto)
-			{
-				bar_box_content.x = track_length * bar_length;
+		// Make sure we haven't gone further than we're allowed to (min-height may have made us too big).
+		bar_box_content.y = Math::Min(bar_box_content.y, track_length);
+	}
+	else
+	{
+		float track_length = track_size.x - bar_box.GetSizeAcross(BoxDirection::Horizontal, BoxArea::Margin, BoxArea::Padding);
 
-				// Check for 'min-width' restrictions.
-				float min_track_length = ResolveValue(computed.min_width(), track_length);
-				bar_box_content.x = Math::Max(min_track_length, bar_box_content.x);
+		if (width.type == width.Auto)
+		{
+			bar_box_content.x = track_length * relative_bar_length;
 
-				// Check for 'max-width' restrictions.
-				float max_track_length = ResolveValue(computed.max_width(), track_length);
-				bar_box_content.x = Math::Min(max_track_length, bar_box_content.x);
-			}
+			// Check for 'min-width' restrictions.
+			float min_track_length = ResolveValue(computed.min_width(), track_length);
+			bar_box_content.x = Math::Max(min_track_length, bar_box_content.x);
 
-			// Make sure we haven't gone further than we're allowed to (min-width may have made us too big).
-			bar_box_content.x = Math::Min(bar_box_content.x, track_length);
+			// Check for 'max-width' restrictions.
+			float max_track_length = ResolveValue(computed.max_width(), track_length);
+			bar_box_content.x = Math::Min(max_track_length, bar_box_content.x);
 		}
+
+		// Make sure we haven't gone further than we're allowed to (min-width may have made us too big).
+		bar_box_content.x = Math::Min(bar_box_content.x, track_length);
 	}
 
 	// Set the new dimensions on the bar to re-decorate it.
@@ -476,20 +480,6 @@ void WidgetScroll::SetBarLength(float _bar_length)
 	bar_length = _bar_length;
 }
 
-void WidgetScroll::FormatElements(const Vector2f containing_block, float slider_length)
-{
-	float relative_bar_length;
-
-	if (track_length <= 0)
-		relative_bar_length = 1;
-	else if (bar_length <= 0)
-		relative_bar_length = 0;
-	else
-		relative_bar_length = bar_length / track_length;
-
-	WidgetScroll::FormatElements(containing_block, true, slider_length, relative_bar_length);
-}
-
 void WidgetScroll::ScrollLineDown()
 {
 	Scroll(SCROLL_LINE_LENGTH * ElementUtilities::GetDensityIndependentPixelRatio(parent), ScrollBehavior::Auto);

+ 2 - 9
Source/Core/WidgetScroll.h

@@ -82,22 +82,15 @@ public:
 
 	/// Lays out and resizes the internal elements.
 	/// @param[in] containing_block The padded box containing the slider. This is used to resolve relative properties.
-	/// @param[in] length The total length, in pixels, of the slider widget.
+	/// @param[in] slider_length The total length, in pixels, of the slider widget.
 	void FormatElements(Vector2f containing_block, float slider_length);
 
 private:
 	/// Handles events coming through from the slider's components.
 	void ProcessEvent(Event& event) override;
 
-	/// Lays out and resizes the slider's internal elements.
-	/// @param[in] containing_block The padded box containing the slider. This is used to resolve relative properties.
-	/// @param[in] resize_element True to resize the parent slider element, false to only resize its components.
-	/// @param[in] slider_length The total length, in pixels, of the slider widget.
-	/// @param[in] bar_length The total length of the bar, as a proportion of the track length. If this is -1, the intrinsic length will be used.
-	void FormatElements(Vector2f containing_block, bool resize_element, float slider_length, float bar_length = -1);
 	/// Lays out and positions the bar element.
-	/// @param[in] bar_length The total length of the bar, as a proportion of the track length. If this is -1, the intrinsic length will be used.
-	void FormatBar(float bar_length = -1);
+	void FormatBar();
 
 	// Set the offset on 'bar' based on its position.
 	void PositionBar();