chore: Add #[inline] to util methods. (#576)

This commit is contained in:
Jason Lee 2025-01-24 15:34:57 +08:00 committed by GitHub
parent 7aade44e7a
commit 86db990b51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 39 additions and 0 deletions

View file

@ -98,10 +98,12 @@ pub fn init(cx: &mut gpui::AppContext) {
table::init(cx);
}
#[inline]
pub fn locale() -> impl Deref<Target = str> {
rust_i18n::locale()
}
#[inline]
pub fn set_locale(locale: &str) {
rust_i18n::set_locale(locale)
}

View file

@ -11,11 +11,13 @@ use gpui::{
use serde::{Deserialize, Serialize};
/// Returns a `Div` as horizontal flex layout.
#[inline]
pub fn h_flex() -> Div {
div().h_flex()
}
/// Returns a `Div` as vertical flex layout.
#[inline]
pub fn v_flex() -> Div {
div().v_flex()
}
@ -23,6 +25,7 @@ pub fn v_flex() -> Div {
macro_rules! font_weight {
($fn:ident, $const:ident) => {
/// [docs](https://tailwindcss.com/docs/font-weight)
#[inline]
fn $fn(self) -> Self {
self.font_weight(gpui::FontWeight::$const)
}
@ -32,11 +35,13 @@ macro_rules! font_weight {
/// Extends [`gpui::Styled`] with specific styling methods.
pub trait StyledExt: Styled + Sized {
/// Apply self into a horizontal flex layout.
#[inline]
fn h_flex(self) -> Self {
self.flex().flex_row().items_center()
}
/// Apply self into a vertical flex layout.
#[inline]
fn v_flex(self) -> Self {
self.flex().flex_col()
}
@ -100,6 +105,7 @@ pub trait StyledExt: Styled + Sized {
}
/// Render a border with a width of 1px, color ring color
#[inline]
fn outline(self, cx: &WindowContext) -> Self {
self.border_color(cx.theme().ring)
}
@ -107,6 +113,7 @@ pub trait StyledExt: Styled + Sized {
/// Wraps the element in a ScrollView.
///
/// Current this is only have a vertical scrollbar.
#[inline]
fn scrollable(self, view_id: EntityId, axis: ScrollbarAxis) -> Scrollable<Self>
where
Self: Element,
@ -125,6 +132,7 @@ pub trait StyledExt: Styled + Sized {
font_weight!(font_black, BLACK);
/// Set as Popover style
#[inline]
fn popover_style(self, cx: &mut WindowContext) -> Self {
self.bg(cx.theme().popover)
.border_1()
@ -149,6 +157,7 @@ pub enum Size {
impl Size {
/// Returns the height for table row.
#[inline]
pub fn table_row_height(&self) -> Pixels {
match self {
Size::XSmall => px(26.),
@ -159,6 +168,7 @@ impl Size {
}
/// Returns the padding for a table cell.
#[inline]
pub fn table_cell_padding(&self) -> Edges<Pixels> {
match self {
Size::XSmall => Edges {
@ -252,6 +262,7 @@ pub trait StyleSized<T: Styled> {
}
impl<T: Styled> StyleSized<T> for T {
#[inline]
fn input_text_size(self, size: Size) -> Self {
match size {
Size::XSmall => self.text_xs(),
@ -262,10 +273,12 @@ impl<T: Styled> StyleSized<T> for T {
}
}
#[inline]
fn input_size(self, size: Size) -> Self {
self.input_px(size).input_py(size).input_h(size)
}
#[inline]
fn input_pl(self, size: Size) -> Self {
match size {
Size::Large => self.pl_5(),
@ -274,6 +287,7 @@ impl<T: Styled> StyleSized<T> for T {
}
}
#[inline]
fn input_pr(self, size: Size) -> Self {
match size {
Size::Large => self.pr_5(),
@ -282,6 +296,7 @@ impl<T: Styled> StyleSized<T> for T {
}
}
#[inline]
fn input_px(self, size: Size) -> Self {
match size {
Size::Large => self.px_5(),
@ -290,6 +305,7 @@ impl<T: Styled> StyleSized<T> for T {
}
}
#[inline]
fn input_py(self, size: Size) -> Self {
match size {
Size::Large => self.py_5(),
@ -298,6 +314,7 @@ impl<T: Styled> StyleSized<T> for T {
}
}
#[inline]
fn input_h(self, size: Size) -> Self {
match size {
Size::Large => self.h_11(),
@ -307,10 +324,12 @@ impl<T: Styled> StyleSized<T> for T {
.input_text_size(size)
}
#[inline]
fn list_size(self, size: Size) -> Self {
self.list_px(size).list_py(size).input_text_size(size)
}
#[inline]
fn list_px(self, size: Size) -> Self {
match size {
Size::Small => self.px_2(),
@ -318,6 +337,7 @@ impl<T: Styled> StyleSized<T> for T {
}
}
#[inline]
fn list_py(self, size: Size) -> Self {
match size {
Size::Large => self.py_2(),
@ -327,6 +347,7 @@ impl<T: Styled> StyleSized<T> for T {
}
}
#[inline]
fn size_with(self, size: Size) -> Self {
match size {
Size::Large => self.size_11(),
@ -337,6 +358,7 @@ impl<T: Styled> StyleSized<T> for T {
}
}
#[inline]
fn table_cell_size(self, size: Size) -> Self {
let padding = size.table_cell_padding();
match size {
@ -357,10 +379,12 @@ pub trait AxisExt {
}
impl AxisExt for Axis {
#[inline]
fn is_horizontal(self) -> bool {
self == Axis::Horizontal
}
#[inline]
fn is_vertical(self) -> bool {
self == Axis::Vertical
}
@ -386,6 +410,7 @@ impl Display for Placement {
}
impl Placement {
#[inline]
pub fn is_horizontal(&self) -> bool {
match self {
Placement::Left | Placement::Right => true,
@ -393,6 +418,7 @@ impl Placement {
}
}
#[inline]
pub fn is_vertical(&self) -> bool {
match self {
Placement::Top | Placement::Bottom => true,
@ -400,6 +426,7 @@ impl Placement {
}
}
#[inline]
pub fn axis(&self) -> Axis {
match self {
Placement::Top | Placement::Bottom => Axis::Vertical,
@ -416,6 +443,7 @@ pub enum Side {
}
impl Side {
#[inline]
pub(crate) fn is_left(&self) -> bool {
matches!(self, Self::Left)
}

View file

@ -16,24 +16,28 @@ pub trait ActiveTheme {
}
impl ActiveTheme for AppContext {
#[inline]
fn theme(&self) -> &Theme {
Theme::global(self)
}
}
impl<V> ActiveTheme for ViewContext<'_, V> {
#[inline]
fn theme(&self) -> &Theme {
self.deref().theme()
}
}
impl<V> ActiveTheme for ModelContext<'_, V> {
#[inline]
fn theme(&self) -> &Theme {
self.deref().theme()
}
}
impl ActiveTheme for WindowContext<'_> {
#[inline]
fn theme(&self) -> &Theme {
self.deref().theme()
}
@ -44,6 +48,7 @@ impl ActiveTheme for WindowContext<'_> {
/// - h: 0..360.0
/// - s: 0.0..100.0
/// - l: 0.0..100.0
#[inline]
pub fn hsl(h: f32, s: f32, l: f32) -> Hsla {
hsla(h / 360., s / 100.0, l / 100.0, 1.0)
}
@ -55,6 +60,7 @@ pub fn hsl(h: f32, s: f32, l: f32) -> Hsla {
/// If CSS is `box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.1);`
///
/// Then the equivalent in Rust is `box_shadow(0., 0., 10., 0., hsla(0., 0., 0., 0.1))`
#[inline]
pub fn box_shadow(
x: impl Into<Pixels>,
y: impl Into<Pixels>,
@ -471,6 +477,7 @@ pub enum ThemeMode {
}
impl ThemeMode {
#[inline]
pub fn is_dark(&self) -> bool {
matches!(self, Self::Dark)
}

View file

@ -25,6 +25,7 @@ use smallvec::SmallVec;
/// This is like `uniform_list` in GPUI, but support two axis.
///
/// The `item_sizes` is the size of each column.
#[inline]
pub fn v_virtual_list<R, V>(
view: View<V>,
id: impl Into<ElementId>,
@ -39,6 +40,7 @@ where
}
/// Create a virtual list in Horizontal direction.
#[inline]
pub fn h_virtual_list<R, V>(
view: View<V>,
id: impl Into<ElementId>,