mirror of
https://github.com/felixangell/phi.git
synced 2024-08-16 19:20:21 +03:00
added a wip command palette; removed weird complex gui system
This commit is contained in:
parent
fe3c3c44a9
commit
be01667137
@ -49,6 +49,9 @@ flash = true
|
||||
[commands.save]
|
||||
shortcut = "ctrl+s"
|
||||
|
||||
[commands.show_palette]
|
||||
shortcut = "ctrl+p"
|
||||
|
||||
[commands.paste]
|
||||
shortcut = "ctrl+v"
|
||||
|
||||
|
@ -49,6 +49,9 @@ flash = true
|
||||
[commands.exit]
|
||||
shortcut = "ctrl+q"
|
||||
|
||||
[commands.show_palette]
|
||||
shortcut = "ctrl+p"
|
||||
|
||||
[commands.save]
|
||||
shortcut = "ctrl+s"
|
||||
|
||||
|
@ -52,6 +52,9 @@ shortcut = "super+q"
|
||||
[commands.save]
|
||||
shortcut = "super+s"
|
||||
|
||||
[commands.show_palette]
|
||||
shortcut = "super+p"
|
||||
|
||||
[commands.paste]
|
||||
shortcut = "super+v"
|
||||
|
||||
|
@ -9,6 +9,7 @@ var actions = map[string]BufferAction{
|
||||
"delete_line": DeleteLine,
|
||||
"close_buffer": CloseBuffer,
|
||||
"paste": Paste,
|
||||
"show_palette": ShowPalette,
|
||||
"exit": func(*Buffer) bool {
|
||||
// TODO do this properly lol
|
||||
os.Exit(0)
|
||||
|
@ -59,10 +59,10 @@ func NewBuffer(conf *cfg.TomlConfig, parent *View, index int) *Buffer {
|
||||
contents: buffContents,
|
||||
curs: &Cursor{},
|
||||
cfg: config,
|
||||
filePath: "/tmp/phi_file_" + time.Now().String(), // TODO make this a randomly chosen temp file
|
||||
filePath: "",
|
||||
cam: &camera{0, 0},
|
||||
}
|
||||
|
||||
buff.appendLine("")
|
||||
return buff
|
||||
}
|
||||
|
||||
@ -243,7 +243,7 @@ func (b *Buffer) processTextInput(r rune) bool {
|
||||
return proc(b)
|
||||
}
|
||||
} else {
|
||||
log.Println("warning, unimplemented shortcut ctrl+", unicode.ToLower(r), actionName)
|
||||
log.Println("warning, unimplemented shortcut ctrl +", string(unicode.ToLower(r)), actionName)
|
||||
}
|
||||
}
|
||||
|
||||
@ -790,6 +790,10 @@ var lastCursorDraw = time.Now()
|
||||
var renderFlashingCursor = true
|
||||
|
||||
func (b *Buffer) OnUpdate() bool {
|
||||
return b.doUpdate(nil)
|
||||
}
|
||||
|
||||
func (b *Buffer) doUpdate(pred func(r int) bool) bool {
|
||||
if !b.HasFocus {
|
||||
return false
|
||||
}
|
||||
@ -802,6 +806,12 @@ func (b *Buffer) OnUpdate() bool {
|
||||
if strife.PollKeys() {
|
||||
keyCode := strife.PopKey()
|
||||
|
||||
if pred != nil {
|
||||
if val := pred(keyCode); val {
|
||||
return val
|
||||
}
|
||||
}
|
||||
|
||||
// try process this key input as an
|
||||
// action first
|
||||
actionPerformed := b.processActionKey(keyCode)
|
||||
|
@ -1,59 +1,5 @@
|
||||
package gui
|
||||
|
||||
func CloseBuffer(b *Buffer) bool {
|
||||
// FIXME
|
||||
|
||||
// no parent. this can happen sometimes
|
||||
// for example if we're trying to close
|
||||
// a buffer illegal, e.g. a palette has
|
||||
// a buffer
|
||||
if b.parent == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// remove focus
|
||||
b.HasFocus = false
|
||||
b.parent.DeleteComponent(b.index)
|
||||
|
||||
// we need to re-calculate the sizes of everything
|
||||
// since we've closed a buffer!
|
||||
|
||||
// work out the size of the buffer and set it
|
||||
// note that we +1 the components because
|
||||
// we haven't yet added the panel
|
||||
var bufferWidth int
|
||||
numComponents := b.parent.NumComponents()
|
||||
if numComponents > 0 {
|
||||
bufferWidth = b.parent.w / numComponents
|
||||
} else {
|
||||
bufferWidth = b.parent.w
|
||||
}
|
||||
|
||||
// TODO track buffer visit history on a stack
|
||||
// when we remove a buffer we pop it from the stack
|
||||
// then we can focus on the top of the stack instead.
|
||||
// for now let's just focus on the most recently
|
||||
// added buffer
|
||||
|
||||
// translate all the components accordingly.
|
||||
i := 0
|
||||
|
||||
var lastBuffer *Buffer
|
||||
for _, p := range b.parent.components {
|
||||
if p == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
p.Resize(bufferWidth, b.parent.h)
|
||||
p.SetPosition(bufferWidth*i, 0)
|
||||
|
||||
i = i + 1
|
||||
lastBuffer = p.(*Buffer)
|
||||
}
|
||||
|
||||
if lastBuffer != nil {
|
||||
lastBuffer.HasFocus = true
|
||||
}
|
||||
|
||||
return true
|
||||
return false
|
||||
}
|
||||
|
@ -25,71 +25,28 @@ type Component interface {
|
||||
}
|
||||
|
||||
type BaseComponent struct {
|
||||
x, y int
|
||||
w, h int
|
||||
components []Component
|
||||
numComponents int
|
||||
inputHandler *InputHandler
|
||||
x, y int
|
||||
w, h int
|
||||
inputHandler *InputHandler
|
||||
}
|
||||
|
||||
func (b *BaseComponent) HandleEvent(evt strife.StrifeEvent) {
|
||||
// NOP
|
||||
}
|
||||
|
||||
func (b *BaseComponent) DeleteComponent(index int) {
|
||||
b.components[index] = nil
|
||||
b.numComponents--
|
||||
}
|
||||
|
||||
func (b *BaseComponent) SetPosition(x, y int) {
|
||||
b.x = x
|
||||
b.y = y
|
||||
for _, c := range b.components {
|
||||
if c == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
c.SetPosition(x, y)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BaseComponent) NumComponents() int {
|
||||
return b.numComponents
|
||||
}
|
||||
|
||||
func (b *BaseComponent) Translate(x, y int) {
|
||||
b.x += x
|
||||
b.y += y
|
||||
for _, c := range b.components {
|
||||
if c == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
c.Translate(x, y)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BaseComponent) Resize(w, h int) {
|
||||
b.w = w
|
||||
b.h = h
|
||||
for _, c := range b.components {
|
||||
if c == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
c.Resize(w, h)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *BaseComponent) GetComponents() []Component {
|
||||
return b.components
|
||||
}
|
||||
|
||||
func (b *BaseComponent) AddComponent(c Component) {
|
||||
b.components = append(b.components, c)
|
||||
b.numComponents++
|
||||
c.SetInputHandler(b.inputHandler)
|
||||
Init(c)
|
||||
}
|
||||
|
||||
func (b *BaseComponent) SetInputHandler(i *InputHandler) {
|
||||
@ -99,50 +56,3 @@ func (b *BaseComponent) SetInputHandler(i *InputHandler) {
|
||||
func (b *BaseComponent) GetInputHandler() *InputHandler {
|
||||
return b.inputHandler
|
||||
}
|
||||
|
||||
func Update(c Component) bool {
|
||||
needsRender := c.OnUpdate()
|
||||
for _, child := range c.GetComponents() {
|
||||
if child == nil {
|
||||
continue
|
||||
}
|
||||
dirty := Update(child)
|
||||
if dirty {
|
||||
needsRender = true
|
||||
}
|
||||
}
|
||||
return needsRender
|
||||
}
|
||||
|
||||
func Render(c Component, ctx *strife.Renderer) {
|
||||
c.OnRender(ctx)
|
||||
for _, child := range c.GetComponents() {
|
||||
if child == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
Render(child, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func Init(c Component) {
|
||||
c.OnInit()
|
||||
for _, child := range c.GetComponents() {
|
||||
if child == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
Init(child)
|
||||
}
|
||||
}
|
||||
|
||||
func Dispose(c Component) {
|
||||
c.OnDispose()
|
||||
for _, child := range c.GetComponents() {
|
||||
if child == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
Dispose(child)
|
||||
}
|
||||
}
|
||||
|
@ -3,36 +3,84 @@ package gui
|
||||
import (
|
||||
"github.com/felixangell/phi/cfg"
|
||||
"github.com/felixangell/strife"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type CommandPalette struct {
|
||||
BaseComponent
|
||||
buff *Buffer
|
||||
HasFocus bool
|
||||
buff *Buffer
|
||||
parentBuff *Buffer
|
||||
}
|
||||
|
||||
func NewCommandPalette(conf *cfg.TomlConfig) *CommandPalette {
|
||||
func NewCommandPalette(conf cfg.TomlConfig) *CommandPalette {
|
||||
conf.Editor.Show_Line_Numbers = false
|
||||
conf.Editor.Highlight_Line = false
|
||||
|
||||
palette := &CommandPalette{
|
||||
buff: NewBuffer(conf, nil, 0),
|
||||
buff: NewBuffer(&conf, nil, 0),
|
||||
parentBuff: nil,
|
||||
HasFocus: false,
|
||||
}
|
||||
palette.buff.HasFocus = true
|
||||
return palette
|
||||
}
|
||||
|
||||
func (b *CommandPalette) OnInit() {
|
||||
b.buff.Translate(b.x, b.y)
|
||||
b.buff.Resize(b.w, b.h)
|
||||
}
|
||||
|
||||
func (b *CommandPalette) processCommand() {
|
||||
tokenizedLine := strings.Split(b.buff.contents[0].String(), " ")
|
||||
|
||||
command := tokenizedLine[0]
|
||||
|
||||
action, exists := actions[command]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
action(b.parentBuff)
|
||||
}
|
||||
|
||||
func (b *CommandPalette) OnUpdate() bool {
|
||||
return b.buff.OnUpdate()
|
||||
if !b.HasFocus {
|
||||
return true
|
||||
}
|
||||
|
||||
override := func(k int) bool {
|
||||
if k != sdl.K_RETURN {
|
||||
return false
|
||||
}
|
||||
|
||||
b.processCommand()
|
||||
|
||||
// regardless we close the command
|
||||
// palette and re-focus on the buffer
|
||||
// that we transferred from.
|
||||
b.parentBuff.SetInputHandler(b.inputHandler)
|
||||
b.parentBuff.HasFocus = true
|
||||
|
||||
return true
|
||||
}
|
||||
return b.buff.doUpdate(override)
|
||||
}
|
||||
|
||||
func (b *CommandPalette) OnRender(ctx *strife.Renderer) {
|
||||
ctx.SetColor(strife.Red)
|
||||
ctx.Rect(b.x, b.y, b.w, b.h, strife.Fill)
|
||||
if !b.HasFocus {
|
||||
return
|
||||
}
|
||||
|
||||
border := 5
|
||||
|
||||
ctx.SetColor(strife.White)
|
||||
ctx.Rect(b.x-border, b.y-border, b.w+(border*2), b.h+(border*2), strife.Fill)
|
||||
|
||||
b.buff.OnRender(ctx)
|
||||
}
|
||||
|
||||
func (b *CommandPalette) OnDispose() {
|
||||
|
||||
log.Println("poop diddity scoop")
|
||||
}
|
||||
|
@ -7,6 +7,12 @@ import (
|
||||
"log"
|
||||
)
|
||||
|
||||
func ShowPalette(b *Buffer) bool {
|
||||
b.parent.UnfocusBuffers()
|
||||
b.parent.focusPalette(b)
|
||||
return true
|
||||
}
|
||||
|
||||
func Paste(b *Buffer) bool {
|
||||
str, err := clipboard.ReadAll()
|
||||
|
||||
|
107
gui/view.go
107
gui/view.go
@ -5,28 +5,67 @@ import (
|
||||
"github.com/felixangell/strife"
|
||||
)
|
||||
|
||||
// View is an array of buffers basically.
|
||||
type View struct {
|
||||
BaseComponent
|
||||
conf *cfg.TomlConfig
|
||||
buffers map[int]*Buffer
|
||||
focusedBuff int
|
||||
|
||||
conf *cfg.TomlConfig
|
||||
buffers map[int]*Buffer
|
||||
focusedBuff int
|
||||
commandPalette *CommandPalette
|
||||
}
|
||||
|
||||
func NewView(width, height int, conf *cfg.TomlConfig) *View {
|
||||
view := &View{
|
||||
conf: conf,
|
||||
buffers: map[int]*Buffer{},
|
||||
conf: conf,
|
||||
buffers: map[int]*Buffer{},
|
||||
commandPalette: NewCommandPalette(*conf),
|
||||
}
|
||||
|
||||
view.Translate(width, height)
|
||||
view.Resize(width, height)
|
||||
view.focusPalette()
|
||||
|
||||
view.commandPalette.Resize(view.w/3, 48)
|
||||
view.commandPalette.Translate((view.w/2)-(view.commandPalette.w/2), 10)
|
||||
|
||||
view.UnfocusBuffers()
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
func (n *View) focusPalette() {
|
||||
func (n *View) hidePalette() {
|
||||
p := n.commandPalette
|
||||
p.HasFocus = false
|
||||
|
||||
// set focus to the buffer
|
||||
// that invoked the cmd palette
|
||||
p.parentBuff.inputHandler = p.buff.inputHandler
|
||||
p.parentBuff.HasFocus = true
|
||||
|
||||
// remove focus from palette
|
||||
p.buff.HasFocus = false
|
||||
p.buff.SetInputHandler(nil)
|
||||
}
|
||||
|
||||
func (n *View) focusPalette(buff *Buffer) {
|
||||
p := n.commandPalette
|
||||
p.HasFocus = true
|
||||
|
||||
// focus the command palette
|
||||
p.buff.HasFocus = true
|
||||
p.buff.SetInputHandler(buff.inputHandler)
|
||||
|
||||
// remove focus from the buffer
|
||||
// that invoked the command palette
|
||||
buff.inputHandler = nil
|
||||
p.parentBuff = buff
|
||||
}
|
||||
|
||||
func (n *View) UnfocusBuffers() {
|
||||
// clear focus from buffers
|
||||
for _, buff := range n.buffers {
|
||||
buff.HasFocus = false
|
||||
buff.inputHandler = nil
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,21 +79,7 @@ func sign(dir int) int {
|
||||
}
|
||||
|
||||
func (n *View) ChangeFocus(dir int) {
|
||||
// remove focus from the curr buffer.
|
||||
if buf, ok := n.buffers[n.focusedBuff]; ok {
|
||||
buf.HasFocus = false
|
||||
}
|
||||
|
||||
newIndex := n.focusedBuff + sign(dir)
|
||||
if newIndex >= n.NumComponents() {
|
||||
newIndex = 0
|
||||
} else if newIndex < 0 {
|
||||
newIndex = n.NumComponents() - 1
|
||||
}
|
||||
|
||||
if buff := n.components[newIndex]; buff != nil {
|
||||
n.focusedBuff = newIndex
|
||||
}
|
||||
// TODO
|
||||
}
|
||||
|
||||
func (n *View) OnInit() {
|
||||
@ -62,19 +87,23 @@ func (n *View) OnInit() {
|
||||
|
||||
func (n *View) OnUpdate() bool {
|
||||
dirty := false
|
||||
for _, comp := range n.components {
|
||||
if comp == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if Update(comp) {
|
||||
for _, buffer := range n.buffers {
|
||||
if buffer.OnUpdate() {
|
||||
dirty = true
|
||||
}
|
||||
}
|
||||
n.commandPalette.OnUpdate()
|
||||
|
||||
return dirty
|
||||
}
|
||||
|
||||
func (n *View) OnRender(ctx *strife.Renderer) {}
|
||||
func (n *View) OnRender(ctx *strife.Renderer) {
|
||||
for _, buffer := range n.buffers {
|
||||
buffer.OnRender(ctx)
|
||||
}
|
||||
n.commandPalette.OnRender(ctx)
|
||||
}
|
||||
|
||||
func (n *View) OnDispose() {}
|
||||
|
||||
@ -83,7 +112,7 @@ func (n *View) AddBuffer() *Buffer {
|
||||
buf.HasFocus = false
|
||||
}
|
||||
|
||||
c := NewBuffer(n.conf, n, n.NumComponents())
|
||||
c := NewBuffer(n.conf, n, len(n.buffers))
|
||||
c.HasFocus = true
|
||||
|
||||
// work out the size of the buffer and set it
|
||||
@ -91,29 +120,17 @@ func (n *View) AddBuffer() *Buffer {
|
||||
// we haven't yet added the panel
|
||||
var bufferWidth int
|
||||
|
||||
// NOTE: because we're ADDING a component
|
||||
// here we add 1 to the components since
|
||||
// we want to calculate the sizes _after_
|
||||
// we've added this component.
|
||||
numComponents := n.NumComponents() + 1
|
||||
if numComponents > 0 {
|
||||
bufferWidth = n.w / numComponents
|
||||
} else {
|
||||
bufferWidth = n.w
|
||||
}
|
||||
|
||||
n.AddComponent(c)
|
||||
n.buffers[c.index] = c
|
||||
n.focusedBuff = c.index
|
||||
|
||||
// translate all the components accordingly.
|
||||
for i, p := range n.components {
|
||||
if p == nil {
|
||||
for i, buff := range n.buffers {
|
||||
if buff == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
p.Resize(bufferWidth, n.h)
|
||||
p.SetPosition(bufferWidth*i, 0)
|
||||
buff.Resize(bufferWidth, n.h)
|
||||
buff.SetPosition(bufferWidth*i, 0)
|
||||
}
|
||||
|
||||
return c
|
||||
|
32
main.go
32
main.go
@ -18,15 +18,13 @@ const (
|
||||
)
|
||||
|
||||
type PhiEditor struct {
|
||||
gui.BaseComponent
|
||||
running bool
|
||||
defaultFont *strife.Font
|
||||
mainView *gui.View
|
||||
}
|
||||
|
||||
func (n *PhiEditor) handleEvent(evt strife.StrifeEvent) {
|
||||
for _, comp := range n.GetComponents() {
|
||||
gui.HandleEvent(comp, evt)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (n *PhiEditor) init(cfg *cfg.TomlConfig) {
|
||||
@ -50,39 +48,21 @@ func (n *PhiEditor) init(cfg *cfg.TomlConfig) {
|
||||
mainView.AddBuffer().OpenFile(tempFile.Name())
|
||||
}
|
||||
|
||||
{
|
||||
palette := gui.NewCommandPalette(cfg)
|
||||
palette.Resize(1280/3, 72)
|
||||
// mainView.AddComponent(palette)
|
||||
}
|
||||
|
||||
n.AddComponent(mainView)
|
||||
n.mainView = mainView
|
||||
n.defaultFont = cfg.Editor.Loaded_Font
|
||||
}
|
||||
|
||||
func (n *PhiEditor) dispose() {
|
||||
for _, comp := range n.GetComponents() {
|
||||
gui.Dispose(comp)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (n *PhiEditor) update() bool {
|
||||
needsRender := false
|
||||
for _, comp := range n.GetComponents() {
|
||||
dirty := comp.OnUpdate()
|
||||
if dirty {
|
||||
needsRender = true
|
||||
}
|
||||
}
|
||||
return needsRender
|
||||
return n.mainView.OnUpdate()
|
||||
}
|
||||
|
||||
func (n *PhiEditor) render(ctx *strife.Renderer) {
|
||||
ctx.SetFont(n.defaultFont)
|
||||
|
||||
for _, child := range n.GetComponents() {
|
||||
gui.Render(child, ctx)
|
||||
}
|
||||
n.mainView.OnRender(ctx)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
Loading…
Reference in New Issue
Block a user