First Commit
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
AnimatedAppComponent::AnimatedAppComponent()
|
||||
{
|
||||
setOpaque (true);
|
||||
}
|
||||
|
||||
void AnimatedAppComponent::setFramesPerSecond (int framesPerSecondIn)
|
||||
{
|
||||
jassert (0 < framesPerSecond && framesPerSecond < 1000);
|
||||
framesPerSecond = framesPerSecondIn;
|
||||
updateSync();
|
||||
}
|
||||
|
||||
void AnimatedAppComponent::updateSync()
|
||||
{
|
||||
if (useVBlank)
|
||||
{
|
||||
stopTimer();
|
||||
|
||||
if (vBlankAttachment.isEmpty())
|
||||
vBlankAttachment = { this, [this] { timerCallback(); } };
|
||||
}
|
||||
else
|
||||
{
|
||||
vBlankAttachment = {};
|
||||
|
||||
const auto interval = 1000 / framesPerSecond;
|
||||
|
||||
if (getTimerInterval() != interval)
|
||||
startTimer (interval);
|
||||
}
|
||||
}
|
||||
|
||||
void AnimatedAppComponent::setSynchroniseToVBlank (bool syncToVBlank)
|
||||
{
|
||||
useVBlank = syncToVBlank;
|
||||
updateSync();
|
||||
}
|
||||
|
||||
int AnimatedAppComponent::getMillisecondsSinceLastUpdate() const noexcept
|
||||
{
|
||||
return (int) (Time::getCurrentTime() - lastUpdateTime).inMilliseconds();
|
||||
}
|
||||
|
||||
void AnimatedAppComponent::timerCallback()
|
||||
{
|
||||
++totalUpdates;
|
||||
update();
|
||||
repaint();
|
||||
lastUpdateTime = Time::getCurrentTime();
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A base class for writing simple one-page graphical apps.
|
||||
|
||||
A subclass can inherit from this and implement just a few methods such as
|
||||
paint() and mouse-handling. The base class provides some simple abstractions
|
||||
to take care of continuously repainting itself.
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API AnimatedAppComponent : public Component,
|
||||
private Timer
|
||||
{
|
||||
public:
|
||||
AnimatedAppComponent();
|
||||
|
||||
/** Your subclass can call this to start a timer running which will
|
||||
call update() and repaint the component at the given frequency.
|
||||
*/
|
||||
void setFramesPerSecond (int framesPerSecond);
|
||||
|
||||
/** You can use this function to synchronise animation updates with the current display's vblank
|
||||
events. When this mode is enabled the value passed to setFramesPerSecond() is ignored.
|
||||
*/
|
||||
void setSynchroniseToVBlank (bool syncToVBlank);
|
||||
|
||||
/** Called periodically, at the frequency specified by setFramesPerSecond().
|
||||
This is a the best place to do things like advancing animation parameters,
|
||||
checking the mouse position, etc.
|
||||
*/
|
||||
virtual void update() = 0;
|
||||
|
||||
/** Returns the number of times that update() has been called since the component
|
||||
started running.
|
||||
*/
|
||||
int getFrameCounter() const noexcept { return totalUpdates; }
|
||||
|
||||
/** When called from update(), this returns the number of milliseconds since the
|
||||
last update call.
|
||||
This might be useful for accurately timing animations, etc.
|
||||
*/
|
||||
int getMillisecondsSinceLastUpdate() const noexcept;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
void updateSync();
|
||||
|
||||
Time lastUpdateTime = Time::getCurrentTime();
|
||||
int totalUpdates = 0;
|
||||
int framesPerSecond = 60;
|
||||
bool useVBlank = false;
|
||||
VBlankAttachment vBlankAttachment;
|
||||
|
||||
void timerCallback() override;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AnimatedAppComponent)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
#if JUCE_MAC || DOXYGEN
|
||||
/**
|
||||
Receives events from an Apple IR remote control device (Only available in OSX!).
|
||||
|
||||
To use it, just create a subclass of this class, implementing the buttonPressed()
|
||||
callback, then call start() and stop() to start or stop receiving events.
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API AppleRemoteDevice
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
AppleRemoteDevice();
|
||||
virtual ~AppleRemoteDevice();
|
||||
|
||||
//==============================================================================
|
||||
/** The set of buttons that may be pressed.
|
||||
@see buttonPressed
|
||||
*/
|
||||
enum ButtonType
|
||||
{
|
||||
menuButton = 0, /**< The menu button (if it's held for a short time). */
|
||||
playButton, /**< The play button. */
|
||||
plusButton, /**< The plus or volume-up button. */
|
||||
minusButton, /**< The minus or volume-down button. */
|
||||
rightButton, /**< The right button (if it's held for a short time). */
|
||||
leftButton, /**< The left button (if it's held for a short time). */
|
||||
rightButton_Long, /**< The right button (if it's held for a long time). */
|
||||
leftButton_Long, /**< The menu button (if it's held for a long time). */
|
||||
menuButton_Long, /**< The menu button (if it's held for a long time). */
|
||||
playButtonSleepMode,
|
||||
switched
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/** Override this method to receive the callback about a button press.
|
||||
|
||||
The callback will happen on the application's message thread.
|
||||
|
||||
Some buttons trigger matching up and down events, in which the isDown parameter
|
||||
will be true and then false. Others only send a single event when the
|
||||
button is pressed.
|
||||
*/
|
||||
virtual void buttonPressed (ButtonType buttonId, bool isDown) = 0;
|
||||
|
||||
//==============================================================================
|
||||
/** Starts the device running and responding to events.
|
||||
|
||||
Returns true if it managed to open the device.
|
||||
|
||||
@param inExclusiveMode if true, the remote will be grabbed exclusively for this app,
|
||||
and will not be available to any other part of the system. If
|
||||
false, it will be shared with other apps.
|
||||
@see stop
|
||||
*/
|
||||
bool start (bool inExclusiveMode);
|
||||
|
||||
/** Stops the device running.
|
||||
@see start
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/** Returns true if the device has been started successfully.
|
||||
*/
|
||||
bool isActive() const;
|
||||
|
||||
/** Returns the ID number of the remote, if it has sent one.
|
||||
*/
|
||||
int getRemoteId() const { return remoteId; }
|
||||
|
||||
//==============================================================================
|
||||
/** @internal */
|
||||
void handleCallbackInternal();
|
||||
|
||||
private:
|
||||
void* device;
|
||||
void* queue;
|
||||
int remoteId;
|
||||
|
||||
bool open (bool openInExclusiveMode);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AppleRemoteDevice)
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
BubbleMessageComponent::BubbleMessageComponent (int fadeOutLengthMs)
|
||||
: fadeOutLength (fadeOutLengthMs), mouseClickCounter (0),
|
||||
expiryTime (0), deleteAfterUse (false)
|
||||
{
|
||||
}
|
||||
|
||||
BubbleMessageComponent::~BubbleMessageComponent()
|
||||
{
|
||||
}
|
||||
|
||||
void BubbleMessageComponent::showAt (const Rectangle<int>& pos,
|
||||
const AttributedString& text,
|
||||
const int numMillisecondsBeforeRemoving,
|
||||
const bool removeWhenMouseClicked,
|
||||
const bool deleteSelfAfterUse)
|
||||
{
|
||||
createLayout (text);
|
||||
setPosition (pos);
|
||||
init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
|
||||
}
|
||||
|
||||
void BubbleMessageComponent::showAt (Component* const component,
|
||||
const AttributedString& text,
|
||||
const int numMillisecondsBeforeRemoving,
|
||||
const bool removeWhenMouseClicked,
|
||||
const bool deleteSelfAfterUse)
|
||||
{
|
||||
createLayout (text);
|
||||
setPosition (component);
|
||||
init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
|
||||
}
|
||||
|
||||
void BubbleMessageComponent::createLayout (const AttributedString& text)
|
||||
{
|
||||
textLayout.createLayoutWithBalancedLineLengths (text, 256);
|
||||
}
|
||||
|
||||
void BubbleMessageComponent::init (const int numMillisecondsBeforeRemoving,
|
||||
const bool removeWhenMouseClicked,
|
||||
const bool deleteSelfAfterUse)
|
||||
{
|
||||
setAlpha (1.0f);
|
||||
setVisible (true);
|
||||
deleteAfterUse = deleteSelfAfterUse;
|
||||
|
||||
expiryTime = numMillisecondsBeforeRemoving > 0
|
||||
? (Time::getMillisecondCounter() + (uint32) numMillisecondsBeforeRemoving) : 0;
|
||||
|
||||
mouseClickCounter = Desktop::getInstance().getMouseButtonClickCounter();
|
||||
|
||||
if (! (removeWhenMouseClicked && isShowing()))
|
||||
mouseClickCounter += 0xfffff;
|
||||
|
||||
startTimer (77);
|
||||
repaint();
|
||||
}
|
||||
|
||||
const float bubblePaddingX = 20.0f;
|
||||
const float bubblePaddingY = 14.0f;
|
||||
|
||||
void BubbleMessageComponent::getContentSize (int& w, int& h)
|
||||
{
|
||||
w = (int) (bubblePaddingX + textLayout.getWidth());
|
||||
h = (int) (bubblePaddingY + textLayout.getHeight());
|
||||
}
|
||||
|
||||
void BubbleMessageComponent::paintContent (Graphics& g, int w, int h)
|
||||
{
|
||||
g.setColour (findColour (TooltipWindow::textColourId));
|
||||
|
||||
textLayout.draw (g, Rectangle<float> (bubblePaddingX / 2.0f, bubblePaddingY / 2.0f,
|
||||
(float) w - bubblePaddingX, (float) h - bubblePaddingY));
|
||||
}
|
||||
|
||||
void BubbleMessageComponent::timerCallback()
|
||||
{
|
||||
if (Desktop::getInstance().getMouseButtonClickCounter() > mouseClickCounter)
|
||||
hide (false);
|
||||
else if (expiryTime != 0 && Time::getMillisecondCounter() > expiryTime)
|
||||
hide (true);
|
||||
}
|
||||
|
||||
void BubbleMessageComponent::hide (const bool fadeOut)
|
||||
{
|
||||
stopTimer();
|
||||
|
||||
if (fadeOut)
|
||||
Desktop::getInstance().getAnimator().fadeOut (this, fadeOutLength);
|
||||
else
|
||||
setVisible (false);
|
||||
|
||||
if (deleteAfterUse)
|
||||
delete this;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A speech-bubble component that displays a short message.
|
||||
|
||||
This can be used to show a message with the tail of the speech bubble
|
||||
pointing to a particular component or location on the screen.
|
||||
|
||||
@see BubbleComponent
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API BubbleMessageComponent : public BubbleComponent,
|
||||
private Timer
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a bubble component.
|
||||
|
||||
After creating one a BubbleComponent, do the following:
|
||||
- add it to an appropriate parent component, or put it on the
|
||||
desktop with Component::addToDesktop (0).
|
||||
- use the showAt() method to show a message.
|
||||
- it will make itself invisible after it times-out (and can optionally
|
||||
also delete itself), or you can reuse it somewhere else by calling
|
||||
showAt() again.
|
||||
*/
|
||||
BubbleMessageComponent (int fadeOutLengthMs = 150);
|
||||
|
||||
/** Destructor. */
|
||||
~BubbleMessageComponent() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Shows a message bubble at a particular position.
|
||||
|
||||
This shows the bubble with its stem pointing to the given location
|
||||
(coordinates being relative to its parent component).
|
||||
|
||||
@param position the coords of the object to point to
|
||||
@param message the text to display
|
||||
@param numMillisecondsBeforeRemoving how long to leave it on the screen before removing itself
|
||||
from its parent component. If this is 0 or less, it
|
||||
will stay there until manually removed.
|
||||
@param removeWhenMouseClicked if this is true, the bubble will disappear as soon as a
|
||||
mouse button is pressed (anywhere on the screen)
|
||||
@param deleteSelfAfterUse if true, then the component will delete itself after
|
||||
it becomes invisible
|
||||
*/
|
||||
void showAt (const Rectangle<int>& position,
|
||||
const AttributedString& message,
|
||||
int numMillisecondsBeforeRemoving,
|
||||
bool removeWhenMouseClicked = true,
|
||||
bool deleteSelfAfterUse = false);
|
||||
|
||||
/** Shows a message bubble next to a particular component.
|
||||
|
||||
This shows the bubble with its stem pointing at the given component.
|
||||
|
||||
@param component the component that you want to point at
|
||||
@param message the text to display
|
||||
@param numMillisecondsBeforeRemoving how long to leave it on the screen before removing itself
|
||||
from its parent component. If this is 0 or less, it
|
||||
will stay there until manually removed.
|
||||
@param removeWhenMouseClicked if this is true, the bubble will disappear as soon as a
|
||||
mouse button is pressed (anywhere on the screen)
|
||||
@param deleteSelfAfterUse if true, then the component will delete itself after
|
||||
it becomes invisible
|
||||
*/
|
||||
void showAt (Component* component,
|
||||
const AttributedString& message,
|
||||
int numMillisecondsBeforeRemoving,
|
||||
bool removeWhenMouseClicked = true,
|
||||
bool deleteSelfAfterUse = false);
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/** @internal */
|
||||
void getContentSize (int& w, int& h) override;
|
||||
/** @internal */
|
||||
void paintContent (Graphics& g, int w, int h) override;
|
||||
/** @internal */
|
||||
void timerCallback() override;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
int fadeOutLength, mouseClickCounter;
|
||||
TextLayout textLayout;
|
||||
int64 expiryTime;
|
||||
bool deleteAfterUse;
|
||||
|
||||
void createLayout (const AttributedString&);
|
||||
void init (int, bool, bool);
|
||||
void hide (bool fadeOut);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BubbleMessageComponent)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,656 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
struct ColourComponentSlider final : public Slider
|
||||
{
|
||||
ColourComponentSlider (const String& name) : Slider (name)
|
||||
{
|
||||
setRange (0.0, 255.0, 1.0);
|
||||
}
|
||||
|
||||
String getTextFromValue (double value) override
|
||||
{
|
||||
return String::toHexString ((int) value).toUpperCase().paddedLeft ('0', 2);
|
||||
}
|
||||
|
||||
double getValueFromText (const String& text) override
|
||||
{
|
||||
return (double) text.getHexValue32();
|
||||
}
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class ColourSelector::ColourSpaceView final : public Component
|
||||
{
|
||||
public:
|
||||
ColourSpaceView (ColourSelector& cs, float& hue, float& sat, float& val, int edgeSize)
|
||||
: owner (cs), h (hue), s (sat), v (val), edge (edgeSize)
|
||||
{
|
||||
addAndMakeVisible (marker);
|
||||
setMouseCursor (MouseCursor::CrosshairCursor);
|
||||
}
|
||||
|
||||
void paint (Graphics& g) override
|
||||
{
|
||||
if (colours.isNull())
|
||||
{
|
||||
auto width = getWidth() / 2;
|
||||
auto height = getHeight() / 2;
|
||||
colours = Image (Image::RGB, width, height, false);
|
||||
|
||||
Image::BitmapData pixels (colours, Image::BitmapData::writeOnly);
|
||||
|
||||
for (int y = 0; y < height; ++y)
|
||||
{
|
||||
auto val = 1.0f - (float) y / (float) height;
|
||||
|
||||
for (int x = 0; x < width; ++x)
|
||||
{
|
||||
auto sat = (float) x / (float) width;
|
||||
pixels.setPixelColour (x, y, Colour (h, sat, val, 1.0f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
g.setOpacity (1.0f);
|
||||
g.drawImageTransformed (colours,
|
||||
RectanglePlacement (RectanglePlacement::stretchToFit)
|
||||
.getTransformToFit (colours.getBounds().toFloat(),
|
||||
getLocalBounds().reduced (edge).toFloat()),
|
||||
false);
|
||||
}
|
||||
|
||||
void mouseDown (const MouseEvent& e) override
|
||||
{
|
||||
mouseDrag (e);
|
||||
}
|
||||
|
||||
void mouseDrag (const MouseEvent& e) override
|
||||
{
|
||||
auto sat = (float) (e.x - edge) / (float) (getWidth() - edge * 2);
|
||||
auto val = 1.0f - (float) (e.y - edge) / (float) (getHeight() - edge * 2);
|
||||
|
||||
owner.setSV (sat, val);
|
||||
}
|
||||
|
||||
void updateIfNeeded()
|
||||
{
|
||||
if (! approximatelyEqual (lastHue, h))
|
||||
{
|
||||
lastHue = h;
|
||||
colours = {};
|
||||
repaint();
|
||||
}
|
||||
|
||||
updateMarker();
|
||||
}
|
||||
|
||||
void resized() override
|
||||
{
|
||||
colours = {};
|
||||
updateMarker();
|
||||
}
|
||||
|
||||
private:
|
||||
ColourSelector& owner;
|
||||
float& h;
|
||||
float& s;
|
||||
float& v;
|
||||
float lastHue = 0;
|
||||
const int edge;
|
||||
Image colours;
|
||||
|
||||
struct ColourSpaceMarker final : public Component
|
||||
{
|
||||
ColourSpaceMarker()
|
||||
{
|
||||
setInterceptsMouseClicks (false, false);
|
||||
}
|
||||
|
||||
void paint (Graphics& g) override
|
||||
{
|
||||
g.setColour (Colour::greyLevel (0.1f));
|
||||
g.drawEllipse (1.0f, 1.0f, (float) getWidth() - 2.0f, (float) getHeight() - 2.0f, 1.0f);
|
||||
g.setColour (Colour::greyLevel (0.9f));
|
||||
g.drawEllipse (2.0f, 2.0f, (float) getWidth() - 4.0f, (float) getHeight() - 4.0f, 1.0f);
|
||||
}
|
||||
};
|
||||
|
||||
ColourSpaceMarker marker;
|
||||
|
||||
void updateMarker()
|
||||
{
|
||||
auto markerSize = jmax (14, edge * 2);
|
||||
auto area = getLocalBounds().reduced (edge);
|
||||
|
||||
marker.setBounds (Rectangle<int> (markerSize, markerSize)
|
||||
.withCentre (area.getRelativePoint (s, 1.0f - v)));
|
||||
}
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (ColourSpaceView)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class ColourSelector::HueSelectorComp final : public Component
|
||||
{
|
||||
public:
|
||||
HueSelectorComp (ColourSelector& cs, float& hue, int edgeSize)
|
||||
: owner (cs), h (hue), edge (edgeSize)
|
||||
{
|
||||
addAndMakeVisible (marker);
|
||||
}
|
||||
|
||||
void paint (Graphics& g) override
|
||||
{
|
||||
ColourGradient cg;
|
||||
cg.isRadial = false;
|
||||
cg.point1.setXY (0.0f, (float) edge);
|
||||
cg.point2.setXY (0.0f, (float) getHeight());
|
||||
|
||||
for (float i = 0.0f; i <= 1.0f; i += 0.02f)
|
||||
cg.addColour (i, Colour (i, 1.0f, 1.0f, 1.0f));
|
||||
|
||||
g.setGradientFill (cg);
|
||||
g.fillRect (getLocalBounds().reduced (edge));
|
||||
}
|
||||
|
||||
void resized() override
|
||||
{
|
||||
auto markerSize = jmax (14, edge * 2);
|
||||
auto area = getLocalBounds().reduced (edge);
|
||||
|
||||
marker.setBounds (Rectangle<int> (getWidth(), markerSize)
|
||||
.withCentre (area.getRelativePoint (0.5f, h)));
|
||||
}
|
||||
|
||||
void mouseDown (const MouseEvent& e) override
|
||||
{
|
||||
mouseDrag (e);
|
||||
}
|
||||
|
||||
void mouseDrag (const MouseEvent& e) override
|
||||
{
|
||||
owner.setHue ((float) (e.y - edge) / (float) (getHeight() - edge * 2));
|
||||
}
|
||||
|
||||
void updateIfNeeded()
|
||||
{
|
||||
resized();
|
||||
}
|
||||
|
||||
private:
|
||||
ColourSelector& owner;
|
||||
float& h;
|
||||
const int edge;
|
||||
|
||||
struct HueSelectorMarker final : public Component
|
||||
{
|
||||
HueSelectorMarker()
|
||||
{
|
||||
setInterceptsMouseClicks (false, false);
|
||||
}
|
||||
|
||||
void paint (Graphics& g) override
|
||||
{
|
||||
auto cw = (float) getWidth();
|
||||
auto ch = (float) getHeight();
|
||||
|
||||
Path p;
|
||||
p.addTriangle (1.0f, 1.0f,
|
||||
cw * 0.3f, ch * 0.5f,
|
||||
1.0f, ch - 1.0f);
|
||||
|
||||
p.addTriangle (cw - 1.0f, 1.0f,
|
||||
cw * 0.7f, ch * 0.5f,
|
||||
cw - 1.0f, ch - 1.0f);
|
||||
|
||||
g.setColour (Colours::white.withAlpha (0.75f));
|
||||
g.fillPath (p);
|
||||
|
||||
g.setColour (Colours::black.withAlpha (0.75f));
|
||||
g.strokePath (p, PathStrokeType (1.2f));
|
||||
}
|
||||
};
|
||||
|
||||
HueSelectorMarker marker;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (HueSelectorComp)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class ColourSelector::SwatchComponent final : public Component
|
||||
{
|
||||
public:
|
||||
SwatchComponent (ColourSelector& cs, int itemIndex)
|
||||
: owner (cs), index (itemIndex)
|
||||
{
|
||||
}
|
||||
|
||||
void paint (Graphics& g) override
|
||||
{
|
||||
auto col = owner.getSwatchColour (index);
|
||||
|
||||
g.fillCheckerBoard (getLocalBounds().toFloat(), 6.0f, 6.0f,
|
||||
Colour (0xffdddddd).overlaidWith (col),
|
||||
Colour (0xffffffff).overlaidWith (col));
|
||||
}
|
||||
|
||||
void mouseDown (const MouseEvent&) override
|
||||
{
|
||||
PopupMenu m;
|
||||
m.addItem (1, TRANS ("Use this swatch as the current colour"));
|
||||
m.addSeparator();
|
||||
m.addItem (2, TRANS ("Set this swatch to the current colour"));
|
||||
|
||||
m.showMenuAsync (PopupMenu::Options().withTargetComponent (this),
|
||||
ModalCallbackFunction::forComponent (menuStaticCallback, this));
|
||||
}
|
||||
|
||||
private:
|
||||
ColourSelector& owner;
|
||||
const int index;
|
||||
|
||||
static void menuStaticCallback (int result, SwatchComponent* comp)
|
||||
{
|
||||
if (comp != nullptr)
|
||||
{
|
||||
if (result == 1) comp->setColourFromSwatch();
|
||||
if (result == 2) comp->setSwatchFromColour();
|
||||
}
|
||||
}
|
||||
|
||||
void setColourFromSwatch()
|
||||
{
|
||||
owner.setCurrentColour (owner.getSwatchColour (index));
|
||||
}
|
||||
|
||||
void setSwatchFromColour()
|
||||
{
|
||||
if (owner.getSwatchColour (index) != owner.getCurrentColour())
|
||||
{
|
||||
owner.setSwatchColour (index, owner.getCurrentColour());
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (SwatchComponent)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class ColourSelector::ColourPreviewComp final : public Component
|
||||
{
|
||||
public:
|
||||
ColourPreviewComp (ColourSelector& cs, bool isEditable)
|
||||
: owner (cs)
|
||||
{
|
||||
colourLabel.setFont (labelFont);
|
||||
colourLabel.setJustificationType (Justification::centred);
|
||||
|
||||
if (isEditable)
|
||||
{
|
||||
colourLabel.setEditable (true);
|
||||
|
||||
colourLabel.onEditorShow = [this]
|
||||
{
|
||||
if (auto* ed = colourLabel.getCurrentTextEditor())
|
||||
ed->setInputRestrictions ((owner.flags & showAlphaChannel) ? 8 : 6, "1234567890ABCDEFabcdef");
|
||||
};
|
||||
|
||||
colourLabel.onEditorHide = [this]
|
||||
{
|
||||
updateColourIfNecessary (colourLabel.getText());
|
||||
};
|
||||
}
|
||||
|
||||
addAndMakeVisible (colourLabel);
|
||||
}
|
||||
|
||||
void updateIfNeeded()
|
||||
{
|
||||
auto newColour = owner.getCurrentColour();
|
||||
|
||||
if (currentColour != newColour)
|
||||
{
|
||||
currentColour = newColour;
|
||||
auto textColour = (Colours::white.overlaidWith (currentColour).contrasting());
|
||||
|
||||
colourLabel.setColour (Label::textColourId, textColour);
|
||||
colourLabel.setColour (Label::textWhenEditingColourId, textColour);
|
||||
colourLabel.setText (currentColour.toDisplayString ((owner.flags & showAlphaChannel) != 0), dontSendNotification);
|
||||
|
||||
labelWidth = GlyphArrangement::getStringWidthInt (labelFont, colourLabel.getText());
|
||||
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
void paint (Graphics& g) override
|
||||
{
|
||||
g.fillCheckerBoard (getLocalBounds().toFloat(), 10.0f, 10.0f,
|
||||
Colour (0xffdddddd).overlaidWith (currentColour),
|
||||
Colour (0xffffffff).overlaidWith (currentColour));
|
||||
}
|
||||
|
||||
void resized() override
|
||||
{
|
||||
colourLabel.centreWithSize (labelWidth + 10, (int) labelFont.getHeight() + 10);
|
||||
}
|
||||
|
||||
private:
|
||||
void updateColourIfNecessary (const String& newColourString)
|
||||
{
|
||||
auto newColour = Colour::fromString (newColourString);
|
||||
|
||||
if (newColour != currentColour)
|
||||
owner.setCurrentColour (newColour);
|
||||
}
|
||||
|
||||
ColourSelector& owner;
|
||||
|
||||
Colour currentColour;
|
||||
Font labelFont { withDefaultMetrics (FontOptions { 14.0f, Font::bold }) };
|
||||
int labelWidth = 0;
|
||||
Label colourLabel;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourPreviewComp)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
ColourSelector::ColourSelector (int sectionsToShow, int edge, int gapAroundColourSpaceComponent)
|
||||
: colour (Colours::white),
|
||||
flags (sectionsToShow),
|
||||
edgeGap (edge)
|
||||
{
|
||||
// not much point having a selector with no components in it!
|
||||
jassert ((flags & (showColourAtTop | showSliders | showColourspace)) != 0);
|
||||
|
||||
updateHSV();
|
||||
|
||||
if ((flags & showColourAtTop) != 0)
|
||||
{
|
||||
previewComponent.reset (new ColourPreviewComp (*this, (flags & editableColour) != 0));
|
||||
addAndMakeVisible (previewComponent.get());
|
||||
}
|
||||
|
||||
if ((flags & showSliders) != 0)
|
||||
{
|
||||
sliders[0].reset (new ColourComponentSlider (TRANS ("red")));
|
||||
sliders[1].reset (new ColourComponentSlider (TRANS ("green")));
|
||||
sliders[2].reset (new ColourComponentSlider (TRANS ("blue")));
|
||||
sliders[3].reset (new ColourComponentSlider (TRANS ("alpha")));
|
||||
|
||||
addAndMakeVisible (sliders[0].get());
|
||||
addAndMakeVisible (sliders[1].get());
|
||||
addAndMakeVisible (sliders[2].get());
|
||||
addChildComponent (sliders[3].get());
|
||||
|
||||
sliders[3]->setVisible ((flags & showAlphaChannel) != 0);
|
||||
|
||||
for (auto& slider : sliders)
|
||||
slider->onValueChange = [this] { changeColour(); };
|
||||
}
|
||||
|
||||
if ((flags & showColourspace) != 0)
|
||||
{
|
||||
colourSpace.reset (new ColourSpaceView (*this, h, s, v, gapAroundColourSpaceComponent));
|
||||
hueSelector.reset (new HueSelectorComp (*this, h, gapAroundColourSpaceComponent));
|
||||
|
||||
addAndMakeVisible (colourSpace.get());
|
||||
addAndMakeVisible (hueSelector.get());
|
||||
}
|
||||
|
||||
update (dontSendNotification);
|
||||
}
|
||||
|
||||
ColourSelector::~ColourSelector()
|
||||
{
|
||||
dispatchPendingMessages();
|
||||
swatchComponents.clear();
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
Colour ColourSelector::getCurrentColour() const
|
||||
{
|
||||
return ((flags & showAlphaChannel) != 0) ? colour : colour.withAlpha ((uint8) 0xff);
|
||||
}
|
||||
|
||||
void ColourSelector::setCurrentColour (Colour c, NotificationType notification)
|
||||
{
|
||||
if (c != colour)
|
||||
{
|
||||
colour = ((flags & showAlphaChannel) != 0) ? c : c.withAlpha ((uint8) 0xff);
|
||||
|
||||
updateHSV();
|
||||
update (notification);
|
||||
}
|
||||
}
|
||||
|
||||
void ColourSelector::setHue (float newH)
|
||||
{
|
||||
newH = jlimit (0.0f, 1.0f, newH);
|
||||
|
||||
if (! approximatelyEqual (h, newH))
|
||||
{
|
||||
h = newH;
|
||||
colour = Colour (h, s, v, colour.getFloatAlpha());
|
||||
update (sendNotification);
|
||||
}
|
||||
}
|
||||
|
||||
void ColourSelector::setSV (float newS, float newV)
|
||||
{
|
||||
newS = jlimit (0.0f, 1.0f, newS);
|
||||
newV = jlimit (0.0f, 1.0f, newV);
|
||||
|
||||
if (! approximatelyEqual (s, newS) || ! approximatelyEqual (v, newV))
|
||||
{
|
||||
s = newS;
|
||||
v = newV;
|
||||
colour = Colour (h, s, v, colour.getFloatAlpha());
|
||||
update (sendNotification);
|
||||
}
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void ColourSelector::updateHSV()
|
||||
{
|
||||
colour.getHSB (h, s, v);
|
||||
}
|
||||
|
||||
void ColourSelector::update (NotificationType notification)
|
||||
{
|
||||
if (sliders[0] != nullptr)
|
||||
{
|
||||
sliders[0]->setValue ((int) colour.getRed(), notification);
|
||||
sliders[1]->setValue ((int) colour.getGreen(), notification);
|
||||
sliders[2]->setValue ((int) colour.getBlue(), notification);
|
||||
sliders[3]->setValue ((int) colour.getAlpha(), notification);
|
||||
}
|
||||
|
||||
if (colourSpace != nullptr)
|
||||
{
|
||||
colourSpace->updateIfNeeded();
|
||||
hueSelector->updateIfNeeded();
|
||||
}
|
||||
|
||||
if (previewComponent != nullptr)
|
||||
previewComponent->updateIfNeeded();
|
||||
|
||||
if (notification != dontSendNotification)
|
||||
sendChangeMessage();
|
||||
|
||||
if (notification == sendNotificationSync)
|
||||
dispatchPendingMessages();
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void ColourSelector::paint (Graphics& g)
|
||||
{
|
||||
g.fillAll (findColour (backgroundColourId));
|
||||
|
||||
if ((flags & showSliders) != 0)
|
||||
{
|
||||
g.setColour (findColour (labelTextColourId));
|
||||
g.setFont (11.0f);
|
||||
|
||||
for (auto& slider : sliders)
|
||||
{
|
||||
if (slider->isVisible())
|
||||
g.drawText (slider->getName() + ":",
|
||||
0, slider->getY(),
|
||||
slider->getX() - 8, slider->getHeight(),
|
||||
Justification::centredRight, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ColourSelector::resized()
|
||||
{
|
||||
const int swatchesPerRow = 8;
|
||||
const int swatchHeight = 22;
|
||||
|
||||
const int numSliders = ((flags & showAlphaChannel) != 0) ? 4 : 3;
|
||||
const int numSwatches = getNumSwatches();
|
||||
|
||||
const int swatchSpace = numSwatches > 0 ? edgeGap + swatchHeight * ((numSwatches + 7) / swatchesPerRow) : 0;
|
||||
const int sliderSpace = ((flags & showSliders) != 0) ? jmin (22 * numSliders + edgeGap, proportionOfHeight (0.3f)) : 0;
|
||||
const int topSpace = ((flags & showColourAtTop) != 0) ? jmin (30 + edgeGap * 2, proportionOfHeight (0.2f)) : edgeGap;
|
||||
|
||||
if (previewComponent != nullptr)
|
||||
previewComponent->setBounds (edgeGap, edgeGap, getWidth() - edgeGap * 2, topSpace - edgeGap * 2);
|
||||
|
||||
int y = topSpace;
|
||||
|
||||
if ((flags & showColourspace) != 0)
|
||||
{
|
||||
const int hueWidth = jmin (50, proportionOfWidth (0.15f));
|
||||
|
||||
colourSpace->setBounds (edgeGap, y,
|
||||
getWidth() - hueWidth - edgeGap - 4,
|
||||
getHeight() - topSpace - sliderSpace - swatchSpace - edgeGap);
|
||||
|
||||
hueSelector->setBounds (colourSpace->getRight() + 4, y,
|
||||
getWidth() - edgeGap - (colourSpace->getRight() + 4),
|
||||
colourSpace->getHeight());
|
||||
|
||||
y = getHeight() - sliderSpace - swatchSpace - edgeGap;
|
||||
}
|
||||
|
||||
if ((flags & showSliders) != 0)
|
||||
{
|
||||
auto sliderHeight = jmax (4, sliderSpace / numSliders);
|
||||
|
||||
for (int i = 0; i < numSliders; ++i)
|
||||
{
|
||||
sliders[i]->setBounds (proportionOfWidth (0.2f), y,
|
||||
proportionOfWidth (0.72f), sliderHeight - 2);
|
||||
|
||||
y += sliderHeight;
|
||||
}
|
||||
}
|
||||
|
||||
if (numSwatches > 0)
|
||||
{
|
||||
const int startX = 8;
|
||||
const int xGap = 4;
|
||||
const int yGap = 4;
|
||||
const int swatchWidth = (getWidth() - startX * 2) / swatchesPerRow;
|
||||
y += edgeGap;
|
||||
|
||||
if (swatchComponents.size() != numSwatches)
|
||||
{
|
||||
swatchComponents.clear();
|
||||
|
||||
for (int i = 0; i < numSwatches; ++i)
|
||||
{
|
||||
auto* sc = new SwatchComponent (*this, i);
|
||||
swatchComponents.add (sc);
|
||||
addAndMakeVisible (sc);
|
||||
}
|
||||
}
|
||||
|
||||
int x = startX;
|
||||
|
||||
for (int i = 0; i < swatchComponents.size(); ++i)
|
||||
{
|
||||
auto* sc = swatchComponents.getUnchecked (i);
|
||||
|
||||
sc->setBounds (x + xGap / 2,
|
||||
y + yGap / 2,
|
||||
swatchWidth - xGap,
|
||||
swatchHeight - yGap);
|
||||
|
||||
if (((i + 1) % swatchesPerRow) == 0)
|
||||
{
|
||||
x = startX;
|
||||
y += swatchHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
x += swatchWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ColourSelector::changeColour()
|
||||
{
|
||||
if (sliders[0] != nullptr)
|
||||
setCurrentColour (Colour ((uint8) sliders[0]->getValue(),
|
||||
(uint8) sliders[1]->getValue(),
|
||||
(uint8) sliders[2]->getValue(),
|
||||
(uint8) sliders[3]->getValue()));
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
int ColourSelector::getNumSwatches() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
Colour ColourSelector::getSwatchColour (int) const
|
||||
{
|
||||
jassertfalse; // if you've overridden getNumSwatches(), you also need to implement this method
|
||||
return Colours::black;
|
||||
}
|
||||
|
||||
void ColourSelector::setSwatchColour (int, const Colour&)
|
||||
{
|
||||
jassertfalse; // if you've overridden getNumSwatches(), you also need to implement this method
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A component that lets the user choose a colour.
|
||||
|
||||
This shows RGB sliders and a colourspace that the user can pick colours from.
|
||||
|
||||
This class is also a ChangeBroadcaster, so listeners can register to be told
|
||||
when the colour changes.
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API ColourSelector : public Component,
|
||||
public ChangeBroadcaster
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Options for the type of selector to show. These are passed into the constructor. */
|
||||
enum ColourSelectorOptions
|
||||
{
|
||||
showAlphaChannel = 1 << 0, /**< if set, the colour's alpha channel can be changed as well as its RGB. */
|
||||
|
||||
showColourAtTop = 1 << 1, /**< if set, a swatch of the colour is shown at the top of the component. */
|
||||
editableColour = 1 << 2, /**< if set, the colour shows at the top of the component is editable. */
|
||||
showSliders = 1 << 3, /**< if set, RGB sliders are shown at the bottom of the component. */
|
||||
showColourspace = 1 << 4 /**< if set, a big HSV selector is shown. */
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/** Creates a ColourSelector object.
|
||||
|
||||
The flags are a combination of values from the ColourSelectorOptions enum, specifying
|
||||
which of the selector's features should be visible.
|
||||
|
||||
The edgeGap value specifies the amount of space to leave around the edge.
|
||||
|
||||
gapAroundColourSpaceComponent indicates how much of a gap to put around the
|
||||
colourspace and hue selector components.
|
||||
*/
|
||||
ColourSelector (int flags = (showAlphaChannel | showColourAtTop | showSliders | showColourspace),
|
||||
int edgeGap = 4,
|
||||
int gapAroundColourSpaceComponent = 7);
|
||||
|
||||
/** Destructor. */
|
||||
~ColourSelector() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Returns the colour that the user has currently selected.
|
||||
|
||||
The ColourSelector class is also a ChangeBroadcaster, so listeners can
|
||||
register to be told when the colour changes.
|
||||
|
||||
@see setCurrentColour
|
||||
*/
|
||||
Colour getCurrentColour() const;
|
||||
|
||||
/** Changes the colour that is currently being shown.
|
||||
|
||||
@param newColour the new colour to show
|
||||
@param notificationType whether to send a notification of the change to listeners.
|
||||
A notification will only be sent if the colour has changed.
|
||||
*/
|
||||
void setCurrentColour (Colour newColour, NotificationType notificationType = sendNotification);
|
||||
|
||||
//==============================================================================
|
||||
/** Tells the selector how many preset colour swatches you want to have on the component.
|
||||
|
||||
To enable swatches, you'll need to override getNumSwatches(), getSwatchColour(), and
|
||||
setSwatchColour(), to return the number of colours you want, and to set and retrieve
|
||||
their values.
|
||||
*/
|
||||
virtual int getNumSwatches() const;
|
||||
|
||||
/** Called by the selector to find out the colour of one of the swatches.
|
||||
|
||||
Your subclass should return the colour of the swatch with the given index.
|
||||
|
||||
To enable swatches, you'll need to override getNumSwatches(), getSwatchColour(), and
|
||||
setSwatchColour(), to return the number of colours you want, and to set and retrieve
|
||||
their values.
|
||||
*/
|
||||
virtual Colour getSwatchColour (int index) const;
|
||||
|
||||
/** Called by the selector when the user puts a new colour into one of the swatches.
|
||||
|
||||
Your subclass should change the colour of the swatch with the given index.
|
||||
|
||||
To enable swatches, you'll need to override getNumSwatches(), getSwatchColour(), and
|
||||
setSwatchColour(), to return the number of colours you want, and to set and retrieve
|
||||
their values.
|
||||
*/
|
||||
virtual void setSwatchColour (int index, const Colour& newColour);
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/** A set of colour IDs to use to change the colour of various aspects of the keyboard.
|
||||
|
||||
These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
|
||||
methods.
|
||||
|
||||
@see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
|
||||
*/
|
||||
enum ColourIds
|
||||
{
|
||||
backgroundColourId = 0x1007000, /**< the colour used to fill the component's background. */
|
||||
labelTextColourId = 0x1007001 /**< the colour used for the labels next to the sliders. */
|
||||
};
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
class SwatchComponent;
|
||||
class ColourSpaceView;
|
||||
class HueSelectorComp;
|
||||
class ColourPreviewComp;
|
||||
|
||||
Colour colour;
|
||||
float h, s, v;
|
||||
std::unique_ptr<Slider> sliders[4];
|
||||
std::unique_ptr<ColourSpaceView> colourSpace;
|
||||
std::unique_ptr<HueSelectorComp> hueSelector;
|
||||
std::unique_ptr<ColourPreviewComp> previewComponent;
|
||||
OwnedArray<SwatchComponent> swatchComponents;
|
||||
const int flags;
|
||||
int edgeGap;
|
||||
|
||||
void setHue (float newH);
|
||||
void setSV (float newS, float newV);
|
||||
void updateHSV();
|
||||
void update (NotificationType);
|
||||
void changeColour();
|
||||
void paint (Graphics&) override;
|
||||
void resized() override;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourSelector)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,491 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class KeyMappingEditorComponent::ChangeKeyButton final : public Button
|
||||
{
|
||||
public:
|
||||
ChangeKeyButton (KeyMappingEditorComponent& kec, CommandID command,
|
||||
const String& keyName, int keyIndex)
|
||||
: Button (keyName),
|
||||
owner (kec),
|
||||
commandID (command),
|
||||
keyNum (keyIndex)
|
||||
{
|
||||
setWantsKeyboardFocus (false);
|
||||
setTriggeredOnMouseDown (keyNum >= 0);
|
||||
|
||||
setTooltip (keyIndex < 0 ? TRANS ("Adds a new key-mapping")
|
||||
: TRANS ("Click to change this key-mapping"));
|
||||
}
|
||||
|
||||
void paintButton (Graphics& g, bool /*isOver*/, bool /*isDown*/) override
|
||||
{
|
||||
getLookAndFeel().drawKeymapChangeButton (g, getWidth(), getHeight(), *this,
|
||||
keyNum >= 0 ? getName() : String());
|
||||
}
|
||||
|
||||
void clicked() override
|
||||
{
|
||||
if (keyNum >= 0)
|
||||
{
|
||||
Component::SafePointer<ChangeKeyButton> button (this);
|
||||
PopupMenu m;
|
||||
|
||||
m.addItem (TRANS ("Change this key-mapping"),
|
||||
[button]
|
||||
{
|
||||
if (button != nullptr)
|
||||
button.getComponent()->assignNewKey();
|
||||
});
|
||||
|
||||
m.addSeparator();
|
||||
|
||||
m.addItem (TRANS ("Remove this key-mapping"),
|
||||
[button]
|
||||
{
|
||||
if (button != nullptr)
|
||||
button->owner.getMappings().removeKeyPress (button->commandID,
|
||||
button->keyNum);
|
||||
});
|
||||
|
||||
m.showMenuAsync (PopupMenu::Options().withTargetComponent (this));
|
||||
}
|
||||
else
|
||||
{
|
||||
assignNewKey(); // + button pressed..
|
||||
}
|
||||
}
|
||||
|
||||
using Button::clicked;
|
||||
|
||||
void fitToContent (const int h) noexcept
|
||||
{
|
||||
if (keyNum < 0)
|
||||
{
|
||||
setSize (h, h);
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto idealWidth = GlyphArrangement::getStringWidthInt (withDefaultMetrics (FontOptions { (float) h * 0.6f }), getName());
|
||||
setSize (jlimit (h * 4, h * 8, 6 + idealWidth), h);
|
||||
}
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
class KeyEntryWindow final : public AlertWindow
|
||||
{
|
||||
public:
|
||||
KeyEntryWindow (KeyMappingEditorComponent& kec)
|
||||
: AlertWindow (TRANS ("New key-mapping"),
|
||||
TRANS ("Please press a key combination now..."),
|
||||
MessageBoxIconType::NoIcon),
|
||||
owner (kec)
|
||||
{
|
||||
addButton (TRANS ("OK"), 1);
|
||||
addButton (TRANS ("Cancel"), 0);
|
||||
|
||||
// (avoid return + escape keys getting processed by the buttons..)
|
||||
for (auto* child : getChildren())
|
||||
child->setWantsKeyboardFocus (false);
|
||||
|
||||
setWantsKeyboardFocus (true);
|
||||
grabKeyboardFocus();
|
||||
}
|
||||
|
||||
bool keyPressed (const KeyPress& key) override
|
||||
{
|
||||
lastPress = key;
|
||||
String message (TRANS ("Key") + ": " + owner.getDescriptionForKeyPress (key));
|
||||
|
||||
auto previousCommand = owner.getMappings().findCommandForKeyPress (key);
|
||||
|
||||
if (previousCommand != 0)
|
||||
message << "\n\n("
|
||||
<< TRANS ("Currently assigned to \"CMDN\"")
|
||||
.replace ("CMDN", TRANS (owner.getCommandManager().getNameOfCommand (previousCommand)))
|
||||
<< ')';
|
||||
|
||||
setMessage (message);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool keyStateChanged (bool) override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
KeyPress lastPress;
|
||||
|
||||
private:
|
||||
KeyMappingEditorComponent& owner;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (KeyEntryWindow)
|
||||
};
|
||||
|
||||
void setNewKey (const KeyPress& newKey, bool dontAskUser)
|
||||
{
|
||||
if (newKey.isValid())
|
||||
{
|
||||
auto previousCommand = owner.getMappings().findCommandForKeyPress (newKey);
|
||||
|
||||
if (previousCommand == 0 || dontAskUser)
|
||||
{
|
||||
owner.getMappings().removeKeyPress (newKey);
|
||||
|
||||
if (keyNum >= 0)
|
||||
owner.getMappings().removeKeyPress (commandID, keyNum);
|
||||
|
||||
owner.getMappings().addKeyPress (commandID, newKey, keyNum);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto options = MessageBoxOptions::makeOptionsOkCancel (MessageBoxIconType::WarningIcon,
|
||||
TRANS ("Change key-mapping"),
|
||||
TRANS ("This key is already assigned to the command \"CMDN\"")
|
||||
.replace ("CMDN", owner.getCommandManager().getNameOfCommand (previousCommand))
|
||||
+ "\n\n"
|
||||
+ TRANS ("Do you want to re-assign it to this new command instead?"),
|
||||
TRANS ("Re-assign"),
|
||||
TRANS ("Cancel"),
|
||||
this);
|
||||
messageBox = AlertWindow::showScopedAsync (options, [this, newKey] (int result)
|
||||
{
|
||||
if (result != 0)
|
||||
setNewKey (newKey, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void keyChosen (int result, ChangeKeyButton* button)
|
||||
{
|
||||
if (button != nullptr && button->currentKeyEntryWindow != nullptr)
|
||||
{
|
||||
if (result != 0)
|
||||
{
|
||||
button->currentKeyEntryWindow->setVisible (false);
|
||||
button->setNewKey (button->currentKeyEntryWindow->lastPress, false);
|
||||
}
|
||||
|
||||
button->currentKeyEntryWindow.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void assignNewKey()
|
||||
{
|
||||
currentKeyEntryWindow.reset (new KeyEntryWindow (owner));
|
||||
currentKeyEntryWindow->enterModalState (true, ModalCallbackFunction::forComponent (keyChosen, this));
|
||||
}
|
||||
|
||||
private:
|
||||
KeyMappingEditorComponent& owner;
|
||||
const CommandID commandID;
|
||||
const int keyNum;
|
||||
std::unique_ptr<KeyEntryWindow> currentKeyEntryWindow;
|
||||
ScopedMessageBox messageBox;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChangeKeyButton)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class KeyMappingEditorComponent::ItemComponent final : public Component
|
||||
{
|
||||
public:
|
||||
ItemComponent (KeyMappingEditorComponent& kec, CommandID command)
|
||||
: owner (kec), commandID (command)
|
||||
{
|
||||
setInterceptsMouseClicks (false, true);
|
||||
|
||||
const bool isReadOnly = owner.isCommandReadOnly (commandID);
|
||||
|
||||
auto keyPresses = owner.getMappings().getKeyPressesAssignedToCommand (commandID);
|
||||
|
||||
for (int i = 0; i < jmin ((int) maxNumAssignments, keyPresses.size()); ++i)
|
||||
addKeyPressButton (owner.getDescriptionForKeyPress (keyPresses.getReference (i)), i, isReadOnly);
|
||||
|
||||
addKeyPressButton ("Change Key Mapping", -1, isReadOnly);
|
||||
}
|
||||
|
||||
void addKeyPressButton (const String& desc, const int index, const bool isReadOnly)
|
||||
{
|
||||
auto* b = new ChangeKeyButton (owner, commandID, desc, index);
|
||||
keyChangeButtons.add (b);
|
||||
|
||||
b->setEnabled (! isReadOnly);
|
||||
b->setVisible (keyChangeButtons.size() <= (int) maxNumAssignments);
|
||||
addChildComponent (b);
|
||||
}
|
||||
|
||||
void paint (Graphics& g) override
|
||||
{
|
||||
g.setFont ((float) getHeight() * 0.7f);
|
||||
g.setColour (owner.findColour (KeyMappingEditorComponent::textColourId));
|
||||
|
||||
g.drawFittedText (TRANS (owner.getCommandManager().getNameOfCommand (commandID)),
|
||||
4, 0, jmax (40, getChildComponent (0)->getX() - 5), getHeight(),
|
||||
Justification::centredLeft, true);
|
||||
}
|
||||
|
||||
void resized() override
|
||||
{
|
||||
int x = getWidth() - 4;
|
||||
|
||||
for (int i = keyChangeButtons.size(); --i >= 0;)
|
||||
{
|
||||
auto* b = keyChangeButtons.getUnchecked (i);
|
||||
|
||||
b->fitToContent (getHeight() - 2);
|
||||
b->setTopRightPosition (x, 1);
|
||||
x = b->getX() - 5;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
|
||||
{
|
||||
return createIgnoredAccessibilityHandler (*this);
|
||||
}
|
||||
|
||||
KeyMappingEditorComponent& owner;
|
||||
OwnedArray<ChangeKeyButton> keyChangeButtons;
|
||||
const CommandID commandID;
|
||||
|
||||
enum { maxNumAssignments = 3 };
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemComponent)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class KeyMappingEditorComponent::MappingItem final : public TreeViewItem
|
||||
{
|
||||
public:
|
||||
MappingItem (KeyMappingEditorComponent& kec, CommandID command)
|
||||
: owner (kec), commandID (command)
|
||||
{}
|
||||
|
||||
String getUniqueName() const override { return String ((int) commandID) + "_id"; }
|
||||
bool mightContainSubItems() override { return false; }
|
||||
int getItemHeight() const override { return 20; }
|
||||
std::unique_ptr<Component> createItemComponent() override { return std::make_unique<ItemComponent> (owner, commandID); }
|
||||
String getAccessibilityName() override { return TRANS (owner.getCommandManager().getNameOfCommand (commandID)); }
|
||||
|
||||
private:
|
||||
KeyMappingEditorComponent& owner;
|
||||
const CommandID commandID;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MappingItem)
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
class KeyMappingEditorComponent::CategoryItem final : public TreeViewItem
|
||||
{
|
||||
public:
|
||||
CategoryItem (KeyMappingEditorComponent& kec, const String& name)
|
||||
: owner (kec), categoryName (name)
|
||||
{}
|
||||
|
||||
String getUniqueName() const override { return categoryName + "_cat"; }
|
||||
bool mightContainSubItems() override { return true; }
|
||||
int getItemHeight() const override { return 22; }
|
||||
String getAccessibilityName() override { return categoryName; }
|
||||
|
||||
void paintItem (Graphics& g, int width, int height) override
|
||||
{
|
||||
g.setFont (owner.withDefaultMetrics (FontOptions ((float) height * 0.7f, Font::bold)));
|
||||
g.setColour (owner.findColour (KeyMappingEditorComponent::textColourId));
|
||||
|
||||
g.drawText (TRANS (categoryName), 2, 0, width - 2, height, Justification::centredLeft, true);
|
||||
}
|
||||
|
||||
void itemOpennessChanged (bool isNowOpen) override
|
||||
{
|
||||
if (isNowOpen)
|
||||
{
|
||||
if (getNumSubItems() == 0)
|
||||
for (auto command : owner.getCommandManager().getCommandsInCategory (categoryName))
|
||||
if (owner.shouldCommandBeIncluded (command))
|
||||
addSubItem (new MappingItem (owner, command));
|
||||
}
|
||||
else
|
||||
{
|
||||
clearSubItems();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
KeyMappingEditorComponent& owner;
|
||||
String categoryName;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CategoryItem)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class KeyMappingEditorComponent::TopLevelItem final : public TreeViewItem,
|
||||
private ChangeListener
|
||||
{
|
||||
public:
|
||||
TopLevelItem (KeyMappingEditorComponent& kec) : owner (kec)
|
||||
{
|
||||
setLinesDrawnForSubItems (false);
|
||||
owner.getMappings().addChangeListener (this);
|
||||
}
|
||||
|
||||
~TopLevelItem() override
|
||||
{
|
||||
owner.getMappings().removeChangeListener (this);
|
||||
}
|
||||
|
||||
bool mightContainSubItems() override { return true; }
|
||||
String getUniqueName() const override { return "keys"; }
|
||||
|
||||
void changeListenerCallback (ChangeBroadcaster*) override
|
||||
{
|
||||
const OpennessRestorer opennessRestorer (*this);
|
||||
clearSubItems();
|
||||
|
||||
for (auto category : owner.getCommandManager().getCommandCategories())
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
for (auto command : owner.getCommandManager().getCommandsInCategory (category))
|
||||
if (owner.shouldCommandBeIncluded (command))
|
||||
++count;
|
||||
|
||||
if (count > 0)
|
||||
addSubItem (new CategoryItem (owner, category));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
KeyMappingEditorComponent& owner;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
KeyMappingEditorComponent::KeyMappingEditorComponent (KeyPressMappingSet& mappingManager,
|
||||
const bool showResetToDefaultButton)
|
||||
: mappings (mappingManager),
|
||||
resetButton (TRANS ("reset to defaults"))
|
||||
{
|
||||
treeItem.reset (new TopLevelItem (*this));
|
||||
|
||||
if (showResetToDefaultButton)
|
||||
{
|
||||
addAndMakeVisible (resetButton);
|
||||
|
||||
resetButton.onClick = [this]
|
||||
{
|
||||
auto options = MessageBoxOptions::makeOptionsOkCancel (MessageBoxIconType::QuestionIcon,
|
||||
TRANS ("Reset to defaults"),
|
||||
TRANS ("Are you sure you want to reset all the key-mappings to their default state?"),
|
||||
TRANS ("Reset"),
|
||||
{},
|
||||
this);
|
||||
messageBox = AlertWindow::showScopedAsync (options, [this] (int result)
|
||||
{
|
||||
if (result != 0)
|
||||
getMappings().resetToDefaultMappings();
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
addAndMakeVisible (tree);
|
||||
tree.setTitle ("Key Mappings");
|
||||
tree.setColour (TreeView::backgroundColourId, findColour (backgroundColourId));
|
||||
tree.setRootItemVisible (false);
|
||||
tree.setDefaultOpenness (true);
|
||||
tree.setRootItem (treeItem.get());
|
||||
tree.setIndentSize (12);
|
||||
}
|
||||
|
||||
KeyMappingEditorComponent::~KeyMappingEditorComponent()
|
||||
{
|
||||
tree.setRootItem (nullptr);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void KeyMappingEditorComponent::setColours (Colour mainBackground,
|
||||
Colour textColour)
|
||||
{
|
||||
setColour (backgroundColourId, mainBackground);
|
||||
setColour (textColourId, textColour);
|
||||
tree.setColour (TreeView::backgroundColourId, mainBackground);
|
||||
}
|
||||
|
||||
void KeyMappingEditorComponent::parentHierarchyChanged()
|
||||
{
|
||||
treeItem->changeListenerCallback (nullptr);
|
||||
}
|
||||
|
||||
void KeyMappingEditorComponent::resized()
|
||||
{
|
||||
int h = getHeight();
|
||||
|
||||
if (resetButton.isVisible())
|
||||
{
|
||||
const int buttonHeight = 20;
|
||||
h -= buttonHeight + 8;
|
||||
int x = getWidth() - 8;
|
||||
|
||||
resetButton.changeWidthToFitText (buttonHeight);
|
||||
resetButton.setTopRightPosition (x, h + 6);
|
||||
}
|
||||
|
||||
tree.setBounds (0, 0, getWidth(), h);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool KeyMappingEditorComponent::shouldCommandBeIncluded (const CommandID commandID)
|
||||
{
|
||||
auto* ci = mappings.getCommandManager().getCommandForID (commandID);
|
||||
|
||||
return ci != nullptr && (ci->flags & ApplicationCommandInfo::hiddenFromKeyEditor) == 0;
|
||||
}
|
||||
|
||||
bool KeyMappingEditorComponent::isCommandReadOnly (const CommandID commandID)
|
||||
{
|
||||
auto* ci = mappings.getCommandManager().getCommandForID (commandID);
|
||||
|
||||
return ci != nullptr && (ci->flags & ApplicationCommandInfo::readOnlyInKeyEditor) != 0;
|
||||
}
|
||||
|
||||
String KeyMappingEditorComponent::getDescriptionForKeyPress (const KeyPress& key)
|
||||
{
|
||||
return key.getTextDescription();
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A component to allow editing of the keymaps stored by a KeyPressMappingSet
|
||||
object.
|
||||
|
||||
@see KeyPressMappingSet
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API KeyMappingEditorComponent : public Component
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a KeyMappingEditorComponent.
|
||||
|
||||
@param mappingSet this is the set of mappings to display and edit. Make sure the
|
||||
mappings object is not deleted before this component!
|
||||
@param showResetToDefaultButton if true, then at the bottom of the list, the
|
||||
component will include a 'reset to defaults' button.
|
||||
*/
|
||||
KeyMappingEditorComponent (KeyPressMappingSet& mappingSet,
|
||||
bool showResetToDefaultButton);
|
||||
|
||||
/** Destructor. */
|
||||
~KeyMappingEditorComponent() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Sets up the colours to use for parts of the component.
|
||||
|
||||
@param mainBackground colour to use for most of the background
|
||||
@param textColour colour to use for the text
|
||||
*/
|
||||
void setColours (Colour mainBackground,
|
||||
Colour textColour);
|
||||
|
||||
/** Returns the KeyPressMappingSet that this component is acting upon. */
|
||||
KeyPressMappingSet& getMappings() const noexcept { return mappings; }
|
||||
|
||||
/** Returns the ApplicationCommandManager that this component is connected to. */
|
||||
ApplicationCommandManager& getCommandManager() const noexcept { return mappings.getCommandManager(); }
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/** Can be overridden if some commands need to be excluded from the list.
|
||||
|
||||
By default this will use the KeyPressMappingSet's shouldCommandBeVisibleInEditor()
|
||||
method to decide what to return, but you can override it to handle special cases.
|
||||
*/
|
||||
virtual bool shouldCommandBeIncluded (CommandID commandID);
|
||||
|
||||
/** Can be overridden to indicate that some commands are shown as read-only.
|
||||
|
||||
By default this will use the KeyPressMappingSet's shouldCommandBeReadOnlyInEditor()
|
||||
method to decide what to return, but you can override it to handle special cases.
|
||||
*/
|
||||
virtual bool isCommandReadOnly (CommandID commandID);
|
||||
|
||||
/** This can be overridden to let you change the format of the string used
|
||||
to describe a keypress.
|
||||
|
||||
This is handy if you're using non-standard KeyPress objects, e.g. for custom
|
||||
keys that are triggered by something else externally. If you override the
|
||||
method, be sure to let the base class's method handle keys you're not
|
||||
interested in.
|
||||
*/
|
||||
virtual String getDescriptionForKeyPress (const KeyPress& key);
|
||||
|
||||
//==============================================================================
|
||||
/** A set of colour IDs to use to change the colour of various aspects of the editor.
|
||||
|
||||
These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
|
||||
methods.
|
||||
|
||||
@see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
|
||||
*/
|
||||
enum ColourIds
|
||||
{
|
||||
backgroundColourId = 0x100ad00, /**< The background colour to fill the editor background. */
|
||||
textColourId = 0x100ad01, /**< The colour for the text. */
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/** @internal */
|
||||
void parentHierarchyChanged() override;
|
||||
/** @internal */
|
||||
void resized() override;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
KeyPressMappingSet& mappings;
|
||||
TreeView tree;
|
||||
TextButton resetButton;
|
||||
|
||||
class TopLevelItem;
|
||||
class ChangeKeyButton;
|
||||
class MappingItem;
|
||||
class CategoryItem;
|
||||
class ItemComponent;
|
||||
std::unique_ptr<TopLevelItem> treeItem;
|
||||
ScopedMessageBox messageBox;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (KeyMappingEditorComponent)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,498 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
#if JUCE_ENABLE_LIVE_CONSTANT_EDITOR
|
||||
|
||||
namespace juce::LiveConstantEditor
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
class AllComponentRepainter final : private Timer,
|
||||
private DeletedAtShutdown
|
||||
{
|
||||
public:
|
||||
AllComponentRepainter() {}
|
||||
~AllComponentRepainter() override { clearSingletonInstance(); }
|
||||
|
||||
JUCE_DECLARE_SINGLETON_INLINE (AllComponentRepainter, false)
|
||||
|
||||
void trigger()
|
||||
{
|
||||
if (! isTimerRunning())
|
||||
startTimer (100);
|
||||
}
|
||||
|
||||
private:
|
||||
void timerCallback() override
|
||||
{
|
||||
stopTimer();
|
||||
|
||||
Array<Component*> alreadyDone;
|
||||
|
||||
for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
|
||||
if (auto* c = TopLevelWindow::getTopLevelWindow (i))
|
||||
repaintAndResizeAllComps (c, alreadyDone);
|
||||
|
||||
auto& desktop = Desktop::getInstance();
|
||||
|
||||
for (int i = desktop.getNumComponents(); --i >= 0;)
|
||||
if (auto* c = desktop.getComponent (i))
|
||||
repaintAndResizeAllComps (c, alreadyDone);
|
||||
}
|
||||
|
||||
static void repaintAndResizeAllComps (Component::SafePointer<Component> c,
|
||||
Array<Component*>& alreadyDone)
|
||||
{
|
||||
if (c->isVisible() && ! alreadyDone.contains (c))
|
||||
{
|
||||
c->repaint();
|
||||
c->resized();
|
||||
|
||||
for (int i = c->getNumChildComponents(); --i >= 0;)
|
||||
{
|
||||
if (auto* child = c->getChildComponent (i))
|
||||
{
|
||||
repaintAndResizeAllComps (child, alreadyDone);
|
||||
alreadyDone.add (child);
|
||||
}
|
||||
|
||||
if (c == nullptr)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
int64 parseInt (String s)
|
||||
{
|
||||
s = s.trimStart();
|
||||
|
||||
if (s.startsWithChar ('-'))
|
||||
return -parseInt (s.substring (1));
|
||||
|
||||
if (s.startsWith ("0x"))
|
||||
return s.substring (2).getHexValue64();
|
||||
|
||||
return s.getLargeIntValue();
|
||||
}
|
||||
|
||||
double parseDouble (const String& s)
|
||||
{
|
||||
return s.retainCharacters ("0123456789.eE-").getDoubleValue();
|
||||
}
|
||||
|
||||
String intToString (int v, bool preferHex) { return preferHex ? "0x" + String::toHexString (v) : String (v); }
|
||||
String intToString (int64 v, bool preferHex) { return preferHex ? "0x" + String::toHexString (v) : String (v); }
|
||||
|
||||
//==============================================================================
|
||||
LiveValueBase::LiveValueBase (const char* file, int line)
|
||||
: sourceFile (file), sourceLine (line)
|
||||
{
|
||||
name = File (sourceFile).getFileName() + " : " + String (sourceLine);
|
||||
}
|
||||
|
||||
LiveValueBase::~LiveValueBase()
|
||||
{
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
LivePropertyEditorBase::LivePropertyEditorBase (LiveValueBase& v, CodeDocument& d)
|
||||
: value (v), document (d), sourceEditor (document, &tokeniser)
|
||||
{
|
||||
setSize (600, 100);
|
||||
|
||||
addAndMakeVisible (name);
|
||||
addAndMakeVisible (resetButton);
|
||||
addAndMakeVisible (valueEditor);
|
||||
addAndMakeVisible (sourceEditor);
|
||||
|
||||
findOriginalValueInCode();
|
||||
selectOriginalValue();
|
||||
|
||||
name.setFont (withDefaultMetrics (FontOptions { 13.0f }));
|
||||
name.setText (v.name, dontSendNotification);
|
||||
valueEditor.setMultiLine (v.isString());
|
||||
valueEditor.setReturnKeyStartsNewLine (v.isString());
|
||||
valueEditor.setText (v.getStringValue (wasHex), dontSendNotification);
|
||||
valueEditor.onTextChange = [this] { applyNewValue (valueEditor.getText()); };
|
||||
sourceEditor.setReadOnly (true);
|
||||
sourceEditor.setFont (sourceEditor.getFont().withHeight (13.0f));
|
||||
resetButton.onClick = [this] { applyNewValue (value.getOriginalStringValue (wasHex)); };
|
||||
}
|
||||
|
||||
void LivePropertyEditorBase::paint (Graphics& g)
|
||||
{
|
||||
g.setColour (Colours::white);
|
||||
g.fillRect (getLocalBounds().removeFromBottom (1));
|
||||
}
|
||||
|
||||
void LivePropertyEditorBase::resized()
|
||||
{
|
||||
auto r = getLocalBounds().reduced (0, 3).withTrimmedBottom (1);
|
||||
|
||||
auto left = r.removeFromLeft (jmax (200, r.getWidth() / 3));
|
||||
|
||||
auto top = left.removeFromTop (25);
|
||||
resetButton.setBounds (top.removeFromRight (35).reduced (0, 3));
|
||||
name.setBounds (top);
|
||||
|
||||
if (customComp != nullptr)
|
||||
{
|
||||
valueEditor.setBounds (left.removeFromTop (25));
|
||||
left.removeFromTop (2);
|
||||
customComp->setBounds (left);
|
||||
}
|
||||
else
|
||||
{
|
||||
valueEditor.setBounds (left);
|
||||
}
|
||||
|
||||
r.removeFromLeft (4);
|
||||
sourceEditor.setBounds (r);
|
||||
}
|
||||
|
||||
void LivePropertyEditorBase::applyNewValue (const String& s)
|
||||
{
|
||||
value.setStringValue (s);
|
||||
|
||||
document.replaceSection (valueStart.getPosition(), valueEnd.getPosition(), value.getCodeValue (wasHex));
|
||||
document.clearUndoHistory();
|
||||
selectOriginalValue();
|
||||
|
||||
valueEditor.setText (s, dontSendNotification);
|
||||
AllComponentRepainter::getInstance()->trigger();
|
||||
}
|
||||
|
||||
void LivePropertyEditorBase::selectOriginalValue()
|
||||
{
|
||||
sourceEditor.selectRegion (valueStart, valueEnd);
|
||||
}
|
||||
|
||||
void LivePropertyEditorBase::findOriginalValueInCode()
|
||||
{
|
||||
CodeDocument::Position pos (document, value.sourceLine, 0);
|
||||
auto line = pos.getLineText();
|
||||
auto p = line.getCharPointer();
|
||||
|
||||
p = CharacterFunctions::find (p, CharPointer_ASCII ("JUCE_LIVE_CONSTANT"));
|
||||
|
||||
if (p.isEmpty())
|
||||
{
|
||||
// Not sure how this would happen - some kind of mix-up between source code and line numbers..
|
||||
jassertfalse;
|
||||
return;
|
||||
}
|
||||
|
||||
p += (int) (sizeof ("JUCE_LIVE_CONSTANT") - 1);
|
||||
p.incrementToEndOfWhitespace();
|
||||
|
||||
if (! CharacterFunctions::find (p, CharPointer_ASCII ("JUCE_LIVE_CONSTANT")).isEmpty())
|
||||
{
|
||||
// Aargh! You've added two JUCE_LIVE_CONSTANT macros on the same line!
|
||||
// They're identified by their line number, so you must make sure each
|
||||
// one goes on a separate line!
|
||||
jassertfalse;
|
||||
}
|
||||
|
||||
if (p.getAndAdvance() == '(')
|
||||
{
|
||||
auto start = p, end = p;
|
||||
|
||||
int depth = 1;
|
||||
|
||||
while (! end.isEmpty())
|
||||
{
|
||||
auto c = end.getAndAdvance();
|
||||
|
||||
if (c == '(') ++depth;
|
||||
if (c == ')') --depth;
|
||||
|
||||
if (depth == 0)
|
||||
{
|
||||
--end;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (end > start)
|
||||
{
|
||||
valueStart = CodeDocument::Position (document, value.sourceLine, (int) (start - line.getCharPointer()));
|
||||
valueEnd = CodeDocument::Position (document, value.sourceLine, (int) (end - line.getCharPointer()));
|
||||
|
||||
valueStart.setPositionMaintained (true);
|
||||
valueEnd.setPositionMaintained (true);
|
||||
|
||||
wasHex = String (start, end).containsIgnoreCase ("0x");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
class ValueListHolderComponent final : public Component
|
||||
{
|
||||
public:
|
||||
ValueListHolderComponent (ValueList& l) : valueList (l)
|
||||
{
|
||||
setVisible (true);
|
||||
}
|
||||
|
||||
void addItem (int width, LiveValueBase& v, CodeDocument& doc)
|
||||
{
|
||||
addAndMakeVisible (editors.add (v.createPropertyComponent (doc)));
|
||||
layout (width);
|
||||
}
|
||||
|
||||
void layout (int width)
|
||||
{
|
||||
setSize (width, editors.size() * itemHeight);
|
||||
resized();
|
||||
}
|
||||
|
||||
void resized() override
|
||||
{
|
||||
auto r = getLocalBounds().reduced (2, 0);
|
||||
|
||||
for (int i = 0; i < editors.size(); ++i)
|
||||
editors.getUnchecked (i)->setBounds (r.removeFromTop (itemHeight));
|
||||
}
|
||||
|
||||
enum { itemHeight = 120 };
|
||||
|
||||
ValueList& valueList;
|
||||
OwnedArray<LivePropertyEditorBase> editors;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class ValueList::EditorWindow final : public DocumentWindow,
|
||||
private DeletedAtShutdown
|
||||
{
|
||||
public:
|
||||
EditorWindow (ValueList& list)
|
||||
: DocumentWindow ("Live Values", Colours::lightgrey, DocumentWindow::closeButton)
|
||||
{
|
||||
setLookAndFeel (&lookAndFeel);
|
||||
setUsingNativeTitleBar (true);
|
||||
|
||||
viewport.setViewedComponent (new ValueListHolderComponent (list), true);
|
||||
viewport.setSize (700, 600);
|
||||
viewport.setScrollBarsShown (true, false);
|
||||
|
||||
setContentNonOwned (&viewport, true);
|
||||
setResizable (true, false);
|
||||
setResizeLimits (500, 400, 10000, 10000);
|
||||
centreWithSize (getWidth(), getHeight());
|
||||
setVisible (true);
|
||||
}
|
||||
|
||||
~EditorWindow() override
|
||||
{
|
||||
setLookAndFeel (nullptr);
|
||||
}
|
||||
|
||||
void closeButtonPressed() override
|
||||
{
|
||||
setVisible (false);
|
||||
}
|
||||
|
||||
void updateItems (ValueList& list)
|
||||
{
|
||||
if (auto* l = dynamic_cast<ValueListHolderComponent*> (viewport.getViewedComponent()))
|
||||
{
|
||||
while (l->getNumChildComponents() < list.values.size())
|
||||
{
|
||||
if (auto* v = list.values [l->getNumChildComponents()])
|
||||
l->addItem (viewport.getMaximumVisibleWidth(), *v, list.getDocument (v->sourceFile));
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
setVisible (true);
|
||||
}
|
||||
}
|
||||
|
||||
void resized() override
|
||||
{
|
||||
DocumentWindow::resized();
|
||||
|
||||
if (auto* l = dynamic_cast<ValueListHolderComponent*> (viewport.getViewedComponent()))
|
||||
l->layout (viewport.getMaximumVisibleWidth());
|
||||
}
|
||||
|
||||
Viewport viewport;
|
||||
LookAndFeel_V3 lookAndFeel;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
ValueList::ValueList() {}
|
||||
ValueList::~ValueList() { clearSingletonInstance(); }
|
||||
|
||||
void ValueList::addValue (LiveValueBase* v)
|
||||
{
|
||||
values.add (v);
|
||||
triggerAsyncUpdate();
|
||||
}
|
||||
|
||||
void ValueList::handleAsyncUpdate()
|
||||
{
|
||||
if (editorWindow == nullptr)
|
||||
editorWindow = new EditorWindow (*this);
|
||||
|
||||
editorWindow->updateItems (*this);
|
||||
}
|
||||
|
||||
CodeDocument& ValueList::getDocument (const File& file)
|
||||
{
|
||||
const int index = documentFiles.indexOf (file.getFullPathName());
|
||||
|
||||
if (index >= 0)
|
||||
return *documents.getUnchecked (index);
|
||||
|
||||
auto* doc = documents.add (new CodeDocument());
|
||||
documentFiles.add (file);
|
||||
doc->replaceAllContent (file.loadFileAsString());
|
||||
doc->clearUndoHistory();
|
||||
return *doc;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
struct ColourEditorComp final : public Component,
|
||||
private ChangeListener
|
||||
{
|
||||
ColourEditorComp (LivePropertyEditorBase& e) : editor (e)
|
||||
{
|
||||
setMouseCursor (MouseCursor::PointingHandCursor);
|
||||
}
|
||||
|
||||
Colour getColour() const
|
||||
{
|
||||
return Colour ((uint32) parseInt (editor.value.getStringValue (false)));
|
||||
}
|
||||
|
||||
void paint (Graphics& g) override
|
||||
{
|
||||
g.fillCheckerBoard (getLocalBounds().toFloat(), 6.0f, 6.0f,
|
||||
Colour (0xffdddddd).overlaidWith (getColour()),
|
||||
Colour (0xffffffff).overlaidWith (getColour()));
|
||||
}
|
||||
|
||||
void mouseDown (const MouseEvent&) override
|
||||
{
|
||||
auto colourSelector = std::make_unique<ColourSelector>();
|
||||
colourSelector->setName ("Colour");
|
||||
colourSelector->setCurrentColour (getColour());
|
||||
colourSelector->addChangeListener (this);
|
||||
colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack);
|
||||
colourSelector->setSize (300, 400);
|
||||
|
||||
CallOutBox::launchAsynchronously (std::move (colourSelector), getScreenBounds(), nullptr);
|
||||
}
|
||||
|
||||
void changeListenerCallback (ChangeBroadcaster* source) override
|
||||
{
|
||||
if (auto* cs = dynamic_cast<ColourSelector*> (source))
|
||||
editor.applyNewValue (getAsString (cs->getCurrentColour(), true));
|
||||
|
||||
repaint();
|
||||
}
|
||||
|
||||
LivePropertyEditorBase& editor;
|
||||
};
|
||||
|
||||
Component* createColourEditor (LivePropertyEditorBase& editor)
|
||||
{
|
||||
return new ColourEditorComp (editor);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
struct SliderComp : public Component
|
||||
{
|
||||
SliderComp (LivePropertyEditorBase& e, bool useFloat)
|
||||
: editor (e), isFloat (useFloat)
|
||||
{
|
||||
slider.setTextBoxStyle (Slider::NoTextBox, true, 0, 0);
|
||||
addAndMakeVisible (slider);
|
||||
updateRange();
|
||||
slider.onDragEnd = [this] { updateRange(); };
|
||||
slider.onValueChange = [this]
|
||||
{
|
||||
editor.applyNewValue (isFloat ? getAsString ((double) slider.getValue(), editor.wasHex)
|
||||
: getAsString ((int64) slider.getValue(), editor.wasHex));
|
||||
};
|
||||
}
|
||||
|
||||
virtual void updateRange()
|
||||
{
|
||||
double v = isFloat ? parseDouble (editor.value.getStringValue (false))
|
||||
: (double) parseInt (editor.value.getStringValue (false));
|
||||
|
||||
double range = isFloat ? 10 : 100;
|
||||
|
||||
slider.setRange (v - range, v + range);
|
||||
slider.setValue (v, dontSendNotification);
|
||||
}
|
||||
|
||||
void resized() override
|
||||
{
|
||||
slider.setBounds (getLocalBounds().removeFromTop (25));
|
||||
}
|
||||
|
||||
LivePropertyEditorBase& editor;
|
||||
Slider slider;
|
||||
bool isFloat;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
struct BoolSliderComp final : public SliderComp
|
||||
{
|
||||
BoolSliderComp (LivePropertyEditorBase& e)
|
||||
: SliderComp (e, false)
|
||||
{
|
||||
slider.onValueChange = [this] { editor.applyNewValue (slider.getValue() > 0.5 ? "true" : "false"); };
|
||||
}
|
||||
|
||||
void updateRange() override
|
||||
{
|
||||
slider.setRange (0.0, 1.0, dontSendNotification);
|
||||
slider.setValue (editor.value.getStringValue (false) == "true", dontSendNotification);
|
||||
}
|
||||
};
|
||||
|
||||
Component* createIntegerSlider (LivePropertyEditorBase& editor) { return new SliderComp (editor, false); }
|
||||
Component* createFloatSlider (LivePropertyEditorBase& editor) { return new SliderComp (editor, true); }
|
||||
Component* createBoolSlider (LivePropertyEditorBase& editor) { return new BoolSliderComp (editor); }
|
||||
|
||||
} // namespace juce::LiveConstantEditor
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,310 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
#if JUCE_ENABLE_LIVE_CONSTANT_EDITOR && ! defined (DOXYGEN)
|
||||
|
||||
//==============================================================================
|
||||
/** You can safely ignore all the stuff in this namespace - it's a bunch of boilerplate
|
||||
code used to implement the JUCE_LIVE_CONSTANT functionality.
|
||||
*/
|
||||
namespace juce::LiveConstantEditor
|
||||
{
|
||||
int64 parseInt (String);
|
||||
double parseDouble (const String&);
|
||||
String intToString (int, bool preferHex);
|
||||
String intToString (int64, bool preferHex);
|
||||
|
||||
template <typename Type>
|
||||
static void setFromString (Type& v, const String& s) { v = static_cast<Type> (s); }
|
||||
inline void setFromString (char& v, const String& s) { v = (char) parseInt (s); }
|
||||
inline void setFromString (unsigned char& v, const String& s) { v = (unsigned char) parseInt (s); }
|
||||
inline void setFromString (short& v, const String& s) { v = (short) parseInt (s); }
|
||||
inline void setFromString (unsigned short& v, const String& s) { v = (unsigned short) parseInt (s); }
|
||||
inline void setFromString (int& v, const String& s) { v = (int) parseInt (s); }
|
||||
inline void setFromString (unsigned int& v, const String& s) { v = (unsigned int) parseInt (s); }
|
||||
inline void setFromString (long& v, const String& s) { v = (long) parseInt (s); }
|
||||
inline void setFromString (unsigned long& v, const String& s) { v = (unsigned long) parseInt (s); }
|
||||
inline void setFromString (int64& v, const String& s) { v = (int64) parseInt (s); }
|
||||
inline void setFromString (uint64& v, const String& s) { v = (uint64) parseInt (s); }
|
||||
inline void setFromString (double& v, const String& s) { v = parseDouble (s); }
|
||||
inline void setFromString (float& v, const String& s) { v = (float) parseDouble (s); }
|
||||
inline void setFromString (bool& v, const String& s) { v = (s == "true"); }
|
||||
inline void setFromString (String& v, const String& s) { v = s; }
|
||||
inline void setFromString (Colour& v, const String& s) { v = Colour ((uint32) parseInt (s)); }
|
||||
|
||||
template <typename Type>
|
||||
inline String getAsString (const Type& v, bool) { return String (v); }
|
||||
inline String getAsString (char v, bool preferHex) { return intToString ((int) v, preferHex); }
|
||||
inline String getAsString (unsigned char v, bool preferHex) { return intToString ((int) v, preferHex); }
|
||||
inline String getAsString (short v, bool preferHex) { return intToString ((int) v, preferHex); }
|
||||
inline String getAsString (unsigned short v, bool preferHex) { return intToString ((int) v, preferHex); }
|
||||
inline String getAsString (int v, bool preferHex) { return intToString ((int) v, preferHex); }
|
||||
inline String getAsString (unsigned int v, bool preferHex) { return intToString ((int) v, preferHex); }
|
||||
inline String getAsString (bool v, bool) { return v ? "true" : "false"; }
|
||||
inline String getAsString (int64 v, bool preferHex) { return intToString ((int64) v, preferHex); }
|
||||
inline String getAsString (uint64 v, bool preferHex) { return intToString ((int64) v, preferHex); }
|
||||
inline String getAsString (Colour v, bool) { return intToString ((int) v.getARGB(), true); }
|
||||
|
||||
template <typename Type> struct isStringType { enum { value = 0 }; };
|
||||
template <> struct isStringType<String> { enum { value = 1 }; };
|
||||
|
||||
template <typename Type>
|
||||
inline String getAsCode (Type& v, bool preferHex) { return getAsString (v, preferHex); }
|
||||
inline String getAsCode (Colour v, bool) { return "Colour (0x" + String::toHexString ((int) v.getARGB()).paddedLeft ('0', 8) + ")"; }
|
||||
inline String getAsCode (const String& v, bool) { return CppTokeniserFunctions::addEscapeChars (v).quoted(); }
|
||||
inline String getAsCode (const char* v, bool) { return getAsCode (String (v), false); }
|
||||
|
||||
template <typename Type>
|
||||
inline const char* castToCharPointer (const Type&) { return ""; }
|
||||
inline const char* castToCharPointer (const String& s) { return s.toRawUTF8(); }
|
||||
|
||||
struct LivePropertyEditorBase;
|
||||
|
||||
//==============================================================================
|
||||
struct JUCE_API LiveValueBase
|
||||
{
|
||||
LiveValueBase (const char* file, int line);
|
||||
virtual ~LiveValueBase();
|
||||
|
||||
virtual LivePropertyEditorBase* createPropertyComponent (CodeDocument&) = 0;
|
||||
virtual String getStringValue (bool preferHex) const = 0;
|
||||
virtual String getCodeValue (bool preferHex) const = 0;
|
||||
virtual void setStringValue (const String&) = 0;
|
||||
virtual String getOriginalStringValue (bool preferHex) const = 0;
|
||||
virtual bool isString() const = 0;
|
||||
|
||||
String name, sourceFile;
|
||||
int sourceLine;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (LiveValueBase)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
struct JUCE_API LivePropertyEditorBase : public Component
|
||||
{
|
||||
LivePropertyEditorBase (LiveValueBase&, CodeDocument&);
|
||||
|
||||
void paint (Graphics&) override;
|
||||
void resized() override;
|
||||
|
||||
void applyNewValue (const String&);
|
||||
void selectOriginalValue();
|
||||
void findOriginalValueInCode();
|
||||
|
||||
LiveValueBase& value;
|
||||
Label name;
|
||||
TextEditor valueEditor;
|
||||
TextButton resetButton { "reset" };
|
||||
CodeDocument& document;
|
||||
CPlusPlusCodeTokeniser tokeniser;
|
||||
CodeEditorComponent sourceEditor;
|
||||
CodeDocument::Position valueStart, valueEnd;
|
||||
std::unique_ptr<Component> customComp;
|
||||
bool wasHex = false;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (LivePropertyEditorBase)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
Component* createColourEditor (LivePropertyEditorBase&);
|
||||
Component* createIntegerSlider (LivePropertyEditorBase&);
|
||||
Component* createFloatSlider (LivePropertyEditorBase&);
|
||||
Component* createBoolSlider (LivePropertyEditorBase&);
|
||||
|
||||
template <typename Type> struct CustomEditor { static Component* create (LivePropertyEditorBase&) { return nullptr; } };
|
||||
template <> struct CustomEditor<char> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
|
||||
template <> struct CustomEditor<unsigned char> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
|
||||
template <> struct CustomEditor<int> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
|
||||
template <> struct CustomEditor<unsigned int> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
|
||||
template <> struct CustomEditor<short> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
|
||||
template <> struct CustomEditor<unsigned short> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
|
||||
template <> struct CustomEditor<int64> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
|
||||
template <> struct CustomEditor<uint64> { static Component* create (LivePropertyEditorBase& e) { return createIntegerSlider (e); } };
|
||||
template <> struct CustomEditor<float> { static Component* create (LivePropertyEditorBase& e) { return createFloatSlider (e); } };
|
||||
template <> struct CustomEditor<double> { static Component* create (LivePropertyEditorBase& e) { return createFloatSlider (e); } };
|
||||
template <> struct CustomEditor<Colour> { static Component* create (LivePropertyEditorBase& e) { return createColourEditor (e); } };
|
||||
template <> struct CustomEditor<bool> { static Component* create (LivePropertyEditorBase& e) { return createBoolSlider (e); } };
|
||||
|
||||
template <typename Type>
|
||||
struct LivePropertyEditor : public LivePropertyEditorBase
|
||||
{
|
||||
template <typename ValueType>
|
||||
LivePropertyEditor (ValueType& v, CodeDocument& d) : LivePropertyEditorBase (v, d)
|
||||
{
|
||||
customComp.reset (CustomEditor<Type>::create (*this));
|
||||
addAndMakeVisible (customComp.get());
|
||||
}
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
template <typename Type>
|
||||
struct LiveValue : public LiveValueBase
|
||||
{
|
||||
LiveValue (const char* file, int line, const Type& initialValue)
|
||||
: LiveValueBase (file, line), value (initialValue), originalValue (initialValue)
|
||||
{}
|
||||
|
||||
operator Type() const noexcept { return value; }
|
||||
Type get() const noexcept { return value; }
|
||||
operator const char*() const { return castToCharPointer (value); }
|
||||
|
||||
LivePropertyEditorBase* createPropertyComponent (CodeDocument& doc) override
|
||||
{
|
||||
return new LivePropertyEditor<Type> (*this, doc);
|
||||
}
|
||||
|
||||
String getStringValue (bool preferHex) const override { return getAsString (value, preferHex); }
|
||||
String getCodeValue (bool preferHex) const override { return getAsCode (value, preferHex); }
|
||||
String getOriginalStringValue (bool preferHex) const override { return getAsString (originalValue, preferHex); }
|
||||
void setStringValue (const String& s) override { setFromString (value, s); }
|
||||
bool isString() const override { return isStringType<Type>::value; }
|
||||
|
||||
Type value, originalValue;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (LiveValue)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class JUCE_API ValueList : private AsyncUpdater,
|
||||
private DeletedAtShutdown
|
||||
{
|
||||
public:
|
||||
ValueList();
|
||||
~ValueList() override;
|
||||
|
||||
JUCE_DECLARE_SINGLETON_INLINE (ValueList, false)
|
||||
|
||||
template <typename Type>
|
||||
LiveValue<Type>& getValue (const char* file, int line, const Type& initialValue)
|
||||
{
|
||||
const ScopedLock sl (lock);
|
||||
using ValueType = LiveValue<Type>;
|
||||
|
||||
for (auto* v : values)
|
||||
if (v->sourceLine == line && v->sourceFile == file)
|
||||
return *static_cast<ValueType*> (v);
|
||||
|
||||
auto v = new ValueType (file, line, initialValue);
|
||||
addValue (v);
|
||||
return *v;
|
||||
}
|
||||
|
||||
private:
|
||||
OwnedArray<LiveValueBase> values;
|
||||
OwnedArray<CodeDocument> documents;
|
||||
Array<File> documentFiles;
|
||||
class EditorWindow;
|
||||
Component::SafePointer<EditorWindow> editorWindow;
|
||||
CriticalSection lock;
|
||||
|
||||
CodeDocument& getDocument (const File&);
|
||||
void addValue (LiveValueBase*);
|
||||
void handleAsyncUpdate() override;
|
||||
};
|
||||
|
||||
template <typename Type>
|
||||
inline LiveValue<Type>& getValue (const char* file, int line, const Type& initialValue)
|
||||
{
|
||||
// If you hit this assertion then the __FILE__ macro is providing a
|
||||
// relative path instead of an absolute path. On Windows this will be
|
||||
// a path relative to the build directory rather than the currently
|
||||
// running application. To fix this you must compile with the /FC flag.
|
||||
jassert (File::isAbsolutePath (file));
|
||||
|
||||
return ValueList::getInstance()->getValue (file, line, initialValue);
|
||||
}
|
||||
|
||||
inline LiveValue<String>& getValue (const char* file, int line, const char* initialValue)
|
||||
{
|
||||
return getValue (file, line, String (initialValue));
|
||||
}
|
||||
|
||||
} // namespace juce::LiveConstantEditor
|
||||
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
#if JUCE_ENABLE_LIVE_CONSTANT_EDITOR || DOXYGEN
|
||||
/**
|
||||
This macro wraps a primitive constant value in some cunning boilerplate code that allows
|
||||
its value to be interactively tweaked in a popup window while your application is running.
|
||||
|
||||
In a release build, this macro disappears and is replaced by only the constant that it
|
||||
wraps, but if JUCE_ENABLE_LIVE_CONSTANT_EDITOR is enabled, it injects a class wrapper
|
||||
that automatically pops-up a window containing an editor that allows the value to be
|
||||
tweaked at run-time. The editor window will also force all visible components to be
|
||||
resized and repainted whenever a value is changed, so that if you use this to wrap
|
||||
a colour or layout parameter, you'll be able to immediately see the effects of changing it.
|
||||
|
||||
The editor will also load the original source-file that contains each JUCE_LIVE_CONSTANT
|
||||
macro, and will display a preview of the modified source code as you adjust the values.
|
||||
|
||||
Things to note:
|
||||
|
||||
- Only one of these per line! The __FILE__ and __LINE__ macros are used to identify
|
||||
the value, so things will get confused if you have more than one per line
|
||||
- Obviously because it needs to load the source code based on the __FILE__ macro,
|
||||
it'll only work if the source files are stored locally in the same location as they
|
||||
were when you compiled the program.
|
||||
- It's only designed to cope with simple types: primitives, string literals, and
|
||||
the Colour class, so if you try using it for other classes or complex expressions,
|
||||
good luck!
|
||||
- The editor window will get popped up whenever a new value is used for the first
|
||||
time. You can close the window, but there's no way to get it back without restarting
|
||||
the app!
|
||||
|
||||
e.g. in this example the colours, font size, and text used in the paint method can
|
||||
all be adjusted live:
|
||||
@code
|
||||
void MyComp::paint (Graphics& g) override
|
||||
{
|
||||
g.fillAll (JUCE_LIVE_CONSTANT (Colour (0xffddddff)));
|
||||
|
||||
Colour fontColour = JUCE_LIVE_CONSTANT (Colour (0xff005500));
|
||||
float fontSize = JUCE_LIVE_CONSTANT (16.0f);
|
||||
|
||||
g.setColour (fontColour);
|
||||
g.setFont (fontSize);
|
||||
|
||||
g.drawFittedText (JUCE_LIVE_CONSTANT ("Hello world!"),
|
||||
getLocalBounds(), Justification::centred, 2);
|
||||
}
|
||||
@endcode
|
||||
*/
|
||||
#define JUCE_LIVE_CONSTANT(initialValue) \
|
||||
(juce::LiveConstantEditor::getValue (__FILE__, __LINE__ - 1, initialValue).get())
|
||||
#else
|
||||
#define JUCE_LIVE_CONSTANT(initialValue) \
|
||||
(initialValue)
|
||||
#endif
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
PreferencesPanel::PreferencesPanel()
|
||||
: buttonSize (70)
|
||||
{
|
||||
}
|
||||
|
||||
PreferencesPanel::~PreferencesPanel()
|
||||
{
|
||||
}
|
||||
|
||||
int PreferencesPanel::getButtonSize() const noexcept
|
||||
{
|
||||
return buttonSize;
|
||||
}
|
||||
|
||||
void PreferencesPanel::setButtonSize (int newSize)
|
||||
{
|
||||
buttonSize = newSize;
|
||||
resized();
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void PreferencesPanel::addSettingsPage (const String& title,
|
||||
const Drawable* icon,
|
||||
const Drawable* overIcon,
|
||||
const Drawable* downIcon)
|
||||
{
|
||||
auto* button = new DrawableButton (title, DrawableButton::ImageAboveTextLabel);
|
||||
buttons.add (button);
|
||||
|
||||
button->setImages (icon, overIcon, downIcon);
|
||||
button->setRadioGroupId (1);
|
||||
button->onClick = [this] { clickedPage(); };
|
||||
button->setClickingTogglesState (true);
|
||||
button->setWantsKeyboardFocus (false);
|
||||
addAndMakeVisible (button);
|
||||
|
||||
resized();
|
||||
|
||||
if (currentPage == nullptr)
|
||||
setCurrentPage (title);
|
||||
}
|
||||
|
||||
void PreferencesPanel::addSettingsPage (const String& title, const void* imageData, int imageDataSize)
|
||||
{
|
||||
DrawableImage icon, iconOver, iconDown;
|
||||
icon.setImage (ImageCache::getFromMemory (imageData, imageDataSize));
|
||||
|
||||
iconOver.setImage (ImageCache::getFromMemory (imageData, imageDataSize));
|
||||
iconOver.setOverlayColour (Colours::black.withAlpha (0.12f));
|
||||
|
||||
iconDown.setImage (ImageCache::getFromMemory (imageData, imageDataSize));
|
||||
iconDown.setOverlayColour (Colours::black.withAlpha (0.25f));
|
||||
|
||||
addSettingsPage (title, &icon, &iconOver, &iconDown);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void PreferencesPanel::showInDialogBox (const String& dialogTitle, int dialogWidth, int dialogHeight, Colour backgroundColour)
|
||||
{
|
||||
setSize (dialogWidth, dialogHeight);
|
||||
|
||||
DialogWindow::LaunchOptions o;
|
||||
o.content.setNonOwned (this);
|
||||
o.dialogTitle = dialogTitle;
|
||||
o.dialogBackgroundColour = backgroundColour;
|
||||
o.escapeKeyTriggersCloseButton = false;
|
||||
o.useNativeTitleBar = false;
|
||||
o.resizable = false;
|
||||
|
||||
o.launchAsync();
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void PreferencesPanel::resized()
|
||||
{
|
||||
for (int i = 0; i < buttons.size(); ++i)
|
||||
buttons.getUnchecked (i)->setBounds (i * buttonSize, 0, buttonSize, buttonSize);
|
||||
|
||||
if (currentPage != nullptr)
|
||||
currentPage->setBounds (getLocalBounds().withTop (buttonSize + 5));
|
||||
}
|
||||
|
||||
void PreferencesPanel::paint (Graphics& g)
|
||||
{
|
||||
g.setColour (Colours::grey);
|
||||
g.fillRect (0, buttonSize + 2, getWidth(), 1);
|
||||
}
|
||||
|
||||
void PreferencesPanel::setCurrentPage (const String& pageName)
|
||||
{
|
||||
if (currentPageName != pageName)
|
||||
{
|
||||
currentPageName = pageName;
|
||||
|
||||
currentPage.reset();
|
||||
currentPage.reset (createComponentForPage (pageName));
|
||||
|
||||
if (currentPage != nullptr)
|
||||
{
|
||||
addAndMakeVisible (currentPage.get());
|
||||
currentPage->toBack();
|
||||
resized();
|
||||
}
|
||||
|
||||
for (auto* b : buttons)
|
||||
{
|
||||
if (b->getName() == pageName)
|
||||
{
|
||||
b->setToggleState (true, dontSendNotification);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PreferencesPanel::clickedPage()
|
||||
{
|
||||
for (auto* b : buttons)
|
||||
{
|
||||
if (b->getToggleState())
|
||||
{
|
||||
setCurrentPage (b->getName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A component with a set of buttons at the top for changing between pages of
|
||||
preferences.
|
||||
|
||||
This is just a handy way of writing a Mac-style preferences panel where you
|
||||
have a row of buttons along the top for the different preference categories,
|
||||
each button having an icon above its name. Clicking these will show an
|
||||
appropriate prefs page below it.
|
||||
|
||||
You can either put one of these inside your own component, or just use the
|
||||
showInDialogBox() method to show it in a window and run it modally.
|
||||
|
||||
To use it, just add a set of named pages with the addSettingsPage() method,
|
||||
and implement the createComponentForPage() method to create suitable components
|
||||
for each of these pages.
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API PreferencesPanel : public Component
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates an empty panel.
|
||||
|
||||
Use addSettingsPage() to add some pages to it in your constructor.
|
||||
*/
|
||||
PreferencesPanel();
|
||||
|
||||
/** Destructor. */
|
||||
~PreferencesPanel() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Creates a page using a set of drawables to define the page's icon.
|
||||
|
||||
Note that the other version of this method is much easier if you're using
|
||||
an image instead of a custom drawable.
|
||||
|
||||
@param pageTitle the name of this preferences page - you'll need to
|
||||
make sure your createComponentForPage() method creates
|
||||
a suitable component when it is passed this name
|
||||
@param normalIcon the drawable to display in the page's button normally
|
||||
@param overIcon the drawable to display in the page's button when the mouse is over
|
||||
@param downIcon the drawable to display in the page's button when the button is down
|
||||
@see DrawableButton
|
||||
*/
|
||||
void addSettingsPage (const String& pageTitle,
|
||||
const Drawable* normalIcon,
|
||||
const Drawable* overIcon,
|
||||
const Drawable* downIcon);
|
||||
|
||||
/** Creates a page using a set of drawables to define the page's icon.
|
||||
|
||||
The other version of this method gives you more control over the icon, but this
|
||||
one is much easier if you're just loading it from a file.
|
||||
|
||||
@param pageTitle the name of this preferences page - you'll need to
|
||||
make sure your createComponentForPage() method creates
|
||||
a suitable component when it is passed this name
|
||||
@param imageData a block of data containing an image file, e.g. a jpeg, png or gif.
|
||||
For this to look good, you'll probably want to use a nice
|
||||
transparent png file.
|
||||
@param imageDataSize the size of the image data, in bytes
|
||||
*/
|
||||
void addSettingsPage (const String& pageTitle,
|
||||
const void* imageData,
|
||||
int imageDataSize);
|
||||
|
||||
/** Utility method to display this panel in a DialogWindow.
|
||||
|
||||
Calling this will create a DialogWindow containing this panel with the
|
||||
given size and title, and will run it modally, returning when the user
|
||||
closes the dialog box.
|
||||
*/
|
||||
void showInDialogBox (const String& dialogTitle,
|
||||
int dialogWidth,
|
||||
int dialogHeight,
|
||||
Colour backgroundColour = Colours::white);
|
||||
|
||||
//==============================================================================
|
||||
/** Subclasses must override this to return a component for each preferences page.
|
||||
|
||||
The subclass should return a pointer to a new component representing the named
|
||||
page, which the panel will then display.
|
||||
|
||||
The panel will delete the component later when the user goes to another page
|
||||
or deletes the panel.
|
||||
*/
|
||||
virtual Component* createComponentForPage (const String& pageName) = 0;
|
||||
|
||||
//==============================================================================
|
||||
/** Changes the current page being displayed. */
|
||||
void setCurrentPage (const String& pageName);
|
||||
|
||||
/** Returns the size of the buttons shown along the top. */
|
||||
int getButtonSize() const noexcept;
|
||||
|
||||
/** Changes the size of the buttons shown along the top. */
|
||||
void setButtonSize (int newSize);
|
||||
|
||||
//==============================================================================
|
||||
/** @internal */
|
||||
void resized() override;
|
||||
/** @internal */
|
||||
void paint (Graphics&) override;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
String currentPageName;
|
||||
std::unique_ptr<Component> currentPage;
|
||||
OwnedArray<DrawableButton> buttons;
|
||||
int buttonSize;
|
||||
|
||||
void clickedPage();
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PreferencesPanel)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,325 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
#if ! JUCE_PUSH_NOTIFICATIONS_IMPL
|
||||
|
||||
struct PushNotifications::Impl
|
||||
{
|
||||
explicit Impl (PushNotifications& o) : owner (o) {}
|
||||
|
||||
void requestPermissionsWithSettings (const Settings&) const
|
||||
{
|
||||
owner.listeners.call ([] (Listener& l) { l.notificationSettingsReceived ({}); });
|
||||
}
|
||||
|
||||
void requestSettingsUsed() const
|
||||
{
|
||||
owner.listeners.call ([] (Listener& l) { l.notificationSettingsReceived ({}); });
|
||||
}
|
||||
|
||||
bool areNotificationsEnabled() const { return false; }
|
||||
void getDeliveredNotifications() const {}
|
||||
void removeAllDeliveredNotifications() const {}
|
||||
String getDeviceToken() const { return {}; }
|
||||
void setupChannels (const Array<ChannelGroup>&, const Array<Channel>&) const {}
|
||||
void getPendingLocalNotifications() const {}
|
||||
void removeAllPendingLocalNotifications() const {}
|
||||
void subscribeToTopic (const String&) const {}
|
||||
void unsubscribeFromTopic (const String&) const {}
|
||||
void sendLocalNotification (const Notification&) const {}
|
||||
void removeDeliveredNotification (const String&) const {}
|
||||
void removePendingLocalNotification (const String&) const {}
|
||||
void sendUpstreamMessage (const String&,
|
||||
const String&,
|
||||
const String&,
|
||||
const String&,
|
||||
int,
|
||||
const StringPairArray&) const {}
|
||||
|
||||
private:
|
||||
PushNotifications& owner;
|
||||
};
|
||||
|
||||
bool PushNotifications::Notification::isValid() const noexcept { return true; }
|
||||
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
PushNotifications::Notification::Notification (const Notification& other)
|
||||
: identifier (other.identifier),
|
||||
title (other.title),
|
||||
body (other.body),
|
||||
subtitle (other.subtitle),
|
||||
groupId (other.groupId),
|
||||
badgeNumber (other.badgeNumber),
|
||||
soundToPlay (other.soundToPlay),
|
||||
properties (other.properties),
|
||||
category (other.category),
|
||||
triggerIntervalSec (other.triggerIntervalSec),
|
||||
repeat (other.repeat),
|
||||
icon (other.icon),
|
||||
channelId (other.channelId),
|
||||
largeIcon (other.largeIcon),
|
||||
tickerText (other.tickerText),
|
||||
actions (other.actions),
|
||||
progress (other.progress),
|
||||
person (other.person),
|
||||
type (other.type),
|
||||
priority (other.priority),
|
||||
lockScreenAppearance (other.lockScreenAppearance),
|
||||
publicVersion (other.publicVersion.get() != nullptr ? new Notification (*other.publicVersion) : nullptr),
|
||||
groupSortKey (other.groupSortKey),
|
||||
groupSummary (other.groupSummary),
|
||||
accentColour (other.accentColour),
|
||||
ledColour (other.ledColour),
|
||||
ledBlinkPattern (other.ledBlinkPattern),
|
||||
vibrationPattern (other.vibrationPattern),
|
||||
shouldAutoCancel (other.shouldAutoCancel),
|
||||
localOnly (other.localOnly),
|
||||
ongoing (other.ongoing),
|
||||
alertOnlyOnce (other.alertOnlyOnce),
|
||||
timestampVisibility (other.timestampVisibility),
|
||||
badgeIconType (other.badgeIconType),
|
||||
groupAlertBehaviour (other.groupAlertBehaviour),
|
||||
timeoutAfterMs (other.timeoutAfterMs)
|
||||
{
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
PushNotifications::PushNotifications()
|
||||
: pimpl (new Impl (*this))
|
||||
{
|
||||
}
|
||||
|
||||
PushNotifications::~PushNotifications() { clearSingletonInstance(); }
|
||||
|
||||
void PushNotifications::addListener (Listener* l) { listeners.add (l); }
|
||||
void PushNotifications::removeListener (Listener* l) { listeners.remove (l); }
|
||||
|
||||
void PushNotifications::requestPermissionsWithSettings (const Settings& settings)
|
||||
{
|
||||
pimpl->requestPermissionsWithSettings (settings);
|
||||
}
|
||||
|
||||
void PushNotifications::requestSettingsUsed()
|
||||
{
|
||||
pimpl->requestSettingsUsed();
|
||||
}
|
||||
|
||||
bool PushNotifications::areNotificationsEnabled() const
|
||||
{
|
||||
return pimpl->areNotificationsEnabled();
|
||||
}
|
||||
|
||||
void PushNotifications::getDeliveredNotifications() const
|
||||
{
|
||||
pimpl->getDeliveredNotifications();
|
||||
}
|
||||
|
||||
void PushNotifications::removeAllDeliveredNotifications()
|
||||
{
|
||||
pimpl->removeAllDeliveredNotifications();
|
||||
}
|
||||
|
||||
String PushNotifications::getDeviceToken() const
|
||||
{
|
||||
return pimpl->getDeviceToken();
|
||||
}
|
||||
|
||||
void PushNotifications::setupChannels (const Array<ChannelGroup>& groups,
|
||||
const Array<Channel>& channels)
|
||||
{
|
||||
pimpl->setupChannels (groups, channels);
|
||||
}
|
||||
|
||||
void PushNotifications::getPendingLocalNotifications() const
|
||||
{
|
||||
pimpl->getPendingLocalNotifications();
|
||||
}
|
||||
|
||||
void PushNotifications::removeAllPendingLocalNotifications()
|
||||
{
|
||||
pimpl->removeAllPendingLocalNotifications();
|
||||
}
|
||||
|
||||
void PushNotifications::subscribeToTopic (const String& topic)
|
||||
{
|
||||
pimpl->subscribeToTopic (topic);
|
||||
}
|
||||
|
||||
void PushNotifications::unsubscribeFromTopic (const String& topic)
|
||||
{
|
||||
pimpl->unsubscribeFromTopic (topic);
|
||||
}
|
||||
|
||||
void PushNotifications::sendLocalNotification (const Notification& n)
|
||||
{
|
||||
pimpl->sendLocalNotification (n);
|
||||
}
|
||||
|
||||
void PushNotifications::removeDeliveredNotification (const String& identifier)
|
||||
{
|
||||
pimpl->removeDeliveredNotification (identifier);
|
||||
}
|
||||
|
||||
void PushNotifications::removePendingLocalNotification (const String& identifier)
|
||||
{
|
||||
pimpl->removePendingLocalNotification (identifier);
|
||||
}
|
||||
|
||||
void PushNotifications::sendUpstreamMessage (const String& serverSenderId,
|
||||
const String& collapseKey,
|
||||
const String& messageId,
|
||||
const String& messageType,
|
||||
int timeToLive,
|
||||
const StringPairArray& additionalData)
|
||||
{
|
||||
pimpl->sendUpstreamMessage (serverSenderId,
|
||||
collapseKey,
|
||||
messageId,
|
||||
messageType,
|
||||
timeToLive,
|
||||
additionalData);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void PushNotifications::Listener::notificationSettingsReceived ([[maybe_unused]] const Settings& settings) {}
|
||||
void PushNotifications::Listener::pendingLocalNotificationsListReceived ([[maybe_unused]] const Array<Notification>& notifications) {}
|
||||
void PushNotifications::Listener::handleNotification ([[maybe_unused]] bool isLocalNotification,
|
||||
[[maybe_unused]] const Notification& notification) {}
|
||||
void PushNotifications::Listener::handleNotificationAction ([[maybe_unused]] bool isLocalNotification,
|
||||
[[maybe_unused]] const Notification& notification,
|
||||
[[maybe_unused]] const String& actionIdentifier,
|
||||
[[maybe_unused]] const String& optionalResponse) {}
|
||||
void PushNotifications::Listener::localNotificationDismissedByUser ([[maybe_unused]] const Notification& notification) {}
|
||||
void PushNotifications::Listener::deliveredNotificationsListReceived ([[maybe_unused]] const Array<Notification>& notifications) {}
|
||||
void PushNotifications::Listener::deviceTokenRefreshed ([[maybe_unused]] const String& token) {}
|
||||
void PushNotifications::Listener::remoteNotificationsDeleted() {}
|
||||
void PushNotifications::Listener::upstreamMessageSent ([[maybe_unused]] const String& messageId) {}
|
||||
void PushNotifications::Listener::upstreamMessageSendingError ([[maybe_unused]] const String& messageId,
|
||||
[[maybe_unused]] const String& error) {}
|
||||
|
||||
//==============================================================================
|
||||
void privatePostSystemNotification (const String& notificationTitle, const String& notificationBody);
|
||||
void privatePostSystemNotification ([[maybe_unused]] const String& notificationTitle,
|
||||
[[maybe_unused]] const String& notificationBody)
|
||||
{
|
||||
#if JUCE_PUSH_NOTIFICATIONS
|
||||
#if JUCE_ANDROID || JUCE_IOS || JUCE_MAC
|
||||
auto* notificationsInstance = PushNotifications::getInstance();
|
||||
|
||||
if (notificationsInstance == nullptr)
|
||||
return;
|
||||
|
||||
#if JUCE_ANDROID
|
||||
notificationsInstance->requestPermissionsWithSettings ({});
|
||||
|
||||
static auto channels = std::invoke ([]() -> Array<PushNotifications::Channel>
|
||||
{
|
||||
PushNotifications::Channel chan;
|
||||
|
||||
chan.identifier = "1";
|
||||
chan.name = "Notifications";
|
||||
chan.description = "Accessibility notifications";
|
||||
chan.groupId = "accessibility";
|
||||
chan.ledColour = Colours::yellow;
|
||||
chan.canShowBadge = true;
|
||||
chan.enableLights = true;
|
||||
chan.enableVibration = true;
|
||||
chan.soundToPlay = URL ("default_os_sound");
|
||||
chan.vibrationPattern = { 1000, 1000 };
|
||||
|
||||
return { chan };
|
||||
});
|
||||
|
||||
notificationsInstance->setupChannels ({ PushNotifications::ChannelGroup { "accessibility", "accessibility" } },
|
||||
channels);
|
||||
#else
|
||||
static auto settings = std::invoke ([]
|
||||
{
|
||||
PushNotifications::Settings s;
|
||||
s.allowAlert = true;
|
||||
s.allowBadge = true;
|
||||
s.allowSound = true;
|
||||
|
||||
#if JUCE_IOS
|
||||
PushNotifications::Settings::Category c;
|
||||
c.identifier = "Accessibility";
|
||||
|
||||
s.categories = { c };
|
||||
#endif
|
||||
|
||||
return s;
|
||||
});
|
||||
|
||||
notificationsInstance->requestPermissionsWithSettings (settings);
|
||||
#endif
|
||||
|
||||
const auto notification = std::invoke ([¬ificationTitle, ¬ificationBody]
|
||||
{
|
||||
PushNotifications::Notification n;
|
||||
|
||||
n.identifier = String (Random::getSystemRandom().nextInt());
|
||||
n.title = notificationTitle;
|
||||
n.body = notificationBody;
|
||||
|
||||
#if JUCE_IOS
|
||||
n.category = "Accessibility";
|
||||
#elif JUCE_ANDROID
|
||||
n.channelId = "1";
|
||||
n.icon = "accessibilitynotificationicon";
|
||||
#endif
|
||||
|
||||
return n;
|
||||
});
|
||||
|
||||
if (notification.isValid())
|
||||
notificationsInstance->sendLocalNotification (notification);
|
||||
|
||||
#else
|
||||
SystemTrayIconComponent systemTrayIcon;
|
||||
|
||||
Image im (Image::ARGB, 128, 128, true);
|
||||
systemTrayIcon.setIconImage (im, im);
|
||||
|
||||
systemTrayIcon.showInfoBubble (notificationTitle, notificationBody);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,710 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
/** Singleton class responsible for push notifications functionality. Both remote and
|
||||
local notifications are supported. To get information about notifications,
|
||||
register a listener on your application startup. It is best to register the
|
||||
listener as soon as possible, because your application can be launched from
|
||||
a push notification too.
|
||||
|
||||
To send a local notification create an instance of Notification, fill the necessary
|
||||
fields and call PushNotifications::sendLocalNotification(). When receiving local or
|
||||
remote notifications, inspect the Notification's fields for notification details.
|
||||
Bear in mind that some fields will not be available when receiving a remote
|
||||
notification.
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API PushNotifications : private DeletedAtShutdown
|
||||
{
|
||||
public:
|
||||
#ifndef DOXYGEN
|
||||
JUCE_DECLARE_SINGLETON_INLINE (PushNotifications, false)
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
/** Represents a notification that can be sent or received. */
|
||||
struct Notification
|
||||
{
|
||||
Notification() = default;
|
||||
Notification (const Notification& other);
|
||||
|
||||
/** Checks whether a given notification is correctly configured for a given OS. */
|
||||
bool isValid() const noexcept;
|
||||
|
||||
/** Represents an action on a notification that can be presented as a button or a text input.
|
||||
On Android, each notification has its action specified explicitly, on iOS you configure an
|
||||
allowed set of actions on startup and pack them into categories (see Settings).
|
||||
*/
|
||||
struct Action
|
||||
{
|
||||
/** Controls the appearance of this action. */
|
||||
enum Style
|
||||
{
|
||||
button, /**< Show this action as a button. */
|
||||
text /**< Show this action as a text input field (on Android API 20 or higher is required). */
|
||||
};
|
||||
|
||||
/** @name Common fields */
|
||||
/**@{*/
|
||||
Style style = button;
|
||||
String title; /**< Required. the name of the action displayed to the user. */
|
||||
String textInputPlaceholder; /**< Optional: placeholder text for text input notification.
|
||||
Note that it will be ignored if button style is used. */
|
||||
var parameters; /**< Optional: additional parameters that can be passed. */
|
||||
/**@}*/
|
||||
|
||||
/** @name iOS only fields */
|
||||
/**@{*/
|
||||
String identifier; /**< Required: unique identifier. This should be one of the
|
||||
identifiers set with requestPermissionsWithSettings(). */
|
||||
bool triggerInBackground = false; /**< Whether the app can process the action in background. */
|
||||
bool destructive = false; /**< Whether to display the action as destructive. */
|
||||
String textInputButtonText; /**< Optional: Text displayed on text input notification
|
||||
button.
|
||||
Note that it will be ignored if style is set to Style::button. */
|
||||
/**@}*/
|
||||
|
||||
/** @name Android only fields */
|
||||
/**@{*/
|
||||
String icon; /**< Optional: name of an icon file (without an extension) to be used for
|
||||
this action. This must be the name of one of the image
|
||||
files included into resources when exporting an Android project
|
||||
(see "Extra Android Raw Resources" setting in Projucer).
|
||||
Note that not all Android versions support an icon for an action, though
|
||||
it is recommended to provide it nevertheless. */
|
||||
|
||||
StringArray allowedResponses; /**< Optional: a list of possible answers if the answer set is limited.
|
||||
When left empty, then the user will be able to input any text. */
|
||||
/**@}*/
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/** @name Common fields */
|
||||
/**@{*/
|
||||
|
||||
String identifier; /**< Required: unique id that can be used to later dismiss the notification
|
||||
(on iOS available from version 10). */
|
||||
|
||||
String title; /**< Required: the title of the notification, usually displayed in the first row. */
|
||||
String body; /**< Required: the content of the notification, usually displayed in the second row. */
|
||||
String subtitle; /**< Optional: additional text, that may be displayed e.g. in the third row or in the header
|
||||
area. Note that on Android, depending on OS version, this may fight for
|
||||
space with other components of the notification, so use this field
|
||||
judiciously. On iOS available from version 10. On Android available from API 16. */
|
||||
|
||||
String groupId; /**< Optional: allows the OS to visually group, collapse, and expand a set of notifications,
|
||||
note that OS may automatically group notifications if no groupId is specified. */
|
||||
|
||||
int badgeNumber = 0; /**< Optional: on platforms that support it, can set a number this notification represents. */
|
||||
URL soundToPlay; /**< Optional: empty when the notification should be silent. When the name is set to
|
||||
"default_os_sound", then a default sound will be used.
|
||||
|
||||
For a custom sound on OSX, set the URL to the name of a sound file (preferably without
|
||||
an extension) and place the sound file directly in bundle's "Resources" directory (you
|
||||
can use "Xcode Resource" tickbox in Projucer to achieve that), i.e. it cannot be in a
|
||||
subdirectory of "Resources" like "Resources/sound". Alternatively, if a sound file
|
||||
cannot be found in bundle's "Resources" directory, the OS may look for the sound in the
|
||||
following paths: "~/Library/Sounds", "/Library/Sounds", "/Network/Library/Sounds",
|
||||
"/System/Library/Sounds".
|
||||
|
||||
For a custom sound on iOS, set the URL to a relative path within your bundle, including
|
||||
file extension. For instance, if your bundle contains "sounds" folder with "my_sound.caf"
|
||||
file, then the URL should be "sounds/my_sound.caf".
|
||||
|
||||
For a custom sound on Android, set URL to the name of a raw resource file
|
||||
(without an extension) that was included when exporting an Android project in
|
||||
Projucer (see "Extra Android Raw Resources" setting). */
|
||||
|
||||
var properties; /**< Optional: collection of additional properties that may be passed as a dictionary. */
|
||||
|
||||
/**@}*/
|
||||
|
||||
//==============================================================================
|
||||
/** @name iOS only fields */
|
||||
/**@{*/
|
||||
|
||||
String category; /**< Required: determines set of actions that will appear (as per setup done
|
||||
in requestPermissionsWithSettings()). */
|
||||
double triggerIntervalSec = 0.; /**< Optional: specifies number of seconds before the notification should trigger. */
|
||||
bool repeat = false; /**< Optional: allows the notification to continuously retrigger after
|
||||
triggerIntervalSec seconds. */
|
||||
|
||||
/**@}*/
|
||||
|
||||
//==============================================================================
|
||||
/** @name Android only fields */
|
||||
/**@{*/
|
||||
|
||||
String icon; /**< Required: name of an icon file (without an extension) to be used for
|
||||
this notification. This must be the name of one of the image
|
||||
files included into resources when exporting an Android project
|
||||
(see "Extra Android Raw Resources" setting in Projucer). */
|
||||
|
||||
String channelId; /**< Required for Android API level 26 or above: specifies notification channel id. Refer to
|
||||
setupChannels(). Ignored on earlier Android versions. */
|
||||
|
||||
Image largeIcon; /**< Optional: an additional large icon displayed in the notification content view. */
|
||||
|
||||
String tickerText; /**< Optional: ticker text used for accessibility services. */
|
||||
|
||||
Array<Action> actions; /**< Optional: actions associated with the notification. Note that the OS may allow only a limited
|
||||
number of actions to be presented, so always present most important actions first.
|
||||
Available from Android API 16 or above. */
|
||||
|
||||
/** Used to represent a progress of some operation. */
|
||||
struct Progress
|
||||
{
|
||||
int max = 0; /**< Max possible value of a progress. A typical usecase is to set max to 100 and increment
|
||||
current's value as percentage complete. */
|
||||
int current = 0; /**< Current progress value, should be from 0 to max. */
|
||||
bool indeterminate = false; /**< If true, then the progress represents a continuing activity indicator with ongoing
|
||||
animation and no numeric value. */
|
||||
};
|
||||
|
||||
Progress progress; /**< Optional: set to default (0, 0, false), to disable progress display. */
|
||||
|
||||
/** Metadata that can be used by the OS to better handle the notification, depending on its priority. */
|
||||
enum Type
|
||||
{
|
||||
unspecified, /**< Category not set. */
|
||||
alarm, /**< Alarm or timer. */
|
||||
call, /**< Incoming voice/video call or similar. */
|
||||
email, /**< Async message like email. */
|
||||
error, /**< Error in background operation or authentication status. */
|
||||
event, /**< Calendar event. */
|
||||
message, /**< Incoming message (sms, instant message etc.). */
|
||||
taskProgress, /**< Progress for a long-running background operation. */
|
||||
promo, /**< Promotion or advertisement. */
|
||||
recommendation, /**< Specific, single thing related recommendation. */
|
||||
reminder, /**< User-scheduled reminder. */
|
||||
service, /**< Running background service. */
|
||||
social, /**< Social network or sharing update. */
|
||||
status, /**< Ongoing information about device or contextual status. */
|
||||
system, /**< System or device status update. */
|
||||
transport /**< Media transport control for playback. */
|
||||
};
|
||||
|
||||
/** Metadata used as a hint to the OS about the priority of the notification. */
|
||||
enum Priority
|
||||
{
|
||||
veryLow = -2,
|
||||
low = -1,
|
||||
medium = 0,
|
||||
high = 1,
|
||||
veryHigh = 2
|
||||
};
|
||||
|
||||
String person; /**< Optional: additional metadata used as a hint to OS that a notification is
|
||||
related to a specific person. Can be useful for instance messaging apps.
|
||||
Available from Android API 21 or above. */
|
||||
|
||||
Type type = unspecified; /**< Optional. Available from Android API 21 or above. */
|
||||
Priority priority = medium; /**< Optional. Available from Android API 16 or above. */
|
||||
|
||||
/** Describes how to show the notification when the screen is locked. Available from Android API 21 or above. */
|
||||
enum LockScreenAppearance
|
||||
{
|
||||
dontShow = -1, /**< The notification is not allowed on the lock screen */
|
||||
showPartially = 0, /**< Only some information is allowed on the lock screen */
|
||||
showCompletely = 1 /**< The entire notification is allowed on the lock screen */
|
||||
};
|
||||
|
||||
LockScreenAppearance lockScreenAppearance = showPartially; /**< Optional. */
|
||||
|
||||
std::unique_ptr<Notification> publicVersion; /**< Optional: if you set lockScreenAppearance to showPartially,
|
||||
then you can provide "public version" of your notification
|
||||
that will be displayed on the lock screen. This way you can
|
||||
control what information is visible when the screen is locked. */
|
||||
|
||||
String groupSortKey; /**< Optional: Used to order notifications within the same group. Available from Android API 20 or above. */
|
||||
bool groupSummary = false; /**< Optional: if true, then this notification will be a group summary of the group set with groupId.
|
||||
Available from Android API 20 or above. */
|
||||
|
||||
Colour accentColour; /**< Optional: sets accent colour. The default colour will be used if accentColour is not set.
|
||||
Available from Android API 21 or above. */
|
||||
Colour ledColour; /**< Optional: Sets the led colour. The hardware will do its best to approximate the colour.
|
||||
The default colour will be used if ledColour is not set. */
|
||||
|
||||
/** Allows to control the time the device's led is on and off. */
|
||||
struct LedBlinkPattern
|
||||
{
|
||||
int msToBeOn = 0; /**< The led will be on for the given number of milliseconds, after which it will turn off. */
|
||||
int msToBeOff = 0; /**< The led will be off for the given number of milliseconds, after which it will turn on. */
|
||||
};
|
||||
|
||||
LedBlinkPattern ledBlinkPattern; /**< Optional. */
|
||||
|
||||
Array<int> vibrationPattern; /**< Optional: sets the vibration pattern in milliseconds. The first value indicates how long
|
||||
to wait until vibration starts. The second value indicates how long to vibrate. The third
|
||||
value will say how long to not vibrate and so on. For instance, if the pattern is:
|
||||
1000, 2000, 3000, 4000 - then one second after receiving a notification the device will
|
||||
vibrate for two seconds, followed by 3 seconds of no vibration and finally, 4 seconds of
|
||||
vibration. */
|
||||
|
||||
bool shouldAutoCancel = true; /**< Optional: If true, the notification will be automatically cancelled when a user clicks it in the panel. */
|
||||
|
||||
bool localOnly = true; /**< Optional: whether or not the notification should bridge to other devices.
|
||||
Available from Android API 20 or above. */
|
||||
|
||||
bool ongoing = false; /**< Optional: If true, then it cannot be dismissed by the user and it must be dismissed manually.
|
||||
Typically used for ongoing background tasks that the user is actively engaged with. To
|
||||
dismiss such notification, you need to call removeDeliveredNotification() or
|
||||
removeAllDeliveredNotifications(). */
|
||||
|
||||
bool alertOnlyOnce = false; /**< Optional: Set this flag if you would only like the sound, vibrate and ticker to be played if the notification
|
||||
is not already showing. */
|
||||
|
||||
/** Controls timestamp visibility and format. */
|
||||
enum TimestampVisibility
|
||||
{
|
||||
off, /**< Do not show timestamp. */
|
||||
normal, /**< Show normal timestamp. */
|
||||
chronometer, /**< Show chronometer as a stopwatch. Available from Android API 16 or above. */
|
||||
countDownChronometer /**< Set the chronometer to count down instead of counting up. Available from Android API 24 or above.*/
|
||||
};
|
||||
|
||||
TimestampVisibility timestampVisibility = normal; /**< Optional. */
|
||||
|
||||
/** Controls badge icon type to use if a notification is shown as a badge. Available from Android API 26 or above. */
|
||||
enum BadgeIconType
|
||||
{
|
||||
none,
|
||||
small,
|
||||
large
|
||||
};
|
||||
|
||||
BadgeIconType badgeIconType = large;
|
||||
|
||||
/** Controls sound and vibration behaviour for group notifications. Available from Android API 26 or above. */
|
||||
enum GroupAlertBehaviour
|
||||
{
|
||||
alertAll, /**< both child notifications and group notifications should produce sound and vibration. */
|
||||
AlertSummary, /**< all child notifications in the group should have no sound nor vibration, even
|
||||
if corresponding notification channel has sounds and vibrations enabled. */
|
||||
AlertChildren /**< summary notifications in the group should have no sound nor vibration, even if
|
||||
corresponding notification channel has sounds and vibrations enabled. */
|
||||
};
|
||||
|
||||
GroupAlertBehaviour groupAlertBehaviour = alertAll;
|
||||
|
||||
int timeoutAfterMs = 0; /**< specifies a duration in milliseconds, after which the notification should be
|
||||
cancelled, if it is not already cancelled. Available from Android API 26 or above. */
|
||||
/**@}*/
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/** Describes settings we want to use for current device. Note that at the
|
||||
moment this is only used on iOS and partially on OSX.
|
||||
|
||||
On OSX only allow* flags are used and they control remote notifications only.
|
||||
To control sound, alert and badge settings for local notifications on OSX,
|
||||
use Notifications settings in System Preferences.
|
||||
|
||||
To setup push notifications for current device, provide permissions required,
|
||||
as well as register categories of notifications you want to support. Each
|
||||
category needs to have a unique identifier and it can optionally have multiple
|
||||
actions. Each action also needs to have a unique identifier. The example setup
|
||||
may look as follows:
|
||||
|
||||
@code
|
||||
|
||||
using Action = PushNotifications::Settings::Action;
|
||||
using Category = PushNotifications::Settings::Category;
|
||||
|
||||
Action okAction;
|
||||
okAction.identifier = "okAction";
|
||||
okAction.title = "OK!";
|
||||
okAction.style = Action::button;
|
||||
okAction.triggerInBackground = true;
|
||||
|
||||
Action cancelAction;
|
||||
cancelAction.identifier = "cancelAction";
|
||||
cancelAction.title = "Cancel";
|
||||
cancelAction.style = Action::button;
|
||||
cancelAction.triggerInBackground = true;
|
||||
cancelAction.destructive = true;
|
||||
|
||||
Action textAction;
|
||||
textAction.identifier = "textAction";
|
||||
textAction.title = "Enter text";
|
||||
textAction.style = Action::text;
|
||||
textAction.triggerInBackground = true;
|
||||
textAction.destructive = false;
|
||||
textAction.textInputButtonText = "Ok";
|
||||
textAction.textInputPlaceholder = "Enter text...";
|
||||
|
||||
Category okCategory;
|
||||
okCategory.identifier = "okCategory";
|
||||
okCategory.actions = { okAction };
|
||||
|
||||
Category okCancelCategory;
|
||||
okCancelCategory.identifier = "okCancelCategory";
|
||||
okCancelCategory.actions = { okAction, cancelAction };
|
||||
|
||||
Category textCategory;
|
||||
textCategory.identifier = "textCategory";
|
||||
textCategory.actions = { textAction };
|
||||
textCategory.sendDismissAction = true;
|
||||
|
||||
PushNotifications::Settings settings;
|
||||
settings.allowAlert = true;
|
||||
settings.allowBadge = true;
|
||||
settings.allowSound = true;
|
||||
settings.categories = { okCategory, okCancelCategory, textCategory };
|
||||
|
||||
@endcode
|
||||
*/
|
||||
struct Settings
|
||||
{
|
||||
using Action = Notification::Action;
|
||||
|
||||
/** Describes a category of a notification. Each category has a unique identifier
|
||||
and a list of associated actions.
|
||||
Note that the OS may allow only a limited number of actions to be presented, so
|
||||
always present most important actions first.
|
||||
*/
|
||||
struct Category
|
||||
{
|
||||
String identifier; /**< unique identifier */
|
||||
Array<Action> actions; /**< optional list of actions within this category */
|
||||
bool sendDismissAction = false; /**< whether dismiss action will be sent to the app */
|
||||
};
|
||||
|
||||
bool allowSound = false; /**< whether the app should play a sound upon notification */
|
||||
bool allowAlert = false; /**< whether the app should present an alert upon notification */
|
||||
bool allowBadge = false; /**< whether the app may badge its icon upon notification */
|
||||
Array<Category> categories; /**< list of categories the app wants to support */
|
||||
};
|
||||
|
||||
/** Initialises push notifications on current device with the settings provided.
|
||||
Call this on your application startup and on iOS the first time the application starts,
|
||||
a user will be presented with a permission request dialog to give push notifications permission.
|
||||
Once a user responds, Listener::notificationSettingsReceived() will be called so that
|
||||
you can check what permissions where actually granted. The listener callback will be called
|
||||
on each subsequent startup too (provided you called requestPermissionsWithSettings() on previous
|
||||
application run). This way you can check what are current push notifications permissions.
|
||||
|
||||
Note that settings are currently only used on iOS. When calling on other platforms, Settings
|
||||
with no categories and all allow* flags set to true will be received in
|
||||
Listener::notificationSettingsReceived().
|
||||
|
||||
You can also call requestSettingsUsed() to explicitly ask for current settings.
|
||||
*/
|
||||
void requestPermissionsWithSettings (const Settings& settings);
|
||||
|
||||
/** Sends an asynchronous request to retrieve current settings that are currently in use.
|
||||
These can be exactly the same as used in requestPermissionsWithSettings(), but depending
|
||||
on user's subsequent changes in OS settings, the actual current settings may be
|
||||
different (e.g. user might have later decided to disable sounds).
|
||||
|
||||
Note that settings are currently only used on iOS and partially on OSX.
|
||||
|
||||
On OSX, only allow* flags are used and they refer to remote notifications only. For
|
||||
local notifications, refer to System Preferences.
|
||||
|
||||
When calling this function on other platforms, Settings with no categories and all allow*
|
||||
flags set to true will be received in Listener::notificationSettingsReceived().
|
||||
*/
|
||||
void requestSettingsUsed();
|
||||
|
||||
//==============================================================================
|
||||
/** Android API level 26 or higher only: Represents notification channel through which
|
||||
notifications will be sent. Starting from Android API level 26, you should call setupChannels()
|
||||
at the start of your application, before posting any notifications. Then, when sending notifications,
|
||||
assign a channel to each created notification.
|
||||
*/
|
||||
struct Channel
|
||||
{
|
||||
String identifier; /**< Required: Unique channel identifier. */
|
||||
String name; /**< Required: User facing name of the channel. */
|
||||
|
||||
/** Controls how interruptive the notification posted on this channel are. */
|
||||
enum Importance
|
||||
{
|
||||
none,
|
||||
min,
|
||||
low,
|
||||
normal,
|
||||
high,
|
||||
max
|
||||
};
|
||||
|
||||
Importance importance = normal; /**< Required. */
|
||||
Notification::LockScreenAppearance lockScreenAppearance = Notification::showPartially; /**< Optional. */
|
||||
|
||||
String description; /**< Optional: user visible description of the channel. */
|
||||
String groupId; /**< Required: group this channel belongs to (see ChannelGroup). */
|
||||
Colour ledColour; /**< Optional: sets the led colour for notifications in this channel. */
|
||||
bool bypassDoNotDisturb = false; /**< Optional: true if notifications in this channel can bypass do not disturb setting. */
|
||||
bool canShowBadge = false; /**< Optional: true if notifications in this channel can show badges in a Launcher application. */
|
||||
bool enableLights = false; /**< Optional: true if notifications in this channel should show lights (subject to hardware support). */
|
||||
bool enableVibration = false; /**< Optional: true if notifications in this channel should trigger vibrations. */
|
||||
|
||||
URL soundToPlay; /**< Optional: sound to play in this channel. See Notification::soundToPlay for more info. */
|
||||
Array<int> vibrationPattern; /**< Optional: vibration pattern for this channel. See Notification::vibrationPattern for more info. */
|
||||
};
|
||||
|
||||
/** Android API level 26 or higher only: represents a channel group. This allows for
|
||||
visual grouping of corresponding channels in notification settings presented to the user.
|
||||
At least one channel group has to be specified before notifications can be sent.
|
||||
*/
|
||||
struct ChannelGroup
|
||||
{
|
||||
String identifier; /**< Required: Unique channel group identifier. */
|
||||
String name; /**< Required: User visible name of the channel group. */
|
||||
};
|
||||
|
||||
/** Android API level 26 or higher only: configures notification channel groups and channels to be
|
||||
used in the app. These have to be setup before notifications can be sent on Android API
|
||||
level 26 or higher.
|
||||
*/
|
||||
void setupChannels (const Array<ChannelGroup>& groups, const Array<Channel>& channels);
|
||||
|
||||
//==============================================================================
|
||||
/** iOS only: sends an asynchronous request to retrieve a list of notifications that were
|
||||
scheduled and not yet delivered.
|
||||
|
||||
When the list is retrieved, Listener::pendingLocalNotificationsListReceived() will be called.
|
||||
*/
|
||||
void getPendingLocalNotifications() const;
|
||||
|
||||
/** Unschedules a pending local notification with a given identifier. */
|
||||
void removePendingLocalNotification (const String& identifier);
|
||||
|
||||
/** Unschedules all pending local notifications. iOS only. */
|
||||
void removeAllPendingLocalNotifications();
|
||||
|
||||
//==============================================================================
|
||||
/** Checks whether notifications are enabled for given application.
|
||||
On iOS and OSX this will always return true, use requestSettingsUsed() instead.
|
||||
*/
|
||||
bool areNotificationsEnabled() const;
|
||||
|
||||
/** On iOS as well as on Android, sends a local notification.
|
||||
This will refresh an existing notification if the same identifier is used as in
|
||||
a notification that was already sent and not yet responded by a user.
|
||||
*/
|
||||
void sendLocalNotification (const Notification& notification);
|
||||
|
||||
/** Sends a request for a list of notifications delivered. Such notifications are visible in the
|
||||
notification area on the device and they are still waiting for user action/response.
|
||||
When the request is finished Listener::deliveredNotificationsListReceived() will be called.
|
||||
|
||||
On iOS, iOS version 10 or higher is required. On Android, API level 18 or higher is required.
|
||||
For unsupported platforms, Listener::deliveredNotificationsListReceived() will return an empty array.
|
||||
*/
|
||||
void getDeliveredNotifications() const;
|
||||
|
||||
/** Removes a previously delivered notification. This can be useful for instance when the
|
||||
information in the notification becomes obsolete.
|
||||
*/
|
||||
void removeDeliveredNotification (const String& identifier);
|
||||
|
||||
/** Removes all notifications that were delivered. */
|
||||
void removeAllDeliveredNotifications();
|
||||
|
||||
//==============================================================================
|
||||
/** Retrieves current device token. Note, it is not a good idea to cache this token
|
||||
because it may change in the meantime. Always call this method to get the current
|
||||
token value.
|
||||
*/
|
||||
String getDeviceToken() const;
|
||||
|
||||
/** Android only: allows to subscribe to messages from a specific topic.
|
||||
So you could for instance subscribe this device to all "sports" topic messages
|
||||
to receive any remote notifications that have "sports" topic set.
|
||||
Refer to Firebase documentation for how to send topic messages.
|
||||
*/
|
||||
void subscribeToTopic (const String& topic);
|
||||
|
||||
/** Android only: allows to remove a topic subscription that was previously added with
|
||||
subscribeToTopic().
|
||||
*/
|
||||
void unsubscribeFromTopic (const String& topic);
|
||||
|
||||
/** Android only: sends an upstream message to your app server. The server must implement
|
||||
XMPP Connection Server protocol (refer to Firebase documentation).
|
||||
|
||||
@param serverSenderId Represents the sender. Consult your Firebase project
|
||||
settings to retrieve the sender id.
|
||||
|
||||
@param collapseKey Remote messages with the same collapse key that were not
|
||||
yet delivered will be collapsed into one, with the
|
||||
newest message replacing all the previous ones.
|
||||
Note that there may be a limit of maximum collapse keys
|
||||
used at the same time and beyond the limit (refer to
|
||||
Firebase documentation) it is not guaranteed which keys
|
||||
will be in use by the server.
|
||||
|
||||
@param messageId A unique message ID. Used in error callbacks and debugging.
|
||||
|
||||
@param messageType Message type.
|
||||
|
||||
@param timeToLive TTL in seconds. If 0, the message sending will be attempted
|
||||
immediately and it will be dropped if the device is not
|
||||
connected. Otherwise, the message will be queued for the
|
||||
period specified.
|
||||
|
||||
@param additionalData Collection of key-value pairs to be used as an additional
|
||||
data for the message.
|
||||
*/
|
||||
void sendUpstreamMessage (const String& serverSenderId,
|
||||
const String& collapseKey,
|
||||
const String& messageId,
|
||||
const String& messageType,
|
||||
int timeToLive,
|
||||
const StringPairArray& additionalData);
|
||||
|
||||
//==============================================================================
|
||||
/** Register a listener (ideally on application startup) to receive information about
|
||||
notifications received and any callbacks to async functions called.
|
||||
*/
|
||||
struct Listener
|
||||
{
|
||||
virtual ~Listener() = default;
|
||||
|
||||
/** This callback will be called after you call requestSettingsUsed() or
|
||||
requestPermissionsWithSettings().
|
||||
|
||||
Note that settings are currently only used on iOS. When called on other platforms, Settings
|
||||
with no categories and all allow flags set to true will be received in
|
||||
Listener::notificationSettingsReceived().
|
||||
*/
|
||||
virtual void notificationSettingsReceived (const Settings& settings);
|
||||
|
||||
/** Called when the list of pending notifications, requested by calling
|
||||
getPendingLocalNotifications() is returned.
|
||||
*/
|
||||
virtual void pendingLocalNotificationsListReceived (const Array<Notification>& notifications);
|
||||
|
||||
/** This can be called in multiple different situations, depending on the OS and the situation.
|
||||
|
||||
This will be called when a user presses on a notification
|
||||
|
||||
Note: On Android, if remote notification was received while the app was in the background and
|
||||
then user pressed on it, the notification object received in this callback will contain only
|
||||
"properties" member set. Hence, if you want to know what was the notification title, content
|
||||
etc, you need to set them as additional properties, so that you will be able to restore them
|
||||
from "properties" dictionary.
|
||||
|
||||
Note you can receive this callback on startup, if the application was launched from a notification.
|
||||
*/
|
||||
virtual void handleNotification (bool isLocalNotification, const Notification& notification);
|
||||
|
||||
/** This can be called when a user performs some action on the notification such as
|
||||
pressing on an action button or responding with a text input.
|
||||
Note that pressing on a notification area, i.e. not on an action button is not considered
|
||||
to be an action, and hence receivedNotification() will be called in that case.
|
||||
|
||||
Note you can receive this callback on startup, if the application was launched from a notification's action.
|
||||
|
||||
@param isLocalNotification If the notification is local
|
||||
@param notification The notification
|
||||
@param actionIdentifier A String identifying the action
|
||||
@param optionalResponse Text response a user inputs for notifications with a text input.
|
||||
Empty for notifications without a text input option.
|
||||
|
||||
*/
|
||||
virtual void handleNotificationAction (bool isLocalNotification,
|
||||
const Notification& notification,
|
||||
const String& actionIdentifier,
|
||||
const String& optionalResponse);
|
||||
|
||||
/** For iOS10 and Android, this can be also called when a user dismissed the notification before
|
||||
responding to it.
|
||||
*/
|
||||
virtual void localNotificationDismissedByUser (const Notification& notification);
|
||||
|
||||
/** Called after getDeliveredNotifications() request is fulfilled. Returns notifications
|
||||
that are visible in the notification area on the device and that are still waiting
|
||||
for a user action/response.
|
||||
|
||||
On iOS, iOS version 10 or higher is required. On Android, API level 18 or higher is required.
|
||||
For unsupported platforms, an empty array will be returned.
|
||||
*/
|
||||
virtual void deliveredNotificationsListReceived (const Array<Notification>& notifications);
|
||||
|
||||
/** Called whenever a token gets refreshed. You should monitor any token updates, because
|
||||
only the last token that is assigned to device is valid and can be used.
|
||||
*/
|
||||
virtual void deviceTokenRefreshed (const String& token);
|
||||
|
||||
/** Called when Firebase Cloud Messaging server deletes pending messages. This can happen when
|
||||
1) too many messages were sent to the server (hint: use collapsible messages).
|
||||
2) the devices hasn't been online in a long time (refer to Firebase documentation for
|
||||
the maximum time a message can be stored on FCM before expiring).
|
||||
*/
|
||||
virtual void remoteNotificationsDeleted();
|
||||
|
||||
/** Called when an upstream message sent with PushNotifications::sendUpstreamMessage() has been
|
||||
sent successfully.
|
||||
Bear in mind that in may take several minutes or more to receive this callback.
|
||||
*/
|
||||
virtual void upstreamMessageSent (const String& messageId);
|
||||
|
||||
/** Called when there was an error sending an upstream message with
|
||||
PushNotifications::sendUpstreamMessage().
|
||||
Bear in mind that in may take several minutes or more to receive this callback.
|
||||
*/
|
||||
virtual void upstreamMessageSendingError (const String& messageId, const String& error);
|
||||
};
|
||||
|
||||
void addListener (Listener* l);
|
||||
void removeListener (Listener* l);
|
||||
|
||||
private:
|
||||
PushNotifications();
|
||||
~PushNotifications() override;
|
||||
|
||||
ListenerList<PushNotifications::Listener> listeners;
|
||||
|
||||
#if JUCE_ANDROID
|
||||
friend bool juce_handleNotificationIntent (void*);
|
||||
|
||||
friend struct JuceFirebaseInstanceIdService;
|
||||
friend struct JuceFirebaseMessagingService;
|
||||
#endif
|
||||
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> pimpl;
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
RecentlyOpenedFilesList::RecentlyOpenedFilesList()
|
||||
: maxNumberOfItems (10)
|
||||
{
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void RecentlyOpenedFilesList::setMaxNumberOfItems (const int newMaxNumber)
|
||||
{
|
||||
maxNumberOfItems = jmax (1, newMaxNumber);
|
||||
|
||||
files.removeRange (maxNumberOfItems, getNumFiles());
|
||||
}
|
||||
|
||||
int RecentlyOpenedFilesList::getNumFiles() const
|
||||
{
|
||||
return files.size();
|
||||
}
|
||||
|
||||
File RecentlyOpenedFilesList::getFile (const int index) const
|
||||
{
|
||||
return File (files [index]);
|
||||
}
|
||||
|
||||
void RecentlyOpenedFilesList::clear()
|
||||
{
|
||||
files.clear();
|
||||
}
|
||||
|
||||
void RecentlyOpenedFilesList::addFile (const File& file)
|
||||
{
|
||||
removeFile (file);
|
||||
files.insert (0, file.getFullPathName());
|
||||
|
||||
setMaxNumberOfItems (maxNumberOfItems);
|
||||
}
|
||||
|
||||
void RecentlyOpenedFilesList::removeFile (const File& file)
|
||||
{
|
||||
files.removeString (file.getFullPathName());
|
||||
}
|
||||
|
||||
void RecentlyOpenedFilesList::removeNonExistentFiles()
|
||||
{
|
||||
for (int i = getNumFiles(); --i >= 0;)
|
||||
if (! getFile (i).exists())
|
||||
files.remove (i);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
int RecentlyOpenedFilesList::createPopupMenuItems (PopupMenu& menuToAddTo,
|
||||
const int baseItemId,
|
||||
const bool showFullPaths,
|
||||
const bool dontAddNonExistentFiles,
|
||||
const File** filesToAvoid)
|
||||
{
|
||||
int num = 0;
|
||||
|
||||
for (int i = 0; i < getNumFiles(); ++i)
|
||||
{
|
||||
const File f (getFile (i));
|
||||
|
||||
if ((! dontAddNonExistentFiles) || f.exists())
|
||||
{
|
||||
bool needsAvoiding = false;
|
||||
|
||||
if (filesToAvoid != nullptr)
|
||||
{
|
||||
for (const File** avoid = filesToAvoid; *avoid != nullptr; ++avoid)
|
||||
{
|
||||
if (f == **avoid)
|
||||
{
|
||||
needsAvoiding = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (! needsAvoiding)
|
||||
{
|
||||
menuToAddTo.addItem (baseItemId + i,
|
||||
showFullPaths ? f.getFullPathName()
|
||||
: f.getFileName());
|
||||
++num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
String RecentlyOpenedFilesList::toString() const
|
||||
{
|
||||
return files.joinIntoString ("\n");
|
||||
}
|
||||
|
||||
void RecentlyOpenedFilesList::restoreFromString (const String& stringifiedVersion)
|
||||
{
|
||||
clear();
|
||||
files.addLines (stringifiedVersion);
|
||||
|
||||
setMaxNumberOfItems (maxNumberOfItems);
|
||||
}
|
||||
|
||||
|
||||
//==============================================================================
|
||||
void RecentlyOpenedFilesList::registerRecentFileNatively ([[maybe_unused]] const File& file)
|
||||
{
|
||||
#if JUCE_MAC
|
||||
JUCE_AUTORELEASEPOOL
|
||||
{
|
||||
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL: createNSURLFromFile (file)];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void RecentlyOpenedFilesList::forgetRecentFileNatively ([[maybe_unused]] const File& file)
|
||||
{
|
||||
#if JUCE_MAC
|
||||
JUCE_AUTORELEASEPOOL
|
||||
{
|
||||
// for some reason, OSX doesn't provide a method to just remove a single file
|
||||
// from the recent list, so we clear them all and add them back excluding
|
||||
// the specified file
|
||||
|
||||
auto sharedDocController = [NSDocumentController sharedDocumentController];
|
||||
auto recentDocumentURLs = [sharedDocController recentDocumentURLs];
|
||||
|
||||
[sharedDocController clearRecentDocuments: nil];
|
||||
|
||||
auto* nsFile = createNSURLFromFile (file);
|
||||
|
||||
auto reverseEnumerator = [recentDocumentURLs reverseObjectEnumerator];
|
||||
|
||||
for (NSURL* url : reverseEnumerator)
|
||||
if (! [url isEqual:nsFile])
|
||||
[sharedDocController noteNewRecentDocumentURL:url];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void RecentlyOpenedFilesList::clearRecentFilesNatively()
|
||||
{
|
||||
#if JUCE_MAC
|
||||
JUCE_AUTORELEASEPOOL
|
||||
{
|
||||
[[NSDocumentController sharedDocumentController] clearRecentDocuments: nil];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Manages a set of files for use as a list of recently-opened documents.
|
||||
|
||||
This is a handy class for holding your list of recently-opened documents, with
|
||||
helpful methods for things like purging any non-existent files, automatically
|
||||
adding them to a menu, and making persistence easy.
|
||||
|
||||
@see File, FileBasedDocument
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API RecentlyOpenedFilesList
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates an empty list.
|
||||
*/
|
||||
RecentlyOpenedFilesList();
|
||||
|
||||
//==============================================================================
|
||||
/** Sets a limit for the number of files that will be stored in the list.
|
||||
|
||||
When addFile() is called, then if there is no more space in the list, the
|
||||
least-recently added file will be dropped.
|
||||
|
||||
@see getMaxNumberOfItems
|
||||
*/
|
||||
void setMaxNumberOfItems (int newMaxNumber);
|
||||
|
||||
/** Returns the number of items that this list will store.
|
||||
@see setMaxNumberOfItems
|
||||
*/
|
||||
int getMaxNumberOfItems() const noexcept { return maxNumberOfItems; }
|
||||
|
||||
/** Returns the number of files in the list.
|
||||
|
||||
The most recently added file is always at index 0.
|
||||
*/
|
||||
int getNumFiles() const;
|
||||
|
||||
/** Returns one of the files in the list.
|
||||
|
||||
The most recently added file is always at index 0.
|
||||
*/
|
||||
File getFile (int index) const;
|
||||
|
||||
/** Returns an array of all the absolute pathnames in the list.
|
||||
*/
|
||||
const StringArray& getAllFilenames() const noexcept { return files; }
|
||||
|
||||
/** Clears all the files from the list. */
|
||||
void clear();
|
||||
|
||||
/** Adds a file to the list.
|
||||
|
||||
The file will be added at index 0. If this file is already in the list, it will
|
||||
be moved up to index 0, but a file can only appear once in the list.
|
||||
|
||||
If the list already contains the maximum number of items that is permitted, the
|
||||
least-recently added file will be dropped from the end.
|
||||
*/
|
||||
void addFile (const File& file);
|
||||
|
||||
/** Removes a file from the list. */
|
||||
void removeFile (const File& file);
|
||||
|
||||
/** Checks each of the files in the list, removing any that don't exist.
|
||||
|
||||
You might want to call this after reloading a list of files, or before putting them
|
||||
on a menu.
|
||||
*/
|
||||
void removeNonExistentFiles();
|
||||
|
||||
/** Tells the OS to add a file to the OS-managed list of recent documents for this app.
|
||||
|
||||
Not all OSes maintain a list of recent files for an application, so this
|
||||
function will have no effect on some OSes. Currently it's just implemented for OSX.
|
||||
*/
|
||||
static void registerRecentFileNatively (const File& file);
|
||||
|
||||
/** Tells the OS to remove a file from the OS-managed list of recent documents for this app.
|
||||
|
||||
Not all OSes maintain a list of recent files for an application, so this
|
||||
function will have no effect on some OSes. Currently it's just implemented for OSX.
|
||||
*/
|
||||
static void forgetRecentFileNatively (const File& file);
|
||||
|
||||
/** Tells the OS to clear the OS-managed list of recent documents for this app.
|
||||
|
||||
Not all OSes maintain a list of recent files for an application, so this
|
||||
function will have no effect on some OSes. Currently it's just implemented for OSX.
|
||||
*/
|
||||
static void clearRecentFilesNatively();
|
||||
|
||||
//==============================================================================
|
||||
/** Adds entries to a menu, representing each of the files in the list.
|
||||
|
||||
This is handy for creating an "open recent file..." menu in your app. The
|
||||
menu items are numbered consecutively starting with the baseItemId value,
|
||||
and can either be added as complete pathnames, or just the last part of the
|
||||
filename.
|
||||
|
||||
If dontAddNonExistentFiles is true, then each file will be checked and only those
|
||||
that exist will be added.
|
||||
|
||||
If filesToAvoid is not a nullptr, then it is considered to be a zero-terminated array
|
||||
of pointers to file objects. Any files that appear in this list will not be added to
|
||||
the menu - the reason for this is that you might have a number of files already open,
|
||||
so might not want these to be shown in the menu.
|
||||
|
||||
It returns the number of items that were added.
|
||||
*/
|
||||
int createPopupMenuItems (PopupMenu& menuToAddItemsTo,
|
||||
int baseItemId,
|
||||
bool showFullPaths,
|
||||
bool dontAddNonExistentFiles,
|
||||
const File** filesToAvoid = nullptr);
|
||||
|
||||
//==============================================================================
|
||||
/** Returns a string that encapsulates all the files in the list.
|
||||
|
||||
The string that is returned can later be passed into restoreFromString() in
|
||||
order to recreate the list. This is handy for persisting your list, e.g. in
|
||||
a PropertiesFile object.
|
||||
|
||||
@see restoreFromString
|
||||
*/
|
||||
String toString() const;
|
||||
|
||||
/** Restores the list from a previously stringified version of the list.
|
||||
|
||||
Pass in a stringified version created with toString() in order to persist/restore
|
||||
your list.
|
||||
|
||||
@see toString
|
||||
*/
|
||||
void restoreFromString (const String& stringifiedVersion);
|
||||
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
StringArray files;
|
||||
int maxNumberOfItems;
|
||||
|
||||
JUCE_LEAK_DETECTOR (RecentlyOpenedFilesList)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
SplashScreen::SplashScreen (const String& title, const Image& image, bool useDropShadow)
|
||||
: Component (title),
|
||||
backgroundImage (image),
|
||||
clickCountToDelete (0)
|
||||
{
|
||||
// You must supply a valid image here!
|
||||
jassert (backgroundImage.isValid());
|
||||
|
||||
setOpaque (! backgroundImage.hasAlphaChannel());
|
||||
|
||||
#if JUCE_IOS || JUCE_ANDROID
|
||||
const bool useFullScreen = true;
|
||||
#else
|
||||
const bool useFullScreen = false;
|
||||
#endif
|
||||
|
||||
makeVisible (image.getWidth(), image.getHeight(), useDropShadow, useFullScreen);
|
||||
}
|
||||
|
||||
SplashScreen::SplashScreen (const String& title, int width, int height, bool useDropShadow)
|
||||
: Component (title),
|
||||
clickCountToDelete (0)
|
||||
{
|
||||
makeVisible (width, height, useDropShadow, false);
|
||||
}
|
||||
|
||||
void SplashScreen::makeVisible (int w, int h, bool useDropShadow, bool fullscreen)
|
||||
{
|
||||
clickCountToDelete = Desktop::getInstance().getMouseButtonClickCounter();
|
||||
creationTime = Time::getCurrentTime();
|
||||
|
||||
const Rectangle<int> screenSize = Desktop::getInstance().getDisplays().getPrimaryDisplay()->userArea;
|
||||
const int width = (fullscreen ? screenSize.getWidth() : w);
|
||||
const int height = (fullscreen ? screenSize.getHeight() : h);
|
||||
|
||||
setAlwaysOnTop (true);
|
||||
setVisible (true);
|
||||
centreWithSize (width, height);
|
||||
addToDesktop (useDropShadow ? ComponentPeer::windowHasDropShadow : 0);
|
||||
|
||||
if (fullscreen)
|
||||
getPeer()->setFullScreen (true);
|
||||
|
||||
toFront (false);
|
||||
}
|
||||
|
||||
SplashScreen::~SplashScreen() {}
|
||||
|
||||
void SplashScreen::deleteAfterDelay (RelativeTime timeout, bool removeOnMouseClick)
|
||||
{
|
||||
// Note that this method must be safe to call from non-GUI threads
|
||||
if (! removeOnMouseClick)
|
||||
clickCountToDelete = std::numeric_limits<int>::max();
|
||||
|
||||
minimumVisibleTime = timeout;
|
||||
|
||||
startTimer (50);
|
||||
}
|
||||
|
||||
void SplashScreen::paint (Graphics& g)
|
||||
{
|
||||
g.setOpacity (1.0f);
|
||||
g.drawImage (backgroundImage, getLocalBounds().toFloat(), RectanglePlacement (RectanglePlacement::fillDestination));
|
||||
}
|
||||
|
||||
void SplashScreen::timerCallback()
|
||||
{
|
||||
if (Time::getCurrentTime() > creationTime + minimumVisibleTime
|
||||
|| Desktop::getInstance().getMouseButtonClickCounter() > clickCountToDelete)
|
||||
delete this;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/** A component for showing a splash screen while your app starts up.
|
||||
|
||||
This will automatically position itself, and can be told to delete itself after
|
||||
being on-screen for a minimum length of time.
|
||||
|
||||
To use it, just create one of these in your JUCEApplicationBase::initialise() method,
|
||||
and when your initialisation tasks have finished running, call its deleteAfterDelay()
|
||||
method to make it automatically get rid of itself.
|
||||
|
||||
Note that although you could call deleteAfterDelay() as soon as you create the
|
||||
SplashScreen object, if you've got a long initialisation procedure, you probably
|
||||
don't want the splash to time-out and disappear before the initialisation has
|
||||
finished, which is why it makes sense to not call this method until the end of
|
||||
your init tasks.
|
||||
|
||||
E.g. @code
|
||||
|
||||
void MyApp::initialise (const String& commandLine)
|
||||
{
|
||||
splash = new SplashScreen ("Welcome to my app!",
|
||||
ImageFileFormat::loadFrom (File ("/foobar/splash.jpg")),
|
||||
true);
|
||||
|
||||
// now kick off your initialisation work on some kind of thread or task, and
|
||||
launchBackgroundInitialisationThread();
|
||||
}
|
||||
|
||||
void MyApp::myInitialisationWorkFinished()
|
||||
{
|
||||
// ..assuming this is some kind of callback method that is triggered when
|
||||
// your background initialisation threads have finished, and it's time to open
|
||||
// your main window, etc..
|
||||
|
||||
splash->deleteAfterDelay (RelativeTime::seconds (4), false);
|
||||
|
||||
...etc...
|
||||
}
|
||||
|
||||
@endcode
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API SplashScreen : public Component,
|
||||
private Timer,
|
||||
private DeletedAtShutdown
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates a SplashScreen object.
|
||||
|
||||
When called, the constructor will position the SplashScreen in the centre of the
|
||||
display, and after the time specified, it will automatically delete itself.
|
||||
|
||||
Bear in mind that if you call this during your JUCEApplicationBase::initialise()
|
||||
method and then block the message thread by performing some kind of task, then
|
||||
obviously neither your splash screen nor any other GUI will appear until you
|
||||
allow the message thread to resume and do its work. So if you have time-consuming
|
||||
tasks to do during startup, use a background thread for them.
|
||||
|
||||
After creating one of these (or your subclass of it), you should do your app's
|
||||
initialisation work, and then call the deleteAfterDelay() method to tell this object
|
||||
to delete itself after the user has had chance to get a good look at it.
|
||||
|
||||
If you're writing a custom splash screen class, there's another protected constructor
|
||||
that your subclass can call, which doesn't take an image.
|
||||
|
||||
@param title the name to give the component
|
||||
@param backgroundImage an image to draw on the component. The component's size
|
||||
will be set to the size of this image, and if the image is
|
||||
semi-transparent, the component will be made non-opaque
|
||||
@param useDropShadow if true, the window will have a drop shadow
|
||||
|
||||
*/
|
||||
SplashScreen (const String& title,
|
||||
const Image& backgroundImage,
|
||||
bool useDropShadow);
|
||||
|
||||
/** Destructor. */
|
||||
~SplashScreen() override;
|
||||
|
||||
/** Tells the component to auto-delete itself after a timeout period, or when the
|
||||
mouse is clicked.
|
||||
|
||||
You should call this after finishing your app's initialisation work.
|
||||
|
||||
Note that although you could call deleteAfterDelay() as soon as you create the
|
||||
SplashScreen object, if you've got a long initialisation procedure, you probably
|
||||
don't want the splash to time-out and disappear before your initialisation has
|
||||
finished, which is why it makes sense to not call this method and start the
|
||||
self-delete timer until you're ready.
|
||||
|
||||
It's safe to call this method from a non-GUI thread as long as there's no danger that
|
||||
the object may be being deleted at the same time.
|
||||
|
||||
@param minimumTotalTimeToDisplayFor how long the splash screen should stay visible for.
|
||||
Note that this time is measured from the construction-time of this
|
||||
object, not from the time that the deleteAfterDelay() method is
|
||||
called, so if you call this method after a long initialisation
|
||||
period, it may be deleted without any further delay.
|
||||
@param removeOnMouseClick if true, the window will be deleted as soon as the user clicks
|
||||
the mouse (anywhere)
|
||||
*/
|
||||
void deleteAfterDelay (RelativeTime minimumTotalTimeToDisplayFor,
|
||||
bool removeOnMouseClick);
|
||||
|
||||
protected:
|
||||
//==============================================================================
|
||||
/** This constructor is for use by custom sub-classes that don't want to provide an image. */
|
||||
SplashScreen (const String& title, int width, int height, bool useDropShadow);
|
||||
|
||||
/** @internal */
|
||||
void paint (Graphics&) override;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
Image backgroundImage;
|
||||
Time creationTime;
|
||||
RelativeTime minimumVisibleTime;
|
||||
int clickCountToDelete;
|
||||
|
||||
void timerCallback() override;
|
||||
void makeVisible (int w, int h, bool shadow, bool fullscreen);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SplashScreen)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
#if JUCE_WINDOWS || JUCE_LINUX || JUCE_BSD || JUCE_MAC
|
||||
|
||||
SystemTrayIconComponent::SystemTrayIconComponent()
|
||||
{
|
||||
addToDesktop (0);
|
||||
}
|
||||
|
||||
SystemTrayIconComponent::~SystemTrayIconComponent()
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
#if JUCE_WINDOWS || JUCE_LINUX || JUCE_BSD || JUCE_MAC || DOXYGEN
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
This component sits in the taskbar tray as a small icon.
|
||||
|
||||
(NB: The exact behaviour of this class will differ between OSes, and it
|
||||
isn't fully implemented for all OSes)
|
||||
|
||||
To use it, just create one of these components, but don't attempt to make it
|
||||
visible, add it to a parent, or put it on the desktop.
|
||||
|
||||
You can then call setIconImage() to create an icon for it in the taskbar.
|
||||
|
||||
To change the icon's tooltip, you can use setIconTooltip().
|
||||
|
||||
To respond to mouse-events, you can override the normal mouseDown(),
|
||||
mouseUp(), mouseDoubleClick() and mouseMove() methods, and although the x, y
|
||||
position will not be valid, you can use this to respond to clicks. Traditionally
|
||||
you'd use a left-click to show your application's window, and a right-click
|
||||
to show a pop-up menu.
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API SystemTrayIconComponent : public Component
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Constructor. */
|
||||
SystemTrayIconComponent();
|
||||
|
||||
/** Destructor. */
|
||||
~SystemTrayIconComponent() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Changes the image shown in the taskbar.
|
||||
|
||||
On Windows and Linux a full colour Image is used as an icon.
|
||||
On macOS a template image is used, where all non-transparent regions will be
|
||||
rendered in a monochrome colour selected dynamically by the operating system.
|
||||
|
||||
@param colourImage An colour image to use as an icon on Windows and Linux
|
||||
@param templateImage A template image to use as an icon on macOS
|
||||
*/
|
||||
void setIconImage (const Image& colourImage, const Image& templateImage);
|
||||
|
||||
/** Changes the icon's tooltip (if the current OS supports this). */
|
||||
void setIconTooltip (const String& tooltip);
|
||||
|
||||
/** Highlights the icon (if the current OS supports this). */
|
||||
void setHighlighted (bool);
|
||||
|
||||
/** Shows a floating text bubble pointing to the icon (if the current OS supports this). */
|
||||
void showInfoBubble (const String& title, const String& content);
|
||||
|
||||
/** Hides the icon's floating text bubble (if the current OS supports this). */
|
||||
void hideInfoBubble();
|
||||
|
||||
/** Returns the raw handle to whatever kind of internal OS structure is
|
||||
involved in showing this icon.
|
||||
@see ComponentPeer::getNativeHandle()
|
||||
*/
|
||||
void* getNativeHandle() const;
|
||||
|
||||
#if JUCE_LINUX || JUCE_BSD
|
||||
/** @internal */
|
||||
void paint (Graphics&) override;
|
||||
#endif
|
||||
|
||||
#if JUCE_MAC
|
||||
/** Shows a menu attached to the OSX menu bar icon. */
|
||||
void showDropdownMenu (const PopupMenu& menu);
|
||||
#endif
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
JUCE_PUBLIC_IN_DLL_BUILD (class Pimpl)
|
||||
std::unique_ptr<Pimpl> pimpl;
|
||||
|
||||
[[deprecated ("The new setIconImage function signature requires different images for macOS and the other platforms.")]]
|
||||
void setIconImage (const Image& newImage);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SystemTrayIconComponent)
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,730 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
#if JUCE_WEB_BROWSER || DOXYGEN
|
||||
|
||||
bool WebBrowserComponent::pageAboutToLoad ([[maybe_unused]] const String& newURL) { return true; }
|
||||
void WebBrowserComponent::pageFinishedLoading ([[maybe_unused]] const String& url) {}
|
||||
bool WebBrowserComponent::pageLoadHadNetworkError ([[maybe_unused]] const String& errorInfo) { return true; }
|
||||
void WebBrowserComponent::windowCloseRequest() {}
|
||||
void WebBrowserComponent::newWindowAttemptingToLoad ([[maybe_unused]] const String& newURL) {}
|
||||
|
||||
// At least this much code has to be injected as user script, since the native backend functions
|
||||
// rely on the presence of the `window.__JUCE__.backend.emitByBackend()` function. The rest can be
|
||||
// optionally imported as JS.
|
||||
static constexpr const char* lowLevelIntegrationsScript = R"(
|
||||
if (
|
||||
typeof window.__JUCE__ !== "undefined" &&
|
||||
typeof window.__JUCE__.getAndroidUserScripts !== "undefined" &&
|
||||
typeof window.inAndroidUserScriptEval === "undefined"
|
||||
) {
|
||||
window.inAndroidUserScriptEval = true;
|
||||
eval(window.__JUCE__.getAndroidUserScripts());
|
||||
delete window.inAndroidUserScriptEval;
|
||||
}
|
||||
|
||||
{
|
||||
if (typeof window.__JUCE__ === "undefined") {
|
||||
console.warn(
|
||||
"The 'window.__JUCE__' object is undefined." +
|
||||
" Native integration features will not work." +
|
||||
" Defining a placeholder 'window.__JUCE__' object."
|
||||
);
|
||||
|
||||
window.__JUCE__ = {
|
||||
postMessage: function () {},
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof window.__JUCE__.initialisationData === "undefined") {
|
||||
window.__JUCE__.initialisationData = {
|
||||
__juce__platform: [],
|
||||
__juce__functions: [],
|
||||
__juce__registeredGlobalEventIds: [],
|
||||
__juce__sliders: [],
|
||||
__juce__toggles: [],
|
||||
__juce__comboBoxes: [],
|
||||
};
|
||||
}
|
||||
|
||||
class ListenerList {
|
||||
constructor() {
|
||||
this.listeners = new Map();
|
||||
this.listenerId = 0;
|
||||
}
|
||||
|
||||
addListener(fn) {
|
||||
const newListenerId = this.listenerId++;
|
||||
this.listeners.set(newListenerId, fn);
|
||||
return newListenerId;
|
||||
}
|
||||
|
||||
removeListener(id) {
|
||||
if (this.listeners.has(id)) {
|
||||
this.listeners.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
callListeners(payload) {
|
||||
for (const [, value] of this.listeners) {
|
||||
value(payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class EventListenerList {
|
||||
constructor() {
|
||||
this.eventListeners = new Map();
|
||||
}
|
||||
|
||||
addEventListener(eventId, fn) {
|
||||
if (!this.eventListeners.has(eventId))
|
||||
this.eventListeners.set(eventId, new ListenerList());
|
||||
|
||||
const id = this.eventListeners.get(eventId).addListener(fn);
|
||||
|
||||
return [eventId, id];
|
||||
}
|
||||
|
||||
removeEventListener([eventId, id]) {
|
||||
if (this.eventListeners.has(eventId)) {
|
||||
this.eventListeners.get(eventId).removeListener(id);
|
||||
}
|
||||
}
|
||||
|
||||
emitEvent(eventId, object) {
|
||||
if (this.eventListeners.has(eventId))
|
||||
this.eventListeners.get(eventId).callListeners(object);
|
||||
}
|
||||
}
|
||||
|
||||
class Backend {
|
||||
constructor() {
|
||||
this.listeners = new EventListenerList();
|
||||
}
|
||||
|
||||
addEventListener(eventId, fn) {
|
||||
return this.listeners.addEventListener(eventId, fn);
|
||||
}
|
||||
|
||||
removeEventListener([eventId, id]) {
|
||||
this.listeners.removeEventListener([eventId, id]);
|
||||
}
|
||||
|
||||
emitEvent(eventId, object) {
|
||||
window.__JUCE__.postMessage(
|
||||
JSON.stringify({ eventId: eventId, payload: object })
|
||||
);
|
||||
}
|
||||
|
||||
emitByBackend(eventId, object) {
|
||||
this.listeners.emitEvent(eventId, JSON.parse(object));
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof window.__JUCE__.backend === "undefined")
|
||||
window.__JUCE__.backend = new Backend();
|
||||
}
|
||||
)";
|
||||
|
||||
static void evaluationHandler (WebBrowserComponent::EvaluationResult r)
|
||||
{
|
||||
if (r.getResult() == nullptr)
|
||||
{
|
||||
// The unsupported return type means a successful Javascript evaluation that yielded a
|
||||
// result that cannot be translated and returned to native code such as a Promise.
|
||||
jassert (r.getError()->type == WebBrowserComponent::EvaluationResult::Error::Type::unsupportedReturnType);
|
||||
DBG (r.getError()->message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static StringArray getUserScriptsForInitialisationData (const std::vector<std::pair<String, var>>& data)
|
||||
{
|
||||
std::map<String, StringArray> sortedData;
|
||||
|
||||
for (const auto& [key, value] : data)
|
||||
sortedData[key].add (JSON::toString (value));
|
||||
|
||||
StringArray result;
|
||||
|
||||
for (const auto& [key, values] : sortedData)
|
||||
result.add ("window.__JUCE__.initialisationData." + key + " = ["
|
||||
+ values.joinIntoString (",") + "];");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static auto getCommaSeparatedList (const T& mapOfStringable)
|
||||
{
|
||||
StringArray keys;
|
||||
|
||||
for (auto it : mapOfStringable)
|
||||
keys.add (it.first.toString().quoted());
|
||||
|
||||
return keys.joinIntoString (",");
|
||||
}
|
||||
|
||||
struct NativeEvents
|
||||
{
|
||||
NativeEvents() = delete;
|
||||
|
||||
struct NativeEvent
|
||||
{
|
||||
String eventId;
|
||||
var payload;
|
||||
|
||||
static constexpr std::optional<int> marshallingVersion = std::nullopt;
|
||||
|
||||
template <typename Archive, typename Item>
|
||||
static void serialise (Archive& archive, Item& item)
|
||||
{
|
||||
archive (named ("eventId", item.eventId),
|
||||
named ("payload", item.payload));
|
||||
}
|
||||
};
|
||||
|
||||
struct Invoke
|
||||
{
|
||||
String name;
|
||||
var params;
|
||||
int64 resultId;
|
||||
|
||||
static constexpr std::optional<int> marshallingVersion = std::nullopt;
|
||||
|
||||
template <typename Archive, typename Item>
|
||||
static void serialise (Archive& archive, Item& item)
|
||||
{
|
||||
archive (named ("name", item.name),
|
||||
named ("params", item.params),
|
||||
named ("resultId", item.resultId));
|
||||
}
|
||||
|
||||
static inline const Identifier eventId { "__juce__invoke" };
|
||||
static inline const Identifier completeId { "__juce__complete" };
|
||||
};
|
||||
};
|
||||
|
||||
class NativeFunctionsProvider : public OptionsBuilder<WebBrowserComponent::Options>,
|
||||
private AsyncUpdater
|
||||
{
|
||||
public:
|
||||
explicit NativeFunctionsProvider (WebBrowserComponent& ownerIn)
|
||||
: owner (ownerIn)
|
||||
{
|
||||
}
|
||||
|
||||
WebBrowserComponent::Options buildOptions (const WebBrowserComponent::Options& initialOptions) override
|
||||
{
|
||||
nativeFunctions = initialOptions.getNativeFunctions();
|
||||
|
||||
if (nativeFunctions.empty())
|
||||
return initialOptions;
|
||||
|
||||
auto options = initialOptions.withNativeIntegrationEnabled()
|
||||
.withEventListener (NativeEvents::Invoke::eventId,
|
||||
[this] (const auto& object)
|
||||
{
|
||||
handleNativeFunctionCall (object);
|
||||
});
|
||||
|
||||
for (auto it : nativeFunctions)
|
||||
options = options.withInitialisationData ("__juce__functions", it.first.toString());
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
private:
|
||||
void handleAsyncUpdate() override
|
||||
{
|
||||
const auto popFront = [&] (auto&& callback)
|
||||
{
|
||||
functionCompletionsMutex.lock();
|
||||
ErasedScopeGuard unlocker { [&] { functionCompletionsMutex.unlock(); } };
|
||||
|
||||
if (! functionCompletions.empty())
|
||||
{
|
||||
auto completion = functionCompletions.front();
|
||||
functionCompletions.pop_front();
|
||||
unlocker.reset();
|
||||
callback (completion);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
while (popFront ([&] (const auto& c) { emitCompletionEvent (c.first, c.second); }))
|
||||
;
|
||||
}
|
||||
|
||||
void handleNativeFunctionCall (const var& object)
|
||||
{
|
||||
const auto invocation = FromVar::convert<NativeEvents::Invoke> (object);
|
||||
|
||||
if (! invocation.has_value())
|
||||
{
|
||||
jassertfalse;
|
||||
return;
|
||||
}
|
||||
|
||||
auto it = nativeFunctions.find (invocation->name);
|
||||
|
||||
if (it == nativeFunctions.end())
|
||||
{
|
||||
jassertfalse;
|
||||
return;
|
||||
}
|
||||
|
||||
jassert (invocation->params.isArray());
|
||||
|
||||
it->second (*invocation->params.getArray(),
|
||||
[this, resultId = invocation->resultId] (auto result)
|
||||
{
|
||||
completeNativeFunctionCall (resultId, result);
|
||||
});
|
||||
}
|
||||
|
||||
void completeNativeFunctionCall (int64 resultId, const var& object)
|
||||
{
|
||||
if (MessageManager::getInstance()->isThisTheMessageThread())
|
||||
{
|
||||
emitCompletionEvent (resultId, object);
|
||||
}
|
||||
else
|
||||
{
|
||||
const std::lock_guard lock { functionCompletionsMutex };
|
||||
functionCompletions.push_back (std::make_pair (resultId, object));
|
||||
triggerAsyncUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
void emitCompletionEvent (int64 resultId, const var& object)
|
||||
{
|
||||
DynamicObject::Ptr eventObject { new DynamicObject() };
|
||||
eventObject->setProperty ("promiseId", resultId);
|
||||
eventObject->setProperty ("result", object);
|
||||
|
||||
jassert (owner.isVisible());
|
||||
owner.emitEventIfBrowserIsVisible (NativeEvents::Invoke::completeId, eventObject.get());
|
||||
}
|
||||
|
||||
WebBrowserComponent& owner;
|
||||
std::map<Identifier, WebBrowserComponent::NativeFunction> nativeFunctions;
|
||||
std::deque<std::pair<int64, var>> functionCompletions;
|
||||
std::mutex functionCompletionsMutex;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (NativeFunctionsProvider)
|
||||
JUCE_DECLARE_NON_MOVEABLE (NativeFunctionsProvider)
|
||||
};
|
||||
|
||||
class NativeEventListeners
|
||||
{
|
||||
public:
|
||||
void addListener (const Identifier& eventId, std::function<void (const var&)> handler)
|
||||
{
|
||||
listeners.push_back (std::move (handler));
|
||||
listenerMap[eventId].add (&listeners.back());
|
||||
}
|
||||
|
||||
void emit (const Identifier& eventId, const var& object)
|
||||
{
|
||||
if (const auto& it = listenerMap.find (eventId); it != listenerMap.end())
|
||||
it->second.call ([&object] (auto& l) { l (object); });
|
||||
}
|
||||
|
||||
private:
|
||||
std::list<WebBrowserComponent::NativeEventListener> listeners;
|
||||
|
||||
std::map<Identifier, ListenerList<WebBrowserComponent::NativeEventListener>> listenerMap;
|
||||
};
|
||||
|
||||
class WebBrowserComponent::Impl
|
||||
{
|
||||
public:
|
||||
Impl (WebBrowserComponent& ownerIn, const Options& optionsIn)
|
||||
: owner (ownerIn),
|
||||
options ([&]
|
||||
{
|
||||
makeFunctionsProviderIfNecessary (nativeFunctionsProvider, *this, optionsIn);
|
||||
|
||||
if (nativeFunctionsProvider.has_value())
|
||||
return getOptions (optionsIn).withOptionsFrom (*nativeFunctionsProvider);
|
||||
|
||||
return getOptions (optionsIn);
|
||||
}())
|
||||
{
|
||||
auto userScripts = options.getUserScripts();
|
||||
|
||||
// Adding user scripts in reverse order of depending on each other. The most depended on
|
||||
// comes last.
|
||||
for (const auto& eventListener : options.getEventListeners())
|
||||
{
|
||||
userScripts.insert (0,
|
||||
"window.__JUCE__.initialisationData.__juce__registeredGlobalEventIds = ["
|
||||
+ getCommaSeparatedList (options.getEventListeners()) + "];");
|
||||
|
||||
addPermanentEventListener (eventListener.first, eventListener.second);
|
||||
}
|
||||
|
||||
for (const auto& script : getUserScriptsForInitialisationData (options.getInitialisationData()))
|
||||
userScripts.insert (0, script);
|
||||
|
||||
userScripts.insert (0, lowLevelIntegrationsScript);
|
||||
|
||||
platform = createAndInitPlatformDependentPart (*this, options, userScripts);
|
||||
}
|
||||
|
||||
void emitEvent (const Identifier& eventId, const var& object)
|
||||
{
|
||||
// The object parameter is serialised into a string and used as a parameter to a Javascript
|
||||
// function call. During this JS parameter substitution, control character escape sequences
|
||||
// would be interpreted as the control characters themselves. So we need to escape anything
|
||||
// that was escaped.
|
||||
//
|
||||
// We also need to escape the ' character since we use this to delimit the parameter string
|
||||
// to emitByBackend.
|
||||
const auto objectAsString = JSON::toString (object, true);
|
||||
const auto escaped = objectAsString.replace ("\\", "\\\\").replace ("'", "\\'");
|
||||
|
||||
evaluateJavascript ("window.__JUCE__.backend.emitByBackend(" + eventId.toString().quoted() + ", "
|
||||
+ escaped.quoted ('\'')
|
||||
+ ");", evaluationHandler);
|
||||
}
|
||||
|
||||
void goToURL (const String& url, const StringArray* headers, const MemoryBlock* postData)
|
||||
{
|
||||
platform->goToURL (url, headers, postData);
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
platform->stop();
|
||||
}
|
||||
|
||||
void goBack()
|
||||
{
|
||||
platform->goBack();
|
||||
|
||||
owner.lastURL.clear();
|
||||
owner.blankPageShown = false;
|
||||
}
|
||||
|
||||
void goForward()
|
||||
{
|
||||
owner.lastURL.clear();
|
||||
|
||||
platform->goForward();
|
||||
}
|
||||
|
||||
void refresh()
|
||||
{
|
||||
platform->refresh();
|
||||
}
|
||||
|
||||
void evaluateJavascript (const String& script, EvaluationCallback callback)
|
||||
{
|
||||
platform->evaluateJavascript (script, std::move (callback));
|
||||
}
|
||||
|
||||
void setSize (int width, int height)
|
||||
{
|
||||
platform->setWebViewSize (width, height);
|
||||
}
|
||||
|
||||
void checkWindowAssociation()
|
||||
{
|
||||
platform->checkWindowAssociation();
|
||||
}
|
||||
|
||||
void fallbackPaint (Graphics& webBrowserComponentContext)
|
||||
{
|
||||
platform->fallbackPaint (webBrowserComponentContext);
|
||||
}
|
||||
|
||||
void focusGainedWithDirection (FocusChangeType type, FocusChangeDirection dir)
|
||||
{
|
||||
platform->focusGainedWithDirection (type, dir);
|
||||
}
|
||||
|
||||
struct Platform;
|
||||
|
||||
private:
|
||||
class PlatformInterface
|
||||
{
|
||||
public:
|
||||
PlatformInterface() = default;
|
||||
virtual ~PlatformInterface() = default;
|
||||
|
||||
virtual void goToURL (const String&, const StringArray*, const MemoryBlock*) = 0;
|
||||
virtual void goBack() = 0;
|
||||
virtual void goForward() = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual void refresh() = 0;
|
||||
virtual void evaluateJavascript (const String&, WebBrowserComponent::EvaluationCallback) = 0;
|
||||
virtual void setWebViewSize (int, int) = 0;
|
||||
virtual void checkWindowAssociation() = 0;
|
||||
|
||||
virtual void focusGainedWithDirection (FocusChangeType, FocusChangeDirection) {}
|
||||
virtual void fallbackPaint (Graphics&) {}
|
||||
};
|
||||
|
||||
static void makeFunctionsProviderIfNecessary (std::optional<NativeFunctionsProvider>& provider,
|
||||
WebBrowserComponent::Impl& impl,
|
||||
const WebBrowserComponent::Options& options)
|
||||
{
|
||||
if (! options.getNativeFunctions().empty())
|
||||
provider.emplace (impl.owner);
|
||||
}
|
||||
|
||||
static Options getOptions (const Options& optionsIn)
|
||||
{
|
||||
const auto os = SystemStats::getOperatingSystemType();
|
||||
|
||||
const auto platformString = [&]
|
||||
{
|
||||
using OsType = SystemStats::OperatingSystemType;
|
||||
|
||||
if ((os & OsType::MacOSX) != 0)
|
||||
return "macos";
|
||||
|
||||
if ((os & OsType::iOS) != 0)
|
||||
return "ios";
|
||||
|
||||
if ((os & OsType::Windows) != 0)
|
||||
return "windows";
|
||||
|
||||
if ((os & OsType::Android) != 0)
|
||||
return "android";
|
||||
|
||||
if ((os & OsType::Linux) != 0)
|
||||
return "linux";
|
||||
|
||||
return "";
|
||||
}();
|
||||
|
||||
return optionsIn.withInitialisationData ("__juce__platform", platformString);
|
||||
}
|
||||
|
||||
void addPermanentEventListener (const Identifier& eventId, NativeEventListener listener)
|
||||
{
|
||||
nativeEventListeners.addListener (eventId, std::move (listener));
|
||||
}
|
||||
|
||||
std::optional<Resource> handleResourceRequest (const String& url)
|
||||
{
|
||||
if (resourceProvider != nullptr)
|
||||
return resourceProvider (url);
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void handleNativeEvent (const var& message)
|
||||
{
|
||||
const auto event = FromVar::convert<NativeEvents::NativeEvent> (message);
|
||||
|
||||
if (! event.has_value())
|
||||
{
|
||||
jassertfalse;
|
||||
return;
|
||||
}
|
||||
|
||||
nativeEventListeners.emit (event->eventId, event->payload);
|
||||
}
|
||||
|
||||
static std::unique_ptr<PlatformInterface> createAndInitPlatformDependentPart (WebBrowserComponent::Impl&,
|
||||
const Options&,
|
||||
const StringArray&);
|
||||
|
||||
WebBrowserComponent& owner;
|
||||
std::optional<NativeFunctionsProvider> nativeFunctionsProvider;
|
||||
Options options;
|
||||
ResourceProvider resourceProvider { options.getResourceProvider() };
|
||||
NativeEventListeners nativeEventListeners;
|
||||
|
||||
std::unique_ptr<PlatformInterface> platform;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
WebBrowserComponent::WebBrowserComponent (const Options& options)
|
||||
: impl (std::make_unique<Impl> (*this, options)),
|
||||
unloadPageWhenHidden (! options.keepsPageLoadedWhenBrowserIsHidden())
|
||||
{
|
||||
setOpaque (true);
|
||||
|
||||
#if JUCE_LINUX
|
||||
ignoreUnused (blankPageShown);
|
||||
ignoreUnused (unloadPageWhenHidden);
|
||||
#endif
|
||||
|
||||
for (const auto& l : options.getLifetimeListeners())
|
||||
lifetimeListeners.add (l);
|
||||
|
||||
lifetimeListeners.call (&WebViewLifetimeListener::webViewConstructed, this);
|
||||
}
|
||||
|
||||
WebBrowserComponent::~WebBrowserComponent()
|
||||
{
|
||||
lifetimeListeners.call (&WebViewLifetimeListener::webViewDestructed, this);
|
||||
}
|
||||
|
||||
void WebBrowserComponent::emitEventIfBrowserIsVisible (const Identifier& eventId, const var& object)
|
||||
{
|
||||
if (isVisible())
|
||||
impl->emitEvent (eventId, object);
|
||||
}
|
||||
|
||||
const String& WebBrowserComponent::getResourceProviderRoot()
|
||||
{
|
||||
const auto os = SystemStats::getOperatingSystemType();
|
||||
|
||||
using OsType = SystemStats::OperatingSystemType;
|
||||
|
||||
if ( (os & OsType::MacOSX) != 0
|
||||
|| (os & OsType::iOS) != 0
|
||||
|| (os & OsType::Linux) != 0)
|
||||
{
|
||||
static String backendUrl { "juce://juce.backend/" };
|
||||
return backendUrl;
|
||||
}
|
||||
|
||||
if ( (os & OsType::Windows) != 0
|
||||
|| (os & OsType::Android) != 0)
|
||||
{
|
||||
static String backendUrl { "https://juce.backend/" };
|
||||
return backendUrl;
|
||||
}
|
||||
|
||||
static String emptyUrl { "" };
|
||||
return emptyUrl;
|
||||
}
|
||||
|
||||
void WebBrowserComponent::goToURL (const String& url,
|
||||
const StringArray* headers,
|
||||
const MemoryBlock* postData)
|
||||
{
|
||||
lastURL = url;
|
||||
|
||||
if (headers != nullptr)
|
||||
lastHeaders = *headers;
|
||||
else
|
||||
lastHeaders.clear();
|
||||
|
||||
if (postData != nullptr)
|
||||
lastPostData = *postData;
|
||||
else
|
||||
lastPostData.reset();
|
||||
|
||||
impl->goToURL (url, headers, postData);
|
||||
|
||||
blankPageShown = false;
|
||||
}
|
||||
|
||||
void WebBrowserComponent::stop()
|
||||
{
|
||||
impl->stop();
|
||||
}
|
||||
|
||||
void WebBrowserComponent::goBack()
|
||||
{
|
||||
impl->goBack();
|
||||
lastURL.clear();
|
||||
blankPageShown = false;
|
||||
}
|
||||
|
||||
void WebBrowserComponent::goForward()
|
||||
{
|
||||
lastURL.clear();
|
||||
impl->goForward();
|
||||
}
|
||||
|
||||
void WebBrowserComponent::refresh()
|
||||
{
|
||||
impl->refresh();
|
||||
}
|
||||
|
||||
void WebBrowserComponent::paint (Graphics& g)
|
||||
{
|
||||
impl->fallbackPaint (g);
|
||||
}
|
||||
|
||||
void WebBrowserComponent::parentHierarchyChanged()
|
||||
{
|
||||
impl->checkWindowAssociation();
|
||||
}
|
||||
|
||||
void WebBrowserComponent::visibilityChanged()
|
||||
{
|
||||
impl->checkWindowAssociation();
|
||||
}
|
||||
|
||||
void WebBrowserComponent::resized()
|
||||
{
|
||||
impl->setSize (getWidth(), getHeight());
|
||||
}
|
||||
|
||||
void WebBrowserComponent::reloadLastURL()
|
||||
{
|
||||
const auto ptrOrNullIfEmpty = [] (auto& value)
|
||||
{
|
||||
return ! value.isEmpty() ? &value : nullptr;
|
||||
};
|
||||
|
||||
if (lastURL.isNotEmpty())
|
||||
{
|
||||
goToURL (lastURL, ptrOrNullIfEmpty (lastHeaders), ptrOrNullIfEmpty (lastPostData));
|
||||
lastURL.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void WebBrowserComponent::evaluateJavascript (const String& script, EvaluationCallback callback)
|
||||
{
|
||||
impl->evaluateJavascript (script, std::move (callback));
|
||||
}
|
||||
|
||||
void WebBrowserComponent::focusGainedWithDirection (FocusChangeType type,
|
||||
FocusChangeDirection direction)
|
||||
{
|
||||
impl->focusGainedWithDirection (type, direction);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,663 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
#if JUCE_WEB_BROWSER || DOXYGEN
|
||||
|
||||
#if JUCE_MAC || JUCE_IOS || (JUCE_WINDOWS && JUCE_USE_WIN_WEBVIEW2) || JUCE_ANDROID || JUCE_LINUX
|
||||
#define JUCE_WEB_BROWSER_RESOURCE_PROVIDER_AVAILABLE 1
|
||||
#else
|
||||
#define JUCE_WEB_BROWSER_RESOURCE_PROVIDER_AVAILABLE 0
|
||||
#endif
|
||||
|
||||
class WebBrowserComponent;
|
||||
|
||||
/** Type for a listener registered with
|
||||
WebBrowserComponent::Options::withWebViewLifetimeListener. This can be useful for
|
||||
types using the WebBrowserComponent::Options::withOptionsFrom function as they have to be
|
||||
constructed before the WebBrowserComponent.
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API WebViewLifetimeListener
|
||||
{
|
||||
public:
|
||||
virtual ~WebViewLifetimeListener() = default;
|
||||
|
||||
/** Called from the WebBrowserComponent constructor. */
|
||||
virtual void webViewConstructed (WebBrowserComponent*) = 0;
|
||||
|
||||
/** Called from the WebBrowserComponent destructor. */
|
||||
virtual void webViewDestructed (WebBrowserComponent*) = 0;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A component that displays an embedded web browser.
|
||||
|
||||
The browser itself will be platform-dependent. On Mac and iOS it will be
|
||||
WebKit, on Android it will be Chrome, and on Linux it will be WebKit.
|
||||
|
||||
The default engine on Windows will be IE, but if JUCE_USE_WIN_WEBVIEW2=1 or
|
||||
JUCE_USE_WIN_WEBVIEW2_WITH_STATIC_LINKING=1 is defined, then passing the
|
||||
WebBrowserComponent::Options::Backend::webview2 value to the constructor will
|
||||
attempt to use the Chrome based Edge WebView, and fall back to IE in case of
|
||||
failure. CMake builds also need to specify the NEEDS_WEBVIEW2 option when
|
||||
creating a JUCE based target.
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API WebBrowserComponent : public Component
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Type for a listener registered with Options::withNativeEventListener. */
|
||||
using NativeEventListener = std::function<void (var)>;
|
||||
|
||||
/** Type for the completion passed as the second parameter of NativeFunction. Can be called
|
||||
from any thread.
|
||||
*/
|
||||
using NativeFunctionCompletion = std::function<void (var)>;
|
||||
|
||||
/** Type for functions registered with Options::withNativeFunction. The first parameter is an
|
||||
Array object containing the arguments of the Javascript function invocation.
|
||||
|
||||
The second parameter is the result that completes the Promise returned by the Javascript
|
||||
function call. It can be called from any thread.
|
||||
*/
|
||||
using NativeFunction = std::function<void (const Array<var>&, NativeFunctionCompletion)>;
|
||||
|
||||
/** A resource returned by a ResourceProvider.
|
||||
|
||||
@see Options::withResourceProvider
|
||||
*/
|
||||
struct Resource
|
||||
{
|
||||
std::vector<std::byte> data;
|
||||
String mimeType;
|
||||
};
|
||||
|
||||
/** The type used in Options::withResourceProvider.
|
||||
*/
|
||||
using ResourceProvider = std::function<std::optional<Resource> (const String&)>;
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Options to configure WebBrowserComponent.
|
||||
*/
|
||||
class JUCE_API Options
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
enum class Backend
|
||||
{
|
||||
/**
|
||||
Default web browser backend. WebKit will be used on macOS, gtk-webkit2 on Linux and internet
|
||||
explorer on Windows. On Windows, the default may change to webview2 in the fututre.
|
||||
*/
|
||||
defaultBackend,
|
||||
|
||||
/**
|
||||
Use Internet Explorer as the backend on Windows. By default, IE will use an ancient version
|
||||
of IE. To change this behaviour, you either need to add the following html element into your page's
|
||||
head section:
|
||||
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
|
||||
or you need to change windows registry values for your application. More information on the latter
|
||||
can be found here:
|
||||
|
||||
https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/ee330730(v=vs.85)?redirectedfrom=MSDN#browser-emulation
|
||||
*/
|
||||
ie,
|
||||
|
||||
/**
|
||||
Use the chromium based WebView2 engine on Windows
|
||||
*/
|
||||
webview2
|
||||
};
|
||||
|
||||
/**
|
||||
Use a particular backend to create the WebViewBrowserComponent. JUCE will silently
|
||||
fallback to the default backend if the selected backend is not supported. To check
|
||||
if a specific backend is supported on your platform or not, use
|
||||
WebBrowserComponent::areOptionsSupported.
|
||||
*/
|
||||
[[nodiscard]] Options withBackend (Backend backend) const { return withMember (*this, &Options::browserBackend, backend); }
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Tell JUCE to keep the web page alive when the WebBrowserComponent is not visible.
|
||||
By default, JUCE will replace the current page with a blank page - this can be
|
||||
handy to stop the browser using resources in the background when it's not
|
||||
actually being used.
|
||||
*/
|
||||
[[nodiscard]] Options withKeepPageLoadedWhenBrowserIsHidden() const { return withMember (*this, &Options::keepPageLoadedWhenBrowserIsHidden, true); }
|
||||
|
||||
/**
|
||||
Use a specific user agent string when requesting web pages.
|
||||
*/
|
||||
[[nodiscard]] Options withUserAgent (String ua) const { return withMember (*this, &Options::userAgent, std::move (ua)); }
|
||||
|
||||
//==============================================================================
|
||||
/** Options specific to the WebView2 backend. These options will be ignored
|
||||
if another backend is used.
|
||||
*/
|
||||
class WinWebView2
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Sets a custom location for the WebView2Loader.dll that is not a part of the
|
||||
standard system DLL search paths.
|
||||
*/
|
||||
[[nodiscard]] WinWebView2 withDLLLocation (const File& location) const { return withMember (*this, &WinWebView2::dllLocation, location); }
|
||||
|
||||
/** Sets a non-default location for storing user data for the browser instance.
|
||||
|
||||
In plugin projects you may find it necessary to use this option and specify a
|
||||
location such as File::SpecialLocationType::tempDirectory. Otherwise WebView2
|
||||
may function incorrectly due to being denied access to the default user data
|
||||
location.
|
||||
*/
|
||||
[[nodiscard]] WinWebView2 withUserDataFolder (const File& folder) const { return withMember (*this, &WinWebView2::userDataFolder, folder); }
|
||||
|
||||
/** If this is set, the status bar usually displayed in the lower-left of the webview
|
||||
will be disabled.
|
||||
*/
|
||||
[[nodiscard]] WinWebView2 withStatusBarDisabled() const { return withMember (*this, &WinWebView2::disableStatusBar, true); }
|
||||
|
||||
/** If this is set, a blank page will be displayed on error instead of the default
|
||||
built-in error page.
|
||||
*/
|
||||
[[nodiscard]] WinWebView2 withBuiltInErrorPageDisabled() const { return withMember (*this, &WinWebView2::disableBuiltInErrorPage, true); }
|
||||
|
||||
/** Sets the background colour that WebView2 renders underneath all web content.
|
||||
|
||||
This colour must either be fully opaque or fully transparent.
|
||||
*/
|
||||
[[nodiscard]] WinWebView2 withBackgroundColour (const Colour& colour) const
|
||||
{
|
||||
// the background colour must be either fully opaque or transparent!
|
||||
jassert (colour.isOpaque() || colour.isTransparent());
|
||||
|
||||
return withMember (*this, &WinWebView2::backgroundColour, colour);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
File getDLLLocation() const { return dllLocation; }
|
||||
File getUserDataFolder() const { return userDataFolder; }
|
||||
bool getIsStatusBarDisabled() const noexcept { return disableStatusBar; }
|
||||
bool getIsBuiltInErrorPageDisabled() const noexcept { return disableBuiltInErrorPage; }
|
||||
Colour getBackgroundColour() const { return backgroundColour; }
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
File dllLocation, userDataFolder;
|
||||
bool disableStatusBar = false, disableBuiltInErrorPage = false;
|
||||
Colour backgroundColour;
|
||||
};
|
||||
|
||||
/** Options specific to the WkWebView backend used on Apple systems. These options will be
|
||||
ignored on non-Apple platforms.
|
||||
*/
|
||||
class AppleWkWebView
|
||||
{
|
||||
public:
|
||||
/** Specifies whether the WebView is allowed to access siblings of files specified with
|
||||
the file:// URL scheme.
|
||||
|
||||
Allowing this is a potential security vulnerability if you don't have full control
|
||||
over the file that you are opening.
|
||||
*/
|
||||
[[nodiscard]] AppleWkWebView withAllowAccessToEnclosingDirectory (bool x) const
|
||||
{
|
||||
return withMember (*this, &AppleWkWebView::allowAccessToEnclosingDirectory, x);
|
||||
}
|
||||
|
||||
/** If this options is specified, the underlying WebView will return NO from its
|
||||
acceptsFirstMouse method.
|
||||
|
||||
This disables the click-through behaviour, meaning that clicking a previously
|
||||
unfocused application window only makes the window focused, but will not pass on the
|
||||
click to whichever control inside the WebView is under the mouse.
|
||||
*/
|
||||
[[nodiscard]] AppleWkWebView withDisabledAcceptsFirstMouse() const
|
||||
{
|
||||
return withMember (*this, &AppleWkWebView::acceptsFirstMouse, false);
|
||||
}
|
||||
|
||||
auto getAllowAccessToEnclosingDirectory() const { return allowAccessToEnclosingDirectory; }
|
||||
auto getAcceptsFirstMouse() const { return acceptsFirstMouse; }
|
||||
|
||||
private:
|
||||
bool allowAccessToEnclosingDirectory = false;
|
||||
bool acceptsFirstMouse = true;
|
||||
};
|
||||
|
||||
/** Specifies options that apply to the Windows implementation when the WebView2 feature is
|
||||
enabled.
|
||||
|
||||
@see withBackend
|
||||
*/
|
||||
[[nodiscard]] Options withWinWebView2Options (const WinWebView2& winWebView2Options) const
|
||||
{
|
||||
return withMember (*this, &Options::winWebView2, winWebView2Options);
|
||||
}
|
||||
|
||||
/** Specifies options that influence the WebBrowserComponent's behaviour on Apple systems.
|
||||
*/
|
||||
[[nodiscard]] Options withAppleWkWebViewOptions (const AppleWkWebView& appleWkWebViewOptions) const
|
||||
{
|
||||
return withMember (*this, &Options::appleWkWebView, appleWkWebViewOptions);
|
||||
}
|
||||
|
||||
/** Enables native integration features for the code running inside the WebBrowserComponent.
|
||||
|
||||
This injects data and function objects under `window.__JUCE__.backend` through which
|
||||
scripts running in the WebBrowserComponent can send events to the backend and call
|
||||
registered native functions.
|
||||
|
||||
You should only enable native integrations if you have full control over the content
|
||||
loaded into the component. Navigating to 3rd party websites with these integrations
|
||||
enabled may expose the application and the computer to security risks.
|
||||
|
||||
@see withNativeFunction, withEventListener
|
||||
*/
|
||||
[[nodiscard]] Options withNativeIntegrationEnabled (bool enabled = true) const
|
||||
{
|
||||
return withMember (*this, &Options::enableNativeIntegration, enabled);
|
||||
}
|
||||
|
||||
/** Registers a NativeFunction under the given name.
|
||||
|
||||
To call this function from the frontend, you can import the JUCE frontend helper module
|
||||
or issue a call to the low-level frontend API.
|
||||
|
||||
The callback is always called on the message thread.
|
||||
|
||||
@code
|
||||
import { getNativeFunction } from "./juce";
|
||||
|
||||
function someJavascriptFunction() {
|
||||
const myBackendFunction = getNativeFunction("myBackendFunction");
|
||||
myBackendFunction (1, 2, "some string");
|
||||
}
|
||||
@endcode
|
||||
*/
|
||||
[[nodiscard]] Options withNativeFunction (const Identifier& name, NativeFunction callback) const
|
||||
{
|
||||
auto copy = *this;
|
||||
jassert (copy.nativeFunctions.count (name) == 0);
|
||||
copy.nativeFunctions[name] = std::move (callback);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/** Registers a NativeEventListener that receives events sent to the specified eventId.
|
||||
|
||||
To send a message to this listener from the frontend, call for example
|
||||
@code window.__JUCE__.backend.emitEvent(eventId, { x: 2, y: 6 }); @endcode
|
||||
*/
|
||||
[[nodiscard]] Options withEventListener (const Identifier& eventId, NativeEventListener listener) const
|
||||
{
|
||||
auto copy = *this;
|
||||
copy.eventListeners.emplace_back (eventId, std::move (listener));
|
||||
return copy;
|
||||
}
|
||||
|
||||
/** Adds a Javascript code that will be evaluated before any other resource is loaded but
|
||||
after the JUCE backend definitions become available, hence the specified script can
|
||||
rely on the presence of `window.__JUCE__.backend`.
|
||||
|
||||
This script will be evaluated after all goToUrl() calls.
|
||||
*/
|
||||
[[nodiscard]] Options withUserScript (StringRef script) const
|
||||
{
|
||||
auto copy = *this;
|
||||
copy.userScripts.add (script);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/** Ensures that there will be a Javascript Array under
|
||||
`window.__JUCE__.initialisationData.name` and that it will contain the value
|
||||
provided here.
|
||||
|
||||
The initialisation data is injected prior to loading any resource. Multiple values added
|
||||
for the same name will all be available in the Array.
|
||||
*/
|
||||
[[nodiscard]] Options withInitialisationData (StringRef name, const var& value) const
|
||||
{
|
||||
auto copy = *this;
|
||||
copy.initialisationData.push_back (std::make_pair (String { name }, value));
|
||||
return copy;
|
||||
}
|
||||
|
||||
#if JUCE_WEB_BROWSER_RESOURCE_PROVIDER_AVAILABLE || JUCE_DOXYGEN
|
||||
/** Sets a ResourceProvider object that can complete WebView resource requests and return
|
||||
data without having to issue a network operation.
|
||||
|
||||
Requests sent to WebBrowserComponent::getResourceProviderRoot() + "resource.path" will
|
||||
invoke the provider with the path "/resource.path".
|
||||
|
||||
If you call WebBrowserComponent::goToURL with the value returned by
|
||||
WebBrowserComponent::getResourceProviderRoot, your resource provider will receive a
|
||||
request for the resource "/" for which you will typically want to return the contents of
|
||||
your index.html.
|
||||
|
||||
You can also specify an optional allowedOriginIn parameter that will make your
|
||||
ResourceProvider available to scripts loaded from that origin. E.g. if you specify
|
||||
"http://localhost:3000", then a script loaded from such a local development server will
|
||||
be able to access resources such as getResourceProviderRoot() + "live_data.bin".
|
||||
|
||||
Allowing external origins is handy for development, but is a potential security risk in
|
||||
publicly released binaries.
|
||||
*/
|
||||
[[nodiscard]] Options withResourceProvider (ResourceProvider provider,
|
||||
std::optional<String> allowedOriginIn = std::nullopt) const
|
||||
{
|
||||
return withMember (withMember (*this, &Options::resourceProvider, std::move (provider)),
|
||||
&Options::allowedOrigin,
|
||||
std::move (allowedOriginIn));
|
||||
}
|
||||
#endif
|
||||
|
||||
/** Adds an object that will be notified when the WebBrowserComponent is constructed and
|
||||
destructed.
|
||||
*/
|
||||
[[nodiscard]] Options withWebViewLifetimeListener (WebViewLifetimeListener* listener)
|
||||
{
|
||||
auto copy = *this;
|
||||
copy.lifetimeListeners.push_back (listener);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/** Adds all options provided by the builder to the returned Options object.
|
||||
*/
|
||||
template <typename OptionsType>
|
||||
OptionsType withOptionsFrom (OptionsBuilder<OptionsType>& builder) const
|
||||
{
|
||||
return builder.buildOptions (static_cast<const OptionsType&> (*this));
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
auto getBackend() const noexcept { return browserBackend; }
|
||||
auto keepsPageLoadedWhenBrowserIsHidden() const noexcept { return keepPageLoadedWhenBrowserIsHidden; }
|
||||
auto getUserAgent() const { return userAgent; }
|
||||
auto getWinWebView2BackendOptions() const { return winWebView2; }
|
||||
auto getAppleWkWebViewOptions() const { return appleWkWebView; }
|
||||
auto getNativeIntegrationsEnabled() const { return enableNativeIntegration; }
|
||||
const auto& getNativeFunctions() const { return nativeFunctions; }
|
||||
const auto& getEventListeners() const { return eventListeners; }
|
||||
const auto& getUserScripts() const { return userScripts; }
|
||||
const auto& getInitialisationData() const { return initialisationData; }
|
||||
auto getResourceProvider() const { return resourceProvider; }
|
||||
const auto& getAllowedOrigin() const { return allowedOrigin; }
|
||||
const auto& getLifetimeListeners() const { return lifetimeListeners; }
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
Backend browserBackend = Backend::defaultBackend;
|
||||
bool keepPageLoadedWhenBrowserIsHidden = false;
|
||||
bool enableNativeIntegration = false;
|
||||
String userAgent;
|
||||
WinWebView2 winWebView2;
|
||||
AppleWkWebView appleWkWebView;
|
||||
std::map<Identifier, NativeFunction> nativeFunctions;
|
||||
std::vector<std::pair<Identifier, NativeEventListener>> eventListeners;
|
||||
StringArray userScripts;
|
||||
std::vector<std::pair<String, var>> initialisationData;
|
||||
ResourceProvider resourceProvider;
|
||||
std::optional<String> allowedOrigin;
|
||||
std::vector<WebViewLifetimeListener*> lifetimeListeners;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/** Creates a WebBrowserComponent with default options*/
|
||||
WebBrowserComponent() : WebBrowserComponent (Options {}) {}
|
||||
|
||||
/** Creates a WebBrowserComponent.
|
||||
|
||||
Once it's created and visible, send the browser to a URL using goToURL().
|
||||
*/
|
||||
explicit WebBrowserComponent (const Options& options);
|
||||
|
||||
/** Destructor. */
|
||||
~WebBrowserComponent() override;
|
||||
|
||||
//==============================================================================
|
||||
/** Check if the specified options are supported on this platform. */
|
||||
static bool areOptionsSupported (const Options& options);
|
||||
|
||||
//==============================================================================
|
||||
/** Sends the browser to a particular URL.
|
||||
|
||||
@param url the URL to go to.
|
||||
@param headers an optional set of parameters to put in the HTTP header. If
|
||||
you supply this, it should be a set of string in the form
|
||||
"HeaderKey: HeaderValue"
|
||||
@param postData an optional block of data that will be attached to the HTTP
|
||||
POST request
|
||||
*/
|
||||
void goToURL (const String& url,
|
||||
const StringArray* headers = nullptr,
|
||||
const MemoryBlock* postData = nullptr);
|
||||
|
||||
/** Stops the current page loading. */
|
||||
void stop();
|
||||
|
||||
/** Sends the browser back one page. */
|
||||
void goBack();
|
||||
|
||||
/** Sends the browser forward one page. */
|
||||
void goForward();
|
||||
|
||||
/** Refreshes the browser. */
|
||||
void refresh();
|
||||
|
||||
/** Clear cookies that the OS has stored for the WebComponents of this application */
|
||||
static void clearCookies();
|
||||
|
||||
/** Returns a platform specific string that represents the root address for resources served
|
||||
by the ResourceProvider.
|
||||
|
||||
If you pass this value to goToURL the ResourceProvider will receive a request with the "/"
|
||||
path parameter. In response to this request the provider may typically want to return the
|
||||
contents of the "index.html" file.
|
||||
|
||||
@see ResourceProvider, Options::withResourceProvider, goToURL
|
||||
*/
|
||||
static const String& getResourceProviderRoot();
|
||||
|
||||
//==============================================================================
|
||||
/** On MacOS, iOS and Linux getResult will return a nullptr if the evaluation failed. In this
|
||||
case getError will return a non-null Error result, which contains more information about
|
||||
why the evaluation failed. It could be e.g. a syntax error or referencing an undefined
|
||||
object.
|
||||
|
||||
On Windows and Android getResult will always return a valid pointer to a variant, and
|
||||
getError will always return a nullptr. In case there was an evaluation failure, getResult
|
||||
returns a void variant, which is indistinguishable from a successful evaluation that yielded
|
||||
a null result. Unfortunately these platforms don't offer a way to detect evaluation errors.
|
||||
*/
|
||||
class EvaluationResult
|
||||
{
|
||||
public:
|
||||
/** A simple error type class. */
|
||||
struct Error
|
||||
{
|
||||
/** Error type. */
|
||||
enum class Type
|
||||
{
|
||||
/** Error occurring for a reason unknown to us. */
|
||||
unknown,
|
||||
|
||||
/** Error occurring because of a Javascript exception being thrown. */
|
||||
javascriptException,
|
||||
|
||||
/** Error occurring because the returned result cannot be serialised into a native
|
||||
type e.g. Promise.
|
||||
*/
|
||||
unsupportedReturnType
|
||||
};
|
||||
|
||||
Type type;
|
||||
String message;
|
||||
};
|
||||
|
||||
EvaluationResult (const var& result) : value { result }
|
||||
{
|
||||
}
|
||||
|
||||
EvaluationResult (const Error& error) : value { error }
|
||||
{
|
||||
}
|
||||
|
||||
const var* getResult() const
|
||||
{
|
||||
return std::get_if<var> (&value);
|
||||
}
|
||||
|
||||
const Error* getError() const
|
||||
{
|
||||
return std::get_if<Error> (&value);
|
||||
}
|
||||
|
||||
private:
|
||||
std::variant<var, Error> value;
|
||||
};
|
||||
|
||||
/** Callback type that can be passed optionally to evaluateJavascript. */
|
||||
using EvaluationCallback = std::function<void (EvaluationResult)>;
|
||||
|
||||
/** Evaluates the specified script in the context of the current state of the
|
||||
WebBrowserComponent.
|
||||
|
||||
If the optional callback is provided it will be called with the result of the evaluation.
|
||||
The callback will be called on the message thread.
|
||||
*/
|
||||
void evaluateJavascript (const String& script, EvaluationCallback callback = nullptr);
|
||||
|
||||
/** Emits an object on the frontend under the specified eventId.
|
||||
|
||||
Ids beginning with `__juce` are reserved for the framework implementation.
|
||||
|
||||
Example for listening to such events on the frontend:
|
||||
@code
|
||||
// Subscribing
|
||||
const removalToken = window.__JUCE__.backend.addEventListener(eventId, (objectFromBackend) => {
|
||||
console.log(objectFromBackend.message);
|
||||
});
|
||||
|
||||
// Unsubscribing
|
||||
window.__JUCE__.backend.removeEventListener(removalToken);
|
||||
@endcode
|
||||
*/
|
||||
void emitEventIfBrowserIsVisible (const Identifier& eventId, const var& object);
|
||||
|
||||
//==============================================================================
|
||||
/** This callback is called when the browser is about to navigate
|
||||
to a new location.
|
||||
|
||||
You can override this method to perform some action when the user
|
||||
tries to go to a particular URL. To allow the operation to carry on,
|
||||
return true, or return false to stop the navigation happening.
|
||||
*/
|
||||
virtual bool pageAboutToLoad (const String& newURL);
|
||||
|
||||
/** This callback happens when the browser has finished loading a page. */
|
||||
virtual void pageFinishedLoading (const String& url);
|
||||
|
||||
/** This callback happens when a network error was encountered while
|
||||
trying to load a page.
|
||||
|
||||
You can override this method to show some other error page by calling
|
||||
goToURL. Return true to allow the browser to carry on to the internal
|
||||
browser error page.
|
||||
|
||||
The errorInfo contains some platform dependent string describing the
|
||||
error.
|
||||
|
||||
Calling goToURL() inside this callback can encounter further network
|
||||
errors, potentially causing an infinite loop.
|
||||
*/
|
||||
virtual bool pageLoadHadNetworkError (const String& errorInfo);
|
||||
|
||||
/** This callback occurs when a script or other activity in the browser asks for
|
||||
the window to be closed.
|
||||
*/
|
||||
virtual void windowCloseRequest();
|
||||
|
||||
/** This callback occurs when the browser attempts to load a URL in a new window.
|
||||
This won't actually load the window but gives you a chance to either launch a
|
||||
new window yourself or just load the URL into the current window with goToURL().
|
||||
*/
|
||||
virtual void newWindowAttemptingToLoad (const String& newURL);
|
||||
|
||||
//==============================================================================
|
||||
/** @internal */
|
||||
void paint (Graphics&) override;
|
||||
/** @internal */
|
||||
void resized() override;
|
||||
/** @internal */
|
||||
void parentHierarchyChanged() override;
|
||||
/** @internal */
|
||||
void visibilityChanged() override;
|
||||
/** @internal */
|
||||
void focusGainedWithDirection (FocusChangeType, FocusChangeDirection) override;
|
||||
|
||||
/** @internal */
|
||||
class Impl;
|
||||
|
||||
private:
|
||||
std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
|
||||
{
|
||||
return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::group);
|
||||
}
|
||||
|
||||
std::unique_ptr<Impl> impl;
|
||||
bool blankPageShown = false, unloadPageWhenHidden;
|
||||
String lastURL;
|
||||
StringArray lastHeaders;
|
||||
MemoryBlock lastPostData;
|
||||
ListenerList<WebViewLifetimeListener> lifetimeListeners;
|
||||
|
||||
void reloadLastURL();
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WebBrowserComponent)
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
#if JUCE_WEB_BROWSER || DOXYGEN
|
||||
|
||||
/** This is a helper class for implementing AudioProcessorEditor::getControlParameterIndex with GUIs
|
||||
using a WebBrowserComponent.
|
||||
|
||||
Create an instance of this class and attach it to the WebBrowserComponent by using
|
||||
WebBrowserComponent::Options::withOptionsFrom.
|
||||
|
||||
In your frontend code you can use the ControlParameterIndexUpdater class, that emits
|
||||
controlParameterIndexChanged events based on the mouse movement, and control parameter index
|
||||
annotations attached to DOM elements.
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API WebControlParameterIndexReceiver : public OptionsBuilder<WebBrowserComponent::Options>
|
||||
{
|
||||
public:
|
||||
/* Returns the control parameter index last reported by the WebBrowserComponent GUI to be
|
||||
active.
|
||||
*/
|
||||
int getControlParameterIndex() const { return controlParameterIndex; }
|
||||
|
||||
//==============================================================================
|
||||
WebBrowserComponent::Options buildOptions (const WebBrowserComponent::Options& initialOptions) override
|
||||
{
|
||||
return initialOptions.withEventListener ("__juce__controlParameterIndexChanged",
|
||||
[this] (auto newIndex)
|
||||
{
|
||||
controlParameterIndex = (int) newIndex;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
int controlParameterIndex = -1;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
#if JUCE_WEB_BROWSER
|
||||
|
||||
WebSliderRelay::WebSliderRelay (StringRef nameIn)
|
||||
: name (nameIn)
|
||||
{
|
||||
}
|
||||
|
||||
void WebSliderRelay::setValue (float newValue)
|
||||
{
|
||||
using namespace detail;
|
||||
|
||||
DynamicObject::Ptr object { new DynamicObject };
|
||||
object->setProperty (WebSliderRelayEvents::Event::eventTypeKey,
|
||||
WebSliderRelayEvents::ValueChanged::eventId.toString());
|
||||
object->setProperty (WebSliderRelayEvents::ValueChanged::newValueKey, newValue);
|
||||
|
||||
value = newValue;
|
||||
emitEvent (object.get());
|
||||
}
|
||||
|
||||
float WebSliderRelay::getValue() const
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
void WebSliderRelay::addListener (Listener* l)
|
||||
{
|
||||
listeners.add (l);
|
||||
}
|
||||
|
||||
void WebSliderRelay::removeListener (Listener* l)
|
||||
{
|
||||
listeners.remove (l);
|
||||
}
|
||||
|
||||
WebBrowserComponent::Options WebSliderRelay::buildOptions (const WebBrowserComponent::Options& initialOptions)
|
||||
{
|
||||
return initialOptions
|
||||
.withEventListener (eventId, [this] (auto object) { handleEvent (object); })
|
||||
.withInitialisationData ("__juce__sliders", name)
|
||||
.withWebViewLifetimeListener (this);
|
||||
}
|
||||
|
||||
void WebSliderRelay::emitEvent (const var& payload)
|
||||
{
|
||||
if (browser != nullptr)
|
||||
browser->emitEventIfBrowserIsVisible (eventId, payload);
|
||||
}
|
||||
|
||||
void WebSliderRelay::webViewConstructed (WebBrowserComponent* browserIn)
|
||||
{
|
||||
browser = browserIn;
|
||||
listeners.call (&Listener::initialUpdateRequested, this);
|
||||
}
|
||||
|
||||
void WebSliderRelay::webViewDestructed (WebBrowserComponent*)
|
||||
{
|
||||
browser = nullptr;
|
||||
}
|
||||
|
||||
void WebSliderRelay::handleEvent (const var& event)
|
||||
{
|
||||
using namespace detail;
|
||||
|
||||
if (const auto sliderEvent = WebSliderRelayEvents::Event::extract (event))
|
||||
{
|
||||
if (const auto valueChanged = WebSliderRelayEvents::ValueChanged::extract (*sliderEvent))
|
||||
{
|
||||
if (! approximatelyEqual (std::exchange (value, valueChanged->newValue), valueChanged->newValue))
|
||||
listeners.call ([this] (Listener& l) { l.sliderValueChanged (this); });
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (const auto dragStarted = WebSliderRelayEvents::SliderDragStarted::extract (*sliderEvent))
|
||||
{
|
||||
listeners.call ([this] (Listener& l) { l.sliderDragStarted (this); });
|
||||
return;
|
||||
}
|
||||
|
||||
if (const auto dragEnded = WebSliderRelayEvents::SliderDragEnded::extract (*sliderEvent))
|
||||
{
|
||||
listeners.call ([this] (Listener& l) { l.sliderDragEnded (this); });
|
||||
return;
|
||||
}
|
||||
|
||||
if (const auto initialUpdate =
|
||||
WebSliderRelayEvents::InitialUpdateRequested::extract (*sliderEvent))
|
||||
{
|
||||
listeners.call ([this] (Listener& l) { l.initialUpdateRequested (this); });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const auto s = JSON::toString (event);
|
||||
jassertfalse;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
WebToggleButtonRelay::WebToggleButtonRelay (StringRef nameIn)
|
||||
: name (nameIn)
|
||||
{
|
||||
}
|
||||
|
||||
void WebToggleButtonRelay::setToggleState (bool newState)
|
||||
{
|
||||
using namespace detail;
|
||||
|
||||
DynamicObject::Ptr object { new DynamicObject };
|
||||
object->setProperty (WebToggleButtonRelayEvents::Event::eventTypeKey,
|
||||
WebToggleButtonRelayEvents::ToggleStateChanged::eventId.toString());
|
||||
object->setProperty (WebToggleButtonRelayEvents::ToggleStateChanged::valueKey, newState);
|
||||
|
||||
emitEvent (object.get());
|
||||
}
|
||||
|
||||
void WebToggleButtonRelay::addListener (Listener* l)
|
||||
{
|
||||
listeners.add (l);
|
||||
}
|
||||
|
||||
void WebToggleButtonRelay::removeListener (Listener* l)
|
||||
{
|
||||
listeners.remove (l);
|
||||
}
|
||||
|
||||
WebBrowserComponent::Options WebToggleButtonRelay::buildOptions (const WebBrowserComponent::Options& initialOptions)
|
||||
{
|
||||
return initialOptions
|
||||
.withEventListener (eventId, [this] (auto object) { handleEvent (object); })
|
||||
.withInitialisationData ("__juce__toggles", name)
|
||||
.withWebViewLifetimeListener (this);
|
||||
}
|
||||
|
||||
void WebToggleButtonRelay::emitEvent (const var& payload)
|
||||
{
|
||||
if (browser != nullptr)
|
||||
browser->emitEventIfBrowserIsVisible (eventId, payload);
|
||||
}
|
||||
|
||||
void WebToggleButtonRelay::webViewConstructed (WebBrowserComponent* browserIn)
|
||||
{
|
||||
browser = browserIn;
|
||||
listeners.call (&Listener::initialUpdateRequested);
|
||||
}
|
||||
|
||||
void WebToggleButtonRelay::webViewDestructed (WebBrowserComponent*)
|
||||
{
|
||||
browser = nullptr;
|
||||
}
|
||||
|
||||
void WebToggleButtonRelay::handleEvent (const var& event)
|
||||
{
|
||||
using namespace detail;
|
||||
|
||||
if (const auto buttonEvent = WebToggleButtonRelayEvents::Event::extract (event))
|
||||
{
|
||||
if (const auto toggleStateChanged = WebToggleButtonRelayEvents::ToggleStateChanged::extract (*buttonEvent))
|
||||
{
|
||||
listeners.call ([&toggleStateChanged] (Listener& l)
|
||||
{ l.toggleStateChanged (toggleStateChanged->value); });
|
||||
return;
|
||||
}
|
||||
|
||||
if (const auto initialUpdate = WebToggleButtonRelayEvents::InitialUpdateRequested::extract (*buttonEvent))
|
||||
{
|
||||
listeners.call ([] (Listener& l) { l.initialUpdateRequested(); });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const auto s = JSON::toString (event);
|
||||
jassertfalse;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
WebComboBoxRelay::WebComboBoxRelay (StringRef nameIn)
|
||||
: name (nameIn)
|
||||
{
|
||||
}
|
||||
|
||||
void WebComboBoxRelay::setValue (float newValue)
|
||||
{
|
||||
using namespace detail;
|
||||
|
||||
DynamicObject::Ptr object { new DynamicObject };
|
||||
object->setProperty (WebComboBoxRelayEvents::Event::eventTypeKey,
|
||||
WebComboBoxRelayEvents::ValueChanged::eventId.toString());
|
||||
object->setProperty (WebComboBoxRelayEvents::ValueChanged::valueKey, newValue);
|
||||
|
||||
emitEvent (object.get());
|
||||
}
|
||||
|
||||
void WebComboBoxRelay::addListener (Listener* l)
|
||||
{
|
||||
listeners.add (l);
|
||||
}
|
||||
|
||||
void WebComboBoxRelay::removeListener (Listener* l)
|
||||
{
|
||||
listeners.remove (l);
|
||||
}
|
||||
|
||||
WebBrowserComponent::Options WebComboBoxRelay::buildOptions (const WebBrowserComponent::Options& initialOptions)
|
||||
{
|
||||
return initialOptions
|
||||
.withEventListener (eventId, [this] (auto object) { handleEvent (object); })
|
||||
.withInitialisationData ("__juce__comboBoxes", name)
|
||||
.withWebViewLifetimeListener (this);
|
||||
}
|
||||
|
||||
void WebComboBoxRelay::emitEvent (const var& payload)
|
||||
{
|
||||
if (browser != nullptr)
|
||||
browser->emitEventIfBrowserIsVisible (eventId, payload);
|
||||
}
|
||||
|
||||
void WebComboBoxRelay::webViewConstructed (WebBrowserComponent* browserIn)
|
||||
{
|
||||
browser = browserIn;
|
||||
listeners.call (&Listener::initialUpdateRequested);
|
||||
}
|
||||
|
||||
void WebComboBoxRelay::webViewDestructed (WebBrowserComponent*)
|
||||
{
|
||||
browser = nullptr;
|
||||
}
|
||||
|
||||
void WebComboBoxRelay::handleEvent (const var& event)
|
||||
{
|
||||
using namespace detail;
|
||||
|
||||
if (const auto buttonEvent = WebComboBoxRelayEvents::Event::extract (event))
|
||||
{
|
||||
if (const auto valueChanged = WebComboBoxRelayEvents::ValueChanged::extract (*buttonEvent))
|
||||
{
|
||||
listeners.call ([&valueChanged] (Listener& l)
|
||||
{ l.valueChanged (valueChanged->value); });
|
||||
return;
|
||||
}
|
||||
|
||||
if (const auto initialUpdate = WebComboBoxRelayEvents::InitialUpdateRequested::extract (*buttonEvent))
|
||||
{
|
||||
listeners.call ([] (Listener& l) { l.initialUpdateRequested(); });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const auto s = JSON::toString (event);
|
||||
jassertfalse;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
@@ -0,0 +1,275 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE framework.
|
||||
Copyright (c) Raw Material Software Limited
|
||||
|
||||
JUCE is an open source framework subject to commercial or open source
|
||||
licensing.
|
||||
|
||||
By downloading, installing, or using the JUCE framework, or combining the
|
||||
JUCE framework with any other source code, object code, content or any other
|
||||
copyrightable work, you agree to the terms of the JUCE End User Licence
|
||||
Agreement, and all incorporated terms including the JUCE Privacy Policy and
|
||||
the JUCE Website Terms of Service, as applicable, which will bind you. If you
|
||||
do not agree to the terms of these agreements, we will not license the JUCE
|
||||
framework to you, and you must discontinue the installation or download
|
||||
process and cease use of the JUCE framework.
|
||||
|
||||
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
|
||||
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
|
||||
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
|
||||
|
||||
Or:
|
||||
|
||||
You may also use this code under the terms of the AGPLv3:
|
||||
https://www.gnu.org/licenses/agpl-3.0.en.html
|
||||
|
||||
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
|
||||
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
|
||||
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
#if JUCE_WEB_BROWSER || DOXYGEN
|
||||
|
||||
/** Helper class that relays audio parameter information to an object inside a WebBrowserComponent.
|
||||
|
||||
In order to create a relay you need to specify an identifier for the relayed state. This will
|
||||
result in a Javascript object becoming available inside the WebBrowserComponent under the
|
||||
provided identifier.
|
||||
|
||||
Pass the relay object to WebBrowserComponent::Options::withOptionsFrom() to associate it with
|
||||
a WebBrowserComponent instance.
|
||||
|
||||
You can then use a WebSliderParameterAttachment as you would a SliderAttachment, to attach the
|
||||
relay to a RangedAudioParameter. This will synchronise the state and events of the Javascript
|
||||
object with the audio parameter at all times.
|
||||
|
||||
@code
|
||||
// Add a relay to your AudioProcessorEditor members
|
||||
WebSliderRelay cutoffSliderRelay { "cutoffSlider" };
|
||||
WebBrowserComponent webComponent { WebBrowserComponent::Options{}::withOptionsFrom (cutoffSliderRelay) };
|
||||
@endcode
|
||||
|
||||
@code
|
||||
// In your Javascript GUI code you obtain an object from the framework
|
||||
import * as Juce from "juce-framework-frontend";
|
||||
const sliderState = Juce.getSliderState("cutoffSlider");
|
||||
@endcode
|
||||
|
||||
@see WebSliderParameterAttachment
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API WebSliderRelay : public OptionsBuilder<WebBrowserComponent::Options>,
|
||||
private WebViewLifetimeListener
|
||||
{
|
||||
public:
|
||||
/** Creating a relay will ensure that a Javascript object under the provided name will be
|
||||
available in the specified WebBrowserComponent's context. Use the frontend framework's
|
||||
getSliderState function with the same name to get a hold of this object.
|
||||
*/
|
||||
WebSliderRelay (StringRef nameIn);
|
||||
|
||||
//==============================================================================
|
||||
/** @internal */
|
||||
struct Listener : public SliderListener<WebSliderRelay>
|
||||
{
|
||||
virtual void initialUpdateRequested (WebSliderRelay*) = 0;
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
void setValue (float newValue);
|
||||
|
||||
/** @internal */
|
||||
float getValue() const;
|
||||
|
||||
/** @internal */
|
||||
void addListener (Listener* l);
|
||||
|
||||
/** @internal */
|
||||
void removeListener (Listener* l);
|
||||
|
||||
/** @internal */
|
||||
WebBrowserComponent::Options buildOptions (const WebBrowserComponent::Options& initialOptions) override;
|
||||
|
||||
/** @internal */
|
||||
void emitEvent (const var& payload);
|
||||
|
||||
private:
|
||||
void handleEvent (const var& event);
|
||||
void webViewConstructed (WebBrowserComponent*) override;
|
||||
void webViewDestructed (WebBrowserComponent*) override;
|
||||
|
||||
WebBrowserComponent* browser = nullptr;
|
||||
String name;
|
||||
float value{};
|
||||
Identifier eventId { "__juce__slider" + name };
|
||||
ListenerList<Listener> listeners;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (WebSliderRelay)
|
||||
JUCE_DECLARE_NON_MOVEABLE (WebSliderRelay)
|
||||
};
|
||||
|
||||
/** Helper class that relays audio parameter information to an object inside a WebBrowserComponent.
|
||||
|
||||
In order to create a relay you need to specify an identifier for the relayed state. This will
|
||||
result in a Javascript object becoming available inside the WebBrowserComponent under the
|
||||
provided identifier.
|
||||
|
||||
Pass the relay object to WebBrowserComponent::Options::withOptionsFrom() to associate it with
|
||||
a WebBrowserComponent instance.
|
||||
|
||||
You can then use a WebToggleButtonParameterAttachment as you would a ButtonParameterAttachment,
|
||||
to attach the relay to a RangedAudioParameter. This will synchronise the state and events of
|
||||
the Javascript object with the audio parameter at all times.
|
||||
|
||||
@code
|
||||
// Add a relay to your AudioProcessorEditor members
|
||||
WebToggleButtonRelay muteToggleRelay { "muteToggle" };
|
||||
WebBrowserComponent webComponent { WebBrowserComponent::Options{}::withOptionsFrom (muteToggleRelay) };
|
||||
@endcode
|
||||
|
||||
@code
|
||||
// In your Javascript GUI code you obtain an object from the framework
|
||||
import * as Juce from "juce-framework-frontend";
|
||||
const checkboxState = Juce.getToggleState("muteToggle");
|
||||
@endcode
|
||||
|
||||
@see WebToggleButtonParameterAttachment
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API WebToggleButtonRelay : public OptionsBuilder<WebBrowserComponent::Options>,
|
||||
private WebViewLifetimeListener
|
||||
{
|
||||
public:
|
||||
/** Creating a relay will ensure that a Javascript object under the provided name will be
|
||||
available in the specified WebBrowserComponent's context. Use the frontend framework's
|
||||
getToggleState function with the same name to get a hold of this object.
|
||||
*/
|
||||
WebToggleButtonRelay (StringRef nameIn);
|
||||
|
||||
//==============================================================================
|
||||
/** @internal */
|
||||
struct Listener
|
||||
{
|
||||
virtual ~Listener() = default;
|
||||
virtual void toggleStateChanged (bool) = 0;
|
||||
virtual void initialUpdateRequested() = 0;
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
void setToggleState (bool newState);
|
||||
|
||||
/** @internal */
|
||||
void addListener (Listener* l);
|
||||
|
||||
/** @internal */
|
||||
void removeListener (Listener* l);
|
||||
|
||||
/** @internal */
|
||||
WebBrowserComponent::Options buildOptions (const WebBrowserComponent::Options& initialOptions) override;
|
||||
|
||||
/** @internal */
|
||||
void emitEvent (const var& payload);
|
||||
|
||||
private:
|
||||
void handleEvent (const var& event);
|
||||
void webViewConstructed (WebBrowserComponent*) override;
|
||||
void webViewDestructed (WebBrowserComponent*) override;
|
||||
|
||||
WebBrowserComponent* browser = nullptr;
|
||||
String name;
|
||||
Identifier eventId { "__juce__toggle" + name };
|
||||
ListenerList<Listener> listeners;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (WebToggleButtonRelay)
|
||||
JUCE_DECLARE_NON_MOVEABLE (WebToggleButtonRelay)
|
||||
};
|
||||
|
||||
/** Helper class that relays audio parameter information to an object inside a WebBrowserComponent.
|
||||
|
||||
In order to create a relay you need to specify an identifier for the relayed state. This will
|
||||
result in a Javascript object becoming available inside the WebBrowserComponent under the
|
||||
provided identifier.
|
||||
|
||||
Pass the relay object to WebBrowserComponent::Options::withOptionsFrom() to associate it with
|
||||
a WebBrowserComponent instance.
|
||||
|
||||
You can then use a WebComboBoxParameterAttachment as you would a ComboBoxParameterAttachment,
|
||||
to attach the relay to a RangedAudioParameter. This will synchronise the state and events of
|
||||
the Javascript object with the audio parameter at all times.
|
||||
|
||||
@code
|
||||
// Add a relay to your AudioProcessorEditor members
|
||||
WebComboBoxRelay filterTypeComboRelay { "filterTypeCombo" };
|
||||
WebBrowserComponent webComponent { WebBrowserComponent::Options{}::withOptionsFrom (filterTypeComboRelay) };
|
||||
@endcode
|
||||
|
||||
@code
|
||||
// In your Javascript GUI code you obtain an object from the framework
|
||||
import * as Juce from "juce-framework-frontend";
|
||||
const comboBoxState = Juce.getComboBoxState("filterTypeCombo");
|
||||
@endcode
|
||||
|
||||
@see WebComboBoxParameterAttachment
|
||||
|
||||
@tags{GUI}
|
||||
*/
|
||||
class JUCE_API WebComboBoxRelay : public OptionsBuilder<WebBrowserComponent::Options>,
|
||||
private WebViewLifetimeListener
|
||||
{
|
||||
public:
|
||||
/** Creating a relay will ensure that a Javascript object under the provided name will be
|
||||
available in the specified WebBrowserComponent's context. Use the frontend framework's
|
||||
getComboBoxState function with the same name to get a hold of this object.
|
||||
*/
|
||||
WebComboBoxRelay (StringRef nameIn);
|
||||
|
||||
//==============================================================================
|
||||
/** @internal */
|
||||
struct Listener
|
||||
{
|
||||
virtual ~Listener() = default;
|
||||
virtual void valueChanged (float) = 0;
|
||||
virtual void initialUpdateRequested() = 0;
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
void setValue (float newValue);
|
||||
|
||||
/** @internal */
|
||||
void addListener (Listener* l);
|
||||
|
||||
/** @internal */
|
||||
void removeListener (Listener* l);
|
||||
|
||||
/** @internal */
|
||||
WebBrowserComponent::Options buildOptions (const WebBrowserComponent::Options& initialOptions) override;
|
||||
|
||||
/** @internal */
|
||||
void emitEvent (const var& payload);
|
||||
|
||||
private:
|
||||
void handleEvent (const var& event);
|
||||
void webViewConstructed (WebBrowserComponent*) override;
|
||||
void webViewDestructed (WebBrowserComponent*) override;
|
||||
|
||||
WebBrowserComponent* browser = nullptr;
|
||||
String name;
|
||||
Identifier eventId { "__juce__comboBox" + name };
|
||||
ListenerList<Listener> listeners;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (WebComboBoxRelay)
|
||||
JUCE_DECLARE_NON_MOVEABLE (WebComboBoxRelay)
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user