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 translation layer #588

Merged
merged 4 commits into from
Nov 20, 2022
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
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* Fix duplicated commit string in some firefox sites [#562](https://github.com/Riey/kime/issues/562).
* Delaying preedit, bypass processes in gtk module [#570](https://github.com/Riey/kime/issues/570)
* Fix typo in symbol name for U+2193(↓): downaroow -> downarrow
* Adding translation layer feature [#586](https://github.com/Riey/kime/issues/586)

## 2.5.6

Expand Down
3 changes: 3 additions & 0 deletions src/engine/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use maplit::btreemap;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::PathBuf;

pub use kime_engine_backend::{Key, KeyCode, ModifierState};
pub use kime_engine_backend_hangul::{HangulConfig, HangulData};
Expand Down Expand Up @@ -137,6 +138,7 @@ impl Default for LogConfig {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct EngineConfig {
pub translation_layer: Option<PathBuf>,
pub default_category: InputCategory,
pub global_category_state: bool,
pub global_hotkeys: BTreeMap<Key, Hotkey>,
Expand All @@ -151,6 +153,7 @@ pub struct EngineConfig {
impl Default for EngineConfig {
fn default() -> Self {
Self {
translation_layer: None,
latin: LatinConfig::default(),
hangul: HangulConfig::default(),
default_category: InputCategory::Latin,
Expand Down
16 changes: 16 additions & 0 deletions src/engine/core/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::KeyMap;
use fontconfig_parser::FontConfig;
use fontdb::{Family, Query};
pub use kime_engine_config::*;
use std::fs;

/// Preprocessed engine config
pub struct Config {
pub translation_layer: Option<KeyMap<Key>>,
pub default_category: InputCategory,
pub global_category_state: bool,
pub category_hotkeys: EnumMap<InputCategory, Vec<(Key, Hotkey)>>,
Expand Down Expand Up @@ -42,7 +45,20 @@ impl Config {
.unwrap_or_default()
};

let translation_layer: Option<KeyMap<Key>> = engine
.translation_layer
.and_then(|f| {
xdg::BaseDirectories::with_prefix("kime")
.ok()
.and_then(|d| d.find_config_file(f))
})
.as_ref()
.and_then(|f| fs::read_to_string(f.as_path()).ok())
.as_ref()
.and_then(|content| serde_yaml::from_str(content).ok());

Self {
translation_layer: translation_layer,
default_category: engine.default_category,
global_category_state: engine.global_category_state,
category_hotkeys: enum_map! {
Expand Down
8 changes: 7 additions & 1 deletion src/engine/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,13 @@ impl EngineImpl {
impl InputEngineBackend for EngineImpl {
type ConfigData = Config;

fn press_key(&mut self, config: &Config, key: Key, commit_buf: &mut String) -> bool {
fn press_key(&mut self, config: &Config, raw_key: Key, commit_buf: &mut String) -> bool {
let key = config
.translation_layer
.as_ref()
.and_then(|tl| tl.get(raw_key))
.unwrap_or(raw_key);

match self.mode {
Some(InputMode::Emoji) => {
do_mode!(@retarm self, emoji_mode, press_key(&config.latin_data, key, commit_buf,))
Expand Down
32 changes: 28 additions & 4 deletions src/tools/check/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use kime_engine_core::{Key, KeyMap};

use ansi_term::Color;
use kime_engine_cffi::{
Config, InputCategory, InputEngine, InputResult_CONSUMED, InputResult_HAS_COMMIT,
Expand Down Expand Up @@ -117,20 +119,42 @@ impl Check {
Some(path) => path,
_ => {
return CondResult::Ignore(
"User config not exists will use default config".into(),
"User config does not exists will use default config".into(),
)
}
};

println!("Loading config path: {}", config_path.display());

let _config: kime_engine_core::RawConfig = match serde_yaml::from_str(
let config: kime_engine_core::RawConfig = match serde_yaml::from_str(
&std::fs::read_to_string(config_path).expect("Read config file"),
) {
Ok(config) => config,
Err(err) => {
return CondResult::Fail(format!("Can't parse config.yaml: {}", err))
Err(err) => return CondResult::Fail(format!("Can't parse config.yaml: {err}")),
};

match config.engine.translation_layer {
Some(ref raw_path) => {
let path = match dirs.find_config_file(raw_path) {
Some(path) => path,
_ => {
return CondResult::Ignore(
"translation layer configuration does not exist. No translation layer will be used".into())
}
};
println!("Loading translation layer config: {}", path.display());

let _translation_layer: KeyMap<Key> = match serde_yaml::from_str(
&std::fs::read_to_string(path.as_path())
.expect("Read translation layer config"),
) {
Ok(c) => c,
Err(err) => {
return CondResult::Fail(format!("Can't parse {path:#?}: {err}"))
}
};
}
None => return CondResult::Ok,
};

// TODO: check layout
Expand Down