Skip to content

Commit

Permalink
Show notification if it's taking too long to copy text
Browse files Browse the repository at this point in the history
  • Loading branch information
ubolonton committed May 9, 2019
1 parent 99d4587 commit 09b2972
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pullover.el
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ text into the clipboard.")
(defcustom pullover-get-current-app-function #'pullover-dyn--get-current-app
"Function used to get currently active app.")

(defcustom pullover-copy-text-function #'pullover-osa--copy-text
(defcustom pullover-copy-text-function #'pullover-dyn--copy-text
"Function used to copy text from the specified app into the clipboard.")

(defcustom pullover-paste-text-function #'pullover-dyn--paste-text
Expand Down
8 changes: 5 additions & 3 deletions src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cocoa::{
};

mod clipboard;
mod notification;

#[link(name = "ScriptingBridge", kind = "framework")]
extern "C" {}
Expand Down Expand Up @@ -113,14 +114,14 @@ fn _copy_text(bundle_id: Option<String>) -> Result<Option<String>> {
let pred = bundle_id_is(&bundle_id);
get_process(pred)
}
None => {
frontmost_app()
}
None => frontmost_app(),
};
let process_id: u32 = msg_send![process, unixId];
if process_id == my_id {
return None;
}
// TODO: Allow the timeout to be customizable.
let (center, ntf) = notification::schedule("Please wait!", "Copying ...", 1.0);
let menu_bar: id = f![process, menuBars];
let edit: id = f![menu_bar, menuBarItems, name = "Edit"];
let menu: id = f![edit, menus];
Expand All @@ -129,6 +130,7 @@ fn _copy_text(bundle_id: Option<String>) -> Result<Option<String>> {
click(menu, select_all);
click(menu, copy);
let bundle: id = msg_send![process, bundleIdentifier];
notification::unschedule(center, ntf);
Some(to_str(bundle).expect("Process has invalid bundle").to_owned())
}))
}
Expand Down
44 changes: 44 additions & 0 deletions src/mac/notification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use emacs::{defun, Result};

use objc::rc::autoreleasepool;
use cocoa::base::id;
use std::time::Instant;

use super::nsstring;

#[link(name = "Foundation", kind = "framework")]
#[allow(unused)]
extern "C" {
pub static NSUserNotificationDefaultSoundName: id;
}

pub unsafe fn schedule(title: &str, text: &str, delay_secs: f64) -> (id, id) {
// XXX: Use the new UNUserNotificationCenter once it's less buggy. See
// https://stackoverflow.com/a/49559863
// https://stackoverflow.com/questions/47255156/crash-invalid-parameter-not-satisfying-bundleproxy-nil-unusernotificatio?noredirect=1&lq=1
let center: id = msg_send![class!(NSUserNotificationCenter), defaultUserNotificationCenter];
let ntf: id = msg_send![class!(NSUserNotification), alloc];
let ntf: id = msg_send![ntf, init];
let identifier = &format!("{:?}", Instant::now());
msg_send![ntf, setIdentifier: nsstring(identifier)];
msg_send![ntf, setTitle: nsstring(title)];
msg_send![ntf, setInformativeText: nsstring(text)];
let ts: id = msg_send![class!(NSDate), dateWithTimeIntervalSinceNow: delay_secs];
msg_send![ntf, setDeliveryDate: ts];

msg_send![center, scheduleNotification: ntf];

(center, msg_send![ntf, autorelease])
}

pub unsafe fn unschedule(notification_center: id, notification: id) -> id {
msg_send![notification_center, removeScheduledNotification:notification]
}

#[defun]
fn _notify(title: String, text: String) -> Result<()> {
autoreleasepool(|| unsafe {
schedule(&title, &text, 0.0);
});
Ok(())
}
3 changes: 3 additions & 0 deletions tests/main.el
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
(benchmark-run (pullover-dyn--copy-text nil)))
(message ":activate-app iTerm2 %s"
(benchmark-run (pullover-dyn--activate-app "com.googlecode.iterm2"))))

(ert-deftest notification ()
(pullover-dyn--notify "Hello" "From Emacs"))

0 comments on commit 09b2972

Please sign in to comment.