Skip to content

Commit 12fe1c2

Browse files
committed
Fixed OnMouseClicked
1 parent 7866e80 commit 12fe1c2

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

Terminal.Gui/View/View.Mouse.cs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ protected virtual void OnMouseLeave () { }
213213
/// </para>
214214
/// <para>
215215
/// This method raises <see cref="RaiseMouseEvent"/>/<see cref="MouseEvent"/>; if not handled, and one of the
216-
/// mouse buttons was clicked, the <see cref="OnMouseClick"/>/<see cref="MouseClick"/> event will be raised
216+
/// mouse buttons was clicked, the <see cref="RaiseMouseClickEvent"/>/<see cref="MouseClick"/> event will be raised
217217
/// </para>
218218
/// <para>
219219
/// See <see cref="SetPressedHighlight"/> for more information.
@@ -286,7 +286,7 @@ protected virtual void OnMouseLeave () { }
286286
// If it's a click, and we didn't handle it, then we need to generate a click event
287287
// We get here if the view did not handle the mouse event via OnMouseEvent/MouseEvent and
288288
// it did not handle the press/release/clicked events via HandlePress/HandleRelease/HandleClicked
289-
return OnMouseClick (mouseEvent);
289+
return RaiseMouseClickEvent (mouseEvent);
290290
}
291291

292292
return false;
@@ -334,31 +334,19 @@ protected virtual bool OnMouseEvent (MouseEventArgs mouseEvent)
334334

335335
#region Mouse Click Events
336336

337-
/// <summary>Raised when a mouse click occurs.</summary>
338-
///
337+
/// <summary>Raises the <see cref="OnMouseClick"/>/<see cref="MouseClick"/> event.</summary>
339338
/// <remarks>
340339
/// <para>
341-
/// Fired when the mouse is either clicked or double-clicked. Check
342-
/// <see cref="Terminal.Gui.MouseEventArgs.Flags"/> to see which button was clicked.
343-
/// </para>
344-
/// <para>
345-
/// The coordinates are relative to <see cref="View.Viewport"/>.
340+
/// Called when the mouse is either clicked or double-clicked.
346341
/// </para>
347-
/// </remarks>
348-
public event EventHandler<MouseEventArgs>? MouseClick;
349-
350-
/// <summary>Invokes the MouseClick event.</summary>
351-
/// <remarks>
352342
/// <para>
353-
/// Called when the mouse is either clicked or double-clicked. Check
354-
/// <see cref="Terminal.Gui.MouseEventArgs.Flags"/> to see which button was clicked.
343+
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be invoked on every mouse event where
344+
/// the mouse button is pressed.
355345
/// </para>
356346
/// </remarks>
357347
/// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
358-
protected bool OnMouseClick (MouseEventArgs args)
348+
protected bool RaiseMouseClickEvent (MouseEventArgs args)
359349
{
360-
// BUGBUG: This should be named NewMouseClickEvent. Fix this in https://github.com/gui-cs/Terminal.Gui/issues/3029
361-
362350
// Pre-conditions
363351
if (!Enabled)
364352
{
@@ -368,7 +356,10 @@ protected bool OnMouseClick (MouseEventArgs args)
368356

369357
// Cancellable event
370358

371-
// BUGBUG: There should be a call to a protected virtual OnMouseClick here. Fix this in https://github.com/gui-cs/Terminal.Gui/issues/3029
359+
if (OnMouseClick (args) || args.Handled)
360+
{
361+
return args.Handled;
362+
}
372363

373364
MouseClick?.Invoke (this, args);
374365

@@ -386,6 +377,34 @@ protected bool OnMouseClick (MouseEventArgs args)
386377
return args.Handled;
387378
}
388379

380+
/// <summary>
381+
/// Called when a mouse click occurs. Check <see cref="MouseEventArgs.Flags"/> to see which button was clicked.
382+
/// </summary>
383+
/// <remarks>
384+
/// <para>
385+
/// Called when the mouse is either clicked or double-clicked.
386+
/// </para>
387+
/// <para>
388+
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be called on every mouse event where
389+
/// the mouse button is pressed.
390+
/// </para>
391+
/// </remarks>
392+
/// <param name="args"></param>
393+
/// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
394+
protected virtual bool OnMouseClick (MouseEventArgs args) { return false; }
395+
396+
/// <summary>Raised when a mouse click occurs.</summary>
397+
/// <remarks>
398+
/// <para>
399+
/// Raised when the mouse is either clicked or double-clicked.
400+
/// </para>
401+
/// <para>
402+
/// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be raised on every mouse event where
403+
/// the mouse button is pressed.
404+
/// </para>
405+
/// </remarks>
406+
public event EventHandler<MouseEventArgs>? MouseClick;
407+
389408
/// <summary>
390409
/// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the click event (typically
391410
/// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
@@ -414,7 +433,7 @@ internal bool WhenGrabbedHandleClicked (MouseEventArgs mouseEvent)
414433
// If mouse is still in bounds, generate a click
415434
if (!WantMousePositionReports && Viewport.Contains (mouseEvent.Position))
416435
{
417-
return OnMouseClick (mouseEvent);
436+
return RaiseMouseClickEvent (mouseEvent);
418437
}
419438

420439
return mouseEvent.Handled = true;
@@ -468,18 +487,18 @@ private bool WhenGrabbedHandlePressed (MouseEventArgs mouseEvent)
468487
|| mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed)
469488
|| mouseEvent.Flags.HasFlag (MouseFlags.Button4Pressed))
470489
{
490+
bool firstPress = true;
471491
// The first time we get pressed event, grab the mouse and set focus
472492
if (Application.MouseGrabView != this)
473493
{
494+
firstPress = true;
474495
Application.GrabMouse (this);
475496

476497
if (!HasFocus && CanFocus)
477498
{
478499
// Set the focus, but don't invoke Accept
479500
SetFocus ();
480501
}
481-
482-
mouseEvent.Handled = true;
483502
}
484503

485504
if (Viewport.Contains (mouseEvent.Position))
@@ -500,10 +519,10 @@ private bool WhenGrabbedHandlePressed (MouseEventArgs mouseEvent)
500519
}
501520
}
502521

503-
if (WantContinuousButtonPressed && Application.MouseGrabView == this)
522+
if (!firstPress && WantContinuousButtonPressed && Application.MouseGrabView == this)
504523
{
505524
// If this is not the first pressed event, generate a click
506-
return OnMouseClick (mouseEvent);
525+
return RaiseMouseClickEvent (mouseEvent);
507526
}
508527

509528
return mouseEvent.Handled = true;

Terminal.Gui/Views/Slider.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,11 +1381,9 @@ protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
13811381

13821382
mouseEvent.Handled = true;
13831383

1384-
// BUGBUG: OnMouseClick is/should be internal.
1385-
return OnMouseClick (mouseEvent);
13861384
}
13871385

1388-
return false;
1386+
return mouseEvent.Handled;
13891387

13901388
Point ClampMovePosition (Point position)
13911389
{

0 commit comments

Comments
 (0)