Skip to content

Commit

Permalink
feat: add shortcut for input source switch
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Feb 26, 2025
1 parent d853267 commit 6b8dc9e
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ lto = "thin"
cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols//", rev = "ed2a481" }
cosmic-client-toolkit = { git = "https://github.com/pop-os/cosmic-protocols//", rev = "ed2a481" }

# [patch.'https://github.com/pop-os/cosmic-settings-daemon']
# cosmic-settings-config = { git = "https://github.com/pop-os/cosmic-settings-daemon//", branch = "input_nobuild" }

# For development and testing purposes
# [patch.'https://github.com/pop-os/libcosmic']
# libcosmic = { git = "https://github.com/pop-os/libcosmic//", branch = "font" }
Expand Down
6 changes: 5 additions & 1 deletion cosmic-settings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ linux = [
# Pages
page-accessibility = ["dep:cosmic-protocols", "dep:sctk"]
page-about = ["dep:cosmic-settings-system", "dep:hostname1-zbus", "dep:zbus"]
page-bluetooth = ["dep:bluez-zbus", "dep:zbus", "dep:cosmic-settings-subscriptions"]
page-bluetooth = [
"dep:bluez-zbus",
"dep:zbus",
"dep:cosmic-settings-subscriptions",
]
page-date = ["dep:timedate-zbus", "dep:zbus"]
page-default-apps = ["dep:mime-apps"]
page-input = [
Expand Down
68 changes: 47 additions & 21 deletions cosmic-settings/src/pages/input/keyboard/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

pub mod shortcuts;

use std::cmp;
Expand Down Expand Up @@ -76,6 +79,7 @@ pub enum SourceContext {
pub type Locale = String;
pub type Variant = String;
pub type Description = String;

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LayoutSource {
Base,
Expand All @@ -89,6 +93,9 @@ const KB_REPEAT_DELAY_MIN: u32 = 200;
const KB_REPEAT_RATE_MAX: u32 = 45;
const KB_REPEAT_RATE_MIN: u32 = 5;

const COSMIC_COMP_CONFIG: &str = "com.system76.CosmicComp";
const COSMIC_COMP_CONFIG_VERSION: u64 = 1;

pub struct Page {
entity: page::Entity,
config: cosmic_config::Config,
Expand All @@ -103,7 +110,8 @@ pub struct Page {

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

Self {
entity: page::Entity::null(),
Expand Down Expand Up @@ -209,7 +217,7 @@ fn popover_menu(id: DefaultKey) -> cosmic::Element<'static, Message> {
color: background.component.divider.into(),
width: 1.0,
radius: cosmic.corner_radii.radius_s.into(),
..Default::default()
..Border::default()
},
shadow: Default::default(),
}
Expand Down Expand Up @@ -609,26 +617,20 @@ impl Page {
}

fn update_xkb_config(&mut self) {
let mut new_layout = String::new();
let mut new_variant = String::new();

for id in &self.active_layouts {
if let Some((locale, variant, _description, _source)) = self.keyboard_layouts.get(*id) {
new_layout.push_str(locale);
new_layout.push(',');
new_variant.push_str(variant);
new_variant.push(',');
}
}

let _excess_comma = new_layout.pop();
let _excess_comma = new_variant.pop();

self.xkb.layout = new_layout;
self.xkb.variant = new_variant;
let result = update_xkb_config(
&self.config,
&mut self.xkb,
&mut self
.active_layouts
.iter()
.filter_map(|id| self.keyboard_layouts.get(*id))
.map(|(locale, variant, _description, _source)| {
(locale.as_str(), variant.as_str())
}),
);

if let Err(err) = self.config.set("xkb_config", &self.xkb) {
tracing::error!(?err, "Failed to set config 'xkb_config'");
if let Err(why) = result {
tracing::error!(?why, "Failed to set config 'xkb_config'");
}
}
}
Expand Down Expand Up @@ -792,3 +794,27 @@ fn keyboard_typing_assist() -> Section<crate::pages::Message> {
.map(crate::pages::Message::Keyboard)
})
}

fn update_xkb_config(
config: &cosmic_config::Config,
xkb: &mut XkbConfig,
active_layouts: &mut dyn Iterator<Item = (&str, &str)>,
) -> Result<(), cosmic_config::Error> {
let mut new_layout = String::new();
let mut new_variant = String::new();

for (locale, variant) in active_layouts {
new_layout.push_str(locale);
new_layout.push(',');
new_variant.push_str(variant);
new_variant.push(',');
}

let _excess_comma = new_layout.pop();
let _excess_comma = new_variant.pop();

xkb.layout = new_layout;
xkb.variant = new_variant;

config.set("xkb_config", xkb)
}
3 changes: 3 additions & 0 deletions cosmic-settings/src/pages/input/keyboard/shortcuts/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

use cosmic::iced::{Alignment, Length};
use cosmic::widget::{self, button, icon, settings, text};
use cosmic::{theme, Apply, Element, Task};
Expand Down
3 changes: 3 additions & 0 deletions cosmic-settings/src/pages/input/keyboard/shortcuts/custom.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
//
use std::str::FromStr;

use super::{ShortcutBinding, ShortcutMessage, ShortcutModel};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

use super::{ShortcutMessage, ShortcutModel};
use cosmic::{Element, Task};
use cosmic_settings_config::shortcuts::action::ResizeDirection;
Expand Down
4 changes: 4 additions & 0 deletions cosmic-settings/src/pages/input/keyboard/shortcuts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

mod common;

pub use common::{Model, ShortcutBinding, ShortcutMessage, ShortcutModel};
Expand Down Expand Up @@ -649,6 +652,7 @@ fn localize_action(action: &Action) -> String {
SystemAction::AppLibrary => fl!("system-shortcut", "app-library"),
SystemAction::BrightnessDown => fl!("system-shortcut", "brightness-down"),
SystemAction::BrightnessUp => fl!("system-shortcut", "brightness-up"),
SystemAction::InputSourceSwitch => fl!("input-source-switch"),
SystemAction::HomeFolder => fl!("system-shortcut", "home-folder"),
SystemAction::KeyboardBrightnessDown => {
fl!("system-shortcut", "keyboard-brightness-down")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
//
use super::{ShortcutMessage, ShortcutModel};
use cosmic::{Element, Task};
use cosmic_settings_config::shortcuts::action::Direction;
Expand Down
3 changes: 3 additions & 0 deletions cosmic-settings/src/pages/input/keyboard/shortcuts/nav.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

use super::{ShortcutMessage, ShortcutModel};
use cosmic::{Element, Task};
use cosmic_settings_config::shortcuts::action::{Direction, FocusDirection};
Expand Down
4 changes: 4 additions & 0 deletions cosmic-settings/src/pages/input/keyboard/shortcuts/system.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

use super::{ShortcutMessage, ShortcutModel};
use cosmic::{Element, Task};
use cosmic_settings_config::shortcuts::action::System as SystemAction;
Expand Down Expand Up @@ -94,6 +97,7 @@ pub const fn actions() -> &'static [Action] {
Action::System(SystemAction::BrightnessUp),
Action::System(SystemAction::KeyboardBrightnessDown),
Action::System(SystemAction::KeyboardBrightnessUp),
Action::System(SystemAction::InputSourceSwitch),
Action::System(SystemAction::Screenshot),
Action::System(SystemAction::Terminal),
Action::System(SystemAction::HomeFolder),
Expand Down
3 changes: 3 additions & 0 deletions cosmic-settings/src/pages/input/keyboard/shortcuts/tiling.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

use super::{ShortcutMessage, ShortcutModel};
use cosmic::{Element, Task};
use cosmic_settings_config::shortcuts::action::Orientation;
Expand Down
3 changes: 3 additions & 0 deletions cosmic-settings/src/pages/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

use crate::app;
use cosmic::{
cosmic_config::{self, ConfigGet, ConfigSet},
Expand Down
3 changes: 3 additions & 0 deletions cosmic-settings/src/pages/input/mouse.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

use cosmic::iced::{Alignment, Length};
use cosmic::widget::{self, row, settings, text};
use cosmic::{Apply, Element};
Expand Down
3 changes: 3 additions & 0 deletions cosmic-settings/src/pages/input/touchpad.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

use cosmic::cosmic_config::ConfigGet;
use cosmic::iced::{Alignment, Length};
use cosmic::widget::{self, row, settings, text};
Expand Down
1 change: 1 addition & 0 deletions i18n/en/cosmic_settings.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ command = Command
custom = Custom
debug = Debug
disabled = Disabled
input-source-switch = Switch keyboard language input source
migrate-workspace-prev = Migrate workspace to previous output
migrate-workspace-next = Migrate workspace to next output
migrate-workspace = Migrate workspace to output { $direction ->
Expand Down

0 comments on commit 6b8dc9e

Please sign in to comment.