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

Simplify Drawing API #1705

Merged
merged 227 commits into from
Dec 21, 2024
Merged

Simplify Drawing API #1705

merged 227 commits into from
Dec 21, 2024

Conversation

beto-rodriguez
Copy link
Owner

@beto-rodriguez beto-rodriguez commented Nov 18, 2024

This PR simplifies the API in the core of the library that handles drawing.

A lot of objects in the core of the library use the generic argument TDrawingContext this argument in most of the cases is not necessary, that was added in early stages of the library, now it's time to refactor the code to make it cleaner.

There should not be breaking changes for the final user, but if you are using the LiveCharts API to draw directly on the canvas, then probably something will brake :(

Breaking changes:

  • IChartLegend<T> no longer requires the generic param here is an example to migrate to the new api.
  • IChartTooltip<T> no longer requires the generic param, here is an example to migrate to the new api.
  • The type of the Chart.VisualElements property, changed from ChartElement<T> to ChartElement, also VisualElement<T> cchanged to VisualElement example here and here.
  • There are probably more braking changes if you are drawing directly on the canvas, please refer to the articles in the web site to see how to use the new API, in general most of the changes just removed the TDrawingContext generic param, or the object was renamed (objects were marked as obsolete and there should be a hint in your code with the new name).

Inspiration on breaking changes

  1. Remove the Generic argument.

  2. The way visuals worked in the library was wrong, transforms (like rotation, opacity, scale) were not applied to children, now Visuals access directly the drawn geometry in the canvas, there are new Layout objects, like the TableLayout or StackLayout that the library uses to build animated tooltips and legends, now the default tooltip uses this feature and fades and scales when the tooltip is shown/hidden, this was impossible with the old approach.

new-tooltip

The docs are updated to explain how to use the new visuals, they are much easier and flexible than the old approach.

  1. The library used interfaces to define all shapes in the core of library, for example the IRoundedRectangleGeometry, then on the SkiaSharp assembly, there was an implementation of this interface; Now instead there is a base abstract class in the core of the library (BaseRoundedRectangleGeometry that inherits from DrawnGeometry), this base class handles animations and transforms, then on the SkiaSharp assembly, we inherit from this base class and just use SkiaSharp to draw the rectangle (see RoundedRectangleGeometry), this is much cleaner and has the potential to easily take LiveCharts to any other drawing engine.

The IDrawable interface implementation in the Drawable class has been updated to remove the generic type parameter TDrawingContext. The Draw method now takes a DrawingContext parameter instead of a SkiaSharpDrawingContext parameter. Additionally, an explicit interface implementation of the Draw method has been added to cast the DrawingContext to SkiaSharpDrawingContext.
Refactoring more... to remove the generic argument on this objects
Refactored the MotionCanvas class to replace anonymous object creation with explicit SkiaSharpDrawingContext instances across multiple methods, including OnPaintSurface, OnPaintGlSurface, SkControl_PaintSurface, SkglControl_PaintSurface, and OnCanvasViewPaintSurface. This change improves code readability, maintainability, and potentially performance. Additionally, set properties like Background on SkiaSharpDrawingContext to ensure correct background color application. Applied changes across different base classes for MotionCanvas, such as UserControl, SkiaDrawable, and ContentView.
Now instead of using the IPaint interface, SkiaPaints inherit from
the absract Paint class.
Refactored codebase to remove TDrawingContext generics, simplifying interfaces and classes. Updated method signatures and references to use non-generic counterparts, enhancing maintainability and readability. Key changes include:

- Replaced `IChartSeries<TDrawingContext>` with `IChartSeries`.
- Updated methods to use non-generic `Sketch`, `VisualElement`, and `PaintSchedule`.
- Simplified methods using expression-bodied members.
- Added `IsDrawn` method to `IChart` interface.
- Updated `IChartView` and series interfaces to non-generic versions.
- Refactored `LiveChartsCore` library to remove unnecessary generics, improving usability.
- Adjusted `using` directives and type casting for compatibility.
Renamed the `Paintable` interface to `IPaintable` and updated its usage across multiple files:
- In `IGeometry.cs`, `IGeometry` now extends `IPaintable` instead of `Paintable<TDrawingContext>`.
- In `IPaintable.cs`, the `Paintable` interface is renamed to `IPaintable`, and the generic type parameter `TDrawingContext` is removed.
- In `IVectorGeometry.cs`, `IVectorGeometry` now extends `IPaintable` instead of `Paintable<TDrawingContext>`.
- In `Geometry.cs` and `VectorGeometry.cs`, updated references in `Stroke` and `Fill` properties to `IPaintable`.
Updated generic constraints in various classes and interfaces:
- Replaced `where TVisual : class, IGeometry<TDrawingContext>, new()` with `where TVisual : class, IGeometry, new()`.
- Removed `TDrawingContext` from `IGeometry` interface.
- Updated methods and properties to use non-generic `IGeometry`.
- Modified `SetParent` method and `Geometry` class to implement non-generic `IGeometry`.
- Added `Opacity` property to `IDrawable` interface.
- Updated `GeneralSeriesTests` to reflect these changes.

These changes simplify the type hierarchy by removing the `TDrawingContext` generic parameter.
…etry

Refactored various classes and interfaces within the LiveChartsCore namespace to remove the type parameter TDrawingContext from the ISizedGeometry interface and its implementations. Replaced ISizedGeometry<TDrawingContext> with ISizedGeometry across multiple files, including series, chart, and geometry classes. Updated methods and properties to use the non-generic ISizedGeometry interface, simplifying type constraints and decoupling from SkiaSharpDrawingContext. These changes enhance code consistency, maintainability, and flexibility.
Simplify the IBoxGeometry interface by removing the generic type
parameter TDrawingContext. This change affects several files:
- CoreBoxSeries.cs: Update constraint on TVisual.
- IBoxGeometry.cs: Remove TDrawingContext from interface.
- BoxSeries.cs: Update constraints on TVisual in two places.
- BoxGeometry.cs: Update interface implementation and XML docs.
Refactored CoreHeatSeries, IColoredGeometry, ColoredRectangleGeometry,
and HeatSeries classes to remove the generic type parameter TDrawingContext
from the IColoredGeometry interface constraint and implementation.
Simplified IDoughnutGeometry by removing the TDrawingContext type parameter and directly extending IGeometry. Updated CorePieSeries, DoughnutGeometry, PieChartExtensions, PieSeries, and PieSeries<TModel, TVisual, TLabel> to reflect these changes, removing the TDrawingContext constraint from TVisual.
The IFinancialGeometry interface was simplified by removing the
generic type parameter TDrawingContext. This change required
modifications to the following classes to update their type
constraints and documentation comments accordingly:
- CoreFinancialSeries.cs
- IFinancialGeometry.cs
- CandlesticksSeries.cs
- CandlestickGeometry.cs
Refactor codebase to remove the TDrawingContext type parameter from the ILabelGeometry interface and its usage across multiple files. Replace ILabelGeometry<TDrawingContext> with a non-generic ILabelGeometry interface, affecting various classes and methods including BarSeries, CartesianSeries, ChartSeries, and more. Update the LabelGeometry class to implement the new non-generic ILabelGeometry interface. Modify generic type constraints for TLabel in several classes and methods to remove dependency on SkiaSharpDrawingContext, enhancing code reusability and consistency.
The changes involve modifying the `ILineGeometry` interface and its usage across various classes in the `LiveChartsCore` library. The generic type parameter `<TDrawingContext>` has been removed from the `ILineGeometry` interface, simplifying it by removing the dependency on the drawing context type.

Consequently, all classes and methods that previously used `ILineGeometry<TDrawingContext>` have been updated to use the non-generic `ILineGeometry` interface. This includes updates to the following classes:
- `CoreAxis`
- `CoreColumnSeries`
- `CoreLineSeries`
- `CorePolarAxis`
- `CorePolarLineSeries`
- `CoreRowSeries`
- `CoreScatterSeries`
- `CoreStackedAreaSeries`
- `CoreStackedColumnSeries`
- `CoreStackedRowSeries`
- `CoreStackedStepAreaSeries`
- `CoreStepLineSeries`
- `AxisVisualSeprator`
- `AngularTicksVisual`
- `LineVisual`
- `LineGeometry`

The `LineGeometry` class in `LiveChartsCore.SkiaSharpView.Drawing.Geometries` has been updated to implement the non-generic `ILineGeometry` interface.

These changes ensure that the `ILineGeometry` interface and its implementations are no longer tied to a specific drawing context, making the codebase more flexible and easier to maintain.
The changes remove the generic type parameter `TDrawingContext` from the `INeedleGeometry` interface and its implementations. This simplifies the interface by no longer requiring a specific drawing context type.

- In `INeedleGeometry.cs`, the `INeedleGeometry<TDrawingContext>` interface is replaced with a non-generic `INeedleGeometry` interface. The constraint on `TDrawingContext` is removed.
- In `NeedleVisual.cs`, the `NeedleVisual` class is updated to use the non-generic `INeedleGeometry` interface instead of `INeedleGeometry<TDrawingContext>`.
- In `NeedleGeometry.cs`, the `NeedleGeometry` class is updated to implement the non-generic `INeedleGeometry` interface. The documentation comments are also updated to reflect this change.
Removed IPaintable interface and updated IGeometry and
IVectorGeometry<TSegment, TDrawingContext> to inherit from
IDrawable. Added Stroke and Fill properties directly to these
interfaces. Updated Geometry and VectorGeometry<TSegment>
classes to use the new properties. Removed license headers
and using directives related to IPaintable.
Refactored the IRoundedGeometry interface by removing the generic
type parameter <TDrawingContext>. Updated CoreColumnSeries and
CoreRowSeries classes to reflect this change. Modified
RoundedRectangleGeometry class to implement the non-generic
IRoundedGeometry interface and updated its BorderRadius property
accordingly. These changes simplify the interface and its
implementation.
Simplified ISolidColorGeometry by removing the generic type parameter and directly extending ISizedGeometry. Added a Color property to ISolidColorGeometry. Updated ColoredRectangleGeometry to align with the new interface definition, including updating the inheritdoc reference for the Color property.
The changes involve removing the generic type parameter <TDrawingContext> from the IVariableSvgPath interface and its usages across multiple files. Specifically:
- Updated interface definition in IVariableSvgPath.cs.
- Modified type casting in CoreColumnSeries.cs, CoreHeatSeries.cs, CoreLineSeries.cs, CorePolarLineSeries.cs, CoreRowSeries.cs, CoreScatterSeries.cs, CoreStepLineSeries.cs, and GeometryVisual.cs.
- Updated documentation comments in ISeries.cs and VariableSVGPathGeometry.cs.
- Adjusted type checks in Series.cs and VisualElementsExtensions.cs.
Refactored the IVectorGeometry interface to use a single type parameter, TSgment, instead of two (TSegment and TDrawingContext). Updated all implementing and using classes, including CoreLineSeries, CorePolarLineSeries, CoreStackedAreaSeries, CoreStackedStepAreaSeries, CoreStepLineSeries, BezierVisualPoint, StepLineVisualPoint, VectorManager, CubicBezierAreaGeometry, and VectorGeometry, to remove the TDrawingContext type parameter. Refactored the OnDrawSegment method in CubicBezierAreaGeometry to use an expression-bodied member.
Refactored the `FromRGB` and `FromArgb` methods in the `LvcColor` struct to use expression-bodied member syntax. This change simplifies the method definitions by replacing the block body with a more concise syntax. The affected methods include:
- `FromRGB`
- `FromArgb` (with four parameters: alpha, red, green, blue)
- `FromArgb` (with two parameters: alpha, LvcColor)
The changes involve the removal of the generic type parameter `TDrawingContext` from several classes and their usages. Specifically, the classes `BezierErrorVisualPoint`, `BezierVisualPoint`, and `StepLineVisualPoint` have been modified to no longer require `TDrawingContext` as a type parameter.

In the `CoreLineSeries`, `CorePolarLineSeries`, and `CoreStepLineSeries` classes, the type parameter `TDrawingContext` has been removed from various method calls and type checks involving these visual point classes.

These changes simplify the type definitions and usages by reducing the number of generic type parameters, making the code easier to read and maintain.
Simplified VectorManager by removing the TDrawingContext type parameter, reducing complexity and potential errors. Updated instantiation in CoreLineSeries and CoreStepLineSeries to match the new class definition. Refactored the Clear method in VectorManager to use an expression-bodied member for improved readability and conciseness.
@beto-rodriguez beto-rodriguez marked this pull request as ready for review December 15, 2024 19:07
@beto-rodriguez beto-rodriguez merged commit 5418f12 into dev Dec 21, 2024
4 checks passed
@beto-rodriguez beto-rodriguez deleted the drawing branch December 21, 2024 18:39
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.

1 participant