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

feat: Adding Colemak #674

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ preference settings.
- HanYu PinYin
- Taiwan Huayu Luomapinyin
- MPS2 Pinyin
- Colemak
- Colemak-DH ANSI
- Colemak-DH Ortholinear

Expand Down
2 changes: 2 additions & 0 deletions capi/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ pub unsafe extern "C" fn chewing_config_set_str(
KB::ThlPinyin => (AnyKeyboardLayout::qwerty(), Box::new(Pinyin::thl())),
KB::Mps2Pinyin => (AnyKeyboardLayout::qwerty(), Box::new(Pinyin::mps2())),
KB::Carpalx => (AnyKeyboardLayout::qwerty(), Box::new(Standard::new())),
KB::Colemak => (AnyKeyboardLayout::colemak(), Box::new(Standard::new())),
KB::ColemakDhAnsi => (
AnyKeyboardLayout::colemak_dh_ansi(),
Box::new(Standard::new()),
Expand Down Expand Up @@ -611,6 +612,7 @@ pub unsafe extern "C" fn chewing_set_KBType(ctx: *mut ChewingContext, kbtype: c_
KB::ThlPinyin => (AnyKeyboardLayout::qwerty(), Box::new(Pinyin::thl())),
KB::Mps2Pinyin => (AnyKeyboardLayout::qwerty(), Box::new(Pinyin::mps2())),
KB::Carpalx => (AnyKeyboardLayout::qwerty(), Box::new(Standard::new())),
KB::Colemak => (AnyKeyboardLayout::colemak(), Box::new(Standard::new())),
KB::ColemakDhAnsi => (
AnyKeyboardLayout::colemak_dh_ansi(),
Box::new(Standard::new()),
Expand Down
1 change: 1 addition & 0 deletions capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ pub mod layout {
/// * KB_THL_PINYIN
/// * KB_MPS2_PINYIN
/// * KB_CARPALX
/// * KB_COLEMAK
/// * KB_COLEMAK_DH_ANSI
/// * KB_COLEMAK_DH_ORTH
/// * KB_WORKMAN
Expand Down
1 change: 1 addition & 0 deletions capi/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub enum KB {
ThlPinyin,
Mps2Pinyin,
Carpalx,
Colemak,
ColemakDhAnsi,
ColemakDhOrth,
Workman,
Expand Down
1 change: 1 addition & 0 deletions doc/libchewing.texi
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ The string @var{str} might be one of the following layouts:
@item @code{KB_THL_PINYIN}
@item @code{KB_MPS2_PINYIN}
@item @code{KB_CARPALX}
@item @code{KB_COLEMAK}
@item @code{KB_COLEMAK_DH_ANSI}
@item @code{KB_COLEMAK_DH_ORTH}
@end itemize
Expand Down
1 change: 1 addition & 0 deletions include/chewing.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ typedef enum KB {
KB_THL_PINYIN,
KB_MPS2_PINYIN,
KB_CARPALX,
KB_COLEMAK,
KB_COLEMAK_DH_ANSI,
KB_COLEMAK_DH_ORTH,
KB_WORKMAN,
Expand Down
48 changes: 48 additions & 0 deletions src/editor/keyboard/colemak.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use super::{
generic_map_keycode,
KeyCode::{self, *},
KeyEvent, KeyboardLayout, Modifiers, MATRIX_SIZE,
};

/// A Colemak keyboard.
#[derive(Debug)]
pub struct Colemak;

#[rustfmt::skip]
static KEYCODE_INDEX: [KeyCode; MATRIX_SIZE] = [
Unknown,
N1, N2, N3, N4, N5, N6, N7, N8, N9, N0, Minus, Equal, BSlash, Grave,
Q, W, F, P, G, J, L, U, Y, SColon, LBracket, RBracket,
A, R, S, T, D, H, N, E, I, O, Quote,
Z, X, C, V, B, K, M, Comma, Dot, Slash, Space,
Esc, Enter, Del, Backspace, Tab, Left, Right, Up, Down, Home, End,
PageUp, PageDown, NumLock,
];

#[rustfmt::skip]
static UNICODE_MAP: [char; MATRIX_SIZE] = [
'�',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\\', '`',
'q', 'w', 'f', 'p', 'g', 'j', 'l', 'u', 'y', ';', '[', ']',
'a', 'r', 's', 't', 'd', 'h', 'n', 'e', 'i', 'o', '\'',
'z', 'x', 'c', 'v', 'b', 'k', 'm', ',', '.', '/', ' ',
'�', '�', '�', '�', '�', '�', '�', '�', '�', '�',
'�', '�', '�', '�',
];

#[rustfmt::skip]
static SHIFT_MAP: [char; MATRIX_SIZE] = [
'�',
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '|', '~',
'Q', 'W', 'F', 'P', 'G', 'J', 'L', 'U', 'Y', ':', '{', '}',
'A', 'R', 'S', 'T', 'D', 'H', 'N', 'E', 'I', 'O', '"',
'Z', 'X', 'C', 'V', 'B', 'K', 'M', '<', '>', '?', ' ',
'�', '�', '�', '�', '�', '�', '�', '�', '�', '�',
'�', '�', '�', '�',
];

impl KeyboardLayout for Colemak {
fn map_with_mod(&self, keycode: KeyCode, modifiers: Modifiers) -> KeyEvent {
generic_map_keycode(&KEYCODE_INDEX, &UNICODE_MAP, &SHIFT_MAP, keycode, modifiers)
}
}
7 changes: 7 additions & 0 deletions src/editor/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! to map different English layouts to layout independent key indexes that can be
//! used to drive the phonetic conversion engines.

mod colemak;
mod colemak_dh_ansi;
mod colemak_dh_orth;
mod dvorak;
Expand All @@ -15,6 +16,7 @@ mod workman;

use core::fmt;

pub use colemak::Colemak;
pub use colemak_dh_ansi::ColemakDhAnsi;
pub use colemak_dh_orth::ColemakDhOrth;
pub use dvorak::Dvorak;
Expand Down Expand Up @@ -139,6 +141,7 @@ pub enum AnyKeyboardLayout {
Dvorak(Dvorak),
DvorakOnQwerty(DvorakOnQwerty),
Qgmlwy(Qgmlwy),
Colemak(Colemak),
ColemakDhAnsi(ColemakDhAnsi),
ColemakDhOrth(ColemakDhOrth),
Workman(Workman),
Expand All @@ -157,6 +160,9 @@ impl AnyKeyboardLayout {
pub fn qgmlwy() -> AnyKeyboardLayout {
AnyKeyboardLayout::Qgmlwy(Qgmlwy)
}
pub fn colemak() -> AnyKeyboardLayout {
AnyKeyboardLayout::Colemak(Colemak)
}
pub fn colemak_dh_ansi() -> AnyKeyboardLayout {
AnyKeyboardLayout::ColemakDhAnsi(ColemakDhAnsi)
}
Expand All @@ -175,6 +181,7 @@ impl KeyboardLayout for AnyKeyboardLayout {
AnyKeyboardLayout::Dvorak(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::DvorakOnQwerty(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::Qgmlwy(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::Colemak(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::ColemakDhAnsi(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::ColemakDhOrth(kb) => kb.map_with_mod(keycode, modifiers),
AnyKeyboardLayout::Workman(kb) => kb.map_with_mod(keycode, modifiers),
Expand Down
11 changes: 8 additions & 3 deletions src/editor/zhuyin_layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub enum KeyboardLayoutCompat {
/// TODO: docs
Carpalx,
/// TODO: docs
Colemak,
/// TODO: docs
ColemakDhAnsi,
/// TODO: docs
ColemakDhOrth,
Expand Down Expand Up @@ -109,6 +111,7 @@ impl FromStr for KeyboardLayoutCompat {
"KB_THL_PINYIN" => Self::ThlPinyin,
"KB_MPS2_PINYIN" => Self::Mps2Pinyin,
"KB_CARPALX" => Self::Carpalx,
"KB_COLEMAK" => Self::Colemak,
"KB_COLEMAK_DH_ANSI" => Self::ColemakDhAnsi,
"KB_COLEMAK_DH_ORTH" => Self::ColemakDhOrth,
"KB_WORKMAN" => Self::Workman,
Expand All @@ -134,6 +137,7 @@ impl Display for KeyboardLayoutCompat {
KeyboardLayoutCompat::ThlPinyin => f.write_str("KB_THL_PINYIN"),
KeyboardLayoutCompat::Mps2Pinyin => f.write_str("KB_MPS2_PINYIN"),
KeyboardLayoutCompat::Carpalx => f.write_str("KB_CARPALX"),
KeyboardLayoutCompat::Colemak => f.write_str("KB_COLEMAK"),
KeyboardLayoutCompat::ColemakDhAnsi => f.write_str("KB_COLEMAK_DH_ANSI"),
KeyboardLayoutCompat::ColemakDhOrth => f.write_str("KB_COLEMAK_DH_ORTH"),
KeyboardLayoutCompat::Workman => f.write_str("KB_WORKMAN"),
Expand All @@ -159,9 +163,10 @@ impl TryFrom<u8> for KeyboardLayoutCompat {
10 => Self::ThlPinyin,
11 => Self::Mps2Pinyin,
12 => Self::Carpalx,
13 => Self::ColemakDhAnsi,
14 => Self::ColemakDhOrth,
15 => Self::Workman,
13 => Self::Colemak,
14 => Self::ColemakDhAnsi,
15 => Self::ColemakDhOrth,
16 => Self::Workman,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not change the existing number. Let's add Colemak as 16th layout.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍
BTW, Workman seems to exist as a layout, so I wonder if it should be on the README's "support phonetic keyboard layout" section

_ => return Err(()),
})
}
Expand Down
Loading