Bläddra i källkod

feat(ui): implement hover tooltips

Added basic hover tooltips using Gtk::EventControllerMotion. Displays the word under the mouse cursor and its line number. Resolved build issues related to iostream include order.
Bernardo Magri 1 månad sedan
förälder
incheckning
bb1fefc2a8
1 ändrade filer med 32 tillägg och 0 borttagningar
  1. 32 0
      src/gtk_editor.cpp

+ 32 - 0
src/gtk_editor.cpp

@@ -6,6 +6,7 @@
 #include <filesystem>
 #include <vector>
 #include <functional> // For std::function
+#include <cctype>     // For std::isalnum
 
 // Check if GTK is enabled in build
 #ifdef LUMACS_WITH_GTK
@@ -581,6 +582,37 @@ protected:
                 }
             });
             drawing_area->add_controller(context_menu_controller);
+
+            // Add motion handling for tooltips
+            auto motion_controller = Gtk::EventControllerMotion::create();
+            std::weak_ptr<Window> weak_window_motion = node->window;
+            motion_controller->signal_motion().connect([this, weak_window_motion, drawing_area](double x, double y) {
+                if (auto window = weak_window_motion.lock()) {
+                    if (auto pos = resolve_screen_pos(window, x, y)) {
+                        const auto& line_text = window->buffer().line(pos->line);
+                        
+                        // Find word under cursor (alphanumeric only for simplicity)
+                        size_t word_start = pos->column;
+                        while (word_start > 0 && word_start <= line_text.length() && std::isalnum(line_text[word_start - 1])) {
+                            word_start--;
+                        }
+                        size_t word_end = pos->column;
+                        while (word_end < line_text.length() && std::isalnum(line_text[word_end])) {
+                            word_end++;
+                        }
+                        std::string word = line_text.substr(word_start, word_end - word_start);
+                        
+                        if (!word.empty()) {
+                            drawing_area->set_tooltip_text("Symbol: " + word + "\nLine: " + std::to_string(pos->line + 1));
+                        } else {
+                            drawing_area->set_tooltip_text(""); // Clear tooltip
+                        }
+                    } else {
+                        drawing_area->set_tooltip_text(""); // Clear tooltip
+                    }
+                }
+            });
+            drawing_area->add_controller(motion_controller);
             
             // Store reference for single-window compatibility
             if (!drawing_area_) {