-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
163 lines (125 loc) · 3.61 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
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"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/ioutil"
"strings"
)
type config struct {
EditWidget *widget.Entry
PreviewWidget *widget.RichText
CurrentFile fyne.URI
SaveMenuItem *fyne.MenuItem
}
var cfg config
func main() {
// create a fyne app
a := app.New()
// use custom theme
a.Settings().SetTheme(&myTheme{})
// create a window for the app
win := a.NewWindow("Markdown")
// get the user interface
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()
}
// makeUI will create the user interface with the entry and rich text widgets
func (app *config) makeUI() (*widget.Entry, *widget.RichText) {
// add a new multi line input widget
edit := widget.NewMultiLineEntry()
// create a preview pane that allows for Rich Text generated by Markdown
preview := widget.NewRichTextFromMarkdown("")
app.EditWidget = edit
app.PreviewWidget = preview
// when editing with Markdown in the Edit window, automatically show the output in the Preview pane as Rich Text
edit.OnChanged = preview.ParseMarkdown
return edit, preview
}
// createMenuItems will create the dropdown menus
func (app *config) createMenuItems(win fyne.Window) {
openMenuItem := fyne.NewMenuItem("Open...", 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) 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 cancelled
return
}
if !strings.HasSuffix(strings.ToLower(write.URI().String()), ".md") {
dialog.ShowInformation("Error", "Please name your file with a .md extension!", win)
return
}
// save the file
write.Write([]byte(app.EditWidget.Text))
app.CurrentFile = write.URI()
defer write.Close()
// update window title
win.SetTitle(win.Title() + " - " + write.URI().Name())
app.SaveMenuItem.Disabled = false
}, win)
saveDialog.SetFileName("untitled.md")
saveDialog.SetFilter(filter)
saveDialog.Show()
}
}
func (app *config) openFunc(win fyne.Window) func() {
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 := ioutil.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()
}
}