use std::mem;
use std::os::raw::{c_char, c_void};
use std::ptr;
use std::thread;
use crate::context::Context;
use crate::fonts::atlas::FontId;
use crate::internal::RawCast;
use crate::style::{StyleColor, StyleVar};
use crate::sys;
use crate::{Id, Ui};
impl<'ui> Ui<'ui> {
#[must_use]
pub fn push_font(&self, id: FontId) -> FontStackToken {
let fonts = self.fonts();
let font = fonts
.get_font(id)
.expect("Font atlas did not contain the given font");
unsafe { sys::igPushFont(font.raw() as *const _ as *mut _) };
FontStackToken { ctx: self.ctx }
}
#[must_use]
pub fn push_style_color(&self, style_color: StyleColor, color: [f32; 4]) -> ColorStackToken {
unsafe { sys::igPushStyleColor(style_color as i32, color.into()) };
ColorStackToken {
count: 1,
ctx: self.ctx,
}
}
#[must_use]
pub fn push_style_colors<'a, I>(&self, style_colors: I) -> ColorStackToken
where
I: IntoIterator<Item = &'a (StyleColor, [f32; 4])>,
{
let mut count = 0;
for &(style_color, color) in style_colors {
unsafe { sys::igPushStyleColor(style_color as i32, color.into()) };
count += 1;
}
ColorStackToken {
count,
ctx: self.ctx,
}
}
#[must_use]
pub fn push_style_var(&self, style_var: StyleVar) -> StyleStackToken {
unsafe { push_style_var(style_var) };
StyleStackToken {
count: 1,
ctx: self.ctx,
}
}
#[must_use]
pub fn push_style_vars<'a, I>(&self, style_vars: I) -> StyleStackToken
where
I: IntoIterator<Item = &'a StyleVar>,
{
let mut count = 0;
for &style_var in style_vars {
unsafe { push_style_var(style_var) };
count += 1;
}
StyleStackToken {
count,
ctx: self.ctx,
}
}
}
#[must_use]
pub struct FontStackToken {
ctx: *const Context,
}
impl FontStackToken {
pub fn pop(mut self, _: &Ui) {
self.ctx = ptr::null();
unsafe { sys::igPopFont() };
}
}
impl Drop for FontStackToken {
fn drop(&mut self) {
if !self.ctx.is_null() && !thread::panicking() {
panic!("A FontStackToken was leaked. Did you call .pop()?");
}
}
}
#[must_use]
pub struct ColorStackToken {
count: usize,
ctx: *const Context,
}
impl ColorStackToken {
pub fn pop(mut self, _: &Ui) {
self.ctx = ptr::null();
unsafe { sys::igPopStyleColor(self.count as i32) };
}
}
impl Drop for ColorStackToken {
fn drop(&mut self) {
if !self.ctx.is_null() && !thread::panicking() {
panic!("A ColorStackToken was leaked. Did you call .pop()?");
}
}
}
#[must_use]
pub struct StyleStackToken {
count: usize,
ctx: *const Context,
}
impl StyleStackToken {
pub fn pop(mut self, _: &Ui) {
self.ctx = ptr::null();
unsafe { sys::igPopStyleVar(self.count as i32) };
}
}
impl Drop for StyleStackToken {
fn drop(&mut self) {
if !self.ctx.is_null() && !thread::panicking() {
panic!("A StyleStackToken was leaked. Did you call .pop()?");
}
}
}
#[inline]
unsafe fn push_style_var(style_var: StyleVar) {
use crate::style::StyleVar::*;
use crate::sys::{igPushStyleVarFloat, igPushStyleVarVec2};
match style_var {
Alpha(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_Alpha as i32, v),
WindowPadding(v) => igPushStyleVarVec2(sys::ImGuiStyleVar_WindowPadding as i32, v.into()),
WindowRounding(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_WindowRounding as i32, v),
WindowBorderSize(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_WindowBorderSize as i32, v),
WindowMinSize(v) => igPushStyleVarVec2(sys::ImGuiStyleVar_WindowMinSize as i32, v.into()),
WindowTitleAlign(v) => {
igPushStyleVarVec2(sys::ImGuiStyleVar_WindowTitleAlign as i32, v.into())
}
ChildRounding(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_ChildRounding as i32, v),
ChildBorderSize(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_ChildBorderSize as i32, v),
PopupRounding(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_PopupRounding as i32, v),
PopupBorderSize(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_PopupBorderSize as i32, v),
FramePadding(v) => igPushStyleVarVec2(sys::ImGuiStyleVar_FramePadding as i32, v.into()),
FrameRounding(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_FrameRounding as i32, v),
FrameBorderSize(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_FrameBorderSize as i32, v),
ItemSpacing(v) => igPushStyleVarVec2(sys::ImGuiStyleVar_ItemSpacing as i32, v.into()),
ItemInnerSpacing(v) => {
igPushStyleVarVec2(sys::ImGuiStyleVar_ItemInnerSpacing as i32, v.into())
}
IndentSpacing(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_IndentSpacing as i32, v),
ScrollbarSize(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_ScrollbarSize as i32, v),
ScrollbarRounding(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_ScrollbarRounding as i32, v),
GrabMinSize(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_GrabMinSize as i32, v),
GrabRounding(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_GrabRounding as i32, v),
TabRounding(v) => igPushStyleVarFloat(sys::ImGuiStyleVar_TabRounding as i32, v),
ButtonTextAlign(v) => {
igPushStyleVarVec2(sys::ImGuiStyleVar_ButtonTextAlign as i32, v.into())
}
SelectableTextAlign(v) => {
igPushStyleVarVec2(sys::ImGuiStyleVar_SelectableTextAlign as i32, v.into())
}
}
}
impl<'ui> Ui<'ui> {
pub fn push_item_width(&self, item_width: f32) -> ItemWidthStackToken {
unsafe { sys::igPushItemWidth(item_width) };
ItemWidthStackToken { ctx: self.ctx }
}
pub fn set_next_item_width(&self, item_width: f32) {
unsafe { sys::igSetNextItemWidth(item_width) };
}
pub fn calc_item_width(&self) -> f32 {
unsafe { sys::igCalcItemWidth() }
}
pub fn push_text_wrap_pos(&self, wrap_pos_x: f32) -> TextWrapPosStackToken {
unsafe { sys::igPushTextWrapPos(wrap_pos_x) };
TextWrapPosStackToken { ctx: self.ctx }
}
pub fn push_item_flag(&self, item_flag: ItemFlag) -> ItemFlagsStackToken {
use self::ItemFlag::*;
match item_flag {
AllowKeyboardFocus(v) => unsafe { sys::igPushAllowKeyboardFocus(v) },
ButtonRepeat(v) => unsafe { sys::igPushButtonRepeat(v) },
}
ItemFlagsStackToken {
discriminant: mem::discriminant(&item_flag),
ctx: self.ctx,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ItemFlag {
AllowKeyboardFocus(bool),
ButtonRepeat(bool),
}
pub struct ItemWidthStackToken {
ctx: *const Context,
}
impl ItemWidthStackToken {
pub fn pop(mut self, _: &Ui) {
self.ctx = ptr::null();
unsafe { sys::igPopItemWidth() };
}
}
pub struct TextWrapPosStackToken {
ctx: *const Context,
}
impl TextWrapPosStackToken {
pub fn pop(mut self, _: &Ui) {
self.ctx = ptr::null();
unsafe { sys::igPopTextWrapPos() };
}
}
pub struct ItemFlagsStackToken {
discriminant: mem::Discriminant<ItemFlag>,
ctx: *const Context,
}
impl ItemFlagsStackToken {
pub fn pop(mut self, _: &Ui) {
self.ctx = ptr::null();
const ALLOW_KEYBOARD_FOCUS: ItemFlag = ItemFlag::AllowKeyboardFocus(true);
const BUTTON_REPEAT: ItemFlag = ItemFlag::ButtonRepeat(true);
if self.discriminant == mem::discriminant(&ALLOW_KEYBOARD_FOCUS) {
unsafe { sys::igPopAllowKeyboardFocus() };
} else if self.discriminant == mem::discriminant(&BUTTON_REPEAT) {
unsafe { sys::igPopButtonRepeat() };
} else {
unreachable!();
}
}
}
impl<'ui> Ui<'ui> {
#[must_use]
pub fn push_id<'a, I: Into<Id<'a>>>(&self, id: I) -> IdStackToken {
let id = id.into();
unsafe {
match id {
Id::Int(i) => sys::igPushIDInt(i),
Id::Str(s) => {
let start = s.as_ptr() as *const c_char;
let end = start.add(s.len());
sys::igPushIDRange(start, end)
}
Id::Ptr(p) => sys::igPushIDPtr(p as *const c_void),
}
}
IdStackToken { ctx: self.ctx }
}
}
#[must_use]
pub struct IdStackToken {
ctx: *const Context,
}
impl IdStackToken {
pub fn pop(mut self, _: &Ui) {
self.ctx = ptr::null();
unsafe { sys::igPopID() };
}
}
impl Drop for IdStackToken {
fn drop(&mut self) {
if !self.ctx.is_null() && !thread::panicking() {
panic!("A IdStackToken was leaked. Did you call .pop()?");
}
}
}