Skip to content

Commit

Permalink
Merge branch 'master' into doubleclick
Browse files Browse the repository at this point in the history
  • Loading branch information
gcla committed May 30, 2022
2 parents f88a0db + 072f088 commit 786c847
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
21 changes: 14 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ type App struct {
prevWasMouseMove bool // True if we last processed simple mouse movement. We can optimize on slow
enableMouseMotion bool
enableBracketedPaste bool
// systems by discarding subsequent mouse movement events.
screenInited bool
dontOwnScreen bool

lastMouse MouseState // So I can tell if a button was previously clicked
MouseState // Track which mouse buttons are currently down
Expand All @@ -117,6 +118,7 @@ type AppArgs struct {
EnableMouseMotion bool
EnableBracketedPaste bool
Log log.StdLogger
DontActivate bool
}

// IUnhandledInput is used as a handler for application user input that is not handled by any
Expand Down Expand Up @@ -284,9 +286,10 @@ func newApp(args AppArgs) (rapp *App, rerr error) {
log: args.Log,
enableMouseMotion: args.EnableMouseMotion,
enableBracketedPaste: args.EnableBracketedPaste,
dontOwnScreen: args.Screen != nil,
}

if args.Screen == nil {
if !res.dontOwnScreen && !args.DontActivate {
if err := res.initScreen(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -813,14 +816,13 @@ func (a *App) Quit() {
// I can't make tcell claim and release the same screen successfully. Clients of
// the app struct shouldn't cache the screen object returned via GetScreen().
//
// Assumes we own the screen...
func (a *App) ActivateScreen() error {
screen, err := tcellScreen()
if err != nil {
return WithKVs(err, map[string]interface{}{"TERM": os.Getenv("TERM")})
}
if a.screen != nil {
a.screen.Fini()
}
a.DeactivateScreen()
a.screen = screen
if err := a.initScreen(); err != nil {
return err
Expand All @@ -831,16 +833,21 @@ func (a *App) ActivateScreen() error {
return nil
}

// Assumes we own the screen
func (a *App) DeactivateScreen() {
a.screen.Fini()
a.screen = nil
if a.screen != nil && a.screenInited {
a.screen.Fini()
a.screen = nil
a.screenInited = false
}
}

func (a *App) initScreen() error {
if err := a.screen.Init(); err != nil {
return WithKVs(err, map[string]interface{}{"TERM": os.Getenv("TERM")})
}

a.screenInited = true
a.initColorMode()

defFg := ColorDefault
Expand Down
4 changes: 3 additions & 1 deletion callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func (c *Callbacks) AddCallback(name interface{}, cb ICallback) {
}

func (c *Callbacks) RemoveCallback(name interface{}, cb IIdentity) bool {
if c == nil {
return false
}
c.Lock()
defer c.Unlock()
cbs, ok := c.callbacks[name]
Expand All @@ -126,7 +129,6 @@ func (c *Callbacks) RemoveCallback(name interface{}, cb IIdentity) bool {
ok = false
for i, cb2 := range cbs {
if cb.ID() == cb2.ID() {
//delete(c.callbacks, name)
// Append backwards for easier deletion later
idxs = append([]int{i}, idxs...)
}
Expand Down
3 changes: 1 addition & 2 deletions widgets/pile/pile.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ func RenderSize(w gowid.ICompositeMultipleWidget, size gowid.IRenderSize, focus
// What if the Pile is rendered as a RenderFlow? Then you can't specify any weighted widgets

func Render(w IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {

subfocus := w.Focus()
// if !focus.Focus {
// subfocus = -1
Expand All @@ -457,7 +456,7 @@ func Render(w IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.I
// a pile of width 20, and put it in a column, the next column starts at 21
// TODO - remember which one has focus
res.AppendBelow(canvases[i], i == subfocus, false)
if haveMaxRow && res.BoxRows() >= rows.Rows() {
if haveMaxRow && res.BoxRows() > rows.Rows() {
trim = true
break
}
Expand Down
39 changes: 39 additions & 0 deletions widgets/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,45 @@ func PreviousPosition(tp IPos, tree IModel) IPos {

//======================================================================

type ISearchPred interface {
CheckNode(IModel, IPos) bool
}

type SearchPred func(IModel, IPos) bool

func (s SearchPred) CheckNode(tree IModel, pos IPos) bool {
return s(tree, pos)
}

func DepthFirstSearch(tree IModel, fn ISearchPred) IPos {
pos := NewPos()
return depthFirstSearchImpl(tree, pos, fn)
}

func depthFirstSearchImpl(tree IModel, pos *TreePos, fn ISearchPred) IPos {
if tree == nil {
return nil
}
if fn.CheckNode(tree, pos) {
return pos
}
cs := tree.Children()
tpos := pos.Copy().(*TreePos)
tpos.Pos = append(tpos.Pos, 0)
i := 0
for cs.Next() {
tpos.Pos[len(tpos.Pos)-1] = i
rpos := depthFirstSearchImpl(cs.Value(), tpos, fn)
if rpos != nil {
return rpos
}
i += 1
}
return nil
}

//======================================================================

type IWidgetMaker interface {
MakeWidget(pos IPos, tree IModel) gowid.IWidget
}
Expand Down
20 changes: 20 additions & 0 deletions widgets/tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ func TestTree1(t *testing.T) {
tt := tp.GetSubStructure(parent1)

assert.Equal(t, leaf1, tt)

count := 0
var pos IPos

dfs := DepthFirstSearch(parent1, SearchPred(func(t IModel, p IPos) bool {
count += 1
t2 := t.(*Tree)
pos = p
return t2.theLeaf == "leaf2"
}))

pos2 := pos.(*TreePos)

assert.NotNil(t, dfs)
assert.Equal(t, 6, count)
assert.Equal(t, []int{2}, pos2.Indices())

st := pos2.GetSubStructure(parent1).(*Tree)
assert.Equal(t, "leaf2", st.theLeaf)

}

//======================================================================
Expand Down

0 comments on commit 786c847

Please sign in to comment.