1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

term.ui: use the new [flag] enums (#8881)

This commit is contained in:
spaceface
2021-02-21 15:07:49 +01:00
committed by GitHub
parent 0470baafa6
commit 260f677469
8 changed files with 92 additions and 81 deletions

View File

@@ -15,6 +15,9 @@ mut:
fn event(e &tui.Event, x voidptr) {
mut app := &App(x)
println(e)
if e.typ == .key_down && e.code == .escape {
exit(0)
}
}
fn frame(x voidptr) {
@@ -77,11 +80,6 @@ In the case of the various callbacks, they will not be fired if a handler has no
#### FAQ
Q: Why does this module not work on Windows?
A: As with many other things, Windows has a completely different and incompatible way of handling
input parsing and drawing primitives, and support has not been implemented yet.
Contributions are definitely welcome though.
Q: My terminal (doesn't receive events / doesn't print anything / prints gibberish characters),
what's up with that?
A: Please check if your terminal. The module has been tested with `xterm`-based terminals on Linux

View File

@@ -122,12 +122,6 @@ pub enum KeyCode {
f24 = 313
}
pub const (
shift = u32(1 << 0)
ctrl = u32(1 << 1)
alt = u32(1 << 2)
)
pub enum Direction {
unknown
up
@@ -154,6 +148,15 @@ pub enum EventType {
resized
}
[flag]
pub enum Modifiers {
ctrl
shift
alt
}
[inline] pub fn (m &Modifiers) is_empty() bool { return int(m) == 0 }
pub struct Event {
pub:
typ EventType
@@ -166,7 +169,7 @@ pub:
// Keyboard event info
code KeyCode
modifiers u32
modifiers Modifiers
ascii byte
utf8 string

View File

@@ -164,10 +164,10 @@ fn (mut ctx Context) parse_events() {
else { KeyCode(ascii) }
}
mut modifiers := u32(0)
if e.dwControlKeyState & (0x1 | 0x2) != 0 { modifiers |= alt }
if e.dwControlKeyState & (0x4 | 0x8) != 0 { modifiers |= ctrl }
if e.dwControlKeyState & 0x10 != 0 { modifiers |= shift }
mut modifiers := Modifiers{}
if e.dwControlKeyState & (0x1 | 0x2) != 0 { modifiers.set(.alt) }
if e.dwControlKeyState & (0x4 | 0x8) != 0 { modifiers.set(.ctrl) }
if e.dwControlKeyState & 0x10 != 0 { modifiers.set(.shift) }
mut event := &Event{
typ: .key_down
@@ -188,10 +188,10 @@ fn (mut ctx Context) parse_events() {
}
x := e.dwMousePosition.X + 1
y := int(e.dwMousePosition.Y) - sb_info.srWindow.Top + 1
mut modifiers := u32(0)
if e.dwControlKeyState & (0x1 | 0x2) != 0 { modifiers |= alt }
if e.dwControlKeyState & (0x4 | 0x8) != 0 { modifiers |= ctrl }
if e.dwControlKeyState & 0x10 != 0 { modifiers |= shift }
mut modifiers := Modifiers{}
if e.dwControlKeyState & (0x1 | 0x2) != 0 { modifiers.set(.alt) }
if e.dwControlKeyState & (0x4 | 0x8) != 0 { modifiers.set(.ctrl) }
if e.dwControlKeyState & 0x10 != 0 { modifiers.set(.shift) }
// TODO: handle capslock/numlock/etc?? events exist for those keys
match int(e.dwEventFlags) {
C.MOUSE_MOVED {

View File

@@ -283,8 +283,8 @@ fn single_char(buf string) &Event {
match ch {
// special handling for `ctrl + letter`
// TODO: Fix assoc in V and remove this workaround :/
// 1 ... 26 { event = Event{ ...event, code: KeyCode(96 | ch), modifiers: ctrl } }
// 65 ... 90 { event = Event{ ...event, code: KeyCode(32 | ch), modifiers: shift } }
// 1 ... 26 { event = Event{ ...event, code: KeyCode(96 | ch), modifiers: .ctrl } }
// 65 ... 90 { event = Event{ ...event, code: KeyCode(32 | ch), modifiers: .shift } }
// The bit `or`s here are really just `+`'s, just written in this way for a tiny performance improvement
// don't treat tab, enter as ctrl+i, ctrl+j
1...8, 11...26 { event = &Event{
@@ -292,14 +292,14 @@ fn single_char(buf string) &Event {
ascii: event.ascii
utf8: event.utf8
code: KeyCode(96 | ch)
modifiers: ctrl
modifiers: .ctrl
} }
65...90 { event = &Event{
typ: event.typ
ascii: event.ascii
utf8: event.utf8
code: KeyCode(32 | ch)
modifiers: shift
modifiers: .shift
} }
else {}
}
@@ -351,13 +351,14 @@ fn escape_sequence(buf_ string) (&Event, int) {
if buf.len == 1 {
c := single_char(buf)
mut modifiers := c.modifiers
modifiers.set(.alt)
return &Event{
typ: c.typ
ascii: c.ascii
code: c.code
utf8: single
modifiers: c.modifiers | alt
modifiers: modifiers
}, 2
}
// ----------------
@@ -374,15 +375,15 @@ fn escape_sequence(buf_ string) (&Event, int) {
lo := typ & 0b00011
hi := typ & 0b11100
mut modifiers := u32(0)
mut modifiers := Modifiers{}
if hi & 4 != 0 {
modifiers |= shift
modifiers.set(.shift)
}
if hi & 8 != 0 {
modifiers |= alt
modifiers.set(.alt)
}
if hi & 16 != 0 {
modifiers |= ctrl
modifiers.set(.ctrl)
}
match typ {
@@ -444,7 +445,7 @@ fn escape_sequence(buf_ string) (&Event, int) {
// ----------------------------
mut code := KeyCode.null
mut modifiers := u32(0)
mut modifiers := Modifiers{}
match buf {
'[A', 'OA' { code = .up }
'[B', 'OB' { code = .down }
@@ -473,35 +474,34 @@ fn escape_sequence(buf_ string) (&Event, int) {
if buf == '[Z' {
code = .tab
modifiers |= shift
modifiers.set(.shift)
}
if buf.len == 5 && buf[0] == `[` && buf[1].is_digit() && buf[2] == `;` {
// code = KeyCode(buf[4] + 197)
modifiers = match buf[3] {
`2` { shift }
`3` { alt }
`4` { shift | alt }
`5` { ctrl }
`6` { ctrl | shift }
`7` { ctrl | alt }
`8` { ctrl | alt | shift }
else { modifiers } // probably unreachable? idk, terminal events are strange
match buf[3] {
`2` { modifiers = .shift }
`3` { modifiers = .alt }
`4` { modifiers = .shift | .alt }
`5` { modifiers = .ctrl }
`6` { modifiers = .ctrl | .shift }
`7` { modifiers = .ctrl | .alt }
`8` { modifiers = .ctrl | .alt | .shift }
else {}
}
if buf[1] == `1` {
code = match buf[4] {
`A` { KeyCode.up }
`B` { KeyCode.down }
`C` { KeyCode.right }
`D` { KeyCode.left }
`F` { KeyCode.end }
`H` { KeyCode.home }
`P` { KeyCode.f1 }
`Q` { KeyCode.f2 }
`R` { KeyCode.f3 }
`S` { KeyCode.f4 }
else { code }
match buf[4] {
`A` { code = KeyCode.up }
`B` { code = KeyCode.down }
`C` { code = KeyCode.right }
`D` { code = KeyCode.left }
`F` { code = KeyCode.end }
`H` { code = KeyCode.home }
`P` { code = KeyCode.f1 }
`Q` { code = KeyCode.f2 }
`R` { code = KeyCode.f3 }
`S` { code = KeyCode.f4 }
else {}
}
} else if buf[1] == `5` {
code = KeyCode.page_up