refactor: Combine new_plugin and new for sidebar entries

This commit is contained in:
Fabio Lenherr / DashieTM 2024-04-01 13:39:55 +02:00
parent 12c937ae39
commit 3d2f38ee46
3 changed files with 84 additions and 46 deletions

View file

@ -13,11 +13,49 @@ extern "C" {
pub fn run_test();
}
type RegularClickEvent = fn(Arc<Listeners>, FlowBox, Rc<RefCell<Position>>);
type PluginClickEvent = Rc<dyn Fn(FlowBox, Rc<RefCell<Position>>, Vec<gtk::Box>)>;
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<RegularClickEvent>;
fn plugin_click_event(&self) -> PluginClickEvent;
fn plugin_boxes(&self) -> Option<Vec<gtk::Box>>;
}
pub struct ReSetSidebarInfo {
pub name: &'static str,
pub icon_name: &'static str,
pub parent: Option<&'static str>,
pub click_event: fn(Arc<Listeners>, FlowBox, Rc<RefCell<Position>>),
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<RegularClickEvent> {
Some(self.click_event)
}
fn plugin_click_event(&self) -> PluginClickEvent {
Rc::new(|_,_,_| {})
}
fn plugin_boxes(&self) -> Option<Vec<gtk::Box>> {
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<dyn Fn(FlowBox, Rc<RefCell<Position>>, Vec<gtk::Box>)>,
pub click_event: PluginClickEvent,
pub plugin_boxes: Vec<gtk::Box>,
}
}
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<RegularClickEvent> {
None
}
fn plugin_click_event(&self) -> PluginClickEvent {
self.click_event.clone()
}
fn plugin_boxes(&self) -> Option<Vec<gtk::Box>> {
Some(self.plugin_boxes.clone())
}
}

View file

@ -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;
}

View file

@ -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<T: TSideBarInfo>(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