Skip to content

Fixes #5143. ColorBar mouse events use MouseBindings with grab/ungrab#5289

Merged
YourRobotOverlord merged 1 commit into
gui-cs:developfrom
YourRobotOverlord:fix/5143-colorbar-mouse-bindings
May 10, 2026
Merged

Fixes #5143. ColorBar mouse events use MouseBindings with grab/ungrab#5289
YourRobotOverlord merged 1 commit into
gui-cs:developfrom
YourRobotOverlord:fix/5143-colorbar-mouse-bindings

Conversation

@YourRobotOverlord
Copy link
Copy Markdown
Collaborator

Copilot Session

cf168204-1066-49b3-be7f-34d23d1ef2c0

Summary

Fixes #5143.

ColorBar.OnMouseEvent previously updated Value directly before the MouseEvent event was raised. This meant external code (e.g. TerminalGuiDesigner) couldn't cancel mouse interaction by subscribing to MouseEvent or by clearing MouseBindings.

Also, drag operations were not bounded to the originating bar — pressing on one ColorBar and dragging into a sibling caused the second bar to react.

Changes

Terminal.Gui/Views/Color/ColorBar.cs

  • Move value update to Command.Activate: Add a MouseBindings.Add(MouseFlags.LeftButtonPressed, Command.Activate) and MouseBindings.Add(MouseFlags.LeftButtonPressed | MouseFlags.PositionReport, Command.Activate) in the constructor. The Command.Activate handler checks whether it was triggered by a LeftButtonPressed mouse event; if so, it updates Value and calls SetFocus(). For all other invocations (e.g. LeftButtonReleased) it falls through to DefaultActivateHandler.
  • Grab lifecycle in OnMouseEvent: OnMouseEvent now only manages grab: GrabMouse(this) on first press, UngrabMouse() on release, always returns false (so the event chain continues to RaiseCommandsBoundToButtonFlags).
  • External cancellability: Callers can now suppress ColorBar mouse interaction by calling mouseBindings.Remove(MouseFlags.LeftButtonPressed) — consistent with how Terminal.Gui cancellable commands work generally.

Tests/UnitTestsParallelizable/Views/ColorPickerTests.cs

  • Four existing test call sites changed from RaiseMouseEvent(...) to NewMouseEvent(...) — necessary because value update moved from OnMouseEvent (called by RaiseMouseEvent) to Command.Activate (only reachable via NewMouseEventRaiseCommandsBoundToButtonFlags).
  • ClickingDifferentBars_ChangesFocus: updated to send a LeftButtonReleased between the two press events (releasing the grab before the second press), matching real terminal behavior.
  • Three new tests added:
    • ColorBar_MousePress_UpdatesValue — verifies value updates on click
    • ColorBar_MouseEvent_CanBeCancelled — subscribes to MouseEvent, sets Handled=true, verifies value is NOT updated (the cancellability fix)
    • ColorBar_Drag_BoundedToOriginatingBar — press on RBar, drag into GBar area, verify only RBar value changed (the drag-bounded fix)

Test Results

All 16,862 tests pass (0 failures, 17 pre-existing skips).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses #5143 by reworking ColorBar mouse handling so callers can cancel/suppress mouse-driven value changes and so drag operations remain bounded to the originating bar (via mouse grab/ungrab).

Changes:

  • Routed ColorBar mouse press/drag into MouseBindingsCommand.Activate, moving value updates out of OnMouseEvent.
  • Added explicit grab/ungrab lifecycle handling to prevent cross-bar drag contamination.
  • Updated and added unit tests to validate click updates, cancellation, and drag bounding behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
Terminal.Gui/Views/Color/ColorBar.cs Moves mouse-driven value updates into Command.Activate and adds grab/ungrab management for bounded drags.
Tests/UnitTestsParallelizable/Views/ColorPickerTests.cs Updates mouse-invocation paths and adds regression tests for cancelability and drag bounding.

Comment thread Terminal.Gui/Views/Color/ColorBar.cs
Comment thread Terminal.Gui/Views/Color/ColorBar.cs
Comment thread Terminal.Gui/Views/Color/ColorBar.cs Outdated
@YourRobotOverlord YourRobotOverlord marked this pull request as draft May 9, 2026 20:53
…/ungrab

Move value-update logic from OnMouseEvent into a Command.Activate
handler bound via MouseBindings.Add. This makes mouse interaction
cancellable by external code (e.g. TerminalGuiDesigner) which can
clear the bindings.

Add GrabMouse/UngrabMouse in OnMouseEvent so drag events are routed
exclusively to the originating bar, preventing cross-bar drag
contamination.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@YourRobotOverlord YourRobotOverlord force-pushed the fix/5143-colorbar-mouse-bindings branch from 7f00968 to 273ca61 Compare May 9, 2026 21:06
@YourRobotOverlord YourRobotOverlord marked this pull request as ready for review May 9, 2026 21:08
@YourRobotOverlord YourRobotOverlord requested a review from tznind May 9, 2026 21:08
Copy link
Copy Markdown
Member

@tig tig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ColorBar mouse events cannot be cancelled

3 participants