Skip to content

Commit

Permalink
Remember the zoom level across sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
junrrein committed Dec 25, 2019
1 parent d8b297d commit edbf16d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/application/appwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ AppWindow::AppWindow(TaskRunner& taskRunner, SettingsManager& settingsManager)
{
set_size_request(500, 500);

loadWindowState();
loadPreviousSessionState();
loadWidgets();
addActions();
setupWidgets();
Expand All @@ -58,7 +58,7 @@ AppWindow::AppWindow(TaskRunner& taskRunner, SettingsManager& settingsManager)

AppWindow::~AppWindow()
{
saveWindowState();
saveCurrentSessionState();
}

void AppWindow::setDocument(std::unique_ptr<Document> document)
Expand Down Expand Up @@ -105,19 +105,22 @@ bool AppWindow::on_delete_event(GdkEventAny*)
return false;
}

void AppWindow::loadWindowState()
void AppWindow::loadPreviousSessionState()
{
m_windowState = m_settingsManager.loadWindowState();

set_default_size(m_windowState.width, m_windowState.height);

if (m_windowState.isMaximized)
maximize();

m_zoomLevel.zoomLevelIndex().set_value(m_settingsManager.loadZoomLevel());
}

void AppWindow::saveWindowState()
void AppWindow::saveCurrentSessionState()
{
m_settingsManager.saveWindowState(m_windowState);
m_settingsManager.saveZoomLevel(m_zoomLevel.zoomLevelIndex().get_value());
}

void AppWindow::loadWidgets()
Expand Down
4 changes: 2 additions & 2 deletions src/application/appwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ class AppWindow : public Gtk::ApplicationWindow {
Glib::RefPtr<Gio::SimpleAction> m_closeWindowAction;

// Functions
void loadWindowState();
void saveWindowState();
void loadPreviousSessionState();
void saveCurrentSessionState();
void loadWidgets();
void addActions();
void setupWidgets();
Expand Down
32 changes: 32 additions & 0 deletions src/application/settingsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ namespace window_state {
static const WindowState defaultWindowState = {800, 600, false};
}

namespace misc {
static const std::string groupName = "misc";

static const struct {
std::string zoomLevel = "zoom-level";
} keys;

static const unsigned int defaultZoomLevel = 0;
}

SettingsManager::SettingsManager()
{
loadConfigFile();
Expand Down Expand Up @@ -74,6 +84,28 @@ void SettingsManager::saveWindowState(const WindowState& windowState)
}
}

unsigned int SettingsManager::loadZoomLevel()
{
try {
return static_cast<unsigned>(m_keyFile.get_integer(misc::groupName, misc::keys.zoomLevel));
}
catch (const Glib::Error& e) {
Logger::logWarning("Error while loading zoom level: " + e.what());

return misc::defaultZoomLevel;
}
}

void SettingsManager::saveZoomLevel(unsigned int zoomLevel)
{
try {
m_keyFile.set_integer(misc::groupName, misc::keys.zoomLevel, static_cast<int>(zoomLevel));
}
catch (const Glib::Error& e) {
Logger::logWarning("Error while writing zoom level: " + e.what());
}
}

void SettingsManager::loadConfigFile()
{
try {
Expand Down
3 changes: 3 additions & 0 deletions src/application/settingsmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class SettingsManager {
WindowState loadWindowState();
void saveWindowState(const WindowState& windowState);

unsigned int loadZoomLevel();
void saveZoomLevel(unsigned int zoomLevel);

private:
Glib::KeyFile m_keyFile;

Expand Down

0 comments on commit edbf16d

Please sign in to comment.