Skip to content

Commit d477668

Browse files
committed
nuon: Combo list
1 parent 52b32dc commit d477668

1 file changed

Lines changed: 71 additions & 9 deletions

File tree

nuon/src/lib.rs

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::hash::{Hash, Hasher};
1+
use std::{
2+
borrow::Cow,
3+
hash::{Hash, Hasher},
4+
};
25

36
pub use euclid;
47

@@ -93,6 +96,10 @@ impl<H: Hash> From<H> for Id {
9396
impl Id {
9497
const NULL: Self = Id(0);
9598

99+
pub fn as_raw(&self) -> u64 {
100+
self.0
101+
}
102+
96103
pub fn hash(v: impl Hash) -> Self {
97104
let mut hasher = std::hash::DefaultHasher::new();
98105
v.hash(&mut hasher);
@@ -788,7 +795,7 @@ pub struct Button {
788795
preseed_color: Color,
789796
border_radius: [f32; 4],
790797
icon: &'static str,
791-
label: &'static str,
798+
label: Cow<'static, str>,
792799
text_justify: TextJustify,
793800
}
794801

@@ -813,7 +820,7 @@ impl Button {
813820
preseed_color: Color::new_u8(67, 65, 72, 1.0),
814821
border_radius: [0.0; 4],
815822
icon: "X",
816-
label: "",
823+
label: Cow::Borrowed(""),
817824
text_justify: TextJustify::Center,
818825
}
819826
}
@@ -878,8 +885,8 @@ impl Button {
878885
self
879886
}
880887

881-
pub fn label(mut self, label: &'static str) -> Self {
882-
self.label = label;
888+
pub fn label(mut self, label: impl Into<Cow<'static, str>>) -> Self {
889+
self.label = label.into();
883890
self
884891
}
885892

@@ -892,7 +899,7 @@ impl Button {
892899
if let Some(id) = self.id {
893900
id
894901
} else if !self.label.is_empty() {
895-
Id::hash(self.label)
902+
Id::hash(&self.label)
896903
} else {
897904
Id::hash(self.icon)
898905
}
@@ -1075,14 +1082,69 @@ impl Label {
10751082
}
10761083

10771084
if !self.icon.is_empty() {
1078-
let x = rect.origin.x + center_x(rect.size.width, self.font_size);
1079-
let y = rect.origin.y + center_x(rect.size.height, self.font_size);
1085+
let pad_x = match self.text_justify {
1086+
TextJustify::Left => 1.0,
1087+
TextJustify::Right => -1.0,
1088+
TextJustify::Center => 0.0,
1089+
};
1090+
1091+
let icon_size = self.font_size;
1092+
let pad_x = pad_x * 10.0;
1093+
1094+
let y = rect.origin.y + self::center_y(rect.size.height, icon_size);
1095+
let x = match self.text_justify {
1096+
TextJustify::Left => rect.origin.x + pad_x,
1097+
TextJustify::Right => {
1098+
let x = rect.origin.x + rect.size.width - icon_size;
1099+
x + pad_x
1100+
}
1101+
TextJustify::Center => rect.origin.x + center_x(rect.size.width, icon_size),
1102+
};
10801103

10811104
layer.icons.push(IconRenderElement {
10821105
origin: Point::new(x, y),
1083-
size: self.font_size,
1106+
size: icon_size,
10841107
icon: self.icon.to_string(),
10851108
});
10861109
}
10871110
}
10881111
}
1112+
1113+
pub fn combo_list<'a, ITEM: ToString>(
1114+
ui: &mut Ui,
1115+
id: impl Into<Id>,
1116+
(item_w, item_h): (f32, f32),
1117+
list: &'a [ITEM],
1118+
) -> Option<&'a ITEM> {
1119+
self::quad()
1120+
.width(item_w)
1121+
.height(item_h * list.len() as f32)
1122+
.color([27, 25, 32])
1123+
.build(ui);
1124+
1125+
let id = id.into();
1126+
1127+
let mut res = None;
1128+
for (nth, item) in list.iter().enumerate() {
1129+
let id = Id::hash_with(|h| {
1130+
id.as_raw().hash(h);
1131+
nth.hash(h);
1132+
});
1133+
1134+
if self::button()
1135+
.id(id)
1136+
.y(item_h * nth as f32)
1137+
.size(item_w, item_h)
1138+
.label(item.to_string())
1139+
.text_justify(TextJustify::Left)
1140+
.border_radius([5.0; 4])
1141+
.hover_color([160, 81, 255])
1142+
.preseed_color([180, 90, 255])
1143+
.build(ui)
1144+
{
1145+
res = Some(item);
1146+
}
1147+
}
1148+
1149+
res
1150+
}

0 commit comments

Comments
 (0)