-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize code struct optimize code struct optimize code optimize code optimize code optimize code optimize code
- Loading branch information
1 parent
bf1f9ec
commit 8387539
Showing
2 changed files
with
68 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,61 @@ | ||
use anyhow::Context; | ||
|
||
use crate::{ | ||
common::{LengthValue, Rect}, | ||
common::Rect, | ||
containers::{ | ||
traits::{CommonGetters, PositionGetters}, | ||
WindowContainer, | ||
}, | ||
windows::{ | ||
traits::WindowGetters, NonTilingWindow, WindowState, | ||
traits::WindowGetters, WindowState, | ||
}, | ||
wm_state::WmState, | ||
}; | ||
|
||
pub fn set_window_position( | ||
window: WindowContainer, | ||
centered: bool, | ||
target_x_pos: Option<LengthValue>, | ||
target_y_pos: Option<LengthValue>, | ||
target_x_pos: Option<i32>, | ||
target_y_pos: Option<i32>, | ||
state: &mut WmState, | ||
) -> anyhow::Result<()> { | ||
match window { | ||
WindowContainer::TilingWindow(_) => { | ||
return Ok(()); | ||
} | ||
WindowContainer::NonTilingWindow(window) => { | ||
if matches!(window.state(), WindowState::Floating(_)) { | ||
set_floating_window_position( | ||
window, | ||
centered, | ||
target_x_pos, | ||
target_y_pos, | ||
state, | ||
)?; | ||
} | ||
} | ||
if matches!(window.state(), WindowState::Floating(_)) { | ||
let window_rect = window.floating_placement(); | ||
let new_x_pos = target_x_pos.unwrap_or(window_rect.x()); | ||
let new_y_pos = target_y_pos.unwrap_or(window_rect.y()); | ||
window.set_floating_placement(Rect::from_xy( | ||
new_x_pos, | ||
new_y_pos, | ||
window.floating_placement().width(), | ||
window.floating_placement().height(), | ||
)); | ||
|
||
state | ||
.pending_sync | ||
.containers_to_redraw | ||
.push(window.clone().into()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn set_floating_window_position( | ||
window: NonTilingWindow, | ||
centered: bool, | ||
target_x_pos: Option<LengthValue>, | ||
target_y_pos: Option<LengthValue>, | ||
pub fn set_window_position_to_center( | ||
window: WindowContainer, | ||
state: &mut WmState, | ||
) -> anyhow::Result<()> { | ||
let window_rect = window.floating_placement(); | ||
|
||
match centered { | ||
true => { | ||
if matches!(window.state(), WindowState::Floating(_)) { | ||
let window_rect = window.floating_placement(); | ||
let workspace = | ||
window.workspace().context("no workspace find.")?; | ||
window.set_floating_placement( | ||
window_rect | ||
.translate_to_center(&workspace.to_rect()?), | ||
) | ||
}, | ||
false => { | ||
let monitor = window.monitor().context("No monitor")?; | ||
let monitor_rect = monitor.to_rect()?; | ||
|
||
let target_x_pos_px = target_x_pos | ||
.map(|target_x_pos| target_x_pos.to_px(monitor_rect.x(), None)); | ||
let target_y_pos_px = target_y_pos | ||
.map(|target_y_pos| target_y_pos.to_px(monitor_rect.y(), None)); | ||
|
||
let new_x_pos = target_x_pos_px.unwrap_or(window_rect.x()); | ||
let new_y_pos = target_y_pos_px.unwrap_or(window_rect.y()); | ||
|
||
window.set_floating_placement(Rect::from_xy( | ||
new_x_pos, | ||
new_y_pos, | ||
window.floating_placement().width(), | ||
window.floating_placement().height(), | ||
)); | ||
} | ||
} | ||
); | ||
|
||
state | ||
state | ||
.pending_sync | ||
.containers_to_redraw | ||
.push(window.clone().into()); | ||
.push(window.clone().into()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |