mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-16 05:41:16 +01:00
feat(wm): implement move resizing and moving
This commit implements moving and resizing by mouse as it works in komorebi for Windows. We're able to make this a bit nicer here because macOS emits separate events for resizing and movement, so now we have MoveStart/MoveEnd and ResizeStart/ResizeEnd. Resizing is a little janky because the system does not update the position of resized windows quickly enough, so if you do a quick drag and release the left mouse button, sometimes the accessibility attributes for the size and position of that window won't have been updated, and no resize will take place.
This commit is contained in:
1 parent
69909df2f5
commit
fb416e75fd
7 files changed
+568
-9
No files matched your search
@@ -1,7 +1,5 @@
|
||||
* The flow for manually adding Accessibility and Screen Recording permissions
|
||||
sucks
|
||||
* Mouse resizing is not implemented
|
||||
* Swapping window containers via mouse dragging is not implemented
|
||||
* Fullscreen windows aren't supported (and probably won't be) and break the
|
||||
state (probably shouldn't)
|
||||
* Application hiding isn't supported (and probably won't be) and breaks the
|
||||
|
||||
@@ -32,6 +32,9 @@ use objc2_core_foundation::CFString;
|
||||
use objc2_core_foundation::CGPoint;
|
||||
use objc2_core_foundation::CGRect;
|
||||
use objc2_core_foundation::CGSize;
|
||||
use objc2_core_graphics::CGEvent;
|
||||
use objc2_core_graphics::CGEventSource;
|
||||
use objc2_core_graphics::CGEventSourceStateID;
|
||||
use objc2_core_graphics::CGMainDisplayID;
|
||||
use objc2_foundation::NSNumber;
|
||||
use std::collections::HashMap;
|
||||
@@ -398,12 +401,17 @@ impl MacosApi {
|
||||
|
||||
pub fn cursor_pos() -> CGPoint {
|
||||
unsafe {
|
||||
let point = NSEvent::mouseLocation();
|
||||
CGPoint::new(point.x, point.y)
|
||||
let source = CGEventSource::new(CGEventSourceStateID::HIDSystemState);
|
||||
let event = CGEvent::new(source.as_deref());
|
||||
CGEvent::location(event.as_deref())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn monitor_from_point(point: CGPoint) -> Option<u32> {
|
||||
CoreGraphicsApi::display_with_point(point)
|
||||
}
|
||||
|
||||
pub fn left_mouse_button_is_pressed() -> bool {
|
||||
(unsafe { NSEvent::pressedMouseButtons() } & 0x1 != 0)
|
||||
}
|
||||
}
|
||||
@@ -10,10 +10,13 @@ use crate::accessibility::error::AccessibilityApiError;
|
||||
use crate::accessibility::error::AccessibilityError;
|
||||
use crate::accessibility::notification_constants::AccessibilityNotification;
|
||||
use crate::app_kit_notification_constants::AppKitWorkspaceNotification;
|
||||
use crate::core::Sizing;
|
||||
use crate::core::WindowContainerBehaviour;
|
||||
use crate::core::config_generation::MatchingRule;
|
||||
use crate::core::default_layout::DefaultLayout;
|
||||
use crate::core::layout::Layout;
|
||||
use crate::core::operation_direction::OperationDirection;
|
||||
use crate::core::rect::Rect;
|
||||
use crate::macos_api::MacosApi;
|
||||
use crate::notify_subscribers;
|
||||
use crate::state::State;
|
||||
@@ -594,6 +597,317 @@ impl WindowManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
WindowManagerEvent::MoveStart(_, _, window_id) => {
|
||||
if self.pending_move_op.is_none() && self.pending_resize_op.is_none() {
|
||||
let monitor_idx = self.focused_monitor_idx();
|
||||
let workspace_idx = self
|
||||
.focused_monitor()
|
||||
.ok_or_eyre("there is no monitor with this idx")?
|
||||
.focused_workspace_idx();
|
||||
|
||||
let pending_move_op = Arc::make_mut(&mut self.pending_move_op);
|
||||
*pending_move_op = Option::from((monitor_idx, workspace_idx, window_id));
|
||||
}
|
||||
}
|
||||
WindowManagerEvent::MoveEnd(_, _, window_id) => {
|
||||
// We need this because if the event ends on a different monitor,
|
||||
// that monitor will already have been focused and updated in the state
|
||||
let pending = *self.pending_move_op;
|
||||
// Always consume the pending move op whenever this event is handled
|
||||
let pending_move_op = Arc::make_mut(&mut self.pending_move_op);
|
||||
*pending_move_op = None;
|
||||
|
||||
if let Some((origin_monitor_idx, origin_workspace_idx, wid)) = pending {
|
||||
// If the window handles don't match then something went wrong and the pending move
|
||||
// is not related to this current move, if so abort this operation.
|
||||
if wid != window_id {
|
||||
eyre::bail!(
|
||||
"window handles for move operation don't match: {} != {}",
|
||||
wid,
|
||||
window_id
|
||||
);
|
||||
}
|
||||
let known_window_ids = self.known_window_ids.clone();
|
||||
|
||||
let target_monitor_idx = self
|
||||
.monitor_idx_from_current_pos()
|
||||
.ok_or_eyre("cannot get monitor idx from current position")?;
|
||||
|
||||
let focused_monitor_idx = self.focused_monitor_idx();
|
||||
let focused_workspace_idx = self.focused_workspace_idx().unwrap_or_default();
|
||||
let window_management_behaviour = self
|
||||
.window_management_behaviour(focused_monitor_idx, focused_workspace_idx);
|
||||
|
||||
let workspace = self.focused_workspace_mut()?;
|
||||
let focused_container_idx = workspace.focused_container_idx();
|
||||
|
||||
if let Some(container) = workspace.focused_container()
|
||||
&& let Some(window) = container.focused_window()
|
||||
{
|
||||
// TODO: not sure about this clone
|
||||
let window = window.clone();
|
||||
let new_position = Rect::from(MacosApi::window_rect(&window.element)?);
|
||||
let old_position = *workspace
|
||||
.latest_layout
|
||||
.get(focused_container_idx)
|
||||
// If the move was to another monitor with an empty workspace, the
|
||||
// workspace here will refer to that empty workspace, which won't
|
||||
// have any latest layout set. We fall back to a Default for Rect
|
||||
// which allows us to make a reasonable guess that the drag has taken
|
||||
// place across a monitor boundary to an empty workspace
|
||||
.unwrap_or(&Rect::default());
|
||||
|
||||
// This will be true if we have moved to another monitor
|
||||
let mut moved_across_monitors = false;
|
||||
|
||||
if let Some((m_idx, _)) = known_window_ids.get(&window_id)
|
||||
&& *m_idx != target_monitor_idx
|
||||
{
|
||||
moved_across_monitors = true;
|
||||
}
|
||||
|
||||
// If we didn't move to another monitor with an empty workspace, it is
|
||||
// still possible that we moved to another monitor with a populated workspace
|
||||
if !moved_across_monitors {
|
||||
// So we'll check if the origin monitor index and the target monitor index
|
||||
// are different, if they are, we can set the override
|
||||
moved_across_monitors = origin_monitor_idx != target_monitor_idx;
|
||||
|
||||
if moved_across_monitors {
|
||||
// Want to make sure that we exclude unmanaged windows from cross-monitor
|
||||
// moves with a mouse, otherwise the currently focused idx container will
|
||||
// be moved when we just want to drag an unmanaged window
|
||||
let origin_workspace = self
|
||||
.monitors()
|
||||
.get(origin_monitor_idx)
|
||||
.ok_or_eyre("cannot get monitor idx")?
|
||||
.workspaces()
|
||||
.get(origin_workspace_idx)
|
||||
.ok_or_eyre("cannot get workspace idx")?;
|
||||
|
||||
let managed_window = origin_workspace.contains_window(window_id);
|
||||
|
||||
if !managed_window {
|
||||
moved_across_monitors = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let workspace = self.focused_workspace_mut()?;
|
||||
if (workspace.tile && workspace.contains_managed_window(window_id))
|
||||
|| moved_across_monitors
|
||||
{
|
||||
let resize = Rect {
|
||||
left: new_position.left - old_position.left,
|
||||
top: new_position.top - old_position.top,
|
||||
right: new_position.right - old_position.right,
|
||||
bottom: new_position.bottom - old_position.bottom,
|
||||
};
|
||||
|
||||
// If we have moved across the monitors, use that override, otherwise determine
|
||||
// if a move has taken place by ruling out a resize
|
||||
let right_bottom_constant = 0;
|
||||
|
||||
let is_move = moved_across_monitors
|
||||
|| resize.right.abs() == right_bottom_constant
|
||||
&& resize.bottom.abs() == right_bottom_constant;
|
||||
|
||||
if is_move {
|
||||
tracing::info!("moving with mouse");
|
||||
|
||||
if moved_across_monitors {
|
||||
if let Some((
|
||||
origin_monitor_idx,
|
||||
origin_workspace_idx,
|
||||
w_hwnd,
|
||||
)) = pending
|
||||
{
|
||||
let target_workspace_idx = self
|
||||
.monitors()
|
||||
.get(target_monitor_idx)
|
||||
.ok_or_eyre("there is no monitor at this idx")?
|
||||
.focused_workspace_idx();
|
||||
|
||||
let target_container_idx = self
|
||||
.monitors()
|
||||
.get(target_monitor_idx)
|
||||
.ok_or_eyre("there is no monitor at this idx")?
|
||||
.focused_workspace()
|
||||
.ok_or_eyre(
|
||||
"there is no focused workspace for this monitor",
|
||||
)?
|
||||
.container_idx_from_current_point()
|
||||
// Default to 0 in the case of an empty workspace
|
||||
.unwrap_or(0);
|
||||
|
||||
let origin =
|
||||
(origin_monitor_idx, origin_workspace_idx, w_hwnd);
|
||||
let target = (
|
||||
target_monitor_idx,
|
||||
target_workspace_idx,
|
||||
target_container_idx,
|
||||
);
|
||||
self.transfer_window(origin, target)?;
|
||||
|
||||
// We want to make sure both the origin and target monitors are updated,
|
||||
// so that we don't have ghost tiles until we force an interaction on
|
||||
// the origin monitor's focused workspace
|
||||
self.focus_monitor(origin_monitor_idx)?;
|
||||
let origin_monitor = self
|
||||
.monitors_mut()
|
||||
.get_mut(origin_monitor_idx)
|
||||
.ok_or_eyre("there is no monitor at this idx")?;
|
||||
origin_monitor.focus_workspace(origin_workspace_idx)?;
|
||||
self.update_focused_workspace(false, false)?;
|
||||
|
||||
self.focus_monitor(target_monitor_idx)?;
|
||||
let target_monitor = self
|
||||
.monitors_mut()
|
||||
.get_mut(target_monitor_idx)
|
||||
.ok_or_eyre("there is no monitor at this idx")?;
|
||||
target_monitor.focus_workspace(target_workspace_idx)?;
|
||||
self.update_focused_workspace(false, false)?;
|
||||
|
||||
// Make sure to give focus to the moved window again
|
||||
window.focus(self.mouse_follows_focus)?;
|
||||
}
|
||||
} else if window_management_behaviour.float_override {
|
||||
// TODO: unsure of this clone
|
||||
workspace.floating_windows_mut().push_back(window);
|
||||
self.update_focused_workspace(false, false)?;
|
||||
} else {
|
||||
match window_management_behaviour.current_behaviour {
|
||||
WindowContainerBehaviour::Create => {
|
||||
match workspace.container_idx_from_current_point() {
|
||||
Some(target_idx) => {
|
||||
workspace.swap_containers(
|
||||
focused_container_idx,
|
||||
target_idx,
|
||||
);
|
||||
self.update_focused_workspace(false, false)?;
|
||||
}
|
||||
None => {
|
||||
self.update_focused_workspace(
|
||||
self.mouse_follows_focus,
|
||||
false,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
WindowContainerBehaviour::Append => {
|
||||
match workspace.container_idx_from_current_point() {
|
||||
Some(target_idx) => {
|
||||
workspace
|
||||
.move_window_to_container(target_idx)?;
|
||||
self.update_focused_workspace(false, false)?;
|
||||
}
|
||||
None => {
|
||||
self.update_focused_workspace(
|
||||
self.mouse_follows_focus,
|
||||
false,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
WindowManagerEvent::ResizeStart(_, _, window_id) => {
|
||||
let workspace = self.focused_workspace_mut()?;
|
||||
if let Some(container) = workspace.focused_container()
|
||||
&& let Some(window) = container.focused_window()
|
||||
{
|
||||
let window = window.clone();
|
||||
let new_position = Rect::from(MacosApi::window_rect(&window.element)?);
|
||||
|
||||
let pending_resize_op = Arc::make_mut(&mut self.pending_resize_op);
|
||||
*pending_resize_op = Option::from((window_id, Some(new_position)));
|
||||
}
|
||||
}
|
||||
WindowManagerEvent::ResizeEnd(_, _, window_id) => {
|
||||
let pending = *self.pending_resize_op;
|
||||
// Always consume the pending resize op whenever this event is handled
|
||||
let pending_resize_op = Arc::make_mut(&mut self.pending_resize_op);
|
||||
*pending_resize_op = None;
|
||||
|
||||
if let Some((wid, Some(new_position))) = pending {
|
||||
// If the window handles don't match then something went wrong and the pending resize
|
||||
// is not related to this current resize, if so abort this operation.
|
||||
if wid != window_id {
|
||||
eyre::bail!(
|
||||
"window handles for resize operation don't match: {} != {}",
|
||||
wid,
|
||||
window_id
|
||||
);
|
||||
}
|
||||
|
||||
let workspace = self.focused_workspace_mut()?;
|
||||
let focused_container_idx = workspace.focused_container_idx();
|
||||
|
||||
if let Some(container) = workspace.focused_container()
|
||||
&& let Some(window) = container.focused_window()
|
||||
{
|
||||
let window = window.clone();
|
||||
|
||||
let old_position = *workspace
|
||||
.latest_layout
|
||||
.get(focused_container_idx)
|
||||
.unwrap_or(&Rect::default());
|
||||
|
||||
let workspace = self.focused_workspace_mut()?;
|
||||
if workspace.tile && workspace.contains_managed_window(window.id) {
|
||||
let resize = Rect {
|
||||
left: new_position.left - old_position.left,
|
||||
top: new_position.top - old_position.top,
|
||||
right: new_position.right - old_position.right,
|
||||
bottom: new_position.bottom - old_position.bottom,
|
||||
};
|
||||
|
||||
tracing::info!("resizing with mouse");
|
||||
let mut ops = vec![];
|
||||
|
||||
macro_rules! resize_op {
|
||||
($coordinate:expr, $comparator:tt, $direction:expr) => {{
|
||||
let adjusted = $coordinate * 2;
|
||||
let sizing = if adjusted $comparator 0 {
|
||||
Sizing::Decrease
|
||||
} else {
|
||||
Sizing::Increase
|
||||
};
|
||||
|
||||
($direction, sizing, adjusted.abs())
|
||||
}};
|
||||
}
|
||||
|
||||
if resize.left != 0 {
|
||||
ops.push(resize_op!(resize.left, >, OperationDirection::Left));
|
||||
}
|
||||
|
||||
if resize.top != 0 {
|
||||
ops.push(resize_op!(resize.top, >, OperationDirection::Up));
|
||||
}
|
||||
|
||||
if resize.right != 0 && (resize.left == 0) {
|
||||
ops.push(resize_op!(resize.right, <, OperationDirection::Right));
|
||||
}
|
||||
|
||||
if resize.bottom != 0 && (resize.top == 0) {
|
||||
ops.push(resize_op!(resize.bottom, <, OperationDirection::Down));
|
||||
}
|
||||
|
||||
for (edge, sizing, delta) in ops {
|
||||
self.resize_window(edge, sizing, delta, true)?;
|
||||
}
|
||||
|
||||
self.update_focused_workspace(false, false)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.update_known_window_ids();
|
||||
|
||||
@@ -1159,6 +1159,8 @@ impl StaticConfig {
|
||||
hotwatch: Hotwatch::new()?,
|
||||
run_loop: CoreFoundationRunLoop(run_loop.clone()),
|
||||
minimized_windows: HashMap::new(),
|
||||
pending_move_op: Arc::new(None),
|
||||
pending_resize_op: Arc::new(None),
|
||||
already_moved_window_handles: Default::default(),
|
||||
known_window_ids: HashMap::new(),
|
||||
};
|
||||
|
||||
@@ -74,6 +74,8 @@ pub struct WindowManager {
|
||||
pub work_area_offset: Option<Rect>,
|
||||
pub incoming_events: Receiver<WindowManagerEvent>,
|
||||
pub minimized_windows: HashMap<u32, Window>,
|
||||
pub pending_move_op: Arc<Option<(usize, usize, u32)>>,
|
||||
pub pending_resize_op: Arc<Option<(u32, Option<Rect>)>>,
|
||||
pub already_moved_window_handles: Arc<Mutex<HashSet<u32>>>,
|
||||
/// Maps each known window id to the (monitor, workspace) index pair managing it
|
||||
pub known_window_ids: HashMap<u32, (usize, usize)>,
|
||||
@@ -154,6 +156,8 @@ impl WindowManager {
|
||||
work_area_offset: None,
|
||||
incoming_events: incoming,
|
||||
minimized_windows: HashMap::new(),
|
||||
pending_move_op: Arc::new(None),
|
||||
pending_resize_op: Arc::new(None),
|
||||
already_moved_window_handles: Default::default(),
|
||||
known_window_ids: Default::default(),
|
||||
})
|
||||
@@ -2959,4 +2963,170 @@ impl WindowManager {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn transfer_window(
|
||||
&mut self,
|
||||
origin: (usize, usize, u32),
|
||||
target: (usize, usize, usize),
|
||||
) -> eyre::Result<()> {
|
||||
let (origin_monitor_idx, origin_workspace_idx, window_id) = origin;
|
||||
let (target_monitor_idx, target_workspace_idx, target_container_idx) = target;
|
||||
|
||||
let origin_workspace = self
|
||||
.monitors_mut()
|
||||
.get_mut(origin_monitor_idx)
|
||||
.ok_or_eyre("cannot get monitor idx")?
|
||||
.workspaces_mut()
|
||||
.get_mut(origin_workspace_idx)
|
||||
.ok_or_eyre("cannot get workspace idx")?;
|
||||
|
||||
let origin_container_idx = origin_workspace
|
||||
.container_for_window(window_id)
|
||||
.and_then(|c| origin_workspace.containers().iter().position(|cc| cc == c));
|
||||
|
||||
if let Some(origin_container_idx) = origin_container_idx {
|
||||
// Moving normal container window
|
||||
self.transfer_container(
|
||||
(
|
||||
origin_monitor_idx,
|
||||
origin_workspace_idx,
|
||||
origin_container_idx,
|
||||
),
|
||||
(
|
||||
target_monitor_idx,
|
||||
target_workspace_idx,
|
||||
target_container_idx,
|
||||
),
|
||||
)?;
|
||||
} else if let Some(idx) = origin_workspace
|
||||
.floating_windows()
|
||||
.iter()
|
||||
.position(|w| w.id == window_id)
|
||||
{
|
||||
// Moving floating window
|
||||
// There is no need to physically move the floating window between areas with
|
||||
// `move_to_area` because the user already did that, so we only need to transfer the
|
||||
// window to the target `floating_windows`
|
||||
if let Some(floating_window) = origin_workspace.floating_windows_mut().remove(idx) {
|
||||
let target_workspace = self
|
||||
.monitors_mut()
|
||||
.get_mut(target_monitor_idx)
|
||||
.ok_or_eyre("there is no monitor at this idx")?
|
||||
.focused_workspace_mut()
|
||||
.ok_or_eyre("there is no focused workspace for this monitor")?;
|
||||
|
||||
target_workspace
|
||||
.floating_windows_mut()
|
||||
.push_back(floating_window);
|
||||
}
|
||||
} else if origin_workspace
|
||||
.monocle_container
|
||||
.as_ref()
|
||||
.and_then(|monocle| monocle.focused_window().map(|w| w.id == window_id))
|
||||
.unwrap_or_default()
|
||||
{
|
||||
// Moving monocle container
|
||||
if let Some(monocle_idx) = origin_workspace.monocle_container_restore_idx {
|
||||
let origin_workspace = self
|
||||
.monitors_mut()
|
||||
.get_mut(origin_monitor_idx)
|
||||
.ok_or_eyre("there is no monitor at this idx")?
|
||||
.workspaces_mut()
|
||||
.get_mut(origin_workspace_idx)
|
||||
.ok_or_eyre("there is no workspace for this monitor")?;
|
||||
|
||||
for container in origin_workspace.containers_mut() {
|
||||
container.restore()?;
|
||||
}
|
||||
|
||||
origin_workspace.reintegrate_monocle_container()?;
|
||||
|
||||
self.transfer_container(
|
||||
(origin_monitor_idx, origin_workspace_idx, monocle_idx),
|
||||
(
|
||||
target_monitor_idx,
|
||||
target_workspace_idx,
|
||||
target_container_idx,
|
||||
),
|
||||
)?;
|
||||
// TODO: don't think this is needed on macOS
|
||||
// After we restore the origin workspace, some windows that were cloacked
|
||||
// by the monocle might now be uncloacked which would trigger a workspace
|
||||
// reconciliation since the focused monitor would be different from origin.
|
||||
// That workspace reconciliation would focus the window on the origin monitor.
|
||||
// So we need to ignore the uncloak events produced by the origin workspace
|
||||
// restore to avoid that issue.
|
||||
// self.uncloack_to_ignore = uncloack_amount;
|
||||
}
|
||||
// TODO: this probably won't be used on macOS
|
||||
} else if origin_workspace
|
||||
.maximized_window
|
||||
.as_ref()
|
||||
.map(|max| max.id == window_id)
|
||||
.unwrap_or_default()
|
||||
{
|
||||
// Moving maximized_window
|
||||
// TODO: not sure if we'll support maximized windows on macOS
|
||||
// if let Some(maximized_idx) = origin_workspace.maximized_window_restore_idx {
|
||||
// self.focus_monitor(origin_monitor_idx)?;
|
||||
// let origin_monitor = self
|
||||
// .focused_monitor_mut()
|
||||
// .ok_or_eyre("there is no origin monitor")?;
|
||||
// origin_monitor.focus_workspace(origin_workspace_idx)?;
|
||||
// self.unmaximize_window()?;
|
||||
// self.focus_monitor(target_monitor_idx)?;
|
||||
// let target_monitor = self
|
||||
// .focused_monitor_mut()
|
||||
// .ok_or_eyre("there is no target monitor")?;
|
||||
// target_monitor.focus_workspace(target_workspace_idx)?;
|
||||
//
|
||||
// self.transfer_container(
|
||||
// (origin_monitor_idx, origin_workspace_idx, maximized_idx),
|
||||
// (
|
||||
// target_monitor_idx,
|
||||
// target_workspace_idx,
|
||||
// target_container_idx,
|
||||
// ),
|
||||
// )?;
|
||||
// }
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn transfer_container(
|
||||
&mut self,
|
||||
origin: (usize, usize, usize),
|
||||
target: (usize, usize, usize),
|
||||
) -> eyre::Result<()> {
|
||||
let (origin_monitor_idx, origin_workspace_idx, origin_container_idx) = origin;
|
||||
let (target_monitor_idx, target_workspace_idx, target_container_idx) = target;
|
||||
|
||||
let origin_container = self
|
||||
.monitors_mut()
|
||||
.get_mut(origin_monitor_idx)
|
||||
.ok_or_eyre("there is no monitor at this index")?
|
||||
.workspaces_mut()
|
||||
.get_mut(origin_workspace_idx)
|
||||
.ok_or_eyre("there is no workspace at this index")?
|
||||
.remove_container(origin_container_idx)
|
||||
.ok_or_eyre("there is no container at this index")?;
|
||||
|
||||
let target_workspace = self
|
||||
.monitors_mut()
|
||||
.get_mut(target_monitor_idx)
|
||||
.ok_or_eyre("there is no monitor at this index")?
|
||||
.workspaces_mut()
|
||||
.get_mut(target_workspace_idx)
|
||||
.ok_or_eyre("there is no workspace at this index")?;
|
||||
|
||||
target_workspace
|
||||
.containers_mut()
|
||||
.insert(target_container_idx, origin_container);
|
||||
|
||||
target_workspace.focus_container(target_container_idx);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use crate::accessibility::notification_constants::AccessibilityNotification;
|
||||
use crate::app_kit_notification_constants::AppKitWorkspaceNotification;
|
||||
use crate::macos_api::MacosApi;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use strum::Display;
|
||||
@@ -37,6 +38,10 @@ pub enum WindowManagerEvent {
|
||||
Restore(SystemNotification, i32, u32),
|
||||
Manage(SystemNotification, i32, u32),
|
||||
Unmanage(SystemNotification, i32, u32),
|
||||
MoveStart(SystemNotification, i32, u32),
|
||||
MoveEnd(SystemNotification, i32, u32),
|
||||
ResizeStart(SystemNotification, i32, u32),
|
||||
ResizeEnd(SystemNotification, i32, u32),
|
||||
}
|
||||
|
||||
impl WindowManagerEvent {
|
||||
@@ -87,11 +92,40 @@ impl WindowManagerEvent {
|
||||
.map(|window_id| WindowManagerEvent::Manage(notification, process_id, window_id)),
|
||||
SystemNotification::Manual(ManualNotification::Unmanage) => window_id
|
||||
.map(|window_id| WindowManagerEvent::Unmanage(notification, process_id, window_id)),
|
||||
// kAXWindowMovedNotification => {}
|
||||
// kAXWindowResizedNotification => {}
|
||||
// kAXTitleChangedNotification => {}
|
||||
// kAXApplicationDeactivatedNotification => {}
|
||||
_ => None,
|
||||
SystemNotification::Accessibility(AccessibilityNotification::AXWindowMoved) => {
|
||||
if MacosApi::left_mouse_button_is_pressed() {
|
||||
window_id.map(|window_id| {
|
||||
WindowManagerEvent::MoveStart(notification, process_id, window_id)
|
||||
})
|
||||
} else {
|
||||
window_id.map(|window_id| {
|
||||
WindowManagerEvent::MoveEnd(notification, process_id, window_id)
|
||||
})
|
||||
}
|
||||
}
|
||||
SystemNotification::Accessibility(AccessibilityNotification::AXWindowResized) => {
|
||||
if MacosApi::left_mouse_button_is_pressed() {
|
||||
window_id.map(|window_id| {
|
||||
WindowManagerEvent::ResizeStart(notification, process_id, window_id)
|
||||
})
|
||||
} else {
|
||||
window_id.map(|window_id| {
|
||||
WindowManagerEvent::ResizeEnd(notification, process_id, window_id)
|
||||
})
|
||||
}
|
||||
}
|
||||
notification => {
|
||||
let notification_string = match notification {
|
||||
SystemNotification::Accessibility(n) => n.to_string(),
|
||||
SystemNotification::AppKitWorkspace(n) => n.to_string(),
|
||||
SystemNotification::Manual(n) => n.to_string(),
|
||||
};
|
||||
|
||||
tracing::trace!(
|
||||
"ignoring system notification: {notification_string} (pid: {process_id}, window id: {window_id:?})"
|
||||
);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,6 +137,10 @@ impl WindowManagerEvent {
|
||||
| WindowManagerEvent::Minimize(_, process_id, _)
|
||||
| WindowManagerEvent::Manage(_, process_id, _)
|
||||
| WindowManagerEvent::Unmanage(_, process_id, _)
|
||||
| WindowManagerEvent::MoveStart(_, process_id, _)
|
||||
| WindowManagerEvent::MoveEnd(_, process_id, _)
|
||||
| WindowManagerEvent::ResizeStart(_, process_id, _)
|
||||
| WindowManagerEvent::ResizeEnd(_, process_id, _)
|
||||
| WindowManagerEvent::Restore(_, process_id, _) => *process_id,
|
||||
}
|
||||
}
|
||||
@@ -114,6 +152,10 @@ impl WindowManagerEvent {
|
||||
| WindowManagerEvent::Destroy(n, _)
|
||||
| WindowManagerEvent::Minimize(n, _, _)
|
||||
| WindowManagerEvent::Manage(n, _, _)
|
||||
| WindowManagerEvent::MoveStart(n, _, _)
|
||||
| WindowManagerEvent::MoveEnd(n, _, _)
|
||||
| WindowManagerEvent::ResizeStart(n, _, _)
|
||||
| WindowManagerEvent::ResizeEnd(n, _, _)
|
||||
| WindowManagerEvent::Unmanage(n, _, _)
|
||||
| WindowManagerEvent::Restore(n, _, _) => match n {
|
||||
SystemNotification::Accessibility(a) => a.to_string(),
|
||||
@@ -130,6 +172,10 @@ impl WindowManagerEvent {
|
||||
WindowManagerEvent::Minimize(_, _, window_id)
|
||||
| WindowManagerEvent::Manage(_, _, window_id)
|
||||
| WindowManagerEvent::Unmanage(_, _, window_id)
|
||||
| WindowManagerEvent::MoveStart(_, _, window_id)
|
||||
| WindowManagerEvent::MoveEnd(_, _, window_id)
|
||||
| WindowManagerEvent::ResizeStart(_, _, window_id)
|
||||
| WindowManagerEvent::ResizeEnd(_, _, window_id)
|
||||
| WindowManagerEvent::Restore(_, _, window_id) => Some(*window_id),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1128,6 +1128,27 @@ impl Workspace {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn container_idx_from_current_point(&self) -> Option<usize> {
|
||||
let mut idx = None;
|
||||
|
||||
let point = MacosApi::cursor_pos();
|
||||
|
||||
for (i, _container) in self.containers().iter().enumerate() {
|
||||
if let Some(rect) = self.latest_layout.get(i)
|
||||
&& rect.contains_point((point.x as i32, point.y as i32))
|
||||
{
|
||||
idx = Option::from(i);
|
||||
}
|
||||
}
|
||||
|
||||
idx
|
||||
}
|
||||
|
||||
pub fn container_for_window(&self, window_id: u32) -> Option<&Container> {
|
||||
self.containers()
|
||||
.get(self.container_idx_for_window(window_id)?)
|
||||
}
|
||||
}
|
||||
|
||||
impl Workspace {
|
||||
|
||||
Reference in new issue
Block a user