Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions druid/src/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<'a> DelegateCtx<'a> {
/// the [`update()`] method is called.
///
/// [`Command`]: struct.Command.html
/// [`update()`]: widget/trait.Widget.html#tymethod.update
/// [`update()`]: trait.Widget.html#tymethod.update
pub fn submit_command(
&mut self,
command: impl Into<Command>,
Expand Down Expand Up @@ -62,7 +62,7 @@ pub trait AppDelegate<T: Data> {
/// be the event that was passed in, a different event, or no event. In all cases,
/// the [`update()`] method will be called as usual.
///
/// [`update()`]: widget/trait.Widget.html#tymethod.update
/// [`update()`]: trait.Widget.html#tymethod.update
fn event(
&mut self,
ctx: &mut DelegateCtx,
Expand Down
2 changes: 1 addition & 1 deletion druid/src/box_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use log;
/// Further, a container widget should compute appropriate constraints
/// for each of its child widgets, and pass those down when recursing.
///
/// [`layout`]: widget/trait.Widget.html#tymethod.layout
/// [`layout`]: trait.Widget.html#tymethod.layout
/// [Flutter BoxConstraints]: https://api.flutter.dev/flutter/rendering/BoxConstraints-class.html
#[derive(Clone, Copy, Debug)]
pub struct BoxConstraints {
Expand Down
14 changes: 7 additions & 7 deletions druid/src/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct EventCtx<'a> {
/// specific lifecycle events; for instance [`register_child`]
/// should only be called while handling [`LifeCycle::WidgetAdded`].
///
/// [`lifecycle`]: widget/trait.Widget.html#tymethod.lifecycle
/// [`lifecycle`]: trait.Widget.html#tymethod.lifecycle
/// [`register_child`]: #method.register_child
/// [`LifeCycle::WidgetAdded`]: enum.LifeCycle.html#variant.WidgetAdded
pub struct LifeCycleCtx<'a> {
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<'a> EventCtx<'a> {

/// Request a [`paint`] pass.
///
/// [`paint`]: widget/trait.Widget.html#tymethod.paint
/// [`paint`]: trait.Widget.html#tymethod.paint
pub fn request_paint(&mut self) {
self.base_state.needs_inval = true;
}
Expand All @@ -147,7 +147,7 @@ impl<'a> EventCtx<'a> {
/// (such as if it would like to change the layout of children in
/// response to some event) it must call this method.
///
/// [`layout`]: widget/trait.Widget.html#tymethod.layout
/// [`layout`]: trait.Widget.html#tymethod.layout
pub fn request_layout(&mut self) {
self.base_state.needs_layout = true;
self.base_state.needs_inval = true;
Expand Down Expand Up @@ -327,7 +327,7 @@ impl<'a> EventCtx<'a> {
/// Generally it will be the same as the size returned by the child widget's
/// [`layout`] method.
///
/// [`layout`]: widget/trait.Widget.html#tymethod.layout
/// [`layout`]: trait.Widget.html#tymethod.layout
pub fn size(&self) -> Size {
self.base_state.size()
}
Expand All @@ -339,7 +339,7 @@ impl<'a> EventCtx<'a> {
/// the [`update`] method is called.
///
/// [`Command`]: struct.Command.html
/// [`update`]: widget/trait.Widget.html#tymethod.update
/// [`update`]: trait.Widget.html#tymethod.update
pub fn submit_command(
&mut self,
command: impl Into<Command>,
Expand Down Expand Up @@ -376,7 +376,7 @@ impl<'a> LifeCycleCtx<'a> {

/// Request a [`paint`] pass.
///
/// [`paint`]: widget/trait.Widget.html#tymethod.paint
/// [`paint`]: trait.Widget.html#tymethod.paint
pub fn request_paint(&mut self) {
self.base_state.needs_inval = true;
}
Expand Down Expand Up @@ -430,7 +430,7 @@ impl<'a> LifeCycleCtx<'a> {
/// the [`update`] method is called.
///
/// [`Command`]: struct.Command.html
/// [`update`]: widget/trait.Widget.html#tymethod.update
/// [`update`]: trait.Widget.html#tymethod.update
pub fn submit_command(
&mut self,
command: impl Into<Command>,
Expand Down
18 changes: 9 additions & 9 deletions druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) type CommandQueue = VecDeque<(Target, Command)>;
/// needs to propagate, and to provide the previous data so that a
/// widget can process a diff between the old value and the new.
///
/// [`update`]: widget/trait.Widget.html#tymethod.update
/// [`update`]: trait.Widget.html#tymethod.update
pub struct WidgetPod<T, W> {
state: BaseState,
old_data: Option<T>,
Expand All @@ -61,7 +61,7 @@ pub struct WidgetPod<T, W> {
/// that, widgets will generally not interact with it directly,
/// but it is an important part of the [`WidgetPod`] struct.
///
/// [`paint`]: widget/trait.Widget.html#tymethod.paint
/// [`paint`]: trait.Widget.html#tymethod.paint
/// [`WidgetPod`]: struct.WidgetPod.html
#[derive(Clone)]
pub(crate) struct BaseState {
Expand Down Expand Up @@ -218,7 +218,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
///
/// [`Insets`]: struct.Insets.html
/// [`set_paint_insets`]: struct.LayoutCtx.html#method.set_paint_insets
/// [`layout`]: widget/trait.Widget.html#tymethod.layout
/// [`layout`]: trait.Widget.html#tymethod.layout
pub fn paint_insets(&self) -> Insets {
self.state.paint_insets
}
Expand All @@ -231,7 +231,7 @@ impl<T, W: Widget<T>> WidgetPod<T, W> {
/// propogate a child's desired paint rect, if it extends beyond the bounds
/// of the parent's layout rect.
///
/// [`layout`]: widget/trait.Widget.html#tymethod.layout
/// [`layout`]: trait.Widget.html#tymethod.layout
/// [`Insets`]: struct.Insets.html
pub fn compute_parent_paint_insets(&self, parent_size: Size) -> Insets {
let parent_bounds = Rect::ZERO.with_size(parent_size);
Expand All @@ -249,8 +249,8 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
/// Note that this method does not apply the offset of the layout rect.
/// If that is desired, use [`paint_with_offset`] instead.
///
/// [`layout`]: widget/trait.Widget.html#tymethod.layout
/// [`paint`]: widget/trait.Widget.html#tymethod.paint
/// [`layout`]: trait.Widget.html#tymethod.layout
/// [`paint`]: trait.Widget.html#tymethod.paint
/// [`paint_with_offset`]: #method.paint_with_offset
pub fn paint(&mut self, ctx: &mut PaintCtx, data: &T, env: &Env) {
let mut inner_ctx = PaintCtx {
Expand Down Expand Up @@ -315,7 +315,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
/// Generally called by container widgets as part of their [`layout`]
/// method.
///
/// [`layout`]: widget/trait.Widget.html#tymethod.layout
/// [`layout`]: trait.Widget.html#tymethod.layout
pub fn layout(
&mut self,
layout_ctx: &mut LayoutCtx,
Expand Down Expand Up @@ -348,7 +348,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
/// flow logic resides, particularly whether to continue propagating
/// the event.
///
/// [`event`]: widget/trait.Widget.html#tymethod.event
/// [`event`]: trait.Widget.html#tymethod.event
pub fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
if self.old_data.is_none() {
log::error!(
Expand Down Expand Up @@ -571,7 +571,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
/// Generally called by container widgets as part of their [`update`]
/// method.
///
/// [`update`]: widget/trait.Widget.html#tymethod.update
/// [`update`]: trait.Widget.html#tymethod.update
pub fn update(&mut self, ctx: &mut UpdateCtx, data: &T, env: &Env) {
match (self.old_data.as_ref(), self.env.as_ref()) {
(Some(d), Some(e)) if d.same(data) && e.same(env) => return,
Expand Down
4 changes: 2 additions & 2 deletions druid/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Env {
///
/// Set by the `debug_paint_layout()` method on [`WidgetExt`]'.
///
/// [`WidgetExt`]: widget/trait.WidgetExt.html
/// [`WidgetExt`]: trait.WidgetExt.html
pub(crate) const DEBUG_PAINT: Key<bool> = Key::new("druid.built-in.debug-paint");

/// A key used to tell widgets to print additional debug information.
Expand All @@ -140,7 +140,7 @@ impl Env {
/// }
/// ```
///
/// [`WidgetExt::debug_widget`]: widget/trait.WidgetExt.html#method.debug_widget
/// [`WidgetExt::debug_widget`]: trait.WidgetExt.html#method.debug_widget
pub const DEBUG_WIDGET: Key<bool> = Key::new("druid.built-in.debug-widget");

/// Gets a value from the environment, expecting it to be present.
Expand Down
4 changes: 2 additions & 2 deletions druid/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{Command, Target, WidgetId};
/// This enum is expected to grow considerably, as there are many, many
/// different kinds of events that are relevant in a GUI.
///
/// [`event`]: widget/trait.Widget.html#tymethod.event
/// [`event`]: trait.Widget.html#tymethod.event
/// [`WidgetPod`]: struct.WidgetPod.html
#[derive(Debug, Clone)]
pub enum Event {
Expand Down Expand Up @@ -124,7 +124,7 @@ pub enum Event {
/// [`Widget`]s, at runtime, with [`EventCtx::submit_command`].
///
/// [`Command`]: struct.Command.html
/// [`Widget`]: widget/trait.Widget.html
/// [`Widget`]: trait.Widget.html
/// [`EventCtx::submit_command`]: struct.EventCtx.html#method.submit_command
Command(Command),
/// A command still in the process of being dispatched. This is an internal
Expand Down
42 changes: 2 additions & 40 deletions druid/src/lens.rs → druid/src/lens/lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Support for lenses, a way of focusing on subfields of data.
//!
//! Lenses are useful whenever a widget only needs access to a subfield of a larger struct or
//! generally access to part of a larger value.
//!
//! For example: If one wants to embed a [`TextBox`] in a widget with a `Data` type
//! that is not `String`, they need to specify how to access a `String` from within the `Data`.
//!
//! [`TextBox`]: ../widget/struct.TextBox.html
//! ```
//! use druid::{Data, Lens, Widget, WidgetExt, widget::{TextBox, Flex}};
//!
//! #[derive(Clone, Debug, Data, Lens)]
//! struct MyState {
//! search_term: String,
//! scale: f64,
//! // ...
//! }
//!
//!
//! fn my_sidebar() -> impl Widget<MyState> {
//! // `TextBox` is of type `Widget<String>`
//! // via `.lens` we get it to be of type `Widget<MyState>`.
//! // `MyState::search_term` is a lens generated by the `derive(Lens)` macro,
//! // that provides access to the search_term field.
//! let searchbar = TextBox::new().lens(MyState::search_term);
//!
//! // ...
//!
//! // We can now use `searchbar` just like any other `Widget<MyState>`
//! Flex::column().with_child(searchbar)
//! }
//! ```

use std::marker::PhantomData;
use std::ops;
use std::sync::Arc;

pub use druid_derive::Lens;

use crate::kurbo::Size;
use crate::{
BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx,
UpdateCtx, Widget, WidgetId,
};
use crate::widget::prelude::*;
use crate::Data;

/// A lens is a datatype that gives access to a part of a larger
/// data structure.
Expand Down
53 changes: 53 additions & 0 deletions druid/src/lens/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2020 The xi-editor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Support for lenses, a way of focusing on subfields of data.
//!
//! Lenses are useful whenever a widget only needs access to a subfield of a larger struct or
//! generally access to part of a larger value.
//!
//! For example: If one wants to embed a [`TextBox`] in a widget with a `Data` type
//! that is not `String`, they need to specify how to access a `String` from within the `Data`.
//!
//! [`TextBox`]: ../widget/struct.TextBox.html
//! ```
//! use druid::{Data, Lens, Widget, WidgetExt, widget::{TextBox, Flex}};
//!
//! #[derive(Clone, Debug, Data, Lens)]
//! struct MyState {
//! search_term: String,
//! scale: f64,
//! // ...
//! }
//!
//!
//! fn my_sidebar() -> impl Widget<MyState> {
//! // `TextBox` is of type `Widget<String>`
//! // via `.lens` we get it to be of type `Widget<MyState>`.
//! // `MyState::search_term` is a lens generated by the `derive(Lens)` macro,
//! // that provides access to the search_term field.
//! let searchbar = TextBox::new().lens(MyState::search_term);
//!
//! // ...
//!
//! // We can now use `searchbar` just like any other `Widget<MyState>`
//! Flex::column().with_child(searchbar)
//! }
//! ```

#[allow(clippy::module_inception)]
mod lens;
pub use lens::{Deref, Field, Id, InArc, Index, Map, Then};
#[doc(hidden)]
pub use lens::{Lens, LensExt, LensWrap};
8 changes: 5 additions & 3 deletions druid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@
//! }
//! ```
//!
//! [`Widget`]: widget/trait.Widget.html
//! [`Widget`]: trait.Widget.html
//! [`Data`]: trait.Data.html
//! [`Lens`]: lens/trait.Lens.html
//! [`Lens`]: trait.Lens.html
//! [`widget`]: ./widget/index.html
//! [`Event`]: enum.Event.html
//! [`druid-shell`]: https://docs.rs/druid-shell
Expand All @@ -104,8 +104,10 @@

// Allows to use macros from druid_derive in this crate
extern crate self as druid;
pub use druid_derive::Lens;

use druid_shell as shell;
#[doc(inline)]
pub use druid_shell::{kurbo, piet};

mod app;
Expand Down Expand Up @@ -146,7 +148,7 @@ pub use app::{AppLauncher, WindowDesc};
pub use app_delegate::{AppDelegate, DelegateCtx};
pub use box_constraints::BoxConstraints;
pub use command::{sys as commands, Command, Selector, Target};
pub use contexts::{EventCtx, LayoutCtx, LifeCycleCtx, PaintCtx, UpdateCtx};
pub use contexts::{EventCtx, LayoutCtx, LifeCycleCtx, PaintCtx, Region, UpdateCtx};
pub use data::Data;
pub use env::{Env, Key, KeyOrValue, Value, ValueType};
pub use event::{Event, LifeCycle, WheelEvent};
Expand Down
2 changes: 1 addition & 1 deletion druid/src/widget/click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{Data, Env, Event, EventCtx, LifeCycle, LifeCycleCtx, Widget};
///
/// [`Controller`]: struct.Controller.html
/// [`ControllerHost`]: struct.ControllerHost.html
/// [`WidgetExt`]: trait.WidgetExt.html
/// [`WidgetExt`]: ../trait.WidgetExt.html
/// [`Button`]: struct.Button.html
/// [`LifeCycle::HotChanged`]: ../enum.LifeCycle.html#variant.HotChanged
pub struct Click<T> {
Expand Down
6 changes: 3 additions & 3 deletions druid/src/widget/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ use crate::{
/// }
/// ```
///
/// [`Widget`]: trait.Widget.html
/// [`Widget`]: ../trait.Widget.html
/// [`TextBox`]: struct.TextBox.html
/// [`ControllerHost`]: struct.ControllerHost.html
/// [`WidgetExt::controller`]: trait.WidgetExt.html#tymethod.controller
/// [`WidgetExt::controller`]: ../trait.WidgetExt.html#tymethod.controller
pub trait Controller<T, W: Widget<T>> {
fn event(&mut self, child: &mut W, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
child.event(ctx, event, data, env)
Expand All @@ -85,7 +85,7 @@ pub trait Controller<T, W: Widget<T>> {

/// A [`Widget`] that manages a child and a [`Controller`].
///
/// [`Widget`]: trait.Widget.html
/// [`Widget`]: ../trait.Widget.html
/// [`Controller`]: trait.Controller.html
pub struct ControllerHost<W, C> {
widget: W,
Expand Down
8 changes: 4 additions & 4 deletions druid/src/widget/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ use crate::{
/// my_row.add_flex_child(Slider::new(), 1.0);
/// ```
///
/// [`layout`]: trait.Widget.html#tymethod.layout
/// [`layout`]: ../trait.Widget.html#tymethod.layout
/// [`MainAxisAlignment`]: enum.MainAxisAlignment.html
/// [`CrossAxisAlignment`]: enum.CrossAxisAlignment.html
/// [`must_fill_main_axis`]: struct.Flex.html#method.must_fill_main_axis
/// [`FlexParams`]: struct.FlexParams.html
/// [`WidgetExt`]: trait.WidgetExt.html
/// [`expand_height`]: trait.WidgetExt.html#method.expand_height
/// [`expand_width`]: trait.WidgetExt.html#method.expand_width
/// [`WidgetExt`]: ../trait.WidgetExt.html
/// [`expand_height`]: ../trait.WidgetExt.html#method.expand_height
/// [`expand_width`]: ../trait.WidgetExt.html#method.expand_width
/// [`TextBox`]: struct.TextBox.html
/// [`SizedBox`]: struct.SizedBox.html
pub struct Flex<T> {
Expand Down
Loading