microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.27k stars 674 forks source link

Proposal: Add Microsoft.Expression.Drawing shapes #2621

Open sonnemaf opened 4 years ago

sonnemaf commented 4 years ago

Can the source code of Microsoft.Expression.Drawing be added to this (or another OSS) repository. It contains Shapes classes like: Arc, Arrows, Callouts, Hexagon, Pentagon, Ring, Star and Triangle.

I would love to have them in WinUI too.

image

I have posted a similar issue in the WPF repository. https://github.com/dotnet/wpf/issues/3097

mdtauk commented 4 years ago

You may want to edit the issue to actually fill in the title

sonnemaf commented 4 years ago

I just noticed that creating custom shapes in WPF is way easier. You only have to override the DefiningGeometry property. Why is this different in UWP/WinUI? Should we add this property?

WPF example

public class Hexagon : Shape {

    public Hexagon() => this.Stretch = Stretch.Fill;

    protected override Geometry DefiningGeometry => GetGeometry();

    private Geometry GetGeometry() {
        double sideLength = 100;
        double x = Math.Sqrt(sideLength * sideLength / 2);
        return Geometry.Parse($"M {x},0 h {sideLength} l {x},{x} l -{x},{x} h -{sideLength} l -{x},-{x} Z");
    }
}

public class Triangle : Shape {

    public Triangle() => this.Stretch = Stretch.Fill;

    protected override Geometry DefiningGeometry => GetGeometry();

    private Geometry GetGeometry() {
        return Geometry.Parse("M 0,1 l 1,1 h -2 Z");
    }
}
mdtauk commented 4 years ago

I just noticed that creating custom shapes in WPF is way easier. You only have to override the DefiningGeometry property. Why is this different in UWP/WinUI? Should we add this property?

I can only think that it is because the ambition for WPF back in the day when it was being designed and built - was broader than what WinRT's Xaml was envisaged to be.

Expression Design was a general vector art tool, and so creating shapes, brushes, and geometry was there to power art tools, and create Path assets which could be used in WPF/Silverlight's Xaml.

But by the time WinRT was being built, XAML as a vector asset file format, did not take the world by storm, and the conversion of assets to path was possible, but was it widespread?

So Xaml's drawing was scaled back to make sense purely for UI design and controls.

sonnemaf commented 4 years ago

Creating an Triangle in UWP isn't really hard. You only have to inherit form Path (not from Shape like in WPF) and set the Data property. Still it would be nice if they where the same.

public class Triangle : Path {

    public Triangle() {
        this.Stretch = Stretch.Fill;
        this.Data = GetGeometry();
    }

    private Geometry GetGeometry() {
        var segment = new PolyLineSegment();
        segment.Points.Add(new Point(2, 1));
        segment.Points.Add(new Point(0, 1));

        var figure = new PathFigure() {
            IsClosed = true,
            StartPoint = new Point(1, 0),
        };
        figure.Segments.Add(segment);

        var pathGeo = new PathGeometry();
        pathGeo.Figures.Add(figure);

        return pathGeo;
    }
}