Implement pencil and line tool

This commit is contained in:
2026-02-24 22:20:06 +08:00
parent b567ef1fed
commit 4e999ed459
6 changed files with 1011 additions and 179 deletions

View File

@@ -1,18 +1,11 @@
use iced::advanced::layout::{self, Layout};
use iced::advanced::{renderer, Clipboard};
use iced::advanced::widget::{self, Widget};
use iced::advanced::{Clipboard, renderer};
use iced::mouse;
use iced::{Element, Event, Length, Rectangle, Size};
// We need the image renderer trait
use iced::advanced::image as img;
// ---------- State ----------
/// Internal state tracking whether the button is currently pressed.
#[derive(Default)]
pub struct State {
is_pressed: bool,
}
// ---------- Widget struct ----------
@@ -26,7 +19,11 @@ pub struct ImageButton<Handle, Message> {
is_pressed: bool,
}
pub fn image_button<Handle, Message>(normal: impl Into<Handle>, pressed: impl Into<Handle>, is_pressed: bool) -> ImageButton<Handle, Message> {
pub fn image_button<Handle, Message>(
normal: impl Into<Handle>,
pressed: impl Into<Handle>,
is_pressed: bool,
) -> ImageButton<Handle, Message> {
ImageButton::new(normal, pressed, is_pressed)
}
@@ -68,22 +65,12 @@ impl<Handle, Message> ImageButton<Handle, Message> {
// ---------- Widget impl ----------
impl<Message, Theme, Renderer, Handle> Widget<Message, Theme, Renderer>
for ImageButton<Handle, Message>
for ImageButton<Handle, Message>
where
Renderer: img::Renderer<Handle = Handle>,
Handle: Clone,
Message: Clone,
{
// --- Tree (internal state) ---
fn tag(&self) -> widget::tree::Tag {
widget::tree::Tag::of::<State>()
}
fn state(&self) -> widget::tree::State {
widget::tree::State::new(State::default())
}
// --- Size ---
fn size(&self) -> Size<Length> {
@@ -116,7 +103,7 @@ where
fn update(
&mut self,
tree: &mut widget::Tree,
_tree: &mut widget::Tree,
event: &Event,
layout: Layout<'_>,
cursor: mouse::Cursor,
@@ -125,7 +112,6 @@ where
shell: &mut iced::advanced::Shell<'_, Message>,
_viewport: &Rectangle,
) {
let state = tree.state.downcast_mut::<State>();
let bounds = layout.bounds();
match event {
@@ -163,7 +149,7 @@ where
fn draw(
&self,
tree: &widget::Tree,
_tree: &widget::Tree,
renderer: &mut Renderer,
_theme: &Theme,
_style: &renderer::Style,
@@ -171,7 +157,6 @@ where
_cursor: mouse::Cursor,
_viewport: &Rectangle,
) {
let state = tree.state.downcast_ref::<State>();
let bounds = layout.bounds();
// Pick the correct image handle.
@@ -199,7 +184,7 @@ where
// ---------- Into<Element> ----------
impl<'a, Message, Theme, Renderer, Handle> From<ImageButton<Handle, Message>>
for Element<'a, Message, Theme, Renderer>
for Element<'a, Message, Theme, Renderer>
where
Renderer: img::Renderer<Handle = Handle> + 'a,
Handle: Clone + 'a,
@@ -209,4 +194,4 @@ where
fn from(widget: ImageButton<Handle, Message>) -> Self {
Self::new(widget)
}
}
}