Skip to content

Commit 661b487

Browse files
committed
feat: add shortcut for input source switch
1 parent d853267 commit 661b487

16 files changed

+93
-24
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ lto = "thin"
5252
cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols//", rev = "ed2a481" }
5353
cosmic-client-toolkit = { git = "https://github.com/pop-os/cosmic-protocols//", rev = "ed2a481" }
5454

55+
# [patch.'https://github.com/pop-os/cosmic-settings-daemon']
56+
# cosmic-settings-config = { git = "https://github.com/pop-os/cosmic-settings-daemon//", branch = "input_nobuild" }
57+
5558
# For development and testing purposes
5659
# [patch.'https://github.com/pop-os/libcosmic']
5760
# libcosmic = { git = "https://github.com/pop-os/libcosmic//", branch = "font" }

cosmic-settings/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ linux = [
132132
# Pages
133133
page-accessibility = ["dep:cosmic-protocols", "dep:sctk"]
134134
page-about = ["dep:cosmic-settings-system", "dep:hostname1-zbus", "dep:zbus"]
135-
page-bluetooth = ["dep:bluez-zbus", "dep:zbus", "dep:cosmic-settings-subscriptions"]
135+
page-bluetooth = [
136+
"dep:bluez-zbus",
137+
"dep:zbus",
138+
"dep:cosmic-settings-subscriptions",
139+
]
136140
page-date = ["dep:timedate-zbus", "dep:zbus"]
137141
page-default-apps = ["dep:mime-apps"]
138142
page-input = [

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

+47-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2023 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
14
pub mod shortcuts;
25

36
use std::cmp;
@@ -76,6 +79,7 @@ pub enum SourceContext {
7679
pub type Locale = String;
7780
pub type Variant = String;
7881
pub type Description = String;
82+
7983
#[derive(Clone, Debug, PartialEq, Eq)]
8084
pub enum LayoutSource {
8185
Base,
@@ -89,6 +93,9 @@ const KB_REPEAT_DELAY_MIN: u32 = 200;
8993
const KB_REPEAT_RATE_MAX: u32 = 45;
9094
const KB_REPEAT_RATE_MIN: u32 = 5;
9195

96+
const COSMIC_COMP_CONFIG: &str = "com.system76.CosmicComp";
97+
const COSMIC_COMP_CONFIG_VERSION: u64 = 1;
98+
9299
pub struct Page {
93100
entity: page::Entity,
94101
config: cosmic_config::Config,
@@ -103,7 +110,8 @@ pub struct Page {
103110

104111
impl Default for Page {
105112
fn default() -> Self {
106-
let config = cosmic_config::Config::new("com.system76.CosmicComp", 1).unwrap();
113+
let config =
114+
cosmic_config::Config::new(COSMIC_COMP_CONFIG, COSMIC_COMP_CONFIG_VERSION).unwrap();
107115

108116
Self {
109117
entity: page::Entity::null(),
@@ -209,7 +217,7 @@ fn popover_menu(id: DefaultKey) -> cosmic::Element<'static, Message> {
209217
color: background.component.divider.into(),
210218
width: 1.0,
211219
radius: cosmic.corner_radii.radius_s.into(),
212-
..Default::default()
220+
..Border::default()
213221
},
214222
shadow: Default::default(),
215223
}
@@ -609,26 +617,20 @@ impl Page {
609617
}
610618

611619
fn update_xkb_config(&mut self) {
612-
let mut new_layout = String::new();
613-
let mut new_variant = String::new();
614-
615-
for id in &self.active_layouts {
616-
if let Some((locale, variant, _description, _source)) = self.keyboard_layouts.get(*id) {
617-
new_layout.push_str(locale);
618-
new_layout.push(',');
619-
new_variant.push_str(variant);
620-
new_variant.push(',');
621-
}
622-
}
623-
624-
let _excess_comma = new_layout.pop();
625-
let _excess_comma = new_variant.pop();
626-
627-
self.xkb.layout = new_layout;
628-
self.xkb.variant = new_variant;
620+
let result = update_xkb_config(
621+
&self.config,
622+
&mut self.xkb,
623+
&mut self
624+
.active_layouts
625+
.iter()
626+
.filter_map(|id| self.keyboard_layouts.get(*id))
627+
.map(|(locale, variant, _description, _source)| {
628+
(locale.as_str(), variant.as_str())
629+
}),
630+
);
629631

630-
if let Err(err) = self.config.set("xkb_config", &self.xkb) {
631-
tracing::error!(?err, "Failed to set config 'xkb_config'");
632+
if let Err(why) = result {
633+
tracing::error!(?why, "Failed to set config 'xkb_config'");
632634
}
633635
}
634636
}
@@ -792,3 +794,27 @@ fn keyboard_typing_assist() -> Section<crate::pages::Message> {
792794
.map(crate::pages::Message::Keyboard)
793795
})
794796
}
797+
798+
fn update_xkb_config(
799+
config: &cosmic_config::Config,
800+
xkb: &mut XkbConfig,
801+
active_layouts: &mut dyn Iterator<Item = (&str, &str)>,
802+
) -> Result<(), cosmic_config::Error> {
803+
let mut new_layout = String::new();
804+
let mut new_variant = String::new();
805+
806+
for (locale, variant) in active_layouts {
807+
new_layout.push_str(locale);
808+
new_layout.push(',');
809+
new_variant.push_str(variant);
810+
new_variant.push(',');
811+
}
812+
813+
let _excess_comma = new_layout.pop();
814+
let _excess_comma = new_variant.pop();
815+
816+
xkb.layout = new_layout;
817+
xkb.variant = new_variant;
818+
819+
config.set("xkb_config", xkb)
820+
}

cosmic-settings/src/pages/input/keyboard/shortcuts/common.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
14
use cosmic::iced::{Alignment, Length};
25
use cosmic::widget::{self, button, icon, settings, text};
36
use cosmic::{theme, Apply, Element, Task};

cosmic-settings/src/pages/input/keyboard/shortcuts/custom.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
//
14
use std::str::FromStr;
25

36
use super::{ShortcutBinding, ShortcutMessage, ShortcutModel};

cosmic-settings/src/pages/input/keyboard/shortcuts/manage_windows.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
14
use super::{ShortcutMessage, ShortcutModel};
25
use cosmic::{Element, Task};
36
use cosmic_settings_config::shortcuts::action::ResizeDirection;

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

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
14
mod common;
25

36
pub use common::{Model, ShortcutBinding, ShortcutMessage, ShortcutModel};
@@ -649,6 +652,7 @@ fn localize_action(action: &Action) -> String {
649652
SystemAction::AppLibrary => fl!("system-shortcut", "app-library"),
650653
SystemAction::BrightnessDown => fl!("system-shortcut", "brightness-down"),
651654
SystemAction::BrightnessUp => fl!("system-shortcut", "brightness-up"),
655+
SystemAction::InputSourceSwitch => fl!("input-source-switch"),
652656
SystemAction::HomeFolder => fl!("system-shortcut", "home-folder"),
653657
SystemAction::KeyboardBrightnessDown => {
654658
fl!("system-shortcut", "keyboard-brightness-down")

cosmic-settings/src/pages/input/keyboard/shortcuts/move_window.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
//
14
use super::{ShortcutMessage, ShortcutModel};
25
use cosmic::{Element, Task};
36
use cosmic_settings_config::shortcuts::action::Direction;

cosmic-settings/src/pages/input/keyboard/shortcuts/nav.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
14
use super::{ShortcutMessage, ShortcutModel};
25
use cosmic::{Element, Task};
36
use cosmic_settings_config::shortcuts::action::{Direction, FocusDirection};

cosmic-settings/src/pages/input/keyboard/shortcuts/system.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
14
use super::{ShortcutMessage, ShortcutModel};
25
use cosmic::{Element, Task};
36
use cosmic_settings_config::shortcuts::action::System as SystemAction;
@@ -94,6 +97,7 @@ pub const fn actions() -> &'static [Action] {
9497
Action::System(SystemAction::BrightnessUp),
9598
Action::System(SystemAction::KeyboardBrightnessDown),
9699
Action::System(SystemAction::KeyboardBrightnessUp),
100+
Action::System(SystemAction::InputSourceSwitch),
97101
Action::System(SystemAction::Screenshot),
98102
Action::System(SystemAction::Terminal),
99103
Action::System(SystemAction::HomeFolder),

cosmic-settings/src/pages/input/keyboard/shortcuts/tiling.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2024 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
14
use super::{ShortcutMessage, ShortcutModel};
25
use cosmic::{Element, Task};
36
use cosmic_settings_config::shortcuts::action::Orientation;

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2023 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
14
use crate::app;
25
use cosmic::{
36
cosmic_config::{self, ConfigGet, ConfigSet},

cosmic-settings/src/pages/input/mouse.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2023 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
14
use cosmic::iced::{Alignment, Length};
25
use cosmic::widget::{self, row, settings, text};
36
use cosmic::{Apply, Element};

cosmic-settings/src/pages/input/touchpad.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2023 System76 <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only
3+
14
use cosmic::cosmic_config::ConfigGet;
25
use cosmic::iced::{Alignment, Length};
36
use cosmic::widget::{self, row, settings, text};

i18n/en/cosmic_settings.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ command = Command
560560
custom = Custom
561561
debug = Debug
562562
disabled = Disabled
563+
input-source-switch = Switch keyboard language input source
563564
migrate-workspace-prev = Migrate workspace to previous output
564565
migrate-workspace-next = Migrate workspace to next output
565566
migrate-workspace = Migrate workspace to output { $direction ->

0 commit comments

Comments
 (0)