diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-22 23:42:41 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-22 23:43:02 +0900 |
| commit | 3b783e97a9aac366a7d6e26059961ba53d5e8b20 (patch) | |
| tree | c3e6bb44007a2ed50dde73ff5e3f625ee981b990 /crates/shirabe/src/script | |
| parent | aa4e9ff27b974343beeccdd14e07e13f0e261599 (diff) | |
| download | php-shirabe-3b783e97a9aac366a7d6e26059961ba53d5e8b20.tar.gz php-shirabe-3b783e97a9aac366a7d6e26059961ba53d5e8b20.tar.zst php-shirabe-3b783e97a9aac366a7d6e26059961ba53d5e8b20.zip | |
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<Box<BaseEvent>>, 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) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/script')
| -rw-r--r-- | crates/shirabe/src/script/event.rs | 70 |
1 files changed, 58 insertions, 12 deletions
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<std::cell::RefCell<dyn IOInterface>>, dev_mode: bool, - originating_event: Option<Box<BaseEvent>>, + originating_event: Option<OriginatingEvent>, +} + +#[derive(Debug)] +pub enum OriginatingEvent { + Base(BaseEvent), + Script(Box<Event>), } 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<String> { + match self { + OriginatingEvent::Base(e) => e.get_arguments(), + OriginatingEvent::Script(e) => e.get_arguments(), + } + } + + fn get_flags(&self) -> &IndexMap<String, PhpMixed> { + 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(), + } + } +} |
