app,io/pointer: [API] remove CursorNameOp and rename CursorName -> Cursor

It's now possible to directly user pointer.Cursor to add to the ops.

   pointer.CursorText.Add(gtx.Ops)

This is an API change. Use pointer.Cursor directly instead of CursorNameOp.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
This commit is contained in:
Egon Elbre
2022-02-26 12:54:42 +02:00
committed by Elias Naur
parent 4172566aad
commit cdb288d1f9
18 changed files with 57 additions and 68 deletions
+1 -1
View File
@@ -163,7 +163,7 @@ type driver interface {
// Configure the window. // Configure the window.
Configure([]Option) Configure([]Option)
// SetCursor updates the current cursor to name. // SetCursor updates the current cursor to name.
SetCursor(name pointer.CursorName) SetCursor(cursor pointer.Cursor)
// Raise the window at the top. // Raise the window at the top.
Raise() Raise()
// Close the window. // Close the window.
+4 -4
View File
@@ -1338,9 +1338,9 @@ func (w *window) Perform(system.Action) {}
func (w *window) Raise() {} func (w *window) Raise() {}
func (w *window) SetCursor(name pointer.CursorName) { func (w *window) SetCursor(cursor pointer.Cursor) {
runInJVM(javaVM(), func(env *C.JNIEnv) { runInJVM(javaVM(), func(env *C.JNIEnv) {
setCursor(env, w.view, name) setCursor(env, w.view, cursor)
}) })
} }
@@ -1379,8 +1379,8 @@ var androidCursor = [...]uint16{
pointer.CursorNorthWestSouthEastResize: 1017, // TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW pointer.CursorNorthWestSouthEastResize: 1017, // TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW
} }
func setCursor(env *C.JNIEnv, view C.jobject, name pointer.CursorName) { func setCursor(env *C.JNIEnv, view C.jobject, cursor pointer.Cursor) {
curID := androidCursor[name] curID := androidCursor[cursor]
callVoidMethod(env, view, gioView.setCursor, jvalue(curID)) callVoidMethod(env, view, gioView.setCursor, jvalue(curID))
} }
+1 -1
View File
@@ -243,7 +243,7 @@ var macosCursorID = [...]byte{
// windowSetCursor updates the cursor from the current one to a new one // windowSetCursor updates the cursor from the current one to a new one
// and returns the new one. // and returns the new one.
func windowSetCursor(from, to pointer.CursorName) pointer.CursorName { func windowSetCursor(from, to pointer.Cursor) pointer.Cursor {
if from == to { if from == to {
return to return to
} }
+3 -3
View File
@@ -97,7 +97,7 @@ type window struct {
displayLink *displayLink displayLink *displayLink
visible bool visible bool
cursor pointer.CursorName cursor pointer.Cursor
config Config config Config
pointerMap []C.CFTypeRef pointerMap []C.CFTypeRef
@@ -301,8 +301,8 @@ func (w *window) SetAnimating(anim bool) {
} }
} }
func (w *window) SetCursor(name pointer.CursorName) { func (w *window) SetCursor(cursor pointer.Cursor) {
w.cursor = windowSetCursor(w.cursor, name) w.cursor = windowSetCursor(w.cursor, cursor)
} }
func (w *window) onKeyCommand(name string) { func (w *window) onKeyCommand(name string) {
+2 -2
View File
@@ -574,9 +574,9 @@ var webCursor = [...]string{
pointer.CursorNorthWestSouthEastResize: "nwse-resize", pointer.CursorNorthWestSouthEastResize: "nwse-resize",
} }
func (w *window) SetCursor(name pointer.CursorName) { func (w *window) SetCursor(cursor pointer.Cursor) {
style := w.cnv.Get("style") style := w.cnv.Get("style")
style.Set("cursor", webCursor[name]) style.Set("cursor", webCursor[cursor])
} }
func (w *window) Wakeup() { func (w *window) Wakeup() {
+3 -3
View File
@@ -205,7 +205,7 @@ type window struct {
w *callbacks w *callbacks
stage system.Stage stage system.Stage
displayLink *displayLink displayLink *displayLink
cursor pointer.CursorName cursor pointer.Cursor
scale float32 scale float32
config Config config Config
@@ -368,8 +368,8 @@ func (w *window) setTitle(prev, cnf Config) {
func (w *window) Perform(system.Action) {} func (w *window) Perform(system.Action) {}
func (w *window) SetCursor(name pointer.CursorName) { func (w *window) SetCursor(cursor pointer.Cursor) {
w.cursor = windowSetCursor(w.cursor, name) w.cursor = windowSetCursor(w.cursor, cursor)
} }
func (w *window) EditorStateChanged(old, new editorState) { func (w *window) EditorStateChanged(old, new editorState) {
+1 -1
View File
@@ -69,7 +69,7 @@ func newWindow(window *callbacks, options []Option) error {
return errors.New("app: no window driver available") return errors.New("app: no window driver available")
} }
// xCursor contains mapping from pointer.CursorName to XCursor. // xCursor contains mapping from pointer.Cursor to XCursor.
var xCursor = [...]string{ var xCursor = [...]string{
pointer.CursorDefault: "left_ptr", pointer.CursorDefault: "left_ptr",
pointer.CursorNone: "", pointer.CursorNone: "",
+3 -3
View File
@@ -1086,17 +1086,17 @@ func (w *window) Raise() {
// https://wayland.app/protocols/xdg-shell#xdg_toplevel:request:set_minimized // https://wayland.app/protocols/xdg-shell#xdg_toplevel:request:set_minimized
} }
func (w *window) SetCursor(name pointer.CursorName) { func (w *window) SetCursor(cursor pointer.Cursor) {
ptr := w.disp.seat.pointer ptr := w.disp.seat.pointer
if ptr == nil { if ptr == nil {
return return
} }
if name == pointer.CursorNone { if cursor == pointer.CursorNone {
C.wl_pointer_set_cursor(ptr, w.serial, nil, 0, 0) C.wl_pointer_set_cursor(ptr, w.serial, nil, 0, 0)
return return
} }
xcursor := xCursor[name] xcursor := xCursor[cursor]
cname := C.CString(xcursor) cname := C.CString(xcursor)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))
c := C.wl_cursor_theme_get_cursor(w.cursor.theme, cname) c := C.wl_cursor_theme_get_cursor(w.cursor.theme, cname)
+6 -6
View File
@@ -704,8 +704,8 @@ func (w *window) writeClipboard(s string) error {
return nil return nil
} }
func (w *window) SetCursor(name pointer.CursorName) { func (w *window) SetCursor(cursor pointer.Cursor) {
c, err := loadCursor(name) c, err := loadCursor(cursor)
if err != nil { if err != nil {
c = resources.cursor c = resources.cursor
} }
@@ -715,7 +715,7 @@ func (w *window) SetCursor(name pointer.CursorName) {
} }
} }
// windowsCursor contains mapping from CursorName to an IDC. // windowsCursor contains mapping from pointer.Cursor to an IDC.
var windowsCursor = [...]uint16{ var windowsCursor = [...]uint16{
pointer.CursorDefault: windows.IDC_ARROW, pointer.CursorDefault: windows.IDC_ARROW,
pointer.CursorNone: 0, pointer.CursorNone: 0,
@@ -745,14 +745,14 @@ var windowsCursor = [...]uint16{
pointer.CursorNorthWestSouthEastResize: windows.IDC_SIZENWSE, pointer.CursorNorthWestSouthEastResize: windows.IDC_SIZENWSE,
} }
func loadCursor(name pointer.CursorName) (syscall.Handle, error) { func loadCursor(cursor pointer.Cursor) (syscall.Handle, error) {
switch name { switch cursor {
case pointer.CursorDefault: case pointer.CursorDefault:
return resources.cursor, nil return resources.cursor, nil
case pointer.CursorNone: case pointer.CursorNone:
return 0, nil return 0, nil
default: default:
return windows.LoadCursor(windowsCursor[name]) return windows.LoadCursor(windowsCursor[cursor])
} }
} }
+8 -8
View File
@@ -107,7 +107,7 @@ type x11Window struct {
clipboard struct { clipboard struct {
content []byte content []byte
} }
cursor pointer.CursorName cursor pointer.Cursor
config Config config Config
wakeups chan struct{} wakeups chan struct{}
@@ -295,22 +295,22 @@ func (w *x11Window) Raise() {
C.XMapRaised(w.display(), w.xw) C.XMapRaised(w.display(), w.xw)
} }
func (w *x11Window) SetCursor(name pointer.CursorName) { func (w *x11Window) SetCursor(cursor pointer.Cursor) {
if name == pointer.CursorNone { if cursor == pointer.CursorNone {
w.cursor = name w.cursor = cursor
C.XFixesHideCursor(w.x, w.xw) C.XFixesHideCursor(w.x, w.xw)
return return
} }
xcursor := xCursor[name] xcursor := xCursor[cursor]
cname := C.CString(xcursor) cname := C.CString(xcursor)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))
c := C.XcursorLibraryLoadCursor(w.x, cname) c := C.XcursorLibraryLoadCursor(w.x, cname)
if c == 0 { if c == 0 {
name = pointer.CursorDefault cursor = pointer.CursorDefault
} }
w.cursor = name w.cursor = cursor
// If c if null (i.e. name was not found), // If c if null (i.e. cursor was not found),
// XDefineCursor will use the default cursor. // XDefineCursor will use the default cursor.
C.XDefineCursor(w.x, w.xw, c) C.XDefineCursor(w.x, w.xw, c)
} }
+1 -8
View File
@@ -69,7 +69,7 @@ type Window struct {
delayedDraw *time.Timer delayedDraw *time.Timer
queue queue queue queue
cursor pointer.CursorName cursor pointer.Cursor
decorations struct { decorations struct {
op.Ops op.Ops
Config Config
@@ -334,13 +334,6 @@ func (w *Window) WriteClipboard(s string) {
}) })
} }
// SetCursorName changes the current window cursor to name.
func (w *Window) SetCursorName(name pointer.CursorName) {
w.driverDefer(func(d driver) {
d.SetCursor(name)
})
}
// Run f in the same thread as the native window event loop, and wait for f to // Run f in the same thread as the native window event loop, and wait for f to
// return or the window to close. Run is guaranteed not to deadlock if it is // return or the window to close. Run is guaranteed not to deadlock if it is
// invoked during the handling of a ViewEvent, system.FrameEvent, // invoked during the handling of a ViewEvent, system.FrameEvent,
+7 -11
View File
@@ -54,11 +54,6 @@ type PassStack struct {
macroID int macroID int
} }
// CursorNameOp sets the cursor for the current area.
type CursorNameOp struct {
Name CursorName
}
// InputOp declares an input handler ready for pointer // InputOp declares an input handler ready for pointer
// events. // events.
type InputOp struct { type InputOp struct {
@@ -90,13 +85,14 @@ type Source uint8
// Buttons is a set of mouse buttons // Buttons is a set of mouse buttons
type Buttons uint8 type Buttons uint8
// CursorName is the name of a cursor. // Cursor denotes a pre-defined cursor shape. Its Add method adds an
type CursorName byte // operation that sets the cursor shape for the current clip area.
type Cursor byte
// The cursors correspond to CSS pointer naming. // The cursors correspond to CSS pointer naming.
const ( const (
// CursorDefault is the default cursor. // CursorDefault is the default cursor.
CursorDefault CursorName = iota CursorDefault Cursor = iota
// CursorNone hides the cursor. To show it again, use any other cursor. // CursorNone hides the cursor. To show it again, use any other cursor.
CursorNone CursorNone
// CursorText is for selecting and inserting text. // CursorText is for selecting and inserting text.
@@ -247,10 +243,10 @@ func (p PassStack) Pop() {
data[0] = byte(ops.TypePopPass) data[0] = byte(ops.TypePopPass)
} }
func (op CursorNameOp) Add(o *op.Ops) { func (op Cursor) Add(o *op.Ops) {
data := ops.Write(&o.Internal, ops.TypeCursorLen) data := ops.Write(&o.Internal, ops.TypeCursorLen)
data[0] = byte(ops.TypeCursor) data[0] = byte(ops.TypeCursor)
data[1] = byte(op.Name) data[1] = byte(op)
} }
// Add panics if the scroll range does not contain zero. // Add panics if the scroll range does not contain zero.
@@ -360,7 +356,7 @@ func (b Buttons) String() string {
return strings.Join(strs, "|") return strings.Join(strs, "|")
} }
func (c CursorName) String() string { func (c Cursor) String() string {
switch c { switch c {
case CursorDefault: case CursorDefault:
return "Default" return "Default"
+5 -5
View File
@@ -18,7 +18,7 @@ type pointerQueue struct {
hitTree []hitNode hitTree []hitNode
areas []areaNode areas []areaNode
cursors []cursorNode cursors []cursorNode
cursor pointer.CursorName cursor pointer.Cursor
handlers map[event.Tag]*pointerHandler handlers map[event.Tag]*pointerHandler
pointers []pointerInfo pointers []pointerInfo
transfers []io.ReadCloser // pending data transfers transfers []io.ReadCloser // pending data transfers
@@ -45,7 +45,7 @@ type hitNode struct {
} }
type cursorNode struct { type cursorNode struct {
name pointer.CursorName cursor pointer.Cursor
area int area int
} }
@@ -306,9 +306,9 @@ func (c *pointerCollector) semanticDisabled(disabled bool) {
area.semantic.content.disabled = disabled area.semantic.content.disabled = disabled
} }
func (c *pointerCollector) cursor(name pointer.CursorName) { func (c *pointerCollector) cursor(cursor pointer.Cursor) {
c.q.cursors = append(c.q.cursors, cursorNode{ c.q.cursors = append(c.q.cursors, cursorNode{
name: name, cursor: cursor,
area: len(c.q.areas) - 1, area: len(c.q.areas) - 1,
}) })
} }
@@ -725,7 +725,7 @@ func (q *pointerQueue) deliverEnterLeaveEvents(p *pointerInfo, events *handlerEv
h := q.handlers[k] h := q.handlers[k]
for i := len(q.cursors) - 1; i >= 0; i-- { for i := len(q.cursors) - 1; i >= 0; i-- {
if c := q.cursors[i]; c.area == h.area { if c := q.cursors[i]; c.area == h.area {
q.cursor = c.name q.cursor = c.cursor
break break
} }
} }
+4 -4
View File
@@ -572,7 +572,7 @@ func TestMultitouch(t *testing.T) {
assertEventPointerTypeSequence(t, r.Events(h2), pointer.Cancel, pointer.Enter, pointer.Press, pointer.Release) assertEventPointerTypeSequence(t, r.Events(h2), pointer.Cancel, pointer.Enter, pointer.Press, pointer.Release)
} }
func TestCursorNameOp(t *testing.T) { func TestCursor(t *testing.T) {
ops := new(op.Ops) ops := new(op.Ops)
var r Router var r Router
var h, h2 int var h, h2 int
@@ -582,7 +582,7 @@ func TestCursorNameOp(t *testing.T) {
defer clip.Rect(image.Rectangle{Max: image.Pt(100, 100)}).Push(ops).Pop() defer clip.Rect(image.Rectangle{Max: image.Pt(100, 100)}).Push(ops).Pop()
// The cursor is checked and changed upon cursor movement. // The cursor is checked and changed upon cursor movement.
pointer.InputOp{Tag: &h}.Add(ops) pointer.InputOp{Tag: &h}.Add(ops)
pointer.CursorNameOp{Name: pointer.CursorPointer}.Add(ops) pointer.CursorPointer.Add(ops)
if widget2 != nil { if widget2 != nil {
widget2() widget2()
} }
@@ -605,7 +605,7 @@ func TestCursorNameOp(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
label string label string
event interface{} event interface{}
want pointer.CursorName want pointer.Cursor
}{ }{
{label: "move inside", {label: "move inside",
event: _at(50, 50), event: _at(50, 50),
@@ -638,7 +638,7 @@ func TestCursorNameOp(t *testing.T) {
event: func() []event.Event { event: func() []event.Event {
widget2 = func() { widget2 = func() {
pointer.InputOp{Tag: &h2}.Add(ops) pointer.InputOp{Tag: &h2}.Add(ops)
pointer.CursorNameOp{Name: pointer.CursorCrosshair}.Add(ops) pointer.CursorCrosshair.Add(ops)
} }
return []event.Event{ return []event.Event{
_at(50, 50), _at(50, 50),
+2 -2
View File
@@ -193,7 +193,7 @@ func (q *Router) ReadClipboard() bool {
} }
// Cursor returns the last cursor set. // Cursor returns the last cursor set.
func (q *Router) Cursor() pointer.CursorName { func (q *Router) Cursor() pointer.Cursor {
return q.pointer.queue.cursor return q.pointer.queue.cursor
} }
@@ -298,7 +298,7 @@ func (q *Router) collect() {
} }
pc.inputOp(op, &q.handlers) pc.inputOp(op, &q.handlers)
case ops.TypeCursor: case ops.TypeCursor:
name := pointer.CursorName(encOp.Data[1]) name := pointer.Cursor(encOp.Data[1])
pc.cursor(name) pc.cursor(name)
case ops.TypeSource: case ops.TypeSource:
op := transfer.SourceOp{ op := transfer.SourceOp{
+2 -2
View File
@@ -49,10 +49,10 @@ const (
ActionResizeSouthEast ActionResizeSouthEast
) )
// CursorName returns the cursor for the action. // Cursor returns the cursor for the action.
// It must be a single action otherwise the default // It must be a single action otherwise the default
// cursor is returned. // cursor is returned.
func (a Action) CursorName() pointer.CursorName { func (a Action) Cursor() pointer.Cursor {
switch a { switch a {
case ActionResizeNorthWest: case ActionResizeNorthWest:
return pointer.CursorNorthWestResize return pointer.CursorNorthWestResize
+1 -1
View File
@@ -93,7 +93,7 @@ func (d *Decorations) LayoutResize(gtx layout.Context, actions system.Action) {
} }
st := clip.Rect(data.Rectangle).Push(gtx.Ops) st := clip.Rect(data.Rectangle).Push(gtx.Ops)
if rsz.Hover.Hovered(gtx) { if rsz.Hover.Hovered(gtx) {
pointer.CursorNameOp{Name: action.CursorName()}.Add(gtx.Ops) action.Cursor().Add(gtx.Ops)
} }
rsz.Drag.Add(gtx.Ops) rsz.Drag.Add(gtx.Ops)
pass := pointer.PassOp{}.Push(gtx.Ops) pass := pointer.PassOp{}.Push(gtx.Ops)
+1 -1
View File
@@ -626,7 +626,7 @@ func (e *Editor) layout(gtx layout.Context, content layout.Widget) layout.Dimens
} }
e.requestFocus = false e.requestFocus = false
defer clip.Rect(image.Rectangle{Max: e.viewSize}).Push(gtx.Ops).Pop() defer clip.Rect(image.Rectangle{Max: e.viewSize}).Push(gtx.Ops).Pop()
pointer.CursorNameOp{Name: pointer.CursorText}.Add(gtx.Ops) pointer.CursorText.Add(gtx.Ops)
var scrollRange image.Rectangle var scrollRange image.Rectangle
if e.SingleLine { if e.SingleLine {