| 🛟 Updated. 28d5985 k33g 9h ago | 1 | package terminal |
| 2 | |
| 3 | import "github.com/gdamore/tcell/v2" |
| 4 | |
| 5 | // HandleKey sends a key press to the shell. |
| 6 | // |
| 7 | // Shift with Page Up or Page Down reads back through the history instead, |
| 8 | // which is where every terminal puts that. Everything else goes to the shell, |
| 9 | // and brings the view back to the live screen on the way — a terminal that |
| 10 | // stayed in its history while you typed would hide your own command. |
| 11 | // |
| 12 | // Once the program has gone, only the scrolling keys are still taken. A dead |
| 13 | // terminal that went on swallowing keys could never be closed with Ctrl-W: the |
| 14 | // write would fail silently and the key would be consumed anyway, leaving the |
| 15 | // mouse as the only way out of a window whose command has finished. |
| 16 | func (v *View) HandleKey(ev *tcell.EventKey) bool { |
| 17 | if !v.Focused() { |
| 18 | return false |
| 19 | } |
| 20 | if v.handleScrollKey(ev) { |
| 21 | return true |
| 22 | } |
| 23 | if v.Exited() { |
| 24 | return false |
| 25 | } |
| 26 | |
| 27 | sequence := Encode(ev, v.applicationCursor()) |
| 28 | if len(sequence) == 0 { |
| 29 | return false |
| 30 | } |
| 31 | |
| 32 | v.ScrollToBottom() |
| 33 | // A write that fails means the shell has gone, which the reading goroutine |
| 34 | // is about to report; there is nothing useful to do with the error here. |
| 35 | _, _ = v.session.Write(sequence) |
| 36 | return true |
| 37 | } |
| 38 | |
| 39 | // handleScrollKey deals with the keys that read back through the history. |
| 40 | func (v *View) handleScrollKey(ev *tcell.EventKey) bool { |
| 41 | if ev.Modifiers()&tcell.ModShift == 0 { |
| 42 | return false |
| 43 | } |
| 44 | |
| 45 | page := max(v.Bounds().H-1, 1) |
| 46 | switch ev.Key() { |
| 47 | case tcell.KeyPgUp: |
| 48 | v.ScrollBy(page) |
| 49 | case tcell.KeyPgDn: |
| 50 | v.ScrollBy(-page) |
| 51 | default: |
| 52 | return false |
| 53 | } |
| 54 | return true |
| 55 | } |
| 56 | |
| 57 | // applicationCursor reports the mode the program has asked for, under the lock. |
| 58 | func (v *View) applicationCursor() bool { |
| 59 | v.mu.Lock() |
| 60 | defer v.mu.Unlock() |
| 61 | return v.parser.Screen().ApplicationCursor() |
| 62 | } |
| 63 | |
| 64 | // HandleMouse scrolls back through the history with the wheel. |
| 65 | // |
| 66 | // Clicks are not forwarded: mouse reporting is a mode this emulator does not |
| 67 | // implement, so a program has no reason to expect them. |
| 68 | func (v *View) HandleMouse(ev *tcell.EventMouse) bool { |
| 69 | if !v.hitTest(ev) { |
| 70 | return false |
| 71 | } |
| 72 | |
| 73 | switch ev.Buttons() { |
| 74 | case tcell.WheelUp: |
| 75 | v.ScrollBy(wheelStep) |
| 76 | case tcell.WheelDown: |
| 77 | v.ScrollBy(-wheelStep) |
| 78 | case tcell.Button1: |
| 79 | // A click inside the window is claimed so that it counts as focusing |
| 80 | // the window rather than falling through to the desktop. |
| 81 | default: |
| 82 | return false |
| 83 | } |
| 84 | return true |
| 85 | } |
| 86 | |
| 87 | // hitTest reports whether a mouse event landed on the view. |
| 88 | func (v *View) hitTest(ev *tcell.EventMouse) bool { |
| 89 | x, y := ev.Position() |
| 90 | return v.Bounds().Contains(x, y) |
| 91 | } |