several: add modifiers to mouse events and clicks

macOS only, for the os-specific bits.

Signed-off-by: Larry Clapp <larry@theclapp.org>
This commit is contained in:
Larry Clapp
2019-11-18 08:11:37 -05:00
committed by Elias Naur
parent 26f34a3331
commit f8de7545f4
4 changed files with 41 additions and 27 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ static void handleMouse(NSView *view, NSEvent *event, int typ, CGFloat dx, CGFlo
dx *= 10;
dy *= 10;
}
gio_onMouse((__bridge CFTypeRef)view, typ, [NSEvent pressedMouseButtons], p.x, p.y, dx, dy, [event timestamp]);
gio_onMouse((__bridge CFTypeRef)view, typ, [NSEvent pressedMouseButtons], p.x, p.y, dx, dy, [event timestamp], [event modifierFlags]);
}
static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext) {
+29 -21
View File
@@ -118,24 +118,14 @@ func gio_onFrameCallback(view C.CFTypeRef) {
//export gio_onKeys
func gio_onKeys(view C.CFTypeRef, cstr *C.char, ti C.double, mods C.NSUInteger) {
str := C.GoString(cstr)
var kmods key.Modifiers
if mods&C.NSAlternateKeyMask != 0 {
kmods |= key.ModAlt
}
if mods&C.NSControlKeyMask != 0 {
kmods |= key.ModCtrl
}
if mods&C.NSCommandKeyMask != 0 {
kmods |= key.ModCommand
}
if mods&C.NSShiftKeyMask != 0 {
kmods |= key.ModShift
}
viewDo(view, func(views viewMap, view C.CFTypeRef) {
w := views[view]
for _, k := range str {
if n, ok := convertKey(k); ok {
w.w.Event(key.Event{Name: n, Modifiers: kmods})
w.w.Event(key.Event{
Name: n,
Modifiers: convertMods(mods),
})
}
}
})
@@ -151,7 +141,7 @@ func gio_onText(view C.CFTypeRef, cstr *C.char) {
}
//export gio_onMouse
func gio_onMouse(view C.CFTypeRef, cdir C.int, cbtns C.NSUInteger, x, y, dx, dy C.CGFloat, ti C.double) {
func gio_onMouse(view C.CFTypeRef, cdir C.int, cbtns C.NSUInteger, x, y, dx, dy C.CGFloat, ti C.double, mods C.NSUInteger) {
var typ pointer.Type
switch cdir {
case C.GIO_MOUSE_MOVE:
@@ -179,12 +169,13 @@ func gio_onMouse(view C.CFTypeRef, cdir C.int, cbtns C.NSUInteger, x, y, dx, dy
x, y := float32(x)*w.scale, float32(y)*w.scale
dx, dy := float32(dx)*w.scale, float32(dy)*w.scale
w.w.Event(pointer.Event{
Type: typ,
Source: pointer.Mouse,
Time: t,
Buttons: btns,
Position: f32.Point{X: x, Y: y},
Scroll: f32.Point{X: dx, Y: dy},
Type: typ,
Source: pointer.Mouse,
Time: t,
Buttons: btns,
Position: f32.Point{X: x, Y: y},
Scroll: f32.Point{X: dx, Y: dy},
Modifiers: convertMods(mods),
})
})
}
@@ -366,3 +357,20 @@ func convertKey(k rune) (string, bool) {
}
return n, true
}
func convertMods(mods C.NSUInteger) key.Modifiers {
var kmods key.Modifiers
if mods&C.NSAlternateKeyMask != 0 {
kmods |= key.ModAlt
}
if mods&C.NSControlKeyMask != 0 {
kmods |= key.ModCtrl
}
if mods&C.NSCommandKeyMask != 0 {
kmods |= key.ModCommand
}
if mods&C.NSShiftKeyMask != 0 {
kmods |= key.ModShift
}
return kmods
}
+7 -5
View File
@@ -16,6 +16,7 @@ import (
"gioui.org/f32"
"gioui.org/internal/fling"
"gioui.org/io/event"
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/op"
"gioui.org/unit"
@@ -34,9 +35,10 @@ type ClickState uint8
// TypePress for the beginning of a click or a
// TypeClick for a completed click.
type ClickEvent struct {
Type ClickType
Position f32.Point
Source pointer.Source
Type ClickType
Position f32.Point
Source pointer.Source
Modifiers key.Modifiers
}
type ClickType uint8
@@ -120,7 +122,7 @@ func (c *Click) Events(q event.Queue) []ClickEvent {
wasPressed := c.state == StatePressed
c.state = StateNormal
if wasPressed {
events = append(events, ClickEvent{Type: TypeClick, Position: e.Position, Source: e.Source})
events = append(events, ClickEvent{Type: TypeClick, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers})
}
case pointer.Cancel:
c.state = StateNormal
@@ -132,7 +134,7 @@ func (c *Click) Events(q event.Queue) []ClickEvent {
break
}
c.state = StatePressed
events = append(events, ClickEvent{Type: TypePress, Position: e.Position, Source: e.Source})
events = append(events, ClickEvent{Type: TypePress, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers})
case pointer.Move:
if c.state == StatePressed && !e.Hit {
c.state = StateNormal
+4
View File
@@ -11,6 +11,7 @@ import (
"gioui.org/f32"
"gioui.org/internal/opconst"
"gioui.org/io/event"
"gioui.org/io/key"
"gioui.org/op"
)
@@ -40,6 +41,9 @@ type Event struct {
Position f32.Point
// Scroll is the scroll amount, if any.
Scroll f32.Point
// Modifiers is the set of active modifiers when
// the mouse button was pressed.
Modifiers key.Modifiers
}
// AreaOp updates the hit area to the intersection of the current