Skip to content

Commit

Permalink
Fixed crash setting OnePage layout mode
Browse files Browse the repository at this point in the history
Fixed sometimes moving to last page when setting layout mode
Fixed sometmes not finding last bookmark
Fixed bad json in sendOpenFileResMsg
  • Loading branch information
mftb0 committed Apr 5, 2023
1 parent 9c264fb commit 213b187
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
12 changes: 3 additions & 9 deletions cmd/cbxv/messagehandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,23 @@ func NewMessageHandlers(m *model.Model, u *ui.UI) *MessageHandlerList {

handlers.List["setLayoutModeOnePage"] = func(data string) {
m.LayoutMode = model.ONE_PAGE
m.SpreadIndex = m.PageToSpread(m.PageIndex)
m.NewSpreads()
if m.SpreadIndex > len(m.Spreads)-1 {
m.SpreadIndex = len(m.Spreads) - 1
}
m.SpreadIndex = m.PageToSpread(m.PageIndex)
m.RefreshPages()
}

handlers.List["setLayoutModeTwoPage"] = func(data string) {
m.LayoutMode = model.TWO_PAGE
m.SpreadIndex = m.PageToSpread(m.PageIndex)
m.NewSpreads()
if m.SpreadIndex > len(m.Spreads)-1 {
m.SpreadIndex = len(m.Spreads) - 1
}
m.SpreadIndex = m.PageToSpread(m.PageIndex)
m.RefreshPages()
}

handlers.List["setLayoutModeLongStrip"] = func(data string) {
m.LayoutMode = model.LONG_STRIP
m.NewSpreads()
m.SpreadIndex = 0
m.PageIndex = m.Spreads[m.SpreadIndex].VersoPage()
m.NewSpreads()
m.RefreshPages()
}

Expand Down
45 changes: 34 additions & 11 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,23 +534,46 @@ func (m *Model) RefreshPages() {
}
}

// page index to spread index
// Returns 0 if spreads are nil or
// page can't be found
// Otherwise it guesses
// fixme: Don't like it, but there's no point in telling a user about any of it
// because errors detected here are almost certainly the result of a
// programming error elsewhere in the program
func (m *Model) PageToSpread(n int) int {
if m.Spreads == nil {
util.Log("p2s: spreads nil %d\n", n)
return 0
} else if n > len(m.Spreads)-1 {
}

if n < 0 {
util.Log("p2s: page out of range %d\n", n)
return 0
}

var pagesNil bool
if m.Pages == nil {
pagesNil = true
util.Log("p2s: pages nil %d\n", n)
}

if !pagesNil && n > len(m.Pages) - 1 {
max := len(m.Pages) - 1
util.Log("p2s: page out of range max: %d, n:%d\n", max, n)
return len(m.Spreads) - 1
} else if m.LayoutMode == TWO_PAGE {
for i := range m.Spreads {
spread := m.Spreads[i]
for j := range spread.PageIdxs {
if n == spread.PageIdxs[j] {
return i
}
}

for i := range m.Spreads {
spread := m.Spreads[i]
for j := range spread.PageIdxs {
if n == spread.PageIdxs[j] {
return i
}
}
}
return -1

util.Log("p2s: page not found %d\n", n)
return 0
}

func (m *Model) loadBookmarks() {
Expand Down Expand Up @@ -622,7 +645,7 @@ func (m *Model) sendOpenFileResMsg(code ResultCode, description string) {
r := Result{code, description}
buf, err := json.Marshal(r)
if err != nil {
d = fmt.Sprintf("{\"code\":%d,\"result\":%s}", r.Code, r.Description)
d = fmt.Sprintf("{\"code\":%d,\"result\":\"%s\"}", r.Code, r.Description)
} else {
d = string(buf)
}
Expand Down

0 comments on commit 213b187

Please sign in to comment.