From f52df8858439da66466c20edc2198ec427e3a73c Mon Sep 17 00:00:00 2001 From: Vincenzo Sicurella Date: Sun, 2 Jun 2024 22:55:58 -0400 Subject: [PATCH] fix window state saving and recall --- Source/LumatoneEditorState.cpp | 3 ++ Source/Main.cpp | 4 +- Source/MainComponent.cpp | 2 +- Source/MainWindow.cpp | 68 +++++++++++++++++++++------------- Source/MainWindow.h | 10 +++-- 5 files changed, 55 insertions(+), 32 deletions(-) diff --git a/Source/LumatoneEditorState.cpp b/Source/LumatoneEditorState.cpp index 363941e..9471f89 100644 --- a/Source/LumatoneEditorState.cpp +++ b/Source/LumatoneEditorState.cpp @@ -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(); } diff --git a/Source/Main.cpp b/Source/Main.cpp index 96e276d..5989860 100644 --- a/Source/Main.cpp +++ b/Source/Main.cpp @@ -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(); diff --git a/Source/MainComponent.cpp b/Source/MainComponent.cpp index 7497c3f..0c05075 100644 --- a/Source/MainComponent.cpp +++ b/Source/MainComponent.cpp @@ -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); diff --git a/Source/MainWindow.cpp b/Source/MainWindow.cpp index 25afc1e..a96f159 100644 --- a/Source/MainWindow.cpp +++ b/Source/MainWindow.cpp @@ -23,12 +23,21 @@ MainWindow::MainWindow(const LumatoneEditorState& stateIn, juce::ApplicationComm , LumatoneEditorState::Controller(static_cast(*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(*this, commandManager); @@ -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 } @@ -72,9 +71,6 @@ MainWindow::~MainWindow() #endif menuModel = nullptr; - - stopTimer(); - setConstrainer(nullptr); } void MainWindow::closeButtonPressed() @@ -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 @@ -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() @@ -139,7 +153,7 @@ void MainWindow::updateBounds() { maxWindowWidth = screenWidth; maxWindowHeight = screenHeight; - constrainer->setMaximumHeight(maxWindowHeight); + // constrainer->setMaximumHeight(maxWindowHeight); fixWindowPositionAndSize(); return; @@ -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() { diff --git a/Source/MainWindow.h b/Source/MainWindow.h index a7e0bf9..2109f7f 100644 --- a/Source/MainWindow.h +++ b/Source/MainWindow.h @@ -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); @@ -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); @@ -67,7 +72,7 @@ class MainWindow : public juce::DocumentWindow void fixWindowPositionAndSize(bool setToDefault=false); - void timerCallback() override; + // void timerCallback() override; void updateTitle(); @@ -81,7 +86,6 @@ class MainWindow : public juce::DocumentWindow private: juce::ApplicationCommandManager* commandManager; std::unique_ptr menuModel; - std::unique_ptr constrainer; int maxWindowWidth = 0; int maxWindowHeight = 0;