This commit is contained in:
akiyosi 2019-10-22 12:04:36 +09:00
parent 124b4d134e
commit 1777efebd1
4 changed files with 319 additions and 67 deletions

View file

@ -502,26 +502,6 @@ func (e *Editor) copyClipBoard() {
}
func (e *Editor) sidebarToggle() {
side := e.wsSide
if side == nil {
return
}
if side.isShown {
side.scrollarea.Hide()
side.isShown = false
} else {
side.scrollarea.Show()
side.isShown = true
// for _, item := range side.items {
// if item.active {
// fileitems := item.Filelist.Fileitems
// fileitems[0].selectItem()
// }
// }
}
}
func (e *Editor) workspaceNew() {
editor.isSetGuiColor = false
ws, err := newWorkspace("")

View file

@ -12,6 +12,7 @@ import (
"time"
"github.com/akiyosi/gonvim/fuzzy"
"github.com/akiyosi/gonvim/filer"
"github.com/akiyosi/gonvim/util"
shortpath "github.com/akiyosi/short_path"
"github.com/jessevdk/go-flags"
@ -368,7 +369,10 @@ func (w *Workspace) attachUI(path string) error {
w.statusline.subscribe()
w.loc.subscribe()
w.message.subscribe()
// Add editor feature
fuzzy.RegisterPlugin(w.nvim, w.uiRemoteAttached)
filer.RegisterPlugin(w.nvim)
w.uiAttached = true
err := w.nvim.AttachUI(w.cols, w.rows, w.attachUIOption())
@ -442,7 +446,7 @@ func (w *Workspace) initGonvim() {
`
}
registerScripts := fmt.Sprintf(`call execute(%s)`, splitVimscript(gonvimAutoCmds))
registerScripts := fmt.Sprintf(`call execute(%s)`, util.SplitVimscript(gonvimAutoCmds))
w.nvim.Command(registerScripts)
gonvimCommands := fmt.Sprintf(`
@ -450,7 +454,7 @@ func (w *Workspace) initGonvim() {
command! GonvimVersion echo "%s"`, editor.version)
if !w.uiRemoteAttached {
gonvimCommands = gonvimCommands + `
command! GonvimSidebarShow call rpcnotify(0, "Gui", "gonvim_sidebar_toggle")
command! GonvimSidebarShow call rpcnotify(0, "Gui", "side_show")
command! GonvimWorkspaceNew call rpcnotify(0, "Gui", "gonvim_workspace_new")
command! GonvimWorkspaceNext call rpcnotify(0, "Gui", "gonvim_workspace_next")
command! GonvimWorkspacePrevious call rpcnotify(0, "Gui", "gonvim_workspace_previous")
@ -460,7 +464,7 @@ func (w *Workspace) initGonvim() {
`
}
registerCommands := fmt.Sprintf(`call execute(%s)`, splitVimscript(gonvimCommands))
registerCommands := fmt.Sprintf(`call execute(%s)`, util.SplitVimscript(gonvimCommands))
w.nvim.Command(registerCommands)
gonvimInitNotify := `
@ -472,28 +476,10 @@ func (w *Workspace) initGonvim() {
call rpcnotify(0, "Gui", "gonvim_minimap_update")
`
}
initialNotify := fmt.Sprintf(`call execute(%s)`, splitVimscript(gonvimInitNotify))
initialNotify := fmt.Sprintf(`call execute(%s)`, util.SplitVimscript(gonvimInitNotify))
w.nvim.Command(initialNotify)
}
func splitVimscript(s string) string {
if string(s[0]) == "\n" {
s = strings.TrimPrefix(s, string("\n"))
}
listLines := "["
lines := strings.Split(s, "\n")
for i, line := range lines {
listLines = listLines + `'` + line + `'`
if i == len(lines)-1 {
listLines = listLines + "]"
} else {
listLines = listLines + ","
}
}
return listLines
}
func (w *Workspace) loadGinitVim() {
if editor.config.Editor.GinitVim != "" {
scripts := strings.NewReplacer("\r\n", "\n", "\r", "\n", "\n", "\n").Replace(editor.config.Editor.GinitVim)
@ -1033,6 +1019,16 @@ func (w *Workspace) handleRPCGui(updates []interface{}) {
w.signature.pos(updates[1:])
case "signature_hide":
w.signature.hide()
case "side_show":
editor.wsSide.show()
case "filer_clear":
editor.wsSide.items[editor.active].clear()
case "filer_item_add":
editor.wsSide.items[editor.active].addItem(updates[1:])
case "filer_item_select":
editor.wsSide.items[editor.active].selectItem(updates[1:])
case "side_toggle":
editor.wsSide.toggle()
case "gonvim_cursormoved":
pos := updates[1].([]interface{})
ln := util.ReflectToInt(pos[1])
@ -1052,8 +1048,6 @@ func (w *Workspace) handleRPCGui(updates []interface{}) {
go editor.copyClipBoard()
case "gonvim_get_maxline":
w.maxLine = util.ReflectToInt(updates[1])
case "gonvim_sidebar_toggle":
editor.sidebarToggle()
case "gonvim_workspace_new":
editor.workspaceNew()
case "gonvim_workspace_next":
@ -1253,9 +1247,47 @@ func (s *WorkspaceSide) newScrollArea() {
s.scrollarea = sideArea
s.scrollarea.SetWidget(s.widget)
// s.scrollarea.ConnectKeyPressEvent(func(event *gui.QKeyEvent){
// return
// })
s.scrollarea.ConnectResizeEvent(func(*gui.QResizeEvent) {
width := s.scrollarea.Width()
for _, item := range s.items {
item.label.SetMaximumWidth(width)
item.label.SetMinimumWidth(width)
item.content.SetMinimumWidth(width)
item.content.SetMinimumWidth(width)
}
})
}
func (side *WorkspaceSide) toggle() {
if side == nil {
return
}
if side.isShown {
side.scrollarea.Hide()
side.isShown = false
} else {
side.scrollarea.Show()
side.isShown = true
// for _, item := range side.items {
// if item.active {
// fileitems := item.Filelist.Fileitems
// fileitems[0].selectItem()
// }
// }
}
}
func (side *WorkspaceSide) show() {
if side == nil {
return
}
if side.isShown {
return
}
side.scrollarea.Show()
side.isShown = true
side.items[editor.active].openContent()
}
// WorkspaceSideItem is
@ -1277,6 +1309,9 @@ type WorkspaceSideItem struct {
labelWidget *widgets.QWidget
label *widgets.QLabel
content *widgets.QListWidget
isContentHide bool
}
func newWorkspaceSideItem() *WorkspaceSideItem {
@ -1290,11 +1325,11 @@ func newWorkspaceSideItem() *WorkspaceSideItem {
labelLayout := widgets.NewQHBoxLayout()
labelWidget.SetLayout(labelLayout)
labelLayout.SetContentsMargins(15, 1, 1, 1)
labelLayout.SetSpacing(editor.iconSize/2)
label := widgets.NewQLabel(nil, 0)
label.SetContentsMargins(0, 0, 0, 0)
label.SetMaximumWidth(editor.config.SideBar.Width)
label.SetMinimumWidth(editor.config.SideBar.Width)
label.SetAlignment(core.Qt__AlignLeft)
openIcon := svg.NewQSvgWidget(nil)
openIcon.SetFixedWidth(editor.iconSize - 1)
@ -1308,13 +1343,23 @@ func newWorkspaceSideItem() *WorkspaceSideItem {
svgContent = editor.getSvg("chevron-right", nil)
closeIcon.Load2(core.NewQByteArray2(svgContent, len(svgContent)))
content := widgets.NewQListWidget(nil)
content.SetFocusPolicy(core.Qt__NoFocus)
labelLayout.AddWidget(openIcon, 0, 0)
labelLayout.AddWidget(closeIcon, 0, 0)
labelLayout.AddWidget(label, 0, 0)
labelLayout.SetAlignment(openIcon, core.Qt__AlignLeft)
labelLayout.SetAlignment(closeIcon, core.Qt__AlignLeft)
labelLayout.SetAlignment(label, core.Qt__AlignLeft)
// layout.AddWidget(flwidget, 0, 0)
layout.AddWidget(labelWidget, 0, 0)
layout.AddWidget(labelWidget, 1, 0)
layout.AddWidget(content, 0, 0)
layout.SetAlignment(labelWidget, core.Qt__AlignLeft)
layout.SetAlignment(content, core.Qt__AlignLeft)
openIcon.Hide()
closeIcon.Hide()
@ -1325,13 +1370,41 @@ func newWorkspaceSideItem() *WorkspaceSideItem {
label: label,
openIcon: openIcon,
closeIcon: closeIcon,
content: content,
}
// sideitem.widget.ConnectMousePressEvent(sideitem.toggleFilelist)
sideitem.widget.ConnectMousePressEvent(sideitem.toggleContent)
return sideitem
}
func (i *WorkspaceSideItem) toggleContent(event *gui.QMouseEvent) {
if i.hidden {
return
}
if i.isContentHide {
i.openContent()
} else {
i.closeContent()
}
}
func (i *WorkspaceSideItem) openContent() {
i.openIcon.Show()
i.closeIcon.Hide()
i.isContentHide = false
i.content.Show()
}
func (i *WorkspaceSideItem) closeContent() {
i.openIcon.Hide()
i.closeIcon.Show()
i.isContentHide = true
i.content.Hide()
}
func (i *WorkspaceSideItem) setText(text string) {
if i.text == text {
return
@ -1350,6 +1423,36 @@ func (i *WorkspaceSideItem) setSideItemLabel(n int) {
i.label.SetContentsMargins(1, 3, 0, 3)
}
func (i *WorkspaceSideItem) clear() {
i.content.Clear()
}
func (i *WorkspaceSideItem) addItem(args []interface{}) {
filename := args[0].(string)
filetype := args[1].(string)
l := widgets.NewQListWidgetItem(i.content, 1)
// l.SetSizeHint(core.NewQSize2(200, 25))
// w := widgets.NewQWidget(i.content, 0)
var svg string
if filetype == `/` {
svg = editor.getSvg("directory", nil)
} else {
svg = editor.getSvg(filetype, nil)
}
pixmap := gui.NewQPixmap()
pixmap.LoadFromData2(core.NewQByteArray2(svg, len(svg)), "SVG", core.Qt__ColorOnly)
icon := gui.NewQIcon2(pixmap)
i.content.AddItem2(l)
// i.content.SetItemWidget(l, w)
l.SetIcon(icon)
l.SetText(filename)
}
func (i *WorkspaceSideItem) selectItem(args []interface{}) {
i.content.SetCurrentRow(util.ReflectToInt(args[0]))
}
func (s *WorkspaceSide) setColor() {
fg := editor.colors.sideBarFg.String()
sfg := editor.colors.scrollBarFg.String()
@ -1361,6 +1464,18 @@ func (s *WorkspaceSide) setColor() {
}
s.scrollarea.SetStyleSheet(fmt.Sprintf(".QScrollBar { border-width: 0px; background-color: %s; width: 5px; margin: 0 0 0 0; } .QScrollBar::handle:vertical {background-color: %s; min-height: 25px;} .QScrollBar::handle:vertical:hover {background-color: %s; min-height: 25px;} .QScrollBar::add-line:vertical, .QScrollBar::sub-line:vertical { border: none; background: none; } .QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical { background: none; }", sbg, sfg, editor.config.SideBar.AccentColor))
for _, item := range s.items {
item.content.SetStyleSheet(fmt.Sprintf(`
QListWidget::item {
color: %s;
background-color: rgba(0, 0, 0, 0.0);
}
QListWidget::item:selected {
background-color: %s;
}
`, fg, editor.colors.selectedBg.String()))
}
if len(editor.workspaces) == 1 {
s.items[0].active = true
s.items[0].labelWidget.SetStyleSheet(fmt.Sprintf(" * { background-color: %s; color: %s; }", editor.colors.sideBarSelectedItemBg, fg))
@ -1408,15 +1523,15 @@ func (i *WorkspaceSideItem) show() {
i.hidden = false
i.label.Show()
// if !i.isFilelistHide {
// i.Filelistwidget.Show()
// i.openIcon.Show()
// i.closeIcon.Hide()
// } else {
// i.Filelistwidget.Hide()
// i.openIcon.Hide()
// i.closeIcon.Show()
// }
if !i.isContentHide {
i.content.Show()
i.openIcon.Show()
i.closeIcon.Hide()
} else {
i.content.Hide()
i.openIcon.Hide()
i.closeIcon.Show()
}
}
func (i *WorkspaceSideItem) hide() {
@ -1428,5 +1543,5 @@ func (i *WorkspaceSideItem) hide() {
i.openIcon.Hide()
i.closeIcon.Hide()
// i.Filelistwidget.Hide()
i.content.Hide()
}

View file

@ -2,24 +2,161 @@ package filer
import (
"fmt"
"strings"
"unicode/utf8"
"github.com/akiyosi/gonvim/util"
// "github.com/akiyosi/gonvim/util"
"github.com/neovim/go-client/nvim"
"github.com/therecipe/qt/widgets"
)
type Filer struct {
Widget *widgets.QListWidget
nvim *nvim.Nvim
isOpen bool
cancelled bool
cwd string
selectnum int
items [](map[string]string)
}
// RegisterPlugin registers this remote plugin
func RegisterPlugin(nvim *nvim.Nvim, isRemoteAttachment bool) {
func RegisterPlugin(nvim *nvim.Nvim) {
nvim.Subscribe("GonvimFiler")
shim := &Filer{
nvim: nvim,
max: 20,
isRemoteAttachment: isRemoteAttachment,
}
nvim.RegisterHandler("GonvimFuzzy", func(args ...interface{}) {
nvim.RegisterHandler("GonvimFiler", func(args ...interface{}) {
go shim.handle(args...)
})
}
func (f *Filer) handle(args ...interface{}) {
if len(args) < 1 {
return
}
event, ok := args[0].(string)
if !ok {
return
}
switch event {
case "cancel":
f.cancel()
case "open":
f.open()
case "left":
f.left()
case "right":
f.right()
case "up":
f.up()
case "down":
f.down()
case "search":
f.search(args[1:])
default:
fmt.Println("unhandleld filer event", event)
}
}
func (f *Filer) open() {
f.nvim.Call("rpcnotify", nil, 0, "Gui", "side_show")
f.nvim.Call("rpcnotify", nil, 0, "Gui", "filer_clear")
pwd := ""
f.nvim.Eval(`expand(getcwd())`, &pwd)
if f.cwd != pwd {
f.selectnum = 0
}
f.cwd = pwd
pwdlen := utf8.RuneCountInString(pwd)+1
command := fmt.Sprintf("globpath(expand(getcwd()), '{,.}*', 1, 0)")
files := ""
f.nvim.Eval(command, &files)
var items []map[string]string
for _, file := range strings.Split(files, "\n") {
file = file[pwdlen:]
// Skip './' and '../'
if file[len(file)-2:] == "./" || file[len(file)-3:] == "../" {
continue
}
// Remove current dir string
parts := strings.SplitN(file, ".", -1)
filetype := ""
if len(parts) > 1 {
filetype = parts[len(parts)-1]
}
// If it is directory
if file[len(file)-1] == '/' {
filetype = `/`
}
f.nvim.Call("rpcnotify", nil, 0, "Gui", "filer_item_add", file, filetype)
item := make(map[string]string)
item["filename"] = file
item["filetype"] = filetype
items = append(items, item)
}
f.items = items
f.nvim.Call("rpcnotify", nil, 0, "Gui", "filer_item_select", f.selectnum)
}
func (f *Filer) up() {
f.selectnum--
if f.selectnum < 0 {
f.selectnum = 0
}
f.nvim.Call("rpcnotify", nil, 0, "Gui", "filer_item_select", f.selectnum)
}
func (f *Filer) down() {
f.selectnum++
if f.selectnum >= len(f.items) {
f.selectnum = len(f.items)-1
}
f.nvim.Call("rpcnotify", nil, 0, "Gui", "filer_item_select", f.selectnum)
}
func (f *Filer) left() {
go func() {
f.nvim.Command("cd ..")
f.open()
}()
}
func (f *Filer) right() {
filename := f.items[f.selectnum]["filename"]
filetype := f.items[f.selectnum]["filetype"]
openCommand := ""
switch filetype {
case "/":
openCommand = ":cd " + filename
default:
openCommand = ":e " + filename
}
fmt.Println(openCommand)
go func() {
f.nvim.Command(openCommand)
f.open()
f.nvim.Input("<Esc>")
}()
}
func (f *Filer) search(args []interface{}) {
}
func (f *Filer) cancel() {
}

View file

@ -1,6 +1,8 @@
package util
import (
"strings"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/widgets"
@ -64,6 +66,24 @@ func IsTrue(d interface{}) bool {
return false
}
func SplitVimscript(s string) string {
if string(s[0]) == "\n" {
s = strings.TrimPrefix(s, string("\n"))
}
listLines := "["
lines := strings.Split(s, "\n")
for i, line := range lines {
listLines = listLines + `'` + line + `'`
if i == len(lines)-1 {
listLines = listLines + "]"
} else {
listLines = listLines + ","
}
}
return listLines
}
func DropShadow(x, y, radius float64, alpha int) *widgets.QGraphicsDropShadowEffect{
shadow := widgets.NewQGraphicsDropShadowEffect(nil)
shadow.SetBlurRadius(radius)