feat: pop-os megasquash
x11: Workaround nvidia driver lacking DRI
feat(mouse area): add double click
mouse area: add double click
compositor: Add code to extract adapter from x11
refactor: Extract ids_from_dev from wayland specific code
wayland: Don't crash if libwayland isn't available
feat(sctk): support for overflow widget
sctk: Fixes for cursor icon
* With multiple windows, `SetCursor` is only sent for the focused
window. Fixing a flicker between icons when two windows are using
different cursors.
* If there is a drag surface, let that surface set the cursor. And not
any other.
* Set cursor on `enter`, and when switching between CSDs and app area.
Fixes https://github.com/pop-os/libcosmic/issues/533.
improv(sctk): per-surface cursor position tracking
feat(sctk): support ShowWindowMenu
Make text wrap configurable
fix(core): state order and handling of new trees
fix: settings.decorations enables SSD
refactor(sctk): convert window actions
fix: enable the tokio feature for accesskit_unix
fix: only try to connect to clipboard if on linux
iced_wgpu: don't query Wayland on macos
Update `window_clipboard`
sctk: Unmap subsurfaces instead of immediately destroying them
Destroying a surface is immediate, rather than synchronized with
commits.
This fixes a flickering behavior with drag and drop in
cosmic-workspaces.
sctk: Add alpha setting to `Subsurface` widget
sctk: Update `sctk`, `wayland-protocols`
Update for cosmic-text undefined buffer size
Adapt to cosmic-text undefined width change
fix: unset VK_LOADER_DRIVERS_DISABLE after enumeration
Allows applications to be launched on the NVIDIA GPU with Vulkan support
Adapt to new cosmic-text
wgpu: Fix wayland device id conversion
wgpu: Fix querying adapter, even if we already have one
wgpu: fix nvidia gpu powering up in hybrid setups
cargo fmt
fix: refactor dnd impl to support responsive widget
fix: update read and write methods so they don't recurse
fix(core): replace debug_assert in diff
fix: avoid with_borrow_mut
fix: better handling of state tree
This persists widget state associated with widgets assigned custom IDs even when the tree structure changes, but resets state if the custom ID is not found.
fix: emit Event::Resized to fix nav bar in cosmic-settings
fix(image): guess the image format before decoding
iced_wgpu: Query wayland for the device to use, if possible
sctk: Support `start_drag` with drags started from touch events
sctk: Add touch support
fix: update widnow-clipboard tag
fix: clean up dnd surfaces when a window is removed
Adjust to line ending needing to be specified as part of cosmic_text::BufferLine
sctk: Add support for drag-and-drop surface offsets
This adds an offset `Vector` as an argument to `on_drag`, and allows
passing an offset to `start_drag`.
Some applications using drag and drop want the top left corner of the
drag surface (as happens without an offset). But others want the drag
surface to be offset based on where the cursor is on the widget when
starting the drag. This can just be `-1 * offset`, but may be scaled if
the drag surface is a different size from the original widget.
fix(sctk): nested popup parent
feat(mouseare): mouse enter and exit
fix(tiny_skia): damage
fix(scrollable): filter scroll events in the wrong direction
sctk: Use empty input region for subsurfaces
This seems to work, and is a better way to deal with subsurface input if
there aren't any problems. This way, input events simply go to the
parent surface, so we don't have to deal with various edge cases related
to that. (Though for compositor-side issues, we still need to fix those
for other clients.)
This helps with an issue with drag-and-drop and subsurfaces on Smithay,
and a different issue on Kwin (in KDE 5.27, at least).
Send `DataSource` events to all surfaces
Previously these events are directed to the first surface, then removed
from `sctk_events`. Which is definitely not right.
slider & toggler roundness
Update window_clipboard to pop-dnd-4
fix(tiny-skia): non-simple border scaling
the issue can be seen with sharp corners when using the screenshot portal with scaling
Add read_primary/write_primary
chore: update tag
fix: translate offer positions in scrollable
fix(winit multi-window): handle exit_on_close request
fix(scrollable): pass child layout when calculating drag destinations
fix(container): id and set_id should use content
Clean up after lock surfaces are destroyed
Call unlock on session lock
chore: update tag
fixes for dnd
sctk: Fix handling of DnD with subsurfaces (#122)
Map subsurface to parent and add offset.
refactor: remove Sync bound for Message
fix: pass correct state and layout for container widgets
fix: docs
feat: update advertised drag destinations after rebuilding an interface
fix: color format & multi-window
fix: doc
feat: winit dnd
fix: ambiguous import
chore: reexport mime from window_clipboard
chore: use tag
clippy
feat: add actions and commands for new clipboard methods
cleanup docs
feat: custom mime types for Clipboard
sctk: Fix handling of layer surface `pointer_interactivity` (#115)
A null `region` represents an infinite region (the default). To set an
empty region, we need to create a `wl_region`.
fix(tiny_skia): disable shadows due to rendering glitch
fix(winit): add static lifetimes to multi-window application update
fix(winit): add static lifetimes to application update
Use `TypeId` to identify `subscription::Map`
(cherry picked from commit f39a5fd895)
fix(sctk): destroy drag icon and send event after cancel action
fix: clipboard cleanup
fix(sctk): clipboard dummy impl typo
refactor(sctk): optional clipboard
fix(sctk): broadcast events after update
when broadcasting events for no specific surface, it should be done after update so that the runtime subscription is current
fix(multi_window): enable drag resize
sctk: Map subsurface pointer events to parent surface, with offset
sctk_subsurface: Use two surfaces, handle button presses
Useful for testing pointer input to subsurfaces.
sctk: Add `subsurface_ids` mapping subsurface to parent and offset
sctk_subsurface_gst: NV12 surface suppport; disabled
Whether or not this works seems to depend on driver, or gstreamer
version...
Handle frame callbacks for subsurfaces, and `commit` parent surface
If the main surface is occluded completely by opaque subsurfaces, it may
not receive `frame` events. So we need to request frame events for all
subsurfaces as well.
Additionally, with "synchronized" subsurfaces, we need to `commit` the
parent surface for subsurface changes to take effect.
Fixes issues with subsurfaces updating slowly, or only when mouse moved
under some circumstances.
examples/sctk_subsurface_gst: Cache `BufferSource` in `BufferRef` qdata
Similar to `waylandsink`. Allows us to avoid creating a buffer source
(and ultimately `wl_buffer`) for every buffer swap.
sctk/subsurface: Cache `wl_buffer`s
Creating a new `wl_buffer` each frame seems to perform poorly. We can
instead keep a cache of `wl_buffer`s we have created from a
`BufferSource`.
sctk/subsurface: Avoid unnecessary subsurface commits if unchanged
feat(slider): add breakpoints
fix: autosize surface layout
Autosized surfaces perform the layout step to get the size and then again when building the interface, but sometimes the calculated size is not enough space when used as a bound, so we need to add a tiny amount to the calculated size. This also makes the event loop timeout duration configurable. Viewport physical size is calculated directly from the logical size now as well in iced-sctk to avoid inconsistencies that resulted from recalculating the logical size after using it to calculate the physical size.
fix(sctk): send close event instead of close requested when a window is closed
sctk: add command to set maximize state
Add `show_window_menu` action
Winit currently supports this only on Windows and Wayland.
This requests that a context menu is shown at the cursor position,
like the menu normally triggered by right clicking the title bar. This
is important for implementing client side decorations with Iced widgets.
Remove unnecessary redraw request
This was particularly visible on Redox where there is no vsync, but also
causes unnecessary redraws on Linux
chore: update accesskit
Disable broken rustdoc links
sctk: Add `Subsurface` widget (#79)
This adds a widget that attaches an shm or dma buffer to a subsurface,
scaled with `wp_viewporter`.
By exposing this as a widget, rather than as a type of window, it can be
positioned and scaled like any other iced widget. It provides an API
that's similar to an iced image.
The initial version of this just took a `wl_buffer`. But this makes
buffer re-use problematic. In particular, the docs for `wl_surface::attach`
note that `wl_buffer::release` events become unreliable if a buffer is
attached to multiple surfaces. And indicates that a client should create
multiple `wl_buffer` instances, or use `wp_linux_buffer_release`.
So we store information about the buffer, and create `wl_buffer`s as
needed. `SubsurfaceBuffer::new` also returns a future that's signaled
when all references are destroyed, both `wl_buffer`s and any instance of
the `SubsurfaceBuffer` that might still be used in the `view`.
So this seems like the best solution for now, within the
model-view-update architecture.
This has two examples: `sctk_subsurface`, showing a single-color shm
buffer, and `sctk_subsurface_gst`, which plays an h264 video to a
subsurface with vaapi decoding.
chore: use pop-os fork of winit
chore: unpin cosmic-text
Update wgpu to a commit that fixes use on Nvidia drivers
This can be tested with something like
`VK_ICD_FILENAMES=/usr/share/vulkan/icd.d/nvidia_icd.json cargo run -p tour
--features iced/wgpu`.
On Nvidia I'm seeing a flood of `Suboptimal present of frame` warnings.
So some improvement may still be needed here. But if it doesn't regress
behavior on other hardware, that seems like an improvement over
freezing.
fix(winit): pass text with modifiers in event
chore: update cosmic-text and glyphon
fix: distinguish between the key character and the utf8 of a key event
feat(wgpu): use alpha modes for compositing if available
chore: use updated softbuffer
fix: typo
fix: downgrade resvg
fix: core/serde
chore: remove default features
typo: add rev to glyphon
Update to cosmic-text refactor
Fix docs error
Add function to fill a Raw
Fixes for last commit
fix: broadcast surface events
dnd_listener: Fix behavior when there are multiple listeners (#87)
A `dnd_listener` widget shouldn't handle a DnD event when the dnd drag
isn't within the widget's bounds. So add a few more checks for this.
Enter/leave events generated by `DndOfferEvent::Motion` also don't
behave as one might expect, since the enter may occur before the leave
depending on the order it calls `on_event` on the widget. Not sure how
to address that, but cosmic-workspaces can just ignore the leave events
for now.
Otherwise, this seems to be working fine, after these changes.
chore: fix sctk multi-window dependency
cleanup: formatting and clippy
fix(example): sctk_drag id
fix: translate the wayland event position for content inside a scrollable
fix: set web-sys to =0.3.64
fix: clip mask checks
chore: use advanced text shaping for pick list
fix: dnd widget layout
fix: ambiguous palette import
chore: remove artifacts job
fix: CI tests
fix: add back the window id to the frames subscription
fix: tooltip children and diff
refactor: udpate gradient angles for slider
reexport limits
fix: editor and sctk_todos examples
cleanup: clippy
cleanup git workflows
chore: cleanup iced_widget
refactor
Update mod.rs
chore: update softbuffer
Hack to remove image blur
iced_core: feature for serde serialization of KeyCode
fix(wgpu): handle border_radius property with image raster
feat: add border radius to image rendering
feat: Add side mouse button events
cleanup: clippy fixes and formatting
Part of this is a refactor of the ID
cleanup: clippy and fmt
fix: test workflow
fix: add note in CHANGELOG
fix: clippy
refactor: restore default style of slider
feat: allow setting the width and height of a rule
fix: slider gradient angle
feat: gradient backgground for the slider rail
feat(mouse-area): added on_drag method
fix(widget): container inherited wrong icon color from renderer
fix(button): inherit icon color if set to none
feat(renderer): define default icon color
By default, this is the same as the text color for best visibility.
feat(winit): client-side resize drag support
feat(winit): client-side resize drag support
Make vertical scroll properties optional
fix: quad rendering including border only inside of the bounds
Move `Screenshot` inside `window` module
Added offscreen rendering support for wgpu & tiny-skia exposed with the window::screenshot command.
Provide access to font from each crate
Use nested for lazy widgets
Use layout with children for nesting
Introduce internal `overlay::Nested` for `UserInterface`
fix: reset button state if the cursor leaves
runtime: Handle widget operations in `program::State` helper (#46)
chore: default line height, text size, and shaping for cosmic
feat: sctk shell
fix: quad rendering including border only inside of the bounds
fix: better slider drawing (it allows just the border part of the handle quad outside of the layout bouds, which isn't great, but is ok for our purposes due to being transparent)
cleanup: fix & format
fix: use iced_core::Font
cleanup
fix: allow leaving out winit & iced-sctk
fix: settings
fix: slider draw improvements
fix: websocket example
fix: modal example
fix: scrollable example
fix: toast example
fix: avoid panicking in iced_sctk with lazy widgets in auto-size surfaces
fix: todos panic
fix: only diff auto-sized surfaces in iced_sctk build_user_interface & improve sctk examples
wip (iced-sctk): window resize with icons
feat (iced-sctk): support for setting cursor
refactor: default decorations to client
fix: set window geometry after receiving configure
fix: size limits with no max bound must be cut off
fix: send size update when autosized surface resizes
fix: use ceil size for positioner
cleanup: remove dbg statement
fix: remove a destroyed surface from compositor surfaces
fix errors after rebase and wip scaling support
fix: handling of scale factor in set_logical_size
fix (sctk_drag example): add .into for border radius
fix: fractional scaling
sctk: Fire RedrawRequests
wip: animations via frame event
fix / refactor iced-sctk redraw & frame event handling
cleanup: note about frame request in iced-sctk
fix: send resize when necessary for layer surface and popups too
fix: always request redraw for a new surface
fix: scaling and autosize surface improvements
refactor: sctk_lazy keyboard interactivity
feat(sctk): configurable natural_scroll property
feat: send state and capabilities events when there are changes
fix: redraw when an update is needed and clean up the logic
Update sctk to latest commit
Fix compilation of sctk drag example
fix(sctk): update interface before checking if it has a redraw request
refactor: after autosize surface resize wait to redraw until the resize has been applied
refactor: better handling of autosize surfaces
chore: update sctk
chore: update sctk
fixes sctk_drag example
fix: default to ControlFlow::Wait for applications with no surface
this seems to help CPU usage for app library and launcher
default to 250ms timeout in the event loop
Update sctk
sctk: Implement xdg-activation support
fix: don't require Flags to be clone for settings on wayland
chore: error if neither winit or wayland feature is set
chore: Allow compiling without windowing system (#65)
fix(iced-sctk): handle exit_on_close_request
fix: make sure that each widget operation operates on every interface
This should be ok even for widget actions like focus next because there can only ever be a single focused widget
cargo fmt
cleanup: dbg statement
fix(iced-sctk): replace panic with handling for remaining enum variants
refactor: use iced clipboard for interacting with the selection
refactor: allow passing an activation token when creating a window
sctk: Add support for `ext-session-lock` protocol
fix(sctk): build and use tree for layout of autosize surfaces
Update winit to latest commit used by upstream iced
fix(sctk): send key characters
fix(sctk): check if key is a named key first
refactor(sctk): keep compositor surface in state
feat: accessibility with some widget impls
feat: stable ids
a11y: Don't unconditionally pull winit (#43)
Update conversion.rs
integration fixes
integration
integration
integration
integration
s
integration
some integration work
more integration
Update Cargo.toml
Update mod.rs
Update multi_window.rs
s
more integration
more integration (ryanabx wip #100000)
more integration!!
integration 2
integration
more integration (rbx)
s
integration
integration work
integration
Update Cargo.toml
integration
simple integration things
int
integration to 175
integration(170)
Co-Authored-By: Ashley Wulber <48420062+wash2@users.noreply.github.com>
Co-Authored-By: Victoria Brekenfeld <4404502+Drakulix@users.noreply.github.com>
Co-Authored-By: Eduardo Flores <edfloreshz@gmail.com>
Co-Authored-By: Michael Murphy <michael@mmurphy.dev>
Co-Authored-By: wiiznokes <78230769+wiiznokes@users.noreply.github.com>
Co-Authored-By: Jeremy Soller <jeremy@system76.com>
Co-Authored-By: Ryan Brue <56272643+ryanabx@users.noreply.github.com>
fix(column): handle keys len change
fix: iced-sctk a11y
wip: winit single window updates
tokio feature hangs even without a11y feature
fix: multiwindow a11y fixes
fix: component
update winit
wip: sctk integration to winit shell
refactor: remove accesskit_unix
fix: svg
fix: remove wayland default feature
feat: derive Hash for image Handle
fix: cleanup 0.13 rebase errors
fix: remove path dependencies
conversion for Radius
conversion for Padding
setter for Svg border radius
re-export Limits
fix: connect clipboard if disconnected on layer surface or popup creation
fix: connect clipboard if disconnected on session lock surface creation
fix: update size of layer surface after configure
fix: insert user interfaces for popup and lock surfaces on creation
fix: svg scaling
feat: popups on winit windows
fix: default text shaping to advanced
fix: fallback to renderer icon style if svg is symbolic
fixes
fix: sctk frame handling
feat: autosize handling
fix: better autosize handling
fix: avoid duplicate window events from sctk
fix: better handling of popups
fix: refactor redraw handling for sctk
fix: include id in frames
fix: image
fix: scrollable delta direction
sctk: unregister clipboard when surface is done
set min / max size when size is requested
fix: popups
filter pointer events
feat: add Hide variant to mouse Interaction
dnd fixes
fix: use physical width for DnD surface
fix: tiny-skia svg quality
refactor: peek_dnd try to parse data
cleanup text conversion
cleanup
svg scaling fixes
background color fix
Introduce consecutive_click_distance like other toolkits do such as gtk,qt,imgui.
This commit is contained in:
parent
595af03a9f
commit
08fe1f3aa5
233 changed files with 24391 additions and 1911 deletions
|
|
@ -28,15 +28,23 @@ markdown = ["dep:pulldown-cmark"]
|
|||
highlighter = ["dep:iced_highlighter"]
|
||||
advanced = []
|
||||
crisp = []
|
||||
a11y = ["iced_accessibility"]
|
||||
wayland = ["sctk", "iced_runtime/wayland"]
|
||||
|
||||
[dependencies]
|
||||
iced_renderer.workspace = true
|
||||
|
||||
iced_runtime.workspace = true
|
||||
iced_accessibility.workspace = true
|
||||
iced_accessibility.optional = true
|
||||
sctk.workspace = true
|
||||
sctk.optional = true
|
||||
num-traits.workspace = true
|
||||
log.workspace = true
|
||||
rustc-hash.workspace = true
|
||||
thiserror.workspace = true
|
||||
unicode-segmentation.workspace = true
|
||||
window_clipboard.workspace = true
|
||||
dnd.workspace = true
|
||||
|
||||
ouroboros.workspace = true
|
||||
ouroboros.optional = true
|
||||
|
|
|
|||
|
|
@ -16,6 +16,13 @@
|
|||
//! button("Press me!").on_press(Message::ButtonPressed).into()
|
||||
//! }
|
||||
//! ```
|
||||
//! Allow your users to perform actions by pressing a button.
|
||||
use iced_runtime::core::border::Radius;
|
||||
use iced_runtime::core::widget::Id;
|
||||
use iced_runtime::{Task, keyboard, task};
|
||||
#[cfg(feature = "a11y")]
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::core::border::{self, Border};
|
||||
use crate::core::layout;
|
||||
use crate::core::mouse;
|
||||
|
|
@ -31,6 +38,8 @@ use crate::core::{
|
|||
Rectangle, Shadow, Shell, Size, Theme, Vector, Widget,
|
||||
};
|
||||
|
||||
use iced_renderer::core::widget::operation;
|
||||
|
||||
/// A generic widget that produces a message when pressed.
|
||||
///
|
||||
/// # Example
|
||||
|
|
@ -75,6 +84,13 @@ where
|
|||
{
|
||||
content: Element<'a, Message, Theme, Renderer>,
|
||||
on_press: Option<OnPress<'a, Message>>,
|
||||
id: Id,
|
||||
#[cfg(feature = "a11y")]
|
||||
name: Option<Cow<'a, str>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: Option<iced_accessibility::Description<'a>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
|
||||
width: Length,
|
||||
height: Length,
|
||||
padding: Padding,
|
||||
|
|
@ -111,6 +127,13 @@ where
|
|||
|
||||
Button {
|
||||
content,
|
||||
id: Id::unique(),
|
||||
#[cfg(feature = "a11y")]
|
||||
name: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
label: None,
|
||||
on_press: None,
|
||||
width: size.width.fluid(),
|
||||
height: size.height.fluid(),
|
||||
|
|
@ -196,11 +219,54 @@ where
|
|||
self.class = class.into();
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the [`Id`] of the [`Button`].
|
||||
pub fn id(mut self, id: Id) -> Self {
|
||||
self.id = id;
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the name of the [`Button`].
|
||||
pub fn name(mut self, name: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.name = Some(name.into());
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description_widget<T: iced_accessibility::Describes>(
|
||||
mut self,
|
||||
description: &T,
|
||||
) -> Self {
|
||||
self.description = Some(iced_accessibility::Description::Id(
|
||||
description.description(),
|
||||
));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description(mut self, description: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.description =
|
||||
Some(iced_accessibility::Description::Text(description.into()));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the label of the [`Button`].
|
||||
pub fn label(mut self, label: &dyn iced_accessibility::Labels) -> Self {
|
||||
self.label =
|
||||
Some(label.label().into_iter().map(|l| l.into()).collect());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
struct State {
|
||||
is_hovered: bool,
|
||||
is_pressed: bool,
|
||||
is_focused: bool,
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||
|
|
@ -222,8 +288,8 @@ where
|
|||
vec![Tree::new(&self.content)]
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
tree.diff_children(std::slice::from_ref(&self.content));
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
tree.diff_children(std::slice::from_mut(&mut self.content))
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
@ -331,9 +397,44 @@ where
|
|||
}
|
||||
}
|
||||
}
|
||||
Event::Touch(touch::Event::FingerLost { .. }) => {
|
||||
#[cfg(feature = "a11y")]
|
||||
Event::A11y(
|
||||
event_id,
|
||||
iced_accessibility::accesskit::ActionRequest { action, .. },
|
||||
) => {
|
||||
let state = tree.state.downcast_mut::<State>();
|
||||
|
||||
if let Some(Some(on_press)) = (self.id == *event_id
|
||||
&& matches!(
|
||||
action,
|
||||
iced_accessibility::accesskit::Action::Click
|
||||
))
|
||||
.then(|| self.on_press.as_ref())
|
||||
{
|
||||
state.is_pressed = false;
|
||||
shell.publish(on_press.get());
|
||||
}
|
||||
return;
|
||||
}
|
||||
Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => {
|
||||
if let Some(on_press) = self.on_press.as_ref() {
|
||||
let state = tree.state.downcast_mut::<State>();
|
||||
if state.is_focused
|
||||
&& matches!(
|
||||
key,
|
||||
keyboard::Key::Named(keyboard::key::Named::Enter)
|
||||
)
|
||||
{
|
||||
state.is_pressed = true;
|
||||
shell.publish(on_press.get());
|
||||
shell.capture_event();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::Touch(touch::Event::FingerLost { .. })
|
||||
| Event::Mouse(mouse::Event::CursorLeft) => {
|
||||
let state = tree.state.downcast_mut::<State>();
|
||||
state.is_hovered = false;
|
||||
state.is_pressed = false;
|
||||
}
|
||||
_ => {}
|
||||
|
|
@ -365,7 +466,7 @@ where
|
|||
tree: &Tree,
|
||||
renderer: &mut Renderer,
|
||||
theme: &Theme,
|
||||
_style: &renderer::Style,
|
||||
renderer_style: &renderer::Style,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
viewport: &Rectangle,
|
||||
|
|
@ -404,6 +505,10 @@ where
|
|||
theme,
|
||||
&renderer::Style {
|
||||
text_color: style.text_color,
|
||||
icon_color: style
|
||||
.icon_color
|
||||
.unwrap_or(renderer_style.icon_color),
|
||||
scale_factor: renderer_style.scale_factor,
|
||||
},
|
||||
content_layout,
|
||||
cursor,
|
||||
|
|
@ -444,6 +549,90 @@ where
|
|||
translation,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// get the a11y nodes for the widget
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
state: &Tree,
|
||||
p: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
use iced_accessibility::{
|
||||
A11yNode, A11yTree,
|
||||
accesskit::{Action, Node, NodeId, Rect, Role},
|
||||
};
|
||||
|
||||
let child_layout = layout.children().next().unwrap();
|
||||
let child_tree = &state.children[0];
|
||||
let child_tree =
|
||||
self.content
|
||||
.as_widget()
|
||||
.a11y_nodes(child_layout, child_tree, p);
|
||||
|
||||
let Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
} = layout.bounds();
|
||||
let bounds = Rect::new(
|
||||
x as f64,
|
||||
y as f64,
|
||||
(x + width) as f64,
|
||||
(y + height) as f64,
|
||||
);
|
||||
let is_hovered = state.state.downcast_ref::<State>().is_hovered;
|
||||
|
||||
let mut node = Node::new(Role::Button);
|
||||
node.add_action(Action::Focus);
|
||||
node.add_action(Action::Click);
|
||||
node.set_bounds(bounds);
|
||||
if let Some(name) = self.name.as_ref()
|
||||
&& self.label.as_ref().is_none_or(|l| l.is_empty())
|
||||
{
|
||||
node.set_label(name.clone());
|
||||
}
|
||||
match self.description.as_ref() {
|
||||
Some(iced_accessibility::Description::Id(id)) => {
|
||||
node.set_described_by(
|
||||
id.iter()
|
||||
.cloned()
|
||||
.map(|id| NodeId::from(id))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
Some(iced_accessibility::Description::Text(text)) => {
|
||||
node.set_description(text.clone());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
if let Some(label) = self.label.as_ref() {
|
||||
node.set_labelled_by(label.clone());
|
||||
}
|
||||
|
||||
if self.on_press.is_none() {
|
||||
node.set_disabled()
|
||||
}
|
||||
// TODO hover
|
||||
// if is_hovered {
|
||||
// node.set_busy()
|
||||
// }
|
||||
|
||||
A11yTree::node_with_child_tree(
|
||||
A11yNode::new(node, self.id.clone()),
|
||||
child_tree,
|
||||
)
|
||||
}
|
||||
|
||||
fn id(&self) -> Option<Id> {
|
||||
Some(self.id.clone())
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
self.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> From<Button<'a, Message, Theme, Renderer>>
|
||||
|
|
@ -487,6 +676,14 @@ pub enum Status {
|
|||
pub struct Style {
|
||||
/// The [`Background`] of the button.
|
||||
pub background: Option<Background>,
|
||||
/// The border radius of the button.
|
||||
pub border_radius: Radius,
|
||||
/// The border width of the button.
|
||||
pub border_width: f32,
|
||||
/// The border [`Color`] of the button.
|
||||
pub border_color: Color,
|
||||
/// The icon [`Color`] of the button.
|
||||
pub icon_color: Option<Color>,
|
||||
/// The text [`Color`] of the button.
|
||||
pub text_color: Color,
|
||||
/// The [`Border`] of the button.
|
||||
|
|
@ -505,12 +702,36 @@ impl Style {
|
|||
..self
|
||||
}
|
||||
}
|
||||
|
||||
// /// Returns whether the [`Button`] is currently focused or not.
|
||||
// pub fn is_focused(&self) -> bool {
|
||||
// self.is_focused
|
||||
// }
|
||||
|
||||
// /// Returns whether the [`Button`] is currently hovered or not.
|
||||
// pub fn is_hovered(&self) -> bool {
|
||||
// self.is_hovered
|
||||
// }
|
||||
|
||||
// /// Focuses the [`Button`].
|
||||
// pub fn focus(&mut self) {
|
||||
// self.is_focused = true;
|
||||
// }
|
||||
|
||||
// /// Unfocuses the [`Button`].
|
||||
// pub fn unfocus(&mut self) {
|
||||
// self.is_focused = false;
|
||||
// }
|
||||
}
|
||||
|
||||
impl Default for Style {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
background: None,
|
||||
border_radius: 0.0.into(),
|
||||
border_width: 0.0,
|
||||
border_color: Color::TRANSPARENT,
|
||||
icon_color: None,
|
||||
text_color: Color::BLACK,
|
||||
border: Border::default(),
|
||||
shadow: Shadow::default(),
|
||||
|
|
@ -750,3 +971,22 @@ fn disabled(style: Style) -> Style {
|
|||
..style
|
||||
}
|
||||
}
|
||||
|
||||
/// Produces a [`Command`] that focuses the [`Button`] with the given [`Id`].
|
||||
pub fn focus<Message: 'static + Send>(id: Id) -> Task<Message> {
|
||||
task::widget(operation::focusable::focus(id))
|
||||
}
|
||||
|
||||
impl operation::Focusable for State {
|
||||
fn is_focused(&self) -> bool {
|
||||
self.is_focused
|
||||
}
|
||||
|
||||
fn focus(&mut self) {
|
||||
self.is_focused = true;
|
||||
}
|
||||
|
||||
fn unfocus(&mut self) {
|
||||
self.is_focused = false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,11 @@
|
|||
//! }
|
||||
//! ```
|
||||
//! 
|
||||
//! Show toggle controls using checkboxes.
|
||||
use iced_runtime::core::widget::Id;
|
||||
#[cfg(feature = "a11y")]
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::core::alignment;
|
||||
use crate::core::layout;
|
||||
use crate::core::mouse;
|
||||
|
|
@ -43,7 +48,7 @@ use crate::core::widget::tree::{self, Tree};
|
|||
use crate::core::window;
|
||||
use crate::core::{
|
||||
Background, Border, Clipboard, Color, Element, Event, Layout, Length,
|
||||
Pixels, Rectangle, Shell, Size, Theme, Widget,
|
||||
Pixels, Rectangle, Shell, Size, Theme, Widget, id::Internal,
|
||||
};
|
||||
|
||||
/// A box that can be checked.
|
||||
|
|
@ -88,6 +93,12 @@ pub struct Checkbox<
|
|||
Renderer: text::Renderer,
|
||||
Theme: Catalog,
|
||||
{
|
||||
id: Id,
|
||||
label_id: Id,
|
||||
#[cfg(feature = "a11y")]
|
||||
name: Option<Cow<'a, str>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: Option<iced_accessibility::Description<'a>>,
|
||||
is_checked: bool,
|
||||
on_toggle: Option<Box<dyn Fn(bool) -> Message + 'a>>,
|
||||
label: Option<text::Fragment<'a>>,
|
||||
|
|
@ -118,6 +129,12 @@ where
|
|||
/// * a boolean describing whether the [`Checkbox`] is checked or not
|
||||
pub fn new(is_checked: bool) -> Self {
|
||||
Checkbox {
|
||||
id: Id::unique(),
|
||||
label_id: Id::unique(),
|
||||
#[cfg(feature = "a11y")]
|
||||
name: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: None,
|
||||
is_checked,
|
||||
on_toggle: None,
|
||||
label: None,
|
||||
|
|
@ -134,7 +151,8 @@ where
|
|||
code_point: Renderer::CHECKMARK_ICON,
|
||||
size: None,
|
||||
line_height: text::LineHeight::default(),
|
||||
shaping: text::Shaping::Basic,
|
||||
shaping: text::Shaping::Advanced,
|
||||
wrap: text::Wrapping::default(),
|
||||
},
|
||||
class: Theme::default(),
|
||||
last_status: None,
|
||||
|
|
@ -248,6 +266,33 @@ where
|
|||
self.class = class.into();
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the name of the [`Button`].
|
||||
pub fn name(mut self, name: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.name = Some(name.into());
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description_widget<T: iced_accessibility::Describes>(
|
||||
mut self,
|
||||
description: &T,
|
||||
) -> Self {
|
||||
self.description = Some(iced_accessibility::Description::Id(
|
||||
description.description(),
|
||||
));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description(mut self, description: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.description =
|
||||
Some(iced_accessibility::Description::Text(description.into()));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||
|
|
@ -416,6 +461,7 @@ where
|
|||
size,
|
||||
line_height,
|
||||
shaping,
|
||||
wrap,
|
||||
} = &self.icon;
|
||||
let size = size.unwrap_or(Pixels(bounds.height * 0.7));
|
||||
|
||||
|
|
@ -472,6 +518,89 @@ where
|
|||
operation.text(None, layout.bounds(), label);
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "a11y")]
|
||||
/// get the a11y nodes for the widget
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
_state: &Tree,
|
||||
cursor: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
use iced_accessibility::{
|
||||
A11yNode, A11yTree,
|
||||
accesskit::{Action, Node, NodeId, Rect, Role},
|
||||
};
|
||||
|
||||
let bounds = layout.bounds();
|
||||
let is_hovered = cursor.is_over(bounds);
|
||||
let Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
} = bounds;
|
||||
|
||||
let bounds = Rect::new(
|
||||
x as f64,
|
||||
y as f64,
|
||||
(x + width) as f64,
|
||||
(y + height) as f64,
|
||||
);
|
||||
|
||||
let mut node = Node::new(Role::CheckBox);
|
||||
node.add_action(Action::Focus);
|
||||
node.add_action(Action::Click);
|
||||
node.set_bounds(bounds);
|
||||
if let Some(name) = self.name.as_ref() {
|
||||
node.set_label(name.clone());
|
||||
}
|
||||
match self.description.as_ref() {
|
||||
Some(iced_accessibility::Description::Id(id)) => {
|
||||
node.set_described_by(
|
||||
id.iter()
|
||||
.cloned()
|
||||
.map(|id| NodeId::from(id))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
Some(iced_accessibility::Description::Text(text)) => {
|
||||
node.set_description(text.clone());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
node.set_selected(self.is_checked);
|
||||
// TODO hover
|
||||
// if is_hovered {
|
||||
// node.set_hovered();
|
||||
// }
|
||||
node.add_action(Action::Click);
|
||||
let mut label_node = Node::new(Role::Label);
|
||||
if let Some(label) = self.label.as_ref() {
|
||||
label_node.set_label(label.clone());
|
||||
}
|
||||
// TODO proper label bounds
|
||||
label_node.set_bounds(bounds);
|
||||
|
||||
A11yTree::node_with_child_tree(
|
||||
A11yNode::new(node, self.id.clone()),
|
||||
A11yTree::leaf(label_node, self.label_id.clone()),
|
||||
)
|
||||
}
|
||||
fn id(&self) -> Option<Id> {
|
||||
Some(Id(Internal::Set(vec![
|
||||
self.id.0.clone(),
|
||||
self.label_id.0.clone(),
|
||||
])))
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
if let Id(Internal::Set(list)) = id {
|
||||
if list.len() == 2 {
|
||||
self.id.0 = list[0].clone();
|
||||
self.label_id.0 = list[1].clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> From<Checkbox<'a, Message, Theme, Renderer>>
|
||||
|
|
@ -501,6 +630,8 @@ pub struct Icon<Font> {
|
|||
pub line_height: text::LineHeight,
|
||||
/// The shaping strategy of the icon.
|
||||
pub shaping: text::Shaping,
|
||||
/// The wrap mode of the icon.
|
||||
pub wrap: text::Wrapping,
|
||||
}
|
||||
|
||||
/// The possible status of a [`Checkbox`].
|
||||
|
|
|
|||
|
|
@ -204,8 +204,8 @@ where
|
|||
self.children.iter().map(Tree::new).collect()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
tree.diff_children(&self.children);
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
tree.diff_children(self.children.as_mut_slice());
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
@ -351,6 +351,48 @@ where
|
|||
translation,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// get the a11y nodes for the widget
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
state: &Tree,
|
||||
cursor: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
use iced_accessibility::A11yTree;
|
||||
A11yTree::join(
|
||||
self.children
|
||||
.iter()
|
||||
.zip(layout.children())
|
||||
.zip(state.children.iter())
|
||||
.map(|((c, c_layout), state)| {
|
||||
c.as_widget().a11y_nodes(c_layout, state, cursor)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
fn drag_destinations(
|
||||
&self,
|
||||
state: &Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
dnd_rectangles: &mut crate::core::clipboard::DndDestinationRectangles,
|
||||
) {
|
||||
for ((e, layout), state) in self
|
||||
.children
|
||||
.iter()
|
||||
.zip(layout.children())
|
||||
.zip(state.children.iter())
|
||||
{
|
||||
e.as_widget().drag_destinations(
|
||||
state,
|
||||
layout,
|
||||
renderer,
|
||||
dnd_rectangles,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> From<Column<'a, Message, Theme, Renderer>>
|
||||
|
|
@ -406,7 +448,7 @@ where
|
|||
self.column.children()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
self.column.diff(tree);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -546,7 +546,7 @@ where
|
|||
vec![widget::Tree::new(&self.text_input as &dyn Widget<_, _, _>)]
|
||||
}
|
||||
|
||||
fn diff(&self, _tree: &mut widget::Tree) {
|
||||
fn diff(&mut self, _tree: &mut widget::Tree) {
|
||||
// do nothing so the children don't get cleared
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,13 +28,15 @@ use crate::core::overlay;
|
|||
use crate::core::renderer;
|
||||
use crate::core::theme;
|
||||
use crate::core::widget::tree::{self, Tree};
|
||||
use crate::core::widget::{self, Operation};
|
||||
use crate::core::widget::{self, Id, Operation};
|
||||
use crate::core::{
|
||||
self, Background, Clipboard, Color, Element, Event, Layout, Length,
|
||||
Padding, Pixels, Rectangle, Shadow, Shell, Size, Theme, Vector, Widget,
|
||||
color,
|
||||
};
|
||||
|
||||
use iced_runtime::{Action, Task, task};
|
||||
|
||||
/// A widget that aligns its contents inside of its boundaries.
|
||||
///
|
||||
/// # Example
|
||||
|
|
@ -245,8 +247,8 @@ where
|
|||
self.content.as_widget().children()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
self.content.as_widget().diff(tree);
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
self.content.as_widget_mut().diff(tree);
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
@ -356,9 +358,13 @@ where
|
|||
renderer,
|
||||
theme,
|
||||
&renderer::Style {
|
||||
icon_color: style
|
||||
.icon_color
|
||||
.unwrap_or(renderer_style.icon_color),
|
||||
text_color: style
|
||||
.text_color
|
||||
.unwrap_or(renderer_style.text_color),
|
||||
scale_factor: renderer_style.scale_factor,
|
||||
},
|
||||
layout.children().next().unwrap(),
|
||||
cursor,
|
||||
|
|
@ -387,6 +393,49 @@ where
|
|||
translation,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// get the a11y nodes for the widget
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
state: &Tree,
|
||||
cursor: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
let c_layout = layout.children().next().unwrap();
|
||||
let c_state = state.children.get(0);
|
||||
|
||||
self.content.as_widget().a11y_nodes(
|
||||
c_layout,
|
||||
c_state.unwrap_or(&Tree::empty()),
|
||||
cursor,
|
||||
)
|
||||
}
|
||||
|
||||
fn drag_destinations(
|
||||
&self,
|
||||
state: &Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
dnd_rectangles: &mut crate::core::clipboard::DndDestinationRectangles,
|
||||
) {
|
||||
if let Some(l) = layout.children().next() {
|
||||
self.content.as_widget().drag_destinations(
|
||||
state,
|
||||
l,
|
||||
renderer,
|
||||
dnd_rectangles,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn id(&self) -> Option<Id> {
|
||||
self.content.as_widget().id().clone()
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
self.content.as_widget_mut().set_id(id);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> From<Container<'a, Message, Theme, Renderer>>
|
||||
|
|
@ -457,9 +506,104 @@ pub fn draw_background<Renderer>(
|
|||
}
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that queries the visible screen bounds of the
|
||||
/// [`Container`] with the given [`Id`].
|
||||
pub fn visible_bounds(id: Id) -> Task<Option<Rectangle>> {
|
||||
struct VisibleBounds {
|
||||
target: widget::Id,
|
||||
depth: usize,
|
||||
scrollables: Vec<(Vector, Rectangle, usize)>,
|
||||
bounds: Option<Rectangle>,
|
||||
}
|
||||
|
||||
impl Operation<Option<Rectangle>> for VisibleBounds {
|
||||
fn scrollable(
|
||||
&mut self,
|
||||
_id: Option<&widget::Id>,
|
||||
bounds: Rectangle,
|
||||
_content_bounds: Rectangle,
|
||||
translation: Vector,
|
||||
_state: &mut dyn widget::operation::Scrollable,
|
||||
) {
|
||||
match self.scrollables.last() {
|
||||
Some((last_translation, last_viewport, _depth)) => {
|
||||
let viewport = last_viewport
|
||||
.intersection(&(bounds - *last_translation))
|
||||
.unwrap_or(Rectangle::new(
|
||||
crate::core::Point::ORIGIN,
|
||||
Size::ZERO,
|
||||
));
|
||||
|
||||
self.scrollables.push((
|
||||
translation + *last_translation,
|
||||
viewport,
|
||||
self.depth,
|
||||
));
|
||||
}
|
||||
None => {
|
||||
self.scrollables.push((translation, bounds, self.depth));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn container(&mut self, id: Option<&Id>, bounds: Rectangle) {
|
||||
if self.bounds.is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
if id == Some(&self.target) {
|
||||
match self.scrollables.last() {
|
||||
Some((translation, viewport, _)) => {
|
||||
self.bounds =
|
||||
viewport.intersection(&(bounds - *translation));
|
||||
}
|
||||
None => {
|
||||
self.bounds = Some(bounds);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
self.depth += 1;
|
||||
// TODO do we need to traverse here??
|
||||
self.depth -= 1;
|
||||
|
||||
match self.scrollables.last() {
|
||||
Some((_, _, depth)) if self.depth == *depth => {
|
||||
let _ = self.scrollables.pop();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn traverse(
|
||||
&mut self,
|
||||
operate: &mut dyn FnMut(&mut dyn Operation<Option<Rectangle>>),
|
||||
) {
|
||||
self.depth += 1;
|
||||
self.traverse(operate);
|
||||
self.depth -= 1;
|
||||
}
|
||||
|
||||
fn finish(&self) -> widget::operation::Outcome<Option<Rectangle>> {
|
||||
widget::operation::Outcome::Some(self.bounds)
|
||||
}
|
||||
}
|
||||
|
||||
task::widget(VisibleBounds {
|
||||
target: id.into(),
|
||||
depth: 0,
|
||||
scrollables: Vec::new(),
|
||||
bounds: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// The appearance of a container.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Style {
|
||||
/// The icon [`Color`] of the container.
|
||||
pub icon_color: Option<Color>,
|
||||
/// The text [`Color`] of the container.
|
||||
pub text_color: Option<Color>,
|
||||
/// The [`Background`] of the container.
|
||||
|
|
@ -475,6 +619,7 @@ pub struct Style {
|
|||
impl Default for Style {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
icon_color: None,
|
||||
text_color: None,
|
||||
background: None,
|
||||
border: Border::default(),
|
||||
|
|
@ -505,6 +650,8 @@ impl Style {
|
|||
pub fn background(self, background: impl Into<Background>) -> Self {
|
||||
Self {
|
||||
background: Some(background.into()),
|
||||
icon_color: None,
|
||||
text_color: None,
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
|
@ -584,6 +731,7 @@ pub fn rounded_box(theme: &Theme) -> Style {
|
|||
let palette = theme.extended_palette();
|
||||
|
||||
Style {
|
||||
icon_color: None,
|
||||
background: Some(palette.background.weak.color.into()),
|
||||
text_color: Some(palette.background.weak.text),
|
||||
border: border::rounded(2),
|
||||
|
|
@ -612,6 +760,7 @@ pub fn dark(_theme: &Theme) -> Style {
|
|||
style(theme::palette::Pair {
|
||||
color: color!(0x111111),
|
||||
text: Color::WHITE,
|
||||
icon: Color::WHITE,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -102,8 +102,8 @@ where
|
|||
self.content.as_widget().children()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut widget::Tree) {
|
||||
self.content.as_widget().diff(tree);
|
||||
fn diff(&mut self, tree: &mut widget::Tree) {
|
||||
self.content.as_widget_mut().diff(tree);
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
|
|||
|
|
@ -157,8 +157,8 @@ where
|
|||
self.children.iter().map(Tree::new).collect()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
tree.diff_children(&self.children);
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
tree.diff_children(&mut self.children);
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ use crate::{
|
|||
};
|
||||
|
||||
use std::borrow::Borrow;
|
||||
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
pub use crate::table::table;
|
||||
|
|
@ -609,8 +610,8 @@ where
|
|||
self.content.as_widget().children()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
self.content.as_widget().diff(tree);
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
self.content.as_widget_mut().diff(tree);
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
@ -772,8 +773,8 @@ where
|
|||
vec![Tree::new(&self.base), Tree::new(&self.top)]
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
tree.diff_children(&[&self.base, &self.top]);
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
tree.diff_children(&mut [&mut self.base, &mut self.top]);
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
@ -1809,7 +1810,10 @@ where
|
|||
/// ```
|
||||
/// <img src="https://github.com/iced-rs/iced/blob/9712b319bb7a32848001b96bd84977430f14b623/examples/resources/ferris.png?raw=true" width="300">
|
||||
#[cfg(feature = "image")]
|
||||
pub fn image<Handle>(handle: impl Into<Handle>) -> crate::Image<Handle> {
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
|
||||
pub fn image<'a, Handle>(
|
||||
handle: impl Into<Handle>,
|
||||
) -> crate::Image<'a, Handle> {
|
||||
crate::Image::new(handle.into())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
//! ```
|
||||
//! <img src="https://github.com/iced-rs/iced/blob/9712b319bb7a32848001b96bd84977430f14b623/examples/resources/ferris.png?raw=true" width="300">
|
||||
pub mod viewer;
|
||||
use iced_runtime::core::widget::Id;
|
||||
pub use viewer::Viewer;
|
||||
|
||||
use crate::core::border;
|
||||
|
|
@ -32,6 +33,9 @@ use crate::core::{
|
|||
|
||||
pub use image::{FilterMethod, Handle};
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// Creates a new [`Viewer`] with the given image `Handle`.
|
||||
pub fn viewer<Handle>(handle: Handle) -> Viewer<Handle> {
|
||||
Viewer::new(handle)
|
||||
|
|
@ -55,7 +59,15 @@ pub fn viewer<Handle>(handle: Handle) -> Viewer<Handle> {
|
|||
/// }
|
||||
/// ```
|
||||
/// <img src="https://github.com/iced-rs/iced/blob/9712b319bb7a32848001b96bd84977430f14b623/examples/resources/ferris.png?raw=true" width="300">
|
||||
pub struct Image<Handle = image::Handle> {
|
||||
#[derive(Debug)]
|
||||
pub struct Image<'a, Handle = image::Handle> {
|
||||
id: Id,
|
||||
#[cfg(feature = "a11y")]
|
||||
name: Option<Cow<'a, str>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: Option<iced_accessibility::Description<'a>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
|
||||
handle: Handle,
|
||||
width: Length,
|
||||
height: Length,
|
||||
|
|
@ -69,21 +81,28 @@ pub struct Image<Handle = image::Handle> {
|
|||
expand: bool,
|
||||
}
|
||||
|
||||
impl<Handle> Image<Handle> {
|
||||
impl<'a, Handle> Image<'a, Handle> {
|
||||
/// Creates a new [`Image`] with the given path.
|
||||
pub fn new(handle: impl Into<Handle>) -> Self {
|
||||
Image {
|
||||
id: Id::unique(),
|
||||
#[cfg(feature = "a11y")]
|
||||
name: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
label: None,
|
||||
handle: handle.into(),
|
||||
width: Length::Shrink,
|
||||
height: Length::Shrink,
|
||||
crop: None,
|
||||
border_radius: border::Radius::default(),
|
||||
content_fit: ContentFit::default(),
|
||||
filter_method: FilterMethod::default(),
|
||||
rotation: Rotation::default(),
|
||||
opacity: 1.0,
|
||||
scale: 1.0,
|
||||
expand: false,
|
||||
border_radius: [0.0; 4].into(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -179,6 +198,41 @@ impl<Handle> Image<Handle> {
|
|||
self.border_radius = border_radius.into();
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the name of the [`Button`].
|
||||
pub fn name(mut self, name: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.name = Some(name.into());
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description_widget<T: iced_accessibility::Describes>(
|
||||
mut self,
|
||||
description: &T,
|
||||
) -> Self {
|
||||
self.description = Some(iced_accessibility::Description::Id(
|
||||
description.description(),
|
||||
));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description(mut self, description: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.description =
|
||||
Some(iced_accessibility::Description::Text(description.into()));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the label of the [`Button`].
|
||||
pub fn label(mut self, label: &dyn iced_accessibility::Labels) -> Self {
|
||||
self.label =
|
||||
Some(label.label().into_iter().map(|l| l.into()).collect());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Computes the layout of an [`Image`].
|
||||
|
|
@ -192,6 +246,7 @@ pub fn layout<Renderer, Handle>(
|
|||
content_fit: ContentFit,
|
||||
rotation: Rotation,
|
||||
expand: bool,
|
||||
_border_radius: [f32; 4],
|
||||
) -> layout::Node
|
||||
where
|
||||
Renderer: image::Renderer<Handle = Handle>,
|
||||
|
|
@ -236,6 +291,8 @@ fn drawing_bounds<Renderer, Handle>(
|
|||
content_fit: ContentFit,
|
||||
rotation: Rotation,
|
||||
scale: f32,
|
||||
opacity: f32,
|
||||
border_radius: [f32; 4],
|
||||
) -> Rectangle
|
||||
where
|
||||
Renderer: image::Renderer<Handle = Handle>,
|
||||
|
|
@ -333,6 +390,8 @@ pub fn draw<Renderer, Handle>(
|
|||
content_fit,
|
||||
rotation,
|
||||
scale,
|
||||
opacity,
|
||||
border_radius.into(),
|
||||
);
|
||||
|
||||
renderer.draw_image(
|
||||
|
|
@ -349,8 +408,8 @@ pub fn draw<Renderer, Handle>(
|
|||
);
|
||||
}
|
||||
|
||||
impl<Message, Theme, Renderer, Handle> Widget<Message, Theme, Renderer>
|
||||
for Image<Handle>
|
||||
impl<'a, Message, Theme, Renderer, Handle> Widget<Message, Theme, Renderer>
|
||||
for Image<'a, Handle>
|
||||
where
|
||||
Renderer: image::Renderer<Handle = Handle>,
|
||||
Handle: Clone,
|
||||
|
|
@ -378,6 +437,7 @@ where
|
|||
self.content_fit,
|
||||
self.rotation,
|
||||
self.expand,
|
||||
self.border_radius.into(),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -404,15 +464,75 @@ where
|
|||
self.scale,
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
_state: &Tree,
|
||||
_cursor: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
use iced_accessibility::{
|
||||
A11yTree,
|
||||
accesskit::{Node, NodeId, Rect, Role},
|
||||
};
|
||||
|
||||
let bounds = layout.bounds();
|
||||
let Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
} = bounds;
|
||||
let bounds = Rect::new(
|
||||
x as f64,
|
||||
y as f64,
|
||||
(x + width) as f64,
|
||||
(y + height) as f64,
|
||||
);
|
||||
let mut node = Node::new(Role::Image);
|
||||
node.set_bounds(bounds);
|
||||
if let Some(name) = self.name.as_ref() {
|
||||
node.set_label(name.clone());
|
||||
}
|
||||
match self.description.as_ref() {
|
||||
Some(iced_accessibility::Description::Id(id)) => {
|
||||
node.set_described_by(
|
||||
id.iter()
|
||||
.cloned()
|
||||
.map(|id| NodeId::from(id))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
Some(iced_accessibility::Description::Text(text)) => {
|
||||
node.set_description(text.clone());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
if let Some(label) = self.label.as_ref() {
|
||||
node.set_labelled_by(label.clone());
|
||||
}
|
||||
|
||||
A11yTree::leaf(node, self.id.clone())
|
||||
}
|
||||
|
||||
fn id(&self) -> Option<Id> {
|
||||
Some(self.id.clone())
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
self.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer, Handle> From<Image<Handle>>
|
||||
impl<'a, Message, Theme, Renderer, Handle> From<Image<'a, Handle>>
|
||||
for Element<'a, Message, Theme, Renderer>
|
||||
where
|
||||
Renderer: image::Renderer<Handle = Handle>,
|
||||
Handle: Clone + 'a,
|
||||
{
|
||||
fn from(image: Image<Handle>) -> Element<'a, Message, Theme, Renderer> {
|
||||
fn from(image: Image<'a, Handle>) -> Element<'a, Message, Theme, Renderer> {
|
||||
Element::new(image)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
//! Keyed columns distribute content vertically while keeping continuity.
|
||||
//! Distribute content vertically.
|
||||
|
||||
use crate::core::event;
|
||||
use crate::core::layout;
|
||||
use crate::core::mouse;
|
||||
use crate::core::overlay;
|
||||
|
|
@ -221,7 +224,7 @@ where
|
|||
self.children.iter().map(Tree::new).collect()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
let Tree {
|
||||
state, children, ..
|
||||
} = tree;
|
||||
|
|
@ -230,11 +233,11 @@ where
|
|||
|
||||
tree::diff_children_custom_with_search(
|
||||
children,
|
||||
&self.children,
|
||||
|tree, child| child.as_widget().diff(tree),
|
||||
&mut self.children,
|
||||
|tree, child| child.as_widget_mut().diff(tree),
|
||||
|index| {
|
||||
self.keys.get(index).or_else(|| self.keys.last()).copied()
|
||||
!= Some(state.keys[index])
|
||||
!= state.keys.get(index).copied()
|
||||
},
|
||||
|child| Tree::new(child.as_widget()),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ pub mod component;
|
|||
|
||||
#[allow(deprecated)]
|
||||
pub use component::Component;
|
||||
use iced_renderer::core::widget::Operation;
|
||||
|
||||
mod cache;
|
||||
|
||||
|
|
@ -123,7 +124,7 @@ where
|
|||
self.with_element(|element| vec![Tree::new(element.as_widget())])
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
let current = tree
|
||||
.state
|
||||
.downcast_mut::<Internal<Message, Theme, Renderer>>();
|
||||
|
|
@ -142,8 +143,10 @@ where
|
|||
current.element = Rc::new(RefCell::new(Some(element)));
|
||||
|
||||
(*self.element.borrow_mut()) = Some(current.element.clone());
|
||||
self.with_element(|element| {
|
||||
tree.diff_children(std::slice::from_ref(&element.as_widget()));
|
||||
self.with_element_mut(|element| {
|
||||
tree.diff_children(std::slice::from_mut(
|
||||
&mut element.as_widget_mut(),
|
||||
))
|
||||
});
|
||||
} else {
|
||||
(*self.element.borrow_mut()) = Some(current.element.clone());
|
||||
|
|
@ -181,7 +184,7 @@ where
|
|||
tree: &mut Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
operation: &mut dyn widget::Operation,
|
||||
operation: &mut dyn crate::core::widget::Operation,
|
||||
) {
|
||||
self.with_element_mut(|element| {
|
||||
element.as_widget_mut().operate(
|
||||
|
|
@ -304,6 +307,40 @@ where
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn set_id(&mut self, _id: iced_runtime::core::id::Id) {
|
||||
if let Some(e) = self.element.borrow_mut().as_mut() {
|
||||
if let Some(e) = e.borrow_mut().as_mut() {
|
||||
e.as_widget_mut().set_id(_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn id(&self) -> Option<iced_runtime::core::id::Id> {
|
||||
if let Some(e) = self.element.borrow().as_ref() {
|
||||
if let Some(e) = e.borrow().as_ref() {
|
||||
return e.as_widget().id();
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn drag_destinations(
|
||||
&self,
|
||||
state: &Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
dnd_rectangles: &mut core::clipboard::DndDestinationRectangles,
|
||||
) {
|
||||
self.with_element(|element| {
|
||||
element.as_widget().drag_destinations(
|
||||
&state.children[0],
|
||||
layout,
|
||||
renderer,
|
||||
dnd_rectangles,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[self_referencing]
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use crate::core::{
|
|||
self, Clipboard, Element, Length, Rectangle, Shell, Size, Vector, Widget,
|
||||
};
|
||||
|
||||
use iced_renderer::core::widget::Operation;
|
||||
use ouroboros::self_referencing;
|
||||
use std::cell::RefCell;
|
||||
use std::marker::PhantomData;
|
||||
|
|
@ -146,13 +147,13 @@ where
|
|||
Renderer: renderer::Renderer,
|
||||
{
|
||||
fn diff_self(&self) {
|
||||
self.with_element(|element| {
|
||||
self.with_element_mut(|element| {
|
||||
self.tree
|
||||
.borrow_mut()
|
||||
.borrow_mut()
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.diff_children(std::slice::from_ref(&element));
|
||||
.diff_children(std::slice::from_mut(element));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -263,6 +264,7 @@ where
|
|||
|
||||
fn state(&self) -> tree::State {
|
||||
let state = Rc::new(RefCell::new(Some(Tree {
|
||||
id: None,
|
||||
tag: tree::Tag::of::<Tag<S>>(),
|
||||
state: tree::State::new(S::default()),
|
||||
children: vec![Tree::empty()],
|
||||
|
|
@ -278,7 +280,7 @@ where
|
|||
vec![]
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
let tree = tree.state.downcast_ref::<Rc<RefCell<Option<Tree>>>>();
|
||||
*self.tree.borrow_mut() = tree.clone();
|
||||
self.rebuild_element_if_necessary();
|
||||
|
|
@ -497,6 +499,49 @@ where
|
|||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
tree: &Tree,
|
||||
cursor: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
let tree = tree.state.downcast_ref::<Rc<RefCell<Option<Tree>>>>();
|
||||
self.with_element(|element| {
|
||||
if let Some(tree) = tree.borrow().as_ref() {
|
||||
element.as_widget().a11y_nodes(
|
||||
layout,
|
||||
&tree.children[0],
|
||||
cursor,
|
||||
)
|
||||
} else {
|
||||
iced_accessibility::A11yTree::default()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn drag_destinations(
|
||||
&self,
|
||||
tree: &Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
dnd_rectangles: &mut core::clipboard::DndDestinationRectangles,
|
||||
) {
|
||||
let mut tree = tree
|
||||
.state
|
||||
.downcast_ref::<Rc<RefCell<Option<Tree>>>>()
|
||||
.borrow_mut();
|
||||
let mut tree = tree.as_ref().unwrap();
|
||||
self.with_element(|element| {
|
||||
element.as_widget().drag_destinations(
|
||||
&tree.children[0],
|
||||
layout,
|
||||
renderer,
|
||||
dnd_rectangles,
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct Overlay<'a, 'b, Message, Theme, Renderer, Event, S>(
|
||||
|
|
|
|||
528
widget/src/lazy/responsive.rs
Normal file
528
widget/src/lazy/responsive.rs
Normal file
|
|
@ -0,0 +1,528 @@
|
|||
use crate::core::event::{self, Event};
|
||||
use crate::core::layout::{self, Layout};
|
||||
use crate::core::mouse;
|
||||
use crate::core::overlay;
|
||||
use crate::core::renderer;
|
||||
use crate::core::widget::tree::{self, Tree};
|
||||
use crate::core::{
|
||||
self, Clipboard, Element, Length, Point, Rectangle, Shell, Size, Vector,
|
||||
Widget,
|
||||
};
|
||||
use crate::horizontal_space;
|
||||
use crate::runtime::overlay::Nested;
|
||||
|
||||
use iced_renderer::core::widget::Operation;
|
||||
use ouroboros::self_referencing;
|
||||
use std::cell::{RefCell, RefMut};
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
|
||||
/// A widget that is aware of its dimensions.
|
||||
///
|
||||
/// A [`Responsive`] widget will always try to fill all the available space of
|
||||
/// its parent.
|
||||
#[cfg(feature = "lazy")]
|
||||
#[allow(missing_debug_implementations)]
|
||||
pub struct Responsive<
|
||||
'a,
|
||||
Message,
|
||||
Theme = crate::Theme,
|
||||
Renderer = crate::Renderer,
|
||||
> {
|
||||
view: Box<dyn Fn(Size) -> Element<'a, Message, Theme, Renderer> + 'a>,
|
||||
content: RefCell<Content<'a, Message, Theme, Renderer>>,
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> Responsive<'a, Message, Theme, Renderer>
|
||||
where
|
||||
Renderer: core::Renderer,
|
||||
{
|
||||
/// Creates a new [`Responsive`] widget with a closure that produces its
|
||||
/// contents.
|
||||
///
|
||||
/// The `view` closure will be provided with the current [`Size`] of
|
||||
/// the [`Responsive`] widget and, therefore, can be used to build the
|
||||
/// contents of the widget in a responsive way.
|
||||
pub fn new(
|
||||
view: impl Fn(Size) -> Element<'a, Message, Theme, Renderer> + 'a,
|
||||
) -> Self {
|
||||
Self {
|
||||
view: Box::new(view),
|
||||
content: RefCell::new(Content {
|
||||
size: Size::ZERO,
|
||||
layout: None,
|
||||
element: Element::new(horizontal_space().width(0)),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Content<'a, Message, Theme, Renderer> {
|
||||
size: Size,
|
||||
layout: Option<layout::Node>,
|
||||
element: Element<'a, Message, Theme, Renderer>,
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer>
|
||||
where
|
||||
Renderer: core::Renderer,
|
||||
{
|
||||
fn layout(&mut self, tree: &mut Tree, renderer: &Renderer) {
|
||||
if self.layout.is_none() {
|
||||
self.layout = Some(self.element.as_widget().layout(
|
||||
tree,
|
||||
renderer,
|
||||
&layout::Limits::new(Size::ZERO, self.size),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
fn update(
|
||||
&mut self,
|
||||
tree: &mut Tree,
|
||||
new_size: Size,
|
||||
view: &dyn Fn(Size) -> Element<'a, Message, Theme, Renderer>,
|
||||
) {
|
||||
if self.size == new_size {
|
||||
return;
|
||||
}
|
||||
|
||||
self.element = view(new_size);
|
||||
self.size = new_size;
|
||||
self.layout = None;
|
||||
|
||||
tree.diff(&mut self.element);
|
||||
}
|
||||
|
||||
fn resolve<R, T>(
|
||||
&mut self,
|
||||
tree: &mut Tree,
|
||||
renderer: R,
|
||||
layout: Layout<'_>,
|
||||
view: &dyn Fn(Size) -> Element<'a, Message, Theme, Renderer>,
|
||||
f: impl FnOnce(
|
||||
&mut Tree,
|
||||
R,
|
||||
Layout<'_>,
|
||||
&mut Element<'a, Message, Theme, Renderer>,
|
||||
) -> T,
|
||||
) -> T
|
||||
where
|
||||
R: Deref<Target = Renderer>,
|
||||
{
|
||||
self.update(tree, layout.bounds().size(), view);
|
||||
self.layout(tree, renderer.deref());
|
||||
|
||||
let content_layout = Layout::with_offset(
|
||||
layout.position() - Point::ORIGIN,
|
||||
self.layout.as_ref().unwrap(),
|
||||
);
|
||||
|
||||
f(tree, renderer, content_layout, &mut self.element)
|
||||
}
|
||||
}
|
||||
|
||||
struct State {
|
||||
tree: RefCell<Tree>,
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||
for Responsive<'a, Message, Theme, Renderer>
|
||||
where
|
||||
Renderer: core::Renderer,
|
||||
{
|
||||
fn tag(&self) -> tree::Tag {
|
||||
tree::Tag::of::<State>()
|
||||
}
|
||||
|
||||
fn state(&self) -> tree::State {
|
||||
tree::State::new(State {
|
||||
tree: RefCell::new(Tree::empty()),
|
||||
})
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
Size {
|
||||
width: Length::Fill,
|
||||
height: Length::Fill,
|
||||
}
|
||||
}
|
||||
|
||||
fn layout(
|
||||
&self,
|
||||
_tree: &mut Tree,
|
||||
_renderer: &Renderer,
|
||||
limits: &layout::Limits,
|
||||
) -> layout::Node {
|
||||
layout::Node::new(limits.max())
|
||||
}
|
||||
|
||||
fn operate(
|
||||
&self,
|
||||
tree: &mut Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
operation: &mut dyn crate::core::widget::Operation,
|
||||
) {
|
||||
let state = tree.state.downcast_mut::<State>();
|
||||
let mut content = self.content.borrow_mut();
|
||||
|
||||
content.resolve(
|
||||
&mut state.tree.borrow_mut(),
|
||||
renderer,
|
||||
layout,
|
||||
&self.view,
|
||||
|tree, renderer, layout, element| {
|
||||
element
|
||||
.as_widget()
|
||||
.operate(tree, layout, renderer, operation);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn on_event(
|
||||
&mut self,
|
||||
tree: &mut Tree,
|
||||
event: Event,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
renderer: &Renderer,
|
||||
clipboard: &mut dyn Clipboard,
|
||||
shell: &mut Shell<'_, Message>,
|
||||
viewport: &Rectangle,
|
||||
) -> event::Status {
|
||||
let state = tree.state.downcast_mut::<State>();
|
||||
let mut content = self.content.borrow_mut();
|
||||
|
||||
let mut local_messages = vec![];
|
||||
let mut local_shell = Shell::new(&mut local_messages);
|
||||
|
||||
let status = content.resolve(
|
||||
&mut state.tree.borrow_mut(),
|
||||
renderer,
|
||||
layout,
|
||||
&self.view,
|
||||
|tree, renderer, layout, element| {
|
||||
element.as_widget_mut().on_event(
|
||||
tree,
|
||||
event,
|
||||
layout,
|
||||
cursor,
|
||||
renderer,
|
||||
clipboard,
|
||||
&mut local_shell,
|
||||
viewport,
|
||||
)
|
||||
},
|
||||
);
|
||||
|
||||
if local_shell.is_layout_invalid() {
|
||||
content.layout = None;
|
||||
}
|
||||
|
||||
shell.merge(local_shell, std::convert::identity);
|
||||
|
||||
status
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
tree: &Tree,
|
||||
renderer: &mut Renderer,
|
||||
theme: &Theme,
|
||||
style: &renderer::Style,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
viewport: &Rectangle,
|
||||
) {
|
||||
let state = tree.state.downcast_ref::<State>();
|
||||
let mut content = self.content.borrow_mut();
|
||||
|
||||
content.resolve(
|
||||
&mut state.tree.borrow_mut(),
|
||||
renderer,
|
||||
layout,
|
||||
&self.view,
|
||||
|tree, renderer, layout, element| {
|
||||
element.as_widget().draw(
|
||||
tree, renderer, theme, style, layout, cursor, viewport,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn mouse_interaction(
|
||||
&self,
|
||||
tree: &Tree,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
viewport: &Rectangle,
|
||||
renderer: &Renderer,
|
||||
) -> mouse::Interaction {
|
||||
let state = tree.state.downcast_ref::<State>();
|
||||
let mut content = self.content.borrow_mut();
|
||||
|
||||
content.resolve(
|
||||
&mut state.tree.borrow_mut(),
|
||||
renderer,
|
||||
layout,
|
||||
&self.view,
|
||||
|tree, renderer, layout, element| {
|
||||
element
|
||||
.as_widget()
|
||||
.mouse_interaction(tree, layout, cursor, viewport, renderer)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn overlay<'b>(
|
||||
&'b mut self,
|
||||
tree: &'b mut Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
translation: Vector,
|
||||
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
|
||||
use std::ops::DerefMut;
|
||||
|
||||
let state = tree.state.downcast_ref::<State>();
|
||||
|
||||
let overlay = OverlayBuilder {
|
||||
content: self.content.borrow_mut(),
|
||||
tree: state.tree.borrow_mut(),
|
||||
types: PhantomData,
|
||||
overlay_builder: |content: &mut RefMut<
|
||||
'_,
|
||||
Content<'_, _, _, _>,
|
||||
>,
|
||||
tree| {
|
||||
content.update(tree, layout.bounds().size(), &self.view);
|
||||
content.layout(tree, renderer);
|
||||
|
||||
let Content {
|
||||
element,
|
||||
layout: content_layout_node,
|
||||
..
|
||||
} = content.deref_mut();
|
||||
|
||||
let content_layout = Layout::with_offset(
|
||||
layout.bounds().position() - Point::ORIGIN,
|
||||
content_layout_node.as_ref().unwrap(),
|
||||
);
|
||||
|
||||
(
|
||||
element
|
||||
.as_widget_mut()
|
||||
.overlay(tree, content_layout, renderer, translation)
|
||||
.map(|overlay| RefCell::new(Nested::new(overlay))),
|
||||
content_layout_node,
|
||||
)
|
||||
},
|
||||
}
|
||||
.build();
|
||||
|
||||
if overlay.with_overlay(|(overlay, _layout)| overlay.is_some()) {
|
||||
Some(overlay::Element::new(Box::new(overlay)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
tree: &Tree,
|
||||
cursor_position: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
use std::rc::Rc;
|
||||
|
||||
let tree = tree.state.downcast_ref::<Rc<RefCell<Option<Tree>>>>();
|
||||
if let Some(tree) = tree.borrow().as_ref() {
|
||||
self.content.borrow().element.as_widget().a11y_nodes(
|
||||
layout,
|
||||
&tree.children[0],
|
||||
cursor_position,
|
||||
)
|
||||
} else {
|
||||
iced_accessibility::A11yTree::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn id(&self) -> Option<core::widget::Id> {
|
||||
self.content.borrow().element.as_widget().id()
|
||||
}
|
||||
|
||||
fn set_id(&mut self, _id: iced_runtime::core::id::Id) {
|
||||
self.content
|
||||
.borrow_mut()
|
||||
.element
|
||||
.as_widget_mut()
|
||||
.set_id(_id);
|
||||
}
|
||||
|
||||
fn drag_destinations(
|
||||
&self,
|
||||
state: &Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
dnd_rectangles: &mut core::clipboard::DndDestinationRectangles,
|
||||
) {
|
||||
let ret = self.content.borrow_mut().resolve(
|
||||
&mut state.state.downcast_ref::<State>().tree.borrow_mut(),
|
||||
renderer,
|
||||
layout,
|
||||
&self.view,
|
||||
|tree, r, layout, element| {
|
||||
element.as_widget().drag_destinations(
|
||||
tree,
|
||||
layout,
|
||||
r,
|
||||
dnd_rectangles,
|
||||
);
|
||||
},
|
||||
);
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer>
|
||||
From<Responsive<'a, Message, Theme, Renderer>>
|
||||
for Element<'a, Message, Theme, Renderer>
|
||||
where
|
||||
Message: 'a,
|
||||
Theme: 'a,
|
||||
Renderer: core::Renderer + 'a,
|
||||
{
|
||||
fn from(responsive: Responsive<'a, Message, Theme, Renderer>) -> Self {
|
||||
Self::new(responsive)
|
||||
}
|
||||
}
|
||||
|
||||
#[self_referencing]
|
||||
struct Overlay<'a, 'b, Message, Theme, Renderer> {
|
||||
content: RefMut<'a, Content<'b, Message, Theme, Renderer>>,
|
||||
tree: RefMut<'a, Tree>,
|
||||
types: PhantomData<Message>,
|
||||
|
||||
#[borrows(mut content, mut tree)]
|
||||
#[not_covariant]
|
||||
overlay: (
|
||||
Option<RefCell<Nested<'this, Message, Theme, Renderer>>>,
|
||||
&'this mut Option<layout::Node>,
|
||||
),
|
||||
}
|
||||
|
||||
impl<'a, 'b, Message, Theme, Renderer>
|
||||
Overlay<'a, 'b, Message, Theme, Renderer>
|
||||
{
|
||||
fn with_overlay_maybe<T>(
|
||||
&self,
|
||||
f: impl FnOnce(&mut Nested<'_, Message, Theme, Renderer>) -> T,
|
||||
) -> Option<T> {
|
||||
self.with_overlay(|(overlay, _layout)| {
|
||||
overlay.as_ref().map(|nested| (f)(&mut nested.borrow_mut()))
|
||||
})
|
||||
}
|
||||
|
||||
fn with_overlay_mut_maybe<T>(
|
||||
&mut self,
|
||||
f: impl FnOnce(&mut Nested<'_, Message, Theme, Renderer>) -> T,
|
||||
) -> Option<T> {
|
||||
self.with_overlay_mut(|(overlay, _layout)| {
|
||||
overlay.as_mut().map(|nested| (f)(nested.get_mut()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, Message, Theme, Renderer>
|
||||
overlay::Overlay<Message, Theme, Renderer>
|
||||
for Overlay<'a, 'b, Message, Theme, Renderer>
|
||||
where
|
||||
Renderer: core::Renderer,
|
||||
{
|
||||
fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node {
|
||||
self.with_overlay_maybe(|overlay| overlay.layout(renderer, bounds))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
renderer: &mut Renderer,
|
||||
theme: &Theme,
|
||||
style: &renderer::Style,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
) {
|
||||
let _ = self.with_overlay_maybe(|overlay| {
|
||||
overlay.draw(renderer, theme, style, layout, cursor);
|
||||
});
|
||||
}
|
||||
|
||||
fn mouse_interaction(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
viewport: &Rectangle,
|
||||
renderer: &Renderer,
|
||||
) -> mouse::Interaction {
|
||||
self.with_overlay_maybe(|overlay| {
|
||||
overlay.mouse_interaction(layout, cursor, viewport, renderer)
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn on_event(
|
||||
&mut self,
|
||||
event: Event,
|
||||
layout: Layout<'_>,
|
||||
cursor: mouse::Cursor,
|
||||
renderer: &Renderer,
|
||||
clipboard: &mut dyn Clipboard,
|
||||
shell: &mut Shell<'_, Message>,
|
||||
) -> event::Status {
|
||||
let mut is_layout_invalid = false;
|
||||
|
||||
let event_status = self
|
||||
.with_overlay_mut_maybe(|overlay| {
|
||||
let event_status = overlay.on_event(
|
||||
event, layout, cursor, renderer, clipboard, shell,
|
||||
);
|
||||
|
||||
is_layout_invalid = shell.is_layout_invalid();
|
||||
|
||||
event_status
|
||||
})
|
||||
.unwrap_or(event::Status::Ignored);
|
||||
|
||||
if is_layout_invalid {
|
||||
self.with_overlay_mut(|(_overlay, layout)| {
|
||||
**layout = None;
|
||||
});
|
||||
}
|
||||
|
||||
event_status
|
||||
}
|
||||
|
||||
fn is_over(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
cursor_position: Point,
|
||||
) -> bool {
|
||||
self.with_overlay_maybe(|overlay| {
|
||||
overlay.is_over(layout, renderer, cursor_position)
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn operate(
|
||||
&mut self,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
operation: &mut dyn Operation,
|
||||
) {
|
||||
let _ = self.with_overlay_mut_maybe(|overlay| {
|
||||
overlay.operate(layout, renderer, operation);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,8 @@
|
|||
//! A container for capturing mouse events.
|
||||
|
||||
use iced_renderer::core::mouse::Click;
|
||||
|
||||
use crate::core::event;
|
||||
use crate::core::layout;
|
||||
use crate::core::mouse;
|
||||
use crate::core::overlay;
|
||||
|
|
@ -18,7 +22,9 @@ pub struct MouseArea<
|
|||
Renderer = crate::Renderer,
|
||||
> {
|
||||
content: Element<'a, Message, Theme, Renderer>,
|
||||
on_drag: Option<Message>,
|
||||
on_press: Option<Message>,
|
||||
on_double_press: Option<Message>,
|
||||
on_release: Option<Message>,
|
||||
on_double_click: Option<Message>,
|
||||
on_right_press: Option<Message>,
|
||||
|
|
@ -33,12 +39,25 @@ pub struct MouseArea<
|
|||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
|
||||
/// The message to emit when a drag is initiated.
|
||||
#[must_use]
|
||||
pub fn on_drag(mut self, message: Message) -> Self {
|
||||
self.on_drag = Some(message);
|
||||
self
|
||||
}
|
||||
|
||||
/// The message to emit on a left button press.
|
||||
#[must_use]
|
||||
pub fn on_press(mut self, message: Message) -> Self {
|
||||
self.on_press = Some(message);
|
||||
self
|
||||
}
|
||||
/// The message to emit on a left double button press.
|
||||
#[must_use]
|
||||
pub fn on_double_press(mut self, message: Message) -> Self {
|
||||
self.on_double_press = Some(message);
|
||||
self
|
||||
}
|
||||
|
||||
/// The message to emit on a left button release.
|
||||
#[must_use]
|
||||
|
|
@ -131,12 +150,28 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
|
|||
}
|
||||
|
||||
/// Local state of the [`MouseArea`].
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
is_hovered: bool,
|
||||
bounds: Rectangle,
|
||||
cursor_position: Option<Point>,
|
||||
previous_click: Option<mouse::Click>,
|
||||
// TODO: Support on_enter and on_exit
|
||||
drag_initiated: Option<Point>,
|
||||
is_out_of_bounds: bool,
|
||||
last_click: Option<Click>,
|
||||
}
|
||||
impl Default for State {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
is_hovered: Default::default(),
|
||||
drag_initiated: Default::default(),
|
||||
is_out_of_bounds: true,
|
||||
last_click: Default::default(),
|
||||
cursor_position: None,
|
||||
bounds: Default::default(),
|
||||
previous_click: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
|
||||
|
|
@ -146,7 +181,9 @@ impl<'a, Message, Theme, Renderer> MouseArea<'a, Message, Theme, Renderer> {
|
|||
) -> Self {
|
||||
MouseArea {
|
||||
content: content.into(),
|
||||
on_drag: None,
|
||||
on_press: None,
|
||||
on_double_press: None,
|
||||
on_release: None,
|
||||
on_double_click: None,
|
||||
on_right_press: None,
|
||||
|
|
@ -180,8 +217,8 @@ where
|
|||
vec![Tree::new(&self.content)]
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
tree.diff_children(std::slice::from_ref(&self.content));
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
tree.diff_children(std::slice::from_mut(&mut self.content));
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
@ -291,7 +328,6 @@ where
|
|||
viewport,
|
||||
);
|
||||
}
|
||||
|
||||
fn overlay<'b>(
|
||||
&'b mut self,
|
||||
tree: &'b mut Tree,
|
||||
|
|
@ -308,6 +344,22 @@ where
|
|||
translation,
|
||||
)
|
||||
}
|
||||
fn drag_destinations(
|
||||
&self,
|
||||
state: &Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
dnd_rectangles: &mut crate::core::clipboard::DndDestinationRectangles,
|
||||
) {
|
||||
if let Some(state) = state.children.iter().next() {
|
||||
self.content.as_widget().drag_destinations(
|
||||
state,
|
||||
layout,
|
||||
renderer,
|
||||
dnd_rectangles,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> From<MouseArea<'a, Message, Theme, Renderer>>
|
||||
|
|
@ -335,14 +387,15 @@ fn update<Message: Clone, Theme, Renderer>(
|
|||
shell: &mut Shell<'_, Message>,
|
||||
) {
|
||||
let state: &mut State = tree.state.downcast_mut();
|
||||
|
||||
let cursor_position = cursor.position();
|
||||
let bounds = layout.bounds();
|
||||
|
||||
if state.cursor_position != cursor_position || state.bounds != bounds {
|
||||
if let Event::Mouse(mouse::Event::CursorMoved { .. })
|
||||
| Event::Touch(touch::Event::FingerMoved { .. }) = event
|
||||
{
|
||||
let was_hovered = state.is_hovered;
|
||||
let bounds = layout.bounds();
|
||||
|
||||
state.is_hovered = cursor.is_over(layout.bounds());
|
||||
state.is_hovered = cursor.is_over(bounds);
|
||||
state.cursor_position = cursor_position;
|
||||
state.bounds = bounds;
|
||||
|
||||
|
|
@ -367,71 +420,153 @@ fn update<Message: Clone, Theme, Renderer>(
|
|||
}
|
||||
|
||||
if !cursor.is_over(layout.bounds()) {
|
||||
if !state.is_out_of_bounds {
|
||||
if widget
|
||||
.on_enter
|
||||
.as_ref()
|
||||
.or(widget.on_exit.as_ref())
|
||||
.is_some()
|
||||
{
|
||||
if let Event::Mouse(mouse::Event::CursorMoved { .. }) = event {
|
||||
state.is_out_of_bounds = true;
|
||||
if let Some(message) = widget.on_exit.as_ref() {
|
||||
shell.publish(message.clone());
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||
if let Some(message) = widget.on_press.as_ref() {
|
||||
if let Some(message) = widget.on_double_press.as_ref() {
|
||||
if let Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) =
|
||||
event
|
||||
{
|
||||
if let Some(cursor_position) = cursor.position() {
|
||||
let click = mouse::Click::new(
|
||||
cursor_position,
|
||||
mouse::Button::Left,
|
||||
state.last_click,
|
||||
);
|
||||
state.last_click = Some(click);
|
||||
if let mouse::click::Kind::Double = click.kind() {
|
||||
shell.publish(message.clone());
|
||||
shell.capture_event();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerPressed { .. }) = event
|
||||
{
|
||||
let mut captured = false;
|
||||
|
||||
if let Some(position) = cursor_position
|
||||
&& let Some(message) = widget.on_double_click.as_ref()
|
||||
{
|
||||
let new_click = mouse::Click::new(
|
||||
position,
|
||||
mouse::Button::Left,
|
||||
state.previous_click,
|
||||
);
|
||||
|
||||
if new_click.kind() == mouse::click::Kind::Double {
|
||||
shell.publish(message.clone());
|
||||
shell.capture_event();
|
||||
}
|
||||
|
||||
if let Some(position) = cursor_position
|
||||
&& let Some(message) = widget.on_double_click.as_ref()
|
||||
{
|
||||
let new_click = mouse::Click::new(
|
||||
position,
|
||||
mouse::Button::Left,
|
||||
state.previous_click,
|
||||
);
|
||||
state.previous_click = Some(new_click);
|
||||
|
||||
if new_click.kind() == mouse::click::Kind::Double {
|
||||
// Even if this is not a double click, but the press is nevertheless
|
||||
// processed by us and should not be popup to parent widgets.
|
||||
shell.capture_event();
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(message) = widget.on_release.as_ref() {
|
||||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerLifted { .. }) => {
|
||||
state.drag_initiated = None;
|
||||
shell.publish(message.clone());
|
||||
shell.capture_event();
|
||||
return;
|
||||
}
|
||||
Event::Mouse(mouse::Event::ButtonReleased(
|
||||
mouse::Button::Right,
|
||||
)) => {
|
||||
if let Some(message) = widget.on_right_release.as_ref() {
|
||||
shell.publish(message.clone());
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse::Event::ButtonPressed(
|
||||
mouse::Button::Middle,
|
||||
)) => {
|
||||
if let Some(message) = widget.on_middle_press.as_ref() {
|
||||
shell.publish(message.clone());
|
||||
shell.capture_event();
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse::Event::ButtonReleased(
|
||||
mouse::Button::Middle,
|
||||
)) => {
|
||||
if let Some(message) = widget.on_middle_release.as_ref() {
|
||||
shell.publish(message.clone());
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
|
||||
if let Some(on_scroll) = widget.on_scroll.as_ref() {
|
||||
shell.publish(on_scroll(*delta));
|
||||
shell.capture_event();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
state.previous_click = Some(new_click);
|
||||
if let Some(on_scroll) = widget.on_scroll.as_ref() {
|
||||
if let Event::Mouse(mouse::Event::WheelScrolled { delta }) = event {
|
||||
shell.publish(on_scroll(*delta));
|
||||
shell.capture_event();
|
||||
|
||||
// Even if this is not a double click, but the press is nevertheless
|
||||
// processed by us and should not be popup to parent widgets.
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(message) = widget.on_enter.as_ref().or(widget.on_exit.as_ref())
|
||||
{
|
||||
if let Event::Mouse(mouse::Event::CursorMoved { .. }) = event {
|
||||
if state.is_out_of_bounds {
|
||||
state.is_out_of_bounds = false;
|
||||
if widget.on_enter.is_some() {
|
||||
shell.publish(message.clone());
|
||||
}
|
||||
shell.capture_event();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerLifted { .. }) => {
|
||||
if let Some(message) = widget.on_release.as_ref() {
|
||||
}
|
||||
|
||||
if state.drag_initiated.is_none() && widget.on_drag.is_some() {
|
||||
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerPressed { .. }) = event
|
||||
{
|
||||
state.drag_initiated = cursor.position();
|
||||
}
|
||||
} else if let Some((message, drag_source)) =
|
||||
widget.on_drag.as_ref().zip(state.drag_initiated)
|
||||
{
|
||||
if let Some(position) = cursor.position() {
|
||||
if position.distance(drag_source) > 1.0 {
|
||||
state.drag_initiated = None;
|
||||
shell.publish(message.clone());
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Right)) => {
|
||||
if let Some(message) = widget.on_right_press.as_ref() {
|
||||
shell.publish(message.clone());
|
||||
shell.capture_event();
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Right)) => {
|
||||
if let Some(message) = widget.on_right_release.as_ref() {
|
||||
shell.publish(message.clone());
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Middle)) => {
|
||||
if let Some(message) = widget.on_middle_press.as_ref() {
|
||||
shell.publish(message.clone());
|
||||
shell.capture_event();
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Middle)) => {
|
||||
if let Some(message) = widget.on_middle_release.as_ref() {
|
||||
shell.publish(message.clone());
|
||||
}
|
||||
}
|
||||
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
|
||||
if let Some(on_scroll) = widget.on_scroll.as_ref() {
|
||||
shell.publish(on_scroll(*delta));
|
||||
shell.capture_event();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ pub struct Menu<
|
|||
text_size: Option<Pixels>,
|
||||
text_line_height: text::LineHeight,
|
||||
text_shaping: text::Shaping,
|
||||
text_wrap: text::Wrapping,
|
||||
font: Option<Renderer::Font>,
|
||||
class: &'a <Theme as Catalog>::Class<'b>,
|
||||
}
|
||||
|
|
@ -72,7 +73,8 @@ where
|
|||
padding: Padding::ZERO,
|
||||
text_size: None,
|
||||
text_line_height: text::LineHeight::default(),
|
||||
text_shaping: text::Shaping::default(),
|
||||
text_shaping: text::Shaping::Advanced,
|
||||
text_wrap: text::Wrapping::default(),
|
||||
font: None,
|
||||
class,
|
||||
}
|
||||
|
|
@ -111,6 +113,12 @@ where
|
|||
self
|
||||
}
|
||||
|
||||
/// Sets the [`text::Wrap`] mode of the [`Menu`].
|
||||
pub fn text_wrap(mut self, wrap: text::Wrapping) -> Self {
|
||||
self.text_wrap = wrap;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the font of the [`Menu`].
|
||||
pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
|
||||
self.font = Some(font.into());
|
||||
|
|
@ -204,10 +212,11 @@ where
|
|||
text_size,
|
||||
text_line_height,
|
||||
text_shaping,
|
||||
text_wrap,
|
||||
class,
|
||||
} = menu;
|
||||
|
||||
let list = Scrollable::new(List {
|
||||
let mut list = Scrollable::new(List {
|
||||
options,
|
||||
hovered_option,
|
||||
on_selected,
|
||||
|
|
@ -215,13 +224,14 @@ where
|
|||
font,
|
||||
text_size,
|
||||
text_line_height,
|
||||
text_wrap,
|
||||
text_shaping,
|
||||
padding,
|
||||
class,
|
||||
})
|
||||
.height(menu_height);
|
||||
|
||||
state.tree.diff(&list as &dyn Widget<_, _, _>);
|
||||
state.tree.diff(&mut list as &mut dyn Widget<_, _, _>);
|
||||
|
||||
Self {
|
||||
position,
|
||||
|
|
@ -342,6 +352,7 @@ where
|
|||
text_size: Option<Pixels>,
|
||||
text_line_height: text::LineHeight,
|
||||
text_shaping: text::Shaping,
|
||||
text_wrap: text::Wrapping,
|
||||
font: Option<Renderer::Font>,
|
||||
class: &'a <Theme as Catalog>::Class<'b>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,7 +84,6 @@ use crate::core::mouse;
|
|||
use crate::core::overlay::{self, Group};
|
||||
use crate::core::renderer;
|
||||
use crate::core::touch;
|
||||
use crate::core::widget;
|
||||
use crate::core::widget::tree::{self, Tree};
|
||||
use crate::core::window;
|
||||
use crate::core::{
|
||||
|
|
@ -165,6 +164,7 @@ pub struct PaneGrid<
|
|||
min_size: f32,
|
||||
on_click: Option<Box<dyn Fn(Pane) -> Message + 'a>>,
|
||||
on_drag: Option<Box<dyn Fn(DragEvent) -> Message + 'a>>,
|
||||
#[allow(clippy::type_complexity)]
|
||||
on_resize: Option<(f32, Box<dyn Fn(ResizeEvent) -> Message + 'a>)>,
|
||||
class: <Theme as Catalog>::Class<'a>,
|
||||
last_mouse_interaction: Option<mouse::Interaction>,
|
||||
|
|
@ -377,8 +377,8 @@ where
|
|||
self.contents.iter().map(Content::state).collect()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
let Memory { order, .. } = tree.state.downcast_ref();
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
let Memory { order, .. } = tree.state.downcast_mut();
|
||||
|
||||
// `Pane` always increments and is iterated by Ord so new
|
||||
// states are always added at the end. We can simply remove
|
||||
|
|
@ -399,11 +399,13 @@ where
|
|||
retain
|
||||
});
|
||||
|
||||
tree.diff_children_custom(
|
||||
&self.contents,
|
||||
|state, content| content.diff(state),
|
||||
Content::state,
|
||||
);
|
||||
// let ids = self.contents.iter().map(|_| None).collect(); // TODO
|
||||
// tree.diff_children_custom(
|
||||
// &mut self.contents,
|
||||
// ids,
|
||||
// |state, (_, content): (_, _)| content.diff(state),
|
||||
// |(_, content): (_, _)| content.state(),
|
||||
// );
|
||||
|
||||
let Memory { order, .. } = tree.state.downcast_mut();
|
||||
order.clone_from(&self.panes);
|
||||
|
|
@ -464,7 +466,7 @@ where
|
|||
tree: &mut Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
operation: &mut dyn widget::Operation,
|
||||
operation: &mut dyn crate::core::widget::Operation,
|
||||
) {
|
||||
operation.container(None, layout.bounds());
|
||||
operation.traverse(&mut |operation| {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use iced_renderer::core::widget::Operation;
|
||||
|
||||
use crate::container;
|
||||
use crate::core::layout;
|
||||
use crate::core::mouse;
|
||||
|
|
@ -90,13 +92,13 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub(super) fn diff(&self, tree: &mut Tree) {
|
||||
pub(super) fn diff(&mut self, tree: &mut Tree) {
|
||||
if tree.children.len() == 2 {
|
||||
if let Some(title_bar) = self.title_bar.as_ref() {
|
||||
if let Some(title_bar) = self.title_bar.as_mut() {
|
||||
title_bar.diff(&mut tree.children[1]);
|
||||
}
|
||||
|
||||
tree.children[0].diff(&self.body);
|
||||
tree.children[0].diff(&mut self.body);
|
||||
} else {
|
||||
*tree = self.state();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
use iced_renderer::core::widget::Operation;
|
||||
|
||||
use crate::container;
|
||||
use crate::core::layout;
|
||||
use crate::core::mouse;
|
||||
use crate::core::overlay;
|
||||
use crate::core::renderer;
|
||||
use crate::core::widget::{self, Tree};
|
||||
use crate::core::widget::Tree;
|
||||
use crate::core::{
|
||||
self, Clipboard, Element, Event, Layout, Padding, Point, Rectangle, Shell,
|
||||
Size, Vector,
|
||||
|
|
@ -127,17 +129,17 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub(super) fn diff(&self, tree: &mut Tree) {
|
||||
pub(super) fn diff(&mut self, tree: &mut Tree) {
|
||||
if tree.children.len() == 3 {
|
||||
if let Some(controls) = self.controls.as_ref() {
|
||||
if let Some(compact) = controls.compact.as_ref() {
|
||||
if let Some(controls) = self.controls.as_mut() {
|
||||
if let Some(compact) = controls.compact.as_mut() {
|
||||
tree.children[2].diff(compact);
|
||||
}
|
||||
|
||||
tree.children[1].diff(&controls.full);
|
||||
tree.children[1].diff(&mut controls.full);
|
||||
}
|
||||
|
||||
tree.children[0].diff(&self.content);
|
||||
tree.children[0].diff(&mut self.content);
|
||||
} else {
|
||||
*tree = self.state();
|
||||
}
|
||||
|
|
@ -161,7 +163,9 @@ where
|
|||
let style = theme.style(&self.class);
|
||||
|
||||
let inherited_style = renderer::Style {
|
||||
icon_color: style.icon_color.unwrap_or(inherited_style.icon_color),
|
||||
text_color: style.text_color.unwrap_or(inherited_style.text_color),
|
||||
scale_factor: inherited_style.scale_factor,
|
||||
};
|
||||
|
||||
container::draw_background(renderer, &style, bounds);
|
||||
|
|
@ -372,7 +376,7 @@ where
|
|||
tree: &mut Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
operation: &mut dyn widget::Operation,
|
||||
operation: &mut dyn crate::core::widget::Operation,
|
||||
) {
|
||||
let mut children = layout.children();
|
||||
let padded = children.next().unwrap();
|
||||
|
|
|
|||
|
|
@ -60,6 +60,8 @@
|
|||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
//! Display a dropdown list of selectable values.
|
||||
|
||||
use crate::core::alignment;
|
||||
use crate::core::keyboard;
|
||||
use crate::core::layout;
|
||||
|
|
@ -168,6 +170,7 @@ pub struct PickList<
|
|||
text_size: Option<Pixels>,
|
||||
text_line_height: text::LineHeight,
|
||||
text_shaping: text::Shaping,
|
||||
text_wrap: text::Wrapping,
|
||||
font: Option<Renderer::Font>,
|
||||
handle: Handle<Renderer::Font>,
|
||||
class: <Theme as Catalog>::Class<'a>,
|
||||
|
|
@ -204,7 +207,8 @@ where
|
|||
padding: crate::button::DEFAULT_PADDING,
|
||||
text_size: None,
|
||||
text_line_height: text::LineHeight::default(),
|
||||
text_shaping: text::Shaping::default(),
|
||||
text_shaping: text::Shaping::Advanced,
|
||||
text_wrap: text::Wrapping::default(),
|
||||
font: None,
|
||||
handle: Handle::default(),
|
||||
class: <Theme as Catalog>::default(),
|
||||
|
|
@ -259,6 +263,12 @@ where
|
|||
self
|
||||
}
|
||||
|
||||
/// Sets the [`text::Wrap`] mode of the [`PickList`].
|
||||
pub fn text_wrap(mut self, wrap: text::Wrapping) -> Self {
|
||||
self.text_wrap = wrap;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the font of the [`PickList`].
|
||||
pub fn font(mut self, font: impl Into<Renderer::Font>) -> Self {
|
||||
self.font = Some(font.into());
|
||||
|
|
@ -381,7 +391,7 @@ where
|
|||
align_x: text::Alignment::Default,
|
||||
align_y: alignment::Vertical::Center,
|
||||
shaping: self.text_shaping,
|
||||
wrapping: text::Wrapping::default(),
|
||||
wrapping: self.text_wrap,
|
||||
};
|
||||
|
||||
for (option, paragraph) in options.iter().zip(state.options.iter_mut())
|
||||
|
|
@ -603,6 +613,7 @@ where
|
|||
*size,
|
||||
text::LineHeight::default(),
|
||||
text::Shaping::Basic,
|
||||
text::Wrapping::default(),
|
||||
)),
|
||||
Handle::Static(Icon {
|
||||
font,
|
||||
|
|
@ -610,7 +621,10 @@ where
|
|||
size,
|
||||
line_height,
|
||||
shaping,
|
||||
}) => Some((*font, *code_point, *size, *line_height, *shaping)),
|
||||
wrap,
|
||||
}) => {
|
||||
Some((*font, *code_point, *size, *line_height, *shaping, *wrap))
|
||||
}
|
||||
Handle::Dynamic { open, closed } => {
|
||||
if state.is_open {
|
||||
Some((
|
||||
|
|
@ -619,6 +633,7 @@ where
|
|||
open.size,
|
||||
open.line_height,
|
||||
open.shaping,
|
||||
open.wrap,
|
||||
))
|
||||
} else {
|
||||
Some((
|
||||
|
|
@ -627,13 +642,16 @@ where
|
|||
closed.size,
|
||||
closed.line_height,
|
||||
closed.shaping,
|
||||
closed.wrap,
|
||||
))
|
||||
}
|
||||
}
|
||||
Handle::None => None,
|
||||
};
|
||||
|
||||
if let Some((font, code_point, size, line_height, shaping)) = handle {
|
||||
if let Some((font, code_point, size, line_height, shaping, wrap)) =
|
||||
handle
|
||||
{
|
||||
let size = size.unwrap_or_else(|| renderer.default_size());
|
||||
|
||||
renderer.fill_text(
|
||||
|
|
@ -649,7 +667,7 @@ where
|
|||
align_x: text::Alignment::Right,
|
||||
align_y: alignment::Vertical::Center,
|
||||
shaping,
|
||||
wrapping: text::Wrapping::default(),
|
||||
wrapping: wrap,
|
||||
},
|
||||
Point::new(
|
||||
bounds.x + bounds.width - self.padding.right,
|
||||
|
|
@ -679,7 +697,7 @@ where
|
|||
align_x: text::Alignment::Default,
|
||||
align_y: alignment::Vertical::Center,
|
||||
shaping: self.text_shaping,
|
||||
wrapping: text::Wrapping::default(),
|
||||
wrapping: self.text_wrap,
|
||||
},
|
||||
Point::new(bounds.x + self.padding.left, bounds.center_y()),
|
||||
if selected.is_some() {
|
||||
|
|
@ -831,6 +849,8 @@ pub struct Icon<Font> {
|
|||
pub line_height: text::LineHeight,
|
||||
/// The shaping strategy of the icon.
|
||||
pub shaping: text::Shaping,
|
||||
/// The wrap mode of the icon.
|
||||
pub wrap: text::Wrapping,
|
||||
}
|
||||
|
||||
/// The possible status of a [`PickList`].
|
||||
|
|
|
|||
|
|
@ -126,8 +126,8 @@ where
|
|||
self.content.as_widget().children()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut widget::Tree) {
|
||||
self.content.as_widget().diff(tree);
|
||||
fn diff(&mut self, tree: &mut widget::Tree) {
|
||||
self.content.as_widget_mut().diff(tree);
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
|
|||
|
|
@ -188,7 +188,7 @@ where
|
|||
spacing: Self::DEFAULT_SPACING,
|
||||
text_size: None,
|
||||
text_line_height: text::LineHeight::default(),
|
||||
text_shaping: text::Shaping::default(),
|
||||
text_shaping: text::Shaping::Advanced,
|
||||
text_wrapping: text::Wrapping::default(),
|
||||
font: None,
|
||||
class: Theme::default(),
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
|||
where
|
||||
Renderer: core::Renderer,
|
||||
{
|
||||
fn diff(&self, _tree: &mut Tree) {
|
||||
fn diff(&mut self, _tree: &mut Tree) {
|
||||
// Diff is deferred to layout
|
||||
}
|
||||
|
||||
|
|
@ -86,7 +86,7 @@ where
|
|||
let size = limits.max();
|
||||
|
||||
self.content = (self.view)(size);
|
||||
tree.diff_children(std::slice::from_ref(&self.content));
|
||||
tree.diff_children(std::slice::from_mut(&mut self.content));
|
||||
|
||||
let node = self.content.as_widget_mut().layout(
|
||||
&mut tree.children[0],
|
||||
|
|
|
|||
|
|
@ -195,8 +195,8 @@ where
|
|||
self.children.iter().map(Tree::new).collect()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
tree.diff_children(&self.children);
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
tree.diff_children(&mut self.children)
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
@ -340,6 +340,48 @@ where
|
|||
translation,
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// get the a11y nodes for the widget
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
state: &Tree,
|
||||
cursor: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
use iced_accessibility::A11yTree;
|
||||
A11yTree::join(
|
||||
self.children
|
||||
.iter()
|
||||
.zip(layout.children())
|
||||
.zip(state.children.iter())
|
||||
.map(|((c, c_layout), state)| {
|
||||
c.as_widget().a11y_nodes(c_layout, state, cursor)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
fn drag_destinations(
|
||||
&self,
|
||||
state: &Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
dnd_rectangles: &mut crate::core::clipboard::DndDestinationRectangles,
|
||||
) {
|
||||
for ((e, layout), state) in self
|
||||
.children
|
||||
.iter()
|
||||
.zip(layout.children())
|
||||
.zip(state.children.iter())
|
||||
{
|
||||
e.as_widget().drag_destinations(
|
||||
state,
|
||||
layout,
|
||||
renderer,
|
||||
dnd_rectangles,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> From<Row<'a, Message, Theme, Renderer>>
|
||||
|
|
@ -397,7 +439,7 @@ where
|
|||
self.row.children()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
self.row.diff(tree);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,8 @@ where
|
|||
Theme: Catalog,
|
||||
{
|
||||
Rule {
|
||||
thickness: Length::Fixed(height.into().0),
|
||||
width: Length::Fill,
|
||||
height: Length::Fixed(height.into().0),
|
||||
is_vertical: false,
|
||||
class: Theme::default(),
|
||||
}
|
||||
|
|
@ -44,7 +45,8 @@ where
|
|||
Theme: Catalog,
|
||||
{
|
||||
Rule {
|
||||
thickness: Length::Fixed(width.into().0),
|
||||
width: Length::Fixed(width.into().0),
|
||||
height: Length::Fill,
|
||||
is_vertical: true,
|
||||
class: Theme::default(),
|
||||
}
|
||||
|
|
@ -72,7 +74,8 @@ pub struct Rule<'a, Theme = crate::Theme>
|
|||
where
|
||||
Theme: Catalog,
|
||||
{
|
||||
thickness: Length,
|
||||
width: Length,
|
||||
height: Length,
|
||||
is_vertical: bool,
|
||||
class: Theme::Class<'a>,
|
||||
}
|
||||
|
|
@ -81,6 +84,44 @@ impl<'a, Theme> Rule<'a, Theme>
|
|||
where
|
||||
Theme: Catalog,
|
||||
{
|
||||
/// Creates a horizontal [`Rule`] with the given height.
|
||||
pub fn horizontal(height: impl Into<Pixels>) -> Self {
|
||||
Rule {
|
||||
width: Length::Fill,
|
||||
height: Length::Fixed(height.into().0),
|
||||
is_vertical: false,
|
||||
class: Theme::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a vertical [`Rule`] with the given width.
|
||||
pub fn vertical(width: impl Into<Pixels>) -> Self {
|
||||
Rule {
|
||||
width: Length::Fixed(width.into().0),
|
||||
height: Length::Fill,
|
||||
is_vertical: true,
|
||||
class: Theme::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the width of the rule
|
||||
/// Will not be applied if it is vertical
|
||||
pub fn width(mut self, width: impl Into<Length>) -> Self {
|
||||
if !self.is_vertical {
|
||||
self.width = width.into();
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the height of the rule
|
||||
/// Will not be applied if it is horizontal
|
||||
pub fn height(mut self, height: impl Into<Length>) -> Self {
|
||||
if self.is_vertical {
|
||||
self.height = height.into();
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the style of the [`Rule`].
|
||||
#[must_use]
|
||||
pub fn style(mut self, style: impl Fn(&Theme) -> Style + 'a) -> Self
|
||||
|
|
@ -107,16 +148,9 @@ where
|
|||
Theme: Catalog,
|
||||
{
|
||||
fn size(&self) -> Size<Length> {
|
||||
if self.is_vertical {
|
||||
Size {
|
||||
width: self.thickness,
|
||||
height: Length::Fill,
|
||||
}
|
||||
} else {
|
||||
Size {
|
||||
width: Length::Fill,
|
||||
height: self.thickness,
|
||||
}
|
||||
Size {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,6 +22,12 @@
|
|||
use crate::container;
|
||||
use crate::core::alignment;
|
||||
use crate::core::border::{self, Border};
|
||||
use crate::core::clipboard::DndDestinationRectangles;
|
||||
use iced_runtime::core::widget::Id;
|
||||
#[cfg(feature = "a11y")]
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::core::event;
|
||||
use crate::core::keyboard;
|
||||
use crate::core::layout;
|
||||
use crate::core::mouse;
|
||||
|
|
@ -30,18 +36,17 @@ use crate::core::renderer;
|
|||
use crate::core::text;
|
||||
use crate::core::time::{Duration, Instant};
|
||||
use crate::core::touch;
|
||||
use crate::core::widget;
|
||||
use crate::core::widget::operation::{self, Operation};
|
||||
use crate::core::widget::tree::{self, Tree};
|
||||
use crate::core::window;
|
||||
use crate::core::{
|
||||
self, Background, Clipboard, Color, Element, Event, InputMethod, Layout,
|
||||
Length, Padding, Pixels, Point, Rectangle, Shadow, Shell, Size, Theme,
|
||||
Vector, Widget,
|
||||
Vector, Widget, id::Internal,
|
||||
};
|
||||
|
||||
use iced_runtime::{Action, Task, task};
|
||||
pub use operation::scrollable::{AbsoluteOffset, RelativeOffset};
|
||||
|
||||
/// A widget that can vertically display an infinite amount of content with a
|
||||
/// scrollbar.
|
||||
///
|
||||
|
|
@ -73,7 +78,14 @@ pub struct Scrollable<
|
|||
Theme: Catalog,
|
||||
Renderer: text::Renderer,
|
||||
{
|
||||
id: Option<widget::Id>,
|
||||
id: Id,
|
||||
scrollbar_id: Id,
|
||||
#[cfg(feature = "a11y")]
|
||||
name: Option<Cow<'a, str>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: Option<iced_accessibility::Description<'a>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
|
||||
width: Length,
|
||||
height: Length,
|
||||
direction: Direction,
|
||||
|
|
@ -102,7 +114,14 @@ where
|
|||
direction: impl Into<Direction>,
|
||||
) -> Self {
|
||||
Scrollable {
|
||||
id: None,
|
||||
id: Id::unique(),
|
||||
scrollbar_id: Id::unique(),
|
||||
#[cfg(feature = "a11y")]
|
||||
name: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
label: None,
|
||||
width: Length::Shrink,
|
||||
height: Length::Shrink,
|
||||
direction: direction.into(),
|
||||
|
|
@ -141,8 +160,8 @@ where
|
|||
}
|
||||
|
||||
/// Sets the [`widget::Id`] of the [`Scrollable`].
|
||||
pub fn id(mut self, id: impl Into<widget::Id>) -> Self {
|
||||
self.id = Some(id.into());
|
||||
pub fn id(mut self, id: impl Into<core::widget::Id>) -> Self {
|
||||
self.id = id.into();
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -255,6 +274,41 @@ where
|
|||
self.class = class.into();
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the name of the [`Button`].
|
||||
pub fn name(mut self, name: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.name = Some(name.into());
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description_widget(
|
||||
mut self,
|
||||
description: &impl iced_accessibility::Describes,
|
||||
) -> Self {
|
||||
self.description = Some(iced_accessibility::Description::Id(
|
||||
description.description(),
|
||||
));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description(mut self, description: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.description =
|
||||
Some(iced_accessibility::Description::Text(description.into()));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the label of the [`Button`].
|
||||
pub fn label(mut self, label: &dyn iced_accessibility::Labels) -> Self {
|
||||
self.label =
|
||||
Some(label.label().into_iter().map(|l| l.into()).collect());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// The direction of [`Scrollable`].
|
||||
|
|
@ -415,8 +469,8 @@ where
|
|||
vec![Tree::new(&self.content)]
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
tree.diff_children(std::slice::from_ref(&self.content));
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
tree.diff_children(std::slice::from_mut(&mut self.content))
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
@ -552,7 +606,7 @@ where
|
|||
state.translation(self.direction, bounds, content_bounds);
|
||||
|
||||
operation.scrollable(
|
||||
self.id.as_ref(),
|
||||
Some(&self.id),
|
||||
bounds,
|
||||
content_bounds,
|
||||
translation,
|
||||
|
|
@ -1406,6 +1460,183 @@ where
|
|||
)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
state: &Tree,
|
||||
cursor: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
use iced_accessibility::{
|
||||
A11yId, A11yNode, A11yTree,
|
||||
accesskit::{Node, NodeId, Rect, Role},
|
||||
};
|
||||
|
||||
let child_layout = layout.children().next().unwrap();
|
||||
let child_tree = &state.children[0];
|
||||
let child_tree = self.content.as_widget().a11y_nodes(
|
||||
child_layout,
|
||||
&child_tree,
|
||||
cursor,
|
||||
);
|
||||
|
||||
let window = layout.bounds();
|
||||
let is_hovered = cursor.is_over(window);
|
||||
let Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
} = window;
|
||||
let bounds = Rect::new(
|
||||
x as f64,
|
||||
y as f64,
|
||||
(x + width) as f64,
|
||||
(y + height) as f64,
|
||||
);
|
||||
let mut node = Node::new(Role::ScrollView);
|
||||
node.set_bounds(bounds);
|
||||
if let Some(name) = self.name.as_ref() {
|
||||
node.set_label(name.clone());
|
||||
}
|
||||
match self.description.as_ref() {
|
||||
Some(iced_accessibility::Description::Id(id)) => {
|
||||
node.set_described_by(
|
||||
id.iter()
|
||||
.cloned()
|
||||
.map(|id| NodeId::from(id))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
Some(iced_accessibility::Description::Text(text)) => {
|
||||
node.set_description(text.clone());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
// TODO hover
|
||||
// if is_hovered {
|
||||
// node.set_hovered();
|
||||
// }
|
||||
|
||||
if let Some(label) = self.label.as_ref() {
|
||||
node.set_labelled_by(label.clone());
|
||||
}
|
||||
|
||||
let content = layout.children().next().unwrap();
|
||||
let content_bounds = content.bounds();
|
||||
|
||||
let mut scrollbar_node = Node::new(Role::ScrollBar);
|
||||
if matches!(state.state, tree::State::Some(_)) {
|
||||
let state = state.state.downcast_ref::<State>();
|
||||
let scrollbars = Scrollbars::new(
|
||||
state,
|
||||
self.direction,
|
||||
content_bounds,
|
||||
content_bounds,
|
||||
);
|
||||
for (window, content, offset, scrollbar) in scrollbars
|
||||
.x
|
||||
.iter()
|
||||
.map(|s| {
|
||||
(window.width, content_bounds.width, state.offset_x, s)
|
||||
})
|
||||
.chain(scrollbars.y.iter().map(|s| {
|
||||
(window.height, content_bounds.height, state.offset_y, s)
|
||||
}))
|
||||
{
|
||||
let scrollbar_bounds = scrollbar.total_bounds;
|
||||
let is_hovered = cursor.is_over(scrollbar_bounds);
|
||||
let Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
} = scrollbar_bounds;
|
||||
let bounds = Rect::new(
|
||||
x as f64,
|
||||
y as f64,
|
||||
(x + width) as f64,
|
||||
(y + height) as f64,
|
||||
);
|
||||
scrollbar_node.set_bounds(bounds);
|
||||
// TODO: hover
|
||||
// if is_hovered {
|
||||
// scrollbar_node.set_hovered();
|
||||
// }
|
||||
scrollbar_node
|
||||
.set_controls(vec![A11yId::Widget(self.id.clone()).into()]);
|
||||
scrollbar_node.set_numeric_value(
|
||||
100.0 * offset.absolute(window, content) as f64
|
||||
/ scrollbar_bounds.height as f64,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let child_tree = A11yTree::join(
|
||||
[
|
||||
child_tree,
|
||||
A11yTree::leaf(scrollbar_node, self.scrollbar_id.clone()),
|
||||
]
|
||||
.into_iter(),
|
||||
);
|
||||
A11yTree::node_with_child_tree(
|
||||
A11yNode::new(node, self.id.clone()),
|
||||
child_tree,
|
||||
)
|
||||
}
|
||||
|
||||
fn id(&self) -> Option<Id> {
|
||||
Some(Id(Internal::Set(vec![
|
||||
self.id.0.clone(),
|
||||
self.scrollbar_id.0.clone(),
|
||||
])))
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
if let Id(Internal::Set(list)) = id {
|
||||
if list.len() == 2 {
|
||||
self.id.0 = list[0].clone();
|
||||
self.scrollbar_id.0 = list[1].clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn drag_destinations(
|
||||
&self,
|
||||
tree: &Tree,
|
||||
layout: Layout<'_>,
|
||||
renderer: &Renderer,
|
||||
dnd_rectangles: &mut crate::core::clipboard::DndDestinationRectangles,
|
||||
) {
|
||||
let my_state = tree.state.downcast_ref::<State>();
|
||||
if let Some((c_layout, c_state)) =
|
||||
layout.children().zip(tree.children.iter()).next()
|
||||
{
|
||||
let mut my_dnd_rectangles = DndDestinationRectangles::new();
|
||||
self.content.as_widget().drag_destinations(
|
||||
c_state,
|
||||
c_layout,
|
||||
renderer,
|
||||
&mut my_dnd_rectangles,
|
||||
);
|
||||
let mut my_dnd_rectangles = my_dnd_rectangles.into_rectangles();
|
||||
|
||||
let bounds = layout.bounds();
|
||||
let content_bounds = c_layout.bounds();
|
||||
for r in &mut my_dnd_rectangles {
|
||||
let translation = my_state.translation(
|
||||
self.direction,
|
||||
bounds,
|
||||
content_bounds,
|
||||
);
|
||||
r.rectangle.x -= translation.x as f64;
|
||||
r.rectangle.y -= translation.y as f64;
|
||||
}
|
||||
dnd_rectangles.append(&mut my_dnd_rectangles);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct AutoScrollIcon<'a, Class> {
|
||||
|
|
@ -1567,6 +1798,24 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that snaps the [`Scrollable`] with the given [`Id`]
|
||||
/// to the provided [`RelativeOffset`].
|
||||
pub fn snap_to<T>(id: Id, offset: RelativeOffset<Option<f32>>) -> Task<T> {
|
||||
task::effect(Action::widget(operation::scrollable::snap_to(id, offset)))
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that scrolls the [`Scrollable`] with the given [`Id`]
|
||||
/// to the provided [`AbsoluteOffset`].
|
||||
pub fn scroll_to<T>(id: Id, offset: AbsoluteOffset<Option<f32>>) -> Task<T> {
|
||||
task::effect(Action::widget(operation::scrollable::scroll_to(id, offset)))
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that scrolls the [`Scrollable`] with the given [`Id`]
|
||||
/// by the provided [`AbsoluteOffset`].
|
||||
pub fn scroll_by<T>(id: Id, offset: AbsoluteOffset) -> Task<T> {
|
||||
task::effect(Action::widget(operation::scrollable::scroll_by(id, offset)))
|
||||
}
|
||||
|
||||
fn notify_scroll<Message>(
|
||||
state: &mut State,
|
||||
on_scroll: &Option<Box<dyn Fn(Viewport) -> Message + '_>>,
|
||||
|
|
|
|||
|
|
@ -180,8 +180,8 @@ where
|
|||
vec![Tree::new(&self.content)]
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
tree.diff_children(&[&self.content]);
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
tree.diff_children(&mut [&mut self.content]);
|
||||
}
|
||||
|
||||
fn update(
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ use crate::core::layout;
|
|||
use crate::core::mouse;
|
||||
use crate::core::renderer;
|
||||
use crate::core::touch;
|
||||
use crate::core::widget::Id;
|
||||
use crate::core::widget::tree::{self, Tree};
|
||||
use crate::core::window;
|
||||
use crate::core::{
|
||||
|
|
@ -44,6 +45,12 @@ use crate::core::{
|
|||
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
use iced_renderer::core::border::Radius;
|
||||
use iced_runtime::core::gradient::Linear;
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// An horizontal bar and a handle that selects a single value from a range of
|
||||
/// values.
|
||||
///
|
||||
|
|
@ -84,11 +91,19 @@ pub struct Slider<'a, T, Message, Theme = crate::Theme>
|
|||
where
|
||||
Theme: Catalog,
|
||||
{
|
||||
id: Id,
|
||||
#[cfg(feature = "a11y")]
|
||||
name: Option<Cow<'a, str>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: Option<iced_accessibility::Description<'a>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
|
||||
range: RangeInclusive<T>,
|
||||
step: T,
|
||||
shift_step: Option<T>,
|
||||
value: T,
|
||||
default: Option<T>,
|
||||
breakpoints: &'a [T],
|
||||
on_change: Box<dyn Fn(T) -> Message + 'a>,
|
||||
on_release: Option<Message>,
|
||||
width: Length,
|
||||
|
|
@ -131,11 +146,19 @@ where
|
|||
};
|
||||
|
||||
Slider {
|
||||
id: Id::unique(),
|
||||
#[cfg(feature = "a11y")]
|
||||
name: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
label: None,
|
||||
value,
|
||||
default: None,
|
||||
range,
|
||||
step: T::from(1),
|
||||
shift_step: None,
|
||||
breakpoints: &[],
|
||||
on_change: Box::new(on_change),
|
||||
on_release: None,
|
||||
width: Length::Fill,
|
||||
|
|
@ -153,12 +176,20 @@ where
|
|||
self
|
||||
}
|
||||
|
||||
/// Defines breakpoints to visibly mark on the slider.
|
||||
///
|
||||
/// The slider will gravitate towards a breakpoint when near it.
|
||||
pub fn breakpoints(mut self, breakpoints: &'a [T]) -> Self {
|
||||
self.breakpoints = breakpoints;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the release message of the [`Slider`].
|
||||
/// This is called when the mouse is released from the slider.
|
||||
///
|
||||
/// Typically, the user's interaction with the slider is finished when this message is produced.
|
||||
/// This is useful if you need to spawn a long-running task from the slider's result, where
|
||||
/// the default on_change message could create too many events.
|
||||
/// the default `on_change` message could create too many events.
|
||||
pub fn on_release(mut self, on_release: Message) -> Self {
|
||||
self.on_release = Some(on_release);
|
||||
self
|
||||
|
|
@ -207,6 +238,41 @@ where
|
|||
self.class = class.into();
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the name of the [`Button`].
|
||||
pub fn name(mut self, name: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.name = Some(name.into());
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description_widget(
|
||||
mut self,
|
||||
description: &impl iced_accessibility::Describes,
|
||||
) -> Self {
|
||||
self.description = Some(iced_accessibility::Description::Id(
|
||||
description.description(),
|
||||
));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description(mut self, description: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.description =
|
||||
Some(iced_accessibility::Description::Text(description.into()));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the label of the [`Button`].
|
||||
pub fn label(mut self, label: &dyn iced_accessibility::Labels) -> Self {
|
||||
self.label =
|
||||
Some(label.label().into_iter().map(|l| l.into()).collect());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||
|
|
@ -448,15 +514,51 @@ where
|
|||
let style =
|
||||
theme.style(&self.class, self.status.unwrap_or(Status::Active));
|
||||
|
||||
let border_width = style
|
||||
.handle
|
||||
.border_width
|
||||
.min(bounds.height / 2.0)
|
||||
.min(bounds.width / 2.0);
|
||||
|
||||
let (handle_width, handle_height, handle_border_radius) =
|
||||
match style.handle.shape {
|
||||
HandleShape::Circle { radius } => {
|
||||
(radius * 2.0, radius * 2.0, radius.into())
|
||||
let radius = (radius)
|
||||
.max(2.0 * border_width)
|
||||
.min(bounds.height / 2.0)
|
||||
.min(bounds.width / 2.0);
|
||||
(radius * 2.0, radius * 2.0, Radius::from(radius))
|
||||
}
|
||||
HandleShape::Rectangle {
|
||||
height,
|
||||
width,
|
||||
border_radius,
|
||||
} => (f32::from(width), bounds.height, border_radius),
|
||||
} => {
|
||||
let width = (f32::from(width))
|
||||
.max(2.0 * border_width)
|
||||
.min(bounds.width);
|
||||
let height = (f32::from(height))
|
||||
.max(2.0 * border_width)
|
||||
.min(bounds.height);
|
||||
let mut border_radius: [f32; 4] = border_radius.into();
|
||||
for r in &mut border_radius {
|
||||
*r = (*r)
|
||||
.min(height / 2.0)
|
||||
.min(width / 2.0)
|
||||
.max(*r * (width + border_width * 2.0) / width);
|
||||
}
|
||||
|
||||
(
|
||||
width,
|
||||
height,
|
||||
Radius {
|
||||
top_left: border_radius[0],
|
||||
top_right: border_radius[1],
|
||||
bottom_right: border_radius[2],
|
||||
bottom_left: border_radius[3],
|
||||
},
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let value = self.value.into() as f32;
|
||||
|
|
@ -475,6 +577,52 @@ where
|
|||
|
||||
let rail_y = bounds.y + bounds.height / 2.0;
|
||||
|
||||
// Draw the breakpoint indicators beneath the slider.
|
||||
const BREAKPOINT_WIDTH: f32 = 2.0;
|
||||
for &value in self.breakpoints {
|
||||
let value: f64 = value.into();
|
||||
let offset = if range_start >= range_end {
|
||||
0.0
|
||||
} else {
|
||||
(bounds.width - BREAKPOINT_WIDTH) * (value as f32 - range_start)
|
||||
/ (range_end - range_start)
|
||||
};
|
||||
|
||||
renderer.fill_quad(
|
||||
renderer::Quad {
|
||||
bounds: Rectangle {
|
||||
x: bounds.x + offset,
|
||||
y: rail_y + 6.0,
|
||||
width: BREAKPOINT_WIDTH,
|
||||
height: 8.0,
|
||||
},
|
||||
border: Border {
|
||||
radius: 0.0.into(),
|
||||
width: 0.0,
|
||||
color: Color::TRANSPARENT,
|
||||
},
|
||||
..renderer::Quad::default()
|
||||
},
|
||||
crate::core::Background::Color(style.breakpoint.color),
|
||||
);
|
||||
}
|
||||
|
||||
renderer.fill_quad(
|
||||
renderer::Quad {
|
||||
bounds: Rectangle {
|
||||
x: bounds.x,
|
||||
y: rail_y - style.rail.width / 2.0,
|
||||
width: offset + handle_width / 2.0,
|
||||
height: style.rail.width,
|
||||
},
|
||||
border: style.rail.border,
|
||||
..renderer::Quad::default()
|
||||
},
|
||||
style.rail.backgrounds.0,
|
||||
);
|
||||
|
||||
// TODO align the angle of the gradient for the slider?
|
||||
|
||||
renderer.fill_quad(
|
||||
renderer::Quad {
|
||||
bounds: Rectangle {
|
||||
|
|
@ -492,9 +640,9 @@ where
|
|||
renderer.fill_quad(
|
||||
renderer::Quad {
|
||||
bounds: Rectangle {
|
||||
x: bounds.x + offset + handle_width / 2.0,
|
||||
x: bounds.x,
|
||||
y: rail_y - style.rail.width / 2.0,
|
||||
width: bounds.width - offset - handle_width / 2.0,
|
||||
width: offset + handle_width / 2.0,
|
||||
height: style.rail.width,
|
||||
},
|
||||
border: style.rail.border,
|
||||
|
|
@ -503,11 +651,12 @@ where
|
|||
style.rail.backgrounds.1,
|
||||
);
|
||||
|
||||
// handle
|
||||
renderer.fill_quad(
|
||||
renderer::Quad {
|
||||
bounds: Rectangle {
|
||||
x: bounds.x + offset,
|
||||
y: rail_y - handle_height / 2.0,
|
||||
y: rail_y - (handle_height / 2.0),
|
||||
width: handle_width,
|
||||
height: handle_height,
|
||||
},
|
||||
|
|
@ -550,6 +699,83 @@ where
|
|||
mouse::Interaction::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
_state: &Tree,
|
||||
cursor: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
use iced_accessibility::{
|
||||
A11yTree,
|
||||
accesskit::{Node, NodeId, Rect, Role},
|
||||
};
|
||||
|
||||
let bounds = layout.bounds();
|
||||
let is_hovered = cursor.is_over(bounds);
|
||||
let Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
} = bounds;
|
||||
let bounds = Rect::new(
|
||||
x as f64,
|
||||
y as f64,
|
||||
(x + width) as f64,
|
||||
(y + height) as f64,
|
||||
);
|
||||
let mut node = Node::new(Role::Slider);
|
||||
node.set_bounds(bounds);
|
||||
if let Some(name) = self.name.as_ref() {
|
||||
node.set_label(name.clone());
|
||||
}
|
||||
match self.description.as_ref() {
|
||||
Some(iced_accessibility::Description::Id(id)) => {
|
||||
node.set_described_by(
|
||||
id.iter()
|
||||
.cloned()
|
||||
.map(|id| NodeId::from(id))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
Some(iced_accessibility::Description::Text(text)) => {
|
||||
node.set_description(text.clone());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
if let Some(label) = self.label.as_ref() {
|
||||
node.set_labelled_by(label.clone());
|
||||
}
|
||||
|
||||
if let Ok(min) = self.range.start().clone().try_into() {
|
||||
node.set_min_numeric_value(min);
|
||||
}
|
||||
if let Ok(max) = self.range.end().clone().try_into() {
|
||||
node.set_max_numeric_value(max);
|
||||
}
|
||||
if let Ok(value) = self.value.clone().try_into() {
|
||||
node.set_numeric_value(value);
|
||||
}
|
||||
if let Ok(step) = self.step.clone().try_into() {
|
||||
node.set_numeric_value_step(step);
|
||||
}
|
||||
|
||||
// TODO: This could be a setting on the slider
|
||||
node.set_live(iced_accessibility::accesskit::Live::Polite);
|
||||
|
||||
A11yTree::leaf(node, self.id.clone())
|
||||
}
|
||||
|
||||
fn id(&self) -> Option<Id> {
|
||||
Some(self.id.clone())
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
self.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, Message, Theme, Renderer> From<Slider<'a, T, Message, Theme>>
|
||||
|
|
@ -591,6 +817,15 @@ pub struct Style {
|
|||
pub rail: Rail,
|
||||
/// The appearance of the [`Handle`] of the slider.
|
||||
pub handle: Handle,
|
||||
/// The appearance of breakpoints.
|
||||
pub breakpoint: Breakpoint,
|
||||
}
|
||||
|
||||
/// The appearance of slider breakpoints.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Breakpoint {
|
||||
/// The color of the slider breakpoint.
|
||||
pub color: Color,
|
||||
}
|
||||
|
||||
impl Style {
|
||||
|
|
@ -615,6 +850,21 @@ pub struct Rail {
|
|||
pub border: Border,
|
||||
}
|
||||
|
||||
/// The background color of the rail
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum RailBackground {
|
||||
/// Start and end colors of the rail
|
||||
Pair(Color, Color),
|
||||
/// Linear gradient for the background of the rail
|
||||
/// includes an option for auto-selecting the angle
|
||||
Gradient {
|
||||
/// the linear gradient of the slider
|
||||
gradient: Linear,
|
||||
/// Let the widget determin the angle of the gradient
|
||||
auto_angle: bool,
|
||||
},
|
||||
}
|
||||
|
||||
/// The appearance of the handle of a slider.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Handle {
|
||||
|
|
@ -640,6 +890,8 @@ pub enum HandleShape {
|
|||
Rectangle {
|
||||
/// The width of the rectangle.
|
||||
width: u16,
|
||||
/// The height of the rectangle.
|
||||
height: u16,
|
||||
/// The border radius of the corners of the rectangle.
|
||||
border_radius: border::Radius,
|
||||
},
|
||||
|
|
@ -698,5 +950,8 @@ pub fn default(theme: &Theme, status: Status) -> Style {
|
|||
border_color: Color::TRANSPARENT,
|
||||
border_width: 0.0,
|
||||
},
|
||||
breakpoint: Breakpoint {
|
||||
color: palette.background.weak.text,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
//! Display content on top of other content.
|
||||
|
||||
use crate::core::event;
|
||||
use crate::core::layout;
|
||||
use crate::core::mouse;
|
||||
use crate::core::overlay;
|
||||
|
|
@ -150,8 +152,8 @@ where
|
|||
self.children.iter().map(Tree::new).collect()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
tree.diff_children(&self.children);
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
tree.diff_children(&mut self.children);
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
|
|||
|
|
@ -15,6 +15,9 @@
|
|||
//! svg("tiger.svg").into()
|
||||
//! }
|
||||
//! ```
|
||||
//! Display vector graphics in your application.
|
||||
use iced_runtime::core::widget::Id;
|
||||
|
||||
use crate::core::layout;
|
||||
use crate::core::mouse;
|
||||
use crate::core::renderer;
|
||||
|
|
@ -26,6 +29,9 @@ use crate::core::{
|
|||
Rectangle, Rotation, Shell, Size, Theme, Vector, Widget,
|
||||
};
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
use std::borrow::Cow;
|
||||
use std::marker::PhantomData;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub use crate::core::svg::Handle;
|
||||
|
|
@ -56,6 +62,13 @@ pub struct Svg<'a, Theme = crate::Theme>
|
|||
where
|
||||
Theme: Catalog,
|
||||
{
|
||||
id: Id,
|
||||
#[cfg(feature = "a11y")]
|
||||
name: Option<Cow<'a, str>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: Option<iced_accessibility::Description<'a>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
|
||||
handle: Handle,
|
||||
width: Length,
|
||||
height: Length,
|
||||
|
|
@ -64,6 +77,7 @@ where
|
|||
rotation: Rotation,
|
||||
opacity: f32,
|
||||
status: Option<Status>,
|
||||
symbolic: bool,
|
||||
}
|
||||
|
||||
impl<'a, Theme> Svg<'a, Theme>
|
||||
|
|
@ -73,6 +87,13 @@ where
|
|||
/// Creates a new [`Svg`] from the given [`Handle`].
|
||||
pub fn new(handle: impl Into<Handle>) -> Self {
|
||||
Svg {
|
||||
id: Id::unique(),
|
||||
#[cfg(feature = "a11y")]
|
||||
name: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
label: None,
|
||||
handle: handle.into(),
|
||||
width: Length::Fill,
|
||||
height: Length::Shrink,
|
||||
|
|
@ -81,6 +102,7 @@ where
|
|||
rotation: Rotation::default(),
|
||||
opacity: 1.0,
|
||||
status: None,
|
||||
symbolic: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -116,6 +138,13 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Symbolic icons inherit their color from the renderer if a color is not defined.
|
||||
#[must_use]
|
||||
pub fn symbolic(mut self, symbolic: bool) -> Self {
|
||||
self.symbolic = symbolic;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the style of the [`Svg`].
|
||||
#[must_use]
|
||||
pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self
|
||||
|
|
@ -148,6 +177,41 @@ where
|
|||
self.opacity = opacity.into();
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the name of the [`Button`].
|
||||
pub fn name(mut self, name: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.name = Some(name.into());
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description_widget<T: iced_accessibility::Describes>(
|
||||
mut self,
|
||||
description: &T,
|
||||
) -> Self {
|
||||
self.description = Some(iced_accessibility::Description::Id(
|
||||
description.description(),
|
||||
));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description(mut self, description: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.description =
|
||||
Some(iced_accessibility::Description::Text(description.into()));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the label of the [`Button`].
|
||||
pub fn label(mut self, label: &dyn iced_accessibility::Labels) -> Self {
|
||||
self.label =
|
||||
Some(label.label().into_iter().map(|l| l.into()).collect());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||
|
|
@ -226,9 +290,9 @@ where
|
|||
_state: &Tree,
|
||||
renderer: &mut Renderer,
|
||||
theme: &Theme,
|
||||
_style: &renderer::Style,
|
||||
renderer_style: &renderer::Style,
|
||||
layout: Layout<'_>,
|
||||
_cursor: mouse::Cursor,
|
||||
cursor: mouse::Cursor,
|
||||
_viewport: &Rectangle,
|
||||
) {
|
||||
let Size { width, height } = renderer.measure_svg(&self.handle);
|
||||
|
|
@ -257,19 +321,100 @@ where
|
|||
|
||||
let drawing_bounds = Rectangle::new(position, final_size);
|
||||
|
||||
let style =
|
||||
theme.style(&self.class, self.status.unwrap_or(Status::Idle));
|
||||
let is_mouse_over = cursor.is_over(bounds);
|
||||
let status = if is_mouse_over {
|
||||
Status::Hovered
|
||||
} else {
|
||||
Status::Idle
|
||||
};
|
||||
|
||||
renderer.draw_svg(
|
||||
svg::Svg {
|
||||
handle: self.handle.clone(),
|
||||
color: style.color,
|
||||
rotation: self.rotation.radians(),
|
||||
opacity: self.opacity,
|
||||
},
|
||||
drawing_bounds,
|
||||
bounds,
|
||||
let mut style = theme.style(&self.class, self.status.unwrap_or(status));
|
||||
|
||||
if self.symbolic && style.color.is_none() {
|
||||
style.color = Some(renderer_style.icon_color);
|
||||
}
|
||||
|
||||
let render = |renderer: &mut Renderer| {
|
||||
renderer.draw_svg(
|
||||
svg::Svg {
|
||||
handle: self.handle.clone(),
|
||||
color: style.color,
|
||||
rotation: self.rotation.radians(),
|
||||
opacity: self.opacity,
|
||||
border_radius: [0., 0., 0., 0.],
|
||||
},
|
||||
drawing_bounds,
|
||||
bounds,
|
||||
);
|
||||
};
|
||||
|
||||
if adjusted_fit.width > bounds.width
|
||||
|| adjusted_fit.height > bounds.height
|
||||
{
|
||||
renderer.with_layer(bounds, render);
|
||||
} else {
|
||||
render(renderer);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
_state: &Tree,
|
||||
_cursor: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
use iced_accessibility::{
|
||||
A11yTree,
|
||||
accesskit::{Node, NodeId, Rect, Role},
|
||||
};
|
||||
|
||||
let bounds = layout.bounds();
|
||||
let Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
} = bounds;
|
||||
let bounds = Rect::new(
|
||||
x as f64,
|
||||
y as f64,
|
||||
(x + width) as f64,
|
||||
(y + height) as f64,
|
||||
);
|
||||
let mut node = Node::new(Role::Image);
|
||||
node.set_bounds(bounds);
|
||||
if let Some(name) = self.name.as_ref() {
|
||||
node.set_label(name.clone());
|
||||
}
|
||||
match self.description.as_ref() {
|
||||
Some(iced_accessibility::Description::Id(id)) => {
|
||||
node.set_described_by(
|
||||
id.iter()
|
||||
.cloned()
|
||||
.map(|id| NodeId::from(id))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
Some(iced_accessibility::Description::Text(text)) => {
|
||||
node.set_description(text.clone());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
||||
if let Some(label) = self.label.as_ref() {
|
||||
node.set_labelled_by(label.clone());
|
||||
}
|
||||
|
||||
A11yTree::leaf(node, self.id.clone())
|
||||
}
|
||||
|
||||
fn id(&self) -> Option<Id> {
|
||||
Some(self.id.clone())
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
self.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -226,8 +226,8 @@ where
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut widget::Tree) {
|
||||
tree.diff_children(&self.cells);
|
||||
fn diff(&mut self, tree: &mut widget::Tree) {
|
||||
tree.diff_children(&mut self.cells);
|
||||
}
|
||||
|
||||
fn layout(
|
||||
|
|
|
|||
|
|
@ -653,7 +653,7 @@ where
|
|||
tree::State::new(State::<Renderer::Paragraph>::new())
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
let state = tree.state.downcast_mut::<State<Renderer::Paragraph>>();
|
||||
|
||||
// Stop pasting if input becomes disabled
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
use crate::container;
|
||||
|
||||
use crate::core::event;
|
||||
use crate::core::layout;
|
||||
use crate::core::mouse;
|
||||
use crate::core::overlay;
|
||||
|
|
@ -75,8 +77,8 @@ where
|
|||
self.content.as_widget().children()
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut Tree) {
|
||||
self.content.as_widget().diff(tree);
|
||||
fn diff(&mut self, tree: &mut Tree) {
|
||||
self.content.as_widget_mut().diff(tree);
|
||||
}
|
||||
|
||||
fn size(&self) -> Size<Length> {
|
||||
|
|
@ -159,7 +161,9 @@ where
|
|||
|
||||
let style = if let Some(text_color) = self.text_color {
|
||||
renderer::Style {
|
||||
text_color: text_color(theme),
|
||||
text_color: text_color(&theme),
|
||||
icon_color: style.icon_color, // TODO(POP): Is this correct?
|
||||
scale_factor: style.scale_factor, // TODO(POP): Is this correct?
|
||||
}
|
||||
} else {
|
||||
*style
|
||||
|
|
|
|||
|
|
@ -30,6 +30,12 @@
|
|||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
//! Show toggle controls using togglers.
|
||||
#[cfg(feature = "a11y")]
|
||||
use std::borrow::Cow;
|
||||
|
||||
use iced_runtime::core::border::Radius;
|
||||
|
||||
use crate::core::alignment;
|
||||
use crate::core::border;
|
||||
use crate::core::layout;
|
||||
|
|
@ -37,12 +43,14 @@ use crate::core::mouse;
|
|||
use crate::core::renderer;
|
||||
use crate::core::text;
|
||||
use crate::core::touch;
|
||||
use crate::core::widget;
|
||||
use crate::core::widget::tree::{self, Tree};
|
||||
use crate::core::window;
|
||||
use crate::core::{
|
||||
Background, Border, Clipboard, Color, Element, Event, Layout, Length,
|
||||
Pixels, Rectangle, Shell, Size, Theme, Widget,
|
||||
Pixels, Rectangle, Shell, Size, Theme, Widget, id,
|
||||
};
|
||||
use crate::core::{
|
||||
widget::{self, Id},
|
||||
window,
|
||||
};
|
||||
|
||||
/// A toggler widget.
|
||||
|
|
@ -86,6 +94,14 @@ pub struct Toggler<
|
|||
Theme: Catalog,
|
||||
Renderer: text::Renderer,
|
||||
{
|
||||
id: Id,
|
||||
label_id: Option<Id>,
|
||||
#[cfg(feature = "a11y")]
|
||||
name: Option<Cow<'a, str>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: Option<iced_accessibility::Description<'a>>,
|
||||
#[cfg(feature = "a11y")]
|
||||
labeled_by_widget: Option<Vec<iced_accessibility::accesskit::NodeId>>,
|
||||
is_toggled: bool,
|
||||
on_toggle: Option<Box<dyn Fn(bool) -> Message + 'a>>,
|
||||
label: Option<text::Fragment<'a>>,
|
||||
|
|
@ -115,11 +131,16 @@ where
|
|||
/// It expects:
|
||||
/// * a boolean describing whether the [`Toggler`] is checked or not
|
||||
/// * An optional label for the [`Toggler`]
|
||||
/// * a function that will be called when the [`Toggler`] is toggled. It
|
||||
/// will receive the new state of the [`Toggler`] and must produce a
|
||||
/// `Message`.
|
||||
pub fn new(is_toggled: bool) -> Self {
|
||||
Toggler {
|
||||
id: Id::unique(),
|
||||
label_id: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
name: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
description: None,
|
||||
#[cfg(feature = "a11y")]
|
||||
labeled_by_widget: None,
|
||||
is_toggled,
|
||||
on_toggle: None,
|
||||
label: None,
|
||||
|
|
@ -128,9 +149,9 @@ where
|
|||
text_size: None,
|
||||
text_line_height: text::LineHeight::default(),
|
||||
text_alignment: text::Alignment::Default,
|
||||
text_shaping: text::Shaping::default(),
|
||||
text_wrapping: text::Wrapping::default(),
|
||||
spacing: Self::DEFAULT_SIZE / 2.0,
|
||||
text_shaping: text::Shaping::Advanced,
|
||||
font: None,
|
||||
class: Theme::default(),
|
||||
last_status: None,
|
||||
|
|
@ -140,6 +161,7 @@ where
|
|||
/// Sets the label of the [`Toggler`].
|
||||
pub fn label(mut self, label: impl text::IntoFragment<'a>) -> Self {
|
||||
self.label = Some(label.into_fragment());
|
||||
self.label_id = Some(Id::unique());
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -246,6 +268,44 @@ where
|
|||
self.class = class.into();
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the name of the [`Button`].
|
||||
pub fn name(mut self, name: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.name = Some(name.into());
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description_widget<T: iced_accessibility::Describes>(
|
||||
mut self,
|
||||
description: &T,
|
||||
) -> Self {
|
||||
self.description = Some(iced_accessibility::Description::Id(
|
||||
description.description(),
|
||||
));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the description of the [`Button`].
|
||||
pub fn description(mut self, description: impl Into<Cow<'a, str>>) -> Self {
|
||||
self.description =
|
||||
Some(iced_accessibility::Description::Text(description.into()));
|
||||
self
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// Sets the label of the [`Button`] using another widget.
|
||||
pub fn labeled_by_widget(
|
||||
mut self,
|
||||
label: &dyn iced_accessibility::Labels,
|
||||
) -> Self {
|
||||
self.labeled_by_widget =
|
||||
Some(label.label().into_iter().map(|l| l.into()).collect());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||
|
|
@ -279,12 +339,8 @@ where
|
|||
|
||||
layout::next_to_each_other(
|
||||
&limits,
|
||||
if self.label.is_some() {
|
||||
self.spacing
|
||||
} else {
|
||||
0.0
|
||||
},
|
||||
|_| layout::Node::new(Size::new(2.0 * self.size, self.size)),
|
||||
self.spacing,
|
||||
|_| layout::Node::new(crate::core::Size::new(48., 24.)),
|
||||
|limits| {
|
||||
if let Some(label) = self.label.as_deref() {
|
||||
let state = tree
|
||||
|
|
@ -309,7 +365,7 @@ where
|
|||
},
|
||||
)
|
||||
} else {
|
||||
layout::Node::new(Size::ZERO)
|
||||
layout::Node::new(crate::core::Size::ZERO)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
|
@ -393,7 +449,7 @@ where
|
|||
theme: &Theme,
|
||||
defaults: &renderer::Style,
|
||||
layout: Layout<'_>,
|
||||
_cursor: mouse::Cursor,
|
||||
cursor: mouse::Cursor,
|
||||
viewport: &Rectangle,
|
||||
) {
|
||||
let mut children = layout.children();
|
||||
|
|
@ -424,15 +480,38 @@ where
|
|||
}
|
||||
|
||||
let bounds = toggler_layout.bounds();
|
||||
let border_radius = style
|
||||
.border_radius
|
||||
.unwrap_or_else(|| border::Radius::new(bounds.height / 2.0));
|
||||
let is_mouse_over = cursor.is_over(layout.bounds());
|
||||
|
||||
let status = if self.on_toggle.is_none() {
|
||||
Status::Disabled {
|
||||
is_toggled: self.is_toggled,
|
||||
}
|
||||
} else if is_mouse_over {
|
||||
Status::Hovered {
|
||||
is_toggled: self.is_toggled,
|
||||
}
|
||||
} else {
|
||||
Status::Active {
|
||||
is_toggled: self.is_toggled,
|
||||
}
|
||||
};
|
||||
|
||||
let style = theme.style(&self.class, status);
|
||||
|
||||
let space = style.handle_margin;
|
||||
|
||||
let toggler_background_bounds = Rectangle {
|
||||
x: bounds.x,
|
||||
y: bounds.y,
|
||||
width: bounds.width,
|
||||
height: bounds.height,
|
||||
};
|
||||
|
||||
renderer.fill_quad(
|
||||
renderer::Quad {
|
||||
bounds,
|
||||
border: Border {
|
||||
radius: border_radius,
|
||||
radius: style.border_radius,
|
||||
width: style.background_border_width,
|
||||
color: style.background_border_color,
|
||||
},
|
||||
|
|
@ -445,20 +524,20 @@ where
|
|||
let toggler_foreground_bounds = Rectangle {
|
||||
x: bounds.x
|
||||
+ if self.is_toggled {
|
||||
bounds.width - bounds.height + padding
|
||||
bounds.width - space - (bounds.height - (2.0 * space))
|
||||
} else {
|
||||
padding
|
||||
space
|
||||
},
|
||||
y: bounds.y + padding,
|
||||
width: bounds.height - (2.0 * padding),
|
||||
height: bounds.height - (2.0 * padding),
|
||||
y: bounds.y + space,
|
||||
width: bounds.height - (2.0 * space),
|
||||
height: bounds.height - (2.0 * space),
|
||||
};
|
||||
|
||||
renderer.fill_quad(
|
||||
renderer::Quad {
|
||||
bounds: toggler_foreground_bounds,
|
||||
border: Border {
|
||||
radius: border_radius,
|
||||
radius: style.handle_radius,
|
||||
width: style.foreground_border_width,
|
||||
color: style.foreground_border_color,
|
||||
},
|
||||
|
|
@ -467,6 +546,103 @@ where
|
|||
style.foreground,
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "a11y")]
|
||||
/// get the a11y nodes for the widget
|
||||
fn a11y_nodes(
|
||||
&self,
|
||||
layout: Layout<'_>,
|
||||
_state: &Tree,
|
||||
cursor: mouse::Cursor,
|
||||
) -> iced_accessibility::A11yTree {
|
||||
use iced_accessibility::{
|
||||
A11yNode, A11yTree,
|
||||
accesskit::{Action, Node, NodeId, Rect, Role},
|
||||
};
|
||||
|
||||
let bounds = layout.bounds();
|
||||
let is_hovered = cursor.is_over(bounds);
|
||||
let Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
} = bounds;
|
||||
|
||||
let bounds = Rect::new(
|
||||
x as f64,
|
||||
y as f64,
|
||||
(x + width) as f64,
|
||||
(y + height) as f64,
|
||||
);
|
||||
|
||||
let mut node = Node::new(Role::Switch);
|
||||
node.add_action(Action::Focus);
|
||||
node.add_action(Action::Click);
|
||||
node.set_bounds(bounds);
|
||||
if let Some(name) = self.name.as_ref() {
|
||||
node.set_label(name.clone());
|
||||
}
|
||||
match self.description.as_ref() {
|
||||
Some(iced_accessibility::Description::Id(id)) => {
|
||||
node.set_described_by(
|
||||
id.iter()
|
||||
.cloned()
|
||||
.map(|id| NodeId::from(id))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
Some(iced_accessibility::Description::Text(text)) => {
|
||||
node.set_description(text.clone());
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
node.set_selected(self.is_toggled);
|
||||
// TODO hover
|
||||
// if is_hovered {
|
||||
// node.set_hovered();
|
||||
// }
|
||||
node.add_action(Action::Click);
|
||||
if let Some(label) = self.label.as_ref() {
|
||||
let mut label_node = Node::new(Role::Label);
|
||||
|
||||
label_node.set_label(label.clone());
|
||||
// TODO proper label bounds for the label
|
||||
label_node.set_bounds(bounds);
|
||||
|
||||
A11yTree::node_with_child_tree(
|
||||
A11yNode::new(node, self.id.clone()),
|
||||
A11yTree::leaf(label_node, self.label_id.clone().unwrap()),
|
||||
)
|
||||
} else {
|
||||
if let Some(labeled_by_widget) = self.labeled_by_widget.as_ref() {
|
||||
node.set_labelled_by(labeled_by_widget.clone());
|
||||
}
|
||||
A11yTree::leaf(node, self.id.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn id(&self) -> Option<Id> {
|
||||
if self.label.is_some() {
|
||||
Some(Id(iced_runtime::core::id::Internal::Set(vec![
|
||||
self.id.0.clone(),
|
||||
self.label_id.clone().unwrap().0,
|
||||
])))
|
||||
} else {
|
||||
Some(self.id.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn set_id(&mut self, id: Id) {
|
||||
if let Id(id::Internal::Set(list)) = id {
|
||||
if list.len() == 2 && self.label.is_some() {
|
||||
self.id.0 = list[0].clone();
|
||||
self.label_id = Some(Id(list[1].clone()));
|
||||
}
|
||||
} else if self.label.is_none() {
|
||||
self.id = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Message, Theme, Renderer> From<Toggler<'a, Message, Theme, Renderer>>
|
||||
|
|
@ -518,14 +694,16 @@ pub struct Style {
|
|||
pub foreground_border_width: f32,
|
||||
/// The [`Color`] of the foreground border of the toggler.
|
||||
pub foreground_border_color: Color,
|
||||
/// The text [`Color`] of the toggler.
|
||||
pub text_color: Option<Color>,
|
||||
/// The border radius of the toggler.
|
||||
///
|
||||
/// If `None`, the toggler will be perfectly round.
|
||||
pub border_radius: Option<border::Radius>,
|
||||
pub border_radius: Radius,
|
||||
/// the radius of the handle of the toggler
|
||||
pub handle_radius: Radius,
|
||||
/// the space between the handle and the border of the toggler
|
||||
pub handle_margin: f32,
|
||||
/// The ratio of separation between the background and the toggle in relative height.
|
||||
pub padding_ratio: f32,
|
||||
/// The text [`Color`] of the toggler.
|
||||
pub text_color: Option<Color>,
|
||||
}
|
||||
|
||||
/// The theme catalog of a [`Toggler`].
|
||||
|
|
@ -606,8 +784,10 @@ pub fn default(theme: &Theme, status: Status) -> Style {
|
|||
foreground_border_color: Color::TRANSPARENT,
|
||||
background_border_width: 0.0,
|
||||
background_border_color: Color::TRANSPARENT,
|
||||
text_color: None,
|
||||
border_radius: None,
|
||||
border_radius: Radius::from(8.0),
|
||||
handle_radius: Radius::from(8.0),
|
||||
handle_margin: 2.0,
|
||||
padding_ratio: 0.1,
|
||||
text_color: None,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -169,13 +169,6 @@ where
|
|||
]
|
||||
}
|
||||
|
||||
fn diff(&self, tree: &mut widget::Tree) {
|
||||
tree.diff_children(&[
|
||||
self.content.as_widget(),
|
||||
self.tooltip.as_widget(),
|
||||
]);
|
||||
}
|
||||
|
||||
fn state(&self) -> widget::tree::State {
|
||||
widget::tree::State::new(State::default())
|
||||
}
|
||||
|
|
@ -192,6 +185,13 @@ where
|
|||
self.content.as_widget().size_hint()
|
||||
}
|
||||
|
||||
fn diff(&mut self, tree: &mut widget::Tree) {
|
||||
tree.diff_children(&mut [
|
||||
self.content.as_widget_mut(),
|
||||
self.tooltip.as_widget_mut(),
|
||||
])
|
||||
}
|
||||
|
||||
fn layout(
|
||||
&mut self,
|
||||
tree: &mut widget::Tree,
|
||||
|
|
@ -568,7 +568,9 @@ where
|
|||
container::draw_background(renderer, &style, layout.bounds());
|
||||
|
||||
let defaults = renderer::Style {
|
||||
icon_color: inherited_style.icon_color,
|
||||
text_color: style.text_color.unwrap_or(inherited_style.text_color),
|
||||
scale_factor: inherited_style.scale_factor,
|
||||
};
|
||||
|
||||
self.tooltip.as_widget().draw(
|
||||
|
|
|
|||
|
|
@ -31,7 +31,8 @@
|
|||
use std::ops::RangeInclusive;
|
||||
|
||||
pub use crate::slider::{
|
||||
Catalog, Handle, HandleShape, Status, Style, StyleFn, default,
|
||||
Catalog, Handle, HandleShape, RailBackground, Status, Style, StyleFn,
|
||||
default,
|
||||
};
|
||||
|
||||
use crate::core::border::Border;
|
||||
|
|
@ -448,7 +449,8 @@ where
|
|||
HandleShape::Rectangle {
|
||||
width,
|
||||
border_radius,
|
||||
} => (f32::from(width), bounds.width, border_radius),
|
||||
height,
|
||||
} => (f32::from(width), f32::from(height), border_radius),
|
||||
};
|
||||
|
||||
let value = self.value.into() as f32;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue