Skip to content

Commit

Permalink
Add a from_egui method to the smaller style structs. Include the gl…
Browse files Browse the repository at this point in the history
…obal `TabsStyle` in `TabViewer::tab_style_override`.
  • Loading branch information
Adanos020 committed May 24, 2023
1 parent 3c8722f commit a660497
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 44 deletions.
113 changes: 76 additions & 37 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,86 @@ impl Style {
/// Fields overwritten by [`egui::Style`] are:
/// - [`Style::border`]
/// - [`Style::selection_color`]
///
/// See also: [`ButtonsStyle::from_egui`], [`SeparatorStyle::from_egui`], [`TabBarStyle::from_egui`],
/// [`TabsStyle::from_egui`]
pub fn from_egui(style: &egui::Style) -> Self {
Self {
border: Stroke {
color: style.visuals.widgets.active.bg_fill,
..Stroke::default()
},
selection_color: style.visuals.selection.bg_fill.linear_multiply(0.5),
buttons: ButtonsStyle::from_egui(style),
separator: SeparatorStyle::from_egui(style),
tab_bar: TabBarStyle::from_egui(style),
tabs: TabsStyle::from_egui(style),
..Self::default()
}
}
}

impl ButtonsStyle {
/// Derives relevant fields from `egui::Style` and sets the remaining fields to their default values.
///
/// Fields overwritten by [`egui::Style`] are:
/// - [`ButtonsStyle::close_tab_bg_fill`]
/// - [`ButtonsStyle::close_tab_color`]
/// - [`ButtonsStyle::close_tab_active_color`]
/// - [`ButtonsStyle::add_tab_bg_fill`]
/// - [`ButtonsStyle::add_tab_color`]
/// - [`ButtonsStyle::add_tab_active_color`]
pub fn from_egui(style: &egui::Style) -> Self {
Self {
close_tab_bg_fill: style.visuals.widgets.active.bg_fill,
close_tab_color: style.visuals.text_color(),
close_tab_active_color: style.visuals.strong_text_color(),
add_tab_bg_fill: style.visuals.widgets.active.bg_fill,
add_tab_color: style.visuals.text_color(),
add_tab_active_color: style.visuals.strong_text_color(),
add_tab_border_color: style.visuals.widgets.active.bg_fill,
..ButtonsStyle::default()
}
}
}

impl SeparatorStyle {
/// Derives relevant fields from `egui::Style` and sets the remaining fields to their default values.
///
/// Fields overwritten by [`egui::Style`] are:
/// - [`SeparatorStyle::color_idle`]
/// - [`SeparatorStyle::color_hovered`]
/// - [`SeparatorStyle::color_dragged`]
pub fn from_egui(style: &egui::Style) -> Self {
Self {
// Same as egui panel resize colors:
color_idle: style.visuals.widgets.noninteractive.bg_stroke.color, // dim
color_hovered: style.visuals.widgets.hovered.fg_stroke.color, // bright
color_dragged: style.visuals.widgets.active.fg_stroke.color, // bright
..SeparatorStyle::default()
}
}
}

impl TabBarStyle {
/// Derives relevant fields from `egui::Style` and sets the remaining fields to their default values.
///
/// Fields overwritten by [`egui::Style`] are:
/// - [`TabBarStyle::bg_fill`]
/// - [`TabBarStyle::hline_color`]
pub fn from_egui(style: &egui::Style) -> Self {
Self {
bg_fill: (Rgba::from(style.visuals.window_fill()) * Rgba::from_gray(0.7)).into(),
hline_color: style.visuals.widgets.active.bg_fill,
..TabBarStyle::default()
}
}
}

impl TabsStyle {
/// Derives relevant fields from `egui::Style` and sets the remaining fields to their default values.
///
/// Fields overwritten by [`egui::Style`] are:
/// - [`TabsStyle::outline_color`]
/// - [`TabsStyle::bg_fill`]
/// - [`TabsStyle::text_color_unfocused`]
Expand All @@ -239,43 +308,13 @@ impl Style {
/// - [`TabsStyle::text_color_active_focused`]
pub fn from_egui(style: &egui::Style) -> Self {
Self {
border: Stroke {
color: style.visuals.widgets.active.bg_fill,
..Stroke::default()
},
selection_color: style.visuals.selection.bg_fill.linear_multiply(0.5),
buttons: ButtonsStyle {
close_tab_bg_fill: style.visuals.widgets.active.bg_fill,
close_tab_color: style.visuals.text_color(),
close_tab_active_color: style.visuals.strong_text_color(),
add_tab_bg_fill: style.visuals.widgets.active.bg_fill,
add_tab_color: style.visuals.text_color(),
add_tab_active_color: style.visuals.strong_text_color(),
add_tab_border_color: style.visuals.widgets.active.bg_fill,
..ButtonsStyle::default()
},
separator: SeparatorStyle {
// Same as egui panel resize colors:
color_idle: style.visuals.widgets.noninteractive.bg_stroke.color, // dim
color_hovered: style.visuals.widgets.hovered.fg_stroke.color, // bright
color_dragged: style.visuals.widgets.active.fg_stroke.color, // bright
..SeparatorStyle::default()
},
tab_bar: TabBarStyle {
bg_fill: (Rgba::from(style.visuals.window_fill()) * Rgba::from_gray(0.7)).into(),
hline_color: style.visuals.widgets.active.bg_fill,
..TabBarStyle::default()
},
tabs: TabsStyle {
outline_color: style.visuals.widgets.active.bg_fill,
bg_fill: style.visuals.window_fill(),
text_color_unfocused: style.visuals.text_color(),
text_color_focused: style.visuals.strong_text_color(),
text_color_active_unfocused: style.visuals.text_color(),
text_color_active_focused: style.visuals.strong_text_color(),
..TabsStyle::default()
},
..Self::default()
outline_color: style.visuals.widgets.active.bg_fill,
bg_fill: style.visuals.window_fill(),
text_color_unfocused: style.visuals.text_color(),
text_color_focused: style.visuals.strong_text_color(),
text_color_active_unfocused: style.visuals.text_color(),
text_color_active_focused: style.visuals.strong_text_color(),
..TabsStyle::default()
}
}
}
11 changes: 5 additions & 6 deletions src/widgets/dock_area/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
let (is_active, label, tab_style) = {
let Node::Leaf { tabs, active, .. } = &mut self.tree[node_index] else { unreachable!() };
let style = self.style.as_ref().unwrap();
let tab_style = tab_viewer.tab_style_override(&tabs[tab_index.0]);
let tab_style = tab_viewer.tab_style_override(&tabs[tab_index.0], &style.tabs);
(
*active == tab_index || is_being_dragged,
tab_viewer.title(&mut tabs[tab_index.0]),
Expand Down Expand Up @@ -587,7 +587,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
let Node::Leaf { tabs, active, .. } = &mut self.tree[node_index] else { unreachable!() };
let tab = &mut tabs[tab_index.0];
let style = self.style.as_ref().unwrap();
let tab_style = tab_viewer.tab_style_override(tab);
let tab_style = tab_viewer.tab_style_override(tab, &style.tabs);
let tab_style = tab_style.as_ref().unwrap_or(&style.tabs);

if !is_active || tab_style.hline_below_active_tab_name {
Expand Down Expand Up @@ -931,10 +931,9 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
}
}

let tabs_style = tab_viewer.tab_style_override(tab);
let tabs_style = tabs_style
.as_ref()
.unwrap_or_else(|| &self.style.as_ref().unwrap().tabs);
let style = self.style.as_ref().unwrap();
let tabs_style = tab_viewer.tab_style_override(tab, &style.tabs);
let tabs_style = tabs_style.as_ref().unwrap_or_else(|| &style.tabs);
if tab_viewer.clear_background(tab) {
ui.painter().rect_filled(body_rect, 0.0, tabs_style.bg_fill);
}
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/tab_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub trait TabViewer {
}

/// Sets custom style for given tab.
fn tab_style_override(&self, _tab: &Self::Tab) -> Option<TabsStyle> {
fn tab_style_override(&self, _tab: &Self::Tab, _global_style: &TabsStyle) -> Option<TabsStyle> {
None
}

Expand Down

0 comments on commit a660497

Please sign in to comment.