File size: 3,775 Bytes
c1a5b70
0ec8914
5e2669b
0ec8914
 
 
b7a23f1
 
 
c1a5b70
 
 
 
 
b7a23f1
 
c1a5b70
 
 
 
 
0ec8914
 
b7a23f1
 
0ec8914
 
 
 
 
 
 
b7a23f1
 
 
 
 
 
 
0ec8914
 
 
 
 
c1a5b70
 
 
0ec8914
c1a5b70
 
b7a23f1
 
 
 
 
0ec8914
 
 
 
 
 
 
 
b7a23f1
 
 
0ec8914
 
 
 
 
 
 
 
b7a23f1
 
 
0ec8914
 
 
19081b7
 
 
 
 
b7a23f1
 
4013857
19081b7
 
 
0ec8914
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! A module that handles the user interface tab for setting page view in the `websurfx` frontend.

use crate::handler::{file_path, FileType};
use maud::{html, Markup};
use std::fs::read_dir;

/// A helper function that helps in building the list of all available colorscheme/theme/animation
/// names present in the colorschemes, animations and themes folder respectively by excluding the
/// ones that have already been selected via the config file.
///
/// # Arguments
///
/// * `style_type` - It takes the style type of the values `theme` and `colorscheme` as an
/// argument.
/// * `selected_style` - It takes the currently selected style value provided via the config file
/// as an argument.
///
/// # Error
///
/// Returns a list of colorscheme/theme names as a vector of tuple strings on success otherwise
/// returns a standard error message.
fn style_option_list(
    style_type: &str,
    selected_style: &str,
) -> Result<Vec<(String, String)>, Box<dyn std::error::Error>> {
    let mut style_option_names: Vec<(String, String)> = Vec::new();
    for file in read_dir(format!(
        "{}static/{}/",
        file_path(FileType::Theme)?,
        style_type,
    ))? {
        let style_name = file?.file_name().to_str().unwrap().replace(".css", "");
        if selected_style != style_name {
            style_option_names.push((style_name.clone(), style_name.replace('-', " ")));
        }
    }

    if style_type == "animations" {
        style_option_names.push(("".to_owned(), "none".to_owned()))
    }

    Ok(style_option_names)
}

/// A functions that handles the html code for the user interface tab for the settings page for the search page.
///
/// # Error
///
/// It returns the compiled html markup code for the user interface tab on success otherwise
/// returns a standard error message.
pub fn user_interface(
    theme: &str,
    colorscheme: &str,
    animation: &Option<String>,
) -> Result<Markup, Box<dyn std::error::Error>> {
    Ok(html!(
        div class="user_interface tab"{
           h1{"User Interface"}
           h3{"select theme"}
           p class="description"{
               "Select the theme from the available themes to be used in user interface"
           }
           select name="themes"{
               // Sets the user selected theme name from the config file as the first option in the selection list.
               option value=(theme){(theme.replace('-', " "))}
               @for (k,v) in style_option_list("themes", theme)?{
                   option value=(k){(v)}
               }
           }
           h3{"select color scheme"}
           p class="description"{
               "Select the color scheme for your theme to be used in user interface"
           }
           select name="colorschemes"{
               // Sets the user selected colorscheme name from the config file as the first option in the selection list.
               option value=(colorscheme){(colorscheme.replace('-', " "))}
               @for (k,v) in style_option_list("colorschemes", colorscheme)?{
                   option value=(k){(v)}
               }
           }
           h3{"select animation"}
           p class="description"{
               "Select the animation for your theme to be used in user interface"
           }
           select name="animations"{
               // Sets the user selected animation name from the config file as the first option in the selection list.
               option value=(animation.as_ref().unwrap_or(&"".to_owned())){(animation.as_ref().unwrap_or(&"".to_owned()).replace('-'," "))}
               @for (k,v) in style_option_list("animations", animation.as_ref().unwrap_or(&"".to_owned()))?{
                   option value=(k){(v)}
               }
           }
        }
    ))
}