gtk_editor.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include "lumacs/ui_interface.hpp"
  3. #include "lumacs/gtk_renderer.hpp"
  4. #include "lumacs/window.hpp"
  5. #include "lumacs/editor_core.hpp"
  6. #include "lumacs/face.hpp"
  7. #include "lumacs/completion_common.hpp" // For CompletionCandidate
  8. #include <chrono> // For std::chrono
  9. #include <gtkmm.h>
  10. #include <pangomm.h>
  11. #include <memory>
  12. #include <vector>
  13. #include <map>
  14. namespace lumacs {
  15. // Forward declarations
  16. class EditorCore;
  17. class Window;
  18. class GtkCompletionPopup; // Forward declaration
  19. // Custom Gtk::ApplicationWindow to make constructor public
  20. class LumacsWindow : public Gtk::ApplicationWindow {
  21. public:
  22. explicit LumacsWindow(const Glib::RefPtr<Gtk::Application>& application);
  23. };
  24. class GtkEditor : public IEditorView {
  25. public:
  26. GtkEditor();
  27. ~GtkEditor() override;
  28. void init() override;
  29. void run() override;
  30. void handle_editor_event(EditorEvent event) override;
  31. void set_core(EditorCore* core) override;
  32. void queue_redraw_all_windows(Gtk::Widget* widget);
  33. private:
  34. EditorCore* core_;
  35. std::shared_ptr<Window> cached_active_window_;
  36. Glib::RefPtr<Gtk::Application> app_;
  37. Gtk::Window* window_ = nullptr;
  38. Gtk::DrawingArea* drawing_area_ = nullptr; // Raw pointer to the main drawing area if single window
  39. Gtk::Widget* content_widget_ = nullptr; // The root widget of the editor content (Paned or DrawingArea)
  40. // For cursor blinking logic
  41. bool cursor_visible_ = true;
  42. sigc::connection cursor_timer_connection_;
  43. std::chrono::steady_clock::time_point last_cursor_move_time_;
  44. static constexpr std::chrono::milliseconds BLINK_INTERVAL = std::chrono::milliseconds(500);
  45. static constexpr std::chrono::milliseconds BLINK_STATIONARY_THRESHOLD = std::chrono::milliseconds(1000); // 1 second
  46. std::unique_ptr<GtkRenderer> gtk_renderer_;
  47. std::unique_ptr<GtkCompletionPopup> completion_popup_; // Completion popup
  48. protected:
  49. void on_activate();
  50. void on_draw(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height);
  51. bool on_cursor_blink();
  52. void rebuild_layout();
  53. Gtk::Widget* create_widget_for_layout_node(std::shared_ptr<LayoutNode> node);
  54. // Completion popup helpers
  55. void show_completion_popup();
  56. void hide_completion_popup();
  57. void on_completion_selected(CompletionCandidate candidate);
  58. void on_completion_cancelled();
  59. // Global key event handler
  60. std::string get_lumacs_key_name(guint keyval, Gdk::ModifierType state);
  61. bool on_global_key_pressed(guint keyval, guint keycode, Gdk::ModifierType state);
  62. };
  63. std::unique_ptr<IEditorView> create_gtk_editor();
  64. } // namespace lumacs