Builder constructors take &Ui instead

This commit is contained in:
Joonas Javanainen 2017-07-13 00:15:03 +03:00
parent a48f0bdbd9
commit 96fe2a8e15
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
12 changed files with 53 additions and 53 deletions

View File

@ -18,8 +18,8 @@
- Button, selectable, histogram, plotlines, and progress bar accept size with `Into<ImVec2>` - Button, selectable, histogram, plotlines, and progress bar accept size with `Into<ImVec2>`
- `ImString::new` always succeeds and any interior NULs truncate the string. **Breaking change** - `ImString::new` always succeeds and any interior NULs truncate the string. **Breaking change**
- All builder constructor functions (e.g. Window::new) are no longer public. - All builder constructor functions (e.g. Window::new) now take `&Ui` reference
The only safe way to construct builders is through a `&Ui` reference. to tie the lifetime of the builder to it.
### Deprecated ### Deprecated

View File

@ -18,7 +18,7 @@ pub struct ChildFrame<'ui, 'p> {
} }
impl<'ui, 'p> ChildFrame<'ui, 'p> { impl<'ui, 'p> ChildFrame<'ui, 'p> {
pub(crate) fn new<S: Into<ImVec2>>(name: &'p ImStr, size: S) -> ChildFrame<'ui, 'p> { pub fn new<S: Into<ImVec2>>(_: &Ui<'ui>, name: &'p ImStr, size: S) -> ChildFrame<'ui, 'p> {
ChildFrame { ChildFrame {
name, name,
size: size.into(), size: size.into(),

View File

@ -135,7 +135,7 @@ pub struct InputText<'ui, 'p> {
} }
impl<'ui, 'p> InputText<'ui, 'p> { impl<'ui, 'p> InputText<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, buf: &'p mut ImString) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, buf: &'p mut ImString) -> Self {
InputText { InputText {
label: label, label: label,
buf: buf, buf: buf,
@ -172,7 +172,7 @@ pub struct InputInt<'ui, 'p> {
} }
impl<'ui, 'p> InputInt<'ui, 'p> { impl<'ui, 'p> InputInt<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, value: &'p mut i32) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut i32) -> Self {
InputInt { InputInt {
label: label, label: label,
value: value, value: value,
@ -209,7 +209,7 @@ pub struct InputFloat<'ui, 'p> {
} }
impl<'ui, 'p> InputFloat<'ui, 'p> { impl<'ui, 'p> InputFloat<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, value: &'p mut f32) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut f32) -> Self {
InputFloat { InputFloat {
label: label, label: label,
value: value, value: value,
@ -249,7 +249,7 @@ macro_rules! impl_input_floatn {
} }
impl<'ui, 'p> $InputFloatN<'ui, 'p> { impl<'ui, 'p> $InputFloatN<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, value: &'p mut [f32;$N]) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [f32;$N]) -> Self {
$InputFloatN { $InputFloatN {
label: label, label: label,
value: value, value: value,
@ -290,7 +290,7 @@ macro_rules! impl_input_intn {
} }
impl<'ui, 'p> $InputIntN<'ui, 'p> { impl<'ui, 'p> $InputIntN<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, value: &'p mut [i32;$N]) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [i32;$N]) -> Self {
$InputIntN { $InputIntN {
label: label, label: label,
value: value, value: value,
@ -325,7 +325,7 @@ pub struct ColorEdit3<'ui, 'p> {
} }
impl<'ui, 'p> ColorEdit3<'ui, 'p> { impl<'ui, 'p> ColorEdit3<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, value: &'p mut [f32; 3]) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [f32; 3]) -> Self {
ColorEdit3 { ColorEdit3 {
label: label, label: label,
value: value, value: value,
@ -347,7 +347,7 @@ pub struct ColorEdit4<'ui, 'p> {
} }
impl<'ui, 'p> ColorEdit4<'ui, 'p> { impl<'ui, 'p> ColorEdit4<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, value: &'p mut [f32; 4]) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [f32; 4]) -> Self {
ColorEdit4 { ColorEdit4 {
label: label, label: label,
value: value, value: value,

View File

@ -369,7 +369,7 @@ impl<'a> Ui<'a> {
// Window // Window
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
pub fn window<'p>(&self, name: &'p ImStr) -> Window<'ui, 'p> { Window::new(name) } pub fn window<'p>(&self, name: &'p ImStr) -> Window<'ui, 'p> { Window::new(self, name) }
} }
// Layout // Layout
@ -501,49 +501,49 @@ impl<'ui> Ui<'ui> {
label: &'p ImStr, label: &'p ImStr,
value: &'p mut [f32; 3]) value: &'p mut [f32; 3])
-> ColorEdit3<'ui, 'p> { -> ColorEdit3<'ui, 'p> {
ColorEdit3::new(label, value) ColorEdit3::new(self, label, value)
} }
pub fn color_edit4<'p>(&self, pub fn color_edit4<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
value: &'p mut [f32; 4]) value: &'p mut [f32; 4])
-> ColorEdit4<'ui, 'p> { -> ColorEdit4<'ui, 'p> {
ColorEdit4::new(label, value) ColorEdit4::new(self, label, value)
} }
pub fn input_text<'p>(&self, label: &'p ImStr, buf: &'p mut ImString) -> InputText<'ui, 'p> { pub fn input_text<'p>(&self, label: &'p ImStr, buf: &'p mut ImString) -> InputText<'ui, 'p> {
InputText::new(label, buf) InputText::new(self, label, buf)
} }
pub fn input_float<'p>(&self, label: &'p ImStr, value: &'p mut f32) -> InputFloat<'ui, 'p> { pub fn input_float<'p>(&self, label: &'p ImStr, value: &'p mut f32) -> InputFloat<'ui, 'p> {
InputFloat::new(label, value) InputFloat::new(self, label, value)
} }
pub fn input_float2<'p>(&self, pub fn input_float2<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
value: &'p mut [f32; 2]) value: &'p mut [f32; 2])
-> InputFloat2<'ui, 'p> { -> InputFloat2<'ui, 'p> {
InputFloat2::new(label, value) InputFloat2::new(self, label, value)
} }
pub fn input_float3<'p>(&self, pub fn input_float3<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
value: &'p mut [f32; 3]) value: &'p mut [f32; 3])
-> InputFloat3<'ui, 'p> { -> InputFloat3<'ui, 'p> {
InputFloat3::new(label, value) InputFloat3::new(self, label, value)
} }
pub fn input_float4<'p>(&self, pub fn input_float4<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
value: &'p mut [f32; 4]) value: &'p mut [f32; 4])
-> InputFloat4<'ui, 'p> { -> InputFloat4<'ui, 'p> {
InputFloat4::new(label, value) InputFloat4::new(self, label, value)
} }
pub fn input_int<'p>(&self, label: &'p ImStr, value: &'p mut i32) -> InputInt<'ui, 'p> { pub fn input_int<'p>(&self, label: &'p ImStr, value: &'p mut i32) -> InputInt<'ui, 'p> {
InputInt::new(label, value) InputInt::new(self, label, value)
} }
pub fn input_int2<'p>(&self, label: &'p ImStr, value: &'p mut [i32; 2]) -> InputInt2<'ui, 'p> { pub fn input_int2<'p>(&self, label: &'p ImStr, value: &'p mut [i32; 2]) -> InputInt2<'ui, 'p> {
InputInt2::new(label, value) InputInt2::new(self, label, value)
} }
pub fn input_int3<'p>(&self, label: &'p ImStr, value: &'p mut [i32; 3]) -> InputInt3<'ui, 'p> { pub fn input_int3<'p>(&self, label: &'p ImStr, value: &'p mut [i32; 3]) -> InputInt3<'ui, 'p> {
InputInt3::new(label, value) InputInt3::new(self, label, value)
} }
pub fn input_int4<'p>(&self, label: &'p ImStr, value: &'p mut [i32; 4]) -> InputInt4<'ui, 'p> { pub fn input_int4<'p>(&self, label: &'p ImStr, value: &'p mut [i32; 4]) -> InputInt4<'ui, 'p> {
InputInt4::new(label, value) InputInt4::new(self, label, value)
} }
} }
@ -555,7 +555,7 @@ impl<'ui> Ui<'ui> {
min: f32, min: f32,
max: f32) max: f32)
-> SliderFloat<'ui, 'p> { -> SliderFloat<'ui, 'p> {
SliderFloat::new(label, value, min, max) SliderFloat::new(self, label, value, min, max)
} }
pub fn slider_float2<'p>(&self, pub fn slider_float2<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
@ -563,7 +563,7 @@ impl<'ui> Ui<'ui> {
min: f32, min: f32,
max: f32) max: f32)
-> SliderFloat2<'ui, 'p> { -> SliderFloat2<'ui, 'p> {
SliderFloat2::new(label, value, min, max) SliderFloat2::new(self, label, value, min, max)
} }
pub fn slider_float3<'p>(&self, pub fn slider_float3<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
@ -571,7 +571,7 @@ impl<'ui> Ui<'ui> {
min: f32, min: f32,
max: f32) max: f32)
-> SliderFloat3<'ui, 'p> { -> SliderFloat3<'ui, 'p> {
SliderFloat3::new(label, value, min, max) SliderFloat3::new(self, label, value, min, max)
} }
pub fn slider_float4<'p>(&self, pub fn slider_float4<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
@ -579,7 +579,7 @@ impl<'ui> Ui<'ui> {
min: f32, min: f32,
max: f32) max: f32)
-> SliderFloat4<'ui, 'p> { -> SliderFloat4<'ui, 'p> {
SliderFloat4::new(label, value, min, max) SliderFloat4::new(self, label, value, min, max)
} }
pub fn slider_int<'p>(&self, pub fn slider_int<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
@ -587,7 +587,7 @@ impl<'ui> Ui<'ui> {
min: i32, min: i32,
max: i32) max: i32)
-> SliderInt<'ui, 'p> { -> SliderInt<'ui, 'p> {
SliderInt::new(label, value, min, max) SliderInt::new(self, label, value, min, max)
} }
pub fn slider_int2<'p>(&self, pub fn slider_int2<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
@ -595,7 +595,7 @@ impl<'ui> Ui<'ui> {
min: i32, min: i32,
max: i32) max: i32)
-> SliderInt2<'ui, 'p> { -> SliderInt2<'ui, 'p> {
SliderInt2::new(label, value, min, max) SliderInt2::new(self, label, value, min, max)
} }
pub fn slider_int3<'p>(&self, pub fn slider_int3<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
@ -603,7 +603,7 @@ impl<'ui> Ui<'ui> {
min: i32, min: i32,
max: i32) max: i32)
-> SliderInt3<'ui, 'p> { -> SliderInt3<'ui, 'p> {
SliderInt3::new(label, value, min, max) SliderInt3::new(self, label, value, min, max)
} }
pub fn slider_int4<'p>(&self, pub fn slider_int4<'p>(&self,
label: &'p ImStr, label: &'p ImStr,
@ -611,15 +611,15 @@ impl<'ui> Ui<'ui> {
min: i32, min: i32,
max: i32) max: i32)
-> SliderInt4<'ui, 'p> { -> SliderInt4<'ui, 'p> {
SliderInt4::new(label, value, min, max) SliderInt4::new(self, label, value, min, max)
} }
} }
// Widgets: Trees // Widgets: Trees
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
pub fn tree_node<'p>(&self, id: &'p ImStr) -> TreeNode<'ui, 'p> { TreeNode::new(id) } pub fn tree_node<'p>(&self, id: &'p ImStr) -> TreeNode<'ui, 'p> { TreeNode::new(self, id) }
pub fn collapsing_header<'p>(&self, label: &'p ImStr) -> CollapsingHeader<'ui, 'p> { pub fn collapsing_header<'p>(&self, label: &'p ImStr) -> CollapsingHeader<'ui, 'p> {
CollapsingHeader::new(label) CollapsingHeader::new(self, label)
} }
} }
@ -655,8 +655,8 @@ impl<'ui> Ui<'ui> {
unsafe { imgui_sys::igEndMenuBar() }; unsafe { imgui_sys::igEndMenuBar() };
} }
} }
pub fn menu<'p>(&self, label: &'p ImStr) -> Menu<'ui, 'p> { Menu::new(label) } pub fn menu<'p>(&self, label: &'p ImStr) -> Menu<'ui, 'p> { Menu::new(self, label) }
pub fn menu_item<'p>(&self, label: &'p ImStr) -> MenuItem<'ui, 'p> { MenuItem::new(label) } pub fn menu_item<'p>(&self, label: &'p ImStr) -> MenuItem<'ui, 'p> { MenuItem::new(self, label) }
} }
// Widgets: Popups // Widgets: Popups
@ -716,13 +716,13 @@ impl<'ui> Ui<'ui> {
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
pub fn plot_lines<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotLines<'ui, 'p> { pub fn plot_lines<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotLines<'ui, 'p> {
PlotLines::new(label, values) PlotLines::new(self, label, values)
} }
} }
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
pub fn plot_histogram<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotHistogram<'ui, 'p> { pub fn plot_histogram<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotHistogram<'ui, 'p> {
PlotHistogram::new(label, values) PlotHistogram::new(self, label, values)
} }
} }
@ -753,7 +753,7 @@ impl<'ui> Ui<'ui> {
/// .overlay_text(im_str!("Progress!")) /// .overlay_text(im_str!("Progress!"))
/// .build(); /// .build();
/// ``` /// ```
pub fn progress_bar<'p>(&self, fraction: f32) -> ProgressBar<'ui, 'p> { ProgressBar::new(fraction) } pub fn progress_bar<'p>(&self, fraction: f32) -> ProgressBar<'ui, 'p> { ProgressBar::new(self, fraction) }
} }
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
@ -777,7 +777,7 @@ impl<'ui> Ui<'ui> {
/// ui.text_colored((1.0, 0.0, 0.0, 1.0), im_str!("hello mate!")); /// ui.text_colored((1.0, 0.0, 0.0, 1.0), im_str!("hello mate!"));
/// }); /// });
/// }); /// });
pub fn child_frame<'p, S: Into<ImVec2>>(&self, name: &'p ImStr, size: S) -> ChildFrame<'ui, 'p> { ChildFrame::new(name, size.into()) } pub fn child_frame<'p, S: Into<ImVec2>>(&self, name: &'p ImStr, size: S) -> ChildFrame<'ui, 'p> { ChildFrame::new(self, name, size.into()) }
} }
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {

View File

@ -12,7 +12,7 @@ pub struct Menu<'ui, 'p> {
} }
impl<'ui, 'p> Menu<'ui, 'p> { impl<'ui, 'p> Menu<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr) -> Self {
Menu { Menu {
label: label, label: label,
enabled: true, enabled: true,
@ -43,7 +43,7 @@ pub struct MenuItem<'ui, 'p> {
} }
impl<'ui, 'p> MenuItem<'ui, 'p> { impl<'ui, 'p> MenuItem<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr) -> Self {
MenuItem { MenuItem {
label: label, label: label,
shortcut: None, shortcut: None,

View File

@ -18,7 +18,7 @@ pub struct PlotHistogram<'ui, 'p> {
} }
impl<'ui, 'p> PlotHistogram<'ui, 'p> { impl<'ui, 'p> PlotHistogram<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, values: &'p [f32]) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, values: &'p [f32]) -> Self {
PlotHistogram { PlotHistogram {
label: label, label: label,
values: values, values: values,

View File

@ -18,7 +18,7 @@ pub struct PlotLines<'ui, 'p> {
} }
impl<'ui, 'p> PlotLines<'ui, 'p> { impl<'ui, 'p> PlotLines<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, values: &'p [f32]) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, values: &'p [f32]) -> Self {
PlotLines { PlotLines {
label: label, label: label,
values: values, values: values,

View File

@ -20,7 +20,7 @@ impl<'ui, 'p> ProgressBar<'ui, 'p> {
/// The progress bar will be automatically sized to fill /// The progress bar will be automatically sized to fill
/// the entire width of the window if no custom size is /// the entire width of the window if no custom size is
/// specified. /// specified.
pub(crate) fn new(fraction: f32) -> Self { pub fn new(_: &Ui<'ui>, fraction: f32) -> Self {
ProgressBar { ProgressBar {
fraction: fraction, fraction: fraction,
size: ImVec2::new(-1.0, 0.0), size: ImVec2::new(-1.0, 0.0),

View File

@ -16,7 +16,7 @@ pub struct SliderInt<'ui, 'p> {
} }
impl<'ui, 'p> SliderInt<'ui, 'p> { impl<'ui, 'p> SliderInt<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, value: &'p mut i32, min: i32, max: i32) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut i32, min: i32, max: i32) -> Self {
SliderInt { SliderInt {
label: label, label: label,
value: value, value: value,
@ -55,7 +55,7 @@ macro_rules! impl_slider_intn {
} }
impl<'ui, 'p> $SliderIntN<'ui, 'p> { impl<'ui, 'p> $SliderIntN<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, value: &'p mut [i32; $N], min: i32, max: i32) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [i32; $N], min: i32, max: i32) -> Self {
$SliderIntN { $SliderIntN {
label: label, label: label,
value: value, value: value,
@ -100,7 +100,7 @@ pub struct SliderFloat<'ui, 'p> {
} }
impl<'ui, 'p> SliderFloat<'ui, 'p> { impl<'ui, 'p> SliderFloat<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, value: &'p mut f32, min: f32, max: f32) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut f32, min: f32, max: f32) -> Self {
SliderFloat { SliderFloat {
label: label, label: label,
value: value, value: value,
@ -147,7 +147,7 @@ macro_rules! impl_slider_floatn {
} }
impl<'ui, 'p> $SliderFloatN<'ui, 'p> { impl<'ui, 'p> $SliderFloatN<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr, value: &'p mut [f32; $N], min: f32, max: f32) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: &'p mut [f32; $N], min: f32, max: f32) -> Self {
$SliderFloatN { $SliderFloatN {
label: label, label: label,
value: value, value: value,

View File

@ -116,15 +116,15 @@ impl fmt::Debug for ImStr {
impl ImStr { impl ImStr {
#[deprecated(since = "0.0.15", note = "please use ImStr::from_bytes_with_nul_unchecked instead")] #[deprecated(since = "0.0.15", note = "please use ImStr::from_bytes_with_nul_unchecked instead")]
pub unsafe fn from_bytes_unchecked<'a>(bytes: &'a [u8]) -> &'a ImStr { pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &ImStr {
ImStr::from_utf8_with_nul_unchecked(bytes) ImStr::from_utf8_with_nul_unchecked(bytes)
} }
pub unsafe fn from_utf8_with_nul_unchecked<'a>(bytes: &'a [u8]) -> &'a ImStr { pub unsafe fn from_utf8_with_nul_unchecked(bytes: &[u8]) -> &ImStr {
mem::transmute(bytes) mem::transmute(bytes)
} }
pub fn as_ptr(&self) -> *const c_char { self.0.as_ptr() } pub fn as_ptr(&self) -> *const c_char { self.0.as_ptr() }
pub fn to_str(&self) -> &str { pub fn to_str(&self) -> &str {
unsafe { str::from_utf8_unchecked(&self.0.to_bytes()) } unsafe { str::from_utf8_unchecked(self.0.to_bytes()) }
} }
} }

View File

@ -16,7 +16,7 @@ pub struct TreeNode<'ui, 'p> {
} }
impl<'ui, 'p> TreeNode<'ui, 'p> { impl<'ui, 'p> TreeNode<'ui, 'p> {
pub(crate) fn new(id: &'p ImStr) -> Self { pub fn new(_: &Ui<'ui>, id: &'p ImStr) -> Self {
TreeNode { TreeNode {
id: id, id: id,
label: None, label: None,
@ -62,7 +62,7 @@ pub struct CollapsingHeader<'ui, 'p> {
} }
impl<'ui, 'p> CollapsingHeader<'ui, 'p> { impl<'ui, 'p> CollapsingHeader<'ui, 'p> {
pub(crate) fn new(label: &'p ImStr) -> Self { pub fn new(_: &Ui<'ui>, label: &'p ImStr) -> Self {
CollapsingHeader { CollapsingHeader {
label: label, label: label,
flags: ImGuiTreeNodeFlags::empty(), flags: ImGuiTreeNodeFlags::empty(),

View File

@ -26,7 +26,7 @@ pub struct Window<'ui, 'p> {
} }
impl<'ui, 'p> Window<'ui, 'p> { impl<'ui, 'p> Window<'ui, 'p> {
pub(crate) fn new(name: &'p ImStr) -> Window<'ui, 'p> { pub fn new(_: &Ui<'ui>, name: &'p ImStr) -> Window<'ui, 'p> {
Window { Window {
pos: (0.0, 0.0), pos: (0.0, 0.0),
pos_cond: ImGuiSetCond::empty(), pos_cond: ImGuiSetCond::empty(),