Skip to content

Commit

Permalink
Add combo ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerthox committed Feb 20, 2024
1 parent 61c5993 commit 7912f48
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arc_util"
version = "0.11.2"
version = "0.11.3"
edition = "2021"
authors = ["Zerthox"]
repository = "https://github.com/zerthox/arcdps-utils"
Expand All @@ -13,6 +13,7 @@ open = { version = "5.0.0", optional = true }
semver = { version = "1.0.17", optional = true }
serde = { version = "1.0.152", features = ["derive"], optional = true }
serde_json = { version = "1.0.93", optional = true }
strum = "0.26.1"

[dependencies.windows]
version = "0.52.0"
Expand Down
58 changes: 58 additions & 0 deletions src/ui/render/combo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::borrow::Cow;

use arcdps::imgui::{Selectable, StyleColor, Ui};
use strum::IntoEnumIterator;

/// Renders a combo box for items from an iterator.
// TODO: make more generic?
pub fn combo<T>(
ui: &Ui,
label: impl AsRef<str>,
all: impl IntoIterator<Item = T>,
current: &mut T,
item_label: impl Fn(&T) -> Cow<str>,
item_color: impl Fn(&T) -> Option<[f32; 4]>,
) -> bool
where
T: PartialEq,
{
let mut changed = false;
if let Some(_token) = ui.begin_combo(label, item_label(current).as_ref()) {
for entry in all {
let selected = entry == *current;

// apply color to selectable
let style =
item_color(&entry).map(|color| ui.push_style_color(StyleColor::Text, color));
if Selectable::new(item_label(&entry).as_ref())
.selected(selected)
.build(ui)
{
changed = true;
*current = entry;
}
drop(style);

// handle focus
if selected {
ui.set_item_default_focus();
}
}
}
changed
}

/// Renders a combo box for an enum implementing [`IntoEnumIterator`].
pub fn enum_combo<T>(ui: &Ui, label: impl AsRef<str>, current: &mut T) -> bool
where
T: PartialEq + AsRef<str> + IntoEnumIterator,
{
combo(
ui,
label,
T::iter(),
current,
|item| item.as_ref().into(),
|_| None,
)
}
2 changes: 2 additions & 0 deletions src/ui/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//! Predefined primitive UI elements & render helpers.
mod button;
mod combo;
mod context_menu;
mod input;
mod table;

pub use self::button::*;
pub use self::combo::*;
pub use self::context_menu::*;
pub use self::input::*;
pub use self::table::*;
Expand Down

0 comments on commit 7912f48

Please sign in to comment.