Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
xarvic committed Dec 18, 2022
1 parent 6aac414 commit 3ad3741
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 50 deletions.
49 changes: 32 additions & 17 deletions druid/examples/viewport_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@

use druid::lens::Unit;
use druid::widget::prelude::*;
use druid::widget::{BackgroundBrush, Button, ClipBox, Flex, Label, List, Side, Slider, Tabs, TextBox, ViewportHeader};
use druid::{AppLauncher, Color, Data, Insets, Lens, LocalizedString, Point, Rect, RoundedRectRadii, Vec2, WidgetExt, WidgetPod, WindowDesc};
use druid::widget::{
BackgroundBrush, Button, ClipBox, Flex, Label, List, Side, Slider, Tabs, TextBox,
ViewportHeader,
};
use druid::{
AppLauncher, Color, Data, Insets, Lens, LocalizedString, Point, Rect, RoundedRectRadii, Vec2,
WidgetExt, WidgetPod, WindowDesc,
};
use im::Vector;
use std::sync::Arc;

Expand All @@ -47,13 +53,14 @@ pub fn main() {

AppLauncher::with_window(window)
.log_to_console()
.launch(AppData { list: Vector::new()})
.launch(AppData {
list: Vector::new(),
})
.expect("launch failed");
}

fn build_widget() -> impl Widget<AppData> {
let list = List::new(||{

let list = List::new(|| {
let body = Flex::column()
.with_child(Label::new("Name:"))
.with_default_spacer()
Expand All @@ -62,9 +69,13 @@ fn build_widget() -> impl Widget<AppData> {
.with_default_spacer()
.with_child(Label::new("Info:"))
.with_default_spacer()
.with_child(List::new(||TextBox::new().padding(Insets::from(5.0))).lens(Contact::info))
.with_child(List::new(|| TextBox::new().padding(Insets::from(5.0))).lens(Contact::info))
.with_default_spacer()
.with_child(Button::new("Add Info").on_click(|_, data: &mut Contact, _|data.info.push_back(Arc::new(String::new()))))
.with_child(
Button::new("Add Info").on_click(|_, data: &mut Contact, _| {
data.info.push_back(Arc::new(String::new()))
}),
)
.align_left()
.fix_width(150.0)
.padding(Insets::uniform_xy(25.0, 0.0))
Expand All @@ -73,24 +84,28 @@ fn build_widget() -> impl Widget<AppData> {

ViewportHeader::new(
body,
Label::dynamic(|data: &Contact, _|format!("Contact \"{}\"", &data.name))
Label::dynamic(|data: &Contact, _| format!("Contact \"{}\"", &data.name))
.center()
.background(Color::BLACK)
.rounded(RoundedRectRadii::new(5.0, 5.0, 0.0, 0.0)),
Side::Top,
)
.clipped_content(true)
.with_minimum_visible_content(20.0)
.padding(Insets::uniform_xy(0.0, 5.0))
.clipped_content(true)
.with_minimum_visible_content(20.0)
.padding(Insets::uniform_xy(0.0, 5.0))
})
.lens(AppData::list)
.scroll();
.lens(AppData::list)
.scroll();

Flex::column()
.with_flex_child(list, 1.0)
.with_default_spacer()
.with_child(Button::new("Add Contact").on_click(|_, data: &mut AppData, _|data.list.push_back(Contact {
name: Arc::new("New Contact".to_string()),
info: Default::default(),
})))
.with_child(
Button::new("Add Contact").on_click(|_, data: &mut AppData, _| {
data.list.push_back(Contact {
name: Arc::new("New Contact".to_string()),
info: Default::default(),
})
}),
)
}
14 changes: 7 additions & 7 deletions druid/src/widget/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

//! A widget that aligns its child (for example, centering it).

use crate::contexts::CommandCtx;
use crate::debug_state::DebugState;
use crate::widget::prelude::*;
use crate::{Data, Rect, Size, UnitPoint, WidgetPod};
use tracing::{instrument, trace};
use crate::contexts::CommandCtx;

/// A widget that aligns its child.
pub struct Align<T> {
Expand Down Expand Up @@ -107,7 +107,8 @@ impl<T> Align<T> {

if self.in_viewport {
// The part of the viewport the origin of the child is allowed to be in
let viewport = Rect::from_origin_size(self.viewport.origin(), self.viewport.size() - size);
let viewport =
Rect::from_origin_size(self.viewport.origin(), self.viewport.size() - size);

// Essentially Rect::intersect but this implementation chooses the point closed to viewport
// inside extra_space to give the child a valid origin even if extra_space and viewport
Expand All @@ -118,10 +119,7 @@ impl<T> Align<T> {
extra_space.y1 = extra_space.y1.min(viewport.y1).max(extra_space.y0);
}

let origin = self
.align
.resolve(extra_space)
.expand();
let origin = self.align.resolve(extra_space).expand();
self.child.set_origin(ctx, origin);
}
}
Expand Down Expand Up @@ -183,7 +181,9 @@ impl<T: Data> Widget<T> for Align<T> {
if self.height_factor.is_some() {
let baseline_offset = self.child.baseline_offset();
if baseline_offset > 0f64 {
ctx.set_baseline_offset(my_size.height - self.child.layout_rect().y1 + baseline_offset);
ctx.set_baseline_offset(
my_size.height - self.child.layout_rect().y1 + baseline_offset,
);
}
}

Expand Down
16 changes: 12 additions & 4 deletions druid/src/widget/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,18 @@ impl Side {
pub fn as_insets(&self, amount: f64) -> Insets {
let mut insets = Insets::ZERO;
match self {
Side::Top => {insets.y0 = amount;}
Side::Left => {insets.x0 = amount;}
Side::Right => {insets.x1 = amount;}
Side::Bottom => {insets.y1 = amount;}
Side::Top => {
insets.y0 = amount;
}
Side::Left => {
insets.x0 = amount;
}
Side::Right => {
insets.x1 = amount;
}
Side::Bottom => {
insets.y1 = amount;
}
}
insets
}
Expand Down
4 changes: 2 additions & 2 deletions druid/src/widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ mod tabs;
mod textbox;
mod value_textbox;
mod view_switcher;
mod viewport_header;
#[allow(clippy::module_inception)]
mod widget;
mod widget_ext;
mod z_stack;
mod viewport_header;

pub use self::image::Image;
pub use added::Added;
Expand All @@ -80,7 +80,7 @@ pub use controller::{Controller, ControllerHost};
pub use disable_if::DisabledIf;
pub use either::Either;
pub use env_scope::EnvScope;
pub use flex::{Axis, CrossAxisAlignment, Flex, FlexParams, MainAxisAlignment, Side, Orientation};
pub use flex::{Axis, CrossAxisAlignment, Flex, FlexParams, MainAxisAlignment, Orientation, Side};
pub use identity_wrapper::IdentityWrapper;
pub use intrinsic_width::IntrinsicWidth;
pub use label::{Label, LabelText, LineBreaking, RawLabel};
Expand Down
51 changes: 31 additions & 20 deletions druid/src/widget/viewport_header.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use druid::RenderContext;
use crate::{BoxConstraints, Color, Data, Env, Event, EventCtx, Insets, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx, Point, Rect, Size, UpdateCtx, ViewContext, Widget, WidgetExt, WidgetId, WidgetPod};
use crate::commands::SCROLL_TO_VIEW;
use crate::widget::flex::{Orientation, Side};
use crate::{
BoxConstraints, Color, Data, Env, Event, EventCtx, Insets, LayoutCtx, LifeCycle, LifeCycleCtx,
PaintCtx, Point, Rect, Size, UpdateCtx, ViewContext, Widget, WidgetExt, WidgetId, WidgetPod,
};
use druid::RenderContext;

/// A widget, containing two widgets with horizontal or vertical layout.
///
Expand Down Expand Up @@ -78,15 +81,10 @@ impl ViewportHeaderConfig {
let orientation = self.header_side.orientation();
let axis = self.header_side.axis();

let (first, _) = orientation.order(
axis.major(self.content_size),
self.header_size
);
let (first, _) = orientation.order(axis.major(self.content_size), self.header_size);

let (content_origin, header_origin) = orientation.order(
Point::ZERO,
Point::from(axis.pack(first, 0.0))
);
let (content_origin, header_origin) =
orientation.order(Point::ZERO, Point::from(axis.pack(first, 0.0)));
let header_origin = header_origin - self.header_side.direction() * self.overlapping();

(content_origin, header_origin)
Expand Down Expand Up @@ -135,12 +133,15 @@ impl ViewportHeaderConfig {
pub fn set_minimum_visible_content(&mut self, visible: f64) {
self.minimum_visible_content = visible;
}

}

impl<T: Data> ViewportHeader<T> {
/// Creates a new ViewportHeader widget with a given side for the header.
pub fn new(content: impl Widget<T> + 'static, header: impl Widget<T> + 'static, side: Side) -> Self {
pub fn new(
content: impl Widget<T> + 'static,
header: impl Widget<T> + 'static,
side: Side,
) -> Self {
Self {
header: WidgetPod::new(Box::new(header)),
content: WidgetPod::new(Box::new(content)),
Expand All @@ -151,7 +152,8 @@ impl<T: Data> ViewportHeader<T> {

/// The amount of Pixels
pub fn with_minimum_visible_content(mut self, minimum_visible_content: f64) -> Self {
self.header_config.set_minimum_visible_content(minimum_visible_content);
self.header_config
.set_minimum_visible_content(minimum_visible_content);
self
}

Expand All @@ -170,7 +172,8 @@ impl<T: Data> Widget<T> for ViewportHeader<T> {
if notification.route() == self.content.id() {
// The content is additionally cropped by the header, therefore we move the scroll
// request by the amount
self.header_config.transform_content_scroll_to_view(ctx, *rect);
self.header_config
.transform_content_scroll_to_view(ctx, *rect);
}
return;
}
Expand All @@ -197,12 +200,17 @@ impl<T: Data> Widget<T> for ViewportHeader<T> {
if self.header.is_hot() {
content_view_context.last_mouse_position = None;
}
content_view_context.clip = content_view_context.clip -
self.header_config.side().as_insets(self.header_config.viewport_crop());
content_view_context.clip = content_view_context.clip
- self
.header_config
.side()
.as_insets(self.header_config.viewport_crop());

self.content.lifecycle(ctx, event, data, env);
}
LifeCycle::BuildFocusChain if self.header_config.side().orientation() == Orientation::End => {
LifeCycle::BuildFocusChain
if self.header_config.side().orientation() == Orientation::End =>
{
self.content.lifecycle(ctx, event, data, env);
self.header.lifecycle(ctx, event, data, env);
}
Expand Down Expand Up @@ -242,12 +250,15 @@ impl<T: Data> Widget<T> for ViewportHeader<T> {
fn paint(&mut self, ctx: &mut PaintCtx, data: &T, env: &Env) {
ctx.with_save(|ctx| {
if self.clip_content {
let content_rect = self.content.layout_rect() -
self.header_config.side().as_insets(self.header_config.overlapping());
let content_rect = self.content.layout_rect()
- self
.header_config
.side()
.as_insets(self.header_config.overlapping());
ctx.clip(content_rect);
}
self.content.paint(ctx, data, env);
});
self.header.paint(ctx, data, env);
}
}
}

0 comments on commit 3ad3741

Please sign in to comment.