Skip to content

Commit

Permalink
Add inactive selection color, tweak selection colors
Browse files Browse the repository at this point in the history
This patch changes the selection color to *not* red, and adds
a new SELECTED_TEXT_INACTIVE_BACKGROUND_COLOR theme item; it
also now draws the selection with that color when the text is
not focused.

This requires a bit of a hack because of how focus is working
in the textbox; the TextBox has focus, not the InputComponent,
but the InputComponent is responsible for drawing the selection
rects, so we need to explicitly pass down changes in focus state.

C'est la vie.

This also renames SELECTION_COLOR to SELECTED_TEXT_BACKGROUND_COLOR,
for clarity & symmetry.
  • Loading branch information
cmyr committed Mar 17, 2021
1 parent 3e763f4 commit c6142e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
16 changes: 15 additions & 1 deletion druid/src/text/input_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ use crate::{theme, Cursor, Env, Modifiers, Selector, TextAlignment, UpdateCtx};
pub struct TextComponent<T> {
inner: Arc<RefCell<EditSession<T>>>,
lock: Arc<Cell<ImeLock>>,
// HACK: because of the way focus works (it is managed higher up, in
// whatever widget is controlling this) we can't rely on `is_focused` in
// the PaintCtx.
/// A manual flag set by the parent to control drawing behaviour.
///
/// The parent should update this when handling [`LifeCycle::FocusChanged`].
pub has_focus: bool,
}

/// Editable text state.
Expand Down Expand Up @@ -393,7 +400,13 @@ impl<T: TextStorage + EditableText> Widget<T> for TextComponent<T> {
if !self.can_read() {
tracing::warn!("Text paint called with IME lock held.");
}
let selection_color = env.get(theme::SELECTION_COLOR);

let selection_color = if self.has_focus {
env.get(theme::SELECTED_TEXT_BACKGROUND_COLOR)
} else {
env.get(theme::SELECTED_TEXT_INACTIVE_BACKGROUND_COLOR)
};

let cursor_color = env.get(theme::CURSOR_COLOR);
let text_offset = Vec2::new(self.borrow().alignment_offset, 0.0);

Expand Down Expand Up @@ -824,6 +837,7 @@ impl<T> Default for TextComponent<T> {
TextComponent {
inner: Arc::new(RefCell::new(inner)),
lock: Arc::new(Cell::new(ImeLock::None)),
has_focus: false,
}
}
}
13 changes: 11 additions & 2 deletions druid/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ pub const BUTTON_BORDER_WIDTH: Key<f64> =
Key::new("org.linebender.druid.theme.button_border_width");
pub const BORDER_DARK: Key<Color> = Key::new("org.linebender.druid.theme.border_dark");
pub const BORDER_LIGHT: Key<Color> = Key::new("org.linebender.druid.theme.border_light");
pub const SELECTION_COLOR: Key<Color> = Key::new("org.linebender.druid.theme.selection_color");
#[deprecated(since = "0.8.0", note = "use SELECTED_TEXT_BACKGROUND_COLOR instead")]
pub const SELECTION_COLOR: Key<Color> = SELECTED_TEXT_BACKGROUND_COLOR;
pub const SELECTED_TEXT_BACKGROUND_COLOR: Key<Color> =
Key::new("org.linebender.druid.theme.selection_color");
pub const SELECTED_TEXT_INACTIVE_BACKGROUND_COLOR: Key<Color> =
Key::new("org.linebender.druid.theme.selection_color_inactive");
pub const SELECTION_TEXT_COLOR: Key<Color> =
Key::new("org.linebender.druid.theme.selection_text_color");
pub const CURSOR_COLOR: Key<Color> = Key::new("org.linebender.druid.theme.cursor_color");
Expand Down Expand Up @@ -114,7 +119,11 @@ pub(crate) fn add_to_env(env: Env) -> Env {
.adding(BUTTON_BORDER_WIDTH, 2.)
.adding(BORDER_DARK, Color::rgb8(0x3a, 0x3a, 0x3a))
.adding(BORDER_LIGHT, Color::rgb8(0xa1, 0xa1, 0xa1))
.adding(SELECTION_COLOR, Color::rgb8(0xf3, 0x00, 0x21))
.adding(
SELECTED_TEXT_BACKGROUND_COLOR,
Color::rgb8(0x43, 0x70, 0xA8),
)
.adding(SELECTED_TEXT_INACTIVE_BACKGROUND_COLOR, Color::grey8(0x74))
.adding(SELECTION_TEXT_COLOR, Color::rgb8(0x00, 0x00, 0x00))
.adding(CURSOR_COLOR, Color::WHITE)
.adding(TEXT_SIZE_NORMAL, 15.0)
Expand Down
2 changes: 2 additions & 0 deletions druid/src/widget/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
let _ = self.text_mut().borrow_mut().set_selection(selection);
ctx.invalidate_text_input(druid_shell::text::Event::SelectionChanged);
}
self.inner.wrapped_mut().child_mut().has_focus = true;
self.reset_cursor_blink(ctx.request_timer(CURSOR_BLINK_DURATION));
self.was_focused_from_click = false;
ctx.request_paint();
Expand All @@ -440,6 +441,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
let _ = self.text_mut().borrow_mut().set_selection(selection);
ctx.invalidate_text_input(druid_shell::text::Event::SelectionChanged);
}
self.inner.wrapped_mut().child_mut().has_focus = false;
self.cursor_timer = TimerToken::INVALID;
self.was_focused_from_click = false;
ctx.request_paint();
Expand Down

0 comments on commit c6142e6

Please sign in to comment.