55
66//! Text editor component
77
8- use super :: highlight:: { self , Highlighter } ;
8+ use super :: highlight:: { self , Highlighter , SchemeColors } ;
99use super :: * ;
1010use kas:: event:: components:: { TextInput , TextInputAction } ;
1111use kas:: event:: { ElementState , FocusSource , Ime , ImePurpose , ImeSurroundingText , Scroll } ;
1212use kas:: geom:: Vec2 ;
1313use kas:: prelude:: * ;
1414use kas:: text:: format:: FormattableText ;
1515use kas:: text:: { CursorRange , NotReady , SelectionHelper , format} ;
16- use kas:: theme:: { Text , TextClass } ;
16+ use kas:: theme:: { Background , Text , TextClass } ;
1717use kas:: util:: UndoStack ;
1818use std:: borrow:: Cow ;
1919use unicode_segmentation:: { GraphemeCursor , UnicodeSegmentation } ;
@@ -31,6 +31,7 @@ pub struct EditorComponent<H: Highlighter> {
3131 id : Id ,
3232 editable : bool ,
3333 text : Text < highlight:: Text < H > > ,
34+ colors : SchemeColors ,
3435 selection : SelectionHelper ,
3536 edit_x_coord : Option < f32 > ,
3637 last_edit : Option < EditOp > ,
@@ -94,6 +95,7 @@ impl<H: Default + Highlighter> Default for Component<H> {
9495 id : Id :: default ( ) ,
9596 editable : true ,
9697 text : Text :: new ( Default :: default ( ) , TextClass :: Editor , false ) ,
98+ colors : SchemeColors :: default ( ) ,
9799 selection : Default :: default ( ) ,
98100 edit_x_coord : None ,
99101 last_edit : Some ( EditOp :: Initial ) ,
@@ -134,6 +136,7 @@ impl<H: Highlighter> Component<H> {
134136 id : self . 0 . id ,
135137 editable : self . 0 . editable ,
136138 text : Text :: new ( text, class, wrap) ,
139+ colors : self . 0 . colors ,
137140 selection : self . 0 . selection ,
138141 edit_x_coord : self . 0 . edit_x_coord ,
139142 last_edit : self . 0 . last_edit ,
@@ -151,6 +154,17 @@ impl<H: Highlighter> Component<H> {
151154 self . text . text_mut ( ) . set_highlighter ( highlighter) ;
152155 }
153156
157+ /// Get the background color
158+ pub fn background_color ( & self ) -> Background {
159+ if self . error_state {
160+ Background :: Error
161+ } else if let Some ( c) = self . colors . background . as_rgba ( ) {
162+ Background :: Rgb ( c. as_rgb ( ) )
163+ } else {
164+ Background :: Default
165+ }
166+ }
167+
154168 /// Access text
155169 #[ inline]
156170 pub fn text ( & self ) -> & Text < impl FormattableText > {
@@ -183,6 +197,8 @@ impl<H: Highlighter> Component<H> {
183197 #[ inline]
184198 pub fn configure ( & mut self , cx : & mut ConfigCx , id : Id ) {
185199 self . id = id;
200+ self . text . text_mut ( ) . configure ( cx) ;
201+ self . 0 . colors = self . text . text ( ) . scheme_colors ( ) ;
186202 self . text . configure ( & mut cx. size_cx ( ) ) ;
187203 }
188204
@@ -205,17 +221,33 @@ impl<H: Highlighter> Component<H> {
205221 let range: Range < u32 > = self . selection . range ( ) . cast ( ) ;
206222
207223 let color_tokens = self . text . color_tokens ( ) ;
208- let mut buf = [ ( 0 , format:: Colors :: default ( ) ) ; 3 ] ;
224+ let default_colors = format:: Colors {
225+ foreground : self . colors . foreground ,
226+ background : None ,
227+ } ;
228+ let mut buf = [ ( 0 , default_colors) ; 3 ] ;
209229 let mut vec = vec ! [ ] ;
210230 let tokens = if range. is_empty ( ) {
211- color_tokens
231+ if color_tokens. is_empty ( ) {
232+ & buf[ ..1 ]
233+ } else {
234+ color_tokens
235+ }
212236 } else if color_tokens. is_empty ( ) {
213237 buf[ 1 ] . 0 = range. start ;
214- buf[ 1 ] . 1 . background = Some ( format:: Color :: default ( ) ) ;
238+ buf[ 1 ] . 1 . foreground = self . colors . selection_foreground ;
239+ buf[ 1 ] . 1 . background = Some ( self . colors . selection_background ) ;
215240 buf[ 2 ] . 0 = range. end ;
216241 let r0 = if range. start > 0 { 0 } else { 1 } ;
217242 & buf[ r0..]
218243 } else {
244+ let set_selection_colors = |colors : & mut format:: Colors | {
245+ if colors. foreground == self . colors . foreground {
246+ colors. foreground = self . colors . selection_foreground ;
247+ }
248+ colors. background = Some ( self . colors . selection_background ) ;
249+ } ;
250+
219251 vec. reserve ( color_tokens. len ( ) + 2 ) ;
220252 let mut i = 0 ;
221253 let mut change_index = range. start ;
@@ -224,12 +256,12 @@ impl<H: Highlighter> Component<H> {
224256 let ( start, mut colors) = color_tokens[ i] ;
225257 if start < change_index {
226258 if in_selection {
227- colors . background = Some ( format :: Color :: default ( ) ) ;
259+ set_selection_colors ( & mut colors ) ;
228260 }
229261 } else if start == change_index {
230262 in_selection = change_index == range. start ;
231263 if in_selection {
232- colors . background = Some ( format :: Color :: default ( ) ) ;
264+ set_selection_colors ( & mut colors ) ;
233265 change_index = range. end ;
234266 } else {
235267 change_index = u32:: MAX ;
@@ -244,7 +276,7 @@ impl<H: Highlighter> Component<H> {
244276 in_selection = change_index == range. start ;
245277 if in_selection {
246278 change_index = range. end ;
247- colors . background = Some ( Default :: default ( ) ) ;
279+ set_selection_colors ( & mut colors ) ;
248280 } else {
249281 change_index = u32:: MAX ;
250282 } ;
@@ -254,15 +286,19 @@ impl<H: Highlighter> Component<H> {
254286 vec. push ( ( start, colors) ) ;
255287 i += 1 ;
256288 }
289+ let last_colors = if i > 0 {
290+ color_tokens[ i - 1 ] . 1
291+ } else {
292+ Default :: default ( )
293+ } ;
257294 if change_index == range. start {
258- vec. push ( ( range. start , format:: Colors {
259- color : Default :: default ( ) ,
260- background : Some ( Default :: default ( ) ) ,
261- } ) ) ;
295+ let mut colors = last_colors;
296+ set_selection_colors ( & mut colors) ;
297+ vec. push ( ( range. start , colors) ) ;
262298 change_index = range. end ;
263299 }
264300 if change_index == range. end {
265- vec. push ( ( range. end , format :: Colors :: default ( ) ) ) ;
301+ vec. push ( ( range. end , last_colors ) ) ;
266302 }
267303 & vec
268304 } ;
@@ -282,7 +318,13 @@ impl<H: Highlighter> Component<H> {
282318 }
283319
284320 if self . editable && draw. ev_state ( ) . has_input_focus ( self . id_ref ( ) ) == Some ( true ) {
285- draw. text_cursor ( pos, rect, & self . text , self . selection . edit_index ( ) ) ;
321+ draw. text_cursor (
322+ pos,
323+ rect,
324+ & self . text ,
325+ self . selection . edit_index ( ) ,
326+ Some ( self . colors . cursor ) ,
327+ ) ;
286328 }
287329 }
288330
0 commit comments