Skip to content

Commit 8950846

Browse files
committed
Added a bool to return of WidgetTouched event, to see, if Widget had handled the event or not.
Removed a bug in the samples
1 parent 85c2f75 commit 8950846

File tree

9 files changed

+41
-14
lines changed

9 files changed

+41
-14
lines changed

Mapsui.UI.Shared/MapControl.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,14 @@ private void OnInfo(MapInfoEventArgs mapInfoEventArgs)
307307
Info?.Invoke(this, mapInfoEventArgs);
308308
}
309309

310-
private void WidgetTouched(IWidget widget, Point screenPosition)
310+
private bool WidgetTouched(IWidget widget, Point screenPosition)
311311
{
312312
if (widget is Hyperlink hyperlink)
313313
{
314314
OpenBrowser(hyperlink.Url);
315315
}
316316

317-
widget.HandleWidgetTouched(Navigator, screenPosition);
317+
return widget.HandleWidgetTouched(Navigator, screenPosition);
318318
}
319319

320320
/// <inheritdoc />
@@ -345,7 +345,7 @@ public MapInfo GetMapInfo(IEnumerable<ILayer> layers, Point screenPosition, int
345345
/// <returns>True, if something done </returns>
346346
private static MapInfoEventArgs InvokeInfo(IEnumerable<ILayer> layers, IEnumerable<IWidget> widgets,
347347
IReadOnlyViewport viewport, Point screenPosition, Point startScreenPosition, ISymbolCache symbolCache,
348-
Action<IWidget, Point> widgetCallback, int numTaps)
348+
Func<IWidget, Point, bool> widgetCallback, int numTaps)
349349
{
350350
var layerWidgets = layers.Select(l => l.Attribution).Where(a => a != null);
351351
var allWidgets = layerWidgets.Concat(widgets).ToList(); // Concat layer widgets and map widgets.
@@ -358,8 +358,15 @@ private static MapInfoEventArgs InvokeInfo(IEnumerable<ILayer> layers, IEnumerab
358358
// How should widgetCallback have a handled type thing?
359359
// Widgets should be iterated through rather than getting a single widget,
360360
// based on Z index and then called until handled = true; Ordered By highest Z
361-
widgetCallback(widget, screenPosition);
362-
return null;
361+
var result = widgetCallback(widget, screenPosition);
362+
363+
if (result)
364+
{
365+
return new MapInfoEventArgs
366+
{
367+
Handled = true
368+
};
369+
}
363370
}
364371

365372
var mapInfo = MapInfoHelper.GetMapInfo(layers, viewport, screenPosition, symbolCache);

Mapsui/Widgets/Hyperlink.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ public class Hyperlink : TextBox
77
{
88
public string Url { get; set; }
99

10-
public override void HandleWidgetTouched(INavigator navigator, Point position)
10+
public override bool HandleWidgetTouched(INavigator navigator, Point position)
1111
{
12+
// Because OpenURL is called from MapControl by default
13+
// TODO: Shouldn't OpenURL() called from here instead of from MapControl?
14+
return true;
1215
}
1316
}
1417
}

Mapsui/Widgets/IWidget.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ public interface IWidget
88
VerticalAlignment VerticalAlignment { get; set; }
99
float MarginX { get; set; }
1010
float MarginY { get; set; }
11+
1112
/// <summary>
1213
/// The hit box of the widget. This needs to be updated from the widget renderer.
1314
/// </summary>
1415
BoundingBox Envelope { get; set; }
1516

16-
void HandleWidgetTouched(INavigator navigator, Point position);
17+
/// <summary>
18+
/// Function, which is called, when a Widget is hiten
19+
/// </summary>
20+
/// <param name="navigator">Navigator of MapControl</param>
21+
/// <param name="position">Screen position</param>
22+
/// <returns>True, if the Widget had handled the touch event</returns>
23+
bool HandleWidgetTouched(INavigator navigator, Point position);
1724
}
1825
}

Mapsui/Widgets/ScaleBar/ScaleBarWidget.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,9 @@ public Point[] GetScaleBarLinePositions(IReadOnlyViewport viewport, float scaleB
458458
}
459459
}
460460

461-
public override void HandleWidgetTouched(INavigator navigator, Point position)
461+
public override bool HandleWidgetTouched(INavigator navigator, Point position)
462462
{
463+
return false;
463464
}
464465

465466
public bool CanTransform()

Mapsui/Widgets/Widget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ public float CalculatePositionY(float top, float bottom, float height)
4545
throw new ArgumentException("Unknown vertical alignment: " + VerticalAlignment);
4646
}
4747

48-
public abstract void HandleWidgetTouched(INavigator navigator, Point position);
48+
public abstract bool HandleWidgetTouched(INavigator navigator, Point position);
4949
}
5050
}

Mapsui/Widgets/WidgetTouchedEventArgs.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,10 @@ public WidgetTouchedEventArgs(Point position)
1111
}
1212

1313
public Point Position { get; }
14+
15+
/// <summary>
16+
/// True, if this Widget had handled this event
17+
/// </summary>
18+
public bool Handled { get; set; }
1419
}
1520
}

Mapsui/Widgets/Zoom/ZoomInOutWidget.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,15 @@ public float Opacity
152152
}
153153
}
154154

155-
public override void HandleWidgetTouched(INavigator navigator, Point position)
155+
public override bool HandleWidgetTouched(INavigator navigator, Point position)
156156
{
157157
var handler = WidgetTouched;
158158

159159
if (handler != null)
160160
{
161-
handler.Invoke(this, new WidgetTouchedEventArgs(position));
162-
return;
161+
var args = new WidgetTouchedEventArgs(position);
162+
handler.Invoke(this, args);
163+
return args.Handled;
163164
}
164165

165166
if (Orientation == Orientation.Vertical && position.Y < Envelope.MinY + Envelope.Height * 0.5 ||
@@ -171,6 +172,8 @@ public override void HandleWidgetTouched(INavigator navigator, Point position)
171172
{
172173
navigator.ZoomOut();
173174
}
175+
176+
return true;
174177
}
175178

176179
internal void OnPropertyChanged([CallerMemberName] string name = "")

Samples/Mapsui.Samples.CustomWidget/CustomWidget.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ public class CustomWidget : IWidget
1111
public float MarginX { get; set; } = 20;
1212
public float MarginY { get; set; } = 20;
1313
public BoundingBox Envelope { get; set; }
14-
public void HandleWidgetTouched(INavigator navigator, Point position)
14+
public bool HandleWidgetTouched(INavigator navigator, Point position)
1515
{
1616
navigator.CenterOn(0, 0);
17+
return true;
1718
}
1819

1920
public Color Color { get; set; }

Samples/Mapsui.Samples.Uwp/MainPage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private void FillComboBoxWithCategories()
4949

5050
private void MapOnInfo(object sender, MapInfoEventArgs args)
5151
{
52-
if (args.MapInfo.Feature != null)
52+
if (args.MapInfo?.Feature != null)
5353
FeatureInfo.Text = $"Click Info:{Environment.NewLine}{args.MapInfo.Feature.ToDisplayText()}";
5454
}
5555

0 commit comments

Comments
 (0)