Added keys.

Fixed bug where copy/cut/paste/select all weren't executed.
Fixed bug where start/end of selection wasn't properly set when typing.
Removed SetClipboard.
This commit is contained in:
Sander Schobers 2020-05-13 15:57:04 +02:00
parent 48aaf30182
commit 1aad3bf11a
6 changed files with 439 additions and 69 deletions

274
allg5ui/key.go Normal file
View File

@ -0,0 +1,274 @@
package allg5ui
import (
"opslag.de/schobers/allg5"
"opslag.de/schobers/zntg/ui"
)
func key(key allg5.Key) ui.Key {
switch key {
case allg5.Key0:
return ui.Key0
case allg5.Key1:
return ui.Key1
case allg5.Key2:
return ui.Key2
case allg5.Key3:
return ui.Key3
case allg5.Key4:
return ui.Key4
case allg5.Key5:
return ui.Key5
case allg5.Key6:
return ui.Key6
case allg5.Key7:
return ui.Key7
case allg5.Key8:
return ui.Key8
case allg5.Key9:
return ui.Key9
case allg5.KeyA:
return ui.KeyA
case allg5.KeyAlt:
return ui.KeyAlt
case allg5.KeyAltGr:
return ui.KeyAltGr
case allg5.KeyAt:
return ui.KeyAt
case allg5.KeyB:
return ui.KeyB
case allg5.KeyBack:
return ui.KeyBack
case allg5.KeyBackslash:
return ui.KeyBackslash
case allg5.KeyBackslash2:
return ui.KeyBackslash
case allg5.KeyBackspace:
return ui.KeyBackspace
case allg5.KeyBackQuote:
return ui.KeyBacktick
case allg5.KeyButtonA:
return ui.KeyButtonA
case allg5.KeyButtonB:
return ui.KeyButtonB
case allg5.KeyButtonL1:
return ui.KeyButtonL1
case allg5.KeyButtonL2:
return ui.KeyButtonL2
case allg5.KeyButtonR1:
return ui.KeyButtonR1
case allg5.KeyButtonR2:
return ui.KeyButtonR2
case allg5.KeyButtonX:
return ui.KeyButtonX
case allg5.KeyButtonY:
return ui.KeyButtonY
case allg5.KeyC:
return ui.KeyC
case allg5.KeyCapsLock:
return ui.KeyCapsLock
case allg5.KeyCircumflex:
return ui.KeyCircumflex
case allg5.KeyCloseBrace:
return ui.KeyCloseBrace
case allg5.KeyColon2:
return ui.KeyColon2
case allg5.KeyComma:
return ui.KeyComma
case allg5.KeyCommand:
return ui.KeyCommand
case allg5.KeyD:
return ui.KeyD
case allg5.KeyDelete:
return ui.KeyDelete
case allg5.KeyDown:
return ui.KeyDown
case allg5.KeyDPadCenter:
return ui.KeyDPadCenter
case allg5.KeyDPadDown:
return ui.KeyDPadDown
case allg5.KeyDPadLeft:
return ui.KeyDPadLeft
case allg5.KeyDPadRight:
return ui.KeyDPadRight
case allg5.KeyDPadUp:
return ui.KeyDPadUp
case allg5.KeyE:
return ui.KeyE
case allg5.KeyEnd:
return ui.KeyEnd
case allg5.KeyEquals:
return ui.KeyEquals
case allg5.KeyEscape:
return ui.KeyEscape
case allg5.KeyF:
return ui.KeyF
case allg5.KeyF1:
return ui.KeyF1
case allg5.KeyF2:
return ui.KeyF2
case allg5.KeyF3:
return ui.KeyF3
case allg5.KeyF4:
return ui.KeyF4
case allg5.KeyF5:
return ui.KeyF5
case allg5.KeyF6:
return ui.KeyF6
case allg5.KeyF7:
return ui.KeyF7
case allg5.KeyF8:
return ui.KeyF8
case allg5.KeyF9:
return ui.KeyF9
case allg5.KeyF10:
return ui.KeyF10
case allg5.KeyF11:
return ui.KeyF11
case allg5.KeyF12:
return ui.KeyF12
case allg5.KeyFullstop:
return ui.KeyFullstop
case allg5.KeyG:
return ui.KeyG
case allg5.KeyH:
return ui.KeyH
case allg5.KeyHome:
return ui.KeyHome
case allg5.KeyI:
return ui.KeyI
case allg5.KeyInsert:
return ui.KeyInsert
case allg5.KeyJ:
return ui.KeyJ
case allg5.KeyK:
return ui.KeyK
case allg5.KeyL:
return ui.KeyL
case allg5.KeyLeft:
return ui.KeyLeft
case allg5.KeyLCtrl:
return ui.KeyLeftControl
case allg5.KeyLShift:
return ui.KeyLeftShift
case allg5.KeyLWin:
return ui.KeyLeftWin
case allg5.KeyM:
return ui.KeyM
case allg5.KeyMenu:
return ui.KeyMenu
case allg5.KeyMinus:
return ui.KeyMinus
case allg5.KeyN:
return ui.KeyN
case allg5.KeyNumLock:
return ui.KeyNumLock
case allg5.KeyO:
return ui.KeyO
case allg5.KeyOpenBrace:
return ui.KeyOpenBrace
case allg5.KeyP:
return ui.KeyP
case allg5.KeyPad0:
return ui.KeyPad0
case allg5.KeyPad1:
return ui.KeyPad1
case allg5.KeyPad2:
return ui.KeyPad2
case allg5.KeyPad3:
return ui.KeyPad3
case allg5.KeyPad4:
return ui.KeyPad4
case allg5.KeyPad5:
return ui.KeyPad5
case allg5.KeyPad6:
return ui.KeyPad6
case allg5.KeyPad7:
return ui.KeyPad7
case allg5.KeyPad8:
return ui.KeyPad8
case allg5.KeyPad9:
return ui.KeyPad9
case allg5.KeyPadAsterisk:
return ui.KeyPadAsterisk
case allg5.KeyPadDelete:
return ui.KeyPadDelete
case allg5.KeyPadEnter:
return ui.KeyPadEnter
case allg5.KeyPadEquals:
return ui.KeyPadEquals
case allg5.KeyPadMinus:
return ui.KeyPadMinus
case allg5.KeyPadPlus:
return ui.KeyPadPlus
case allg5.KeyPadSlash:
return ui.KeyPadSlash
case allg5.KeyPageDown:
return ui.KeyPageDown
case allg5.KeyPageUp:
return ui.KeyPageUp
case allg5.KeyPause:
return ui.KeyPause
case allg5.KeyPrintScreen:
return ui.KeyPrintScreen
case allg5.KeyQ:
return ui.KeyQ
case allg5.KeyQuote:
return ui.KeyQuote
case allg5.KeyR:
return ui.KeyR
case allg5.KeyRight:
return ui.KeyRight
case allg5.KeyRCtrl:
return ui.KeyRightControl
case allg5.KeyRShift:
return ui.KeyRightShift
case allg5.KeyRWin:
return ui.KeyRightWin
case allg5.KeyS:
return ui.KeyS
case allg5.KeyScrollLock:
return ui.KeyScrollLock
case allg5.KeySearch:
return ui.KeySearch
case allg5.KeySelect:
return ui.KeySelect
case allg5.KeySemicolon:
return ui.KeySemicolon
case allg5.KeySlash:
return ui.KeySlash
case allg5.KeySpace:
return ui.KeySpace
case allg5.KeyStart:
return ui.KeyStart
case allg5.KeyT:
return ui.KeyT
case allg5.KeyTab:
return ui.KeyTab
case allg5.KeyThumbL:
return ui.KeyThumbL
case allg5.KeyThumbR:
return ui.KeyThumbR
case allg5.KeyTilde:
return ui.KeyTilde
case allg5.KeyU:
return ui.KeyU
case allg5.KeyUp:
return ui.KeyUp
case allg5.KeyV:
return ui.KeyV
case allg5.KeyVolumeDown:
return ui.KeyVolumeDown
case allg5.KeyVolumeUp:
return ui.KeyVolumeUp
case allg5.KeyW:
return ui.KeyW
case allg5.KeyX:
return ui.KeyX
case allg5.KeyY:
return ui.KeyY
case allg5.KeyZ:
return ui.KeyZ
}
return ui.KeyNone
}

View File

@ -296,30 +296,6 @@ func eventBase(e allg5.Event) ui.EventBase {
return ui.EventBase{StampInSeconds: e.Stamp()}
}
func key(key allg5.Key) ui.Key {
switch key {
case allg5.KeyBackspace:
return ui.KeyBackspace
case allg5.KeyDelete:
return ui.KeyDelete
case allg5.KeyDown:
return ui.KeyDown
case allg5.KeyEnd:
return ui.KeyEnd
case allg5.KeyEscape:
return ui.KeyEscape
case allg5.KeyHome:
return ui.KeyHome
case allg5.KeyLeft:
return ui.KeyLeft
case allg5.KeyRight:
return ui.KeyRight
case allg5.KeyUp:
return ui.KeyUp
}
return ui.KeyNone
}
func keyModifiers(mods allg5.KeyMod) ui.KeyModifier {
var m ui.KeyModifier
if mods&allg5.KeyModShift == allg5.KeyModShift {

View File

@ -7,8 +7,6 @@ type Clipboard interface {
var DefaultClipboard Clipboard = &clipboard{}
func SetClipboard(c Clipboard) { DefaultClipboard = c }
type clipboard struct {
value string
}

View File

@ -23,21 +23,6 @@ func (e *EventBase) Stamp() float64 {
return e.StampInSeconds
}
type Key int
const (
KeyNone Key = iota
KeyBackspace
KeyDelete
KeyDown
KeyEnd
KeyEscape
KeyHome
KeyLeft
KeyRight
KeyUp
)
type KeyModifier int
const (

137
ui/key.go Normal file
View File

@ -0,0 +1,137 @@
package ui
type Key int
const (
KeyNone Key = iota
Key0
Key1
Key2
Key3
Key4
Key5
Key6
Key7
Key8
Key9
KeyA
KeyAlt
KeyAltGr
KeyAt
KeyB
KeyBack
KeyBackslash //x2
KeyBackspace
KeyBacktick
KeyButtonA
KeyButtonB
KeyButtonL1
KeyButtonL2
KeyButtonR1
KeyButtonR2
KeyButtonX
KeyButtonY
KeyC
KeyCapsLock
KeyCircumflex
KeyCloseBrace
KeyColon2
KeyComma
KeyCommand
KeyD
KeyDelete
KeyDown
KeyDPadCenter
KeyDPadDown
KeyDPadLeft
KeyDPadRight
KeyDPadUp
KeyE
KeyEnd
KeyEquals
KeyEscape
KeyF
KeyF1
KeyF2
KeyF3
KeyF4
KeyF5
KeyF6
KeyF7
KeyF8
KeyF9
KeyF10
KeyF11
KeyF12
KeyFullstop
KeyG
KeyH
KeyHome
KeyI
KeyInsert
KeyJ
KeyK
KeyL
KeyLeft
KeyLeftControl
KeyLeftShift
KeyLeftWin
KeyM
KeyMenu
KeyMinus
KeyN
KeyNumLock
KeyO
KeyOpenBrace
KeyP
KeyPad0
KeyPad1
KeyPad2
KeyPad3
KeyPad4
KeyPad5
KeyPad6
KeyPad7
KeyPad8
KeyPad9
KeyPadAsterisk
KeyPadDelete
KeyPadEnter
KeyPadEquals
KeyPadMinus
KeyPadPlus
KeyPadSlash
KeyPageDown
KeyPageUp
KeyPause
KeyPrintScreen
KeyQ
KeyQuote
KeyR
KeyRight
KeyRightControl
KeyRightShift
KeyRightWin
KeyS
KeyScrollLock
KeySearch
KeySelect
KeySemicolon
KeySlash
KeySpace
KeyStart
KeyT
KeyTab
KeyThumbL
KeyThumbR
KeyTilde
KeyU
KeyUp
KeyV
KeyVolumeDown
KeyVolumeUp
KeyW
KeyX
KeyY
KeyZ
)

View File

@ -192,37 +192,37 @@ func (b *TextBox) Handle(ctx Context, e Event) {
b.Selection.MoveCaret(1)
selectAfterMove()
}
case !unicode.IsControl(e.Character):
if e.Modifiers == KeyModifierNone {
b.cut()
case e.Modifiers&KeyModifierControl == KeyModifierControl:
switch e.Key {
case KeyA:
b.Selection.Start = 0
b.Selection.End = len(b.Text)
b.Selection.Caret = b.Selection.End
case KeyC:
DefaultClipboard.WriteText(b.selection())
case KeyV:
text, err := DefaultClipboard.ReadText()
text = strings.Map(func(r rune) rune {
if unicode.IsControl(r) {
return -1
}
return r
}, text)
if err == nil {
b.cut()
caret := b.Selection.Caret
b.Text = b.Text[:caret] + text + b.Text[caret:]
b.Selection.Caret = caret + len(text)
}
case KeyX:
DefaultClipboard.WriteText(b.cut())
}
default:
if e.Modifiers == KeyModifierNone || e.Modifiers&KeyModifierShift == KeyModifierShift {
caret := b.Selection.Caret
b.Text = fmt.Sprintf("%s%c%s", b.Text[:caret], e.Character, b.Text[caret:])
b.Selection.Caret = caret + 1
} else if e.Modifiers&KeyModifierControl == KeyModifierControl {
switch e.Character {
case 'a':
b.Selection.Start = 0
b.Selection.End = len(b.Text)
b.Selection.Caret = b.Selection.End
case 'c':
DefaultClipboard.WriteText(b.selection())
case 'v':
text, err := DefaultClipboard.ReadText()
text = strings.Map(func(r rune) rune {
if unicode.IsControl(r) {
return -1
}
return r
}, text)
if err == nil {
b.cut()
caret := b.Selection.Caret
b.Text = b.Text[:caret] + text + b.Text[caret:]
b.Selection.Caret = caret + len(text)
}
case 'x':
DefaultClipboard.WriteText(b.cut())
}
b.Selection.SetSelectionToCaret()
}
}
}