Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Padding takes KeyOrValue, TEXTBOX_INSETS moves back to Env #1662

Merged
merged 2 commits into from
Mar 29, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ You can find its changes [documented below](#070---2021-01-01).
- New `TextBox` widget with IME integration ([#1636] by [@cmyr])
- `Notification`s can be submitted while handling other `Notification`s ([#1640] by [@cmyr])
- Added ListIter implementations for OrdMap ([#1641] by [@Lejero])
- `Padding` can now use `Key<Insets>` ([#1662] by [@cmyr])

### Changed

Expand Down Expand Up @@ -644,6 +645,7 @@ Last release without a changelog :(
[#1640]: https://github.com/linebender/druid/pull/1640
[#1641]: https://github.com/linebender/druid/pull/1641
[#1647]: https://github.com/linebender/druid/pull/1647
[#1662]: https://github.com/linebender/druid/pull/1662

[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0
Expand Down
18 changes: 18 additions & 0 deletions druid/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,24 @@ impl<T: ValueType> From<Key<T>> for KeyOrValue<T> {
}
}

impl Into<KeyOrValue<Insets>> for f64 {
fn into(self) -> KeyOrValue<Insets> {
KeyOrValue::Concrete(self.into())
}
}

impl Into<KeyOrValue<Insets>> for (f64, f64) {
fn into(self) -> KeyOrValue<Insets> {
KeyOrValue::Concrete(self.into())
}
}

impl Into<KeyOrValue<Insets>> for (f64, f64, f64, f64) {
fn into(self) -> KeyOrValue<Insets> {
KeyOrValue::Concrete(self.into())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion druid/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub(crate) fn add_to_env(env: Env) -> Env {
.adding(BORDERED_WIDGET_HEIGHT, 24.0)
.adding(TEXTBOX_BORDER_RADIUS, 2.)
.adding(TEXTBOX_BORDER_WIDTH, 1.)
.adding(TEXTBOX_INSETS, Insets::new(4.0, 2.0, 4.0, 4.0))
.adding(TEXTBOX_INSETS, Insets::new(4.0, 4.0, 4.0, 4.0))
.adding(SCROLLBAR_COLOR, Color::rgb8(0xff, 0xff, 0xff))
.adding(SCROLLBAR_BORDER_COLOR, Color::rgb8(0x77, 0x77, 0x77))
.adding(SCROLLBAR_MAX_OPACITY, 0.7)
Expand Down
45 changes: 20 additions & 25 deletions druid/src/widget/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@
//! A widget that just adds padding during layout.

use crate::widget::{prelude::*, WidgetWrapper};
use crate::{Data, Insets, Point, WidgetPod};
use crate::{Data, Insets, KeyOrValue, Point, WidgetPod};

use tracing::{instrument, trace};

/// A widget that just adds padding around its child.
pub struct Padding<T, W> {
left: f64,
right: f64,
top: f64,
bottom: f64,

insets: KeyOrValue<Insets>,
child: WidgetPod<T, W>,
}

impl<T, W: Widget<T>> Padding<T, W> {
/// Create a new widget with the specified padding. This can either be an instance
/// of [`kurbo::Insets`], a f64 for uniform padding, a 2-tuple for axis-uniform padding
/// or 4-tuple with (left, top, right, bottom) values.
/// Create a new `Padding` with the specified padding and child.
///
/// The `insets` argument can either be an instance of [`Insets`],
/// a [`Key`] referring to [`Insets`] in the [`Env`],
/// an `f64` for uniform padding, an `(f64, f64)` for axis-uniform padding,
/// or `(f64, f64, f64, f64)` (left, top, right, bottom) values.
///
/// # Examples
///
Expand All @@ -58,14 +57,10 @@ impl<T, W: Widget<T>> Padding<T, W> {
/// let _: Padding<(), _> = Padding::new(Insets::uniform_xy(10.0, 20.0), Label::new("ditto :)"));
/// ```
///
/// [`kurbo::Insets`]: https://docs.rs/kurbo/0.5.3/kurbo/struct.Insets.html
pub fn new(insets: impl Into<Insets>, child: W) -> Padding<T, W> {
let insets = insets.into();
/// [`Key`]: crate::Key
pub fn new(insets: impl Into<KeyOrValue<Insets>>, child: W) -> Padding<T, W> {
Padding {
left: insets.x0,
right: insets.x1,
top: insets.y0,
bottom: insets.y1,
insets: insets.into(),
child: WidgetPod::new(child),
}
}
Expand All @@ -86,25 +81,25 @@ impl<T: Data, W: Widget<T>> Widget<T> for Padding<T, W> {
self.child.lifecycle(ctx, event, data, env)
}

#[instrument(
name = "Padding",
level = "trace",
skip(self, ctx, _old_data, data, env)
)]
fn update(&mut self, ctx: &mut UpdateCtx, _old_data: &T, data: &T, env: &Env) {
#[instrument(name = "Padding", level = "trace", skip(self, ctx, _old, data, env))]
fn update(&mut self, ctx: &mut UpdateCtx, _old: &T, data: &T, env: &Env) {
if ctx.env_key_changed(&self.insets) {
ctx.request_layout();
}
self.child.update(ctx, data, env);
}

#[instrument(name = "Padding", level = "trace", skip(self, ctx, bc, data, env))]
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size {
bc.debug_check("Padding");
let insets = self.insets.resolve(env);

let hpad = self.left + self.right;
let vpad = self.top + self.bottom;
let hpad = insets.x0 + insets.x1;
let vpad = insets.y0 + insets.y1;

let child_bc = bc.shrink((hpad, vpad));
let size = self.child.layout(ctx, &child_bc, data, env);
let origin = Point::new(self.left, self.top);
let origin = Point::new(insets.x0, insets.y0);
self.child.set_origin(ctx, data, env, origin);

let my_size = Size::new(size.width + hpad, size.height + vpad);
Expand Down
11 changes: 6 additions & 5 deletions druid/src/widget/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use crate::{
theme, Color, FontDescriptor, KeyOrValue, Point, Rect, TextAlignment, TimerToken, Vec2,
};

const TEXTBOX_INSETS: Insets = Insets::new(4.0, 2.0, 4.0, 2.0);
const CURSOR_BLINK_DURATION: Duration = Duration::from_millis(500);
const MAC_OR_LINUX: bool = cfg!(any(target_os = "macos", target_os = "linux"));

Expand Down Expand Up @@ -73,7 +72,7 @@ impl<T: EditableText + TextStorage> TextBox<T> {
let mut scroll = Scroll::new(TextComponent::default()).content_must_fill(true);
scroll.set_enabled_scrollbars(crate::scroll_component::ScrollbarsEnabled::None);
Self {
inner: Padding::new(TEXTBOX_INSETS, scroll),
inner: Padding::new(theme::TEXTBOX_INSETS, scroll),
scroll_to_selection_after_layout: false,
placeholder,
multiline: false,
Expand Down Expand Up @@ -471,6 +470,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
tracing::warn!("Widget::layout called with outstanding IME lock.");
}
let min_width = env.get(theme::WIDE_WIDGET_WIDTH);
let textbox_insets = env.get(theme::TEXTBOX_INSETS);

self.placeholder.rebuild_if_needed(ctx.text(), env);
let min_size = bc.constrain((min_width, 0.0));
Expand All @@ -488,7 +488,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
let baseline_off = layout_baseline
- (self.inner.wrapped().child_size().height
- self.inner.wrapped().viewport_rect().height())
+ TEXTBOX_INSETS.y1;
+ textbox_insets.y1;
ctx.set_baseline_offset(baseline_off);
if self.scroll_to_selection_after_layout {
self.scroll_to_selection_end();
Expand All @@ -513,6 +513,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
let background_color = env.get(theme::BACKGROUND_LIGHT);
let cursor_color = env.get(theme::CURSOR_COLOR);
let border_width = env.get(theme::TEXTBOX_BORDER_WIDTH);
let textbox_insets = env.get(theme::TEXTBOX_INSETS);

let is_focused = ctx.is_focused();

Expand All @@ -537,7 +538,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
ctx.with_save(|ctx| {
ctx.clip(clip_rect);
self.placeholder
.draw(ctx, (TEXTBOX_INSETS.x0, TEXTBOX_INSETS.y0));
.draw(ctx, (textbox_insets.x0, textbox_insets.y0));
})
}

Expand All @@ -551,7 +552,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
.borrow()
.cursor_line_for_text_position(cursor_pos);

let padding_offset = Vec2::new(TEXTBOX_INSETS.x0, TEXTBOX_INSETS.y0);
let padding_offset = Vec2::new(textbox_insets.x0, textbox_insets.y0);

let cursor = if data.is_empty() {
cursor_line + padding_offset
Expand Down
9 changes: 6 additions & 3 deletions druid/src/widget/widget_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ use crate::{
pub trait WidgetExt<T: Data>: Widget<T> + Sized + 'static {
/// Wrap this widget in a [`Padding`] widget with the given [`Insets`].
///
/// [`Padding`]: widget/struct.Padding.html
/// [`Insets`]: kurbo/struct.Insets.html
fn padding(self, insets: impl Into<Insets>) -> Padding<T, Self> {
/// Like [`Padding::new`], this can accept a variety of arguments, including
/// a [`Key`] referring to [`Insets`] in the [`Env`].
///
/// [`Key`]: crate::Key
/// [`Insets`]: crate::Insets
fn padding(self, insets: impl Into<KeyOrValue<Insets>>) -> Padding<T, Self> {
Padding::new(insets, self)
}

Expand Down