Skip to content

Commit

Permalink
feat: Use preventDefault to prevent browser from processing Handled e…
Browse files Browse the repository at this point in the history
…vents

chore: Prevent default only for non-Pointer events

chore: Comment
  • Loading branch information
MartinZikmund committed May 28, 2021
1 parent 0a39f2e commit 974f689
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Input\PointersTests\ScrollHandled.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Input\PointersTests\ScrollViewer_PointerMoved.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -4530,6 +4534,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Input\PointersTests\HitTest_Shapes.xaml.cs">
<DependentUpon>HitTest_Shapes.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Input\PointersTests\ScrollHandled.xaml.cs">
<DependentUpon>ScrollHandled.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Input\PointersTests\ScrollViewer_PointerMoved.xaml.cs">
<DependentUpon>ScrollViewer_PointerMoved.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -7399,4 +7406,4 @@
<None Include="$(MSBuildThisFileDirectory)ItemExclusions.props" />
</ItemGroup>
<Import Project="ItemExclusions.props" />
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<Page
x:Class="UITests.Windows_UI_Input.PointersTests.ScrollHandled"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Windows_UI_Input.PointersTests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<ScrollViewer>
<StackPanel PointerWheelChanged="OnPointerWheelChanged">
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
<Button>Test</Button>
</StackPanel>
</ScrollViewer>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Uno.UI.Samples.Controls;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

namespace UITests.Windows_UI_Input.PointersTests
{
[Sample("Pointers", Description = "When scroll wheel is used while hovering above the buttons, it should not scroll. When scroll wheel is used above the page's blank area, it should scroll.")]
public sealed partial class ScrollHandled : Page
{
public ScrollHandled()
{
InitializeComponent();
}

private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs args)
{
args.Handled = true;
}
}
}
1 change: 1 addition & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ var Uno;
var handled = this.dispatchEvent(element, eventName, eventPayload);
if (handled) {
event.stopPropagation();
event.preventDefault();
}
};
element.addEventListener(eventName, eventHandler, onCapturePhase);
Expand Down
4 changes: 4 additions & 0 deletions src/Uno.UI/ts/WindowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,9 @@ namespace Uno.UI {
const handled = WindowManager.current.dispatchEvent(element, evt.type, payload);
if (handled) {
evt.stopPropagation();
// Not calling preventDefault() here, as that will break native focus dispatch for pointerdown
// If needed, we may add preventDefault() for some specific event type later, but it is not needed
// for any scenario yet.
}
}

Expand Down Expand Up @@ -1028,6 +1031,7 @@ namespace Uno.UI {
var handled = this.dispatchEvent(element, eventName, eventPayload);
if (handled) {
event.stopPropagation();
event.preventDefault();
}
};

Expand Down

0 comments on commit 974f689

Please sign in to comment.