[niepce] rust: Rewrite toolbox-item widget in Rust



commit 8275e79122b347da0d6a138039e4d051059bcfa3
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Oct 15 22:53:48 2022 -0400

    rust: Rewrite toolbox-item widget in Rust

 crates/npc-fwk/src/toolkit/widgets.rs              | 11 ++-
 crates/npc-fwk/src/toolkit/widgets/toolbox_item.rs | 97 ++++++++++++++++++++++
 niepce-main/examples/widget-test.rs                |  5 +-
 src/Makefile.am                                    |  1 +
 4 files changed, 112 insertions(+), 2 deletions(-)
---
diff --git a/crates/npc-fwk/src/toolkit/widgets.rs b/crates/npc-fwk/src/toolkit/widgets.rs
index 737ba538..1f374779 100644
--- a/crates/npc-fwk/src/toolkit/widgets.rs
+++ b/crates/npc-fwk/src/toolkit/widgets.rs
@@ -1,7 +1,7 @@
 /*
  * niepce - crates/npc-fwk/src/toolkit/widgets/mod.rs
  *
- * Copyright (C) 2020 Hubert Figuière
+ * Copyright (C) 2020-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
@@ -18,3 +18,12 @@
  */
 
 pub mod rating_label;
+mod toolbox_item;
+
+// Re-exports
+pub use toolbox_item::ToolboxItem;
+
+pub mod prelude {
+    pub use super::toolbox_item::ToolboxItemExt;
+    pub use super::toolbox_item::ToolboxItemImpl;
+}
diff --git a/crates/npc-fwk/src/toolkit/widgets/toolbox_item.rs 
b/crates/npc-fwk/src/toolkit/widgets/toolbox_item.rs
new file mode 100644
index 00000000..21ddec50
--- /dev/null
+++ b/crates/npc-fwk/src/toolkit/widgets/toolbox_item.rs
@@ -0,0 +1,97 @@
+/*
+ * niepce - fwk/toolkit/widgets/toolbox_item.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 glib::subclass::prelude::*;
+use gtk4::prelude::*;
+use gtk4::subclass::prelude::*;
+
+glib::wrapper! {
+    /// A `ToolboxItem` is just a box with a `gtk4::Expander`
+    /// The content is the child, which is actually the expander child
+    /// The label is the `gtk4::Expander` label.
+    pub struct ToolboxItem(
+        ObjectSubclass<imp::ToolboxItem>)
+        @extends gtk4::Box, gtk4::Widget;
+}
+
+impl ToolboxItem {
+    pub fn new(label: &str) -> ToolboxItem {
+        let obj: ToolboxItem = glib::Object::new(&[
+            ("spacing", &0),
+            ("orientation", &gtk4::Orientation::Vertical),
+        ])
+        .expect("Failed to create ToolboxItem Widget");
+        obj.imp().expander.set_label(Some(label));
+        obj
+    }
+}
+
+pub trait ToolboxItemExt {
+    /// Set the child (of the `gtk4::Expander`).
+    fn set_child(&self, child: Option<&impl IsA<gtk4::Widget>>);
+}
+
+impl ToolboxItemExt for ToolboxItem {
+    fn set_child(&self, child: Option<&impl IsA<gtk4::Widget>>) {
+        self.imp().expander.set_child(child);
+        if let Some(child) = child {
+            child.set_vexpand(true);
+        }
+    }
+}
+
+pub trait ToolboxItemImpl: BoxImpl {}
+
+unsafe impl<T: ToolboxItemImpl> IsSubclassable<T> for ToolboxItem {}
+
+mod imp {
+    use glib::subclass::prelude::*;
+    use gtk4::prelude::*;
+    use gtk4::subclass::prelude::*;
+
+    pub struct ToolboxItem {
+        pub(super) expander: gtk4::Expander,
+    }
+
+    impl ObjectImpl for ToolboxItem {
+        fn constructed(&self, obj: &Self::Type) {
+            self.parent_constructed(obj);
+
+            obj.append(&self.expander);
+            self.expander.set_expanded(true);
+            self.expander.set_use_markup(true);
+        }
+    }
+
+    #[glib::object_subclass]
+    impl ObjectSubclass for ToolboxItem {
+        const NAME: &'static str = "NpcToolboxItem";
+        type Type = super::ToolboxItem;
+        type ParentType = gtk4::Box;
+
+        fn new() -> Self {
+            Self {
+                expander: gtk4::Expander::new(None),
+            }
+        }
+    }
+
+    impl BoxImpl for ToolboxItem {}
+    impl WidgetImpl for ToolboxItem {}
+}
diff --git a/niepce-main/examples/widget-test.rs b/niepce-main/examples/widget-test.rs
index 59b087eb..baae7a38 100644
--- a/niepce-main/examples/widget-test.rs
+++ b/niepce-main/examples/widget-test.rs
@@ -26,6 +26,7 @@ use gtk4::prelude::*;
 use niepce_rust::niepce::ui::image_grid_view::ImageGridView;
 use niepce_rust::niepce::ui::thumb_nav::{ThumbNav, ThumbNavMode};
 use niepce_rust::niepce::ui::thumb_strip_view::ThumbStripView;
+use npc_fwk::toolkit::widgets::prelude::*;
 use npc_fwk::toolkit::widgets::rating_label::RatingLabel;
 
 fn init() -> Result<(), Error> {
@@ -77,7 +78,9 @@ pub fn main() {
         (&image_grid).set_hexpand(true);
         (&image_grid).set_vexpand(true);
         box_.append(&rating);
-        box_.append(image_grid.deref());
+        let tb_item = npc_fwk::toolkit::widgets::ToolboxItem::new("Grid View");
+        tb_item.set_child(Some(image_grid.deref()));
+        box_.append(&tb_item);
         box_.append(&thn);
 
         let window = gtk4::Window::new();
diff --git a/src/Makefile.am b/src/Makefile.am
index e267ef32..eb0152b4 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/toolbox_item.rs \
        @top_srcdir@/crates/npc-fwk/src/utils.rs \
        @top_srcdir@/crates/npc-fwk/src/utils/exempi.rs \
        @top_srcdir@/crates/npc-fwk/src/utils/exiv2.rs \


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