Skip to content

Commit

Permalink
feat: Add Colemak
Browse files Browse the repository at this point in the history
  • Loading branch information
New9c committed Mar 5, 2025
1 parent ea45c5b commit 8a76307
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
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

0 comments on commit 8a76307

Please sign in to comment.