Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k committed Aug 11, 2024
1 parent 20c0a3a commit f72c7a9
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 276 deletions.
113 changes: 55 additions & 58 deletions src/main_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::cache::Cache;
use crate::config::ConfigService;
use crate::format;
use crate::model::record::{Record, RecordType, RECORD_TYPE_GENERIC};
use crate::model::tree::RecordNode;
use crate::model::tree::RecordTree;
use crate::model::tree::{RecordNode, RecordTree};
use crate::search::item::SearchMatch;
use crate::ui;
use crate::ui::dashboard::PSDashboard;
Expand All @@ -14,7 +13,7 @@ use crate::ui::dialogs::say::say;
use crate::ui::edit_record::dialog::edit_record;
use crate::ui::forms::entry::form_password_entry;
use crate::ui::open_file::OpenFile;
use crate::ui::search_bar::SearchEvent;
use crate::ui::search_bar::{SearchEvent, SearchEventType};
use crate::utils::typed_list_store::TypedListStore;
use crate::utils::ui::*;
use gtk::{gio, glib, prelude::*, subclass::prelude::*};
Expand Down Expand Up @@ -311,68 +310,66 @@ impl PSMainWindow {
return;
}

fn traverse(
records: &TypedListStore<RecordNode>,
path: TypedListStore<RecordNode>,
query: &str,
search_in_secrets: bool,
result: &mut TypedListStore<SearchMatch>,
) {
for record in records {
if record.record().has_text(query, search_in_secrets) {
result.append(&SearchMatch::new(&record, &path.clone_list()));
}
if let Some(children) = record.children() {
let path_to_record = path.appended(record.clone());
traverse(children, path_to_record, query, search_in_secrets, result);
}
}
}
// fn traverse(
// records: &TypedListStore<RecordNode>,
// path: TypedListStore<RecordNode>,
// query: &str,
// search_in_secrets: bool,
// result: &mut TypedListStore<SearchMatch>,
// ) {
// for record in records {
// if record.record().has_text(query, search_in_secrets) {
// result.append(&SearchMatch::new(&record, &path.clone_list()));
// }
// if let Some(children) = record.children() {
// let path_to_record = path.appended(record.clone());
// traverse(children, path_to_record, query, search_in_secrets, result);
// }
// }
// }

let mut matching_records = Default::default();
traverse(
&self.imp().file_pane.file().records, // current records ????
Default::default(),
&event.query,
event.search_in_secrets,
&mut matching_records,
);
// let mut matching_records = Default::default();
// traverse(
// &self.imp().file_pane.file().records, // current records ????
// Default::default(),
// &event.query,
// event.search_in_secrets,
// &mut matching_records,
// );

// self.imp().view.select_position_async(0).await; // TODO: throttle

// let private = self.private();
// let model = private.data.borrow().as_model();
// let iters = flatten_tree(&model);

// let mut search_iter: Box<dyn Iterator<Item = &gtk::TreeIter>> = match event.event_type {
// SearchEventType::Change | SearchEventType::Next => Box::new(iters.iter()),
// SearchEventType::Prev => Box::new(iters.iter().rev()),
// };
// if let Some((_selection_record, selection_iter)) = private.view.get_selected_record() {
// search_iter = Box::new(
// search_iter.skip_while(move |iter| model.path(iter) != model.path(&selection_iter)),
// );
// }
// match event.event_type {
// SearchEventType::Change => {}
// SearchEventType::Next | SearchEventType::Prev => {
// search_iter = Box::new(search_iter.skip(1))
// }
// };
let model = self.imp().file.borrow();

// let next_match = search_iter
// .map(|iter| (iter, private.data.borrow().get(iter)))
// .find(|(_iter, record)| record.has_text(&event.query, event.search_in_secrets));
let mut search_iter: Box<dyn Iterator<Item = &gtk::TreeIter>> = match event.event_type {
SearchEventType::Change | SearchEventType::Next => Box::new(iters.iter()),
SearchEventType::Prev => Box::new(iters.iter().rev()),
};
if let Some((_selection_record, selection_iter)) = private.view.get_selected_record() {
search_iter = Box::new(
search_iter.skip_while(move |iter| model.path(iter) != model.path(&selection_iter)),
);
}
match event.event_type {
SearchEventType::Change => {}
SearchEventType::Next | SearchEventType::Prev => {
search_iter = Box::new(search_iter.skip(1))
}
};

// if let Some(next_match) = next_match {
// private.view.select_iter(next_match.0);
// self.listview_cursor_changed(&[next_match.1]);
// } else {
// self.error_bell();
// }
let next_match = search_iter
.map(|iter| (iter, private.data.borrow().get(iter)))
.find(|(_iter, record)| record.has_text(&event.query, event.search_in_secrets));

if let Some(next_match) = next_match {
self.imp().view.select_iter(next_match.0);
self.listview_cursor_changed(&[next_match.1]);
} else {
self.error_bell();
}

self.imp().set_mode(imp::AppMode::SearchResults);
self.imp().search_pane.set_model(matching_records).await;
// self.imp().set_mode(imp::AppMode::SearchResults);
// self.imp().search_pane.set_model(matching_records).await;
}

fn get_usernames(&self) -> Vec<String> {
Expand Down
34 changes: 34 additions & 0 deletions src/model/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,37 @@ 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);
}

pub fn depth_first_iter(&self, forward: bool) -> impl Iterator<Item = RecordNode> {
RecordTreeIter {}
}
}

pub struct RecordTreeIter {
records: TypedListStore<RecordNode>,
stack: Vec<TypedListStore<RecordNode>>,
position: u32,
}

impl Iterator for RecordTreeIter {
type Item = RecordNode;

fn next(&mut self) -> Option<Self::Item> {
None
}
}
Loading

0 comments on commit f72c7a9

Please sign in to comment.