Skip to content

Commit c9a605b

Browse files
Nashenas88mmstick
authored andcommitted
feat(keyboard): add numlock configuration
1 parent 7cf3f68 commit c9a605b

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

cosmic-settings/src/pages/input/keyboard/mod.rs

+74-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use cosmic::{
1313
widget::{self, button, container, icon, radio, row, settings, ListColumn},
1414
Apply, Element, Task,
1515
};
16-
use cosmic_comp_config::XkbConfig;
16+
use cosmic_comp_config::{KeyboardConfig, NumlockState, XkbConfig};
1717
use cosmic_settings_page::{self as page, section, Section};
1818
use itertools::Itertools;
1919
use slab::Slab;
@@ -58,13 +58,15 @@ pub enum Message {
5858
ExpandInputSourcePopover(Option<DefaultKey>),
5959
InputSourceSearch(String),
6060
OpenSpecialCharacterContext(SpecialKey),
61+
OpenNumlockContext,
6162
ShowInputSourcesContext,
6263
SourceAdd(DefaultKey),
6364
SourceContext(SourceContext),
6465
SpecialCharacterSelect(Option<&'static str>),
6566
SetRepeatKeysDelay(u32),
6667
SetRepeatKeysRate(u32),
6768
SetShowExtendedInputSources(bool),
69+
SetNumlockState(NumlockState),
6870
}
6971

7072
#[derive(Clone, Debug)]
@@ -102,6 +104,7 @@ pub struct Page {
102104
context: Option<Context>,
103105
input_source_search: String,
104106
xkb: XkbConfig,
107+
keyboard_config: KeyboardConfig,
105108
keyboard_layouts: SlotMap<DefaultKey, (Locale, Variant, Description, LayoutSource)>,
106109
active_layouts: Vec<DefaultKey>,
107110
expanded_source_popover: Option<DefaultKey>,
@@ -120,6 +123,7 @@ impl Default for Page {
120123
keyboard_layouts: SlotMap::new(),
121124
active_layouts: Vec::new(),
122125
xkb: XkbConfig::default(),
126+
keyboard_config: KeyboardConfig::default(),
123127
input_source_search: String::new(),
124128
show_extended_input_sources: false,
125129
config,
@@ -130,6 +134,7 @@ impl Default for Page {
130134
enum Context {
131135
ShowInputSourcesContext,
132136
SpecialCharacter(SpecialKey),
137+
NumlockState,
133138
}
134139

135140
#[derive(Copy, Clone, Debug)]
@@ -279,6 +284,7 @@ impl page::Page<crate::pages::Message> for Page {
279284
sections.insert(special_character_entry()),
280285
sections.insert(keyboard_shortcuts()),
281286
sections.insert(keyboard_typing_assist()),
287+
sections.insert(keyboard_num_lock()),
282288
])
283289
}
284290

@@ -295,13 +301,18 @@ impl page::Page<crate::pages::Message> for Page {
295301
.special_character_key_view(special_key)
296302
.map(crate::pages::Message::Keyboard)
297303
.apply(Some),
304+
Some(Context::NumlockState) => self
305+
.numlock_state_view()
306+
.map(crate::pages::Message::Keyboard)
307+
.apply(Some),
298308

299309
None => None,
300310
}
301311
}
302312

303313
fn on_enter(&mut self) -> Task<crate::pages::Message> {
304314
self.xkb = super::get_config(&self.config, "xkb_config");
315+
self.keyboard_config = super::get_config(&self.config, "keyboard_config");
305316
match (
306317
xkb_data::keyboard_layouts(),
307318
xkb_data::extra_keyboard_layouts(),
@@ -490,6 +501,14 @@ impl Page {
490501
));
491502
}
492503

504+
Message::OpenNumlockContext => {
505+
self.context = Some(Context::NumlockState);
506+
return cosmic::task::message(crate::app::Message::OpenContextDrawer(
507+
self.entity,
508+
fl!("keyboard-numlock-boot", "set").into(),
509+
));
510+
}
511+
493512
Message::SpecialCharacterSelect(id) => {
494513
if let Some(Context::SpecialCharacter(special_key)) = self.context {
495514
let options = self.xkb.options.as_deref().unwrap_or_default();
@@ -518,6 +537,12 @@ impl Page {
518537
Message::SetShowExtendedInputSources(value) => {
519538
self.show_extended_input_sources = value;
520539
}
540+
Message::SetNumlockState(numlock_state) => {
541+
self.keyboard_config.numlock_state = numlock_state;
542+
if let Err(err) = self.config.set("keyboard_config", &self.keyboard_config) {
543+
tracing::error!(?err, "Failed to set config 'keyboard_config'");
544+
}
545+
}
521546
}
522547

523548
Task::none()
@@ -613,6 +638,32 @@ impl Page {
613638
cosmic::widget::container(list).padding(24).into()
614639
}
615640

641+
fn numlock_state_view(&self) -> cosmic::Element<'_, Message> {
642+
let current = self.keyboard_config.numlock_state;
643+
let options = [
644+
(fl!("keyboard-numlock-boot", "off"), NumlockState::BootOff),
645+
(fl!("keyboard-numlock-boot", "on"), NumlockState::BootOn),
646+
(
647+
fl!("keyboard-numlock-boot", "last-boot"),
648+
NumlockState::LastBoot,
649+
),
650+
];
651+
652+
let mut list = cosmic::widget::list_column();
653+
for (desc, state) in options {
654+
list = list.add(settings::item_row(vec![radio(
655+
cosmic::widget::text(desc),
656+
Some(state),
657+
Some(Some(current)),
658+
|_| Message::SetNumlockState(state),
659+
)
660+
.width(Length::Fill)
661+
.into()]));
662+
}
663+
664+
cosmic::widget::container(list).padding(24).into()
665+
}
666+
616667
fn update_xkb_config(&mut self) {
617668
let result = update_xkb_config(
618669
&self.config,
@@ -792,6 +843,28 @@ fn keyboard_typing_assist() -> Section<crate::pages::Message> {
792843
})
793844
}
794845

846+
fn keyboard_num_lock() -> Section<crate::pages::Message> {
847+
let mut descriptions = Slab::new();
848+
849+
let boot_state = descriptions.insert(fl!("keyboard-numlock-boot", "boot-state"));
850+
851+
Section::default()
852+
.title(fl!("keyboard-numlock-boot"))
853+
.descriptions(descriptions)
854+
.view::<Page>(move |_binder, _page, section| {
855+
let descriptions = &section.descriptions;
856+
857+
settings::section()
858+
.title(&section.title)
859+
.add(crate::widget::go_next_item(
860+
&descriptions[boot_state],
861+
Message::OpenNumlockContext,
862+
))
863+
.apply(cosmic::Element::from)
864+
.map(crate::pages::Message::Keyboard)
865+
})
866+
}
867+
795868
fn update_xkb_config(
796869
config: &cosmic_config::Config,
797870
xkb: &mut XkbConfig,

i18n/en/cosmic_settings.ftl

+7
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,13 @@ keyboard-typing-assist = Typing
545545
.repeat-rate = Repeat rate
546546
.repeat-delay = Repeat delay
547547
548+
keyboard-numlock-boot = Numlock
549+
.boot-state = State on boot
550+
.last-boot = Last boot
551+
.on = On
552+
.off = Off
553+
.set = Set numlock boot state
554+
548555
added = Added
549556
type-to-search = Type to search...
550557
show-extended-input-sources = Show extended input sources

0 commit comments

Comments
 (0)