mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-18 14:48:34 +01:00
feat(wm): implement monocle containers
This commit migrates the code around Monocle containers from komorebi and implements everything needed to handle SocketMessage::ToggleMonocle in the wm and send the SocketMessage from komorebic. I think I should spend time implementing as many Workspace features as I can before I turn my attention to anything else, because hunting for where to re-introduce the monocle code I had previously omitted to get everything compiling was quite tedious and I'm sure I've probably missed things.
This commit is contained in:
1 parent
061d7e3e73
commit
558a1578c8
7 files changed
+182
-4
No files matched your search
@@ -34,6 +34,7 @@ pub enum SocketMessage {
|
||||
UnstackWindow,
|
||||
ChangeLayout(DefaultLayout),
|
||||
TogglePause,
|
||||
ToggleMonocle,
|
||||
FocusWorkspaceNumber(usize),
|
||||
MoveContainerToWorkspaceNumber(usize),
|
||||
SendContainerToWorkspaceNumber(usize),
|
||||
|
||||
@@ -148,6 +148,14 @@ impl Monitor {
|
||||
Some(workspace) => workspace,
|
||||
};
|
||||
|
||||
if target_workspace.monocle_container.is_some() {
|
||||
for container in target_workspace.containers_mut() {
|
||||
container.restore()?;
|
||||
}
|
||||
|
||||
target_workspace.reintegrate_monocle_container()?;
|
||||
}
|
||||
|
||||
if let Some(direction) = direction {
|
||||
self.add_container_with_direction(container, Some(target_workspace_idx), direction)?;
|
||||
} else {
|
||||
|
||||
@@ -90,6 +90,7 @@ impl WindowManager {
|
||||
SocketMessage::SendContainerToWorkspaceNumber(workspace_idx) => {
|
||||
self.move_container_to_workspace(workspace_idx, false, None)?;
|
||||
}
|
||||
SocketMessage::ToggleMonocle => self.toggle_monocle()?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -274,7 +274,7 @@ impl Window {
|
||||
self.set_size(CGSize::new(rect.right as CGFloat, rect.bottom as CGFloat))
|
||||
}
|
||||
|
||||
pub fn focus(&self, focus_follows_mouse: bool) -> Result<(), LibraryError> {
|
||||
pub fn focus(&self, mouse_follows_focus: bool) -> Result<(), LibraryError> {
|
||||
unsafe {
|
||||
NSRunningApplication::runningApplicationWithProcessIdentifier(
|
||||
self.application.process_id,
|
||||
@@ -289,8 +289,8 @@ impl Window {
|
||||
AccessibilityApi::set_attribute_cf_value(&self.element, kAXMainAttribute, value)?;
|
||||
}
|
||||
|
||||
if focus_follows_mouse {
|
||||
// MacosApi::center_cursor_in_rect(&MacosApi::window_rect(&self.element))?
|
||||
if mouse_follows_focus {
|
||||
MacosApi::center_cursor_in_rect(&MacosApi::window_rect(&self.element)?.into())?
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -454,4 +454,44 @@ impl WindowManager {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn toggle_monocle(&mut self) -> eyre::Result<()> {
|
||||
let workspace = self.focused_workspace()?;
|
||||
match workspace.monocle_container {
|
||||
None => self.monocle_on()?,
|
||||
Some(_) => self.monocle_off()?,
|
||||
}
|
||||
|
||||
self.update_focused_workspace(true, true)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn monocle_on(&mut self) -> eyre::Result<()> {
|
||||
tracing::info!("enabling monocle");
|
||||
|
||||
let workspace = self.focused_workspace_mut()?;
|
||||
workspace.new_monocle_container()?;
|
||||
|
||||
for container in workspace.containers_mut() {
|
||||
container.hide(None)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn monocle_off(&mut self) -> eyre::Result<()> {
|
||||
tracing::info!("disabling monocle");
|
||||
|
||||
let workspace = self.focused_workspace_mut()?;
|
||||
|
||||
for container in workspace.containers_mut() {
|
||||
container.restore()?;
|
||||
}
|
||||
|
||||
workspace.reintegrate_monocle_container()
|
||||
}
|
||||
}
|
||||
+124
-1
@@ -17,6 +17,8 @@ impl_ring_elements!(Workspace, Container);
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Workspace {
|
||||
pub containers: Ring<Container>,
|
||||
pub monocle_container: Option<Container>,
|
||||
pub monocle_container_restore_idx: Option<usize>,
|
||||
pub workspace_padding: Option<i32>,
|
||||
pub container_padding: Option<i32>,
|
||||
pub resize_dimensions: Vec<Option<Rect>>,
|
||||
@@ -33,6 +35,8 @@ impl Default for Workspace {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
containers: Default::default(),
|
||||
monocle_container: None,
|
||||
monocle_container_restore_idx: None,
|
||||
workspace_padding: None,
|
||||
container_padding: None,
|
||||
resize_dimensions: vec![],
|
||||
@@ -98,7 +102,12 @@ impl Workspace {
|
||||
|
||||
#[allow(clippy::collapsible_if)]
|
||||
if self.tile {
|
||||
if !self.containers().is_empty() {
|
||||
if let Some(container) = self.monocle_container.as_mut() {
|
||||
if let Some(window) = container.focused_window_mut() {
|
||||
adjusted_work_area.add_padding(container_padding);
|
||||
window.set_position(&adjusted_work_area)?;
|
||||
};
|
||||
} else if !self.containers().is_empty() {
|
||||
let mut layouts = self.layout.as_boxed_arrangement().calculate(
|
||||
&adjusted_work_area,
|
||||
NonZeroUsize::new(self.containers().len()).ok_or_else(|| {
|
||||
@@ -126,9 +135,26 @@ impl Workspace {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.latest_layout = layouts;
|
||||
}
|
||||
}
|
||||
|
||||
// Always make sure that the length of the resize dimensions vec is the same as the
|
||||
// number of layouts / containers. This should never actually truncate as the remove_window
|
||||
// function takes care of cleaning up resize dimensions when destroying empty containers
|
||||
let container_count = self.containers().len();
|
||||
|
||||
// since monocle is a toggle, we never want to truncate the resize dimensions since it will
|
||||
// almost always be toggled off and the container will be reintegrated into layout
|
||||
//
|
||||
// without this check, if there are exactly two containers, when one is toggled to monocle
|
||||
// the resize dimensions will be truncated to len == 1, and when it is reintegrated, if it
|
||||
// had a resize adjustment before, that will have been lost
|
||||
if self.monocle_container.is_none() {
|
||||
self.resize_dimensions.resize(container_count, None);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -253,6 +279,28 @@ impl Workspace {
|
||||
}
|
||||
|
||||
pub fn remove_window(&mut self, window_id: u32) -> eyre::Result<Window> {
|
||||
if let Some(container) = self.monocle_container.as_mut()
|
||||
&& let Some(window_idx) = container
|
||||
.windows()
|
||||
.iter()
|
||||
.position(|window| window.id == window_id)
|
||||
{
|
||||
let window = container
|
||||
.remove_window_by_idx(window_idx)
|
||||
.ok_or_else(|| eyre!("there is no window"))?;
|
||||
|
||||
if container.windows().is_empty() {
|
||||
self.monocle_container = None;
|
||||
self.monocle_container_restore_idx = None;
|
||||
}
|
||||
|
||||
for c in self.containers_mut() {
|
||||
c.restore()?;
|
||||
}
|
||||
|
||||
return Ok(window);
|
||||
}
|
||||
|
||||
let container_idx = self
|
||||
.container_idx_for_window(window_id)
|
||||
.ok_or_else(|| eyre!("there is no window"))?;
|
||||
@@ -309,6 +357,12 @@ impl Workspace {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(container) = &self.monocle_container
|
||||
&& container.contains_window(window_id)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
@@ -382,10 +436,21 @@ impl Workspace {
|
||||
container.hide(omit)?;
|
||||
}
|
||||
|
||||
if let Some(container) = self.monocle_container.as_mut() {
|
||||
container.hide(omit)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn restore(&mut self, mouse_follows_focus: bool) -> eyre::Result<()> {
|
||||
if let Some(container) = self.monocle_container.as_mut() {
|
||||
container.restore()?;
|
||||
if let Some(window) = container.focused_window() {
|
||||
window.focus(mouse_follows_focus)?;
|
||||
}
|
||||
}
|
||||
|
||||
let idx = self.focused_container_idx();
|
||||
let mut to_focus = None;
|
||||
|
||||
@@ -437,4 +502,62 @@ impl Workspace {
|
||||
fn focus_first_container(&mut self) {
|
||||
self.focus_container(0);
|
||||
}
|
||||
|
||||
pub fn new_monocle_container(&mut self) -> eyre::Result<()> {
|
||||
let focused_idx = self.focused_container_idx();
|
||||
|
||||
// we shouldn't use remove_container_by_idx here because it doesn't make sense for
|
||||
// monocle and maximized toggles which take over the whole screen before being reinserted
|
||||
// at the same index to respect locked container indexes
|
||||
let container = self
|
||||
.containers_mut()
|
||||
.remove(focused_idx)
|
||||
.ok_or_else(|| eyre!("there is no container"))?;
|
||||
|
||||
// We don't remove any resize adjustments for a monocle, because when this container is
|
||||
// inevitably reintegrated, it would be weird if it doesn't go back to the dimensions
|
||||
// it had before
|
||||
|
||||
self.monocle_container = Option::from(container);
|
||||
self.monocle_container_restore_idx = Option::from(focused_idx);
|
||||
self.focus_previous_container();
|
||||
|
||||
self.monocle_container
|
||||
.as_mut()
|
||||
.ok_or_else(|| eyre!("there is no monocle container"))?
|
||||
.load_focused_window()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn reintegrate_monocle_container(&mut self) -> eyre::Result<()> {
|
||||
let restore_idx = self
|
||||
.monocle_container_restore_idx
|
||||
.ok_or_else(|| eyre!("there is no monocle restore index"))?;
|
||||
|
||||
let container = self
|
||||
.monocle_container
|
||||
.as_ref()
|
||||
.ok_or_else(|| eyre!("there is no monocle container"))?;
|
||||
|
||||
let container = container.clone();
|
||||
if restore_idx >= self.containers().len() {
|
||||
self.containers_mut()
|
||||
.resize(restore_idx, Container::default());
|
||||
}
|
||||
|
||||
// we shouldn't use insert_container_at_index here because it doesn't make sense for
|
||||
// monocle and maximized toggles which take over the whole screen before being reinserted
|
||||
// at the same index to respect locked container indexes
|
||||
self.containers_mut().insert(restore_idx, container);
|
||||
self.focus_container(restore_idx);
|
||||
self.focused_container_mut()
|
||||
.ok_or_else(|| eyre!("there is no container"))?
|
||||
.load_focused_window()?;
|
||||
|
||||
self.monocle_container = None;
|
||||
self.monocle_container_restore_idx = None;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -103,6 +103,8 @@ enum SubCommand {
|
||||
CycleStack(CycleStack),
|
||||
/// Toggle the paused state for all window tiling
|
||||
TogglePause,
|
||||
/// Toggle monocle mode for the focused container
|
||||
ToggleMonocle,
|
||||
/// Set the layout on the focused workspace
|
||||
#[clap(arg_required_else_help = true)]
|
||||
ChangeLayout(ChangeLayout),
|
||||
@@ -151,6 +153,9 @@ fn main() -> eyre::Result<()> {
|
||||
SubCommand::SendToWorkspace(arg) => {
|
||||
send_message(&SocketMessage::SendContainerToWorkspaceNumber(arg.target))?;
|
||||
}
|
||||
SubCommand::ToggleMonocle => {
|
||||
send_message(&SocketMessage::ToggleMonocle)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in new issue
Block a user