Extract make_screenshot_path()

This commit is contained in:
Ivan Molodetskikh
2023-09-19 19:05:03 +04:00
parent 495cce054b
commit 69f561cd6f
2 changed files with 29 additions and 23 deletions
+2 -23
View File
@@ -8,7 +8,6 @@ use std::time::Duration;
use std::{env, thread};
use anyhow::Context;
use directories::UserDirs;
use sd_notify::NotifyState;
use smithay::backend::allocator::dmabuf::Dmabuf;
use smithay::backend::allocator::Fourcc;
@@ -56,7 +55,6 @@ use smithay::wayland::shell::xdg::XdgShellState;
use smithay::wayland::shm::ShmState;
use smithay::wayland::socket::ListeningSocketSource;
use smithay::wayland::tablet_manager::TabletManagerState;
use time::OffsetDateTime;
use zbus::fdo::RequestNameFlags;
use crate::backend::{Backend, Tty, Winit};
@@ -67,7 +65,7 @@ use crate::dbus::mutter_service_channel::ServiceChannel;
use crate::frame_clock::FrameClock;
use crate::layout::{MonitorRenderElement, MonitorSet};
use crate::pw_utils::{Cast, PipeWire};
use crate::utils::{center, get_monotonic_time, load_default_cursor};
use crate::utils::{center, get_monotonic_time, load_default_cursor, make_screenshot_path};
use crate::LoopData;
pub struct Niri {
@@ -1069,26 +1067,7 @@ impl Niri {
.context("error mapping texture")?;
let pixels = copy.to_vec();
let dirs = UserDirs::new().context("error retrieving home directory")?;
let mut path = dirs.picture_dir().map(|p| p.to_owned()).unwrap_or_else(|| {
let mut dir = dirs.home_dir().to_owned();
dir.push("Pictures");
dir
});
path.push("Screenshots");
unsafe {
// are you kidding me
time::util::local_offset::set_soundness(time::util::local_offset::Soundness::Unsound);
};
let now = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
let desc = time::macros::format_description!(
"Screenshot from [year]-[month]-[day] [hour]-[minute]-[second].png"
);
let name = now.format(desc).context("error formatting time")?;
path.push(name);
let path = make_screenshot_path().context("error making screenshot path")?;
debug!("saving screenshot to {path:?}");
thread::spawn(move || {
+27
View File
@@ -1,13 +1,16 @@
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::time::Duration;
use anyhow::{anyhow, Context};
use directories::UserDirs;
use smithay::backend::allocator::Fourcc;
use smithay::backend::renderer::element::texture::TextureBuffer;
use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
use smithay::reexports::nix::time::{clock_gettime, ClockId};
use smithay::utils::{Logical, Physical, Point, Rectangle, Transform};
use time::OffsetDateTime;
use xcursor::parser::parse_xcursor;
use xcursor::CursorTheme;
@@ -80,3 +83,27 @@ pub fn load_default_cursor(
.unwrap();
(texture, (frame.xhot as i32, frame.yhot as i32).into())
}
pub fn make_screenshot_path() -> anyhow::Result<PathBuf> {
let dirs = UserDirs::new().context("error retrieving home directory")?;
let mut path = dirs.picture_dir().map(|p| p.to_owned()).unwrap_or_else(|| {
let mut dir = dirs.home_dir().to_owned();
dir.push("Pictures");
dir
});
path.push("Screenshots");
unsafe {
// are you kidding me
time::util::local_offset::set_soundness(time::util::local_offset::Soundness::Unsound);
};
let now = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
let desc = time::macros::format_description!(
"Screenshot from [year]-[month]-[day] [hour]-[minute]-[second].png"
);
let name = now.format(desc).context("error formatting time")?;
path.push(name);
Ok(path)
}