Skip to content

Commit 225ecfa

Browse files
authored
Merge pull request #316 from multiplex55/qun1d4-codex/fix-screenshot-region-functionalities-and-update-directory
Fix screenshot region saving
2 parents f7a0306 + 78d7b30 commit 225ecfa

4 files changed

Lines changed: 58 additions & 16 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ value as shown below:
104104
"static_location_enabled": false,
105105
"static_pos": [0, 0],
106106
"static_size": [400, 220],
107-
"screenshot_dir": "C:/Users/YourName/Pictures",
107+
"screenshot_dir": "./MultiLauncher_Screenshots",
108108
"screenshot_save_file": true
109109
}
110110
```
@@ -148,7 +148,7 @@ Example: typing `test` will only list entries containing `test`. If an alias mat
148148
`clipboard_limit` sets how many clipboard entries are persisted for the clipboard plugin.
149149
`preserve_command` keeps the typed command prefix (like `bm add` or `f add`) in the search field after running an action.
150150
`enabled_capabilities` maps plugin names to capability identifiers so features can be toggled individually. The folders plugin, for example, exposes `show_full_path`.
151-
`screenshot_dir` sets the directory used when saving screenshots. If omitted, the Pictures or home folder is used by default.
151+
`screenshot_dir` sets the directory used when saving screenshots. If omitted, screenshots are stored in a `MultiLauncher_Screenshots` folder in the current working directory.
152152
`screenshot_save_file` determines whether screenshots copied to the clipboard are also written to disk. The default is `true`.
153153

154154

@@ -220,7 +220,7 @@ Built-in plugins and their command prefixes are:
220220

221221
### Screenshot Plugin (Windows only)
222222
Use `ss` to capture the active window, a custom region or the whole desktop. Add `clip` to copy the result to the clipboard.
223-
Screenshots are saved in a `screenshots` folder next to the executable by default or the path set in `screenshot_dir`.
223+
Screenshots are saved in a `MultiLauncher_Screenshots` folder in the current working directory by default or the path set in `screenshot_dir`.
224224
Set `screenshot_save_file` to `true` to always keep a file when copying to the clipboard.
225225

226226
When the search box is empty the launcher shows these shortcuts along with `app <alias>` entries for saved actions.

src/actions/screenshot.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,42 @@ pub fn capture(mode: Mode, clipboard: bool) -> anyhow::Result<PathBuf> {
5454
}
5555
}
5656
Mode::Region => {
57-
let screen = Screen::from_point(0, 0)?;
58-
let image = screen.capture()?;
59-
image.save(&path)?;
57+
use std::process::Command;
58+
use std::thread::sleep;
59+
use std::time::{Duration, Instant};
60+
61+
// Wait for the snipping tool to provide a new clipboard image
62+
let mut cb = arboard::Clipboard::new()?;
63+
let old = cb.get_image().ok().map(|img| {
64+
(img.width, img.height, img.bytes.into_owned())
65+
});
66+
67+
let _ = Command::new("explorer").arg("ms-screenclip:").status();
68+
69+
let start = Instant::now();
70+
let img = loop {
71+
match cb.get_image() {
72+
Ok(img) => {
73+
let cur = (img.width, img.height, img.bytes.as_ref().to_vec());
74+
if Some(cur.clone()) != old {
75+
break img;
76+
}
77+
}
78+
Err(_) => {}
79+
}
80+
if start.elapsed() > Duration::from_secs(30) {
81+
anyhow::bail!("timed out waiting for snip");
82+
}
83+
sleep(Duration::from_millis(200));
84+
};
85+
86+
let buf = image::RgbaImage::from_raw(
87+
img.width as u32,
88+
img.height as u32,
89+
img.bytes.into_owned(),
90+
)
91+
.ok_or_else(|| anyhow::anyhow!("invalid clipboard image"))?;
92+
buf.save(&path)?;
6093
}
6194
}
6295
if clipboard {

src/plugins/screenshot.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
use crate::actions::Action;
22
use crate::plugin::Plugin;
3+
use crate::settings::Settings;
34
use std::path::PathBuf;
45

56
/// Return the directory used to store screenshots.
67
///
7-
/// The directory is created inside the folder of the current executable if
8-
/// possible, otherwise a temporary directory is used.
8+
/// The path is loaded from `settings.json` if available. When no directory is
9+
/// configured, a `MultiLauncher_Screenshots` folder in the current working
10+
/// directory is used.
911
pub fn screenshot_dir() -> PathBuf {
10-
let base = std::env::current_exe()
11-
.ok()
12-
.and_then(|p| p.parent().map(|d| d.to_path_buf()))
13-
.unwrap_or_else(std::env::temp_dir);
14-
base.join("screenshots")
12+
if let Ok(s) = Settings::load("settings.json") {
13+
if let Some(dir) = s.screenshot_dir {
14+
return PathBuf::from(dir);
15+
}
16+
}
17+
std::env::current_dir()
18+
.unwrap_or_else(|_| std::env::temp_dir())
19+
.join("MultiLauncher_Screenshots")
1520
}
1621

1722
pub struct ScreenshotPlugin;

src/settings.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,13 @@ impl Default for Settings {
181181
disable_timer_updates: false,
182182
preserve_command: false,
183183
show_examples: false,
184-
screenshot_dir: dirs_next::picture_dir()
185-
.or_else(dirs_next::home_dir)
186-
.map(|p| p.to_string_lossy().to_string()),
184+
screenshot_dir: Some(
185+
std::env::current_dir()
186+
.unwrap_or_else(|_| std::env::temp_dir())
187+
.join("MultiLauncher_Screenshots")
188+
.to_string_lossy()
189+
.to_string(),
190+
),
187191
screenshot_save_file: true,
188192
}
189193
}

0 commit comments

Comments
 (0)