Skip to content

Commit 8a666a0

Browse files
committed
Fix int and animation option writes
Trying to set int or animate options does not work when typing them into the spin button field. Scrolling and clicking the tick boxes to change the value does, however. Apparently, this is a problem with gtkmm when calling get_value_as_int() in the changed handler. It does not affect double options, which use get_value(). This solution uses the C API to work around the problem.
1 parent 5aea83e commit 8a666a0

File tree

2 files changed

+80
-28
lines changed

2 files changed

+80
-28
lines changed

src/wcm.cpp

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,41 @@ void Option::set_save(const ArgTypes &... args)
319319
WCM::get_instance()->save_config(plugin);
320320
}
321321

322+
static void update_int_sb_option_value(GtkSpinButton *spin_button, Option *option)
323+
{
324+
option->set_save(std::to_string(gtk_spin_button_get_value_as_int(spin_button)));
325+
}
326+
327+
static void update_animate_sb_option_value(GtkSpinButton *spin_button, animate_option *option)
328+
{
329+
option->option->set_save(std::to_string(gtk_spin_button_get_value_as_int(
330+
spin_button)) + "ms " + gtk_combo_box_text_get_active_text(option->combo_box->gobj()));
331+
}
332+
333+
static void update_animate_cb_option_value(GtkComboBoxText *combo_box, animate_option *option)
334+
{
335+
option->option->set_save(std::to_string(gtk_spin_button_get_value_as_int(
336+
option->spin_button->gobj())) + "ms " + gtk_combo_box_text_get_active_text(combo_box));
337+
}
338+
339+
OptionWidget::~OptionWidget()
340+
{
341+
if (int_spin_button)
342+
{
343+
g_signal_handler_disconnect(int_spin_button->gobj(), int_sb_handle);
344+
}
345+
346+
if (animate_spin_button)
347+
{
348+
g_signal_handler_disconnect(animate_spin_button->gobj(), animate_sb_handle);
349+
}
350+
351+
if (animate_combo_box)
352+
{
353+
g_signal_handler_disconnect(animate_combo_box->gobj(), animate_cb_handle);
354+
}
355+
}
356+
322357
OptionWidget::OptionWidget(Option *option) : Gtk::Box(Gtk::ORIENTATION_HORIZONTAL,
323358
10)
324359
{
@@ -344,20 +379,18 @@ OptionWidget::OptionWidget(Option *option) : Gtk::Box(Gtk::ORIENTATION_HORIZONTA
344379
std::get<int>(option->default_value));
345380
if (option->int_labels.empty())
346381
{
347-
auto spin_button = std::make_unique<Gtk::SpinButton>(
382+
int_spin_button = std::make_unique<Gtk::SpinButton>(
348383
Gtk::Adjustment::create(value, option->data.min, option->data.max,
349384
1));
350-
spin_button->signal_changed().connect(sigc::track_obj(
351-
[=, widget = spin_button.get()]
352-
{
353-
option->set_save(widget->get_value_as_int());
354-
}, tracker));
385+
int_sb_handle =
386+
g_signal_connect(int_spin_button->gobj(), "value-changed", G_CALLBACK(
387+
update_int_sb_option_value), option);
355388
reset_button.signal_clicked().connect(
356-
[=, widget = spin_button.get()]
389+
[=, widget = int_spin_button.get()]
357390
{
358391
widget->set_value(std::get<int>(option->default_value));
359392
});
360-
pack_end(std::move(spin_button));
393+
pack_end(std::move(int_spin_button));
361394
} else
362395
{
363396
auto combo_box = std::make_unique<Gtk::ComboBoxText>();
@@ -397,9 +430,9 @@ OptionWidget::OptionWidget(Option *option) : Gtk::Box(Gtk::ORIENTATION_HORIZONTA
397430
int length_value = set_value ? set_value->length_ms : default_value->length_ms;
398431
std::string easing_value = set_value ? set_value->easing_name : default_value->easing_name;
399432

400-
auto spin_button = std::make_unique<Gtk::SpinButton>(
433+
animate_spin_button = std::make_unique<Gtk::SpinButton>(
401434
Gtk::Adjustment::create(length_value, option->data.min, option->data.max, 1));
402-
auto combo_box = std::make_unique<Gtk::ComboBoxText>();
435+
animate_combo_box = std::make_unique<Gtk::ComboBoxText>();
403436
for (const auto& easing : wf::animation::smoothing::get_available_smooth_functions())
404437
{
405438
static const std::map<std::string, int> preffered_easing_position = {
@@ -409,30 +442,34 @@ OptionWidget::OptionWidget(Option *option) : Gtk::Box(Gtk::ORIENTATION_HORIZONTA
409442
};
410443
if (preffered_easing_position.count(easing) != 0)
411444
{
412-
combo_box->insert(preffered_easing_position.at(easing), easing);
445+
animate_combo_box->insert(preffered_easing_position.at(easing), easing);
413446
} else
414447
{
415-
combo_box->append(easing);
448+
animate_combo_box->append(easing);
416449
}
417450
}
418451

419-
combo_box->set_active_text(easing_value);
420-
421-
auto update_option_value = [=, length_widget = spin_button.get(), easing_widget = combo_box.get()]
422-
{
423-
option->set_save(std::to_string(
424-
length_widget->get_value_as_int()) + "ms " + easing_widget->get_active_text().c_str());
425-
};
426-
spin_button->signal_changed().connect(sigc::track_obj(update_option_value, tracker));
427-
combo_box->signal_changed().connect(std::move(update_option_value));
428-
reset_button.signal_clicked().connect([=, length_widget = spin_button.get(),
429-
easing_widget = combo_box.get()]
452+
ao = {
453+
.option = option,
454+
.spin_button = animate_spin_button.get(),
455+
.combo_box = animate_combo_box.get(),
456+
};
457+
458+
animate_combo_box->set_active_text(easing_value);
459+
animate_sb_handle =
460+
g_signal_connect(animate_spin_button->gobj(), "value-changed", G_CALLBACK(
461+
update_animate_sb_option_value), &ao);
462+
animate_cb_handle =
463+
g_signal_connect(animate_combo_box->gobj(), "changed", G_CALLBACK(
464+
update_animate_cb_option_value), &ao);
465+
reset_button.signal_clicked().connect([=, length_widget = animate_spin_button.get(),
466+
easing_widget = animate_combo_box.get()]
430467
{
431468
length_widget->set_value(default_value->length_ms);
432469
easing_widget->set_active_text(default_value->easing_name);
433470
});
434-
pack_end(std::move(spin_button));
435-
pack_end(std::move(combo_box));
471+
pack_end(std::move(animate_spin_button));
472+
pack_end(std::move(animate_combo_box));
436473
}
437474
break;
438475

@@ -995,8 +1032,8 @@ OptionSubgroupWidget::OptionSubgroupWidget(Option *subgroup)
9951032
expander.add(expander_layout);
9961033
for (Option *option : subgroup->options)
9971034
{
998-
option_widgets.emplace_back(option);
999-
expander_layout.pack_start(option_widgets.back());
1035+
option_widgets.push_back(std::make_unique<OptionWidget>(option));
1036+
expander_layout.pack_start(*option_widgets.back());
10001037
}
10011038
}
10021039

src/wcm.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939

4040
#include "metadata.hpp"
4141

42+
struct animate_option
43+
{
44+
Option *option;
45+
Gtk::SpinButton *spin_button;
46+
Gtk::ComboBoxText *combo_box;
47+
};
48+
4249
class MainPage : public Gtk::ScrolledWindow
4350
{
4451
public:
@@ -140,6 +147,13 @@ class OptionWidget : public Gtk::Box
140147
Gtk::Button reset_button;
141148
sigc::trackable tracker;
142149

150+
guint int_sb_handle;
151+
guint animate_sb_handle;
152+
guint animate_cb_handle;
153+
std::unique_ptr<Gtk::SpinButton> animate_spin_button, int_spin_button;
154+
std::unique_ptr<Gtk::ComboBoxText> animate_combo_box;
155+
animate_option ao;
156+
143157
inline void pack_end(std::unique_ptr<Gtk::Widget> && widget, bool expand = false,
144158
bool fill = false)
145159
{
@@ -149,6 +163,7 @@ class OptionWidget : public Gtk::Box
149163

150164
public:
151165
OptionWidget(Option *option);
166+
~OptionWidget();
152167
};
153168

154169
class DynamicListBase : public Gtk::Box
@@ -261,7 +276,7 @@ class OptionSubgroupWidget : public Gtk::Frame
261276
{
262277
Gtk::Expander expander;
263278
Gtk::Box expander_layout = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 10);
264-
std::vector<OptionWidget> option_widgets;
279+
std::vector<std::unique_ptr<OptionWidget>> option_widgets;
265280

266281
public:
267282
OptionSubgroupWidget(Option *subgroup);

0 commit comments

Comments
 (0)