Skip to content

Commit e55f3a4

Browse files
committed
Layout rework
1 parent 6a9b2bb commit e55f3a4

File tree

5 files changed

+43
-39
lines changed

5 files changed

+43
-39
lines changed

src/config.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
mod exercise;
2-
mod keyboard;
3-
mod lesson;
42
mod index;
3+
mod lesson;
54

65
use index::Index;
76
use serde::{Deserialize, Serialize};
87
use std::{fs, path::PathBuf};
98
use thiserror::Error;
109

1110
use crate::{environment, Result};
12-
pub use keyboard::{Keyboard, PressedKeyCoord};
13-
pub use lesson::Exercise;
1411
pub use index::IndexRecord;
12+
pub use lesson::Exercise;
1513
use lesson::{Lesson, LessonPage};
1614

1715
#[derive(Deserialize, Serialize, Default)]
@@ -28,10 +26,9 @@ pub struct Configuration {
2826

2927
#[derive(Debug, Clone, Default)]
3028
pub struct Config {
31-
pub keyboard: Keyboard,
3229
pub lesson: Lesson,
3330
pub index: Index,
34-
current_keyboard: String,
31+
pub current_keyboard: String,
3532
current_lesson: String,
3633
current_page: usize,
3734
current_exercise: usize,
@@ -75,15 +72,9 @@ impl Config {
7572
}
7673
};
7774

78-
let keyboard = Keyboard::load(
79-
Self::data_dir()
80-
.join("keyboards")
81-
.join(format!("{}.yaml", current_keyboard)),
82-
)?;
8375
let lesson = Lesson::load(Self::data_dir().join(format!("{}.yaml", current_lesson)))?;
8476
let index = Index::load(Self::data_dir().join("index.yaml"))?;
8577
Ok(Config {
86-
keyboard,
8778
lesson,
8879
index,
8980
current_keyboard,
@@ -94,15 +85,18 @@ impl Config {
9485
}
9586

9687
pub async fn save(self) -> core::result::Result<(), Error> {
97-
let config_to_save = Configuration {
88+
let config_to_save = Configuration {
9889
current_keyboard: self.current_keyboard.clone(),
9990
current_lesson: self.current_lesson.clone(),
10091
current_page: self.current_page,
10192
current_exercise: self.current_exercise,
10293
};
103-
let config = serde_yaml::to_string(&config_to_save).map_err(|e| Error::Parse(e.to_string()))?;
94+
let config =
95+
serde_yaml::to_string(&config_to_save).map_err(|e| Error::Parse(e.to_string()))?;
10496
let path = Self::path();
105-
tokio::fs::write(path, &config).await.map_err(|e| Error::Write(e.to_string()))?;
97+
tokio::fs::write(path, &config)
98+
.await
99+
.map_err(|e| Error::Write(e.to_string()))?;
106100
Ok(())
107101
}
108102

src/config/lesson.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use serde::Deserialize;
22
use std::{fs, path::PathBuf};
33
use thiserror::Error;
44

5-
use super::keyboard::PressedKeyCoord;
5+
use crate::keyboard_config::PressedKeyCoord;
6+
67
pub use super::exercise::Exercise;
78

89
#[derive(Debug, Default, Clone, Deserialize)]
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use iced::{
66
Color, Element, Event, Length, Point, Rectangle, Renderer, Size, Theme,
77
};
88

9-
use crate::config::PressedKeyCoord;
9+
use crate::keyboard_config::{KeyboardConfig, PressedKeyCoord};
1010

1111
#[derive(Debug, Clone, PartialEq)]
1212
pub enum Message {
@@ -17,18 +17,18 @@ pub enum Message {
1717
}
1818

1919
#[derive(Default)]
20-
pub struct Keyboard {
20+
pub struct KeyboardComponent {
2121
draw_cache: Cache,
22-
config: crate::config::Keyboard,
22+
config: KeyboardConfig,
2323
pressed_keys: Vec<PressedKeyCoord>,
2424
show_keys: Vec<PressedKeyCoord>,
2525
key_to_show: usize,
2626
hide: bool,
2727
}
2828

29-
impl Keyboard {
30-
pub fn new(config: crate::config::Keyboard) -> Keyboard {
31-
Keyboard {
29+
impl KeyboardComponent {
30+
pub fn new(config: KeyboardConfig) -> KeyboardComponent {
31+
KeyboardComponent {
3232
config,
3333
..Default::default()
3434
}
@@ -94,7 +94,7 @@ impl Keyboard {
9494
self.pressed_keys.clear();
9595
self.key_to_show = 0;
9696
self.hide = false;
97-
},
97+
}
9898
}
9999
}
100100

@@ -106,7 +106,7 @@ impl Keyboard {
106106
}
107107
}
108108

109-
impl<Message> canvas::Program<Message> for Keyboard {
109+
impl<Message> canvas::Program<Message> for KeyboardComponent {
110110
type State = ();
111111

112112
fn draw(
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub struct Row {
103103
}
104104

105105
#[derive(Debug, Clone, Default, Deserialize)]
106-
pub struct Keyboard {
106+
pub struct KeyboardConfig {
107107
pub cols_for_keys: f32,
108108
pub space_between_keys: f32,
109109
pub keyboard_corner_curve: f32,
@@ -113,10 +113,10 @@ pub struct Keyboard {
113113
pub rows: Vec<Row>,
114114
}
115115

116-
impl Keyboard {
116+
impl KeyboardConfig {
117117
pub fn load(path: PathBuf) -> Result<Self, Error> {
118118
let content = fs::read_to_string(path).map_err(|e| Error::Read(e.to_string()))?;
119-
let keyboard: Keyboard =
119+
let keyboard: KeyboardConfig =
120120
serde_yaml::from_str(&content).map_err(|e| Error::Parse(e.to_string()))?;
121121
Ok(keyboard)
122122
}

src/main.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,31 @@ pub type Error = Box<dyn std::error::Error>;
33

44
use config::{Config, IndexRecord};
55
use exercise::Exercise;
6-
use keyboard::Keyboard;
6+
use keyboard_component::KeyboardComponent;
77

88
use handlebars::Handlebars;
99
use 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
};
1517
use serde_json::json;
1618

19+
use crate::keyboard_config::KeyboardConfig;
20+
1721
mod config;
1822
mod environment;
1923
mod exercise;
2024
mod font;
21-
mod keyboard;
25+
mod keyboard_component;
26+
mod keyboard_config;
2227

2328
pub const TICK_MILIS: u64 = 500;
2429

2530
fn 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

Comments
 (0)