11use anyhow:: Context ;
22use detect_desktop_environment:: DesktopEnvironment ;
33use ini:: Ini ;
4- use std:: path:: { Path , PathBuf } ;
54
65use crate :: Mode ;
76
8- const XDG_KDEGLOBALS : & str = "/etc/xdg/kdeglobals" ;
7+ use super :: { CINNAMON , GNOME , MATE } ;
98
10- fn detect_gtk ( pattern : & str ) -> Mode {
11- match dconf_rs :: get_string ( pattern ) {
12- Ok ( theme ) => Mode :: from ( theme . to_lowercase ( ) . contains ( "dark" ) ) ,
9+ pub fn detect ( ) -> Mode {
10+ match legacy_detect ( ) {
11+ Ok ( mode ) => mode ,
1312 Err ( _) => Mode :: Default ,
1413 }
1514}
1615
17- fn detect_kde ( path : & str ) -> anyhow:: Result < Mode > {
18- let cfg = Ini :: load_from_file ( path) ?;
19- let section = cfg. section ( Some ( "Colors:Window" ) ) . with_context ( || "Failed to get section Colors:Window" ) ?;
20- let values = section. get ( "BackgroundNormal" ) . with_context ( || "Failed to get BackgroundNormal inside Colors:Window" ) ?;
21- let rgb = values
22- . split ( ',' )
23- . map ( |s| s. parse :: < u32 > ( ) . unwrap_or ( 255 ) )
24- . collect :: < Vec < u32 > > ( ) ;
25- let rgb = if rgb. len ( ) >= 3 {
26- rgb
27- } else {
28- vec ! [ 255 , 255 , 255 ]
29- } ;
30- let ( r, g, b) = ( rgb[ 0 ] , rgb[ 1 ] , rgb[ 2 ] ) ;
31- Ok ( Mode :: from_rgb ( r, g, b) )
32- }
33-
3416fn legacy_detect ( ) -> anyhow:: Result < Mode > {
3517 let mode = match DesktopEnvironment :: detect ( ) {
36- DesktopEnvironment :: Kde => {
37- let path = if Path :: new ( XDG_KDEGLOBALS ) . exists ( ) {
38- PathBuf :: from ( XDG_KDEGLOBALS )
39- } else {
40- dirs:: home_dir ( ) . unwrap ( ) . join ( ".config/kdeglobals" )
41- } ;
42- detect_kde ( path. to_str ( ) . unwrap ( ) ) ?
43- }
44- DesktopEnvironment :: Cinnamon => detect_gtk ( "/org/cinnamon/desktop/interface/gtk-theme" ) ,
45- DesktopEnvironment :: Gnome => detect_gtk ( "/org/gnome/desktop/interface/gtk-theme" ) ,
46- DesktopEnvironment :: Mate => detect_gtk ( "/org/mate/desktop/interface/gtk-theme" ) ,
47- DesktopEnvironment :: Unity => detect_gtk ( "/org/gnome/desktop/interface/gtk-theme" ) ,
18+ DesktopEnvironment :: Kde => kde_detect ( ) ?,
19+ DesktopEnvironment :: Cinnamon => dconf_detect ( CINNAMON ) ,
20+ DesktopEnvironment :: Gnome => dconf_detect ( GNOME ) ,
21+ DesktopEnvironment :: Mate => dconf_detect ( MATE ) ,
22+ DesktopEnvironment :: Unity => dconf_detect ( GNOME ) ,
4823 _ => Mode :: Default ,
4924 } ;
5025 Ok ( mode)
5126}
5227
53- pub fn detect ( ) -> Mode {
54- match legacy_detect ( ) {
55- Ok ( mode ) => mode ,
28+ fn dconf_detect ( path : & str ) -> Mode {
29+ match dconf_rs :: get_string ( path ) {
30+ Ok ( theme ) => Mode :: from ( Some ( theme . to_lowercase ( ) . contains ( "dark" ) ) ) ,
5631 Err ( _) => Mode :: Default ,
5732 }
5833}
5934
35+ fn kde_detect ( ) -> anyhow:: Result < Mode > {
36+ let xdg = xdg:: BaseDirectories :: new ( ) ?;
37+ let path = xdg. find_config_file ( "kdeglobals" )
38+ . context ( "Path not found" ) ?;
39+ let cfg = Ini :: load_from_file ( path) ?;
40+ let properties = cfg. section ( Some ( "Colors:Window" ) )
41+ . context ( "Failed to get section Colors:Window" ) ?;
42+ let background = properties. get ( "BackgroundNormal" )
43+ . context ( "Failed to get BackgroundNormal inside Colors:Window" ) ?;
44+ let rgb = rgb_from_string ( background) ?;
45+ Ok ( Mode :: from_rgb ( & rgb) )
46+ }
47+
48+ fn rgb_from_string ( rgb : & str ) -> anyhow:: Result < Vec < u32 > > {
49+ rgb. split ( ',' )
50+ . map ( |s| s. parse :: < u32 > ( ) . unwrap_or_else ( |_| 255 ) )
51+ . try_fold ( vec ! [ 255 , 255 , 255 ] , |mut acc, x| {
52+ if acc. len ( ) < 3 {
53+ acc. push ( x) ;
54+ Ok ( acc)
55+ } else {
56+ Err ( anyhow:: anyhow!( "Too many elements" ) )
57+ }
58+ } )
59+ }
0 commit comments