Setting focus is now based on a "mouse down" event instead of a "click" event.

pull/759/head
Oliver 2 years ago
parent 384c772b0b
commit cecb44578c

@ -125,7 +125,7 @@ func (b *Box) GetInnerRect() (int, int, int, int) {
// if this primitive is part of a layout (e.g. Flex, Grid) or if it was added
// like this:
//
// application.SetRoot(p, true)
// application.SetRoot(p, true)
func (b *Box) SetRect(x, y, width, height int) {
b.x = x
b.y = y
@ -215,7 +215,7 @@ func (b *Box) WrapMouseHandler(mouseHandler func(MouseAction, *tcell.EventMouse,
// MouseHandler returns nil.
func (b *Box) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return b.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if action == MouseLeftClick && b.InRect(event.Position()) {
if action == MouseLeftDown && b.InRect(event.Position()) {
setFocus(b)
consumed = true
}
@ -272,7 +272,7 @@ func (b *Box) SetBorderColor(color tcell.Color) *Box {
// SetBorderAttributes sets the border's style attributes. You can combine
// different attributes using bitmask operations:
//
// box.SetBorderAttributes(tcell.AttrUnderline | tcell.AttrBold)
// box.SetBorderAttributes(tcell.AttrUnderline | tcell.AttrBold)
func (b *Box) SetBorderAttributes(attr tcell.AttrMask) *Box {
b.borderStyle = b.borderStyle.Attributes(attr)
return b

@ -145,8 +145,10 @@ func (b *Button) MouseHandler() func(action MouseAction, event *tcell.EventMouse
}
// Process mouse event.
if action == MouseLeftClick {
if action == MouseLeftDown {
setFocus(b)
consumed = true
} else if action == MouseLeftClick {
if b.selected != nil {
b.selected()
}

@ -226,13 +226,17 @@ func (c *Checkbox) MouseHandler() func(action MouseAction, event *tcell.EventMou
}
// Process mouse event.
if action == MouseLeftClick && y == rectY {
setFocus(c)
c.checked = !c.checked
if c.changed != nil {
c.changed(c.checked)
if y == rectY {
if action == MouseLeftDown {
setFocus(c)
consumed = true
} else if action == MouseLeftClick {
c.checked = !c.checked
if c.changed != nil {
c.changed(c.checked)
}
consumed = true
}
consumed = true
}
return

@ -645,9 +645,9 @@ func (f *Form) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
}
}
// A mouse click anywhere else will return the focus to the last selected
// A mouse down anywhere else will return the focus to the last selected
// element.
if action == MouseLeftClick && f.InRect(event.Position()) {
if action == MouseLeftDown && f.InRect(event.Position()) {
consumed = true
}

@ -49,6 +49,18 @@ func NewFrame(primitive Primitive) *Frame {
return f
}
// SetPrimitive replaces the contained primitive with the given one. To remove
// a primitive, set it to nil.
func (f *Frame) SetPrimitive(p Primitive) *Frame {
f.primitive = p
return f
}
// GetPrimitive returns the primitive contained in this frame.
func (f *Frame) GetPrimitive() Primitive {
return f.primitive
}
// AddText adds text to the frame. Set "header" to true if the text is to appear
// in the header, above the contained primitive. Set it to false for it to
// appear in the footer, below the contained primitive. "align" must be one of
@ -169,10 +181,19 @@ func (f *Frame) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
// Pass mouse events on to contained primitive.
if f.primitive != nil {
return f.primitive.MouseHandler()(action, event, setFocus)
consumed, capture = f.primitive.MouseHandler()(action, event, setFocus)
if consumed {
return true, capture
}
}
// Clicking on the frame parts.
if action == MouseLeftDown {
setFocus(f)
consumed = true
}
return false, nil
return
})
}
@ -182,11 +203,9 @@ func (f *Frame) InputHandler() func(event *tcell.EventKey, setFocus func(p Primi
if f.primitive == nil {
return
}
if f.primitive.HasFocus() {
if handler := f.primitive.InputHandler(); handler != nil {
handler(event, setFocus)
return
}
if handler := f.primitive.InputHandler(); handler != nil {
handler(event, setFocus)
return
}
})
}

@ -685,21 +685,25 @@ func (i *InputField) MouseHandler() func(action MouseAction, event *tcell.EventM
}
// Process mouse event.
if action == MouseLeftClick && y == rectY {
// Determine where to place the cursor.
if x >= i.fieldX {
if !iterateString(i.text[i.offset:], func(main rune, comb []rune, textPos int, textWidth int, screenPos int, screenWidth, boundaries int) bool {
if x-i.fieldX < screenPos+screenWidth {
i.cursorPos = textPos + i.offset
return true
if y == rectY {
if action == MouseLeftDown {
setFocus(i)
consumed = true
} else if action == MouseLeftClick {
// Determine where to place the cursor.
if x >= i.fieldX {
if !iterateString(i.text[i.offset:], func(main rune, comb []rune, textPos int, textWidth int, screenPos int, screenWidth, boundaries int) bool {
if x-i.fieldX < screenPos+screenWidth {
i.cursorPos = textPos + i.offset
return true
}
return false
}) {
i.cursorPos = len(i.text)
}
return false
}) {
i.cursorPos = len(i.text)
}
consumed = true
}
setFocus(i)
consumed = true
}
return

@ -699,7 +699,6 @@ func (l *List) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
// Process mouse event.
switch action {
case MouseLeftClick:
setFocus(l)
index := l.indexAtPoint(event.Position())
if index != -1 {
item := l.items[index]
@ -728,6 +727,7 @@ func (l *List) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
if _, _, _, height := l.GetInnerRect(); lines > height {
l.itemOffset++
}
setFocus(l)
consumed = true
}

@ -190,7 +190,7 @@ func (m *Modal) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
return m.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
// Pass mouse events on to the form.
consumed, capture = m.form.MouseHandler()(action, event, setFocus)
if !consumed && action == MouseLeftClick && m.InRect(event.Position()) {
if !consumed && action == MouseLeftDown && m.InRect(event.Position()) {
setFocus(m)
consumed = true
}

@ -135,7 +135,7 @@ func (c *TableCell) SetTransparency(transparent bool) *TableCell {
// SetAttributes sets the cell's text attributes. You can combine different
// attributes using bitmask operations:
//
// cell.SetAttributes(tcell.AttrUnderline | tcell.AttrBold)
// cell.SetAttributes(tcell.AttrUnderline | tcell.AttrBold)
func (c *TableCell) SetAttributes(attr tcell.AttrMask) *TableCell {
c.Attributes = attr
return c
@ -388,13 +388,13 @@ func (t *tableDefaultContent) GetColumnCount() int {
// Columns will use as much horizontal space as they need. You can constrain
// their size with the MaxWidth parameter of the TableCell type.
//
// Fixed Columns
// # Fixed Columns
//
// You can define fixed rows and rolumns via SetFixed(). They will always stay
// in their place, even when the table is scrolled. Fixed rows are always the
// top rows. Fixed columns are always the leftmost columns.
//
// Selections
// # Selections
//
// You can call SetSelectable() to set columns and/or rows to "selectable". If
// the flag is set only for columns, entire columns can be selected by the user.
@ -402,7 +402,7 @@ func (t *tableDefaultContent) GetColumnCount() int {
// set, individual cells can be selected. The "selected" handler set via
// SetSelectedFunc() is invoked when the user presses Enter on a selection.
//
// Navigation
// # Navigation
//
// If the table extends beyond the available space, it can be navigated with
// key bindings similar to Vim:
@ -551,7 +551,7 @@ func (t *Table) SetBordersColor(color tcell.Color) *Table {
//
// To reset a previous setting to its default, make the following call:
//
// table.SetSelectedStyle(tcell.Style{})
// table.SetSelectedStyle(tcell.Style{})
func (t *Table) SetSelectedStyle(style tcell.Style) *Table {
t.selectedStyle = style
return t
@ -1596,6 +1596,9 @@ func (t *Table) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
}
switch action {
case MouseLeftDown:
setFocus(t)
consumed = true
case MouseLeftClick:
selectEvent := true
row, column := t.cellAt(x, y)
@ -1608,7 +1611,6 @@ func (t *Table) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
if selectEvent && (t.rowsSelectable || t.columnsSelectable) {
t.Select(row, column)
}
setFocus(t)
consumed = true
case MouseScrollUp:
t.trackEnd = false

@ -2106,7 +2106,7 @@ func (t *TextArea) MouseHandler() func(action MouseAction, event *tcell.EventMou
return false, nil
}
// Trigger a "moved" event if requested.
// Trigger a "moved" event at the end if requested.
if t.moved != nil {
selectionStart, cursor := t.selectionStart, t.cursor
defer func() {
@ -2140,11 +2140,9 @@ func (t *TextArea) MouseHandler() func(action MouseAction, event *tcell.EventMou
break
}
t.moveCursor(row, column)
setFocus(t)
consumed = true
case MouseLeftUp:
t.moveCursor(row, column)
setFocus(t)
consumed = true
capture = nil
t.dragging = false

@ -84,7 +84,7 @@ func (w TextViewWriter) HasFocus() bool {
// but if a handler is installed via SetChangedFunc(), you can cause it to be
// redrawn. (See SetChangedFunc() for more details.)
//
// Navigation
// # Navigation
//
// If the text view is scrollable (the default), text is kept in a buffer which
// may be larger than the screen and can be navigated similarly to Vim:
@ -103,27 +103,27 @@ func (w TextViewWriter) HasFocus() bool {
//
// Use SetInputCapture() to override or modify keyboard input.
//
// Colors
// # Colors
//
// If dynamic colors are enabled via SetDynamicColors(), text color can be
// changed dynamically by embedding color strings in square brackets. This works
// the same way as anywhere else. Please see the package documentation for more
// information.
//
// Regions and Highlights
// # Regions and Highlights
//
// If regions are enabled via SetRegions(), you can define text regions within
// the text and assign region IDs to them. Text regions start with region tags.
// Region tags are square brackets that contain a region ID in double quotes,
// for example:
//
// We define a ["rg"]region[""] here.
// We define a ["rg"]region[""] here.
//
// A text region ends with the next region tag. Tags with no region ID ([""])
// don't start new regions. They can therefore be used to mark the end of a
// region. Region IDs must satisfy the following regular expression:
//
// [a-zA-Z0-9_,;: \-\.]+
// [a-zA-Z0-9_,;: \-\.]+
//
// Regions can be highlighted by calling the Highlight() function with one or
// more region IDs. This can be used to display search results, for example.
@ -131,7 +131,7 @@ func (w TextViewWriter) HasFocus() bool {
// The ScrollToHighlight() function can be used to jump to the currently
// highlighted region once when the text view is drawn the next time.
//
// Large Texts
// # Large Texts
//
// This widget is not designed for very large texts as word wrapping, color and
// region tag handling, and proper Unicode handling will result in a significant
@ -756,13 +756,13 @@ func (t *TextView) write(p []byte) (n int, err error) {
// BatchWriter is called, and will be released when the returned writer is
// closed. Example:
//
// tv := tview.NewTextView()
// w := tv.BatchWriter()
// defer w.Close()
// w.Clear()
// fmt.Fprintln(w, "To sit in solemn silence")
// fmt.Fprintln(w, "on a dull, dark, dock")
// fmt.Println(tv.GetText(false))
// tv := tview.NewTextView()
// w := tv.BatchWriter()
// defer w.Close()
// w.Clear()
// fmt.Fprintln(w, "To sit in solemn silence")
// fmt.Fprintln(w, "on a dull, dark, dock")
// fmt.Println(tv.GetText(false))
//
// Note that using the batch writer requires you to manage any issues that may
// arise from concurrency yourself. See package description for details on
@ -1317,6 +1317,9 @@ func (t *TextView) MouseHandler() func(action MouseAction, event *tcell.EventMou
}
switch action {
case MouseLeftDown:
setFocus(t)
consumed = true
case MouseLeftClick:
if t.regions {
// Find a region to highlight.
@ -1331,7 +1334,6 @@ func (t *TextView) MouseHandler() func(action MouseAction, event *tcell.EventMou
break
}
}
setFocus(t)
consumed = true
case MouseScrollUp:
t.trackEnd = false

@ -367,8 +367,8 @@ func (t *TreeView) SetTopLevel(topLevel int) *TreeView {
//
// For example, to display a hierarchical list with bullet points:
//
// treeView.SetGraphics(false).
// SetPrefixes([]string{"* ", "- ", "x "})
// treeView.SetGraphics(false).
// SetPrefixes([]string{"* ", "- ", "x "})
func (t *TreeView) SetPrefixes(prefixes []string) *TreeView {
t.prefixes = prefixes
return t
@ -792,8 +792,10 @@ func (t *TreeView) MouseHandler() func(action MouseAction, event *tcell.EventMou
}
switch action {
case MouseLeftClick:
case MouseLeftDown:
setFocus(t)
consumed = true
case MouseLeftClick:
_, rectY, _, _ := t.GetInnerRect()
y += t.offsetY - rectY
if y >= 0 && y < len(t.nodes) {

Loading…
Cancel
Save