iced-rs / iced

A cross-platform GUI library for Rust, inspired by Elm

Home Page:https://iced.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Images drawn to `Canvas` are not clipped by `with_clip`

barskern opened this issue · comments

Is your issue REALLY a bug?

  • My issue is indeed a bug!
  • I am not crazy! I will not fill out this form just to ask a question or request a feature. Pinky promise.

Is there an existing issue for this?

  • I have searched the existing issues.

Is this issue related to iced?

  • My hardware is compatible and my graphics drivers are up-to-date.

What happened?

When using the brand new feature of images drawn to the canvas, the images draw are not clipped by with_clip.

I've created a SSCCE which can be found here, and attached as code below. The example shows that the red rectangle (which is draw at the size of the image), is correctly clipped, while the image ignores the clipping. They should both be vertically clipped in the example (ref. screenshot).

A screenshot of my SSCCE showing the issue

//! Showcasing image clipping on the canvas.
use iced::advanced::image::Renderer as IRenderer;
use iced::mouse;
use iced::widget::canvas::Geometry;
use iced::widget::{canvas, image};
use iced::{color, Element, Fill, Point, Rectangle, Renderer, Size};
use tracing::debug;

pub fn main() -> iced::Result {
    tracing_subscriber::fmt::init();

    iced::application(
        "Image Clipping - Iced",
        ImageClipping::update,
        ImageClipping::view,
    )
    .run()
}

#[derive(Default)]
struct ImageClipping {
    state: State,
}

#[derive(Debug, Clone, Copy)]
enum Message {}

impl ImageClipping {
    fn update(&mut self, _message: Message) {}
    fn view(&self) -> Element<Message> {
        canvas(&self.state).width(Fill).height(Fill).into()
    }
}

#[derive(Debug)]
struct State {
    sun: image::Handle,
    cache: canvas::Cache,
}

impl State {
    pub fn new() -> State {
        State {
            sun: image::Handle::from_bytes(
                include_bytes!("../../solar_system/assets/sun.png").as_slice(),
            ),
            cache: Default::default(),
        }
    }
}

impl<Message> canvas::Program<Message> for State {
    type State = ();

    fn draw(
        &self,
        _state: &Self::State,
        renderer: &Renderer,
        _theme: &iced::Theme,
        bounds: Rectangle,
        _cursor: mouse::Cursor,
    ) -> Vec<Geometry> {

        let result = self.cache.draw(renderer, bounds.size(), |frame| {
            let image_size = {
                let s = renderer.measure_image(&self.sun);
                Size::new(s.width as f32, s.height as f32)
            };

            let image_rect = Rectangle::with_size(image_size);
            // Image should be halved in the horizontal direction
            let mut clipping_rect = Rectangle::with_size(Size::new(image_size.width / 2.0, image_size.height));

            debug!("Clipping Area: {:?}", clipping_rect);
            debug!("Image Area: {:?}", image_rect);

            frame.with_clip(clipping_rect, |clipped_frame| {
                // This rectangle with the same dimensions as the image is clipped correctly
                clipped_frame.fill_rectangle(
                    Point::ORIGIN,
                    image_size,
                    color!(0xFF0000),
                );
            });

            // Move the clipping area "down" to draw the examples at the same time
            clipping_rect.y += image_size.height;

            frame.with_clip(clipping_rect, |clipped_frame| {
                // This image is not clipped/cropped to the clipping_rect area and is overflowing.
                clipped_frame.draw_image(image_rect, &self.sun);
            });
        });

        vec![result]
    }
}

impl Default for State {
    fn default() -> Self {
        Self::new()
    }
}

What is the expected behavior?

I expected the Image to be clipped so as to not overflow the bounds of the clipping rectangle provided to with_clip.

Version

master

Operating System

Linux

Do you have any log output?

2024-08-19T13:19:29.918659Z  WARN wgpu_hal::vulkan::instance: InstanceFlags::VALIDATION requested, but unable to find layer: VK_LAYER_KHRONOS_validation    
2024-08-19T13:19:29.919716Z  INFO wgpu_hal::vulkan::instance: Debug utils not enabled: debug_utils_user_data not passed to Instance::from_raw    
2024-08-19T13:19:29.921067Z  INFO wgpu_hal::gles::egl: Using Wayland platform    
2024-08-19T13:19:29.934732Z  INFO iced_wgpu::window::compositor: Settings {
    present_mode: AutoVsync,
    backends: Backends(
        VULKAN | GL | METAL | DX12 | BROWSER_WEBGPU,
    ),
    default_font: Font {
        family: SansSerif,
        weight: Normal,
        stretch: Normal,
        style: Normal,
    },
    default_text_size: Pixels(
        16.0,
    ),
    antialiasing: None,
}    
2024-08-19T13:19:29.936398Z  INFO wgpu_core::instance: Adapter Vulkan AdapterInfo { name: "Intel(R) Graphics (ADL GT2)", vendor: 32902, device: 18086, device_type: IntegratedGpu, driver: "Intel open-source Mesa driver", driver_info: "Mesa 24.1.6-arch1.1", backend: Vulkan }    
2024-08-19T13:19:29.938046Z  WARN wgpu_hal::gles::adapter: Detected skylake derivative running on mesa i915. Clears to srgb textures will use manual shader clears.    
2024-08-19T13:19:29.938081Z  INFO wgpu_core::instance: Adapter Gl AdapterInfo { name: "Mesa Intel(R) Graphics (ADL GT2)", vendor: 32902, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Gl }    
2024-08-19T13:19:29.938182Z  INFO iced_wgpu::window::compositor: Available adapters: [
    AdapterInfo {
        name: "Intel(R) Graphics (ADL GT2)",
        vendor: 32902,
        device: 18086,
        device_type: IntegratedGpu,
        driver: "Intel open-source Mesa driver",
        driver_info: "Mesa 24.1.6-arch1.1",
        backend: Vulkan,
    },
    AdapterInfo {
        name: "Mesa Intel(R) Graphics (ADL GT2)",
        vendor: 32902,
        device: 0,
        device_type: IntegratedGpu,
        driver: "",
        driver_info: "",
        backend: Gl,
    },
]    
2024-08-19T13:19:29.940080Z  WARN wgpu_hal::gles::adapter: Detected skylake derivative running on mesa i915. Clears to srgb textures will use manual shader clears.    
2024-08-19T13:19:29.940130Z  INFO wgpu_core::instance: Adapter Vulkan AdapterInfo { name: "Intel(R) Graphics (ADL GT2)", vendor: 32902, device: 18086, device_type: IntegratedGpu, driver: "Intel open-source Mesa driver", driver_info: "Mesa 24.1.6-arch1.1", backend: Vulkan }    
2024-08-19T13:19:29.940177Z  INFO iced_wgpu::window::compositor: Selected: AdapterInfo {
    name: "Intel(R) Graphics (ADL GT2)",
    vendor: 32902,
    device: 18086,
    device_type: IntegratedGpu,
    driver: "Intel open-source Mesa driver",
    driver_info: "Mesa 24.1.6-arch1.1",
    backend: Vulkan,
}    
2024-08-19T13:19:29.940830Z  INFO iced_wgpu::window::compositor: Available formats: Copied {
    it: Iter(
        [
            Bgra8UnormSrgb,
            Rgba8UnormSrgb,
            Bgra8Unorm,
            Rgba8Unorm,
        ],
    ),
}    
2024-08-19T13:19:29.940850Z  INFO iced_wgpu::window::compositor: Available alpha modes: [
    Opaque,
    PreMultiplied,
]    
2024-08-19T13:19:29.940863Z  INFO iced_wgpu::window::compositor: Selected format: Bgra8UnormSrgb with alpha mode: PreMultiplied