-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (112 loc) · 3.03 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
package main
import (
"io"
"strings"
"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"
)
type config struct {
EditWidget *widget.Entry
PreviewWidget *widget.RichText
CurrentFile fyne.URI
SaveMenuItem *fyne.MenuItem
}
var cfg config
func main() {
a := app.New()
win := a.NewWindow("GOMDE - Markdown")
edit, preview := cfg.makeUI()
cfg.createMenuItems(win)
win.SetContent(container.NewHSplit(edit, preview))
win.Resize(fyne.NewSize(400, 400))
win.CenterOnScreen()
win.ShowAndRun()
}
func (app *config) makeUI() (*widget.Entry, *widget.RichText) {
edit := widget.NewMultiLineEntry()
preview := widget.NewRichTextFromMarkdown("")
app.EditWidget = edit
app.PreviewWidget = preview
// the right panel gonna show the edited version
edit.OnChanged = preview.ParseMarkdown
return edit, preview
}
func (app *config) createMenuItems(win fyne.Window) {
openMenuItem := fyne.NewMenuItem("Open File", app.openFunc(win))
saveMenuItem := fyne.NewMenuItem("Save", app.saveFunc(win))
app.SaveMenuItem = saveMenuItem
app.SaveMenuItem.Disabled = true
saveAsMenuItem := fyne.NewMenuItem("Save as...", app.saveAsFunc(win))
fileMenu := fyne.NewMenu("File", openMenuItem, saveMenuItem, saveAsMenuItem)
menu := fyne.NewMainMenu(fileMenu)
win.SetMainMenu(menu)
}
var filter = storage.NewExtensionFileFilter([]string{".md", ".MD"})
func (app *config) saveFunc(win fyne.Window) func() {
return func() {
if app.CurrentFile != nil {
write, err := storage.Writer(app.CurrentFile)
if err != nil {
dialog.ShowError(err, win)
return
}
write.Write([]byte(app.EditWidget.Text))
defer write.Close()
}
}
}
func (app *config) openFunc(win fyne.Window) func() {
return func() {
openDialog := dialog.NewFileOpen(func(read fyne.URIReadCloser, err error) {
defer read.Close()
if err != nil {
dialog.ShowError(err, win)
return
}
if read == nil {
return
}
data, err := io.ReadAll(read)
if err != nil {
dialog.ShowError(err, win)
return
}
app.EditWidget.SetText(string(data))
app.CurrentFile = read.URI()
win.SetTitle(win.Title() + " - " + read.URI().Name())
app.SaveMenuItem.Disabled = false
}, win)
openDialog.SetFilter(filter)
openDialog.Show()
}
}
func (app *config) saveAsFunc(win fyne.Window) func() {
return func() {
saveDialog := dialog.NewFileSave(func(write fyne.URIWriteCloser, err error) {
if err != nil {
dialog.ShowError(err, win)
return
}
if write == nil {
// user cancelou acao
return
}
if !strings.HasSuffix(strings.ToLower(write.URI().String()), ".md") {
dialog.ShowInformation("Error", "Suffix your file with .md extension!", win)
return
}
write.Write([]byte(app.EditWidget.Text))
app.CurrentFile = write.URI()
defer write.Close()
win.SetTitle(win.Title() + " - " + write.URI().Name())
app.SaveMenuItem.Disabled = false
}, win)
saveDialog.SetFileName("untitled.md")
saveDialog.SetFilter(filter)
saveDialog.Show()
}
}