diff --git a/core/embed/rust/src/ui/model_tr/component/input_methods/wordlist.rs b/core/embed/rust/src/ui/model_tr/component/input_methods/wordlist.rs index 6b4f8e9270..8dd2f63f5b 100644 --- a/core/embed/rust/src/ui/model_tr/component/input_methods/wordlist.rs +++ b/core/embed/rust/src/ui/model_tr/component/input_methods/wordlist.rs @@ -157,19 +157,49 @@ where ChoiceFactoryWordlist::new(self.wordlist_type, self.textbox.content()) } + fn get_last_textbox_letter(&self) -> Option { + self.textbox.content().chars().last() + } + + fn get_new_page_counter(&self, new_choices: &ChoiceFactoryWordlist) -> usize { + // Starting at the random position in case of letters and at the beginning in + // case of words. + if self.offer_words { + INITIAL_PAGE_COUNTER + } else { + let choices_count = >::count(new_choices); + // There should be always DELETE and at least one letter + assert!(choices_count > 1); + if choices_count == 2 { + // In case there is only DELETE and one letter, starting on that letter + // (regardless of the last letter in the textbox) + return INITIAL_PAGE_COUNTER; + } + // We do not want to end up at the same letter as the last one in the textbox + loop { + let random_position = get_random_position(choices_count); + let current_action = + >::get(new_choices, random_position) + .1; + if let WordlistAction::Letter(current_letter) = current_action { + if let Some(last_letter) = self.get_last_textbox_letter() { + if current_letter == last_letter { + // Randomly trying again when the last and current letter match + continue; + } + } + } + break random_position; + } + } + } + /// Updates the whole page. fn update(&mut self, ctx: &mut EventCtx) { self.update_chosen_letters(ctx); let new_choices = self.get_current_choices(); self.offer_words = new_choices.offer_words; - // Starting at the random position in case of letters and at the beginning in - // case of words - let new_page_counter = if self.offer_words { - INITIAL_PAGE_COUNTER - } else { - let choices_count = >::count(&new_choices); - get_random_position(choices_count) - }; + let new_page_counter = self.get_new_page_counter(&new_choices); // Not using carousel in case of words, as that looks weird in case // there is only one word to choose from. self.choice_page