Struct show_image::ContextProxy
source · [−]pub struct ContextProxy { /* private fields */ }
Expand description
Proxy object to interact with the global context from a user thread.
You should not use proxy objects from withing the global context thread. The proxy objects often wait for the global context to perform some action. Doing so from within the global context thread would cause a deadlock.
Implementations
sourceimpl ContextProxy
impl ContextProxy
sourcepub fn add_event_handler<F>(&self, handler: F) where
F: FnMut(&mut ContextHandle<'_>, &mut Event, &mut EventHandlerControlFlow) + Send + 'static,
pub fn add_event_handler<F>(&self, handler: F) where
F: FnMut(&mut ContextHandle<'_>, &mut Event, &mut EventHandlerControlFlow) + Send + 'static,
Add a global event handler to the context.
Events that are already queued with the event loop will not be passed to the handler.
This function uses Self::run_function_wait
internally, so it blocks until the event handler is added.
To avoid blocking, you can use Self::run_function
to post a lambda that adds an error handler instead.
Panics
This function will panic if called from within the context thread.
sourcepub fn add_window_event_handler<F>(
&self,
window_id: WindowId,
handler: F
) -> Result<(), InvalidWindowId> where
F: FnMut(WindowHandle<'_>, &mut WindowEvent, &mut EventHandlerControlFlow) + Send + 'static,
pub fn add_window_event_handler<F>(
&self,
window_id: WindowId,
handler: F
) -> Result<(), InvalidWindowId> where
F: FnMut(WindowHandle<'_>, &mut WindowEvent, &mut EventHandlerControlFlow) + Send + 'static,
Add an event handler for a specific window.
Events that are already queued with the event loop will not be passed to the handler.
This function uses Self::run_function_wait
internally, so it blocks until the event handler is added.
To avoid blocking, you can use Self::run_function
to post a lambda that adds an error handler instead.
Panics
This function will panic if called from within the context thread.
sourcepub fn run_function<F>(&self, function: F) where
F: 'static + FnOnce(&mut ContextHandle<'_>) + Send,
pub fn run_function<F>(&self, function: F) where
F: 'static + FnOnce(&mut ContextHandle<'_>) + Send,
Post a function for execution in the context thread without waiting for it to execute.
This function returns immediately, without waiting for the posted function to start or complete.
If you want to get a return value back from the function, use Self::run_function_wait
instead.
Note:
You should not post functions to the context thread that block for a long time.
Doing so will block the event loop and will make the windows unresponsive until the event loop can continue.
Consider using Self::run_background_task
for long blocking tasks instead.
Panics
This function will panic if called from within the context thread.
sourcepub fn run_function_wait<F, T>(&self, function: F) -> T where
F: FnOnce(&mut ContextHandle<'_>) -> T + Send + 'static,
T: Send + 'static,
pub fn run_function_wait<F, T>(&self, function: F) -> T where
F: FnOnce(&mut ContextHandle<'_>) -> T + Send + 'static,
T: Send + 'static,
Post a function for execution in the context thread and wait for the return value.
If you do not need a return value from the posted function,
you can use Self::run_function
to avoid blocking the calling thread until it completes.
Note:
You should not post functions to the context thread that block for a long time.
Doing so will block the event loop and will make the windows unresponsive until the event loop can continue.
Consider using Self::run_background_task
for long blocking tasks instead.
Panics
This function will panic if called from within the context thread.
sourcepub fn run_background_task<F>(&self, task: F) where
F: FnOnce() + Send + 'static,
pub fn run_background_task<F>(&self, task: F) where
F: FnOnce() + Send + 'static,
Run a task in a background thread and register it with the context.
The task will be executed in a different thread than the context. Currently, each task is spawned in a separate thread. In the future, tasks may be run in a dedicated thread pool.
The background task will be joined before the process is terminated when you use Self::exit()
or one of the other exit functions of this crate.
sourcepub fn event_channel(&self) -> Receiver<Event>
pub fn event_channel(&self) -> Receiver<Event>
Create a channel that receives events from the context.
To close the channel, simply drop de receiver.
Warning: The created channel blocks when you request an event until one is available. You should never use the receiver from within an event handler or a function posted to the global context thread. Doing so would cause a deadlock.
Panics
This function will panic if called from within the context thread.
sourcepub fn window_event_channel(
&self,
window_id: WindowId
) -> Result<Receiver<WindowEvent>, InvalidWindowId>
pub fn window_event_channel(
&self,
window_id: WindowId
) -> Result<Receiver<WindowEvent>, InvalidWindowId>
Create a channel that receives events from a window.
To close the channel, simply drop de receiver. The channel is closed automatically when the window is destroyed.
Warning: The created channel blocks when you request an event until one is available. You should never use the receiver from within an event handler or a function posted to the global context thread. Doing so would cause a deadlock.
Panics
This function will panic if called from within the context thread.
sourcepub fn exit(&self, code: i32) -> !
pub fn exit(&self, code: i32) -> !
Join all background tasks and then exit the process.
If you use std::process::exit
, running background tasks may be killed.
To ensure no data loss occurs, you should use this function instead.
Background tasks are spawned when an image is saved through the built-in Ctrl+S or Ctrl+Shift+S shortcut, or by user code.
Panics
This function will panic if called from within the context thread.
Trait Implementations
sourceimpl Clone for ContextProxy
impl Clone for ContextProxy
sourcefn clone(&self) -> ContextProxy
fn clone(&self) -> ContextProxy
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl !RefUnwindSafe for ContextProxy
impl Send for ContextProxy
impl !Sync for ContextProxy
impl Unpin for ContextProxy
impl !UnwindSafe for ContextProxy
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more