From bfc7510b5e58f5c6c1a880b58d586c555fa72636 Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Thu, 4 Nov 2021 00:08:31 +0100 Subject: [PATCH] Actions for window source --- src/main.rs | 22 +++++++------- src/sources/test.rs | 2 +- src/sources/windows.rs | 65 ++++++++++++++++++++++++++++++++++++++---- src/ui/ui.rs | 2 +- 4 files changed, 73 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4fdf794..8e0bc91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,9 +21,9 @@ fn main() -> Result<(), Box> { debug!{"Set up roftl"}; let mut roftl = roftl::Roftl::new() .add_source(sources::TestSource::new("ts1")) - // .add_source(sources::Window::new()) - // .add_source(sources::Apps::new()) - // .add_source(sources::ShellHost::new()) + .add_source(sources::Window::new()) + .add_source(sources::Apps::new()) + .add_source(sources::ShellHost::new()) .with_matcher(FuseMatcher::new()); debug!{"Source roftl sources"} @@ -233,23 +233,23 @@ impl RoftlLoop { fn process_action(&mut self, character: char) -> ControlFlow { match character { - '0' => { - trace!{"Retrieved action selection 0. Trigger primary action"} + '1' => { + trace!{"Retrieved action selection 1. Trigger primary action"} self.roftl.action(self.selection, roftl::Action::Primary); ControlFlow::Exit }, - '1' => { - trace!{"Retrieved action selection 1. Trigger secondary action"} + '2' => { + trace!{"Retrieved action selection 2. Trigger secondary action"} self.roftl.action(self.selection, roftl::Action::Secondary); ControlFlow::Exit }, - '2' => { - trace!{"Retrieved action selection 2. Trigger tertiary action"} + '3' => { + trace!{"Retrieved action selection 3. Trigger tertiary action"} self.roftl.action(self.selection, roftl::Action::Tertiary); ControlFlow::Exit }, - '3' => { - trace!{"Retrieved action selection 2. Trigger quaternary action"} + '4' => { + trace!{"Retrieved action selection 4. Trigger quaternary action"} self.roftl.action(self.selection, roftl::Action::Quaternary); ControlFlow::Exit }, diff --git a/src/sources/test.rs b/src/sources/test.rs index e6df8cd..c8dba4c 100644 --- a/src/sources/test.rs +++ b/src/sources/test.rs @@ -43,7 +43,7 @@ impl Source for TestSource { } fn actions(&self, _entry: &Entry) -> Vec { - ["Primary", "Secondary", "Ternary"] + ["Primary", "Secondary", "Tertiary", "Quaternary"] .iter().map(|s| (*s).into()).collect() } } diff --git a/src/sources/windows.rs b/src/sources/windows.rs index 5b83b84..5467594 100644 --- a/src/sources/windows.rs +++ b/src/sources/windows.rs @@ -46,6 +46,33 @@ impl Window { Ok(()) } + + fn toggle_maximize_window(&self, con: &ewmh::Connection, window: u32, screen: i32) -> Result<(), xcb::GenericError> { + let max_horz_atom = ewmh::Connection::WM_STATE_MAXIMIZED_HORZ(con); + let max_vert_atom = ewmh::Connection::WM_STATE_MAXIMIZED_VERT(con); + let action_atom = ewmh::STATE_TOGGLE; + + debug!{"Toggle maximize for {}", window} + ewmh::request_change_wm_state(con, screen, window, action_atom, max_horz_atom, max_vert_atom, 0).request_check()?; + + Ok(()) + } + + fn close_window(&self, con: &ewmh::Connection, window: u32, screen: i32) -> Result<(), xcb::GenericError> { + debug!{"Toggle maximize for {}", window} + ewmh::request_close_window(con, screen, window, XCB_CURRENT_TIME, 0).request_check() + + } + + fn toggle_hide_window(&self, con: &ewmh::Connection, window: u32, screen: i32) -> Result<(), xcb::GenericError> { + let hidden_atom = ewmh::Connection::WM_STATE_HIDDEN(con); + let action_atom = ewmh::STATE_TOGGLE; + + debug!{"Toggle hidden for {}", window} + ewmh::request_change_wm_state(con, screen, window, action_atom, hidden_atom, 0, 0).request_check()?; + + Ok(()) + } } impl Source for Window { @@ -85,13 +112,41 @@ impl Source for Window { } fn action(&self, entry: &Entry, action: Action) { - println!("Doing action {:?} for entry {}", action, entry.name); + debug!("Doing action {:?} for entry {}", action, entry.name); - let (window, screen) = *self.action_data.get(&entry.identifier).unwrap(); + match action { + Action::Primary => { + let (window, screen) = *self.action_data.get(&entry.identifier).unwrap(); + let (xcb_conn, _screen_num) = xcb::base::Connection::connect(None).expect("Could not connect to X server"); + let ewmh_conn = ewmh::Connection::connect(xcb_conn).map_err(|(e,_)| e).unwrap(); + self.switch_window(&ewmh_conn, window, screen).unwrap() + }, + Action::Secondary => { + let (window, screen) = *self.action_data.get(&entry.identifier).unwrap(); + let (xcb_conn, _screen_num) = xcb::base::Connection::connect(None).expect("Could not connect to X server"); + let ewmh_conn = ewmh::Connection::connect(xcb_conn).map_err(|(e,_)| e).unwrap(); + self.toggle_maximize_window(&ewmh_conn, window, screen).unwrap(); + self.switch_window(&ewmh_conn, window, screen).unwrap() + }, + Action::Tertiary => { + let (window, screen) = *self.action_data.get(&entry.identifier).unwrap(); + let (xcb_conn, _screen_num) = xcb::base::Connection::connect(None).expect("Could not connect to X server"); + let ewmh_conn = ewmh::Connection::connect(xcb_conn).map_err(|(e,_)| e).unwrap(); - let (xcb_conn, _screen_num) = xcb::base::Connection::connect(None).expect("Could not connect to X server"); - let ewmh_conn = ewmh::Connection::connect(xcb_conn).map_err(|(e,_)| e).unwrap(); + self.toggle_hide_window(&ewmh_conn, window, screen).unwrap(); + }, + Action::Quaternary => { + let (window, screen) = *self.action_data.get(&entry.identifier).unwrap(); + let (xcb_conn, _screen_num) = xcb::base::Connection::connect(None).expect("Could not connect to X server"); + let ewmh_conn = ewmh::Connection::connect(xcb_conn).map_err(|(e,_)| e).unwrap(); - self.switch_window(&ewmh_conn, window, screen).unwrap() + self.close_window(&ewmh_conn, window, screen).unwrap(); + }, + _ => panic!{"Unknown action {:?}", action} + } + } + + fn actions(&self, _entry: &Entry) -> Vec { + vec!["Switch".into(), "Toggle maximize".into(), "Toggle hide".into(), "Close".into()] } } diff --git a/src/ui/ui.rs b/src/ui/ui.rs index 3d2bd15..20660b7 100644 --- a/src/ui/ui.rs +++ b/src/ui/ui.rs @@ -127,7 +127,7 @@ pub fn draw_actions(window: &Window, actions: Vec) actions.iter().enumerate() .for_each(|(i, r)| { - painter.result_box(0, 1+(i as u32), &usize::to_string(&i), r, &[], false); + painter.result_box(0, 1+(i as u32), &usize::to_string(&(i+1)), r, &[], false); }); }