From e29feef8ba4f95f286039fcc1ca2e53bfe5019c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 18 Apr 2020 19:53:27 +0200 Subject: [PATCH] Render arrow icon in `ComboBox` --- glow/src/backend.rs | 1 + graphics/fonts/Icons.ttf | Bin 4912 -> 5032 bytes graphics/src/backend.rs | 7 +++- graphics/src/font.rs | 6 ++++ graphics/src/widget/combo_box.rs | 54 +++++++++++++++++++------------ native/src/widget/combo_box.rs | 4 ++- wgpu/src/backend.rs | 1 + 7 files changed, 51 insertions(+), 22 deletions(-) diff --git a/glow/src/backend.rs b/glow/src/backend.rs index 8b5b4f9c..e1685816 100644 --- a/glow/src/backend.rs +++ b/glow/src/backend.rs @@ -193,6 +193,7 @@ impl iced_graphics::Backend for Backend { impl backend::Text for Backend { const ICON_FONT: Font = font::ICONS; const CHECKMARK_ICON: char = font::CHECKMARK_ICON; + const ARROW_DOWN_ICON: char = font::ARROW_DOWN_ICON; fn default_size(&self) -> u16 { self.default_text_size diff --git a/graphics/fonts/Icons.ttf b/graphics/fonts/Icons.ttf index 1c832f86576e51451b729a7fe513616dea38d21a..5e455b6988de00cc4a4e928aec96a27f555669a3 100644 GIT binary patch delta 584 zcmXw$PiPZC6vn?dv-v|dYzxM14nfu+wWSq9)G9O;JyoTL9JDkDwM+hrO|zsy+fzMB z@szsYO%I-W=%EmAdg#?ddr}1NLQ#YqT96>bZxbDM=Dpwdy?ryYyFb2rvF&W#bn+*F z#o<{{Cq^-<$O_m+H%5Q`UVhb(|0Ofwf=IaT_EoRrdM^>J?f+6ZN_MIb+a-* zcXR{L8S+7`?3R+-$w_jTJX50(nbQvdV}g94R&NzHVkT1<3tZx>jiQSQtdN(;(Ym|g zVb6R^rUqxtt(Onqj3vp>0d?p#)>{j`*LQe;JpJolv+RApJ@pYd0U!A#fK5MJlSkFwE zJ=3=2#Iog+#hg#dw*U3>&*^6B>*>#Tk4AE*F!f;PZwEgX*%LwZ_jEeb@6#CGJkWOwM(VPP1SgUjulDA F{{l;Pc6I;& delta 444 zcmXw!%}YW-6vgk%ynL2M5*96lOd^TiMuD6l|U#KZi6dO4(oK~NAxv`KsG zS_^F|TSoK;R7BCDbp=8#ObXhBoiUtY?%dy*b7$_9gQr79|JI~2yF@X87m(z==VrTydpSPvI&!LHTVO;VVMO0<@@-<|lBl~}`dMIAJI12C=C%*na3n?y9-?Uy&CI~w zfp5l0qX2Xb6ONj@(P%|5?k0B5J6VrU6=O6#%As<5TNHd}2a(^!uIM+pL{&MS>#0g> zx4l}xK>yX|n@VjBuFU@hkkqeLf4q{?oQ#LUwrsl)AqrQ5jw#m88E1lccDb^v2i$+E CU0?eE diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index b73c636e..dd7dbbc2 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -22,9 +22,14 @@ pub trait Text { /// The `char` representing a ✔ icon in the [`ICON_FONT`]. /// - /// [`ICON_FONT`]: #associatedconst.ICON_FONt + /// [`ICON_FONT`]: #associatedconst.ICON_FONT const CHECKMARK_ICON: char; + /// The `char` representing a ▼ icon in the built-in [`ICONS`] font. + /// + /// [`ICON_FONT`]: #associatedconst.ICON_FONT + const ARROW_DOWN_ICON: char; + /// Returns the default size of text. fn default_size(&self) -> u16; diff --git a/graphics/src/font.rs b/graphics/src/font.rs index bcc28857..5c62681c 100644 --- a/graphics/src/font.rs +++ b/graphics/src/font.rs @@ -31,3 +31,9 @@ pub const ICONS: iced_native::Font = iced_native::Font::External { #[cfg(feature = "font-icons")] #[cfg_attr(docsrs, doc(cfg(feature = "font-icons")))] pub const CHECKMARK_ICON: char = '\u{F00C}'; + +/// The `char` representing a ▼ icon in the built-in [`ICONS`] font. +/// +/// [`ICONS`]: const.ICONS.html +#[cfg(feature = "font-icons")] +pub const ARROW_DOWN_ICON: char = '\u{E800}'; diff --git a/graphics/src/widget/combo_box.rs b/graphics/src/widget/combo_box.rs index 27ea762a..92024c6c 100644 --- a/graphics/src/widget/combo_box.rs +++ b/graphics/src/widget/combo_box.rs @@ -35,27 +35,41 @@ where border_radius: 0, }; - ( - if let Some(label) = selected { - let label = Primitive::Text { - content: label, - size: f32::from(text_size), - font: Font::Default, - color: Color::BLACK, - bounds: Rectangle { - x: bounds.x + f32::from(padding), - y: bounds.center_y(), - ..bounds - }, - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Center, - }; + let arrow_down = Primitive::Text { + content: B::ARROW_DOWN_ICON.to_string(), + font: B::ICON_FONT, + size: bounds.height * 0.7, + bounds: Rectangle { + x: bounds.x + bounds.width - f32::from(padding) * 2.0, + y: bounds.center_y(), + ..bounds + }, + color: Color::BLACK, + horizontal_alignment: HorizontalAlignment::Right, + vertical_alignment: VerticalAlignment::Center, + }; - Primitive::Group { - primitives: vec![background, label], - } - } else { - background + ( + Primitive::Group { + primitives: if let Some(label) = selected { + let label = Primitive::Text { + content: label, + size: f32::from(text_size), + font: Font::Default, + color: Color::BLACK, + bounds: Rectangle { + x: bounds.x + f32::from(padding), + y: bounds.center_y(), + ..bounds + }, + horizontal_alignment: HorizontalAlignment::Left, + vertical_alignment: VerticalAlignment::Center, + }; + + vec![background, label, arrow_down] + } else { + vec![background, arrow_down] + }, }, if is_mouse_over { mouse::Interaction::Pointer diff --git a/native/src/widget/combo_box.rs b/native/src/widget/combo_box.rs index 0b25b836..1b04a9a8 100644 --- a/native/src/widget/combo_box.rs +++ b/native/src/widget/combo_box.rs @@ -126,7 +126,9 @@ where let size = { let intrinsic = Size::new( - max_width as f32 + f32::from(text_size), + max_width as f32 + + f32::from(text_size) + + f32::from(self.padding), f32::from(text_size), ); diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index a25f42f7..c71a6a77 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -248,6 +248,7 @@ impl iced_graphics::Backend for Backend { impl backend::Text for Backend { const ICON_FONT: Font = font::ICONS; const CHECKMARK_ICON: char = font::CHECKMARK_ICON; + const ARROW_DOWN_ICON: char = font::ARROW_DOWN_ICON; fn default_size(&self) -> u16 { self.default_text_size