Skip to content

Commit

Permalink
chore: custom text input style
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Aug 29, 2023
1 parent 2e3d9af commit 6a07e34
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/widget/text_input/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,21 @@ pub trait StyleSheet {
fn disabled(&self, style: &Self::Style) -> Appearance;
}

#[derive(Copy, Clone, Default)]
#[derive(Default)]
pub enum TextInput {
#[default]
Default,
ExpandableSearch,
Search,
Inline,
Custom {
active: Box<dyn Fn(&crate::Theme) -> Appearance>,
error: Box<dyn Fn(&crate::Theme) -> Appearance>,
hovered: Box<dyn Fn(&crate::Theme) -> Appearance>,
focused: Box<dyn Fn(&crate::Theme) -> Appearance>,
disabled: Box<dyn Fn(&crate::Theme) -> Appearance>,
placeholder_color: Box<dyn Fn(&crate::Theme) -> Color>,
},
}

impl StyleSheet for crate::Theme {
Expand Down Expand Up @@ -113,6 +121,7 @@ impl StyleSheet for crate::Theme {
selected_fill: palette.accent_color().into(),
label_color: label_color.into(),
},
TextInput::Custom { active, .. } => active(self),
}
}

Expand Down Expand Up @@ -157,6 +166,7 @@ impl StyleSheet for crate::Theme {
selected_fill: palette.accent_color().into(),
label_color: label_color.into(),
},
TextInput::Custom { error, .. } => error(self),
}
}

Expand Down Expand Up @@ -212,6 +222,7 @@ impl StyleSheet for crate::Theme {
selected_fill: palette.accent_color().into(),
label_color: label_color.into(),
},
TextInput::Custom { hovered, .. } => hovered(self),
}
}

Expand Down Expand Up @@ -258,17 +269,27 @@ impl StyleSheet for crate::Theme {
selected_fill: palette.accent_color().into(),
label_color: label_color.into(),
},
TextInput::Custom { focused, .. } => focused(self),
}
}

fn placeholder_color(&self, _style: &Self::Style) -> Color {
fn placeholder_color(&self, style: &Self::Style) -> Color {
if let TextInput::Custom {
placeholder_color, ..
} = style
{
return placeholder_color(self);
}
let palette = self.cosmic();
let mut neutral_9 = palette.palette.neutral_9;
neutral_9.alpha = 0.7;
neutral_9.into()
}

fn disabled(&self, style: &Self::Style) -> Appearance {
if let TextInput::Custom { disabled, .. } = style {
return disabled(self);
}
self.active(style)
}
}

0 comments on commit 6a07e34

Please sign in to comment.