Skip to content

Commit

Permalink
More work on undo
Browse files Browse the repository at this point in the history
  • Loading branch information
abique committed Sep 16, 2024
1 parent a5a9c0a commit 7084f4a
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 94 deletions.
2 changes: 2 additions & 0 deletions plugins/clap-entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "plugs/offline-latency/offline-latency.hh"
#include "plugs/realtime-requirement/realtime-requirement.hh"
#include "plugs/track-info/track-info.hh"
#include "plugs/undo-test/undo-test.hh"

struct PluginEntry {
using create_func = std::function<const clap_plugin *(const clap_host &)>;
Expand Down Expand Up @@ -53,6 +54,7 @@ static bool clap_init(const char *plugin_path) {
addPlugin<clap::OfflineLatency>();
addPlugin<clap::RealtimeRequirement>();
addPlugin<clap::TrackInfo>();
addPlugin<clap::UndoTest>();
return true;
}

Expand Down
104 changes: 81 additions & 23 deletions plugins/core-plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@
#include "gui/abstract-gui.hh"

#ifndef CLAP_PLUGINS_HEADLESS
# ifdef CLAP_REMOTE_GUI
# include "gui/remote-gui-factory-proxy.hh"
# else
# include "gui/local-gui-factory.hh"
# include "gui/threaded-gui-factory.hh"
# endif
# include "gui/local-gui-factory.hh"
# include "gui/threaded-gui-factory.hh"
#endif

namespace clap {
Expand Down Expand Up @@ -151,23 +147,12 @@ namespace clap {
#ifndef CLAP_PLUGINS_HEADLESS
bool CorePlugin::implementsGui() const noexcept { return true; }

bool CorePlugin::guiIsApiSupported(const char *api, bool isFloating) noexcept {
# if defined(__APPLE__) && defined(CLAP_REMOTE_GUI)
return isFloating;
# else
return true;
# endif
}
bool CorePlugin::guiIsApiSupported(const char *api, bool isFloating) noexcept { return true; }

bool CorePlugin::guiCreate(const char *api, bool isFloating) noexcept {
# ifdef CLAP_REMOTE_GUI
auto guiPath = _pathProvider->getGuiExecutable();
_guiFactory = RemoteGuiFactoryProxy::getInstance(guiPath);
# else
_guiFactory = LocalGuiFactory::getInstance();
if (!_guiFactory)
_guiFactory = ThreadedGuiFactory::getInstance();
# endif

_guiHandle = _guiFactory->createGui(*this);

Expand All @@ -176,6 +161,7 @@ namespace clap {

guiDefineParameters();
guiDefineTrackInfo();
guiSubscribeUndo();

auto skinPath = _pathProvider->getQmlSkinPath();
_guiHandle->gui().addImportPath(_pathProvider->getQmlLibraryPath());
Expand Down Expand Up @@ -234,7 +220,10 @@ namespace clap {
}
}

void CorePlugin::guiDestroy() noexcept { _guiHandle.reset(); }
void CorePlugin::guiDestroy() noexcept {
guiUnsubscribeUndo();
_guiHandle.reset();
}

bool CorePlugin::guiGetSize(uint32_t *width, uint32_t *height) noexcept {
if (!_guiHandle)
Expand Down Expand Up @@ -263,13 +252,31 @@ namespace clap {
bool CorePlugin::guiShow() noexcept {
if (!_guiHandle)
return false;
return _guiHandle->gui().show();
if (!_guiHandle->gui().show())
return false;

guiSubscribeUndo();
return true;
}

bool CorePlugin::guiHide() noexcept {
if (!_guiHandle)
return false;
return _guiHandle->gui().hide();
if (!_guiHandle->gui().hide())
return false;

guiUnsubscribeUndo();
return true;
}

void CorePlugin::guiSubscribeUndo() {
if (implementsUndo() && _host.canUseUndo())
_host.undoSetWantsContextUpdates(true);
}

void CorePlugin::guiUnsubscribeUndo() {
if (implementsUndo() && _host.canUseUndo())
_host.undoSetWantsContextUpdates(false);
}

//---------------------//
Expand Down Expand Up @@ -344,14 +351,14 @@ namespace clap {

void CorePlugin::onGuiUndo() {
runOnMainThread([this] {
if (_host.canUseUndo())
if (_canUndo && _host.canUseUndo())
_host.undoUndo();
});
}

void CorePlugin::onGuiRedo() {
runOnMainThread([this] {
if (_host.canUseUndo())
if (_canRedo && _host.canUseUndo())
_host.undoRedo();
});
}
Expand Down Expand Up @@ -911,4 +918,55 @@ namespace clap {
return true;
}

void CorePlugin::undoSetCanUndo(bool can_undo) noexcept {
_canUndo = can_undo;

#ifndef CLAP_PLUGINS_HEADLESS
if (_guiHandle)
_guiHandle->gui().setCanUndo(can_undo);
#endif

if (!can_undo)
undoSetUndoName(nullptr);
}

void CorePlugin::undoSetCanRedo(bool can_redo) noexcept {
_canRedo = can_redo;

#ifndef CLAP_PLUGINS_HEADLESS
if (_guiHandle)
_guiHandle->gui().setCanRedo(can_redo);
#endif

if (!can_redo)
undoSetRedoName(nullptr);
}

void CorePlugin::undoSetUndoName(const char *name) noexcept {
if (name && *name) {
if (!_canUndo)
hostMisbehaving("set undo name while it isn't possible to undo");
_undoName = name;
} else
_undoName.reset();

#ifndef CLAP_PLUGINS_HEADLESS
if (_guiHandle)
_guiHandle->gui().setUndoName(_undoName.value_or("(none)"));
#endif
}

void CorePlugin::undoSetRedoName(const char *name) noexcept {
if (name && *name) {
if (!_canRedo)
hostMisbehaving("set undo name while it isn't possible to redo");
_redoName = name;
} else
_redoName.reset();

#ifndef CLAP_PLUGINS_HEADLESS
if (_guiHandle)
_guiHandle->gui().setRedoName(_undoName.value_or("(none)"));
#endif
}
} // namespace clap
16 changes: 16 additions & 0 deletions plugins/core-plugin.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <memory>
#include <optional>

#include <clap/helpers/param-queue.hh>
#include <clap/helpers/plugin.hh>
Expand Down Expand Up @@ -164,6 +165,8 @@ namespace clap {
bool guiShow() noexcept override;
bool guiHide() noexcept override;
void guiDefineParameters();
void guiSubscribeUndo();
void guiUnsubscribeUndo();

//---------------------//
// AbstractGuiListener //
Expand Down Expand Up @@ -199,6 +202,14 @@ namespace clap {
bool implementsLatency() const noexcept override { return true; }
uint32_t latencyGet() const noexcept override { return _rootModule->latency(); }

//------------------//
// clap_plugin_undo //
//------------------//
void undoSetCanUndo(bool can_undo) noexcept override;
void undoSetCanRedo(bool can_redo) noexcept override;
void undoSetUndoName(const char *name) noexcept override;
void undoSetRedoName(const char *name) noexcept override;

//////////////////////
// Cached Host Info //
//////////////////////
Expand Down Expand Up @@ -295,5 +306,10 @@ namespace clap {
std::unique_ptr<Module> _rootModule;

TuningProvider _tuningProvider;

bool _canUndo{false};
bool _canRedo{false};
std::optional<std::string> _undoName;
std::optional<std::string> _redoName;
};
} // namespace clap
1 change: 1 addition & 0 deletions plugins/gui/qml/clap/skins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ qt6_add_qml_module(clap-qml-skins
char-check/main.qml
synth/main.qml
track-info/main.qml
undo-test/main.qml
)
4 changes: 2 additions & 2 deletions plugins/gui/qml/clap/skins/char-check/main.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import QtQuick 2.1
import QtQuick.Controls 2.1
import QtQuick 6.0
import QtQuick.Controls 6.0

Rectangle {
width: 250
Expand Down
4 changes: 2 additions & 2 deletions plugins/gui/qml/clap/skins/dc-offset/main.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import QtQuick 2.1
import QtQuick.Controls 2.1
import QtQuick 6.0
import QtQuick.Controls 6.0
import clap.lib

Rectangle {
Expand Down
4 changes: 2 additions & 2 deletions plugins/gui/qml/clap/skins/gain/main.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import QtQuick 2.1
import QtQuick.Controls 2.1
import QtQuick 6.0
import QtQuick.Controls 6.0
import clap.lib

Rectangle {
Expand Down
4 changes: 2 additions & 2 deletions plugins/gui/qml/clap/skins/synth/main.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import QtQuick 2.1
import QtQuick.Controls 2.1
import QtQuick 6.0
import QtQuick.Controls 6.0
import QtQuick.Layouts
import clap.lib

Expand Down
4 changes: 2 additions & 2 deletions plugins/gui/qml/clap/skins/track-info/main.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import QtQuick 2.1
import QtQuick.Controls 2.1
import QtQuick 6.0
import QtQuick.Controls 6.0

Rectangle {
width: 450
Expand Down
4 changes: 2 additions & 2 deletions plugins/gui/qml/clap/skins/transport-info/main.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import QtQuick 2.1
import QtQuick.Controls 2.1
import QtQuick 6.0
import QtQuick.Controls 6.0

Rectangle {
width: 450
Expand Down
35 changes: 35 additions & 0 deletions plugins/gui/qml/clap/skins/undo-test/main.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import QtQuick 6.7
import QtQuick.Controls 6.7
import QtQuick.Layouts

Rectangle {
width: 450
height: 300
color: "#f8f8f8"

ColumnLayout {
Text {
text: "# Undo Test"
}
RowLayout {
Button {
text: "Undo"
onClicked: undo.undo()
enabled: undo.canUndo
}
Text {
text: undo.undoName
}
}
RowLayout {
Button {
text: "Redo"
onClicked: undo.redo()
enabled: undo.canRedo
}
Text {
text: undo.redoName
}
}
}
}
51 changes: 8 additions & 43 deletions plugins/plugs/undo-test/undo-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace clap {
UndoTestModule(UndoTest &plugin) : Module(plugin, "", 0) {}

clap_process_status process(const Context &c, uint32_t numFrames) noexcept override {
return CLAP_PROCESS_CONTINUE;
return CLAP_PROCESS_SLEEP;
}
};

Expand All @@ -38,10 +38,6 @@ namespace clap {
return &desc;
}

enum {
kParamIdOffset = 0,
};

UndoTest::UndoTest(const std::string &pluginPath, const clap_host &host)
: CorePlugin(PathProvider::create(pluginPath, "undo-test"), descriptor(), host) {
_rootModule = std::make_unique<UndoTestModule>(*this);
Expand Down Expand Up @@ -87,36 +83,6 @@ namespace clap {
return false;
}

void UndoTest::undoSetCanUndo(bool can_undo) noexcept {
_canUndo = can_undo;
if (!can_undo)
_undoName.reset();
}

void UndoTest::undoSetCanRedo(bool can_redo) noexcept {
_canRedo = can_redo;
if (!can_redo)
_redoName.reset();
}

void UndoTest::undoSetUndoName(const char *name) noexcept {
if (name && *name) {
if (!_canUndo)
hostMisbehaving("set undo name while it isn't possible to undo");
_undoName = name;
} else
_undoName.reset();
}

void UndoTest::undoSetRedoName(const char *name) noexcept {
if (name && *name) {
if (!_canRedo)
hostMisbehaving("set undo name while it isn't possible to redo");
_redoName = name;
} else
_redoName.reset();
}

void UndoTest::incrementState() {
if (!_host.canUseUndo())
return;
Expand All @@ -126,14 +92,13 @@ namespace clap {
delta.new_value = ++_state;
_host.undoChangeMade("inc", &delta, sizeof(delta), true);
}
bool UndoTest::init() noexcept {
if (!super::init())
return false;

void UndoTest::requestHostUndo() {
if (_canUndo && _host.canUseUndo())
_host.undoUndo();
}

void UndoTest::requestHostRedo() {
if (_canRedo && _host.canUseUndo())
_host.undoRedo();
if (_host.canUseUndo()) {
_host.undoSetWantsContextUpdates(true);
}
return true;
}
} // namespace clap
Loading

0 comments on commit 7084f4a

Please sign in to comment.