1
- use egui:: Color32 ;
1
+ use egui:: { Color32 , FontFamily , FontId , TextStyle } ;
2
+ use time:: UtcOffset ;
2
3
3
- use crate :: app:: ViewState ;
4
+ #[ derive( serde:: Deserialize , serde:: Serialize ) ]
5
+ #[ serde( default ) ]
6
+ pub struct Appearance {
7
+ pub ui_font : FontId ,
8
+ pub code_font : FontId ,
9
+ pub diff_colors : Vec < Color32 > ,
10
+ pub reverse_fn_order : bool ,
11
+ pub theme : eframe:: Theme ,
12
+
13
+ // Applied by theme
14
+ #[ serde( skip) ]
15
+ pub text_color : Color32 , // GRAY
16
+ #[ serde( skip) ]
17
+ pub emphasized_text_color : Color32 , // LIGHT_GRAY
18
+ #[ serde( skip) ]
19
+ pub deemphasized_text_color : Color32 , // DARK_GRAY
20
+ #[ serde( skip) ]
21
+ pub highlight_color : Color32 , // WHITE
22
+ #[ serde( skip) ]
23
+ pub replace_color : Color32 , // LIGHT_BLUE
24
+ #[ serde( skip) ]
25
+ pub insert_color : Color32 , // GREEN
26
+ #[ serde( skip) ]
27
+ pub delete_color : Color32 , // RED
28
+
29
+ // Global
30
+ #[ serde( skip) ]
31
+ pub utc_offset : UtcOffset ,
32
+ }
33
+
34
+ impl Default for Appearance {
35
+ fn default ( ) -> Self {
36
+ Self {
37
+ ui_font : FontId { size : 12.0 , family : FontFamily :: Proportional } ,
38
+ code_font : FontId { size : 14.0 , family : FontFamily :: Monospace } ,
39
+ diff_colors : DEFAULT_COLOR_ROTATION . to_vec ( ) ,
40
+ reverse_fn_order : false ,
41
+ theme : eframe:: Theme :: Dark ,
42
+ text_color : Color32 :: GRAY ,
43
+ emphasized_text_color : Color32 :: LIGHT_GRAY ,
44
+ deemphasized_text_color : Color32 :: DARK_GRAY ,
45
+ highlight_color : Color32 :: WHITE ,
46
+ replace_color : Color32 :: LIGHT_BLUE ,
47
+ insert_color : Color32 :: GREEN ,
48
+ delete_color : Color32 :: from_rgb ( 200 , 40 , 41 ) ,
49
+ utc_offset : UtcOffset :: UTC ,
50
+ }
51
+ }
52
+ }
53
+
54
+ impl Appearance {
55
+ pub fn apply ( & mut self , style : & egui:: Style ) -> egui:: Style {
56
+ let mut style = style. clone ( ) ;
57
+ style. text_styles . insert ( TextStyle :: Body , FontId {
58
+ size : ( self . ui_font . size * 0.75 ) . floor ( ) ,
59
+ family : self . ui_font . family . clone ( ) ,
60
+ } ) ;
61
+ style. text_styles . insert ( TextStyle :: Body , self . ui_font . clone ( ) ) ;
62
+ style. text_styles . insert ( TextStyle :: Button , self . ui_font . clone ( ) ) ;
63
+ style. text_styles . insert ( TextStyle :: Heading , FontId {
64
+ size : ( self . ui_font . size * 1.5 ) . floor ( ) ,
65
+ family : self . ui_font . family . clone ( ) ,
66
+ } ) ;
67
+ style. text_styles . insert ( TextStyle :: Monospace , self . code_font . clone ( ) ) ;
68
+ match self . theme {
69
+ eframe:: Theme :: Dark => {
70
+ style. visuals = egui:: Visuals :: dark ( ) ;
71
+ self . text_color = Color32 :: GRAY ;
72
+ self . emphasized_text_color = Color32 :: LIGHT_GRAY ;
73
+ self . deemphasized_text_color = Color32 :: DARK_GRAY ;
74
+ self . highlight_color = Color32 :: WHITE ;
75
+ self . replace_color = Color32 :: LIGHT_BLUE ;
76
+ self . insert_color = Color32 :: GREEN ;
77
+ self . delete_color = Color32 :: from_rgb ( 200 , 40 , 41 ) ;
78
+ }
79
+ eframe:: Theme :: Light => {
80
+ style. visuals = egui:: Visuals :: light ( ) ;
81
+ self . text_color = Color32 :: GRAY ;
82
+ self . emphasized_text_color = Color32 :: DARK_GRAY ;
83
+ self . deemphasized_text_color = Color32 :: LIGHT_GRAY ;
84
+ self . highlight_color = Color32 :: BLACK ;
85
+ self . replace_color = Color32 :: DARK_BLUE ;
86
+ self . insert_color = Color32 :: DARK_GREEN ;
87
+ self . delete_color = Color32 :: from_rgb ( 200 , 40 , 41 ) ;
88
+ }
89
+ }
90
+ style
91
+ }
92
+ }
4
93
5
94
pub const DEFAULT_COLOR_ROTATION : [ Color32 ; 9 ] = [
6
95
Color32 :: from_rgb ( 255 , 0 , 255 ) ,
@@ -14,31 +103,27 @@ pub const DEFAULT_COLOR_ROTATION: [Color32; 9] = [
14
103
Color32 :: from_rgb ( 213 , 138 , 138 ) ,
15
104
] ;
16
105
17
- pub fn appearance_window ( ctx : & egui:: Context , view_state : & mut ViewState ) {
18
- egui:: Window :: new ( "Appearance" ) . open ( & mut view_state . show_view_config ) . show ( ctx, |ui| {
106
+ pub fn appearance_window ( ctx : & egui:: Context , show : & mut bool , appearance : & mut Appearance ) {
107
+ egui:: Window :: new ( "Appearance" ) . open ( show ) . show ( ctx, |ui| {
19
108
egui:: ComboBox :: from_label ( "Theme" )
20
- . selected_text ( format ! ( "{:?}" , view_state . view_config . theme) )
109
+ . selected_text ( format ! ( "{:?}" , appearance . theme) )
21
110
. show_ui ( ui, |ui| {
22
- ui. selectable_value ( & mut view_state. view_config . theme , eframe:: Theme :: Dark , "Dark" ) ;
23
- ui. selectable_value (
24
- & mut view_state. view_config . theme ,
25
- eframe:: Theme :: Light ,
26
- "Light" ,
27
- ) ;
111
+ ui. selectable_value ( & mut appearance. theme , eframe:: Theme :: Dark , "Dark" ) ;
112
+ ui. selectable_value ( & mut appearance. theme , eframe:: Theme :: Light , "Light" ) ;
28
113
} ) ;
29
114
ui. label ( "UI font:" ) ;
30
- egui:: introspection:: font_id_ui ( ui, & mut view_state . view_config . ui_font ) ;
115
+ egui:: introspection:: font_id_ui ( ui, & mut appearance . ui_font ) ;
31
116
ui. separator ( ) ;
32
117
ui. label ( "Code font:" ) ;
33
- egui:: introspection:: font_id_ui ( ui, & mut view_state . view_config . code_font ) ;
118
+ egui:: introspection:: font_id_ui ( ui, & mut appearance . code_font ) ;
34
119
ui. separator ( ) ;
35
120
ui. label ( "Diff colors:" ) ;
36
121
if ui. button ( "Reset" ) . clicked ( ) {
37
- view_state . view_config . diff_colors = DEFAULT_COLOR_ROTATION . to_vec ( ) ;
122
+ appearance . diff_colors = DEFAULT_COLOR_ROTATION . to_vec ( ) ;
38
123
}
39
124
let mut remove_at: Option < usize > = None ;
40
- let num_colors = view_state . view_config . diff_colors . len ( ) ;
41
- for ( idx, color) in view_state . view_config . diff_colors . iter_mut ( ) . enumerate ( ) {
125
+ let num_colors = appearance . diff_colors . len ( ) ;
126
+ for ( idx, color) in appearance . diff_colors . iter_mut ( ) . enumerate ( ) {
42
127
ui. horizontal ( |ui| {
43
128
ui. color_edit_button_srgba ( color) ;
44
129
if num_colors > 1 && ui. small_button ( "-" ) . clicked ( ) {
@@ -47,10 +132,10 @@ pub fn appearance_window(ctx: &egui::Context, view_state: &mut ViewState) {
47
132
} ) ;
48
133
}
49
134
if let Some ( idx) = remove_at {
50
- view_state . view_config . diff_colors . remove ( idx) ;
135
+ appearance . diff_colors . remove ( idx) ;
51
136
}
52
137
if ui. small_button ( "+" ) . clicked ( ) {
53
- view_state . view_config . diff_colors . push ( Color32 :: BLACK ) ;
138
+ appearance . diff_colors . push ( Color32 :: BLACK ) ;
54
139
}
55
140
} ) ;
56
141
}
0 commit comments