Skip to content

Commit ea15002

Browse files
committed
fix(wall): wallpaper traits
1 parent 3df104e commit ea15002

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

libs/core/src/resource/interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub trait SluResource: Sized + Serialize + DeserializeOwned {
8383
/// Sanitize the resource data
8484
fn sanitize(&mut self) {}
8585

86-
/// Validates the resource
86+
/// Validates the resource after sanitization
8787
fn validate(&self) -> Result<()> {
8888
Ok(())
8989
}

libs/core/src/state/wallpaper/mod.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,31 @@ use crate::{
1111
};
1212

1313
#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema, TS)]
14+
#[serde(default, rename_all = "camelCase")]
1415
#[cfg_attr(feature = "gen-binds", ts(export))]
1516
pub struct Wallpaper {
1617
pub id: WallpaperId,
1718
pub metadata: ResourceMetadata,
19+
pub r#type: WallpaperKind,
1820
pub url: Option<Url>,
1921
pub thumbnail_url: Option<Url>,
2022
pub filename: Option<String>,
23+
#[serde(alias = "thumbnail_filename")]
2124
pub thumbnail_filename: Option<String>,
2225
}
2326

27+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema, TS)]
28+
#[ts(repr(enum = name))]
29+
pub enum WallpaperKind {
30+
#[serde(alias = "image")]
31+
Image,
32+
#[serde(alias = "video")]
33+
Video,
34+
/// used for wallpapers created before v2.4.9, will be changed on sanitization
35+
#[default]
36+
Unsupported,
37+
}
38+
2439
impl SluResource for Wallpaper {
2540
const KIND: ResourceKind = ResourceKind::Wallpaper;
2641

@@ -31,6 +46,33 @@ impl SluResource for Wallpaper {
3146
fn metadata_mut(&mut self) -> &mut ResourceMetadata {
3247
&mut self.metadata
3348
}
49+
50+
fn sanitize(&mut self) {
51+
// migration step for old wallpapers
52+
if WallpaperKind::Unsupported == self.r#type {
53+
if let Some(filename) = &self.filename {
54+
if Self::SUPPORTED_VIDEOS
55+
.iter()
56+
.any(|ext| filename.ends_with(ext))
57+
{
58+
self.r#type = WallpaperKind::Video;
59+
}
60+
if Self::SUPPORTED_IMAGES
61+
.iter()
62+
.any(|ext| filename.ends_with(ext))
63+
{
64+
self.r#type = WallpaperKind::Image;
65+
}
66+
}
67+
}
68+
}
69+
70+
fn validate(&self) -> Result<()> {
71+
if self.r#type == WallpaperKind::Unsupported {
72+
return Err("Unsupported wallpaper extension".into());
73+
}
74+
Ok(())
75+
}
3476
}
3577

3678
impl Wallpaper {
@@ -74,16 +116,26 @@ impl Wallpaper {
74116
std::fs::rename(path, folder_to_store.join(&filename))?;
75117
}
76118

119+
let r#type = if Self::SUPPORTED_IMAGES.contains(&ext.as_str()) {
120+
WallpaperKind::Image
121+
} else if Self::SUPPORTED_VIDEOS.contains(&ext.as_str()) {
122+
WallpaperKind::Video
123+
} else {
124+
WallpaperKind::Unsupported
125+
};
126+
77127
let wallpaper = Self {
78128
id,
79129
metadata,
130+
r#type,
131+
url: None,
132+
thumbnail_url: None,
80133
filename: Some(filename.clone()),
81134
thumbnail_filename: if Self::SUPPORTED_IMAGES.contains(&ext.as_str()) {
82135
Some(filename)
83136
} else {
84137
None
85138
},
86-
..Default::default()
87139
};
88140
wallpaper.save()?;
89141

src/ui/react/settings/modules/resources/ResourceCard.tsx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,29 +153,15 @@ function ResourcePortraitInner({ resource, kind }: ResourcePortraitProps) {
153153

154154
if (kind === "Wallpaper") {
155155
const wallpaper = resource as Wallpaper;
156-
if (wallpaper.thumbnail_filename) {
156+
if (wallpaper.thumbnailFilename) {
157157
return (
158158
<img
159-
src={convertFileSrc(`${resource.metadata.path}\\${wallpaper.thumbnail_filename}`)}
159+
src={convertFileSrc(`${resource.metadata.path}\\${wallpaper.thumbnailFilename}`)}
160160
style={{ filter: "blur(0.4px)" }}
161161
loading="lazy"
162162
/>
163163
);
164164
}
165-
166-
/* if (
167-
wallpaper.filename &&
168-
SUPPORTED_VIDEO_WALLPAPER_EXTENSIONS.includes(wallpaper.filename.split('.').pop()!)
169-
) {
170-
return (
171-
<video
172-
src={convertFileSrc(`${resource.metadata.path}\\${wallpaper.filename}`)}
173-
controls={false}
174-
preload="metadata"
175-
style={{ filter: 'blur(0.4px)' }}
176-
/>
177-
);
178-
} */
179165
}
180166

181167
return <ResourceIcon kind={kind} />;

0 commit comments

Comments
 (0)