Skip to content

Commit

Permalink
Dangerous changes to prevent clicks in non designable subcomponents (…
Browse files Browse the repository at this point in the history
…requires thorough testing.
  • Loading branch information
tznind committed Aug 25, 2024
1 parent 8ec52d5 commit 446c48f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Design.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ public Design CreateSubControlDesign(string name, View subView)
if (subView is TextView txt)
{
// prevent control from responding to events
txt.MouseClick += this.SuppressNativeClickEvents;
txt.MouseClick += (s, e) => this.SuppressNativeClickEvents(s, e);
txt.KeyDown += this.SuppressNativeKeyboardEvents;
}

if (subView is TextField tf)
{
// prevent control from responding to events
tf.MouseClick += this.SuppressNativeClickEvents;
tf.MouseClick += (s,e)=>this.SuppressNativeClickEvents(s,e);
tf.KeyDown += SuppressNativeKeyboardEvents;
}

Expand All @@ -245,6 +245,13 @@ public Design CreateSubControlDesign(string name, View subView)
tree.AddObject(new TreeNode($"Example Leaf {l}"));
}
}

// Disable click events for sub controls. This allows you to prevent clicking
// in non designed subcomponents e.g. the bar of a true color picker.
foreach (var v in subView.GetAllNonDesignableSubviews())
{
v.MouseClick += (s,e)=>this.SuppressNativeClickEvents(s,e,true);
}

var d = new Design(this.SourceCode, name, subView);
return d;
Expand Down Expand Up @@ -611,10 +618,17 @@ private void CreateSubControlDesigns(View view)
}
}

private void SuppressNativeClickEvents(object? sender, MouseEventEventArgs obj)
private void SuppressNativeClickEvents(object? sender, MouseEventEventArgs obj, bool alsoSuppressClick = false)
{
// Suppress everything except single click (selection)
obj.Handled = obj.MouseEvent.Flags != MouseFlags.Button1Clicked;
if (alsoSuppressClick)
{
obj.Handled = true;
}
else
{
// Suppress everything except single click (selection)
obj.Handled = obj.MouseEvent.Flags != MouseFlags.Button1Clicked;
}
}

private void SuppressNativeKeyboardEvents(object? sender, Key e)
Expand Down
31 changes: 31 additions & 0 deletions src/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ public static bool IsBorderlessContainerView(this View v)
/// <returns>The <see cref="View"/> at the given screen location or null if none found.</returns>
public static View? HitTest(this View w, MouseEvent m, out bool isBorder, out bool isLowerRight, params View[] ignoring)
{
ignoring = ignoring.Union(w.GetAllNonDesignableSubviews()).ToArray();

// hide the views while we perform the hit test
foreach (View v in ignoring)
{
Expand Down Expand Up @@ -301,6 +303,7 @@ public static bool IsBorderlessContainerView(this View v)
return hit is Adornment a ? a.Parent : hit;
}


/// <summary>
/// <para>
/// Sometimes <see cref="View.FindDeepestView"/> returns what the library considers
Expand Down Expand Up @@ -394,6 +397,32 @@ public static IEnumerable<View> OrderViewsByScreenPosition(IEnumerable<View> vie
.ThenBy(v => v.Frame.X);
}

/// <summary>
/// Returns all subviews that are not Designable. Beware that designs can be nested
/// e.g. calling this on a root view with return subviews that belong to other designed
/// components that were added to the root designed window.
/// </summary>
/// <param name="view"></param>
/// <returns></returns>
public static IEnumerable<View> GetAllNonDesignableSubviews(this View view)
{
var alsoIgnore = new List<View>();
RecursivelyIgnoreAllNonDesignableSubviews(view, alsoIgnore);
return alsoIgnore;
}
private static void RecursivelyIgnoreAllNonDesignableSubviews(View view, List<View> alsoIgnore)
{
foreach (var sub in view.Subviews)
{
if (sub.Data is not Design)
{
alsoIgnore.Add(sub);
}

RecursivelyIgnoreAllNonDesignableSubviews(sub, alsoIgnore);
}
}

private static bool HasNoBorderProperty(this View v)
{
if (v.Border == null || v.BorderStyle == LineStyle.None)
Expand All @@ -403,4 +432,6 @@ private static bool HasNoBorderProperty(this View v)

return false;
}


}

0 comments on commit 446c48f

Please sign in to comment.