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:
Ashley Wulber 2023-05-02 15:48:20 -07:00
parent 595af03a9f
commit 08fe1f3aa5
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
233 changed files with 24391 additions and 1911 deletions

View file

@ -18,6 +18,8 @@ advanced = []
crisp = []
basic-shaping = []
advanced-shaping = []
a11y = ["iced_accessibility"]
wayland = ["sctk"]
[dependencies]
bitflags.workspace = true
@ -34,3 +36,22 @@ web-time.workspace = true
serde.workspace = true
serde.optional = true
serde.features = ["derive"]
# TODO(POP): I think some of these dependencies were removed. Check on that
# xxhash-rust.workspace = true
window_clipboard.workspace = true
dnd.workspace = true
mime.workspace = true
sctk.workspace = true
sctk.optional = true
# /TODO(POP)
[dependencies.iced_accessibility]
version = "0.1.0"
path = "../accessibility"
optional = true
[target.'cfg(windows)'.dependencies]
raw-window-handle.workspace = true
[dev-dependencies]
approx = "0.5"

View file

@ -276,3 +276,54 @@ impl std::ops::Mul<f32> for Radius {
}
}
}
impl From<[f32; 4]> for Radius {
/// [
/// radi.top_left,
/// radi.top_right,
/// radi.bottom_right,
/// radi.bottom_left,
/// ]
fn from(value: [f32; 4]) -> Self {
Self {
top_left: value[0],
top_right: value[1],
bottom_right: value[2],
bottom_left: value[3],
}
}
}
impl From<[u8; 4]> for Radius {
/// [
/// radi.top_left,
/// radi.top_right,
/// radi.bottom_right,
/// radi.bottom_left,
/// ]
fn from(value: [u8; 4]) -> Self {
Self {
top_left: f32::from(value[0]),
top_right: f32::from(value[1]),
bottom_right: f32::from(value[2]),
bottom_left: f32::from(value[3]),
}
}
}
impl From<[u16; 4]> for Radius {
/// [
/// radi.top_left,
/// radi.top_right,
/// radi.bottom_right,
/// radi.bottom_left,
/// ]
fn from(value: [u16; 4]) -> Self {
Self {
top_left: f32::from(value[0]),
top_right: f32::from(value[1]),
bottom_right: f32::from(value[2]),
bottom_left: f32::from(value[3]),
}
}
}

View file

@ -1,5 +1,61 @@
//! Access the clipboard.
use std::{any::Any, sync::Arc};
use dnd::{DndAction, DndDestinationRectangle, DndSurface};
use mime::{self, AllowedMimeTypes, AsMimeTypes, ClipboardStoreData};
use crate::{Element, Vector, widget::tree::State, window};
#[derive(Debug)]
pub struct IconSurface<E> {
pub element: E,
pub state: State,
pub offset: Vector,
}
pub type DynIconSurface = IconSurface<Box<dyn Any>>;
impl<T: 'static, R: 'static> IconSurface<Element<'static, (), T, R>> {
pub fn new(
element: Element<'static, (), T, R>,
state: State,
offset: Vector,
) -> Self {
Self {
element,
state,
offset,
}
}
fn upcast(self) -> DynIconSurface {
IconSurface {
element: Box::new(self.element),
state: self.state,
offset: self.offset,
}
}
}
impl DynIconSurface {
/// Downcast `element` to concrete type `Element<(), T, R>`
///
/// Panics if type doesn't match
pub fn downcast<T: 'static, R: 'static>(
self,
) -> IconSurface<Element<'static, (), T, R>> {
IconSurface {
element: *self
.element
.downcast()
.expect("drag-and-drop icon surface has invalid element type"),
state: self.state,
offset: self.offset,
}
}
}
/// A buffer for short-term storage and transfer within and between
/// applications.
pub trait Clipboard {
@ -8,6 +64,62 @@ pub trait Clipboard {
/// Writes the given text contents to the [`Clipboard`].
fn write(&mut self, kind: Kind, contents: String);
/// Consider using [`read_data`] instead
/// Reads the current content of the [`Clipboard`] as text.
fn read_data(
&self,
kind: Kind,
_mimes: Vec<String>,
) -> Option<(Vec<u8>, String)> {
None
}
/// Writes the given contents to the [`Clipboard`].
fn write_data(
&mut self,
kind: Kind,
_contents: ClipboardStoreData<
Box<dyn Send + Sync + 'static + mime::AsMimeTypes>,
>,
) {
}
/// Starts a DnD operation.
fn register_dnd_destination(
&self,
_surface: DndSurface,
_rectangles: Vec<DndDestinationRectangle>,
) {
}
/// Set the final action for the DnD operation.
/// Only should be done if it is requested.
fn set_action(&self, _action: DndAction) {}
/// Registers Dnd destinations
fn start_dnd(
&mut self,
_internal: bool,
_source_surface: Option<DndSource>,
_icon_surface: Option<DynIconSurface>,
_content: Box<dyn AsMimeTypes + Send + 'static>,
_actions: DndAction,
) {
}
/// Ends a DnD operation.
fn end_dnd(&self) {}
/// Consider using [`peek_dnd`] instead
/// Peeks the data on the DnD with a specific mime type.
/// Will return an error if there is no ongoing DnD operation.
fn peek_dnd(&self, _mime: String) -> Option<(Vec<u8>, String)> {
None
}
/// Request window size
fn request_logical_window_size(&self, width: f32, height: f32) {}
}
/// The kind of [`Clipboard`].
@ -21,6 +133,25 @@ pub enum Kind {
Primary,
}
/// Starts a DnD operation.
/// icon surface is a tuple of the icon element and optionally the icon element state.
pub fn start_dnd<T: 'static, R: 'static>(
clipboard: &mut dyn Clipboard,
internal: bool,
source_surface: Option<DndSource>,
icon_surface: Option<IconSurface<Element<'static, (), T, R>>>,
content: Box<dyn AsMimeTypes + Send + 'static>,
actions: DndAction,
) {
clipboard.start_dnd(
internal,
source_surface,
icon_surface.map(IconSurface::upcast),
content,
actions,
);
}
/// A null implementation of the [`Clipboard`] trait.
#[derive(Debug, Clone, Copy)]
pub struct Null;
@ -32,3 +163,90 @@ impl Clipboard for Null {
fn write(&mut self, _kind: Kind, _contents: String) {}
}
/// Reads the current content of the [`Clipboard`].
pub fn read_data<T: AllowedMimeTypes>(
clipboard: &mut dyn Clipboard,
) -> Option<T> {
clipboard
.read_data(Kind::Standard, T::allowed().into())
.and_then(|data| T::try_from(data).ok())
}
/// Reads the current content of the primary [`Clipboard`].
pub fn read_primary_data<T: AllowedMimeTypes>(
clipboard: &mut dyn Clipboard,
) -> Option<T> {
clipboard
.read_data(Kind::Primary, T::allowed().into())
.and_then(|data| T::try_from(data).ok())
}
/// Reads the current content of the primary [`Clipboard`].
pub fn peek_dnd<T: AllowedMimeTypes>(
clipboard: &mut dyn Clipboard,
mime: Option<String>,
) -> Option<T> {
let Some(mime) = mime.or_else(|| T::allowed().first().cloned().into())
else {
return None;
};
clipboard
.peek_dnd(mime)
.and_then(|data| T::try_from(data).ok())
}
/// Source of a DnD operation.
#[derive(Debug, Clone)]
pub enum DndSource {
/// A widget is the source of the DnD operation.
Widget(crate::id::Id),
/// A surface is the source of the DnD operation.
Surface(window::Id),
}
/// A list of DnD destination rectangles.
#[derive(Debug, Clone)]
pub struct DndDestinationRectangles {
/// The rectangle of the DnD destination.
rectangles: Vec<DndDestinationRectangle>,
}
impl DndDestinationRectangles {
/// Creates a new [`DndDestinationRectangles`].
pub fn new() -> Self {
Self {
rectangles: Vec::new(),
}
}
/// Creates a new [`DndDestinationRectangles`] with the given capacity.
pub fn with_capacity(capacity: usize) -> Self {
Self {
rectangles: Vec::with_capacity(capacity),
}
}
/// Pushes a new rectangle to the list of DnD destination rectangles.
pub fn push(&mut self, rectangle: DndDestinationRectangle) {
self.rectangles.push(rectangle);
}
/// Appends the list of DnD destination rectangles to the current list.
pub fn append(&mut self, other: &mut Vec<DndDestinationRectangle>) {
self.rectangles.append(other);
}
/// Returns the list of DnD destination rectangles.
/// This consumes the [`DndDestinationRectangles`].
pub fn into_rectangles(mut self) -> Vec<DndDestinationRectangle> {
self.rectangles.reverse();
self.rectangles
}
}
impl AsRef<[DndDestinationRectangle]> for DndDestinationRectangles {
fn as_ref(&self) -> &[DndDestinationRectangle] {
&self.rectangles
}
}

View file

@ -1,3 +1,5 @@
use crate::event::{self, Event};
use crate::id::Id;
use crate::layout;
use crate::mouse;
use crate::overlay;
@ -5,11 +7,11 @@ use crate::renderer;
use crate::widget;
use crate::widget::tree::{self, Tree};
use crate::{
Border, Clipboard, Color, Event, Layout, Length, Rectangle, Shell, Size,
Vector, Widget,
Border, Clipboard, Color, Layout, Length, Rectangle, Shell, Size, Vector,
Widget,
};
use std::borrow::Borrow;
use std::borrow::{Borrow, BorrowMut};
/// A generic [`Widget`].
///
@ -238,6 +240,37 @@ impl<'a, Message, Theme, Renderer>
}
}
impl<'a, Message, Theme, Renderer>
Borrow<dyn Widget<Message, Theme, Renderer> + 'a>
for &mut Element<'a, Message, Theme, Renderer>
{
fn borrow(&self) -> &(dyn Widget<Message, Theme, Renderer> + 'a) {
self.widget.borrow()
}
}
impl<'a, Message, Theme, Renderer>
BorrowMut<dyn Widget<Message, Theme, Renderer> + 'a>
for &mut Element<'a, Message, Theme, Renderer>
{
fn borrow_mut(
&mut self,
) -> &mut (dyn Widget<Message, Theme, Renderer> + 'a) {
self.widget.borrow_mut()
}
}
impl<'a, Message, Theme, Renderer>
BorrowMut<dyn Widget<Message, Theme, Renderer> + 'a>
for Element<'a, Message, Theme, Renderer>
{
fn borrow_mut(
&mut self,
) -> &mut (dyn Widget<Message, Theme, Renderer> + 'a) {
self.widget.borrow_mut()
}
}
struct Map<'a, A, B, Theme, Renderer> {
widget: Box<dyn Widget<A, Theme, Renderer> + 'a>,
mapper: Box<dyn Fn(A) -> B + 'a>,
@ -277,8 +310,8 @@ where
self.widget.children()
}
fn diff(&self, tree: &mut Tree) {
self.widget.diff(tree);
fn diff(&mut self, tree: &mut Tree) {
self.widget.diff(tree)
}
fn size(&self) -> Size<Length> {
@ -376,6 +409,35 @@ where
.overlay(tree, layout, renderer, viewport, translation)
.map(move |overlay| overlay.map(mapper))
}
#[cfg(feature = "a11y")]
fn a11y_nodes(
&self,
_layout: Layout<'_>,
_state: &Tree,
_cursor_position: mouse::Cursor,
) -> iced_accessibility::A11yTree {
self.widget.a11y_nodes(_layout, _state, _cursor_position)
}
fn id(&self) -> Option<Id> {
self.widget.id()
}
fn set_id(&mut self, id: Id) {
self.widget.set_id(id);
}
fn drag_destinations(
&self,
state: &Tree,
layout: Layout<'_>,
renderer: &Renderer,
dnd_rectangles: &mut crate::clipboard::DndDestinationRectangles,
) {
self.widget
.drag_destinations(state, layout, renderer, dnd_rectangles);
}
}
struct Explain<'a, Message, Theme, Renderer: crate::Renderer> {
@ -420,7 +482,7 @@ where
self.element.widget.children()
}
fn diff(&self, tree: &mut Tree) {
fn diff(&mut self, tree: &mut Tree) {
self.element.widget.diff(tree);
}
@ -532,8 +594,31 @@ where
translation,
)
}
}
fn id(&self) -> Option<Id> {
self.element.widget.id()
}
fn set_id(&mut self, id: Id) {
self.element.widget.set_id(id);
}
fn drag_destinations(
&self,
state: &Tree,
layout: Layout<'_>,
renderer: &Renderer,
dnd_rectangles: &mut crate::clipboard::DndDestinationRectangles,
) {
self.element.widget.drag_destinations(
state,
layout,
renderer,
dnd_rectangles,
);
}
// TODO maybe a11y_nodes
}
impl<'a, T, Message, Theme, Renderer> From<Option<T>>
for Element<'a, Message, Theme, Renderer>
where

View file

@ -1,10 +1,15 @@
//! Handle events of a user interface.
use crate::input_method;
use dnd::DndEvent;
use dnd::DndSurface;
use crate::keyboard;
use crate::mouse;
use crate::touch;
use crate::window;
#[cfg(feature = "wayland")]
/// A platform specific event for wayland
pub mod wayland;
/// A user interface event.
///
/// _**Note:** This type is largely incomplete! If you need to track
@ -27,6 +32,26 @@ pub enum Event {
/// An input method event
InputMethod(input_method::Event),
#[cfg(feature = "a11y")]
/// An Accesskit event for a specific Accesskit Node in an accessible widget
A11y(
crate::widget::Id,
iced_accessibility::accesskit::ActionRequest,
),
/// A DnD event.
Dnd(DndEvent<DndSurface>),
/// Platform specific events
PlatformSpecific(PlatformSpecific),
}
/// A platform specific event
#[derive(Debug, Clone, PartialEq)]
pub enum PlatformSpecific {
#[cfg(feature = "wayland")]
/// A Wayland specific event
Wayland(wayland::Event),
}
/// The status of an [`Event`] after being processed.

View file

@ -0,0 +1,10 @@
/// layer surface events
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LayerEvent {
/// layer surface Done
Done,
/// layer surface focused
Focused,
/// layer_surface unfocused
Unfocused,
}

View file

@ -0,0 +1,39 @@
mod layer;
mod output;
mod popup;
mod seat;
mod session_lock;
mod window;
use crate::{time::Instant, window::Id};
use sctk::reexports::client::protocol::{
wl_output::WlOutput, wl_seat::WlSeat, wl_surface::WlSurface,
};
pub use layer::*;
pub use output::*;
pub use popup::*;
pub use seat::*;
pub use session_lock::*;
pub use window::*;
/// wayland events
#[derive(Debug, Clone, PartialEq)]
pub enum Event {
/// layer surface event
Layer(LayerEvent, WlSurface, Id),
/// popup event
Popup(PopupEvent, WlSurface, Id),
/// output event
Output(OutputEvent, WlOutput),
/// window event
Window(WindowEvent),
/// Seat Event
Seat(SeatEvent, WlSeat),
/// Session lock events
SessionLock(SessionLockEvent),
/// Frame events
Frame(Instant, WlSurface, Id),
/// Request Resize
RequestResize,
}

View file

@ -0,0 +1,34 @@
use sctk::output::OutputInfo;
/// output events
#[derive(Debug, Clone)]
pub enum OutputEvent {
/// created output
Created(Option<OutputInfo>),
/// removed output
Removed,
/// Output Info
InfoUpdate(OutputInfo),
}
impl Eq for OutputEvent {}
impl PartialEq for OutputEvent {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Created(l0), Self::Created(r0)) => {
if let Some((l0, r0)) = l0.as_ref().zip(r0.as_ref()) {
l0.id == r0.id && l0.make == r0.make && l0.model == r0.model
} else {
l0.is_none() && r0.is_none()
}
}
(Self::InfoUpdate(l0), Self::InfoUpdate(r0)) => {
l0.id == r0.id && l0.make == r0.make && l0.model == r0.model
}
_ => {
core::mem::discriminant(self) == core::mem::discriminant(other)
}
}
}
}

View file

@ -0,0 +1,21 @@
/// popup events
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PopupEvent {
/// Done
Done,
/// repositioned,
Configured {
/// x position
x: i32,
/// y position
y: i32,
/// width
width: u32,
/// height
height: u32,
},
/// popup focused
Focused,
/// popup unfocused
Unfocused,
}

View file

@ -0,0 +1,9 @@
/// seat events
/// Only one seat can interact with an iced_sctk application at a time, but many may interact with the application over the lifetime of the application
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SeatEvent {
/// A new seat is interacting with the application
Enter,
/// A seat is not interacting with the application anymore
Leave,
}

View file

@ -0,0 +1,19 @@
use crate::window::Id;
use sctk::reexports::client::protocol::wl_surface::WlSurface;
/// session lock events
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SessionLockEvent {
/// Compositor has activated lock
Locked,
/// Lock rejected / canceled by compositor
Finished,
/// Session lock protocol not supported
NotSupported,
/// Session lock surface focused
Focused(WlSurface, Id),
/// Session lock surface unfocused
Unfocused(WlSurface, Id),
/// Session unlock has been processed by server
Unlocked,
}

View file

@ -0,0 +1,8 @@
#![allow(missing_docs)]
/// window events
#[derive(Debug, PartialEq, Clone)]
pub enum WindowEvent {
/// Window suggested bounds.
SuggestedBounds(Option<crate::Size>),
}

170
core/src/id.rs Normal file
View file

@ -0,0 +1,170 @@
//! Widget and Window IDs.
use std::borrow;
use std::num::NonZeroU128;
use std::sync::atomic::{self, AtomicU64};
static NEXT_ID: AtomicU64 = AtomicU64::new(1);
static NEXT_WINDOW_ID: AtomicU64 = AtomicU64::new(1);
/// The identifier of a generic widget.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Id(pub Internal);
impl Id {
/// Creates a custom [`Id`].
pub fn new(id: impl Into<borrow::Cow<'static, str>>) -> Self {
Self(Internal::Custom(Self::next(), id.into()))
}
/// resets the id counter
pub fn reset() {
NEXT_ID.store(1, atomic::Ordering::Relaxed);
}
fn next() -> u64 {
NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)
}
/// Creates a unique [`Id`].
///
/// This function produces a different [`Id`] every time it is called.
pub fn unique() -> Self {
let id = Self::next();
Self(Internal::Unique(id))
}
}
impl From<&'static str> for Id {
fn from(value: &'static str) -> Self {
Id(Internal::Custom(Id::next(), borrow::Cow::Borrowed(value)))
}
}
impl<'a> From<String> for Id {
fn from(value: String) -> Self {
Id(Internal::Custom(
Id::next(),
borrow::Cow::Owned(value.to_string()),
))
}
}
// Not meant to be used directly
impl From<u64> for Id {
fn from(value: u64) -> Self {
Self(Internal::Unique(value))
}
}
// Not meant to be used directly
impl From<Id> for NonZeroU128 {
fn from(id: Id) -> NonZeroU128 {
match &id.0 {
Internal::Unique(id) => NonZeroU128::try_from(*id as u128).unwrap(),
Internal::Custom(id, _) => {
NonZeroU128::try_from(*id as u128).unwrap()
}
// this is a set id, which is not a valid id and will not ever be converted to a NonZeroU128
// so we panic
Internal::Set(_) => {
panic!("Cannot convert a set id to a NonZeroU128")
}
}
}
}
impl ToString for Id {
fn to_string(&self) -> String {
match &self.0 {
Internal::Unique(_) => "Undefined".to_string(),
Internal::Custom(_, id) => id.to_string(),
Internal::Set(_) => "Set".to_string(),
}
}
}
// XXX WIndow IDs are made unique by adding u64::MAX to them
/// get window node id that won't conflict with other node ids for the duration of the program
pub fn window_node_id() -> NonZeroU128 {
std::num::NonZeroU128::try_from(
u64::MAX as u128
+ NEXT_WINDOW_ID.fetch_add(1, atomic::Ordering::Relaxed) as u128,
)
.unwrap()
}
// TODO refactor to make panic impossible?
#[derive(Debug, Clone, Eq)]
/// Internal representation of an [`Id`].
pub enum Internal {
/// a unique id
Unique(u64),
/// a custom id, which is equal to any [`Id`] with a matching number or string
Custom(u64, borrow::Cow<'static, str>),
/// XXX Do not use this as an id for an accessibility node, it will panic!
/// XXX Only meant to be used for widgets that have multiple accessibility nodes, each with a
/// unique or custom id
/// an Id Set, which is equal to any [`Id`] with a matching number or string
Set(Vec<Self>),
}
impl PartialEq for Internal {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Unique(l0), Self::Unique(r0)) => l0 == r0,
(Self::Custom(_, l1), Self::Custom(_, r1)) => l1 == r1,
(Self::Set(l0), Self::Set(r0)) => l0 == r0,
_ => false,
}
}
}
/// Similar to PartialEq, but only intended for use when comparing Ids
pub trait IdEq {
/// Compare two Ids for equality based on their number or name
fn eq(&self, other: &Self) -> bool;
}
impl IdEq for Internal {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Unique(l0), Self::Unique(r0)) => l0 == r0,
(Self::Custom(l0, l1), Self::Custom(r0, r1)) => {
l0 == r0 || l1 == r1
}
// allow custom ids to be equal to unique ids
(Self::Unique(l0), Self::Custom(r0, _))
| (Self::Custom(l0, _), Self::Unique(r0)) => l0 == r0,
(Self::Set(l0), Self::Set(r0)) => l0 == r0,
// allow set ids to just be equal to any of their members
(Self::Set(l0), r) | (r, Self::Set(l0)) => {
l0.iter().any(|l| l == r)
}
}
}
}
impl std::hash::Hash for Internal {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
Self::Unique(id) => id.hash(state),
Self::Custom(name, _) => name.hash(state),
Self::Set(ids) => ids.hash(state),
}
}
}
#[cfg(test)]
mod tests {
use super::Id;
#[test]
fn unique_generates_different_ids() {
let a = Id::unique();
let b = Id::unique();
assert_ne!(a, b);
}
}

View file

@ -48,6 +48,7 @@ impl Image<Handle> {
border_radius: border::Radius::default(),
opacity: 1.0,
snap: false,
// border_radius: [0.0; 4],
}
}
@ -83,7 +84,7 @@ impl From<&Handle> for Image {
}
/// A handle of some image data.
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Handle {
/// A file handle. The image data will be read
/// from the file path.

View file

@ -7,6 +7,7 @@ use crate::SmolStr;
///
/// [`winit`]: https://docs.rs/winit/0.30/winit/keyboard/enum.Key.html
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Key<C = SmolStr> {
/// A key with an established name.
Named(Named),
@ -129,6 +130,7 @@ impl From<Named> for Key {
///
/// [`winit`]: https://docs.rs/winit/0.30/winit/keyboard/enum.Key.html
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(missing_docs)]
pub enum Named {
/// The `Alt` (Alternative) key.

View file

@ -37,6 +37,8 @@ mod background;
mod color;
mod content_fit;
mod element;
#[cfg(not(feature = "a11y"))]
pub mod id;
mod length;
mod pixels;
mod point;
@ -61,6 +63,9 @@ pub use element::Element;
pub use event::Event;
pub use font::Font;
pub use gradient::Gradient;
#[cfg(feature = "a11y")]
pub use iced_accessibility::id;
pub use image::Image;
pub use input_method::InputMethod;
pub use layout::Layout;

View file

@ -83,7 +83,6 @@ impl Click {
} else {
None
};
self.position.distance(new_position) < 6.0
&& duration
.map(|duration| duration.as_millis() <= 300)

View file

@ -10,7 +10,7 @@ pub use nested::Nested;
use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::widget;
use crate::widget::Operation;
use crate::widget::Tree;
use crate::{Clipboard, Event, Layout, Rectangle, Shell, Size, Vector};
@ -37,12 +37,12 @@ where
cursor: mouse::Cursor,
);
/// Applies a [`widget::Operation`] to the [`Overlay`].
/// Applies an [`Operation`] to the [`Overlay`].
fn operate(
&mut self,
_layout: Layout<'_>,
_renderer: &Renderer,
_operation: &mut dyn widget::Operation,
_operation: &mut dyn crate::widget::Operation,
) {
}

View file

@ -3,7 +3,8 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::widget;
use crate::{Clipboard, Event, Layout, Overlay, Shell, Size};
use crate::widget::Operation;
use crate::{Clipboard, Event, Layout, Overlay, Point, Rectangle, Shell, Size};
/// An [`Overlay`] container that displays multiple overlay [`overlay::Element`]
/// children.

View file

@ -213,6 +213,17 @@ impl From<[u16; 2]> for Padding {
}
}
impl From<[u16; 4]> for Padding {
fn from(p: [u16; 4]) -> Self {
Padding {
top: f32::from(p[0]),
right: f32::from(p[1]),
bottom: f32::from(p[2]),
left: f32::from(p[3]),
}
}
}
impl From<f32> for Padding {
fn from(p: f32) -> Self {
Padding {
@ -235,6 +246,18 @@ impl From<[f32; 2]> for Padding {
}
}
impl From<[f32; 4]> for Padding {
/// [top, rght, bottom, left]
fn from(p: [f32; 4]) -> Self {
Padding {
top: p[0],
right: p[1],
bottom: p[2],
left: p[3],
}
}
}
impl From<Padding> for Size {
fn from(padding: Padding) -> Self {
Self::new(padding.x(), padding.y())

View file

@ -188,6 +188,18 @@ impl Rectangle<f32> {
/// Returns true if the current [`Rectangle`] is within the given
/// `container`. Includes the right and bottom edges.
/// Returns true if the given [`Point`] is contained in the [`Rectangle`].
/// The [`Point`] must be strictly contained, i.e. it must not be on the
/// border.
pub fn contains_strict(&self, point: Point) -> bool {
self.x < point.x
&& point.x < self.x + self.width
&& self.y < point.y
&& point.y < self.y + self.height
}
/// Returns true if the current [`Rectangle`] is completely within the given
/// `container`.
pub fn is_within(&self, container: &Rectangle) -> bool {
self.x >= container.x
&& self.y >= container.y
@ -195,6 +207,16 @@ impl Rectangle<f32> {
&& self.y + self.height <= container.y + container.height
}
/// Returns true if the current [`Rectangle`] is completely within the given
/// `container`. The [`Rectangle`] must be strictly contained, i.e. it must
/// not be on the border.
pub fn is_within_strict(&self, container: &Rectangle) -> bool {
container.contains_strict(self.position())
&& container.contains_strict(
self.position() + Vector::new(self.width, self.height),
)
}
/// Computes the intersection with the given [`Rectangle`].
pub fn intersection(
&self,

View file

@ -104,14 +104,20 @@ impl Default for Quad {
/// The styling attributes of a [`Renderer`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Style {
/// The color to apply to symbolic icons.
pub icon_color: Color,
/// The text color
pub text_color: Color,
/// The scale factor
pub scale_factor: f64,
}
impl Default for Style {
fn default() -> Self {
Style {
icon_color: Color::BLACK,
text_color: Color::BLACK,
scale_factor: 1.0,
}
}
}

View file

@ -41,6 +41,7 @@ impl text::Renderer for () {
type Font = Font;
type Paragraph = ();
type Editor = ();
type Raw = ();
const ICON_FONT: Font = Font::DEFAULT;
const CHECKMARK_ICON: char = '0';
@ -56,7 +57,7 @@ impl text::Renderer for () {
}
fn default_size(&self) -> Pixels {
Pixels(16.0)
Pixels(14.0)
}
fn fill_paragraph(
@ -77,6 +78,8 @@ impl text::Renderer for () {
) {
}
fn fill_raw(&mut self, _raw: Self::Raw) {}
fn fill_text(
&mut self,
_paragraph: Text,

View file

@ -40,6 +40,9 @@ pub struct Settings {
///
/// By default, it is enabled.
pub vsync: bool,
/// If set to true the application will exit when the main window is closed.
pub exit_on_close_request: bool,
}
impl Default for Settings {
@ -48,9 +51,20 @@ impl Default for Settings {
id: None,
fonts: Vec::new(),
default_font: Font::default(),
default_text_size: Pixels(16.0),
antialiasing: true,
vsync: true,
vsync: false,
default_text_size: Pixels(14.0),
antialiasing: false,
exit_on_close_request: false,
}
}
}
#[cfg(feature = "winit")]
impl From<Settings> for iced_winit::Settings {
fn from(settings: Settings) -> iced_winit::Settings {
iced_winit::Settings {
id: settings.id,
fonts: settings.fonts,
}
}
}

View file

@ -29,6 +29,9 @@ pub struct Svg<H = Handle> {
///
/// 0 means transparent. 1 means opaque.
pub opacity: f32,
/// The border radius for the svg
pub border_radius: [f32; 4],
}
impl Svg<Handle> {
@ -39,6 +42,7 @@ impl Svg<Handle> {
color: None,
rotation: Radians(0.0),
opacity: 1.0,
border_radius: [0.0; 4],
}
}
@ -59,6 +63,12 @@ impl Svg<Handle> {
self.opacity = opacity.into();
self
}
/// Sets the border radius of the [`Svg`]
pub fn border_radius(mut self, border_radius: impl Into<[f32; 4]>) -> Self {
self.border_radius = border_radius.into();
self
}
}
impl From<&Handle> for Svg {

View file

@ -214,7 +214,7 @@ impl LineHeight {
impl Default for LineHeight {
fn default() -> Self {
Self::Relative(1.3)
Self::Relative(1.4)
}
}
@ -299,6 +299,9 @@ pub trait Renderer: crate::Renderer {
/// The [`Editor`] of this [`Renderer`].
type Editor: Editor<Font = Self::Font> + 'static;
/// The Raw of this [`Renderer`].
type Raw;
/// The icon font of the backend.
const ICON_FONT: Self::Font;
@ -363,6 +366,9 @@ pub trait Renderer: crate::Renderer {
clip_bounds: Rectangle,
);
/// Draws the given Raw
fn fill_raw(&mut self, raw: Self::Raw);
/// Draws the given [`Text`] at the given position and with the given
/// [`Color`].
fn fill_text(

View file

@ -227,6 +227,9 @@ pub struct Style {
/// The default text [`Color`] of the application.
pub text_color: Color,
/// The default icon [`iced_core::Color`] of the application.
pub icon_color: Color,
}
/// The default blank style of a theme.
@ -332,5 +335,6 @@ pub fn default(theme: &Theme) -> Style {
Style {
background_color: palette.background.base.color,
text_color: palette.background.base.text,
icon_color: palette.background.base.text,
}
}

View file

@ -421,12 +421,15 @@ impl Extended {
}
}
/// A pair of background and text colors.
/// Recommended background, icon, and text [`Color`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Pair {
/// The background color.
pub color: Color,
/// The icon color, which defaults to the text color.
pub icon: Color,
/// The text color.
///
/// It's guaranteed to be readable on top of the background [`color`].
@ -438,9 +441,12 @@ pub struct Pair {
impl Pair {
/// Creates a new [`Pair`] from a background [`Color`] and some text [`Color`].
pub fn new(color: Color, text: Color) -> Self {
let text = readable(color, text);
Self {
color,
text: readable(color, text),
icon: text,
text,
}
}
}

View file

@ -3,9 +3,7 @@ pub mod operation;
pub mod text;
pub mod tree;
mod id;
pub use id::Id;
pub use crate::id::Id;
pub use operation::Operation;
pub use text::Text;
pub use tree::Tree;
@ -92,7 +90,7 @@ where
}
/// Reconciles the [`Widget`] with the provided [`Tree`].
fn diff(&self, tree: &mut Tree) {
fn diff(&mut self, tree: &mut Tree) {
tree.children.clear();
}
@ -147,4 +145,35 @@ where
) -> Option<overlay::Element<'a, Message, Theme, Renderer>> {
None
}
#[cfg(feature = "a11y")]
/// get the a11y nodes for the widget and its children
fn a11y_nodes(
&self,
_layout: Layout<'_>,
_state: &Tree,
_cursor: mouse::Cursor,
) -> iced_accessibility::A11yTree {
iced_accessibility::A11yTree::default()
}
/// Returns the id of the widget
fn id(&self) -> Option<Id> {
None
}
/// Sets the id of the widget
/// This may be called while diffing the widget tree
fn set_id(&mut self, _id: Id) {}
/// Adds the drag destination rectangles of the widget.
/// Runs after the layout phase for each widget in the tree.
fn drag_destinations(
&self,
_state: &Tree,
_layout: Layout<'_>,
_renderer: &Renderer,
_dnd_rectangles: &mut crate::clipboard::DndDestinationRectangles,
) {
}
}

View file

@ -1,6 +1,7 @@
//! Query or update internal widget state.
pub mod focusable;
pub mod scrollable;
pub mod search_id;
pub mod text_input;
pub use focusable::Focusable;
@ -525,7 +526,7 @@ where
/// Produces an [`Operation`] that applies the given [`Operation`] to the
/// children of a container with the given [`Id`].
pub fn scope<T: 'static>(
pub fn scoped<T: 'static>(
target: Id,
operation: impl Operation<T> + 'static,
) -> impl Operation<T> {

View file

@ -1,5 +1,6 @@
//! Operate on widgets that can be focused.
use crate::Rectangle;
use crate::id::IdEq;
use crate::widget::Id;
use crate::widget::operation::{self, Operation, Outcome};
@ -39,7 +40,7 @@ pub fn focus<T>(target: Id) -> impl Operation<T> {
state: &mut dyn Focusable,
) {
match id {
Some(id) if id == &self.target => {
Some(id) if IdEq::eq(&id.0, &self.target.0) => {
state.focus();
}
_ => {

View file

@ -0,0 +1,49 @@
//! Search for widgets with the target Id.
use super::Operation;
use crate::{
Rectangle,
id::Id,
widget::operation::{Outcome, focusable::Count},
};
/// Produces an [`Operation`] that searches for the Id
pub fn search_id(target: Id) -> impl Operation<Id> {
struct Find {
found: bool,
target: Id,
}
impl Operation<Id> for Find {
fn custom(
&mut self,
id: Option<&Id>,
_bounds: Rectangle,
_state: &mut dyn std::any::Any,
) {
if Some(&self.target) == id {
self.found = true;
}
}
fn finish(&self) -> Outcome<Id> {
if self.found {
Outcome::Some(self.target.clone())
} else {
Outcome::None
}
}
fn traverse(
&mut self,
operate: &mut dyn FnMut(&mut dyn Operation<Id>),
) {
operate(self);
}
}
Find {
found: false,
target,
}
}

View file

@ -24,13 +24,14 @@ use crate::alignment;
use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::text;
use crate::text::paragraph::{self, Paragraph};
use crate::text::{self, Fragment};
use crate::widget::tree::{self, Tree};
use crate::{
Color, Element, Layout, Length, Pixels, Rectangle, Size, Theme, Widget,
};
use std::borrow::Cow;
pub use text::{Alignment, LineHeight, Shaping, Wrapping};
/// A bunch of text.
@ -60,6 +61,7 @@ where
Theme: Catalog,
Renderer: text::Renderer,
{
id: crate::widget::Id,
fragment: text::Fragment<'a>,
format: Format<Renderer::Font>,
class: Theme::Class<'a>,
@ -73,6 +75,7 @@ where
/// Create a new fragment of [`Text`] with the given contents.
pub fn new(fragment: impl text::IntoFragment<'a>) -> Self {
Text {
id: crate::widget::Id::unique(),
fragment: fragment.into_fragment(),
format: Format::default(),
class: Theme::default(),
@ -263,6 +266,50 @@ where
) {
operation.text(None, layout.bounds(), &self.fragment);
}
#[cfg(feature = "a11y")]
fn a11y_nodes(
&self,
layout: Layout<'_>,
_state: &Tree,
_: mouse::Cursor,
) -> iced_accessibility::A11yTree {
use iced_accessibility::{
A11yTree,
accesskit::{Live, Node, Rect, Role},
};
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 mut node = Node::new(Role::Paragraph);
// TODO is the name likely different from the content?
node.set_label(self.fragment.to_string().into_boxed_str());
node.set_bounds(bounds);
// TODO make this configurable
node.set_live(Live::Polite);
A11yTree::leaf(node, self.id.clone())
}
fn id(&self) -> Option<crate::widget::Id> {
Some(self.id.clone())
}
fn set_id(&mut self, id: crate::widget::Id) {
self.id = id;
}
}
/// The format of some [`Text`].
@ -370,6 +417,29 @@ where
}
}
// impl<'a, Theme, Renderer> Clone for Text<'a, Theme, Renderer>
// where
// Renderer: text::Renderer,
// {
// fn clone(&self) -> Self {
// Self {
// id: self.id.clone(),
// content: self.content.clone(),
// size: self.size,
// line_height: self.line_height,
// width: self.width,
// height: self.height,
// horizontal_alignment: self.horizontal_alignment,
// vertical_alignment: self.vertical_alignment,
// font: self.font,
// style: self.style,
// shaping: self.shaping,
// wrap: self.wrap,
// }
// }
// }
// TODO(POP): Clone no longer can be implemented because of style being a Box(style)
impl<'a, Theme, Renderer> From<&'a str> for Text<'a, Theme, Renderer>
where
Theme: Catalog + 'a,

View file

@ -1,9 +1,16 @@
//! Store internal widget state in a state tree to ensure continuity.
use crate::Widget;
use crate::id::{Id, Internal};
use std::any::{self, Any};
use std::borrow::Borrow;
use std::fmt;
use std::borrow::{Borrow, BorrowMut, Cow};
use std::collections::HashMap;
use std::hash::Hash;
use std::{fmt, mem};
thread_local! {
/// A map of named widget states.
pub static NAMED: std::cell::RefCell<HashMap<Cow<'static, str>, (State, Vec<(usize, Tree)>)>> = std::cell::RefCell::new(HashMap::new());
}
/// A persistent state widget tree.
///
@ -13,6 +20,9 @@ pub struct Tree {
/// The tag of the [`Tree`].
pub tag: Tag,
/// the Id of the [`Tree`]
pub id: Option<Id>,
/// The [`State`] of the [`Tree`].
pub state: State,
@ -24,6 +34,7 @@ impl Tree {
/// Creates an empty, stateless [`Tree`] with no children.
pub fn empty() -> Self {
Self {
id: None,
tag: Tag::stateless(),
state: State::None,
children: Vec::new(),
@ -40,13 +51,104 @@ impl Tree {
let widget = widget.borrow();
Self {
id: widget.id(),
tag: widget.tag(),
state: widget.state(),
children: widget.children(),
}
}
/// Reconciles the current tree with the provided [`Widget`].
/// Takes all named widgets from the tree.
pub fn take_all_named(
&mut self,
) -> HashMap<Cow<'static, str>, (State, Vec<(usize, Tree)>)> {
let mut named = HashMap::new();
struct Visit {
parent: Cow<'static, str>,
index: usize,
visited: bool,
}
// tree traversal to find all named widgets
// and keep their state and children
let mut stack = vec![(self, None)];
while let Some((tree, visit)) = stack.pop() {
if let Some(Id(Internal::Custom(_, n))) = tree.id.clone() {
let state = mem::replace(&mut tree.state, State::None);
let children_count = tree.children.len();
let children =
tree.children.iter_mut().enumerate().rev().map(|(i, c)| {
if matches!(c.id, Some(Id(Internal::Custom(_, _)))) {
(c, None)
} else {
(
c,
Some(Visit {
index: i,
parent: n.clone(),
visited: false,
}),
)
}
});
_ = named.insert(
n.clone(),
(state, Vec::with_capacity(children_count)),
);
stack.extend(children);
} else if let Some(visit) = visit {
if visit.visited {
named.get_mut(&visit.parent).unwrap().1.push((
visit.index,
mem::replace(
tree,
Tree {
id: tree.id.clone(),
tag: tree.tag,
..Tree::empty()
},
),
));
} else {
let ptr = tree as *mut Tree;
stack.push((
// TODO remove this unsafe block
#[allow(unsafe_code)]
// SAFETY: when the reference is finally accessed, all the children references will have been processed first.
unsafe {
ptr.as_mut().unwrap()
},
Some(Visit {
visited: true,
..visit
}),
));
stack.extend(tree.children.iter_mut().map(|c| (c, None)));
}
} else {
stack.extend(tree.children.iter_mut().map(|s| (s, None)));
}
}
named
}
/// Finds a widget state in the tree by its id.
pub fn find<'a>(&'a self, id: &Id) -> Option<&'a Tree> {
if self.id == Some(id.clone()) {
return Some(self);
}
for child in self.children.iter() {
if let Some(tree) = child.find(id) {
return Some(tree);
}
}
None
}
/// Reconciliates the current tree with the provided [`Widget`].
///
/// If the tag of the [`Widget`] matches the tag of the [`Tree`], then the
/// [`Widget`] proceeds with the reconciliation (i.e. [`Widget::diff`] is called).
@ -56,53 +158,203 @@ impl Tree {
/// [`Widget::diff`]: crate::Widget::diff
pub fn diff<'a, Message, Theme, Renderer>(
&mut self,
new: impl Borrow<dyn Widget<Message, Theme, Renderer> + 'a>,
mut new: impl BorrowMut<dyn Widget<Message, Theme, Renderer> + 'a>,
) where
Renderer: crate::Renderer,
{
if self.tag == new.borrow().tag() {
new.borrow().diff(self);
let borrowed: &mut dyn Widget<Message, Theme, Renderer> =
new.borrow_mut();
let mut needs_reset = false;
let tag_match = self.tag == borrowed.tag();
if let Some(Id(Internal::Custom(_, n))) = borrowed.id() {
if let Some((mut state, children)) = NAMED
.with(|named| named.borrow_mut().remove(&n))
.or_else(|| {
//check self.id
if let Some(Id(Internal::Custom(_, ref name))) = self.id {
if name == &n {
Some((
mem::replace(&mut self.state, State::None),
self.children
.iter_mut()
.map(|s| {
// take the data
mem::replace(
s,
Tree {
id: s.id.clone(),
tag: s.tag,
..Tree::empty()
},
)
})
.enumerate()
.collect(),
))
} else {
None
}
} else {
None
}
})
{
std::mem::swap(&mut self.state, &mut state);
let widget_children = borrowed.children();
if !tag_match || self.children.len() != widget_children.len() {
self.children = borrowed.children();
} else {
for (old_i, mut old) in children {
let Some(my_state) = self.children.get_mut(old_i)
else {
continue;
};
if my_state.tag != old.tag || {
!match (&old.id, &my_state.id) {
(
Some(Id(Internal::Custom(_, old_name))),
Some(Id(Internal::Custom(_, my_name))),
) => old_name == my_name,
(
Some(Id(Internal::Set(a))),
Some(Id(Internal::Set(b))),
) => a.len() == b.len(),
(
Some(Id(Internal::Unique(_))),
Some(Id(Internal::Unique(_))),
) => true,
(None, None) => true,
_ => false,
}
} {
continue;
}
mem::swap(my_state, &mut old);
}
}
} else {
needs_reset = true;
}
} else if tag_match {
if let Some(id) = self.id.clone() {
borrowed.set_id(id);
}
if self.children.len() != borrowed.children().len() {
self.children = borrowed.children();
}
} else {
*self = Self::new(new);
needs_reset = true;
}
if needs_reset {
*self = Self::new(borrowed);
let borrowed = new.borrow_mut();
borrowed.diff(self);
} else {
borrowed.diff(self);
}
}
/// Reconciles the children of the tree with the provided list of widgets.
pub fn diff_children<'a, Message, Theme, Renderer>(
&mut self,
new_children: &[impl Borrow<dyn Widget<Message, Theme, Renderer> + 'a>],
new_children: &mut [impl BorrowMut<
dyn Widget<Message, Theme, Renderer> + 'a,
>],
) where
Renderer: crate::Renderer,
{
self.diff_children_custom(
new_children,
|tree, widget| tree.diff(widget.borrow()),
|widget| Self::new(widget.borrow()),
);
new_children.iter().map(|c| c.borrow().id()).collect(),
|tree, widget| {
let borrowed: &mut dyn Widget<_, _, _> = widget.borrow_mut();
tree.diff(borrowed)
},
|widget| {
let borrowed: &dyn Widget<_, _, _> = widget.borrow();
Self::new(borrowed)
},
)
}
/// Reconciles the children of the tree with the provided list of widgets using custom
/// logic both for diffing and creating new widget state.
pub fn diff_children_custom<T>(
&mut self,
new_children: &[T],
diff: impl Fn(&mut Tree, &T),
new_children: &mut [T],
new_ids: Vec<Option<Id>>,
diff: impl Fn(&mut Tree, &mut T),
new_state: impl Fn(&T) -> Self,
) {
if self.children.len() > new_children.len() {
self.children.truncate(new_children.len());
}
for (child_state, new) in
self.children.iter_mut().zip(new_children.iter())
let len_changed = self.children.len() != new_children.len();
let children_len = self.children.len();
let (mut id_map, mut id_list): (
HashMap<String, &mut Tree>,
Vec<&mut Tree>,
) = self.children.iter_mut().fold(
(HashMap::new(), Vec::with_capacity(children_len)),
|(mut id_map, mut id_list), c| {
if let Some(id) = c.id.as_ref() {
if let Internal::Custom(_, ref name) = id.0 {
let _ = id_map.insert(name.to_string(), c);
} else {
id_list.push(c);
}
} else {
id_list.push(c);
}
(id_map, id_list)
},
);
let mut child_state_i = 0;
let mut new_trees: Vec<(Tree, usize)> =
Vec::with_capacity(new_children.len());
for (i, (new, new_id)) in
new_children.iter_mut().zip(new_ids.iter()).enumerate()
{
let child_state = if let Some(c) = new_id.as_ref().and_then(|id| {
if let Internal::Custom(_, ref name) = id.0 {
id_map.remove(name.as_ref())
} else {
None
}
}) {
c
} else if child_state_i < id_list.len()
&& !matches!(
id_list[child_state_i].id,
Some(Id(Internal::Custom(_, _)))
)
{
let c = &mut id_list[child_state_i];
if len_changed {
c.id.clone_from(new_id);
}
child_state_i += 1;
c
} else {
let mut my_new_state = new_state(new);
diff(&mut my_new_state, new);
new_trees.push((my_new_state, i));
continue;
};
diff(child_state, new);
}
if self.children.len() < new_children.len() {
self.children.extend(
new_children[self.children.len()..].iter().map(new_state),
);
for (new_tree, i) in new_trees {
if self.children.len() > i {
self.children[i] = new_tree;
} else {
self.children.push(new_tree);
}
}
}
}
@ -114,8 +366,8 @@ impl Tree {
/// `maybe_changed` closure.
pub fn diff_children_custom_with_search<T>(
current_children: &mut Vec<Tree>,
new_children: &[T],
diff: impl Fn(&mut Tree, &T),
new_children: &mut [T],
diff: impl Fn(&mut Tree, &mut T),
maybe_changed: impl Fn(usize) -> bool,
new_state: impl Fn(&T) -> Tree,
) {
@ -183,7 +435,7 @@ pub fn diff_children_custom_with_search<T>(
// TODO: Merge loop with extend logic (?)
for (child_state, new) in
current_children.iter_mut().zip(new_children.iter())
current_children.iter_mut().zip(new_children.iter_mut())
{
diff(child_state, new);
}

View file

@ -53,7 +53,7 @@ pub enum Event {
/// ## Platform-specific
///
/// - **Wayland:** Not implemented.
FileHovered(PathBuf),
FileHovered(Vec<PathBuf>),
/// A file has been dropped into the window.
///
@ -63,7 +63,7 @@ pub enum Event {
/// ## Platform-specific
///
/// - **Wayland:** Not implemented.
FileDropped(PathBuf),
FileDropped(Vec<PathBuf>),
/// A file was hovered, but has exited the window.
///

View file

@ -10,9 +10,20 @@ pub struct Id(u64);
static COUNT: AtomicU64 = AtomicU64::new(1);
impl Id {
/// No window will match this Id
pub const NONE: Id = Id(0);
pub const RESERVED: Id = Id(1);
/// Creates a new unique window [`Id`].
pub fn unique() -> Id {
Id(COUNT.fetch_add(1, atomic::Ordering::Relaxed))
let id = Id(COUNT.fetch_add(1, atomic::Ordering::Relaxed));
if id.0 == 0 {
Id(COUNT.fetch_add(2, atomic::Ordering::Relaxed))
} else if id.0 == 1 {
Id(COUNT.fetch_add(1, atomic::Ordering::Relaxed))
} else {
id
}
}
}

View file

@ -5,7 +5,7 @@ use std::fmt::{Debug, Formatter};
/// Data of a screenshot, captured with `window::screenshot()`.
///
/// The `bytes` of this screenshot will always be ordered as `RGBA` in the `sRGB` color space.
/// The `bytes` of this screenshot will always be ordered as `RGBA` in the sRGB color space.
#[derive(Clone)]
pub struct Screenshot {
/// The RGBA bytes of the [`Screenshot`].

View file

@ -101,8 +101,8 @@ pub struct Settings {
}
impl Default for Settings {
fn default() -> Self {
Self {
fn default() -> Settings {
Settings {
size: Size::new(1024.0, 768.0),
maximized: false,
fullscreen: false,