Merge pull request #664 from jpcima/file-dialogs-ui

Prevent window input while file dialog is open
This commit is contained in:
JP Cimalando 2021-02-25 09:09:02 +01:00 committed by GitHub
commit 22e0aec6b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 111 additions and 4 deletions

View file

@ -34,6 +34,8 @@ add_library(sfizz_editor STATIC EXCLUDE_FROM_ALL
src/editor/EditorController.h
src/editor/GUIComponents.h
src/editor/GUIComponents.cpp
src/editor/GUIHelpers.h
src/editor/GUIHelpers.cpp
src/editor/GUIPiano.h
src/editor/GUIPiano.cpp
src/editor/ColorHelpers.h

View file

@ -8,6 +8,7 @@
#include "EditorController.h"
#include "EditIds.h"
#include "GUIComponents.h"
#include "GUIHelpers.h"
#include "GUIPiano.h"
#include "NativeHelpers.h"
#include "BitArray.h"
@ -40,6 +41,7 @@ const int Editor::viewHeight { 475 };
struct Editor::Impl : EditorController::Receiver, IControlListener {
EditorController* ctrl_ = nullptr;
CFrame* frame_ = nullptr;
SharedPointer<SFrameDisabler> frameDisabler_;
SharedPointer<CViewContainer> mainView_;
std::string currentSfzFile_;
@ -227,6 +229,8 @@ void Editor::open(CFrame& frame)
impl.frame_ = &frame;
frame.addView(impl.mainView_.get());
impl.frameDisabler_ = makeOwned<SFrameDisabler>(&frame);
impl.memQueryTimer_ = makeOwned<CVSTGUITimer>([this](CVSTGUITimer*) {
impl_->sendQueuedOSC("/mem/buffers", "", nullptr);
}, 1000, true);
@ -245,6 +249,8 @@ void Editor::close()
impl.memQueryTimer_ = nullptr;
impl.frameDisabler_ = nullptr;
if (impl.frame_) {
impl.frame_->removeView(impl.mainView_.get(), false);
impl.frame_ = nullptr;
@ -966,7 +972,11 @@ void Editor::Impl::chooseSfzFile()
if (!initialDir.empty())
fs->setInitialDirectory(initialDir.c_str());
if (fs->runModal()) {
frameDisabler_->disable();
bool runOk = fs->runModal();
frameDisabler_->enable();
if (runOk) {
UTF8StringPtr file = fs->getSelectedFile(0);
if (file)
changeSfzFile(file);
@ -996,7 +1006,11 @@ void Editor::Impl::createNewSfzFile()
if (!initialDir.empty())
fs->setInitialDirectory(initialDir.c_str());
if (fs->runModal()) {
frameDisabler_->disable();
bool runOk = fs->runModal();
frameDisabler_->enable();
if (runOk) {
UTF8StringPtr file = fs->getSelectedFile(0);
std::string fileStr;
if (file && !absl::EndsWithIgnoreCase(file, ".sfz")) {
@ -1084,7 +1098,11 @@ void Editor::Impl::chooseScalaFile()
if (!initialDir.empty())
fs->setInitialDirectory(initialDir.c_str());
if (fs->runModal()) {
frameDisabler_->disable();
bool runOk = fs->runModal();
frameDisabler_->enable();
if (runOk) {
UTF8StringPtr file = fs->getSelectedFile(0);
if (file)
changeScalaFile(file);
@ -1105,7 +1123,11 @@ void Editor::Impl::chooseUserFilesDir()
fs->setTitle("Set user files directory");
if (fs->runModal()) {
frameDisabler_->disable();
bool runOk = fs->runModal();
frameDisabler_->enable();
if (runOk) {
UTF8StringPtr dir = fs->getSelectedFile(0);
if (dir) {
userFilesDir_ = std::string(dir);

View file

@ -0,0 +1,53 @@
// SPDX-License-Identifier: BSD-2-Clause
// This code is part of the sfizz library and is licensed under a BSD 2-clause
// license. You should have receive a LICENSE.md file along with the code.
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#include "GUIHelpers.h"
class SFrameDisabler::KeyAndMouseHook : public CBaseObject,
public IKeyboardHook,
public IMouseObserver {
public:
void setEnabled(bool value) { enabled_ = value; }
protected:
int32_t onKeyDown(const VstKeyCode&, CFrame*) { return enabled_ ? -1 : 1; }
int32_t onKeyUp(const VstKeyCode&, CFrame*) { return enabled_ ? -1 : 1; }
void onMouseEntered(CView*, CFrame*) {}
void onMouseExited(CView*, CFrame*) {}
CMouseEventResult onMouseMoved(CFrame*, const CPoint&, const CButtonState&) { return enabled_ ? kMouseEventNotHandled : kMouseEventHandled; }
CMouseEventResult onMouseDown(CFrame*, const CPoint&, const CButtonState&) { return enabled_ ? kMouseEventNotHandled : kMouseEventHandled; }
private:
bool enabled_ = true;
};
SFrameDisabler::SFrameDisabler(CFrame* frame)
: frame_(frame), hook_(makeOwned<KeyAndMouseHook>())
{
frame->registerKeyboardHook(hook_);
frame->registerMouseObserver(hook_);
delayedEnabler_ = makeOwned<CVSTGUITimer>(
[this](CVSTGUITimer* t) { hook_->setEnabled(true); t->stop(); },
1, false);
}
SFrameDisabler::~SFrameDisabler()
{
frame_->unregisterKeyboardHook(hook_);
frame_->unregisterMouseObserver(hook_);
}
void SFrameDisabler::enable()
{
delayedEnabler_->start();
}
void SFrameDisabler::disable()
{
hook_->setEnabled(false);
delayedEnabler_->stop();
}

View file

@ -0,0 +1,30 @@
// SPDX-License-Identifier: BSD-2-Clause
// This code is part of the sfizz library and is licensed under a BSD 2-clause
// license. You should have receive a LICENSE.md file along with the code.
// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz
#pragma once
#include "utility/vstgui_before.h"
#include <vstgui/lib/cframe.h>
#include <vstgui/lib/cvstguitimer.h>
#include "utility/vstgui_after.h"
using namespace VSTGUI;
class SFrameDisabler : public CBaseObject {
public:
explicit SFrameDisabler(CFrame* frame);
~SFrameDisabler();
void enable();
void disable();
private:
class KeyAndMouseHook;
private:
CFrame* frame_ = nullptr;
SharedPointer<KeyAndMouseHook> hook_;
SharedPointer<CVSTGUITimer> delayedEnabler_;
};