-- flaw, a Lua OO management framework for Awesome WM widgets. -- Copyright (C) 2009,2011 David Soulayrol -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation, either version 3 of the License, or -- (at your option) any later version. -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . -- Grab environment. local setmetatable = setmetatable --- Core events objects. -- --

Events are a way for the user to modify the gadget behaviour or -- properties when certain conditions are met. An event is composed of -- a trigger, which computes the condition, and an action.

-- --

Trigger and Action

-- --

The trigger is an object which takes a condition and provides -- some status depending of its type. The trigger is tested each time -- the gadget's provider refreshes its data, and the action called if -- the event fires. There are three kind of triggers; the simple Trigger, the LatchTrigger and the EdgeTrigger described -- below. All depend on the condition function but fire the event -- differently, depending on their previous status.

-- --

The condition function takes one parameter which is the data -- table updated by the provider. This table layout depends on the -- provider and is detailed in the provider documentation. The -- condition function shall return true if its conditions are met, or -- false otherwise. The event will then fire depending on its -- type.

-- --

The action function takes the gadget as parameter. It can do -- absolutely anything, and its return value has no meaning for -- flaw.

-- --

Usage

-- --

Condition and action are written by the user. Events must be -- registered to a gadget, which in turn registers the trigger to its -- provider.

-- --
-- t = flaw.event.LatchTrigger:new{
--    condition = function(d) return d.load < 25 end },
-- a = function(g) g.pattern = '<bg color="#ff6565"/>$load%' end
-- gadget:add_event(e, a)
--
-- --

Assuming g is a battery icon gadget created as -- above and battery_low_icon a beautiful property -- holding the path to an explicit low battery icon, here is a short -- snippet to change the icon in a gadget when the battery load gets -- really low.

-- --
-- g.add_event(
--    flaw.event.LatchTriger:new{ -- condition = function(d) return d.load < 9 end },
--    function(g) g.widget.image = -- image(beautiful.battery_low_icon) end) --
-- --

It is important to carefully choose the correct trigger for the -- event to be raised. The LatchTrigger object used here -- will make the event happen only at the moment the load gets under 9 -- percents. If the percentage of load remains under 9, the event will -- not be triggered again. If a raw Trigger object had been used, the -- event would be triggered again at each provider's refresh.

-- --

If a repeated refresh can be considered as a minor optimisation -- issue here, it can become a really annoying problem, like in the -- following sample where naughty is used to be notified if the -- same situation.

-- --
-- g.add_event(
--    flaw.event.LatchTriger:new{ -- condition = function(d) return d.load < 9 end },
--    function(g) naughty.notify{
--       title = "Battery Warning",
--       text = "Battery low! " .. g.provider.data.load .. "% left!",
--       timeout = 5,
--       position = "top_right",
--       fg = beautiful.fg_focus,
--       bg = beautiful.bg_focus} end) --
-- --

Here is another example which introduces the EdgeTrigger. This trigger is -- activated when the condition's state change, that is if it becomes -- true or if it becomes false. Since Lua objects can be -- extended at will, the following snippet also moves the refresh -- action content into a my_update object's member. This -- way, behaviour which is common to multiple events can be written -- only once.

-- --
-- bg = flaw.gadget.icon.battery('BAT0',
--    {
--       my_icons = {
--          image( -- beautiful.icon_battery_low ),
--          image( -- beautiful.icon_battery_mid ),
--          image( -- beautiful.icon_battery_full )
--       },
--       my_load_icon = image( -- beautiful.icon_battery_plugged ),
--       my_update = function(self)
--          if -- self.provider.data.st_symbol == flaw.battery.STATUS_CHARGING then
--             self.widget.image -- = self.my_load_icon
--          else
--             self.widget.image -- = self.my_icons[math.floor(self.provider.data.load / 30) + 1]
--          end
--       end
--    }, {
--       image -- = image(beautiful.icon_battery_full)
--    } )
--
bg:add_event(
--    flaw.event.EdgeTrigger:new{ -- condition = function(d) return d.load < 60 end },
--    function (g) g:my_update() end )
-- bg:add_event(
--    flaw.event.EdgeTrigger:new{ -- condition = function(d) return d.load < 30 end },
--    function (g) g:my_update() end )
-- bg:add_event(
--    flaw.event.EdgeTrigger:new{
--       condition -- = function(d) return d.st_symbol == flaw.battery.STATUS_CHARGING end },
--    function (g) g:my_update() end )
--
-- --

This module provides the different Trigger prototypes.

-- -- -- @author David Soulayrol <david.soulayrol AT gmail DOT com> -- @copyright 2009,2011 David Soulayrol module('flaw.event') --- The simple trigger prototype. -- --

The simple trigger starts the event it belongs to each time the -- condition is successfully checked.

-- -- @class table -- @name Trigger -- @field condition the boolean computing routine, normally provided -- by the user, which decides if the event should be started. Trigger = { condition = function() return true end } --- The simple trigger constructor. -- -- @param o a table, which should at least contain the condition -- function at the index condition. -- @return the fresh new trigger. function Trigger:new(o) o = o or {} setmetatable(o, self) self.__index = self return o end --- Event trigger using the raw condition. -- -- @param data the provider data. function Trigger:test(data) return self.condition(data) end --- The latch trigger prototype. -- --

The latch trigger starts the event it belongs to only when the -- condition becomes true. That is, while the condition remains true, -- the event is started exactly once. It will be started again only if -- the condition becomes false again and then true.

-- -- @class table -- @name LatchTrigger -- @field condition the boolean computing routine, normally provided -- by the user, which decides if the event should be started. -- @field status the trigger memory. LatchTrigger = Trigger:new{ status = false } --- Event trigger using a bistable mechanism around the raw condition. -- -- @param data the provider data. function LatchTrigger:test(data) local old_status = self.status self.status = self.condition(data) return not old_status and self.status end --- The edge trigger prototype. -- --

The edge trigger starts the event it belongs to only when the -- condition result changes. That is, the event is started each time -- the condition becomes true or becomes false.

-- -- @class table -- @name EdgeTrigger -- @field condition the boolean computing routine, normally provided -- by the user, which decides if the event should be started. -- @field status the trigger memory. EdgeTrigger = Trigger:new{ status = false } --- Event trigger using edge detection of the raw condition results. -- -- @param data the provider data. function EdgeTrigger:test(data) local old_status = self.status self.status = self.condition(data) return old_status and not self.status or not old_status and self.status end