From c2c501ebd9a855a8b243ab68d1a16605f8bc9074 Mon Sep 17 00:00:00 2001 From: Dongdong Zhou Date: Thu, 15 Feb 2018 02:26:39 +0000 Subject: [PATCH] split cursor --- cmd/xi-go/xi-go.go | 11 ++++++++- editor/editor.go | 17 ++++++++++--- editor/win.go | 59 ++++++++++++++++++++++++++++++++++++---------- xi-client/xi.go | 7 +++--- 4 files changed, 73 insertions(+), 21 deletions(-) diff --git a/cmd/xi-go/xi-go.go b/cmd/xi-go/xi-go.go index 4c87e989..05929f63 100644 --- a/cmd/xi-go/xi-go.go +++ b/cmd/xi-go/xi-go.go @@ -1,8 +1,17 @@ package main -import "github.com/dzhou121/xi-go/editor" +import ( + "net/http" + + _ "net/http/pprof" + + "github.com/dzhou121/xi-go/editor" +) func main() { + go func() { + http.ListenAndServe("localhost:6020", nil) + }() editor, err := editor.NewEditor() if err != nil { return diff --git a/editor/editor.go b/editor/editor.go index 3a3a66e1..8d06f294 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -111,6 +111,7 @@ func NewEditor() (*Editor, error) { defer e.winsRWMutext.RUnlock() for _, win := range e.wins { win.view.SetStyleSheet(scrollBarStyleSheet) + win.cline.SetStyleSheet(e.getClineStylesheet()) } } }) @@ -123,6 +124,15 @@ func NewEditor() (*Editor, error) { return e, nil } +func (e *Editor) getClineStylesheet() string { + if e.theme == nil { + return "" + } + cline := e.theme.Theme.LineHighlight + styleSheet := fmt.Sprintf("background-color: rgba(%d, %d, %d, %f);", cline.R, cline.G, cline.B, float64(cline.A)/255) + return styleSheet +} + func (e *Editor) getScrollbarStylesheet() string { bg := e.theme.Theme.Background guide := e.theme.Theme.LineHighlight @@ -232,10 +242,8 @@ func (e *Editor) initMainWindow() { editor: e, } e.topWin = NewWindow(e, e.topFrame) - e.topWin.view.Move2(0, 0) - e.topWin.view.Resize2(e.width, e.height) e.topWin.loadBuffer(NewBuffer(e, "/Users/Lulu/xi-editor/rust/core-lib/src/rpc.rs")) - e.topWin.setScroll() + e.equalWins() e.cursor = widgets.NewQWidget(nil, 0) e.cursor.Resize2(1, 20) @@ -278,6 +286,9 @@ func (e *Editor) organizeWins() { win.view.Move2(win.frame.x, win.frame.y) win.view.Hide() win.view.Show() + win.cline.Resize2(win.frame.width, int(win.buffer.font.lineHeight)) + win.cline.Hide() + win.cline.Show() win.setScroll() } } diff --git a/editor/win.go b/editor/win.go index e9086ba5..462a826d 100644 --- a/editor/win.go +++ b/editor/win.go @@ -67,6 +67,7 @@ func (f *Frame) split(vertical bool) { win.editor.equalWins() newWin.view.HorizontalScrollBar().SetValue(win.view.HorizontalScrollBar().Value()) newWin.view.VerticalScrollBar().SetValue(win.view.VerticalScrollBar().Value()) + newWin.scrollto(newWin.col, newWin.row) f.setFocus() } @@ -295,8 +296,11 @@ type Window struct { id int editor *Editor view *widgets.QGraphicsView + cline *widgets.QWidget frame *Frame buffer *Buffer + x float64 + y float64 cursorX int cursorY int row int @@ -313,7 +317,32 @@ func NewWindow(editor *Editor, frame *Frame) *Window { editor: editor, frame: frame, view: widgets.NewQGraphicsView(nil), + cline: widgets.NewQWidget(nil, 0), } + + w.view.ConnectEventFilter(func(watched *core.QObject, event *core.QEvent) bool { + if event.Type() == core.QEvent__MouseButtonPress { + mousePress := gui.NewQMouseEventFromPointer(event.Pointer()) + w.view.MousePressEvent(mousePress) + return true + } + // else if event.Type() == core.QEvent__Wheel { + // wheelEvent := gui.NewQWheelEventFromPointer(event.Pointer()) + // // delta := wheelEvent.PixelDelta() + // // w.view.ScrollContentsByDefault(delta.X(), delta.Y()) + // // fmt.Println("scroll by", delta.X(), delta.Y()) + // w.view.WheelEventDefault(wheelEvent) + // return true + // } + return w.view.EventFilterDefault(watched, event) + }) + w.cline.SetParent(w.view) + w.cline.SetStyleSheet(editor.getClineStylesheet()) + w.cline.SetFocusPolicy(core.Qt__NoFocus) + w.cline.InstallEventFilter(w.view) + w.cline.ConnectWheelEvent(func(event *gui.QWheelEvent) { + w.view.WheelEventDefault(event) + }) frame.win = w editor.winIndex++ editor.wins[w.id] = w @@ -428,8 +457,20 @@ func (w *Window) update() { } } +func (w *Window) updateCursor() { + w.cursorX = int(w.x+0.5) - w.view.HorizontalScrollBar().Value() + w.cursorY = int(w.y+0.5) - w.view.VerticalScrollBar().Value() + cursor := w.editor.cursor + cursor.Move2(w.cursorX, w.cursorY) + cursor.Resize2(1, int(w.buffer.font.lineHeight+0.5)) + cursor.Hide() + cursor.Show() + w.cline.Move2(0, w.cursorY) +} + func (w *Window) setScroll() { w.update() + w.updateCursor() w.buffer.xiView.Scroll(w.start, w.end) } @@ -440,25 +481,17 @@ func (w *Window) loadBuffer(buffer *Buffer) { func (w *Window) scrollto(col, row int) { b := w.buffer - x := b.font.fontMetrics.Width(b.lines[row].text[:col]) - 0.5 - y := float64(row) * b.font.lineHeight + w.x = b.font.fontMetrics.Width(b.lines[row].text[:col]) - 0.5 + w.y = float64(row) * b.font.lineHeight w.view.EnsureVisible2( - x, - y, + w.x, + w.y, 1, b.font.lineHeight, 20, 20, ) - x -= float64(w.view.HorizontalScrollBar().Value()) - y -= float64(w.view.VerticalScrollBar().Value()) w.row = row w.col = col - w.cursorX = int(x + 0.5) - w.cursorY = int(y + 0.5) - cursor := w.editor.cursor - cursor.Move2(w.cursorX, w.cursorY) - cursor.Resize2(1, int(b.font.lineHeight+0.5)) - cursor.Hide() - cursor.Show() + w.updateCursor() } diff --git a/xi-client/xi.go b/xi-client/xi.go index 7f5e0f80..d8527d42 100644 --- a/xi-client/xi.go +++ b/xi-client/xi.go @@ -4,7 +4,6 @@ import ( "bufio" "context" "encoding/json" - "fmt" "io" "os/exec" @@ -139,9 +138,9 @@ func (h *handler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2 if err != nil { return } - fmt.Println("-------------------------") - fmt.Println(req.Method) - fmt.Println(string(params)) + // fmt.Println("-------------------------") + // fmt.Println(req.Method) + // fmt.Println(string(params)) switch req.Method { case "update": var notification UpdateNotification