ui: add virtual keyboard for keyboard-less desktop clients

This commit is contained in:
jean-emmanuel 2022-10-25 13:20:33 +02:00
parent 7b06f7e1a8
commit 3f30ec32d3
13 changed files with 387 additions and 4 deletions

View file

@ -16,7 +16,7 @@ Client options can be set either with the server's `--client-options` option, or
| noFocus | 1 / 0 | 0 | (built-in client only) prevent the client window from taking focus unless a text input or a dropdown is clicked |
| clientSync | 1 / 0 | 1 | set to 0 to prevent the client from syncing with other clients |
| altTraversing | 1 / 0 | 0 | set to 1 to enable an alternative traversing behavior for toggle buttons (traversing gestures on toggle buttons will change their values in only one way depending on the first touched button's state) |
| virtualKeyboard | 1 / 0 | 0 | (desktop only) set to 1 to enable the virtual keyboard by default |
Example:

View file

@ -28,6 +28,7 @@
- Console
- Enable: toggle console's state
- Clear console: remove logged messages from the console
- Virtual keyboard: (desktop only) toggle virtual keyboard (displayed only when a text input is in focus)
- Fullscreen: toggle fullscreen
- Prevent sleep (mobile only): attempt to prevent device from going to sleep when idle

View file

@ -32,6 +32,8 @@ window.CLIENT_SYNC = parseInt(ENV.clientSync) !== 0
window.TOGGLE_ALT_TRAVERSING = !!ENV.altTraversing
window.VIRTUAL_KEYBOARD = !!ENV.virtualKeyboard && !navigator.userAgent.match(/Android|iPhone|iPad|iPod/i)
window.JSON.parseFlex = require('json5').parse

View file

@ -53,6 +53,26 @@ module.exports = {
file_import: 'Import',
file_export: 'Export',
// keyboard
keyboard: 'Virtual keyboard',
keyboard_layout: {
default: [
'{esc} ` 1 2 3 4 5 6 7 8 9 0 - = {bksp}',
'{tab} q w e r t y u i o p [ ] \\',
'{lock} a s d f g h j k l ; \' {enter}',
'{shift} z x c v b n m , . / {shift}',
'{space}'
],
shift: [
'{esc} ~ ! @ # $ % ^ & * ( ) _ + {bksp}',
'{tab} Q W E R T Y U I O P { } |',
'{lock} A S D F G H J K L : " {enter}',
'{shift} Z X C V B N M < > ? {shift}',
'{space}'
]
},
keyboard_escape: 'Esc',
// fragment mode
fragment_mode: 'Fragment mode',
fragment_mode_warning: 'Fragment mode enabled',

View file

@ -52,6 +52,26 @@ module.exports = {
file_import: 'Importer',
file_export: 'Exporter',
// keyboard
keyboard: 'Clavier virtuel',
keyboard_layout: {
default: [
'{esc} \u00B2 & \u00E9 \" \' ( - \u00E8 _ \u00E7 \u00E0 ) = {bksp}',
'{tab} a z e r t y u i o p ^ $',
'{lock} q s d f g h j k l m \u00F9 * {enter}',
'{shift} < w x c v b n , ; : ! {shift}',
'{space}',
],
shift: [
'{esc} ` 1 2 3 4 5 6 7 8 9 0 \u00B0 + {bksp}',
'{tab} A Z E R T Y U I O P \u00A8 \u00A3',
'{lock} Q S D F G H J K L M % \u00B5 {enter}',
'{shift} > W X C V B N ? . / \u00A7 {shift}',
'{space}'
]
},
keyboard_escape: 'Échap',
// fullscreen
fullscreen: 'Plein écran',
fs_unnavailable: 'Plein écran indisponible',

View file

@ -1,7 +1,10 @@
var locales = {
debug: {/* empty */},
en: require('./en'),
fr: require('./fr')
fr: require('./fr'),
debug: {
/* empty */
keyboard_layout: require('./en').keyboard_layout
}
}
var lang = locales[LANG] ? LANG : 'en'

View file

@ -5,3 +5,4 @@ require('./notifications')
require('./ui-workspace')
require('./main-menu')
require('./ui-console')
require('./ui-keyboard')

View file

@ -231,6 +231,16 @@ if (navigator.userAgent.match(/Android|iPhone|iPad|iPod/i)) {
}
})
} else {
menuEntries.push({
label: locales('keyboard'),
class: ()=>{return 'toggle ' + (VIRTUAL_KEYBOARD ? 'on' : 'off')},
action: ()=>{
VIRTUAL_KEYBOARD = !VIRTUAL_KEYBOARD
}
})
}

View file

@ -0,0 +1,242 @@
var UiWidget = require('./ui-widget'),
{icon} = require('./utils'),
locales = require('../locales'),
layout = locales('keyboard_layout'),
html = require('nanohtml'),
raw = require('nanohtml/raw')
const numericLayout = [
'{sep} 7 8 9',
'{sep} 6 7 8',
'{sep} 1 2 3',
'{sep} 0 . {enter}'
]
const keysDisplay = {
'{esc}': locales('keyboard_escape'),
'{bksp}': icon('delete-left'),
'{enter}': icon('arrow-turn-down.rotate-90'),
'{enter}': icon('arrow-turn-down.rotate-90'),
'{lock}': icon('arrow-up-from-bracket'),
'{shift}': icon('arrow-up'),
'{shift}': icon('arrow-up'),
'{tab}': icon('arrow-right-arrow-left'),
'{space}': ' ',
}
class OscKeybard extends UiWidget {
constructor(options) {
super(options)
this.keys = {}
this.pressedKeys = {}
this.lock = false
this.shift = false
this.input = null
this.value = null
for (var k in layout) {
layout[k] = layout[k].map((r, i)=>(r + ' ' + (numericLayout[i]||'')).trim().split(' '))
}
var keyId = 0
for (var i = 0; i < layout.default.length; i++) {
var rowData = layout.default[i],
row = html`<div class="row"></div>`
for (var j = 0; j < rowData.length; j++) {
var keyData = rowData[j],
shiftData = layout.shift[i][j],
key = html`
<div class="key">
<span class="display-default">${raw(keysDisplay[keyData] || keyData)}</span>
<span class="display-shift">${raw(keysDisplay[shiftData] || shiftData)}</span>
</div>`
keyId++
this.keys[keyId] = key
key.appendChild = keysDisplay[keyData] || keyData
key.classList.add(keyData.replace('{', '').replace('}', ''))
key.setAttribute('data-id', keyId)
key.setAttribute('data-key', keyData)
key.setAttribute('data-shift', shiftData)
row.appendChild(key)
}
this.container.appendChild(row)
}
this.container.addEventListener('mousedown', (e)=>{
// prevent keyboard from stealing focus from input
e.preventDefault()
},{capture:true})
this.on('draginit', (e)=>{
if (this.pressedKeys[e.pointerId]) return
var keyId = e.target.getAttribute('data-id')
if (keyId === null) return
var key = this.keys[keyId]
if (key.getAttribute('data-key') === '{lock}') {
this.lock = !this.lock
key.classList.toggle('active', this.lock)
} else {
key.classList.add('active')
this.pressedKeys[e.pointerId] = key
}
this.checkShift()
this.keyDown(key.getAttribute(this.shift !== this.lock ? 'data-shift' : 'data-key'))
}, {element: this.container, multitouch: true})
this.on('dragend', (e)=>{
if (!this.pressedKeys[e.pointerId]) return
var key = this.pressedKeys[e.pointerId],
keyData = key.getAttribute('data-key')
key.classList.remove('active')
if (keyData === '{lock}') return
delete this.pressedKeys[e.pointerId]
this.checkShift()
this.keyUp(key.getAttribute(this.shift != this.lock ? 'data-shift' : 'data-key'))
}, {element: this.container, multitouch: true})
document.addEventListener('focus', (e)=>{
if (!VIRTUAL_KEYBOARD) return
// show keyboard when entering input focus
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
this.show()
this.input = e.target
this.value = this.input.value
this.input.scrollIntoView()
}
}, {capture: true})
document.addEventListener('blur', (e)=>{
// hide keyboard when leaving input focus
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') {
this.hide()
this.input = null
this.value = null
}
}, {capture: true})
}
keyDown(key) {
if (!this.input) return
var startPos = this.input.selectionStart,
endPos = this.input.selectionEnd,
editLength, str
switch (key) {
case '{lock}':
case '{shift}':
return
case '{esc}':
this.input.value = this.value
this.input.blur()
return
case '{enter}':
if (this.shift) {
str = '\n'
editLength = 1
break
} else {
this.input.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 13}))
if (this.input) DOM.dispatchEvent(this.input, 'change')
return
}
case '{bksp}':
this.input.dispatchEvent(new KeyboardEvent('keydown', {keyCode: 8}))
this.input.value = this.input.value.substring(0, startPos - 1)
+ this.input.value.substring(endPos, this.input.value.length)
editLength = -1
break
case '{tab}':
str = ' '
break
case '{space}':
str = ' '
break
default:
str = key
break
}
if (str !== undefined) {
this.input.value = this.input.value.substring(0, startPos)
+ str
+ this.input.value.substring(endPos, this.input.value.length)
if (editLength === undefined) editLength = str.length
}
this.input.selectionStart = startPos + editLength
this.input.selectionEnd = startPos + editLength
DOM.dispatchEvent(this.input, 'input')
DOM.dispatchEvent(this.input, 'keydown')
// this.input.dispatchEvent(new KeyboardEvent('keydown'))
}
keyUp(key) {
}
checkShift() {
var shift = false
for (var k in this.pressedKeys) {
var dataKey = this.pressedKeys[k].getAttribute('data-key')
if (dataKey === '{shift}') {
shift = true
}
}
this.shift = shift
this.container.classList.toggle('shift', this.lock !== this.shift)
}
show(){
this.container.style.display = 'flex'
DOM.dispatchEvent(window, 'resize')
}
hide(){
this.container.style.display = 'none'
DOM.dispatchEvent(window, 'resize')
}
}
const k = new OscKeybard({element: DOM.get('osc-keyboard')[0]})
module.exports = k

View file

@ -178,6 +178,7 @@ class Input extends Canvas {
if (this.value === '' || this.value === null) this.value = this.getProp('default')
this.stringValue = this.getStringValue(this.value)
this.batchDraw()
if (options.send && !options.fromSync) this.sendValue()

View file

@ -53,7 +53,8 @@
<osc-panel-content></osc-panel-content>
</osc-panel-inner>
</osc-panel-container>
<osc-keyboard class="simple-keyboard">
</osc-keyboard>
</osc-workspace>
<div id="notifications"></div>
</body>

View file

@ -14,3 +14,4 @@
@import 'ui/loading';
@import 'ui/toolbar';
@import 'ui/launcher';
@import 'ui/keyboard';

81
src/scss/ui/keyboard.scss Normal file
View file

@ -0,0 +1,81 @@
@import '../../../node_modules/simple-keyboard/build/css/index';
osc-keyboard {
--spacing: 4rem;
height:33%;
z-index: 100;
position: relative;
display: none;
flex-direction: column;
padding: calc(var(--spacing) / 2);
background: var(--color-ui-foreground);
border-top: 1rem solid var(--color-ui-background);
.row {
flex-direction: row;
display: flex;
flex: 1;
}
.key {
background: var(--color-ui-button);
border-color: var(--color-ui-background);
border-radius: 4rem;
border-bottom: 1rem solid var(--color-ui-background);
flex: 1;
display: inline-flex;
align-items: center;
justify-content: center;
margin: calc(var(--spacing) / 2);
span {
pointer-events: none;
align-items: center;
justify-content: center;
&.display-default {
display: flex;
}
&.display-shift {
display: none;
}
}
&.active {
background: var(--color-ui-background);
}
}
.tab, .shift {
flex: 1.5;
}
.enter, .right-shift, .lock, .bksp {
flex: 2;
}
.sep {
width:0;
visibility: hidden;
flex-shrink: 0;
flex-grow: 0;
margin-right: var(--spacing);
margin-left: var(--spacing);
}
.sep ~ .key {
flex-shrink: 0;
flex-grow: 0;
min-width: 5%;
}
&.shift {
.key {
.display-shift {
display: flex;
}
.display-default {
display: none;
}
}
}
}