feat(client): add RU/EN localization and native capture preview
This commit is contained in:
parent
8e5ab56611
commit
8460fb28fd
43 changed files with 4103 additions and 275 deletions
|
|
@ -64,8 +64,11 @@ pub fn wire(window: &MainWindow, state: &Rc<RefCell<AppState>>, persist: bool) {
|
|||
let task_window = weak.clone();
|
||||
let result = slint::spawn_local(async move {
|
||||
let Some(file) = rfd::AsyncFileDialog::new()
|
||||
.set_title("Выбрать фото профиля")
|
||||
.add_filter("Фото (PNG, JPEG, WebP)", &["png", "jpg", "jpeg", "webp"])
|
||||
.set_title(crate::ui::i18n::text("Choose profile photo"))
|
||||
.add_filter(
|
||||
crate::ui::i18n::text("Photos (PNG, JPEG, WebP)"),
|
||||
&["png", "jpg", "jpeg", "webp"],
|
||||
)
|
||||
.pick_file()
|
||||
.await
|
||||
else {
|
||||
|
|
@ -78,7 +81,10 @@ pub fn wire(window: &MainWindow, state: &Rc<RefCell<AppState>>, persist: bool) {
|
|||
Ok(Ok(source)) => begin_crop(&window, &pending, source),
|
||||
Ok(Err(error)) => window.set_avatar_error(error.into()),
|
||||
Err(_) => window.set_avatar_error(
|
||||
"Не удалось обработать фото. Попробуй другое изображение.".into(),
|
||||
crate::ui::i18n::text(
|
||||
"Could not process this photo. Try another image.",
|
||||
)
|
||||
.into(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
@ -86,7 +92,10 @@ pub fn wire(window: &MainWindow, state: &Rc<RefCell<AppState>>, persist: bool) {
|
|||
if result.is_err()
|
||||
&& let Some(window) = weak.upgrade()
|
||||
{
|
||||
window.set_avatar_error("Не удалось открыть выбор файла в этом окружении.".into());
|
||||
window.set_avatar_error(
|
||||
crate::ui::i18n::text("The file picker is unavailable in this environment.")
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -162,7 +171,13 @@ pub fn wire(window: &MainWindow, state: &Rc<RefCell<AppState>>, persist: bool) {
|
|||
&& let Err(error) = std::fs::remove_file(&path)
|
||||
&& error.kind() != std::io::ErrorKind::NotFound
|
||||
{
|
||||
window.set_avatar_error(format!("Не удалось удалить фото: {error}").into());
|
||||
window.set_avatar_error(
|
||||
crate::ui::i18n::format(
|
||||
"Could not remove photo: {error}",
|
||||
&[("error", error.to_string())],
|
||||
)
|
||||
.into(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
window.set_own_avatar(slint::Image::default());
|
||||
|
|
|
|||
|
|
@ -17,10 +17,11 @@ pub struct CropSource {
|
|||
|
||||
impl CropSource {
|
||||
pub fn load(path: &Path) -> Result<Self, String> {
|
||||
let file =
|
||||
std::fs::File::open(path).map_err(|e| format!("Не удалось открыть фото: {e}"))?;
|
||||
let file = std::fs::File::open(path).map_err(|e| {
|
||||
crate::ui::i18n::format("Could not open photo: {e}", &[("e", e.to_string())])
|
||||
})?;
|
||||
if file.metadata().map_err(|e| e.to_string())?.len() > MAX_FILE_BYTES as u64 {
|
||||
return Err("Фото слишком большое: максимум 10 МБ.".into());
|
||||
return Err(crate::ui::i18n::text("Photo is too large: maximum 10 MB."));
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
file.take(MAX_FILE_BYTES as u64 + 1)
|
||||
|
|
@ -31,14 +32,15 @@ impl CropSource {
|
|||
|
||||
fn decode(bytes: &[u8]) -> Result<Self, String> {
|
||||
if bytes.len() > MAX_FILE_BYTES {
|
||||
return Err("Фото слишком большое: максимум 10 МБ.".into());
|
||||
return Err(crate::ui::i18n::text("Photo is too large: maximum 10 MB."));
|
||||
}
|
||||
let format = image::guess_format(bytes)
|
||||
.map_err(|_| "Не удалось распознать изображение. Выбери PNG, JPEG или WebP.")?;
|
||||
let format = image::guess_format(bytes).map_err(|_| {
|
||||
crate::ui::i18n::text("Could not recognise the image. Choose PNG, JPEG or WebP.")
|
||||
})?;
|
||||
if ![ImageFormat::Png, ImageFormat::Jpeg, ImageFormat::WebP].contains(&format) {
|
||||
return Err(
|
||||
"Поддерживаются только PNG, JPEG и WebP. Анимации и SVG не поддерживаются.".into(),
|
||||
);
|
||||
return Err(crate::ui::i18n::text(
|
||||
"Only PNG, JPEG and WebP are supported. Animations and SVG are not supported.",
|
||||
));
|
||||
}
|
||||
reject_animation(bytes, format)?;
|
||||
let mut limits = image::Limits::default();
|
||||
|
|
@ -47,20 +49,26 @@ impl CropSource {
|
|||
limits.max_alloc = Some(64 * 1024 * 1024);
|
||||
let mut reader = ImageReader::with_format(Cursor::new(bytes), format);
|
||||
reader.limits(limits);
|
||||
let mut decoder = reader
|
||||
.into_decoder()
|
||||
.map_err(|e| format!("Не удалось прочитать фото: {e}"))?;
|
||||
let mut decoder = reader.into_decoder().map_err(|e| {
|
||||
crate::ui::i18n::format("Could not read photo: {e}", &[("e", e.to_string())])
|
||||
})?;
|
||||
if decoder.dimensions().0 > MAX_DIMENSION || decoder.dimensions().1 > MAX_DIMENSION {
|
||||
return Err("Сторона изображения должна быть не больше 4096 пикселей.".into());
|
||||
return Err(crate::ui::i18n::text(
|
||||
"Image dimensions must not exceed 4096 pixels.",
|
||||
));
|
||||
}
|
||||
let orientation = decoder
|
||||
.orientation()
|
||||
.map_err(|e| format!("Не удалось прочитать ориентацию фото: {e}"))?;
|
||||
let mut image = DynamicImage::from_decoder(decoder)
|
||||
.map_err(|e| format!("Не удалось декодировать фото: {e}"))?;
|
||||
let orientation = decoder.orientation().map_err(|e| {
|
||||
crate::ui::i18n::format(
|
||||
"Could not read photo orientation: {e}",
|
||||
&[("e", e.to_string())],
|
||||
)
|
||||
})?;
|
||||
let mut image = DynamicImage::from_decoder(decoder).map_err(|e| {
|
||||
crate::ui::i18n::format("Could not decode photo: {e}", &[("e", e.to_string())])
|
||||
})?;
|
||||
image.apply_orientation(orientation);
|
||||
if image.width() == 0 || image.height() == 0 {
|
||||
return Err("Изображение пустое.".into());
|
||||
return Err(crate::ui::i18n::text("The image is empty."));
|
||||
}
|
||||
Ok(Self {
|
||||
image,
|
||||
|
|
@ -119,16 +127,22 @@ impl CropSource {
|
|||
let mut encoded = Cursor::new(Vec::new());
|
||||
image
|
||||
.write_to(&mut encoded, ImageFormat::Png)
|
||||
.map_err(|e| format!("Не удалось подготовить аватар: {e}"))?;
|
||||
.map_err(|e| {
|
||||
crate::ui::i18n::format("Could not prepare avatar: {e}", &[("e", e.to_string())])
|
||||
})?;
|
||||
if let Some(parent) = path.parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
|
||||
}
|
||||
let temporary = path.with_extension(format!("{}.tmp", uuid::Uuid::new_v4()));
|
||||
std::fs::write(&temporary, encoded.into_inner())
|
||||
.map_err(|e| format!("Не удалось сохранить фото: {e}"))?;
|
||||
std::fs::write(&temporary, encoded.into_inner()).map_err(|e| {
|
||||
crate::ui::i18n::format("Could not save photo: {e}", &[("e", e.to_string())])
|
||||
})?;
|
||||
if let Err(error) = std::fs::rename(&temporary, path) {
|
||||
let _ = std::fs::remove_file(&temporary);
|
||||
return Err(format!("Не удалось применить аватар: {error}"));
|
||||
return Err(crate::ui::i18n::format(
|
||||
"Could not apply avatar: {error}",
|
||||
&[("error", error.to_string())],
|
||||
));
|
||||
}
|
||||
Ok(to_ui_image(&image))
|
||||
}
|
||||
|
|
@ -145,24 +159,26 @@ fn reject_animation(bytes: &[u8], format: ImageFormat) -> Result<(), String> {
|
|||
let size = u32::from_be_bytes(
|
||||
bytes[offset..offset + 4]
|
||||
.try_into()
|
||||
.map_err(|_| "Повреждённый PNG.")?,
|
||||
.map_err(|_| crate::ui::i18n::text("Corrupt PNG."))?,
|
||||
) as usize;
|
||||
(size, &bytes[offset + 4..offset + 8], 12usize)
|
||||
} else {
|
||||
let size = u32::from_le_bytes(
|
||||
bytes[offset + 4..offset + 8]
|
||||
.try_into()
|
||||
.map_err(|_| "Повреждённый WebP.")?,
|
||||
.map_err(|_| crate::ui::i18n::text("Corrupt WebP."))?,
|
||||
) as usize;
|
||||
(size, &bytes[offset..offset + 4], 8usize + size % 2)
|
||||
};
|
||||
if kind == b"acTL" || kind == b"ANIM" || kind == b"ANMF" {
|
||||
return Err("Анимированные изображения не поддерживаются. Выбери обычное фото.".into());
|
||||
return Err(crate::ui::i18n::text(
|
||||
"Animated images are not supported. Choose a still photo.",
|
||||
));
|
||||
}
|
||||
offset = offset
|
||||
.checked_add(length)
|
||||
.and_then(|n| n.checked_add(padding))
|
||||
.ok_or("Повреждённое изображение.")?;
|
||||
.ok_or(crate::ui::i18n::text("Corrupt image."))?;
|
||||
if offset > bytes.len() {
|
||||
break;
|
||||
} // The decoder reports truncated image details.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue