Prefer .map for conditionals with else conditions (#15118)

This PR updates instances where we were using `.when_else` and
`.when_else_some` to use `.map` with a conditional inside.

This allows us to avoid reinventing Rust's syntax for conditionals and
(IMO) makes the code easier to read.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-07-24 17:09:07 -04:00 committed by GitHub
parent 596ee58be8
commit 298ca5ff1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 55 additions and 93 deletions

View file

@ -1320,9 +1320,8 @@ fn render_same_line_diagnostics(
let editor_handle = editor_handle.clone(); let editor_handle = editor_handle.clone();
let parent = h_flex() let parent = h_flex()
.items_start() .items_start()
.child(v_flex().size_full().when_some_else( .child(v_flex().size_full().map(|parent| {
toggle_expand_label, if let Some(label) = toggle_expand_label {
|parent, label| {
parent.child(Button::new(cx.block_id, label).on_click({ parent.child(Button::new(cx.block_id, label).on_click({
let diagnostics = Arc::clone(&diagnostics); let diagnostics = Arc::clone(&diagnostics);
move |_, cx| { move |_, cx| {
@ -1353,16 +1352,15 @@ fn render_same_line_diagnostics(
}); });
} }
})) }))
}, } else {
|parent| {
parent.child( parent.child(
h_flex() h_flex()
.size(IconSize::default().rems()) .size(IconSize::default().rems())
.invisible() .invisible()
.flex_none(), .flex_none(),
) )
}, }
)); }));
let max_message_rows = if expanded { let max_message_rows = if expanded {
None None
} else { } else {

View file

@ -40,44 +40,6 @@ pub trait FluentBuilder {
} }
}) })
} }
/// Conditionally unwrap and modify self with one closure if the given option is Some, or another if it is None.
fn when_some_else<T>(
self,
option: Option<T>,
then: impl FnOnce(Self, T) -> Self,
otherwise: impl FnOnce(Self) -> Self,
) -> Self
where
Self: Sized,
{
self.map(|this| {
if let Some(value) = option {
then(this, value)
} else {
otherwise(this)
}
})
}
/// Conditionally modify self with one closure or another
fn when_else(
self,
condition: bool,
then: impl FnOnce(Self) -> Self,
otherwise: impl FnOnce(Self) -> Self,
) -> Self
where
Self: Sized,
{
self.map(|this| {
if condition {
then(this)
} else {
otherwise(this)
}
})
}
} }
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]

View file

@ -464,15 +464,12 @@ impl ProjectPanel {
let is_remote = project.is_remote() && project.dev_server_project_id().is_none(); let is_remote = project.is_remote() && project.dev_server_project_id().is_none();
let context_menu = ContextMenu::build(cx, |menu, cx| { let context_menu = ContextMenu::build(cx, |menu, cx| {
menu.context(self.focus_handle.clone()).when_else( menu.context(self.focus_handle.clone()).map(|menu| {
is_read_only, if is_read_only {
|menu| { menu.when(is_dir, |menu| {
menu.action("Copy Relative Path", Box::new(CopyRelativePath)) menu.action("Search Inside", Box::new(NewSearchInDirectory))
.when(is_dir, |menu| { })
menu.action("Search Inside", Box::new(NewSearchInDirectory)) } else {
})
},
|menu| {
menu.action("New File", Box::new(NewFile)) menu.action("New File", Box::new(NewFile))
.action("New Folder", Box::new(NewDirectory)) .action("New Folder", Box::new(NewDirectory))
.separator() .separator()
@ -545,8 +542,8 @@ impl ProjectPanel {
menu.separator() menu.separator()
.action("Collapse All", Box::new(CollapseAllEntries)) .action("Collapse All", Box::new(CollapseAllEntries))
}) })
}, }
) })
}); });
cx.focus_view(&context_menu); cx.focus_view(&context_menu);

View file

@ -83,37 +83,34 @@ impl QuickActionBar {
let status = menu_state.status; let status = menu_state.status;
let editor = editor.clone(); let editor = editor.clone();
menu.when_else( menu.map(|menu| {
status.is_connected(), if status.is_connected() {
|running| {
let status = status.clone(); let status = status.clone();
running menu.custom_row(move |_cx| {
.custom_row(move |_cx| { h_flex()
h_flex() .child(
.child( Label::new(format!(
Label::new(format!( "kernel: {} ({})",
"kernel: {} ({})", menu_state.kernel_name.clone(),
menu_state.kernel_name.clone(), menu_state.kernel_language.clone()
menu_state.kernel_language.clone() ))
)) .size(LabelSize::Small)
.color(Color::Muted),
)
.into_any_element()
})
.custom_row(move |_cx| {
h_flex()
.child(
Label::new(status.clone().to_string())
.size(LabelSize::Small) .size(LabelSize::Small)
.color(Color::Muted), .color(Color::Muted),
) )
.into_any_element() .into_any_element()
}) })
.custom_row(move |_cx| { } else {
h_flex()
.child(
Label::new(status.clone().to_string())
.size(LabelSize::Small)
.color(Color::Muted),
)
.into_any_element()
})
},
|not_running| {
let status = status.clone(); let status = status.clone();
not_running.custom_row(move |_cx| { menu.custom_row(move |_cx| {
h_flex() h_flex()
.child( .child(
Label::new(format!("{}...", status.clone().to_string())) Label::new(format!("{}...", status.clone().to_string()))
@ -122,8 +119,8 @@ impl QuickActionBar {
) )
.into_any_element() .into_any_element()
}) })
}, }
) })
.separator() .separator()
.custom_entry( .custom_entry(
move |_cx| { move |_cx| {

View file

@ -342,11 +342,13 @@ impl Render for LegacySettingsMenu {
.max_w_96() .max_w_96()
.max_h_2_3() .max_h_2_3()
.px_2() .px_2()
.when_else( .map(|el| {
is_empty, if is_empty {
|empty| empty.py_1(), el.py_1()
|not_empty| not_empty.pt_0().pb_1(), } else {
) el.pt_0().pb_1()
}
})
.gap_1() .gap_1()
.when(is_empty, |this| { .when(is_empty, |this| {
this.child(Label::new("No settings found").color(Color::Muted)) this.child(Label::new("No settings found").color(Color::Muted))

View file

@ -119,9 +119,15 @@ impl Render for Toolbar {
.when(has_right_items, |this| { .when(has_right_items, |this| {
this.child( this.child(
h_flex() h_flex()
// We're using `flex_none` here to prevent some flickering that can occur when the .map(|el| {
// size of the left items container changes. if has_left_items {
.when_else(has_left_items, Div::flex_none, Div::flex_auto) // We're using `flex_none` here to prevent some flickering that can occur when the
// size of the left items container changes.
el.flex_none()
} else {
el.flex_auto()
}
})
.justify_end() .justify_end()
.children(self.right_items().map(|item| item.to_any())), .children(self.right_items().map(|item| item.to_any())),
) )