Skip to content

Instantly share code, notes, and snippets.

@sudara
Created April 29, 2022 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudara/4b6b9d16a7741d27883a57d2387cd1ac to your computer and use it in GitHub Desktop.
Save sudara/4b6b9d16a7741d27883a57d2387cd1ac to your computer and use it in GitHub Desktop.
// This is convoluted, but....does the job.
class SaneCaret : public juce::CaretComponent
{
// this only exists because our CaretComponent can't inherit from timer...
class Timer : public juce::Timer
{
public:
explicit Timer (SaneCaret& caret) : saneCaret (caret) {}
void timerCallback() override
{
saneCaret.toggle();
}
private:
SaneCaret& saneCaret;
};
public:
explicit SaneCaret (Component* keyFocusOwner) : CaretComponent (keyFocusOwner), textField (keyFocusOwner)
{
setPaintingIsUnclipped (true);
setInterceptsMouseClicks (false, false);
}
void paint (juce::Graphics& g) override
{
g.setColour (Colors::cursor);
g.fillRect (getLocalBounds());
}
void toggle()
{
setVisible (shouldShow() && !isVisible());
}
[[nodiscard]] bool shouldShow() const
{
return textField == nullptr || (textField->hasKeyboardFocus (false) && !textField->isCurrentlyBlockedByAnotherModalComponent());
}
void setCaretPosition (const juce::Rectangle<int>& characterArea) override
{
timer.startTimer (550);
setVisible (shouldShow());
setBounds (characterArea.withWidth (1));
}
private:
Component* textField;
Timer timer { *this };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment