-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (122 loc) · 3.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
"io"
"strings"
)
type config struct {
CurrentFile fyne.URI
EditWidget *widget.Entry
PreviewWidget *widget.RichText
SaveMenuItem *fyne.MenuItem
}
var cfg config
func main() {
// Create a Fyne app
fa := app.New()
//fa.Settings().SetTheme(&myTheme{})
// Create a window for the app
win := fa.NewWindow("Markdown Editor")
// Get the user interfaces
edit, preview := cfg.makeUI()
cfg.createMenuItems(win)
// Set the content of the window
win.SetContent(container.NewHSplit(edit, preview))
// Show window and run app
win.Resize(fyne.Size{Width: 800, Height: 500})
win.CenterOnScreen()
win.ShowAndRun()
}
func (c *config) makeUI() (*widget.Entry, *widget.RichText) {
edit := widget.NewMultiLineEntry()
preview := widget.NewRichTextFromMarkdown("")
c.EditWidget = edit
c.PreviewWidget = preview
edit.OnChanged = preview.ParseMarkdown
return edit, preview
}
func (c *config) createMenuItems(w fyne.Window) {
openMenuItem := fyne.NewMenuItem("Open...", c.openFunc(w))
saveMenuItem := fyne.NewMenuItem("Save", c.saveFunc(w))
c.SaveMenuItem = saveMenuItem
c.SaveMenuItem.Disabled = true
saveAsMenuItem := fyne.NewMenuItem("Save as...", c.saveAsFunc(w))
fileMenu := fyne.NewMenu("File", openMenuItem, saveMenuItem, saveAsMenuItem)
mainMenu := fyne.NewMainMenu(fileMenu)
w.SetMainMenu(mainMenu)
}
var filter = storage.NewExtensionFileFilter([]string{".md", ".MD"})
func (c *config) openFunc(win fyne.Window) func() {
fmt.Println("openFunc")
return func() {
openDialog := dialog.NewFileOpen(func(read fyne.URIReadCloser, err error) {
if err != nil {
dialog.ShowError(err, win)
return
}
if read == nil {
return
}
defer read.Close()
data, err := io.ReadAll(read)
if err != nil {
dialog.ShowError(err, win)
return
}
c.EditWidget.SetText(string(data))
c.CurrentFile = read.URI()
win.SetTitle(win.Title() + " " + read.URI().Name())
c.SaveMenuItem.Disabled = false
}, win)
openDialog.SetFilter(filter)
openDialog.Show()
}
}
func (c *config) saveAsFunc(win fyne.Window) func() {
fmt.Println("saveAsFunc")
return func() {
saveDialog := dialog.NewFileSave(func(write fyne.URIWriteCloser, err error) {
if err != nil {
dialog.ShowError(err, win)
return
}
if write == nil {
// User cancelled
return
}
defer write.Close()
if !strings.HasSuffix(strings.ToLower(write.URI().String()), ".md") {
dialog.ShowInformation("Error", "Please name your file with .md extension!", win)
return
}
// Save the file
write.Write([]byte(c.EditWidget.Text))
c.CurrentFile = write.URI()
win.SetTitle(win.Title() + " " + write.URI().Name())
c.SaveMenuItem.Disabled = false
}, win)
saveDialog.SetFileName("untitled.md")
saveDialog.SetFilter(filter)
saveDialog.Show()
}
}
func (c *config) saveFunc(win fyne.Window) func() {
fmt.Println("saveFunc")
return func() {
if c.CurrentFile != nil {
write, err := storage.Writer(c.CurrentFile)
if err != nil {
dialog.ShowError(err, win)
return
}
defer write.Close()
write.Write([]byte(c.EditWidget.Text))
}
}
}