-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Svg and icon support #111
Merged
Merged
Svg and icon support #111
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8032428
Implemented SVG support in iced_wgpu.
Maldela a88aae5
Added an `Icon` widget to native.
Maldela 5696afc
Ran cargo_fmt over changed files.
Maldela f737c6d
Improved dpi handling
Maldela 895eaef
Merged svg pipeline into image
Maldela 27717bc
Renamed `Icon` widget to `Svg` and gave it separate width and height.
Maldela 09707f2
Rerasterize SVGs when resized and refactor a bit
hecrj 6ba2461
Update `Svg` documentation
hecrj aa29849
Add `svg` example
hecrj 232d487
Put `svg` rendering behind a feature gate
hecrj 514ccf8
Cache `Svg` load result properly
hecrj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//! Display an icon. | ||
use crate::{ | ||
layout, Element, Hasher, Layout, Length, Point, Rectangle, Widget, | ||
}; | ||
|
||
use std::{ | ||
hash::Hash, | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod button; | ||
mod checkbox; | ||
mod column; | ||
mod icon; | ||
mod image; | ||
mod radio; | ||
mod row; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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, | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would name this widget
Svg
and let it be configurable both in width and height.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking a bit about it, it may make sense to merge this with the
Image
widget. We will probably want to keep the SVG aspect ratio after all.We can check the extension as you were doing and have an additional
Memory
variant inimage
to deal with caching aware of DPI and viewport dimensions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said, that widget can handle svg as well as png or other files since some icons still come in png format. So I think naming it
Svg
would be rather misleading about its capabilities.I also thought that since icons are usually squares, giving it width and height would be unnecessary and a single size attribute simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about the image widget too. I would like to give the user more control about aspect ratio of the widget. Maybe an enum with
KeepAspectRatio
,Stretch
andKeepAspectRatioCrop
at least. But icons don't need that.Also having a separate widget makes integrating themed icons easier in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What an icon is depends on each particular application.
To keep things simple, I was proposing that we should instead expose a widget that is able to render only SVG with configurable dimensions. One that can be used to render SVG directly, not just icons.
This way, you could easily create a function
icon
in your application that choosesSvg
or anImage
widget based on the path, sets square dimensions, and returns anElement
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the
Icon
widget does all that for you. You don't have to create anicon
function yourself.It's very easy to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convenient, but not simple.
If our renderer supports SVG, we need a widget that can leverage it to its full extent, not just squares.
For instance, I may want to draw a logo using SVG. I won't be able to use
Icon
for that unless the logo is a square.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could easily merge the svg capability into
Image
. If simplicity is the main goal here, I could try to merge the two pipelines too. That shouldn't be too hard.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not completely sure about it.
I like the idea of having a dedicated
Svg
widget. It makes everything more explicit and forces users to keep in mindSvg
andImage
have different performance implications. It may also help us in the long run to feature-gate some particular widgets and reduce binary size. I'd say let's keep it as different widgets for now.However, I believe we can reuse the
image
pipeline by adding a new variant to theMemory
enum to handle theSvg
primitive. This should reduce the scope of the changes too.For instance: