Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ windows = { version = "0.62", features = [
"Win32_System_Ole",
"Win32_System_Threading",
"Win32_UI_WindowsAndMessaging",
"Win32_UI_Accessibility",
"Win32_Graphics_Gdi",
"Win32_UI_HiDpi",
"Win32_Graphics_Dwm",
Expand Down
8 changes: 8 additions & 0 deletions app/src/news.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@
{
"title": "Tus atajos, a tu gusto",
"said": "en Ajustes › Atajos de teclado puedes cambiar o quitar cada uno, y si otro programa ya usa una combinación, CoPanda te sugiere otras libres."
},
{
"title": "Imágenes que llegan a la terminal",
"said": "en Windows, si copias una imagen y pasas a una terminal, Ctrl+V pega la ruta del archivo lista para usar; al volver a cualquier otra app, se pega la imagen como siempre."
}
],
"en": [
Expand All @@ -271,6 +275,10 @@
{
"title": "Your shortcuts, your way",
"said": "in Settings › Keyboard shortcuts you can change or remove each one, and if another program already uses a combination, CoPanda suggests free ones."
},
{
"title": "Pictures that reach the terminal",
"said": "on Windows, if you copy a picture and switch to a terminal, Ctrl+V pastes the path to its file, ready to use; back in any other app, the picture pastes as always."
}
]
}
Expand Down
9 changes: 9 additions & 0 deletions crates/cp-core/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn insist<T>(
pub struct Watcher {
last: Option<i64>,
ours: Option<(i64, Option<i64>)>,
before_writing: Option<i64>,
missed: u64,
cadence: Cadence,
}
Expand All @@ -69,15 +70,23 @@ impl Watcher {
Self {
last: None,
ours: None,
before_writing: None,
missed: 0,
cadence,
}
}

pub fn writing(&mut self, from: i64) {
self.before_writing = self.last;
self.ours = Some((from, None));
}

pub fn abandoned(&mut self) {
if self.ours.take().is_some() {
self.last = self.before_writing.take();
}
}

pub fn wrote(&mut self, count: i64) {
let from = self.ours.map_or(count, |(from, _)| from);
self.ours = Some((from, Some(count)));
Expand Down
50 changes: 50 additions & 0 deletions crates/cp-core/src/watch_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,53 @@ fn a_counter_that_goes_backwards_is_a_new_session_not_a_burst() {
assert_eq!(watcher.tick(3), Seen::Fresh { skipped: Some(0) });
assert_eq!(watcher.missed(), Some(0));
}

#[test]
fn a_write_given_up_lets_the_copy_that_beat_it_be_seen() {
let mut watcher = Watcher::new(Cadence::Opaque);
watcher.tick(10);
watcher.writing(10);
assert_eq!(
watcher.tick(11),
Seen::Ours,
"while writing, the next count looks ours"
);
watcher.abandoned();
assert!(
matches!(watcher.tick(11), Seen::Fresh { .. }),
"once given up, that same count is someone else's copy"
);
assert_eq!(watcher.tick(11), Seen::Nothing);
}

#[test]
fn a_copy_seen_before_the_write_began_is_not_seen_twice_when_it_is_given_up() {
let mut watcher = Watcher::new(Cadence::Opaque);
watcher.tick(10);
assert!(matches!(watcher.tick(11), Seen::Fresh { .. }));
watcher.writing(11);
watcher.abandoned();
assert_eq!(
watcher.tick(11),
Seen::Nothing,
"already kept before the write"
);
watcher.abandoned();
assert_eq!(
watcher.tick(11),
Seen::Nothing,
"giving up twice changes nothing"
);
}

#[test]
fn a_copy_made_before_a_write_began_and_never_ticked_is_seen_once_it_is_given_up() {
let mut watcher = Watcher::new(Cadence::Opaque);
watcher.tick(10);
watcher.writing(11);
watcher.abandoned();
assert!(
matches!(watcher.tick(11), Seen::Fresh { .. }),
"the copy that slipped in before the write is still someone's copy"
);
}
8 changes: 8 additions & 0 deletions crates/cp-core/src/watching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ impl Watching {
true
}

pub fn abandoned(&self) -> bool {
let Ok(mut watcher) = self.watcher.lock() else {
return false;
};
watcher.abandoned();
true
}

pub fn ours(&self) -> bool {
let Some(count) = (self.count)() else {
return false;
Expand Down
24 changes: 24 additions & 0 deletions crates/cp-core/src/watching_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,27 @@ fn closing_in_the_middle_of_a_long_period_is_prompt() {
assert!(watching.close());
assert!(started.elapsed() < Duration::from_millis(400));
}

#[test]
fn a_copy_that_lands_while_a_write_is_given_up_still_wakes_the_watcher() {
let count = Arc::new(AtomicI64::new(30));
let seen = Arc::new(AtomicUsize::new(0));
let watching = Watching::every(
Cadence::Opaque,
QUICK,
NAP,
at(&count),
never_busy(),
counted(&seen),
);
std::thread::sleep(QUICK * 4);
assert!(watching.writing());
count.store(31, Ordering::Relaxed);
std::thread::sleep(QUICK * 4);
assert!(watching.abandoned());
assert!(
waited_for(&seen, 1) >= 1,
"the copy that beat the write is kept"
);
assert!(watching.close());
}
40 changes: 38 additions & 2 deletions crates/cp-panel/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct App {
struct State {
store: Rc<Store>,
engine: Option<Rc<crate::engine::Engine>>,
dressing: Option<Rc<crate::dressing::Dressing<crate::handing::Shelves>>>,
ahead: Arc<AtomicIsize>,
query: String,
tags: Vec<String>,
Expand Down Expand Up @@ -96,6 +97,7 @@ impl App {
let state = Rc::new(RefCell::new(State {
store: Rc::new(store),
engine: None,
dressing: None,
ahead: Arc::new(AtomicIsize::new(0)),
query: String::new(),
tags: Vec::new(),
Expand Down Expand Up @@ -127,6 +129,10 @@ impl App {
}

pub fn close(&self) {
let dressing = self.state.borrow().dressing.clone();
if let Some(dressing) = dressing {
dressing.put_back();
}
let engine = self.state.borrow().engine.clone();
if let Some(engine) = engine {
engine.close();
Expand Down Expand Up @@ -169,11 +175,17 @@ impl App {
fn keep_watch(&self, ui: slint::Weak<Panel>) {
let db = self.state.borrow().options.db.clone();
let drawn = ui.clone();
let arrived = move |_| {
let latest: crate::dressing::Latest = Arc::new(std::sync::Mutex::new(None));
let noted = latest.clone();
let arrived = move |_, copied| {
if let Ok(mut held) = noted.lock() {
*held = copied;
}
let _ = ui.upgrade_in_event_loop(|panel| {
if panel.window().is_visible() && !panel.get_sheet_open() {
panel.invoke_arrived();
}
panel.invoke_copied();
});
};
let changed = move |id| {
Expand All @@ -182,14 +194,38 @@ impl App {
});
};
match crate::engine::Engine::start(&db, arrived, changed) {
Ok(engine) => self.state.borrow_mut().engine = Some(Rc::new(engine)),
Ok(engine) => {
let engine = Rc::new(engine);
self.dress_terminals(&engine, latest);
self.state.borrow_mut().engine = Some(engine);
}
Err(why) => crate::note::trouble(
cp_core::trouble::Trouble::Unwatched,
&format!("nothing watches the clipboard: {why}"),
),
}
}

fn dress_terminals(&self, engine: &Rc<crate::engine::Engine>, latest: crate::dressing::Latest) {
if !cfg!(target_os = "windows") {
return;
}
let store = self.state.borrow().store.clone();
let shelves = crate::handing::Shelves {
store,
engine: engine.clone(),
};
let dressing = crate::dressing::Dressing::start(shelves, latest);
if !dressing.listening() {
note("nobody hears which window comes forward, so terminals get no image paths");
}
if let Some(panel) = self.ui.upgrade() {
let heard = dressing.clone();
panel.on_copied(move || heard.arrived());
}
self.state.borrow_mut().dressing = Some(dressing);
}

pub fn ui(&self) -> slint::Weak<Panel> {
self.ui.clone()
}
Expand Down
Loading
Loading