forge
Explore
Log in

Don't you dare go Hollow

Extract make_screenshot_path()

Browse files at this commit

69f561cd6f9d9b87d5624dd09be0fd5660f9cf2eIvan MolodetskikhParent: 495cce0
src/niri.rsmodified+2 -23
@@ -8,7 +8,6 @@ use std::time::Duration;
8 use std::{env, thread};8 use std::{env, thread};
9 9
10 use anyhow::Context;10 use anyhow::Context;
11-use directories::UserDirs;
12 use sd_notify::NotifyState;11 use sd_notify::NotifyState;
13 use smithay::backend::allocator::dmabuf::Dmabuf;12 use smithay::backend::allocator::dmabuf::Dmabuf;
14 use smithay::backend::allocator::Fourcc;13 use smithay::backend::allocator::Fourcc;
@@ -56,7 +55,6 @@ use smithay::wayland::shell::xdg::XdgShellState;
56 use smithay::wayland::shm::ShmState;55 use smithay::wayland::shm::ShmState;
57 use smithay::wayland::socket::ListeningSocketSource;56 use smithay::wayland::socket::ListeningSocketSource;
58 use smithay::wayland::tablet_manager::TabletManagerState;57 use smithay::wayland::tablet_manager::TabletManagerState;
59-use time::OffsetDateTime;
60 use zbus::fdo::RequestNameFlags;58 use zbus::fdo::RequestNameFlags;
61 59
62 use crate::backend::{Backend, Tty, Winit};60 use crate::backend::{Backend, Tty, Winit};
@@ -67,7 +65,7 @@ use crate::dbus::mutter_service_channel::ServiceChannel;
67 use crate::frame_clock::FrameClock;65 use crate::frame_clock::FrameClock;
68 use crate::layout::{MonitorRenderElement, MonitorSet};66 use crate::layout::{MonitorRenderElement, MonitorSet};
69 use crate::pw_utils::{Cast, PipeWire};67 use crate::pw_utils::{Cast, PipeWire};
70-use crate::utils::{center, get_monotonic_time, load_default_cursor};68+use crate::utils::{center, get_monotonic_time, load_default_cursor, make_screenshot_path};
71 use crate::LoopData;69 use crate::LoopData;
72 70
73 pub struct Niri {71 pub struct Niri {
@@ -1069,26 +1067,7 @@ impl Niri {
1069 .context("error mapping texture")?;1067 .context("error mapping texture")?;
1070 let pixels = copy.to_vec();1068 let pixels = copy.to_vec();
1071 1069
1072- let dirs = UserDirs::new().context("error retrieving home directory")?;1070+ let path = make_screenshot_path().context("error making screenshot path")?;
1073- let mut path = dirs.picture_dir().map(|p| p.to_owned()).unwrap_or_else(|| {
1074- let mut dir = dirs.home_dir().to_owned();
1075- dir.push("Pictures");
1076- dir
1077- });
1078- path.push("Screenshots");
1079-
1080- unsafe {
1081- // are you kidding me
1082- time::util::local_offset::set_soundness(time::util::local_offset::Soundness::Unsound);
1083- };
1084-
1085- let now = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
1086- let desc = time::macros::format_description!(
1087- "Screenshot from [year]-[month]-[day] [hour]-[minute]-[second].png"
1088- );
1089- let name = now.format(desc).context("error formatting time")?;
1090- path.push(name);
1091-
1092 debug!("saving screenshot to {path:?}");1071 debug!("saving screenshot to {path:?}");
1093 1072
1094 thread::spawn(move || {1073 thread::spawn(move || {
src/utils.rsmodified+27 -0
@@ -1,13 +1,16 @@
1 use std::fs::File;1 use std::fs::File;
2 use std::io::Read;2 use std::io::Read;
3+use std::path::PathBuf;
3 use std::time::Duration;4 use std::time::Duration;
4 5
5 use anyhow::{anyhow, Context};6 use anyhow::{anyhow, Context};
7+use directories::UserDirs;
6 use smithay::backend::allocator::Fourcc;8 use smithay::backend::allocator::Fourcc;
7 use smithay::backend::renderer::element::texture::TextureBuffer;9 use smithay::backend::renderer::element::texture::TextureBuffer;
8 use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};10 use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
9 use smithay::reexports::nix::time::{clock_gettime, ClockId};11 use smithay::reexports::nix::time::{clock_gettime, ClockId};
10 use smithay::utils::{Logical, Physical, Point, Rectangle, Transform};12 use smithay::utils::{Logical, Physical, Point, Rectangle, Transform};
13+use time::OffsetDateTime;
11 use xcursor::parser::parse_xcursor;14 use xcursor::parser::parse_xcursor;
12 use xcursor::CursorTheme;15 use xcursor::CursorTheme;
13 16
@@ -80,3 +83,27 @@ pub fn load_default_cursor(
80 .unwrap();83 .unwrap();
81 (texture, (frame.xhot as i32, frame.yhot as i32).into())84 (texture, (frame.xhot as i32, frame.yhot as i32).into())
82 }85 }
86+
87+pub fn make_screenshot_path() -> anyhow::Result<PathBuf> {
88+ let dirs = UserDirs::new().context("error retrieving home directory")?;
89+ let mut path = dirs.picture_dir().map(|p| p.to_owned()).unwrap_or_else(|| {
90+ let mut dir = dirs.home_dir().to_owned();
91+ dir.push("Pictures");
92+ dir
93+ });
94+ path.push("Screenshots");
95+
96+ unsafe {
97+ // are you kidding me
98+ time::util::local_offset::set_soundness(time::util::local_offset::Soundness::Unsound);
99+ };
100+
101+ let now = OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc());
102+ let desc = time::macros::format_description!(
103+ "Screenshot from [year]-[month]-[day] [hour]-[minute]-[second].png"
104+ );
105+ let name = now.format(desc).context("error formatting time")?;
106+ path.push(name);
107+
108+ Ok(path)
109+}