From 74f76e98ccdc22b26c96e6ad6ba99f2a5c30d4d6 Mon Sep 17 00:00:00 2001 From: chrox Date: Wed, 20 Feb 2013 11:42:02 +0800 Subject: [PATCH] separate abstract interface EventListener from Widget The rationale is that some non-widget modules like ReaderKoptListener should be able to handle events. --- frontend/ui/widget.lua | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/frontend/ui/widget.lua b/frontend/ui/widget.lua index bf503f79f..eaa1d0ae3 100644 --- a/frontend/ui/widget.lua +++ b/frontend/ui/widget.lua @@ -7,6 +7,31 @@ require "ui/inputevent" require "ui/gesturedetector" require "ui/font" +--[[ +The EventListener is an interface that handles events + +EventListeners have a rudimentary event handler/dispatcher that +will call a method "onEventName" for an event with name +"EventName" + +]] +EventListener = {} + +function EventListener:new(o) + local o = o or {} + setmetatable(o, self) + self.__index = self + if o.init then o:init() end + return o +end + +function EventListener:handleEvent(event) + if self[event.handler] then + return self[event.handler](self, unpack(event.args)) + end +end + + --[[ This is a generic Widget interface @@ -18,7 +43,7 @@ if the table that was given to us as parameter has an "init" method, it will be called. use this to set _instance_ variables rather than class variables. ]] -Widget = {} +Widget = EventListener:new() function Widget:new(o) local o = o or {} @@ -40,19 +65,6 @@ end function Widget:paintTo(bb, x, y) end ---[[ -Widgets have a rudimentary event handler/dispatcher that -will call a method "onEventName" for an event with name -"EventName" - -These methods -]] -function Widget:handleEvent(event) - if self[event.handler] then - return self[event.handler](self, unpack(event.args)) - end -end - --[[ WidgetContainer is a container for another Widget ]]