Skip to content

Commit b51b95c

Browse files
committed
trinary_choice: make labels optional
So one can make a binary choice by leaving the middle one empty.
1 parent 8e1cf1f commit b51b95c

File tree

7 files changed

+57
-36
lines changed

7 files changed

+57
-36
lines changed

src/rust/bitbox02-rust/src/hww/api/bip85.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async fn process_bip39() -> Result<(), Error> {
5959
})
6060
.await?;
6161

62-
let num_words: u32 = match choose("How many words?", "12", "18", "24").await {
62+
let num_words: u32 = match choose("How many words?", Some("12"), Some("18"), Some("24")).await {
6363
TrinaryChoice::TRINARY_CHOICE_LEFT => 12,
6464
TrinaryChoice::TRINARY_CHOICE_MIDDLE => 18,
6565
TrinaryChoice::TRINARY_CHOICE_RIGHT => 24,

src/rust/bitbox02-rust/src/workflow/mnemonic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ async fn get_12th_18th_word(
290290

291291
/// Retrieve a BIP39 mnemonic sentence of 12, 18 or 24 words from the user.
292292
pub async fn get() -> Result<zeroize::Zeroizing<String>, CancelError> {
293-
let num_words: usize = match choose("How many words?", "12", "18", "24").await {
293+
let num_words: usize = match choose("How many words?", Some("12"), Some("18"), Some("24")).await
294+
{
294295
TrinaryChoice::TRINARY_CHOICE_LEFT => 12,
295296
TrinaryChoice::TRINARY_CHOICE_MIDDLE => 18,
296297
TrinaryChoice::TRINARY_CHOICE_RIGHT => 24,

src/rust/bitbox02-rust/src/workflow/trinary_choice.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ pub use bitbox02::ui::TrinaryChoice;
2222

2323
pub async fn choose(
2424
message: &str,
25-
label_left: &str,
26-
label_middle: &str,
27-
label_right: &str,
25+
label_left: Option<&str>,
26+
label_middle: Option<&str>,
27+
label_right: Option<&str>,
2828
) -> TrinaryChoice {
2929
let result = RefCell::new(None as Option<TrinaryChoice>);
3030

src/rust/bitbox02/src/ui/ui.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ pub fn menu_create(params: MenuParams<'_>) -> Component<'_> {
318318

319319
pub fn trinary_choice_create<'a>(
320320
message: &'a str,
321-
label_left: &'a str,
322-
label_middle: &'a str,
323-
label_right: &'a str,
321+
label_left: Option<&'a str>,
322+
label_middle: Option<&'a str>,
323+
label_right: Option<&'a str>,
324324
chosen_callback: TrinaryChoiceCb,
325325
) -> Component<'a> {
326326
unsafe extern "C" fn c_chosen_cb(choice: TrinaryChoice, param: *mut c_void) {
@@ -329,12 +329,25 @@ pub fn trinary_choice_create<'a>(
329329
}
330330

331331
let chosen_cb_param = Box::into_raw(Box::new(chosen_callback)) as *mut c_void;
332+
let label_left = label_left.map(|label| crate::util::str_to_cstr_vec(label).unwrap());
333+
let label_middle = label_middle.map(|label| crate::util::str_to_cstr_vec(label).unwrap());
334+
let label_right = label_right.map(|label| crate::util::str_to_cstr_vec(label).unwrap());
332335
let component = unsafe {
333336
bitbox02_sys::trinary_choice_create(
334-
crate::util::str_to_cstr_vec(message).unwrap().as_ptr(), // copied in C
335-
crate::util::str_to_cstr_vec(label_left).unwrap().as_ptr(), // copied in C
336-
crate::util::str_to_cstr_vec(label_middle).unwrap().as_ptr(), // copied in C
337-
crate::util::str_to_cstr_vec(label_right).unwrap().as_ptr(), // copied in C
337+
// copied in C
338+
crate::util::str_to_cstr_vec(message).unwrap().as_ptr(),
339+
// copied in C
340+
label_left
341+
.as_ref()
342+
.map_or_else(|| core::ptr::null(), |label| label.as_ptr()),
343+
// copied in C
344+
label_middle
345+
.as_ref()
346+
.map_or_else(|| core::ptr::null(), |label| label.as_ptr()),
347+
// copied in C
348+
label_right
349+
.as_ref()
350+
.map_or_else(|| core::ptr::null(), |label| label.as_ptr()),
338351
Some(c_chosen_cb as _),
339352
chosen_cb_param,
340353
core::ptr::null_mut(), // parent component, there is no parent.

src/rust/bitbox02/src/ui/ui_stub.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ pub fn menu_create(_params: MenuParams<'_>) -> Component<'_> {
107107

108108
pub fn trinary_choice_create<'a>(
109109
_message: &'a str,
110-
_label_left: &'a str,
111-
_label_middle: &'a str,
112-
_label_right: &'a str,
110+
_label_left: Option<&'a str>,
111+
_label_middle: Option<&'a str>,
112+
_label_right: Option<&'a str>,
113113
_chosen_callback: TrinaryChoiceCb,
114114
) -> Component<'a> {
115115
panic!("not implemented")

src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ pub fn menu_create(_params: MenuParams<'_>) -> Component<'_> {
117117

118118
pub fn trinary_choice_create<'a>(
119119
_message: &'a str,
120-
_label_left: &'a str,
121-
_label_middle: &'a str,
122-
_label_right: &'a str,
120+
_label_left: Option<&'a str>,
121+
_label_middle: Option<&'a str>,
122+
_label_right: Option<&'a str>,
123123
_chosen_callback: TrinaryChoiceCb,
124124
) -> Component<'a> {
125125
panic!("not implemented")

src/ui/components/trinary_choice.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,31 @@ component_t* trinary_choice_create(
9595
ui_util_add_sub_component(component, label_create(message, NULL, CENTER, component));
9696
}
9797

98-
data->button_left = button_create(label_left, bottom_slider, 0, _left_selected, component);
99-
ui_util_add_sub_component(component, data->button_left);
100-
101-
data->button_middle =
102-
button_create(label_middle, bottom_slider, 0, _middle_selected, component);
103-
ui_util_add_sub_component(component, data->button_middle);
104-
105-
data->button_right = button_create(label_right, bottom_slider, 0, _right_selected, component);
106-
ui_util_add_sub_component(component, data->button_right);
107-
108-
ui_util_position_left_bottom_offset(component, data->button_left, 0, 0);
109-
ui_util_position_left_bottom_offset(
110-
component,
111-
data->button_middle,
112-
SCREEN_WIDTH / 2 - data->button_middle->dimension.width / 2,
113-
0);
114-
ui_util_position_left_bottom_offset(
115-
component, data->button_right, SCREEN_WIDTH - data->button_right->dimension.width, 0);
98+
if (label_left != NULL) {
99+
data->button_left = button_create(label_left, bottom_slider, 0, _left_selected, component);
100+
ui_util_add_sub_component(component, data->button_left);
101+
ui_util_position_left_bottom_offset(component, data->button_left, 0, 0);
102+
}
103+
104+
if (label_middle != NULL) {
105+
data->button_middle =
106+
button_create(label_middle, bottom_slider, 0, _middle_selected, component);
107+
ui_util_add_sub_component(component, data->button_middle);
108+
ui_util_position_left_bottom_offset(
109+
component,
110+
data->button_middle,
111+
SCREEN_WIDTH / 2 - data->button_middle->dimension.width / 2,
112+
0);
113+
}
114+
115+
if (label_right != NULL) {
116+
data->button_right =
117+
button_create(label_right, bottom_slider, 0, _right_selected, component);
118+
ui_util_add_sub_component(component, data->button_right);
119+
120+
ui_util_position_left_bottom_offset(
121+
component, data->button_right, SCREEN_WIDTH - data->button_right->dimension.width, 0);
122+
}
116123

117124
return component;
118125
}

0 commit comments

Comments
 (0)