Skip to content

AvaPlot: Always render to new layer#4759

Merged
swharden merged 4 commits intoScottPlot:mainfrom
bclehmann:fix/avaplot-transparency
Feb 15, 2025
Merged

AvaPlot: Always render to new layer#4759
swharden merged 4 commits intoScottPlot:mainfrom
bclehmann:fix/avaplot-transparency

Conversation

@bclehmann
Copy link
Member

@bclehmann bclehmann commented Jan 31, 2025

This fixes #4732, and in a more permanent way than just fixing the workaround that we used to have. That workaround is no longer necessary.

What I believe happened is our Render method was permitted to overwrite the SKCanvas of controls beneath us, Avalonia was not implicitly creating new canvases for different controls and blending them during compositing according to each control's depth. Instead, we need to do that ourselves.

Testing

My starting point for all of these examples is the Avalonia Sandbox project in src/ScottPlot5/ScottPlot5 Sandbox/Sandbox.Avalonia.Desktop

I replaced MainView.axaml.cs with this:

using Avalonia.Controls;
using ScottPlot.Avalonia;
using ScottPlot;

namespace Sandbox.Avalonia;

public partial class MainView : UserControl
{
    public MainView()
    {
        InitializeComponent();

        AvaPlot.UserInputProcessor.IsEnabled = true;
        AvaPlot.Plot.FigureBackground.Color = Colors.Transparent;

        AvaPlot.Plot.Add.Signal(Generate.Sin());
        AvaPlot.Plot.Add.Signal(Generate.Cos());
        AvaPlot.Refresh();
    }
}

And to ensure that the background is visible through the plot I replaced MainWindow.axaml with this, making the MainView component cyan:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
		xmlns:local="clr-namespace:Sandbox.Avalonia"
        xmlns:ScottPlot="clr-namespace:ScottPlot.Avalonia;assembly=ScottPlot.Avalonia"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="Sandbox.Avalonia.MainWindow"
        Title="Avalonia">

	<local:MainView Background="Cyan" />
</Window>

As expected, it gave this result:
image

To ensure I didn't break anything if users wanted a truly see-through window, I also tested this MainWindow.axaml:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
		xmlns:local="clr-namespace:Sandbox.Avalonia"
        xmlns:ScottPlot="clr-namespace:ScottPlot.Avalonia;assembly=ScottPlot.Avalonia"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="Sandbox.Avalonia.MainWindow"
		Background="Transparent" <!-- Note that the window is also set to transparent, not just its child -->
        Title="Avalonia">

	<local:MainView Background="Transparent" />
</Window>

Which gave the expected result:
image

If the plot has transparency then this ensures it's blitted properly,
rather than making us and the controls beneath us transparent.
@bclehmann bclehmann marked this pull request as ready for review January 31, 2025 04:33
@swharden swharden enabled auto-merge (squash) February 15, 2025 13:52
@swharden swharden merged commit 761fd21 into ScottPlot:main Feb 15, 2025
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avalonia: plots with transparent backgrounds make the window see-thru (again)

2 participants