-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.lua
237 lines (222 loc) · 9.54 KB
/
event.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
-- flaw, a Lua OO management framework for Awesome WM widgets.
-- Copyright (C) 2009,2011 David Soulayrol <david.soulayrol AT gmail DOT net>
-- 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 <http://www.gnu.org/licenses/>.
-- Grab environment.
local setmetatable = setmetatable
--- Core events objects.
--
-- <p>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.</p>
--
-- <h2>Trigger and Action</h2>
--
-- <p>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 <a
-- href='#Trigger'><code>Trigger</code></a>, the <a
-- href='#LatchTrigger'><code>LatchTrigger</code></a> and the <a
-- href='#EdgeTrigger'><code>EdgeTrigger</code></a> described
-- below. All depend on the condition function but fire the event
-- differently, depending on their previous status.</p>
--
-- <p>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.</p>
--
-- <p>The action function takes the gadget as parameter. It can do
-- absolutely anything, and its return value has no meaning for
-- <b>flaw</b>.</p>
--
-- <h2>Usage</h2>
--
-- <p>Condition and action are written by the user. Events must be
-- registered to a gadget, which in turn registers the trigger to its
-- provider.</p>
--
-- <div class='example'>
-- t = flaw.event.LatchTrigger:new{<br/>
-- condition = function(d) return d.load < 25 end },<br/>
-- a = function(g) g.pattern = '<bg color="#ff6565"/>$load%' end<br/>
-- gadget:add_event(e, a)<br/>
-- </div>
--
-- <p>Assuming <code>g</code> is a battery icon gadget created as
-- above and <code>battery_low_icon</code> 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.</p>
--
-- <div class='example'>
-- g.add_event(<br/>
-- flaw.event.LatchTriger:new{
-- condition = function(d) return d.load < 9 end },<br/>
-- function(g) g.widget.image =
-- image(beautiful.battery_low_icon) end)
-- </div>
--
-- <p>It is important to carefully choose the correct trigger for the
-- event to be raised. The <a
-- href='#LatchTrigger'><code>LatchTrigger</code></a> 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 <a
-- href='#Trigger'><code>Trigger</code></a> object had been used, the
-- event would be triggered again at each provider's refresh.</p>
--
-- <p> 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 <b>naughty</b> is used to be notified if the
-- same situation.</p>
--
-- <div class='example'>
-- g.add_event(<br/>
-- flaw.event.LatchTriger:new{
-- condition = function(d) return d.load < 9 end },<br/>
-- function(g) naughty.notify{<br/>
-- title = "Battery Warning",<br/>
-- text = "Battery low! " .. g.provider.data.load .. "% left!",<br/>
-- timeout = 5,<br/>
-- position = "top_right",<br/>
-- fg = beautiful.fg_focus,<br/>
-- bg = beautiful.bg_focus} end)
-- </div>
--
-- <p>Here is another example which introduces the <a
-- href='#EdgeTrigger'><code>EdgeTrigger</code></a>. This trigger is
-- activated when the condition's state change, that is if it becomes
-- true or if it becomes false. Since <b>Lua</b> objects can be
-- extended at will, the following snippet also moves the refresh
-- action content into a <code>my_update</code> object's member. This
-- way, behaviour which is common to multiple events can be written
-- only once.</p>
--
-- <div class='example'>
-- bg = flaw.gadget.icon.battery('BAT0',<br/>
-- {<br/>
-- my_icons = {<br/>
-- image(
-- beautiful.icon_battery_low ),<br/>
-- image(
-- beautiful.icon_battery_mid ),<br/>
-- image(
-- beautiful.icon_battery_full )<br/>
-- },<br/>
-- my_load_icon = image(
-- beautiful.icon_battery_plugged ),<br/>
-- my_update = function(self)<br/>
-- if
-- self.provider.data.st_symbol == flaw.battery.STATUS_CHARGING then<br/>
-- self.widget.image
-- = self.my_load_icon<br/>
-- else<br/>
-- self.widget.image
-- = self.my_icons[math.floor(self.provider.data.load / 30) + 1]<br/>
-- end<br/>
-- end<br/>
-- }, {<br/>
-- image
-- = image(beautiful.icon_battery_full)<br/>
-- } )<br/>
-- <br/>bg:add_event(<br/>
-- flaw.event.EdgeTrigger:new{
-- condition = function(d) return d.load < 60 end },<br/>
-- function (g) g:my_update() end )<br/>
-- bg:add_event(<br/>
-- flaw.event.EdgeTrigger:new{
-- condition = function(d) return d.load < 30 end },<br/>
-- function (g) g:my_update() end )<br/>
-- bg:add_event(<br/>
-- flaw.event.EdgeTrigger:new{<br/>
-- condition
-- = function(d) return d.st_symbol == flaw.battery.STATUS_CHARGING end },<br/>
-- function (g) g:my_update() end )<br/>
-- </div>
--
-- <p>This module provides the different Trigger prototypes.</p>
--
--
-- @author David Soulayrol <david.soulayrol AT gmail DOT com>
-- @copyright 2009,2011 David Soulayrol
module('flaw.event')
--- The simple trigger prototype.
--
-- <p>The simple trigger starts the event it belongs to each time the
-- condition is successfully checked.</p>
--
-- @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 <code>condition</code>.
-- @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.
--
-- <p>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.</p>
--
-- @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.
--
-- <p>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.</p>
--
-- @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