LookAndFeel & resized?

Hi,

I am wondering: what is the best way to make your resized() functions depend on the current LookAndFeel ?

I have an application, where the user can choose between two LookAndFeels. One “retro” and one “modern”.

My application has several sliders. When the user chooses the “retro” look, I want the sliders to be positioned close together. But if the user chooses the “modern” look, there should be more space between the sliders.

The position of the sliders I set in my resized() function ( by calling slider.setBounds() ). But as far as I can see, the JUCE LookAndFeel class only has paint() functions, but no resized() functions.

What is a good way to make the resized() functions depend on your LookAndFeel?

Have you tried implementing an override of:

virtual void Component::lookAndFeelChanged 	( 		) 	

https://docs.juce.com/master/classComponent.html#a74af5830b815f6f985768a5673d01cf2

(sorry, I don’t know why the embedded link won’t preview, but click on it.)

With this, I think you can respond to a lookAndFeel change by calling your own resized method.

1 Like

Ah, thanks for the hint. :slight_smile:

Another idea, if both LookAndFeel are your owns, you can change Slider::LookAndFeelMethods::getSliderLayout()

That will allow you to place the knobs slightly different (or smaller) in a specific layout).

1 Like

Oh, interesting.
Is this new? Missed getSliderLayout.

About 6 years now :slight_smile:

Haha ok :smiley:

There’s nothing stopping you adding custom methods to your sub-class of LookAndFeel.

You could create a base that has pure-virtual declarations for a whatever method(s) you need, and then your Retro and Modern L&Fs would implement them accordingly.

Currently I am doing this:
In the resized() function I get the look and feel, and try to cast it to my “Retro” look and feel. If that works, I know the UI is using the retro look. If it fails, I know that the UI is using the modern look:

void resized()
{
   if (auto lf = dynamic_cast<RetroLnF*>(&getLookAndFeel))
   {
      //... do retro positioning
   }
   else
   {
      //... do modern positioning
   }
}

There’s nothing stopping you adding custom methods to your sub-class of LookAndFeel.

Doesn’t that mean that you would need a dynamic_cast inside paint(), in order to access your custom method?

Depending on the type of Component (think Slider), the paint() method could get called quite frequently. I’m concerned that the dynamic_cast could become expensive.

Could you just hold a (base class) pointer to the LookAndFeel that is updated in an overridden Component::lookAndFeelChanged perhaps?

1 Like

Indeed! I think that is a good solution.

The cost of the dynamic cast will be negligible compared to the cost of drawing graphics! Even if your rendering is super efficient, you still can’t beat the screen’s refresh rate so chances are your optimizations aren’t actually doing anything to benefit the user.

3 Likes