Skip to content

Commit

Permalink
Improvements to page turning
Browse files Browse the repository at this point in the history
Reverted to an earlier page turning strategy, but with a new optimization.
Turning a page, a single time, should feel effortless and smooth. Most of all it
should not distract from reading. Before sometimes there was grinding (the
feeling that the computer was struggling). On another attempt it felt jerky or
uneven. With the new setup, the page can always be displayed and then any other
work follows.

Added refreshSpreads command
Reduced time wasted in printLoaded()
Ensure never trying to render a partially loaded spread
Added trace routines to better time function execution
  • Loading branch information
mftb0 committed Aug 17, 2023
1 parent e9432d7 commit 213b64b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 24 deletions.
14 changes: 12 additions & 2 deletions cmd/cbxv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,37 @@ import (

const (
NAME = "cbxv"
VERSION = "0.4.9"
VERSION = "0.5.0"
)

// Update listens for messages on the message channel and
// handles messages by invoking messageHandlers
func update(m *model.Model, u *ui.UI, msgChan chan util.Message, msgHandlers *MessageHandlerList) {
for msg := range msgChan {
msgHandler := msgHandlers.List[msg.TypeName]

// If spreads are nil, the list below are the only commands
// that are allowed to run
if m.Spreads == nil &&
(msg.TypeName != "quit" &&
msg.TypeName != "openFile" &&
msg.TypeName != "openFileResult" &&
msg.TypeName != "toggleFullscreen") {
continue
}

// We have a handler, schedule it to run on event dispatch thread
if msgHandler != nil {
u.RunFunc(func() {
msgHandler(msg.Data)
u.Render(m)
})
}

// Render after every command, except refreshSpreads
if msg.TypeName != "refreshSpreads" {
u.Render(m)
}

runtime.GC()

// Handling the quit message above
Expand Down
14 changes: 7 additions & 7 deletions cmd/cbxv/messagehandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ func NewMessageHandlers(m *model.Model, u *ui.UI) *MessageHandlerList {
if m.SpreadIndex < len(m.Spreads)-1 {
m.SpreadIndex++
m.PageIndex = m.Spreads[m.SpreadIndex].VersoPage()
if !m.SpreadLoaded(m.SpreadIndex) {
m.RefreshSpreads()
}
} else {
handlers.List["nextFile"]("")
}
Expand All @@ -44,9 +41,6 @@ func NewMessageHandlers(m *model.Model, u *ui.UI) *MessageHandlerList {
if m.SpreadIndex > 0 {
m.SpreadIndex--
m.PageIndex = m.Spreads[m.SpreadIndex].VersoPage()
if !m.SpreadLoaded(m.SpreadIndex) {
m.RefreshSpreads()
}
} else {
handlers.List["previousFile"]("")
}
Expand Down Expand Up @@ -292,7 +286,13 @@ func NewMessageHandlers(m *model.Model, u *ui.UI) *MessageHandlerList {
}

handlers.List["render"] = func(data string) {
//noop render always gets called after cmd
// noop render always gets called after cmd
}

// Unless it's refreshSpreads, this is the one cmd that
// after running we don't call render
handlers.List["refreshSpreads"] = func(data string) {
m.RefreshSpreads()
}

handlers.List["quit"] = func(data string) {
Expand Down
20 changes: 11 additions & 9 deletions internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,18 +678,20 @@ func (m *Model) checkSpreads() {

// dbg
func (m *Model) printLoaded() {
var buf string
for i := range m.Pages {
if !m.Pages[i].Loaded {
buf += "0"
} else {
if i == m.PageIndex {
buf += "_"
if util.DEBUG == true {
var buf string
for i := range m.Pages {
if !m.Pages[i].Loaded {
buf += "0"
} else {
buf += "1"
if i == m.PageIndex {
buf += "_"
} else {
buf += "1"
}
}
}
util.Log("%s\n", buf)
}
util.Log("%s\n", buf)
}

2 changes: 2 additions & 0 deletions internal/ui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ func NewCommands(m *model.Model, u *UI) *CommandList {
[]uint{gdk.KEY_d, gdk.KEY_Right, gdk.KEY_l},
func(args ...any) {
u.SendMessage(util.Message{TypeName: "rightPage"})
u.SendMessage(util.Message{TypeName: "refreshSpreads"})
}))

AddCommand(cmds, NewCommand("leftPage", "Left Page",
[]uint{gdk.KEY_a, gdk.KEY_Left, gdk.KEY_h},
func(args ...any) {
u.SendMessage(util.Message{TypeName: "leftPage"})
u.SendMessage(util.Message{TypeName: "refreshSpreads"})
}))

AddCommand(cmds, NewCommand("firstPage", "First Page",
Expand Down
14 changes: 8 additions & 6 deletions internal/ui/pageview.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ func (v *PageView) initRenderer(m *model.Model) {
}

spread := m.Spreads[m.SpreadIndex]
if m.LayoutMode == model.TWO_PAGE {
s := newTwoPageSpread(m, canvas, cr, spread)
renderTwoPageSpread(s)
} else if m.LayoutMode == model.ONE_PAGE {
s := newOnePageSpread(canvas, cr, spread.Pages[0])
renderOnePageSpread(s)
if m.SpreadLoaded(m.SpreadIndex) {
if m.LayoutMode == model.TWO_PAGE {
s := newTwoPageSpread(m, canvas, cr, spread)
renderTwoPageSpread(s)
} else if m.LayoutMode == model.ONE_PAGE {
s := newOnePageSpread(canvas, cr, spread.Pages[0])
renderOnePageSpread(s)
}
}
w := v.hud.GetAllocatedWidth() - 40
v.hdrControl.container.SetSizeRequest(w, 8)
Expand Down
9 changes: 9 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,12 @@ func Log(format string, a ...any) {
}
}

func TrcStart (s string) (string, time.Time) {
return s, time.Now()
}

func TrcEnd (s string, startTime time.Time) {
endTime := time.Now()
fmt.Printf("Name:%s, Time:%d\n", s, endTime.Sub(startTime).Milliseconds())
}

0 comments on commit 213b64b

Please sign in to comment.