ltk/input/keyboard/
text_keys.rsuse crate::app::App;
use crate::event_loop::{ AppData, SurfaceFocus };
use crate::tree::{ find_handlers, find_widget };
use crate::Keysym;
use crate::platform::Platform;
use super::super::event::KeyInput;
impl<A: App, P: Platform> AppData<A, P>
{
pub( crate ) fn focused_text_input( &self, focus: SurfaceFocus ) -> bool
{
let ss = self.surface( focus );
ss.focused_idx
.and_then( |idx| find_handlers( &ss.frame.widget_rects, idx ) )
.is_some_and( |h| h.is_text_input() )
}
pub( super ) fn handle_key_backspace( &mut self, focus: SurfaceFocus, event: &KeyInput )
{
if self.focused_text_input( focus ) { self.handle_backspace( focus ); }
else if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed )
{
self.pending_msgs.push( msg );
}
}
pub( super ) fn handle_key_delete( &mut self, focus: SurfaceFocus, event: &KeyInput )
{
if self.focused_text_input( focus ) { self.handle_delete_forward( focus ); }
else if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed )
{
self.pending_msgs.push( msg );
}
}
pub( super ) fn handle_key_return( &mut self, focus: SurfaceFocus, event: &KeyInput )
{
let focused = self.surface( focus ).focused_idx;
let is_multi = focused.and_then( |idx|
find_handlers( &self.surface( focus ).frame.widget_rects, idx )
.map( |h| h.is_multiline_text_input() ) ).unwrap_or( false );
if is_multi
{
self.handle_text_insert( focus, "\n" );
} else {
let ss = self.surface( focus );
let hovered = ss.hovered_idx.filter( |&idx| find_widget( &ss.frame.widget_rects, idx ).is_some_and( |w| w.focus_on_press ) );
let target = focused.or( hovered );
let mut handled = false;
if let Some( idx ) = target
{
let msg = find_handlers( &self.surface( focus ).frame.widget_rects, idx )
.and_then( |h| h.submit_msg().or_else( || h.press_msg() ) );
if let Some( m ) = msg
{
self.pending_msgs.push( m );
handled = true;
}
}
if !handled
{
if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed )
{
self.pending_msgs.push( msg );
}
}
}
}
pub( super ) fn handle_key_arrow_vertical( &mut self, focus: SurfaceFocus, event: &KeyInput )
{
let is_text = self.focused_text_input( focus );
let reverse = event.keysym == Keysym::Up;
let extend = self.shift_pressed;
let intercepted = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed );
if let Some( msg ) = intercepted
{
self.pending_msgs.push( msg );
}
else
{
let consumed = if is_text
{
if reverse { self.handle_cursor_up( focus, extend ) }
else { self.handle_cursor_down( focus, extend ) }
} else { false };
if !consumed && !self.move_keyboard_hover( focus, reverse )
{
}
}
}
pub( super ) fn handle_key_arrow_horizontal( &mut self, focus: SurfaceFocus, event: &KeyInput )
{
let is_text = self.focused_text_input( focus );
let extend = self.shift_pressed;
let intercepted = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed );
if let Some( msg ) = intercepted
{
self.pending_msgs.push( msg );
}
else if is_text
{
if event.keysym == Keysym::Left
{
self.handle_cursor_left( focus, extend );
} else {
self.handle_cursor_right( focus, extend );
}
}
}
pub( super ) fn handle_key_home( &mut self, focus: SurfaceFocus )
{
if self.focused_text_input( focus ) { self.handle_cursor_home( focus, self.shift_pressed ); }
}
pub( super ) fn handle_key_end( &mut self, focus: SurfaceFocus )
{
if self.focused_text_input( focus ) { self.handle_cursor_end( focus, self.shift_pressed ); }
}
pub( super ) fn handle_key_page( &mut self, focus: SurfaceFocus, event: &KeyInput )
{
let up = event.keysym == Keysym::Page_Up;
if !self.ctrl_pressed && self.focused_text_input( focus ) && self.handle_cursor_page( focus, up, self.shift_pressed ) { return; }
if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed )
{
self.pending_msgs.push( msg );
}
}
pub( super ) fn handle_key_space( &mut self, focus: SurfaceFocus, event: &KeyInput )
{
let ss = self.surface( focus );
let press = ss.focused_idx
.and_then( |idx| find_handlers( &ss.frame.widget_rects, idx ) )
.and_then( |h| h.press_msg() );
if let Some( msg ) = press
{
self.pending_msgs.push( msg );
} else if self.focused_text_input( focus ) {
self.handle_text_insert( focus, " " );
} else if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, self.ctrl_pressed, self.shift_pressed )
{
self.pending_msgs.push( msg );
}
}
pub( super ) fn handle_key_default( &mut self, focus: SurfaceFocus, event: &KeyInput )
{
if self.ctrl_pressed
{
if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, true, self.shift_pressed )
{
self.pending_msgs.push( msg );
}
} else if self.focused_text_input( focus )
{
let typed = event.text.clone().filter( |txt| !txt.is_empty() && txt.chars().all( |c| !c.is_control() ) );
match typed
{
Some( txt ) => self.handle_text_insert( focus, &txt ),
None =>
{
if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, false, self.shift_pressed )
{
self.pending_msgs.push( msg );
}
}
}
} else if let Some( msg ) = self.app.on_key_with_modifiers( event.keysym, false, self.shift_pressed )
{
self.pending_msgs.push( msg );
}
}
}