Skip to content

Commit

Permalink
Refactor key handling per design document
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Feb 15, 2019
2 parents 076e8bf + 2c6c55d commit 145de86
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 265 deletions.
6 changes: 4 additions & 2 deletions canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Canvas interface {
Scale() float32
SetScale(float32)

OnKeyDown() func(*KeyEvent)
SetOnKeyDown(func(*KeyEvent))
OnTypedRune() func(rune)
SetOnTypedRune(func(rune))
OnTypedKey() func(*KeyEvent)
SetOnTypedKey(func(*KeyEvent))
}
12 changes: 7 additions & 5 deletions canvasobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ type ScrollableObject interface {
}

// FocusableObject describes any CanvasObject that can respond to being focused.
// It will receive the OnFocusGained and OnFocusLost events appropriately and,
// when focused, it will also have OnKeyDown called as keys are pressed.
// It will receive the FocusGained and FocusLost events appropriately.
// When focused it will also have TypedRune called as text is input and
// TypedKey called when other keys are pressed.
type FocusableObject interface {
OnFocusGained()
OnFocusLost()
FocusGained()
FocusLost()
Focused() bool

OnKeyDown(*KeyEvent)
TypedRune(rune)
TypedKey(*KeyEvent)
}
23 changes: 16 additions & 7 deletions driver/efl/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ type eflCanvas struct {
window *window
focused fyne.FocusableObject

onKeyDown func(*fyne.KeyEvent)
onTypedRune func(rune)
onTypedKey func(*fyne.KeyEvent)

objects map[*C.Evas_Object]fyne.CanvasObject
native map[fyne.CanvasObject]*C.Evas_Object
Expand Down Expand Up @@ -522,11 +523,11 @@ func (c *eflCanvas) Focus(obj fyne.FocusableObject) {
return
}

c.focused.OnFocusLost()
c.focused.FocusLost()
}

c.focused = obj
obj.OnFocusGained()
obj.FocusGained()
}

func (c *eflCanvas) Focused() fyne.FocusableObject {
Expand Down Expand Up @@ -628,10 +629,18 @@ func (c *eflCanvas) SetScale(scale float32) {
C.ecore_evas_resize(c.window.ee, C.int(width), C.int(height))
}

func (c *eflCanvas) OnKeyDown() func(*fyne.KeyEvent) {
return c.onKeyDown
func (c *eflCanvas) OnTypedRune() func(rune) {
return c.onTypedRune
}

func (c *eflCanvas) SetOnKeyDown(keyDown func(*fyne.KeyEvent)) {
c.onKeyDown = keyDown
func (c *eflCanvas) SetOnTypedRune(typed func(rune)) {
c.onTypedRune = typed
}

func (c *eflCanvas) OnTypedKey() func(*fyne.KeyEvent) {
return c.onTypedKey
}

func (c *eflCanvas) SetOnTypedKey(typed func(*fyne.KeyEvent)) {
c.onTypedKey = typed
}
57 changes: 20 additions & 37 deletions driver/efl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func onWindowFocusGained(ee *C.Ecore_Evas) {
}

if canFocus, ok := w.canvas.(*eflCanvas); ok && canFocus.focused != nil {
canFocus.focused.OnFocusGained()
canFocus.focused.FocusGained()
}
}

Expand All @@ -282,7 +282,7 @@ func onWindowFocusLost(ee *C.Ecore_Evas) {
}

if canFocus, ok := w.canvas.(*eflCanvas); ok && canFocus.focused != nil {
canFocus.focused.OnFocusLost()
canFocus.focused.FocusLost()
}
}

Expand Down Expand Up @@ -312,49 +312,32 @@ func onWindowKeyDown(ew C.Ecore_Window, info *C.Ecore_Event_Key) {
}
canvas := w.canvas.(*eflCanvas)

if canvas.focused == nil && canvas.onKeyDown == nil {
if canvas.focused == nil && canvas.onTypedRune == nil && canvas.onTypedKey == nil {
return
}

ev := new(fyne.KeyEvent)
ev.String = C.GoString(info.string)
switch C.GoString(info.keyname) {
case "Shift_L":
fallthrough
case "Shift_R":
ev.Name = fyne.KeyShift
case "Control_L":
fallthrough
case "Control_R":
ev.Name = fyne.KeyControl
case "Alt_L":
fallthrough
case "Alt_R":
ev.Name = fyne.KeyAlt
case "Super_L":
fallthrough
case "Super_R":
ev.Name = fyne.KeySuper

default:
ev.Name = fyne.KeyName(C.GoString(info.keyname))
}

if (info.modifiers & C.ECORE_EVENT_MODIFIER_SHIFT) != 0 {
ev.Modifiers |= fyne.ShiftModifier
}
if (info.modifiers & C.ECORE_EVENT_MODIFIER_CTRL) != 0 {
ev.Modifiers |= fyne.ControlModifier
}
if (info.modifiers & C.ECORE_EVENT_MODIFIER_ALT) != 0 {
ev.Modifiers |= fyne.AltModifier
str := C.GoString(info.string)
if str != "" && []rune(str)[0] < ' ' {
str = ""
}
ev.Name = fyne.KeyName(C.GoString(info.keyname))

if canvas.focused != nil {
canvas.focused.OnKeyDown(ev)
if str != "" {
canvas.focused.TypedRune([]rune(str)[0])
} else {
canvas.focused.TypedKey(ev)
}
}
if canvas.onKeyDown != nil {
canvas.onKeyDown(ev)
if str != "" {
if canvas.onTypedRune != nil {
canvas.onTypedRune([]rune(str)[0])
}
} else {
if canvas.onTypedKey != nil {
canvas.onTypedKey(ev)
}
}
}

Expand Down
23 changes: 16 additions & 7 deletions driver/gl/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type glCanvas struct {
content fyne.CanvasObject
focused fyne.FocusableObject

onKeyDown func(*fyne.KeyEvent)
onTypedRune func(rune)
onTypedKey func(*fyne.KeyEvent)

program uint32
scale float32
Expand Down Expand Up @@ -73,11 +74,11 @@ func (c *glCanvas) Refresh(obj fyne.CanvasObject) {

func (c *glCanvas) Focus(obj fyne.FocusableObject) {
if c.focused != nil {
c.focused.(fyne.FocusableObject).OnFocusLost()
c.focused.(fyne.FocusableObject).FocusLost()
}

c.focused = obj
obj.OnFocusGained()
obj.FocusGained()
}

func (c *glCanvas) Focused() fyne.FocusableObject {
Expand All @@ -102,12 +103,20 @@ func (c *glCanvas) SetScale(scale float32) {
c.setDirty()
}

func (c *glCanvas) OnKeyDown() func(*fyne.KeyEvent) {
return c.onKeyDown
func (c *glCanvas) OnTypedRune() func(rune) {
return c.onTypedRune
}

func (c *glCanvas) SetOnKeyDown(keyDown func(*fyne.KeyEvent)) {
c.onKeyDown = keyDown
func (c *glCanvas) SetOnTypedRune(typed func(rune)) {
c.onTypedRune = typed
}

func (c *glCanvas) OnTypedKey() func(*fyne.KeyEvent) {
return c.onTypedKey
}

func (c *glCanvas) SetOnTypedKey(typed func(*fyne.KeyEvent)) {
c.onTypedKey = typed
}

func (c *glCanvas) paint(size fyne.Size) {
Expand Down
70 changes: 8 additions & 62 deletions driver/gl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,6 @@ func (w *window) mouseScrolled(viewport *glfw.Window, xoff float64, yoff float64

func keyToName(key glfw.Key) fyne.KeyName {
switch key {
// printable
case glfw.KeySpace:
return fyne.KeySpace

// non-printable
case glfw.KeyEscape:
return fyne.KeyEscape
Expand Down Expand Up @@ -480,42 +476,14 @@ func keyToName(key glfw.Key) fyne.KeyName {
case glfw.KeyF12:
return fyne.KeyF12

case glfw.KeyLeftShift:
fallthrough
case glfw.KeyRightShift:
return fyne.KeyShift
case glfw.KeyLeftControl:
fallthrough
case glfw.KeyRightControl:
return fyne.KeyControl
case glfw.KeyLeftAlt:
fallthrough
case glfw.KeyRightAlt:
return fyne.KeyAlt
case glfw.KeyLeftSuper:
fallthrough
case glfw.KeyRightSuper:
return fyne.KeySuper
case glfw.KeyMenu:
return fyne.KeyMenu

case glfw.KeyKPEnter:
return fyne.KeyEnter
}
return ""
}

func charToName(char rune) fyne.KeyName {
switch char {
case ' ':
return fyne.KeySpace

}
return ""
}

func (w *window) keyPressed(viewport *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if w.canvas.Focused() == nil && w.canvas.onKeyDown == nil {
if w.canvas.Focused() == nil && w.canvas.onTypedKey == nil {
return
}
if action != glfw.Press { // ignore key up
Expand All @@ -528,47 +496,25 @@ func (w *window) keyPressed(viewport *glfw.Window, key glfw.Key, scancode int, a

ev := new(fyne.KeyEvent)
ev.Name = keyToName(key)
if (mods & glfw.ModShift) != 0 {
ev.Modifiers |= fyne.ShiftModifier
}
if (mods & glfw.ModControl) != 0 {
ev.Modifiers |= fyne.ControlModifier
}
if (mods & glfw.ModAlt) != 0 {
ev.Modifiers |= fyne.AltModifier
}

if w.canvas.Focused() != nil {
go w.canvas.Focused().OnKeyDown(ev)
go w.canvas.Focused().TypedKey(ev)
}
if w.canvas.onKeyDown != nil {
go w.canvas.onKeyDown(ev)
if w.canvas.onTypedKey != nil {
go w.canvas.onTypedKey(ev)
}
}

func (w *window) charModInput(viewport *glfw.Window, char rune, mods glfw.ModifierKey) {
if w.canvas.Focused() == nil && w.canvas.onKeyDown == nil {
if w.canvas.Focused() == nil && w.canvas.onTypedRune == nil {
return
}

ev := new(fyne.KeyEvent)
ev.Name = charToName(char)
ev.String = string(char)
if (mods & glfw.ModShift) != 0 {
ev.Modifiers |= fyne.ShiftModifier
}
if (mods & glfw.ModControl) != 0 {
ev.Modifiers |= fyne.ControlModifier
}
if (mods & glfw.ModAlt) != 0 {
ev.Modifiers |= fyne.AltModifier
}

if w.canvas.Focused() != nil {
w.canvas.Focused().OnKeyDown(ev)
w.canvas.Focused().TypedRune(char)
}
if w.canvas.onKeyDown != nil {
w.canvas.onKeyDown(ev)
if w.canvas.onTypedRune != nil {
w.canvas.onTypedRune(char)
}
}

Expand Down
5 changes: 1 addition & 4 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package fyne

// KeyEvent describes a keyboard input event.
type KeyEvent struct {
String string

Name KeyName
Modifiers Modifier
Name KeyName
}

// PointEvent describes a pointer input event. The position is relative to the top-left
Expand Down
Loading

0 comments on commit 145de86

Please sign in to comment.