Skip to content

Commit 6744488

Browse files
committed
fix: Allow explicit disabling of the browser context menu
1 parent e1e6c88 commit 6744488

File tree

7 files changed

+78
-35
lines changed

7 files changed

+78
-35
lines changed

src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.wasm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Uno.Extensions;
2-
using Uno.Foundation;
2+
using Windows.UI.Xaml.Controls.Primitives;
33
using Windows.UI.Xaml.Input;
44
using Windows.UI.Xaml.Media;
55

@@ -43,7 +43,7 @@ private void UpdateTextBoxView()
4343
}
4444
}
4545
}
46-
46+
4747
partial void InitializePropertiesPartial()
4848
{
4949
if (_header != null)

src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.wasm.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using Uno.UI;
5-
using Uno.Extensions;
6-
using Windows.UI.Xaml.Media;
7-
using Uno.Foundation.Logging;
8-
using Windows.Foundation;
92
using System.Globalization;
103
using Uno.Disposables;
114
using Uno.Foundation;
5+
using Uno.UI;
6+
using Windows.Foundation;
127
using Windows.UI.Xaml.Input;
8+
using Windows.UI.Xaml.Media;
139

1410
namespace Windows.UI.Xaml.Controls
1511
{

src/Uno.UI/UI/Xaml/UIElement.cs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,29 @@
22
#pragma warning disable CS0067
33
#endif
44

5-
using Windows.Foundation;
6-
using Windows.UI.Xaml.Input;
7-
using Windows.UI.Xaml.Media;
8-
using System.Collections.Generic;
9-
using Uno.Extensions;
10-
using Uno.Foundation.Logging;
11-
using Uno.Disposables;
12-
using Windows.UI.Xaml.Controls;
13-
using Uno.UI;
14-
using Uno;
15-
using Uno.UI.Controls;
16-
using Uno.UI.Media;
175
using System;
18-
using System.Collections;
6+
using System.Collections.Generic;
197
using System.Numerics;
208
using System.Reflection;
21-
using Windows.UI.Xaml.Markup;
22-
23-
using Windows.UI.Xaml.Controls.Primitives;
24-
using Windows.UI.Core;
9+
using System.Runtime.CompilerServices;
2510
using System.Text;
11+
using Uno;
12+
using Uno.Disposables;
13+
using Uno.Extensions;
14+
using Uno.Foundation.Logging;
15+
using Uno.UI.Extensions;
16+
using Uno.UI.Media;
2617
using Uno.UI.Xaml;
27-
using Windows.UI.Xaml.Automation.Peers;
2818
using Uno.UI.Xaml.Core;
2919
using Uno.UI.Xaml.Input;
30-
using System.Runtime.CompilerServices;
20+
using Windows.Foundation;
3121
using Windows.Graphics.Display;
32-
using Uno.UI.Extensions;
22+
using Windows.UI.Xaml.Automation.Peers;
23+
using Windows.UI.Xaml.Controls;
24+
using Windows.UI.Xaml.Controls.Primitives;
25+
using Windows.UI.Xaml.Input;
26+
using Windows.UI.Xaml.Markup;
27+
using Windows.UI.Xaml.Media;
3328

3429
#if __IOS__
3530
using UIKit;
@@ -531,13 +526,16 @@ private protected virtual void OnContextFlyoutChanged(FlyoutBase oldValue, Flyou
531526
{
532527
RightTapped -= OpenContextFlyout;
533528
}
529+
OnContextFlyoutChangedPartial(newValue);
534530
}
535531

532+
partial void OnContextFlyoutChangedPartial(FlyoutBase newValue);
533+
536534
private void OpenContextFlyout(object sender, RightTappedRoutedEventArgs args)
537535
{
538-
if (this is FrameworkElement fe)
536+
if (this is FrameworkElement fe && ContextFlyout is { } flyout)
539537
{
540-
ContextFlyout?.ShowAt(
538+
flyout.ShowAt(
541539
placementTarget: fe,
542540
showOptions: new FlyoutShowOptions()
543541
{

src/Uno.UI/UI/Xaml/UIElement.wasm.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Uno.UI.Xaml;
1616
using Uno.UI.Xaml.Core;
1717
using Windows.UI.Xaml.Controls;
18+
using Windows.UI.Xaml.Controls.Primitives;
1819
using Windows.System;
1920

2021
namespace Windows.UI.Xaml
@@ -366,6 +367,18 @@ partial void OnUidChangedPartial()
366367
}
367368
}
368369

370+
partial void OnContextFlyoutChangedPartial(FlyoutBase newValue)
371+
{
372+
if (newValue is { } flyout)
373+
{
374+
SetCssClasses("context-menu-disabled");
375+
}
376+
else
377+
{
378+
UnsetCssClasses("context-menu-disabled");
379+
}
380+
}
381+
369382
public int MeasureCallCount { get; protected set; }
370383
public int ArrangeCallCount { get; protected set; }
371384

src/Uno.UI/WasmScripts/Uno.UI.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ declare namespace Uno.UI {
562562
private init;
563563
private static initMethods;
564564
private initDom;
565+
private static elementOrAncestorHasClass;
565566
private removeLoading;
566567
private resize;
567568
private onfocusin;

src/Uno.UI/WasmScripts/Uno.UI.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1648,12 +1648,29 @@ var Uno;
16481648
document.body.appendChild(this.containerElement);
16491649
window.addEventListener("resize", x => this.resize());
16501650
window.addEventListener("contextmenu", x => {
1651-
if (!(x.target instanceof HTMLInputElement)) {
1651+
if (!(x.target instanceof HTMLInputElement) ||
1652+
WindowManager.elementOrAncestorHasClass(x.target, "context-menu-disabled")) {
16521653
x.preventDefault();
16531654
}
16541655
});
16551656
window.addEventListener("blur", this.onWindowBlur);
16561657
}
1658+
static elementOrAncestorHasClass(element, className) {
1659+
if (!element) {
1660+
return false;
1661+
}
1662+
var parent = element;
1663+
do {
1664+
if (parent === document) {
1665+
break;
1666+
}
1667+
if (parent instanceof HTMLElement &&
1668+
parent.classList.contains(className)) {
1669+
return true;
1670+
}
1671+
} while (parent = parent.parentNode);
1672+
return false;
1673+
}
16571674
removeLoading() {
16581675
if (!this.loadingElementId) {
16591676
return;

src/Uno.UI/ts/WindowManager.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ namespace Uno.UI {
279279
}
280280

281281
if (contentDefinition) {
282-
let classes = element.classList.value;
282+
let classes = element.classList.value;
283283
for (const className of uiElementRegistration.classNames) {
284284
classes += " uno-" + className;
285285
}
@@ -1281,7 +1281,7 @@ namespace Uno.UI {
12811281
*
12821282
* @param maxWidth string containing width in pixels. Empty string means infinite.
12831283
* @param maxHeight string containing height in pixels. Empty string means infinite.
1284-
* @param measureContent if we're interested by the content of the control (<img>'s image, <input>'s text...)
1284+
* @param measureContent if we're interested by the content of the control (<img>'s image, <input>'s text...)
12851285
*/
12861286
public measureView(viewId: string, maxWidth: string, maxHeight: string, measureContent: boolean = true): string {
12871287

@@ -1727,13 +1727,31 @@ namespace Uno.UI {
17271727

17281728
window.addEventListener("resize", x => this.resize());
17291729
window.addEventListener("contextmenu", x => {
1730-
if (!(x.target instanceof HTMLInputElement)) {
1730+
if (!(x.target instanceof HTMLInputElement) ||
1731+
WindowManager.elementOrAncestorHasClass(x.target, "context-menu-disabled")) {
17311732
x.preventDefault();
17321733
}
17331734
})
17341735
window.addEventListener("blur", this.onWindowBlur);
17351736
}
17361737

1738+
private static elementOrAncestorHasClass(element: Node, className: string) {
1739+
if (!element) {
1740+
return false;
1741+
}
1742+
var parent = element;
1743+
do {
1744+
if (parent === document) {
1745+
break;
1746+
}
1747+
if (parent instanceof HTMLElement &&
1748+
parent.classList.contains(className)) {
1749+
return true;
1750+
}
1751+
} while (parent = parent.parentNode);
1752+
return false;
1753+
}
1754+
17371755
private removeLoading() {
17381756

17391757
if (!this.loadingElementId) {

0 commit comments

Comments
 (0)