Skip to content

Commit

Permalink
cre.cpp: split newDocView() into readDefaults() (#766)
Browse files Browse the repository at this point in the history
The code moved into the new readDefaults() (that loads some settings
from cr3.ini) will then only be called by ReaderUI, and not by other
parts that only open a document to grab its metadata or cover image.
Otherwise, these other openings could set some new global state
and variables that would affect the currently opened-for-displaying
document (eg. invalidate its cache, change its font...)
  • Loading branch information
poire-z committed Nov 19, 2018
1 parent 84299a5 commit 461b750
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions cre.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,18 @@ static int newDocView(lua_State *L) {
doc->text_view->setViewMode(view_mode, -1);
doc->text_view->Resize(width, height);
doc->text_view->setPageHeaderInfo(PGHDR_AUTHOR|PGHDR_TITLE|PGHDR_PAGE_NUMBER|PGHDR_PAGE_COUNT|PGHDR_CHAPTER_MARKS|PGHDR_CLOCK);
doc->text_view->setBatteryIcons(getBatteryIcons(0x000000));

return 1;
}

static int readDefaults(lua_State *L) {
// This is to be called only when the document is opened to be
// read by ReaderUI - not when the document is opened to just
// get its metadata or cover image - as it will affect some
// crengine global variables and state, whose change would
// affect the currently opened for reading document.
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");

// it will overwrite all settings by values found in ./data/cr3.ini
CRPropRef props = doc->text_view->propsGetCurrent();
Expand All @@ -362,10 +374,14 @@ static int newDocView(lua_State *L) {
stream = LVOpenFileStream("data/cr3.ini", LVOM_WRITE);
props->saveToStream(stream.get());
}
return 0;
}

doc->text_view->setBatteryIcons(getBatteryIcons(0x000000));

return 1;
static int saveDefaults(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
CRPropRef props = doc->text_view->propsGetCurrent();
LVStreamRef stream = LVOpenFileStream("data/cr3.ini", LVOM_WRITE);
return props->saveToStream(stream.get());
}

static int getLatestDomVersion(lua_State *L) {
Expand All @@ -379,13 +395,6 @@ static int requestDomVersion(lua_State *L) {
return 0;
}

static int saveDefaults(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
CRPropRef props = doc->text_view->propsGetCurrent();
LVStreamRef stream = LVOpenFileStream("data/cr3.ini", LVOM_WRITE);
return props->saveToStream(stream.get());
}

static int getIntProperty(lua_State *L) {
CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument");
const char *propName = luaL_checkstring(L, 2);
Expand Down Expand Up @@ -2611,6 +2620,7 @@ static const struct luaL_Reg credocument_meth[] = {
{"goBack", goBack},
{"goForward", goForward},
{"clearSelection", clearSelection},
{"readDefaults", readDefaults},
{"saveDefaults", saveDefaults},
{"close", closeDocument},
{"__gc", closeDocument},
Expand Down

0 comments on commit 461b750

Please sign in to comment.