Merge pull request #7 from bnema/feat/non-qwerty-alt-digits
fix(keys): support non-QWERTY Alt digit bindings
2 files · +226 -20
M internal/usecase/keys/router.go +89 -17
| @@ -5,6 +5,7 @@ package keys | |||
| 5 | 5 | import ( | |
| 6 | 6 | "sync" | |
| 7 | 7 | "time" | |
| 8 | + | "unicode/utf8" | |
| 8 | 9 | ||
| 9 | 10 | "github.com/bnema/vev/internal/ports" | |
| 10 | 11 | ) | |
| @@ -48,6 +49,7 @@ type Router struct { | |||
| 48 | 49 | ||
| 49 | 50 | mu sync.Mutex | |
| 50 | 51 | pending bool | |
| 52 | + | pendingAlt []byte | |
| 51 | 53 | timer ports.Timer | |
| 52 | 54 | pendingDone chan struct{} | |
| 53 | 55 | } | |
| @@ -64,10 +66,13 @@ func (r *Router) Route(data []byte) { | |||
| 64 | 66 | r.mu.Lock() | |
| 65 | 67 | defer r.mu.Unlock() | |
| 66 | 68 | if r.pending { | |
| 69 | + | pendingAlt := r.pendingAlt | |
| 67 | 70 | r.stopTimer() | |
| 68 | 71 | r.pending = false | |
| 69 | - | if consumed := r.routeAfterPendingESC(data); consumed > 0 { | |
| 70 | - | data = data[consumed:] | |
| 72 | + | r.pendingAlt = nil | |
| 73 | + | combined := append(append([]byte(nil), pendingAlt...), data...) | |
| 74 | + | if consumed := r.routeAfterPendingESC(combined, len(pendingAlt)); consumed > len(pendingAlt) { | |
| 75 | + | data = data[consumed-len(pendingAlt):] | |
| 71 | 76 | } | |
| 72 | 77 | } | |
| 73 | 78 | r.route(data) | |
| @@ -99,34 +104,57 @@ func (r *Router) route(data []byte) { | |||
| 99 | 104 | i += 2 | |
| 100 | 105 | continue | |
| 101 | 106 | } | |
| 102 | - | if action, ok := binding(next); ok { | |
| 107 | + | remaining := data[i+1:] | |
| 108 | + | if action, size, ok := binding(remaining); ok { | |
| 103 | 109 | flush() | |
| 104 | 110 | r.h.Action(action) | |
| 105 | - | i += 2 | |
| 111 | + | i += 1 + size | |
| 106 | 112 | continue | |
| 107 | 113 | } | |
| 114 | + | if partialUTF8Rune(remaining) { | |
| 115 | + | flush() | |
| 116 | + | r.retainESC(remaining) | |
| 117 | + | return | |
| 118 | + | } | |
| 108 | 119 | buf = append(buf, ESC) | |
| 109 | 120 | i++ | |
| 110 | 121 | } | |
| 111 | 122 | flush() | |
| 112 | 123 | } | |
| 113 | 124 | ||
| 114 | - | func (r *Router) routeAfterPendingESC(data []byte) int { | |
| 125 | + | func (r *Router) routeAfterPendingESC(data []byte, pendingAltLen int) int { | |
| 115 | 126 | next := data[0] | |
| 116 | 127 | if passThroughPrefix(next) { | |
| 117 | 128 | r.forward([]byte{ESC, next}) | |
| 118 | 129 | return 1 | |
| 119 | 130 | } | |
| 120 | - | if action, ok := binding(next); ok { | |
| 131 | + | if action, size, ok := binding(data); ok { | |
| 121 | 132 | r.h.Action(action) | |
| 122 | - | return 1 | |
| 133 | + | return size | |
| 134 | + | } | |
| 135 | + | if partialUTF8Rune(data) { | |
| 136 | + | r.retainESC(data) | |
| 137 | + | return len(data) | |
| 138 | + | } | |
| 139 | + | key, size := utf8.DecodeRune(data) | |
| 140 | + | if size > 1 { | |
| 141 | + | r.forward(append([]byte{ESC}, data[:size]...)) | |
| 142 | + | return size | |
| 143 | + | } | |
| 144 | + | if key == utf8.RuneError && pendingAltLen > 0 { | |
| 145 | + | r.forward(append([]byte{ESC}, data[:pendingAltLen]...)) | |
| 146 | + | return pendingAltLen | |
| 123 | 147 | } | |
| 124 | 148 | r.forward([]byte{ESC}) | |
| 125 | 149 | return 0 | |
| 126 | 150 | } | |
| 127 | 151 | ||
| 128 | - | func (r *Router) retainESC() { | |
| 152 | + | func (r *Router) retainESC(altBytes ...[]byte) { | |
| 129 | 153 | r.pending = true | |
| 154 | + | r.pendingAlt = nil | |
| 155 | + | if len(altBytes) > 0 { | |
| 156 | + | r.pendingAlt = append([]byte(nil), altBytes[0]...) | |
| 157 | + | } | |
| 130 | 158 | r.timer = r.clock.NewTimer(r.delay) | |
| 131 | 159 | r.pendingDone = make(chan struct{}) | |
| 132 | 160 | go func(timer ports.Timer, done <-chan struct{}) { | |
| @@ -139,7 +167,9 @@ func (r *Router) retainESC() { | |||
| 139 | 167 | defer r.mu.Unlock() | |
| 140 | 168 | if r.pending && r.timer == timer { | |
| 141 | 169 | r.pending = false | |
| 142 | - | r.forward([]byte{ESC}) | |
| 170 | + | data := append([]byte{ESC}, r.pendingAlt...) | |
| 171 | + | r.pendingAlt = nil | |
| 172 | + | r.forward(data) | |
| 143 | 173 | } | |
| 144 | 174 | }(r.timer, r.pendingDone) | |
| 145 | 175 | } | |
| @@ -155,6 +185,14 @@ func (r *Router) stopTimer() { | |||
| 155 | 185 | } | |
| 156 | 186 | } | |
| 157 | 187 | ||
| 188 | + | func partialUTF8Rune(data []byte) bool { | |
| 189 | + | if len(data) == 0 || utf8.FullRune(data) { | |
| 190 | + | return false | |
| 191 | + | } | |
| 192 | + | key, size := utf8.DecodeRune(data) | |
| 193 | + | return key == utf8.RuneError && size == 1 | |
| 194 | + | } | |
| 195 | + | ||
| 158 | 196 | func (r *Router) forward(data []byte) { | |
| 159 | 197 | cp := append([]byte(nil), data...) | |
| 160 | 198 | r.h.Forward(cp) | |
| @@ -162,15 +200,49 @@ func (r *Router) forward(data []byte) { | |||
| 162 | 200 | ||
| 163 | 201 | func passThroughPrefix(b byte) bool { return b == '[' || b == 'O' } | |
| 164 | 202 | ||
| 165 | - | func binding(b byte) (Action, bool) { | |
| 166 | - | switch b { | |
| 203 | + | func binding(data []byte) (Action, int, bool) { | |
| 204 | + | if len(data) == 0 { | |
| 205 | + | return 0, 0, false | |
| 206 | + | } | |
| 207 | + | switch data[0] { | |
| 167 | 208 | case ' ': | |
| 168 | - | return ActionOpenPalette, true | |
| 169 | - | case '1', '2', '3', '4', '5', '6', '7', '8', '9': | |
| 170 | - | return ActionSwitchTab1 + Action(b-'1'), true | |
| 209 | + | return ActionOpenPalette, 1, true | |
| 171 | 210 | case 'a': | |
| 172 | - | return ActionJumpAttention, true | |
| 173 | - | default: | |
| 174 | - | return 0, false | |
| 211 | + | return ActionJumpAttention, 1, true | |
| 212 | + | } | |
| 213 | + | ||
| 214 | + | key, size := utf8.DecodeRune(data) | |
| 215 | + | if key == utf8.RuneError && size == 1 { | |
| 216 | + | return 0, 0, false | |
| 217 | + | } | |
| 218 | + | if idx, ok := topRowDigitIndex(key); ok { | |
| 219 | + | return ActionSwitchTab1 + Action(idx), size, true | |
| 220 | + | } | |
| 221 | + | return 0, 0, false | |
| 222 | + | } | |
| 223 | + | ||
| 224 | + | var topRowDigitAliases = [][]rune{ | |
| 225 | + | {'1', '&'}, | |
| 226 | + | {'2', 'é'}, | |
| 227 | + | {'3', '"'}, | |
| 228 | + | {'4', '\''}, | |
| 229 | + | {'5', '('}, | |
| 230 | + | {'6', '-', '§'}, | |
| 231 | + | {'7', 'è'}, | |
| 232 | + | {'8', '_', '!'}, | |
| 233 | + | {'9', 'ç'}, | |
| 234 | + | } | |
| 235 | + | ||
| 236 | + | // topRowDigitIndex maps symbols emitted by physical top-row digit keys to | |
| 237 | + | // zero-based digit positions. It is modifier-agnostic so Alt+digit and future | |
| 238 | + | // Ctrl+digit bindings can share the same layout support. | |
| 239 | + | func topRowDigitIndex(key rune) (int, bool) { | |
| 240 | + | for idx, aliases := range topRowDigitAliases { | |
| 241 | + | for _, alias := range aliases { | |
| 242 | + | if key == alias { | |
| 243 | + | return idx, true | |
| 244 | + | } | |
| 245 | + | } | |
| 175 | 246 | } | |
| 247 | + | return 0, false | |
| 176 | 248 | } | |
| @@ -5,6 +5,7 @@ package keys | |||||
| 5 | import ( | 5 | import ( | ||
| 6 | "sync" | 6 | "sync" | ||
| 7 | "time" | 7 | "time" | ||
| 8 | + | "unicode/utf8" | |||
| 8 | 9 | ||||
| 9 | "github.com/bnema/vev/internal/ports" | 10 | "github.com/bnema/vev/internal/ports" | ||
| 10 | ) | 11 | ) | ||
| @@ -48,6 +49,7 @@ type Router struct { | |||||
| 48 | 49 | ||||
| 49 | mu sync.Mutex | 50 | mu sync.Mutex | ||
| 50 | pending bool | 51 | pending bool | ||
| 52 | + | pendingAlt []byte | |||
| 51 | timer ports.Timer | 53 | timer ports.Timer | ||
| 52 | pendingDone chan struct{} | 54 | pendingDone chan struct{} | ||
| 53 | } | 55 | } | ||
| @@ -64,10 +66,13 @@ func (r *Router) Route(data []byte) { | |||||
| 64 | r.mu.Lock() | 66 | r.mu.Lock() | ||
| 65 | defer r.mu.Unlock() | 67 | defer r.mu.Unlock() | ||
| 66 | if r.pending { | 68 | if r.pending { | ||
| 69 | + | pendingAlt := r.pendingAlt | |||
| 67 | r.stopTimer() | 70 | r.stopTimer() | ||
| 68 | r.pending = false | 71 | r.pending = false | ||
| 69 | - | if consumed := r.routeAfterPendingESC(data); consumed > 0 { | 72 | + | r.pendingAlt = nil |
| 70 | - | data = data[consumed:] | 73 | + | combined := append(append([]byte(nil), pendingAlt...), data...) |
| 74 | + | if consumed := r.routeAfterPendingESC(combined, len(pendingAlt)); consumed > len(pendingAlt) { | |||
| 75 | + | data = data[consumed-len(pendingAlt):] | |||
| 71 | } | 76 | } | ||
| 72 | } | 77 | } | ||
| 73 | r.route(data) | 78 | r.route(data) | ||
| @@ -99,34 +104,57 @@ func (r *Router) route(data []byte) { | |||||
| 99 | i += 2 | 104 | i += 2 | ||
| 100 | continue | 105 | continue | ||
| 101 | } | 106 | } | ||
| 102 | - | if action, ok := binding(next); ok { | 107 | + | remaining := data[i+1:] |
| 108 | + | if action, size, ok := binding(remaining); ok { | |||
| 103 | flush() | 109 | flush() | ||
| 104 | r.h.Action(action) | 110 | r.h.Action(action) | ||
| 105 | - | i += 2 | 111 | + | i += 1 + size |
| 106 | continue | 112 | continue | ||
| 107 | } | 113 | } | ||
| 114 | + | if partialUTF8Rune(remaining) { | |||
| 115 | + | flush() | |||
| 116 | + | r.retainESC(remaining) | |||
| 117 | + | return | |||
| 118 | + | } | |||
| 108 | buf = append(buf, ESC) | 119 | buf = append(buf, ESC) | ||
| 109 | i++ | 120 | i++ | ||
| 110 | } | 121 | } | ||
| 111 | flush() | 122 | flush() | ||
| 112 | } | 123 | } | ||
| 113 | 124 | ||||
| 114 | - | func (r *Router) routeAfterPendingESC(data []byte) int { | 125 | + | func (r *Router) routeAfterPendingESC(data []byte, pendingAltLen int) int { |
| 115 | next := data[0] | 126 | next := data[0] | ||
| 116 | if passThroughPrefix(next) { | 127 | if passThroughPrefix(next) { | ||
| 117 | r.forward([]byte{ESC, next}) | 128 | r.forward([]byte{ESC, next}) | ||
| 118 | return 1 | 129 | return 1 | ||
| 119 | } | 130 | } | ||
| 120 | - | if action, ok := binding(next); ok { | 131 | + | if action, size, ok := binding(data); ok { |
| 121 | r.h.Action(action) | 132 | r.h.Action(action) | ||
| 122 | - | return 1 | 133 | + | return size |
| 134 | + | } | |||
| 135 | + | if partialUTF8Rune(data) { | |||
| 136 | + | r.retainESC(data) | |||
| 137 | + | return len(data) | |||
| 138 | + | } | |||
| 139 | + | key, size := utf8.DecodeRune(data) | |||
| 140 | + | if size > 1 { | |||
| 141 | + | r.forward(append([]byte{ESC}, data[:size]...)) | |||
| 142 | + | return size | |||
| 143 | + | } | |||
| 144 | + | if key == utf8.RuneError && pendingAltLen > 0 { | |||
| 145 | + | r.forward(append([]byte{ESC}, data[:pendingAltLen]...)) | |||
| 146 | + | return pendingAltLen | |||
| 123 | } | 147 | } | ||
| 124 | r.forward([]byte{ESC}) | 148 | r.forward([]byte{ESC}) | ||
| 125 | return 0 | 149 | return 0 | ||
| 126 | } | 150 | } | ||
| 127 | 151 | ||||
| 128 | - | func (r *Router) retainESC() { | 152 | + | func (r *Router) retainESC(altBytes ...[]byte) { |
| 129 | r.pending = true | 153 | r.pending = true | ||
| 154 | + | r.pendingAlt = nil | |||
| 155 | + | if len(altBytes) > 0 { | |||
| 156 | + | r.pendingAlt = append([]byte(nil), altBytes[0]...) | |||
| 157 | + | } | |||
| 130 | r.timer = r.clock.NewTimer(r.delay) | 158 | r.timer = r.clock.NewTimer(r.delay) | ||
| 131 | r.pendingDone = make(chan struct{}) | 159 | r.pendingDone = make(chan struct{}) | ||
| 132 | go func(timer ports.Timer, done <-chan struct{}) { | 160 | go func(timer ports.Timer, done <-chan struct{}) { | ||
| @@ -139,7 +167,9 @@ func (r *Router) retainESC() { | |||||
| 139 | defer r.mu.Unlock() | 167 | defer r.mu.Unlock() | ||
| 140 | if r.pending && r.timer == timer { | 168 | if r.pending && r.timer == timer { | ||
| 141 | r.pending = false | 169 | r.pending = false | ||
| 142 | - | r.forward([]byte{ESC}) | 170 | + | data := append([]byte{ESC}, r.pendingAlt...) |
| 171 | + | r.pendingAlt = nil | |||
| 172 | + | r.forward(data) | |||
| 143 | } | 173 | } | ||
| 144 | }(r.timer, r.pendingDone) | 174 | }(r.timer, r.pendingDone) | ||
| 145 | } | 175 | } | ||
| @@ -155,6 +185,14 @@ func (r *Router) stopTimer() { | |||||
| 155 | } | 185 | } | ||
| 156 | } | 186 | } | ||
| 157 | 187 | ||||
| 188 | + | func partialUTF8Rune(data []byte) bool { | |||
| 189 | + | if len(data) == 0 || utf8.FullRune(data) { | |||
| 190 | + | return false | |||
| 191 | + | } | |||
| 192 | + | key, size := utf8.DecodeRune(data) | |||
| 193 | + | return key == utf8.RuneError && size == 1 | |||
| 194 | + | } | |||
| 195 | + | ||||
| 158 | func (r *Router) forward(data []byte) { | 196 | func (r *Router) forward(data []byte) { | ||
| 159 | cp := append([]byte(nil), data...) | 197 | cp := append([]byte(nil), data...) | ||
| 160 | r.h.Forward(cp) | 198 | r.h.Forward(cp) | ||
| @@ -162,15 +200,49 @@ func (r *Router) forward(data []byte) { | |||||
| 162 | 200 | ||||
| 163 | func passThroughPrefix(b byte) bool { return b == '[' || b == 'O' } | 201 | func passThroughPrefix(b byte) bool { return b == '[' || b == 'O' } | ||
| 164 | 202 | ||||
| 165 | - | func binding(b byte) (Action, bool) { | 203 | + | func binding(data []byte) (Action, int, bool) { |
| 166 | - | switch b { | 204 | + | if len(data) == 0 { |
| 205 | + | return 0, 0, false | |||
| 206 | + | } | |||
| 207 | + | switch data[0] { | |||
| 167 | case ' ': | 208 | case ' ': | ||
| 168 | - | return ActionOpenPalette, true | 209 | + | return ActionOpenPalette, 1, true |
| 169 | - | case '1', '2', '3', '4', '5', '6', '7', '8', '9': | |||
| 170 | - | return ActionSwitchTab1 + Action(b-'1'), true | |||
| 171 | case 'a': | 210 | case 'a': | ||
| 172 | - | return ActionJumpAttention, true | 211 | + | return ActionJumpAttention, 1, true |
| 173 | - | default: | 212 | + | } |
| 174 | - | return 0, false | 213 | + | |
| 214 | + | key, size := utf8.DecodeRune(data) | |||
| 215 | + | if key == utf8.RuneError && size == 1 { | |||
| 216 | + | return 0, 0, false | |||
| 217 | + | } | |||
| 218 | + | if idx, ok := topRowDigitIndex(key); ok { | |||
| 219 | + | return ActionSwitchTab1 + Action(idx), size, true | |||
| 220 | + | } | |||
| 221 | + | return 0, 0, false | |||
| 222 | + | } | |||
| 223 | + | ||||
| 224 | + | var topRowDigitAliases = [][]rune{ | |||
| 225 | + | {'1', '&'}, | |||
| 226 | + | {'2', 'é'}, | |||
| 227 | + | {'3', '"'}, | |||
| 228 | + | {'4', '\''}, | |||
| 229 | + | {'5', '('}, | |||
| 230 | + | {'6', '-', '§'}, | |||
| 231 | + | {'7', 'è'}, | |||
| 232 | + | {'8', '_', '!'}, | |||
| 233 | + | {'9', 'ç'}, | |||
| 234 | + | } | |||
| 235 | + | ||||
| 236 | + | // topRowDigitIndex maps symbols emitted by physical top-row digit keys to | |||
| 237 | + | // zero-based digit positions. It is modifier-agnostic so Alt+digit and future | |||
| 238 | + | // Ctrl+digit bindings can share the same layout support. | |||
| 239 | + | func topRowDigitIndex(key rune) (int, bool) { | |||
| 240 | + | for idx, aliases := range topRowDigitAliases { | |||
| 241 | + | for _, alias := range aliases { | |||
| 242 | + | if key == alias { | |||
| 243 | + | return idx, true | |||
| 244 | + | } | |||
| 245 | + | } | |||
| 175 | } | 246 | } | ||
| 247 | + | return 0, false | |||
| 176 | } | 248 | } | ||
M internal/usecase/keys/router_test.go +137 -3
| @@ -61,12 +61,146 @@ func TestRouterInterceptsAltDigits(t *testing.T) { | |||
| 61 | 61 | for b := byte('1'); b <= '9'; b++ { | |
| 62 | 62 | r.Route([]byte{ESC, b}) | |
| 63 | 63 | } | |
| 64 | - | require.Equal(t, []Action{ | |
| 64 | + | require.Equal(t, switchTabActions(), h.actions) | |
| 65 | + | require.Empty(t, h.forwards) | |
| 66 | + | } | |
| 67 | + | ||
| 68 | + | func TestRouterInterceptsAltLayoutDigitAliases(t *testing.T) { | |
| 69 | + | cases := []struct { | |
| 70 | + | name string | |
| 71 | + | keys []string | |
| 72 | + | }{ | |
| 73 | + | {name: "QWERTY", keys: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}}, | |
| 74 | + | {name: "French AZERTY", keys: []string{"&", "é", "\"", "'", "(", "-", "è", "_", "ç"}}, | |
| 75 | + | {name: "Belgian AZERTY", keys: []string{"&", "é", "\"", "'", "(", "§", "è", "!", "ç"}}, | |
| 76 | + | } | |
| 77 | + | ||
| 78 | + | for _, tc := range cases { | |
| 79 | + | t.Run(tc.name, func(t *testing.T) { | |
| 80 | + | clk := &fakeClock{} | |
| 81 | + | h := &captureHandler{} | |
| 82 | + | r := NewRouter(clk, h) | |
| 83 | + | for _, key := range tc.keys { | |
| 84 | + | r.Route(append([]byte{ESC}, []byte(key)...)) | |
| 85 | + | } | |
| 86 | + | require.Equal(t, switchTabActions(), h.actions) | |
| 87 | + | require.Empty(t, h.forwards) | |
| 88 | + | }) | |
| 89 | + | } | |
| 90 | + | } | |
| 91 | + | ||
| 92 | + | func TestRouterInterceptsAltAZERTYUTF8SplitAcrossReads(t *testing.T) { | |
| 93 | + | clk := &fakeClock{} | |
| 94 | + | h := &captureHandler{} | |
| 95 | + | r := NewRouter(clk, h) | |
| 96 | + | ||
| 97 | + | r.Route([]byte{ESC, 0xc3}) | |
| 98 | + | require.Empty(t, h.actions) | |
| 99 | + | require.Empty(t, h.forwards) | |
| 100 | + | require.Len(t, clk.timers, 1) | |
| 101 | + | ||
| 102 | + | r.Route([]byte{0xa9}) | |
| 103 | + | require.Equal(t, []Action{ActionSwitchTab2}, h.actions) | |
| 104 | + | require.Empty(t, h.forwards) | |
| 105 | + | assertRouterPendingCleared(t, r) | |
| 106 | + | ||
| 107 | + | r.Route([]byte{ESC, ' '}) | |
| 108 | + | require.Equal(t, []Action{ActionSwitchTab2, ActionOpenPalette}, h.actions) | |
| 109 | + | require.Empty(t, h.forwards) | |
| 110 | + | } | |
| 111 | + | ||
| 112 | + | func TestRouterInterceptsRetainedAltAZERTYUTF8SplitAcrossReads(t *testing.T) { | |
| 113 | + | clk := &fakeClock{} | |
| 114 | + | h := &captureHandler{} | |
| 115 | + | r := NewRouter(clk, h) | |
| 116 | + | ||
| 117 | + | r.Route([]byte{ESC}) | |
| 118 | + | r.Route([]byte{0xc3}) | |
| 119 | + | require.Empty(t, h.actions) | |
| 120 | + | require.Empty(t, h.forwards) | |
| 121 | + | require.Len(t, clk.timers, 2) | |
| 122 | + | ||
| 123 | + | r.Route([]byte{0xa9}) | |
| 124 | + | require.Equal(t, []Action{ActionSwitchTab2}, h.actions) | |
| 125 | + | require.Empty(t, h.forwards) | |
| 126 | + | assertRouterPendingCleared(t, r) | |
| 127 | + | ||
| 128 | + | r.Route([]byte{ESC, ' '}) | |
| 129 | + | require.Equal(t, []Action{ActionSwitchTab2, ActionOpenPalette}, h.actions) | |
| 130 | + | require.Empty(t, h.forwards) | |
| 131 | + | } | |
| 132 | + | ||
| 133 | + | func TestRouterForwardsUnboundAltUTF8SplitAcrossReads(t *testing.T) { | |
| 134 | + | clk := &fakeClock{} | |
| 135 | + | h := &captureHandler{} | |
| 136 | + | r := NewRouter(clk, h) | |
| 137 | + | ||
| 138 | + | r.Route([]byte{ESC, 0xc3}) | |
| 139 | + | r.Route([]byte{0xb1}) | |
| 140 | + | ||
| 141 | + | require.Empty(t, h.actions) | |
| 142 | + | require.Equal(t, [][]byte{{ESC, 0xc3, 0xb1}}, h.forwards) | |
| 143 | + | assertRouterPendingCleared(t, r) | |
| 144 | + | ||
| 145 | + | r.Route([]byte("Z")) | |
| 146 | + | require.Empty(t, h.actions) | |
| 147 | + | require.Equal(t, [][]byte{{ESC, 0xc3, 0xb1}, []byte("Z")}, h.forwards) | |
| 148 | + | } | |
| 149 | + | ||
| 150 | + | func TestRouterForwardsInvalidSplitAltUTF8WithoutDroppingBytes(t *testing.T) { | |
| 151 | + | clk := &fakeClock{} | |
| 152 | + | h := &captureHandler{} | |
| 153 | + | r := NewRouter(clk, h) | |
| 154 | + | ||
| 155 | + | r.Route([]byte{ESC, 0xc3}) | |
| 156 | + | r.Route([]byte("X")) | |
| 157 | + | ||
| 158 | + | require.Empty(t, h.actions) | |
| 159 | + | require.Equal(t, [][]byte{{ESC, 0xc3}, []byte("X")}, h.forwards) | |
| 160 | + | assertRouterPendingCleared(t, r) | |
| 161 | + | ||
| 162 | + | r.Route([]byte{ESC, ' '}) | |
| 163 | + | require.Equal(t, []Action{ActionOpenPalette}, h.actions) | |
| 164 | + | require.Equal(t, [][]byte{{ESC, 0xc3}, []byte("X")}, h.forwards) | |
| 165 | + | } | |
| 166 | + | ||
| 167 | + | func assertRouterPendingCleared(t *testing.T, r *Router) { | |
| 168 | + | t.Helper() | |
| 169 | + | require.False(t, r.pending) | |
| 170 | + | require.Nil(t, r.pendingAlt) | |
| 171 | + | require.Nil(t, r.timer) | |
| 172 | + | require.Nil(t, r.pendingDone) | |
| 173 | + | } | |
| 174 | + | ||
| 175 | + | func TestTopRowDigitIndexAcceptsLayoutAliases(t *testing.T) { | |
| 176 | + | for want, aliases := range [][]rune{ | |
| 177 | + | {'1', '&'}, | |
| 178 | + | {'2', 'é'}, | |
| 179 | + | {'3', '"'}, | |
| 180 | + | {'4', '\''}, | |
| 181 | + | {'5', '('}, | |
| 182 | + | {'6', '-', '§'}, | |
| 183 | + | {'7', 'è'}, | |
| 184 | + | {'8', '_', '!'}, | |
| 185 | + | {'9', 'ç'}, | |
| 186 | + | } { | |
| 187 | + | for _, key := range aliases { | |
| 188 | + | got, ok := topRowDigitIndex(key) | |
| 189 | + | require.True(t, ok, "key %q", key) | |
| 190 | + | require.Equal(t, want, got, "key %q", key) | |
| 191 | + | } | |
| 192 | + | } | |
| 193 | + | ||
| 194 | + | _, ok := topRowDigitIndex('0') | |
| 195 | + | require.False(t, ok) | |
| 196 | + | } | |
| 197 | + | ||
| 198 | + | func switchTabActions() []Action { | |
| 199 | + | return []Action{ | |
| 65 | 200 | ActionSwitchTab1, ActionSwitchTab2, ActionSwitchTab3, | |
| 66 | 201 | ActionSwitchTab4, ActionSwitchTab5, ActionSwitchTab6, | |
| 67 | 202 | ActionSwitchTab7, ActionSwitchTab8, ActionSwitchTab9, | |
| 68 | - | }, h.actions) | |
| 69 | - | require.Empty(t, h.forwards) | |
| 203 | + | } | |
| 70 | 204 | } | |
| 71 | 205 | ||
| 72 | 206 | func TestRouterInterceptsAltAForJumpAttention(t *testing.T) { | |
| @@ -61,12 +61,146 @@ func TestRouterInterceptsAltDigits(t *testing.T) { | |||||
| 61 | for b := byte('1'); b <= '9'; b++ { | 61 | for b := byte('1'); b <= '9'; b++ { | ||
| 62 | r.Route([]byte{ESC, b}) | 62 | r.Route([]byte{ESC, b}) | ||
| 63 | } | 63 | } | ||
| 64 | - | require.Equal(t, []Action{ | 64 | + | require.Equal(t, switchTabActions(), h.actions) |
| 65 | + | require.Empty(t, h.forwards) | |||
| 66 | + | } | |||
| 67 | + | ||||
| 68 | + | func TestRouterInterceptsAltLayoutDigitAliases(t *testing.T) { | |||
| 69 | + | cases := []struct { | |||
| 70 | + | name string | |||
| 71 | + | keys []string | |||
| 72 | + | }{ | |||
| 73 | + | {name: "QWERTY", keys: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}}, | |||
| 74 | + | {name: "French AZERTY", keys: []string{"&", "é", "\"", "'", "(", "-", "è", "_", "ç"}}, | |||
| 75 | + | {name: "Belgian AZERTY", keys: []string{"&", "é", "\"", "'", "(", "§", "è", "!", "ç"}}, | |||
| 76 | + | } | |||
| 77 | + | ||||
| 78 | + | for _, tc := range cases { | |||
| 79 | + | t.Run(tc.name, func(t *testing.T) { | |||
| 80 | + | clk := &fakeClock{} | |||
| 81 | + | h := &captureHandler{} | |||
| 82 | + | r := NewRouter(clk, h) | |||
| 83 | + | for _, key := range tc.keys { | |||
| 84 | + | r.Route(append([]byte{ESC}, []byte(key)...)) | |||
| 85 | + | } | |||
| 86 | + | require.Equal(t, switchTabActions(), h.actions) | |||
| 87 | + | require.Empty(t, h.forwards) | |||
| 88 | + | }) | |||
| 89 | + | } | |||
| 90 | + | } | |||
| 91 | + | ||||
| 92 | + | func TestRouterInterceptsAltAZERTYUTF8SplitAcrossReads(t *testing.T) { | |||
| 93 | + | clk := &fakeClock{} | |||
| 94 | + | h := &captureHandler{} | |||
| 95 | + | r := NewRouter(clk, h) | |||
| 96 | + | ||||
| 97 | + | r.Route([]byte{ESC, 0xc3}) | |||
| 98 | + | require.Empty(t, h.actions) | |||
| 99 | + | require.Empty(t, h.forwards) | |||
| 100 | + | require.Len(t, clk.timers, 1) | |||
| 101 | + | ||||
| 102 | + | r.Route([]byte{0xa9}) | |||
| 103 | + | require.Equal(t, []Action{ActionSwitchTab2}, h.actions) | |||
| 104 | + | require.Empty(t, h.forwards) | |||
| 105 | + | assertRouterPendingCleared(t, r) | |||
| 106 | + | ||||
| 107 | + | r.Route([]byte{ESC, ' '}) | |||
| 108 | + | require.Equal(t, []Action{ActionSwitchTab2, ActionOpenPalette}, h.actions) | |||
| 109 | + | require.Empty(t, h.forwards) | |||
| 110 | + | } | |||
| 111 | + | ||||
| 112 | + | func TestRouterInterceptsRetainedAltAZERTYUTF8SplitAcrossReads(t *testing.T) { | |||
| 113 | + | clk := &fakeClock{} | |||
| 114 | + | h := &captureHandler{} | |||
| 115 | + | r := NewRouter(clk, h) | |||
| 116 | + | ||||
| 117 | + | r.Route([]byte{ESC}) | |||
| 118 | + | r.Route([]byte{0xc3}) | |||
| 119 | + | require.Empty(t, h.actions) | |||
| 120 | + | require.Empty(t, h.forwards) | |||
| 121 | + | require.Len(t, clk.timers, 2) | |||
| 122 | + | ||||
| 123 | + | r.Route([]byte{0xa9}) | |||
| 124 | + | require.Equal(t, []Action{ActionSwitchTab2}, h.actions) | |||
| 125 | + | require.Empty(t, h.forwards) | |||
| 126 | + | assertRouterPendingCleared(t, r) | |||
| 127 | + | ||||
| 128 | + | r.Route([]byte{ESC, ' '}) | |||
| 129 | + | require.Equal(t, []Action{ActionSwitchTab2, ActionOpenPalette}, h.actions) | |||
| 130 | + | require.Empty(t, h.forwards) | |||
| 131 | + | } | |||
| 132 | + | ||||
| 133 | + | func TestRouterForwardsUnboundAltUTF8SplitAcrossReads(t *testing.T) { | |||
| 134 | + | clk := &fakeClock{} | |||
| 135 | + | h := &captureHandler{} | |||
| 136 | + | r := NewRouter(clk, h) | |||
| 137 | + | ||||
| 138 | + | r.Route([]byte{ESC, 0xc3}) | |||
| 139 | + | r.Route([]byte{0xb1}) | |||
| 140 | + | ||||
| 141 | + | require.Empty(t, h.actions) | |||
| 142 | + | require.Equal(t, [][]byte{{ESC, 0xc3, 0xb1}}, h.forwards) | |||
| 143 | + | assertRouterPendingCleared(t, r) | |||
| 144 | + | ||||
| 145 | + | r.Route([]byte("Z")) | |||
| 146 | + | require.Empty(t, h.actions) | |||
| 147 | + | require.Equal(t, [][]byte{{ESC, 0xc3, 0xb1}, []byte("Z")}, h.forwards) | |||
| 148 | + | } | |||
| 149 | + | ||||
| 150 | + | func TestRouterForwardsInvalidSplitAltUTF8WithoutDroppingBytes(t *testing.T) { | |||
| 151 | + | clk := &fakeClock{} | |||
| 152 | + | h := &captureHandler{} | |||
| 153 | + | r := NewRouter(clk, h) | |||
| 154 | + | ||||
| 155 | + | r.Route([]byte{ESC, 0xc3}) | |||
| 156 | + | r.Route([]byte("X")) | |||
| 157 | + | ||||
| 158 | + | require.Empty(t, h.actions) | |||
| 159 | + | require.Equal(t, [][]byte{{ESC, 0xc3}, []byte("X")}, h.forwards) | |||
| 160 | + | assertRouterPendingCleared(t, r) | |||
| 161 | + | ||||
| 162 | + | r.Route([]byte{ESC, ' '}) | |||
| 163 | + | require.Equal(t, []Action{ActionOpenPalette}, h.actions) | |||
| 164 | + | require.Equal(t, [][]byte{{ESC, 0xc3}, []byte("X")}, h.forwards) | |||
| 165 | + | } | |||
| 166 | + | ||||
| 167 | + | func assertRouterPendingCleared(t *testing.T, r *Router) { | |||
| 168 | + | t.Helper() | |||
| 169 | + | require.False(t, r.pending) | |||
| 170 | + | require.Nil(t, r.pendingAlt) | |||
| 171 | + | require.Nil(t, r.timer) | |||
| 172 | + | require.Nil(t, r.pendingDone) | |||
| 173 | + | } | |||
| 174 | + | ||||
| 175 | + | func TestTopRowDigitIndexAcceptsLayoutAliases(t *testing.T) { | |||
| 176 | + | for want, aliases := range [][]rune{ | |||
| 177 | + | {'1', '&'}, | |||
| 178 | + | {'2', 'é'}, | |||
| 179 | + | {'3', '"'}, | |||
| 180 | + | {'4', '\''}, | |||
| 181 | + | {'5', '('}, | |||
| 182 | + | {'6', '-', '§'}, | |||
| 183 | + | {'7', 'è'}, | |||
| 184 | + | {'8', '_', '!'}, | |||
| 185 | + | {'9', 'ç'}, | |||
| 186 | + | } { | |||
| 187 | + | for _, key := range aliases { | |||
| 188 | + | got, ok := topRowDigitIndex(key) | |||
| 189 | + | require.True(t, ok, "key %q", key) | |||
| 190 | + | require.Equal(t, want, got, "key %q", key) | |||
| 191 | + | } | |||
| 192 | + | } | |||
| 193 | + | ||||
| 194 | + | _, ok := topRowDigitIndex('0') | |||
| 195 | + | require.False(t, ok) | |||
| 196 | + | } | |||
| 197 | + | ||||
| 198 | + | func switchTabActions() []Action { | |||
| 199 | + | return []Action{ | |||
| 65 | ActionSwitchTab1, ActionSwitchTab2, ActionSwitchTab3, | 200 | ActionSwitchTab1, ActionSwitchTab2, ActionSwitchTab3, | ||
| 66 | ActionSwitchTab4, ActionSwitchTab5, ActionSwitchTab6, | 201 | ActionSwitchTab4, ActionSwitchTab5, ActionSwitchTab6, | ||
| 67 | ActionSwitchTab7, ActionSwitchTab8, ActionSwitchTab9, | 202 | ActionSwitchTab7, ActionSwitchTab8, ActionSwitchTab9, | ||
| 68 | - | }, h.actions) | 203 | + | } |
| 69 | - | require.Empty(t, h.forwards) | |||
| 70 | } | 204 | } | ||
| 71 | 205 | ||||
| 72 | func TestRouterInterceptsAltAForJumpAttention(t *testing.T) { | 206 | func TestRouterInterceptsAltAForJumpAttention(t *testing.T) { | ||