Skip to content

Conversation

@andreaskrath
Copy link
Contributor

@andreaskrath andreaskrath commented Nov 18, 2025

This makes use of the IconNamed trait and a blanked implementation to convert anything that implements this to an Icon.

This allows for easily defined custom versions of IconName, while minimally changing existing code (essentially only if you previously made use of the .path() method on the IconName enum; this now requires an import of the IconNamed trait).

Example

use gpui_component::IconNamed;

pub enum IconName {
    Encounters,
    Monsters,
    Spells,
}

impl IconNamed for IconName {
    fn path(self) -> gpui::SharedString {
        match self {
            IconName::Encounters => "icons/encounters.svg",
            IconName::Monsters => "icons/monsters.svg",
            IconName::Spells => "icons/spells.svg",
        }
        .into()
    }
}

// this allows for the following interactions (works with anything that has the `.icon(icon)` method
Button::new("my-button").icon(IconName::Spells);
Icon::new(IconName::Monsters); 

If you want to directly "render" a custom IconName you must implement the RenderOnce trait and derive IntoElement on the IconName.

use gpui::{IntoElement, RenderOnce};
use gpui_component::IconNamed;

#[derive(IntoElement)]
pub enum IconName {
    // The same as before
}

impl IconNamed for IconName {
    // The same as before
}

impl RenderOnce for IconName {
    fn render(self, _: &mut gpui::Window, _: &mut gpui::App) -> impl gpui::IntoElement {
        gpui_component::Icon::empty().path(self.path())
    }
}

// this allows for the following interaction
div()
    .child(IconName::Monsters)

Overall I think is an improvement to the existing way to do custom IconName implementations.

I am unsure if this change should also be reflected in the documentation on the section with "Icons & Assets", though I personally think it would make sense to highlight this way to do custom versions of IconName as it is considerably less involved than the current approach.

Closes #1627.

@huacnlee huacnlee changed the title rework icons to make use of the IconNamed trait icon: Rework icons to make use of the IconNamed trait Nov 20, 2025
@huacnlee huacnlee enabled auto-merge (squash) November 20, 2025 02:28
@huacnlee
Copy link
Member

Thank you. I have also updated the docs for this.

@huacnlee huacnlee merged commit d251a9b into longbridge:main Nov 20, 2025
4 checks passed
@andreaskrath andreaskrath deleted the icon-rework branch November 20, 2025 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Icon rework

2 participants