| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
use makeover_layout as layout; |
| 46 |
use quasi_router::{Act, Action, Field, Node, Part, Row, Screen, Slot}; |
| 47 |
|
| 48 |
|
| 49 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 50 |
pub enum Spot { |
| 51 |
|
| 52 |
Act { |
| 53 |
|
| 54 |
action: Action, |
| 55 |
|
| 56 |
confirm: Option<String>, |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
key: Option<String>, |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
over: Option<String>, |
| 68 |
}, |
| 69 |
|
| 70 |
Link { |
| 71 |
|
| 72 |
action: Action, |
| 73 |
}, |
| 74 |
|
| 75 |
Field(Box<FieldSpot>), |
| 76 |
|
| 77 |
Submit { |
| 78 |
|
| 79 |
action: Action, |
| 80 |
|
| 81 |
|
| 82 |
names: Vec<String>, |
| 83 |
}, |
| 84 |
|
| 85 |
Row { |
| 86 |
|
| 87 |
activate: Option<Action>, |
| 88 |
|
| 89 |
toggle: Option<Action>, |
| 90 |
|
| 91 |
ticked: Option<bool>, |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
value: Option<String>, |
| 100 |
|
| 101 |
|
| 102 |
menu: Vec<Act>, |
| 103 |
}, |
| 104 |
|
| 105 |
Choice { |
| 106 |
|
| 107 |
action: Action, |
| 108 |
}, |
| 109 |
|
| 110 |
More { |
| 111 |
|
| 112 |
action: Action, |
| 113 |
}, |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 118 |
pub struct FieldSpot { |
| 119 |
|
| 120 |
pub name: String, |
| 121 |
|
| 122 |
pub kind: layout::FieldKind, |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
pub value: Option<String>, |
| 130 |
|
| 131 |
pub options: Vec<String>, |
| 132 |
|
| 133 |
pub max_length: Option<u32>, |
| 134 |
|
| 135 |
pub changes: Option<Action>, |
| 136 |
} |
| 137 |
|
| 138 |
impl Spot { |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
#[must_use] |
| 145 |
pub fn enters(&self) -> Option<&Action> { |
| 146 |
match self { |
| 147 |
Self::Act { action, .. } |
| 148 |
| Self::Link { action } |
| 149 |
| Self::Submit { action, .. } |
| 150 |
| Self::Choice { action } |
| 151 |
| Self::More { action } => Some(action), |
| 152 |
Self::Row { activate, .. } => activate.as_ref(), |
| 153 |
Self::Field(_) => None, |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
#[must_use] |
| 159 |
pub const fn field(&self) -> Option<&FieldSpot> { |
| 160 |
match self { |
| 161 |
Self::Field(spot) => Some(spot), |
| 162 |
_ => None, |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 175 |
pub struct Reach { |
| 176 |
|
| 177 |
pub region: String, |
| 178 |
|
| 179 |
pub spot: Spot, |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
#[must_use] |
| 184 |
pub fn reaches(screen: &Screen) -> Vec<Reach> { |
| 185 |
let mut found = Vec::new(); |
| 186 |
for slot in crate::region::reachable(screen) { |
| 187 |
slot_spots(slot, &mut found); |
| 188 |
} |
| 189 |
found |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
#[must_use] |
| 194 |
pub fn spots(screen: &Screen) -> Vec<Spot> { |
| 195 |
reaches(screen) |
| 196 |
.into_iter() |
| 197 |
.map(|reach| reach.spot) |
| 198 |
.collect() |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
fn slot_spots(slot: &Slot, found: &mut Vec<Reach>) { |
| 207 |
if matches!(slot.readiness, layout::Readiness::Pending) { |
| 208 |
return; |
| 209 |
} |
| 210 |
for node in &slot.body { |
| 211 |
node_spots(node, &slot.id, found); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
pub(crate) fn node_spots(node: &Node, region: &str, found: &mut Vec<Reach>) { |
| 217 |
|
| 218 |
|
| 219 |
macro_rules! push { |
| 220 |
($spot:expr) => { |
| 221 |
found.push(Reach { |
| 222 |
region: region.to_string(), |
| 223 |
spot: $spot, |
| 224 |
}) |
| 225 |
}; |
| 226 |
} |
| 227 |
|
| 228 |
match node { |
| 229 |
Node::Act(act) => push_act(act, region, found), |
| 230 |
|
| 231 |
Node::Link { action, .. } => push!(Spot::Link { |
| 232 |
action: action.clone(), |
| 233 |
}), |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
Node::Token(tag) => { |
| 240 |
if let layout::Token::Chip { .. } = tag.kind |
| 241 |
&& let Some(action) = tag.action.clone() |
| 242 |
{ |
| 243 |
push!(Spot::Act { |
| 244 |
action, |
| 245 |
confirm: None, |
| 246 |
key: None, |
| 247 |
over: None, |
| 248 |
}); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
Node::StandIn { act, .. } => { |
| 253 |
if let Some(act) = act { |
| 254 |
push_act(act, region, found); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
Node::Field(field) => push_field(field, region, found), |
| 259 |
|
| 260 |
Node::Form { |
| 261 |
action, |
| 262 |
fields, |
| 263 |
submit: _, |
| 264 |
} => { |
| 265 |
for field in fields { |
| 266 |
push_field(field, region, found); |
| 267 |
} |
| 268 |
push!(Spot::Submit { |
| 269 |
action: action.clone(), |
| 270 |
names: fields.iter().map(|field| field.name.clone()).collect(), |
| 271 |
}); |
| 272 |
} |
| 273 |
|
| 274 |
Node::List { rows, more } => { |
| 275 |
for row in rows { |
| 276 |
push_row(row, region, found); |
| 277 |
} |
| 278 |
if let Some(rest) = more { |
| 279 |
push!(Spot::More { |
| 280 |
action: rest.action.clone(), |
| 281 |
}); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
Node::Timeline { entries, .. } => { |
| 290 |
for entry in entries { |
| 291 |
push_row(&entry.row, region, found); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
Node::Table { rows, .. } => { |
| 302 |
for cells in rows { |
| 303 |
if let Some(activate) = cells.activate.clone() { |
| 304 |
push!(Spot::Row { |
| 305 |
activate: Some(activate), |
| 306 |
toggle: None, |
| 307 |
ticked: None, |
| 308 |
value: None, |
| 309 |
menu: Vec::new(), |
| 310 |
}); |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
Node::Select { |
| 316 |
options, action, .. |
| 317 |
} => { |
| 318 |
for (choice, own) in options { |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
let call = own.clone().or_else(|| { |
| 323 |
action |
| 324 |
.clone() |
| 325 |
.map(|action| action.with(Node::SELECTED, choice.value.clone())) |
| 326 |
}); |
| 327 |
if let Some(action) = call { |
| 328 |
push!(Spot::Choice { action }); |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
Node::Region(slot) => slot_spots(slot, found), |
| 334 |
|
| 335 |
|
| 336 |
Node::Heading { .. } |
| 337 |
| Node::Text { .. } |
| 338 |
| Node::Rich { .. } |
| 339 |
| Node::Figure(_) |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
| Node::Image(_) |
| 344 |
| Node::Notice { .. } |
| 345 |
| Node::Meter(_) |
| 346 |
| Node::Stats { .. } => {} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
fn push_act(act: &Act, region: &str, found: &mut Vec<Reach>) { |
| 352 |
if act.state.is_some_and(layout::State::suppresses_interaction) { |
| 353 |
return; |
| 354 |
} |
| 355 |
found.push(Reach { |
| 356 |
region: region.to_string(), |
| 357 |
spot: Spot::Act { |
| 358 |
action: act.action.clone(), |
| 359 |
confirm: act.confirm.clone(), |
| 360 |
key: act.key.clone(), |
| 361 |
over: act.over.clone(), |
| 362 |
}, |
| 363 |
}); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
fn push_field(field: &Field, region: &str, found: &mut Vec<Reach>) { |
| 371 |
if matches!(field.kind, layout::FieldKind::Hidden) { |
| 372 |
return; |
| 373 |
} |
| 374 |
found.push(Reach { |
| 375 |
region: region.to_string(), |
| 376 |
spot: Spot::Field(Box::new(FieldSpot { |
| 377 |
name: field.name.clone(), |
| 378 |
kind: field.kind, |
| 379 |
value: field.value.clone(), |
| 380 |
options: field |
| 381 |
.options |
| 382 |
.iter() |
| 383 |
.map(|choice| choice.value.clone()) |
| 384 |
.collect(), |
| 385 |
max_length: field.max_length, |
| 386 |
changes: field.changes.clone(), |
| 387 |
})), |
| 388 |
}); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
fn push_row(row: &Row, region: &str, found: &mut Vec<Reach>) { |
| 402 |
if row.activate.is_some() |
| 403 |
|| row.toggle.is_some() |
| 404 |
|| row.selected.is_some() |
| 405 |
|| !row.menu.is_empty() |
| 406 |
{ |
| 407 |
found.push(Reach { |
| 408 |
region: region.to_string(), |
| 409 |
spot: Spot::Row { |
| 410 |
activate: row.activate.clone(), |
| 411 |
toggle: row.toggle.clone(), |
| 412 |
ticked: row.selected, |
| 413 |
value: row.value.clone(), |
| 414 |
menu: row.menu.clone(), |
| 415 |
}, |
| 416 |
}); |
| 417 |
} |
| 418 |
for Part { node, .. } in &row.parts { |
| 419 |
node_spots(node, region, found); |
| 420 |
} |
| 421 |
} |
| 422 |
|