1use super::{FrameVisuals, Theme, ThemeKind};
2use egui::{Color32, ComboBox, Frame, Response, Sense, Stroke, Ui};
3
4pub const TINT_1: Color32 = Color32::from_rgba_premultiplied(216, 216, 216, 255);
8
9pub 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
28pub fn apply_theme_changes(theme: &mut Theme, ui: &mut Ui) {
33 ui.style_mut().visuals = theme.style.visuals.clone();
34}
35
36pub fn no_border_on_idle(ui: &mut Ui) {
42 ui.visuals_mut().widgets.inactive.bg_stroke = Stroke::NONE;
43}
44
45pub fn no_border_on_click(ui: &mut Ui) {
49 ui.visuals_mut().widgets.active.bg_stroke = Stroke::NONE;
50}
51
52pub fn no_border_on_hover(ui: &mut Ui) {
56 ui.visuals_mut().widgets.hovered.bg_stroke = Stroke::NONE;
57}
58
59pub fn no_border(ui: &mut Ui) {
63 ui.visuals_mut().widgets.inactive.bg_stroke = Stroke::NONE;
64 ui.visuals_mut().widgets.active.bg_stroke = Stroke::NONE;
65 ui.visuals_mut().widgets.hovered.bg_stroke = Stroke::NONE;
66}
67
68pub fn border_on_idle(ui: &mut Ui, width: f32, color: Color32) {
72 ui.visuals_mut().widgets.inactive.bg_stroke = Stroke::new(width, color);
73}
74
75pub fn border_on_click(ui: &mut Ui, width: f32, color: Color32) {
79 ui.visuals_mut().widgets.active.bg_stroke = Stroke::new(width, color);
80}
81
82pub fn border_on_open(ui: &mut Ui, width: f32, color: Color32) {
86 ui.visuals_mut().widgets.open.bg_stroke = Stroke::new(width, color);
87}
88
89pub fn border_on_hover(ui: &mut Ui, width: f32, color: Color32) {
93 ui.visuals_mut().widgets.hovered.bg_stroke = Stroke::new(width, color);
94}
95
96pub fn bg_color_on_idle(ui: &mut Ui, color: Color32) {
100 ui.visuals_mut().widgets.inactive.weak_bg_fill = color;
101}
102
103pub fn bg_color_on_hover(ui: &mut Ui, color: Color32) {
107 ui.visuals_mut().widgets.hovered.weak_bg_fill = color;
108}
109
110pub fn bg_color_on_click(ui: &mut Ui, color: Color32) {
114 ui.visuals_mut().widgets.active.weak_bg_fill = color;
115}
116
117pub fn bg_color_on_open(ui: &mut Ui, color: Color32) {
119 ui.visuals_mut().widgets.open.weak_bg_fill = color;
120}
121
122pub fn window_fill(ui: &mut Ui, color: Color32) {
126 ui.visuals_mut().window_fill = color;
127}
128
129pub fn window_border(ui: &mut Ui, width: f32, color: Color32) {
133 ui.visuals_mut().window_stroke = Stroke::new(width, color);
134}
135
136pub fn frame_it(
138 frame: &mut Frame,
139 visuals: Option<FrameVisuals>,
140 ui: &mut Ui,
141 add_contents: impl FnOnce(&mut Ui),
142) -> Response {
143 let mut frame = frame.begin(ui);
144 let res = frame.content_ui.scope(|ui| add_contents(ui));
145
146 if let Some(visuals) = visuals {
147 if res.response.interact(Sense::click()).clicked() {
148 frame.frame = frame.frame.fill(visuals.bg_on_click);
149 frame.frame = frame.frame.stroke(Stroke::new(
150 visuals.border_on_click.0,
151 visuals.border_on_click.1,
152 ));
153 } else if res.response.hovered() {
154 frame.frame = frame.frame.fill(visuals.bg_on_hover);
155 frame.frame = frame.frame.stroke(Stroke::new(
156 visuals.border_on_hover.0,
157 visuals.border_on_hover.1,
158 ));
159 }
160 }
161 frame.end(ui);
162 res.response
163}