diff --git a/editor/editor.go b/editor/editor.go index 5f8c08f6..90e1dc38 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -23,6 +23,7 @@ type Editor struct { buffers map[string]*Buffer buffersRWMutex sync.RWMutex + activeWin *Window winIndex int wins map[int]*Window winsRWMutext sync.RWMutex @@ -73,7 +74,16 @@ func NewEditor() (*Editor, error) { } buffer.applyUpdate(u) case *xi.ScrollTo: - // e.view.scrollto(u.Col, u.Line) + if e.activeWin == nil { + return + } + if e.activeWin.buffer == nil { + return + } + if e.activeWin.buffer.xiView.ID != u.ViewID { + return + } + e.activeWin.scrollto(u.Col, u.Line) } }) e.xi.HandleUpdate = e.handleUpdate diff --git a/editor/view.go b/editor/view.go index c1e9811f..444d9ca1 100644 --- a/editor/view.go +++ b/editor/view.go @@ -114,23 +114,31 @@ func NewView(e *Editor) *View { } func (v *View) scrollto(col, row int) { - if v.cursor == nil { - v.cursor = v.scence.AddRect2( - 0, - 0, - 1, - v.font.lineHeight, - gui.NewQPen(), - gui.NewQBrush3(gui.NewQColor3(0, 0, 0, 255), core.Qt__SolidPattern)) - } - if row >= len(v.lines) { - row = len(v.lines) - 1 - } - v.cursor.SetPos2( - v.font.fontMetrics.Width(v.lines[row].text[:col])-0.5, - float64(row)*v.font.lineHeight) - v.view.EnsureVisible3(v.cursor, 20, 20) + // if v.cursor == nil { + // v.cursor = v.scence.AddRect2( + // 0, + // 0, + // 1, + // v.font.lineHeight, + // gui.NewQPen(), + // gui.NewQBrush3(gui.NewQColor3(0, 0, 0, 255), core.Qt__SolidPattern)) + // } + // if row >= len(v.lines) { + // row = len(v.lines) - 1 + // } + // v.cursor.SetPos2( + // v.font.fontMetrics.Width(v.lines[row].text[:col])-0.5, + // ) + // v.view.EnsureVisible3(v.cursor, 20, 20) + v.view.EnsureVisible2( + v.font.fontMetrics.Width(v.lines[row].text[:col])-0.5, + float64(row)*v.font.lineHeight, + 1, + v.font.lineHeight, + 20, + 20, + ) } func (v *View) height() int { diff --git a/editor/win.go b/editor/win.go index 43baa35a..550099d0 100644 --- a/editor/win.go +++ b/editor/win.go @@ -148,6 +148,7 @@ func (f *Frame) setFocus() { return } f.win.view.SetFocus2() + f.editor.activeWin = f.win } func (f *Frame) close() *Frame { @@ -237,6 +238,7 @@ func NewWindow(editor *Editor, frame *Frame) *Window { editor.winsRWMutext.Unlock() w.view.ConnectKeyPressEvent(func(event *gui.QKeyEvent) { + editor.activeWin = w if w.buffer == nil { return } @@ -293,6 +295,7 @@ func NewWindow(editor *Editor, frame *Frame) *Window { w.buffer.xiView.Insert(event.Text()) }) w.view.ConnectScrollContentsBy(func(dx, dy int) { + editor.activeWin = w w.view.ScrollContentsByDefault(dx, dy) }) w.view.SetAlignment(core.Qt__AlignLeft | core.Qt__AlignTop) @@ -305,3 +308,15 @@ func (w *Window) loadBuffer(buffer *Buffer) { w.buffer = buffer w.view.SetScene(buffer.scence) } + +func (w *Window) scrollto(col, row int) { + b := w.buffer + w.view.EnsureVisible2( + b.font.fontMetrics.Width(b.lines[row].text[:col])-0.5, + float64(row)*b.font.lineHeight, + 1, + b.font.lineHeight, + 20, + 20, + ) +}