Added an Icon widget to native.
This commit is contained in:
parent
8032428428
commit
a88aae5e04
7 changed files with 138 additions and 6 deletions
|
|
@ -24,6 +24,7 @@ pub mod button;
|
||||||
pub mod checkbox;
|
pub mod checkbox;
|
||||||
pub mod column;
|
pub mod column;
|
||||||
pub mod container;
|
pub mod container;
|
||||||
|
pub mod icon;
|
||||||
pub mod image;
|
pub mod image;
|
||||||
pub mod radio;
|
pub mod radio;
|
||||||
pub mod row;
|
pub mod row;
|
||||||
|
|
@ -41,6 +42,8 @@ pub use column::Column;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use container::Container;
|
pub use container::Container;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
|
pub use icon::Icon;
|
||||||
|
#[doc(no_inline)]
|
||||||
pub use image::Image;
|
pub use image::Image;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use radio::Radio;
|
pub use radio::Radio;
|
||||||
|
|
|
||||||
103
native/src/widget/icon.rs
Normal file
103
native/src/widget/icon.rs
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
//! Display an icon.
|
||||||
|
use crate::{layout, Element, Hasher, Layout, Length, Point, Rectangle, Widget};
|
||||||
|
|
||||||
|
use std::hash::Hash;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
/// A simple icon_loader widget.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Icon {
|
||||||
|
path: PathBuf,
|
||||||
|
size: Length,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Icon {
|
||||||
|
/// Create a new [`Icon`] from the file at `path`.
|
||||||
|
///
|
||||||
|
/// [`Icon`]: struct.Icon.html
|
||||||
|
pub fn new(path: impl Into<PathBuf>) -> Self {
|
||||||
|
Icon {
|
||||||
|
path: path.into(),
|
||||||
|
size: Length::Fill,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the size of the [`Icon`].
|
||||||
|
///
|
||||||
|
/// [`Icon`]: struct.Icon.html
|
||||||
|
pub fn size(mut self, size: Length) -> Self {
|
||||||
|
self.size = size;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Message, Renderer> Widget<Message, Renderer> for Icon
|
||||||
|
where
|
||||||
|
Renderer: self::Renderer,
|
||||||
|
{
|
||||||
|
fn width(&self) -> Length {
|
||||||
|
self.size
|
||||||
|
}
|
||||||
|
|
||||||
|
fn height(&self) -> Length {
|
||||||
|
self.size
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout(&self, _: &Renderer, limits: &layout::Limits) -> layout::Node {
|
||||||
|
let mut size = limits.width(self.size).height(self.size).max();
|
||||||
|
|
||||||
|
if size.width > size.height {
|
||||||
|
size.width = size.height;
|
||||||
|
} else if size.width < size.height {
|
||||||
|
size.height = size.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
layout::Node::new(size)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(
|
||||||
|
&self,
|
||||||
|
renderer: &mut Renderer,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
_cursor_position: Point,
|
||||||
|
) -> Renderer::Output {
|
||||||
|
let bounds = layout.bounds();
|
||||||
|
|
||||||
|
renderer.draw(
|
||||||
|
bounds,
|
||||||
|
self.path.as_path(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_layout(&self, state: &mut Hasher) {
|
||||||
|
self.size.hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The renderer of an [`Icon`].
|
||||||
|
///
|
||||||
|
/// Your [renderer] will need to implement this trait before being
|
||||||
|
/// able to use [`Icon`] in your [`UserInterface`].
|
||||||
|
///
|
||||||
|
/// [`Icon`]: struct.Icon.html
|
||||||
|
/// [renderer]: ../../renderer/index.html
|
||||||
|
/// [`UserInterface`]: ../../struct.UserInterface.html
|
||||||
|
pub trait Renderer: crate::Renderer {
|
||||||
|
/// Draws an [`Icon`].
|
||||||
|
///
|
||||||
|
/// [`Icon`]: struct.Icon.html
|
||||||
|
fn draw(
|
||||||
|
&mut self,
|
||||||
|
bounds: Rectangle,
|
||||||
|
path: &Path,
|
||||||
|
) -> Self::Output;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> From<Icon> for Element<'a, Message, Renderer>
|
||||||
|
where
|
||||||
|
Renderer: self::Renderer,
|
||||||
|
{
|
||||||
|
fn from(icon: Icon) -> Element<'a, Message, Renderer> {
|
||||||
|
Element::new(icon)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -80,11 +80,16 @@ pub mod widget {
|
||||||
pub use iced_winit::image::{Handle, Image};
|
pub use iced_winit::image::{Handle, Image};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod icon {
|
||||||
|
//! Display icons in your user interface.
|
||||||
|
pub use iced_winit::icon::Icon;
|
||||||
|
}
|
||||||
|
|
||||||
pub use iced_winit::{Checkbox, Radio, Text};
|
pub use iced_winit::{Checkbox, Radio, Text};
|
||||||
|
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use {
|
pub use {
|
||||||
button::Button, image::Image, scrollable::Scrollable, slider::Slider,
|
button::Button, icon::Icon, image::Image, scrollable::Scrollable, slider::Slider,
|
||||||
text_input::TextInput,
|
text_input::TextInput,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,6 @@ use iced_native::{
|
||||||
VerticalAlignment,
|
VerticalAlignment,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::svg;
|
|
||||||
|
|
||||||
/// A rendering primitive.
|
/// A rendering primitive.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Primitive {
|
pub enum Primitive {
|
||||||
|
|
@ -50,8 +48,8 @@ pub enum Primitive {
|
||||||
},
|
},
|
||||||
/// A svg icon primitive
|
/// A svg icon primitive
|
||||||
Svg {
|
Svg {
|
||||||
/// The handle of the icon
|
/// The path of the icon
|
||||||
handle: svg::Handle,
|
handle: crate::svg::Handle,
|
||||||
/// The bounds of the icon
|
/// The bounds of the icon
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
mod button;
|
mod button;
|
||||||
mod checkbox;
|
mod checkbox;
|
||||||
mod column;
|
mod column;
|
||||||
|
mod icon;
|
||||||
mod image;
|
mod image;
|
||||||
mod radio;
|
mod radio;
|
||||||
mod row;
|
mod row;
|
||||||
|
|
|
||||||
21
wgpu/src/renderer/widget/icon.rs
Normal file
21
wgpu/src/renderer/widget/icon.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
use crate::{svg::Handle, Primitive, Renderer};
|
||||||
|
use iced_native::{
|
||||||
|
icon, MouseCursor, Rectangle,
|
||||||
|
};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
impl icon::Renderer for Renderer {
|
||||||
|
fn draw(
|
||||||
|
&mut self,
|
||||||
|
bounds: Rectangle,
|
||||||
|
path: &Path,
|
||||||
|
) -> Self::Output {
|
||||||
|
(
|
||||||
|
Primitive::Svg {
|
||||||
|
handle: Handle::from_path(path),
|
||||||
|
bounds,
|
||||||
|
},
|
||||||
|
MouseCursor::OutOfBounds,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,12 +4,13 @@ use iced_native::{Hasher, Rectangle};
|
||||||
use std::{
|
use std::{
|
||||||
cell::RefCell,
|
cell::RefCell,
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
|
fmt::Debug,
|
||||||
hash::{Hash, Hasher as _},
|
hash::{Hash, Hasher as _},
|
||||||
mem,
|
mem,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
|
u32,
|
||||||
};
|
};
|
||||||
use std::fmt::Debug;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue