Skip to content

Commit fa75c7f

Browse files
committed
remove scroll_area from tree_view implementation
1 parent f2b1c45 commit fa75c7f

File tree

3 files changed

+45
-75
lines changed

3 files changed

+45
-75
lines changed

src/ruis/widget/group/tree_view.cpp

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,19 @@ tree_view::tree_view( //
4242
std::move(params.layout_params),
4343
std::move(params.widget_params)
4444
),
45-
scroll_area(this->context, {}, {}),
46-
item_list(
47-
// clang-format off
48-
ruis::make::list(this->context,
49-
{
50-
.layout_params = {
51-
.dims = {ruis::dim::min, ruis::dim::max}
52-
},
53-
.oriented_params = {
54-
.vertical = true
55-
}
45+
list(this->context,
46+
{
47+
.oriented_params{
48+
.vertical = true
5649
}
57-
)
58-
// clang-format on
50+
}
5951
)
6052
{
61-
this->push_back(this->item_list);
62-
63-
this->item_list.get().model_change_handler = [this](list&) {
64-
this->notify_view_change();
65-
};
66-
67-
this->item_list.get().scroll_change_handler = [this](list&) {
53+
this->list::model_change_handler = [this](list&) {
6854
this->notify_view_change();
6955
};
7056

71-
this->scroll_area::scroll_change_handler = [this](scroll_area& sa) {
57+
this->list::scroll_change_handler = [this](list&) {
7258
this->notify_view_change();
7359
};
7460

@@ -85,14 +71,14 @@ void tree_view::notify_view_change()
8571
void tree_view::set_provider(std::shared_ptr<provider> item_provider)
8672
{
8773
if (!item_provider) {
88-
this->item_list.get().set_provider(nullptr);
74+
this->list::set_provider(nullptr);
8975
return;
9076
}
9177

9278
// TODO: why do we need to do the notification here?
9379
item_provider->notify_model_change();
9480

95-
this->item_list.get().set_provider(
81+
this->list::set_provider(
9682
// use aliasing shared_ptr constructor becasue list::provider
9783
// is a private base of tree_view::provider, so not possible
9884
// to use std::static_pointer_cast() because it does not see the

src/ruis/widget/group/tree_view.hpp

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2828
#include "../widget.hpp"
2929

3030
#include "list.hpp"
31-
#include "scroll_area.hpp"
3231

3332
namespace ruis {
3433

3534
// NOLINTNEXTLINE(bugprone-incorrect-enable-shared-from-this, "std::shared_from_this is public via widget")
3635
class tree_view :
3736
virtual public widget, //
38-
private scroll_area
37+
private list
3938
{
40-
utki::shared_ref<list> item_list;
41-
4239
public:
4340
/**
4441
* @brief tree_view item provider base class.
@@ -49,7 +46,10 @@ class tree_view :
4946
{
5047
friend class tree_view;
5148

52-
void recycle(size_t index, utki::shared_ref<widget> w) override;
49+
void recycle(
50+
size_t index, //
51+
utki::shared_ref<widget> w
52+
) override;
5353

5454
utki::shared_ref<widget> get_widget(size_t index) override;
5555

@@ -214,45 +214,21 @@ class tree_view :
214214
* @brief Set vertical scroll position by factor.
215215
* @param factor - new scroll position specified by factor from [0:1].
216216
*/
217-
void set_vertical_scroll_factor(real factor)
218-
{
219-
this->item_list.get().set_scroll_factor(factor);
220-
}
221-
222-
/**
223-
* @brief Set horizontal scroll position by factor.
224-
* @param factor - new scroll position specified by factor from [0:1].
225-
*/
226-
void set_horizontal_scroll_factor(real factor)
227-
{
228-
this->set_scroll_factor(vector2(factor, 0));
229-
}
217+
using list::set_scroll_factor;
230218

231219
/**
232220
* @brief Get scroll position.
233-
* @return Vector of (horizontal scroll factor, vertical scroll factor).
221+
* @return Scroll factor.
234222
*/
235-
vector2 get_scroll_factor() const
236-
{
237-
return vector2(
238-
this->scroll_area::get_scroll_factor().x(), //
239-
this->item_list.get().get_scroll_factor()
240-
);
241-
}
223+
using list::get_scroll_factor;
242224

243225
/**
244226
* @brief Get scroll band.
245227
* Get scroll band size.
246228
* The scroll band size is a fraction of [0:1] interval.
247-
* @return Vector of (horizontal scroll band, vertical scroll band).
229+
* @return Vertical scroll band.
248230
*/
249-
vector2 get_scroll_band() const noexcept
250-
{
251-
return vector2(
252-
this->scroll_area::get_visible_area_fraction().x(), //
253-
this->item_list.get().get_scroll_band()
254-
);
255-
}
231+
using list::get_scroll_band;
256232

257233
private:
258234
void notify_view_change();

tests/app/src/tree_view_window.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
#include <ruis/widget/group/tree_view.hpp>
44
#include <ruis/widget/slider/scroll_bar.hpp>
55
#include <ruis/widget/button/push_button.hpp>
6+
#include <ruis/widget/group/scroll_area.hpp>
67
#include <ruis/default_style.hpp>
78

89
using namespace std::string_literals;
10+
using namespace std::string_view_literals;
911

1012
using namespace ruis::length_literals;
1113

@@ -311,18 +313,21 @@ utki::shared_ref<ruis::window> make_tree_view_window(
311313
}
312314
},
313315
{
314-
m::pile(c,
316+
m::scroll_area(c,
315317
{
316318
.layout_params{
317-
.dims{ruis::dim::fill, ruis::dim::max},
319+
.dims{ruis::dim::fill, ruis::dim::fill},
318320
.weight = 1
321+
},
322+
.widget_params{
323+
.id = "tree_view_scroll_area"s
319324
}
320325
},
321326
{
322327
m::tree_view(c,
323328
{
324329
.layout_params{
325-
.dims{ruis::dim::fill, ruis::dim::fill}
330+
.dims{ruis::dim::min, ruis::dim::fill}
326331
},
327332
.widget_params{
328333
.id = "treeview_widget"s,
@@ -409,44 +414,47 @@ utki::shared_ref<ruis::window> make_tree_view_window(
409414

410415
auto& treeview = w.get().get_widget_as<ruis::tree_view>("treeview_widget");
411416

412-
// TODO: remove
413-
// auto provider = std::make_shared<tree_view_items_provider>(w.get().context);
414-
// treeview.set_provider(provider);
415-
416417
auto tv = utki::make_weak_from(treeview);
417418

418419
auto& vertical_slider = w.get().get_widget_as<ruis::fraction_band_widget>("treeview_vertical_slider");
419420
auto vs = utki::make_weak_from(vertical_slider);
420421

421422
vertical_slider.fraction_change_handler = [tv](ruis::fraction_widget& slider){
422423
if(auto t = tv.lock()){
423-
t->set_vertical_scroll_factor(slider.get_fraction());
424+
t->set_scroll_factor(slider.get_fraction());
424425
}
425426
};
426427

428+
auto& tvsa = w.get().get_widget_as<ruis::scroll_area>("tree_view_scroll_area"sv);
429+
427430
auto& horizontal_slider = w.get().get_widget_as<ruis::fraction_band_widget>("treeview_horizontal_slider");
428431
auto hs = utki::make_weak_from(horizontal_slider);
429432

430-
horizontal_slider.fraction_change_handler = [tv](ruis::fraction_widget& slider){
431-
if(auto t = tv.lock()){
432-
t->set_horizontal_scroll_factor(slider.get_fraction());
433+
horizontal_slider.fraction_change_handler = [tvsa = utki::make_weak_from(tvsa)](ruis::fraction_widget& slider){
434+
if(auto sa = tvsa.lock()){
435+
sa->set_scroll_factor({
436+
slider.get_fraction(),
437+
0
438+
});
439+
}
440+
};
441+
442+
tvsa.scroll_change_handler = [hs = utki::make_weak_from(horizontal_slider)](ruis::scroll_area& sa){
443+
if(auto h = hs.lock()){
444+
h->set_band_fraction(sa.get_visible_area_fraction().x());
445+
h->set_fraction(sa.get_scroll_factor().x(), false);
433446
}
434447
};
435448

436449
treeview.scroll_change_handler = [
437-
hs = utki::make_weak_from(horizontal_slider),
438450
vs = utki::make_weak_from(vertical_slider)
439451
](ruis::tree_view& tw)
440452
{
441453
auto f = tw.get_scroll_factor();
442454
auto b = tw.get_scroll_band();
443-
if(auto h = hs.lock()){
444-
h->set_band_fraction(b.x());
445-
h->set_fraction(f.x(), false);
446-
}
447455
if(auto v = vs.lock()){
448-
v->set_band_fraction(b.y());
449-
v->set_fraction(f.y(), false);
456+
v->set_band_fraction(b);
457+
v->set_fraction(f, false);
450458
}
451459
};
452460

0 commit comments

Comments
 (0)