| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use quasi_router::{Chrome, Field, Node, Screen, Slot}; |
| 25 |
|
| 26 |
use crate::View; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#[derive(Debug, Clone, Default)] |
| 35 |
pub(crate) struct Hidden<'a> { |
| 36 |
|
| 37 |
pub(crate) regions: Vec<&'a str>, |
| 38 |
|
| 39 |
pub(crate) fields: Vec<&'a str>, |
| 40 |
} |
| 41 |
|
| 42 |
impl Hidden<'_> { |
| 43 |
|
| 44 |
pub(crate) const fn none() -> Self { |
| 45 |
Self { |
| 46 |
regions: Vec::new(), |
| 47 |
fields: Vec::new(), |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
pub(crate) fn out(&self, id: &str) -> bool { |
| 53 |
self.regions.contains(&id) |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
pub(crate) fn field_out(&self, name: &str) -> bool { |
| 58 |
self.fields.contains(&name) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
pub(crate) fn hidden<'a>(screen: &'a Screen, chrome: &Chrome, view: &View) -> Hidden<'a> { |
| 69 |
let mut found = Hidden::none(); |
| 70 |
for slot in &screen.slots { |
| 71 |
walk(slot, screen, chrome, view, &mut found); |
| 72 |
} |
| 73 |
found |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
fn asked(field: &Field, screen: &Screen, chrome: &Chrome, view: &View) -> bool { |
| 78 |
let Some(control) = field.watches() else { |
| 79 |
return true; |
| 80 |
}; |
| 81 |
field.revealed(held(control, screen, chrome, view)) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
fn questions<'a>( |
| 90 |
node: &'a Node, |
| 91 |
screen: &Screen, |
| 92 |
chrome: &Chrome, |
| 93 |
view: &View, |
| 94 |
found: &mut Hidden<'a>, |
| 95 |
) { |
| 96 |
let fields: &[Field] = match node { |
| 97 |
Node::Field(field) => std::slice::from_ref(field.as_ref()), |
| 98 |
Node::Form { fields, .. } => fields, |
| 99 |
_ => &[], |
| 100 |
}; |
| 101 |
for field in fields { |
| 102 |
if !asked(field, screen, chrome, view) { |
| 103 |
found.fields.push(field.name.as_str()); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
fn walk<'a>(slot: &'a Slot, screen: &Screen, chrome: &Chrome, view: &View, found: &mut Hidden<'a>) { |
| 113 |
if !out(slot, screen, chrome, view) { |
| 114 |
found.regions.push(slot.id.as_str()); |
| 115 |
return; |
| 116 |
} |
| 117 |
for placed in slot |
| 118 |
.run |
| 119 |
.iter() |
| 120 |
.flat_map(|run| run.members.iter()) |
| 121 |
.chain(slot.body.iter()) |
| 122 |
{ |
| 123 |
if let Node::Region(inner) = &placed.node { |
| 124 |
walk(inner, screen, chrome, view, found); |
| 125 |
} else { |
| 126 |
questions(&placed.node, screen, chrome, view, found); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
fn out(slot: &Slot, screen: &Screen, chrome: &Chrome, view: &View) -> bool { |
| 133 |
let Some(control) = slot.watches() else { |
| 134 |
return true; |
| 135 |
}; |
| 136 |
slot.revealed(held(control, screen, chrome, view)) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
fn held<'a>( |
| 149 |
control: &str, |
| 150 |
screen: &'a Screen, |
| 151 |
chrome: &'a Chrome, |
| 152 |
view: &'a View, |
| 153 |
) -> Option<&'a str> { |
| 154 |
view.edit(control).or_else(|| { |
| 155 |
screen.holds(control).or_else(|| { |
| 156 |
chrome |
| 157 |
.panels |
| 158 |
.iter() |
| 159 |
.find_map(|panel| panel.content.holds(control)) |
| 160 |
}) |
| 161 |
}) |
| 162 |
} |
| 163 |
|