From 3b783e97a9aac366a7d6e26059961ba53d5e8b20 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 22 Jun 2026 23:42:41 +0900 Subject: feat(script): make originating event polymorphic via enum Script\Event extends BaseEvent in PHP, so setOriginatingEvent accepts both a base event and a nested Script\Event, and calculateOriginatingEvent walks the chain via an instanceof check. The port had pinned the field to a concrete Option>, leaving calculate_originating_event as todo!() and the nested-event test unportable. Introduce an OriginatingEvent enum (Base/Script) to model the instanceof tag, implement the recursive flattening, and un-ignore both originating event tests. Co-Authored-By: Claude Opus 4.8 (1M context) --- crates/shirabe/src/script/event.rs | 70 +++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 12 deletions(-) (limited to 'crates/shirabe/src/script/event.rs') diff --git a/crates/shirabe/src/script/event.rs b/crates/shirabe/src/script/event.rs index 5680c22..1b1e76d 100644 --- a/crates/shirabe/src/script/event.rs +++ b/crates/shirabe/src/script/event.rs @@ -13,7 +13,13 @@ pub struct Event { composer: ComposerWeakHandle, io: std::rc::Rc>, dev_mode: bool, - originating_event: Option>, + originating_event: Option, +} + +#[derive(Debug)] +pub enum OriginatingEvent { + Base(BaseEvent), + Script(Box), } impl Event { @@ -46,22 +52,25 @@ impl Event { self.dev_mode } - pub fn get_originating_event(&self) -> Option<&BaseEvent> { - self.originating_event.as_deref() + pub fn get_originating_event(&self) -> Option<&OriginatingEvent> { + self.originating_event.as_ref() } - pub fn set_originating_event(&mut self, event: BaseEvent) -> &mut Self { - self.originating_event = Some(Box::new(self.calculate_originating_event(event))); + pub fn set_originating_event(&mut self, event: OriginatingEvent) -> &mut Self { + self.originating_event = Some(Self::calculate_originating_event(event)); self } - fn calculate_originating_event(&self, event: BaseEvent) -> BaseEvent { - // if ($event instanceof Event && $event->getOriginatingEvent()) { - // return $this->calculateOriginatingEvent($event->getOriginatingEvent()); - // } - // - // return $event; - todo!() + fn calculate_originating_event(event: OriginatingEvent) -> OriginatingEvent { + if let OriginatingEvent::Script(boxed) = event { + let mut inner_event = *boxed; + if let Some(originating) = inner_event.originating_event.take() { + return Self::calculate_originating_event(originating); + } + return OriginatingEvent::Script(Box::new(inner_event)); + } + + event } } @@ -86,3 +95,40 @@ impl EventInterface for Event { self.inner.stop_propagation(); } } + +impl EventInterface for OriginatingEvent { + fn get_name(&self) -> &str { + match self { + OriginatingEvent::Base(e) => e.get_name(), + OriginatingEvent::Script(e) => e.get_name(), + } + } + + fn get_arguments(&self) -> &Vec { + match self { + OriginatingEvent::Base(e) => e.get_arguments(), + OriginatingEvent::Script(e) => e.get_arguments(), + } + } + + fn get_flags(&self) -> &IndexMap { + match self { + OriginatingEvent::Base(e) => e.get_flags(), + OriginatingEvent::Script(e) => e.get_flags(), + } + } + + fn is_propagation_stopped(&self) -> bool { + match self { + OriginatingEvent::Base(e) => e.is_propagation_stopped(), + OriginatingEvent::Script(e) => e.is_propagation_stopped(), + } + } + + fn stop_propagation(&mut self) { + match self { + OriginatingEvent::Base(e) => e.stop_propagation(), + OriginatingEvent::Script(e) => e.stop_propagation(), + } + } +} -- cgit v1.3.1