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

Rectangle doesn't rerender when Stroke's DynamicResource SolidColorBrush changes #12408

Closed
SKProCH opened this issue Aug 1, 2023 · 4 comments
Assignees

Comments

@SKProCH
Copy link
Contributor

SKProCH commented Aug 1, 2023

Describe the bug
When i change color of SolidColorBrush, which bind with DynamicResource to Rectangle's Stroke, then Rectangle doesn't rerender.

To Reproduce
Steps to reproduce the behavior:
Repro project: AvaloniaApplication1.zip

  1. Add a SolidColorBrush to resources (for example in App)
    <Application.Resources>
      <SolidColorBrush x:Key="TestBrush" Color="Red" />
    </Application.Resources>
  2. Add a rect, set Stroke property
    <Rectangle Stroke="{StaticResource TestBrush}" StrokeThickness="1" Height="10"></Rectangle>
  3. Add a way to change TestBrush color (button callback in my case)
    private void Button_OnClick(object? sender, RoutedEventArgs e) {
        var solidColorBrush = App.Current.Resources.First().Value as SolidColorBrush;
        var random = new Random();
        solidColorBrush.Color = Color.FromRgb((byte)random.Next(0, 256), (byte)random.Next(0, 256), (byte)random.Next(0, 256));
    }
  4. Launch app. click button, see nothing changed.

Expected behavior
Rectangle should rerender when Stroke SolidColorBrush Color changed

Screenshots

3af0a3e9-665a-4e66-88d6-65d447ab4dee.mp4

Desktop (please complete the following information):

  • Version: 11.0.1

Additional context
Rectangle.Fill property working fine.
Also related to #11673

@SKProCH
Copy link
Contributor Author

SKProCH commented Nov 23, 2023

Additional info: when we have different ThemeDictionaries with target brush, switching between them works fine for Rectangle.Stroke.

But issue still valid and plenty of people searched for fix in Material.Avalonia.

@SKProCH
Copy link
Contributor Author

SKProCH commented Dec 4, 2023

Seems like this is affects Border's BorderBrush as well. @kekekeks there is no other options to property draw borders with changeable SolidColorBrush.

Original issue about Border reported here: AvaloniaCommunity/Material.Avalonia#325

Also, I've updated an repro and put it into a git repository: https://github.com/SKProCH/AvaloniaBugsRepros (BorderBindingToSolidColorBrush project) to provide an easy way to reproduce it.
Current latest Avalonia version (11.0.5) are affected too.

Animation.mp4

@SKProCH
Copy link
Contributor Author

SKProCH commented Dec 17, 2023

@kekekeks seems like i found a root cause of this problem.

As far as I know, Render method now only define how the control should be rendered by composition renderer, since it called once and other brushes updates do not call the Render method (but control actually re-rendered when Fill brush changes by the compositor).

If we look at the code for rendering Shape:

pen = new ImmutablePen(
stroke.ToImmutable(),
StrokeThickness,
dashStyle,
StrokeLineCap,
StrokeJoin);
}
context.DrawGeometry(Fill, pen, geometry);

We pass the Fill brush directly to DrawGeometry, but for the Stroke property we calls .ToImmutable(). I think what this is the reason why composition renderer property re-render control when Fill brush value changed, but do not properly re-render control when Stroke brush value changes - cuz we pass the immutable value to DrawingContext.

So, if we change it to:

    pen = new Pen(
        stroke,
        StrokeThickness,
        dashStyle,
        StrokeLineCap,
        StrokeJoin);
}
context.DrawGeometry(Fill, pen, geometry);

everything start works properly.

@SKProCH
Copy link
Contributor Author

SKProCH commented Dec 26, 2023

Fixed in master branch - 11.1.999-cibuild0043160-beta

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants