From 6707f203d50d304bc7e66d8fad0c3ca3f1cede2e Mon Sep 17 00:00:00 2001 From: xarvic Date: Sun, 18 Dec 2022 15:25:37 +0100 Subject: [PATCH] reformat --- druid/examples/viewport_header.rs | 49 +++++++++++++++++---------- druid/src/widget/align.rs | 14 ++++---- druid/src/widget/flex.rs | 16 ++++++--- druid/src/widget/mod.rs | 4 +-- druid/src/widget/viewport_header.rs | 51 ++++++++++++++++++----------- 5 files changed, 84 insertions(+), 50 deletions(-) diff --git a/druid/examples/viewport_header.rs b/druid/examples/viewport_header.rs index e8f8738681..72a35087f2 100755 --- a/druid/examples/viewport_header.rs +++ b/druid/examples/viewport_header.rs @@ -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; @@ -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 { - let list = List::new(||{ - + let list = List::new(|| { let body = Flex::column() .with_child(Label::new("Name:")) .with_default_spacer() @@ -62,9 +69,13 @@ fn build_widget() -> impl Widget { .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)) @@ -73,24 +84,28 @@ fn build_widget() -> impl Widget { 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(), + }) + }), + ) } diff --git a/druid/src/widget/align.rs b/druid/src/widget/align.rs index 659a5386ed..ffdc0cd066 100644 --- a/druid/src/widget/align.rs +++ b/druid/src/widget/align.rs @@ -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 { @@ -107,7 +107,8 @@ impl Align { 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 @@ -118,10 +119,7 @@ impl Align { 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); } } @@ -183,7 +181,9 @@ impl Widget for Align { 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, + ); } } diff --git a/druid/src/widget/flex.rs b/druid/src/widget/flex.rs index 2adc0e4512..f4bffcbc89 100644 --- a/druid/src/widget/flex.rs +++ b/druid/src/widget/flex.rs @@ -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 } diff --git a/druid/src/widget/mod.rs b/druid/src/widget/mod.rs index 340a5da7a9..590d490599 100644 --- a/druid/src/widget/mod.rs +++ b/druid/src/widget/mod.rs @@ -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; @@ -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}; diff --git a/druid/src/widget/viewport_header.rs b/druid/src/widget/viewport_header.rs index 6c708ab7b8..c9be93a032 100644 --- a/druid/src/widget/viewport_header.rs +++ b/druid/src/widget/viewport_header.rs @@ -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. /// @@ -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) @@ -135,12 +133,15 @@ impl ViewportHeaderConfig { pub fn set_minimum_visible_content(&mut self, visible: f64) { self.minimum_visible_content = visible; } - } impl ViewportHeader { /// Creates a new ViewportHeader widget with a given side for the header. - pub fn new(content: impl Widget + 'static, header: impl Widget + 'static, side: Side) -> Self { + pub fn new( + content: impl Widget + 'static, + header: impl Widget + 'static, + side: Side, + ) -> Self { Self { header: WidgetPod::new(Box::new(header)), content: WidgetPod::new(Box::new(content)), @@ -151,7 +152,8 @@ impl ViewportHeader { /// 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 } @@ -170,7 +172,8 @@ impl Widget for ViewportHeader { 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; } @@ -197,12 +200,17 @@ impl Widget for ViewportHeader { 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); } @@ -242,12 +250,15 @@ impl Widget for ViewportHeader { 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); } -} \ No newline at end of file +}