@ -108,12 +108,6 @@ impl CCursorPair {
}
}
}
}
#[ derive(Clone, Copy, Debug, Default) ]
#[ cfg_attr(feature = " persistence " , derive(serde::Deserialize, serde::Serialize)) ]
pub struct CodingConfig {
pub tab_moves_focus : bool ,
}
/// A text region that the user can edit the contents of.
/// A text region that the user can edit the contents of.
///
///
/// See also [`Ui::text_edit_singleline`] and [`Ui::text_edit_multiline`].
/// See also [`Ui::text_edit_singleline`] and [`Ui::text_edit_multiline`].
@ -146,7 +140,7 @@ pub struct TextEdit<'t> {
enabled : bool ,
enabled : bool ,
desired_width : Option < f32 > ,
desired_width : Option < f32 > ,
desired_height_rows : usize ,
desired_height_rows : usize ,
tab_moves _focus : bool ,
lock _focus : bool ,
}
}
impl < 't > TextEdit < 't > {
impl < 't > TextEdit < 't > {
pub fn cursor ( ui : & Ui , id : Id ) -> Option < CursorPair > {
pub fn cursor ( ui : & Ui , id : Id ) -> Option < CursorPair > {
@ -178,7 +172,7 @@ impl<'t> TextEdit<'t> {
enabled : true ,
enabled : true ,
desired_width : None ,
desired_width : None ,
desired_height_rows : 1 ,
desired_height_rows : 1 ,
tab_moves _focus : true ,
lock _focus : true ,
}
}
}
}
@ -197,37 +191,19 @@ impl<'t> TextEdit<'t> {
enabled : true ,
enabled : true ,
desired_width : None ,
desired_width : None ,
desired_height_rows : 4 ,
desired_height_rows : 4 ,
tab_moves _focus : true ,
lock _focus : true ,
}
}
}
}
/// When this is true, then pass focus to the next
/// widget.
pub fn tab_moves_focus ( mut self , b : bool ) -> Self {
self . tab_moves_focus = b ;
self
}
/// Build a `TextEdit` focused on code editing.
/// Build a `TextEdit` focused on code editing.
/// By default it comes with:
/// By default it comes with:
/// - monospaced font
/// - monospaced font
/// - focus lock
/// - focus lock
pub fn code_editor ( self ) -> Self {
pub fn code_editor ( self ) -> Self {
self . text_style ( TextStyle ::Monospace ) . tab_moves_focus ( false )
self . text_style ( TextStyle ::Monospace ) . lock_focus ( true )
}
/// Build a `TextEdit` focused on code editing with configurable `Tab` management.
///
/// Shortcut for:
/// ```rust, ignore
/// egui::TextEdit::multiline(code_snippet)
/// .code_editor()
/// .tab_moves_focus(tab_moves_focus);
/// ```
pub fn code_editor_with_config ( self , config : CodingConfig ) -> Self {
self . code_editor ( ) . tab_moves_focus ( config . tab_moves_focus )
}
}
/// Use if you want to set an explicit `Id` for this widget.
pub fn id ( mut self , id : Id ) -> Self {
pub fn id ( mut self , id : Id ) -> Self {
self . id = Some ( id ) ;
self . id = Some ( id ) ;
self
self
@ -292,6 +268,16 @@ impl<'t> TextEdit<'t> {
self . desired_height_rows = desired_height_rows ;
self . desired_height_rows = desired_height_rows ;
self
self
}
}
/// When `false` (default), pressing TAB will move focus
/// to the next widget.
///
/// When `true`, the widget will keep the focus and pressing TAB
/// will insert the `'\t'` character.
pub fn lock_focus ( mut self , b : bool ) -> Self {
self . lock_focus = b ;
self
}
}
}
impl < 't > Widget for TextEdit < 't > {
impl < 't > Widget for TextEdit < 't > {
@ -348,7 +334,7 @@ impl<'t> TextEdit<'t> {
enabled ,
enabled ,
desired_width ,
desired_width ,
desired_height_rows ,
desired_height_rows ,
tab_moves _focus,
lock _focus,
} = self ;
} = self ;
let text_style = text_style . unwrap_or_else ( | | ui . style ( ) . body_text_style ) ;
let text_style = text_style . unwrap_or_else ( | | ui . style ( ) . body_text_style ) ;
@ -454,7 +440,7 @@ impl<'t> TextEdit<'t> {
let mut text_cursor = None ;
let mut text_cursor = None ;
if ui . memory ( ) . has_focus ( id ) & & enabled {
if ui . memory ( ) . has_focus ( id ) & & enabled {
ui . memory ( ) . lock_focus ( id , ! tab_moves _focus) ;
ui . memory ( ) . lock_focus ( id , lock _focus) ;
let mut cursorp = state
let mut cursorp = state
. cursorp
. cursorp