Skip to content

Commit

Permalink
use EnumBitset for CListControl classes
Browse files Browse the repository at this point in the history
  • Loading branch information
scheffle committed Mar 15, 2024
1 parent a2ee09d commit 73b510f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
4 changes: 3 additions & 1 deletion vstgui/lib/controls/clistcontrol.cpp
Expand Up @@ -187,7 +187,9 @@ void CListControl::drawRect (CDrawContext* context, const CRect& updateRect)
rowSize.setHeight (impl->rowDescriptions[row].height);
if (updateRect.rectOverlap (rowSize))
{
int32_t flags = selectedRow == row ? IListControlDrawer::Row::Selected : 0;
IListControlDrawer::Row::Flags flags;
if (selectedRow == row)
flags = IListControlDrawer::Row::Selected;
if (impl->rowDescriptions[row].flags & CListControlRowDesc::Selectable)
flags |= IListControlDrawer::Row::Selectable;
if (impl->hoveredRow && *impl->hoveredRow == row + getMinRowIndex ())
Expand Down
28 changes: 17 additions & 11 deletions vstgui/lib/controls/clistcontrol.h
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "../optional.h"
#include "../enumbitset.h"
#include "ccontrol.h"

//------------------------------------------------------------------------
Expand Down Expand Up @@ -84,20 +85,22 @@ class CListControl final : public CControl
//------------------------------------------------------------------------
struct CListControlRowDesc
{
enum Flags
enum Flag : uint32_t
{
/** Indicates that the row is selectable */
Selectable = 1 << 0,
/** Indicates that the row should be redrawn when the mouse hovers it */
Hoverable = 1 << 1,
};
using Flags = EnumBitset<Flag, true>;

/** The height of the row */
CCoord height {0};
/** The flags of the row, see the Flags enum above */
int32_t flags {Selectable};
Flags flags {Selectable};

CListControlRowDesc () = default;
CListControlRowDesc (CCoord h, int32_t f) : height (h), flags (f) {}
CListControlRowDesc (CCoord h, Flags f) : height (h), flags (f) {}
};

//------------------------------------------------------------------------
Expand All @@ -115,13 +118,14 @@ class IListControlDrawer : virtual public IReference

struct Row
{
enum
enum Flag : uint32_t
{
Selectable = 1 << 0,
Selected = 1 << 1,
Hovered = 1 << 2,
LastRow = 1 << 3,
};
using Flags = EnumBitset<Flag, true>;

operator int32_t () const { return getIndex (); }
int32_t getIndex () const { return index; }
Expand All @@ -131,11 +135,11 @@ class IListControlDrawer : virtual public IReference
bool isHovered () const { return (flags & Hovered) != 0; }
bool isLastRow () const { return (flags & LastRow) != 0; }

Row (int32_t index, int32_t flags) : index (index), flags (flags) {}
Row (int32_t index, Flags flags) : index (index), flags (flags) {}

private:
int32_t index;
int32_t flags;
Flags flags;
};

virtual void drawBackground (CDrawContext* context, CRect size) = 0;
Expand Down Expand Up @@ -168,23 +172,25 @@ class StaticListControlConfigurator : public IListControlConfigurator,
public NonAtomicReferenceCounted
{
public:
using Flags = CListControlRowDesc::Flags;

StaticListControlConfigurator (CCoord inRowHeight,
int32_t inFlags = CListControlRowDesc::Selectable |
CListControlRowDesc::Hoverable)
Flags inFlags = {CListControlRowDesc::Selectable,
CListControlRowDesc::Hoverable})
: rowHeight (inRowHeight), flags (inFlags)
{
}

void setRowHeight (CCoord height) { rowHeight = height; }
void setFlags (int32_t f) { flags = f; }
void setFlags (Flags f) { flags = f; }
CCoord getRowHeight () const { return rowHeight; }
int32_t getFlags () const { return flags; }
Flags getFlags () const { return flags; }

CListControlRowDesc getRowDesc (int32_t row) const override { return {rowHeight, flags}; }

private:
CCoord rowHeight;
int32_t flags;
Flags flags;
};

//------------------------------------------------------------------------
Expand Down
Expand Up @@ -151,7 +151,7 @@ class WeekdaysListConfigurator : public StaticListControlConfigurator
CListControlRowDesc getRowDesc (int32_t row) const override
{
if (row == 0)
return {getRowHeight () * 2., 0};
return {getRowHeight () * 2., {}};
return {getRowHeight (), getFlags ()};
}
};
Expand Down
11 changes: 6 additions & 5 deletions vstgui/tests/unittest/lib/controls/clistcontrol_test.cpp
Expand Up @@ -11,9 +11,10 @@
namespace VSTGUI {

//------------------------------------------------------------------------
static SharedPointer<CListControl> createTestListControl (
CCoord rowHeight, int32_t numRows = 10,
int32_t rowFlags = CListControlRowDesc::Selectable | CListControlRowDesc::Hoverable)
static SharedPointer<CListControl>
createTestListControl (CCoord rowHeight, int32_t numRows = 10,
CListControlRowDesc::Flags rowFlags = CListControlRowDesc::Flags {
CListControlRowDesc::Selectable | CListControlRowDesc::Hoverable})
{
auto listControl = makeOwned<CListControl> (CRect (0, 0, 100, 100));
auto config = makeOwned<StaticListControlConfigurator> (rowHeight, rowFlags);
Expand Down Expand Up @@ -121,7 +122,7 @@ TEST_CASE (CListControlTest, KeyDownOnUnselectableRows)
{
constexpr auto rowHeight = 20;
constexpr auto numRows = 20;
auto listControl = createTestListControl (rowHeight, numRows, 0);
auto listControl = createTestListControl (rowHeight, numRows, {});
listControl->setValue (2.f);
EXPECT (listControl->getValue () == 2.f);

Expand All @@ -135,7 +136,7 @@ TEST_CASE (CListControlTest, KeyWithModifier)
{
constexpr auto rowHeight = 20;
constexpr auto numRows = 20;
auto listControl = createTestListControl (rowHeight, numRows, 0);
auto listControl = createTestListControl (rowHeight, numRows, {});

listControl->setValue (1.f);

Expand Down
9 changes: 6 additions & 3 deletions vstgui/uidescription/viewcreator/stringlistcontrolcreator.cpp
Expand Up @@ -147,9 +147,12 @@ bool StringListControlCreator::apply (CView* view, const UIAttributes& attribute
configurator->setRowHeight (d);
bool b;
if (attributes.getBooleanAttribute (kAttrStyleHover, b))
configurator->setFlags (CListControlRowDesc::Selectable |
(b ? CListControlRowDesc::Hoverable : 0));

{
CListControlRowDesc::Flags f (CListControlRowDesc::Selectable);
if (b)
f |= CListControlRowDesc::Hoverable;
configurator->setFlags (f);
}
control->invalid ();
control->recalculateLayout ();

Expand Down

0 comments on commit 73b510f

Please sign in to comment.