@@ -13,7 +13,7 @@ use cosmic::{
13
13
widget:: { self , button, container, icon, radio, row, settings, ListColumn } ,
14
14
Apply , Element , Task ,
15
15
} ;
16
- use cosmic_comp_config:: XkbConfig ;
16
+ use cosmic_comp_config:: { KeyboardConfig , NumlockState , XkbConfig } ;
17
17
use cosmic_settings_page:: { self as page, section, Section } ;
18
18
use itertools:: Itertools ;
19
19
use slab:: Slab ;
@@ -58,13 +58,15 @@ pub enum Message {
58
58
ExpandInputSourcePopover ( Option < DefaultKey > ) ,
59
59
InputSourceSearch ( String ) ,
60
60
OpenSpecialCharacterContext ( SpecialKey ) ,
61
+ OpenNumlockContext ,
61
62
ShowInputSourcesContext ,
62
63
SourceAdd ( DefaultKey ) ,
63
64
SourceContext ( SourceContext ) ,
64
65
SpecialCharacterSelect ( Option < & ' static str > ) ,
65
66
SetRepeatKeysDelay ( u32 ) ,
66
67
SetRepeatKeysRate ( u32 ) ,
67
68
SetShowExtendedInputSources ( bool ) ,
69
+ SetNumlockState ( NumlockState ) ,
68
70
}
69
71
70
72
#[ derive( Clone , Debug ) ]
@@ -102,6 +104,7 @@ pub struct Page {
102
104
context : Option < Context > ,
103
105
input_source_search : String ,
104
106
xkb : XkbConfig ,
107
+ keyboard_config : KeyboardConfig ,
105
108
keyboard_layouts : SlotMap < DefaultKey , ( Locale , Variant , Description , LayoutSource ) > ,
106
109
active_layouts : Vec < DefaultKey > ,
107
110
expanded_source_popover : Option < DefaultKey > ,
@@ -120,6 +123,7 @@ impl Default for Page {
120
123
keyboard_layouts : SlotMap :: new ( ) ,
121
124
active_layouts : Vec :: new ( ) ,
122
125
xkb : XkbConfig :: default ( ) ,
126
+ keyboard_config : KeyboardConfig :: default ( ) ,
123
127
input_source_search : String :: new ( ) ,
124
128
show_extended_input_sources : false ,
125
129
config,
@@ -130,6 +134,7 @@ impl Default for Page {
130
134
enum Context {
131
135
ShowInputSourcesContext ,
132
136
SpecialCharacter ( SpecialKey ) ,
137
+ NumlockState ,
133
138
}
134
139
135
140
#[ derive( Copy , Clone , Debug ) ]
@@ -279,6 +284,7 @@ impl page::Page<crate::pages::Message> for Page {
279
284
sections. insert( special_character_entry( ) ) ,
280
285
sections. insert( keyboard_shortcuts( ) ) ,
281
286
sections. insert( keyboard_typing_assist( ) ) ,
287
+ sections. insert( keyboard_num_lock( ) ) ,
282
288
] )
283
289
}
284
290
@@ -295,13 +301,18 @@ impl page::Page<crate::pages::Message> for Page {
295
301
. special_character_key_view ( special_key)
296
302
. map ( crate :: pages:: Message :: Keyboard )
297
303
. apply ( Some ) ,
304
+ Some ( Context :: NumlockState ) => self
305
+ . numlock_state_view ( )
306
+ . map ( crate :: pages:: Message :: Keyboard )
307
+ . apply ( Some ) ,
298
308
299
309
None => None ,
300
310
}
301
311
}
302
312
303
313
fn on_enter ( & mut self ) -> Task < crate :: pages:: Message > {
304
314
self . xkb = super :: get_config ( & self . config , "xkb_config" ) ;
315
+ self . keyboard_config = super :: get_config ( & self . config , "keyboard_config" ) ;
305
316
match (
306
317
xkb_data:: keyboard_layouts ( ) ,
307
318
xkb_data:: extra_keyboard_layouts ( ) ,
@@ -490,6 +501,14 @@ impl Page {
490
501
) ) ;
491
502
}
492
503
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
+
493
512
Message :: SpecialCharacterSelect ( id) => {
494
513
if let Some ( Context :: SpecialCharacter ( special_key) ) = self . context {
495
514
let options = self . xkb . options . as_deref ( ) . unwrap_or_default ( ) ;
@@ -518,6 +537,12 @@ impl Page {
518
537
Message :: SetShowExtendedInputSources ( value) => {
519
538
self . show_extended_input_sources = value;
520
539
}
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
+ }
521
546
}
522
547
523
548
Task :: none ( )
@@ -613,6 +638,32 @@ impl Page {
613
638
cosmic:: widget:: container ( list) . padding ( 24 ) . into ( )
614
639
}
615
640
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
+
616
667
fn update_xkb_config ( & mut self ) {
617
668
let result = update_xkb_config (
618
669
& self . config ,
@@ -792,6 +843,28 @@ fn keyboard_typing_assist() -> Section<crate::pages::Message> {
792
843
} )
793
844
}
794
845
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
+
795
868
fn update_xkb_config (
796
869
config : & cosmic_config:: Config ,
797
870
xkb : & mut XkbConfig ,
0 commit comments