<commit_before>use std::sync::{Arc, Mutex};

use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{
    self, Document, Element, HtmlCanvasElement, KeyboardEvent, MouseEvent, Node, UiEvent, Window,
};

use crate::input::prelude::{InputEvent, MouseButton};
use crate::window::prelude::{Event, WindowEvent, WindowParams};

use crate::math::prelude::Vector2;
use crate::errors::*;

use super::{types, Visitor};

#[allow(dead_code)]
pub struct WebVisitor {
    window: Window,
    document: Document,
    canvas: HtmlCanvasElement,
    events: Arc<Mutex<Vec<Event>>>,
    on_mouse_move: Closure<FnMut(MouseEvent)>,
    on_mouse_down: Closure<FnMut(MouseEvent)>,
    on_mouse_up: Closure<FnMut(MouseEvent)>,
    on_key_down: Closure<FnMut(KeyboardEvent)>,
    on_key_up: Closure<FnMut(KeyboardEvent)>,
    on_resize: Closure<FnMut(UiEvent)>,
    on_focus: Closure<FnMut(UiEvent)>,
    on_lost_focus: Closure<FnMut(UiEvent)>,
}

impl WebVisitor {
    pub fn new(params: WindowParams) -> Result<Self> {
        let window = web_sys::window().expect("no global `window` exists");
        let document = window.document().expect("should have a document on window");
        let body = document.body().expect("document should have a body");

        let header = document.create_element("p").unwrap();
        header.set_inner_html(&params.title);
        AsRef::<Node>::as_ref(&body)
            .append_child(header.as_ref())
            .unwrap();

        let canvas = document
            .create_element("canvas")
            .unwrap()
            .dyn_into::<HtmlCanvasElement>()
            .map_err(|_| ())
            .unwrap();

        canvas
            .unchecked_ref::<Element>()
            .set_attribute("id", "canvas")
            .unwrap();

        AsRef::<Node>::as_ref(&body)
            .append_child(canvas.as_ref())
            .unwrap();

        let events = Arc::new(Mutex::new(Vec::new()));

        let on_mouse_down = {
            let clone = events.clone();
            Closure::wrap(Box::new(move |v: MouseEvent| {
                let button = match v.button() {
                    0 => MouseButton::Left,<commit_msg>Add tabindex attribute to the canvas element<commit_after>use std::sync::{Arc, Mutex};

use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{
    self, Document, Element, HtmlCanvasElement, KeyboardEvent, MouseEvent, Node, UiEvent, Window,
};

use crate::input::prelude::{InputEvent, MouseButton};
use crate::window::prelude::{Event, WindowEvent, WindowParams};

use crate::math::prelude::Vector2;
use crate::errors::*;

use super::{types, Visitor};

#[allow(dead_code)]
pub struct WebVisitor {
    window: Window,
    document: Document,
    canvas: HtmlCanvasElement,
    events: Arc<Mutex<Vec<Event>>>,
    on_mouse_move: Closure<FnMut(MouseEvent)>,
    on_mouse_down: Closure<FnMut(MouseEvent)>,
    on_mouse_up: Closure<FnMut(MouseEvent)>,
    on_key_down: Closure<FnMut(KeyboardEvent)>,
    on_key_up: Closure<FnMut(KeyboardEvent)>,
    on_resize: Closure<FnMut(UiEvent)>,
    on_focus: Closure<FnMut(UiEvent)>,
    on_lost_focus: Closure<FnMut(UiEvent)>,
}

impl WebVisitor {
    pub fn new(params: WindowParams) -> Result<Self> {
        let window = web_sys::window().expect("no global `window` exists");
        let document = window.document().expect("should have a document on window");
        let body = document.body().expect("document should have a body");

        let header = document.create_element("p").unwrap();
        header.set_inner_html(&params.title);
        AsRef::<Node>::as_ref(&body)
            .append_child(header.as_ref())
            .unwrap();

        let canvas = document
            .create_element("canvas")
            .unwrap()
            .dyn_into::<HtmlCanvasElement>()
            .map_err(|_| ())
            .unwrap();

        canvas
            .unchecked_ref::<Element>()
            .set_attribute("id", "canvas")
            .unwrap();

        canvas
            .unchecked_ref::<Element>()
            .set_attribute("tabindex", "1")
            .unwrap();

        AsRef::<Node>::as_ref(&body)
            .append_child(canvas.as_ref())
            .unwrap();

        let events = Arc::new(Mutex::new(Vec::new()));

        let on_mouse_down = {
            let clone = events.clone();
            Closure::wrap(Box::new(move |v: MouseEvent| {
                let button = match v.button() {
                    0 => MouseButton::Left,
{'fraction_non_alphanumeric': 0.15004472271914132, 'fraction_numerical': 0.0011180679785330948, 'mean_word_length': 2.97246891651865, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5982541', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1394, 'n_words': 266}