Cleanup: Use C++ functor for button pushed query callback

C-style callbacks often rely on `void` pointer arguments that are unsafe
because of the removed type. C++ functors allow passing arbitrary data
along the callback, plus convenient features like defining the callback
using a lambda.

Didn't port the `typedef` because it doesn't add much in this case, just
hides the type from the reader who has to look it up first.

Note that this function isn't used in the main branch currently.
This commit is contained in:
Julian Eisel
2023-06-05 12:45:33 +02:00
parent 8eb75deb46
commit cdd4beeb5e
4 changed files with 7 additions and 8 deletions

View File

@@ -552,7 +552,6 @@ typedef void (*uiButSearchListenFn)(const struct wmRegionListenerParams *params,
/* Must return allocated string. */
typedef char *(*uiButToolTipFunc)(struct bContext *C, void *argN, const char *tip);
typedef int (*uiButPushedStateFunc)(struct uiBut *but, const void *arg);
typedef void (*uiBlockHandleFunc)(struct bContext *C, void *arg, int event);
@@ -1758,8 +1757,6 @@ void UI_but_focus_on_enter_event(struct wmWindow *win, uiBut *but);
void UI_but_func_hold_set(uiBut *but, uiButHandleHoldFunc func, void *argN);
void UI_but_func_pushed_state_set(uiBut *but, uiButPushedStateFunc func, const void *arg);
struct PointerRNA *UI_but_extra_operator_icon_add(uiBut *but,
const char *opname,
wmOperatorCallContext opcontext,

View File

@@ -8,6 +8,7 @@
#pragma once
#include <functional>
#include <memory>
#include "BLI_function_ref.hh"
@@ -25,6 +26,7 @@ struct bContext;
struct PointerRNA;
struct StructRNA;
struct uiBlock;
struct uiBut;
struct uiLayout;
struct uiList;
struct uiSearchItems;
@@ -37,6 +39,8 @@ namespace blender::ui {
class AbstractGridView;
class AbstractTreeView;
void UI_but_func_pushed_state_set(uiBut *but, std::function<bool(const uiBut &)> func);
/**
* An item in a breadcrumb-like context. Currently this struct is very simple, but more
* could be added to it in the future, to support interactivity or tooltips, for example.

View File

@@ -2204,7 +2204,7 @@ int ui_but_is_pushed_ex(uiBut *but, double *value)
{
int is_push = 0;
if (but->pushed_state_func) {
return but->pushed_state_func(but, but->pushed_state_arg);
return but->pushed_state_func(*but);
}
if (but->bit) {
@@ -6089,10 +6089,9 @@ void UI_but_func_tooltip_set(uiBut *but, uiButToolTipFunc func, void *arg, uiFre
but->tip_arg_free = free_arg;
}
void UI_but_func_pushed_state_set(uiBut *but, uiButPushedStateFunc func, const void *arg)
void UI_but_func_pushed_state_set(uiBut *but, std::function<bool(const uiBut &)> func)
{
but->pushed_state_func = func;
but->pushed_state_arg = arg;
ui_but_update(but);
}

View File

@@ -267,8 +267,7 @@ struct uiBut {
double *editval = nullptr;
float *editvec = nullptr;
uiButPushedStateFunc pushed_state_func = nullptr;
const void *pushed_state_arg = nullptr;
std::function<bool(const uiBut &)> pushed_state_func;
/** Little indicator (e.g., counter) displayed on top of some icons. */
IconTextOverlay icon_overlay_text = {};