Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add numlock configuration to keyboard page #983

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 74 additions & 1 deletion cosmic-settings/src/pages/input/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use cosmic::{
widget::{self, button, container, icon, radio, row, settings, ListColumn},
Apply, Element, Task,
};
use cosmic_comp_config::XkbConfig;
use cosmic_comp_config::{KeyboardConfig, NumlockState, XkbConfig};
use cosmic_settings_page::{self as page, section, Section};
use itertools::Itertools;
use slab::Slab;
Expand Down Expand Up @@ -58,13 +58,15 @@ pub enum Message {
ExpandInputSourcePopover(Option<DefaultKey>),
InputSourceSearch(String),
OpenSpecialCharacterContext(SpecialKey),
OpenNumlockContext,
ShowInputSourcesContext,
SourceAdd(DefaultKey),
SourceContext(SourceContext),
SpecialCharacterSelect(Option<&'static str>),
SetRepeatKeysDelay(u32),
SetRepeatKeysRate(u32),
SetShowExtendedInputSources(bool),
SetNumlockState(NumlockState),
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -102,6 +104,7 @@ pub struct Page {
context: Option<Context>,
input_source_search: String,
xkb: XkbConfig,
keyboard_config: KeyboardConfig,
keyboard_layouts: SlotMap<DefaultKey, (Locale, Variant, Description, LayoutSource)>,
active_layouts: Vec<DefaultKey>,
expanded_source_popover: Option<DefaultKey>,
Expand All @@ -120,6 +123,7 @@ impl Default for Page {
keyboard_layouts: SlotMap::new(),
active_layouts: Vec::new(),
xkb: XkbConfig::default(),
keyboard_config: KeyboardConfig::default(),
input_source_search: String::new(),
show_extended_input_sources: false,
config,
Expand All @@ -130,6 +134,7 @@ impl Default for Page {
enum Context {
ShowInputSourcesContext,
SpecialCharacter(SpecialKey),
NumlockState,
}

#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -279,6 +284,7 @@ impl page::Page<crate::pages::Message> for Page {
sections.insert(special_character_entry()),
sections.insert(keyboard_shortcuts()),
sections.insert(keyboard_typing_assist()),
sections.insert(keyboard_num_lock()),
])
}

Expand All @@ -295,13 +301,18 @@ impl page::Page<crate::pages::Message> for Page {
.special_character_key_view(special_key)
.map(crate::pages::Message::Keyboard)
.apply(Some),
Some(Context::NumlockState) => self
.numlock_state_view()
.map(crate::pages::Message::Keyboard)
.apply(Some),

None => None,
}
}

fn on_enter(&mut self) -> Task<crate::pages::Message> {
self.xkb = super::get_config(&self.config, "xkb_config");
self.keyboard_config = super::get_config(&self.config, "keyboard_config");
match (
xkb_data::keyboard_layouts(),
xkb_data::extra_keyboard_layouts(),
Expand Down Expand Up @@ -490,6 +501,14 @@ impl Page {
));
}

Message::OpenNumlockContext => {
self.context = Some(Context::NumlockState);
return cosmic::task::message(crate::app::Message::OpenContextDrawer(
self.entity,
fl!("keyboard-numlock-boot", "set").into(),
));
}

Message::SpecialCharacterSelect(id) => {
if let Some(Context::SpecialCharacter(special_key)) = self.context {
let options = self.xkb.options.as_deref().unwrap_or_default();
Expand Down Expand Up @@ -518,6 +537,12 @@ impl Page {
Message::SetShowExtendedInputSources(value) => {
self.show_extended_input_sources = value;
}
Message::SetNumlockState(numlock_state) => {
self.keyboard_config.numlock_state = numlock_state;
if let Err(err) = self.config.set("keyboard_config", &self.keyboard_config) {
tracing::error!(?err, "Failed to set config 'keyboard_config'");
}
}
}

Task::none()
Expand Down Expand Up @@ -613,6 +638,32 @@ impl Page {
cosmic::widget::container(list).padding(24).into()
}

fn numlock_state_view(&self) -> cosmic::Element<'_, Message> {
let current = self.keyboard_config.numlock_state;
let options = [
(fl!("keyboard-numlock-boot", "off"), NumlockState::BootOff),
(fl!("keyboard-numlock-boot", "on"), NumlockState::BootOn),
(
fl!("keyboard-numlock-boot", "last-boot"),
NumlockState::LastBoot,
),
];

let mut list = cosmic::widget::list_column();
for (desc, state) in options {
list = list.add(settings::item_row(vec![radio(
cosmic::widget::text(desc),
Some(state),
Some(Some(current)),
|_| Message::SetNumlockState(state),
)
.width(Length::Fill)
.into()]));
}

cosmic::widget::container(list).padding(24).into()
}

fn update_xkb_config(&mut self) {
let result = update_xkb_config(
&self.config,
Expand Down Expand Up @@ -792,6 +843,28 @@ fn keyboard_typing_assist() -> Section<crate::pages::Message> {
})
}

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

let boot_state = descriptions.insert(fl!("keyboard-numlock-boot", "boot-state"));

Section::default()
.title(fl!("keyboard-numlock-boot"))
.descriptions(descriptions)
.view::<Page>(move |_binder, _page, section| {
let descriptions = &section.descriptions;

settings::section()
.title(&section.title)
.add(crate::widget::go_next_item(
&descriptions[boot_state],
Message::OpenNumlockContext,
))
.apply(cosmic::Element::from)
.map(crate::pages::Message::Keyboard)
})
}

fn update_xkb_config(
config: &cosmic_config::Config,
xkb: &mut XkbConfig,
Expand Down
7 changes: 7 additions & 0 deletions i18n/en/cosmic_settings.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,13 @@ keyboard-typing-assist = Typing
.repeat-rate = Repeat rate
.repeat-delay = Repeat delay

keyboard-numlock-boot = Numlock
.boot-state = State on boot
.last-boot = Last boot
.on = On
.off = Off
.set = Set numlock boot state

added = Added
type-to-search = Type to search...
show-extended-input-sources = Show extended input sources
Expand Down