Skip to content

Commit 3bddb37

Browse files
authored
Merge pull request #210 from ryanoneill/feature/component-tracing
Add event-flow tracing to Component trait
2 parents eba643d + 95cf809 commit 3bddb37

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

src/component/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,20 @@ pub trait Component: Sized {
437437
///
438438
/// This is the primary method users should call for event routing.
439439
fn dispatch_event(state: &mut Self::State, event: &Event) -> Option<Self::Output> {
440-
if let Some(msg) = Self::handle_event(state, event) {
441-
#[cfg(feature = "tracing")]
442-
let _span = tracing::debug_span!(
443-
"component_dispatch",
444-
component = std::any::type_name::<Self>(),
445-
)
446-
.entered();
440+
#[cfg(feature = "tracing")]
441+
let _span = tracing::debug_span!(
442+
"component_dispatch",
443+
component = std::any::type_name::<Self>(),
444+
event_kind = event.kind_name(),
445+
)
446+
.entered();
447+
448+
let msg = Self::handle_event(state, event);
449+
450+
#[cfg(feature = "tracing")]
451+
tracing::trace!(produced_message = msg.is_some(), "handle_event complete");
452+
453+
if let Some(msg) = msg {
447454
let output = Self::update(state, msg);
448455
#[cfg(feature = "tracing")]
449456
tracing::trace!(has_output = output.is_some(), "update complete");

src/input/events/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,33 @@ impl Event {
249249
_ => None,
250250
}
251251
}
252+
253+
/// Returns a short string identifying the event variant.
254+
///
255+
/// This is useful for logging and tracing. It returns the variant
256+
/// name (e.g., `"Key"`, `"Mouse"`, `"Resize"`) without the inner data.
257+
///
258+
/// # Example
259+
///
260+
/// ```rust
261+
/// use envision::input::{Event, KeyCode};
262+
///
263+
/// assert_eq!(Event::char('a').kind_name(), "Key");
264+
/// assert_eq!(Event::click(0, 0).kind_name(), "Mouse");
265+
/// assert_eq!(Event::Resize(80, 24).kind_name(), "Resize");
266+
/// assert_eq!(Event::FocusGained.kind_name(), "FocusGained");
267+
/// assert_eq!(Event::FocusLost.kind_name(), "FocusLost");
268+
/// ```
269+
pub fn kind_name(&self) -> &'static str {
270+
match self {
271+
Event::Key(_) => "Key",
272+
Event::Mouse(_) => "Mouse",
273+
Event::Resize(_, _) => "Resize",
274+
Event::FocusGained => "FocusGained",
275+
Event::FocusLost => "FocusLost",
276+
Event::Paste(_) => "Paste",
277+
}
278+
}
252279
}
253280

254281
impl From<KeyEvent> for Event {

src/input/events/tests.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,37 @@ fn test_mouse_event_builder_default() {
374374
assert_eq!(event.row, 0);
375375
assert!(matches!(event.kind, MouseEventKind::Moved));
376376
}
377+
378+
// -------------------------------------------------------------------------
379+
// Event::kind_name
380+
// -------------------------------------------------------------------------
381+
382+
#[test]
383+
fn test_kind_name_key() {
384+
assert_eq!(Event::char('a').kind_name(), "Key");
385+
assert_eq!(Event::key(KeyCode::Enter).kind_name(), "Key");
386+
assert_eq!(Event::ctrl('c').kind_name(), "Key");
387+
}
388+
389+
#[test]
390+
fn test_kind_name_mouse() {
391+
assert_eq!(Event::click(0, 0).kind_name(), "Mouse");
392+
assert_eq!(Event::mouse_move(5, 5).kind_name(), "Mouse");
393+
assert_eq!(Event::scroll_up(0, 0).kind_name(), "Mouse");
394+
}
395+
396+
#[test]
397+
fn test_kind_name_resize() {
398+
assert_eq!(Event::Resize(80, 24).kind_name(), "Resize");
399+
}
400+
401+
#[test]
402+
fn test_kind_name_focus() {
403+
assert_eq!(Event::FocusGained.kind_name(), "FocusGained");
404+
assert_eq!(Event::FocusLost.kind_name(), "FocusLost");
405+
}
406+
407+
#[test]
408+
fn test_kind_name_paste() {
409+
assert_eq!(Event::Paste("hello".to_string()).kind_name(), "Paste");
410+
}

0 commit comments

Comments
 (0)