diff --git a/src/components/plugin/function.rs b/src/components/plugin/function.rs index 556edaa..bdd0b91 100644 --- a/src/components/plugin/function.rs +++ b/src/components/plugin/function.rs @@ -13,11 +13,49 @@ extern "C" { pub fn run_test(); } +type RegularClickEvent = fn(Arc, FlowBox, Rc>); +type PluginClickEvent = Rc>, Vec)>; + +pub trait TSideBarInfo { + fn name(&self) -> &'static str; + fn icon_name(&self) -> &'static str; + fn parent(&self) -> Option<&'static str>; + fn regular_click_event(&self) -> Option; + fn plugin_click_event(&self) -> PluginClickEvent; + fn plugin_boxes(&self) -> Option>; +} + pub struct ReSetSidebarInfo { pub name: &'static str, pub icon_name: &'static str, pub parent: Option<&'static str>, - pub click_event: fn(Arc, FlowBox, Rc>), + pub click_event: RegularClickEvent, +} + +impl TSideBarInfo for ReSetSidebarInfo { + fn name(&self) -> &'static str { + self.name + } + + fn icon_name(&self) -> &'static str { + self.icon_name + } + + fn parent(&self) -> Option<&'static str> { + self.parent + } + + fn regular_click_event(&self) -> Option { + Some(self.click_event) + } + + fn plugin_click_event(&self) -> PluginClickEvent { + Rc::new(|_,_,_| {}) + } + + fn plugin_boxes(&self) -> Option> { + None + } } #[repr(C)] @@ -25,6 +63,34 @@ pub struct PluginSidebarInfo { pub name: &'static str, pub icon_name: &'static str, pub parent: Option<&'static str>, - pub click_event: Rc>, Vec)>, + pub click_event: PluginClickEvent, pub plugin_boxes: Vec, -} \ No newline at end of file +} + + +impl TSideBarInfo for PluginSidebarInfo { + fn name(&self) -> &'static str { + self.name + } + + fn icon_name(&self) -> &'static str { + self.icon_name + } + + fn parent(&self) -> Option<&'static str> { + self.parent + } + + fn regular_click_event(&self) -> Option { + None + } + + fn plugin_click_event(&self) -> PluginClickEvent { + self.click_event.clone() + } + + fn plugin_boxes(&self) -> Option> { + Some(self.plugin_boxes.clone()) + } +} + diff --git a/src/components/window/reset_window.rs b/src/components/window/reset_window.rs index 2980014..fff7064 100644 --- a/src/components/window/reset_window.rs +++ b/src/components/window/reset_window.rs @@ -208,7 +208,7 @@ impl ReSetWindow { self_imp.reset_sidebar_list.insert(&create_separator(), i); i += 1; } - let entry = SidebarEntry::new_plugin(&info); + let entry = SidebarEntry::new(&info); self_imp.reset_sidebar_list.insert(&entry, i); i += 1; } diff --git a/src/components/window/sidebar_entry.rs b/src/components/window/sidebar_entry.rs index 1410efb..d47b9b7 100644 --- a/src/components/window/sidebar_entry.rs +++ b/src/components/window/sidebar_entry.rs @@ -1,11 +1,11 @@ use std::rc::Rc; +use crate::components::plugin::function::{PluginSidebarInfo, ReSetSidebarInfo, TSideBarInfo}; use crate::components::window::sidebar_entry_impl; -use crate::components::window::sidebar_entry_impl::{SidebarAction}; +use crate::components::window::sidebar_entry_impl::SidebarAction; use adw::subclass::prelude::ObjectSubclassIsExt; use glib::Object; use gtk::prelude::*; -use crate::components::plugin::function::{PluginSidebarInfo, ReSetSidebarInfo}; use super::handle_sidebar_click::HANDLE_HOME; @@ -16,16 +16,18 @@ glib::wrapper! { } impl SidebarEntry { - // TODO: refactor new and new_plugin - pub fn new(info: &ReSetSidebarInfo) -> Self { + pub fn new(info: &T) -> Self { let entry: SidebarEntry = Object::builder().build(); let entry_imp = entry.imp(); - entry_imp.reset_sidebar_label.get().set_text(info.name); + entry_imp.reset_sidebar_label.get().set_text(info.name()); entry_imp .reset_sidebar_image - .set_from_icon_name(Some(info.icon_name)); - - match &info.parent { + .set_from_icon_name(Some(info.icon_name())); + if let Some(boxes) = info.plugin_boxes() { + entry_imp.plugin_boxes.borrow_mut().extend(boxes); + } + + match &info.parent() { None => {} Some(parent) => { let mut name = entry_imp.parent.borrow_mut(); @@ -33,44 +35,14 @@ impl SidebarEntry { entry.child().unwrap().set_margin_start(30); } } - + { let mut name = entry_imp.name.borrow_mut(); - *name = info.name.to_string(); + *name = info.name().to_string(); let mut action = entry_imp.on_click_event.borrow_mut(); *action = SidebarAction { - on_click_event: Some(info.click_event), - on_plugin_click_event: Rc::new(|_,_,_|{}), - }; - } - entry - } - - pub fn new_plugin(info: &PluginSidebarInfo) -> Self { - let entry: SidebarEntry = Object::builder().build(); - let entry_imp = entry.imp(); - entry_imp.reset_sidebar_label.get().set_text(info.name); - entry_imp - .reset_sidebar_image - .set_from_icon_name(Some(info.icon_name)); - entry_imp.plugin_boxes.borrow_mut().extend(info.plugin_boxes.clone()); - - match &info.parent { - None => {} - Some(parent) => { - let mut name = entry_imp.parent.borrow_mut(); - *name = parent.to_string(); - entry.child().unwrap().set_margin_start(30); - } - } - - { - let mut name = entry_imp.name.borrow_mut(); - *name = info.name.to_string(); - let mut action = entry_imp.on_click_event.borrow_mut(); - *action = SidebarAction { - on_click_event: None, - on_plugin_click_event: info.click_event.clone(), + on_click_event: info.regular_click_event(), + on_plugin_click_event: info.plugin_click_event(), }; } entry