Skip to content

Commit abae9f2

Browse files
committed
Use proper semantic mutability for methods
1 parent 94b7e12 commit abae9f2

File tree

10 files changed

+85
-80
lines changed

10 files changed

+85
-80
lines changed

iui/examples/basic.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,31 @@ fn main() {
66
// Initialize the UI library
77
let ui = UI::init().expect("Couldn't initialize UI library");
88
// Create a window into which controls can be placed
9-
let win = Window::new(&ui, "Test App", 200, 200, WindowType::NoMenubar);
9+
let mut win = Window::new(&ui, "Test App", 200, 200, WindowType::NoMenubar);
1010

1111
// Create a vertical layout to hold the controls
12-
let vbox = VerticalBox::new(&ui);
12+
let mut vbox = VerticalBox::new(&ui);
1313
vbox.set_padded(&ui, true);
1414

15-
let group_vbox = VerticalBox::new(&ui);
16-
let group = Group::new(&ui, "Group");
15+
let mut group_vbox = VerticalBox::new(&ui);
16+
let mut group = Group::new(&ui, "Group");
1717

1818
// Create two buttons to place in the window
19-
let button = Button::new(&ui, "Button");
20-
let quit_button = Button::new(&ui, "Quit");
19+
let mut button = Button::new(&ui, "Button");
20+
button.on_clicked(&ui, {
21+
let ui = ui.clone();
22+
move |btn| {
23+
btn.set_text(&ui, "Clicked!");
24+
}
25+
});
26+
27+
let mut quit_button = Button::new(&ui, "Quit");
28+
quit_button.on_clicked(&ui, {
29+
let ui = ui.clone();
30+
move |_| {
31+
ui.quit();
32+
}
33+
});
2134

2235
// Create a new label. Note that labels don't auto-wrap!
2336
let mut label_text = String::new();

iui/examples/files.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ fn main() {
1414

1515
// Create the input controls
1616
let entry = MultilineEntry::new(&ui);
17-
let button = Button::new(&ui, "Save Buffer");
17+
let mut button = Button::new(&ui, "Save Buffer");
1818

1919
// Set up the application's layout
20-
let window = Window::new(&ui, "Save Buffer to File", 640, 480, WindowType::NoMenubar);
21-
let vbox = VerticalBox::new(&ui);
20+
let mut window = Window::new(&ui, "Save Buffer to File", 640, 480, WindowType::NoMenubar);
21+
let mut vbox = VerticalBox::new(&ui);
2222
vbox.append(&ui, entry.clone(), LayoutStrategy::Stretchy);
2323
vbox.append(&ui, button.clone(), LayoutStrategy::Compact);
2424
window.set_child(&ui, vbox);

iui/examples/inputs.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ fn main() {
2525
// While it's not necessary to create a block for this, it makes the code a lot easier
2626
// to read; the indentation presents a visual cue informing the reader that these
2727
// statements are related.
28-
let (input_group, slider, spinner, entry, multi) = {
28+
let (input_group, mut slider, mut spinner, mut entry, mut multi) = {
2929
// The group will hold all the inputs
30-
let input_group = Group::new(&ui, "Inputs");
30+
let mut input_group = Group::new(&ui, "Inputs");
3131
// The vertical box arranges the inputs within the groups
32-
let input_vbox = VerticalBox::new(&ui);
32+
let mut input_vbox = VerticalBox::new(&ui);
3333
input_vbox.set_padded(&ui, true);
3434
// Numerical inputs
3535
let slider = Slider::new(&ui, 1, 100);
@@ -53,8 +53,8 @@ fn main() {
5353
// Set up the outputs for the application. Organization is very similar to the
5454
// previous setup.
5555
let (output_group, add_label, sub_label, text_label, bigtext_label) = {
56-
let output_group = Group::new(&ui, "Outputs");
57-
let output_vbox = VerticalBox::new(&ui);
56+
let mut output_group = Group::new(&ui, "Outputs");
57+
let mut output_vbox = VerticalBox::new(&ui);
5858
let add_label = Label::new(&ui, "");
5959
let sub_label = Label::new(&ui, "");
6060
let text_label = Label::new(&ui, "");
@@ -68,12 +68,12 @@ fn main() {
6868
};
6969

7070
// This horizontal box will arrange the two groups of controls.
71-
let hbox = HorizontalBox::new(&ui);
71+
let mut hbox = HorizontalBox::new(&ui);
7272
hbox.append(&ui, input_group, LayoutStrategy::Stretchy);
7373
hbox.append(&ui, output_group, LayoutStrategy::Stretchy);
7474

7575
// The window allows all constituent components to be displayed.
76-
let window = Window::new(&ui, "Input Output Test", 300, 150, WindowType::NoMenubar);
76+
let mut window = Window::new(&ui, "Input Output Test", 300, 150, WindowType::NoMenubar);
7777
window.set_child(&ui, hbox);
7878
window.show(&ui);
7979

@@ -101,13 +101,16 @@ fn main() {
101101
});
102102

103103

104-
// This update_view function is defined inline so that it can capture the environment, rather than
105-
// needing to be passed ui, the labels, and the state each time it is invoked.
106-
// It is defined last so that it can operate with the minimum number of `.clone()` calls (since
107-
// the code won't need to access the controls after this).
108-
// It is wrapped in a refcounted pointer so it can be shared with several other closures.
109-
let update_view = Rc::new({
104+
// Rather than just invoking ui.run(), using EventLoop gives a lot more control
105+
// over the user interface event loop.
106+
// Here, the on_tick() callback is used to update the view against the state.
107+
let mut event_loop = ui.event_loop();
108+
event_loop.on_tick(&ui, {
110109
let ui = ui.clone();
110+
let mut add_label = add_label.clone();
111+
let mut sub_label = sub_label.clone();
112+
let mut text_label = text_label.clone();
113+
let mut bigtext_label = bigtext_label.clone();
111114
move || {
112115
let state = state.borrow();
113116

@@ -118,11 +121,5 @@ fn main() {
118121
bigtext_label.set_text(&ui, &format!("Multiline Text: {}", state.multi_val));
119122
}
120123
});
121-
122-
// Rather than just invoking ui.run(), using EventLoop gives a lot more control
123-
// over the user interface event loop.
124-
// Here, the on_tick() callback is used to update the view against the state.
125-
let mut event_loop = ui.event_loop();
126-
event_loop.on_tick(&ui, move || { update_view(); });
127124
event_loop.run(&ui);
128125
}

iui/src/controls/basic.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,38 @@ impl Button {
4141
}
4242

4343
/// Set the text on the button.
44-
pub fn set_text(&self, _ctx: &UI, text: &str) {
44+
pub fn set_text(&mut self, _ctx: &UI, text: &str) {
4545
unsafe {
4646
let c_string = CString::new(text.as_bytes().to_vec()).unwrap();
4747
ui_sys::uiButtonSetText(self.uiButton, c_string.as_ptr())
4848
}
4949
}
5050

5151
/// Run the given callback when the button is clicked.
52-
pub fn on_clicked<F: FnMut(&Button)>(&self, _ctx: &UI, callback: F) {
52+
pub fn on_clicked<F: FnMut(&mut Button)>(&mut self, _ctx: &UI, callback: F) {
5353
unsafe {
54-
let mut data: Box<Box<FnMut(&Button)>> = Box::new(Box::new(callback));
54+
let mut data: Box<Box<FnMut(&mut Button)>> = Box::new(Box::new(callback));
5555
ui_sys::uiButtonOnClicked(
5656
self.uiButton,
5757
c_callback,
58-
&mut *data as *mut Box<FnMut(&Button)> as *mut c_void,
58+
&mut *data as *mut Box<FnMut(&mut Button)> as *mut c_void,
5959
);
6060
mem::forget(data);
6161
}
6262

6363
extern "C" fn c_callback(button: *mut uiButton, data: *mut c_void) {
6464
unsafe {
65-
let button = Button { uiButton: button };
66-
mem::transmute::<*mut c_void, &mut Box<FnMut(&Button)>>(data)(&button)
65+
let mut button = Button { uiButton: button };
66+
mem::transmute::<*mut c_void, &mut Box<FnMut(&mut Button)>>(data)(&mut button)
6767
}
6868
}
6969
}
7070
}
7171

7272
impl Label {
7373
/// Create a new label with the given string as its text.
74+
/// Note that labels do not auto-wrap their text; they will expand as far as needed
75+
/// to fit.
7476
pub fn new(_ctx: &UI, text: &str) -> Label {
7577
unsafe {
7678
let c_string = CString::new(text.as_bytes().to_vec()).unwrap();
@@ -93,7 +95,7 @@ impl Label {
9395
}
9496

9597
/// Set the text on the label.
96-
pub fn set_text(&self, _ctx: &UI, text: &str) {
98+
pub fn set_text(&mut self, _ctx: &UI, text: &str) {
9799
unsafe {
98100
let c_string = CString::new(text.as_bytes().to_vec()).unwrap();
99101
ui_sys::uiLabelSetText(self.uiLabel, c_string.as_ptr())

iui/src/controls/create_macro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ macro_rules! define_control {
4444

4545
impl $rust_type {
4646
// Show this control to the user. This will also show its non-hidden children.
47-
pub fn show(&self, _ctx: &UI) {
47+
pub fn show(&mut self, _ctx: &UI) {
4848
let control: Control = self.clone().into();
4949
unsafe { ui_sys::uiControlShow(control.ui_control) }
5050
}
5151

5252
// Hide this control from the user. This will hide its children.
53-
pub fn hide(&self, _ctx: &UI) {
53+
pub fn hide(&mut self, _ctx: &UI) {
5454
let control: Control = self.clone().into();
5555
unsafe { ui_sys::uiControlHide(control.ui_control) }
5656
}

iui/src/controls/entry.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ use ui::UI;
1010

1111
pub trait NumericEntry {
1212
fn value(&self, ctx: &UI) -> i64;
13-
fn set_value(&self, ctx: &UI, value: i64);
14-
fn on_changed<F: FnMut(i64)>(&self, ctx: &UI, callback: F);
13+
fn set_value(&mut self, ctx: &UI, value: i64);
14+
fn on_changed<F: FnMut(i64)>(&mut self, ctx: &UI, callback: F);
1515
}
1616

1717
pub trait TextEntry {
1818
fn value(&self, ctx: &UI) -> String;
19-
fn set_value(&self, ctx: &UI, value: &str);
20-
fn on_changed<F: FnMut(String)>(&self, ctx: &UI, callback: F);
19+
fn set_value(&mut self, ctx: &UI, value: &str);
20+
fn on_changed<F: FnMut(String)>(&mut self, ctx: &UI, callback: F);
2121
}
2222

2323
define_control!{
@@ -56,11 +56,11 @@ impl NumericEntry for Spinbox {
5656
unsafe { ui_sys::uiSpinboxValue(self.uiSpinbox) }
5757
}
5858

59-
fn set_value(&self, _ctx: &UI, value: i64) {
59+
fn set_value(&mut self, _ctx: &UI, value: i64) {
6060
unsafe { ui_sys::uiSpinboxSetValue(self.uiSpinbox, value) }
6161
}
6262

63-
fn on_changed<F: FnMut(i64)>(&self, _ctx: &UI, callback: F) {
63+
fn on_changed<F: FnMut(i64)>(&mut self, _ctx: &UI, callback: F) {
6464
unsafe {
6565
let mut data: Box<Box<FnMut(i64)>> = Box::new(Box::new(callback));
6666
ui_sys::uiSpinboxOnChanged(
@@ -85,11 +85,11 @@ impl NumericEntry for Slider {
8585
unsafe { ui_sys::uiSliderValue(self.uiSlider) }
8686
}
8787

88-
fn set_value(&self, _ctx: &UI, value: i64) {
88+
fn set_value(&mut self, _ctx: &UI, value: i64) {
8989
unsafe { ui_sys::uiSliderSetValue(self.uiSlider, value) }
9090
}
9191

92-
fn on_changed<F: FnMut(i64)>(&self, _ctx: &UI, callback: F) {
92+
fn on_changed<F: FnMut(i64)>(&mut self, _ctx: &UI, callback: F) {
9393
unsafe {
9494
let mut data: Box<Box<FnMut(i64)>> = Box::new(Box::new(callback));
9595
ui_sys::uiSliderOnChanged(
@@ -137,12 +137,12 @@ impl TextEntry for Entry {
137137
fn value(&self, _ctx: &UI) -> String {
138138
unsafe { CStr::from_ptr(ui_sys::uiEntryText(self.uiEntry)).to_string_lossy().into_owned() }
139139
}
140-
fn set_value(&self, _ctx: &UI, value: &str) {
140+
fn set_value(&mut self, _ctx: &UI, value: &str) {
141141
let cstring = CString::new(value.as_bytes().to_vec()).unwrap();
142142
unsafe { ui_sys::uiEntrySetText(self.uiEntry, cstring.as_ptr()) }
143143
}
144144

145-
fn on_changed<F: FnMut(String)>(&self, _ctx: &UI, callback: F) {
145+
fn on_changed<F: FnMut(String)>(&mut self, _ctx: &UI, callback: F) {
146146
unsafe {
147147
let mut data: Box<Box<FnMut(String)>> = Box::new(Box::new(callback));
148148
ui_sys::uiEntryOnChanged(
@@ -167,12 +167,12 @@ impl TextEntry for MultilineEntry {
167167
fn value(&self, _ctx: &UI) -> String {
168168
unsafe { CStr::from_ptr(ui_sys::uiMultilineEntryText(self.uiMultilineEntry)).to_string_lossy().into_owned() }
169169
}
170-
fn set_value(&self, _ctx: &UI, value: &str) {
170+
fn set_value(&mut self, _ctx: &UI, value: &str) {
171171
let cstring = CString::new(value.as_bytes().to_vec()).unwrap();
172172
unsafe { ui_sys::uiMultilineEntrySetText(self.uiMultilineEntry, cstring.as_ptr()) }
173173
}
174174

175-
fn on_changed<F: FnMut(String)>(&self, _ctx: &UI, callback: F) {
175+
fn on_changed<F: FnMut(String)>(&mut self, _ctx: &UI, callback: F) {
176176
unsafe {
177177
let mut data: Box<Box<FnMut(String)>> = Box::new(Box::new(callback));
178178
ui_sys::uiMultilineEntryOnChanged(

iui/src/controls/layout.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn set_padded(b: *mut uiBox, padded: bool, _ctx: &UI) {
6666

6767
impl VerticalBox {
6868
/// Add a control to the end of the box, sized by the given layout strategy.
69-
pub fn append<T: Into<Control>>(&self, _ctx: &UI, child: T, strategy: LayoutStrategy) {
69+
pub fn append<T: Into<Control>>(&mut self, _ctx: &UI, child: T, strategy: LayoutStrategy) {
7070
append(self.uiBox, _ctx, child, strategy)
7171
}
7272

@@ -76,14 +76,14 @@ impl VerticalBox {
7676
}
7777

7878
/// Set whether or not the box should provide padding around its children.
79-
pub fn set_padded(&self, _ctx: &UI, padded: bool) {
79+
pub fn set_padded(&mut self, _ctx: &UI, padded: bool) {
8080
set_padded(self.uiBox, padded, _ctx)
8181
}
8282
}
8383

8484
impl HorizontalBox {
8585
/// Add a control to the end of the box, sized by the given layout strategy.
86-
pub fn append<T: Into<Control>>(&self, _ctx: &UI, child: T, strategy: LayoutStrategy) {
86+
pub fn append<T: Into<Control>>(&mut self, _ctx: &UI, child: T, strategy: LayoutStrategy) {
8787
append(self.uiBox, _ctx, child, strategy)
8888
}
8989

@@ -93,7 +93,7 @@ impl HorizontalBox {
9393
}
9494

9595
/// Set whether or not the box should provide padding around its children.
96-
pub fn set_padded(&self, _ctx: &UI, padded: bool) {
96+
pub fn set_padded(&mut self, _ctx: &UI, padded: bool) {
9797
set_padded(self.uiBox, padded, _ctx)
9898
}
9999
}
@@ -113,7 +113,7 @@ define_control! {
113113
impl Group {
114114
/// Create a new group with the given title.
115115
pub fn new(_ctx: &UI, title: &str) -> Group {
116-
let group = unsafe {
116+
let mut group = unsafe {
117117
let c_string = CString::new(title.as_bytes().to_vec()).unwrap();
118118
Group::from_raw(ui_sys::uiNewGroup(c_string.as_ptr()))
119119
};
@@ -136,15 +136,15 @@ impl Group {
136136
}
137137

138138
// Set the group's title.
139-
pub fn set_title(&self, _ctx: &UI, title: &str) {
139+
pub fn set_title(&mut self, _ctx: &UI, title: &str) {
140140
unsafe {
141141
let c_string = CString::new(title.as_bytes().to_vec()).unwrap();
142142
ui_sys::uiGroupSetTitle(self.uiGroup, c_string.as_ptr())
143143
}
144144
}
145145

146146
// Set the group's child widget.
147-
pub fn set_child<T: Into<Control>>(&self, _ctx: &UI, child: T) {
147+
pub fn set_child<T: Into<Control>>(&mut self, _ctx: &UI, child: T) {
148148
unsafe { ui_sys::uiGroupSetChild(self.uiGroup, child.into().ui_control) }
149149
}
150150

@@ -154,7 +154,7 @@ impl Group {
154154
}
155155

156156
// Set whether or not the group draws a margin.
157-
pub fn set_margined(&self, _ctx: &UI, margined: bool) {
157+
pub fn set_margined(&mut self, _ctx: &UI, margined: bool) {
158158
unsafe { ui_sys::uiGroupSetMargined(self.uiGroup, margined as c_int) }
159159
}
160160
}
@@ -168,7 +168,7 @@ impl TabGroup {
168168
/// Add the given control as a new tab in the tab group with the given name.
169169
///
170170
/// Returns the number of tabs in the group after adding the new tab.
171-
pub fn append<T: Into<Control>>(&self, _ctx: &UI, name: &str, control: T) -> u64 {
171+
pub fn append<T: Into<Control>>(&mut self, _ctx: &UI, name: &str, control: T) -> u64 {
172172
let control = control.into();
173173
unsafe {
174174
let c_string = CString::new(name.as_bytes().to_vec()).unwrap();
@@ -180,7 +180,7 @@ impl TabGroup {
180180
/// Add the given control before the given index in the tab group, as a new tab with a given name.
181181
///
182182
/// Returns the number of tabs in the group after adding the new tab.
183-
pub fn insert_at<T: Into<Control>>(&self, _ctx: &UI, name: &str, before: u64, control: T) -> u64 {
183+
pub fn insert_at<T: Into<Control>>(&mut self, _ctx: &UI, name: &str, before: u64, control: T) -> u64 {
184184
unsafe {
185185
let c_string = CString::new(name.as_bytes().to_vec()).unwrap();
186186
ui_sys::uiTabInsertAt(self.uiTab, c_string.as_ptr(), before, control.into().ui_control);
@@ -195,7 +195,7 @@ impl TabGroup {
195195
/// NOTE: This will leak the deleted control! We have no way of actually getting it
196196
/// to decrement its reference count per `libui`'s UI as of today, unless we maintain a
197197
/// separate list of children ourselves…
198-
pub fn delete(&self, _ctx: &UI, index: u64) -> Result<u64, UIError> {
198+
pub fn delete(&mut self, _ctx: &UI, index: u64) -> Result<u64, UIError> {
199199
let n = unsafe { ui_sys::uiTabNumPages(self.uiTab) as u64 };
200200
if index < n {
201201
unsafe { ui_sys::uiTabDelete(self.uiTab, index) };
@@ -211,7 +211,7 @@ impl TabGroup {
211211
}
212212

213213
/// Set whether or not the tab group provides margins around its children.
214-
pub fn set_margined(&self, _ctx: &UI, page: u64, margined: bool) {
214+
pub fn set_margined(&mut self, _ctx: &UI, page: u64, margined: bool) {
215215
unsafe { ui_sys::uiTabSetMargined(self.uiTab, page, margined as c_int) }
216216
}
217217
}

0 commit comments

Comments
 (0)