Author Topic: Problems with the property Control.minWidth  (Read 5709 times)

Offline Andres.Pozo

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 927
Problems with the property Control.minWidth
« on: 2013 March 14 03:59:21 »
I think that the following could be a bug but, since there is no documentation of PJSR, I could be misled.

My understanding is that if I set the minWidth o minHeight properties in a control, the automatic layout would never size it with dimensions smaller than the minimum. The property minWidth seems to work this way, but the behavior of minHeight is different, in fact it seems to do nothing.

I have written a short script that shows a dialog with a coloured frame three times:
  • The first it doesn't set minWidth nor minHeight in the frame. The initial size of the dialog is small and the frame cannot be seen because the frame has size (0,0).
  • The second dialog sets minWith but not minHeight. This time the initial size of the dialog is much wider than before but the frame cannot be seen yet because it has size (400,0). The dialog can be resized but the witdh has a lower limit.
  • The third dialog sets both minWidth and minHeight. This time I would expect to see a square dialog with the colored frame inside and I should not be able to resize it smaller than the initial dimensions. However, the behavior of the dialog is the same as when minHeight was not set.
I don't know if this is a bug or something I don't understand correctly.

The code is this:
Code: [Select]
#include <pjsr/Sizer.jsh>

function TestDialog1(minWidth, minHeight)
{
   this.__base__ = Dialog;
   this.__base__();

   this.frame = new Frame(this);
   this.frame.backgroundColor=0xff00ff00;
   if(minWidth>0)
      this.frame.minWidth=400;
   if(minHeight>0)
      this.frame.minHeight=400;

   // Stack of rows
   this.sizer = new VerticalSizer;
   this.sizer.margin = 8;
   this.sizer.spacing = 6;
   this.sizer.add(this.frame);

   this.windowTitle = format("mw=%d mh=%d",minWidth,minHeight);
   this.adjustToContents();
}
TestDialog1.prototype = new Dialog;

new TestDialog1(0,0).execute();
new TestDialog1(400,0).execute();
new TestDialog1(400,400).execute();

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Problems with the property Control.minWidth
« Reply #1 on: 2013 March 14 04:18:30 »
Yes, this is a bug. Using the Control.setMinSize() method works correctly:

Code: [Select]
#include <pjsr/Sizer.jsh>

function TestDialog1(minWidth, minHeight)
{
   this.__base__ = Dialog;
   this.__base__();

   this.frame = new Frame(this);
   this.frame.backgroundColor=0xff00ff00;
   this.frame.setMinSize( (minWidth > 0) ? 400 : 0, (minHeight > 0) ? 400 : 0 );
   /*
   if(minWidth>0)
      this.frame.minWidth=400;
   if(minHeight>0)
      this.frame.minHeight=400;
   */

   // Stack of rows
   this.sizer = new VerticalSizer;
   this.sizer.margin = 8;
   this.sizer.spacing = 6;
   this.sizer.add(this.frame);

   this.windowTitle = format("mw=%d mh=%d",minWidth,minHeight);
   this.adjustToContents();
}
TestDialog1.prototype = new Dialog;

new TestDialog1(0,0).execute();
new TestDialog1(400,0).execute();
new TestDialog1(400,400).execute();

Just for the sake of completeness (and fun), note that the same can be accomplished with pure CSS:

Code: [Select]
#include <pjsr/Sizer.jsh>

function TestDialog1(minWidth, minHeight)
{
   this.__base__ = Dialog;
   this.__base__();

   this.frame = new Frame(this);
   this.frame.styleSheet = "QWidget { background: #00ff00; " +
         "min-width: " + ((minWidth > 0) ? 400 : 0).toString() + "px; " +
         "min-height: " + ((minHeight > 0) ? 400 : 0).toString() + "px; }";
   /*
   this.frame.backgroundColor=0xff00ff00;
   this.frame.setMinSize( (minWidth > 0) ? 400 : 0, (minHeight > 0) ? 400 : 0 );
   */
   /*
   if(minWidth>0)
      this.frame.minWidth=400;
   if(minHeight>0)
      this.frame.minHeight=400;
   */

   // Stack of rows
   this.sizer = new VerticalSizer;
   this.sizer.margin = 8;
   this.sizer.spacing = 6;
   this.sizer.add(this.frame);

   this.windowTitle = format("mw=%d mh=%d",minWidth,minHeight);
   this.adjustToContents();
}
TestDialog1.prototype = new Dialog;

new TestDialog1(0,0).execute();
new TestDialog1(400,0).execute();
new TestDialog1(400,400).execute();

But of course calling setMinSize(), or setting the minWidth and minHeight properties (when I fix this bug) are obviously much simpler and more readable.

Thanks for catching this one!
Juan Conejero
PixInsight Development Team
http://pixinsight.com/