glib/auto/main_loop.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, translate::*, MainContext};
6
7crate::wrapper! {
8 /// The `GMainLoop` struct is an opaque data type
9 /// representing the main event loop of a GLib or GTK application.
10 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
11 pub struct MainLoop(Shared<ffi::GMainLoop>);
12
13 match fn {
14 ref => |ptr| ffi::g_main_loop_ref(ptr),
15 unref => |ptr| ffi::g_main_loop_unref(ptr),
16 type_ => || ffi::g_main_loop_get_type(),
17 }
18}
19
20impl MainLoop {
21 /// Creates a new [`MainLoop`][crate::MainLoop] structure.
22 /// ## `context`
23 /// a #GMainContext (if [`None`], the global-default
24 /// main context will be used).
25 /// ## `is_running`
26 /// set to [`true`] to indicate that the loop is running. This
27 /// is not very important since calling [`run()`][Self::run()] will set this
28 /// to [`true`] anyway.
29 ///
30 /// # Returns
31 ///
32 /// a new #GMainLoop.
33 #[doc(alias = "g_main_loop_new")]
34 pub fn new(context: Option<&MainContext>, is_running: bool) -> MainLoop {
35 unsafe {
36 from_glib_full(ffi::g_main_loop_new(
37 context.to_glib_none().0,
38 is_running.into_glib(),
39 ))
40 }
41 }
42
43 /// Returns the [`MainContext`][crate::MainContext] of @self.
44 ///
45 /// # Returns
46 ///
47 /// the [`MainContext`][crate::MainContext] of @self
48 #[doc(alias = "g_main_loop_get_context")]
49 #[doc(alias = "get_context")]
50 pub fn context(&self) -> MainContext {
51 unsafe { from_glib_none(ffi::g_main_loop_get_context(self.to_glib_none().0)) }
52 }
53
54 /// Checks to see if the main loop is currently being run via
55 /// [`run()`][Self::run()].
56 ///
57 /// # Returns
58 ///
59 /// [`true`] if the mainloop is currently being run.
60 #[doc(alias = "g_main_loop_is_running")]
61 pub fn is_running(&self) -> bool {
62 unsafe { from_glib(ffi::g_main_loop_is_running(self.to_glib_none().0)) }
63 }
64
65 /// Stops a [`MainLoop`][crate::MainLoop] from running. Any calls to
66 /// [`run()`][Self::run()] for the loop will return.
67 ///
68 /// Note that sources that have already been dispatched when
69 /// [`quit()`][Self::quit()] is called will still be executed.
70 #[doc(alias = "g_main_loop_quit")]
71 pub fn quit(&self) {
72 unsafe {
73 ffi::g_main_loop_quit(self.to_glib_none().0);
74 }
75 }
76
77 /// Runs a main loop until [`quit()`][Self::quit()] is called on the loop.
78 /// If this is called for the thread of the loop's #GMainContext,
79 /// it will process events from the loop, otherwise it will
80 /// simply wait.
81 #[doc(alias = "g_main_loop_run")]
82 pub fn run(&self) {
83 unsafe {
84 ffi::g_main_loop_run(self.to_glib_none().0);
85 }
86 }
87}
88
89unsafe impl Send for MainLoop {}
90unsafe impl Sync for MainLoop {}