Skip to content

Commit

Permalink
fix input method
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Jan 18, 2018
1 parent 65bac43 commit 16d28c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
40 changes: 0 additions & 40 deletions editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ func InitEditor() {
e.initSpecialKeys()
e.window.ConnectKeyPressEvent(e.keyPress)

e.window.SetAttribute(core.Qt__WA_InputMethodEnabled, true)
e.window.ConnectInputMethodEvent(e.InputMethodEvent)
e.window.ConnectInputMethodQuery(e.InputMethodQuery)
e.window.SetAcceptDrops(true)

layout := widgets.NewQHBoxLayout()
Expand Down Expand Up @@ -248,43 +245,6 @@ func (e *Editor) keyPress(event *gui.QKeyEvent) {
}
}

// InputMethodEvent is
func (e *Editor) InputMethodEvent(event *gui.QInputMethodEvent) {
if event.CommitString() != "" {
e.workspaces[e.active].nvim.Input(event.CommitString())
e.workspaces[e.active].screen.tooltip.Hide()
} else {
preeditString := event.PreeditString()
if preeditString == "" {
e.workspaces[e.active].screen.tooltip.Hide()
e.workspaces[e.active].cursor.update()
} else {
e.workspaces[e.active].screen.toolTip(preeditString)
}
}
}

// InputMethodQuery is
func (e *Editor) InputMethodQuery(query core.Qt__InputMethodQuery) *core.QVariant {
qv := core.NewQVariant()
if query == core.Qt__ImCursorRectangle {
imrect := core.NewQRect()
row := e.workspaces[e.active].screen.cursor[0]
col := e.workspaces[e.active].screen.cursor[1]
x := int(float64(col + 6) * e.workspaces[e.active].font.truewidth)
y := 0
switch runtime.GOOS {
case "windows":
y = (row + 2) * e.workspaces[e.active].font.lineHeight + 2
default:
y = (row + 3) * e.workspaces[e.active].font.lineHeight + 5
}
imrect.SetRect(x, y, 1, 1)
return core.NewQVariant33(imrect)
}
return qv
}

func (e *Editor) convertKey(text string, key int, mod core.Qt__KeyboardModifier) string {
if mod&core.Qt__KeypadModifier > 0 {
switch core.Qt__Key(key) {
Expand Down
36 changes: 36 additions & 0 deletions editor/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/dzhou121/gonvim/fuzzy"
"github.com/neovim/go-client/nvim"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
)

Expand Down Expand Up @@ -166,6 +167,9 @@ func newWorkspace(path string) (*Workspace, error) {
w.widget.SetContentsMargins(0, 0, 0, 0)
w.widget.SetLayout(layout)
w.widget.SetFocusPolicy(core.Qt__WheelFocus)
w.widget.SetAttribute(core.Qt__WA_InputMethodEnabled, true)
w.widget.ConnectInputMethodEvent(w.InputMethodEvent)
w.widget.ConnectInputMethodQuery(w.InputMethodQuery)
layout.AddWidget(w.tabline.widget, 0, 0)
layout.AddWidget(w.screen.widget, 1, 0)
layout.AddWidget(w.statusline.widget, 0, 0)
Expand Down Expand Up @@ -205,6 +209,7 @@ func (w *Workspace) show() {
}
w.hidden = false
w.widget.Show()
w.widget.SetFocus2Default()
}

func (w *Workspace) startNvim(path string) error {
Expand Down Expand Up @@ -611,6 +616,37 @@ func (w *Workspace) guiLinespace(args ...interface{}) {
w.updateSize()
}

// InputMethodEvent is
func (w *Workspace) InputMethodEvent(event *gui.QInputMethodEvent) {
if event.CommitString() != "" {
w.nvim.Input(event.CommitString())
w.screen.tooltip.Hide()
} else {
preeditString := event.PreeditString()
if preeditString == "" {
w.screen.tooltip.Hide()
w.cursor.update()
} else {
w.screen.toolTip(preeditString)
}
}
}

// InputMethodQuery is
func (w *Workspace) InputMethodQuery(query core.Qt__InputMethodQuery) *core.QVariant {
qv := core.NewQVariant()
if query == core.Qt__ImCursorRectangle {
imrect := core.NewQRect()
row := w.screen.cursor[0]
col := w.screen.cursor[1]
x := int(float64(col)*w.font.truewidth) - 1
y := row*w.font.lineHeight + w.tabline.height + w.tabline.marginTop + w.tabline.marginBottom
imrect.SetRect(x, y, 1, w.font.lineHeight)
return core.NewQVariant33(imrect)
}
return qv
}

// WorkspaceSide is
type WorkspaceSide struct {
widget *widgets.QWidget
Expand Down

0 comments on commit 16d28c6

Please sign in to comment.