Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
Correcting the Windows views so they can be disabled. Fixes #94
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Nov 16, 2017
1 parent effb5e1 commit 928e01b
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 45 deletions.
20 changes: 19 additions & 1 deletion src/SignaturePad.Forms.Shared/SignaturePadView.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Xamarin.Forms;

Expand Down Expand Up @@ -92,6 +93,8 @@ public partial class SignaturePadView : Grid
(double)0,
propertyChanged: (bindable, oldValue, newValue) => ((SignaturePadView)bindable).BackgroundImageView.Opacity = (double)newValue);

private TapGestureRecognizer clearLabelTap;

public SignaturePadView ()
{
Initialize ();
Expand Down Expand Up @@ -186,13 +189,28 @@ private void Initialize ()
Children.Add (ClearLabel);

// attach the "clear" command
ClearLabel.GestureRecognizers.Add (new TapGestureRecognizer { Command = new Command (() => Clear ()) });
clearLabelTap = new TapGestureRecognizer { Command = new Command (() => Clear ()) };
ClearLabel.GestureRecognizers.Add (clearLabelTap);
}

// clear / initialize the view
Clear ();
}

protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);

if (propertyName == IsEnabledProperty.PropertyName)
{
SignaturePadCanvas.IsEnabled = IsEnabled;
if (IsEnabled)
ClearLabel.GestureRecognizers.Add (clearLabelTap);
else
ClearLabel.GestureRecognizers.Remove (clearLabelTap);
}
}

public IEnumerable<IEnumerable<Point>> Strokes
{
get { return SignaturePadCanvas.Strokes; }
Expand Down
24 changes: 15 additions & 9 deletions src/SignaturePad.UWP/SignaturePad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Xamarin.Controls
{
public class SignaturePad : Grid
public class SignaturePad : ContentControl
{
public SignaturePad ()
{
Expand All @@ -24,16 +24,18 @@ private void Initialize ()
const int ThickPad = 20;
const int LineHeight = 2;

RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) });
RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });
var grid = new Grid ();

grid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) });
grid.RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });

// add the background view
{
BackgroundImageView = new Image ();
BackgroundImageView.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
BackgroundImageView.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Stretch);
BackgroundImageView.SetValue (Grid.RowProperty, 0);
Children.Add (BackgroundImageView);
grid.Children.Add (BackgroundImageView);
}

// add the main signature view
Expand All @@ -48,7 +50,7 @@ private void Initialize ()
StrokeCompleted?.Invoke (this, EventArgs.Empty);
};
SignaturePadCanvas.Cleared += (sender, e) => Cleared?.Invoke (this, EventArgs.Empty);
Children.Add (SignaturePadCanvas);
grid.Children.Add (SignaturePadCanvas);
}

// add the caption
Expand All @@ -64,7 +66,7 @@ private void Initialize ()
Caption.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
Caption.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Bottom);
Caption.SetValue (Grid.RowProperty, 1);
Children.Add (Caption);
grid.Children.Add (Caption);
}

// add the signature line
Expand All @@ -77,7 +79,7 @@ private void Initialize ()
};
SignatureLine.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
SignatureLine.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Bottom);
Children.Add (SignatureLine);
grid.Children.Add (SignatureLine);
}

// add the prompt
Expand All @@ -91,7 +93,7 @@ private void Initialize ()
};
SignaturePrompt.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Left);
SignaturePrompt.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Bottom);
Children.Add (SignaturePrompt);
grid.Children.Add (SignaturePrompt);
}

// add the clear label
Expand All @@ -107,12 +109,16 @@ private void Initialize ()
};
ClearLabel.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Right);
ClearLabel.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Top);
Children.Add (ClearLabel);
grid.Children.Add (ClearLabel);

// attach the "clear" command
ClearLabel.Tapped += (sender, e) => Clear ();
}

HorizontalContentAlignment = HorizontalAlignment.Stretch;
VerticalContentAlignment = VerticalAlignment.Stretch;
Content = grid;

// clear / initialize the view
Clear ();
}
Expand Down
15 changes: 13 additions & 2 deletions src/SignaturePad.UWP/SignaturePadCanvasView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Xamarin.Controls
{
public partial class SignaturePadCanvasView : Grid
public partial class SignaturePadCanvasView : ContentControl
{
private Color strokeColor;
private float lineWidth;
Expand All @@ -31,10 +31,12 @@ public SignaturePadCanvasView ()

private void Initialize ()
{
var grid = new Grid ();

inkCanvas = new InkCanvas ();
inkCanvas.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
inkCanvas.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Stretch);
Children.Add (inkCanvas);
grid.Children.Add (inkCanvas);

inkPresenter = inkCanvas.InkPresenter;
inkPresenter.StrokesCollected += (sender, e) => OnStrokeCompleted ();
Expand All @@ -46,6 +48,15 @@ private void Initialize ()

StrokeWidth = settings.StrokeWidth.Value;
StrokeColor = settings.StrokeColor.Value;

HorizontalContentAlignment = HorizontalAlignment.Stretch;
VerticalContentAlignment = VerticalAlignment.Stretch;
Content = grid;

IsEnabledChanged += delegate
{
inkPresenter.IsInputEnabled = IsEnabled;
};
}

public Color StrokeColor
Expand Down
24 changes: 15 additions & 9 deletions src/SignaturePad.WP8/SignaturePad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Xamarin.Controls
{
public class SignaturePad : Grid
public class SignaturePad : ContentControl
{
public SignaturePad ()
{
Expand All @@ -21,16 +21,18 @@ private void Initialize ()
const int ThickPad = 20;
const int LineHeight = 2;

RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) });
RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });
var grid = new Grid ();

grid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) });
grid.RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });

// add the background view
{
BackgroundImageView = new Image ();
BackgroundImageView.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
BackgroundImageView.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Stretch);
BackgroundImageView.SetValue (Grid.RowProperty, 0);
Children.Add (BackgroundImageView);
grid.Children.Add (BackgroundImageView);
}

// add the main signature view
Expand All @@ -45,7 +47,7 @@ private void Initialize ()
StrokeCompleted?.Invoke (this, EventArgs.Empty);
};
SignaturePadCanvas.Cleared += (sender, e) => Cleared?.Invoke (this, EventArgs.Empty);
Children.Add (SignaturePadCanvas);
grid.Children.Add (SignaturePadCanvas);
}

// add the caption
Expand All @@ -61,7 +63,7 @@ private void Initialize ()
Caption.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
Caption.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Bottom);
Caption.SetValue (Grid.RowProperty, 1);
Children.Add (Caption);
grid.Children.Add (Caption);
}

// add the signature line
Expand All @@ -74,7 +76,7 @@ private void Initialize ()
};
SignatureLine.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
SignatureLine.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Bottom);
Children.Add (SignatureLine);
grid.Children.Add (SignatureLine);
}

// add the prompt
Expand All @@ -88,7 +90,7 @@ private void Initialize ()
};
SignaturePrompt.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Left);
SignaturePrompt.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Bottom);
Children.Add (SignaturePrompt);
grid.Children.Add (SignaturePrompt);
}

// add the clear label
Expand All @@ -104,12 +106,16 @@ private void Initialize ()
};
ClearLabel.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Right);
ClearLabel.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Top);
Children.Add (ClearLabel);
grid.Children.Add (ClearLabel);

// attach the "clear" command
ClearLabel.MouseLeftButtonUp += (sender, e) => Clear ();
}

HorizontalContentAlignment = HorizontalAlignment.Stretch;
VerticalContentAlignment = VerticalAlignment.Stretch;
Content = grid;

// clear / initialize the view
Clear ();
}
Expand Down
29 changes: 27 additions & 2 deletions src/SignaturePad.WP8/SignaturePadCanvasView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Xamarin.Controls
{
public partial class SignaturePadCanvasView : Grid
public partial class SignaturePadCanvasView : ContentControl
{
private Color strokeColor;
private float lineWidth;
Expand All @@ -26,11 +26,13 @@ public SignaturePadCanvasView ()

private void Initialize ()
{
var grid = new Grid ();

inkPresenter = new InkPresenter ();
inkPresenter.SetValue (Grid.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);
inkPresenter.SetValue (Grid.VerticalAlignmentProperty, VerticalAlignment.Stretch);
inkPresenter.Background = new SolidColorBrush (Colors.Transparent);
Children.Add (inkPresenter);
grid.Children.Add (inkPresenter);

inkPresenter.MouseLeftButtonDown += OnMouseDown;
inkPresenter.MouseMove += OnMouseMove;
Expand All @@ -43,6 +45,10 @@ private void Initialize ()

StrokeWidth = settings.StrokeWidth.Value;
StrokeColor = settings.StrokeColor.Value;

HorizontalContentAlignment = HorizontalAlignment.Stretch;
VerticalContentAlignment = VerticalAlignment.Stretch;
Content = grid;
}

public Color StrokeColor
Expand Down Expand Up @@ -147,6 +153,11 @@ private Task<Stream> GetImageStreamInternal (SignatureImageFormat format, Size s

private void OnMouseDown (object sender, MouseButtonEventArgs e)
{
if (!IsEnabled)
{
return;
}

inkPresenter.CaptureMouse ();

var points = new StylusPointCollection ();
Expand All @@ -166,6 +177,11 @@ private void OnMouseDown (object sender, MouseButtonEventArgs e)

private void OnMouseMove (object sender, MouseEventArgs e)
{
if (!IsEnabled)
{
return;
}

if (currentStroke != null)
{
currentStroke.StylusPoints.Add (e.StylusDevice.GetStylusPoints (inkPresenter));
Expand All @@ -174,6 +190,11 @@ private void OnMouseMove (object sender, MouseEventArgs e)

private void OnMouseLost (object sender, MouseEventArgs e)
{
if (!IsEnabled)
{
return;
}

var curr = currentStroke;

if (curr != null)
Expand All @@ -192,6 +213,10 @@ private void OnMouseLost (object sender, MouseEventArgs e)

private void OnMouseUp (object sender, MouseButtonEventArgs e)
{
if (!IsEnabled)
{
return;
}
}
}
}
18 changes: 18 additions & 0 deletions src/SignaturePad.Windows81/InkPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public InkPresenter ()
PointerReleased += OnCanvasPointerReleased;

// defaults
IsInputEnabled = true;
Background = new SolidColorBrush (Colors.Transparent);
StrokeColor = ImageConstructionSettings.Black;
StrokeWidth = 2f;
Expand All @@ -62,6 +63,8 @@ public bool ClipToBounds
}
}

public bool IsInputEnabled { get; set; }

private void OnSizeChanged (object sender, SizeChangedEventArgs e)
{
UpdateClip ();
Expand Down Expand Up @@ -144,6 +147,11 @@ private InkDrawingAttributes CreateDefaultDrawingAttributes ()

private void OnCanvasPointerPressed (object sender, PointerRoutedEventArgs e)
{
if (!IsInputEnabled)
{
return;
}

if (currentPointerId != 0)
{
// we only handle a single "pen" at a time
Expand Down Expand Up @@ -171,6 +179,11 @@ private void OnCanvasPointerPressed (object sender, PointerRoutedEventArgs e)

private void OnCanvasPointerMoved (object sender, PointerRoutedEventArgs e)
{
if (!IsInputEnabled)
{
return;
}

var pointerPoint = e.GetCurrentPoint (this);

if (pointerPoint.PointerId == currentPointerId)
Expand All @@ -185,6 +198,11 @@ private void OnCanvasPointerMoved (object sender, PointerRoutedEventArgs e)

private void OnCanvasPointerReleased (object sender, PointerRoutedEventArgs e)
{
if (!IsInputEnabled)
{
return;
}

var pointerPoint = e.GetCurrentPoint (this);

if (pointerPoint.PointerId == currentPointerId)
Expand Down
Loading

0 comments on commit 928e01b

Please sign in to comment.