diff options
author | Robert Tari <robert@tari.in> | 2021-06-14 23:08:17 +0200 |
---|---|---|
committer | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2021-06-16 08:26:44 +0200 |
commit | 9be11d94c663ea66cd41a9364a78531537ae405e (patch) | |
tree | 51f125b8c5b0560b1ed44668591f6cf3f7b7e5f0 | |
parent | db18ded6c8a4ab9d4a0921a3f52e8f3507607238 (diff) | |
download | libayatana-common-9be11d94c663ea66cd41a9364a78531537ae405e.tar.gz libayatana-common-9be11d94c663ea66cd41a9364a78531537ae405e.tar.bz2 libayatana-common-9be11d94c663ea66cd41a9364a78531537ae405e.zip |
Add ability to ellipsize dynamic menu item lengths.
Plus making the maximum length of non-ellipsized strings configurable
via GSettings.
- data/org.ayatana.common.gschema.xml.in: Add file.
- data/CMakeLists.txt: Add file.
- CMakeLists.txt: Add gio-2.0 dependency + 'data' build folder.
- src/utils.*: Add ayatana_common_utils_elipsize function + include
glib-object.h and gio.h
- tests/tst_utils.cpp: Add StringFunctionsTest.
- tests/CMakeLists.txt: Add GLIB_LIBRARIES to target.
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | data/CMakeLists.txt | 6 | ||||
-rw-r--r-- | data/org.ayatana.common.gschema.xml.in | 10 | ||||
-rw-r--r-- | src/utils.c | 26 | ||||
-rw-r--r-- | src/utils.h | 4 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/tst_utils.cpp | 52 |
7 files changed, 101 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 7430e86..f2b4b50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ include (FindPkgConfig) pkg_check_modules(GLIB REQUIRED glib-2.0>=2.36 + gio-2.0>=2.36 ) include_directories (${GLIB_INCLUDE_DIRS}) @@ -46,6 +47,7 @@ include_directories(${URLDISPATCHER_INCLUDE_DIRS}) set(CC_WARNING_ARGS " -Wall -pedantic -Wextra -Wno-missing-field-initializers") add_subdirectory(src) +add_subdirectory(data) if (ENABLE_TESTS) include(CTest) diff --git a/data/CMakeLists.txt b/data/CMakeLists.txt new file mode 100644 index 0000000..7f12337 --- /dev/null +++ b/data/CMakeLists.txt @@ -0,0 +1,6 @@ +# org.ayatana.common.gschema.xml + +find_package(GSettings) +set(ENV{LC_ALL} "C") +execute_process(COMMAND intltool-merge -quiet --xml-style --utf8 --no-translations "${CMAKE_CURRENT_SOURCE_DIR}/org.ayatana.common.gschema.xml.in" "${CMAKE_CURRENT_BINARY_DIR}/org.ayatana.common.gschema.xml") +add_schema("org.ayatana.common.gschema.xml") diff --git a/data/org.ayatana.common.gschema.xml.in b/data/org.ayatana.common.gschema.xml.in new file mode 100644 index 0000000..30c5e03 --- /dev/null +++ b/data/org.ayatana.common.gschema.xml.in @@ -0,0 +1,10 @@ +<schemalist> + <schema gettext-domain="ayatana-common" id="org.ayatana.common" path="/org/ayatana/common/"> + <key name="max-menu-text-length" type="u"> + <range min="1" max="100"/> + <default>50</default> + <_summary>Maximum menu text length</_summary> + <_description>The number of characters to show in dynamic menu items before the text is ellipsized.</_description> + </key> + </schema> +</schemalist> diff --git a/src/utils.c b/src/utils.c index d735172..1d21b02 100644 --- a/src/utils.c +++ b/src/utils.c @@ -273,3 +273,29 @@ ayatana_common_utils_zenity_warning (const char * icon_name, g_free (zenity); return confirmed; } + +void ayatana_common_utils_elipsize(char *sText) +{ + guint nMaxLetters = 50; + glong nLetters = g_utf8_strlen(sText, -1); + GSettingsSchemaSource *pSource = g_settings_schema_source_get_default(); + + if (pSource != NULL) + { + GSettingsSchema *pSchema = g_settings_schema_source_lookup(pSource, "org.ayatana.common", FALSE); + + if (pSchema != NULL) + { + g_settings_schema_unref(pSchema); + GSettings *pSettings = g_settings_new("org.ayatana.common"); + nMaxLetters = g_settings_get_uint(pSettings, "max-menu-text-length"); + g_object_unref(pSettings); + } + } + + if (nLetters > nMaxLetters + 4) + { + gchar *pLastChar = g_utf8_offset_to_pointer(sText, nMaxLetters); + memcpy(pLastChar, "...\0", 4); + } +} diff --git a/src/utils.h b/src/utils.h index 0fa643b..13e2d2f 100644 --- a/src/utils.h +++ b/src/utils.h @@ -19,6 +19,8 @@ #pragma once #include <glib.h> +#include <glib-object.h> +#include <gio/gio.h> #define DESKTOP_LOMIRI "Lomiri" #define DESKTOP_UNITY "Unity" @@ -47,3 +49,5 @@ gboolean ayatana_common_utils_execute_command(const gchar * cmd); gboolean ayatana_common_utils_open_url(const gchar * url); gboolean ayatana_common_utils_have_program(const gchar * program); gboolean ayatana_common_utils_zenity_warning(const char *icon_name, const char *title, const char *text); + +void ayatana_common_utils_elipsize(char *sText); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c6e26ae..d0d05c0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,7 +9,7 @@ include_directories( add_executable(tst_utils tst_utils.cpp) target_link_libraries(tst_utils ayatana-common - + ${GLIB_LIBRARIES} ${GTEST_LIBRARIES} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARIES} diff --git a/tests/tst_utils.cpp b/tests/tst_utils.cpp index c62bf43..25099fd 100644 --- a/tests/tst_utils.cpp +++ b/tests/tst_utils.cpp @@ -79,3 +79,55 @@ TEST_F(XdgCurrentDesktopUtilsTest, isBudgie) setenv("XDG_CURRENT_DESKTOP", "Budgie:GNOME", 1); EXPECT_TRUE(ayatana_common_utils_is_budgie()); } + +class StringFunctionsTest : public ::testing::Test +{ +public: + + StringFunctionsTest() + { + } + + void SetUp() + { + GSettingsSchemaSource *pSource = g_settings_schema_source_get_default(); + + if (pSource != NULL) + { + GSettingsSchema *pSchema = g_settings_schema_source_lookup(pSource, "org.ayatana.common", FALSE); + + if (pSchema != NULL) + { + g_settings_schema_unref(pSchema); + this->pSettings = g_settings_new("org.ayatana.common"); + this->nMaxLetters = g_settings_get_uint(pSettings, "max-menu-text-length"); + g_settings_set_uint(this->pSettings, "max-menu-text-length", 50); + } + } + } + + void TearDown() + { + if (this->pSettings != NULL) + { + g_settings_set_uint(pSettings, "max-menu-text-length", this->nMaxLetters); + g_object_unref(this->pSettings); + } + } + +private: + + GSettings *pSettings; + guint nMaxLetters; +}; + +TEST_F(StringFunctionsTest, elipsize) +{ + gchar *sTest1 = g_strdup("öüóőúéáűšđß"); + ayatana_common_utils_elipsize((gchar*)sTest1); + EXPECT_STREQ(sTest1, "öüóőúéáűšđß"); + + gchar *sTest2 = g_strdup("123456789012345678901234567890123456789012345öüóőúéáűšđß"); + ayatana_common_utils_elipsize((gchar*)sTest2); + EXPECT_STREQ(sTest2, "123456789012345678901234567890123456789012345öüóőú..."); +} |