expose api for users to configure styling colors for input elements

This commit is contained in:
temportalflux
2026-07-11 09:31:07 -04:00
parent 4b23b30357
commit c37593343a
4 changed files with 48 additions and 12 deletions
+6 -4
View File
@@ -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),
}
}
}
+38 -4
View File
@@ -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<InputState>,
pub(super) interactivity: Interactivity,
pub(super) placeholder: Option<SharedString>,
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<A: Action>(
+3 -3
View File
@@ -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<Pixels>,
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,
);
}
+1 -1
View File
@@ -95,7 +95,7 @@ pub(super) struct InputLogicalLine {
/// The shaped and wrapped text for this line, if available.
pub wrapped_line: Option<Arc<WrappedLine>>,
/// 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,
}