[gnote] Add accelerator Ctrl+D for insert timestamp



commit 46a9f3cb287636f7f2f39dc82c689366591d9fcc
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Jan 17 15:19:54 2021 +0200

    Add accelerator Ctrl+D for insert timestamp

 .../inserttimestamp/inserttimestampnoteaddin.cpp   | 39 ++++++++++++++++++++++
 .../inserttimestamp/inserttimestampnoteaddin.hpp   |  6 ++++
 2 files changed, 45 insertions(+)
---
diff --git a/src/addins/inserttimestamp/inserttimestampnoteaddin.cpp 
b/src/addins/inserttimestamp/inserttimestampnoteaddin.cpp
index 829501d5..761c3db3 100644
--- a/src/addins/inserttimestamp/inserttimestampnoteaddin.cpp
+++ b/src/addins/inserttimestamp/inserttimestampnoteaddin.cpp
@@ -22,6 +22,7 @@
 
 #include <glibmm/i18n.h>
 
+#include "debug.hpp"
 #include "sharp/datetime.hpp"
 #include "iactionmanager.hpp"
 #include "ignote.hpp"
@@ -63,6 +64,9 @@ namespace inserttimestamp {
         .connect(sigc::ptr_fun(InsertTimestampNoteAddin::on_format_setting_changed));
       s_date_format = InsertTimestampPreferences::settings()->get_string(INSERT_TIMESTAMP_FORMAT);
     }
+
+    get_window()->signal_foregrounded.connect(sigc::mem_fun(*this, 
&InsertTimestampNoteAddin::on_note_foregrounded));
+    get_window()->signal_backgrounded.connect(sigc::mem_fun(*this, 
&InsertTimestampNoteAddin::on_note_backgrounded));
   }
 
 
@@ -75,7 +79,42 @@ namespace inserttimestamp {
   }
 
 
+  void InsertTimestampNoteAddin::on_note_foregrounded()
+  {
+    auto window = dynamic_cast<gnote::MainWindow*>(get_window()->host());
+    if(!window) {
+      ERR_OUT("No host on foregrounded note window");
+      return;
+    }
+
+    m_accelerator = window->keybinder().add_accelerator(
+      sigc::mem_fun(*this, &InsertTimestampNoteAddin::on_insert_activated), GDK_KEY_D, Gdk::CONTROL_MASK, 
(Gtk::AccelFlags)0);
+  }
+
+
+  void InsertTimestampNoteAddin::on_note_backgrounded()
+  {
+    if(m_accelerator) {
+      auto window = dynamic_cast<gnote::MainWindow*>(get_window()->host());
+      if(window) {
+        window->keybinder().remove_accelerator(m_accelerator);
+      }
+      else {
+        ERR_OUT("No host on just backgrounded note window");
+      }
+
+      m_accelerator = nullptr;
+    }
+  }
+
+
   void InsertTimestampNoteAddin::on_menu_item_activated(const Glib::VariantBase&)
+  {
+    on_insert_activated();
+  }
+
+
+  void InsertTimestampNoteAddin::on_insert_activated()
   {
     Glib::ustring text = sharp::date_time_to_string(Glib::DateTime::create_now_local(), s_date_format);
     Gtk::TextIter cursor = get_buffer()->get_iter_at_mark (get_buffer()->get_insert());
diff --git a/src/addins/inserttimestamp/inserttimestampnoteaddin.hpp 
b/src/addins/inserttimestamp/inserttimestampnoteaddin.hpp
index 26814182..818c0a0e 100644
--- a/src/addins/inserttimestamp/inserttimestampnoteaddin.hpp
+++ b/src/addins/inserttimestamp/inserttimestampnoteaddin.hpp
@@ -49,6 +49,7 @@ public:
     {
       return new InsertTimestampNoteAddin;
     }
+  InsertTimestampNoteAddin() : m_accelerator(nullptr) {}
   virtual void initialize() override;
   virtual void shutdown() override;
   virtual void on_note_opened() override;
@@ -59,7 +60,12 @@ private:
   static Glib::ustring s_date_format;
   static sigc::connection s_on_format_setting_changed_cid;
 
+  void on_note_foregrounded();
+  void on_note_backgrounded();
   void on_menu_item_activated(const Glib::VariantBase&);
+  void on_insert_activated();
+
+  void *m_accelerator;
 };
 
 }


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