From 305f4debffa922cddc093389d552b4ac2592bfb7 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Sat, 5 Jan 2019 16:27:04 -0500 Subject: [PATCH] Split improvements --- cmd/micro/action/editpane.go | 1 + cmd/micro/action/tab.go | 10 +++++ cmd/micro/display/infowindow.go | 1 + cmd/micro/display/uiwindow.go | 63 +++++++++++++++++++++++++++ cmd/micro/info/infobuffer.go | 9 ++-- cmd/micro/micro.go | 1 + cmd/micro/views/splits.go | 75 +++++++++++++++++++-------------- 7 files changed, 125 insertions(+), 35 deletions(-) create mode 100644 cmd/micro/display/uiwindow.go diff --git a/cmd/micro/action/editpane.go b/cmd/micro/action/editpane.go index a017ec2d..cf9ad392 100644 --- a/cmd/micro/action/editpane.go +++ b/cmd/micro/action/editpane.go @@ -31,6 +31,7 @@ func NewBufEditPane(x, y, width, height int, b *buffer.Buffer) *EditPane { func NewTabPane(width, height int, b *buffer.Buffer) *TabPane { t := new(TabPane) t.Node = views.NewRoot(0, 0, width, height) + t.Window = display.NewUIWindow(t.Node) e := NewBufEditPane(0, 0, width, height, b) e.splitID = t.ID() diff --git a/cmd/micro/action/tab.go b/cmd/micro/action/tab.go index bb9e997f..5bb1008b 100644 --- a/cmd/micro/action/tab.go +++ b/cmd/micro/action/tab.go @@ -1,6 +1,8 @@ package action import ( + "github.com/zyedidia/micro/cmd/micro/display" + "github.com/zyedidia/micro/cmd/micro/screen" "github.com/zyedidia/micro/cmd/micro/views" "github.com/zyedidia/tcell" ) @@ -9,12 +11,20 @@ var MainTab *TabPane type TabPane struct { *views.Node + display.Window Panes []*EditPane active int + + resizing bool } func (t *TabPane) HandleEvent(event tcell.Event) { switch e := event.(type) { + case *tcell.EventResize: + w, h := screen.Screen.Size() + InfoBar.Resize(w, h-1) + t.Node.Resize(w, h-1) + t.Resize() case *tcell.EventMouse: switch e.Buttons() { case tcell.Button1: diff --git a/cmd/micro/display/infowindow.go b/cmd/micro/display/infowindow.go index 603c56c3..44a5b48d 100644 --- a/cmd/micro/display/infowindow.go +++ b/cmd/micro/display/infowindow.go @@ -50,6 +50,7 @@ func NewInfoWindow(b *info.InfoBuf) *InfoWindow { func (i *InfoWindow) Resize(w, h int) { i.width = w + i.y = h } // func (i *InfoWindow) YesNoPrompt() (bool, bool) { diff --git a/cmd/micro/display/uiwindow.go b/cmd/micro/display/uiwindow.go new file mode 100644 index 00000000..6d866aef --- /dev/null +++ b/cmd/micro/display/uiwindow.go @@ -0,0 +1,63 @@ +package display + +import ( + "github.com/zyedidia/micro/cmd/micro/buffer" + "github.com/zyedidia/micro/cmd/micro/config" + "github.com/zyedidia/micro/cmd/micro/screen" + "github.com/zyedidia/micro/cmd/micro/views" +) + +type UIWindow struct { + root *views.Node +} + +func NewUIWindow(n *views.Node) *UIWindow { + uw := new(UIWindow) + uw.root = n + return uw +} + +func (w *UIWindow) drawNode(n *views.Node) { + cs := n.Children() + for i, c := range cs { + if c.IsLeaf() && c.Kind == views.STVert { + if i != len(cs)-1 { + for h := 0; h < c.H; h++ { + screen.Screen.SetContent(c.X+c.W, c.Y+h, '|', nil, config.DefStyle.Reverse(true)) + } + } + } else { + w.drawNode(c) + } + } +} + +func (w *UIWindow) Display() { + w.drawNode(w.root) +} + +func (w *UIWindow) Clear() {} +func (w *UIWindow) Relocate() bool { return false } +func (w *UIWindow) GetView() *View { return nil } +func (w *UIWindow) SetView(*View) {} +func (w *UIWindow) GetMouseLoc(vloc buffer.Loc) buffer.Loc { + var mouseLoc func(*views.Node) buffer.Loc + mouseLoc = func(n *views.Node) buffer.Loc { + cs := n.Children() + for i, c := range cs { + if c.IsLeaf() && c.Kind == views.STVert { + if i != len(cs)-1 { + if vloc.X == c.X+c.W { + return vloc + } + } + } else { + return mouseLoc(c) + } + } + return buffer.Loc{} + } + return mouseLoc(w.root) +} +func (w *UIWindow) Resize(width, height int) {} +func (w *UIWindow) SetActive(b bool) {} diff --git a/cmd/micro/info/infobuffer.go b/cmd/micro/info/infobuffer.go index 1a29cfc1..4e22d2f6 100644 --- a/cmd/micro/info/infobuffer.go +++ b/cmd/micro/info/infobuffer.go @@ -115,7 +115,10 @@ func (i *InfoBuf) YNPrompt(prompt string, donecb func(bool, bool)) { // DonePrompt finishes the current prompt and indicates whether or not it was canceled func (i *InfoBuf) DonePrompt(canceled bool) { - if i.PromptCallback != nil && !i.HasYN { + hadYN := i.HasYN + i.HasPrompt = false + i.HasYN = false + if i.PromptCallback != nil && !hadYN { if canceled { i.PromptCallback("", true) h := i.History[i.PromptType] @@ -127,11 +130,9 @@ func (i *InfoBuf) DonePrompt(canceled bool) { h[len(h)-1] = resp } } - if i.YNCallback != nil && i.HasYN { + if i.YNCallback != nil && hadYN { i.YNCallback(i.YNResp, canceled) } - i.HasPrompt = false - i.HasYN = false i.PromptCallback = nil i.EventCallback = nil i.YNCallback = nil diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index cdece46d..51349922 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -209,6 +209,7 @@ func main() { for _, ep := range action.MainTab.Panes { ep.Display() } + action.MainTab.Display() action.InfoBar.Display() screen.Screen.Show() diff --git a/cmd/micro/views/splits.go b/cmd/micro/views/splits.go index 6b28f3e3..144437ba 100644 --- a/cmd/micro/views/splits.go +++ b/cmd/micro/views/splits.go @@ -29,7 +29,7 @@ type View struct { type Node struct { View - kind SplitType + Kind SplitType parent *Node children []*Node @@ -69,6 +69,9 @@ func (n *Node) GetView() View { func (n *Node) SetView(v View) { n.X, n.Y, n.W, n.H = v.X, v.Y, v.W, v.H } +func (n *Node) Children() []*Node { + return n.children +} func (n *Node) GetNode(id uint64) *Node { if n.id == id && n.IsLeaf() { @@ -86,9 +89,9 @@ func (n *Node) GetNode(id uint64) *Node { return nil } -func NewNode(kind SplitType, x, y, w, h int, parent *Node, id uint64) *Node { +func NewNode(Kind SplitType, x, y, w, h int, parent *Node, id uint64) *Node { n := new(Node) - n.kind = kind + n.Kind = Kind n.canResize = true n.propScale = true n.X, n.Y, n.W, n.H = x, y, w, h @@ -145,7 +148,7 @@ func (n *Node) ResizeSplit(size int) bool { ind = i } } - if n.parent.kind == STVert { + if n.parent.Kind == STVert { return n.parent.vResizeSplit(ind, size) } return n.parent.hResizeSplit(ind, size) @@ -282,10 +285,10 @@ func (n *Node) HSplit(bottom bool) uint64 { if !n.IsLeaf() { return 0 } - if n.kind == STUndef { - n.kind = STVert + if n.Kind == STUndef { + n.Kind = STVert } - if n.kind == STVert { + if n.Kind == STVert { return n.vHSplit(0, bottom) } return n.hHSplit(bottom) @@ -295,32 +298,41 @@ func (n *Node) VSplit(right bool) uint64 { if !n.IsLeaf() { return 0 } - if n.kind == STUndef { - n.kind = STHoriz + if n.Kind == STUndef { + n.Kind = STHoriz } - if n.kind == STVert { + if n.Kind == STVert { return n.vVSplit(right) } return n.hVSplit(0, right) } func (n *Node) Resize(w, h int) { - propW, propH := float64(w)/float64(n.W), float64(h)/float64(n.H) - x, y := n.X, n.Y - for _, c := range n.children { - cW := int(float64(c.W) * propW) - cH := int(float64(c.H) * propH) - c.Resize(cW, cH) - c.X = x - c.Y = y - if n.kind == STHoriz { - x += cW - } else { - y += cH + if n.IsLeaf() { + n.W, n.H = w, h + } else { + propW, propH := float64(w)/float64(n.W), float64(h)/float64(n.H) + log.Println(w, h, n.W, n.H, propW, propH) + x, y := n.X, n.Y + for i, c := range n.children { + cW := int(float64(c.W) * propW) + // if c.IsLeaf() && i != len(n.children)-1 { + // cW++ + // } + log.Println("WIDTH:", cW, c.W) + cH := int(float64(c.H) * propH) + c.Resize(cW, cH) + c.X = x + c.Y = y + if n.Kind == STHoriz { + x += cW + } else { + y += cH + } } + n.alignSize() + n.W, n.H = w, h } - n.alignSize() - n.W, n.H = w, h } func (n *Node) alignSize() { @@ -329,21 +341,22 @@ func (n *Node) alignSize() { } totw, toth := 0, 0 - for _, c := range n.children { - if n.kind == STHoriz { + for i, c := range n.children { + if n.Kind == STHoriz { + if i != len(n.children)-1 { + c.Resize(c.W-1, c.H) + } totw += c.W } else { toth += c.H } } - if n.kind == STVert && toth != n.H { + if n.Kind == STVert && toth != n.H { last := n.children[len(n.children)-1] last.Resize(last.W, last.H+n.H-toth) - log.Println("bad height") - } else if n.kind == STHoriz && totw != n.W { + } else if n.Kind == STHoriz && totw != n.W { last := n.children[len(n.children)-1] last.Resize(last.W+n.W-totw, last.H) - log.Println("bad width") } } @@ -355,7 +368,7 @@ func (n *Node) String() string { var strf func(n *Node, ident int) string strf = func(n *Node, ident int) string { marker := "|" - if n.kind == STHoriz { + if n.Kind == STHoriz { marker = "-" } str := fmt.Sprint(strings.Repeat("\t", ident), marker, n.View, n.id)