diff options
| author | salmonik <raisersoft@proton.me> | 2026-07-11 01:46:59 +0200 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-07-11 01:46:59 +0200 |
| commit | 247d2805db0c1b686ef4c6cc0f786ff416aeb7e3 (patch) | |
| tree | b6dae4a848df5ae75c4d1bc8e73bad7c63d17575 /src/layout.rs | |
| download | xkbpreview-247d2805db0c1b686ef4c6cc0f786ff416aeb7e3.tar.gz xkbpreview-247d2805db0c1b686ef4c6cc0f786ff416aeb7e3.zip | |
init
Diffstat (limited to '')
| -rw-r--r-- | src/layout.rs | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/src/layout.rs b/src/layout.rs new file mode 100644 index 0000000..4dad3fb --- /dev/null +++ b/src/layout.rs | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | #[derive(Debug, Clone, Copy)] | ||
| 2 | pub struct KeyPos { | ||
| 3 | pub row: usize, | ||
| 4 | pub col: f32, | ||
| 5 | pub width: f32, | ||
| 6 | } | ||
| 7 | |||
| 8 | fn ansi_row0(name: &str) -> Option<KeyPos> { | ||
| 9 | let keys: &[(&str, f32, f32)] = &[ | ||
| 10 | ("ESC", 0.0, 1.0), | ||
| 11 | ("TLDE", 1.0, 1.0), | ||
| 12 | ("AE01", 2.0, 1.0), | ||
| 13 | ("AE02", 3.0, 1.0), | ||
| 14 | ("AE03", 4.0, 1.0), | ||
| 15 | ("AE04", 5.0, 1.0), | ||
| 16 | ("AE05", 6.0, 1.0), | ||
| 17 | ("AE06", 7.0, 1.0), | ||
| 18 | ("AE07", 8.0, 1.0), | ||
| 19 | ("AE08", 9.0, 1.0), | ||
| 20 | ("AE09", 10.0, 1.0), | ||
| 21 | ("AE10", 11.0, 1.0), | ||
| 22 | ("AE11", 12.0, 1.0), | ||
| 23 | ("AE12", 13.0, 1.0), | ||
| 24 | ("BKSP", 14.0, 1.5), | ||
| 25 | ]; | ||
| 26 | keys.iter().find(|(n, _, _)| *n == name).map(|(_, c, w)| KeyPos { row: 0, col: *c, width: *w }) | ||
| 27 | } | ||
| 28 | |||
| 29 | fn ansi_row1(name: &str) -> Option<KeyPos> { | ||
| 30 | let keys: &[(&str, f32, f32)] = &[ | ||
| 31 | ("TAB", 0.0, 1.5), | ||
| 32 | ("AD01", 1.5, 1.0), | ||
| 33 | ("AD02", 2.5, 1.0), | ||
| 34 | ("AD03", 3.5, 1.0), | ||
| 35 | ("AD04", 4.5, 1.0), | ||
| 36 | ("AD05", 5.5, 1.0), | ||
| 37 | ("AD06", 6.5, 1.0), | ||
| 38 | ("AD07", 7.5, 1.0), | ||
| 39 | ("AD08", 8.5, 1.0), | ||
| 40 | ("AD09", 9.5, 1.0), | ||
| 41 | ("AD10", 10.5, 1.0), | ||
| 42 | ("AD11", 11.5, 1.0), | ||
| 43 | ("AD12", 12.5, 1.0), | ||
| 44 | ("BKSL", 13.5, 1.5), | ||
| 45 | ]; | ||
| 46 | keys.iter().find(|(n, _, _)| *n == name).map(|(_, c, w)| KeyPos { row: 1, col: *c, width: *w }) | ||
| 47 | } | ||
| 48 | |||
| 49 | fn ansi_row2(name: &str) -> Option<KeyPos> { | ||
| 50 | let keys: &[(&str, f32, f32)] = &[ | ||
| 51 | ("CAPS", 0.0, 1.75), | ||
| 52 | ("AC01", 1.75, 1.0), | ||
| 53 | ("AC02", 2.75, 1.0), | ||
| 54 | ("AC03", 3.75, 1.0), | ||
| 55 | ("AC04", 4.75, 1.0), | ||
| 56 | ("AC05", 5.75, 1.0), | ||
| 57 | ("AC06", 6.75, 1.0), | ||
| 58 | ("AC07", 7.75, 1.0), | ||
| 59 | ("AC08", 8.75, 1.0), | ||
| 60 | ("AC09", 9.75, 1.0), | ||
| 61 | ("AC10", 10.75, 1.0), | ||
| 62 | ("AC11", 11.75, 1.0), | ||
| 63 | ("RET", 12.75, 2.25), | ||
| 64 | ]; | ||
| 65 | keys.iter().find(|(n, _, _)| *n == name).map(|(_, c, w)| KeyPos { row: 2, col: *c, width: *w }) | ||
| 66 | } | ||
| 67 | |||
| 68 | fn ansi_row3(name: &str) -> Option<KeyPos> { | ||
| 69 | let keys: &[(&str, f32, f32)] = &[ | ||
| 70 | ("LFSH", 0.0, 2.25), | ||
| 71 | ("AB01", 2.25, 1.0), | ||
| 72 | ("AB02", 3.25, 1.0), | ||
| 73 | ("AB03", 4.25, 1.0), | ||
| 74 | ("AB04", 5.25, 1.0), | ||
| 75 | ("AB05", 6.25, 1.0), | ||
| 76 | ("AB06", 7.25, 1.0), | ||
| 77 | ("AB07", 8.25, 1.0), | ||
| 78 | ("AB08", 9.25, 1.0), | ||
| 79 | ("AB09", 10.25, 1.0), | ||
| 80 | ("AB10", 11.25, 1.0), | ||
| 81 | ("RTSH", 12.25, 2.75), | ||
| 82 | ]; | ||
| 83 | keys.iter().find(|(n, _, _)| *n == name).map(|(_, c, w)| KeyPos { row: 3, col: *c, width: *w }) | ||
| 84 | } | ||
| 85 | |||
| 86 | fn ansi_row4(name: &str) -> Option<KeyPos> { | ||
| 87 | let keys: &[(&str, f32, f32)] = &[ | ||
| 88 | ("LCTL", 0.0, 1.25), | ||
| 89 | ("LWIN", 1.25, 1.25), | ||
| 90 | ("LALT", 2.50, 1.25), | ||
| 91 | ("SPCE", 3.75, 6.25), | ||
| 92 | ("RALT", 10.0, 1.25), | ||
| 93 | ("RWIN", 11.25, 1.25), | ||
| 94 | ("MENU", 12.50, 1.25), | ||
| 95 | ("RCTL", 13.75, 1.25), | ||
| 96 | ]; | ||
| 97 | keys.iter().find(|(n, _, _)| *n == name).map(|(_, c, w)| KeyPos { row: 4, col: *c, width: *w }) | ||
| 98 | } | ||
| 99 | |||
| 100 | fn iso_row3(name: &str, _has_lsgt: bool) -> Option<KeyPos> { | ||
| 101 | let keys: &[(&str, f32, f32)] = &[ | ||
| 102 | ("LFSH", 0.0, 1.25), | ||
| 103 | ("LSGT", 1.25, 1.0), | ||
| 104 | ("AB01", 2.25, 1.0), | ||
| 105 | ("AB02", 3.25, 1.0), | ||
| 106 | ("AB03", 4.25, 1.0), | ||
| 107 | ("AB04", 5.25, 1.0), | ||
| 108 | ("AB05", 6.25, 1.0), | ||
| 109 | ("AB06", 7.25, 1.0), | ||
| 110 | ("AB07", 8.25, 1.0), | ||
| 111 | ("AB08", 9.25, 1.0), | ||
| 112 | ("AB09", 10.25, 1.0), | ||
| 113 | ("AB10", 11.25, 1.0), | ||
| 114 | ("RTSH", 12.25, 2.75), | ||
| 115 | ]; | ||
| 116 | keys.iter().find(|(n, _, _)| *n == name).map(|(_, c, w)| KeyPos { row: 3, col: *c, width: *w }) | ||
| 117 | } | ||
| 118 | |||
| 119 | fn iso_row2(name: &str) -> Option<KeyPos> { | ||
| 120 | let keys: &[(&str, f32, f32)] = &[ | ||
| 121 | ("CAPS", 0.0, 1.75), | ||
| 122 | ("AC01", 1.75, 1.0), | ||
| 123 | ("AC02", 2.75, 1.0), | ||
| 124 | ("AC03", 3.75, 1.0), | ||
| 125 | ("AC04", 4.75, 1.0), | ||
| 126 | ("AC05", 5.75, 1.0), | ||
| 127 | ("AC06", 6.75, 1.0), | ||
| 128 | ("AC07", 7.75, 1.0), | ||
| 129 | ("AC08", 8.75, 1.0), | ||
| 130 | ("AC09", 9.75, 1.0), | ||
| 131 | ("AC10", 10.75, 1.0), | ||
| 132 | ("AC11", 11.75, 1.0), | ||
| 133 | ("RET", 12.75, 2.25), | ||
| 134 | ]; | ||
| 135 | keys.iter().find(|(n, _, _)| *n == name).map(|(_, c, w)| KeyPos { row: 2, col: *c, width: *w }) | ||
| 136 | } | ||
| 137 | |||
| 138 | pub fn resolve_position(name: &str, is_iso: bool) -> KeyPos { | ||
| 139 | if is_iso { | ||
| 140 | if let Some(pos) = iso_row3(name, true).or_else(|| ansi_row0(name)) | ||
| 141 | .or_else(|| ansi_row1(name)) | ||
| 142 | .or_else(|| iso_row2(name)) | ||
| 143 | .or_else(|| ansi_row4(name)) | ||
| 144 | { | ||
| 145 | return pos; | ||
| 146 | } | ||
| 147 | } else { | ||
| 148 | if let Some(pos) = ansi_row0(name) | ||
| 149 | .or_else(|| ansi_row1(name)) | ||
| 150 | .or_else(|| ansi_row2(name)) | ||
| 151 | .or_else(|| ansi_row3(name)) | ||
| 152 | .or_else(|| ansi_row4(name)) | ||
| 153 | { | ||
| 154 | return pos; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | let extra = DEFAULT_POS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | ||
| 159 | KeyPos { | ||
| 160 | row: 5, | ||
| 161 | col: (extra % 16) as f32, | ||
| 162 | width: 1.0, | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | use std::sync::atomic::AtomicU32; | ||
| 167 | static DEFAULT_POS: AtomicU32 = AtomicU32::new(0); | ||
| 168 | |||
| 169 | pub fn reset_default_pos() { | ||
| 170 | DEFAULT_POS.store(0, std::sync::atomic::Ordering::Relaxed); | ||
| 171 | } | ||
