Skip to content

Commit

Permalink
refactor: add layout helper functions (Astrabit-ST#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
white-axe authored Jan 25, 2024
1 parent 7973cb5 commit 41205d6
Show file tree
Hide file tree
Showing 6 changed files with 578 additions and 615 deletions.
10 changes: 4 additions & 6 deletions crates/components/src/filesystem_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// terms of the Steamworks API by Valve Corporation, the licensors of this
// Program grant you additional permission to convey the resulting work.

use crate::UiExt;
use itertools::Itertools;

pub struct FileSystemView<T> {
Expand Down Expand Up @@ -265,17 +266,14 @@ where

let mut should_toggle = false;

let mut frame = egui::containers::Frame::none();
if self.row_index % 2 != 0 {
frame = frame.fill(ui.visuals().faint_bg_color);
}
let is_faint = self.row_index % 2 != 0;
self.row_index += 1;

let mut header_response = None;

match self.arena[node_id].get_mut() {
Entry::File { name, selected } => {
frame.show(ui, |ui| {
ui.with_stripe(is_faint, |ui| {
if ui
.add(egui::SelectableLabel::new(*selected, name.to_string()))
.clicked()
Expand Down Expand Up @@ -315,7 +313,7 @@ where
let layout = *ui.layout();
header_response = Some(header.show_header(ui, |ui| {
ui.with_layout(layout, |ui| {
frame.show(ui, |ui| {
ui.with_stripe(is_faint, |ui| {
if ui
.add(egui::SelectableLabel::new(
*selected,
Expand Down
189 changes: 83 additions & 106 deletions crates/components/src/id_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
// terms of the Steamworks API by Valve Corporation, the licensors of this
// Program grant you additional permission to convey the resulting work.

use crate::UiExt;

#[derive(Default, Clone)]
struct IdVecSelectionState {
pivot: Option<usize>,
Expand Down Expand Up @@ -122,53 +124,40 @@ where

let mut response = ui
.group(|ui| {
ui.with_layout(
egui::Layout {
cross_justify: true,
..Default::default()
},
|ui| {
ui.set_width(ui.available_width());

ui.add(
egui::TextEdit::singleline(&mut state.search_string)
.hint_text("Search"),
);

let mut is_faint = false;

for id in 0..self.len {
let is_id_selected =
self.reference.get(index).is_some_and(|x| *x == id);
if is_id_selected {
index += 1;
}
ui.with_cross_justify(|ui| {
ui.set_width(ui.available_width());

let formatted = (self.formatter)(id);
if matcher
.fuzzy(&formatted, &state.search_string, false)
.is_none()
{
continue;
}
ui.add(
egui::TextEdit::singleline(&mut state.search_string).hint_text("Search"),
);

let mut frame = egui::Frame::none();
if is_faint {
frame = frame.fill(ui.visuals().faint_bg_color);
}
is_faint = !is_faint;

frame.show(ui, |ui| {
if ui
.selectable_label(is_id_selected, (self.formatter)(id))
.clicked()
{
clicked_id = Some(id);
}
});
let mut is_faint = false;

for id in 0..self.len {
let is_id_selected = self.reference.get(index).is_some_and(|x| *x == id);
if is_id_selected {
index += 1;
}
},
)

let formatted = (self.formatter)(id);
if matcher
.fuzzy(&formatted, &state.search_string, false)
.is_none()
{
continue;
}

ui.with_stripe(is_faint, |ui| {
if ui
.selectable_label(is_id_selected, (self.formatter)(id))
.clicked()
{
clicked_id = Some(id);
}
});
is_faint = !is_faint;
}
})
.inner
})
.response;
Expand Down Expand Up @@ -284,73 +273,61 @@ where

let mut response = ui
.group(|ui| {
ui.with_layout(
egui::Layout {
cross_justify: true,
..Default::default()
},
|ui| {
ui.set_width(ui.available_width());

ui.add(
egui::TextEdit::singleline(&mut state.search_string)
.hint_text("Search"),
);

let mut is_faint = false;

for id in 0..self.len {
let is_id_plus = self.plus.get(plus_index).is_some_and(|x| *x == id);
if is_id_plus {
plus_index += 1;
}
let is_id_minus = self.minus.get(minus_index).is_some_and(|x| *x == id);
ui.with_cross_justify(|ui| {
ui.set_width(ui.available_width());

ui.add(
egui::TextEdit::singleline(&mut state.search_string).hint_text("Search"),
);

let mut is_faint = false;

for id in 0..self.len {
let is_id_plus = self.plus.get(plus_index).is_some_and(|x| *x == id);
if is_id_plus {
plus_index += 1;
}
let is_id_minus = self.minus.get(minus_index).is_some_and(|x| *x == id);
if is_id_minus {
minus_index += 1;
}

let formatted = (self.formatter)(id);
if matcher
.fuzzy(&formatted, &state.search_string, false)
.is_none()
{
continue;
}

ui.with_stripe(is_faint, |ui| {
// Make the background of the selectable label red if it's
// a minus
if is_id_minus {
minus_index += 1;
ui.visuals_mut().selection.bg_fill =
ui.visuals().gray_out(ui.visuals().error_fg_color);
}

let formatted = (self.formatter)(id);
if matcher
.fuzzy(&formatted, &state.search_string, false)
.is_none()
let label = (self.formatter)(id);
if ui
.selectable_label(
is_id_plus || is_id_minus,
if is_id_plus {
format!("+ {label}")
} else if is_id_minus {
format!("‒ {label}")
} else {
label
},
)
.clicked()
{
continue;
clicked_id = Some(id);
}

let mut frame = egui::Frame::none();
if is_faint {
frame = frame.fill(ui.visuals().faint_bg_color);
}
is_faint = !is_faint;

frame.show(ui, |ui| {
// Make the background of the selectable label red if it's
// a minus
if is_id_minus {
ui.visuals_mut().selection.bg_fill =
ui.visuals().gray_out(ui.visuals().error_fg_color);
}

let label = (self.formatter)(id);
if ui
.selectable_label(
is_id_plus || is_id_minus,
if is_id_plus {
format!("+ {label}")
} else if is_id_minus {
format!("‒ {label}")
} else {
label
},
)
.clicked()
{
clicked_id = Some(id);
}
});
}
},
)
});
is_faint = !is_faint;
}
})
.inner
})
.response;
Expand Down
18 changes: 6 additions & 12 deletions crates/components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub use filesystem_view::FileSystemView;
mod id_vec;
pub use id_vec::{IdVecPlusMinusSelection, IdVecSelection};

mod ui_ext;
pub use ui_ext::UiExt;

pub struct EnumMenuButton<'e, T> {
current_value: &'e mut T,
id: egui::Id,
Expand Down Expand Up @@ -145,11 +148,7 @@ where
.selected_text(self.reference.to_string())
.show_ui(ui, |ui| {
for (i, variant) in T::iter().enumerate() {
let mut frame = egui::Frame::none();
if i % 2 != 0 {
frame = frame.fill(ui.visuals().faint_bg_color);
}
frame.show(ui, |ui| {
ui.with_stripe(i % 2 != 0, |ui| {
if ui
.selectable_label(
std::mem::discriminant(self.reference)
Expand Down Expand Up @@ -255,13 +254,7 @@ where
continue;
}

let mut frame = egui::Frame::none();
if is_faint {
frame = frame.fill(ui.visuals().faint_bg_color);
}
is_faint = !is_faint;

frame.show(ui, |ui| {
ui.with_stripe(is_faint, |ui| {
if ui
.selectable_label(*self.reference == Some(id), formatted)
.clicked()
Expand All @@ -270,6 +263,7 @@ where
changed = true;
}
});
is_faint = !is_faint;
}
});

Expand Down
Loading

0 comments on commit 41205d6

Please sign in to comment.