diff --git a/ui/canvas.go b/ui/canvas.go index 13048a9970..f2d4ddcd9c 100644 --- a/ui/canvas.go +++ b/ui/canvas.go @@ -10,6 +10,8 @@ type Canvas interface { Size() Size Scale() float32 SetScale(float32) + + SetOnKeyDown(func(*KeyEvent)) } // GetCanvas returns the canvas containing the passed CanvasObject. diff --git a/ui/canvasobject.go b/ui/canvasobject.go index 36df5bcc1a..3fc353301e 100644 --- a/ui/canvasobject.go +++ b/ui/canvasobject.go @@ -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) +} diff --git a/ui/event.go b/ui/event.go new file mode 100644 index 0000000000..23a29db2f4 --- /dev/null +++ b/ui/event.go @@ -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 +} diff --git a/ui/input/key.go b/ui/input/key.go new file mode 100644 index 0000000000..aac8aca66f --- /dev/null +++ b/ui/input/key.go @@ -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 +) diff --git a/ui/event/mouse.go b/ui/input/mouse.go similarity index 51% rename from ui/event/mouse.go rename to ui/input/mouse.go index be65b173c2..affc752c42 100644 --- a/ui/event/mouse.go +++ b/ui/input/mouse.go @@ -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 diff --git a/ui/widget/button.go b/ui/widget/button.go index 07bb7dd891..a0733669cd 100644 --- a/ui/widget/button.go +++ b/ui/widget/button.go @@ -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" @@ -11,7 +10,7 @@ type Button struct { baseWidget Style ButtonStyle - OnClicked func(*event.MouseEvent) + OnTapped func() label *canvas.Text background *canvas.Rectangle @@ -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()) @@ -56,7 +61,7 @@ func NewButton(label string, clicked func(*event.MouseEvent)) *Button { }, }, DefaultButton, - clicked, + tapped, text, bg, }