app/internal/window: [wasm] report key modifiers for pointer events

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-27 17:12:11 +02:00
parent 6a0da04598
commit 266b01bb5d
+37 -15
View File
@@ -220,20 +220,30 @@ func (w *window) focus() {
func (w *window) keyEvent(e js.Value) { func (w *window) keyEvent(e js.Value) {
k := e.Get("key").String() k := e.Get("key").String()
if n, ok := translateKey(k); ok { if n, ok := translateKey(k); ok {
cmd := key.Event{Name: n} cmd := key.Event{
if e.Call("getModifierState", "Alt").Bool() { Name: n,
cmd.Modifiers |= key.ModAlt Modifiers: modifiersFor(e),
}
if e.Call("getModifierState", "Control").Bool() {
cmd.Modifiers |= key.ModCtrl
}
if e.Call("getModifierState", "Shift").Bool() {
cmd.Modifiers |= key.ModShift
} }
w.w.Event(cmd) w.w.Event(cmd)
} }
} }
// modifiersFor returns the modifier set for a DOM MouseEvent or
// KeyEvent.
func modifiersFor(e js.Value) key.Modifiers {
var mods key.Modifiers
if e.Call("getModifierState", "Alt").Bool() {
mods |= key.ModAlt
}
if e.Call("getModifierState", "Control").Bool() {
mods |= key.ModCtrl
}
if e.Call("getModifierState", "Shift").Bool() {
mods |= key.ModShift
}
return mods
}
func (w *window) touchEvent(typ pointer.Type, e js.Value) { func (w *window) touchEvent(typ pointer.Type, e js.Value) {
e.Call("preventDefault") e.Call("preventDefault")
t := time.Duration(e.Get("timeStamp").Int()) * time.Millisecond t := time.Duration(e.Get("timeStamp").Int()) * time.Millisecond
@@ -243,6 +253,16 @@ func (w *window) touchEvent(typ pointer.Type, e js.Value) {
w.mu.Lock() w.mu.Lock()
scale := w.scale scale := w.scale
w.mu.Unlock() w.mu.Unlock()
var mods key.Modifiers
if e.Get("shiftKey").Bool() {
mods |= key.ModShift
}
if e.Get("altKey").Bool() {
mods |= key.ModAlt
}
if e.Get("ctrlKey").Bool() {
mods |= key.ModCtrl
}
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
touch := changedTouches.Index(i) touch := changedTouches.Index(i)
pid := w.touchIDFor(touch) pid := w.touchIDFor(touch)
@@ -259,6 +279,7 @@ func (w *window) touchEvent(typ pointer.Type, e js.Value) {
Position: pos, Position: pos,
PointerID: pid, PointerID: pid,
Time: t, Time: t,
Modifiers: modifiersFor(e),
}) })
} }
} }
@@ -305,12 +326,13 @@ func (w *window) pointerEvent(typ pointer.Type, dx, dy float32, e js.Value) {
btns |= pointer.ButtonMiddle btns |= pointer.ButtonMiddle
} }
w.w.Event(pointer.Event{ w.w.Event(pointer.Event{
Type: typ, Type: typ,
Source: pointer.Mouse, Source: pointer.Mouse,
Buttons: btns, Buttons: btns,
Position: pos, Position: pos,
Scroll: scroll, Scroll: scroll,
Time: t, Time: t,
Modifiers: modifiersFor(e),
}) })
} }