-
Notifications
You must be signed in to change notification settings - Fork 1
/
jumpmarks.go
215 lines (190 loc) · 5.29 KB
/
jumpmarks.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2013-2021 Utkan Güngördü <[email protected]>
* Copyright (c) 2021-2024 Piotr Grabowski
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gomicsv
import (
"fmt"
"sort"
"github.com/gotk3/gotk3/gtk"
"github.com/fauu/gomicsv/pagecache"
"github.com/fauu/gomicsv/util"
)
type Jumpmarks struct {
list []int
cycle JumpmarksCycle
}
type JumpmarksCycle struct {
page *int
returnPage *int
dontClear bool
}
type JumpmarkCycleDirection int
const (
cycleDirectionBackward JumpmarkCycleDirection = iota
cycleDirectionForward
)
func (jumpmarks Jumpmarks) has(page int) bool {
for _, mark := range jumpmarks.list {
// ASSUMPTION: The slice is sorted
if mark < page {
continue
} else if mark == page {
return true
} else {
break
}
}
return false
}
func (jumpmarks Jumpmarks) size() int {
return len(jumpmarks.list)
}
func (jumpmarks *Jumpmarks) toggle(page int) bool {
for i, mark := range jumpmarks.list {
// ASSUMPTION: The slice is sorted
if mark < page {
continue
} else if mark == page {
// Is in the marked list. Remove by swapping with the last element
jumpmarks.list[i] = jumpmarks.list[len(jumpmarks.list)-1]
jumpmarks.list = jumpmarks.list[:len(jumpmarks.list)-1]
sort.Ints(jumpmarks.list)
return false
} else {
break
}
}
// Isn't in the marked list. Add
jumpmarks.list = append(jumpmarks.list, page)
sort.Ints(jumpmarks.list)
return true
}
func (app *App) clearJumpmarks() {
// The following isn't needed until we use this function elsewhere than during archive closing, which we currently don't
// for _, mark := range app.S.Jumpmarks.list {
// app.S.PageCache.UnforbidRemoval(mark)
// }
app.S.Jumpmarks = Jumpmarks{}
}
var jumpmarkMenuItems []*gtk.MenuItem
func (app *App) currentPageIsJumpmarked() bool {
return app.S.Jumpmarks.has(app.S.ArchivePos + 1)
}
func (app *App) toggleJumpmark() {
page := app.S.ArchivePos + 1
currentMarked := app.S.Jumpmarks.toggle(page)
var prefix string
if currentMarked {
app.S.PageCache.Keep(app.S.ArchivePos, pagecache.KeepReasonJumpmark)
prefix = "Marked"
} else {
app.S.PageCache.DontKeep(app.S.ArchivePos, pagecache.KeepReasonJumpmark)
prefix = "Unmarked"
}
app.notificationShow(fmt.Sprintf("%s page %d", prefix, page), ShortNotification)
app.updateJumpmarkToggleLabel(currentMarked)
app.updateJumpmarkCycleMenuItems()
app.rebuildJumpmarksMenuList()
app.updateStatus()
}
func (app *App) cycleJumpmarks(direction JumpmarkCycleDirection) {
if app.S.Jumpmarks.size() == 0 {
return
}
nextPage := -1
if app.S.Jumpmarks.cycle.page != nil {
nextPage = *app.S.Jumpmarks.cycle.page
} else {
curr := app.S.ArchivePos + 1
app.S.Jumpmarks.cycle.returnPage = &curr
}
if direction == cycleDirectionForward {
nextPage++
} else {
nextPage--
}
if nextPage < 0 {
nextPage = app.S.Jumpmarks.size() - 1
} else if nextPage >= app.S.Jumpmarks.size() {
nextPage = 0
}
app.S.Jumpmarks.cycle.page = &nextPage
app.S.Jumpmarks.cycle.dontClear = true
app.setPage(app.S.Jumpmarks.list[nextPage] - 1)
app.S.Jumpmarks.cycle.dontClear = false
}
func (app *App) returnFromCyclingJumpmarks() {
p := app.S.Jumpmarks.cycle.returnPage
if p != nil {
app.setPage(*p - 1)
}
}
func (app *App) jumpmarksHandleSetPage(page int) {
jumpmarks := &app.S.Jumpmarks
if !jumpmarks.cycle.dontClear {
jumpmarks.cycle.page = nil
jumpmarks.cycle.returnPage = nil
}
app.updateJumpmarkToggleLabel(app.currentPageIsJumpmarked())
app.updateJumpmarkCycleMenuItems()
s := false
if jumpmarks.cycle.returnPage != nil && page != *jumpmarks.cycle.returnPage {
s = true
}
app.W.MenuItemJumpmarksReturnFromCycling.SetSensitive(s)
}
func (app *App) updateJumpmarkToggleLabel(currentMarked bool) {
var w string
if currentMarked {
w = "Unmark"
} else {
w = "Mark"
}
label := fmt.Sprintf("%s current page", w)
app.W.MenuItemToggleJumpmark.SetLabel(label)
}
func (app *App) updateJumpmarkCycleMenuItems() {
s := false
if app.S.Jumpmarks.size() > 0 {
s = true
}
app.W.MenuItemCycleJumpmarksBackward.SetSensitive(s)
app.W.MenuItemCycleJumpmarksForward.SetSensitive(s)
}
func (app *App) rebuildJumpmarksMenuList() {
for _, item := range jumpmarkMenuItems {
app.W.MenuJumpmarks.Remove(item)
item.Destroy()
}
jumpmarkMenuItems = nil
util.GC()
for _, mark := range app.S.Jumpmarks.list {
label := fmt.Sprintf("%d", mark)
menuItem, err := gtk.MenuItemNewWithLabel(label)
if err != nil {
app.showError(err.Error())
return
}
page := mark - 1 // Make a new variable so that the correct value gets passed to the callback
menuItem.Connect("activate", func() {
app.setPage(page)
})
jumpmarkMenuItems = append(jumpmarkMenuItems, menuItem)
app.W.MenuJumpmarks.Append(menuItem)
menuItem.Show()
}
}