Skip to content

Commit

Permalink
improve egui viewer display
Browse files Browse the repository at this point in the history
  • Loading branch information
sbwtw committed Aug 17, 2024
1 parent 485b1c4 commit c69787f
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 60 deletions.
22 changes: 21 additions & 1 deletion viewer/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use stc::analysis::TypeAnalyzer;
use stc::backend::{CodeGenBackend, CodeGenDriver, LuaBackend};
use stc::context::{Scope, UnitsManager};
use stc::prelude::*;
use stc::utils::write_ast_to_file;

use crate::storage::Application;
use log::info;
use quick_xml::de::from_str;
use std::fs;
use std::io::Write;

Expand All @@ -13,10 +15,28 @@ pub(crate) struct StcViewerApp {
}

impl StcViewerApp {
#[inline]
pub fn with_mgr(mgr: UnitsManager) -> Self {
Self { mgr }
}

#[inline]
pub fn from_app(app: Application) -> Self {
let mgr = UnitsManager::new();
let ctx: ModuleContext = app.into();
mgr.write().add_context(ctx.clone());
mgr.write().set_active_application(Some(ctx.read().id()));

Self::with_mgr(mgr)
}

#[inline]
#[cfg(debug_assertions)]
pub fn load_test_project() -> Self {
let app = from_str(include_str!("../test_projects/example1/test_proj.xml"));
Self::from_app(app.unwrap())
}

pub fn compile(&mut self) {
let app = self.mgr.read().active_application().unwrap();
let app_read = app.read();
Expand Down
34 changes: 34 additions & 0 deletions viewer/src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use stc::prelude::*;

use std::fmt::Write;

mod app;
mod storage;

Expand Down Expand Up @@ -40,3 +42,35 @@ impl PrototypeDisplayName for Prototype {
}
}
}

pub(crate) trait PrototypeContent {
fn content(&self) -> String;
}

impl PrototypeContent for Prototype {
fn content(&self) -> String {
let proto = self.read().unwrap();
let mut buf = String::with_capacity(1024 * 4);

// Prototype properties
let uuid = proto.object_id();
if !uuid.is_nil() {
writeln!(buf, "ObjectId: {}", uuid).unwrap();
}
writeln!(buf, "Id: {}", proto.id()).unwrap();
writeln!(buf).unwrap();

// name : return_value
write!(buf, "{}", proto.name()).unwrap();
if let Some(ty) = proto.return_value().and_then(|x| x.ty()) {
write!(buf, " : {}", ty).unwrap();
}
writeln!(buf).unwrap();

for v in proto.variables() {
writeln!(buf, "{}: {}: {}", v.flags(), v.name(), v.ty().unwrap()).unwrap();
}

buf
}
}
60 changes: 41 additions & 19 deletions viewer/src/egui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
mod widgets;
use widgets::*;

use crate::app::StcViewerApp;
use crate::storage;
use crate::{PrototypeContent, PrototypeDisplayName};

use eframe::egui;
use eframe::egui::{vec2, FontId, Label, RichText, TextEdit};
use log::*;
use quick_xml::de::from_str;
use stc::prelude::*;
use std::default::Default;

#[derive(Default)]
Expand All @@ -15,6 +12,8 @@ struct StcViewerEGui {

// UI stuffs
search_text: String,
content: RichText,
show_origin_ast: bool,
}

impl StcViewerEGui {
Expand All @@ -36,7 +35,11 @@ impl eframe::App for StcViewerEGui {
ui.vertical(|ui| {
ui.horizontal(|ui| {
// Search input
ui.text_edit_singleline(&mut self.search_text);
let input = TextEdit::singleline(&mut self.search_text)
.min_size(vec2(100., 0.))
.desired_width(200.);
ui.add(input);
// ui.add_sized([100f32, ui.available_height()], input);

// Refresh button
let search_button = ui.button("Search");
Expand All @@ -47,8 +50,33 @@ impl eframe::App for StcViewerEGui {

// Data-Tree
if let Some(active_app) = self.app.mgr.read().active_application() {
let app_widget = AppWidget::new(active_app);
ui.add(app_widget);
let active_app = active_app.read();
ui.label(format!("App {}", active_app.id()));

// Prototypes
ui.label("\tPrototypes");
for proto in active_app.declarations() {
let label_resp = ui.label(format!("\t\t{}", proto.display_name()));
if label_resp.clicked() {
self.content =
RichText::new(proto.content()).font(FontId::monospace(12.));
}
}

// Functions
ui.label("\tFunctions");
for func in active_app.functions() {
let func = func.read();
let proto = active_app.get_declaration_by_id(func.decl_id()).unwrap();
let func_resp = ui.label(format!("\t\t{}", proto.display_name()));
if func_resp.clicked() {
let content = match (self.show_origin_ast, func.compiled_code()) {
(false, Some(code)) => format!("{}", code),
_ => format!("{}", func.parse_tree()),
};
self.content = RichText::new(content).font(FontId::monospace(12.));
}
}
}
});
});
Expand All @@ -61,14 +89,15 @@ impl eframe::App for StcViewerEGui {
if compile_button.clicked() {
self.app.compile()
}

ui.checkbox(&mut self.show_origin_ast, "Show AST");
});

// Content Area
egui::ScrollArea::vertical()
.auto_shrink(true)
.show(ui, |ui| {
// TODO: multi line content
ui.add(egui::Label::new(&self.search_text).selectable(true));
ui.add(Label::new(self.content.clone()).selectable(true));
});
});
}
Expand All @@ -82,14 +111,7 @@ pub fn main() -> Result<(), eframe::Error> {
..Default::default()
};

let mgr = UnitsManager::new();
let proj: Result<storage::Application, _> =
from_str(include_str!("../../test_projects/example1/test_proj.xml"));
let ctx: ModuleContext = proj.unwrap().into();
mgr.write().add_context(ctx.clone());
mgr.write().set_active_application(Some(ctx.read().id()));

let viewer = StcViewerEGui::new(StcViewerApp::with_mgr(mgr));
let viewer = StcViewerEGui::new(StcViewerApp::load_test_project());
eframe::run_native(
"stc-viewer - egui",
options,
Expand Down
10 changes: 5 additions & 5 deletions viewer/src/egui/widgets.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::PrototypeDisplayName;
use crate::{PrototypeContent, PrototypeDisplayName};
use eframe::egui::{Response, Ui, Widget};
use stc::prelude::*;

Expand All @@ -13,15 +13,15 @@ impl AppWidget {
}

impl Widget for AppWidget {
fn ui(self, ui: &mut Ui) -> Response {
fn ui(mut self, ui: &mut Ui) -> Response {
ui.label(format!("App {}", self.app.read().id()));

// Prototypes
let r = ui.label("\tPrototypes");
let resp = ui.label("\tPrototypes");
for proto in self.app.read().declarations() {
ui.label(format!("\t\t{}", proto.display_name()));
let _label_resp = ui.label(format!("\t\t{}", proto.display_name()));
}

r
resp
}
}
26 changes: 2 additions & 24 deletions viewer/src/gtk4/column_object.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::PrototypeContent;
use glib::Object;
use gtk4::subclass::prelude::*;
use stc::context::{Function, Prototype};
Expand Down Expand Up @@ -60,30 +61,7 @@ impl Display for ColumnObjectData {
writeln!(f, "{}", ctx)?;
writeln!(f, "ID: {}", ctx.id())
}
Self::Prototype(proto) => {
let proto = proto.read().unwrap();

// Prototype properties
let uuid = proto.object_id();
if !uuid.is_nil() {
writeln!(f, "ObjectId: {}", uuid)?;
}
writeln!(f, "Id: {}", proto.id())?;
writeln!(f)?;

// name : return_value
write!(f, "{}", proto.name())?;
if let Some(ty) = proto.return_value().and_then(|x| x.ty()) {
write!(f, " : {}", ty)?;
}
writeln!(f)?;

for v in proto.variables() {
writeln!(f, "{}: {}: {:?}", v.flags(), v.name(), v.ty())?;
}

Ok(())
}
Self::Prototype(proto) => writeln!(f, "{}", proto.content()),
Self::Function(func) => {
let func = func.read();

Expand Down
12 changes: 1 addition & 11 deletions viewer/src/gtk4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod stc_viewer;
use stc_viewer::{StcViewerGtk4, UIMessages, STC_VIEWER_COLUMN_NAME};

use crate::app::StcViewerApp;
use crate::storage;

use glib::MainContext;
use gtk4::gdk::ffi::GDK_BUTTON_SECONDARY;
Expand All @@ -13,23 +12,14 @@ use gtk4::{
Application, ApplicationWindow, CellRendererText, EventSequenceState, GestureClick,
Orientation, Paned, PopoverMenu, ScrolledWindow, TreeViewColumn, WrapMode,
};
use quick_xml::de::from_str;
use stc::prelude::*;
use std::rc::Rc;
use std::sync::Mutex;

pub fn main() {
pretty_env_logger::init();

let mgr = UnitsManager::new();
let proj: Result<storage::Application, _> =
from_str(include_str!("../../test_projects/example1/test_proj.xml"));
let ctx: ModuleContext = proj.unwrap().into();
mgr.write().add_context(ctx.clone());
mgr.write().set_active_application(Some(ctx.read().id()));

let gtk_app = Application::builder().build();
gtk_app.connect_activate(move |app| build_ui(app, StcViewerApp::with_mgr(mgr.clone())));
gtk_app.connect_activate(move |app| build_ui(app, StcViewerApp::load_test_project()));
gtk_app.run();
}

Expand Down

0 comments on commit c69787f

Please sign in to comment.