In my small GUI library, different things can cause a Widget
to become invisible.
- A window may be collapsed. All children will must recursively become invisible.
- The user may manually hide a widget.
- A widget may become "excluded". Think about a "shutter" list-box: the shutter and its contents will be shown only when the list-box button is pressed.
This is my current solution:
class Widget {
// ...
bool collapsed;
bool hidden;
bool excluded;
public:
bool isVisible() { return !collapsed && !hidden && !excluded; }
void hide() { hidden = true; }
void show() { hidden = false; }
// ...
};
I don't like the fact that three booleans value are used to represent the same thing.
I thought about using an int
:
class Widget {
int hiddenLevel{0};
bool isVisible() { return hiddenLevel == 0; }
void hide() { ++hiddenLevel; }
void show() { --hiddenLevel; }
};
But the user may accidentally call hide()
twice.
Is there an elegant way to avoid repetition of variables that basically do the same thing? I don't care if the widget is collapsed
, hidden
or excluded
, I just want to know if it's visible or not, and revert the effects of collapsing it, hiding it, or excluding it.