-
-
Save sudara/4b6b9d16a7741d27883a57d2387cd1ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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