Skip to main content

EventLoop

Struct EventLoop 

Source
pub struct EventLoop { /* private fields */ }

Implementations§

Source§

impl EventLoop

Source

pub fn new() -> Result<EventLoop>

Examples found in repository?
examples/basic.rs (line 80)
79fn main() {
80    let event_loop = EventLoop::new().unwrap();
81
82    let state = Rc::new(RefCell::new(State {
83        event_loop: event_loop.clone(),
84        window: None,
85        framebuffer: Vec::new(),
86        width: 0,
87        height: 0,
88        timer: None,
89    }));
90
91    let window = WindowOptions::new()
92        .title("window")
93        .size(Size::new(512.0, 512.0))
94        .open(&event_loop, {
95            let state = Rc::downgrade(&state);
96            move |event| state.upgrade().unwrap().borrow_mut().handle_event(event)
97        })
98        .unwrap();
99
100    window.show();
101
102    state.borrow_mut().window = Some(window);
103
104    state.borrow_mut().timer = Some(
105        Timer::repeat(&event_loop, Duration::from_millis(1000), || {
106            println!("timer")
107        })
108        .unwrap(),
109    );
110
111    event_loop.run().unwrap();
112}
More examples
Hide additional examples
examples/child_window.rs (line 64)
63fn main() {
64    let parent_event_loop = EventLoop::new().unwrap();
65
66    let parent_state = Rc::new(RefCell::new(ParentState {
67        event_loop: parent_event_loop.clone(),
68        framebuffer: Vec::new(),
69        window: None,
70    }));
71
72    let window = WindowOptions::new()
73        .title("parent window")
74        .size(Size::new(512.0, 512.0))
75        .open(&parent_event_loop, {
76            let state = Rc::downgrade(&parent_state);
77            move |event| state.upgrade().unwrap().borrow_mut().handle_event(event)
78        })
79        .unwrap();
80
81    window.show();
82
83    let parent_window_raw = window.as_raw().unwrap();
84    parent_state.borrow_mut().window = Some(window);
85
86    let child_event_loop = EventLoopOptions::new().mode(EventLoopMode::Guest).build().unwrap();
87
88    let child_state = Rc::new(RefCell::new(ChildState {
89        framebuffer: Vec::new(),
90        window: None,
91    }));
92
93    let mut window_opts = WindowOptions::new();
94    unsafe {
95        window_opts.raw_parent(parent_window_raw);
96    }
97
98    let window = window_opts
99        .position(Point::new(128.0, 128.0))
100        .size(Size::new(256.0, 256.0))
101        .open(&child_event_loop, {
102            let state = Rc::downgrade(&child_state);
103            move |event| state.upgrade().unwrap().borrow_mut().handle_event(event)
104        })
105        .unwrap();
106
107    window.show();
108
109    child_state.borrow_mut().window = Some(window);
110
111    parent_event_loop.run().unwrap();
112
113    child_state.borrow_mut().window = None;
114
115    parent_state.borrow_mut().window = None;
116}
Source

pub fn run(&self) -> Result<()>

Examples found in repository?
examples/basic.rs (line 111)
79fn main() {
80    let event_loop = EventLoop::new().unwrap();
81
82    let state = Rc::new(RefCell::new(State {
83        event_loop: event_loop.clone(),
84        window: None,
85        framebuffer: Vec::new(),
86        width: 0,
87        height: 0,
88        timer: None,
89    }));
90
91    let window = WindowOptions::new()
92        .title("window")
93        .size(Size::new(512.0, 512.0))
94        .open(&event_loop, {
95            let state = Rc::downgrade(&state);
96            move |event| state.upgrade().unwrap().borrow_mut().handle_event(event)
97        })
98        .unwrap();
99
100    window.show();
101
102    state.borrow_mut().window = Some(window);
103
104    state.borrow_mut().timer = Some(
105        Timer::repeat(&event_loop, Duration::from_millis(1000), || {
106            println!("timer")
107        })
108        .unwrap(),
109    );
110
111    event_loop.run().unwrap();
112}
More examples
Hide additional examples
examples/child_window.rs (line 111)
63fn main() {
64    let parent_event_loop = EventLoop::new().unwrap();
65
66    let parent_state = Rc::new(RefCell::new(ParentState {
67        event_loop: parent_event_loop.clone(),
68        framebuffer: Vec::new(),
69        window: None,
70    }));
71
72    let window = WindowOptions::new()
73        .title("parent window")
74        .size(Size::new(512.0, 512.0))
75        .open(&parent_event_loop, {
76            let state = Rc::downgrade(&parent_state);
77            move |event| state.upgrade().unwrap().borrow_mut().handle_event(event)
78        })
79        .unwrap();
80
81    window.show();
82
83    let parent_window_raw = window.as_raw().unwrap();
84    parent_state.borrow_mut().window = Some(window);
85
86    let child_event_loop = EventLoopOptions::new().mode(EventLoopMode::Guest).build().unwrap();
87
88    let child_state = Rc::new(RefCell::new(ChildState {
89        framebuffer: Vec::new(),
90        window: None,
91    }));
92
93    let mut window_opts = WindowOptions::new();
94    unsafe {
95        window_opts.raw_parent(parent_window_raw);
96    }
97
98    let window = window_opts
99        .position(Point::new(128.0, 128.0))
100        .size(Size::new(256.0, 256.0))
101        .open(&child_event_loop, {
102            let state = Rc::downgrade(&child_state);
103            move |event| state.upgrade().unwrap().borrow_mut().handle_event(event)
104        })
105        .unwrap();
106
107    window.show();
108
109    child_state.borrow_mut().window = Some(window);
110
111    parent_event_loop.run().unwrap();
112
113    child_state.borrow_mut().window = None;
114
115    parent_state.borrow_mut().window = None;
116}
Source

pub fn poll(&self) -> Result<()>

Source

pub fn exit(&self)

Examples found in repository?
examples/child_window.rs (line 29)
16    fn handle_event(&mut self, event: Event) -> Response {
17        match event {
18            Event::Frame => {
19                let window = &self.window.as_ref().unwrap();
20
21                let scale = window.scale();
22                let size = window.size();
23                let width = (scale * size.width) as usize;
24                let height = (scale * size.height) as usize;
25                self.framebuffer.resize(width * height, 0xFF00FFFF);
26                window.present(Bitmap::new(&self.framebuffer, width, height));
27            }
28            Event::Close => {
29                self.event_loop.exit();
30            }
31            _ => {}
32        }
33
34        Response::Ignore
35    }
More examples
Hide additional examples
examples/basic.rs (line 71)
26    fn handle_event(&mut self, event: Event) -> Response {
27        match event {
28            Event::Expose(rects) => {
29                println!("expose: {:?}", rects);
30            }
31            Event::Frame => {
32                println!("frame");
33
34                let window = self.window.as_ref().unwrap();
35
36                let scale = window.scale();
37                self.width = (WIDTH as f64 * scale) as usize;
38                self.height = (HEIGHT as f64 * scale) as usize;
39                self.framebuffer.resize(self.width * self.height, 0xFFFF00FF);
40
41                window.present(Bitmap::new(&self.framebuffer, self.width, self.height));
42            }
43            Event::GainFocus => {
44                println!("gain focus");
45            }
46            Event::LoseFocus => {
47                println!("lose focus");
48            }
49            Event::MouseEnter => {
50                println!("mouse enter");
51            }
52            Event::MouseExit => {
53                println!("mouse exit");
54            }
55            Event::MouseMove(pos) => {
56                println!("mouse move: {:?}", pos);
57            }
58            Event::MouseDown(btn) => {
59                println!("mouse down: {:?}", btn);
60                return Response::Capture;
61            }
62            Event::MouseUp(btn) => {
63                println!("mouse up: {:?}", btn);
64                return Response::Capture;
65            }
66            Event::Scroll(delta) => {
67                println!("scroll: {:?}", delta);
68                return Response::Capture;
69            }
70            Event::Close => {
71                self.event_loop.exit();
72            }
73        }
74
75        Response::Ignore
76    }

Trait Implementations§

Source§

impl AsRawFd for EventLoop

Available on Linux only.
Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Clone for EventLoop

Source§

fn clone(&self) -> EventLoop

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EventLoop

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.