diff --git a/gui/action.go b/action/action.go similarity index 56% rename from gui/action.go rename to action/action.go index 22ca7ed..181ee28 100644 --- a/gui/action.go +++ b/action/action.go @@ -1,18 +1,20 @@ -package gui +package action import ( "log" "strconv" "strings" + + "github.com/felixangell/phi/buff" ) type BufferAction struct { name string - proc func(*View, []string) bool + proc func(*buff.BufferView, []string) bool showInPalette bool } -func NewBufferAction(name string, proc func(*View, []string) bool) BufferAction { +func NewBufferAction(name string, proc func(*buff.BufferView, []string) bool) BufferAction { return BufferAction{ name: name, proc: proc, @@ -20,7 +22,7 @@ func NewBufferAction(name string, proc func(*View, []string) bool) BufferAction } } -func OpenFile(v *View, commands []string) bool { +func OpenFile(v *buff.BufferView, commands []string) bool { path := "" if path == "" { panic("unimplemented") @@ -42,7 +44,7 @@ func OpenFile(v *View, commands []string) bool { return false } -func NewFile(v *View, commands []string) bool { +func NewFile(v *buff.BufferView, commands []string) bool { // TODO some nice error stuff // have an error roll thing in the view? @@ -55,7 +57,7 @@ func NewFile(v *View, commands []string) bool { return false } -func GotoLine(v *View, commands []string) bool { +func GotoLine(v *buff.BufferView, commands []string) bool { if len(commands) == 0 { return false } @@ -75,7 +77,7 @@ func GotoLine(v *View, commands []string) bool { return false } -func focusLeft(v *View, commands []string) bool { +func focusLeft(v *buff.BufferView, commands []string) bool { if v == nil { return false } @@ -83,7 +85,7 @@ func focusLeft(v *View, commands []string) bool { return false } -func focusRight(v *View, commands []string) bool { +func focusRight(v *buff.BufferView, commands []string) bool { if v == nil { return false } @@ -91,7 +93,7 @@ func focusRight(v *View, commands []string) bool { return false } -func pageDown(v *View, commands []string) bool { +func pageDown(v *buff.BufferView, commands []string) bool { if v == nil { return false } @@ -107,7 +109,7 @@ func pageDown(v *View, commands []string) bool { return false } -func pageUp(v *View, commands []string) bool { +func pageUp(v *buff.BufferView, commands []string) bool { if v == nil { return false } @@ -122,24 +124,3 @@ func pageUp(v *View, commands []string) bool { } return false } - -var actions = map[string]BufferAction{ - "page_down": NewBufferAction("page_down", pageDown), - "page_up": NewBufferAction("page_up", pageUp), - - "undo": NewBufferAction("undo", Undo), - "redo": NewBufferAction("redo", Redo), - - "focus_left": NewBufferAction("focus_left", focusLeft), - "focus_right": NewBufferAction("focus_right", focusRight), - - "goto": NewBufferAction("goto", GotoLine), - "new": NewBufferAction("new", NewFile), - "open": NewBufferAction("open", OpenFile), - "save": NewBufferAction("save", Save), - "delete_line": NewBufferAction("delete_line", DeleteLine), - "close_buffer": NewBufferAction("close_buffer", CloseBuffer), - "paste": NewBufferAction("paste", Paste), - "show_palette": NewBufferAction("show_palette", ShowPalette), - "exit": NewBufferAction("exit", ExitPhi), -} diff --git a/gui/close_buffer.go b/action/close_buffer.go similarity index 81% rename from gui/close_buffer.go rename to action/close_buffer.go index 516cb65..a7e1f08 100644 --- a/gui/close_buffer.go +++ b/action/close_buffer.go @@ -1,10 +1,12 @@ -package gui +package action import ( "fmt" + + "github.com/felixangell/phi/buff" ) -func CloseBuffer(v *View, commands []string) bool { +func CloseBuffer(v *buff.BufferView, commands []string) bool { b := v.getCurrentBuff() if b == nil { return false diff --git a/action/delete_line.go b/action/delete_line.go new file mode 100644 index 0000000..3c0d381 --- /dev/null +++ b/action/delete_line.go @@ -0,0 +1,13 @@ +package action + +import "github.com/felixangell/phi/buff" + +func DeleteLine(v *buff.BufferView, commands []string) bool { + b := v.getCurrentBuff() + if b == nil { + return false + } + + b.deleteLine() + return true +} diff --git a/gui/exit_action.go b/action/exit_action.go similarity index 74% rename from gui/exit_action.go rename to action/exit_action.go index 57c577d..157ec15 100644 --- a/gui/exit_action.go +++ b/action/exit_action.go @@ -1,11 +1,13 @@ -package gui +package action import ( "log" "os" + + "github.com/felixangell/phi/buff" ) -func ExitPhi(v *View, commands []string) bool { +func ExitPhi(v *buff.BufferView, commands []string) bool { // todo this probably wont work... // would also be nice to have a thing // that asks if we want to save all buffers diff --git a/action/register.go b/action/register.go new file mode 100644 index 0000000..72f6e9b --- /dev/null +++ b/action/register.go @@ -0,0 +1,22 @@ +package action + +const Register = map[string]BufferAction{ + "page_down": NewBufferAction("page_down", pageDown), + "page_up": NewBufferAction("page_up", pageUp), + + "undo": NewBufferAction("undo", Undo), + "redo": NewBufferAction("redo", Redo), + + "focus_left": NewBufferAction("focus_left", focusLeft), + "focus_right": NewBufferAction("focus_right", focusRight), + + "goto": NewBufferAction("goto", GotoLine), + "new": NewBufferAction("new", NewFile), + "open": NewBufferAction("open", OpenFile), + "save": NewBufferAction("save", Save), + "delete_line": NewBufferAction("delete_line", DeleteLine), + "close_buffer": NewBufferAction("close_buffer", CloseBuffer), + "paste": NewBufferAction("paste", Paste), + "show_palette": NewBufferAction("show_palette", ShowPalette), + "exit": NewBufferAction("exit", ExitPhi), +} diff --git a/gui/shortcuts.go b/action/shortcuts.go similarity index 88% rename from gui/shortcuts.go rename to action/shortcuts.go index b47578b..420f281 100644 --- a/gui/shortcuts.go +++ b/action/shortcuts.go @@ -1,4 +1,4 @@ -package gui +package action import ( "bytes" @@ -10,16 +10,17 @@ import ( "path/filepath" "github.com/atotto/clipboard" + "github.com/felixangell/phi/buff" ) -func ShowPalette(v *View, commands []string) bool { +func ShowPalette(v *buff.BufferView, commands []string) bool { b := v.getCurrentBuff() v.UnfocusBuffers() v.focusPalette(b) return true } -func Paste(v *View, commands []string) bool { +func Paste(v *buff.BufferView, commands []string) bool { b := v.getCurrentBuff() if b == nil { return false @@ -37,7 +38,7 @@ func Paste(v *View, commands []string) bool { return false } -func Undo(v *View, commands []string) bool { +func Undo(v *buff.BufferView, commands []string) bool { b := v.getCurrentBuff() if b == nil { return false @@ -46,7 +47,7 @@ func Undo(v *View, commands []string) bool { return false } -func Redo(v *View, commands []string) bool { +func Redo(v *buff.BufferView, commands []string) bool { b := v.getCurrentBuff() if b == nil { return false @@ -67,7 +68,7 @@ func genFileName(dir, prefix, suffix string) string { // if the buffer is modified it will be // re-rendered. -func Save(v *View, commands []string) bool { +func Save(v *buff.BufferView, commands []string) bool { // TODO Config option for this. atomicFileSave := true diff --git a/gui/buffer.go b/buff/buffer.go similarity index 96% rename from gui/buffer.go rename to buff/buffer.go index 02dbee8..e3bbe7b 100644 --- a/gui/buffer.go +++ b/buff/buffer.go @@ -1,4 +1,4 @@ -package gui +package buff import ( "fmt" @@ -14,6 +14,7 @@ import ( "github.com/felixangell/fuzzysearch/fuzzy" "github.com/felixangell/phi/cfg" + "github.com/felixangell/phi/gui" "github.com/felixangell/phi/lex" "github.com/felixangell/phi/piecetable" "github.com/felixangell/strife" @@ -128,9 +129,9 @@ type BufferConfig struct { // Buffer is a structure representing // a buffer of text. type Buffer struct { - BaseComponent + gui.BaseComponent index int - parent *View + parent *BufferView curs *Cursor cfg *cfg.TomlConfig buffOpts BufferConfig @@ -144,7 +145,7 @@ type Buffer struct { } // NewBuffer creates a new buffer with the given configurations -func NewBuffer(conf *cfg.TomlConfig, buffOpts BufferConfig, parent *View, index int) *Buffer { +func NewBuffer(conf *cfg.TomlConfig, buffOpts BufferConfig, parent *BufferView, index int) *Buffer { config := conf if config == nil { config = cfg.NewDefaultConfig() @@ -472,14 +473,17 @@ func (b *Buffer) processTextInput(r rune) bool { key = string(unicode.ToLower(r)) } - actionName, actionExists := source[key] - if actionExists { - if action, ok := actions[actionName]; ok { - return action.proc(b.parent, []string{}) - } - } else { - log.Println("warning, unimplemented shortcut", shortcutName, "+", unicode.ToLower(r), "#", int(r), actionName) - } + log.Println("warning, unimplemented shortcut", shortcutName, "+", unicode.ToLower(r), "#", int(r), actionName) + + /* + actionName, actionExists := source[key] + if actionExists { + if action, ok := action.Register[actionName]; ok { + return action.proc(b.parent, []string{}) + } + } else { + } + */ } if shiftDown { @@ -728,7 +732,8 @@ func (b *Buffer) moveDown() { b.curs.move(offs, 1) - visibleLines := int(math.Ceil(float64(b.h-b.ey) / float64(lastCharH+pad))) + _, h := b.GetSize() + visibleLines := int(math.Ceil(float64(h-b.ey) / float64(lastCharH+pad))) if b.curs.y >= visibleLines && b.curs.y-b.cam.y == visibleLines { b.scrollDown(1) @@ -1316,9 +1321,12 @@ func (b *Buffer) syntaxHighlightLine(currLine string) map[int]syntaxRuneInfo { func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { // TODO load this from config files! + x, y := b.GetPos() + w, h := b.GetSize() + // BACKGROUND ctx.SetColor(strife.HexRGB(b.buffOpts.background)) - ctx.Rect(b.x, b.y, b.w, b.h, strife.Fill) + ctx.Rect(x, y, w, h, strife.Fill) if b.cfg.Editor.Highlight_Line && b.HasFocus() { ctx.SetColor(strife.HexRGB(b.buffOpts.highlightLine)) // highlight_line_col? @@ -1326,7 +1334,7 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { highlightLinePosY := b.ey + (ry + b.curs.ry*(lastCharH+pad)) - (b.cam.y * (lastCharH + pad)) highlightLinePosX := b.ex + rx - ctx.Rect(highlightLinePosX, highlightLinePosY, b.w-b.ex, (lastCharH+pad)-b.ey, strife.Fill) + ctx.Rect(highlightLinePosX, highlightLinePosY, w-b.ex, (lastCharH+pad)-b.ey, strife.Fill) } var visibleLines, visibleChars int = 50, -1 @@ -1342,17 +1350,17 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { // lastCharH > 0 means we have done // a render. - if int(lastCharH) > 0 && int(b.h) != 0 { + if int(lastCharH) > 0 && int(h) != 0 { // render an extra three lines just // so we dont cut anything off if its // not evenly divisible - visibleLines = (int(b.h-b.ey) / int(lastCharH)) + 3 + visibleLines = (int(h-b.ey) / int(lastCharH)) + 3 } // calculate how many chars we can fit // on the screen. - if int(lastCharW) > 0 && int(b.w) != 0 { - visibleChars = (int(b.w-b.ex) / int(lastCharW)) - 3 + if int(lastCharW) > 0 && int(w) != 0 { + visibleChars = (int(w-b.ex) / int(lastCharW)) - 3 } start := b.cam.y @@ -1367,7 +1375,7 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { // render the selection if any if lastSelection != nil { - lastSelection.renderAt(ctx, b.x+b.ex, b.y+b.ey) + lastSelection.renderAt(ctx, x+b.ex, y+b.ey) } // calculate cursor sizes... does @@ -1458,7 +1466,7 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { ctx.SetColor(strife.HexRGB(characterColor.fg)) lastCharW, lastCharH = ctx.Text(string(char), xPos, yPos) - if DEBUG_MODE { + if cfg.DebugMode { ctx.SetColor(strife.HexRGB(0xff00ff)) ctx.Rect(xPos, yPos, lastCharW, lastCharH, strife.Line) } @@ -1477,11 +1485,11 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { // render the line numbers ctx.SetColor(strife.HexRGB(b.buffOpts.lineNumBackground)) - ctx.Rect(rx, yPos, gutterWidth, b.h, strife.Fill) + ctx.Rect(rx, yPos, gutterWidth, h, strife.Fill) - if DEBUG_MODE { + if cfg.DebugMode { ctx.SetColor(strife.HexRGB(0xff00ff)) - ctx.Rect(rx, yPos, gutterWidth, b.h, strife.Line) + ctx.Rect(rx, yPos, gutterWidth, h, strife.Line) } ctx.SetColor(strife.HexRGB(b.buffOpts.lineNumForeground)) @@ -1504,13 +1512,15 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { b.autoComplete.renderAt(xPos, yPos, ctx) } - if DEBUG_MODE { + if cfg.DebugMode { ctx.SetColor(strife.HexRGB(0xff00ff)) - ctx.Rect(b.ex+rx, b.ey+ry, b.w-b.ex, b.h-b.ey, strife.Line) + ctx.Rect(b.ex+rx, b.ey+ry, w-b.ex, h-b.ey, strife.Line) } } func (b *Buffer) OnRender(ctx *strife.Renderer) { + x, y := b.GetPos() + ctx.SetFont(b.buffOpts.font) - b.renderAt(ctx, b.x, b.y) + b.renderAt(ctx, x, y) } diff --git a/gui/buffer_pane.go b/buff/buffer_pane.go similarity index 83% rename from gui/buffer_pane.go rename to buff/buffer_pane.go index 9b1c520..529f99b 100644 --- a/gui/buffer_pane.go +++ b/buff/buffer_pane.go @@ -1,4 +1,4 @@ -package gui +package buff import ( "fmt" @@ -6,13 +6,14 @@ import ( "path/filepath" "github.com/felixangell/phi/cfg" + "github.com/felixangell/phi/gui" "github.com/felixangell/strife" ) var metaPanelHeight = 32 type BufferPane struct { - BaseComponent + gui.BaseComponent Buff *Buffer font *strife.Font } @@ -26,7 +27,7 @@ func NewBufferPane(buff *Buffer) *BufferPane { } return &BufferPane{ - BaseComponent{}, + gui.BaseComponent{}, buff, metaPanelFont, } @@ -42,8 +43,11 @@ func (b *BufferPane) SetFocus(focus bool) { func (b *BufferPane) renderMetaPanel(ctx *strife.Renderer) { conf := b.Buff.cfg.Theme.Palette + x, y := b.GetPos() + w, h := b.GetSize() + pad := 6 - mpY := (b.y + b.h) - (metaPanelHeight) + mpY := (y + h) - (metaPanelHeight) focused := b.Buff.index == b.Buff.parent.focusedBuff @@ -58,7 +62,7 @@ func (b *BufferPane) renderMetaPanel(ctx *strife.Renderer) { // panel backdrop ctx.SetColor(colour) - ctx.Rect(b.x, mpY, b.w, metaPanelHeight, strife.Fill) + ctx.Rect(x, mpY, w, metaPanelHeight, strife.Fill) // tab info etc. on right hand side { @@ -71,7 +75,7 @@ func (b *BufferPane) renderMetaPanel(ctx *strife.Renderer) { ctx.SetColor(strife.HexRGB(conf.Suggestion.Foreground)) ctx.SetFont(b.font) - lastWidth, _ = ctx.Text(infoLine, ((b.x + b.w) - (lastWidth + (pad))), mpY+(pad/2)) + lastWidth, _ = ctx.Text(infoLine, ((x + w) - (lastWidth + (pad))), mpY+(pad/2)) } { @@ -82,19 +86,19 @@ func (b *BufferPane) renderMetaPanel(ctx *strife.Renderer) { infoLine := fmt.Sprintf("%s%c Line %d, Column %d", b.Buff.filePath, modified, b.Buff.curs.y+1, b.Buff.curs.x) - if DEBUG_MODE { + if cfg.DebugMode { infoLine = fmt.Sprintf("%s, BuffIndex: %d", infoLine, b.Buff.index) } ctx.SetColor(strife.HexRGB(conf.Suggestion.Foreground)) ctx.SetFont(b.font) - _, strHeight := ctx.Text(infoLine, b.x+pad, mpY+(pad/2)+1) + _, strHeight := ctx.Text(infoLine, x+pad, mpY+(pad/2)+1) metaPanelHeight = strHeight + pad } // resize to match new height if any - b.Buff.Resize(b.w, b.h-metaPanelHeight) + b.Buff.Resize(w, h-metaPanelHeight) } func (b *BufferPane) Resize(w, h int) { diff --git a/gui/cursor.go b/buff/cursor.go similarity index 94% rename from gui/cursor.go rename to buff/cursor.go index a61b899..64c4773 100644 --- a/gui/cursor.go +++ b/buff/cursor.go @@ -1,6 +1,7 @@ -package gui +package buff import ( + "github.com/felixangell/phi/cfg" "github.com/felixangell/strife" ) @@ -72,7 +73,7 @@ func (c *Cursor) Render(ctx *strife.Renderer, xOff, yOff int) { ctx.SetColor(strife.HexRGB(b.buffOpts.cursor)) ctx.Rect(xPos, yPos, c.width, c.height, strife.Fill) - if DEBUG_MODE { + if cfg.DebugMode { ctx.SetColor(strife.HexRGB(0xff00ff)) ctx.Rect(xPos, yPos, c.width, c.height, strife.Line) } diff --git a/gui/palette.go b/buff/palette.go similarity index 88% rename from gui/palette.go rename to buff/palette.go index 4872dcf..eabdc1b 100644 --- a/gui/palette.go +++ b/buff/palette.go @@ -1,4 +1,4 @@ -package gui +package buff import ( "log" @@ -6,6 +6,7 @@ import ( "github.com/felixangell/fuzzysearch/fuzzy" "github.com/felixangell/phi/cfg" + "github.com/felixangell/phi/gui" "github.com/felixangell/strife" "github.com/veandco/go-sdl2/sdl" ) @@ -13,20 +14,20 @@ import ( var commandSet []string func init() { - commandSet = make([]string, len(actions)) - idx := 0 - for _, action := range actions { - commandSet[idx] = action.name - idx++ - } + // commandSet = make([]string, len(action.Register)) + // idx := 0 + // for _, action := range action.Register { + // commandSet[idx] = action.name + // idx++ + // } } type CommandPalette struct { - BaseComponent + gui.BaseComponent buff *Buffer parentBuff *Buffer conf *cfg.TomlConfig - parent *View + parent *BufferView pathToIndex map[string]int @@ -88,7 +89,7 @@ func (s *suggestion) render(x, y int, ctx *strife.Renderer) { ctx.Text(s.name, x+border, y+yOffs) } -func NewCommandPalette(conf cfg.TomlConfig, view *View) *CommandPalette { +func NewCommandPalette(conf cfg.TomlConfig, view *BufferView) *CommandPalette { conf.Editor.Show_Line_Numbers = false conf.Editor.Highlight_Line = false @@ -114,13 +115,16 @@ func NewCommandPalette(conf cfg.TomlConfig, view *View) *CommandPalette { } palette.buff.appendLine("") - palette.Resize(view.w/3, 48) - palette.Translate((view.w/2)-(palette.w/2), 10) + vW, vH := view.GetSize() + pW, pH := palette.GetSize() + + palette.Resize(vW/3, 48) + palette.Translate((vW/2)-(pW/2), 10) // the buffer is not rendered // relative to the palette so we have to set its position - palette.buff.Resize(palette.w, palette.h) - palette.buff.Translate((view.w/2)-(palette.w/2), 10) + palette.buff.Resize(pW, pH) + palette.buff.Translate((vW/2)-(pW/2), 10) // this is technically a hack. this ex is an xoffset // for the line numbers but we're going to use it for @@ -129,7 +133,7 @@ func NewCommandPalette(conf cfg.TomlConfig, view *View) *CommandPalette { palette.buff.ex = 5 palette.buff.ey = 0 - suggestionBoxWidth = palette.w + suggestionBoxWidth = pW return palette } @@ -150,12 +154,14 @@ func (b *CommandPalette) processCommand() { log.Println("command palette: ", tokenizedLine) - action, exists := actions[command] - if !exists { - return - } + /* + action, exists := action.Register[command] + if !exists { + return + } - action.proc(b.parent, tokenizedLine[1:]) + action.proc(b.parent, tokenizedLine[1:]) + */ return } @@ -333,11 +339,14 @@ func (b *CommandPalette) OnRender(ctx *strife.Renderer) { conf := b.conf.Theme.Palette + x, y := b.GetPos() + w, h := b.GetSize() + border := 5 - xPos := b.x - border - yPos := b.y - border - paletteWidth := b.w + (border * 2) - paletteHeight := b.h + (border * 2) + xPos := x - border + yPos := y - border + paletteWidth := w + (border * 2) + paletteHeight := h + (border * 2) ctx.SetColor(strife.HexRGB(conf.Outline)) ctx.Rect(xPos, yPos, paletteWidth, paletteHeight, strife.Fill) @@ -350,14 +359,14 @@ func (b *CommandPalette) OnRender(ctx *strife.Renderer) { if b.recentSuggestions != nil { for i, sugg := range *b.recentSuggestions { if b.suggestionIndex != i { - sugg.render(b.x, b.y+((i+1)*(suggestionBoxHeight+border)), ctx) + sugg.render(x, y+((i+1)*(suggestionBoxHeight+border)), ctx) } else { - sugg.renderHighlighted(b.x, b.y+((i+1)*(suggestionBoxHeight+border)), ctx) + sugg.renderHighlighted(x, y+((i+1)*(suggestionBoxHeight+border)), ctx) } } } - if DEBUG_MODE { + if cfg.DebugMode { ctx.SetColor(strife.HexRGB(0xff00ff)) ctx.Rect(xPos, yPos, paletteWidth, paletteHeight, strife.Line) } diff --git a/gui/view.go b/buff/view.go similarity index 80% rename from gui/view.go rename to buff/view.go index 458887c..54a1399 100644 --- a/gui/view.go +++ b/buff/view.go @@ -1,4 +1,4 @@ -package gui +package buff import ( "fmt" @@ -7,13 +7,14 @@ import ( "unicode" "github.com/felixangell/phi/cfg" + "github.com/felixangell/phi/gui" "github.com/felixangell/strife" "github.com/fsnotify/fsnotify" "github.com/veandco/go-sdl2/sdl" ) type bufferEvent interface { - Process(view *View) + Process(view *BufferView) String() string } @@ -21,7 +22,7 @@ type reloadBufferEvent struct { buff *Buffer } -func (r *reloadBufferEvent) Process(view *View) { +func (r *reloadBufferEvent) Process(view *BufferView) { log.Println("reloading buffer", r.buff.filePath) r.buff.reload() } @@ -31,8 +32,8 @@ func (r *reloadBufferEvent) String() string { } // View is an array of buffers basically. -type View struct { - BaseComponent +type BufferView struct { + gui.BaseComponent conf *cfg.TomlConfig buffers []*BufferPane @@ -46,8 +47,8 @@ type View struct { // NewView creaets a new view with the given width and height // as well as configurations. -func NewView(width, height int, conf *cfg.TomlConfig) *View { - view := &View{ +func NewView(width, height int, conf *cfg.TomlConfig) *BufferView { + view := &BufferView{ conf: conf, buffers: []*BufferPane{}, bufferMap: map[string]*Buffer{}, @@ -104,7 +105,7 @@ func NewView(width, height int, conf *cfg.TomlConfig) *View { return view } -func (n *View) registerFile(path string, buff *Buffer) { +func (n *BufferView) registerFile(path string, buff *Buffer) { log.Println("Registering file ", path) err := n.watcher.Add(path) @@ -117,11 +118,11 @@ func (n *View) registerFile(path string, buff *Buffer) { } // Close will close the view and all of the components -func (n *View) Close() { +func (n *BufferView) Close() { n.watcher.Close() } -func (n *View) hidePalette() { +func (n *BufferView) hidePalette() { p := n.commandPalette p.clearInput() p.SetFocus(false) @@ -130,7 +131,7 @@ func (n *View) hidePalette() { p.buff.SetFocus(false) } -func (n *View) focusPalette(buff *Buffer) { +func (n *BufferView) focusPalette(buff *Buffer) { p := n.commandPalette p.SetFocus(true) @@ -141,7 +142,7 @@ func (n *View) focusPalette(buff *Buffer) { // UnfocusBuffers will remove focus // from all of the buffers in this view. -func (n *View) UnfocusBuffers() { +func (n *BufferView) UnfocusBuffers() { // clear focus from buffers for _, buffPane := range n.buffers { buffPane.SetFocus(false) @@ -157,23 +158,25 @@ func sign(dir int) int { return 0 } -func (n *View) removeBuffer(index int) { +func (n *BufferView) removeBuffer(index int) { log.Println("Removing buffer index:", index) log.Println("num buffs before delete: ", len(n.buffers)) n.buffers = append(n.buffers[:index], n.buffers[index+1:]...) + w, h := n.GetSize() + // only resize the buffers if we have // some remaining in the window if len(n.buffers) > 0 { - bufferWidth := n.w / len(n.buffers) + bufferWidth := w / len(n.buffers) // translate all the components accordingly. for idx, buffPane := range n.buffers { // re-write the index. buffPane.Buff.index = idx - buffPane.Resize(bufferWidth, n.h) + buffPane.Resize(bufferWidth, h) buffPane.SetPosition(bufferWidth*idx, 0) } } @@ -186,7 +189,7 @@ func (n *View) removeBuffer(index int) { n.ChangeFocus(dir) } -func (n *View) setFocusTo(index int) { +func (n *BufferView) setFocusTo(index int) { log.Println("Moving focus from ", n.focusedBuff, " to ", index) n.UnfocusBuffers() @@ -203,7 +206,7 @@ func (n *View) setFocusTo(index int) { // // NOTE: if we have no buffers to the left, we will // wrap around to the buffer on the far right. -func (n *View) ChangeFocus(dir int) { +func (n *BufferView) ChangeFocus(dir int) { // we cant change focus if there are no // buffers to focus to if len(n.buffers) == 0 { @@ -228,7 +231,7 @@ func (n *View) ChangeFocus(dir int) { n.setFocusTo(newFocus) } -func (n *View) getCurrentBuffPane() *BufferPane { +func (n *BufferView) getCurrentBuffPane() *BufferPane { if len(n.buffers) == 0 { return nil } @@ -239,7 +242,7 @@ func (n *View) getCurrentBuffPane() *BufferPane { return nil } -func (n *View) getCurrentBuff() *Buffer { +func (n *BufferView) getCurrentBuff() *Buffer { buff := n.getCurrentBuffPane() if buff != nil { return buff.Buff @@ -248,11 +251,11 @@ func (n *View) getCurrentBuff() *Buffer { } // OnInit ... -func (n *View) OnInit() { +func (n *BufferView) OnInit() { } // OnUpdate ... -func (n *View) OnUpdate() bool { +func (n *BufferView) OnUpdate() bool { dirty := false controlDown = strife.KeyPressed(sdl.K_LCTRL) || strife.KeyPressed(sdl.K_RCTRL) @@ -277,7 +280,7 @@ func (n *View) OnUpdate() bool { r := rune(strife.PopKey()) if r == 'l' { - DEBUG_MODE = !DEBUG_MODE + cfg.DebugMode = !cfg.DebugMode } left := sdl.K_LEFT @@ -301,15 +304,17 @@ func (n *View) OnUpdate() bool { key = string(unicode.ToLower(r)) } - actionName, actionExists := source[key] - if actionExists { - if action, ok := actions[actionName]; ok { - log.Println("Executing action '" + actionName + "'") - return action.proc(n, []string{}) - } - } else { - log.Println("view: unimplemented shortcut", shortcutName, "+", string(unicode.ToLower(r)), "#", int(r), actionName, key) - } + /* + actionName, actionExists := source[key] + if actionExists { + if action, ok := action.Register[actionName]; ok { + log.Println("Executing action '" + actionName + "'") + return action.proc(n, []string{}) + } + } else { + log.Println("view: unimplemented shortcut", shortcutName, "+", string(unicode.ToLower(r)), "#", int(r), actionName, key) + } + */ } buff := n.getCurrentBuffPane() @@ -328,7 +333,7 @@ func (n *View) OnUpdate() bool { // simply: // // viewWidth / bufferCount -func (n *View) Resize(w, h int) { +func (n *BufferView) Resize(w, h int) { n.BaseComponent.Resize(w, h) // dont resize any buffer panes @@ -337,44 +342,47 @@ func (n *View) Resize(w, h int) { return } + oldWidth, oldHeight := n.GetSize() + // work out the size of the buffer and set it // note that we +1 the components because // we haven't yet added the panel - bufferWidth := n.w / len(n.buffers) + bufferWidth := oldWidth / len(n.buffers) // translate all the buffers accordingly. idx := 0 for _, buffPane := range n.buffers { - buffPane.Resize(bufferWidth, n.h) + buffPane.Resize(bufferWidth, oldHeight) buffPane.SetPosition(bufferWidth*idx, 0) idx++ } } // OnRender ... -func (n *View) OnRender(ctx *strife.Renderer) { +func (n *BufferView) OnRender(ctx *strife.Renderer) { for _, buffPane := range n.buffers { buffPane.OnRender(ctx) } n.commandPalette.OnRender(ctx) - if DEBUG_MODE { + if cfg.DebugMode { ctx.SetColor(strife.HexRGB(0xff00ff)) mx, my := strife.MouseCoords() ctx.Rect(mx, my, 16, 16, strife.Line) - renderDebugPane(ctx, 10, 10) + // FIXME + // renderDebugPane(ctx, 10, 10) } } // OnDispose ... -func (n *View) OnDispose() {} +func (n *BufferView) OnDispose() {} // AddBuffer will unfocus all of the buffers // and insert a new buffer. Focus is given to this // new buffer, which is then returned from this function. -func (n *View) AddBuffer() *Buffer { +func (n *BufferView) AddBuffer() *Buffer { n.UnfocusBuffers() cfg := n.conf @@ -391,9 +399,11 @@ func (n *View) AddBuffer() *Buffer { c.SetFocus(true) + w, h := n.GetSize() + n.focusedBuff = c.index n.buffers = append(n.buffers, NewBufferPane(c)) - n.Resize(n.w, n.h) + n.Resize(w, h) return c } diff --git a/cfg/flags.go b/cfg/flags.go new file mode 100644 index 0000000..fc242ee --- /dev/null +++ b/cfg/flags.go @@ -0,0 +1,5 @@ +package cfg + +var ( + DebugMode = false +) diff --git a/gui/component.go b/gui/component.go index 5baca57..677981e 100644 --- a/gui/component.go +++ b/gui/component.go @@ -4,13 +4,14 @@ import ( "github.com/felixangell/strife" ) -var DEBUG_MODE = false - type Component interface { SetPosition(x, y int) Translate(x, y int) Resize(w, h int) + GetPos() (int, int) + GetSize() (int, int) + OnInit() OnUpdate() bool OnRender(*strife.Renderer) @@ -32,6 +33,14 @@ type BaseComponent struct { focused bool } +func (b *BaseComponent) GetPos() (int, int) { + return b.x, b.y +} + +func (b *BaseComponent) GetSize() (int, int) { + return b.w, b.h +} + func (b *BaseComponent) SetFocus(focus bool) { b.focused = focus } diff --git a/gui/delete_line.go b/gui/delete_line.go deleted file mode 100644 index 352fd39..0000000 --- a/gui/delete_line.go +++ /dev/null @@ -1,11 +0,0 @@ -package gui - -func DeleteLine(v *View, commands []string) bool { - b := v.getCurrentBuff() - if b == nil { - return false - } - - b.deleteLine() - return true -} diff --git a/main.go b/main.go index 5f53766..a06168c 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "runtime" "time" + "github.com/felixangell/phi/buff" "github.com/felixangell/phi/cfg" "github.com/felixangell/phi/gui" "github.com/felixangell/strife" @@ -21,7 +22,7 @@ const ( type PhiEditor struct { running bool defaultFont *strife.Font - mainView *gui.View + mainView *buff.BufferView } func (n *PhiEditor) resize(w, h int) {