Files
jap_learn/src/pages/randomizer.rs
T

82 lines
2.2 KiB
Rust
Raw Normal View History

2026-08-24 01:22:00 +03:00
use crate::navigation::RootMessage;
2026-08-01 19:43:07 +03:00
use crate::navigation::{NavigatedPage, Page};
2026-08-24 01:22:00 +03:00
use crate::pages::randomizer::RandomizerMessage::{Back, Edit, Start};
2026-09-09 14:26:55 +03:00
use crate::styling::{HALF_SPACING, back_overlay};
use crate::jl_button;
2026-08-01 19:43:07 +03:00
use iced::Task;
2026-09-09 14:26:55 +03:00
use iced::widget::text_editor;
2026-08-01 19:43:07 +03:00
use rand::prelude::SliceRandom;
2026-08-01 19:43:07 +03:00
#[derive(Clone, Debug)]
pub struct RandomizerState {
text: text_editor::Content,
list: Vec<String>,
}
2026-08-01 19:43:07 +03:00
#[derive(Debug, Clone)]
pub enum RandomizerMessage {
Back,
Start,
Edit(text_editor::Action),
}
2026-08-01 19:43:07 +03:00
impl NavigatedPage<RandomizerMessage> for RandomizerState {
2026-08-18 12:14:35 +03:00
fn navigate(&mut self, message: &RandomizerMessage) -> Option<Page> {
2026-08-01 19:43:07 +03:00
if let Back = message {
return Some(Page::PreviousPage);
}
2026-08-01 19:43:07 +03:00
None
}
2026-08-06 23:20:14 +03:00
2026-08-15 21:32:24 +03:00
fn navigated(&mut self) {}
2026-08-13 22:03:47 +03:00
fn update(&mut self, message: RandomizerMessage) -> Task<RootMessage> {
2026-08-01 19:43:07 +03:00
match message {
Edit(action) => {
self.text.perform(action);
self.list = self
.text
.text()
.split("\n")
.map(|s| s.to_string())
.collect();
}
2026-08-01 19:43:07 +03:00
Start => {
self.list.shuffle(&mut rand::rng());
self.text = text_editor::Content::with_text(self.list.join("\n").as_str());
}
_ => {}
}
2026-08-01 19:43:07 +03:00
Task::none()
}
2026-08-13 22:03:47 +03:00
fn view(&self) -> iced::Element<'_, RandomizerMessage> {
2026-08-01 19:43:07 +03:00
back_overlay(
iced::widget::column![
text_editor(&self.text)
.on_action(Edit)
.width(400)
.height(400)
.placeholder("Каждый элемент с новой строки"),
2026-09-09 14:26:55 +03:00
jl_button!("Перемешать").on_press(Start),
2026-08-01 19:43:07 +03:00
]
2026-08-15 21:32:24 +03:00
.spacing(HALF_SPACING)
.into(),
2026-08-01 19:43:07 +03:00
Back,
)
2026-08-15 21:32:24 +03:00
}
}
2026-08-13 22:03:47 +03:00
impl Default for RandomizerState {
fn default() -> Self {
Self::new()
}
}
2026-08-13 22:03:47 +03:00
impl RandomizerState {
pub fn new() -> RandomizerState {
RandomizerState {
text: Default::default(),
list: vec![],
}
}
}