cancelled by user case

This commit is contained in:
Michael Zhang 2020-12-23 23:43:03 -06:00
parent 39d80bddf7
commit c681ad2648
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
2 changed files with 11 additions and 4 deletions

View file

@ -46,7 +46,7 @@ impl Gui {
Ok(ScreenCapture { image }) Ok(ScreenCapture { image })
} }
pub fn interactive_select(&self, image: &ScreenCapture) -> Result<Rectangle> { pub fn interactive_select(&self, image: &ScreenCapture) -> Result<Option<Rectangle>> {
let id = self.conn.generate_id(); let id = self.conn.generate_id();
let screen = self.get_default_screen(); let screen = self.get_default_screen();
let root_window = screen.root(); let root_window = screen.root();
@ -166,6 +166,7 @@ impl Gui {
} }
let mut state = State::default(); let mut state = State::default();
let mut cancelled = false;
let redraw = |state: &State| { let redraw = |state: &State| {
xcb_image::put(&self.conn, id, gc, &image.image, 0, 0); xcb_image::put(&self.conn, id, gc, &image.image, 0, 0);
@ -185,6 +186,7 @@ impl Gui {
xcb::KEY_RELEASE => { xcb::KEY_RELEASE => {
let key_evt = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&evt) }; let key_evt = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&evt) };
if key_evt.detail() == 9 { if key_evt.detail() == 9 {
cancelled = true;
break; break;
} }
} }
@ -226,7 +228,7 @@ impl Gui {
xproto::ungrab_keyboard(&self.conn, xcb::CURRENT_TIME).request_check()?; xproto::ungrab_keyboard(&self.conn, xcb::CURRENT_TIME).request_check()?;
xproto::ungrab_pointer(&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()) })
} }
} }

View file

@ -5,6 +5,7 @@ extern crate anyhow;
mod gui; mod gui;
use std::process;
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::Result; use anyhow::Result;
@ -29,8 +30,12 @@ fn main() -> Result<()> {
capture.save_to(&opt.outfile)?; capture.save_to(&opt.outfile)?;
} }
Region::Selection => { Region::Selection => {
let rectangle = gui.interactive_select(&capture)?; if let Some(rectangle) = gui.interactive_select(&capture)? {
capture.save_cropped_to(&opt.outfile, rectangle)?; capture.save_cropped_to(&opt.outfile, rectangle)?;
} else {
info!("Cancelled by user.");
process::exit(1);
}
} }
} }