Skip to content

Commit 98c1e69

Browse files
committed
Add basic layout functionality.
1 parent 766d626 commit 98c1e69

File tree

5 files changed

+124
-12
lines changed

5 files changed

+124
-12
lines changed

iui/examples/basic.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
extern crate iui;
22
use iui::prelude::*;
3+
use iui::controls::{Button, VerticalBox};
34

45
fn main() {
56
// Initialize the UI library
67
let ui = UI::init().expect("Couldn't initialize UI library");
78
// Create a window into which controls can be placed
89
let win = Window::new(&ui, "Test App", 200, 200, WindowType::NoMenubar);
9-
// Create a button to place in the window
10-
let btn = iui::controls::Button::new(&ui, "Button");
10+
11+
// Create a vertical layout to hold the buttons
12+
let vbox = VerticalBox::new(&ui);
13+
vbox.set_padded(&ui, true);
14+
15+
let button = iui::controls::Button::new(&ui, "Button");
16+
let quit_button = iui::controls::Button::new(&ui, "Quit");
17+
18+
vbox.append(&ui, button, LayoutStrategy::Compact);
19+
vbox.append(&ui, quit_button, LayoutStrategy::Compact);
1120

1221
// Actually put the button in the window
13-
win.set_child(&ui, btn);
22+
win.set_child(&ui, vbox);
1423
// Show the window
1524
win.show(&ui);
1625
// Run the application

iui/src/controls/basic.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ use ui_sys::{self, uiButton, uiControl, uiLabel};
55
use super::Control;
66
use ui::UI;
77

8+
define_control!{
9+
/// A non-interactable piece of text.
10+
rust_type: Label,
11+
sys_type: uiLabel
12+
}
13+
814
define_control!{
915
/// A textual button which users can click on, causing a callback to run.
1016
rust_type: Button,
@@ -62,9 +68,3 @@ impl Button {
6268
}
6369
}
6470
}
65-
66-
define_control!{
67-
/// A non-interactable piece of text.
68-
rust_type: Label,
69-
sys_type: uiLabel
70-
}

iui/src/controls/layout.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use std::mem;
2+
use std::ffi::{CStr, CString};
3+
use libc::c_int;
4+
use ui_sys::{self, uiBox, uiControl, uiGroup};
5+
use super::Control;
6+
use ui::UI;
7+
8+
/// Defines the ways in which the children of boxes can be layed out.
9+
pub enum LayoutStrategy {
10+
/// Make the control the minimum possible size to contain its content
11+
Compact,
12+
/// Make the control expand to its maximum size
13+
Stretchy,
14+
}
15+
16+
define_control! {
17+
/// A box that lays out its children vertically; see [`BoxExt`](trait.BoxExt.html) for functionality.
18+
rust_type: VerticalBox,
19+
sys_type: uiBox
20+
}
21+
22+
define_control! {
23+
/// A box that lays out its children horizontally; see [`BoxExt`](trait.BoxExt.html) for functionality.
24+
rust_type: HorizontalBox,
25+
sys_type: uiBox
26+
}
27+
28+
impl VerticalBox {
29+
/// Create a new vertical box layout.
30+
pub fn new(_ctx: &UI) -> VerticalBox {
31+
VerticalBox {
32+
uiBox: unsafe { ui_sys::uiNewVerticalBox() },
33+
}
34+
}
35+
}
36+
37+
impl HorizontalBox {
38+
/// Create a new horizontal box layout.
39+
pub fn new(_ctx: &UI) -> VerticalBox {
40+
VerticalBox {
41+
uiBox: unsafe { ui_sys::uiNewHorizontalBox() },
42+
}
43+
}
44+
}
45+
46+
fn append<T: Into<Control>>(b: *mut uiBox, ctx: &UI, child: T, strategy: LayoutStrategy) {
47+
let stretchy = match strategy {
48+
LayoutStrategy::Compact => false,
49+
LayoutStrategy::Stretchy => true,
50+
};
51+
let control = child.into();
52+
unsafe {
53+
assert!(ctx.parent_of(control.clone()).is_none());
54+
ui_sys::uiBoxAppend(b, control.ui_control, stretchy as c_int)
55+
}
56+
}
57+
58+
fn padded(b: *mut uiBox, _ctx: &UI) -> bool {
59+
unsafe { ui_sys::uiBoxPadded(b) != 0 }
60+
}
61+
62+
fn set_padded(b: *mut uiBox, padded: bool, _ctx: &UI) {
63+
unsafe { ui_sys::uiBoxSetPadded(b, padded as c_int) }
64+
}
65+
66+
impl VerticalBox {
67+
/// Add a control to the end of the box, sized by the given layout strategy.
68+
pub fn append<T: Into<Control>>(&self, _ctx: &UI, child: T, strategy: LayoutStrategy) {
69+
append(self.uiBox, _ctx, child, strategy)
70+
}
71+
72+
/// Determine whenther the box provides padding around its children.
73+
pub fn padded(&self, _ctx: &UI) -> bool {
74+
padded(self.uiBox, _ctx)
75+
}
76+
77+
/// Set whether or not the box should provide padding around its children.
78+
pub fn set_padded(&self, _ctx: &UI, padded: bool) {
79+
set_padded(self.uiBox, padded, _ctx)
80+
}
81+
}
82+
83+
impl HorizontalBox {
84+
/// Add a control to the end of the box, sized by the given layout strategy.
85+
pub fn append<T: Into<Control>>(&self, _ctx: &UI, child: T, strategy: LayoutStrategy) {
86+
append(self.uiBox, _ctx, child, strategy)
87+
}
88+
89+
/// Determine whenther the box provides padding around its children.
90+
pub fn padded(&self, _ctx: &UI) -> bool {
91+
padded(self.uiBox, _ctx)
92+
}
93+
94+
/// Set whether or not the box should provide padding around its children.
95+
pub fn set_padded(&self, _ctx: &UI, padded: bool) {
96+
set_padded(self.uiBox, padded, _ctx)
97+
}
98+
}

iui/src/controls/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//! Available controls and related functionality. Note that these structs are created by methods on the `UI` struct.
1+
//! Available user interface controls and related functionality.
2+
//!
3+
//! Note that `Control` and all specific control types are references to memory which is owned by the UI library.
24
35
use ui_sys::{self, uiControl};
46
use ui::UI;
@@ -11,24 +13,25 @@ mod basic;
1113
pub use self::basic::*;
1214
mod window;
1315
pub use self::window::*;
16+
mod layout;
17+
pub use self::layout::*;
1418

1519
/// A generic UI control. Any UI control can be turned into this type.
20+
///
1621
/// Note that `Control` and all specific control types are references
1722
/// whose memory is owned by the UI library.
1823
pub struct Control {
1924
ui_control: *mut uiControl,
2025
}
2126

2227
impl Drop for Control {
23-
#[inline]
2428
fn drop(&mut self) {
2529
// For now this does nothing, but in the future, when `libui` supports proper memory
2630
// management, this will likely need to twiddle reference counts.
2731
}
2832
}
2933

3034
impl Clone for Control {
31-
#[inline]
3235
fn clone(&self) -> Control {
3336
Control {
3437
ui_control: self.ui_control,

iui/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ pub mod controls;
2525
pub use ui::UI;
2626
pub use error::UIError;
2727

28+
/// Common imports are packaged into this module. It's meant to be glob-imported: `use iui::prelude::*`.
2829
pub mod prelude {
2930
pub use ui::UI;
3031
pub use controls::{Window, WindowType};
32+
pub use controls::{LayoutStrategy};
3133
}

0 commit comments

Comments
 (0)