source: appstream-generator/build/girepo/glib/Idle.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

File size: 6.2 KB
Line 
1/*
2 * Licensed under the GNU Lesser General Public License Version 3
3 *
4 * This library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser 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 software 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 Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18// generated automatically - do not change
19
20
21module glib.Idle;
22
23private import gi.glib;
24public  import gi.glibtypes;
25private import glib.Source;
26
27
28/** */
29public class Idle
30{
31        /** Holds all idle delegates */
32        bool delegate()[] idleListeners;
33        /** our idle ID */
34        uint idleID;
35       
36        /**
37         * Creates a new idle cycle.
38         * Params:
39         *      interval = the idle in milieconds
40         *      dlg = the delegate to be executed
41         *      fireNow = When true the delegate will be executed emmidiatly
42         */
43        this(bool delegate() dlg, bool fireNow=false)
44        {
45                idleListeners ~= dlg;
46                idleID = g_idle_add(cast(GSourceFunc)&idleCallback, cast(void*)this);
47                if ( fireNow )
48                {
49                        if ( !dlg() )
50                        {
51                                idleListeners.length = 0;
52                        }
53                }
54        }
55       
56        /**
57         * Creates a new idle cycle.
58         * Params:
59         *      dlg = the delegate to be executed
60         *      priority = Priority for the idle function
61         *      fireNow = When true the delegate will be executed emmidiatly
62         */
63        this(bool delegate() dlg, GPriority priority, bool fireNow=false)
64        {
65                idleListeners ~= dlg;
66                idleID = g_idle_add_full(priority, cast(GSourceFunc)&idleCallback, cast(void*)this, null);
67                if ( fireNow )
68                {
69                        if ( !dlg() )
70                        {
71                                idleListeners.length = 0;
72                        }
73                }
74        }
75       
76        /** */
77        public void stop()
78        {
79                if ( idleID > 0 )
80                {
81                        g_source_remove(idleID);
82                }
83                idleListeners.length = 0;
84        }
85       
86        /**
87         * Removes the idle from gtk
88         */
89        ~this()
90        {
91                stop();
92        }
93       
94        /**
95         * Adds a new delegate to this idle cycle
96         * Params:
97         *      dlg =
98         *      fireNow =
99         */
100        public void addListener(bool delegate() dlg, bool fireNow=false)
101        {
102                idleListeners ~= dlg;
103                if ( fireNow )
104                {
105                        if ( !dlg() )
106                        {
107                                idleListeners.length = idleListeners.length - 1;
108                        }
109                }
110        }
111       
112        /**
113         * The callback execution from glib
114         * Params:
115         *      idle =
116         * Returns:
117         */
118        extern(C) static bool idleCallback(Idle idle)
119        {
120                return idle.callAllListeners();
121        }
122       
123        /**
124         * Executes all delegates on the execution list
125         * Returns:
126         */
127        private bool callAllListeners()
128        {
129                bool runAgain = false;
130               
131                int i = 0;
132               
133                while ( i<idleListeners.length )
134                {
135                        if ( !idleListeners[i]() )
136                        {
137                                idleListeners = idleListeners[0..i] ~ idleListeners[i+1..idleListeners.length];
138                        }
139                        else
140                        {
141                                runAgain = true;
142                                ++i;
143                        }
144                }
145               
146                // Set idleID to 0 if all delegates are removed
147                if (idleListeners.length == 0)
148                        idleID = 0;
149               
150                return runAgain;
151        }
152
153        /**
154         */
155
156        /**
157         * Adds a function to be called whenever there are no higher priority
158         * events pending to the default main loop. The function is given the
159         * default idle priority, #G_PRIORITY_DEFAULT_IDLE.  If the function
160         * returns %FALSE it is automatically removed from the list of event
161         * sources and will not be called again.
162         *
163         * See [memory management of sources][mainloop-memory-management] for details
164         * on how to handle the return value and memory management of @data.
165         *
166         * This internally creates a main loop source using g_idle_source_new()
167         * and attaches it to the global #GMainContext using g_source_attach(), so
168         * the callback will be invoked in whichever thread is running that main
169         * context. You can do these steps manually if you need greater control or to
170         * use a custom main context.
171         *
172         * Params:
173         *     funct = function to call
174         *     data = data to pass to @function.
175         *
176         * Returns: the ID (greater than 0) of the event source.
177         */
178        public static uint add(GSourceFunc funct, void* data)
179        {
180                return g_idle_add(funct, data);
181        }
182
183        /**
184         * Adds a function to be called whenever there are no higher priority
185         * events pending.  If the function returns %FALSE it is automatically
186         * removed from the list of event sources and will not be called again.
187         *
188         * See [memory management of sources][mainloop-memory-management] for details
189         * on how to handle the return value and memory management of @data.
190         *
191         * This internally creates a main loop source using g_idle_source_new()
192         * and attaches it to the global #GMainContext using g_source_attach(), so
193         * the callback will be invoked in whichever thread is running that main
194         * context. You can do these steps manually if you need greater control or to
195         * use a custom main context.
196         *
197         * Params:
198         *     priority = the priority of the idle source. Typically this will be in the
199         *         range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
200         *     funct = function to call
201         *     data = data to pass to @function
202         *     notify = function to call when the idle is removed, or %NULL
203         *
204         * Returns: the ID (greater than 0) of the event source.
205         */
206        public static uint addFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify)
207        {
208                return g_idle_add_full(priority, funct, data, notify);
209        }
210
211        /**
212         * Removes the idle function with the given data.
213         *
214         * Params:
215         *     data = the data for the idle source's callback.
216         *
217         * Returns: %TRUE if an idle source was found and removed.
218         */
219        public static bool removeByData(void* data)
220        {
221                return g_idle_remove_by_data(data) != 0;
222        }
223
224        /**
225         * Creates a new idle source.
226         *
227         * The source will not initially be associated with any #GMainContext
228         * and must be added to one with g_source_attach() before it will be
229         * executed. Note that the default priority for idle sources is
230         * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
231         * have a default priority of %G_PRIORITY_DEFAULT.
232         *
233         * Returns: the newly-created idle source
234         */
235        public static Source sourceNew()
236        {
237                auto p = g_idle_source_new();
238               
239                if(p is null)
240                {
241                        return null;
242                }
243               
244                return new Source(cast(GSource*) p, true);
245        }
246}
Note: See TracBrowser for help on using the repository browser.