@@ -247,6 +247,7 @@ impl InputField {
247247 wrap_content_fun ! ( move_current_line_up) ;
248248 wrap_content_fun ! ( move_current_line_down) ;
249249 wrap_content_fun ! ( select_word_around) ;
250+ wrap_content_fun ! ( select_non_space_around) ;
250251
251252 pub fn page_up ( & mut self ) -> bool {
252253 if self . content . move_lines_up ( self . area . height as usize ) {
@@ -372,6 +373,34 @@ impl InputField {
372373 }
373374 }
374375
376+ /// Get the position in the input field from row, column
377+ pub fn get_pos ( & self , row : u16 , column : u16 ) -> Option < Pos > {
378+ if self . area . contains ( column, row) {
379+ let y = ( ( row - self . area . top ) as usize + self . scroll . y )
380+ . min ( self . content . line_count ( ) - 1 ) ;
381+ let line = & self . content . lines ( ) [ y] ;
382+ let x = line
383+ . col_to_char_idx ( ( column - self . area . left ) as usize + self . scroll . x )
384+ . unwrap_or ( line. chars . len ( ) ) ;
385+ Some ( Pos { x, y } )
386+ } else {
387+ None
388+ }
389+ }
390+
391+ /// Set the cursor position in the input field
392+ ///
393+ /// The position set may be different to ensure consistency
394+ /// (for example if it's after the end, it will be set back).
395+ pub fn set_cursor_pos ( & mut self , pos : Pos ) {
396+ self . content . set_cursor_pos ( pos) ;
397+ }
398+
399+ /// Get the position in the input field from a mouse event
400+ pub fn get_mouse_event_pos ( & self , mouse_event : MouseEvent ) -> Option < Pos > {
401+ self . get_pos ( mouse_event. row , mouse_event. column )
402+ }
403+
375404 /// Apply a mouse event
376405 pub fn apply_mouse_event ( & mut self , mouse_event : MouseEvent , is_double_click : bool ) -> bool {
377406 let MouseEvent {
@@ -380,50 +409,43 @@ impl InputField {
380409 row,
381410 modifiers,
382411 } = mouse_event;
383- if self . area . contains ( column, row) {
384- if self . focused {
385- let y = ( ( row - self . area . top ) as usize + self . scroll . y )
386- . min ( self . content . line_count ( ) - 1 ) ;
387- let line = & self . content . lines ( ) [ y] ;
388- let x = line
389- . col_to_char_idx ( ( column - self . area . left ) as usize + self . scroll . x )
390- . unwrap_or ( line. chars . len ( ) ) ;
391- // We handle the selection click on down, so that it's set at the
392- // start of drag.
393- match kind {
394- MouseEventKind :: Down ( MouseButton :: Left ) => {
395- // FIXME Crossterm doesn't seem to send shift modifier
396- // with up or down mouse events
397- if modifiers == KeyModifiers :: SHIFT {
398- self . content . make_selection ( ) ;
399- } else {
400- self . content . unselect ( ) ;
401- }
402- self . content . set_cursor_pos ( Pos { x, y } ) ;
403- }
404- MouseEventKind :: Up ( MouseButton :: Left ) if is_double_click => {
405- self . content . set_cursor_pos ( Pos { x, y } ) ;
406- self . content . select_word_around ( ) ;
407- }
408- MouseEventKind :: Drag ( MouseButton :: Left ) => {
412+ let Some ( Pos { x, y } ) = self . get_pos ( row, column) else {
413+ return false ;
414+ } ;
415+ if self . focused {
416+ // We handle the selection click on down, so that it's set at the
417+ // start of drag.
418+ match kind {
419+ MouseEventKind :: Down ( MouseButton :: Left ) => {
420+ // FIXME Crossterm doesn't seem to send shift modifier
421+ // with up or down mouse events
422+ if modifiers == KeyModifiers :: SHIFT {
409423 self . content . make_selection ( ) ;
410- self . content . set_cursor_pos ( Pos { x, y } ) ;
411- }
412- MouseEventKind :: ScrollDown => {
413- self . scroll_down ( ) ;
414- }
415- MouseEventKind :: ScrollUp => {
416- self . scroll_up ( ) ;
424+ } else {
425+ self . content . unselect ( ) ;
417426 }
418- _ => { }
427+ self . content . set_cursor_pos ( Pos { x , y } ) ;
419428 }
420- } else if matches ! ( kind, MouseEventKind :: Down ( MouseButton :: Left ) ) {
421- self . focused = true ;
429+ MouseEventKind :: Up ( MouseButton :: Left ) if is_double_click => {
430+ self . content . set_cursor_pos ( Pos { x, y } ) ;
431+ self . content . select_word_around ( ) ;
432+ }
433+ MouseEventKind :: Drag ( MouseButton :: Left ) => {
434+ self . content . make_selection ( ) ;
435+ self . content . set_cursor_pos ( Pos { x, y } ) ;
436+ }
437+ MouseEventKind :: ScrollDown => {
438+ self . scroll_down ( ) ;
439+ }
440+ MouseEventKind :: ScrollUp => {
441+ self . scroll_up ( ) ;
442+ }
443+ _ => { }
422444 }
423- true
424- } else {
425- false
445+ } else if matches ! ( kind, MouseEventKind :: Down ( MouseButton :: Left ) ) {
446+ self . focused = true ;
426447 }
448+ true
427449 }
428450
429451 /// apply the event to change the state (content, cursor)
0 commit comments