1+ use crate :: raw_config:: constant:: {
2+ APP_GUI_SETTINGS , APP_SETTINGS , BACKGROUND_COLOR , BACKGROUND_COLOR_BUTTON ,
3+ FALLBACK_BG_BUTTON_COLOR , FALLBACK_BG_COLOR , FALLBACK_FG_BUTTON_COLOR ,
4+ FALLBACK_FG_COLOR , FOREGROUND_COLOR , FOREGROUND_COLOR_BUTTON ,
5+ HIDE_WHEN_CLOSED , HIDE_WHEN_CLOSED_FALLBACK , NOTIFICATION ,
6+ NOTIFICATION_FALLBACK ,
7+ } ;
18use std:: {
29 ffi:: { CStr , CString , c_char} ,
310 fs,
@@ -14,6 +21,8 @@ pub struct RawAppSettings {
1421pub struct RawAppGuiSettings {
1522 pub background_color : * mut c_char ,
1623 pub foreground_color : * mut c_char ,
24+ pub background_button_color : * mut c_char ,
25+ pub foreground_button_color : * mut c_char ,
1726}
1827
1928#[ repr( C ) ]
@@ -29,10 +38,38 @@ pub enum RawReadAppConfigStatus {
2938 Utf8Error ,
3039 ParseTomlFailed ,
3140 ConvertToMutFailed ,
41+ ConvertToCStringFailed ,
3242}
3343
34- const FALLBACK_BG_COLOR : & str = "#2f3136" ;
35- const FALLBACK_FG_COLOR : & str = "#ffffff" ;
44+ fn color_by_settings (
45+ toml_value : & Value ,
46+ field_name : & str ,
47+ fallback : & str ,
48+ ) -> String {
49+ let color = toml_value
50+ . get ( APP_GUI_SETTINGS )
51+ . and_then ( |value| value. get ( field_name) )
52+ . and_then ( Value :: as_str)
53+ . unwrap_or ( fallback) ;
54+
55+ color. to_string ( )
56+ }
57+
58+ fn raw_app_settings ( toml_value : & Value ) -> RawAppSettings {
59+ RawAppSettings {
60+ hide_when_closed : toml_value
61+ . get ( APP_SETTINGS )
62+ . and_then ( |value| value. get ( HIDE_WHEN_CLOSED ) )
63+ . and_then ( Value :: as_bool)
64+ . unwrap_or ( HIDE_WHEN_CLOSED_FALLBACK ) ,
65+
66+ notification : toml_value
67+ . get ( APP_SETTINGS )
68+ . and_then ( |value| value. get ( NOTIFICATION ) )
69+ . and_then ( Value :: as_bool)
70+ . unwrap_or ( NOTIFICATION_FALLBACK ) ,
71+ }
72+ }
3673
3774/// # Safety
3875/// Careful with raw pointers.
@@ -56,45 +93,48 @@ pub unsafe extern "C" fn raw_exists_config(
5693 return Status :: ParseTomlFailed ;
5794 } ;
5895
59- let app_settings = RawAppSettings {
60- hide_when_closed : toml_value
61- . get ( "app_settings" )
62- . and_then ( |value| value. get ( "hide_when_closed" ) )
63- . and_then ( Value :: as_bool)
64- . unwrap_or ( false ) ,
96+ let app_settings = raw_app_settings ( & toml_value) ;
97+
98+ let bg_color =
99+ color_by_settings ( & toml_value, BACKGROUND_COLOR , FALLBACK_BG_COLOR ) ;
100+ let fg_color =
101+ color_by_settings ( & toml_value, FOREGROUND_COLOR , FALLBACK_FG_COLOR ) ;
102+ let bg_button_color = color_by_settings (
103+ & toml_value,
104+ BACKGROUND_COLOR_BUTTON ,
105+ FALLBACK_BG_BUTTON_COLOR ,
106+ ) ;
107+ let fg_button_color = color_by_settings (
108+ & toml_value,
109+ FOREGROUND_COLOR_BUTTON ,
110+ FALLBACK_FG_BUTTON_COLOR ,
111+ ) ;
65112
66- notification : toml_value
67- . get ( "app_settings" )
68- . and_then ( |value| value. get ( "notification" ) )
69- . and_then ( Value :: as_bool)
70- . unwrap_or ( false ) ,
113+ let Ok ( bg_cstr) = CString :: new ( bg_color) else {
114+ return Status :: ConvertToCStringFailed ;
71115 } ;
72116
73- let bg_color = toml_value
74- . get ( "app_gui_settings" )
75- . and_then ( |value| value. get ( "background_color" ) )
76- . and_then ( Value :: as_str)
77- . unwrap_or ( FALLBACK_BG_COLOR ) ;
78-
79- let fg_color = toml_value
80- . get ( "app_gui_settings" )
81- . and_then ( |value| value. get ( "foreground_color" ) )
82- . and_then ( Value :: as_str)
83- . unwrap_or ( FALLBACK_FG_COLOR ) ;
117+ let Ok ( fg_cstr) = CString :: new ( fg_color) else {
118+ return Status :: ConvertToCStringFailed ;
119+ } ;
84120
85- let Ok ( bg_cstr ) = CString :: new ( bg_color ) else {
86- return Status :: ParseTomlFailed ;
121+ let Ok ( bg_btn_cstr ) = CString :: new ( bg_button_color ) else {
122+ return Status :: ConvertToCStringFailed ;
87123 } ;
88124
89- let Ok ( fg_cstr ) = CString :: new ( fg_color ) else {
90- return Status :: ParseTomlFailed ;
125+ let Ok ( fg_btn_cstr ) = CString :: new ( fg_button_color ) else {
126+ return Status :: ConvertToCStringFailed ;
91127 } ;
92128
93129 if let Some ( config) = unsafe { raw_cfg_out. as_mut ( ) } {
94130 config. app_settings . hide_when_closed = app_settings. hide_when_closed ;
95131 config. app_settings . notification = app_settings. notification ;
96132 config. app_gui_settings . background_color = bg_cstr. into_raw ( ) ;
97133 config. app_gui_settings . foreground_color = fg_cstr. into_raw ( ) ;
134+ config. app_gui_settings . background_button_color =
135+ bg_btn_cstr. into_raw ( ) ;
136+ config. app_gui_settings . foreground_button_color =
137+ fg_btn_cstr. into_raw ( ) ;
98138
99139 return RawReadAppConfigStatus :: Ok ;
100140 }
@@ -110,6 +150,10 @@ pub unsafe extern "C" fn raw_free_cstr_app_config(config: *mut RawAppConfig) {
110150 if let Some ( cfg) = config. as_mut ( ) {
111151 let _ = CString :: from_raw ( cfg. app_gui_settings . background_color ) ;
112152 let _ = CString :: from_raw ( cfg. app_gui_settings . foreground_color ) ;
153+ let _ =
154+ CString :: from_raw ( cfg. app_gui_settings . background_button_color ) ;
155+ let _ =
156+ CString :: from_raw ( cfg. app_gui_settings . foreground_button_color ) ;
113157 }
114158 }
115159}
0 commit comments