gpui: Reduce manual shifting & other minor improvements (#34407)

Minor cleanup in gpui.

- Reduce manual shifting by using `u32::to_be_bytes`
- Remove eager `Vec` allocation when listing registered actions
- Remove unnecessary return statements
- Replace manual `if let Some(_)` with `.as_deref_mut()`

Release Notes:

- N/A
This commit is contained in:
tidely 2025-07-15 17:39:33 +03:00 committed by GitHub
parent 05065985e7
commit d1abba0d33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 12 additions and 25 deletions

View file

@ -243,7 +243,6 @@ struct ActionDef {
fn dump_all_gpui_actions() -> Vec<ActionDef> { fn dump_all_gpui_actions() -> Vec<ActionDef> {
let mut actions = gpui::generate_list_of_all_registered_actions() let mut actions = gpui::generate_list_of_all_registered_actions()
.into_iter()
.map(|action| ActionDef { .map(|action| ActionDef {
name: action.name, name: action.name,
human_name: command_palette::humanize_action_name(action.name), human_name: command_palette::humanize_action_name(action.name),

View file

@ -403,12 +403,10 @@ impl ActionRegistry {
/// Useful for transforming the list of available actions into a /// Useful for transforming the list of available actions into a
/// format suited for static analysis such as in validating keymaps, or /// format suited for static analysis such as in validating keymaps, or
/// generating documentation. /// generating documentation.
pub fn generate_list_of_all_registered_actions() -> Vec<MacroActionData> { pub fn generate_list_of_all_registered_actions() -> impl Iterator<Item = MacroActionData> {
let mut actions = Vec::new(); inventory::iter::<MacroActionBuilder>
for builder in inventory::iter::<MacroActionBuilder> { .into_iter()
actions.push(builder.0()); .map(|builder| builder.0())
}
actions
} }
mod no_action { mod no_action {

View file

@ -1250,11 +1250,7 @@ impl App {
.downcast::<T>() .downcast::<T>()
.unwrap() .unwrap()
.update(cx, |entity_state, cx| { .update(cx, |entity_state, cx| {
if let Some(window) = window { on_new(entity_state, window.as_deref_mut(), cx)
on_new(entity_state, Some(window), cx);
} else {
on_new(entity_state, None, cx);
}
}) })
}, },
), ),

View file

@ -12,18 +12,13 @@ use std::{
/// Convert an RGB hex color code number to a color type /// Convert an RGB hex color code number to a color type
pub fn rgb(hex: u32) -> Rgba { pub fn rgb(hex: u32) -> Rgba {
let r = ((hex >> 16) & 0xFF) as f32 / 255.0; let [_, r, g, b] = hex.to_be_bytes().map(|b| (b as f32) / 255.0);
let g = ((hex >> 8) & 0xFF) as f32 / 255.0;
let b = (hex & 0xFF) as f32 / 255.0;
Rgba { r, g, b, a: 1.0 } Rgba { r, g, b, a: 1.0 }
} }
/// Convert an RGBA hex color code number to [`Rgba`] /// Convert an RGBA hex color code number to [`Rgba`]
pub fn rgba(hex: u32) -> Rgba { pub fn rgba(hex: u32) -> Rgba {
let r = ((hex >> 24) & 0xFF) as f32 / 255.0; let [r, g, b, a] = hex.to_be_bytes().map(|b| (b as f32) / 255.0);
let g = ((hex >> 16) & 0xFF) as f32 / 255.0;
let b = ((hex >> 8) & 0xFF) as f32 / 255.0;
let a = (hex & 0xFF) as f32 / 255.0;
Rgba { r, g, b, a } Rgba { r, g, b, a }
} }
@ -63,14 +58,14 @@ impl Rgba {
if other.a >= 1.0 { if other.a >= 1.0 {
other other
} else if other.a <= 0.0 { } else if other.a <= 0.0 {
return *self; *self
} else { } else {
return Rgba { Rgba {
r: (self.r * (1.0 - other.a)) + (other.r * other.a), r: (self.r * (1.0 - other.a)) + (other.r * other.a),
g: (self.g * (1.0 - other.a)) + (other.g * other.a), g: (self.g * (1.0 - other.a)) + (other.g * other.a),
b: (self.b * (1.0 - other.a)) + (other.b * other.a), b: (self.b * (1.0 - other.a)) + (other.b * other.a),
a: self.a, a: self.a,
}; }
} }
} }
} }
@ -494,12 +489,12 @@ impl Hsla {
if alpha >= 1.0 { if alpha >= 1.0 {
other other
} else if alpha <= 0.0 { } else if alpha <= 0.0 {
return self; self
} else { } else {
let converted_self = Rgba::from(self); let converted_self = Rgba::from(self);
let converted_other = Rgba::from(other); let converted_other = Rgba::from(other);
let blended_rgb = converted_self.blend(converted_other); let blended_rgb = converted_self.blend(converted_other);
return Hsla::from(blended_rgb); Hsla::from(blended_rgb)
} }
} }

View file

@ -1391,7 +1391,6 @@ fn dump_all_gpui_actions() {
documentation: Option<&'static str>, documentation: Option<&'static str>,
} }
let mut actions = gpui::generate_list_of_all_registered_actions() let mut actions = gpui::generate_list_of_all_registered_actions()
.into_iter()
.map(|action| ActionDef { .map(|action| ActionDef {
name: action.name, name: action.name,
human_name: command_palette::humanize_action_name(action.name), human_name: command_palette::humanize_action_name(action.name),