Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy to clipboard #150

Merged
merged 8 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add UI feedback for when failed to copy the image.
  • Loading branch information
ArturKovacs committed Sep 8, 2020
commit 046372577178cd66d93b2b177a23428d566460e4
Binary file added resource/copy-failed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified resource_dev/copy.xcf
Binary file not shown.
19 changes: 4 additions & 15 deletions src/clipboard_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ struct ClipboardRequestHandle {
}

pub struct ClipboardHandler {
prev_state: ClipboardState,
request_handle: Arc<ClipboardRequestHandle>,

thread_handle: Option<std::thread::JoinHandle<()>>,
Expand All @@ -44,7 +43,7 @@ impl ClipboardHandler {
})
};

ClipboardHandler { prev_state, request_handle, thread_handle: Some(handle) }
ClipboardHandler { request_handle, thread_handle: Some(handle) }
}

pub fn request_copy(&mut self, target: PathBuf) -> bool {
Expand All @@ -66,17 +65,8 @@ impl ClipboardHandler {
self.request_handle.condvar.notify_one();
}

pub fn requests_pending(&self) -> bool {
let state = self.request_handle.state.lock().unwrap();
if let ClipboardState::Pending(..) = &*state {
true
} else {
false
}
}

pub fn try_get_result(&self) -> Option<bool> {
let mut state = self.request_handle.state.lock().unwrap();
let state = self.request_handle.state.lock().unwrap();
match &*state {
ClipboardState::Pending(..) => None,
ClipboardState::Succeeded => Some(true),
Expand Down Expand Up @@ -142,9 +132,8 @@ impl ClipboardHandler {
}
Err("Could not set the clipboard image.".into())
});
let mut state_guard = request_handle.state.lock().unwrap();
*state_guard =
if result.is_ok() { ClipboardState::Succeeded } else { ClipboardState::Failed };
let mut state = request_handle.state.lock().unwrap();
*state = if result.is_ok() { ClipboardState::Succeeded } else { ClipboardState::Failed };
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/widgets/copy_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use gelatin::{label::Label, misc::*, picture::Picture, NextUpdate, Widget};

static COPY_STARTED: &[u8] = include_bytes!("../../resource/copy-started.png");
static COPY_READY: &[u8] = include_bytes!("../../resource/copy-ready.png");
static COPY_FAILED: &[u8] = include_bytes!("../../resource/copy-failed.png");

const READY_DISPLAY_TIME: Duration = Duration::from_secs(3);

Expand All @@ -14,6 +15,7 @@ pub struct CopyNotifications {
pub widget: Weak<Label>,
copy_started_img: Rc<Picture>,
copy_ready_img: Rc<Picture>,
copy_failed_img: Rc<Picture>,
finished: bool,
finished_time: std::time::Instant,
}
Expand All @@ -22,6 +24,7 @@ impl CopyNotifications {
pub fn new(widget: &Rc<Label>) -> CopyNotifications {
let copy_started_img = Rc::new(Picture::from_encoded_bytes(COPY_STARTED));
let copy_ready_img = Rc::new(Picture::from_encoded_bytes(COPY_READY));
let copy_failed_img = Rc::new(Picture::from_encoded_bytes(COPY_FAILED));

widget.set_icon(None);
widget.set_ignore_layout(true);
Expand All @@ -37,23 +40,27 @@ impl CopyNotifications {
widget: Rc::downgrade(widget),
copy_started_img,
copy_ready_img,
copy_failed_img,
finished: true,
finished_time: Instant::now(),
}
}

pub fn set_started(&mut self) {
println!("Set started called");
let widget = self.widget.upgrade().unwrap();
widget.set_icon(Some(self.copy_started_img.clone()));
widget.set_visible(true);
self.finished = false;
}

pub fn set_finished(&mut self) {
println!("Set finished called");
pub fn set_finished(&mut self, succeeded: bool) {
let widget = self.widget.upgrade().unwrap();
widget.set_icon(Some(self.copy_ready_img.clone()));
let icon = if succeeded {
self.copy_ready_img.clone()
} else {
self.copy_failed_img.clone()
};
widget.set_icon(Some(icon));
self.finished_time = Instant::now();
self.finished = true;
}
Expand Down
10 changes: 5 additions & 5 deletions src/widgets/picture_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,12 @@ impl Widget for PictureWidget {
}
}
if let Some(clipboard_handler) = &data.clipboard_handler {
let request_pending = clipboard_handler.requests_pending();
let clipboard_result = clipboard_handler.try_get_result();
let request_pending = clipboard_result.is_none();
if data.clipboard_request_was_pending != request_pending {
if request_pending {
data.copy_notifications.set_started();
} else {
data.copy_notifications.set_finished();
match clipboard_result {
Some(succeeded) => data.copy_notifications.set_finished(succeeded),
None => data.copy_notifications.set_started(),
}
data.clipboard_request_was_pending = request_pending;
} else if request_pending {
Expand Down