Skip to content

Commit

Permalink
Make every widget scrollable
Browse files Browse the repository at this point in the history
  • Loading branch information
dancazarin committed Dec 23, 2024
1 parent 2a9e11a commit 7f08f45
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 333 deletions.
5 changes: 3 additions & 2 deletions examples/showcase/src/Messenger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ RC<Widget> ShowcaseMessenger::build(RC<Notifications> notifications) {
painter = Painter(&backgroundPainter),

rcnew VLayout{
flexGrow = 1,
alignSelf = AlignSelf::Stretch,
flexGrow = 1,
alignSelf = AlignSelf::Stretch,
scrollBarColor = 0x32a852_rgb,
rcnew VScrollBox{
flexGrow = 1,
alignSelf = AlignSelf::Stretch,
Expand Down
40 changes: 38 additions & 2 deletions include/brisk/gui/GUI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ template <typename Callable, typename R, typename... Args>
concept invocable_r = std::is_invocable_r_v<R, Callable, Args...>;

namespace Internal {
constexpr inline size_t numProperties = 101;
constexpr inline size_t numProperties = 104;
extern const std::string_view propNames[numProperties];

template <typename T, int subfield = -1>
Expand Down Expand Up @@ -979,6 +979,14 @@ class WIDGET Widget : public BindingObject<Widget, &uiThread> {

void reveal();

bool setScrollOffset(Point newOffset);
bool setScrollOffset(Orientation orientation, int newOffset);
Point scrollOffset() const;
int scrollOffset(Orientation orientation) const;
bool hasScrollBar(Orientation orientation) const noexcept;
int scrollSize(Orientation orientation) const noexcept;
Size scrollSize() const noexcept;

using enum PropFlags;

friend struct Rules;
Expand Down Expand Up @@ -1032,6 +1040,7 @@ class WIDGET Widget : public BindingObject<Widget, &uiThread> {
Internal::Transition<ColorF> m_borderColor{ Palette::transparent };
Internal::Transition<ColorF> m_color{ Palette::white };
Internal::Transition<ColorF> m_shadowColor{ Palette::black.multiplyAlpha(0.4f) };
Internal::Transition<ColorF> m_scrollBarColor{ Palette::grey };
float m_backgroundColorTransition = 0;
float m_borderColorTransition = 0;
float m_colorTransition = 0;
Expand Down Expand Up @@ -1074,6 +1083,8 @@ class WIDGET Widget : public BindingObject<Widget, &uiThread> {
Internal::Resolve<Length> m_tabSize{ 40, 40 };
Internal::Resolve<Length> m_letterSpacing{ 0_px, 0.f };
Internal::Resolve<Length> m_wordSpacing{ 0_px, 0.f };
Internal::Resolve<Length> m_scrollBarThickness{ 8_px, 8.f };
Internal::Resolve<Length> m_scrollBarRadius{ 0_px, 0.f };

// uint8_t
mutable WidgetState m_state = WidgetState::None;
Expand All @@ -1091,7 +1102,7 @@ class WIDGET Widget : public BindingObject<Widget, &uiThread> {
Placement m_placement = Placement::Normal;
ZOrder m_zorder = ZOrder::Normal;
WidgetClip m_clip = WidgetClip::All;
Overflow m_overflow = Overflow::Hidden;
Overflow m_overflow = Overflow::None;
AlignContent m_alignContent = AlignContent::FlexStart;
Wrap m_flexWrap = Wrap::NoWrap;
BoxSizingPerAxis m_boxSizing = BoxSizingPerAxis::BorderBox;
Expand All @@ -1112,6 +1123,19 @@ class WIDGET Widget : public BindingObject<Widget, &uiThread> {
bool m_stateTriggersRestyle = false;
bool m_isHintExclusive = false;

std::array<bool, 2> m_scrollBarDrag{ false, false };
int m_savedScrollOffset = 0;

struct ScrollBarGeometry {
Rectangle track;
Rectangle thumb;
};

Range<int> scrollBarRange(Orientation orientation) const noexcept;
ScrollBarGeometry scrollBarGeometry(Orientation orientation) const noexcept;

void updateScrollAxes();

std::bitset<Internal::propStateBits * Internal::numProperties> m_propStates;
Internal::PropState getPropState(size_t index) const noexcept;
void setPropState(size_t index, Internal::PropState state) noexcept;
Expand Down Expand Up @@ -1170,10 +1194,13 @@ class WIDGET Widget : public BindingObject<Widget, &uiThread> {
void doPaint(Canvas& canvas) const;
virtual void paint(Canvas& canvas) const;
virtual void postPaint(Canvas& canvas) const;
virtual void paintScrollBar(Canvas& canvas, Orientation orientation,
const ScrollBarGeometry& geometry) const;
void paintBackground(Canvas& canvas, Rectangle rect) const;
void paintHint(Canvas& canvas) const;
void paintFocusFrame(Canvas& canvas) const;
void paintChildren(Canvas& canvas) const;
void paintScrollBars(Canvas& canvas) const;

///////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -1422,6 +1449,11 @@ class WIDGET Widget : public BindingObject<Widget, &uiThread> {
padding;
GUIProperty<100, OpenTypeFeatureFlags, AffectLayout | AffectFont | Inheritable, &This::m_fontFeatures>
fontFeatures;

GUIProperty<101, ColorF, Transition | Inheritable, &This::m_scrollBarColor> scrollBarColor;
GUIProperty<102, Length, Resolvable, &This::m_scrollBarThickness> scrollBarThickness;
GUIProperty<103, Length, Resolvable, &This::m_scrollBarRadius> scrollBarRadius;

Property<This, bool, &This::m_state, &This::isDisabled, &This::setDisabled> disabled;
BRISK_PROPERTIES_END
};
Expand Down Expand Up @@ -1540,6 +1572,10 @@ extern const Argument<Tag::PropArg<decltype(Widget::disabled)>> disabled;

extern const Argument<Tag::PropArg<decltype(Widget::fontFeatures)>> fontFeatures;

extern const Argument<Tag::PropArg<decltype(Widget::scrollBarColor)>> scrollBarColor;
extern const Argument<Tag::PropArg<decltype(Widget::scrollBarThickness)>> scrollBarThickness;
extern const Argument<Tag::PropArg<decltype(Widget::scrollBarRadius)>> scrollBarRadius;

} // namespace Arg

namespace Tag {
Expand Down
13 changes: 5 additions & 8 deletions include/brisk/gui/Layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,13 @@ constexpr auto operator+(Wrap value) noexcept {
}

enum class Overflow : uint8_t {
Visible,
Hidden,
ScrollX,
ScrollY,
ScrollBoth,
None = 0,
ScrollX = 1,
ScrollY = 2,
ScrollBoth = 3,
};

constexpr auto operator+(Overflow value) noexcept {
return static_cast<std::underlying_type_t<decltype(value)>>(value);
}
BRISK_FLAGS(Overflow)

enum class Gutter : uint8_t {
Column,
Expand Down
51 changes: 0 additions & 51 deletions include/brisk/widgets/ScrollBar.hpp

This file was deleted.

26 changes: 0 additions & 26 deletions include/brisk/widgets/ScrollBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#pragma once

#include <brisk/gui/GUI.hpp>
#include "ScrollBar.hpp"

namespace Brisk {

Expand All @@ -36,36 +35,11 @@ class WIDGET ScrollBox : public Widget {
endConstruction();
}

std::shared_ptr<ScrollBar> scrollBar() const;
bool scrollable() const;

protected:
Orientation m_orientation;
float m_scrollPosition = 0;
mutable float m_scrollSize = 0;

void onEvent(Event& event) override;
void onLayoutUpdated() override;
bool setScrollOffset(float value);
void updateOffsets();
void createScrollBar();
void revealChild(Widget* child) override;
Ptr cloneThis() const override;
explicit ScrollBox(Construction construction, Orientation orientation, ArgumentsView<ScrollBox> args);

public:
BRISK_PROPERTIES_BEGIN
Property<ScrollBox, float, &ScrollBox::m_scrollPosition, nullptr, nullptr, &ScrollBox::updateOffsets>
scrollPosition;
Property<ScrollBox, float, &ScrollBox::m_scrollSize> scrollSize;
BRISK_PROPERTIES_END
};

inline namespace Arg {
constexpr inline Argument<Tag::PropArg<decltype(ScrollBox::scrollPosition)>> scrollPosition{};
constexpr inline Argument<Tag::PropArg<decltype(ScrollBox::scrollSize)>> scrollSize{};
} // namespace Arg

class WIDGET VScrollBox final : public ScrollBox {
public:
using Base = ScrollBox;
Expand Down
1 change: 0 additions & 1 deletion include/brisk/widgets/Widgets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include "PopupDialog.hpp"
#include "Progress.hpp"
#include "RadioButton.hpp"
#include "ScrollBar.hpp"
#include "ScrollBox.hpp"
#include "Slider.hpp"
#include "Spacer.hpp"
Expand Down
Loading

0 comments on commit 7f08f45

Please sign in to comment.