Skip to content

Commit

Permalink
feat(window_management): add window edge snapping setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Koranir authored Feb 14, 2025
1 parent a44071b commit 8f9f287
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
30 changes: 28 additions & 2 deletions cosmic-settings/src/pages/desktop/window_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum Message {
ShowActiveWindowHint(bool),
ShowMaximizeButton(bool),
ShowMinimizeButton(bool),
SetEdgeSnapThreshold(u32),
}

pub struct Page {
Expand All @@ -36,6 +37,7 @@ pub struct Page {
focus_delay_text: String,
cursor_follows_focus: bool,
show_active_hint: bool,
edge_snap_threshold: u32,
}

impl Default for Page {
Expand Down Expand Up @@ -76,6 +78,15 @@ impl Default for Page {
})
.unwrap_or(true);

let edge_snap_threshold = comp_config
.get("edge_snap_threshold")
.inspect_err(|err| {
if !matches!(err, cosmic_config::Error::NoConfigDirectory) {
error!(?err, "Failed to read config 'edge_snap_threshold'")
}
})
.unwrap_or(0);

Page {
super_key_selections: vec![
fl!("super-key", "launcher"),
Expand All @@ -90,6 +101,7 @@ impl Default for Page {
focus_delay_text: format!("{focus_follows_cursor_delay}"),
cursor_follows_focus,
show_active_hint,
edge_snap_threshold,
}
}
}
Expand Down Expand Up @@ -168,6 +180,12 @@ impl Page {
error!(?err, "Failed to set config 'active_hint'");
}
}
Message::SetEdgeSnapThreshold(value) => {
self.edge_snap_threshold = value;
if let Err(err) = self.comp_config.set("edge_snap_threshold", value) {
error!(?err, "Failed to set config 'edge_snap_threshold'");
}
}
}
}
}
Expand All @@ -179,7 +197,7 @@ impl page::Page<crate::pages::Message> for Page {
sections: &mut SlotMap<section::Entity, Section<crate::pages::Message>>,
) -> Option<page::Content> {
Some(vec![
sections.insert(super_key_action()),
sections.insert(window_management()),
sections.insert(window_controls()),
sections.insert(focus_navigation()),
])
Expand All @@ -197,7 +215,7 @@ impl page::Page<crate::pages::Message> for Page {

impl page::AutoBind<crate::pages::Message> for Page {}

pub fn super_key_action() -> Section<crate::pages::Message> {
pub fn window_management() -> Section<crate::pages::Message> {
let mut descriptions = Slab::new();

let super_key = descriptions.insert(fl!("super-key"));
Expand All @@ -206,6 +224,8 @@ pub fn super_key_action() -> Section<crate::pages::Message> {
let _applications = descriptions.insert(fl!("super-key", "applications"));
let _disable = descriptions.insert(fl!("super-key", "disable"));

let edge_gravity = descriptions.insert(fl!("edge-gravity"));

Section::default()
.descriptions(descriptions)
.view::<Page>(move |_binder, page, section| {
Expand All @@ -220,6 +240,12 @@ pub fn super_key_action() -> Section<crate::pages::Message> {
Message::SuperKey,
)),
)
.add(settings::item(
&descriptions[edge_gravity],
toggler(page.edge_snap_threshold != 0).on_toggle(|is_enabled| {
Message::SetEdgeSnapThreshold(if is_enabled { 10 } else { 0 })
}),
))
.apply(Element::from)
.map(crate::pages::Message::WindowManagement)
})
Expand Down
2 changes: 2 additions & 0 deletions i18n/en/cosmic_settings.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ super-key = Super key action
.applications = Open Applications
.disable = Disable
edge-gravity = Floating windows gravitate to nearby edges
window-controls = Window Controls
.maximize = Show maximize button
.minimize = Show minimize button
Expand Down

0 comments on commit 8f9f287

Please sign in to comment.