use std::path::{Path, PathBuf};
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct IconDir {
path: PathBuf,
size: u16,
scale: u16,
context: Option<String>,
size_type: IconSizeType,
max_size: Option<u16>,
min_size: Option<u16>,
threshold: Option<u16>,
}
impl IconDir {
pub(crate) fn new(path: PathBuf, properties: &ini::Properties) -> Self {
let mut dir_info = Self {
path,
size: 0,
scale: 1,
context: None,
size_type: IconSizeType::Threshold,
max_size: None,
min_size: None,
threshold: None,
};
for (key, value) in properties.iter() {
match key {
"Size" => {
if let Ok(size) = value.parse() {
dir_info.size = size;
}
}
"Scale" => {
if let Ok(scale) = value.parse() {
dir_info.scale = scale;
}
}
"Context" => {
dir_info.context = Some(String::from(value));
}
"Type" => dir_info.size_type = value.into(),
"Threshold" => {
if let Ok(threshold) = value.parse() {
dir_info.threshold = Some(threshold);
}
}
"MinSize" => {
if let Ok(min_size) = value.parse() {
dir_info.min_size = Some(min_size);
}
}
"MaxSize" => {
if let Ok(max_size) = value.parse() {
dir_info.max_size = Some(max_size);
}
}
_ => {}
}
}
dir_info
}
pub fn path(&self) -> &Path {
&self.path
}
pub const fn size(&self) -> u16 {
self.size
}
pub const fn scale(&self) -> u16 {
self.scale
}
pub fn context(&self) -> Option<&str> {
self.context.as_deref()
}
pub const fn size_type(&self) -> IconSizeType {
self.size_type
}
pub fn max_size(&self) -> u16 {
self.max_size.unwrap_or_else(|| self.size())
}
pub fn min_size(&self) -> u16 {
self.min_size.unwrap_or_else(|| self.size())
}
pub fn threshold(&self) -> u16 {
self.threshold.unwrap_or(2)
}
pub(crate) const fn is_valid(&self) -> bool {
self.size != 0
}
}
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)]
pub enum IconSizeType {
Fixed,
Scalable,
Threshold,
}
impl<S: AsRef<str>> From<S> for IconSizeType {
fn from(s: S) -> Self {
match s.as_ref() {
"Fixed" => IconSizeType::Fixed,
"Scalable" => IconSizeType::Scalable,
_ => IconSizeType::Threshold,
}
}
}