Skip to content

Commit faa4109

Browse files
committed
- Start/Stop API
- Improved example
1 parent 40c71c6 commit faa4109

File tree

3 files changed

+82
-79
lines changed

3 files changed

+82
-79
lines changed

examples/status_bars/main.cpp

+55-37
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,78 @@ using cycfi::artist::rgba;
1010
namespace colors = cycfi::artist::colors;
1111
using namespace std::chrono_literals;
1212

13-
constexpr auto bred = colors::red.opacity(0.4);
14-
constexpr auto bgreen = colors::green.level(0.7).opacity(0.4);
15-
constexpr auto brblue = colors::royal_blue.opacity(0.4);
13+
constexpr auto bgreen = colors::green.level(0.7).opacity(0.8);
1614
constexpr auto pgold = colors::gold.opacity(0.8);
1715

1816
// Main window background color
1917
auto constexpr bkd_color = rgba(35, 35, 37, 255);
2018
auto background = box(bkd_color);
2119

20+
bool run = false;
21+
template <typename ProgressBar>
22+
void prog_incr(ProgressBar& prog_bar, view& view_)
23+
{
24+
auto val = prog_bar.value();
25+
if (val > 1.0)
26+
{
27+
prog_bar.value(1.0);
28+
run = false;
29+
}
30+
else
31+
{
32+
prog_bar.value(val + 0.005);
33+
}
34+
view_.refresh(prog_bar);
35+
}
36+
37+
template <typename ProgressBar>
38+
void prog_animate(ProgressBar& prog_bar, view& view_)
39+
{
40+
if (run)
41+
{
42+
prog_incr(prog_bar, view_);
43+
view_.post(10ms,
44+
[&]()
45+
{
46+
prog_animate(prog_bar, view_);
47+
}
48+
);
49+
}
50+
}
51+
2252
auto make_bars(view& view_)
2353
{
2454
auto prog_bar = share(progress_bar(rbox(colors::black), rbox(pgold)));
25-
auto bsy_bar = share(busy_bar(rbox(colors::black), rbox(brblue)));
26-
auto prog_advance = icon_button(icons::plus);
27-
auto busy_start = button("I", 1.0, bgreen);
28-
auto busy_stop = button("O", 1.0, bred);
55+
auto bsy_bar = share(busy_bar(rbox(colors::black), rbox(bgreen)));
56+
auto start_stop = toggle_icon_button(icons::play, icons::stop, 2.0);
2957

30-
prog_advance.on_click =
31-
[prog_bar, &view_](bool) mutable
58+
start_stop.on_click =
59+
[prog_bar, bsy_bar, &view_](bool state) mutable
3260
{
33-
auto val = prog_bar->value();
34-
if (val > 0.9)
61+
if (state)
62+
{
63+
bsy_bar->start(view_, 10ms);
64+
run = true;
3565
prog_bar->value(0.0);
66+
prog_animate(*prog_bar, view_);
67+
}
3668
else
37-
prog_bar->value(val + 0.125);
38-
view_.refresh(*prog_bar);
39-
};
40-
41-
busy_stop.on_click =
42-
[bsy_bar, &view_](bool) mutable
43-
{
44-
bsy_bar->animate(view_, 0ms);
45-
};
46-
47-
busy_start.on_click =
48-
[bsy_bar, &view_](bool) mutable
49-
{
50-
bsy_bar->animate(view_, 50ms);
69+
{
70+
bsy_bar->stop(view_);
71+
run = false;
72+
}
5173
};
5274

5375
return
54-
margin({20, 0, 20, 20},
55-
vtile(
56-
margin_top(20, htile(
57-
margin_right(3, valign(0.5, prog_advance)),
58-
vsize(27, hold(prog_bar))
59-
)),
60-
margin_top(20, vtile(
61-
margin_right(3, htile(
62-
busy_stop,
63-
busy_start
64-
)),
76+
margin({20, 20, 20, 20},
77+
htile(
78+
start_stop,
79+
hspace(10),
80+
vtile(
81+
vsize(27, hold(prog_bar)),
82+
vspace(10),
6583
vsize(27, hold(bsy_bar))
66-
))
84+
)
6785
)
6886
);
6987
}

lib/include/elements/element/status_bar.hpp

+13-35
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
namespace cycfi::elements
1616
{
17-
////////////////////////////////////////////////////////////////////////////
18-
// Progress Bar
19-
////////////////////////////////////////////////////////////////////////////
2017
class status_bar_base : public element, public receiver<double>
2118
{
2219
public:
@@ -44,14 +41,6 @@ namespace cycfi::elements
4441
double _value;
4542
};
4643

47-
class basic_status_bar_base : public status_bar_base
48-
{
49-
public:
50-
basic_status_bar_base(double init_value)
51-
: status_bar_base(init_value)
52-
{}
53-
};
54-
5544
namespace concepts
5645
{
5746
template <typename T>
@@ -61,7 +50,7 @@ namespace cycfi::elements
6150
template <
6251
concepts::Element Background
6352
, concepts::Element Foreground
64-
, concepts::StatusBar Base = basic_status_bar_base>
53+
, concepts::StatusBar Base = status_bar_base>
6554
class basic_status_bar : public Base
6655
{
6756
public:
@@ -92,7 +81,7 @@ namespace cycfi::elements
9281
};
9382

9483
template <concepts::Element Background, concepts::Element Foreground>
95-
basic_status_bar<Background, Foreground, basic_status_bar_base>
84+
inline basic_status_bar<Background, Foreground, status_bar_base>
9685
progress_bar(Background&& bg, Foreground&& fg, double init_value = 0.0)
9786
{
9887
return {
@@ -107,54 +96,43 @@ namespace cycfi::elements
10796
public:
10897
busy_bar_base(
10998
double init_value = 0.0
110-
, double start_value = 0.0
99+
, double start_pos = 0.0
111100
)
112101
: status_bar_base(init_value)
113-
, _start(start_value)
102+
, _start_pos(start_pos)
114103
, _status(-0.2)
115104
{}
116105

117-
void start(double val);
118-
double start() const { return _start; }
119-
void animate(view& view_, duration time);
106+
void start_pos(double val);
107+
double start_pos() const { return _start_pos; }
108+
109+
void start(view& view_, duration time);
110+
void stop(view& view_);
120111

121112
rect foreground_bounds(context const& ctx) const override;
122113

123114
private:
124115

125116
void animate(view& view_);
126117

127-
double _start; // Start position
118+
double _start_pos;
128119
double _status;
129120
duration _time;
130121
};
131122

132-
class basic_busy_bar_base : public busy_bar_base
133-
{
134-
public:
135-
basic_busy_bar_base(
136-
double init_value
137-
, double start_value
138-
)
139-
: busy_bar_base(init_value, start_value)
140-
{}
141-
};
142-
143123
template <concepts::Element Background, concepts::Element Foreground>
144-
inline basic_status_bar<Background, Foreground, basic_busy_bar_base>
124+
inline basic_status_bar<Background, Foreground, busy_bar_base>
145125
busy_bar(
146126
Background&& bg
147127
, Foreground&& fg
148128
, double init_value = 0.0
149-
, double start_value = 0.0
129+
, double start_pos = 0.0
150130
)
151131
{
152132
return {
153133
std::forward<Background>(bg),
154134
std::forward<Foreground>(fg),
155-
init_value,
156-
start_value
157-
};
135+
init_value, start_pos};
158136
}
159137
}
160138

lib/src/element/status_bar.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ namespace cycfi::elements
8787
}
8888

8989
// Implement the start method as per the patch
90-
void busy_bar_base::start(double val)
90+
void busy_bar_base::start_pos(double val)
9191
{
92-
_start = clamp(val, 0.0, 1.0);
92+
_start_pos = clamp(val, 0.0, 1.0);
9393
}
9494

9595
// Implement foreground_bounds to include the start value
@@ -104,32 +104,39 @@ namespace cycfi::elements
104104

105105
// Adjust bounds based on the start value
106106
if (bounds.width() > bounds.height())
107-
bounds.left = bounds.left + (bounds.width() * start());
107+
bounds.left = bounds.left + (bounds.width() * start_pos());
108108
else
109-
bounds.bottom = bounds.bottom + (bounds.height() * start());
109+
bounds.bottom = bounds.bottom + (bounds.height() * start_pos());
110110

111111
return bounds;
112112
}
113113

114-
void busy_bar_base::animate(view& view_, duration time)
114+
void busy_bar_base::start(view& view_, duration time)
115115
{
116116
_time = time;
117117
animate(view_);
118118
}
119119

120+
void busy_bar_base::stop(view& view_)
121+
{
122+
using namespace std::chrono_literals;
123+
_time = 0ms;
124+
animate(view_);
125+
}
126+
120127
void busy_bar_base::animate(view& view_)
121128
{
122129
if (_time == _time.zero())
123130
{
124131
_status = -0.2;
125132
value(0.0);
126-
start(0.0);
133+
start_pos(0.0);
127134
view_.refresh();
128135
return;
129136
}
130137

131138
_status += 0.01;
132-
start(_status);
139+
start_pos(_status);
133140
value(_status + 0.2);
134141
if (_status >= 1.0)
135142
_status = -0.2;

0 commit comments

Comments
 (0)