Skip to main content

zeus_theme/
utils.rs

1use super::{FrameVisuals, Theme, ThemeKind};
2use egui::{Color32, ComboBox, Frame, Response, Sense, Stroke, Ui};
3
4// Tint/Overlay colors, useful for example if you want to slightly darken an image on the fly
5
6/// Should work for most images that are shown on a very dark background
7pub const TINT_1: Color32 = Color32::from_rgba_premultiplied(216, 216, 216, 255);
8
9/// Show a ComboBox to change the theme
10///
11/// Returns the new theme if we select one, the new theme is also applied to the [egui::Context]
12pub fn change_theme(current_theme: &Theme, ui: &mut Ui) -> Option<Theme> {
13   let mut new_theme_opt = None;
14   ComboBox::from_label("Theme")
15      .selected_text(current_theme.kind.to_str())
16      .show_ui(ui, |ui| {
17         for kind in ThemeKind::to_vec() {
18            if ui.selectable_label(current_theme.kind == kind, kind.to_str()).clicked() {
19               let new_theme = Theme::new(kind);
20               ui.ctx().set_global_style(new_theme.style.clone());
21               new_theme_opt = Some(new_theme);
22            }
23         }
24      });
25   new_theme_opt
26}
27
28/// Apply any theme changes to the ui
29pub fn apply_theme_changes(theme: &mut Theme, ui: &mut Ui) {
30   theme.set_window_frame_colors();
31   theme.set_frame1_colors();
32   theme.set_frame2_colors();
33
34   ui.style_mut().visuals = theme.style.visuals.clone();
35}
36
37// Helper functions to override the visuals
38
39/// Removes the border from widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
40///
41/// When the widget is inactive (no hover, clicks etc..)
42pub fn no_border_on_idle(ui: &mut Ui) {
43   ui.visuals_mut().widgets.inactive.bg_stroke = Stroke::NONE;
44}
45
46/// Removes the border from widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
47///
48/// When the widget is active (click)
49pub fn no_border_on_click(ui: &mut Ui) {
50   ui.visuals_mut().widgets.active.bg_stroke = Stroke::NONE;
51}
52
53/// Removes the border from widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
54///
55/// When the widget is hovered
56pub fn no_border_on_hover(ui: &mut Ui) {
57   ui.visuals_mut().widgets.hovered.bg_stroke = Stroke::NONE;
58}
59
60/// Removes the border from widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
61///
62/// At any state
63pub fn no_border(ui: &mut Ui) {
64   ui.visuals_mut().widgets.inactive.bg_stroke = Stroke::NONE;
65   ui.visuals_mut().widgets.active.bg_stroke = Stroke::NONE;
66   ui.visuals_mut().widgets.hovered.bg_stroke = Stroke::NONE;
67}
68
69/// Give a border to widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
70///
71/// When the widget is inactive (no hover, clicks etc..)
72pub fn border_on_idle(ui: &mut Ui, width: f32, color: Color32) {
73   ui.visuals_mut().widgets.inactive.bg_stroke = Stroke::new(width, color);
74}
75
76/// Give a border to widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
77///
78/// When the widget is active (click)
79pub fn border_on_click(ui: &mut Ui, width: f32, color: Color32) {
80   ui.visuals_mut().widgets.active.bg_stroke = Stroke::new(width, color);
81}
82
83/// Give a border to widgets like ComboxBox
84///
85/// When the widget is open
86pub fn border_on_open(ui: &mut Ui, width: f32, color: Color32) {
87   ui.visuals_mut().widgets.open.bg_stroke = Stroke::new(width, color);
88}
89
90/// Give a border to widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
91///
92/// When the widget is hovered
93pub fn border_on_hover(ui: &mut Ui, width: f32, color: Color32) {
94   ui.visuals_mut().widgets.hovered.bg_stroke = Stroke::new(width, color);
95}
96
97/// Give a background color to widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
98///
99/// When the widget is inactive (no hover, clicks etc..)
100pub fn bg_color_on_idle(ui: &mut Ui, color: Color32) {
101   ui.visuals_mut().widgets.inactive.weak_bg_fill = color;
102}
103
104/// Give a background color to widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
105///
106/// When the widget is hovered
107pub fn bg_color_on_hover(ui: &mut Ui, color: Color32) {
108   ui.visuals_mut().widgets.hovered.weak_bg_fill = color;
109}
110
111/// Give a background color to widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
112///
113/// When the widget is active (click)
114pub fn bg_color_on_click(ui: &mut Ui, color: Color32) {
115   ui.visuals_mut().widgets.active.weak_bg_fill = color;
116}
117
118/// Give a background color to widgets like Button, ComboxBox, TextEdit, Slider, RadioButton
119pub fn bg_color_on_open(ui: &mut Ui, color: Color32) {
120   ui.visuals_mut().widgets.open.weak_bg_fill = color;
121}
122
123/// Window Fill Color
124///
125/// It also affects the bg color of an opened ComboBox
126pub fn window_fill(ui: &mut Ui, color: Color32) {
127   ui.visuals_mut().window_fill = color;
128}
129
130/// Window Border
131///
132/// It also affects the border of an opened ComboBox
133pub fn window_border(ui: &mut Ui, width: f32, color: Color32) {
134   ui.visuals_mut().window_stroke = Stroke::new(width, color);
135}
136
137/// Put this ui on top of this frame
138pub fn frame_it(
139   frame: &mut Frame,
140   visuals: Option<FrameVisuals>,
141   ui: &mut Ui,
142   add_contents: impl FnOnce(&mut Ui),
143) -> Response {
144   let mut frame = frame.begin(ui);
145   let res = frame.content_ui.scope(|ui| add_contents(ui));
146
147   if let Some(visuals) = visuals {
148      if res.response.interact(Sense::click()).clicked() {
149         frame.frame = frame.frame.fill(visuals.bg_on_click);
150         frame.frame = frame.frame.stroke(Stroke::new(
151            visuals.border_on_click.0,
152            visuals.border_on_click.1,
153         ));
154      } else if res.response.hovered() {
155         frame.frame = frame.frame.fill(visuals.bg_on_hover);
156         frame.frame = frame.frame.stroke(Stroke::new(
157            visuals.border_on_hover.0,
158            visuals.border_on_hover.1,
159         ));
160      }
161   }
162   frame.end(ui);
163   res.response
164}