mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-22 16:34:15 +01:00
feat(cli): implement close cmd
This commit is contained in:
1 parent
a89b9cf5ac
commit
20d6121568
6 files changed
+50
-3
No files matched your search
@@ -0,0 +1,12 @@
|
||||
#![allow(non_upper_case_globals)]
|
||||
|
||||
pub const kAXPressAction: &str = "AXPress";
|
||||
pub const kAXIncrementAction: &str = "AXIncrement";
|
||||
pub const kAXDecrementAction: &str = "AXDecrement";
|
||||
pub const kAXConfirmAction: &str = "AXConfirm";
|
||||
pub const kAXCancelAction: &str = "AXCancel";
|
||||
pub const kAXShowAlternateUIAction: &str = "AXShowAlternateUI";
|
||||
pub const kAXShowDefaultUIAction: &str = "AXShowDefaultUI";
|
||||
pub const kAXRaiseAction: &str = "AXRaise";
|
||||
pub const kAXShowMenuAction: &str = "AXShowMenu";
|
||||
pub const kAXPickAction: &str = "AXPick";
|
||||
@@ -17,6 +17,7 @@ use objc2_core_foundation::kCFRunLoopDefaultMode;
|
||||
use objc2_core_graphics::CGWindowID;
|
||||
use std::ptr::NonNull;
|
||||
|
||||
pub mod action_constants;
|
||||
pub mod attribute_constants;
|
||||
pub mod error;
|
||||
pub mod notification_constants;
|
||||
@@ -100,6 +101,20 @@ impl AccessibilityApi {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn perform_action(
|
||||
element: &AXUIElement,
|
||||
action: &'static str,
|
||||
) -> Result<(), AccessibilityError> {
|
||||
unsafe {
|
||||
match AccessibilityError::from(
|
||||
element.perform_action(&CFString::from_static_str(action)),
|
||||
) {
|
||||
AccessibilityError::Api(AccessibilityApiError::Success) => Ok(()),
|
||||
error => Err(error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_application(process_id: i32) -> CFRetained<AXUIElement> {
|
||||
unsafe { AXUIElement::new_application(process_id) }
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ pub enum SocketMessage {
|
||||
MoveWorkspaceToMonitorNumber(usize),
|
||||
SwapWorkspacesToMonitorNumber(usize),
|
||||
// ForceFocus,
|
||||
// Close,
|
||||
Close,
|
||||
Minimize,
|
||||
Promote,
|
||||
PromoteFocus,
|
||||
|
||||
@@ -245,6 +245,11 @@ impl WindowManager {
|
||||
MacosApi::foreground_window().ok_or_eyre("there is no foreground window")?;
|
||||
AdhocWindow::minimize(&foreground_window)?;
|
||||
}
|
||||
SocketMessage::Close => {
|
||||
let foreground_window =
|
||||
MacosApi::foreground_window().ok_or_eyre("there is no foreground window")?;
|
||||
AdhocWindow::close(&foreground_window)?;
|
||||
}
|
||||
SocketMessage::LockMonitorWorkspaceContainer(
|
||||
monitor_idx,
|
||||
workspace_idx,
|
||||
|
||||
@@ -9,6 +9,8 @@ use crate::PERMAIGNORE_CLASSES;
|
||||
use crate::REGEX_IDENTIFIERS;
|
||||
use crate::WINDOW_RESTORE_POSITIONS;
|
||||
use crate::accessibility::AccessibilityApi;
|
||||
use crate::accessibility::action_constants::kAXPressAction;
|
||||
use crate::accessibility::attribute_constants::kAXCloseButtonAttribute;
|
||||
use crate::accessibility::attribute_constants::kAXFocusedAttribute;
|
||||
use crate::accessibility::attribute_constants::kAXMainAttribute;
|
||||
use crate::accessibility::attribute_constants::kAXMinimizedAttribute;
|
||||
@@ -758,6 +760,16 @@ impl AdhocWindow {
|
||||
AccessibilityApi::set_attribute_cf_value(element, kAXMinimizedAttribute, value)
|
||||
}
|
||||
|
||||
pub fn close(element: &CFRetained<AXUIElement>) -> Result<(), AccessibilityError> {
|
||||
if let Some(close_button) =
|
||||
AccessibilityApi::copy_attribute_value::<AXUIElement>(element, kAXCloseButtonAttribute)
|
||||
{
|
||||
AccessibilityApi::perform_action(&close_button, kAXPressAction)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
pub fn hide(id: u32, element: &CFRetained<AXUIElement>) -> Result<(), AccessibilityError> {
|
||||
let mut window_restore_positions = WINDOW_RESTORE_POSITIONS.lock();
|
||||
|
||||
@@ -662,8 +662,8 @@ enum SubCommand {
|
||||
Move(Move),
|
||||
/// Minimize the focused window
|
||||
Minimize,
|
||||
// /// Close the focused window
|
||||
// Close,
|
||||
/// Close the focused window
|
||||
Close,
|
||||
// /// Forcibly focus the window at the cursor with a left mouse click
|
||||
// ForceFocus,
|
||||
/// Change focus to the window in the specified cycle direction
|
||||
@@ -1307,6 +1307,9 @@ fn main() -> eyre::Result<()> {
|
||||
SubCommand::Retile => {
|
||||
send_message(&SocketMessage::Retile)?;
|
||||
}
|
||||
SubCommand::Close => {
|
||||
send_message(&SocketMessage::Close)?;
|
||||
}
|
||||
SubCommand::Minimize => {
|
||||
send_message(&SocketMessage::Minimize)?;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user