[niepce] rust: Port TokenTextView to Rust



commit 171cd8ef8b9952b79f29bf5cf6de1f7ae610c0d9
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Oct 15 22:57:15 2022 -0400

    rust: Port TokenTextView to Rust

 crates/npc-fwk/src/toolkit/widgets.rs              |  2 +
 .../npc-fwk/src/toolkit/widgets/token_text_view.rs | 82 ++++++++++++++++++++++
 src/Makefile.am                                    |  1 +
 3 files changed, 85 insertions(+)
---
diff --git a/crates/npc-fwk/src/toolkit/widgets.rs b/crates/npc-fwk/src/toolkit/widgets.rs
index 1f374779..c20ec9f7 100644
--- a/crates/npc-fwk/src/toolkit/widgets.rs
+++ b/crates/npc-fwk/src/toolkit/widgets.rs
@@ -18,9 +18,11 @@
  */
 
 pub mod rating_label;
+mod token_text_view;
 mod toolbox_item;
 
 // Re-exports
+pub use token_text_view::TokenTextView;
 pub use toolbox_item::ToolboxItem;
 
 pub mod prelude {
diff --git a/crates/npc-fwk/src/toolkit/widgets/token_text_view.rs 
b/crates/npc-fwk/src/toolkit/widgets/token_text_view.rs
new file mode 100644
index 00000000..36c25b36
--- /dev/null
+++ b/crates/npc-fwk/src/toolkit/widgets/token_text_view.rs
@@ -0,0 +1,82 @@
+/*
+ * niepce - fwk/toolkit/widgets/token_text_view.rs
+ *
+ * Copyright (C) 2022 Hubert Figuière
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+use gtk4::prelude::*;
+
+glib::wrapper! {
+    /// A text view that get receive a list of token.
+    ///
+    /// Work in progress.
+    pub struct TokenTextView(
+        ObjectSubclass<imp::TokenTextView>)
+        @extends gtk4::TextView, gtk4::Widget;
+}
+
+impl TokenTextView {
+    pub fn new() -> TokenTextView {
+        glib::Object::new(&[("wrap-mode", &gtk4::WrapMode::Word)])
+            .expect("Failed to create TokenTextView Widget")
+    }
+
+    /// Get the tokens from the text.
+    pub fn tokens(&self) -> Vec<String> {
+        let start = self.buffer().start_iter();
+        let end = self.buffer().end_iter();
+        let text = self.buffer().text(&start, &end, true);
+        text.split(',').map(|s| s.to_string()).collect()
+    }
+
+    /// Set tht tokens.
+    pub fn set_tokens(&self, tokens: &[String]) {
+        let text = tokens.join(",");
+        self.buffer().set_text(&text);
+    }
+}
+
+impl Default for TokenTextView {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+mod imp {
+    use gtk4::subclass::prelude::*;
+
+    pub struct TokenTextView {}
+
+    impl ObjectImpl for TokenTextView {
+        fn constructed(&self, obj: &Self::Type) {
+            self.parent_constructed(obj);
+        }
+    }
+
+    #[glib::object_subclass]
+    impl ObjectSubclass for TokenTextView {
+        const NAME: &'static str = "NpcTokenTextView";
+        type Type = super::TokenTextView;
+        type ParentType = gtk4::TextView;
+
+        fn new() -> Self {
+            Self {}
+        }
+    }
+
+    impl TextViewImpl for TokenTextView {}
+    impl WidgetImpl for TokenTextView {}
+}
diff --git a/src/Makefile.am b/src/Makefile.am
index eb0152b4..cbfb8686 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -45,6 +45,7 @@ RUST_SOURCES = \
        @top_srcdir@/crates/npc-fwk/src/toolkit/thumbnail.rs \
        @top_srcdir@/crates/npc-fwk/src/toolkit/widgets.rs \
        @top_srcdir@/crates/npc-fwk/src/toolkit/widgets/rating_label.rs \
+       @top_srcdir@/crates/npc-fwk/src/toolkit/widgets/token_text_view.rs \
        @top_srcdir@/crates/npc-fwk/src/toolkit/widgets/toolbox_item.rs \
        @top_srcdir@/crates/npc-fwk/src/utils.rs \
        @top_srcdir@/crates/npc-fwk/src/utils/exempi.rs \


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