forked from reya/gpui-native-kit
chore: reduce component overhead (#3)
Reviewed-on: reya/gpui-native-kit#3
This commit is contained in:
@@ -9,7 +9,7 @@ use gpui::{
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon};
|
||||
use gpui_native_theme::{ActiveTheme as _, Size};
|
||||
use gpui_native_utils::{adjacent_selectable, focus_handle, focus_ring};
|
||||
use gpui_native_utils::{focus_handle, focus_ring};
|
||||
|
||||
use crate::common::{Sizable, StyledExt, h_flex};
|
||||
|
||||
@@ -64,7 +64,8 @@ where
|
||||
T: Clone + PartialEq + 'static,
|
||||
{
|
||||
id: ElementId,
|
||||
options: Vec<SegmentedOption<T>>,
|
||||
/// Shared with the per-segment and key handlers, so captures are bumps.
|
||||
options: Rc<[SegmentedOption<T>]>,
|
||||
selected: T,
|
||||
a11y_label: Option<SharedString>,
|
||||
size: Size,
|
||||
@@ -82,7 +83,7 @@ where
|
||||
{
|
||||
Self {
|
||||
id: id.into(),
|
||||
options: Vec::new(),
|
||||
options: Rc::from([]),
|
||||
selected,
|
||||
a11y_label: None,
|
||||
size: Size::Small,
|
||||
@@ -163,12 +164,12 @@ where
|
||||
.options
|
||||
.iter()
|
||||
.position(|segment| segment.value == self.selected);
|
||||
let option_count = self.options.len();
|
||||
|
||||
let selectable: Vec<usize> = (0..self.options.len()).collect();
|
||||
let on_change = self.on_change.clone();
|
||||
let key_on_change = on_change.clone();
|
||||
let key_options = self.options.clone();
|
||||
let key_selectable = selectable.clone();
|
||||
let options = self.options.clone();
|
||||
|
||||
let label = self
|
||||
.a11y_label
|
||||
@@ -176,14 +177,13 @@ where
|
||||
.unwrap_or_else(|| SharedString::from(self.id.to_string()));
|
||||
|
||||
window.with_element_namespace(self.id.clone(), |window| {
|
||||
let focus = focus_handle(&self.id, window, cx);
|
||||
|
||||
let geometry: Entity<Vec<Option<Bounds<gpui::Pixels>>>> =
|
||||
window.use_keyed_state("segmented-geometry", cx, |_, _| Vec::new());
|
||||
|
||||
let track_origin: Entity<Option<Point<gpui::Pixels>>> =
|
||||
window.use_keyed_state("segmented-track-origin", cx, |_, _| None);
|
||||
|
||||
let focus = focus_handle(&self.id, window, cx);
|
||||
let anim_config = SpringConfig::new(500., 35., 1.);
|
||||
let track_point = *track_origin.read(cx);
|
||||
|
||||
@@ -197,12 +197,13 @@ where
|
||||
|
||||
let segments = self
|
||||
.options
|
||||
.into_iter()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(move |(index, segment)| {
|
||||
let is_selected = Some(index) == selected_index;
|
||||
let on_change = on_change.clone();
|
||||
let value = segment.value;
|
||||
let options = options.clone();
|
||||
|
||||
let content_color = if is_selected {
|
||||
foreground
|
||||
} else {
|
||||
@@ -228,7 +229,7 @@ where
|
||||
.when_some(segment.icon, |segment, role| {
|
||||
segment.child(icon(role).size(icon_size).color(content_color))
|
||||
})
|
||||
.child(segment.label)
|
||||
.child(segment.label.clone())
|
||||
.child(
|
||||
canvas(
|
||||
{
|
||||
@@ -251,8 +252,9 @@ where
|
||||
move |_, window, cx| {
|
||||
if Some(index) != selected_index
|
||||
&& let Some(on_change) = &on_change
|
||||
&& let Some(option) = options.get(index)
|
||||
{
|
||||
on_change(value.clone(), window, cx);
|
||||
on_change(option.value.clone(), window, cx);
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -319,22 +321,26 @@ where
|
||||
.on_key_down({
|
||||
move |event: &KeyDownEvent, window, cx| {
|
||||
let target = match event.keystroke.key.as_str() {
|
||||
"right" => {
|
||||
adjacent_selectable(&key_selectable, selected_index, true)
|
||||
}
|
||||
"left" => {
|
||||
adjacent_selectable(&key_selectable, selected_index, false)
|
||||
}
|
||||
"home" => key_selectable.first().copied(),
|
||||
"end" => key_selectable.last().copied(),
|
||||
"right" => match selected_index {
|
||||
Some(index) => {
|
||||
index.checked_add(1).filter(|next| *next < option_count)
|
||||
}
|
||||
None => (option_count > 0).then_some(0),
|
||||
},
|
||||
"left" => selected_index.and_then(|index| index.checked_sub(1)),
|
||||
"home" => (option_count > 0).then_some(0),
|
||||
"end" => option_count.checked_sub(1),
|
||||
_ => return,
|
||||
};
|
||||
|
||||
if let Some(next) = target
|
||||
&& Some(next) != selected_index
|
||||
&& let Some(on_change) = &key_on_change
|
||||
&& let Some(option) = key_options.get(next)
|
||||
{
|
||||
on_change(key_options[next].value.clone(), window, cx);
|
||||
on_change(option.value.clone(), window, cx);
|
||||
}
|
||||
|
||||
cx.stop_propagation();
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user