Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k committed Jun 2, 2024
1 parent 1ea64af commit 37787fa
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 93 deletions.
176 changes: 90 additions & 86 deletions src/ui/file_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ mod imp {
pub nav_bar: PSNavBar,
pub view: PSRecordView,
pub file: RefCell<RecordTree>,
pub current_path: TypedListStore<RecordNode>,
pub current_records: RefCell<TypedListStore<RecordNode>>,
// pub current_path: TypedListStore<RecordNode>,
// pub current_records: RefCell<TypedListStore<RecordNode>>,
}

#[glib::object_subclass]
Expand Down Expand Up @@ -75,8 +75,8 @@ mod imp {
drag_and_drop: true,
}),
file: Default::default(),
current_path: Default::default(),
current_records: Default::default(),
// current_path: Default::default(),
// current_records: Default::default(),
}
}
}
Expand Down Expand Up @@ -173,10 +173,10 @@ mod imp {
}),
);

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

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

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

impl FilePane {
pub fn set_view_model(&self, model: &TypedListStore<RecordNode>) {
*self.current_records.borrow_mut() = model.clone();
self.view.set_model(model.untyped().upcast_ref());
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())
});
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;
// 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;
}
// 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;
}
// 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);
}
// 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);
// }
}

fn move_record(&self, src: &RecordNode, dst: &RecordNode, option: DropOption) {
let Some(src_pos) = self.current_records.borrow().iter().position(|r| r == *src) else {
return;
};
self.current_records.borrow().remove(src_pos as u32);

let dst_pos = self.current_records.borrow().iter().position(|r| r == *dst);
match option {
DropOption::Above => {
self.current_records
.borrow()
.insert(dst_pos.unwrap_or(0), src);
}
DropOption::Below => {
self.current_records
.borrow()
.insert(dst_pos.map(|p| p + 1), src);
}
DropOption::Into => {
self.obj().append_records_to(Some(dst), &[src.clone()]);
}
}
self.obj().emit_file_changed();
// let Some(src_pos) = self.current_records.borrow().iter().position(|r| r == *src) else {
// return;
// };
// self.current_records.borrow().remove(src_pos as u32);

// let dst_pos = self.current_records.borrow().iter().position(|r| r == *dst);
// match option {
// DropOption::Above => {
// self.current_records
// .borrow()
// .insert(dst_pos.unwrap_or(0), src);
// }
// DropOption::Below => {
// self.current_records
// .borrow()
// .insert(dst_pos.map(|p| p + 1), src);
// }
// DropOption::Into => {
// self.obj().append_records_to(Some(dst), &[src.clone()]);
// }
// }
// self.obj().emit_file_changed();
}
}
}
Expand All @@ -320,8 +324,7 @@ impl FilePane {

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

self.imp().view.select_position_async(0).await;
self.selection_changed(gtk::Bitset::new_empty());
Expand All @@ -333,35 +336,36 @@ impl FilePane {
// }

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

pub fn append_record(&self, record_node: &RecordNode) {
self.imp().current_records.borrow().append(record_node);
let position = self.imp().current_records.borrow().len() - 1;
self.imp().view.select_position(position);
self.selection_changed(gtk::Bitset::new_range(position, 1));
// self.imp().current_records.borrow().append(record_node);
// let position = self.imp().current_records.borrow().len() - 1;
// self.imp().view.select_position(position);
// self.selection_changed(gtk::Bitset::new_range(position, 1));
}

pub fn replace_record(&self, position: u32, record_node: RecordNode) {
self.imp()
.current_records
.borrow()
.set(position, record_node);
self.imp().view.select_position(position);
self.selection_changed(gtk::Bitset::new_range(position, 1));
// self.imp()
// .current_records
// .borrow()
// .set(position, record_node);
// self.imp().view.select_position(position);
// self.selection_changed(gtk::Bitset::new_range(position, 1));
}

fn remove_record(&self, position: u32) {
self.imp().current_records.borrow().remove(position);
self.selection_changed(gtk::Bitset::new_empty());
// self.imp().current_records.borrow().remove(position);
// self.selection_changed(gtk::Bitset::new_empty());
}

fn remove_records(&self, mut positions: Vec<u32>) {
positions.sort();
for position in positions.into_iter().rev() {
self.imp().current_records.borrow().remove(position);
}
// positions.sort();
// for position in positions.into_iter().rev() {
// self.imp().current_records.borrow().remove(position);
// }
}

fn append_records_to(&self, parent: Option<&RecordNode>, records: &[RecordNode]) {
Expand Down
28 changes: 22 additions & 6 deletions src/ui/record_view/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,32 @@ mod imp {
let Some(list_item) = item.downcast_ref::<gtk::ListItem>() else {
return;
};

let child = PSRecordViewItem::new(*self.options.get().unwrap());
let expander = gtk::TreeExpander::builder().child(&child).build();

let popup_model = self.popup_model.clone();
child.connect_context_menu(move |_record| popup_model.borrow().clone());
let obj = self.obj();
child.connect_move_record(glib::clone!(@weak obj => move |_, src, dst, opt| obj.emit_move_record(src, dst, opt)));
list_item.set_child(Some(&child));
list_item.set_child(Some(&expander));
}

fn bind_item(&self, item: &glib::Object) {
let Some(list_item) = item.downcast_ref::<gtk::ListItem>() else {
return;
};
let Some(child) = list_item.child().and_downcast::<PSRecordViewItem>() else {
let Some(expander) = list_item.child().and_downcast::<gtk::TreeExpander>() else {
return;
};
let Some(child) = expander.child().and_downcast::<PSRecordViewItem>() else {
return;
};
let Some(tree_list_row) = list_item.item().and_downcast::<gtk::TreeListRow>() else {
return;
};
let Some(record_node) = list_item.item() else {
expander.set_list_row(Some(&tree_list_row));
let Some(record_node) = tree_list_row.item() else {
return;
};
let position = list_item.position();
Expand All @@ -161,7 +171,10 @@ mod imp {
let Some(list_item) = item.downcast_ref::<gtk::ListItem>() else {
return;
};
let Some(child) = list_item.child().and_downcast::<PSRecordViewItem>() else {
let Some(expander) = list_item.child().and_downcast::<gtk::TreeExpander>() else {
return;
};
let Some(child) = expander.child().and_downcast::<PSRecordViewItem>() else {
return;
};
self.mapping.remove_key(list_item.position());
Expand All @@ -173,7 +186,10 @@ mod imp {
let Some(list_item) = item.downcast_ref::<gtk::ListItem>() else {
return;
};
if let Some(child) = list_item.child().and_downcast::<PSRecordViewItem>() {
let Some(expander) = list_item.child().and_downcast::<gtk::TreeExpander>() else {
return;
};
if let Some(child) = expander.child().and_downcast::<PSRecordViewItem>() {
child.set_record_node(None);
self.mapping.remove_value(&child);
}
Expand All @@ -194,7 +210,7 @@ impl PSRecordView {
obj
}

pub fn set_model(&self, model: &gio::ListModel) {
pub fn set_model(&self, model: &gtk::TreeListModel) {
self.imp().selection.set_model(Some(model));
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/search_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ mod imp {
impl SearchPane {
pub fn set_view_model(&self, model: &TypedListStore<SearchMatch>) {
*self.current_records.borrow_mut() = model.clone();
self.view.set_model(model.untyped().upcast_ref());
// self.view.set_model(model.untyped().upcast_ref());
}

async fn row_activated(&self, position: u32) {
Expand Down

0 comments on commit 37787fa

Please sign in to comment.