From 3e5c7e1a92a78fe256e57c601979ff0b5ccd413a Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Sat, 2 Nov 2024 15:30:48 +0100 Subject: [PATCH] Fixes --- .github/workflows/check.yml | 2 + .../egui_flex/examples/simple_flex_example.rs | 4 +- crates/egui_flex/src/lib.rs | 38 +++++++------------ crates/egui_flex/tests/flex_tests.rs | 17 +++------ crates/hello_egui_utils/src/example.rs | 13 ++++--- crates/hello_egui_utils/src/lib.rs | 2 + fancy-example/src/chat.rs | 6 +++ fancy-example/src/gallery.rs | 7 +++- fancy-example/src/stargazers.rs | 6 +++ fancy-example/tests/fancy_example.rs | 10 ++--- 10 files changed, 54 insertions(+), 51 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 654f6ec..ef6f7e6 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -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 diff --git a/crates/egui_flex/examples/simple_flex_example.rs b/crates/egui_flex/examples/simple_flex_example.rs index 368fdfe..0e21216 100644 --- a/crates/egui_flex/examples/simple_flex_example.rs +++ b/crates/egui_flex/examples/simple_flex_example.rs @@ -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(); diff --git a/crates/egui_flex/src/lib.rs b/crates/egui_flex/src/lib.rs index ee6b2fa..c8e3e2c 100644 --- a/crates/egui_flex/src/lib.rs +++ b/crates/egui_flex/src/lib.rs @@ -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), } @@ -71,6 +74,7 @@ impl From 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, @@ -80,7 +84,7 @@ impl Size { } /// A flex container. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct Flex { id_salt: Option, direction: FlexDirection, @@ -93,22 +97,6 @@ pub struct Flex { height: Option, } -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 { @@ -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; @@ -578,7 +566,7 @@ 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); @@ -586,7 +574,7 @@ impl Flex { 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; } }; @@ -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; @@ -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 @@ -1223,7 +1211,7 @@ impl FlexContainerUi { ) -> FlexContainerResponse { let Self { frame_rect, - margin, + margin: _, mut max_item_size, remeasure_widget: _, last_inner_size: _, diff --git a/crates/egui_flex/tests/flex_tests.rs b/crates/egui_flex/tests/flex_tests.rs index bfb5c33..15efb50 100644 --- a/crates/egui_flex/tests/flex_tests.rs +++ b/crates/egui_flex/tests/flex_tests.rs @@ -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; @@ -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 { @@ -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()); } @@ -128,7 +125,7 @@ fn test_size( )] height: Option, ) { - let mut harness = Harness::new_ui(|ui| { + let harness = Harness::new_ui(|ui| { ui.group(|ui| { let mut flex = Flex::horizontal(); @@ -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)")); @@ -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")); }); @@ -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| { diff --git a/crates/hello_egui_utils/src/example.rs b/crates/hello_egui_utils/src/example.rs index f402498..22ce357 100644 --- a/crates/hello_egui_utils/src/example.rs +++ b/crates/hello_egui_utils/src/example.rs @@ -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"); @@ -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) => { diff --git a/crates/hello_egui_utils/src/lib.rs b/crates/hello_egui_utils/src/lib.rs index 6c23df6..91824d6 100644 --- a/crates/hello_egui_utils/src/lib.rs +++ b/crates/hello_egui_utils/src/lib.rs @@ -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; diff --git a/fancy-example/src/chat.rs b/fancy-example/src/chat.rs index 1b9b0a0..6427956 100644 --- a/fancy-example/src/chat.rs +++ b/fancy-example/src/chat.rs @@ -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()); diff --git a/fancy-example/src/gallery.rs b/fancy-example/src/gallery.rs index 8981063..f677b99 100644 --- a/fancy-example/src/gallery.rs +++ b/fancy-example/src/gallery.rs @@ -1,4 +1,3 @@ -use egui::load::TextureLoadResult; use egui::{Id, Image, OpenUrl, ScrollArea, Sense, Ui, Vec2}; use serde::Deserialize; @@ -38,6 +37,12 @@ pub struct Gallery { items: InfiniteScroll, } +impl Default for Gallery { + fn default() -> Self { + Self::new() + } +} + impl Gallery { pub fn new() -> Gallery { let items = include_str!("gallery/index.json"); diff --git a/fancy-example/src/stargazers.rs b/fancy-example/src/stargazers.rs index 5e24614..6017c03 100644 --- a/fancy-example/src/stargazers.rs +++ b/fancy-example/src/stargazers.rs @@ -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(); diff --git a/fancy-example/tests/fancy_example.rs b/fancy-example/tests/fancy_example.rs index 53903f3..8b6e0d5 100644 --- a/fancy-example/tests/fancy_example.rs +++ b/fancy-example/tests/fancy_example.rs @@ -1,4 +1,3 @@ -use casey::shouty; use egui::accesskit::Role; use egui_kittest::kittest::{Node, Queryable}; use egui_kittest::Harness; @@ -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; @@ -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(); } } @@ -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;