Skip to content

Commit 4bb44c7

Browse files
authored
sidebar: make SidebarMenuItem implement Styled and add label_style (longbridge#2957)
## Description Make `SidebarMenuItem` implement `Styled` (adding `style: StyleRefinement`) and expose a dedicated `label_style` method / field. This brings the item API in line with `SidebarMenu` (which already implements `Styled`) and gives callers a clean way to customize both the item container and the label text independently. ## How to Test - Updated gallery example to verify both `style` and `label_style` apply correctly - Manually checked the story/gallery UI ## Checklist - [x] I have read the [CONTRIBUTING](../CONTRIBUTING.md) document and followed the guidelines. - [x] Reviewed the changes in this PR and confirmed AI generated code (If any) is accurate. - [x] Passed `cargo run` for story tests related to the changes. - [ ] Tested macOS, Windows and Linux platforms performance (if the change is platform-specific)
1 parent bc0ac76 commit 4bb44c7

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎crates/component/src/sidebar/menu.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ impl Styled for SidebarMenu {
9393
pub struct SidebarMenuItem {
9494
icon: Option<Icon>,
9595
label: SharedString,
96+
label_style: StyleRefinement,
97+
style: StyleRefinement,
9698
handler: Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
9799
active: bool,
98100
default_open: bool,
@@ -111,6 +113,8 @@ impl SidebarMenuItem {
111113
Self {
112114
icon: None,
113115
label: label.into(),
116+
label_style: StyleRefinement::default(),
117+
style: StyleRefinement::default(),
114118
handler: Rc::new(|_, _, _| {}),
115119
active: false,
116120
collapsed: false,
@@ -130,6 +134,12 @@ impl SidebarMenuItem {
130134
self
131135
}
132136

137+
/// Set the style for the label
138+
pub fn label_style(mut self, style: StyleRefinement) -> Self {
139+
self.label_style = style;
140+
self
141+
}
142+
133143
/// Set the active state of the menu item
134144
pub fn active(mut self, active: bool) -> Self {
135145
self.active = active;
@@ -273,6 +283,7 @@ impl SidebarItem for SidebarMenuItem {
273283
.gap_x_2()
274284
.rounded(cx.theme().radius)
275285
.text_sm()
286+
.refine_style(&self.style)
276287
.when(is_hoverable, |this| {
277288
this.hover(|this| {
278289
this.bg(cx.theme().sidebar_accent.opacity(0.8))
@@ -303,6 +314,7 @@ impl SidebarItem for SidebarMenuItem {
303314
h_flex()
304315
.flex_1()
305316
.overflow_x_hidden()
317+
.refine_style(&self.label_style)
306318
.child(self.label.clone()),
307319
)
308320
.when_some(self.suffix.clone(), |this, suffix| {
@@ -399,6 +411,12 @@ impl SidebarItem for SidebarMenuItem {
399411
}
400412
}
401413

414+
impl Styled for SidebarMenuItem {
415+
fn style(&mut self) -> &mut StyleRefinement {
416+
&mut self.style
417+
}
418+
}
419+
402420
#[cfg(test)]
403421
mod tests {
404422
use super::*;

‎crates/story/src/stories/sidebar_story.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22

3+
use gpui_kit::component::red_500;
34
use gpui_kit::prelude::FluentBuilder;
45
use gpui_kit::*;
56

@@ -551,6 +552,9 @@ impl Render for SidebarStory {
551552
.active(is_active)
552553
.default_open(ix == 0)
553554
.click_to_open(self.click_to_open_submenu)
555+
.when(is_active, |this| {
556+
this.border_1().border_color(Hsla::white())
557+
})
554558
.when(ix == 0, |this| {
555559
this.context_menu({
556560
move |this, _, _| {
@@ -580,6 +584,10 @@ impl Render for SidebarStory {
580584
))
581585
}
582586
})
587+
.label_style(
588+
StyleRefinement::default()
589+
.text_color(red_500()),
590+
)
583591
.context_menu({
584592
move |this, _, _| {
585593
this.label("This is a label")

0 commit comments

Comments
 (0)