Skip to content

Commit

Permalink
Merge pull request #15 from IeuanWalker/Fix/ButtonMoved
Browse files Browse the repository at this point in the history
  • Loading branch information
IeuanWalker authored Mar 16, 2022
2 parents 890ff5e + 4049814 commit e56a3c0
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Demo/App.iOS/App.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<WarningLevel>4</WarningLevel>
<MtouchLink>None</MtouchLink>
<MtouchArch>x86_64</MtouchArch>
<CodesignKey>Apple Development: Ieuan Walker (R4SVVV33HW)</CodesignKey>
<CodesignProvision>VS: WildCard Development</CodesignProvision>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhone' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
6 changes: 6 additions & 0 deletions Demo/App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
<ItemGroup>
<ProjectReference Include="..\..\Scr\StateButton\StateButton.csproj" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="ButtonNotInScrollViewPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Demo/App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public App()
{
InitializeComponent();

MainPage = new MainPage();
MainPage = new NavigationPage(new MainPage());
}
}
}
36 changes: 36 additions & 0 deletions Demo/App/ButtonNotInScrollViewPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="App.ButtonNotInScrollViewPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:stateButton="clr-namespace:StateButton;assembly=StateButton">
<ContentPage.Content>
<StackLayout Padding="20">
<stateButton:StateButton AutomationProperties.IsInAccessibleTree="True"
AutomationProperties.Name="Test"
Clicked="StateButton_Clicked"
HorizontalOptions="Center"
Released="StateButton_Released">
<stateButton:StateButton.Content>
<Frame Padding="20,10" BackgroundColor="Red">
<Frame.Triggers>
<DataTrigger Binding="{Binding Source={RelativeSource AncestorType={x:Type stateButton:StateButton}}, Path=State}"
TargetType="Frame"
Value="Pressed">
<Setter Property="BackgroundColor" Value="LightGray" />
</DataTrigger>
</Frame.Triggers>
<Label Text="Test" TextColor="White">
<Label.Triggers>
<DataTrigger Binding="{Binding Source={RelativeSource AncestorType={x:Type stateButton:StateButton}}, Path=State}"
TargetType="Label"
Value="Pressed">
<Setter Property="TextColor" Value="Blue" />
</DataTrigger>
</Label.Triggers>
</Label>
</Frame>
</stateButton:StateButton.Content>
</stateButton:StateButton>
</StackLayout>
</ContentPage.Content>
</ContentPage>
30 changes: 30 additions & 0 deletions Demo/App/ButtonNotInScrollViewPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ButtonNotInScrollViewPage : ContentPage
{
public ButtonNotInScrollViewPage()
{
InitializeComponent();
}

private async void StateButton_Clicked(object sender, EventArgs e)
{
await DisplayAlert("Clicked", string.Empty, "OK");
}

private async void StateButton_Released(object sender, EventArgs e)
{
await DisplayAlert("Release", string.Empty, "OK");
}
}
}
15 changes: 9 additions & 6 deletions Demo/App/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:app="clr-namespace:App;assembly=App"
xmlns:stateButton="clr-namespace:StateButton;assembly=StateButton"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
NavigationPage.HasNavigationBar="False">
<ContentPage.BindingContext>
<app:TestViewModel />
</ContentPage.BindingContext>
<ScrollView>
<StackLayout Padding="20,10">
<!-- Events -->
<Button Clicked="BtnNotInScrollView_Clicked" Text="Test button not in scrollview" />

<!-- Events -->
<Label FontSize="Large" Text="Events" />
<Label FontSize="Medium" Text="Clicked" />
<stateButton:StateButton AutomationProperties.IsInAccessibleTree="True"
Expand Down Expand Up @@ -89,7 +92,7 @@
BackgroundColor="LightGray"
HeightRequest="1"
HorizontalOptions="Fill" />
<!-- Commands -->
<!-- Commands -->
<Label FontSize="Large" Text="Commands" />
<Label FontSize="Medium" Text="Clicked" />
<stateButton:StateButton ClickedCommand="{Binding ClickedCommand}" HorizontalOptions="Center">
Expand Down Expand Up @@ -165,7 +168,7 @@
BackgroundColor="LightGray"
HeightRequest="1"
HorizontalOptions="Fill" />
<!-- Visual state manager -->
<!-- Visual state manager -->
<Label FontSize="Large" Text="Visual state manager" />
<stateButton:StateButton HorizontalOptions="Center">
<stateButton:StateButton.Content>
Expand Down Expand Up @@ -193,7 +196,7 @@
BackgroundColor="LightGray"
HeightRequest="1"
HorizontalOptions="Fill" />
<!-- Complex -->
<!-- Complex -->
<Label FontSize="Large" Text="Complex" />
<stateButton:StateButton Clicked="Card_OnClicked" HorizontalOptions="Fill">
<stateButton:StateButton.Behaviors>
Expand Down Expand Up @@ -285,7 +288,7 @@
BackgroundColor="LightGray"
HeightRequest="1"
HorizontalOptions="Fill" />
<!-- Experiment -->
<!-- Experiment -->
<Label FontSize="Large" Text="Experiment" />
<stateButton:StateButton HorizontalOptions="Fill">
<stateButton:StateButton.Content>
Expand Down
5 changes: 5 additions & 0 deletions Demo/App/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@ private async void NestButton_OnClicked(object sender, EventArgs e)
{
await DisplayAlert("Nested state button clicked", string.Empty, "OK");
}

private async void BtnNotInScrollView_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new ButtonNotInScrollViewPage());
}
}
}
27 changes: 25 additions & 2 deletions Scr/StateButton.Android/StateButtonRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public StateButtonRenderer(Context context) : base(context)
SetAccessibilityDelegate(new MyAccessibilityDelegate());
}

private Rect rect;
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
Expand All @@ -33,9 +34,11 @@ protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)

Touch += (sender, te) =>
{
var v = (AndroidView.View)sender;
switch (te.Event.Action)
{
case MotionEventActions.Down:
rect = new Rect(v.Left, v.Top, v.Right, v.Bottom);
foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
{
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
Expand All @@ -46,12 +49,20 @@ protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
break;

case MotionEventActions.Up:

foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
{
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
{
touchGestureRecognizer.Released();
touchGestureRecognizer.Clicked();
if (rect.Contains(v.Left + (int)te.Event.GetX(), v.Top + (int)te.Event.GetY()))
{
touchGestureRecognizer.Released();
touchGestureRecognizer.Clicked();
}
else
{
touchGestureRecognizer.Released();
}
}
}
break;
Expand All @@ -65,6 +76,18 @@ protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
}
}
break;
case MotionEventActions.Move:
foreach (IGestureRecognizer recognizer in Element.GestureRecognizers.Where(x => x is TouchGestureRecognizer))
{
if (recognizer is TouchGestureRecognizer touchGestureRecognizer)
{
if (!rect.Contains(v.Left + (int)te.Event.GetX(), v.Top + (int)te.Event.GetY()))
{
touchGestureRecognizer.Released();
}
}
}
break;
}
};
}
Expand Down
2 changes: 2 additions & 0 deletions Scr/StateButton/StateButton.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ private void ReleasedGesture()
{
if (!IsEnabled) return;

if (State.Equals(ButtonStateEnum.NotPressed)) return;

Released?.Invoke(this, EventArgs.Empty);
ReleasedCommand?.Execute(ReleasedCommandParameter);

Expand Down
41 changes: 41 additions & 0 deletions Settings.XamlStyler
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"AttributesTolerance": 2,
"KeepFirstAttributeOnSameLine": true,
"MaxAttributeCharactersPerLine": 0,
"MaxAttributesPerLine": 1,
"NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransfom, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
"SeparateByGroups": false,
"AttributeIndentation": 0,
"AttributeIndentationStyle": 1,
"RemoveDesignTimeReferences": false,
"EnableAttributeReordering": true,
"AttributeOrderingRuleGroups": [
"x:Class",
"xmlns, xmlns:x",
"xmlns:*",
"x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
"Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
"Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
"Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
"*:*, *",
"PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
"mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
"Storyboard.*, From, To, Duration"
],
"FirstLineAttributes": "",
"OrderAttributesByName": true,
"PutEndingBracketOnNewLine": false,
"RemoveEndingTagOfEmptyElement": true,
"SpaceBeforeClosingSlash": true,
"RootElementLineBreakRule": 0,
"ReorderVSM": 2,
"ReorderGridChildren": false,
"ReorderCanvasChildren": false,
"ReorderSetters": 0,
"FormatMarkupExtension": true,
"NoNewLineMarkupExtensions": "x:Bind, Binding",
"ThicknessSeparator": 2,
"ThicknessAttributes": "Margin, Padding, BorderThickness, ThumbnailClipMargin",
"FormatOnSave": true,
"CommentPadding": 1
}

0 comments on commit e56a3c0

Please sign in to comment.