Skip to content

Commit

Permalink
1. Change the inspector to a virtual list.
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-observer committed Dec 26, 2024
1 parent 52e6a7c commit 2abd383
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 49 deletions.
23 changes: 15 additions & 8 deletions src/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod data;
mod view;

pub use view::capture;
use crate::app_state::AppState;
use crate::context::StyleCx;
use crate::event::{Event, EventListener, EventPropagation};
Expand All @@ -10,8 +9,7 @@ use crate::style::{Style, StyleClassRef, StylePropRef, Transition};
use crate::view::{IntoView, View};
use crate::view_state::ChangeFlags;
use crate::views::{
button, dyn_container, stack, static_label,
text, v_stack, v_stack_from_iter, Decorators, Label,
button, dyn_container, stack, static_label, text, v_stack, v_stack_from_iter, Decorators, Label,
};
use crate::{style, Clipboard};
use floem_reactive::{batch, RwSignal, Scope, SignalGet, SignalUpdate};
Expand All @@ -23,15 +21,16 @@ use std::cell::Cell;
use std::collections::{HashMap, HashSet};
use std::fmt::Display;
use std::rc::Rc;
pub use view::capture;

#[cfg(not(target_arch = "wasm32"))]
use std::time::{Duration, Instant};
#[cfg(target_arch = "wasm32")]
use web_time::{Duration, Instant};

use taffy::prelude::Layout;
use taffy::style::{FlexDirection};
use crate::inspector::data::CapturedDatas;
use taffy::prelude::Layout;
use taffy::style::FlexDirection;

#[derive(Clone, Debug)]
pub struct CapturedView {
Expand Down Expand Up @@ -182,7 +181,8 @@ fn add_event(
name: String,
id: ViewId,
capture_view: CaptureView,
capture: &Rc<Capture>, datas: RwSignal<CapturedDatas>
capture: &Rc<Capture>,
datas: RwSignal<CapturedDatas>,
) -> impl View {
let capture = capture.clone();
row.keyboard_navigable()
Expand Down Expand Up @@ -623,12 +623,19 @@ fn find_relative_view_by_id_with_self(
}
}

fn update_select_view_id(id: ViewId, capture: &CaptureView, request_focus: bool, datas: RwSignal<CapturedDatas>) {
fn update_select_view_id(
id: ViewId,
capture: &CaptureView,
request_focus: bool,
datas: RwSignal<CapturedDatas>,
) {
capture.selected.set(Some(id));
capture.highlighted.set(Some(id));
capture.expanding_selection.set(Some((id, request_focus)));
batch(|| {
datas.update(|x| { x.focus(id);});
datas.update(|x| {
x.focus(id);
});
});
}

Expand Down
54 changes: 23 additions & 31 deletions src/inspector/data.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::inspector::CapturedView;
use crate::views::VirtualVector;
use crate::ViewId;
use floem_reactive::{create_rw_signal, RwSignal, SignalGet, SignalUpdate};
use std::ops::AddAssign;
use std::rc::Rc;
use floem_reactive::{create_rw_signal, RwSignal, SignalGet, SignalUpdate};
use crate::inspector::{CapturedView};
use crate::{ ViewId};
use crate::views::VirtualVector;

#[derive(Clone)]
pub struct CapturedDatas {
pub root: CapturedData,
pub focus_line: RwSignal<usize>
pub focus_line: RwSignal<usize>,
}

impl CapturedDatas {
pub fn init_from_view(view: Rc<CapturedView>,) -> Self {
pub fn init_from_view(view: Rc<CapturedView>) -> Self {
let root = CapturedData::init_from_view(view);
Self {
root,
Expand Down Expand Up @@ -41,7 +41,7 @@ impl CapturedDatas {
next: &mut usize,
min: usize,
max: usize,
level: usize
level: usize,
) -> Vec<(usize, usize, CapturedData)> {
self.root.get_children(next, min, max, level)
}
Expand All @@ -50,9 +50,9 @@ impl CapturedDatas {
pub enum DataType {
Internal {
children: Vec<CapturedData>,
expanded: RwSignal<bool>
expanded: RwSignal<bool>,
},
Leaf
Leaf,
}
#[derive(Clone, Debug)]
pub struct CapturedData {
Expand All @@ -78,17 +78,14 @@ impl CapturedData {
Self {
id: view.id,
view_conf: view,
ty: DataType::Internal {
children,
expanded,
},
ty: DataType::Internal { children, expanded },
}
}
}
pub fn count_line(&self, id: ViewId, line: &mut usize) -> bool {
line.add_assign(1);
if self.id == id {
return true
return true;
}
match &self.ty {
DataType::Internal { children, expanded } => {
Expand All @@ -100,45 +97,41 @@ impl CapturedData {
}
}
}
DataType::Leaf => {
}
DataType::Leaf => {}
}
false
}

pub fn focus(&mut self, id: ViewId) -> bool {
if self.id == id {
return true
return true;
}
match &mut self.ty {
DataType::Internal { children, expanded } => {
for child in children.iter_mut() {
if child.focus(id) {
expanded.set(true);
return true
return true;
}
}
}
DataType::Leaf => {
}
DataType::Leaf => {}
}
false
}

pub fn expanded(&self) -> Option<RwSignal<bool>> {
match &self.ty {
DataType::Internal { expanded, .. } => {
Some(*expanded)
}
DataType::Leaf => {None}
DataType::Internal { expanded, .. } => Some(*expanded),
DataType::Leaf => None,
}
}

/// contain self?
fn total(&self) -> usize {
match &self.ty {
DataType::Internal { expanded, children } => {
if expanded.get() == true {
if expanded.get() {
let mut total = 1;
for child in children {
total += child.total();
Expand All @@ -148,7 +141,7 @@ impl CapturedData {
1
}
}
DataType::Leaf => {1}
DataType::Leaf => 1,
}
}

Expand All @@ -157,7 +150,7 @@ impl CapturedData {
next: &mut usize,
min: usize,
max: usize,
level: usize
level: usize,
) -> Vec<(usize, usize, CapturedData)> {
let mut children_data = Vec::new();
if *next >= min && *next < max {
Expand All @@ -168,7 +161,7 @@ impl CapturedData {
next.add_assign(1);
match &self.ty {
DataType::Internal { expanded, children } => {
if expanded.get() == true {
if expanded.get() {
for child in children {
let child_children = child.get_children(next, min, max, level + 1);
if !child_children.is_empty() {
Expand All @@ -180,21 +173,20 @@ impl CapturedData {
}
}
}
DataType::Leaf => { }
DataType::Leaf => {}
}
children_data
}
}


impl VirtualVector<(usize, usize, CapturedData)> for CapturedDatas {
fn total_len(&self) -> usize {
self.total()
}

fn slice(
&mut self,
range: std::ops::Range<usize>
range: std::ops::Range<usize>,
) -> impl Iterator<Item = (usize, usize, CapturedData)> {
let min = range.start;
let max = range.end;
Expand Down
16 changes: 6 additions & 10 deletions src/inspector/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ fn capture_view(
selected: create_rw_signal(None),
highlighted: create_rw_signal(None),
};
let datas = create_rw_signal(CapturedDatas::init_from_view(
capture.root.clone(),
));
let datas = create_rw_signal(CapturedDatas::init_from_view(capture.root.clone()));
let window = capture.window.clone();
let capture_ = capture.clone();
let (image_width, image_height) = capture
Expand Down Expand Up @@ -584,16 +582,14 @@ fn tree_node_name(view: &CapturedData, marge_left: f64) -> impl IntoView {
// .background(Color::WHITE.multiply_alpha(0.3))
.width(12.0)
.height(12.0)
.margin_right(4.0)
// .border(1.0)
// .border_radius(4.0)
// .border_color(Color::WHITE.multiply_alpha(0.4)),
.margin_right(4.0), // .border(1.0)
// .border_radius(4.0)
// .border_color(Color::WHITE.multiply_alpha(0.4)),
})
.on_click_stop(move |_| match ty {
Some(expanded) => {
.on_click_stop(move |_| {
if let Some(expanded) = ty {
expanded.set(!expanded.get_untracked());
}
None => {}
});
h_stack((checkbox, id, tab, name)).style(move |s| s.items_center().margin_left(marge_left))
}

0 comments on commit 2abd383

Please sign in to comment.