Skip to content

Commit

Permalink
Implement keydown, keyup, keypress, and input events
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Oct 7, 2024
1 parent 537b7b2 commit af149a9
Show file tree
Hide file tree
Showing 8 changed files with 816 additions and 86 deletions.
19 changes: 11 additions & 8 deletions packages/blitz-dom/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ pub trait DocumentLike: AsRef<Document> + AsMut<Document> + Into<Document> + 'st
false
}

fn handle_event(&mut self, _event: RendererEvent) -> bool {
fn handle_event(&mut self, _event: RendererEvent) {
// Default implementation does nothing
false
}

fn as_any_mut(&mut self) -> &mut dyn Any {
Expand Down Expand Up @@ -125,8 +124,10 @@ pub struct Document {
}

impl DocumentLike for Document {
fn handle_event(&mut self, event: RendererEvent) -> bool {
// let node_id = event.target;
fn handle_event(&mut self, event: RendererEvent) {
let target_node_id = event.target;

let event = dbg!(event);

match event.data {
EventData::Click { x, y, mods } => {
Expand All @@ -140,12 +141,12 @@ impl DocumentLike for Document {
y: node.final_layout.padding.top + node.final_layout.border.top,
};
let Some(el) = node.raw_dom_data.downcast_element_mut() else {
return true;
return;
};

let disabled = el.attr(local_name!("disabled")).is_some();
if disabled {
return true;
return;
}

if let NodeSpecificData::TextInput(ref mut text_input_data) =
Expand Down Expand Up @@ -185,6 +186,10 @@ impl DocumentLike for Document {
}
EventData::KeyPress { event, mods } => {
if let Some(node_id) = self.focus_node_id {
if target_node_id != node_id {
return;
}

let node = &mut self.nodes[node_id];
let text_input_data = node
.raw_dom_data
Expand Down Expand Up @@ -213,8 +218,6 @@ impl DocumentLike for Document {
}
EventData::Hover => {}
}

true
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/blitz-dom/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub struct EventListener {
pub name: String,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct RendererEvent {
pub target: usize,
pub data: EventData,
Expand All @@ -17,7 +17,7 @@ impl RendererEvent {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum EventData {
Click { x: f32, y: f32, mods: Modifiers },
KeyPress { event: KeyEvent, mods: Modifiers },
Expand Down
4 changes: 2 additions & 2 deletions packages/blitz-dom/src/html_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl From<HtmlDocument> for Document {
}
}
impl DocumentLike for HtmlDocument {
fn handle_event(&mut self, event: RendererEvent) -> bool {
self.inner.as_mut().handle_event(event)
fn handle_event(&mut self, event: RendererEvent) {
self.inner.as_mut().handle_event(event);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/dioxus-blitz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tracing = { workspace = true, optional = true }
url = { version = "2.5.0", features = ["serde"] }
ureq = "2.9"
rustc-hash = "1.1.0"
keyboard-types = "0.7"

[target.'cfg(target_os = "android")'.dependencies]
android-activity = { version = "0.6.0", features = ["native-activity"] }
Expand Down
Loading

0 comments on commit af149a9

Please sign in to comment.