Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/include/UI_tree_view.hh
| Show First 20 Lines • Show All 211 Lines • ▼ Show 20 Lines | |||||
| * It also stores state information that needs to be persistent over redraws, like the collapsed | * It also stores state information that needs to be persistent over redraws, like the collapsed | ||||
| * state. | * state. | ||||
| */ | */ | ||||
| class AbstractTreeViewItem : public TreeViewItemContainer { | class AbstractTreeViewItem : public TreeViewItemContainer { | ||||
| friend class AbstractTreeView; | friend class AbstractTreeView; | ||||
| friend class TreeViewLayoutBuilder; | friend class TreeViewLayoutBuilder; | ||||
| public: | public: | ||||
| using IsActiveFn = std::function<bool()>; | |||||
| private: | private: | ||||
| bool is_open_ = false; | bool is_open_ = false; | ||||
| bool is_active_ = false; | bool is_active_ = false; | ||||
| bool is_renaming_ = false; | bool is_renaming_ = false; | ||||
| IsActiveFn is_active_fn_; | |||||
| protected: | protected: | ||||
| /** This label is used for identifying an item (together with its parent's labels). */ | /** This label is used for identifying an item (together with its parent's labels). */ | ||||
| std::string label_{}; | std::string label_{}; | ||||
| /** Every item gets a button of type during the layout building #UI_BTYPE_TREEROW. */ | /** Every item gets a button of type during the layout building #UI_BTYPE_TREEROW. */ | ||||
| uiButTreeRow *tree_row_but_ = nullptr; | uiButTreeRow *tree_row_but_ = nullptr; | ||||
| public: | public: | ||||
| virtual ~AbstractTreeViewItem() = default; | virtual ~AbstractTreeViewItem() = default; | ||||
| virtual void build_row(uiLayout &row) = 0; | virtual void build_row(uiLayout &row) = 0; | ||||
| virtual void build_context_menu(bContext &C, uiLayout &column) const; | virtual void build_context_menu(bContext &C, uiLayout &column) const; | ||||
| virtual void on_activate(); | virtual void on_activate(); | ||||
| /** | |||||
| * Set a custom callback to check if this item should be active. There's a version without | |||||
| * arguments for checking if the item is currently in an active state. | |||||
| */ | |||||
| virtual void is_active(IsActiveFn is_active_fn); | |||||
| /** | /** | ||||
| * Queries if the tree-view item supports renaming in principle. Renaming may still fail, e.g. if | * Queries if the tree-view item supports renaming in principle. Renaming may still fail, e.g. if | ||||
| * another item is already being renamed. | * another item is already being renamed. | ||||
| */ | */ | ||||
| virtual bool can_rename() const; | virtual bool can_rename() const; | ||||
| /** | /** | ||||
| * Try renaming the item, or the data it represents. Can assume | * Try renaming the item, or the data it represents. Can assume | ||||
| ▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | protected: | ||||
| /** | /** | ||||
| * Activates this item, deactivates other items, calls the #AbstractTreeViewItem::on_activate() | * Activates this item, deactivates other items, calls the #AbstractTreeViewItem::on_activate() | ||||
| * function and ensures this item's parents are not collapsed (so the item is visible). | * function and ensures this item's parents are not collapsed (so the item is visible). | ||||
| * Requires the tree to have completed reconstruction, see #is_reconstructed(). Otherwise the | * Requires the tree to have completed reconstruction, see #is_reconstructed(). Otherwise the | ||||
| * actual item state is unknown, possibly calling state-change update functions incorrectly. | * actual item state is unknown, possibly calling state-change update functions incorrectly. | ||||
| */ | */ | ||||
| void activate(); | void activate(); | ||||
| /** | |||||
| * If the result is not empty, it controls whether the item should be active or not, | |||||
| * usually depending on the data that the view represents. | |||||
| */ | |||||
| virtual std::optional<bool> should_be_active() const; | |||||
| /** | |||||
| * Return whether the item can be collapsed. Used to disable collapsing for items with children. | |||||
| */ | |||||
| virtual bool supports_collapsing() const; | |||||
| private: | private: | ||||
| static void rename_button_fn(bContext *, void *, char *); | static void rename_button_fn(bContext *, void *, char *); | ||||
| static AbstractTreeViewItem *find_tree_item_from_rename_button(const uiBut &but); | static AbstractTreeViewItem *find_tree_item_from_rename_button(const uiBut &but); | ||||
| static void tree_row_click_fn(struct bContext *, void *, void *); | static void tree_row_click_fn(struct bContext *, void *, void *); | ||||
| static void collapse_chevron_click_fn(bContext *, void *but_arg1, void *); | static void collapse_chevron_click_fn(bContext *, void *but_arg1, void *); | ||||
| static bool is_collapse_chevron_but(const uiBut *but); | static bool is_collapse_chevron_but(const uiBut *but); | ||||
| /** See #AbstractTreeView::change_state_delayed() */ | /** See #AbstractTreeView::change_state_delayed() */ | ||||
| ▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | |||||
| * Common, Basic Tree-View Item Types. | * Common, Basic Tree-View Item Types. | ||||
| * \{ */ | * \{ */ | ||||
| /** | /** | ||||
| * The most basic type, just a label with an icon. | * The most basic type, just a label with an icon. | ||||
| */ | */ | ||||
| class BasicTreeViewItem : public AbstractTreeViewItem { | class BasicTreeViewItem : public AbstractTreeViewItem { | ||||
| public: | public: | ||||
| using IsActiveFn = std::function<bool()>; | |||||
| using ActivateFn = std::function<void(BasicTreeViewItem &new_active)>; | using ActivateFn = std::function<void(BasicTreeViewItem &new_active)>; | ||||
| BIFIconID icon; | BIFIconID icon; | ||||
| explicit BasicTreeViewItem(StringRef label, BIFIconID icon = ICON_NONE); | explicit BasicTreeViewItem(StringRef label, BIFIconID icon = ICON_NONE); | ||||
| void build_row(uiLayout &row) override; | void build_row(uiLayout &row) override; | ||||
| void add_label(uiLayout &layout, StringRefNull label_override = ""); | void add_label(uiLayout &layout, StringRefNull label_override = ""); | ||||
| void on_activate(ActivateFn fn); | void set_on_activate_fn(ActivateFn fn); | ||||
| /** | |||||
| * Set a custom callback to check if this item should be active. | |||||
| */ | |||||
| void set_is_active_fn(IsActiveFn fn); | |||||
| protected: | protected: | ||||
| /** | /** | ||||
| * Optionally passed to the #BasicTreeViewItem constructor. Called when activating this tree | * Optionally passed to the #BasicTreeViewItem constructor. Called when activating this tree | ||||
| * view item. This way users don't have to sub-class #BasicTreeViewItem, just to implement | * view item. This way users don't have to sub-class #BasicTreeViewItem, just to implement | ||||
| * custom activation behavior (a common thing to do). | * custom activation behavior (a common thing to do). | ||||
| */ | */ | ||||
| ActivateFn activate_fn_; | ActivateFn activate_fn_; | ||||
| IsActiveFn is_active_fn_; | |||||
| private: | private: | ||||
| static void tree_row_click_fn(struct bContext *C, void *arg1, void *arg2); | static void tree_row_click_fn(struct bContext *C, void *arg1, void *arg2); | ||||
| std::optional<bool> should_be_active() const override; | |||||
| void on_activate() override; | void on_activate() override; | ||||
| }; | }; | ||||
| /** \} */ | /** \} */ | ||||
| /* ---------------------------------------------------------------------- */ | /* ---------------------------------------------------------------------- */ | ||||
| template<class ItemT, typename... Args> | template<class ItemT, typename... Args> | ||||
| Show All 17 Lines | |||||