-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
152 lines (129 loc) · 3.16 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
package main
import (
"fmt"
"runtime"
"time"
"fyne.io/cloud"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type ui struct {
current *note
notes *notelist
content *widget.Entry
list *widget.List
}
func (u *ui) addNote() {
newNote := u.notes.add()
u.setNote(newNote)
}
func (u *ui) setNote(n *note) {
u.content.Unbind()
if n == nil {
u.content.SetText(u.placeholderContent())
return
}
u.current = n
u.content.Bind(n.content)
u.content.Validator = nil
u.list.Refresh()
}
func (u *ui) buildList() *widget.List {
l := widget.NewList(
func() int {
return len(u.notes.notes())
},
func() fyne.CanvasObject {
return widget.NewLabel("Title")
},
func(id widget.ListItemID, obj fyne.CanvasObject) {
l := obj.(*widget.Label)
n := u.notes.notes()[id]
l.Bind(n.title())
})
l.OnSelected = func(id widget.ListItemID) {
n := u.notes.notes()[id]
u.setNote(n)
}
return l
}
func (u *ui) removeCurrentNote() {
u.notes.delete(u.current)
visible := u.notes.notes()
if len(visible) > 0 {
u.setNote(visible[0])
} else {
u.setNote(nil)
}
u.list.Refresh()
}
func (u *ui) loadUI() fyne.CanvasObject {
u.content = widget.NewMultiLineEntry()
u.content.SetText(u.placeholderContent())
time.Sleep(time.Millisecond * 100) // de-bounce Entry? #4235
u.list = u.buildList()
visible := u.notes.notes()
if len(visible) > 0 {
u.setNote(visible[0])
u.list.Select(0)
}
bar := widget.NewToolbar(
widget.NewToolbarAction(theme.ContentAddIcon(), func() {
u.addNote()
}),
widget.NewToolbarAction(theme.ContentRemoveIcon(), func() {
u.removeCurrentNote()
}),
)
if fyne.CurrentDevice().IsMobile() {
// a mock button to leave space for the mobile menu
bar.Items = append([]widget.ToolbarItem{widget.NewToolbarAction(theme.MenuIcon(), func() {})}, bar.Items...)
}
side := fyne.NewContainerWithLayout(layout.NewBorderLayout(bar, nil, nil, nil),
bar, container.NewVScroll(u.list))
return newAdaptiveSplit(side, u.content)
}
func (u *ui) registerKeys(w fyne.Window) {
shortcut := &desktop.CustomShortcut{KeyName: fyne.KeyN, Modifier: desktop.ControlModifier}
if runtime.GOOS == "darwin" {
shortcut.Modifier = desktop.SuperModifier
}
w.Canvas().AddShortcut(shortcut, func(_ fyne.Shortcut) {
u.addNote()
})
}
func (u *ui) placeholderContent() string {
text := "Welcome!\nTap '+' in the toolbar to add a note."
if fyne.CurrentDevice().HasKeyboard() {
modifier := "ctrl"
if runtime.GOOS == "darwin" {
modifier = "cmd"
}
text += fmt.Sprintf("\n\nOr use the keyboard shortcut %s+N.", modifier)
}
return text
}
func main() {
a := app.NewWithID("com.fynelabs.notes")
cloud.Enable(a)
a.SetIcon(resourceIconPng)
a.Settings().SetTheme(&myTheme{})
w := a.NewWindow("Fyne Notes")
w.SetMainMenu(fyne.NewMainMenu(
fyne.NewMenu("File",
fyne.NewMenuItem("Sync...", func() {
cloud.ShowSettings(a, w)
}))))
list := ¬elist{pref: a.Preferences()}
list.load()
notesUI := &ui{notes: list}
w.SetContent(notesUI.loadUI())
notesUI.registerKeys(w)
w.Resize(fyne.NewSize(500, 320))
w.ShowAndRun()
}