Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounding box for label does not set focus #17701

Open
RivenSkaye opened this issue Dec 5, 2024 · 9 comments
Open

Bounding box for label does not set focus #17701

RivenSkaye opened this issue Dec 5, 2024 · 9 comments
Labels
bug help-wanted A contribution from the community would be most welcome.

Comments

@RivenSkaye
Copy link

RivenSkaye commented Dec 5, 2024

Describe the bug

When adding a label and another element to a document tree, the label's bounding box does not trigger focus onto the other element unless its background property has been set. The label's inner text does trigger the focus change regardless.

To Reproduce

Shove this into a Window and click anywhere in the bounding box of either label, but not on the text itself. Setting the background to Transparent works, but leaving it out does not.

Violet is for illustrative purposes - it visualizes the bounding box size

<Grid Width="{Binding $parent.Bounds.Width}" ColumnDefinitions="Auto, 120, Auto, 120"
          HorizontalAlignment="Center">
      <Label VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center"
             Content="Clicking outside the text does not change focus"
             Height="{Binding $parent.Bounds.Height}" Target="FactInput"
             HorizontalContentAlignment="Center" Grid.Column="0" />
      <TextBox Grid.Column="1" Watermark="I don't get focus" Name="FactInput" />
      <Label VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center"
             Content="This works, note the next property" Background="Violet"
             Height="{Binding $parent.Bounds.Height}"  Target="OrderInput"
             HorizontalContentAlignment="Center" Grid.Column="2" />
      <TextBox Grid.Column="3" Watermark="I do get focus" Name="OrderInput" />
</Grid>

Expected behavior

Focus shifting when clicking anywhere in the bounding box

Avalonia version

Avalonia 11.2.1

OS

Windows

Additional context

I have not tested other OSes, but I assume it might be affected there as well.
Perhaps changing Background on Label to default to Transparent would be a good fix here?

@RivenSkaye RivenSkaye added the bug label Dec 5, 2024
@timunie
Copy link
Contributor

timunie commented Dec 5, 2024

Yes, it needs a transparent background for hittest to work. That's by design. Updating the style however may be a good idea 🤔

@RivenSkaye
Copy link
Author

Ah, makes sense. It was surprising though, having a block element with no initial hitbox when its entire purpose is making it easier to select another element with a smaller hitbox.

Will close this issue anyways, as this is intended behavior then.

@timunie
Copy link
Contributor

timunie commented Dec 6, 2024

I meant it's intended that it needs a Background. So we may want to add it by default, might be an oversight when it was first created. Feel free to provide a PR.

@timunie timunie added the help-wanted A contribution from the community would be most welcome. label Dec 6, 2024
@timunie timunie reopened this Dec 6, 2024
@KieranDevvs
Copy link
Contributor

@timunie Is this as simple as setting the background as transparent in the label constructor so that all label have a transparent background rather than none/null?

        /// <summary>
        /// Initializes instance of <see cref="Label"/> control
        /// </summary>
        public Label()
        {
+           Background = Brushes.Transparent;
        }

@timunie
Copy link
Contributor

timunie commented Dec 22, 2024

In the constructor would be a bad idea. Add it here as setter:

https://github.com/AvaloniaUI/Avalonia/blob/master/src%2FAvalonia.Themes.Fluent%2FControls%2FLabel.xaml#L5-L6

Need also to be updated for simple theme if you want to file a PR

@KieranDevvs
Copy link
Contributor

KieranDevvs commented Dec 22, 2024

In the constructor would be a bad idea. Add it here as setter:

https://github.com/AvaloniaUI/Avalonia/blob/master/src%2FAvalonia.Themes.Fluent%2FControls%2FLabel.xaml#L5-L6

Need also to be updated for simple theme if you want to file a PR

Why would it be better to add the default behaviour to a theme rather than the base properties of the control itself?
I'm just thinking of cases where users don't want to use the default fluent theme and now they lose this standard default behaviour.

@timunie
Copy link
Contributor

timunie commented Dec 22, 2024

In that case their theme should have it.

@KieranDevvs
Copy link
Contributor

In that case their theme should have it.

Perhaps that's the case but that doesn't help me understand why?
All I'm seeing is that this would violate polymorphic and DRY principles by forcing the implementer to add the behaviour rather than the base, thus if the behaviour changes, it needs to be made in multiple places. I'm sure there's a valid reason why it should be done this way, I'm just not currently seeing it?

@timunie
Copy link
Contributor

timunie commented Dec 22, 2024

Okay let me try to explain it a bit more. Setting it as you did will cause it to be a local set property which then can't be changed except for other local set values. So that will break all styles that have set it before.

If anything we could consider is to override default metadata. Not sure if that's a breaking change or not.

See: https://docs.avaloniaui.net/docs/guides/custom-controls/how-to-create-advanced-custom-controls

https://docs.avaloniaui.net/docs/guides/styles-and-resources/setter-precedence#bindingpriority-values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help-wanted A contribution from the community would be most welcome.
Projects
None yet
Development

No branches or pull requests

3 participants