Skip to content

Commit c6142e6

Browse files
committed
Add inactive selection color, tweak selection colors
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.
1 parent 3e763f4 commit c6142e6

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

druid/src/text/input_component.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ use crate::{theme, Cursor, Env, Modifiers, Selector, TextAlignment, UpdateCtx};
5656
pub struct TextComponent<T> {
5757
inner: Arc<RefCell<EditSession<T>>>,
5858
lock: Arc<Cell<ImeLock>>,
59+
// HACK: because of the way focus works (it is managed higher up, in
60+
// whatever widget is controlling this) we can't rely on `is_focused` in
61+
// the PaintCtx.
62+
/// A manual flag set by the parent to control drawing behaviour.
63+
///
64+
/// The parent should update this when handling [`LifeCycle::FocusChanged`].
65+
pub has_focus: bool,
5966
}
6067

6168
/// Editable text state.
@@ -393,7 +400,13 @@ impl<T: TextStorage + EditableText> Widget<T> for TextComponent<T> {
393400
if !self.can_read() {
394401
tracing::warn!("Text paint called with IME lock held.");
395402
}
396-
let selection_color = env.get(theme::SELECTION_COLOR);
403+
404+
let selection_color = if self.has_focus {
405+
env.get(theme::SELECTED_TEXT_BACKGROUND_COLOR)
406+
} else {
407+
env.get(theme::SELECTED_TEXT_INACTIVE_BACKGROUND_COLOR)
408+
};
409+
397410
let cursor_color = env.get(theme::CURSOR_COLOR);
398411
let text_offset = Vec2::new(self.borrow().alignment_offset, 0.0);
399412

@@ -824,6 +837,7 @@ impl<T> Default for TextComponent<T> {
824837
TextComponent {
825838
inner: Arc::new(RefCell::new(inner)),
826839
lock: Arc::new(Cell::new(ImeLock::None)),
840+
has_focus: false,
827841
}
828842
}
829843
}

druid/src/theme.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ pub const BUTTON_BORDER_WIDTH: Key<f64> =
4141
Key::new("org.linebender.druid.theme.button_border_width");
4242
pub const BORDER_DARK: Key<Color> = Key::new("org.linebender.druid.theme.border_dark");
4343
pub const BORDER_LIGHT: Key<Color> = Key::new("org.linebender.druid.theme.border_light");
44-
pub const SELECTION_COLOR: Key<Color> = Key::new("org.linebender.druid.theme.selection_color");
44+
#[deprecated(since = "0.8.0", note = "use SELECTED_TEXT_BACKGROUND_COLOR instead")]
45+
pub const SELECTION_COLOR: Key<Color> = SELECTED_TEXT_BACKGROUND_COLOR;
46+
pub const SELECTED_TEXT_BACKGROUND_COLOR: Key<Color> =
47+
Key::new("org.linebender.druid.theme.selection_color");
48+
pub const SELECTED_TEXT_INACTIVE_BACKGROUND_COLOR: Key<Color> =
49+
Key::new("org.linebender.druid.theme.selection_color_inactive");
4550
pub const SELECTION_TEXT_COLOR: Key<Color> =
4651
Key::new("org.linebender.druid.theme.selection_text_color");
4752
pub const CURSOR_COLOR: Key<Color> = Key::new("org.linebender.druid.theme.cursor_color");
@@ -114,7 +119,11 @@ pub(crate) fn add_to_env(env: Env) -> Env {
114119
.adding(BUTTON_BORDER_WIDTH, 2.)
115120
.adding(BORDER_DARK, Color::rgb8(0x3a, 0x3a, 0x3a))
116121
.adding(BORDER_LIGHT, Color::rgb8(0xa1, 0xa1, 0xa1))
117-
.adding(SELECTION_COLOR, Color::rgb8(0xf3, 0x00, 0x21))
122+
.adding(
123+
SELECTED_TEXT_BACKGROUND_COLOR,
124+
Color::rgb8(0x43, 0x70, 0xA8),
125+
)
126+
.adding(SELECTED_TEXT_INACTIVE_BACKGROUND_COLOR, Color::grey8(0x74))
118127
.adding(SELECTION_TEXT_COLOR, Color::rgb8(0x00, 0x00, 0x00))
119128
.adding(CURSOR_COLOR, Color::WHITE)
120129
.adding(TEXT_SIZE_NORMAL, 15.0)

druid/src/widget/textbox.rs

+2
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
429429
let _ = self.text_mut().borrow_mut().set_selection(selection);
430430
ctx.invalidate_text_input(druid_shell::text::Event::SelectionChanged);
431431
}
432+
self.inner.wrapped_mut().child_mut().has_focus = true;
432433
self.reset_cursor_blink(ctx.request_timer(CURSOR_BLINK_DURATION));
433434
self.was_focused_from_click = false;
434435
ctx.request_paint();
@@ -440,6 +441,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
440441
let _ = self.text_mut().borrow_mut().set_selection(selection);
441442
ctx.invalidate_text_input(druid_shell::text::Event::SelectionChanged);
442443
}
444+
self.inner.wrapped_mut().child_mut().has_focus = false;
443445
self.cursor_timer = TimerToken::INVALID;
444446
self.was_focused_from_click = false;
445447
ctx.request_paint();

0 commit comments

Comments
 (0)