Skip to content

Commit

Permalink
make the UI a lot snappier
Browse files Browse the repository at this point in the history
Before, we were recalculating after each char, even though the char's
knowledge was Unknown (so we didn't actually have anything new to
report)! Change that, so that we only recalculate when we need to. This
makes the UI go from very sluggish to snappy.
  • Loading branch information
yshavit committed Apr 10, 2023
1 parent 9c35a10 commit ce9d20b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/guess/guesses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::slice::Iter;

use crate::guess::known_word_constraints::CharKnowledge;

#[derive(Default)]
#[derive(Default, Clone, PartialEq, Eq)]
pub struct GuessChar {
knowledge: CharKnowledge,
ch: Option<char>,
Expand Down Expand Up @@ -37,6 +37,7 @@ impl GuessChar {
return false;
}
self.ch = Some(ch.to_ascii_uppercase());
self.knowledge = CharKnowledge::Unknown;
true
}

Expand Down
2 changes: 1 addition & 1 deletion src/guess/known_word_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cmp::{max, min};
use std::collections::{HashMap, HashSet};
use strum::{EnumCount, FromRepr};

#[derive(Copy, Clone, PartialEq, EnumCount, FromRepr)]
#[derive(Copy, Clone, PartialEq, Eq, EnumCount, FromRepr)]
pub enum CharKnowledge {
Unknown,
WrongPosition,
Expand Down
11 changes: 8 additions & 3 deletions src/ui/guesses_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ impl<'a, const N: usize, const R: usize> Widget for GuessesUI<N, R> {
Input::Character('\n') => self.handle_newline(),
Input::Character('\x7F') => self.unset_active_ch(), // delete
Input::Character(input_ch) if input_ch.is_ascii_alphabetic() => {
if self.set_active_ch(input_ch) {
self.has_new_knowledge.set(true);
} else {
if !(self.set_active_ch(input_ch)) {
return Some(input);
} else {
}
}
_ => {
Expand Down Expand Up @@ -186,8 +185,14 @@ impl<const N: usize, const R: usize> GuessesUI<N, R> {

fn set_active_ch(&mut self, ch: char) -> bool {
let guess_str = &mut self.grid.guess_mut(self.active_row);
let had_knowledge_before =
guess_str.guesses()[self.active_col].knowledge() != CharKnowledge::Unknown;
if guess_str.guess_mut(self.active_col).set_ch(ch) {
self.move_active_ch(true);
if had_knowledge_before {
// We don't have knowledge after set_ch, so if we used to, that's a change.
self.has_new_knowledge.set(true);
}
true
} else {
false
Expand Down

0 comments on commit ce9d20b

Please sign in to comment.