13#include "../../../Item_p.h"
14#include "../../../LayoutingHost_p.h"
15#include "../../../LayoutingGuest_p.h"
16#include "../../../LayoutingSeparator_p.h"
23class Host :
public KDDockWidgets::Core::LayoutingHost
26 explicit Host(slint::ComponentHandle<AppWindow> ui);
29 separatorForId(
int id)
const;
31 bool supportsHonouringLayoutMinSize()
const override
36 slint::ComponentHandle<AppWindow> ui;
41int index_of_row(std::string name, slint::ComponentHandle<AppWindow> ui)
43 auto model = ui->get_dockWidgets();
47 for (
int i = 0, end = model->row_count(); i < end; ++i) {
48 auto dw = model->row_data(i);
49 if (dw->uniqueName.data() == name) {
57int index_of_separator_row(
int id, slint::ComponentHandle<AppWindow> ui)
59 auto model = ui->get_separators();
63 for (
int i = 0, end = model->row_count(); i < end; ++i) {
64 auto dw = model->row_data(i);
73void dump_model(slint::ComponentHandle<AppWindow> ui)
75 if (
auto model = ui->get_dockWidgets()) {
76 for (
int i = 0, end = model->row_count(); i < end; ++i) {
77 std::cout <<
"i=" << i <<
"name=" << model->row_data(i)->uniqueName.data() <<
"\n";
80 std::cerr <<
"null model\n";
84void update_model_row(DockWidgetDescriptor desc, slint::ComponentHandle<AppWindow> ui)
86 const int index = index_of_row(desc.uniqueName.data(), ui);
88 std::cerr <<
"Failed to find descriptor for: " << desc.uniqueName <<
"; count is: " << ui->get_dockWidgets()->row_count() <<
"\n";
90 auto model = ui->get_dockWidgets();
92 model->set_row_data(index, desc);
96void add_model_row(DockWidgetDescriptor desc, slint::ComponentHandle<AppWindow> ui)
98 const int index = index_of_row(desc.uniqueName.data(), ui);
100 std::cerr <<
"add_model_row: Row already exists!\n";
104 if (
auto model = ui->get_dockWidgets()) {
105 auto vecModel = std::static_pointer_cast<slint::VectorModel<DockWidgetDescriptor>>(model);
106 vecModel->push_back(desc);
108 std::vector<DockWidgetDescriptor> dockDescriptors = {
112 ui->set_dockWidgets(std::make_shared<slint::VectorModel<DockWidgetDescriptor>>(dockDescriptors));
116void add_separator_model_row(SeparatorDescriptor desc, slint::ComponentHandle<AppWindow> ui)
118 auto separatorModel = ui->get_separators();
119 if (separatorModel) {
120 auto vecModel = std::static_pointer_cast<slint::VectorModel<SeparatorDescriptor>>(separatorModel);
121 vecModel->push_back(desc);
123 std::vector<SeparatorDescriptor> sepDescriptors { desc };
124 ui->set_separators(std::make_shared<slint::VectorModel<SeparatorDescriptor>>(sepDescriptors));
128void update_separator_model_row(SeparatorDescriptor desc, slint::ComponentHandle<AppWindow> ui)
130 const int index = index_of_separator_row(desc.id, ui);
132 std::cerr <<
"Failed to find separator descriptor for: " << desc.id <<
"; count is: " << ui->get_separators()->row_count() <<
"\n";
134 auto model = ui->get_separators();
136 model->set_row_data(index, desc);
140void remove_separator_model_row(
int id, slint::ComponentHandle<AppWindow> ui)
142 const int index = index_of_separator_row(
id, ui);
144 std::cerr <<
"remove_separator_model_row: Failed to find separator descriptor for: " <<
id <<
"; count is: " << ui->get_separators()->row_count() <<
"\n";
146 auto vecModel = std::static_pointer_cast<slint::VectorModel<SeparatorDescriptor>>(ui->get_separators());
147 vecModel->erase(index);
153class Guest :
public KDDockWidgets::Core::LayoutingGuest
156 explicit Guest(Host *host,
const QString &uniqueName, slint::ComponentHandle<AppWindow> ui)
157 : _uniqueName(uniqueName)
160 auto item =
new Core::Item(host);
161 item->setGuest(
this);
164 Size minSize()
const override
169 Size maxSizeHint()
const override
171 return { 1000, 1000 };
180 update_model_row(descriptor(), ui);
183 DockWidgetDescriptor descriptor()
const
185 return DockWidgetDescriptor { _isVisible, slint::SharedString(_uniqueName), float(_geometry.x()), float(
geometry().
y()), float(_geometry.width()), float(_geometry.height()) };
188 void setVisible(
bool is)
override
190 if (_isVisible == is)
194 update_model_row(descriptor(), ui);
202 void setHost(Core::LayoutingHost *parent)
override
204 if (_layoutingHost == parent)
207 add_model_row(descriptor(), ui);
208 _layoutingHost = parent;
211 Core::LayoutingHost *host()
const override
213 return _layoutingHost;
221 Core::LayoutingHost *_layoutingHost =
nullptr;
223 slint::ComponentHandle<AppWindow> ui;
225 bool _isVisible =
false;
228class Separator :
public Core::LayoutingSeparator
232 : Core::LayoutingSeparator(host, orientation, container)
233 , _host(static_cast<Host *>(host))
235 static int counter = 0;
238 add_separator_model_row(descriptor(), this->_host->ui);
241 ~Separator()
override
243 remove_separator_model_row(_id, _host->ui);
258 update_separator_model_row(descriptor(), _host->ui);
261 SeparatorDescriptor descriptor()
const
263 return SeparatorDescriptor { float(_geo.x()), float(_geo.y()), float(_geo.width()), float(_geo.height()), _id, isVertical() };
271Host::Host(slint::ComponentHandle<AppWindow> ui)
275 m_rootItem =
new Core::ItemBoxContainer(
this);
277 ui->on_window_size_changed_callback([
this](
float w,
float h) ->
bool {
278 m_rootItem->setSize_recursive({ int(w), int(h) });
282 ui->on_separator_event([&](
int id, slint::private_api::PointerEvent ev) {
283 if (Separator *separator = separatorForId(
id)) {
284 if (ev.kind == slint::cbindgen_private::PointerEventKind::Down) {
285 separator->onMousePress();
286 }
else if (ev.kind == slint::cbindgen_private::PointerEventKind::Up) {
287 separator->onMouseRelease();
290 std::cerr <<
"on_separator_event: Expected separator with id=" <<
id <<
"\n";
294 ui->on_separator_moved([&](
int id,
float x,
float y) {
295 if (Separator *sep = separatorForId(
id)) {
296 if (sep->isVertical()) {
297 const int oldPos = sep->position();
298 sep->onMouseMove({ 0, int(oldPos + y) });
300 const int oldPos = sep->position();
301 sep->onMouseMove({ int(oldPos + x), 0 });
304 std::cerr <<
"on_separator_moved: Expected separator with id=" <<
id <<
"\n";
309Separator *Host::separatorForId(
int id)
const
311 auto separators =
static_cast<Core::ItemBoxContainer *
>(m_rootItem)->separators_recursive();
312 for (
auto separator : separators) {
313 auto slintSeparator =
static_cast<Separator *
>(separator);
314 if (slintSeparator->_id ==
id)
315 return slintSeparator;
323 auto ui = AppWindow::create();
324 ui->set_image(slint::Image::load_from_path(SRC_DIR
"/slint-logo-full-dark-large.png"));
327 Core::Item::setCreateSeparatorFunc([](Core::LayoutingHost *host,
Qt::Orientation orientation, Core::ItemBoxContainer *container) -> Core::LayoutingSeparator * {
328 return new Separator(host, orientation, container);
332 auto guest1 =
new Guest(&host,
"1", ui);
333 auto guest2 =
new Guest(&host,
"2", ui);
334 auto guest3 =
new Guest(&host,
"3", ui);
335 auto guest4 =
new Guest(&host,
"4", ui);
int main(int argc, char **argv)