setSelectAllWhenFocused broken?


#1

Hi Guys,

Looks something has been broken in Juce recently.
I have a juce::TextEditor which uses setSelectAllWhenFocused and then grabKeyboardFocus

It uses to select the text and it doesn’t anymore.

Any ideas ?

Thanks !


#2

It seems to work for things like the textboxes in the projucer… Are you sure it’s not something else you’ve changed in your own code?


#3

Yep. Didn’t touch this code since at least 1 year.
Checking juce code right now.
Very strange


#4

in my Slider::mouseDoubleClick

 // text input for the value 
  mpTextEditor.reset(new TextEditor("EditTextBox"));

  mpTextEditor->setText(getTextFromValue(getValue()));
  mpTextEditor->addListener(this);
  mpTextEditor->setMultiLine(false);
  mpTextEditor->setSelectAllWhenFocused(true);
  mpTextEditor->setKeyboardType(juce::TextEditor::numericKeyboard);

  int width = std::max(80, mpTextEditor->getTextWidth());
  mpTextEditor->setSize(width, mpTextEditor->getTextHeight());
  
  if (juce::Component *pModalComponent = juce::Component::getCurrentlyModalComponent(0))
  {
    pModalComponent->addAndMakeVisible(mpTextEditor.get());
  }
  else
  {
    getTopLevelComponent()->addAndMakeVisible(mpTextEditor.get());
  }

  juce::Point<int> centre = mpTextEditor->getLocalPoint(this, getLocalBounds().getCentre());
  mpTextEditor->setCentrePosition(centre.getX(), centre.getY());

  mpTextEditor->grabKeyboardFocus();

Don’t see much what could be bogus here.

As opposed to juce::Label textEditor I am not using modal stuff.

I tried using as well setHighlightedRegion after the keyboard focus like in Label but same issue.


#5

Looks like to be related to some textWasChangedByValue call that clears the caret position called by ValueSource::sendChangeMessage


#6

calling mpTextEditor->setText(getTextFromValue(getValue(), false)); don’t fix the issue


#7

TextEditor::setText change the value source that trigger an asyncupdater that will later call textWasChangedByValue and move the carret resulting in a change of selection after the select all was made.

Something is wrong here.

Too much async updating :slight_smile:


#8

You guys should probably not send the changes for the TextEditor itself when it is the one changing it
or do it synchronously
Issue here is that Value::handleAysncUpdate() is private…


#9

@jules, do you confirm ?

Thanks !


#10

Well… Having a quick look, that doesn’t really seem to agree with the code you posted. The textWasChangedByValue() method will do nothing unless other clients are sharing the TextEditor’s Value, and if you call setText(), that won’t happen… ?

[EDIT] oh, hang on, you didn’t say that was the cause…


#11

TextEditor listen to its own Value so he gets notified by its own changes but it an async way which breaks the settings made synchronously.


#12

OK, well the thing that changed to cause this has nothing to do with TextEditor, but some code in TextEditor was using a dodgy trick to see whether the Value was being shared externally by looking at whether its reference count was 1. However, some other code changed such that a reference to a Value is now held for safety while callbacks happen, making the ref-count always higher than 1, and this the TextEditor invokes an unnecessary callback. TBH I’m pretty stumped about what a good solution to this would be! I’ll carry on having a think about it…


#13

My idea would be that you need to be able to say explicitly if Value set are sync/async
and in that case, have it sync.
i.e add 2nd parameter to Value::setValue (default to false in order to not break current code)


#14

Or check that the text hasn’t changed in the callback and don’t do anything


#15

Hmm, it already checks whether the text has changed - textWasChangedByValue() calls setText() and that contains a check. I’ve not spent too long staring at this, but can’t see any obvious easy ways to sort it out - where were you suggesting the check should happen?


#16

Looks my issue is because styleChanged is true so the code is processed which happens
once parentHierarchyChanged -> lookAndFeelChanged -> colourChanged

so i I call my setText after the addAndMakeVisible of my TextEditor, it works back again…