Skip to content
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

Add image::Viewer widget #319

Merged
merged 25 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0d8cefb
Add `ImagePane` widget
tarkah Apr 23, 2020
7f7e803
Show idle cursor if image can't be panned
tarkah Apr 23, 2020
6bf459e
Rebase to master and update for api changes
tarkah May 14, 2020
431171f
Rename and add to iced image module
tarkah May 15, 2020
5d045c2
rename to image::Viewer
tarkah May 27, 2020
de176be
centered image and zoom to cursor
tarkah May 27, 2020
5dd62ba
update docs
tarkah May 27, 2020
c7bb434
remove re-export on viewer::State
tarkah May 27, 2020
0b73e5f
Merge remote-tracking branch 'tarkah/image-pane' into image-pane
hecrj Dec 18, 2020
74ee7cc
Format use declarations in `image::viewer`
hecrj Dec 18, 2020
71de341
Turn methods into helper functions in `image::viewer`
hecrj Dec 18, 2020
0cdf8d5
Use `image::Viewer` in `pokedex` example
hecrj Dec 18, 2020
21b10dc
Fix `layout` of `image::Viewer`
hecrj Dec 18, 2020
add167d
Pan `image::Viewer` even if the cursor is out of bounds
hecrj Dec 18, 2020
ca3e4e9
Simplify pattern match in `image::Viewer::on_event`
hecrj Dec 18, 2020
e6f23e3
Rename `scale_pct` to `scale_step` in `image::Viewer`
hecrj Dec 18, 2020
64af860
Remove unnecessary `Option` in `image::viewer::State`
hecrj Dec 18, 2020
cf97982
Build `todos` examples instead of `pokedex` in CI
hecrj Dec 18, 2020
4eb5779
Remove redundant `f32` conversions in `image::Viewer`
hecrj Dec 18, 2020
8245a11
Negate values instead of multipling by `-1.0` in `image::Viewer`
hecrj Dec 18, 2020
43ef85a
Rename `starting_cursor_pos` to `cursor_grabbed_at` in `image::Viewer`
hecrj Dec 18, 2020
149098c
Remove use of `unwrap` in `image::Viewer`
hecrj Dec 18, 2020
6a51282
Simplify `cursor_to_center` in `image::Viewer`
hecrj Dec 18, 2020
c54a644
Use intra-doc links in `image::viewer`
hecrj Dec 18, 2020
10d6df7
Remove `max_width` and `max_height` from `image::Viewer`
hecrj Dec 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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