-
Notifications
You must be signed in to change notification settings - Fork 642
Making the Forms views more extendable #269
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
763427c
A few adjustments to head towards resolving issue #260
mattleibow b5d20f8
The other solutions
mattleibow 05f2fb2
Use the older build tools for mono
mattleibow cb447a1
Android uses a renderer, not events
mattleibow bf85c63
Merge branch 'master' into forms-changes
mattleibow ccfb347
Merge branch 'master' into forms-changes
mattleibow 562cf20
Merge branch 'master' into forms-changes
mattleibow 1c30427
newline character at the end of the file
mattleibow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
source/SkiaSharp.Views.Forms/SkiaSharp.Views.Forms.Native.Shared/SKCanvasViewRendererBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| using System; | ||
| using System.ComponentModel; | ||
|
|
||
| using SKFormsView = SkiaSharp.Views.Forms.SKCanvasView; | ||
|
|
||
| #if __ANDROID__ | ||
| using Xamarin.Forms.Platform.Android; | ||
| using SKNativeView = SkiaSharp.Views.Android.SKCanvasView; | ||
| using SKNativePaintSurfaceEventArgs = SkiaSharp.Views.Android.SKPaintSurfaceEventArgs; | ||
| #elif __IOS__ | ||
| using Xamarin.Forms.Platform.iOS; | ||
| using SKNativeView = SkiaSharp.Views.iOS.SKCanvasView; | ||
| using SKNativePaintSurfaceEventArgs = SkiaSharp.Views.iOS.SKPaintSurfaceEventArgs; | ||
| #elif WINDOWS_UWP | ||
| using Xamarin.Forms.Platform.UWP; | ||
| using SKNativeView = SkiaSharp.Views.UWP.SKXamlCanvas; | ||
| using SKNativePaintSurfaceEventArgs = SkiaSharp.Views.UWP.SKPaintSurfaceEventArgs; | ||
| #endif | ||
|
|
||
| namespace SkiaSharp.Views.Forms | ||
| { | ||
| public abstract class SKCanvasViewRendererBase<TFormsView, TNativeView> : ViewRenderer<TFormsView, TNativeView> | ||
| where TFormsView : SKFormsView | ||
| where TNativeView : SKNativeView | ||
| { | ||
| protected override void OnElementChanged(ElementChangedEventArgs<TFormsView> e) | ||
| { | ||
| if (e.OldElement != null) | ||
| { | ||
| var oldController = (ISKCanvasViewController)e.OldElement; | ||
|
|
||
| // unsubscribe from events | ||
| oldController.SurfaceInvalidated -= OnSurfaceInvalidated; | ||
| oldController.GetCanvasSize -= OnGetCanvasSize; | ||
| } | ||
|
|
||
| if (Control != null) | ||
| { | ||
| var control = Control; | ||
| control.PaintSurface -= OnPaintSurface; | ||
| } | ||
|
|
||
| if (e.NewElement != null) | ||
| { | ||
| var newController = (ISKCanvasViewController)e.NewElement; | ||
|
|
||
| // create the native view | ||
| var view = CreateNativeControl(); | ||
| view.IgnorePixelScaling = e.NewElement.IgnorePixelScaling; | ||
| view.PaintSurface += OnPaintSurface; | ||
| SetNativeControl(view); | ||
|
|
||
| // subscribe to events from the user | ||
| newController.SurfaceInvalidated += OnSurfaceInvalidated; | ||
| newController.GetCanvasSize += OnGetCanvasSize; | ||
|
|
||
| // paint for the first time | ||
| OnSurfaceInvalidated(newController, EventArgs.Empty); | ||
| } | ||
|
|
||
| base.OnElementChanged(e); | ||
| } | ||
|
|
||
| #if __ANDROID__ | ||
| protected override TNativeView CreateNativeControl() | ||
| { | ||
| return (TNativeView)Activator.CreateInstance(typeof(TNativeView), new[] { Context }); | ||
| } | ||
| #else | ||
| protected virtual TNativeView CreateNativeControl() | ||
| { | ||
| return (TNativeView)Activator.CreateInstance(typeof(TNativeView)); | ||
| } | ||
| #endif | ||
|
|
||
| protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) | ||
| { | ||
| base.OnElementPropertyChanged(sender, e); | ||
|
|
||
| if (e.PropertyName == nameof(SKFormsView.IgnorePixelScaling)) | ||
| { | ||
| Control.IgnorePixelScaling = Element.IgnorePixelScaling; | ||
| } | ||
| } | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| // detach all events before disposing | ||
| var controller = (ISKCanvasViewController)Element; | ||
| if (controller != null) | ||
| { | ||
| controller.SurfaceInvalidated -= OnSurfaceInvalidated; | ||
| controller.GetCanvasSize -= OnGetCanvasSize; | ||
| } | ||
|
|
||
| var control = Control; | ||
| if (control != null) | ||
| { | ||
| control.PaintSurface -= OnPaintSurface; | ||
| } | ||
|
|
||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| private void OnPaintSurface(object sender, SKNativePaintSurfaceEventArgs e) | ||
| { | ||
| var controller = Element as ISKCanvasViewController; | ||
|
|
||
| // the control is being repainted, let the user know | ||
| controller?.OnPaintSurface(new SKPaintSurfaceEventArgs(e.Surface, e.Info)); | ||
| } | ||
|
|
||
| private void OnSurfaceInvalidated(object sender, EventArgs eventArgs) | ||
| { | ||
| // repaint the native control | ||
| #if __IOS__ | ||
| Control.SetNeedsDisplay(); | ||
| #else | ||
| Control.Invalidate(); | ||
| #endif | ||
| } | ||
|
|
||
| // the user asked for the size | ||
| private void OnGetCanvasSize(object sender, GetCanvasSizeEventArgs e) | ||
| { | ||
| e.CanvasSize = Control?.CanvasSize ?? SKSize.Empty; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our current beta, we had several crashes, because "OnSurfaceInvalidated" was called by a background thread.
Maybe it is better to post these on the main thread?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is a bit tough... I think the two sides of the fence is to either let the user make sure that invalidate is done from the main/UI thread via
Xamarin.Forms.Device.BeginInvokeOnMainThread(), or to let the framework do it automatically.For most cases, the app will always be using the main thread, so this should not be an issue. Thus, the overhead for posting to the main thread on each call might be undesirable. However, if some event out of the user's control is causing this to be called, then we will have to wrap the invalidate.
In the case of your beta, are you able to determine if a call was being made from a background thread that you could control?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah of course. And I agree with you. We will apply the fix in our app logic ;-)