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

Add SCROLLBAR_MIN_SIZE to env, and use Entry #1661

Merged
merged 12 commits into from
Mar 18, 2021
17 changes: 11 additions & 6 deletions druid/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::any;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::collections::{hash_map::Entry, HashMap};
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::ops::Deref;
Expand Down Expand Up @@ -342,13 +342,18 @@ impl Env {
) -> Result<(), ValueTypeError> {
let env = Arc::make_mut(&mut self.0);
let key = key.into();
// TODO: use of Entry might be more efficient
if let Some(existing) = env.map.get(&key) {
if !existing.is_same_type(&raw) {
return Err(ValueTypeError::new(any::type_name::<V>(), raw));
match env.map.entry(key) {
Entry::Occupied(mut e) => {
let existing = e.get_mut();
if !existing.is_same_type(&raw) {
return Err(ValueTypeError::new(any::type_name::<V>(), raw));
}
*existing = raw;
}
Entry::Vacant(e) => {
e.insert(raw);
}
}
env.map.insert(key, raw);
Ok(())
}

Expand Down
14 changes: 6 additions & 8 deletions druid/src/scroll_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@

use std::time::Duration;

use crate::kurbo::{Point, Rect, Vec2};
use crate::theme;
use crate::widget::{Axis, Viewport};
use crate::{
kurbo::{Point, Rect, Vec2},
theme::SCROLLBAR_MIN_SIZE,
};
use crate::{Env, Event, EventCtx, LifeCycle, LifeCycleCtx, PaintCtx, RenderContext, TimerToken};

//TODO: Add this to env
/// Minimum length for any scrollbar to be when measured on that
/// scrollbar's primary axis.
pub const SCROLLBAR_MIN_SIZE: f64 = 45.0;

#[derive(Debug, Copy, Clone)]
/// Which scroll bars of a scroll area are currently enabled.
pub enum ScrollbarsEnabled {
Expand Down Expand Up @@ -192,7 +190,7 @@ impl ScrollComponent {
let percent_scrolled = scroll_offset.y / (content_size.height - viewport_size.height);

let length = (percent_visible * viewport_size.height).ceil();
let length = length.max(SCROLLBAR_MIN_SIZE);
let length = length.max(env.get(SCROLLBAR_MIN_SIZE));

let vertical_padding = bar_pad + bar_pad + bar_width;

Expand Down Expand Up @@ -227,7 +225,7 @@ impl ScrollComponent {
let percent_scrolled = scroll_offset.x / (content_size.width - viewport_size.width);

let length = (percent_visible * viewport_size.width).ceil();
let length = length.max(SCROLLBAR_MIN_SIZE);
let length = length.max(env.get(SCROLLBAR_MIN_SIZE));

let horizontal_padding = bar_pad + bar_pad + bar_width;

Expand Down
4 changes: 4 additions & 0 deletions druid/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ pub const SCROLLBAR_PAD: Key<f64> = Key::new("org.linebender.druid.theme.scrollb
pub const SCROLLBAR_RADIUS: Key<f64> = Key::new("org.linebender.druid.theme.scrollbar_radius");
pub const SCROLLBAR_EDGE_WIDTH: Key<f64> =
Key::new("org.linebender.druid.theme.scrollbar_edge_width");
/// Minimum length for any scrollbar to be when measured on that
/// scrollbar's primary axis.
pub const SCROLLBAR_MIN_SIZE: Key<f64> = Key::new("org.linebender.theme.scrollbar_min_size");

/// An initial theme.
pub(crate) fn add_to_env(env: Env) -> Env {
Expand Down Expand Up @@ -161,6 +164,7 @@ pub(crate) fn add_to_env(env: Env) -> Env {
.with_style(FontStyle::Italic)
.with_size(15.0),
)
.adding(SCROLLBAR_MIN_SIZE, 45.0)
}

#[deprecated(since = "0.7.0", note = "use Env::default() instead")]
Expand Down