Skip to content

ToolStripSeparator.GetPreferredSize has a suspicious always-true if check #2576

Open

Description

@hughbe

The implementation of ToolStripSeparator.GetPreferredSize is as follows:

public override Size GetPreferredSize(Size constrainingSize)
{
    ToolStrip parent = ParentInternal;

    if (parent == null)
    {
        parent = Owner;
    }
    if (parent == null)
    {
        return new Size(ToolStrip_SEPARATORTHICKNESS, ToolStrip_SEPARATORTHICKNESS);
    }

    if (parent is ToolStripDropDownMenu dropDownMenu)
    {
        return new Size(parent.Width - (parent.Padding.Horizontal - dropDownMenu.ImageMargin.Width), ToolStrip_SEPARATORTHICKNESS);
    }
    else
    {
        if (parent.LayoutStyle != ToolStripLayoutStyle.HorizontalStackWithOverflow || parent.LayoutStyle != ToolStripLayoutStyle.VerticalStackWithOverflow)
        {
            // we dont actually know what size to make it, so just keep it a stock size.
            constrainingSize.Width = ToolStrip_SEPARATORHEIGHT;
            constrainingSize.Height = ToolStrip_SEPARATORHEIGHT;
        }
        if (IsVertical)
        {
            return new Size(ToolStrip_SEPARATORTHICKNESS, constrainingSize.Height);
        }
        else
        {
            return new Size(constrainingSize.Width, ToolStrip_SEPARATORTHICKNESS);
        }
    }

}

Notice this case: if (parent.LayoutStyle != ToolStripLayoutStyle.HorizontalStackWithOverflow || parent.LayoutStyle != ToolStripLayoutStyle.VerticalStackWithOverflow)

This appears to always be true, as parent.LayoutStyle can't be both HorizontalStackWithOverflow and VerticalStackWithOverflow.

Should this actually be an && check rather than ||

Activity

  1. RussKie commented on Dec 29, 2019

    @RussKie
    Contributor

    Yes, it looks like a logic bug. Nicely spotted!

    There are two other similar looking conditions that use &&
    image

  2. added
    🪲 bug
    on Dec 29, 2019
  3. added this to the 5.0 milestone on Dec 29, 2019
  4. hughbe commented on Dec 30, 2019

    @hughbe
    ContributorAuthor

    Not sure whether this would affect various use cases. Will have to investigate further looking at layouts etc

  5. modified the milestones: 5.0 Previews 1-4, 5.0 on Apr 20, 2020
  6. modified the milestones: 5.0, 6.0 on Sep 8, 2020
  7. modified the milestones: 6.0, 7.0 on Aug 27, 2021
  8. modified the milestones: .NET 7.0, .NET 8.0 on Aug 15, 2022
  9. elachlan commented on Jan 10, 2023

    @elachlan
    Contributor

    @dreddy-work other than the review of use cases this changes seems minor. Should I put in a PR?

  10. dreddy-work commented on Jan 10, 2023

    @dreddy-work
    Member

    @dreddy-work other than the review of use cases this changes seems minor. Should I put in a PR?

    I see this was the case since .NET framework 4.0 ( may be even before). Go ahead and create PR but check if LayoutSetting can be bitwise AND anywhere.

  11. dreddy-work commented on Jan 10, 2023

    @dreddy-work
    Member

    We can validate as part of preview test pass and take it early in the previews for .NET 8.

  12. 13 remaining items

  13. modified the milestones: 8.0 RC1, on Aug 15, 2023
  14. modified the milestones: , .NET 9.0 on Aug 15, 2023
  15. ghost removed
    🚧 work in progress
    on Nov 21, 2023
  16. modified the milestones: .NET 9.0, Future on Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions