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

Fix tab text color #32

Merged
merged 3 commits into from
Sep 6, 2022
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

### Added
- Added opt-in `serde` feature to enable serialization of `Tree`.
- You can now change the tab text color with `Style::tab_text_color_unfocused` and `Style::tab_text_color_focused`.

### Fixed
* `Tree::push_to_first_leaf` no longer panics when used on an empty `Tree`
- `Tree::push_to_first_leaf` no longer panics when used on an empty `Tree`.
- The tab text color will now follow the egui text color.


## 0.2.0 - 2022-09-04
Expand Down
34 changes: 30 additions & 4 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ pub struct Style {

pub border_color: Color32,
pub border_width: f32,

/// Color used when previewing where a tab will end up.
pub selection_color: Color32,

pub separator_width: f32,
pub separator_extra: f32,
pub separator_color: Color32,
Expand All @@ -20,6 +23,9 @@ pub struct Style {
pub tab_rounding: Rounding,
pub tab_background_color: Color32,

pub tab_text_color_unfocused: Color32,
pub tab_text_color_focused: Color32,

pub close_tab_color: Color32,
pub close_tab_active_color: Color32,
pub close_tab_background_color: Color32,
Expand All @@ -42,8 +48,11 @@ impl Default for Style {
tab_bar_background_color: Color32::WHITE,

tab_outline_color: Color32::BLACK,
tab_background_color: Color32::WHITE,
tab_rounding: Default::default(),
tab_background_color: Color32::WHITE,

tab_text_color_unfocused: Color32::DARK_GRAY,
tab_text_color_focused: Color32::BLACK,

close_tab_color: Color32::WHITE,
close_tab_active_color: Color32::WHITE,
Expand Down Expand Up @@ -74,6 +83,9 @@ impl Style {
tab_outline_color: style.visuals.widgets.active.bg_fill,
tab_background_color: style.visuals.window_fill(),

tab_text_color_unfocused: style.visuals.text_color(),
tab_text_color_focused: style.visuals.strong_text_color(),

separator_color: style.visuals.widgets.active.bg_fill,
border_color: style.visuals.widgets.active.bg_fill,

Expand Down Expand Up @@ -166,6 +178,7 @@ impl Style {
)
}

/// `active` means "the tab that is opened in the parent panel".
pub(crate) fn tab_title(
&self,
ui: &mut Ui,
Expand All @@ -178,7 +191,7 @@ impl Style {
let px = ui.ctx().pixels_per_point().recip();
let rounding = self.tab_rounding;

let galley = label.into_galley(ui, None, 14.0, TextStyle::Button);
let galley = label.into_galley(ui, None, f32::INFINITY, TextStyle::Button);

let x_text_gap = 5.0;
let x_size = Vec2::new(galley.size().y / 1.3, galley.size().y / 1.3);
Expand Down Expand Up @@ -234,7 +247,20 @@ impl Style {
.anchor_rect(rect.shrink2(vec2(8.0, 5.0)))
.min;

ui.painter().galley(pos, galley.galley);
let override_text_color = if galley.galley_has_color {
None // respect the color the user has chosen
} else if focused {
Some(self.tab_text_color_focused)
} else {
Some(self.tab_text_color_unfocused)
};
ui.painter().add(epaint::TextShape {
pos,
galley: galley.galley,
underline: Stroke::none(),
override_text_color,
angle: 0.0,
});

if (active || response.hovered()) && self.show_close_buttons {
if x_res.as_ref().unwrap().hovered() {
Expand Down Expand Up @@ -279,7 +305,7 @@ impl StyleBuilder {
Self::default()
}

/// Sets `padding` to indent from the edges of the window. By `Default` it's `None`.
/// Sets `padding` to indent from the edges of the window. By `Default` it's `None`.
#[inline(always)]
pub fn with_padding(mut self, padding: Option<Margin>) -> Self {
self.style.padding = padding;
Expand Down
1 change: 1 addition & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum Node<Tab> {
rect: Rect,
viewport: Rect,
tabs: Vec<Tab>,
/// The opened tab.
active: TabIndex,
},
/// Parent node in the vertical orientation
Expand Down