Skip to content

Commit

Permalink
fix window state saving and recall
Browse files Browse the repository at this point in the history
  • Loading branch information
vsicurella committed Jun 3, 2024
1 parent fd8c7ce commit f52df88
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 32 deletions.
3 changes: 3 additions & 0 deletions Source/LumatoneEditorState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ bool LumatoneEditorState::Controller::savePropertiesFile() const
jassert(editorState.propertiesFile != nullptr);
editorState.propertiesFile->setValue(LumatoneEditorProperty::RecentFiles, editorState.recentFiles->toString());

auto saveWindowState = editorState.getStringProperty(LumatoneEditorProperty::MainWindowState);
editorState.propertiesFile->setValue(LumatoneEditorProperty::MainWindowState, saveWindowState);

return editorState.propertiesFile->saveIfNeeded();
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ void TerpstraSysExApplication::initialise(const String& commandLine)
commandManager->registerAllCommandsForTarget(this);

mainWindow.reset(new MainWindow(state, commandManager.get()));
mainWindow->restoreStateFromPropertiesFile(getPropertiesFile());

// mainWindow->restoreStateFromPropertiesFile(getPropertiesFile());
mainWindow->setVisible(true);

if (state.getCurrentFile().existsAsFile())
resetToCurrentFile();
Expand Down
2 changes: 1 addition & 1 deletion Source/MainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ MainContentComponent::MainContentComponent(const LumatoneEditorState& stateIn, j
addStatusListener(this);

// Initial size
setSize(DEFAULTMAINWINDOWWIDTH, DEFAULTMAINWINDOWHEIGHT);
// setSize(DEFAULTMAINWINDOWWIDTH, DEFAULTMAINWINDOWHEIGHT);

// Select first board and first key
// noteEditArea->getOctaveBoardSelectorTab()->setCurrentTabIndex(0, true);
Expand Down
68 changes: 42 additions & 26 deletions Source/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ MainWindow::MainWindow(const LumatoneEditorState& stateIn, juce::ApplicationComm
, LumatoneEditorState::Controller(static_cast<LumatoneEditorState&>(*this))
, commandManager(cmdManager)
{
setContentOwned(new MainContentComponent(*this, commandManager), true);
setResizable(true, true);
// Window aspect ratio
setResizeLimits(800, juce::roundToInt(800 / DEFAULTMAINWINDOWASPECT), juce::roundToInt(1000 * DEFAULTMAINWINDOWASPECT), 1000);
getConstrainer()->setFixedAspectRatio(DEFAULTMAINWINDOWASPECT);
getConstrainer()->setMinimumOnscreenAmounts(0xffffff, 0xffffff, 0xffffff, 0xffffff);

restoreStateFromPropertiesFile(getPropertiesFile());

auto mainComponent = new MainContentComponent(*this, commandManager);
mainComponent->setSize(getWidth(), getHeight());
setContentOwned(mainComponent, true);

#if JUCE_ANDROID
setFullScreen(true);
#else
setResizable(true, true);
setLookAndFeel(&getEditorLookAndFeel());

menuModel = std::make_unique<Lumatone::Menu::MainMenuModel>(*this, commandManager);
Expand All @@ -43,20 +52,10 @@ MainWindow::MainWindow(const LumatoneEditorState& stateIn, juce::ApplicationComm
);
#endif

// Window aspect ratio
constrainer.reset(new juce::ComponentBoundsConstrainer());

constrainer->setFixedAspectRatio(DEFAULTMAINWINDOWASPECT);
constrainer->setMinimumSize(800, juce::roundToInt(800 / DEFAULTMAINWINDOWASPECT));
constrainer->setMaximumHeight(1000);
setConstrainer(constrainer.get());

addKeyListener(commandManager->getKeyMappings());
updateBounds();

addEditorListener(this);

startTimer(2000);
#endif
}

Expand All @@ -72,9 +71,6 @@ MainWindow::~MainWindow()
#endif

menuModel = nullptr;

stopTimer();
setConstrainer(nullptr);
}

void MainWindow::closeButtonPressed()
Expand Down Expand Up @@ -110,6 +106,23 @@ bool MainWindow::isOutOfHorizontalBounds() const
|| getScreenX() >= (maxWindowWidth - horizontalBoundsThreshold);
}

void MainWindow::saveBounds()
{
setWindowState(getBounds(), getWindowStateAsString());
}

void MainWindow::resized()
{
juce::DocumentWindow::resized();
saveBounds();
}

void MainWindow::moved()
{
juce::DocumentWindow::moved();
saveBounds();
}

void MainWindow::saveStateToPropertiesFile(PropertiesFile* propertiesFile)
{
// Save state of main window
Expand All @@ -119,12 +132,13 @@ void MainWindow::saveStateToPropertiesFile(PropertiesFile* propertiesFile)

void MainWindow::restoreStateFromPropertiesFile(PropertiesFile* propertiesFile)
{
bool useSavedState = restoreWindowStateFromString(getProperty(LumatoneEditorProperty::MainWindowState));
// auto restoredState = getProperty(LumatoneEditorProperty::MainWindowState);
bool useSavedState = restoreWindowStateFromString(propertiesFile->getValue(LumatoneEditorProperty::MainWindowState));

fixWindowPositionAndSize(!useSavedState);
setWindowState(getBounds(), getWindowStateAsString());
// fixWindowPositionAndSize(!useSavedState);
// setWindowState(getBounds(), getWindowStateAsString());

setVisible(true);
saveBounds();
}

void MainWindow::updateBounds()
Expand All @@ -139,7 +153,7 @@ void MainWindow::updateBounds()
{
maxWindowWidth = screenWidth;
maxWindowHeight = screenHeight;
constrainer->setMaximumHeight(maxWindowHeight);
// constrainer->setMaximumHeight(maxWindowHeight);

fixWindowPositionAndSize();
return;
Expand Down Expand Up @@ -177,13 +191,15 @@ void MainWindow::fixWindowPositionAndSize(bool setToDefault)
}
}

void MainWindow::timerCallback()
{
// Set threshold to be a quarter of the window handle height
verticalBoundsThreshold = round(getTitleBarHeight() * 0.25f);
// void MainWindow::timerCallback()
// {
// // Set threshold to be a quarter of the window handle height
// // verticalBoundsThreshold = round(getTitleBarHeight() * 0.25f);

updateBounds();
}
// // updateBounds();

// setWindowState(getBounds(), getWindowStateAsString());
// }

void MainWindow::updateTitle()
{
Expand Down
10 changes: 7 additions & 3 deletions Source/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class MainWindow : public juce::DocumentWindow
, public LumatoneEditorState
, private LumatoneEditorState::Controller
, private LumatoneEditor::EditorListener
, private juce::Timer
{
public:
MainWindow(const LumatoneEditorState& stateIn, juce::ApplicationCommandManager* commandManager);
Expand All @@ -58,6 +57,12 @@ class MainWindow : public juce::DocumentWindow
// Returns true if left side of window is too far right, or if right side of window is too far left
bool isOutOfHorizontalBounds() const;

void saveBounds();

void resized() override;

void moved() override;

void saveStateToPropertiesFile(PropertiesFile* propertiesFile);

void restoreStateFromPropertiesFile(PropertiesFile* propertiesFile);
Expand All @@ -67,7 +72,7 @@ class MainWindow : public juce::DocumentWindow

void fixWindowPositionAndSize(bool setToDefault=false);

void timerCallback() override;
// void timerCallback() override;

void updateTitle();

Expand All @@ -81,7 +86,6 @@ class MainWindow : public juce::DocumentWindow
private:
juce::ApplicationCommandManager* commandManager;
std::unique_ptr<Lumatone::Menu::MainMenuModel> menuModel;
std::unique_ptr<ComponentBoundsConstrainer> constrainer;

int maxWindowWidth = 0;
int maxWindowHeight = 0;
Expand Down

0 comments on commit f52df88

Please sign in to comment.