lua_widget_attributes: use existing functions in gui/widgets/helper.cpp

This commit is contained in:
Subhraman Sarkar 2024-11-28 11:57:21 +05:30
parent 98f0d3721c
commit 2e0f8c453e
3 changed files with 69 additions and 68 deletions

View file

@ -101,6 +101,37 @@ PangoAlignment decode_text_alignment(const std::string& alignment)
return PANGO_ALIGN_LEFT;
}
PangoEllipsizeMode decode_ellipsize_mode(const std::string& ellipsize_mode)
{
if(ellipsize_mode == "start") {
return PANGO_ELLIPSIZE_START;
} else if(ellipsize_mode == "middle") {
return PANGO_ELLIPSIZE_MIDDLE;
} else if(ellipsize_mode == "end") {
return PANGO_ELLIPSIZE_END;
}
if(!ellipsize_mode.empty() && ellipsize_mode != "none") {
ERR_GUI_E << "Invalid text ellipsization mode '" << ellipsize_mode << "', falling back to 'none'.";
}
return PANGO_ELLIPSIZE_NONE;
}
std::string encode_ellipsize_mode(const PangoEllipsizeMode ellipsize_mode)
{
switch(ellipsize_mode) {
case PANGO_ELLIPSIZE_START:
return "start";
case PANGO_ELLIPSIZE_MIDDLE:
return "middle";
case PANGO_ELLIPSIZE_END:
return "end";
default:
return "none";
}
}
std::string encode_text_alignment(const PangoAlignment alignment)
{
switch(alignment) {

View file

@ -18,8 +18,6 @@
#include "color.hpp"
#include "font/text.hpp"
#include <pango/pango-layout.h>
#include <string>
#include <string_view>
@ -36,7 +34,7 @@ namespace gui2
/**
* Converts a color string to a color.
*
* @param color A color string.
* @param color A color string, @see color_t::from_rgba_string.
*
* @returns The color.
*/
@ -45,7 +43,7 @@ color_t decode_color(const std::string& color);
/**
* Converts a text alignment string to a text alignment.
*
* @param alignment An alignment string.
* @param alignment An alignment string, possible values: "left", "right", "center".
*
* @returns The text alignment.
*/
@ -54,7 +52,7 @@ PangoAlignment decode_text_alignment(const std::string& alignment);
/**
* Converts a text weight string to a PangoWeight.
*
* @param weight A weight string, possible values: "thin", "light", "normal", "semibold", "bold", "heavy"
* @param weight A weight string, possible values: "thin", "light", "normal", "semibold", "bold", "heavy".
*
* @returns The corresponding PangoWeight.
*/
@ -63,30 +61,48 @@ PangoWeight decode_text_weight(const std::string& weight);
/**
* Converts a text style string to a PangoStyle.
*
* @param style A style string, possible values: "normal", "italic", "oblique
* @param style A style string, possible values: "normal", "italic", "oblique".
*
* @returns The corresponding PangoStyle.
* @returns The corresponding PangoStyle.
*/
PangoStyle decode_text_style(const std::string& style);
/**
* Converts a text alignment to its string representation.
*
* @param alignment An alignment.
*
* @returns An alignment string.
*/
std::string encode_text_alignment(const PangoAlignment alignment);
/**
* Converts a font style string to a font style.
*
* @param style A font style string.
* @param style A font style string, possible values: "bold", "italic", "normal", "underline".
*
* @returns The font style.
*/
font::pango_text::FONT_STYLE decode_font_style(const std::string& style);
/**
* Converts a text ellipsize mode string to a PangoEllipsizeMode.
*
* @param ellipsize_mode A ellipsize mode string, possible values: "none", "start", "middle", "end".
*
* @returns The corresponding PangoEllipsizeMode.
*/
PangoEllipsizeMode decode_ellipsize_mode(const std::string& ellipsize_mode);
/**
* Converts a PangoAlignment to its string representation.
*
* @param alignment A PangoAlignment.
*
* @returns An alignment string, possible values: "left", "right", "center".
*/
std::string encode_text_alignment(const PangoAlignment alignment);
/**
* Converts a PangoEllipsizeMode to its string representation.
*
* @param ellipsize_mode A PangoEllipsizeMode.
*
* @returns An pango ellipsize mode string, possible values: "none", "start", "middle", "end".
*/
std::string encode_ellipsize_mode(const PangoEllipsizeMode ellipsize_mode);
/**
* Returns a default error message if a mandatory widget is omitted.
*

View file

@ -15,6 +15,7 @@
#include "gui/auxiliary/iterator/iterator.hpp"
#include "gui/widgets/clickable_item.hpp"
#include "gui/widgets/helper.hpp"
#include "gui/widgets/styled_widget.hpp"
#include "gui/widgets/combobox.hpp"
#include "gui/widgets/label.hpp"
@ -459,38 +460,12 @@ WIDGET_SETTER("editable", bool, gui2::text_box)
WIDGET_GETTER("ellipsize_mode", std::string, gui2::styled_widget)
{
std::string s;
switch(w.get_text_ellipse_mode()) {
case(PangoEllipsizeMode::PANGO_ELLIPSIZE_NONE):
s = "none";
break;
case(PangoEllipsizeMode::PANGO_ELLIPSIZE_START):
s = "start";
break;
case(PangoEllipsizeMode::PANGO_ELLIPSIZE_MIDDLE):
s = "middle";
break;
case(PangoEllipsizeMode::PANGO_ELLIPSIZE_END):
s = "end";
}
return s;
return gui2::encode_ellipsize_mode(w.get_text_ellipse_mode());
}
WIDGET_SETTER("ellipsize_mode", std::string, gui2::styled_widget)
{
if(value == "none") {
w.set_text_ellipse_mode(PangoEllipsizeMode::PANGO_ELLIPSIZE_NONE);
} else if(value == "start") {
w.set_text_ellipse_mode(PangoEllipsizeMode::PANGO_ELLIPSIZE_START);
} else if(value == "middle") {
w.set_text_ellipse_mode(PangoEllipsizeMode::PANGO_ELLIPSIZE_MIDDLE);
} else if(value == "end") {
w.set_text_ellipse_mode(PangoEllipsizeMode::PANGO_ELLIPSIZE_END);
} else {
throw std::invalid_argument("ellipsize_mode must be one of <none,start,middle,end>");
}
w.set_text_ellipse_mode(gui2::decode_ellipsize_mode(value));
try_invalidate_layout(w);
}
@ -674,33 +649,12 @@ WIDGET_SETTER("step_size", int, gui2::slider)
WIDGET_GETTER("text_alignment", std::string, gui2::styled_widget)
{
std::string s;
switch(w.get_text_alignment()) {
case(PangoAlignment::PANGO_ALIGN_LEFT):
s = "left";
break;
case(PangoAlignment::PANGO_ALIGN_RIGHT):
s = "right";
break;
case(PangoAlignment::PANGO_ALIGN_CENTER):
s = "center";
}
return s;
return gui2::encode_text_alignment(w.get_text_alignment());
}
WIDGET_SETTER("text_alignment", std::string, gui2::styled_widget)
{
if(value == "left") {
w.set_text_alignment(PangoAlignment::PANGO_ALIGN_LEFT);
} else if(value == "right") {
w.set_text_alignment(PangoAlignment::PANGO_ALIGN_RIGHT);
} else if(value == "center") {
w.set_text_alignment(PangoAlignment::PANGO_ALIGN_CENTER);
} else {
throw std::invalid_argument("text_alignment must be one of <left,center,right>");
}
w.set_text_alignment(gui2::decode_text_alignment(value));
}
WIDGET_GETTER("tooltip", t_string, gui2::styled_widget)