Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Nov 2, 2024
1 parent a7cba90 commit 3e5c7e1
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
execute_install_scripts: true

- uses: actions/checkout@v4
with:
lfs: true

- name: Set up cargo cache
uses: Swatinem/rust-cache@v2
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_flex/examples/simple_flex_example.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eframe::NativeOptions;
use egui::{Button, CentralPanel, Color32, Frame, Label, Stroke, TextEdit, Widget};
use egui_flex::{item, Flex, FlexAlign, FlexAlignContent, FlexItem};
use egui::{CentralPanel, TextEdit};
use egui_flex::{Flex, FlexAlignContent, FlexItem};

fn main() -> eframe::Result {
let mut text = "Hello, World!".to_string();
Expand Down
38 changes: 13 additions & 25 deletions crates/egui_flex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ pub enum FlexAlignContent {
SpaceAround,
}

/// A size value, either in points or as a percentage of the available space.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Size {
/// Size in points (pixels).
Points(f32),
/// Size as a percentage of the available space.
Percent(f32),
}

Expand All @@ -71,6 +74,7 @@ impl From<f32> for Size {
}

impl Size {
/// Get the size in points (pixels) based on the total available space.
pub fn get(&self, total: f32) -> f32 {
match self {
Size::Points(p) => *p,
Expand All @@ -80,7 +84,7 @@ impl Size {
}

/// A flex container.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Flex {
id_salt: Option<Id>,
direction: FlexDirection,
Expand All @@ -93,22 +97,6 @@ pub struct Flex {
height: Option<Size>,
}

impl Default for Flex {
fn default() -> Self {
Self {
id_salt: None,
direction: FlexDirection::default(),
justify: FlexJustify::default(),
align_content: FlexAlignContent::default(),
gap: None,
default_item: FlexItem::default(),
wrap: false,
width: None,
height: None,
}
}
}

/// Configuration for a flex item.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct FlexItem {
Expand Down Expand Up @@ -560,15 +548,15 @@ impl Flex {

let mut extra_cross_gap_start = 0.0;
let mut extra_cross_gap = 0.0;
let mut extra_cross_gap_end = 0.0; // TODO: How to handle extra end space?
let mut _extra_cross_gap_end = 0.0; // TODO: How to handle extra end space?
let mut extra_cross_space_per_row = 0.0;

if self.wrap == false {
if !self.wrap {
self.align_content = FlexAlignContent::Stretch;
}
match self.align_content {
FlexAlignContent::Start => {
extra_cross_gap_end = extra_cross_space;
_extra_cross_gap_end = extra_cross_space;
}
FlexAlignContent::Stretch => {
extra_cross_space_per_row = extra_cross_space / rows.len() as f32;
Expand All @@ -578,15 +566,15 @@ impl Flex {
}
FlexAlignContent::Center => {
extra_cross_gap_start = extra_cross_space / 2.0;
extra_cross_gap_end = extra_cross_space / 2.0;
_extra_cross_gap_end = extra_cross_space / 2.0;
}
FlexAlignContent::SpaceBetween => {
extra_cross_gap = extra_cross_space / (rows.len() as f32 - 1.0);
}
FlexAlignContent::SpaceAround => {
extra_cross_gap = extra_cross_space / rows.len() as f32;
extra_cross_gap_start = extra_cross_gap / 2.0;
extra_cross_gap_end = extra_cross_gap / 2.0;
_extra_cross_gap_end = extra_cross_gap / 2.0;
}
};

Expand All @@ -595,7 +583,7 @@ impl Flex {
row_position[cross_direction] += extra_cross_gap_start;

let row_count = rows.len();
for (idx, row) in &mut rows.iter_mut().enumerate() {
for (_idx, row) in &mut rows.iter_mut().enumerate() {
let mut row_size = Vec2::ZERO;
row_size[direction] = available_length;
row_size[cross_direction] = row.cross_size + extra_cross_space_per_row;
Expand All @@ -614,7 +602,7 @@ impl Flex {
let diff = available_length - row.total_size;
// Only grow items if a explicit size is set or if we wrapped
// If diff is < 0.0, we also set extra_space so we can shrink
if (size[direction].is_some() || row_count > 1 || diff < 0.0) {
if size[direction].is_some() || row_count > 1 || diff < 0.0 {
row.extra_space = diff;
}
if row.total_grow == 0.0 && row.extra_space > 0.0
Expand Down Expand Up @@ -1223,7 +1211,7 @@ impl FlexContainerUi {
) -> FlexContainerResponse<R> {
let Self {
frame_rect,
margin,
margin: _,
mut max_item_size,
remeasure_widget: _,
last_inner_size: _,
Expand Down
17 changes: 6 additions & 11 deletions crates/egui_flex/tests/flex_tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use eframe::emath::Vec2;
use egui::{
Align, Button, Checkbox, Context, DragValue, Frame, Label, Layout, ScrollArea, TextEdit, Ui,
};
use egui::{Align, Button, Checkbox, DragValue, Frame, Label, Layout, ScrollArea, TextEdit, Ui};
use egui_flex::{item, Flex, FlexAlign, FlexAlignContent, FlexItem, FlexJustify, Size};
use egui_kittest::wgpu::TestRenderer;
use egui_kittest::Harness;
use hello_egui_utils::run;
use rstest::rstest;
use std::cell::Cell;

Expand Down Expand Up @@ -48,7 +45,7 @@ fn test_justify(
FlexJustify::SpaceEvenly,
];

let mut app = |ui: &mut Ui| {
let app = |ui: &mut Ui| {
ui.label(format!("align: {align:?}, grow: {grow}"));

for justify in &justify_values {
Expand All @@ -72,7 +69,7 @@ fn test_justify(
}
};

let mut harness = Harness::new_ui(app);
let harness = Harness::new_ui(app);

harness.wgpu_snapshot(&snapshot_name());
}
Expand Down Expand Up @@ -128,7 +125,7 @@ fn test_size(
)]
height: Option<Size>,
) {
let mut harness = Harness::new_ui(|ui| {
let harness = Harness::new_ui(|ui| {
ui.group(|ui| {
let mut flex = Flex::horizontal();

Expand Down Expand Up @@ -298,7 +295,7 @@ fn egui_justify_interaction() {
ui.group(|ui| {
ui.label("vertical_centered_justified");
ui.vertical_centered_justified(|ui| {
ui.button("Justified normal button");
_ = ui.button("Justified normal button");

Flex::vertical().show(ui, |flex| {
flex.add(item(), Button::new("Justified flex button (vertical)"));
Expand All @@ -316,7 +313,7 @@ fn egui_justify_interaction() {
ui.group(|ui| {
ui.label("vertical normal");

ui.button("Non-justified normal button");
_ = ui.button("Non-justified normal button");
Flex::horizontal().show(ui, |flex| {
flex.add(item().grow(1.0), Button::new("Non-justified flex button"));
});
Expand Down Expand Up @@ -412,8 +409,6 @@ fn truncate() {
flex.add(item(), Button::new("World!"));
});

Flex::horizontal().w_full().show(ui, |flex| {});

ui.add(Button::new("Helloooooooooooooooooooooo").truncate());
Flex::horizontal().w_full().show(ui, |flex| {
flex.add_ui(item().shrink(), |ui| {
Expand Down
13 changes: 7 additions & 6 deletions crates/hello_egui_utils/src/example.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use eframe::NativeOptions;
use egui::{CentralPanel, Style, Ui};
use egui::{CentralPanel, Ui};

/// Run an example with the given name and content.
pub fn run(name: &str, mut f: impl FnMut(&mut Ui) + 'static) {
let mut initalized = false;
eframe::run_simple_native("helloui", NativeOptions::default(), move |ctx, _frame| {
if !initalized {
initalized = true;
let mut initialized = false;
eframe::run_simple_native(name, NativeOptions::default(), move |ctx, _frame| {
if !initialized {
initialized = true;
return;
}
CentralPanel::default().show(ctx, |ui| {
//ctx.inspection_ui(ui);
let mut style = (*ctx.style()).clone();
ui.checkbox(&mut style.debug.debug_on_hover, "Debug on hover");
ui.checkbox(&mut style.visuals.dark_mode, "Dark mode");
Expand All @@ -21,6 +21,7 @@ pub fn run(name: &str, mut f: impl FnMut(&mut Ui) + 'static) {
.unwrap();
}

/// Run an example with the given content.
#[macro_export]
macro_rules! run {
($content:expr) => {
Expand Down
2 changes: 2 additions & 0 deletions crates/hello_egui_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

/// Helper struct to easily align things with unknown sizes
pub mod center;

/// Utilities to conveniently create egui examples
#[cfg(feature = "example_util")]
pub mod example;

Expand Down
6 changes: 6 additions & 0 deletions fancy-example/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ pub struct ChatExample {
msgs_received: usize,
}

impl Default for ChatExample {
fn default() -> Self {
Self::new()
}
}

impl ChatExample {
pub fn new() -> Self {
let history_loader = Arc::new(HistoryLoader::new());
Expand Down
7 changes: 6 additions & 1 deletion fancy-example/src/gallery.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use egui::load::TextureLoadResult;
use egui::{Id, Image, OpenUrl, ScrollArea, Sense, Ui, Vec2};
use serde::Deserialize;

Expand Down Expand Up @@ -38,6 +37,12 @@ pub struct Gallery {
items: InfiniteScroll<GalleryItem, usize>,
}

impl Default for Gallery {
fn default() -> Self {
Self::new()
}
}

impl Gallery {
pub fn new() -> Gallery {
let items = include_str!("gallery/index.json");
Expand Down
6 changes: 6 additions & 0 deletions fancy-example/src/stargazers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ impl ExampleTrait for Stargazers {
}
}

impl Default for Stargazers {
fn default() -> Self {
Self::new()
}
}

impl Stargazers {
pub fn new() -> Self {
let mut infinite_scroll = InfiniteScroll::new();
Expand Down
10 changes: 4 additions & 6 deletions fancy-example/tests/fancy_example.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use casey::shouty;
use egui::accesskit::Role;
use egui_kittest::kittest::{Node, Queryable};
use egui_kittest::Harness;
Expand All @@ -7,9 +6,7 @@ use fancy_example::example::{Example, EXAMPLES};
use fancy_example::gallery::GALLERY_EXAMPLE;
use fancy_example::stargazers::STARGAZERS_EXAMPLE;
use fancy_example::App;
use std::panic::catch_unwind;
use std::time::Duration;
use wasm_bindgen_futures::js_sys::Atomics::notify;

pub fn app() -> Harness<'static> {
let mut app = None;
Expand All @@ -22,11 +19,11 @@ pub fn app() -> Harness<'static> {

fn open(example: &Example, harness: &mut Harness) {
harness
.get_by_role_and_name(Role::Button, &example.name)
.get_by_role_and_name(Role::Button, example.name)
.click();

// TODO: Remove once run runs until no more redraws are needed
for i in 0..30 {
for _ in 0..30 {
harness.run();
}
}
Expand Down Expand Up @@ -59,7 +56,8 @@ pub async fn test_stargazers() {

wait_for(&mut harness, |harness| {
harness.query_by_name_contains("lucasmerlin")
});
})
.await;

tokio::time::sleep(Duration::from_secs(1)).await;

Expand Down

0 comments on commit 3e5c7e1

Please sign in to comment.