Skip to content

Commit

Permalink
optimize code struct
Browse files Browse the repository at this point in the history
optimize code struct

optimize code struct

optimize code

optimize code

optimize code

optimize code

optimize code
  • Loading branch information
DreamMaoMao committed Nov 7, 2024
1 parent bf1f9ec commit 8387539
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 80 deletions.
66 changes: 40 additions & 26 deletions packages/wm/src/app_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use crate::{
user_config::{FloatingStateConfig, FullscreenStateConfig, UserConfig},
windows::{
commands::{
ignore_window, move_window_in_direction, move_window_to_workspace, resize_window, set_window_position, set_window_size, update_window_state
ignore_window, move_window_in_direction, move_window_to_workspace,
resize_window, set_window_position, set_window_position_to_center,
set_window_size, update_window_state,
},
traits::WindowGetters,
WindowState,
Expand Down Expand Up @@ -187,8 +189,8 @@ pub enum InvokeCommand {
#[clap(long)]
direction: Direction,
},
Resize(InvokeResizeCommand),
Position(InvokePositionCommand),
Resize(InvokeResizeCommand),
SetFloating {
#[clap(long, default_missing_value = "true", require_equals = true, num_args = 0..=1)]
shown_on_top: Option<bool>,
Expand All @@ -197,10 +199,10 @@ pub enum InvokeCommand {
centered: Option<bool>,

#[clap(long, allow_hyphen_values = true)]
x_pos: Option<LengthValue>,
x_pos: Option<i32>,

#[clap(long, allow_hyphen_values = true)]
y_pos: Option<LengthValue>,
y_pos: Option<i32>,

#[clap(long, allow_hyphen_values = true)]
width: Option<LengthValue>,
Expand Down Expand Up @@ -439,24 +441,31 @@ impl InvokeCommand {
config,
)
}
InvokeCommand::Resize(args) => {
InvokeCommand::Position(args) => {
match subject_container.as_window_container() {
Ok(window) => resize_window(
window,
args.width.clone(),
args.height.clone(),
state,
),
Ok(window) => {
match args.centered {
true => set_window_position_to_center(window, state),
false => {
set_window_position(
window,
args.x_pos.clone(),
args.y_pos.clone(),
state,
)
}
}

},
_ => Ok(()),
}
}
InvokeCommand::Position(args) => {
InvokeCommand::Resize(args) => {
match subject_container.as_window_container() {
Ok(window) => set_window_position(
Ok(window) => resize_window(
window,
args.centered,
args.x_pos.clone(),
args.y_pos.clone(),
args.width.clone(),
args.height.clone(),
state,
),
_ => Ok(()),
Expand Down Expand Up @@ -488,18 +497,23 @@ impl InvokeCommand {
)?;

if width.is_some() || height.is_some() {
set_window_size(window.clone(),
width.clone(),
height.clone(),
state)?;
set_window_size(
window.clone(),
width.clone(),
height.clone(),
state,
)?;
}

if is_centered || x_pos.is_some() || y_pos.is_some() {
if is_centered {
set_window_position_to_center(window, state)?;
} else if x_pos.is_some() || y_pos.is_some() {
set_window_position(
window.clone(),
is_centered,
x_pos.clone(),
y_pos.clone(), state)?;
x_pos.clone(),
y_pos.clone(),
state,
)?;
}

Ok(())
Expand Down Expand Up @@ -854,8 +868,8 @@ pub struct InvokePositionCommand {
centered: bool,

#[clap(long, allow_hyphen_values = true)]
x_pos: Option<LengthValue>,
x_pos: Option<i32>,

#[clap(long, allow_hyphen_values = true)]
y_pos: Option<LengthValue>,
y_pos: Option<i32>,
}
82 changes: 28 additions & 54 deletions packages/wm/src/windows/commands/set_window_position.rs
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(())
}
}

0 comments on commit 8387539

Please sign in to comment.