[nautilus/wip/corey/drop-text-filename] files-view-dnd: Improve filename generated from dropped text




commit 2885cae71a2dc2a1309bde3540e5e6179f729e35
Author: Corey Berla <corey berla me>
Date:   Fri Sep 2 11:37:30 2022 -0700

    files-view-dnd: Improve filename generated from dropped text
    
    Make several improvements:
    
    1) Max filename was too long at 128 characters, half it to 64
    2) Tweak min filename to 8 characters (still meaningful)
    3) Stop at the first sentence rather than last sentence. A filename
    should be short and concise, multiple sentences don't make sense.
    4) Start at the start_sentence (i.e. eliminate leading whitespace)
    
    Importantly this patch eliminates the potential inclusion of newlines
    which is problematic in the views (it causes more than 3 lines of the
    filename to appear because of the GtkLabel behavior).

 src/nautilus-files-view-dnd.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
---
diff --git a/src/nautilus-files-view-dnd.c b/src/nautilus-files-view-dnd.c
index 5d3220d37..96a5c8240 100644
--- a/src/nautilus-files-view-dnd.c
+++ b/src/nautilus-files-view-dnd.c
@@ -120,8 +120,8 @@ nautilus_files_view_handle_uri_list_drop (NautilusFilesView *view,
     g_free (container_uri);
 }
 
-#define MAX_LEN_FILENAME 128
-#define MIN_LEN_FILENAME 10
+#define MAX_LEN_FILENAME 64
+#define MIN_LEN_FILENAME 8
 
 static char *
 get_drop_filename (const char *text)
@@ -130,8 +130,9 @@ get_drop_filename (const char *text)
     char trimmed[MAX_LEN_FILENAME];
     int i;
     int last_word = -1;
-    int last_sentence = -1;
+    int end_sentence = -1;
     int last_nonspace = -1;
+    int start_sentence = -1;
     int num_attrs;
     PangoLogAttr *attrs;
     gchar *current_char;
@@ -144,35 +145,39 @@ get_drop_filename (const char *text)
     /* since the end of the text will always match a word boundary don't include it */
     for (i = 0; (i < num_attrs - 1); i++)
     {
-        if (!attrs[i].is_white)
+        if (attrs[i].is_sentence_start && start_sentence == -1)
         {
-            last_nonspace = i;
+            start_sentence = i;
         }
-        if (attrs[i].is_sentence_end)
+        if (!attrs[i].is_white)
         {
-            last_sentence = last_nonspace;
+            last_nonspace = i;
         }
         if (attrs[i].is_word_boundary)
         {
             last_word = last_nonspace;
         }
+        if (attrs[i].is_sentence_end)
+        {
+            end_sentence = last_nonspace;
+            break;
+        }
     }
     g_free (attrs);
 
-    if (last_sentence > 0)
+    if (end_sentence > 0)
     {
-        i = last_sentence;
+        i = end_sentence;
     }
     else
     {
         i = last_word;
     }
 
-    if (i > MIN_LEN_FILENAME)
+    if (i - start_sentence > MIN_LEN_FILENAME)
     {
-        char basename[MAX_LEN_FILENAME];
-        g_utf8_strncpy (basename, trimmed, i);
-        filename = g_strdup_printf ("%s.txt", basename);
+        g_autofree char *substring = g_utf8_substring (trimmed, start_sentence, i);
+        filename = g_strdup_printf ("%s.txt", substring);
     }
     else
     {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]