Skip to content

Commit 1cdc9f3

Browse files
authored
Merge pull request #250 from AntoinePrv/missing
Fix some missing widgets and test for missing ones
2 parents cec7cb1 + 4b0df9e commit 1cdc9f3

File tree

6 files changed

+190
-13
lines changed

6 files changed

+190
-13
lines changed

include/xwidgets/xall.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace xw
5353
using AllWidgets = std::tuple<
5454
accordion,
5555
audio,
56+
box,
5657
button,
5758
checkbox,
5859
color_picker,
@@ -102,6 +103,7 @@ namespace xw
102103
checkbox_style,
103104
description_style,
104105
html_style,
106+
label_style,
105107
progress_style,
106108
slider_style,
107109
text_style,

include/xwidgets/xbox.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ namespace xw
5757
protected:
5858

5959
xbox();
60-
using base_type::base_type;
6160

6261
private:
6362

6463
void set_defaults();
6564
};
6665

66+
using box = xmaterialize<xbox>;
67+
6768
/********************
6869
* hbox declaration *
6970
********************/
@@ -216,12 +217,12 @@ namespace xw
216217
template <class D>
217218
inline void xbox<D>::set_defaults()
218219
{
219-
this->_model_name() = "BoxModel";
220-
this->_view_name() = "BoxView";
221220
this->_model_module() = "@jupyter-widgets/controls";
222-
this->_view_module() = "@jupyter-widgets/controls";
223221
this->_model_module_version() = XWIDGETS_CONTROLS_VERSION;
222+
this->_model_name() = "BoxModel";
223+
this->_view_module() = "@jupyter-widgets/controls";
224224
this->_view_module_version() = XWIDGETS_CONTROLS_VERSION;
225+
this->_view_name() = "BoxView";
225226
}
226227

227228
/************************
@@ -230,7 +231,6 @@ namespace xw
230231

231232
template <class D>
232233
inline xhbox<D>::xhbox()
233-
: base_type()
234234
{
235235
set_defaults();
236236
}
@@ -248,7 +248,6 @@ namespace xw
248248

249249
template <class D>
250250
inline xvbox<D>::xvbox()
251-
: base_type()
252251
{
253252
set_defaults();
254253
}

include/xwidgets/xlabel.hpp

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,53 @@
99
#ifndef XWIDGETS_LABEL_HPP
1010
#define XWIDGETS_LABEL_HPP
1111

12-
#include "xhtml.hpp"
12+
#include <string>
13+
14+
#include <xtl/xoptional.hpp>
15+
1316
#include "xmaterialize.hpp"
1417
#include "xstring.hpp"
18+
#include "xstyle.hpp"
1519

1620
namespace xw
1721
{
22+
23+
/*****************************
24+
* label_style declaration *
25+
*****************************/
26+
27+
template <class D>
28+
class xlabel_style : public xstyle<D>
29+
{
30+
public:
31+
32+
using base_type = xstyle<D>;
33+
using derived_type = D;
34+
35+
void serialize_state(nl::json&, xeus::buffer_sequence&) const;
36+
void apply_patch(const nl::json&, const xeus::buffer_sequence&);
37+
38+
XPROPERTY(xtl::xoptional<std::string>, derived_type, background);
39+
XPROPERTY(std::string, derived_type, description_width);
40+
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_family);
41+
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_size);
42+
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_style);
43+
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_variant);
44+
XPROPERTY(xtl::xoptional<std::string>, derived_type, font_weight);
45+
XPROPERTY(xtl::xoptional<std::string>, derived_type, text_color);
46+
XPROPERTY(xtl::xoptional<std::string>, derived_type, text_decoration);
47+
48+
protected:
49+
50+
xlabel_style();
51+
52+
private:
53+
54+
void set_defaults();
55+
};
56+
57+
using label_style = xmaterialize<xlabel_style>;
58+
1859
/*********************
1960
* label declaration *
2061
*********************/
@@ -30,12 +71,11 @@ namespace xw
3071
void serialize_state(nl::json&, xeus::buffer_sequence&) const;
3172
void apply_patch(const nl::json&, const xeus::buffer_sequence&);
3273

33-
XPROPERTY(::xw::html_style, derived_type, style);
74+
XPROPERTY(::xw::label_style, derived_type, style);
3475

3576
protected:
3677

3778
xlabel();
38-
using base_type::base_type;
3979

4080
private:
4181

@@ -44,6 +84,56 @@ namespace xw
4484

4585
using label = xmaterialize<xlabel>;
4686

87+
/*********************************
88+
* xlabel_style implementation *
89+
*********************************/
90+
91+
template <class D>
92+
inline void xlabel_style<D>::serialize_state(nl::json& state, xeus::buffer_sequence& buffers) const
93+
{
94+
base_type::serialize_state(state, buffers);
95+
96+
xwidgets_serialize(background(), state["background"], buffers);
97+
xwidgets_serialize(description_width(), state["description_width"], buffers);
98+
xwidgets_serialize(font_family(), state["font_family"], buffers);
99+
xwidgets_serialize(font_size(), state["font_size"], buffers);
100+
xwidgets_serialize(font_style(), state["font_style"], buffers);
101+
xwidgets_serialize(font_variant(), state["font_variant"], buffers);
102+
xwidgets_serialize(font_weight(), state["font_weight"], buffers);
103+
xwidgets_serialize(text_color(), state["text_color"], buffers);
104+
xwidgets_serialize(text_decoration(), state["text_decoration"], buffers);
105+
}
106+
107+
template <class D>
108+
inline void xlabel_style<D>::apply_patch(const nl::json& patch, const xeus::buffer_sequence& buffers)
109+
{
110+
base_type::apply_patch(patch, buffers);
111+
112+
set_property_from_patch(background, patch, buffers);
113+
set_property_from_patch(description_width, patch, buffers);
114+
set_property_from_patch(font_family, patch, buffers);
115+
set_property_from_patch(font_size, patch, buffers);
116+
set_property_from_patch(font_style, patch, buffers);
117+
set_property_from_patch(font_variant, patch, buffers);
118+
set_property_from_patch(font_weight, patch, buffers);
119+
set_property_from_patch(text_color, patch, buffers);
120+
set_property_from_patch(text_decoration, patch, buffers);
121+
}
122+
123+
template <class D>
124+
inline xlabel_style<D>::xlabel_style()
125+
{
126+
set_defaults();
127+
}
128+
129+
template <class D>
130+
inline void xlabel_style<D>::set_defaults()
131+
{
132+
this->_model_module() = "@jupyter-widgets/controls";
133+
this->_model_module_version() = XWIDGETS_CONTROLS_VERSION;
134+
this->_model_name() = "LabelStyleModel";
135+
}
136+
47137
/*************************
48138
* xlabel implementation *
49139
*************************/
@@ -74,8 +164,8 @@ namespace xw
74164
template <class D>
75165
inline void xlabel<D>::set_defaults()
76166
{
77-
this->_model_name() = "HTMLModel";
78-
this->_view_name() = "HTMLView";
167+
this->_model_name() = "LabelModel";
168+
this->_view_name() = "LabelView";
79169
}
80170

81171
/*********************

include/xwidgets/xstyle.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ namespace xw
3131
protected:
3232

3333
xstyle();
34-
using base_type::base_type;
3534

3635
private:
3736

@@ -56,7 +55,6 @@ namespace xw
5655

5756
template <class D>
5857
inline xstyle<D>::xstyle()
59-
: base_type()
6058
{
6159
set_defaults();
6260
}

test/test_widget_model.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <iterator>
1414
#include <map>
1515
#include <string>
16+
#include <tuple>
1617

1718
#include <doctest/doctest.h>
1819
#include <nlohmann/json-schema.hpp>
@@ -31,6 +32,8 @@ namespace xw
3132
nl::json serialize();
3233
template <typename Widget>
3334
std::string model_name();
35+
auto all_widget_model_names() -> std::array<std::string, std::tuple_size_v<AllWidgets>>;
36+
auto all_style_model_names() -> std::array<std::string, std::tuple_size_v<AllStyles>>;
3437

3538
#ifndef XWIDGETS_MODELS_FILE
3639
#error "XWIDGETS_MODELS_FILE must be defined pointing to model file"
@@ -78,6 +81,56 @@ namespace xw
7881

7982
TEST_CASE_TEMPLATE_APPLY(packages_widgets, AllWidgets);
8083
TEST_CASE_TEMPLATE_APPLY(packages_widgets, AllStyles);
84+
85+
TEST_CASE("missing")
86+
{
87+
auto widgets_contains = [](auto const& val)
88+
{
89+
static auto const all_widgets = all_widget_model_names();
90+
return std::find(all_widgets.begin(), all_widgets.end(), val) < all_widgets.end();
91+
};
92+
93+
auto styles_contains = [](auto const& val)
94+
{
95+
static auto const all_styles = all_style_model_names();
96+
return std::find(all_styles.begin(), all_styles.end(), val) < all_styles.end();
97+
};
98+
99+
// Missing widgets tracked in https://github.com/jupyter-xeus/xwidgets/issues/249
100+
// This test is still useful even with the todo so that we don't get model names
101+
// wrong.
102+
auto todo_contains = [](auto const& val)
103+
{
104+
static auto const todo = std::array{
105+
"ColorsInputModel",
106+
"ComboboxModel",
107+
"DatePickerModel",
108+
"DatetimeModel",
109+
"FileUploadModel",
110+
"FloatLogSliderModel",
111+
"FloatRangeSliderModel",
112+
"FloatsInputModel",
113+
"GridBoxModel",
114+
"HTMLMathModel",
115+
"HTMLMathStyleModel",
116+
"IntRangeSliderModel",
117+
"IntsInputModel",
118+
"NaiveDatetimeModel",
119+
"StackModel",
120+
"TagsInputModel",
121+
"TimeModel",
122+
};
123+
return std::find(todo.begin(), todo.end(), val) < todo.end();
124+
};
125+
126+
for (auto const& [model, _] : schemas)
127+
{
128+
// Somehow using `model` directly in the macro does not compile on clang 15.0.7
129+
auto const& model_that_pleases_clang = model;
130+
INFO("Checking for model ", model_that_pleases_clang);
131+
CHECK((widgets_contains(model) || styles_contains(model) || todo_contains(model)));
132+
}
133+
}
81134
}
82135

83136
/***************************************
@@ -253,4 +306,25 @@ namespace xw
253306
throw std::invalid_argument("Widget does not have a model name");
254307
}
255308

309+
template <typename>
310+
struct all_model_names_impl;
311+
312+
template <typename... Widgets>
313+
struct all_model_names_impl<std::tuple<Widgets...>>
314+
{
315+
static auto get()
316+
{
317+
return std::array{model_name<Widgets>()...};
318+
}
319+
};
320+
321+
auto all_widget_model_names() -> std::array<std::string, std::tuple_size_v<AllWidgets>>
322+
{
323+
return all_model_names_impl<AllWidgets>{}.get();
324+
}
325+
326+
auto all_style_model_names() -> std::array<std::string, std::tuple_size_v<AllStyles>>
327+
{
328+
return all_model_names_impl<AllStyles>{}.get();
329+
}
256330
}

test/test_xwidgets.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ namespace xw
7777
CHECK_EQ(true, c.indent());
7878
}
7979

80+
TEST_CASE("checkbox.style")
81+
{
82+
checkbox c;
83+
c.style = checkbox_style::initialize() //
84+
.background("black")
85+
.description_width("3")
86+
.finalize();
87+
REQUIRE_EQ("black", c.style().background());
88+
REQUIRE_EQ("3", c.style().description_width());
89+
90+
c.style().description_width = "50";
91+
REQUIRE_EQ("50", c.style().description_width());
92+
}
93+
8094
TEST_CASE("html")
8195
{
8296
html h;

0 commit comments

Comments
 (0)