Skip to content

Commit

Permalink
Add keyboard handling code and refactor input handlers
Browse files Browse the repository at this point in the history
Canvas gets the keyboard handler (top level)
CanvasObjects can now be clickable
Buttons refactored to be 'tappable' as the high level API should not care about event types
  • Loading branch information
andydotxyz committed May 15, 2018
1 parent 5853f6b commit f78d89c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
2 changes: 2 additions & 0 deletions ui/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type Canvas interface {
Size() Size
Scale() float32
SetScale(float32)

SetOnKeyDown(func(*KeyEvent))
}

// GetCanvas returns the canvas containing the passed CanvasObject.
Expand Down
7 changes: 7 additions & 0 deletions ui/canvasobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ type CanvasObject interface {

MinSize() Size
}

// ClickableObject describes any CanvasObject that can also be clicked
// (i.e. has mouse handlers). This should be implemented by buttons etc that
// wish to handle pointer interactions.
type ClickableObject interface {
OnMouseDown(*MouseEvent)
}
19 changes: 19 additions & 0 deletions ui/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ui

import "github.com/fyne-io/fyne/ui/input"

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

Name string
Code input.KeyCode
Modifiers input.Modifier
}

// MouseEvent describes a pointer input event. The position is relative to the top-left
// of the CanvasObject this is triggered on.
type MouseEvent struct {
Position Position // The position of the event
Button input.MouseButton // The mouse button which caused the event
}
13 changes: 13 additions & 0 deletions ui/input/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package input

// KeyCode represents the numeric code of a key pressed without modifiers
type KeyCode int

// Modifier captures any key modifiers (shift etc) pressed during this key event
type Modifier int

const (
ShiftModifier Modifier = 1 << iota
ControlModifier
AltModifier
)
13 changes: 1 addition & 12 deletions ui/event/mouse.go → ui/input/mouse.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
// Package event defines the various types of events that widgets or apps can
// listen for
package event

import "github.com/fyne-io/fyne/ui"

// MouseEvent describes an input event. The position is relative to the top-left
// of the CanvasObject this is triggered on.
type MouseEvent struct {
Position ui.Position // The position of the event
Button MouseButton // The mouse button which caused the event
}
package input

// MouseButton represents a single button in a MouseEvent
type MouseButton int
Expand Down
15 changes: 10 additions & 5 deletions ui/widget/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package widget

import "github.com/fyne-io/fyne/ui"
import "github.com/fyne-io/fyne/ui/canvas"
import "github.com/fyne-io/fyne/ui/event"
import "github.com/fyne-io/fyne/ui/layout"
import "github.com/fyne-io/fyne/ui/theme"

Expand All @@ -11,7 +10,7 @@ type Button struct {
baseWidget
Style ButtonStyle

OnClicked func(*event.MouseEvent)
OnTapped func()

label *canvas.Text
background *canvas.Rectangle
Expand Down Expand Up @@ -43,8 +42,14 @@ func (b *Button) Layout(size ui.Size) []ui.CanvasObject {
return b.objects
}

// NewButton creates a new button widget with the set label and click handler
func NewButton(label string, clicked func(*event.MouseEvent)) *Button {
func (b *Button) OnMouseDown(*ui.MouseEvent) {
if b.OnTapped != nil {
b.OnTapped()
}
}

// NewButton creates a new button widget with the set label and tap handler
func NewButton(label string, tapped func()) *Button {
text := canvas.NewText(label)
bg := canvas.NewRectangle(theme.ButtonColor())

Expand All @@ -56,7 +61,7 @@ func NewButton(label string, clicked func(*event.MouseEvent)) *Button {
},
},
DefaultButton,
clicked,
tapped,
text,
bg,
}
Expand Down

0 comments on commit f78d89c

Please sign in to comment.