Skip to content

Commit

Permalink
[viewer] Add theme file to look&feel
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Oct 24, 2023
1 parent 48a70bc commit c6c859e
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 9 deletions.
4 changes: 4 additions & 0 deletions tools/jml-viewer/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ auto Application::initialise(juce::String const& commandLine) -> void
}

juce::LookAndFeel::setDefaultLookAndFeel(&_lnf);
if (auto const theme = loadTheme(getApplicationSettings().getThemeFile()); theme) {
apply(*theme, _lnf);
}

_mainWindow = std::make_unique<MainWindow>(getApplicationName());
}

Expand Down
3 changes: 2 additions & 1 deletion tools/jml-viewer/Application/MainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace jml::viewer {

MainComponent::MainComponent()
{
_menuBar.onFileLoad = [this](auto const& file) { _documents.openScript(file); };
_settings.onThemeChange = [this] { getTopLevelComponent()->repaint(); };
_menuBar.onFileLoad = [this](auto const& file) { _documents.openScript(file); };

_commandManager.registerAllCommandsForTarget(this);
addKeyListener(_commandManager.getKeyMappings());
Expand Down
42 changes: 36 additions & 6 deletions tools/jml-viewer/Application/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ auto createSettingsOptions() -> juce::PropertiesFile::Options
options.folderName = "neo-sonar/" + juce::String{name};
#endif

DBG(options.getDefaultFile().getFullPathName());
return options;
}

Expand Down Expand Up @@ -72,6 +71,16 @@ auto Settings::setKeyMapping(juce::XmlElement const& xml) -> void
settings.setValue("key_mapping", &xml);
}

auto Settings::getThemeFile() -> juce::File
{
return juce::File{_settings.getUserSettings()->getValue("theme_file")};
}

auto Settings::setThemeFile(juce::File const& file) -> void
{
_settings.getUserSettings()->setValue("theme_file", file.getFullPathName());
}

auto Settings::saveRecentFiles() -> void
{
auto& settings = *_settings.getUserSettings();
Expand All @@ -96,8 +105,8 @@ SettingsWindow::~SettingsWindow() { _commandManager.getKeyMappings()->removeChan
auto SettingsWindow::createComponentForPage(juce::String const& pageName) -> juce::Component*
{
if (pageName == "Code") {
auto foo = tree.getPropertyAsValue("foo", nullptr);
auto bar = tree.getPropertyAsValue("bar", nullptr);
auto foo = _tree.getPropertyAsValue("foo", nullptr);
auto bar = _tree.getPropertyAsValue("bar", nullptr);
auto panel = std::make_unique<juce::PropertyPanel>();
panel->addProperties(juce::Array<juce::PropertyComponent*>{
std::make_unique<juce::SliderPropertyComponent>(foo, "Foo", 0.0, 1.0, 0.0).release(),
Expand All @@ -112,13 +121,17 @@ auto SettingsWindow::createComponentForPage(juce::String const& pageName) -> juc
}

if (pageName == "Look") {
auto theme = tree.getPropertyAsValue("theme", nullptr);
auto themeOptions = FilePropertyComponent::Options{.pattern = "*.lua"};
auto themeOptions = FilePropertyComponent::Options{.pattern = "*.xml"};
auto theme = _tree.getPropertyAsValue("theme_file", nullptr);
if (auto file = getApplicationSettings().getThemeFile(); file.existsAsFile()) {
theme.setValue(file.getFullPathName());
}

auto panel = std::make_unique<juce::PropertyPanel>();
panel->addProperties(juce::Array<juce::PropertyComponent*>{
std::make_unique<FilePropertyComponent>(theme, "Theme", themeOptions).release(),
std::make_unique<CallbackPropertyComponent>("Reload", [] {}).release(),
std::make_unique<CallbackPropertyComponent>("Reload", [this] { remapColours(); })
.release(),
});
return panel.release();
}
Expand All @@ -135,4 +148,21 @@ auto SettingsWindow::changeListenerCallback(juce::ChangeBroadcaster* source) ->
}
}

auto SettingsWindow::remapColours() -> void
{
auto const file = fromVar<juce::File>(_tree["theme_file"]);
auto const theme = loadTheme(file);
if (not theme) {
return;
}

apply(*theme, getLookAndFeel());
getTopLevelComponent()->repaint();
if (onThemeChange) {
onThemeChange();
}

getApplicationSettings().setThemeFile(file);
}

} // namespace jml::viewer
9 changes: 8 additions & 1 deletion tools/jml-viewer/Application/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct Settings
[[nodiscard]] auto getKeyMapping() -> std::unique_ptr<juce::XmlElement>;
auto setKeyMapping(juce::XmlElement const& xml) -> void;

[[nodiscard]] auto getThemeFile() -> juce::File;
auto setThemeFile(juce::File const& file) -> void;

private:
auto saveRecentFiles() -> void;
juce::ApplicationProperties _settings;
Expand All @@ -31,9 +34,13 @@ struct SettingsWindow final
auto createComponentForPage(juce::String const& pageName) -> juce::Component* override;
auto changeListenerCallback(juce::ChangeBroadcaster* source) -> void override;

std::function<void()> onThemeChange;

private:
auto remapColours() -> void;

juce::ApplicationCommandManager& _commandManager;
juce::ValueTree tree{"Settings"};
juce::ValueTree _tree{"Settings"};
};

} // namespace jml::viewer
2 changes: 1 addition & 1 deletion tools/jml-viewer/Viewer/ScriptPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ auto MultiScriptPanel::openScript(juce::File const& script) -> void
{
auto panel = std::make_unique<ScriptPanel>(_commandManager);
panel->setScriptFile(script);
addDocument(panel.release(), juce::Colours::white, true);
addDocument(panel.release(), getSchemeWindowBackgroundColour(), true);
}

auto MultiScriptPanel::reloadActiveScript() -> void
Expand Down
1 change: 1 addition & 0 deletions tools/jml_tools/jml_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "graphics/Color.cpp" // NOLINT
#include "graphics/Drawable.cpp" // NOLINT
#include "look_and_feel/LookAndFeel.cpp" // NOLINT
#include "look_and_feel/Theme.cpp" // NOLINT
#include "property/CallbackPropertyComponent.cpp" // NOLINT
#include "property/ColourPropertyComponent.cpp" // NOLINT
#include "property/FilePropertyComponent.cpp" // NOLINT
Expand Down
1 change: 1 addition & 0 deletions tools/jml_tools/jml_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ END_JUCE_MODULE_DECLARATION
#include "property/FilePropertyComponent.hpp"
#include "look_and_feel/Colours.hpp"
#include "look_and_feel/LookAndFeel.hpp"
#include "look_and_feel/Theme.hpp"
// clang-format on
Loading

0 comments on commit c6c859e

Please sign in to comment.