Skip to content

Commit

Permalink
debug_ui: Show StyleSheet in EditText
Browse files Browse the repository at this point in the history
  • Loading branch information
kjarosh committed Jan 14, 2025
1 parent f1e5562 commit 0f4ecc7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 41 deletions.
4 changes: 4 additions & 0 deletions core/src/avm2/object/stylesheet_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ impl StyleSheetObject<'_> {
pub fn clear(self) {
self.0.styles.borrow_mut().clear();
}

pub fn selectors(self) -> Vec<WString> {
self.0.styles.borrow().keys().cloned().collect()
}
}
29 changes: 28 additions & 1 deletion core/src/debug_ui/avm2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::avm2::object::StyleSheetObject;
use crate::avm2::property::Property;
use crate::avm2::{
Activation, ArrayStorage, ClassObject, Error, Namespace, Object, TObject, Value,
Expand All @@ -6,12 +7,13 @@ use crate::context::UpdateContext;
use crate::debug_ui::display_object::open_display_object_button;
use crate::debug_ui::handle::{AVM2ObjectHandle, DisplayObjectHandle};
use crate::debug_ui::{ItemToSave, Message};
use egui::{Align, Checkbox, Grid, Id, Layout, TextEdit, Ui, Window};
use egui::{Align, Checkbox, CollapsingHeader, Grid, Id, Layout, TextEdit, Ui, Window};
use egui_extras::{Column, TableBody, TableBuilder, TableRow};
use fnv::FnvHashMap;
use gc_arena::Mutation;
use std::borrow::Cow;

use super::display_object::show_text_format;
use super::movie::open_movie_button;

#[derive(Debug, Eq, PartialEq, Hash, Default, Copy, Clone)]
Expand All @@ -21,6 +23,7 @@ enum Panel {
Properties,
Elements,
Class,
StyleSheet,
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -62,6 +65,9 @@ impl Avm2ObjectWindow {
if object.as_class_object().is_some() {
ui.selectable_value(&mut self.open_panel, Panel::Class, "Class Info");
}
if object.as_style_sheet().is_some() {
ui.selectable_value(&mut self.open_panel, Panel::StyleSheet, "Style Sheet");
}
});
ui.separator();

Expand All @@ -82,6 +88,11 @@ impl Avm2ObjectWindow {
self.show_class(class, messages, &mut activation, ui)
}
}
Panel::StyleSheet => {
if let Some(style_sheet) = object.as_style_sheet() {
self.show_style_sheet(style_sheet, ui)
}
}
}
});
keep_open
Expand Down Expand Up @@ -280,6 +291,22 @@ impl Avm2ObjectWindow {
});
}

fn show_style_sheet(&mut self, style_sheet: StyleSheetObject<'_>, ui: &mut Ui) {
let mut selectors = style_sheet.selectors();
selectors.sort();
for selector in selectors {
CollapsingHeader::new(selector.to_utf8_lossy())
.id_salt(ui.id().with(selector.to_utf8_lossy()))
.show(ui, |ui| {
if let Some(tf) = style_sheet.get_style(&selector) {
show_text_format(ui, &tf, true);
} else {
ui.weak("No styles");
}
});
}
}

fn show_properties<'gc>(
&mut self,
object: Object<'gc>,
Expand Down
102 changes: 62 additions & 40 deletions core/src/debug_ui/display_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl DisplayObjectWindow {
if let DisplayObject::MovieClip(object) = object {
self.show_movieclip(ui, context, object)
} else if let DisplayObject::EditText(object) = object {
self.show_edit_text(ui, context, object)
self.show_edit_text(ui, context, object, messages)
} else if let DisplayObject::Bitmap(object) = object {
self.show_bitmap(ui, context, object)
} else if let DisplayObject::Stage(object) = object {
Expand Down Expand Up @@ -307,6 +307,7 @@ impl DisplayObjectWindow {
ui: &mut Ui,
context: &mut UpdateContext<'gc>,
object: EditText<'gc>,
messages: &mut Vec<Message>,
) {
Grid::new(ui.id().with("edittext"))
.num_columns(2)
Expand Down Expand Up @@ -530,10 +531,23 @@ impl DisplayObjectWindow {

ui.label("Default Text Format");
ui.horizontal(|ui| {
show_text_format(ui, object.spans().default_format());
show_text_format_hover(ui, object.spans().default_format());
});
ui.end_row();

ui.label("Style Sheet");
if let Some(style_sheet) = object.style_sheet() {
if ui.button(format!("{:p}", style_sheet.as_ptr())).clicked() {
messages.push(Message::TrackAVM2Object(AVM2ObjectHandle::new(
context,
crate::avm2::Object::StyleSheetObject(style_sheet),
)));
}
} else {
ui.weak("None");
}
ui.end_row();

ui.label("Text Width");
ui.label(format!("{}", object.measure_text(context).0.to_pixels()));
ui.end_row();
Expand Down Expand Up @@ -585,7 +599,7 @@ impl DisplayObjectWindow {
ui.label(format!("{}–{} ({})", start, end, format.span_length));

ui.horizontal(|ui| {
show_text_format(ui, &format.get_text_format());
show_text_format_hover(ui, &format.get_text_format());
});

ui.label(text.to_string());
Expand Down Expand Up @@ -1458,48 +1472,56 @@ fn bounds_label(ui: &mut Ui, bounds: Rectangle<Twips>, hover: &mut Option<Rectan
}
}

fn show_text_format(ui: &mut Ui, tf: &TextFormat) {
fn show_text_format_hover(ui: &mut Ui, tf: &TextFormat) {
ui.weak("(hover)").on_hover_ui(|ui| {
ui.style_mut().interaction.selectable_labels = true;
Grid::new(ui.id().with("text_format_table"))
.num_columns(2)
.striped(true)
.show(ui, |ui| {
for (key, value) in [
("Font Face", tf.font.as_ref().map(|v| v.to_string())),
("Font Size", tf.size.map(|v| v.to_string())),
("Color", tf.color.map(|v| format!("{v:?}"))),
("Align", tf.align.map(|v| format!("{v:?}"))),
("Bold?", tf.bold.map(|v| v.to_string())),
("Italic?", tf.italic.map(|v| v.to_string())),
("Underline?", tf.underline.map(|v| v.to_string())),
("Left Margin", tf.left_margin.map(|v| v.to_string())),
("Right Margin", tf.right_margin.map(|v| v.to_string())),
("Indent", tf.indent.map(|v| v.to_string())),
("Block Indent", tf.block_indent.map(|v| v.to_string())),
("Kerning?", tf.kerning.map(|v| v.to_string())),
("Leading", tf.leading.map(|v| v.to_string())),
("Letter Spacing", tf.letter_spacing.map(|v| v.to_string())),
("Tab Stops", tf.tab_stops.as_ref().map(|v| format!("{v:?}"))),
("Bullet?", tf.bullet.map(|v| v.to_string())),
("URL", tf.url.as_ref().map(|v| v.to_string())),
("Target", tf.target.as_ref().map(|v| v.to_string())),
("Display", tf.display.map(|v| format!("{v:?}"))),
] {
ui.label(key);
if let Some(value) = value {
if !value.is_empty() {
ui.label(value);
} else {
ui.weak("Empty");
}
show_text_format(ui, tf, false);
});
}

pub fn show_text_format(ui: &mut Ui, tf: &TextFormat, skip_none: bool) {
Grid::new(ui.id().with("text_format_table"))
.num_columns(2)
.striped(true)
.show(ui, |ui| {
for (key, value) in [
("Font Face", tf.font.as_ref().map(|v| v.to_string())),
("Font Size", tf.size.map(|v| v.to_string())),
("Color", tf.color.map(|v| format!("{v:?}"))),
("Align", tf.align.map(|v| format!("{v:?}"))),
("Bold?", tf.bold.map(|v| v.to_string())),
("Italic?", tf.italic.map(|v| v.to_string())),
("Underline?", tf.underline.map(|v| v.to_string())),
("Left Margin", tf.left_margin.map(|v| v.to_string())),
("Right Margin", tf.right_margin.map(|v| v.to_string())),
("Indent", tf.indent.map(|v| v.to_string())),
("Block Indent", tf.block_indent.map(|v| v.to_string())),
("Kerning?", tf.kerning.map(|v| v.to_string())),
("Leading", tf.leading.map(|v| v.to_string())),
("Letter Spacing", tf.letter_spacing.map(|v| v.to_string())),
("Tab Stops", tf.tab_stops.as_ref().map(|v| format!("{v:?}"))),
("Bullet?", tf.bullet.map(|v| v.to_string())),
("URL", tf.url.as_ref().map(|v| v.to_string())),
("Target", tf.target.as_ref().map(|v| v.to_string())),
("Display", tf.display.map(|v| format!("{v:?}"))),
] {
if skip_none && value.is_none() {
continue;
}

ui.label(key);
if let Some(value) = value {
if !value.is_empty() {
ui.label(value);
} else {
ui.weak("None");
ui.weak("Empty");
}
ui.end_row();
} else {
ui.weak("None");
}
});
});
ui.end_row();
}
});
}

pub fn open_display_object_button<'gc>(
Expand Down

0 comments on commit 0f4ecc7

Please sign in to comment.