66// License: MIT
77// -----------------------------------------------------------------------
88
9+ using SharpConsoleUI . Configuration ;
910using SharpConsoleUI . Drivers ;
1011using SharpConsoleUI . Events ;
11-
1212using SharpConsoleUI . Extensions ;
1313namespace SharpConsoleUI . Controls ;
1414
@@ -124,7 +124,7 @@ public bool ProcessMouseEvent(MouseEventArgs args)
124124 if ( args . HasFlag ( MouseFlags . ButtonShift ) )
125125 {
126126 int oldH = _horizontalScrollOffset ;
127- _horizontalScrollOffset = Math . Max ( 0 , _horizontalScrollOffset - 3 ) ;
127+ _horizontalScrollOffset = Math . Max ( 0 , _horizontalScrollOffset - ControlDefaults . DefaultScrollWheelLines ) ;
128128 if ( _horizontalScrollOffset != oldH )
129129 {
130130 Container ? . Invalidate ( true ) ;
@@ -134,7 +134,7 @@ public bool ProcessMouseEvent(MouseEventArgs args)
134134 }
135135
136136 int oldOffset = _scrollOffset ;
137- ScrollOffset = Math . Max ( 0 , _scrollOffset - 3 ) ;
137+ ScrollOffset = Math . Max ( 0 , _scrollOffset - ControlDefaults . DefaultScrollWheelLines ) ;
138138 return _scrollOffset != oldOffset ; // bubble if didn't scroll
139139 }
140140
@@ -143,7 +143,7 @@ public bool ProcessMouseEvent(MouseEventArgs args)
143143 if ( args . HasFlag ( MouseFlags . ButtonShift ) )
144144 {
145145 int oldH = _horizontalScrollOffset ;
146- _horizontalScrollOffset += 3 ;
146+ _horizontalScrollOffset += ControlDefaults . DefaultScrollWheelLines ;
147147 if ( _horizontalScrollOffset != oldH )
148148 {
149149 Container ? . Invalidate ( true ) ;
@@ -154,12 +154,12 @@ public bool ProcessMouseEvent(MouseEventArgs args)
154154
155155 int oldOffset = _scrollOffset ;
156156 int maxOffset = Math . Max ( 0 , RowCount - GetVisibleRowCount ( ) ) ;
157- ScrollOffset = Math . Min ( maxOffset , _scrollOffset + 3 ) ;
157+ ScrollOffset = Math . Min ( maxOffset , _scrollOffset + ControlDefaults . DefaultScrollWheelLines ) ;
158158 return _scrollOffset != oldOffset ; // bubble if didn't scroll
159159 }
160160
161- // Left-click
162- if ( args . HasFlag ( MouseFlags . Button1Clicked ) || args . HasFlag ( MouseFlags . Button1Pressed ) )
161+ // Button1Pressed: handle operations that need immediate response (drag initiation)
162+ if ( args . HasFlag ( MouseFlags . Button1Pressed ) )
163163 {
164164 // Cancel any active cell edit before changing selection
165165 if ( _isEditing )
@@ -169,25 +169,50 @@ public bool ProcessMouseEvent(MouseEventArgs args)
169169 if ( ! HasFocus && CanFocusWithMouse )
170170 this . GetParentWindow ( ) ? . FocusManager . SetFocus ( this , FocusReason . Mouse ) ;
171171
172- // Check if clicking on scrollbar
172+ // Scrollbar thumb drag initiation
173173 if ( IsClickOnVerticalScrollbar ( args ) )
174174 {
175- HandleVerticalScrollbarClick ( args ) ;
175+ HandleVerticalScrollbarThumbPress ( args ) ;
176176 return true ;
177177 }
178178
179179 if ( IsClickOnHorizontalScrollbar ( args ) )
180180 {
181- HandleHorizontalScrollbarClick ( args ) ;
181+ HandleHorizontalScrollbarThumbPress ( args ) ;
182182 return true ;
183183 }
184184
185- // Check if clicking on column resize border (only when not read-only)
185+ // Column resize initiation (only when not read-only)
186186 if ( ! _readOnly && _columnResizeEnabled && IsClickOnColumnBorder ( args ) )
187187 {
188188 BeginColumnResize ( args ) ;
189189 return true ;
190190 }
191+ }
192+
193+ // Button1Clicked: handle discrete click actions (arrows, track, sorting, selection)
194+ if ( args . HasFlag ( MouseFlags . Button1Clicked ) )
195+ {
196+ // Cancel any active cell edit before changing selection
197+ if ( _isEditing )
198+ CancelEdit ( ) ;
199+
200+ // Set focus on click
201+ if ( ! HasFocus && CanFocusWithMouse )
202+ this . GetParentWindow ( ) ? . FocusManager . SetFocus ( this , FocusReason . Mouse ) ;
203+
204+ // Scrollbar arrow/track clicks
205+ if ( IsClickOnVerticalScrollbar ( args ) )
206+ {
207+ HandleVerticalScrollbarClick ( args ) ;
208+ return true ;
209+ }
210+
211+ if ( IsClickOnHorizontalScrollbar ( args ) )
212+ {
213+ HandleHorizontalScrollbarClick ( args ) ;
214+ return true ;
215+ }
191216
192217 // Check if clicking on header (for sorting)
193218 if ( IsClickOnHeader ( args ) )
@@ -444,10 +469,24 @@ private int GetScrollbarDataStartY()
444469 return dataStartY ;
445470 }
446471
472+ private void HandleVerticalScrollbarThumbPress ( MouseEventArgs args )
473+ {
474+ int contentAreaHeight = GetScrollbarContentHeight ( ) ;
475+ var ( _, _, thumbY , thumbHeight ) = GetVerticalScrollbarGeometry ( contentAreaHeight ) ;
476+ int relY = args . Position . Y - GetScrollbarDataStartY ( ) ;
477+
478+ if ( relY >= thumbY && relY < thumbY + thumbHeight )
479+ {
480+ _isVerticalScrollbarDragging = true ;
481+ _scrollbarDragStartY = args . Position . Y ;
482+ _scrollbarDragStartOffset = _scrollOffset ;
483+ }
484+ }
485+
447486 private void HandleVerticalScrollbarClick ( MouseEventArgs args )
448487 {
449488 int contentAreaHeight = GetScrollbarContentHeight ( ) ;
450- var ( trackTop , trackHeight , thumbY , thumbHeight ) = GetVerticalScrollbarGeometry ( contentAreaHeight ) ;
489+ var ( _ , trackHeight , thumbY , thumbHeight ) = GetVerticalScrollbarGeometry ( contentAreaHeight ) ;
451490 int relY = args . Position . Y - GetScrollbarDataStartY ( ) ;
452491 int maxOffset = Math . Max ( 0 , RowCount - GetVisibleRowCount ( ) ) ;
453492
@@ -461,19 +500,12 @@ private void HandleVerticalScrollbarClick(MouseEventArgs args)
461500 // Arrow down
462501 ScrollOffset = Math . Min ( maxOffset , _scrollOffset + 1 ) ;
463502 }
464- else if ( relY >= thumbY && relY < thumbY + thumbHeight )
465- {
466- // Thumb: start drag
467- _isVerticalScrollbarDragging = true ;
468- _scrollbarDragStartY = args . Position . Y ;
469- _scrollbarDragStartOffset = _scrollOffset ;
470- }
471503 else if ( relY < thumbY )
472504 {
473505 // Track above thumb: page up
474506 ScrollOffset = Math . Max ( 0 , _scrollOffset - GetVisibleRowCount ( ) ) ;
475507 }
476- else
508+ else if ( relY >= thumbY + thumbHeight )
477509 {
478510 // Track below thumb: page down
479511 ScrollOffset = Math . Min ( maxOffset , _scrollOffset + GetVisibleRowCount ( ) ) ;
@@ -506,11 +538,28 @@ private int GetScrollbarContentWidth()
506538 return Math . Max ( 0 , contentWidth ) ;
507539 }
508540
541+ private void HandleHorizontalScrollbarThumbPress ( MouseEventArgs args )
542+ {
543+ int contentWidth = GetScrollbarContentWidth ( ) ;
544+ int totalColumnsWidth = GetTotalColumnsWidth ( ) ;
545+ var ( _, _, thumbX , thumbWidth ) = GetHorizontalScrollbarGeometry ( contentWidth , totalColumnsWidth ) ;
546+
547+ int relX = args . Position . X - Margin . Left ;
548+ if ( _borderStyle != BorderStyle . None ) relX -- ;
549+
550+ if ( relX >= thumbX && relX < thumbX + thumbWidth )
551+ {
552+ _isHorizontalScrollbarDragging = true ;
553+ _scrollbarDragStartX = args . Position . X ;
554+ _scrollbarDragStartOffset = _horizontalScrollOffset ;
555+ }
556+ }
557+
509558 private void HandleHorizontalScrollbarClick ( MouseEventArgs args )
510559 {
511560 int contentWidth = GetScrollbarContentWidth ( ) ;
512561 int totalColumnsWidth = GetTotalColumnsWidth ( ) ;
513- var ( trackLeft , trackWidth , thumbX , thumbWidth ) = GetHorizontalScrollbarGeometry ( contentWidth , totalColumnsWidth ) ;
562+ var ( _ , trackWidth , thumbX , thumbWidth ) = GetHorizontalScrollbarGeometry ( contentWidth , totalColumnsWidth ) ;
514563
515564 int relX = args . Position . X - Margin . Left ;
516565 if ( _borderStyle != BorderStyle . None ) relX -- ; // account for left border
@@ -526,19 +575,12 @@ private void HandleHorizontalScrollbarClick(MouseEventArgs args)
526575 // Arrow right
527576 _horizontalScrollOffset = Math . Min ( maxHScroll , _horizontalScrollOffset + 1 ) ;
528577 }
529- else if ( relX >= thumbX && relX < thumbX + thumbWidth )
530- {
531- // Thumb: start drag
532- _isHorizontalScrollbarDragging = true ;
533- _scrollbarDragStartX = args . Position . X ;
534- _scrollbarDragStartOffset = _horizontalScrollOffset ;
535- }
536578 else if ( relX < thumbX )
537579 {
538580 // Track left of thumb: page left
539581 _horizontalScrollOffset = Math . Max ( 0 , _horizontalScrollOffset - contentWidth ) ;
540582 }
541- else
583+ else if ( relX >= thumbX + thumbWidth )
542584 {
543585 // Track right of thumb: page right
544586 _horizontalScrollOffset = Math . Min ( maxHScroll , _horizontalScrollOffset + contentWidth ) ;
0 commit comments