use rand::Rng;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GazePattern {
FPattern,
ZPattern,
}
impl GazePattern {
#[must_use]
pub fn detect(context: &str) -> Self {
let lower = context.to_lowercase();
let z_signals = ["buy", "shop", "product", "sale", "offer", "price", "deal"];
if z_signals.iter().any(|s| lower.contains(s)) {
GazePattern::ZPattern
} else {
GazePattern::FPattern
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FocusPoint {
pub selector: String,
pub duration: Duration,
}
impl FocusPoint {
fn new(selector: impl Into<String>, duration: Duration) -> Self {
Self {
selector: selector.into(),
duration,
}
}
}
#[derive(Debug, Clone)]
pub struct AttentionConfig {
pub high_priority_selectors: Vec<String>,
pub pattern: GazePattern,
pub reading_wpm: f64,
}
impl Default for AttentionConfig {
fn default() -> Self {
Self {
high_priority_selectors: vec![
"h1".into(),
"h2".into(),
"h3".into(),
"img[src]".into(),
"button".into(),
"a.cta".into(),
"[data-cta]".into(),
".cta".into(),
"form".into(),
"nav".into(),
],
pattern: GazePattern::FPattern,
reading_wpm: 225.0,
}
}
}
pub struct AttentionSimulator {
config: AttentionConfig,
}
impl AttentionSimulator {
#[must_use]
pub fn new(config: AttentionConfig) -> Self {
Self { config }
}
#[must_use]
pub fn viewport_focus_pattern(&self) -> Vec<FocusPoint> {
let mut rng = rand::thread_rng();
let structural = match self.config.pattern {
GazePattern::FPattern => self.f_pattern_selectors(),
GazePattern::ZPattern => self.z_pattern_selectors(),
};
let mut points: Vec<FocusPoint> = Vec::new();
for selector in &structural {
let dur = self.gaze_duration_for(selector, &mut rng);
points.push(FocusPoint::new(selector, dur));
}
let insert_pos = if points.len() > 1 { 1 } else { points.len() };
let mut priority_points: Vec<FocusPoint> = self
.config
.high_priority_selectors
.iter()
.map(|sel| {
let dur = self.gaze_duration_for(sel, &mut rng);
FocusPoint::new(sel, dur)
})
.collect();
for i in (1..priority_points.len()).rev() {
let j = rng.gen_range(0..=i);
priority_points.swap(i, j);
}
for (offset, pp) in priority_points.into_iter().enumerate() {
let pos = (insert_pos + offset).min(points.len());
points.insert(pos, pp);
}
points
}
#[must_use]
pub fn viewport_focus_tuples(&self) -> Vec<(String, Duration)> {
self.viewport_focus_pattern()
.into_iter()
.map(|fp| (fp.selector, fp.duration))
.collect()
}
fn f_pattern_selectors(&self) -> Vec<String> {
vec![
"header".into(),
"h1, [role='banner']".into(),
"h2".into(),
"p:first-of-type".into(),
"aside, nav, ul:first-of-type".into(),
"article > p".into(),
"footer".into(),
]
}
fn z_pattern_selectors(&self) -> Vec<String> {
vec![
"header .logo, header img, h1".into(),
"nav, header .cta, header button".into(),
".hero, .banner, [class*='hero'], [class*='banner']".into(),
".features, .testimonials, [class*='feature']".into(),
".cta, button[type='submit'], a[href*='signup'], a[href*='buy']".into(),
"footer".into(),
]
}
fn gaze_duration_for<R: Rng>(&self, selector: &str, rng: &mut R) -> Duration {
let (base_ms, variance_ms): (u64, u64) = if is_heading(selector) {
(400, 200) } else if is_image(selector) {
(800, 400) } else if is_cta(selector) {
(600, 300) } else if is_nav(selector) {
(300, 150) } else if is_body_text(selector) {
let words_visible: u64 = rng.gen_range(15..50);
let read_ms = words_visible * 60_000 / self.config.reading_wpm as u64;
(read_ms, read_ms / 3)
} else {
(300, 200) };
let noise_ms = rng.gen_range(0..variance_ms.max(1));
let total = base_ms + noise_ms;
Duration::from_millis(total.max(100))
}
}
fn is_heading(sel: &str) -> bool {
["h1", "h2", "h3", "h4", "h5", "h6"]
.iter()
.any(|h| sel.contains(h))
}
fn is_image(sel: &str) -> bool {
sel.contains("img") || sel.contains("picture") || sel.contains("figure")
}
fn is_cta(sel: &str) -> bool {
sel.contains("button")
|| sel.contains("cta")
|| sel.contains("submit")
|| sel.contains("signup")
|| sel.contains("buy")
}
fn is_nav(sel: &str) -> bool {
sel.contains("nav") || sel.contains("menu") || sel.contains("header")
}
fn is_body_text(sel: &str) -> bool {
sel.contains(" p") || sel.starts_with('p') || sel.contains("article") || sel.contains("aside")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detect_z_for_shop_context() {
assert_eq!(
GazePattern::detect("Best deals and shop now!"),
GazePattern::ZPattern
);
}
#[test]
fn detect_z_for_buy_context() {
assert_eq!(
GazePattern::detect("Buy a new laptop today"),
GazePattern::ZPattern
);
}
#[test]
fn detect_f_for_article_context() {
assert_eq!(
GazePattern::detect("How Rust memory safety works"),
GazePattern::FPattern
);
}
#[test]
fn detect_f_by_default() {
assert_eq!(GazePattern::detect(""), GazePattern::FPattern);
}
#[test]
fn f_pattern_has_expected_count() {
let sim = AttentionSimulator::new(AttentionConfig {
pattern: GazePattern::FPattern,
high_priority_selectors: vec![],
..Default::default()
});
let pts = sim.viewport_focus_pattern();
assert!(
pts.len() >= 5,
"F-pattern should produce at least 5 points, got {}",
pts.len()
);
}
#[test]
fn z_pattern_has_expected_count() {
let sim = AttentionSimulator::new(AttentionConfig {
pattern: GazePattern::ZPattern,
high_priority_selectors: vec![],
..Default::default()
});
let pts = sim.viewport_focus_pattern();
assert!(
pts.len() >= 5,
"Z-pattern should produce at least 5 points, got {}",
pts.len()
);
}
#[test]
fn all_high_priority_selectors_included() {
let priorities = vec!["h1".to_string(), "button".to_string(), "img".to_string()];
let sim = AttentionSimulator::new(AttentionConfig {
high_priority_selectors: priorities.clone(),
..Default::default()
});
let pts = sim.viewport_focus_pattern();
for p in &priorities {
assert!(
pts.iter().any(|fp| &fp.selector == p),
"selector '{p}' missing from focus pattern"
);
}
}
#[test]
fn all_durations_at_least_100ms() {
let sim = AttentionSimulator::new(AttentionConfig::default());
for fp in sim.viewport_focus_pattern() {
assert!(
fp.duration >= Duration::from_millis(100),
"focus point '{}' has duration {:?} < 100 ms",
fp.selector,
fp.duration
);
}
}
#[test]
fn viewport_focus_tuples_matches_pattern() {
let sim = AttentionSimulator::new(AttentionConfig::default());
let tuples = sim.viewport_focus_tuples();
assert!(
tuples.len() >= 5,
"expected ≥5 tuples, got {}",
tuples.len()
);
for (sel, dur) in &tuples {
assert!(!sel.is_empty(), "selector should be non-empty");
assert!(
*dur >= Duration::from_millis(100),
"duration {:?} < 100ms for '{}'",
dur,
sel
);
}
}
#[test]
fn is_heading_detects_h1_h2() {
assert!(is_heading("h1"));
assert!(is_heading("h2"));
assert!(is_heading("h3.title"));
assert!(!is_heading("div"));
}
#[test]
fn is_image_detects_img() {
assert!(is_image("img[src]"));
assert!(is_image("picture"));
assert!(is_image("figure.hero"));
assert!(!is_image("span"));
}
#[test]
fn is_cta_detects_button_and_signup() {
assert!(is_cta("button"));
assert!(is_cta("a[href*='signup']"));
assert!(is_cta(".cta"));
assert!(!is_cta("p"));
}
#[test]
fn is_nav_detects_nav_and_header() {
assert!(is_nav("nav"));
assert!(is_nav("header"));
assert!(!is_nav("footer"));
}
#[test]
fn is_body_text_detects_p_and_article() {
assert!(is_body_text("article > p"));
assert!(is_body_text("p:first-of-type"));
assert!(!is_body_text("h1"));
}
#[test]
fn image_duration_longer_than_heading() {
let sim = AttentionSimulator::new(AttentionConfig::default());
let mut rng = rand::thread_rng();
let sum_img: u128 = (0..200)
.map(|_| sim.gaze_duration_for("img", &mut rng).as_millis())
.sum();
let sum_h1: u128 = (0..200)
.map(|_| sim.gaze_duration_for("h1", &mut rng).as_millis())
.sum();
assert!(
sum_img > sum_h1,
"image avg ({:.0}) should exceed heading avg ({:.0})",
sum_img as f64 / 200.0,
sum_h1 as f64 / 200.0
);
}
#[test]
fn focus_point_stores_selector_and_duration() {
let fp = FocusPoint::new("h1", Duration::from_millis(500));
assert_eq!(fp.selector, "h1");
assert_eq!(fp.duration, Duration::from_millis(500));
}
}