From c681ad2648cbcf60f43070f08efb5409bf8a3c2d Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Wed, 23 Dec 2020 23:43:03 -0600 Subject: [PATCH] cancelled by user case --- src/gui.rs | 6 ++++-- src/main.rs | 9 +++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/gui.rs b/src/gui.rs index d142583..cb48c6a 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -46,7 +46,7 @@ impl Gui { Ok(ScreenCapture { image }) } - pub fn interactive_select(&self, image: &ScreenCapture) -> Result { + pub fn interactive_select(&self, image: &ScreenCapture) -> Result> { let id = self.conn.generate_id(); let screen = self.get_default_screen(); let root_window = screen.root(); @@ -166,6 +166,7 @@ impl Gui { } let mut state = State::default(); + let mut cancelled = false; let redraw = |state: &State| { xcb_image::put(&self.conn, id, gc, &image.image, 0, 0); @@ -185,6 +186,7 @@ impl Gui { xcb::KEY_RELEASE => { let key_evt = unsafe { xcb::cast_event::(&evt) }; if key_evt.detail() == 9 { + cancelled = true; break; } } @@ -226,7 +228,7 @@ impl Gui { xproto::ungrab_keyboard(&self.conn, xcb::CURRENT_TIME).request_check()?; xproto::ungrab_pointer(&self.conn, xcb::CURRENT_TIME).request_check()?; - Ok(state.rect()) + Ok(if cancelled { None } else { Some(state.rect()) }) } } diff --git a/src/main.rs b/src/main.rs index c4abdab..b6e7aa8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ extern crate anyhow; mod gui; +use std::process; use std::path::PathBuf; use anyhow::Result; @@ -29,8 +30,12 @@ fn main() -> Result<()> { capture.save_to(&opt.outfile)?; } Region::Selection => { - let rectangle = gui.interactive_select(&capture)?; - capture.save_cropped_to(&opt.outfile, rectangle)?; + if let Some(rectangle) = gui.interactive_select(&capture)? { + capture.save_cropped_to(&opt.outfile, rectangle)?; + } else { + info!("Cancelled by user."); + process::exit(1); + } } }