Skip to content

Commit

Permalink
Use Iced advanced_text branch
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmoody committed Apr 6, 2023
1 parent f32644f commit 6596466
Show file tree
Hide file tree
Showing 25 changed files with 2,101 additions and 2,053 deletions.
2,282 changes: 1,239 additions & 1,043 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 4 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
dirs-next = "2.0"
itertools = "0.10"
palette = "0.5"
palette = "0.6"
rand = "0.8"
serde_json = "1.0"
statrs = "0.15"
Expand All @@ -25,20 +25,6 @@ version = "1.0"
features = ["fs"]

[dependencies.iced]
version = "0.3"
features = ["glow", "palette", "tokio"]

[dependencies.iced_native]
version = "0.4"

[dependencies.iced_graphics]
version = "0.2"

[dependencies.iced_futures]
version = "0.3"

[patch.crates-io]
iced = { git = "https://github.com/hecrj/iced.git", rev = "4aa943cbc63230dfcb995c469ceec9f74e6132e1"}
iced_native = { git = "https://github.com/hecrj/iced.git", rev = "4aa943cbc63230dfcb995c469ceec9f74e6132e1"}
iced_graphics = { git = "https://github.com/hecrj/iced.git", rev = "4aa943cbc63230dfcb995c469ceec9f74e6132e1"}
iced_futures = { git = "https://github.com/hecrj/iced.git", rev = "4aa943cbc63230dfcb995c469ceec9f74e6132e1"}
features = ["palette", "tokio", "wgpu", "tiny-skia"]
git = "https://github.com/hecrj/iced.git"
rev = "0b459c8e240abf83bb62902a504c018194acdbb6"
4 changes: 2 additions & 2 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ pub struct Saved {
}

impl Saved {
pub fn new(profiles: profile::List, theme: &Theme) -> Self {
pub fn new(profiles: profile::List, theme_name: &str) -> Self {
Self {
version: VERSION,
profiles: profiles.into(),
theme_name: theme.name.clone(),
theme_name: theme_name.to_string(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/data/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Dictionary {
words,
dist,
rng: rand::thread_rng(),
unit_dist: self.unit_dist.clone(),
unit_dist: self.unit_dist,
}
}

Expand All @@ -64,7 +64,7 @@ impl Dictionary {
words,
dist,
rng: rand::thread_rng(),
unit_dist: self.unit_dist.clone(),
unit_dist: self.unit_dist,
}
})
}
Expand All @@ -84,7 +84,7 @@ impl Dictionary {
words,
dist,
rng: rand::thread_rng(),
unit_dist: self.unit_dist.clone(),
unit_dist: self.unit_dist,
}
})
}
Expand Down
11 changes: 3 additions & 8 deletions src/data/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
use super::CharSet;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Deserialize, Serialize)]
pub enum Layout {
Colemak,
ColemakDH,
ColemakDHOrtholinear,
ColemakOrtholinear,
Dvorak,
DvorakOrtholinear,
#[default]
Qwerty,
QwertyOrtholinear,
Workman,
WorkmanOrtholinear,
}

impl Default for Layout {
fn default() -> Self {
Layout::Qwerty
}
}

impl std::fmt::Display for Layout {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Expand Down Expand Up @@ -62,7 +57,7 @@ impl Layout {
pub fn next_char(&self, char_set: &CharSet) -> Option<char> {
self.letter_order()
.iter()
.filter(|&letter| !char_set.contains(&letter))
.filter(|&letter| !char_set.contains(letter))
.cloned()
.next()
}
Expand Down
6 changes: 5 additions & 1 deletion src/data/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ impl List {
self.zipper.len()
}

pub fn is_empty(&self) -> bool {
self.zipper.is_empty()
}

fn iter(&self) -> impl Iterator<Item = Item<&Profile, &Active>> {
self.zipper.iter()
}
Expand Down Expand Up @@ -158,7 +162,7 @@ impl From<Active> for Profile {
layout: active.layout,
difficulty: active.difficulty,
state: active.state,
words: active.session.words_setting().clone(),
words: active.session.words_setting(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/data/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn cumulative(mapping: HashMap<String, f32>) -> Vec<(u16, char)> {
.iter()
.map(|(letter, ratio)| ((s32 * ratio).round() as u16, letter.chars().next().unwrap()))
.collect();
cumulative.sort_by(|(a, _), (b, _)| b.cmp(&a));
cumulative.sort_by(|(a, _), (b, _)| b.cmp(a));
let mut c = 0;
cumulative
.into_iter()
Expand Down
25 changes: 17 additions & 8 deletions src/data/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use palette::{Mix, Srgb};

#[derive(Debug, Clone)]
pub struct Theme {
pub name: String,
pub name: &'static str,
pub bg: Color,
pub text: Color,
pub target: Color,
Expand All @@ -14,7 +14,12 @@ pub struct Theme {

impl Theme {
pub fn all() -> Vec<Self> {
vec![Self::ayu(), Self::monokai(), Self::tokyo_night(), Self::one_dark()]
vec![
Self::ayu(),
Self::monokai(),
Self::tokyo_night(),
Self::one_dark(),
]
}

pub fn from_name(name: &str) -> Option<Self> {
Expand All @@ -23,7 +28,7 @@ impl Theme {

pub fn monokai() -> Self {
Self {
name: "Monokai".to_string(),
name: "Monokai",
bg: Color::from_rgba8(0x27, 0x28, 0x22, 1.0),
text: Color::from_rgba8(0xf8, 0xf8, 0xf2, 1.0),
target: Color::from_rgba8(0xa6, 0xe2, 0x2e, 1.0),
Expand All @@ -35,7 +40,7 @@ impl Theme {

pub fn ayu() -> Self {
Self {
name: "Ayu".to_string(),
name: "Ayu",
bg: Color::from_rgba8(0x0A, 0x0E, 0x14, 1.0),
text: Color::from_rgba8(0xB3, 0xB1, 0xAD, 1.0),
target: Color::from_rgba8(0xc2, 0xd9, 0x4c, 1.0),
Expand All @@ -47,7 +52,7 @@ impl Theme {

pub fn tokyo_night() -> Self {
Self {
name: "Tokyo Night".to_string(),
name: "Tokyo Night",
bg: Color::from_rgba8(0x1A, 0x1B, 0x26, 1.0),
text: Color::from_rgba8(0xC0, 0xCA, 0xF5, 1.0),
target: Color::from_rgba8(0x9E, 0xCE, 0x6A, 1.0),
Expand All @@ -59,7 +64,7 @@ impl Theme {

pub fn one_dark() -> Self {
Self {
name: "One Dark".to_string(),
name: "One Dark",
bg: Color::from_rgba8(0x28, 0x2C, 0x34, 1.0),
text: Color::from_rgba8(0xAB, 0xB2, 0xBF, 1.0),
target: Color::from_rgba8(0x98, 0xC3, 0x79, 1.0),
Expand All @@ -77,15 +82,19 @@ impl Theme {
if value < 0.5 {
let pct = value / 0.5;
let error = Srgb::from(self.error).into_linear();
return Srgb::from_linear(error.mix(&text, pct)).into();
Srgb::from_linear(error.mix(&text, pct)).into()
} else {
let pct = (value - 0.5) / 0.5;
let target = Srgb::from(self.target).into_linear();
return Srgb::from_linear(text.mix(&target, pct)).into();
Srgb::from_linear(text.mix(&target, pct)).into()
}
}
}

pub fn alpha(color: Color, alpha: f32) -> Color {
Color { a: alpha, ..color }
}

impl Default for Theme {
fn default() -> Self {
Theme::monokai()
Expand Down
27 changes: 7 additions & 20 deletions src/data/training.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub const CLEAN_ALPHA_COEFF: f32 = 1.0 / (1.0 + 10.0);
pub const MIN_CLEAN_PCT: f32 = 0.75;
const CHARACTERS_PER_WORD: f64 = 5.0;

#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct State {
/// Which characters are in our set
char_set: CharSet,
Expand All @@ -30,15 +30,16 @@ pub struct State {
#[derive(Debug, Clone, Copy, Default, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct WordsPerMinute(f64);

#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Deserialize, Serialize)]
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, PartialOrd, Deserialize, Serialize)]
pub enum Difficulty {
Easy,
Casual,
#[default]
Normal,
Strict,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct Stats {
raw: Vec<WordsPerMinute>,
pub wpm_mean: WordsPerMinute,
Expand Down Expand Up @@ -86,6 +87,7 @@ pub struct Session {
#[derive(Debug, Clone)]
pub struct Line {
hits: Vec<Hit>,
#[allow(dead_code)]
time: OffsetDateTime,
}

Expand All @@ -95,6 +97,7 @@ pub struct Hit {
/// The character to type
target: char,
/// The previous character typed
#[allow(dead_code)]
prev: char,
/// Incorrect keys hit instead of the target
misses: CharSet,
Expand Down Expand Up @@ -255,12 +258,6 @@ impl Difficulty {
}
}

impl Default for Difficulty {
fn default() -> Self {
Self::Normal
}
}

impl std::fmt::Display for Difficulty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Expand Down Expand Up @@ -290,16 +287,6 @@ impl Stats {
}
}

impl Default for Stats {
fn default() -> Self {
Self {
raw: Vec::new(),
wpm_mean: WordsPerMinute::default(),
wpm_harmonic_mean: WordsPerMinute::default(),
}
}
}

impl Event {
fn unlock(letter: char) -> Self {
Self::Unlock {
Expand Down Expand Up @@ -439,7 +426,7 @@ pub struct TriplePoint {

impl TriplePoint {
pub fn new(lower: f32, mid: f32, upper: f32) -> Option<Self> {
(lower < mid && mid < upper).then(|| Self { lower, mid, upper })
(lower < mid && mid < upper).then_some(Self { lower, mid, upper })
}

/// Map values on two linear scales between [lower, mid] and [mid, upper]
Expand Down
11 changes: 8 additions & 3 deletions src/data/zipper_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ impl<T, U> ZipperList<T, U> {
self.prev.len() + 1 + self.next.len()
}

pub fn is_empty(&self) -> bool {
// Zipper List cannot be empty, by definition
false
}

pub fn iter(&self) -> impl Iterator<Item = Item<&T, &U>> {
self.prev
.iter()
.map(|item| Item::Other(item))
.map(Item::Other)
.chain(std::iter::once(Item::Current(&self.current)))
.chain(self.next.iter().map(|item| Item::Other(item)))
.chain(self.next.iter().map(Item::Other))
}

pub fn push(&mut self, item: T) {
Expand All @@ -51,7 +56,7 @@ where
next.append(&mut self.next);
self.next = next;
true
} else if index >= self.prev.len() + 1 && index < self.len() {
} else if index > self.prev.len() && index < self.len() {
let index = index - self.prev.len() - 1;
let next_current = self.next.remove(index);
let old_current = std::mem::replace(&mut self.current, next_current.into());
Expand Down
51 changes: 37 additions & 14 deletions src/font.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
use iced::Font;

/// Iosevka Fixed Extended Thin - Weight 100
pub const THIN: Font = Font::External {
name: "Iosevka Extended Thin",
bytes: include_bytes!("../fonts/iosevka-fixed-extendedthin.ttf"),
};
// name: "Iosevka Extended Thin",
pub const THIN: &[u8] = include_bytes!("../fonts/iosevka-fixed-extendedthin.ttf");

/// Iosevka Fixed Extended Light - Weight 300
pub const LIGHT: Font = Font::External {
name: "Iosevka Extended Light",
bytes: include_bytes!("../fonts/iosevka-fixed-extendedlight.ttf"),
};
// name: "Iosevka Extended Light",
pub const LIGHT: &[u8] = include_bytes!("../fonts/iosevka-fixed-extendedlight.ttf");

/// Iosevka Fixed Extended Medium - Weight 500
pub const MEDIUM: Font = Font::External {
name: "Iosevka Extended Medium",
bytes: include_bytes!("../fonts/iosevka-fixed-extendedmedium.ttf"),
};
// name: "Iosevka Extended Medium",
pub const MEDIUM: &[u8] = include_bytes!("../fonts/iosevka-fixed-extendedmedium.ttf");

pub enum Font {
Thin,
Light,
Medium,
}

impl Font {
fn name(&self) -> &'static str {
"Iosevka Fixed"
}

fn weight(&self) -> iced::font::Weight {
match self {
Font::Thin => iced::font::Weight::Thin,
Font::Light => iced::font::Weight::Light,
Font::Medium => iced::font::Weight::Medium,
}
}
}

impl From<Font> for iced::Font {
fn from(font: Font) -> Self {
iced::Font {
family: iced::font::Family::Name(font.name()),
weight: font.weight(),
stretch: iced::font::Stretch::Expanded,
monospaced: true,
}
}
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod data;
pub mod font;
pub mod screen;
pub mod style;

pub type Element<'a, Message> = iced::Element<'a, Message, iced::Renderer<crate::style::Theme>>;
Loading

0 comments on commit 6596466

Please sign in to comment.