mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
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:
@@ -18,7 +18,7 @@ static void handleMouse(NSView *view, NSEvent *event, int typ, CGFloat dx, CGFlo
|
|||||||
dx *= 10;
|
dx *= 10;
|
||||||
dy *= 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) {
|
static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext) {
|
||||||
|
|||||||
@@ -118,24 +118,14 @@ func gio_onFrameCallback(view C.CFTypeRef) {
|
|||||||
//export gio_onKeys
|
//export gio_onKeys
|
||||||
func gio_onKeys(view C.CFTypeRef, cstr *C.char, ti C.double, mods C.NSUInteger) {
|
func gio_onKeys(view C.CFTypeRef, cstr *C.char, ti C.double, mods C.NSUInteger) {
|
||||||
str := C.GoString(cstr)
|
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) {
|
viewDo(view, func(views viewMap, view C.CFTypeRef) {
|
||||||
w := views[view]
|
w := views[view]
|
||||||
for _, k := range str {
|
for _, k := range str {
|
||||||
if n, ok := convertKey(k); ok {
|
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
|
//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
|
var typ pointer.Type
|
||||||
switch cdir {
|
switch cdir {
|
||||||
case C.GIO_MOUSE_MOVE:
|
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
|
x, y := float32(x)*w.scale, float32(y)*w.scale
|
||||||
dx, dy := float32(dx)*w.scale, float32(dy)*w.scale
|
dx, dy := float32(dx)*w.scale, float32(dy)*w.scale
|
||||||
w.w.Event(pointer.Event{
|
w.w.Event(pointer.Event{
|
||||||
Type: typ,
|
Type: typ,
|
||||||
Source: pointer.Mouse,
|
Source: pointer.Mouse,
|
||||||
Time: t,
|
Time: t,
|
||||||
Buttons: btns,
|
Buttons: btns,
|
||||||
Position: f32.Point{X: x, Y: y},
|
Position: f32.Point{X: x, Y: y},
|
||||||
Scroll: f32.Point{X: dx, Y: dy},
|
Scroll: f32.Point{X: dx, Y: dy},
|
||||||
|
Modifiers: convertMods(mods),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -366,3 +357,20 @@ func convertKey(k rune) (string, bool) {
|
|||||||
}
|
}
|
||||||
return n, true
|
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
@@ -16,6 +16,7 @@ import (
|
|||||||
"gioui.org/f32"
|
"gioui.org/f32"
|
||||||
"gioui.org/internal/fling"
|
"gioui.org/internal/fling"
|
||||||
"gioui.org/io/event"
|
"gioui.org/io/event"
|
||||||
|
"gioui.org/io/key"
|
||||||
"gioui.org/io/pointer"
|
"gioui.org/io/pointer"
|
||||||
"gioui.org/op"
|
"gioui.org/op"
|
||||||
"gioui.org/unit"
|
"gioui.org/unit"
|
||||||
@@ -34,9 +35,10 @@ type ClickState uint8
|
|||||||
// TypePress for the beginning of a click or a
|
// TypePress for the beginning of a click or a
|
||||||
// TypeClick for a completed click.
|
// TypeClick for a completed click.
|
||||||
type ClickEvent struct {
|
type ClickEvent struct {
|
||||||
Type ClickType
|
Type ClickType
|
||||||
Position f32.Point
|
Position f32.Point
|
||||||
Source pointer.Source
|
Source pointer.Source
|
||||||
|
Modifiers key.Modifiers
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClickType uint8
|
type ClickType uint8
|
||||||
@@ -120,7 +122,7 @@ func (c *Click) Events(q event.Queue) []ClickEvent {
|
|||||||
wasPressed := c.state == StatePressed
|
wasPressed := c.state == StatePressed
|
||||||
c.state = StateNormal
|
c.state = StateNormal
|
||||||
if wasPressed {
|
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:
|
case pointer.Cancel:
|
||||||
c.state = StateNormal
|
c.state = StateNormal
|
||||||
@@ -132,7 +134,7 @@ func (c *Click) Events(q event.Queue) []ClickEvent {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
c.state = StatePressed
|
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:
|
case pointer.Move:
|
||||||
if c.state == StatePressed && !e.Hit {
|
if c.state == StatePressed && !e.Hit {
|
||||||
c.state = StateNormal
|
c.state = StateNormal
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"gioui.org/f32"
|
"gioui.org/f32"
|
||||||
"gioui.org/internal/opconst"
|
"gioui.org/internal/opconst"
|
||||||
"gioui.org/io/event"
|
"gioui.org/io/event"
|
||||||
|
"gioui.org/io/key"
|
||||||
"gioui.org/op"
|
"gioui.org/op"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,6 +41,9 @@ type Event struct {
|
|||||||
Position f32.Point
|
Position f32.Point
|
||||||
// Scroll is the scroll amount, if any.
|
// Scroll is the scroll amount, if any.
|
||||||
Scroll f32.Point
|
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
|
// AreaOp updates the hit area to the intersection of the current
|
||||||
|
|||||||
Reference in New Issue
Block a user