-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[viewer] Add option to restore recent files
- Loading branch information
1 parent
1d4b60f
commit d6ed3f3
Showing
8 changed files
with
232 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
#include "Application.hpp" | ||
|
||
#include "Application/CommandLine.hpp" | ||
#include "Application/MainComponent.hpp" | ||
#include "Command/Snapshot.hpp" | ||
#include "Command/Test.hpp" | ||
|
||
#include <jml_tools/jml_tools.hpp> | ||
|
||
namespace jml::viewer { | ||
|
||
namespace { | ||
|
||
auto createSettingsOptions() -> juce::PropertiesFile::Options | ||
{ | ||
auto const name = getApplication().getApplicationName(); | ||
|
||
auto options = juce::PropertiesFile::Options{}; | ||
options.applicationName = "settings"; | ||
options.filenameSuffix = ".xml"; | ||
options.commonToAllUsers = false; | ||
options.ignoreCaseOfKeyNames = false; | ||
options.doNotSave = false; | ||
options.millisecondsBeforeSaving = 3'000; | ||
options.storageFormat = juce::PropertiesFile::StorageFormat::storeAsXML; | ||
options.processLock = nullptr; | ||
options.osxLibrarySubFolder = "Application Support"; | ||
|
||
#if JUCE_LINUX | ||
options.folderName = ".config/neo-sonar/" + juce::String{name}; | ||
#else | ||
options.folderName = "neo-sonar/" + juce::String{name}; | ||
#endif | ||
|
||
DBG(options.getDefaultFile().getFullPathName()); | ||
return options; | ||
} | ||
|
||
auto runCommand(auto const func, auto const& cli) -> void | ||
{ | ||
auto& app = *juce::JUCEApplication::getInstance(); | ||
app.setApplicationReturnValue(EXIT_SUCCESS); | ||
|
||
if (auto const result = func(cli); result.failed()) { | ||
std::cerr << result.getErrorMessage() << '\n'; | ||
app.setApplicationReturnValue(EXIT_FAILURE); | ||
} | ||
|
||
app.quit(); | ||
} | ||
|
||
} // namespace | ||
|
||
Application::Application() { _settings.setStorageParameters(createSettingsOptions()); } | ||
|
||
auto Application::getSettings() -> juce::ApplicationProperties& { return _settings; } | ||
|
||
auto Application::getApplicationName() -> juce::String const { return JUCE_APPLICATION_NAME_STRING; } | ||
|
||
auto Application::getApplicationVersion() -> juce::String const | ||
{ | ||
return JUCE_APPLICATION_VERSION_STRING; | ||
} | ||
|
||
auto Application::moreThanOneInstanceAllowed() -> bool { return true; } | ||
|
||
auto Application::initialise(juce::String const& commandLine) -> void | ||
{ | ||
auto const [cli, shouldExit] = jml::viewer::parseCommandLine(commandLine); | ||
if (cli == nullptr or shouldExit) { | ||
setApplicationReturnValue(cli == nullptr ? EXIT_FAILURE : EXIT_SUCCESS); | ||
quit(); | ||
return; | ||
} | ||
|
||
if (cli->app.got_subcommand("test")) { | ||
return runCommand(jml::viewer::runTestScript, *cli); | ||
} | ||
if (cli->app.got_subcommand("snapshot")) { | ||
return runCommand(jml::viewer::runSnapshotScript, *cli); | ||
} | ||
|
||
juce::LookAndFeel::setDefaultLookAndFeel(&_lnf); | ||
_mainWindow = std::make_unique<MainWindow>(getApplicationName()); | ||
} | ||
|
||
auto Application::shutdown() -> void | ||
{ | ||
_mainWindow = nullptr; | ||
juce::LookAndFeel::setDefaultLookAndFeel(nullptr); | ||
} | ||
|
||
auto Application::systemRequestedQuit() -> void { quit(); } | ||
|
||
auto Application::anotherInstanceStarted(juce::String const& commandLine) -> void | ||
{ | ||
juce::ignoreUnused(commandLine); | ||
} | ||
|
||
Application::MainWindow::MainWindow(juce::String const& name) | ||
: DocumentWindow( | ||
name, | ||
juce::Desktop::getInstance().getDefaultLookAndFeel().findColour( | ||
ResizableWindow::backgroundColourId | ||
), | ||
DocumentWindow::allButtons | ||
) | ||
{ | ||
setUsingNativeTitleBar(true); | ||
setContentOwned(std::make_unique<jml::viewer::MainComponent>().release(), true); | ||
|
||
#if JUCE_IOS || JUCE_ANDROID | ||
setFullScreen(true); | ||
#else | ||
setResizable(true, true); | ||
centreWithSize(getWidth(), getHeight()); | ||
#endif | ||
|
||
setVisible(true); | ||
} | ||
|
||
auto Application::MainWindow::closeButtonPressed() -> void | ||
{ | ||
JUCEApplication::getInstance()->systemRequestedQuit(); | ||
} | ||
|
||
auto getApplication() -> Application& | ||
{ | ||
auto* app = dynamic_cast<Application*>(juce::JUCEApplication::getInstance()); | ||
jassert(app != nullptr); | ||
return *app; | ||
} | ||
|
||
auto getApplicationSettings() -> juce::PropertiesFile& | ||
{ | ||
auto* settings = getApplication().getSettings().getUserSettings(); | ||
jassert(settings != nullptr); | ||
return *settings; | ||
} | ||
|
||
auto getRecentOpenFiles() -> juce::StringArray | ||
{ | ||
auto& settings = getApplicationSettings(); | ||
auto const property = settings.getValue("recent_open_files", ""); | ||
return juce::StringArray::fromTokens(property, ";", ""); | ||
} | ||
|
||
auto appendToRecentOpenFiles(juce::File const& file) -> void | ||
{ | ||
auto fileList = getRecentOpenFiles(); | ||
fileList.addIfNotAlreadyThere(file.getFullPathName()); | ||
getApplicationSettings().setValue("recent_open_files", fileList.joinIntoString(";")); | ||
} | ||
|
||
auto clearRecentOpenFiles() -> void { getApplicationSettings().setValue("recent_open_files", ""); } | ||
|
||
} // namespace jml::viewer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#pragma once | ||
|
||
#include <jml_tools/jml_tools.hpp> | ||
#include <juce_gui_basics/juce_gui_basics.h> | ||
|
||
namespace jml::viewer { | ||
|
||
struct Application final : juce::JUCEApplication | ||
{ | ||
Application(); | ||
~Application() override = default; | ||
|
||
auto getSettings() -> juce::ApplicationProperties&; | ||
|
||
auto getApplicationName() -> juce::String const override; | ||
auto getApplicationVersion() -> juce::String const override; | ||
|
||
auto initialise(juce::String const& commandLine) -> void override; | ||
auto shutdown() -> void override; | ||
auto systemRequestedQuit() -> void override; | ||
|
||
auto moreThanOneInstanceAllowed() -> bool override; | ||
auto anotherInstanceStarted(juce::String const& commandLine) -> void override; | ||
|
||
struct MainWindow : juce::DocumentWindow | ||
{ | ||
explicit MainWindow(juce::String const& name); | ||
~MainWindow() override = default; | ||
|
||
auto closeButtonPressed() -> void override; | ||
}; | ||
|
||
private: | ||
juce::ApplicationProperties _settings; | ||
jml::LookAndFeel _lnf; | ||
std::unique_ptr<MainWindow> _mainWindow; | ||
}; | ||
|
||
[[nodiscard]] auto getApplication() -> Application&; | ||
[[nodiscard]] auto getApplicationSettings() -> juce::PropertiesFile&; | ||
|
||
[[nodiscard]] auto getRecentOpenFiles() -> juce::StringArray; | ||
auto appendToRecentOpenFiles(juce::File const& file) -> void; | ||
auto clearRecentOpenFiles() -> void; | ||
|
||
} // namespace jml::viewer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,3 @@ | ||
#include "Application/CommandLine.hpp" | ||
#include "Application/MainComponent.hpp" | ||
#include "Command/Snapshot.hpp" | ||
#include "Command/Test.hpp" | ||
#include "Application/Application.hpp" | ||
|
||
#include <jml_tools/jml_tools.hpp> | ||
|
||
namespace { | ||
auto runCommand(auto const func, auto const& cli) -> void | ||
{ | ||
auto& app = *juce::JUCEApplication::getInstance(); | ||
app.setApplicationReturnValue(EXIT_SUCCESS); | ||
|
||
if (auto const result = func(cli); result.failed()) { | ||
std::cerr << result.getErrorMessage() << '\n'; | ||
app.setApplicationReturnValue(EXIT_FAILURE); | ||
} | ||
|
||
app.quit(); | ||
} | ||
} // namespace | ||
|
||
struct GuiAppApplication final : juce::JUCEApplication | ||
{ | ||
|
||
GuiAppApplication() = default; | ||
|
||
auto getApplicationName() -> juce::String const override { return JUCE_APPLICATION_NAME_STRING; } | ||
|
||
auto getApplicationVersion() -> juce::String const override | ||
{ | ||
return JUCE_APPLICATION_VERSION_STRING; | ||
} | ||
|
||
auto moreThanOneInstanceAllowed() -> bool override { return true; } | ||
|
||
auto initialise(juce::String const& commandLine) -> void override | ||
{ | ||
auto const [cli, shouldExit] = jml::viewer::parseCommandLine(commandLine); | ||
if (cli == nullptr or shouldExit) { | ||
setApplicationReturnValue(cli == nullptr ? EXIT_FAILURE : EXIT_SUCCESS); | ||
quit(); | ||
return; | ||
} | ||
|
||
if (cli->app.got_subcommand("test")) { | ||
return runCommand(jml::viewer::runTestScript, *cli); | ||
} | ||
if (cli->app.got_subcommand("snapshot")) { | ||
return runCommand(jml::viewer::runSnapshotScript, *cli); | ||
} | ||
|
||
juce::LookAndFeel::setDefaultLookAndFeel(&_lnf); | ||
_mainWindow = std::make_unique<MainWindow>(getApplicationName()); | ||
} | ||
|
||
auto shutdown() -> void override | ||
{ | ||
_mainWindow = nullptr; | ||
juce::LookAndFeel::setDefaultLookAndFeel(nullptr); | ||
} | ||
|
||
auto systemRequestedQuit() -> void override { quit(); } | ||
|
||
auto anotherInstanceStarted(juce::String const& commandLine) -> void override | ||
{ | ||
juce::ignoreUnused(commandLine); | ||
} | ||
|
||
struct MainWindow : juce::DocumentWindow | ||
{ | ||
|
||
explicit MainWindow(juce::String const& name) | ||
: DocumentWindow( | ||
name, | ||
juce::Desktop::getInstance().getDefaultLookAndFeel().findColour( | ||
ResizableWindow::backgroundColourId | ||
), | ||
DocumentWindow::allButtons | ||
) | ||
{ | ||
setUsingNativeTitleBar(true); | ||
setContentOwned(std::make_unique<jml::viewer::MainComponent>().release(), true); | ||
|
||
#if JUCE_IOS || JUCE_ANDROID | ||
setFullScreen(true); | ||
#else | ||
setResizable(true, true); | ||
centreWithSize(getWidth(), getHeight()); | ||
#endif | ||
|
||
setVisible(true); | ||
} | ||
|
||
auto closeButtonPressed() -> void override | ||
{ | ||
JUCEApplication::getInstance()->systemRequestedQuit(); | ||
} | ||
|
||
private: | ||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MainWindow) // NOLINT | ||
}; | ||
|
||
private: | ||
jml::LookAndFeel _lnf; | ||
std::unique_ptr<MainWindow> _mainWindow; | ||
}; | ||
|
||
// This macro generates the main() routine that launches the app. | ||
START_JUCE_APPLICATION(GuiAppApplication) // NOLINT | ||
START_JUCE_APPLICATION(jml::viewer::Application) // NOLINT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.