Debian Codegraph @

flaw/event.lua

raw symbols SCIP
1 -- flaw, a Lua OO management framework for Awesome WM widgets.
2 -- Copyright (C) 2009,2011 David Soulayrol <david.soulayrol AT gmail DOT net>
3
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of the GNU General Public License as published by
6 -- the Free Software Foundation, either version 3 of the License, or
7 -- (at your option) any later version.
8
9 -- This program is distributed in the hope that it will be useful,
10 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
11 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 -- GNU General Public License for more details.
13
14 -- You should have received a copy of the GNU General Public License
15 -- along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 -- Grab environment.
19 local setmetatable = setmetatable
20
21
22 --- Core events objects.
23 --
24 -- <p>Events are a way for the user to modify the gadget behaviour or
25 -- properties when certain conditions are met. An event is composed of
26 -- a trigger, which computes the condition, and an action.</p>
27 --
28 -- <h2>Trigger and Action</h2>
29 --
30 -- <p>The trigger is an object which takes a condition and provides
31 -- some status depending of its type. The trigger is tested each time
32 -- the gadget's provider refreshes its data, and the action called if
33 -- the event fires. There are three kind of triggers; the simple <a
34 -- href='#Trigger'><code>Trigger</code></a>, the <a
35 -- href='#LatchTrigger'><code>LatchTrigger</code></a> and the <a
36 -- href='#EdgeTrigger'><code>EdgeTrigger</code></a> described
37 -- below. All depend on the condition function but fire the event
38 -- differently, depending on their previous status.</p>
39 --
40 -- <p>The condition function takes one parameter which is the data
41 -- table updated by the provider. This table layout depends on the
42 -- provider and is detailed in the provider documentation. The
43 -- condition function shall return true if its conditions are met, or
44 -- false otherwise. The event will then fire depending on its
45 -- type.</p>
46 --
47 -- <p>The action function takes the gadget as parameter. It can do
48 -- absolutely anything, and its return value has no meaning for
49 -- <b>flaw</b>.</p>
50 --
51 -- <h2>Usage</h2>
52 --
53 -- <p>Condition and action are written by the user. Events must be
54 -- registered to a gadget, which in turn registers the trigger to its
55 -- provider.</p>
56 --
57 -- <div class='example'>
58 -- t = flaw.event.LatchTrigger:new{<br/>
59 -- &nbsp;&nbsp;&nbsp;condition = function(d) return d.load &lt; 25 end },<br/>
60 -- a = function(g) g.pattern = '&lt;bg color="#ff6565"/&gt;$load%' end<br/>
61 -- gadget:add_event(e, a)<br/>
62 -- </div>
63 --
64 -- <p>Assuming <code>g</code> is a battery icon gadget created as
65 -- above and <code>battery_low_icon</code> a beautiful property
66 -- holding the path to an explicit low battery icon, here is a short
67 -- snippet to change the icon in a gadget when the battery load gets
68 -- really low.</p>
69 --
70 -- <div class='example'>
71 -- g.add_event(<br/>
72 -- &nbsp;&nbsp;&nbsp;flaw.event.LatchTriger:new{
73 -- condition = function(d) return d.load &lt; 9 end },<br/>
74 -- &nbsp;&nbsp;&nbsp;function(g) g.widget.image =
75 -- image(beautiful.battery_low_icon) end)
76 -- </div>
77 --
78 -- <p>It is important to carefully choose the correct trigger for the
79 -- event to be raised. The <a
80 -- href='#LatchTrigger'><code>LatchTrigger</code></a> object used here
81 -- will make the event happen only at the moment the load gets under 9
82 -- percents. If the percentage of load remains under 9, the event will
83 -- not be triggered again. If a raw <a
84 -- href='#Trigger'><code>Trigger</code></a> object had been used, the
85 -- event would be triggered again at each provider's refresh.</p>
86 --
87 -- <p> If a repeated refresh can be considered as a minor optimisation
88 -- issue here, it can become a really annoying problem, like in the
89 -- following sample where <b>naughty</b> is used to be notified if the
90 -- same situation.</p>
91 --
92 -- <div class='example'>
93 -- g.add_event(<br/>
94 -- &nbsp;&nbsp;&nbsp;flaw.event.LatchTriger:new{
95 -- condition = function(d) return d.load &lt; 9 end },<br/>
96 -- &nbsp;&nbsp;&nbsp;function(g) naughty.notify{<br/>
97 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;title = "Battery Warning",<br/>
98 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text = "Battery low! " .. g.provider.data.load .. "% left!",<br/>
99 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeout = 5,<br/>
100 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;position = "top_right",<br/>
101 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fg = beautiful.fg_focus,<br/>
102 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bg = beautiful.bg_focus} end)
103 -- </div>
104 --
105 -- <p>Here is another example which introduces the <a
106 -- href='#EdgeTrigger'><code>EdgeTrigger</code></a>. This trigger is
107 -- activated when the condition's state change, that is if it becomes
108 -- true or if it becomes false. Since <b>Lua</b> objects can be
109 -- extended at will, the following snippet also moves the refresh
110 -- action content into a <code>my_update</code> object's member. This
111 -- way, behaviour which is common to multiple events can be written
112 -- only once.</p>
113 --
114 -- <div class='example'>
115 -- bg = flaw.gadget.icon.battery('BAT0',<br/>
116 -- &nbsp;&nbsp;&nbsp;{<br/>
117 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_icons = {<br/>
118 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image(
119 -- beautiful.icon_battery_low ),<br/>
120 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image(
121 -- beautiful.icon_battery_mid ),<br/>
122 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image(
123 -- beautiful.icon_battery_full )<br/>
124 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},<br/>
125 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_load_icon = image(
126 -- beautiful.icon_battery_plugged ),<br/>
127 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my_update = function(self)<br/>
128 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if
129 -- self.provider.data.st_symbol == flaw.battery.STATUS_CHARGING then<br/>
130 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.widget.image
131 -- = self.my_load_icon<br/>
132 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br/>
133 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.widget.image
134 -- = self.my_icons[math.floor(self.provider.data.load / 30) + 1]<br/>
135 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br/>
136 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br/>
137 -- &nbsp;&nbsp;&nbsp;}, {<br/>
138 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image
139 -- = image(beautiful.icon_battery_full)<br/>
140 -- &nbsp;&nbsp;&nbsp;} )<br/>
141 -- <br/>bg:add_event(<br/>
142 -- &nbsp;&nbsp;&nbsp;flaw.event.EdgeTrigger:new{
143 -- condition = function(d) return d.load &lt; 60 end },<br/>
144 -- &nbsp;&nbsp;&nbsp;function (g) g:my_update() end )<br/>
145 -- bg:add_event(<br/>
146 -- &nbsp;&nbsp;&nbsp;flaw.event.EdgeTrigger:new{
147 -- condition = function(d) return d.load &lt; 30 end },<br/>
148 -- &nbsp;&nbsp;&nbsp;function (g) g:my_update() end )<br/>
149 -- bg:add_event(<br/>
150 -- &nbsp;&nbsp;&nbsp;flaw.event.EdgeTrigger:new{<br/>
151 -- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;condition
152 -- = function(d) return d.st_symbol == flaw.battery.STATUS_CHARGING end },<br/>
153 -- &nbsp;&nbsp;&nbsp;function (g) g:my_update() end )<br/>
154 -- </div>
155 --
156 -- <p>This module provides the different Trigger prototypes.</p>
157 --
158 --
159 -- @author David Soulayrol &lt;david.soulayrol AT gmail DOT com&gt;
160 -- @copyright 2009,2011 David Soulayrol
161 module('flaw.event')
162
163
164 --- The simple trigger prototype.
165 --
166 -- <p>The simple trigger starts the event it belongs to each time the
167 -- condition is successfully checked.</p>
168 --
169 -- @class table
170 -- @name Trigger
171 -- @field condition the boolean computing routine, normally provided
172 -- by the user, which decides if the event should be started.
173 Trigger = { condition = function() return true end }
174
175 --- The simple trigger constructor.
176 --
177 -- @param o a table, which should at least contain the condition
178 -- function at the index <code>condition</code>.
179 -- @return the fresh new trigger.
180 function Trigger:new(o)
181 o = o or {}
182 setmetatable(o, self)
183 self.__index = self
184 return o
185 end
186
187 --- Event trigger using the raw condition.
188 --
189 -- @param data the provider data.
190 function Trigger:test(data)
191 return self.condition(data)
192 end
193
194 --- The latch trigger prototype.
195 --
196 -- <p>The latch trigger starts the event it belongs to only when the
197 -- condition becomes true. That is, while the condition remains true,
198 -- the event is started exactly once. It will be started again only if
199 -- the condition becomes false again and then true.</p>
200 --
201 -- @class table
202 -- @name LatchTrigger
203 -- @field condition the boolean computing routine, normally provided
204 -- by the user, which decides if the event should be started.
205 -- @field status the trigger memory.
206 LatchTrigger = Trigger:new{ status = false }
207
208 --- Event trigger using a bistable mechanism around the raw condition.
209 --
210 -- @param data the provider data.
211 function LatchTrigger:test(data)
212 local old_status = self.status
213 self.status = self.condition(data)
214 return not old_status and self.status
215 end
216
217 --- The edge trigger prototype.
218 --
219 -- <p>The edge trigger starts the event it belongs to only when the
220 -- condition result changes. That is, the event is started each time
221 -- the condition becomes true or becomes false.</p>
222 --
223 -- @class table
224 -- @name EdgeTrigger
225 -- @field condition the boolean computing routine, normally provided
226 -- by the user, which decides if the event should be started.
227 -- @field status the trigger memory.
228 EdgeTrigger = Trigger:new{ status = false }
229
230 --- Event trigger using edge detection of the raw condition results.
231 --
232 -- @param data the provider data.
233 function EdgeTrigger:test(data)
234 local old_status = self.status
235 self.status = self.condition(data)
236 return old_status and not self.status or not old_status and self.status
237 end