@@ -3,27 +3,31 @@ pub type Error = Box<dyn std::error::Error>;
33
44use config:: { Config , IndexRecord } ;
55use exercise:: Exercise ;
6- use keyboard :: Keyboard ;
6+ use keyboard_component :: KeyboardComponent ;
77
88use handlebars:: Handlebars ;
99use iced:: {
1010 event,
1111 keyboard:: { key, Modifiers } ,
12- widget:: { self , button, canvas:: path:: lyon_path:: geom:: euclid:: num:: Round , column, container, text} ,
12+ widget:: {
13+ self , button, canvas:: path:: lyon_path:: geom:: euclid:: num:: Round , column, container, text,
14+ } ,
1315 window, Element , Event , Length , Subscription , Task ,
1416} ;
1517use serde_json:: json;
1618
19+ use crate :: keyboard_config:: KeyboardConfig ;
20+
1721mod config;
1822mod environment;
1923mod exercise;
2024mod font;
21- mod keyboard;
25+ mod keyboard_component;
26+ mod keyboard_config;
2227
2328pub const TICK_MILIS : u64 = 500 ;
2429
2530fn main ( ) -> iced:: Result {
26-
2731 font:: set ( ) ;
2832
2933 iced:: application ( "Raiti - Touch typing tutor" , Raiti :: update, Raiti :: view)
@@ -43,7 +47,7 @@ struct Raiti {
4347 exercise : Vec < Exercise > ,
4448 was_errors : u64 ,
4549 was_wpm : f64 ,
46- keyboard : Keyboard ,
50+ keyboard : KeyboardComponent ,
4751 show_confirm : bool ,
4852}
4953
@@ -52,7 +56,7 @@ pub enum Message {
5256 Event ( Event ) ,
5357 Tick ,
5458 Exercise ( exercise:: Message ) ,
55- Keyboard ( keyboard :: Message ) ,
59+ Keyboard ( keyboard_component :: Message ) ,
5660 LessonSelected ( IndexRecord ) ,
5761 Confirm ,
5862 WindowSettingsSaved ( core:: result:: Result < ( ) , config:: Error > ) ,
@@ -62,13 +66,18 @@ impl Raiti {
6266 fn new ( ) -> ( Self , Task < Message > ) {
6367 // Read config & initialize state
6468 let config = Config :: load ( ) . expect ( "Error loading context" ) ;
69+ let keyboard_config = KeyboardConfig :: load (
70+ Config :: data_dir ( )
71+ . join ( "keyboards" )
72+ . join ( format ! ( "{}.yaml" , & config. current_keyboard) ) ,
73+ ) . expect ( "Error loading keyboard config" ) ;
6574
6675
6776 (
6877 Self {
6978 config : config. clone ( ) ,
7079 exercise : vec ! [ ] ,
71- keyboard : Keyboard :: new ( config . keyboard . clone ( ) ) ,
80+ keyboard : KeyboardComponent :: new ( keyboard_config ) ,
7281 ..Default :: default ( )
7382 } ,
7483 widget:: focus_next ( ) ,
@@ -89,7 +98,7 @@ impl Raiti {
8998 exercise. update ( exercise:: Message :: Event ( event. clone ( ) ) ) ;
9099 }
91100 self . keyboard
92- . update ( keyboard :: Message :: Event ( event. clone ( ) ) ) ;
101+ . update ( keyboard_component :: Message :: Event ( event. clone ( ) ) ) ;
93102 if let Event :: Keyboard ( iced:: keyboard:: Event :: KeyPressed {
94103 key,
95104 location,
@@ -137,7 +146,7 @@ impl Raiti {
137146 exercise. update ( exercise:: Message :: Tick ) ;
138147 }
139148
140- self . keyboard . update ( keyboard :: Message :: Tick ) ;
149+ self . keyboard . update ( keyboard_component :: Message :: Tick ) ;
141150 Task :: none ( )
142151 }
143152 Message :: Keyboard ( message) => {
@@ -232,12 +241,12 @@ impl Raiti {
232241 self . calculate_stats ( ) ;
233242
234243 self . exercise . clear ( ) ;
235- self . keyboard . update ( keyboard :: Message :: ClearKeys ) ;
244+ self . keyboard . update ( keyboard_component :: Message :: ClearKeys ) ;
236245 self . config . next_page ( ) ;
237246 if let Some ( page) = self . config . get_page ( ) {
238247 if !page. show_keys . is_empty ( ) {
239248 self . keyboard
240- . update ( keyboard :: Message :: SetShowKeys ( page. show_keys . clone ( ) ) )
249+ . update ( keyboard_component :: Message :: SetShowKeys ( page. show_keys . clone ( ) ) )
241250 }
242251 }
243252 if let Some ( ex) = self . config . get_exercise ( ) {
@@ -269,7 +278,7 @@ impl Raiti {
269278 length += ex. exercise . chars ( ) . map ( |_| 1 ) . sum :: < u64 > ( ) ;
270279 }
271280 self . was_errors = errors. round ( ) ;
272- let was_wpm = ( ( length as f64 - errors as f64 ) / ( mseconds as f64 / 60000.0 ) ) / 5.0 ;
281+ let was_wpm = ( ( length as f64 - errors as f64 ) / ( mseconds as f64 / 60000.0 ) ) / 5.0 ;
273282 self . was_wpm = ( was_wpm * 100.0 ) . round ( ) / 100.0 ;
274283 }
275284}
0 commit comments