abstract class EventsHandler( val fillPayload: (cache: Payloads) -> Unit ) { abstract val payloadCache: Payloads private val eventMap = mutableMapOf, Payloads.(event: Event) -> Unit>()
abstract class EventsHandler( val fillPayload: (cache: Payloads) -> Unit ) { infix fun KClass.to(lambda: Payloads.(event: EventType) -> Unit) { eventMap[event] = lambda as Payloads.(event: Event) -> Unit } // ... (rest of the code remains the same) }
class BlockEventsHandler( manager: EventsManager, override val fillPayload: (cache: TextBlockEventsHandler) -> Unit ) : EventsHandler(manager) { lateinit var engine: Engine lateinit var block: DesignBlock lateinit var fontFamilyMap: Map
init { BlockEvent.OnDelete::class to { engine.delete(block) } BlockEvent.OnBackward::class to { engine.sendBackward(block) } BlockEvent.OnDuplicate::class to { engine.duplicate(block) } BlockEvent.OnForward::class to { engine.bringForward(block) } BlockEvent.ToBack::class to { engine.sendToBack(block) } BlockEvent.ToFront::class to { engine.bringToFront(block) } BlockEvent.OnChangeFinish::class to { engine.editor.addUndoStep() } BlockEvent.OnChangeBlendMode::class to { onChangeBlendMode(engine, block, it.blendMode) } BlockEvent.OnChangeOpacity::class to { engine.block.setOpacity(block, it.opacity) } BlockEvent.OnChangeFillColor::class to { onChangeFillColor(engine, block, it.color) } // ... } }
class EventsHandler( register: EventsHandler.() -> Unit, ) { inline fun register(noinline lambda: (event: EventType) -> Unit) : Any { this[EventType::class] = lambda return lambda } // ... (rest of the code remains the same) }
fun EventsHandler.textBlockEvents( engine: () -> Engine, block: () -> DesignBlock, fontFamilyMap: () -> Map, ) { // Inject the dependencies val engine by Inject(engine) val block by Inject(block) val fontFamilyMap by Inject(fontFamilyMap)
fun EventsHandler.blockEvents( engine: () -> Engine, block: () -> DesignBlock ) { val engine: Engine by Inject(engine) val block: DesignBlock by Inject(block)
fun EventsHandler.cropEvents( engine: () -> Engine, block: () -> DesignBlock ) { val engine: Engine by Inject(engine) val block: DesignBlock by Inject(block) // ... (event handling logic for cropping events) }
fun EventsHandler.textBlockEvents( engine: () -> Engine, block: () -> DesignBlock, fontFamilyMap: () -> Map, ) { val engine by Inject(engine) val block by Inject(block) val fontFamilyMap by Inject(fontFamilyMap) // ... (event handling logic for text block events) }
在代码的触发及其 API 保持不变的同时, 也不需要传递额外的参数:
open fun onEvent(event: Event) { eventHandler.handleEvent(event) }