slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.94k stars 568 forks source link

SortModel::reset() has an infinite recursion #4968

Closed ogoffart closed 5 months ago

ogoffart commented 5 months ago

Discussed in https://github.com/slint-ui/slint/discussions/4960

Originally posted by **mous16** March 27, 2024 Hello everybody, I'm trying to wrap my head around custom model implementation in Slint, using C++. Let's say I have a simple project, with a Slint file like this: ```Slint, appwindow.slint // appwindow.slint import { ListView, VerticalBox, Switch } from "std-widgets.slint"; export component AppWindow inherits Window { in property <[int]> model; in-out property ascending <=> ascending-switch.checked; callback ascending-changed <=> ascending-switch.toggled; VerticalBox { ascending-switch := Switch { text: "Ascending"; } ListView { for item in root.model: Text { text: item; } } } } ``` On C++ side, this is the entire implementation: ```C++, main.cpp // main.cpp #include #include #include "appwindow.h" #include "slint.h" class MySortModel: public slint::SortModel { bool ascending{true}; [[nodiscard]] auto sort(const int& first, const int& second) const -> bool { return ascending ? first < second : first >= second; } public: MySortModel(std::shared_ptr> source_model): slint::SortModel{std::move(source_model), [this](const int& first, const int& second) { return sort(first, second); }} { } [[nodiscard]] auto getAscending() const -> bool { return ascending; } void setAscending(bool value) { ascending = value; // Perform some kind of magic to force sorting re-evaluation } }; auto main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) -> int { auto appWindow{AppWindow::create()}; const auto model{std::make_shared>(std::vector{1, 2, 3, 4, 5})}; const auto sortModel{std::make_shared(model)}; appWindow->set_ascending(sortModel->getAscending()); appWindow->set_model(sortModel); appWindow->on_ascending_changed([appWindow, sortModel]() { sortModel->setAscending(appWindow->get_ascending()); }); appWindow->run(); return 0; } ``` Inside `MySortModel::setAscending(bool)`, I need to notify the parent `SortModel` that the sorting function is changed, and sorting must be re-evaluated for every element in the underlying model. How to achieve this behavior? Reading the documentation, it seems that `SortModel::reset()` is the closest thing, but instead it triggers a call loop between `SortModel::reset()` and `SortModelInner::reset()`. Thanks in advance!
mous16 commented 5 months ago

Thanks @ogoffart.

Analogous behavior is triggered by custom filter model.

Here the example code:

// appwindow.slint

import {  ListView, VerticalBox, Switch } from "std-widgets.slint";

export component AppWindow inherits Window {
    in property <[int]> model;
    in-out property only-evens <=> only-evens-switch.checked;
    callback only-evens-changed <=> only-evens-switch.toggled;

    VerticalBox {
        only-evens-switch := Switch {
            text: "Only evens";
        }

        ListView {
            for item in root.model: Text {
                text: item;
            }
        }
    }
}
// main.cpp

#include <memory>
#include <utility>

#include "appwindow.h"
#include "slint.h"

class MyFilterModel: public slint::FilterModel<int>
{
    bool onlyEvens{false};

    [[nodiscard]] auto filter(const int& element) const -> bool { return !onlyEvens || element % 2 == 0; }

  public:
    MyFilterModel(std::shared_ptr<Model<int>> source_model):
      slint::FilterModel<int>{std::move(source_model), [this](const int& element) { return filter(element); }}
    { }

    [[nodiscard]] auto getOnlyEvens() const -> bool { return onlyEvens; }

    void setOnlyEvens(bool value)
    {
        onlyEvens = value;
        // Perform some kind of magic to force filtering re-evaluation
    }
};

auto main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) -> int
{
    auto appWindow{AppWindow::create()};

    const auto model{std::make_shared<slint::VectorModel<int>>(std::vector<int>{1, 2, 3, 4, 5})};
    const auto filterModel{std::make_shared<MyFilterModel>(model)};

    appWindow->set_only_evens(filterModel->getOnlyEvens());
    appWindow->set_model(filterModel);
    appWindow->on_only_evens_changed([appWindow, filterModel]() {
        filterModel->setOnlyEvens(appWindow->get_only_evens());
    });

    appWindow->run();

    return 0;
}

Similar, calling FilterModel::reset() triggers a call loop between FilterModel::reset() and FilterModelInner::reset(), that with my current toolchain ends with an exception in std::vector implementation, after some iterations.

I don't know if a different issue should be filled for FilterModel.

ogoffart commented 5 months ago

Indeed, all the reset function were having a problem. https://github.com/slint-ui/slint/pull/4969 fixes it.