Skip to content

Commit

Permalink
ui/components/menu: rename continue to finish
Browse files Browse the repository at this point in the history
The component could be at the end or in the middle of a
workflow. "finish" just means that the component is done and the name
does not assume if something comes after or not, whereas "continue"
implies something should come after.
  • Loading branch information
benma committed May 18, 2023
1 parent 0e68357 commit 000eb6a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/rust/bitbox02-rust/src/workflow/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub async fn pick(words: &[&str], title: Option<&str>) -> Result<u8, CancelError
select_word_cb: Some(Box::new(|choice_idx| {
*result.borrow_mut() = Some(Ok(choice_idx));
})),
continue_on_last_cb: None,
finish_on_last_cb: None,
cancel_cb: Some(Box::new(|| {
*result.borrow_mut() = Some(Err(CancelError::Cancelled));
})),
Expand Down
4 changes: 2 additions & 2 deletions src/rust/bitbox02-rust/src/workflow/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub async fn show_mnemonic(words: &[&str]) -> Result<(), CancelError> {
words,
title: None,
select_word_cb: None,
continue_on_last_cb: Some(Box::new(|| {
finish_on_last_cb: Some(Box::new(|| {
set_result(&result, ());
})),
cancel_cb: Some(Box::new(|| {
Expand All @@ -57,7 +57,7 @@ pub async fn confirm_word(choices: &[&str], title: &str) -> Result<u8, CancelErr
select_word_cb: Some(Box::new(|idx| {
set_result(&result, idx);
})),
continue_on_last_cb: None,
finish_on_last_cb: None,
cancel_cb: Some(Box::new(|| {
cancel(&result);
})),
Expand Down
6 changes: 3 additions & 3 deletions src/rust/bitbox02/src/ui/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ impl<'a> TrinaryInputStringParams<'a> {
}

pub type SelectWordCb<'a> = Box<dyn FnMut(u8) + 'a>;
pub type ContinueCancelCb<'a> = Box<dyn FnMut() + 'a>;
pub type FinishCancelCb<'a> = Box<dyn FnMut() + 'a>;

pub struct MenuParams<'a> {
pub words: &'a [&'a str],
pub title: Option<&'a str>,
pub select_word_cb: Option<SelectWordCb<'a>>,
pub continue_on_last_cb: Option<ContinueCancelCb<'a>>,
pub cancel_cb: Option<ContinueCancelCb<'a>>,
pub finish_on_last_cb: Option<FinishCancelCb<'a>>,
pub cancel_cb: Option<FinishCancelCb<'a>>,
}

pub type TrinaryChoiceCb<'a> = Box<dyn FnMut(TrinaryChoice) + 'a>;
Expand Down
28 changes: 14 additions & 14 deletions src/rust/bitbox02/src/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.

pub use super::types::{
AcceptRejectCb, ConfirmParams, ContinueCancelCb, Font, MenuParams, SelectWordCb, TrinaryChoice,
AcceptRejectCb, ConfirmParams, FinishCancelCb, Font, MenuParams, SelectWordCb, TrinaryChoice,
TrinaryChoiceCb, TrinaryInputStringParams,
};

Expand Down Expand Up @@ -67,7 +67,7 @@ impl<'a> Drop for Component<'a> {
pub fn trinary_input_string_create<'a, F>(
params: &TrinaryInputStringParams,
confirm_callback: F,
cancel_callback: Option<ContinueCancelCb<'a>>,
cancel_callback: Option<FinishCancelCb<'a>>,
) -> Component<'a>
where
// Callback must outlive component.
Expand All @@ -89,7 +89,7 @@ where
}

unsafe extern "C" fn c_cancel_callback(param: *mut c_void) {
let callback = param as *mut ContinueCancelCb;
let callback = param as *mut FinishCancelCb;
(*callback)();
}

Expand Down Expand Up @@ -117,7 +117,7 @@ where
on_drop: Some(Box::new(move || unsafe {
// Drop all callbacks.
if !cancel_cb_param.is_null() {
drop(Box::from_raw(cancel_cb_param as *mut ContinueCancelCb));
drop(Box::from_raw(cancel_cb_param as *mut FinishCancelCb));
}
})),
_p: PhantomData,
Expand Down Expand Up @@ -234,8 +234,8 @@ pub fn menu_create(params: MenuParams<'_>) -> Component<'_> {
(*callback)(word_idx);
}

unsafe extern "C" fn c_continue_cancel_cb(param: *mut c_void) {
let callback = param as *mut ContinueCancelCb;
unsafe extern "C" fn c_finish_cancel_cb(param: *mut c_void) {
let callback = param as *mut FinishCancelCb;
(*callback)();
}

Expand All @@ -261,18 +261,18 @@ pub fn menu_create(params: MenuParams<'_>) -> Component<'_> {
),
};

let (continue_on_last_cb, continue_on_last_cb_param) = match params.continue_on_last_cb {
let (finish_on_last_cb, finish_on_last_cb_param) = match params.finish_on_last_cb {
None => (None, core::ptr::null_mut()),
Some(cb) => (
Some(c_continue_cancel_cb as _),
Some(c_finish_cancel_cb as _),
Box::into_raw(Box::new(cb)) as *mut c_void,
),
};

let (cancel_cb, cancel_cb_param) = match params.cancel_cb {
None => (None, core::ptr::null_mut()),
Some(cb) => (
Some(c_continue_cancel_cb as _),
Some(c_finish_cancel_cb as _),
Box::into_raw(Box::new(cb)) as *mut c_void,
),
};
Expand All @@ -289,8 +289,8 @@ pub fn menu_create(params: MenuParams<'_>) -> Component<'_> {
title
.as_ref()
.map_or_else(|| core::ptr::null(), |title| title.as_ptr()),
continue_on_last_cb,
continue_on_last_cb_param,
finish_on_last_cb,
finish_on_last_cb_param,
cancel_cb,
cancel_cb_param,
core::ptr::null_mut(),
Expand All @@ -304,13 +304,13 @@ pub fn menu_create(params: MenuParams<'_>) -> Component<'_> {
if !select_word_cb_param.is_null() {
drop(Box::from_raw(select_word_cb_param as *mut SelectWordCb));
}
if !continue_on_last_cb_param.is_null() {
if !finish_on_last_cb_param.is_null() {
drop(Box::from_raw(
continue_on_last_cb_param as *mut ContinueCancelCb,
finish_on_last_cb_param as *mut FinishCancelCb,
));
}
if !cancel_cb_param.is_null() {
drop(Box::from_raw(cancel_cb_param as *mut ContinueCancelCb));
drop(Box::from_raw(cancel_cb_param as *mut FinishCancelCb));
}
})),
_p: PhantomData,
Expand Down
4 changes: 2 additions & 2 deletions src/rust/bitbox02/src/ui/ui_stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! Stubs for testing.
pub use super::types::{
AcceptRejectCb, ConfirmParams, ContinueCancelCb, Font, MenuParams, SelectWordCb, TrinaryChoice,
AcceptRejectCb, ConfirmParams, FinishCancelCb, Font, MenuParams, SelectWordCb, TrinaryChoice,
TrinaryChoiceCb, TrinaryInputStringParams,
};

Expand Down Expand Up @@ -50,7 +50,7 @@ impl<'a> Drop for Component<'a> {
pub fn trinary_input_string_create<'a, F>(
params: &TrinaryInputStringParams,
mut confirm_callback: F,
_cancel_callback: Option<ContinueCancelCb<'a>>,
_cancel_callback: Option<FinishCancelCb<'a>>,
) -> Component<'a>
where
F: FnMut(SafeInputString) + 'a,
Expand Down
32 changes: 16 additions & 16 deletions src/ui/components/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ typedef struct {
int32_t diff_to_middle;
void (*select_word_cb)(uint8_t, void*);
void* select_word_cb_param;
component_t* continue_on_last_button;
void (*continue_on_last_cb)(void*);
void* continue_on_last_cb_param;
component_t* finish_on_last_button;
void (*finish_on_last_cb)(void*);
void* finish_on_last_cb_param;
void (*cancel_cb)(void*);
void* cancel_cb_param;
} menu_data_t;

static const uint8_t part_width = 20;

static void _continue(component_t* component)
static void _finish(component_t* component)
{
menu_data_t* data = (menu_data_t*)component->parent->data;
if (data->continue_on_last_cb != NULL) {
data->continue_on_last_cb(data->continue_on_last_cb_param);
if (data->finish_on_last_cb != NULL) {
data->finish_on_last_cb(data->finish_on_last_cb_param);
}
}

Expand Down Expand Up @@ -105,11 +105,11 @@ static void _update_positions(component_t* menu, int32_t velocity)
_display_index(menu);
}

if (data->index == data->length - 1 && data->continue_on_last_cb != NULL &&
data->continue_on_last_button == NULL) {
data->continue_on_last_button =
button_create("Continue", top_slider, SCREEN_WIDTH - 23, _continue, menu);
ui_util_add_sub_component(menu, data->continue_on_last_button);
if (data->index == data->length - 1 && data->finish_on_last_cb != NULL &&
data->finish_on_last_button == NULL) {
data->finish_on_last_button =
button_create("Continue", top_slider, SCREEN_WIDTH - 23, _finish, menu);
ui_util_add_sub_component(menu, data->finish_on_last_button);
}
}

Expand Down Expand Up @@ -236,8 +236,8 @@ component_t* menu_create(
void* select_word_cb_param,
const uint8_t length,
const char* title,
void (*continue_on_last_cb)(void*),
void* continue_on_last_cb_param,
void (*finish_on_last_cb)(void*),
void* finish_on_last_cb_param,
void (*cancel_cb)(void*),
void* cancel_cb_param,
component_t* parent)
Expand Down Expand Up @@ -271,9 +271,9 @@ component_t* menu_create(
data->length = length;
data->index = 0;
data->show_index = !title;
data->continue_on_last_cb = continue_on_last_cb;
data->continue_on_last_cb_param = continue_on_last_cb_param;
data->continue_on_last_button = NULL;
data->finish_on_last_cb = finish_on_last_cb;
data->finish_on_last_cb_param = finish_on_last_cb_param;
data->finish_on_last_button = NULL;
data->cancel_cb = cancel_cb;
data->cancel_cb_param = cancel_cb_param;
menu->data = data;
Expand Down
6 changes: 3 additions & 3 deletions src/ui/components/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @param[in] title Title for the window.
* If NULL, displays the index of the current word instead (starting at 1).
* For no title, set this to "".
* @param[in] continue_on_last_cb If set, a checkmark appears when reaching the last word, calling
* @param[in] finish_on_last_cb If set, a checkmark appears when reaching the last word, calling
* this callback.
* @param[in] cancel_cb Called when the cancel button is pressed.
* @param[in] parent The parent component.
Expand All @@ -39,8 +39,8 @@ component_t* menu_create(
void* select_word_cb_param,
uint8_t length,
const char* title,
void (*continue_on_last_cb)(void*),
void* continue_on_last_cb_param,
void (*finish_on_last_cb)(void*),
void* finish_on_last_cb_param,
void (*cancel_cb)(void*),
void* cancel_cb_param,
component_t* parent);
Expand Down

0 comments on commit 000eb6a

Please sign in to comment.