Skip to content

Commit

Permalink
Merge pull request #319 from tarkah/image-pane
Browse files Browse the repository at this point in the history
Add `ImagePane` widget
  • Loading branch information
hecrj authored Dec 18, 2020
2 parents 07b5700 + 10d6df7 commit 0e9f649
Show file tree
Hide file tree
Showing 7 changed files with 466 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ jobs:
run: cargo check --package iced --target wasm32-unknown-unknown
- name: Check compilation of `tour` example
run: cargo build --package tour --target wasm32-unknown-unknown
- name: Check compilation of `pokedex` example
run: cargo build --package pokedex --target wasm32-unknown-unknown
- name: Check compilation of `todos` example
run: cargo build --package todos --target wasm32-unknown-unknown
11 changes: 8 additions & 3 deletions examples/pokedex/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use iced::{
button, futures, image, Align, Application, Button, Column, Command,
Container, Element, Image, Length, Row, Settings, Text,
Container, Element, Length, Row, Settings, Text,
};

pub fn main() -> iced::Result {
Expand Down Expand Up @@ -112,16 +112,20 @@ struct Pokemon {
name: String,
description: String,
image: image::Handle,
image_viewer: image::viewer::State,
}

impl Pokemon {
const TOTAL: u16 = 807;

fn view(&self) -> Element<Message> {
fn view(&mut self) -> Element<Message> {
Row::new()
.spacing(20)
.align_items(Align::Center)
.push(Image::new(self.image.clone()))
.push(image::Viewer::new(
&mut self.image_viewer,
self.image.clone(),
))
.push(
Column::new()
.spacing(20)
Expand Down Expand Up @@ -200,6 +204,7 @@ impl Pokemon {
.map(|c| if c.is_control() { ' ' } else { c })
.collect(),
image,
image_viewer: image::viewer::State::new(),
})
}

Expand Down
5 changes: 4 additions & 1 deletion graphics/src/widget/image.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! Display images in your user interface.
pub mod viewer;

use crate::backend::{self, Backend};

use crate::{Primitive, Renderer};
use iced_native::image;
use iced_native::mouse;
use iced_native::Layout;

pub use iced_native::image::{Handle, Image};
pub use iced_native::image::{Handle, Image, Viewer};

impl<B> image::Renderer for Renderer<B>
where
Expand Down
55 changes: 55 additions & 0 deletions graphics/src/widget/image/viewer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Zoom and pan on an image.
use crate::backend::{self, Backend};
use crate::{Primitive, Renderer};

use iced_native::image;
use iced_native::image::viewer;
use iced_native::mouse;
use iced_native::{Rectangle, Size, Vector};

impl<B> viewer::Renderer for Renderer<B>
where
B: Backend + backend::Image,
{
fn draw(
&mut self,
state: &viewer::State,
bounds: Rectangle,
image_size: Size,
translation: Vector,
handle: image::Handle,
is_mouse_over: bool,
) -> Self::Output {
(
{
Primitive::Clip {
bounds,
content: Box::new(Primitive::Translate {
translation,
content: Box::new(Primitive::Image {
handle,
bounds: Rectangle {
x: bounds.x,
y: bounds.y,
..Rectangle::with_size(image_size)
},
}),
}),
offset: Vector::new(0, 0),
}
},
{
if state.is_cursor_grabbed() {
mouse::Interaction::Grabbing
} else if is_mouse_over
&& (image_size.width > bounds.width
|| image_size.height > bounds.height)
{
mouse::Interaction::Grab
} else {
mouse::Interaction::Idle
}
},
)
}
}
3 changes: 3 additions & 0 deletions native/src/widget/image.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! Display images in your user interface.
pub mod viewer;
pub use viewer::Viewer;

use crate::layout;
use crate::{Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget};

Expand Down
Loading

0 comments on commit 0e9f649

Please sign in to comment.