From c37593343a6368f55aad7c55d094e2b1858058a5 Mon Sep 17 00:00:00 2001 From: temportalflux Date: Sat, 6 Jun 2026 18:07:53 -0400 Subject: [PATCH] expose api for users to configure styling colors for input elements --- crates/gpui_elements/src/input/colors.rs | 10 +++--- crates/gpui_elements/src/input/element.rs | 42 ++++++++++++++++++++--- crates/gpui_elements/src/input/paint.rs | 6 ++-- crates/gpui_elements/src/input/state.rs | 2 +- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/crates/gpui_elements/src/input/colors.rs b/crates/gpui_elements/src/input/colors.rs index f21737034c..210db77453 100644 --- a/crates/gpui_elements/src/input/colors.rs +++ b/crates/gpui_elements/src/input/colors.rs @@ -1,18 +1,20 @@ use gpui::Hsla; #[derive(Clone, Copy, Debug)] -pub struct PaintColors { +pub struct InputColors { pub selection: Hsla, pub cursor: Hsla, pub placeholder: Hsla, + pub marked: Hsla, } -impl Default for PaintColors { +impl Default for InputColors { fn default() -> Self { Self { - selection: Hsla::blue().opacity(0.2), + selection: gpui::hsla(0.583, 0.519, 0.31, 1.0), cursor: Hsla::white().opacity(0.8), - placeholder: gpui::hsla(0.6, 0.6, 0.6, 1.0), + marked: Hsla::white().opacity(0.6), + placeholder: gpui::hsla(0., 0., 0.5, 1.0), } } } diff --git a/crates/gpui_elements/src/input/element.rs b/crates/gpui_elements/src/input/element.rs index f016ee4292..31f7eabfaf 100644 --- a/crates/gpui_elements/src/input/element.rs +++ b/crates/gpui_elements/src/input/element.rs @@ -1,6 +1,6 @@ -use crate::input::{InputState, PaintColors}; +use crate::input::{InputColors, InputState}; use gpui::{ - Action, App, Context, Entity, FocusHandle, Focusable, InteractiveElement, Interactivity, + Action, App, Context, Entity, FocusHandle, Focusable, Hsla, InteractiveElement, Interactivity, IntoElement, SharedString, StyleRefinement, Styled, Window, }; @@ -14,7 +14,7 @@ pub struct Input { pub(super) input: Entity, pub(super) interactivity: Interactivity, pub(super) placeholder: Option, - pub(super) colors: PaintColors, + pub(super) colors: InputColors, } impl Input { @@ -25,7 +25,7 @@ impl Input { input: input_state.clone(), interactivity: Interactivity::new(), placeholder: None, - colors: PaintColors::default(), + colors: InputColors::default(), }; input.register_actions(); input @@ -129,6 +129,40 @@ impl Input { self.placeholder = Some(placeholder.into()); self } + + /// Sets the styling colors for the input element + pub fn colors(mut self, colors: InputColors) -> Self { + self.colors = colors; + self + } + + /// Sets the "selection" color for the input element. + /// This is the background color applied to the range of text that is currently selected by the user. + pub fn selection_color(mut self, color: Hsla) -> Self { + self.colors.selection = color; + self + } + + /// Sets the "cursor" color for the input element. + /// This is the color of the user's text cursor. + pub fn cursor_color(mut self, color: Hsla) -> Self { + self.colors.cursor = color; + self + } + + /// Sets the "placeholder" color for the input element. + /// This is the color of the placeholder string, when one is assigned and the text field is empty. + pub fn placeholder_color(mut self, color: Hsla) -> Self { + self.colors.placeholder = color; + self + } + + /// Sets the "marked" color for the input element. + /// Marking text comes from IME and needs further doc clarification. + pub fn marked_color(mut self, color: Hsla) -> Self { + self.colors.marked = color; + self + } } fn register_action( diff --git a/crates/gpui_elements/src/input/paint.rs b/crates/gpui_elements/src/input/paint.rs index ed4fdeff32..6a86ae721a 100644 --- a/crates/gpui_elements/src/input/paint.rs +++ b/crates/gpui_elements/src/input/paint.rs @@ -1,4 +1,4 @@ -use crate::input::{Input, InputLayoutData, InputLogicalLine, InputState, PaintColors}; +use crate::input::{Input, InputColors, InputLayoutData, InputLogicalLine, InputState}; use gpui::{ Along, App, Axis, Bounds, ContentMask, CursorStyle, DispatchPhase, Element, ElementId, ElementInputHandler, Entity, Focusable, GlobalElementId, Hitbox, HitboxBehavior, Hsla, @@ -228,7 +228,7 @@ struct PaintContext<'app> { bounds: Bounds, text_style: &'app TextStyle, placeholder: Option<&'app SharedString>, - colors: &'app PaintColors, + colors: &'app InputColors, cursor_visible: bool, } @@ -491,7 +491,7 @@ impl<'app> PaintContext<'app> { window, line, marked_range, - self.colors.cursor, + self.colors.marked, underline_offset, ); } diff --git a/crates/gpui_elements/src/input/state.rs b/crates/gpui_elements/src/input/state.rs index 815482ef43..f882dfd0b9 100644 --- a/crates/gpui_elements/src/input/state.rs +++ b/crates/gpui_elements/src/input/state.rs @@ -95,7 +95,7 @@ pub(super) struct InputLogicalLine { /// The shaped and wrapped text for this line, if available. pub wrapped_line: Option>, /// The vertical offset from the top of the text area in pixels. - pub y_offset: Pixels, + pub y_offset: Pixels, // TODO: replace with a counter such that the offset is determined by multipling the counter by the line_height /// The number of visual lines this logical line spans (due to wrapping). pub visual_line_count: usize, }