use crate::sys;
use super::DrawListMut;
#[must_use]
pub struct DrawListTextureToken<'draw_list, 'tex> {
scope: crate::scope::NativeScopeToken<'draw_list>,
_texture: std::marker::PhantomData<&'tex ()>,
}
impl<'draw_list, 'tex> DrawListTextureToken<'draw_list, 'tex> {
fn new(
ui: &'draw_list crate::Ui,
draw_list: *mut sys::ImDrawList,
window_scoped: bool,
) -> Self {
Self {
scope: ui.begin_native_scope(
crate::scope::NativeScopePop::DrawListPopTexture {
draw_list,
window_scoped,
},
"DrawListTextureToken",
),
_texture: std::marker::PhantomData,
}
}
#[doc(alias = "PopTexture")]
pub fn pop(self) {}
#[doc(alias = "PopTexture")]
pub fn end(self) {}
}
impl Drop for DrawListTextureToken<'_, '_> {
fn drop(&mut self) {
self.scope.finish();
}
}
impl<'ui> DrawListMut<'ui> {
#[doc(alias = "PushTexture")]
pub fn push_texture<'tex>(
&self,
texture: impl Into<crate::texture::TextureRef<'tex>>,
) -> DrawListTextureToken<'_, 'tex> {
let texture = texture.into();
self.assert_scope("DrawListMut::push_texture()");
self.ui().run_with_bound_context(|| {
let tex_ref = self
.ui()
.resolve_texture_ref(texture)
.unwrap_or_else(|error| {
panic!("DrawListMut::push_texture() rejected texture: {error}")
});
unsafe { sys::ImDrawList_PushTexture(self.draw_list, tex_ref) };
});
DrawListTextureToken::new(self.ui(), self.draw_list, self.is_window_scoped())
}
#[doc(alias = "PushTexture", alias = "PopTexture")]
pub fn with_texture<'tex, R>(
&self,
texture: impl Into<crate::texture::TextureRef<'tex>>,
f: impl FnOnce() -> R,
) -> R {
let texture = self.push_texture(texture);
let result = f();
drop(texture);
result
}
}