feat(window_management): add focus_navigation

This commit is contained in:
Vukašin Vojinović 2024-09-05 14:22:05 +02:00 committed by GitHub
parent 544da2bdc8
commit 090efbfad7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 88 additions and 1 deletions

View file

@ -12,19 +12,43 @@ use cosmic_settings_page::Section;
use cosmic_settings_page::{self as page, section};
use slab::Slab;
use slotmap::SlotMap;
use tracing::error;
#[derive(Copy, Clone, Debug)]
pub enum Message {
SuperKey(usize),
SetFocusFollowsCursor(bool),
SetCursorFollowsFocus(bool),
}
pub struct Page {
pub super_key_selections: Vec<String>,
pub super_key_active: Option<usize>,
comp_config: cosmic_config::Config,
focus_follows_cursor: bool,
cursor_follows_focus: bool,
}
impl Default for Page {
fn default() -> Self {
let comp_config = cosmic_config::Config::new("com.system76.CosmicComp", 1).unwrap();
let focus_follows_cursor = comp_config
.get("focus_follows_cursor")
.unwrap_or_else(|err| {
if !matches!(err, cosmic_config::Error::NoConfigDirectory) {
error!(?err, "Failed to read config 'focus_follows_cursor'");
}
false
});
let cursor_follows_focus = comp_config
.get("cursor_follows_focus")
.unwrap_or_else(|err| {
if !matches!(err, cosmic_config::Error::NoConfigDirectory) {
error!(?err, "Failed to read config 'cursor_follows_focus'");
}
false
});
Page {
super_key_selections: vec![
fl!("super-key", "launcher"),
@ -32,6 +56,9 @@ impl Default for Page {
fl!("super-key", "applications"),
],
super_key_active: super_key_active_config(),
comp_config,
focus_follows_cursor,
cursor_follows_focus,
}
}
}
@ -50,6 +77,24 @@ impl Page {
self.super_key_active = Some(id);
super_key_set(action);
}
Message::SetFocusFollowsCursor(value) => {
self.focus_follows_cursor = value;
if let Err(err) = self
.comp_config
.set("focus_follows_cursor", &self.focus_follows_cursor)
{
error!(?err, "Failed to set config 'focus_follows_cursor'");
}
}
Message::SetCursorFollowsFocus(value) => {
self.cursor_follows_focus = value;
if let Err(err) = self
.comp_config
.set("cursor_follows_focus", &self.cursor_follows_focus)
{
error!(?err, "Failed to set config 'cursor_follows_focus'");
}
}
}
}
}
@ -63,6 +108,7 @@ impl page::Page<crate::pages::Message> for Page {
Some(vec![
sections.insert(super_key_action()),
sections.insert(window_controls()),
sections.insert(focus_navigation()),
])
}
@ -141,6 +187,40 @@ pub fn window_controls() -> Section<crate::pages::Message> {
})
}
pub fn focus_navigation() -> Section<crate::pages::Message> {
let mut descriptions = Slab::new();
let focus_follows_cursor = descriptions.insert(fl!("focus-navigation", "focus-follows-cursor"));
let cursor_follows_focus = descriptions.insert(fl!("focus-navigation", "cursor-follows-focus"));
Section::default()
.title(fl!("focus-navigation"))
.descriptions(descriptions)
.view::<Page>(move |_binder, page, section| {
let descriptions = &section.descriptions;
settings::view_section(&section.title)
.add(settings::item(
&descriptions[focus_follows_cursor],
toggler(
None,
page.focus_follows_cursor,
Message::SetFocusFollowsCursor,
),
))
.add(settings::item(
&descriptions[cursor_follows_focus],
toggler(
None,
page.cursor_follows_focus,
Message::SetCursorFollowsFocus,
),
))
.apply(Element::from)
.map(crate::pages::Message::WindowManagement)
})
}
fn super_key_active_config() -> Option<usize> {
let super_binding = Binding::new(shortcuts::Modifiers::new().logo(), None);

View file

@ -1164,6 +1164,7 @@ pub fn legacy_applications() -> Section<crate::pages::Message> {
Some(page.comp_config_descale_xwayland),
Message::SetXwaylandDescaling,
)
.width(Length::Fill)
.into()]))
.add(widget::settings::item_row(vec![widget::radio(
widget::column()
@ -1173,6 +1174,7 @@ pub fn legacy_applications() -> Section<crate::pages::Message> {
Some(page.comp_config_descale_xwayland),
Message::SetXwaylandDescaling,
)
.width(Length::Fill)
.into()]))
.apply(Element::from)
.map(crate::pages::Message::Displays)

View file

@ -162,7 +162,7 @@ fn popover_menu(id: DefaultKey) -> cosmic::Element<'static, Message> {
SourceContext::MoveDown,
)
.into(),
cosmic::widget::divider::horizontal::light().into(),
cosmic::widget::divider::horizontal::default().into(),
popover_menu_row(
id,
fl!("keyboard-sources", "settings"),
@ -177,6 +177,7 @@ fn popover_menu(id: DefaultKey) -> cosmic::Element<'static, Message> {
.into(),
popover_menu_row(id, fl!("keyboard-sources", "remove"), SourceContext::Remove).into(),
])
.padding(8)
.width(Length::Shrink)
.height(Length::Shrink)
.apply(cosmic::widget::container)

View file

@ -182,6 +182,10 @@ window-controls = Window Controls
.maximize = Show maximize button
.minimize = Show minimize button
focus-navigation = Focus Navigation
.focus-follows-cursor = Focus follows cursor
.cursor-follows-focus = Cursor follows focus
## Desktop: Workspaces
workspaces = Workspaces