This commit is contained in:
Erik Winter 2024-09-14 14:54:41 +02:00
parent da7aaca2a3
commit 23977cfe9e
6 changed files with 48 additions and 64 deletions

View File

@ -1,6 +1,6 @@
package editor
type Command interface {
Do(s *State)
Do(lines []string) error
//Undo(s *State) error
}

32
editor/document.go Normal file
View File

@ -0,0 +1,32 @@
package editor
type Document struct {
Title string
Lines []string
in chan Command
refresh chan bool
}
func NewDocument(title string) *Document {
return &Document{
Title: title,
Lines: make([]string, 0),
in: make(chan Command),
refresh: make(chan bool),
}
}
func (d *Document) In() chan Command {
return d.in
}
func (d *Document) Out() chan bool {
return d.refresh
}
func (d *Document) Run() {
for cmd := range d.in {
cmd.Do(d.Lines)
d.refresh <- true
}
}

View File

@ -1,33 +0,0 @@
package editor
type Editor struct {
s *State
in chan Command
refresh chan bool
}
func NewEditor(s *State) *Editor {
e := &Editor{
s: s,
in: make(chan Command),
refresh: make(chan bool),
}
go e.Run()
return e
}
func (e *Editor) In() chan Command {
return e.in
}
func (e *Editor) Out() chan bool {
return e.refresh
}
func (e *Editor) Run() {
for cmd := range e.in {
cmd.Do(e.s)
e.refresh <- true
}
}

View File

@ -1,31 +1,17 @@
package editor
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
type GUI struct {
a fyne.App
w fyne.Window
cmd chan Command
refresh chan bool
s *State
}
func NewGUI(refresh chan bool, cmd chan Command) *GUI {
a := app.New()
w := a.NewWindow("Narratio")
w.Resize(fyne.NewSize(800, 600))
func NewGUI(s *State, refresh chan bool, cmd chan Command) *GUI {
g := &GUI{
a: a,
w: w,
refresh: refresh,
cmd: cmd,
s: s,
}
g.SetContent()
return g
}
@ -36,17 +22,10 @@ func (g *GUI) Run() {
g.Update()
}
}()
g.w.ShowAndRun()
}
func (g *GUI) Update() {
}
func (g *GUI) SetContent() {
input := widget.NewEntry()
input.SetPlaceHolder("Enter text...")
grid := container.NewBorder(nil, nil, nil, nil, input)
g.w.SetContent(grid)
content := []string{g.s.Document.Title}
content = append(content, g.s.Document.Lines...)
}

View File

@ -1,9 +1,12 @@
package editor
type State struct {
Err error
Err error
Document *Document
}
func NewState() *State {
return &State{}
return &State{
Document: NewDocument("new doc"),
}
}

View File

@ -7,6 +7,9 @@ import (
func main() {
s := editor.NewState()
e := editor.NewEditor(s)
g := editor.NewGUI(e.Out(), e.In())
g := editor.NewGUI(s, e.Out(), e.In())
go func() {
e.Out() <- true
}()
g.Run()
}