Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k committed Jun 9, 2024
1 parent 37787fa commit 367dc26
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 178 deletions.
16 changes: 16 additions & 0 deletions src/model/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,19 @@ impl RecordNode {
pub struct RecordTree {
pub records: TypedListStore<RecordNode>,
}

impl RecordTree {
pub fn remove(&self, record_node: &RecordNode) {
fn traverse(list: &TypedListStore<RecordNode>, record_node: &RecordNode) {
if let Some(position) = list.find(record_node) {
list.remove(position);
}
for item in list {
if let Some(children) = item.children() {
traverse(children, record_node);
}
}
}
traverse(&self.records, record_node);
}
}
155 changes: 41 additions & 114 deletions src/ui/file_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ mod imp {
use crate::ui::record_type_popover::RecordTypePopoverBuilder;
use crate::ui::record_view::has_record::{PSRecordViewOptions, RECORD_NODE_HAS_RECORD};
use crate::ui::record_view::item::DropOption;
use crate::utils::typed_list_store::TypedListStore;
use crate::utils::ui::{action_button, action_popover_button, orphan_all_children};
use std::cell::RefCell;
use std::sync::OnceLock;
Expand All @@ -36,8 +35,6 @@ mod imp {
pub nav_bar: PSNavBar,
pub view: PSRecordView,
pub file: RefCell<RecordTree>,
// pub current_path: TypedListStore<RecordNode>,
// pub current_records: RefCell<TypedListStore<RecordNode>>,
}

#[glib::object_subclass]
Expand Down Expand Up @@ -75,8 +72,6 @@ mod imp {
drag_and_drop: true,
}),
file: Default::default(),
// current_path: Default::default(),
// current_records: Default::default(),
}
}
}
Expand Down Expand Up @@ -130,53 +125,21 @@ mod imp {
}),
);

self.view
.connect_record_activated(glib::clone!(@weak self as imp => move |position| {
self.view.connect_record_activated(
glib::clone!(@weak self as imp => move |position, record_node| {
glib::MainContext::default().spawn_local(async move {
imp.row_activated(position).await;
imp.row_activated(position, record_node).await;
});
}));
}),
);

self.nav_bar
.connect_go_home(glib::clone!(@weak self as imp => move || {
glib::MainContext::default().spawn_local(async move {
imp.go_home().await
});
}));
self.nav_bar
.connect_go_path(glib::clone!(@weak self as imp => move |position| {
glib::MainContext::default().spawn_local(async move {
imp.go_path(position).await
});
}));
self.nav_bar
.connect_go_up(glib::clone!(@weak self as imp => move || {
glib::MainContext::default().spawn_local(async move {
imp.go_up().await
});
}));
self.view
.connect_go_home(glib::clone!(@weak self as imp => move || {
glib::MainContext::default().spawn_local(async move {
imp.go_home().await
});
}));
self.view
.connect_go_up(glib::clone!(@weak self as imp => move || {
glib::MainContext::default().spawn_local(async move {
imp.go_up().await
});
}));
self.view.connect_move_record(
glib::clone!(@weak self as imp => move |_, src, dst, opt| {
imp.move_record(src.downcast_ref().unwrap(), dst.downcast_ref().unwrap(), opt);
}),
);

// self.set_view_model(&self.file.borrow());

// self.nav_bar
// .set_model(self.current_path.untyped().upcast_ref());
self.update_view_model();

let shortcuts = gtk::ShortcutController::new();
shortcuts.add_shortcut(
Expand Down Expand Up @@ -219,60 +182,22 @@ mod imp {
impl WidgetImpl for FilePane {}

impl FilePane {
pub fn set_view_model(&self, model: RecordTree) {
let tree_model = gtk::TreeListModel::new(model.records.clone().untyped().clone(), false, false, |node| {
let r = node.downcast_ref::<RecordNode>()?;
let children = r.children()?;
Some(children.untyped().clone().upcast())
});
pub fn update_view_model(&self) {
let tree_model = gtk::TreeListModel::new(
self.file.borrow().records.untyped().clone(),
false,
false,
|node| {
let r = node.downcast_ref::<RecordNode>()?;
let children = r.children()?;
Some(children.untyped().clone().upcast())
},
);
self.view.set_model(&tree_model);
}

async fn go_home(&self) {
// self.current_path.remove_all();
// self.set_view_model(&self.file.borrow().records);
// self.view.select_position_async(0).await;
}

async fn go_path(&self, position: u32) {
// if position + 1 >= self.current_path.len() {
// return;
// }
// let prev = self.current_path.get(position + 1);
// self.current_path.truncate(position + 1);
// let records = match self.current_path.last() {
// Some(parent) => parent.children().unwrap().clone(),
// None => self.file.borrow().records.clone(),
// };
// self.set_view_model(&records);
// if let Some(prev) = prev {
// self.view.select_object(prev.upcast_ref()).await;
// }
}

async fn go_up(&self) {
// let prev = self.current_path.pop_back();
// let records = match self.current_path.last() {
// Some(parent) => parent.children().unwrap().clone(),
// None => self.file.borrow().records.clone(),
// };
// self.set_view_model(&records);
// if let Some(prev) = prev {
// self.view.select_object(prev.upcast_ref()).await;
// }
}

async fn row_activated(&self, position: u32) {
// let Some(record) = self.current_records.borrow().get(position) else {
// return;
// };
// if let Some(children) = record.children() {
// self.current_path.append(&record);
// self.set_view_model(children);
// self.view.select_position_async(0).await;
// } else {
// self.obj().emit_edit_record(position, &record);
// }
async fn row_activated(&self, position: u32, record: RecordNode) {
self.obj().emit_edit_record(position, &record);
}

fn move_record(&self, src: &RecordNode, dst: &RecordNode, option: DropOption) {
Expand Down Expand Up @@ -324,20 +249,19 @@ impl FilePane {

pub async fn set_file(&self, file: RecordTree) {
*self.imp().file.borrow_mut() = file;
self.imp().set_view_model(self.imp().file.borrow().clone());
self.imp().update_view_model();

self.imp().view.select_position_async(0).await;
self.selection_changed(gtk::Bitset::new_empty());
self.grab_focus_to_view();
}

// pub async fn reset(&self) {
// self.set_model(Default::default()).await;
// }
pub async fn reset(&self) {
self.set_file(Default::default()).await;
}

fn get_record(&self, position: u32) -> Option<RecordNode> {
// self.imp().current_records.borrow().get(position)
None
fn record_at(&self, position: u32) -> Option<RecordNode> {
self.imp().view.record_at(position)
}

pub fn append_record(&self, record_node: &RecordNode) {
Expand All @@ -357,15 +281,18 @@ impl FilePane {
}

fn remove_record(&self, position: u32) {
// self.imp().current_records.borrow().remove(position);
// self.selection_changed(gtk::Bitset::new_empty());
if let Some(record) = self.record_at(position) {
self.imp().file.borrow().remove(&record);
self.selection_changed(gtk::Bitset::new_range(position, 1));
}
}

fn remove_records(&self, mut positions: Vec<u32>) {
// positions.sort();
// for position in positions.into_iter().rev() {
// self.imp().current_records.borrow().remove(position);
// }
fn remove_records(&self, positions: Vec<u32>) {
let record_nodes = positions.into_iter().flat_map(|p| self.record_at(p)).collect::<Vec<_>>();
for record_node in record_nodes {
self.imp().file.borrow().remove(&record_node);
}
self.selection_changed(gtk::Bitset::new_empty());
}

fn append_records_to(&self, parent: Option<&RecordNode>, records: &[RecordNode]) {
Expand All @@ -389,7 +316,7 @@ impl FilePane {

fn selection_changed(&self, selected: gtk::Bitset) {
let selected_record = if selected.size() == 1 {
self.get_record(selected.nth(0))
self.imp().view.record_at(selected.nth(0))
} else {
None
};
Expand All @@ -412,7 +339,7 @@ impl FilePane {
let Some(position) = self.imp().view.get_selected_position() else {
return;
};
let Some(record_node) = self.get_record(position) else {
let Some(record_node) = self.record_at(position) else {
return;
};
let Some(username) = record_node.record().username() else {
Expand All @@ -426,7 +353,7 @@ impl FilePane {
let Some(position) = self.imp().view.get_selected_position() else {
return;
};
let Some(record_node) = self.get_record(position) else {
let Some(record_node) = self.record_at(position) else {
return;
};
let Some(password) = record_node.record().password() else {
Expand All @@ -452,7 +379,7 @@ impl FilePane {
let (positions, records): (Vec<u32>, Vec<RecordNode>) = positions
.iter_asc()
.filter_map(|position| {
let record = self.get_record(position)?;
let record = self.record_at(position)?;
if dest.iter().any(|p| p == record) {
None
} else {
Expand All @@ -470,7 +397,7 @@ impl FilePane {
let Some(position) = self.imp().view.get_selected_position() else {
return;
};
let Some(record_node) = self.get_record(position) else {
let Some(record_node) = self.record_at(position) else {
return;
};
self.emit_edit_record(position, &record_node);
Expand Down Expand Up @@ -505,7 +432,7 @@ impl FilePane {
let (positions, records): (Vec<u32>, Vec<RecordNode>) = positions
.iter_asc()
.filter_map(|position| {
let record = self.get_record(position)?;
let record = self.record_at(position)?;
if record.is_group() {
None
} else {
Expand Down
Loading

0 comments on commit 367dc26

Please sign in to comment.