Skip to content

Commit

Permalink
Fix int and animation option writes (#86)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
soreau authored Aug 23, 2024
1 parent 197d492 commit 74a16f5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 28 deletions.
91 changes: 64 additions & 27 deletions src/wcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,41 @@ void Option::set_save(const ArgTypes &... args)
WCM::get_instance()->save_config(plugin);
}

static void update_int_sb_option_value(GtkSpinButton *spin_button, Option *option)
{
option->set_save(std::to_string(gtk_spin_button_get_value_as_int(spin_button)));
}

static void update_animate_sb_option_value(GtkSpinButton *spin_button, animate_option *option)
{
option->option->set_save(std::to_string(gtk_spin_button_get_value_as_int(
spin_button)) + "ms " + gtk_combo_box_text_get_active_text(option->combo_box->gobj()));
}

static void update_animate_cb_option_value(GtkComboBoxText *combo_box, animate_option *option)
{
option->option->set_save(std::to_string(gtk_spin_button_get_value_as_int(
option->spin_button->gobj())) + "ms " + gtk_combo_box_text_get_active_text(combo_box));
}

OptionWidget::~OptionWidget()
{
if (int_spin_button)
{
g_signal_handler_disconnect(int_spin_button->gobj(), int_sb_handle);
}

if (animate_spin_button)
{
g_signal_handler_disconnect(animate_spin_button->gobj(), animate_sb_handle);
}

if (animate_combo_box)
{
g_signal_handler_disconnect(animate_combo_box->gobj(), animate_cb_handle);
}
}

OptionWidget::OptionWidget(Option *option) : Gtk::Box(Gtk::ORIENTATION_HORIZONTAL,
10)
{
Expand All @@ -344,20 +379,18 @@ OptionWidget::OptionWidget(Option *option) : Gtk::Box(Gtk::ORIENTATION_HORIZONTA
std::get<int>(option->default_value));
if (option->int_labels.empty())
{
auto spin_button = std::make_unique<Gtk::SpinButton>(
int_spin_button = std::make_unique<Gtk::SpinButton>(
Gtk::Adjustment::create(value, option->data.min, option->data.max,
1));
spin_button->signal_changed().connect(sigc::track_obj(
[=, widget = spin_button.get()]
{
option->set_save(widget->get_value_as_int());
}, tracker));
int_sb_handle =
g_signal_connect(int_spin_button->gobj(), "value-changed", G_CALLBACK(
update_int_sb_option_value), option);
reset_button.signal_clicked().connect(
[=, widget = spin_button.get()]
[=, widget = int_spin_button.get()]
{
widget->set_value(std::get<int>(option->default_value));
});
pack_end(std::move(spin_button));
pack_end(std::move(int_spin_button));
} else
{
auto combo_box = std::make_unique<Gtk::ComboBoxText>();
Expand Down Expand Up @@ -397,9 +430,9 @@ OptionWidget::OptionWidget(Option *option) : Gtk::Box(Gtk::ORIENTATION_HORIZONTA
int length_value = set_value ? set_value->length_ms : default_value->length_ms;
std::string easing_value = set_value ? set_value->easing_name : default_value->easing_name;

auto spin_button = std::make_unique<Gtk::SpinButton>(
animate_spin_button = std::make_unique<Gtk::SpinButton>(
Gtk::Adjustment::create(length_value, option->data.min, option->data.max, 1));
auto combo_box = std::make_unique<Gtk::ComboBoxText>();
animate_combo_box = std::make_unique<Gtk::ComboBoxText>();
for (const auto& easing : wf::animation::smoothing::get_available_smooth_functions())
{
static const std::map<std::string, int> preffered_easing_position = {
Expand All @@ -409,30 +442,34 @@ OptionWidget::OptionWidget(Option *option) : Gtk::Box(Gtk::ORIENTATION_HORIZONTA
};
if (preffered_easing_position.count(easing) != 0)
{
combo_box->insert(preffered_easing_position.at(easing), easing);
animate_combo_box->insert(preffered_easing_position.at(easing), easing);
} else
{
combo_box->append(easing);
animate_combo_box->append(easing);
}
}

combo_box->set_active_text(easing_value);

auto update_option_value = [=, length_widget = spin_button.get(), easing_widget = combo_box.get()]
{
option->set_save(std::to_string(
length_widget->get_value_as_int()) + "ms " + easing_widget->get_active_text().c_str());
};
spin_button->signal_changed().connect(sigc::track_obj(update_option_value, tracker));
combo_box->signal_changed().connect(std::move(update_option_value));
reset_button.signal_clicked().connect([=, length_widget = spin_button.get(),
easing_widget = combo_box.get()]
ao = {
.option = option,
.spin_button = animate_spin_button.get(),
.combo_box = animate_combo_box.get(),
};

animate_combo_box->set_active_text(easing_value);
animate_sb_handle =
g_signal_connect(animate_spin_button->gobj(), "value-changed", G_CALLBACK(
update_animate_sb_option_value), &ao);
animate_cb_handle =
g_signal_connect(animate_combo_box->gobj(), "changed", G_CALLBACK(
update_animate_cb_option_value), &ao);
reset_button.signal_clicked().connect([=, length_widget = animate_spin_button.get(),
easing_widget = animate_combo_box.get()]
{
length_widget->set_value(default_value->length_ms);
easing_widget->set_active_text(default_value->easing_name);
});
pack_end(std::move(spin_button));
pack_end(std::move(combo_box));
pack_end(std::move(animate_spin_button));
pack_end(std::move(animate_combo_box));
}
break;

Expand Down Expand Up @@ -995,8 +1032,8 @@ OptionSubgroupWidget::OptionSubgroupWidget(Option *subgroup)
expander.add(expander_layout);
for (Option *option : subgroup->options)
{
option_widgets.emplace_back(option);
expander_layout.pack_start(option_widgets.back());
option_widgets.push_back(std::make_unique<OptionWidget>(option));
expander_layout.pack_start(*option_widgets.back());
}
}

Expand Down
17 changes: 16 additions & 1 deletion src/wcm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@

#include "metadata.hpp"

struct animate_option
{
Option *option;
Gtk::SpinButton *spin_button;
Gtk::ComboBoxText *combo_box;
};

class MainPage : public Gtk::ScrolledWindow
{
public:
Expand Down Expand Up @@ -140,6 +147,13 @@ class OptionWidget : public Gtk::Box
Gtk::Button reset_button;
sigc::trackable tracker;

guint int_sb_handle;
guint animate_sb_handle;
guint animate_cb_handle;
std::unique_ptr<Gtk::SpinButton> animate_spin_button, int_spin_button;
std::unique_ptr<Gtk::ComboBoxText> animate_combo_box;
animate_option ao;

inline void pack_end(std::unique_ptr<Gtk::Widget> && widget, bool expand = false,
bool fill = false)
{
Expand All @@ -149,6 +163,7 @@ class OptionWidget : public Gtk::Box

public:
OptionWidget(Option *option);
~OptionWidget();
};

class DynamicListBase : public Gtk::Box
Expand Down Expand Up @@ -261,7 +276,7 @@ class OptionSubgroupWidget : public Gtk::Frame
{
Gtk::Expander expander;
Gtk::Box expander_layout = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 10);
std::vector<OptionWidget> option_widgets;
std::vector<std::unique_ptr<OptionWidget>> option_widgets;

public:
OptionSubgroupWidget(Option *subgroup);
Expand Down

0 comments on commit 74a16f5

Please sign in to comment.