Skip to content

Commit

Permalink
[ui] pick first part & first field on init
Browse files Browse the repository at this point in the history
  • Loading branch information
torymur committed Oct 1, 2024
1 parent 603742c commit 38bca79
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 91 deletions.
2 changes: 1 addition & 1 deletion ui/assets/tailwind.css

Large diffs are not rendered by default.

161 changes: 77 additions & 84 deletions ui/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ fn move_to(direction: NavMove, nf: usize, np: usize) {
let field = &parts[next_np].fields()[next_nf];

*locked_field.write() = Some((next_np, next_nf));
*selected_field.write() = Some(field.clone());
*selected_part.write() = Some(part.clone());
*selected_field.write() = field.clone();
*selected_part.write() = part.clone();
}

fn try_jump(nf: usize, np: usize) {
Expand All @@ -73,13 +73,16 @@ fn try_jump(nf: usize, np: usize) {
}

fn update_selected_page(page: Rc<dyn PageView>) {
let viewer = use_context::<AppState>().viewer;
let mut selected_page = use_context::<AppState>().selected_page;
let mut selected_part = use_context::<AppState>().selected_part;
let mut selected_field = use_context::<AppState>().selected_field;
let mut locked_field = use_context::<AppState>().locked_field;
let part = viewer.read().get_part(&page, 0);
let field = viewer.read().get_field(&part, 0);
*selected_page.write() = page;
*selected_part.write() = None;
*selected_field.write() = None;
*selected_part.write() = part;
*selected_field.write() = field;
*locked_field.write() = None;
}

Expand Down Expand Up @@ -230,10 +233,13 @@ pub fn Body() -> Element {
pub fn RightSide() -> Element {
rsx! {
div {
Description { }
}
div {
Visual { }
class: "h-[calc(100vh-48px)] overflow-y-auto",
div {
Description { }
}
div {
Visual { }
}
}
}
}
Expand All @@ -244,15 +250,15 @@ pub fn LeftSide() -> Element {
div {
class: "p-4 h-[calc(100vh-48px)] overflow-y-auto w-full text-sm font-medium",
div {
class: "flex w-full",
class: "flex w-full opacity-90",
div {
class: "border border-slate-800 hover:bg-slate-800 hover:text-slate-330",
class: if list() {"bg-slate-800 text-slate-330"},
onclick: move |_| {
list.set(true);
},
div {
class: "p-2",
class: "px-2 py-1",
"Page View"
}
}
Expand All @@ -263,7 +269,7 @@ pub fn LeftSide() -> Element {
list.set(false);
},
div {
class: "p-2",
class: "px-2 py-1",
"Tree View"
}
}
Expand Down Expand Up @@ -435,79 +441,66 @@ pub fn Description() -> Element {
let selected_page = use_context::<AppState>().selected_page;
let selected_part = use_context::<AppState>().selected_part;
let selected_field = use_context::<AppState>().selected_field;
let (part_desc, part_label) = match selected_part() {
None => ("", "".to_string()),
Some(p) => (p.desc(), p.label()),
};
match selected_field() {
None => {
rsx! {
let part_desc = selected_part().desc();
let part_label = selected_part().label();
let field = selected_field();
rsx! {
div {
class: "p-4 h-80 w-full overflow-auto",
div {
"{selected_page().desc()}"
}
FieldNavigation { title: part_label }
div {
class: "text-xs",
"{part_desc}"
}
div {
class: "flex pt-6 text-xs space-x-6",
div {
class: "p-4 h-80 w-full overflow-auto",
"{selected_page().desc()}"
class: "w-2/3",
"{field.desc}"
}
}
}
Some(field) => {
rsx! {
div {
class: "p-4 h-80 w-full overflow-auto",
div {
"{selected_page().desc()}"
}
FieldNavigation { title: part_label }
div {
class: "text-xs",
"{part_desc}"
}
div {
class: "flex pt-6 text-xs space-x-6",
div {
class: "w-2/3",
"{field.desc}"
}
div {
class: "w-1/3",
table {
class: "table table-xs table-fixed",
tbody {
tr {
td {
"Offset"
}
td {
"{field.offset} byte(s)"
}
}
tr {
td {
"Size"
}
td {
"{field.size} byte(s)"
}
}
tr {
td {
"Value"
}
td {
div {
class: "truncate",
"{field.value}"
}
}
class: "w-1/3",
table {
class: "table table-xs table-fixed",
tbody {
tr {
td {
"Offset"
}
td {
"{field.offset} byte(s)"
}
}
tr {
td {
"Size"
}
td {
"{field.size} byte(s)"
}
}
tr {
td {
"Value"
}
td {
div {
class: "truncate",
"{field.value}"
}
tr {
td {
"Hex"
}
td {
div {
class: "truncate",
"{field.to_hex()}"
}
}
}
}
tr {
td {
"Hex"
}
td {
div {
class: "truncate",
"{field.to_hex()}"
}
}
}
Expand Down Expand Up @@ -638,8 +631,8 @@ pub fn FieldElement(nf: usize, np: usize) -> Element {
if locked().is_some() {return}

// If field is not locked we want it to move freely.
*selected_field.write() = Some(field.clone());
*selected_part.write() = Some(part.clone());
*selected_field.write() = field.clone();
*selected_part.write() = part.clone();
}
},
onclick: {
Expand All @@ -658,8 +651,8 @@ pub fn FieldElement(nf: usize, np: usize) -> Element {

if locked().is_none() || locked() != Some((np, nf)) {
*locked.write() = Some((np, nf));
*selected_field.write() = Some(field.clone());
*selected_part.write() = Some(part.clone());
*selected_field.write() = field.clone();
*selected_part.write() = part.clone();
} else {
*locked.write() = None;
}
Expand Down
13 changes: 8 additions & 5 deletions ui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub struct AppState {
pub current_db: Signal<String>,
pub viewer: Signal<Viewer>,
pub selected_page: Signal<Rc<dyn PageView>>,
pub selected_field: Signal<Option<Rc<Field>>>,
pub selected_part: Signal<Option<Rc<dyn Part>>>,
pub selected_field: Signal<Rc<Field>>,
pub selected_part: Signal<Rc<dyn Part>>,
pub locked_field: Signal<Option<(usize, usize)>>,
pub format: Signal<Format>,
}
Expand All @@ -31,12 +31,15 @@ impl AppState {
// preloaded db shouldn't fail
let viewer =
Viewer::new_from_included(SIMPLE_DB).expect("Viewer failed to init for preloaded db.");
let page = viewer.get_page(1);
let part = viewer.get_part(&page, 0);
let field = viewer.get_field(&part, 0);

AppState {
current_db: Signal::new(SIMPLE_DB.to_string()),
selected_page: Signal::new(viewer.get_page(1)),
selected_part: Signal::new(None),
selected_field: Signal::new(None),
selected_page: Signal::new(page),
selected_part: Signal::new(part),
selected_field: Signal::new(field),
locked_field: Signal::new(None),
format: Signal::new(Format::Hybrid),
viewer: Signal::new(viewer),
Expand Down
16 changes: 15 additions & 1 deletion ui/src/viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::rc::Rc;
use parser::*;

use crate::included_db::INCLUDED_DB;
use crate::{BTreeNodeView, BTreeView, PageElementBuilder, PageLayout, PageView};
use crate::{BTreeNodeView, BTreeView, Field, PageElementBuilder, PageLayout, PageView, Part};

#[derive(Debug)]
pub struct Viewer {
Expand Down Expand Up @@ -66,6 +66,20 @@ impl Viewer {
.clone()
}

pub fn get_part(&self, page: &Rc<dyn PageView>, index: usize) -> Rc<dyn Part> {
page.parts()
.get(index)
.expect("Part is outside of Viewer Page range.")
.clone()
}

pub fn get_field(&self, part: &Rc<dyn Part>, index: usize) -> Rc<Field> {
part.fields()
.get(index)
.expect("Fields is outside of Viewer Part range.")
.clone()
}

fn load_btree_node(
node: BTreeNode,
pmap: &mut BTreeMap<usize, Rc<dyn PageView>>,
Expand Down

0 comments on commit 38bca79

Please sign in to comment.