Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added selection text color to textbox. #1093

Merged
merged 2 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ Hilmar Gústafsson
Dmitry Borodin
Kaiyin Zhong
Kaur Kuut
Leopold Luley
Leopold Luley
Andrey Kabylin
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ You can find its changes [documented below](#060---2020-06-01).
- Export `Image` and `ImageData` by default. ([#1011] by [@covercash2])
- Re-export `druid_shell::Scalable` under `druid` namespace. ([#1075] by [@ForLoveOfCats])
- `TextBox` now supports ctrl and shift hotkeys. ([#1076] by [@vkahl])
- Added selection text color to textbox. ([#1093] by [@sysint64])

### Changed

Expand Down Expand Up @@ -254,6 +255,7 @@ Last release without a changelog :(
[@chris-zen]: https://github.com/chris-zen
[@vkahl]: https://github.com/vkahl
[@psychon]: https://github.com/psychon
[@sysint64]: https://github.com/sysint64

[#599]: https://github.com/linebender/druid/pull/599
[#611]: https://github.com/linebender/druid/pull/611
Expand Down Expand Up @@ -369,8 +371,10 @@ Last release without a changelog :(
[#1081]: https://github.com/linebender/druid/pull/1081
[#1096]: https://github.com/linebender/druid/pull/1096
[#1097]: https://github.com/linebender/druid/pull/1097
[#1093]: https://github.com/linebender/druid/pull/1093
[#1100]: https://github.com/linebender/druid/pull/1100


[Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0
[0.5.0]: https://github.com/linebender/druid/compare/v0.4.0...v0.5.0
Expand Down
2 changes: 2 additions & 0 deletions druid/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub const BUTTON_BORDER_WIDTH: Key<f64> = Key::new("button_border_width");
pub const BORDER_DARK: Key<Color> = Key::new("border");
pub const BORDER_LIGHT: Key<Color> = Key::new("border_light");
pub const SELECTION_COLOR: Key<Color> = Key::new("selection_color");
pub const SELECTION_TEXT_COLOR: Key<Color> = Key::new("selection_text_color");
pub const CURSOR_COLOR: Key<Color> = Key::new("cursor_color");

pub const FONT_NAME: Key<&str> = Key::new("font_name");
Expand Down Expand Up @@ -79,6 +80,7 @@ pub fn init() -> Env {
.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(SELECTION_TEXT_COLOR, Color::rgb8(0x00, 0x00, 0x00))
.adding(CURSOR_COLOR, Color::WHITE)
.adding(TEXT_SIZE_NORMAL, 15.0)
.adding(TEXT_SIZE_LARGE, 24.0)
Expand Down
27 changes: 16 additions & 11 deletions druid/src/widget/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ impl Widget<String> for TextBox {
let height = env.get(theme::BORDERED_WIDGET_HEIGHT);
let background_color = env.get(theme::BACKGROUND_LIGHT);
let selection_color = env.get(theme::SELECTION_COLOR);
let selection_text_color = env.get(theme::SELECTION_TEXT_COLOR);
let text_color = env.get(theme::LABEL_COLOR);
let placeholder_color = env.get(theme::PLACEHOLDER_COLOR);
let cursor_color = env.get(theme::CURSOR_COLOR);
Expand Down Expand Up @@ -429,6 +430,17 @@ impl Widget<String> for TextBox {
// Shift everything inside the clip by the hscroll_offset
rc.transform(Affine::translate((-self.hscroll_offset, 0.)));

// Layout, measure, and draw text
let text_height = font_size * 0.8;
let text_pos = Point::new(0.0 + PADDING_LEFT, text_height + PADDING_TOP);
let color = if data.is_empty() {
&placeholder_color
} else {
&text_color
};

rc.draw_text(&text_layout, text_pos, color);

// Draw selection rect
if !self.selection.is_caret() {
let (left, right) = (self.selection.min(), self.selection.max());
Expand All @@ -445,18 +457,11 @@ impl Widget<String> for TextBox {
1.,
);
rc.fill(selection_rect, &selection_color);
}

// Layout, measure, and draw text
let text_height = font_size * 0.8;
let text_pos = Point::new(0.0 + PADDING_LEFT, text_height + PADDING_TOP);
let color = if data.is_empty() {
&placeholder_color
} else {
&text_color
};

rc.draw_text(&text_layout, text_pos, color);
// Draw selection text
rc.clip(selection_rect);
rc.draw_text(&text_layout, text_pos, &selection_text_color);
}

// Paint the cursor if focused and there's no selection
if is_focused && self.cursor_on && self.selection.is_caret() {
Expand Down