Blender V5.3
abstract_view.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "interface_intern.hh"
10
11#include "UI_abstract_view.hh"
12
13namespace blender::ui {
14
16{
17 /* Actually modifies the item, not the view. But for the public API it "feels" a bit nicer to
18 * have the view base class register the items, rather than setting the view on the item. */
19 item.view_ = this;
20}
21
22/* ---------------------------------------------------------------------- */
25
27{
28 return is_reconstructed_;
29}
30
32{
33 const AbstractViewItem *found_item = nullptr;
34
35 this->foreach_view_item([&](const AbstractViewItem &item) {
36 if (!found_item && item.is_search_highlight()) {
37 found_item = &item;
38 }
39 });
40 return found_item;
41}
42
44{
45 Block *old_block = new_block.oldblock;
46 if (!old_block) {
47 is_reconstructed_ = true;
48 return;
49 }
50
51 AbstractView *old_view = block_view_find_matching_in_old_block(new_block, *this);
52 if (old_view == nullptr) {
53 /* Initial construction, nothing to update. */
54 is_reconstructed_ = true;
55 return;
56 }
57
58 /* Update own persistent data. */
59 prev_filter_string_ = old_view->prev_filter_string_;
60 /* Keep the rename buffer persistent while renaming! The rename button uses the buffer's
61 * pointer to identify itself over redraws. */
62 rename_buffer_ = std::move(old_view->rename_buffer_);
63 old_view->rename_buffer_ = nullptr;
64
65 this->update_children_from_old(*old_view);
66
67 /* Finished (re-)constructing the tree. */
68 is_reconstructed_ = true;
69}
70
72
73/* ---------------------------------------------------------------------- */
76
78{
80 this->is_reconstructed(),
81 "These state changes are supposed to be delayed until reconstruction is completed");
82
83/* Debug-only sanity check: Ensure only one item requests to be active. */
84#ifndef NDEBUG
85 bool has_active = false;
86 foreach_view_item([&has_active](AbstractViewItem &item) {
87 if (item.should_be_active().value_or(false)) {
89 !has_active,
90 "Only one view item should ever return true for its `should_be_active()` method");
91 has_active = true;
92 }
93 });
94#endif
95
96 this->foreach_view_item([](AbstractViewItem &item) { item.change_state_delayed(); });
97}
98
100
101/* ---------------------------------------------------------------------- */
104
105std::unique_ptr<DropTargetInterface> AbstractView::create_drop_target()
106{
107 /* There's no drop target (and hence no drop support) by default. */
108 return nullptr;
109}
110
111bool AbstractView::listen(const wmNotifier & /*notifier*/) const
112{
113 /* Nothing by default. */
114 return false;
115}
116
118{
119 return false;
120}
121
122void AbstractView::draw_overlays(const ARegion & /*region*/, const Block & /*block*/) const
123{
124 /* Nothing by default. */
125}
126
128{
129 return false;
130}
131
133{
134 return false;
135}
136
138{
139 BLI_assert_msg(false, "Unsupported for this view type");
140}
141
142std::optional<uiViewState> AbstractView::persistent_state() const
143{
144 return {};
145}
146
148
150
151/* ---------------------------------------------------------------------- */
154
155void AbstractView::filter(std::optional<StringRef> filter_str)
156{
157 needs_filtering_ = false;
158
159 if (!filter_str) {
160 return;
161 }
162
163 const bool is_empty = filter_str->is_empty();
164 const bool filter_changed = filter_str != prev_filter_string_;
165 prev_filter_string_ = *filter_str;
166
167 bool has_search_highlight = false;
168 this->foreach_view_item([&](AbstractViewItem &item) {
169 item.is_filtered_visible_ = is_empty ||
170 item.should_be_filtered_visible(StringRefNull(*filter_str));
171
172 if (!is_empty) {
173 /* Allow view types to hook into the filtering. For example tree views ensure matching
174 * children have their parents visible and uncollapsed. If the search query is empty, all
175 * items are visible by default, and nothing has to be done. */
176 item.on_filter();
177 }
178
179 if (filter_changed) {
180 item.is_highlighted_search_ = false;
181 /* On new filtering input, force the first visible item to be highlighted and in view, so
182 * enter activates it. */
183 if (item.is_filtered_visible_ && !has_search_highlight) {
184 item.is_highlighted_search_ = true;
185 has_search_highlight = true;
186 }
187 }
188 });
189}
190
192
193/* ---------------------------------------------------------------------- */
196
198{
199 return rename_buffer_ != nullptr;
200}
201
203{
204 if (this->is_renaming()) {
205 return false;
206 }
207
208 rename_buffer_ = std::make_unique<decltype(rename_buffer_)::element_type>();
209 return true;
210}
211
213{
215 rename_buffer_ = nullptr;
216}
217
219{
220 return *rename_buffer_;
221}
223{
224 return *rename_buffer_;
225}
226
227std::optional<rcti> AbstractView::get_bounds() const
228{
229 return bounds_;
230}
231
233{
234 return context_menu_title;
235}
236
237void AbstractView::set_context_menu_title(const std::string &title)
238{
239 context_menu_title = title;
240}
241
243{
244 return popup_keep_open_;
245}
246
248{
249 popup_keep_open_ = true;
250}
251
256
258{
259 is_multiselect_supported_ = true;
260}
261
263{
264 return is_multiselect_supported_;
265}
266
267
268} // namespace blender::ui
#define BLI_assert_msg(a, msg)
constexpr StringRefNull()
virtual bool should_be_filtered_visible(StringRefNull filter_string) const
virtual std::optional< bool > should_be_active() const
virtual bool supports_scrolling() const
virtual bool is_fully_visible() const
const AbstractViewItem * search_highlight_item() const
virtual void persistent_state_apply(const uiViewState &state)
bool is_multiselect_supported() const
virtual bool listen(const wmNotifier &) const
std::optional< rcti > get_bounds() const
virtual void change_state_delayed()
virtual std::unique_ptr< DropTargetInterface > create_drop_target()
virtual void foreach_view_item(FunctionRef< void(AbstractViewItem &)> iter_fn) const =0
virtual bool begin_filtering(const bContext &C) const
virtual void scroll(ViewScrollDirection direction)
virtual void update_children_from_old(const AbstractView &old_view)=0
void register_item(AbstractViewItem &item)
Span< char > get_rename_buffer() const
virtual void draw_overlays(const ARegion &region, const Block &block) const
virtual std::optional< uiViewState > persistent_state() const
void update_from_old(Block &new_block)
std::string get_context_menu_title() const
void filter(std::optional< StringRef > filter_str)
void set_context_menu_title(const std::string &title)
AbstractView * block_view_find_matching_in_old_block(const Block &new_block, const AbstractView &new_view)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)