Skip to content

Commit

Permalink
Make it so StructureListModel doesn't subscribe to the document and n…
Browse files Browse the repository at this point in the history
…eeds an external update source so that we can make sure it stays in sync with the SelectionModel in a reasonable way
  • Loading branch information
misson20000 committed Sep 20, 2023
1 parent a32b1f9 commit 438bfc7
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 57 deletions.
2 changes: 0 additions & 2 deletions src/model/selection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
pub mod hierarchy;
pub mod listing;
pub mod tree;

pub use hierarchy::Selection as HierarchySelection;
pub use listing::Selection as ListingSelection;
pub use tree::Selection as TreeSelection;
144 changes: 99 additions & 45 deletions src/view/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,14 @@ use crate::model::document;
use crate::model::document::change;
use crate::model::document::structure;
use crate::model::versioned::Versioned;
use crate::view::helpers;

/// Our TreeListModel doesn't update itself. This is so that we can guarantee that the selection model updates first by having it be the one that subscribes to SelectionHost updates and informs us when the document updates. We still need a document_host though so we can change the document when properties are edited.
pub fn create_tree_list_model(document_host: sync::Arc<document::DocumentHost>, document: sync::Arc<document::Document>, autoexpand: bool) -> gtk::TreeListModel {
let root_model = gio::ListStore::new(NodeItem::static_type());

let root_item = NodeItem::new(NodeInfo {
path: vec![],
node: document.root.clone(),
props: document.root.props.clone(),
offset: addr::unit::NULL,
address: addr::unit::NULL,
document_host: document_host.clone(),
document: document.clone(),
});

root_model.append(&root_item);
let root_model = RootListModel::new(document_host, document);

let model = gtk::TreeListModel::new(root_model, false, autoexpand, |obj| {
Some(StructureListModel::from_node_info(
&obj.downcast_ref::<NodeItem>().unwrap().imp().info.get().unwrap().borrow()
).upcast())
});

let subscriber = helpers::subscribe_to_updates(root_item.downgrade(), document_host.clone(), document, move |root_item, new_document| {
root_item.stage(NodeInfo {
path: vec![],
node: new_document.root.clone(),
props: new_document.root.props.clone(),
offset: addr::unit::NULL,
address: addr::unit::NULL,
document: new_document.clone(),
document_host: document_host.clone(),
});
root_item.update();
Some(obj.downcast_ref::<NodeItem>().unwrap().imp().expand().upcast())
});

/* The root item lasts forever. */
/* TODO: no it doesn't; you can close and reopen the window. */
std::mem::forget(subscriber);

model
}
Expand Down Expand Up @@ -83,7 +52,6 @@ mod imp {
use crate::model::addr;
use crate::model::document;
use crate::model::document::structure;
use crate::view::helpers;

use super::NodeInfo;

Expand All @@ -94,7 +62,6 @@ mod imp {
pub address: addr::Address,
pub document_host: sync::Arc<document::DocumentHost>,
pub document: sync::Arc<document::Document>,
pub subscriber: helpers::AsyncSubscriber,
pub deleted: bool,
}

Expand Down Expand Up @@ -134,6 +101,7 @@ mod imp {
pub struct NodeItem {
pub info: once_cell::unsync::OnceCell<cell::RefCell<NodeInfo>>,
pub staged_info: cell::RefCell<Option<NodeInfo>>,
pub expansion: cell::RefCell<glib::object::WeakRef<super::StructureListModel>>
}

#[glib::object_subclass]
Expand Down Expand Up @@ -182,7 +150,7 @@ mod imp {
println!("failed to alter node: {:?}", e);
std::mem::drop(info);
self.obj().stage(old_info);
self.obj().update();
self.obj().trigger_notifies();
}
}

Expand All @@ -200,14 +168,63 @@ mod imp {
_ => unimplemented!(),
}
}
}
}

impl NodeItem {
pub fn expand(&self) -> super::StructureListModel {
let expansion = self.expansion.borrow_mut();
match expansion.upgrade() {
Some(slm) => slm,
None => {
let slm = super::StructureListModel::from_node_info(&*self.info.get().unwrap().borrow());
expansion.set(Some(&slm));
slm
}
}
}
}

#[derive(Default)]
pub struct RootListModel {
pub root_item: once_cell::unsync::OnceCell<super::NodeItem>,
}

#[glib::object_subclass]
impl ObjectSubclass for RootListModel {
const NAME: &'static str = "CharmRootListModel";
type Type = super::RootListModel;
type Interfaces = (gio::ListModel,);
}

impl ObjectImpl for RootListModel {
}

impl ListModelImpl for RootListModel {
fn item_type(&self) -> glib::Type {
super::NodeItem::static_type()
}

fn n_items(&self) -> u32 {
1
}

fn item(&self, position: u32) -> Option<glib::Object> {
assert_eq!(position, 0);
self.root_item.get().map(|ch| ch.clone().upcast())
}
}
}

glib::wrapper! {
pub struct StructureListModel(ObjectSubclass<imp::StructureListModel>)
@implements gio::ListModel;
}

glib::wrapper! {
pub struct RootListModel(ObjectSubclass<imp::RootListModel>)
@implements gio::ListModel;
}

glib::wrapper! {
pub struct NodeItem(ObjectSubclass<imp::NodeItem>)
;
Expand All @@ -217,10 +234,6 @@ impl StructureListModel {
fn from_node_info(info: &NodeInfo) -> Self {
let model: Self = glib::Object::builder().build();

let subscriber = helpers::subscribe_to_updates(model.downgrade(), info.document_host.clone(), info.document.clone(), move |model, new_document| {
model.update(new_document);
});

model.imp().interior.set(cell::RefCell::new(imp::StructureListModelInterior {
path: info.path.clone(),
children: info.node.children[..].iter().enumerate().map(|tuple| {
Expand All @@ -242,7 +255,6 @@ impl StructureListModel {
address: info.address,
document_host: info.document_host.clone(),
document: info.document.clone(),
subscriber,
deleted: false,
})).unwrap();

Expand Down Expand Up @@ -390,7 +402,8 @@ impl StructureListModel {
* updates before deciding whether to filter them out or not. */

for item in &self.imp().interior.get().unwrap().borrow().children {
item.update();
item.update_document(new_doc);
item.trigger_notifies();
}
}
}
Expand All @@ -410,8 +423,14 @@ impl NodeItem {
pub fn info(&self) -> cell::Ref<'_, NodeInfo> {
self.imp().info.get().unwrap().borrow()
}

fn update_document(&self, new_doc: &sync::Arc<document::Document>) {
if let Some(slm) = self.imp().expansion.borrow().upgrade() {
slm.update(new_doc);
}
}

fn update(&self) {
fn trigger_notifies(&self) {
if let Some(new_info) = self.imp().staged_info.borrow_mut().take() {
let mut info = self.imp().info.get().unwrap().borrow_mut();

Expand All @@ -432,3 +451,38 @@ impl NodeItem {
}
}
}

impl RootListModel {
fn new(document_host: sync::Arc<document::DocumentHost>, document: sync::Arc<document::Document>) -> Self {
let model: Self = glib::Object::builder().build();

model.imp().root_item.set(NodeItem::new(NodeInfo {
path: vec![],
node: document.root.clone(),
props: document.root.props.clone(),
offset: addr::unit::NULL,
address: addr::unit::NULL,
document: document.clone(),
document_host: document_host.clone(),
})).unwrap();

model
}

pub fn update_document(&self, new_document: &sync::Arc<document::Document>) {
let root_item = self.imp().root_item.get().unwrap();

root_item.stage(NodeInfo {
path: vec![],
node: new_document.root.clone(),
props: new_document.root.props.clone(),
offset: addr::unit::NULL,
address: addr::unit::NULL,
document: new_document.clone(),
document_host: root_item.info().document_host.clone(),
});

root_item.update_document(new_document);
root_item.trigger_notifies();
}
}
31 changes: 21 additions & 10 deletions src/view/selection/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod imp {

fn change(&self, interior: std::cell::RefMut<'_, TreeSelectionModelInterior>, change: tree_model::Change) {
if let Ok(new_selection) = interior.selection_host.change(change) {
self.obj().update(interior, new_selection)
self.obj().update_selection(interior, new_selection)
} else {
// TODO: these shouldn't really fail... log this somewhere?
}
Expand Down Expand Up @@ -181,11 +181,13 @@ impl TreeSelectionModel {
let model: TreeSelectionModel = glib::Object::builder().build();

let gtk_model = hierarchy::create_tree_list_model(document_host.clone(), selection.document.clone(), true);
gtk_model.connect_items_changed(clone!(@weak model => move |_, pos, removed, added| model.items_changed(pos, removed, added)));
gtk_model.connect_items_changed(clone!(@weak model => move |_, pos, removed, added| {
model.items_changed(pos, removed, added)
}));

let selection_subscriber = helpers::subscribe_to_updates(model.downgrade(), selection_host.clone(), selection.clone(), move |model, new_selection| {
if let Some(interior) = model.imp().borrow_interior_mut() {
model.update(interior, new_selection.clone());
model.update_selection(interior, new_selection.clone());
}
});

Expand All @@ -203,15 +205,24 @@ impl TreeSelectionModel {
pub fn selection(&self) -> sync::Arc<selection::TreeSelection> {
self.imp().borrow_interior().unwrap().selection.clone()
}

fn update(&self, mut interior: std::cell::RefMut<'_, imp::TreeSelectionModelInterior>, new_selection: sync::Arc<selection::TreeSelection>) {
if interior.selection.is_outdated(&*new_selection) {
interior.selection = new_selection;

fn update_selection(&self, mut interior: std::cell::RefMut<'_, imp::TreeSelectionModelInterior>, new_selection: sync::Arc<selection::TreeSelection>) {
let mut selection_changed = false;

let n_items = interior.gtk_model.n_items();
drop(interior);
new_selection.changes_since(&interior.selection.clone(), &mut |new_selection, record| {
interior.selection = new_selection.clone();
selection_changed = selection_changed || record.selection_changed;
});

let tlm = interior.gtk_model.clone();
let rlm = interior.gtk_model.model().downcast::<hierarchy::RootListModel>().unwrap();
let document = interior.selection.document.clone();
drop(interior);

self.selection_changed(0, n_items);
rlm.update_document(&document);

if selection_changed {
self.selection_changed(0, tlm.n_items());
}
}
}

0 comments on commit 438bfc7

Please sign in to comment.