Skip to content

Commit

Permalink
LoadPackWidget: make line stick when click
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Jan 24, 2024
1 parent 5f3b4b1 commit b184824
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
60 changes: 49 additions & 11 deletions src/ui/loadpackwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,57 @@ void LoadPackWidget::update()
_variants->clearChildren();
_curPackLabel = nullptr;
_curVariantLabel = nullptr;
_disableHoverSelect = false;

for (auto& pack : availablePacks) {
auto lbl = new Label(0, 0, 0, 0, _font, " " + pack.packName + " " + pack.version); // TODO: button instead of label
lbl->setGrow(1,0);
lbl->setTextAlignment(Label::HAlign::LEFT, Label::VAlign::MIDDLE);
lbl->setMinSize({64,lbl->getAutoHeight()});
lbl->setSize({_size.width/2,32}); // TODO; hbox with even split instead
lbl->setBackground({32,32,32});
lbl->setBackground(PACK_BG_DEFAULT);
_packs->addChild(lbl);

lbl->onMouseEnter += {this,[this,pack](void* s, int x, int y, unsigned buttons) {
if (_curPackLabel == s) return;
if (_curPackLabel) _curPackLabel->setBackground({32,32,32});
if (_curPackHover != s) {
if (_curPackHover == _curPackLabel && _disableHoverSelect)
_curPackHover->setBackground(PACK_BG_ACTIVE);
else if (_curPackHover)
_curPackHover->setBackground(PACK_BG_DEFAULT);

_curPackHover = (Label*)s;
if (_curPackHover == _curPackLabel && _disableHoverSelect)
_curPackHover->setBackground(PACK_BG_ACTIVE_HOVER);
else
_curPackHover->setBackground(PACK_BG_HOVER);
}
if (_disableHoverSelect || _curPackLabel == s)
return;

if (_curPackLabel)
_curPackLabel->setBackground(PACK_BG_DEFAULT);

_curPackLabel = (Label*)s;
_curPackLabel->setBackground({64,64,64});
_variants->clearChildren();
for (auto& variant: pack.variants) {
auto lbl = new Label(0,0,0,0, _font, " " + variant.name); // TODO: button instead of label
lbl->setGrow(1,0);
lbl->setTextAlignment(Label::HAlign::LEFT, Label::VAlign::MIDDLE);
lbl->setMinSize(lbl->getAutoSize());
lbl->setSize({_variants->getWidth(),32});
lbl->setBackground({64,64,64});
lbl->setMinSize({64,lbl->getAutoHeight()});
lbl->setSize({_variants->getWidth(),32}); // FIXME: this should be done by vbox
lbl->setBackground(VARIANT_BG_DEFAULT);
_variants->addChild(lbl);
lbl->onMouseLeave += {this,[this,variant](void *s) {
if (_curVariantLabel != s) return;
_curVariantLabel->setBackground({64,64,64});
_curVariantLabel->setBackground(VARIANT_BG_DEFAULT);
_curVariantLabel = nullptr;
}};
std::string path = pack.path;
lbl->onMouseEnter += {this,[this](void *s, int x, int y, unsigned buttons) {
if (!s || s==_curVariantLabel) return;
if (_curVariantLabel) _curVariantLabel->onMouseLeave.emit(_curVariantLabel);
_curVariantLabel = (Label*)s;
_curVariantLabel->setBackground({96,96,96});
_curVariantLabel->setBackground(VARIANT_BG_HOVER);
}};
lbl->onClick += {this,[this,path,variant](void *s, int x, int y, int button) {
if (button == MouseButton::BUTTON_LEFT) {
Expand All @@ -93,10 +111,30 @@ void LoadPackWidget::update()
auto spacer = new Label(0, 0, 0, 0, nullptr, "");
spacer->setGrow(1,1);
_variants->addChild(spacer);
_variants->relayout();
_main->relayout();
_main->relayout(); // changes width of _variants
_variants->relayout(); // changes width of labels
_main->relayout(); // fix split in hbox // FIXME: this should not be required
}};

lbl->onClick += {this, [this](void* s, int x, int y, int buttons) {
_disableHoverSelect = false;
((Label*)s)->onMouseEnter.emit(s, x, y, (unsigned)buttons);
_disableHoverSelect = true;
if (_curPackLabel)
_curPackLabel->setBackground(PACK_BG_ACTIVE_HOVER);
}};
}

_packs->onMouseLeave += {this, [this](void* s) {
if (_disableHoverSelect && _curPackHover) {
if (_curPackHover == _curPackLabel) {
_curPackHover->setBackground(PACK_BG_ACTIVE);
} else {
_curPackHover->setBackground(PACK_BG_DEFAULT);
}
}
}};

if (availablePacks.empty()) {
auto lbl = new Label(0, 0, 0, 0, _font, "No packs installed!");
lbl->setGrow(1,1);
Expand Down
9 changes: 9 additions & 0 deletions src/ui/loadpackwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class LoadPackWidget : public SimpleContainer {
HBox *_main;
Label *_curPackLabel = nullptr;
Label *_curVariantLabel = nullptr;
Label *_curPackHover = nullptr;
bool _disableHoverSelect = false;

static constexpr Color PACK_BG_DEFAULT = {32, 32, 32};
static constexpr Color PACK_BG_ACTIVE = {32, 128, 32};
static constexpr Color PACK_BG_HOVER = {64, 64, 64};
static constexpr Color PACK_BG_ACTIVE_HOVER = {64, 160, 64};
static constexpr Color VARIANT_BG_DEFAULT = {64, 64, 64};
static constexpr Color VARIANT_BG_HOVER = {96, 96, 96};
};

} // namespace Ui
Expand Down

0 comments on commit b184824

Please sign in to comment.