Skip to content

Commit 6e6af5d

Browse files
authored
Align the Sender between the gestures when emit the events (#28305)
* Added repro sample * Added UITest * Applied changes in drag and drop gestures * Updated existing sample
1 parent 1784c46 commit 6e6af5d

File tree

6 files changed

+299
-20
lines changed

6 files changed

+299
-20
lines changed

Diff for: src/Controls/src/Core/DragAndDrop/DragGestureRecognizer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ internal void SendDropCompleted(DropCompletedEventArgs args)
105105
_ = args ?? throw new ArgumentNullException(nameof(args));
106106

107107
DropCompletedCommand?.Execute(DropCompletedCommandParameter);
108-
DropCompleted?.Invoke(this, args);
108+
DropCompleted?.Invoke(Parent ?? this, args);
109109
}
110110

111111
internal DragStartingEventArgs SendDragStarting(View element, Func<IElement?, Point?>? getPosition = null, PlatformDragStartingEventArgs? platformArgs = null)
112112
{
113113
var args = new DragStartingEventArgs(getPosition, platformArgs);
114114

115115
DragStartingCommand?.Execute(DragStartingCommandParameter);
116-
DragStarting?.Invoke(this, args);
116+
DragStarting?.Invoke(element, args);
117117

118118
#pragma warning disable CS0618 // Type or member is obsolete
119119
if (!args.Handled)

Diff for: src/Controls/src/Core/DragAndDrop/DropGestureRecognizer.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ public object DropCommandParameter
9494
public void SendDragOver(DragEventArgs args)
9595
{
9696
DragOverCommand?.Execute(DragOverCommandParameter);
97-
DragOver?.Invoke(this, args);
97+
DragOver?.Invoke(Parent ?? this, args);
9898
}
9999

100100
internal void SendDragLeave(DragEventArgs args)
101101
{
102102
DragLeaveCommand?.Execute(DragLeaveCommandParameter);
103-
DragLeave?.Invoke(this, args);
103+
DragLeave?.Invoke(Parent ?? this, args);
104104
}
105105

106106
internal async Task SendDrop(DropEventArgs args)
@@ -109,7 +109,7 @@ internal async Task SendDrop(DropEventArgs args)
109109
return;
110110

111111
DropCommand?.Execute(DropCommandParameter);
112-
Drop?.Invoke(this, args);
112+
Drop?.Invoke(Parent ?? this, args);
113113

114114
if (!args.Handled)
115115
{

Diff for: src/Controls/tests/TestCases.HostApp/Elements/DragAndDropBetweenLayouts.xaml.cs

+14-15
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ public DragAndDropBetweenLayouts()
1515
void OnDragStarting(object sender, DragStartingEventArgs e)
1616
{
1717
_emittedDragOver = false;
18-
var label = (Label)(sender as Element).Parent;
18+
var label = (Label)sender;
1919
var sl = label.Parent as StackLayout;
2020
e.Data.Properties.Add("Color", label);
2121
e.Data.Properties.Add("Source", sl);
2222

2323
if (sl == SLAllColors)
24-
SLRainbow.Background = SolidColorBrush.LightBlue;
24+
SLRainbow.Background = Brush.LightBlue;
2525
else
26-
SLAllColors.Background = SolidColorBrush.LightBlue;
26+
SLAllColors.Background = Brush.LightBlue;
2727

2828
dragStartEvent.Text = "DragStarting";
2929

@@ -34,12 +34,12 @@ void OnDragStarting(object sender, DragStartingEventArgs e)
3434

3535
void OnDropCompleted(object sender, DropCompletedEventArgs e)
3636
{
37-
var sl = (sender as Element).Parent.Parent as StackLayout;
37+
var sl = ((Element)sender).Parent as StackLayout;
3838

3939
if (sl == SLAllColors)
40-
SLRainbow.Background = SolidColorBrush.White;
40+
SLRainbow.Background = Brush.White;
4141
else
42-
SLAllColors.Background = SolidColorBrush.White;
42+
SLAllColors.Background = Brush.White;
4343

4444
dragCompletedEvent.Text = "DropCompleted";
4545
}
@@ -49,15 +49,14 @@ void OnDragOver(object sender, DragEventArgs e)
4949
if (!e.Data.Properties.ContainsKey("Source"))
5050
return;
5151

52-
//e.AcceptedOperation = DataPackageOperation.None;
53-
var sl = (StackLayout)(sender as Element).Parent;
52+
var sl = ((StackLayout)sender);
5453
if (e.Data.Properties["Source"] == sl)
5554
{
5655
e.AcceptedOperation = DataPackageOperation.None;
5756
return;
5857
}
5958

60-
sl.Background = SolidColorBrush.LightPink;
59+
sl.Background = Brush.LightPink;
6160

6261
if (!_emittedDragOver) // This can generate a lot of noise, only add it once
6362
{
@@ -75,14 +74,14 @@ void OnDragLeave(object sender, DragEventArgs e)
7574
if (!e.Data.Properties.ContainsKey("Source"))
7675
return;
7776

78-
var sl = (StackLayout)(sender as Element).Parent;
77+
var sl = (StackLayout)sender;
7978
if (e.Data.Properties["Source"] == sl)
8079
{
8180
e.AcceptedOperation = DataPackageOperation.None;
8281
return;
8382
}
8483

85-
sl.Background = SolidColorBrush.LightBlue;
84+
sl.Background = Brush.LightBlue;
8685

8786
dragLeaveEvent.Text = "DragLeave";
8887
}
@@ -92,7 +91,7 @@ void OnDrop(object sender, DropEventArgs e)
9291
if (!e.Data.Properties.ContainsKey("Source"))
9392
return;
9493

95-
var sl = (sender as Element).Parent as StackLayout;
94+
var sl = (StackLayout)sender;
9695
if (e.Data.Properties["Source"] == sl)
9796
{
9897
return;
@@ -116,13 +115,13 @@ void OnDrop(object sender, DropEventArgs e)
116115
dropRelativeScreen.Text = $"Drop relative to screen: {(int)e.GetPosition(null).Value.X},{(int)e.GetPosition(null).Value.Y}";
117116
dropRelativeLabel.Text = $"Drop relative to this label: {(int)e.GetPosition(dropRelativeLabel).Value.X},{(int)e.GetPosition(dropRelativeLabel).Value.Y}";
118117

119-
SLAllColors.Background = SolidColorBrush.White;
120-
SLRainbow.Background = SolidColorBrush.White;
118+
SLAllColors.Background = Brush.White;
119+
SLRainbow.Background = Brush.White;
121120

122121
dropEvent.Text = "Drop";
123122
}
124123

125-
void ResetLayouts(object sender, System.EventArgs e)
124+
void ResetLayouts(object sender, EventArgs e)
126125
{
127126
SLAllColors.Clear();
128127
SLRainbow.Clear();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<local:TestContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:Maui.Controls.Sample.Issues"
5+
x:Class="Maui.Controls.Sample.Issues.Issue17531">
6+
<local:TestContentPage.Content>
7+
<StackLayout>
8+
<Label
9+
x:Name="TapLabel"
10+
AutomationId="TapLabel"
11+
Text="Tap Me!">
12+
<Label.GestureRecognizers>
13+
<TapGestureRecognizer Tapped="OnTap"/>
14+
</Label.GestureRecognizers>
15+
</Label>
16+
<Label
17+
x:Name="TapResultLabel"
18+
AutomationId="TapResultLabel"/>
19+
<Label
20+
x:Name="DragLabel"
21+
AutomationId="DragLabel"
22+
Text="Drag Me!">
23+
<Label.GestureRecognizers>
24+
<DragGestureRecognizer
25+
DragStarting="OnDragStart"/>
26+
</Label.GestureRecognizers>
27+
</Label>
28+
<Label
29+
x:Name="DragResultLabel"
30+
AutomationId="DragResultLabel"/>
31+
<Grid>
32+
<Grid.ColumnDefinitions>
33+
<ColumnDefinition Width="*"/>
34+
<ColumnDefinition Width="*"/>
35+
</Grid.ColumnDefinitions>
36+
<ScrollView
37+
AutomationId="AllColorsScrollView"
38+
Grid.Column="0">
39+
<StackLayout
40+
AutomationId="AllColorsStack"
41+
x:Name="SLAllColors">
42+
<StackLayout.GestureRecognizers>
43+
<DropGestureRecognizer
44+
DragOver="OnDragOver"
45+
DragLeave="OnDragLeave"
46+
Drop="OnDrop">
47+
</DropGestureRecognizer>
48+
</StackLayout.GestureRecognizers>
49+
<Label HeightRequest="50"
50+
Background="Red"
51+
Text="Red"
52+
AutomationId="Red">
53+
<Label.GestureRecognizers>
54+
<DragGestureRecognizer
55+
DropCompleted="OnDropCompleted"
56+
DragStarting="OnDragStarting"/>
57+
</Label.GestureRecognizers>
58+
</Label>
59+
<Label HeightRequest="50"
60+
Background="Yellow"
61+
Text="Yellow"
62+
AutomationId="Yellow">
63+
<Label.GestureRecognizers>
64+
<DragGestureRecognizer
65+
DropCompleted="OnDropCompleted"
66+
DragStarting="OnDragStarting"/>
67+
</Label.GestureRecognizers>
68+
</Label>
69+
<Label HeightRequest="50"
70+
Background="Blue"
71+
Text="Blue"
72+
AutomationId="Blue">
73+
<Label.GestureRecognizers>
74+
<DragGestureRecognizer
75+
DropCompleted="OnDropCompleted"
76+
DragStarting="OnDragStarting"/>
77+
</Label.GestureRecognizers>
78+
</Label>
79+
</StackLayout>
80+
</ScrollView>
81+
<ScrollView
82+
AutomationId="RainbowScrollView"
83+
Grid.Column="1"
84+
Background="Brown">
85+
<StackLayout
86+
AutomationId="RainbowStack"
87+
Background="Blue"
88+
x:Name="SLRainbow">
89+
<StackLayout.GestureRecognizers>
90+
<DropGestureRecognizer
91+
DragOver="OnDragOver"
92+
DragLeave="OnDragLeave"
93+
Drop="OnDrop">
94+
</DropGestureRecognizer>
95+
</StackLayout.GestureRecognizers>
96+
<Label
97+
HeightRequest="50"
98+
Background="Green"
99+
Text="Green"
100+
AutomationId="Green">
101+
<Label.GestureRecognizers>
102+
<DragGestureRecognizer
103+
DropCompleted="OnDropCompleted"
104+
DragStarting="OnDragStarting"/>
105+
</Label.GestureRecognizers>
106+
</Label>
107+
</StackLayout>
108+
</ScrollView>
109+
</Grid>
110+
<Label
111+
x:Name="DropResultLabel"
112+
AutomationId="DropResultLabel"/>
113+
</StackLayout>
114+
</local:TestContentPage.Content>
115+
</local:TestContentPage>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
namespace Maui.Controls.Sample.Issues
2+
{
3+
[Issue(IssueTracker.Github, 17531, "Make senders be the same for different gesture EventArgs")]
4+
public partial class Issue17531 : TestContentPage
5+
{
6+
public Issue17531()
7+
{
8+
InitializeComponent();
9+
}
10+
11+
protected override void Init()
12+
{
13+
14+
}
15+
16+
void OnTap(object sender, TappedEventArgs e)
17+
{
18+
if (sender is View)
19+
{
20+
TapResultLabel.Text = "Success";
21+
TapResultLabel.BackgroundColor = Colors.Green;
22+
}
23+
else
24+
{
25+
TapResultLabel.Text = "Failed";
26+
TapResultLabel.BackgroundColor = Colors.Red;
27+
}
28+
}
29+
30+
void OnDragStart(object sender, DragStartingEventArgs e)
31+
{
32+
if (sender is View)
33+
{
34+
DragResultLabel.Text = "Success";
35+
DragResultLabel.BackgroundColor = Colors.Green;
36+
}
37+
else
38+
{
39+
DragResultLabel.Text = "Failed";
40+
DragResultLabel.BackgroundColor = Colors.Red;
41+
}
42+
}
43+
44+
void OnDragStarting(object sender, DragStartingEventArgs e)
45+
{
46+
var label = (Label)sender;
47+
var sl = label.Parent as StackLayout;
48+
e.Data.Properties.Add("Color", label);
49+
e.Data.Properties.Add("Source", sl);
50+
51+
if (sl == SLAllColors)
52+
SLRainbow.Background = Brush.LightBlue;
53+
else
54+
SLAllColors.Background = Brush.LightBlue;
55+
}
56+
57+
void OnDropCompleted(object sender, DropCompletedEventArgs e)
58+
{
59+
var sl = ((Element)sender).Parent as StackLayout;
60+
61+
if (sl == SLAllColors)
62+
SLRainbow.Background = Brush.White;
63+
else
64+
SLAllColors.Background = Brush.White;
65+
}
66+
67+
void OnDragOver(object sender, DragEventArgs e)
68+
{
69+
if (!e.Data.Properties.ContainsKey("Source"))
70+
return;
71+
72+
var sl = ((StackLayout)sender);
73+
if (e.Data.Properties["Source"] == sl)
74+
{
75+
e.AcceptedOperation = DataPackageOperation.None;
76+
return;
77+
}
78+
79+
sl.Background = Brush.LightPink;
80+
}
81+
82+
void OnDragLeave(object sender, DragEventArgs e)
83+
{
84+
if (!e.Data.Properties.ContainsKey("Source"))
85+
return;
86+
87+
var sl = (StackLayout)sender;
88+
if (e.Data.Properties["Source"] == sl)
89+
{
90+
e.AcceptedOperation = DataPackageOperation.None;
91+
return;
92+
}
93+
94+
sl.Background = Brush.LightBlue;
95+
}
96+
97+
void OnDrop(object sender, DropEventArgs e)
98+
{
99+
if (!e.Data.Properties.ContainsKey("Source"))
100+
return;
101+
102+
var sl = (StackLayout)sender;
103+
if (e.Data.Properties["Source"] == sl)
104+
{
105+
return;
106+
}
107+
108+
var color = e.Data.Properties["Color"] as Label;
109+
110+
if (SLAllColors.Children.Contains(color))
111+
{
112+
SLAllColors.Children.Remove(color);
113+
SLRainbow.Children.Add(color);
114+
}
115+
else
116+
{
117+
SLRainbow.Children.Remove(color);
118+
SLAllColors.Children.Add(color);
119+
}
120+
121+
SLAllColors.Background = Brush.White;
122+
SLRainbow.Background = Brush.White;
123+
124+
if (sender is View)
125+
{
126+
DropResultLabel.Text = "Success";
127+
DropResultLabel.BackgroundColor = Colors.Green;
128+
}
129+
else
130+
{
131+
DropResultLabel.Text = "Failed";
132+
DropResultLabel.BackgroundColor = Colors.Red;
133+
}
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)