Skip to content

Commit

Permalink
Replace SideBarMenu with SideBarButton.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Mar 9, 2020
1 parent 15af905 commit 1b673b7
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 375 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ PRIVATE
ui/widgets/popup_menu.h
ui/widgets/scroll_area.cpp
ui/widgets/scroll_area.h
ui/widgets/side_bar_menu.cpp
ui/widgets/side_bar_menu.h
ui/widgets/side_bar_button.cpp
ui/widgets/side_bar_button.h
ui/widgets/shadow.cpp
ui/widgets/shadow.h
ui/widgets/tooltip.cpp
Expand Down
80 changes: 80 additions & 0 deletions ui/widgets/side_bar_button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#include "ui/widgets/side_bar_button.h"

#include "ui/effects/ripple_animation.h"

#include <QtGui/QtEvents>

namespace Ui {
namespace {

constexpr auto kMaxLabelLines = 3;

} // namespace

SideBarButton::SideBarButton(
not_null<QWidget*> parent,
const QString &title,
const style::SideBarButton &st)
: RippleButton(parent, st.ripple)
, _st(st)
, _text(_st.minTextWidth) {
_text.setText(_st.style, title);
setAttribute(Qt::WA_OpaquePaintEvent);
}

void SideBarButton::setActive(bool active) {
if (_active == active) {
return;
}
_active = active;
update();
}

void SideBarButton::setBadge(const QString &badge) {
if (_badge.toString() == badge) {
return;
}
_badge.setText(_st.badgeStyle, badge);
update();
}

int SideBarButton::resizeGetHeight(int newWidth) {
auto result = _st.minHeight;
const auto text = _text.countHeight(newWidth - _st.textSkip * 2);
const auto add = text - _st.style.font->height;
return result + std::max(add, 0);
}

void SideBarButton::paintEvent(QPaintEvent *e) {
auto p = Painter(this);
const auto clip = e->rect();

p.fillRect(clip, _active ? _st.textBgActive : _st.textBg);

RippleButton::paintRipple(p, 0, 0);

const auto &icon = _active ? _st.iconActive : _st.icon;
const auto x = (_st.iconPosition.x() < 0)
? (width() - icon.width()) / 2
: _st.iconPosition.x();
const auto y = (_st.iconPosition.y() < 0)
? (height() - icon.height()) / 2
: _st.iconPosition.y();
icon.paint(p, x, y, width());
p.setPen(_active ? _st.textFgActive : _st.textFg);
_text.drawElided(
p,
_st.textSkip,
_st.textTop,
(width() - 2 * _st.textSkip),
kMaxLabelLines,
style::al_top);
}

} // namespace Ui
103 changes: 103 additions & 0 deletions ui/widgets/side_bar_button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#pragma once

#include "ui/widgets/buttons.h"
#include "ui/text/text.h"

namespace style {
struct SideBarButton;
} // namespace style

namespace Ui {

class RippleAnimation;

class SideBarButton final : public Ui::RippleButton {
public:
SideBarButton(
not_null<QWidget*> parent,
const QString &title,
const style::SideBarButton &st);

void setActive(bool active);
void setBadge(const QString &badge);

int resizeGetHeight(int newWidth) override;

private:
void paintEvent(QPaintEvent *e) override;

const style::SideBarButton &_st;
Ui::Text::String _text;
Ui::Text::String _badge;
bool _active = false;

};
//
//class SideBarMenu final {
//public:
// struct Item {
// QString id;
// QString title;
// QString badge;
// not_null<const style::icon*> icon;
// not_null<const style::icon*> iconActive;
// int iconTop = 0;
// };
//
// SideBarMenu(not_null<QWidget*> parent, const style::SideBarMenu &st);
// ~SideBarMenu();
//
// [[nodiscard]] not_null<const Ui::RpWidget*> widget() const;
//
// void setGeometry(QRect geometry);
// void setItems(std::vector<Item> items);
// void setActive(
// const QString &id,
// anim::type animated = anim::type::normal);
// [[nodiscard]] rpl::producer<QString> activateRequests() const;
//
// [[nodiscard]] rpl::lifetime &lifetime();
//
//private:
// struct MenuItem {
// Item data;
// Ui::Text::String text;
// mutable std::unique_ptr<Ui::RippleAnimation> ripple;
// int top = 0;
// int height = 0;
// };
// void setup();
// void paint(Painter &p, QRect clip) const;
// [[nodiscard]] int countContentHeight(int width, int outerHeight);
//
// void mouseMove(QPoint position);
// void mousePress(Qt::MouseButton button);
// void mouseRelease(Qt::MouseButton button);
//
// void setSelected(int selected);
// void setPressed(int pressed);
// void addRipple(MenuItem &item, QPoint position);
// void repaint(const QString &id);
// [[nodiscard]] MenuItem *itemById(const QString &id);
//
// const style::SideBarMenu &_st;
//
// Ui::RpWidget _outer;
// const not_null<Ui::ScrollArea*> _scroll;
// const not_null<Ui::RpWidget*> _inner;
// std::vector<MenuItem> _items;
// int _selected = -1;
// int _pressed = -1;
//
// QString _activeId;
// rpl::event_stream<QString> _activateRequests;
//
//};

} // namespace Ui
Loading

0 comments on commit 1b673b7

Please sign in to comment.