diff --git a/crates/behavior/src/wheel.rs b/crates/behavior/src/wheel.rs index 787e5f714..14f2b129c 100644 --- a/crates/behavior/src/wheel.rs +++ b/crates/behavior/src/wheel.rs @@ -83,8 +83,8 @@ pub struct WheelTaker { pub vertical: bool, /// It takes zoom. pub zoom: bool, - /// It takes any axis (scroll or zoom) only while focused or armed: a value control without wheel bits, or any - /// taker inside a wheel-gated scope. + /// It takes any axis (scroll or zoom) only while focused or armed: a value control, unless its scope frees it, + /// or any taker inside a gated scope. pub gated: bool, /// A scroll region (a scrolling body or collection): Shift turns the vertical wheel sideways over it. pub scrolls: bool, diff --git a/crates/imaging/src/ui/adjust/basic/brightness_contrast.rs b/crates/imaging/src/ui/adjust/basic/brightness_contrast.rs index 89851ddb9..df5ba9583 100644 --- a/crates/imaging/src/ui/adjust/basic/brightness_contrast.rs +++ b/crates/imaging/src/ui/adjust/basic/brightness_contrast.rs @@ -1,12 +1,11 @@ //! Brightness/Contrast: its value and editor. use craft_ui::choice::Checkbox; -use craft_ui::contract::{Bind, EditResponse, Pattern, Props as _, Remember as _, Show as _, ShowContainer as _}; -use craft_ui::form::FieldGroup; +use craft_ui::contract::{Bind, EditResponse, Pattern, Props as _, Remember as _, Show as _}; use craft_ui::runtime::env::Label; use egui::Ui; -use crate::ui::adjust::sliders::{Run, lifted, read, words}; +use crate::ui::adjust::sliders::{Run, editor_group, lifted, read, words}; use crate::ui::adjust::{AdjustContext, AdjustField}; /// Contrast's lowest value, and with Use Legacy. @@ -68,7 +67,7 @@ impl<'a> BrightnessContrastEditor<'a> { let BrightnessContrastEditor { label, mut value, context } = self; let words = words(ui); let mut edit = None; - let shown = FieldGroup::new(label).show(ui, |ui, cx| { + let shown = editor_group(ui, label, context, |ui, cx| { let now = read(&mut value, cx).unwrap_or_default(); let lowest = if now.legacy { LEGACY_CONTRAST_MIN } else { CONTRAST_MIN }; let brightness = AdjustField::new("brightness", words.brightness.clone(), -150.0..=150.0, 0.0); diff --git a/crates/imaging/src/ui/adjust/basic/color_balance.rs b/crates/imaging/src/ui/adjust/basic/color_balance.rs index 4715b416e..0b6976d77 100644 --- a/crates/imaging/src/ui/adjust/basic/color_balance.rs +++ b/crates/imaging/src/ui/adjust/basic/color_balance.rs @@ -2,12 +2,12 @@ use craft_ui::choice::{Checkbox, Radio, RadioGroup}; use craft_ui::contract::{Bind, EditResponse, Outcome, Pattern, Props as _, Remember as _, Show as _, ShowContainer as _}; -use craft_ui::form::{Field, FieldGroup}; +use craft_ui::form::Field; use craft_ui::layout_components::Row; use craft_ui::runtime::env::Label; use egui::Ui; -use crate::ui::adjust::sliders::{Run, lifted, read, words}; +use crate::ui::adjust::sliders::{Run, editor_group, lifted, read, words}; use crate::ui::adjust::{AdjustContext, AdjustField}; /// The tone Color Balance shows. @@ -156,7 +156,7 @@ impl<'a> ColorBalanceEditor<'a> { let ColorBalanceEditor { label, mut value, tone: host_tone, context } = self; let words = words(ui); let mut out = None; - let shown = FieldGroup::new(label).show(ui, |ui, cx| { + let shown = editor_group(ui, label, context, |ui, cx| { let now = read(&mut value, cx).unwrap_or_default(); let tone = host_tone.or(cx.memory::().tone).unwrap_or_default(); let mut picked = None; diff --git a/crates/imaging/src/ui/adjust/basic/photo_filter.rs b/crates/imaging/src/ui/adjust/basic/photo_filter.rs index 90211932c..a1b1ef575 100644 --- a/crates/imaging/src/ui/adjust/basic/photo_filter.rs +++ b/crates/imaging/src/ui/adjust/basic/photo_filter.rs @@ -5,14 +5,14 @@ use craft_ui::choice::{Checkbox, Radio, RadioGroup, Swatch}; use craft_ui::contract::{ Bind, EditResponse, Entry, LabelKind, Outcome, Pattern, Props as _, Remember as _, Show as _, ShowContainer as _, SideLabel, }; -use craft_ui::form::{Field, FieldGroup}; +use craft_ui::form::Field; use craft_ui::layout_components::Row; use craft_ui::pickers::Select; use craft_ui::runtime::env::{Label, UnitKind}; use craft_ui::runtime::requests::ColorRequest; use egui::Ui; -use crate::ui::adjust::sliders::{Run, lifted, read, words}; +use crate::ui::adjust::sliders::{Run, editor_group, lifted, read, words}; use crate::ui::adjust::{AdjustContext, AdjustField, AdjustWords}; /// An opaque colour from 8-bit sRGB channels. @@ -255,7 +255,7 @@ impl<'a> PhotoFilterEditor<'a> { let PhotoFilterEditor { label, mut value, context } = self; let words = words(ui); let mut out = None; - let shown = FieldGroup::new(label).show(ui, |ui, cx| { + let shown = editor_group(ui, label, context, |ui, cx| { let now = read(&mut value, cx).unwrap_or_default(); let preset = PhotoFilterPreset::of(now.color); let use_color = cx.memory::().use_color.unwrap_or(preset.is_none()); diff --git a/crates/imaging/src/ui/adjust/basic/threshold.rs b/crates/imaging/src/ui/adjust/basic/threshold.rs index 497ac471f..262ebf78e 100644 --- a/crates/imaging/src/ui/adjust/basic/threshold.rs +++ b/crates/imaging/src/ui/adjust/basic/threshold.rs @@ -1,11 +1,10 @@ //! Threshold: its editor. -use craft_ui::contract::{Bind, EditResponse, Pattern, Remember as _, Show as _, ShowContainer as _}; -use craft_ui::form::FieldGroup; +use craft_ui::contract::{Bind, EditResponse, Pattern, Remember as _, Show as _}; use craft_ui::runtime::env::Label; use egui::Ui; -use crate::ui::adjust::sliders::{Run, lifted, read, words}; +use crate::ui::adjust::sliders::{Run, editor_group, lifted, read, words}; use crate::ui::adjust::{AdjustContext, AdjustField}; use crate::ui::histogram::{ChannelColor, Histogram, HistogramData}; @@ -59,7 +58,7 @@ impl<'a> ThresholdEditor<'a> { let ThresholdEditor { label, mut level, histogram, context } = self; let words = words(ui); let mut edit = None; - let shown = FieldGroup::new(label).show(ui, |ui, cx| { + let shown = editor_group(ui, label, context, |ui, cx| { let now = read(&mut level, cx).unwrap_or(THRESHOLD_DEFAULT).clamp(THRESHOLD_MIN, THRESHOLD_MAX); let empty = HistogramData::tone(&[0u32; THRESHOLD_BINS][..], ChannelColor::Neutral); let data = histogram.unwrap_or(&empty); diff --git a/crates/imaging/src/ui/adjust/context.rs b/crates/imaging/src/ui/adjust/context.rs index 900d67113..67af11c3c 100644 --- a/crates/imaging/src/ui/adjust/context.rs +++ b/crates/imaging/src/ui/adjust/context.rs @@ -28,14 +28,14 @@ pub struct AdjustContext { } impl AdjustContext { - /// An editor in an Image › Adjustments dialog. `dynamic` is Preferences › Interface › Dynamic - /// Color Sliders: colour tracks on the colour sliders, the plain track when off. + /// An editor in an Image › Adjustments dialog: the wheel over a slider steps it without focus. `dynamic` is + /// Preferences › Interface › Dynamic Color Sliders: colour tracks on the colour sliders, the plain track when off. pub fn dialog(dynamic: bool) -> Self { Self { dialog: true, dynamic_color_sliders: dynamic, gray: false } } - /// An editor in the Properties panel (an adjustment layer). `dynamic` as in - /// [`dialog`](Self::dialog). + /// An editor in the Properties panel (an adjustment layer): the wheel steps only a focused or armed slider and + /// scrolls the panel over the others. `dynamic` as in [`dialog`](Self::dialog). pub fn properties(dynamic: bool) -> Self { Self { dialog: false, dynamic_color_sliders: dynamic, gray: false } } diff --git a/crates/imaging/src/ui/adjust/hue_saturation.rs b/crates/imaging/src/ui/adjust/hue_saturation.rs index 637064e19..1299016b9 100644 --- a/crates/imaging/src/ui/adjust/hue_saturation.rs +++ b/crates/imaging/src/ui/adjust/hue_saturation.rs @@ -9,14 +9,14 @@ use craft_theme::{Hsb, Srgba}; use craft_ui::choice::Checkbox; use craft_ui::contract::{Bind, EditResponse, Entry, Outcome, Pattern, Props as _, Remember as _, Show as _, ShowContainer as _}; use craft_ui::display::{Shade, Text}; -use craft_ui::form::{Field, FieldGroup}; +use craft_ui::form::Field; use craft_ui::pickers::Select; use craft_ui::runtime::env::{Env, Label, UnitKind}; use craft_ui::style::{DataColor, TypeRole}; use craft_ui::value_drag::{Anchor, Thumb, ThumbRow, TrackFace}; use egui::{Color32, Ui}; -use super::sliders::{Run, lifted, read, words}; +use super::sliders::{Run, editor_group, lifted, read, words}; use super::style::tones; use super::{AdjustContext, AdjustField, AdjustTrack}; use crate::ui::curve::ToneRamp; @@ -295,7 +295,7 @@ impl<'a> HueSaturationEditor<'a> { let words = words(ui); let units = Env::of(ui).units(); let mut out = None; - let shown = FieldGroup::new(label).show(ui, |ui, cx| { + let shown = editor_group(ui, label, context, |ui, cx| { let now = read(&mut value, cx).unwrap_or_default(); let memory = cx.memory::().clone(); // Colorize works on the master only. diff --git a/crates/imaging/src/ui/adjust/mixers/black_white.rs b/crates/imaging/src/ui/adjust/mixers/black_white.rs index e1362fc13..a927173b6 100644 --- a/crates/imaging/src/ui/adjust/mixers/black_white.rs +++ b/crates/imaging/src/ui/adjust/mixers/black_white.rs @@ -4,13 +4,12 @@ use craft_theme::Srgba; use craft_ui::buttons::Button; use craft_ui::choice::{Checkbox, Swatch}; use craft_ui::contract::{Bind, EditResponse, Outcome, Pattern, Props as _, Remember as _, Show as _, ShowContainer as _, clicked}; -use craft_ui::form::FieldGroup; use craft_ui::layout_components::Row; use craft_ui::runtime::env::{Label, UnitKind}; use craft_ui::runtime::requests::ColorRequest; use egui::Ui; -use crate::ui::adjust::sliders::{Run, lifted, read, words}; +use crate::ui::adjust::sliders::{Run, editor_group, lifted, read, words}; use crate::ui::adjust::style::{mix, tones}; use crate::ui::adjust::{AdjustContext, AdjustField, AdjustTrack}; @@ -117,7 +116,7 @@ impl<'a> BlackWhiteEditor<'a> { let BlackWhiteEditor { label, mut value, context } = self; let words = words(ui); let mut out = None; - let shown = FieldGroup::new(label).show(ui, |ui, cx| { + let shown = editor_group(ui, label, context, |ui, cx| { let now = read(&mut value, cx).unwrap_or_default(); let names = [&words.reds, &words.yellows, &words.greens, &words.cyans, &words.blues, &words.magentas]; let keys = ["reds", "yellows", "greens", "cyans", "blues", "magentas"]; diff --git a/crates/imaging/src/ui/adjust/mixers/channel_mixer.rs b/crates/imaging/src/ui/adjust/mixers/channel_mixer.rs index 186b73b8d..bee271e7a 100644 --- a/crates/imaging/src/ui/adjust/mixers/channel_mixer.rs +++ b/crates/imaging/src/ui/adjust/mixers/channel_mixer.rs @@ -4,13 +4,13 @@ use craft_theme::variant::Tone; use craft_ui::choice::Checkbox; use craft_ui::contract::{Bind, EditResponse, Entry, Pattern, Props as _, Remember as _, Show as _, ShowContainer as _}; use craft_ui::display::Text; -use craft_ui::form::{Field, FieldGroup, Help}; +use craft_ui::form::{Field, Help}; use craft_ui::runtime::env::{Env, Label, UnitKind, Units}; use craft_ui::style::TypeRole; use egui::Ui; use super::Tables; -use crate::ui::adjust::sliders::{Run, lifted, read, words}; +use crate::ui::adjust::sliders::{Run, editor_group, lifted, read, words}; use crate::ui::adjust::style::{mix, tones}; use crate::ui::adjust::{AdjustContext, AdjustField, AdjustTrack, AdjustWords}; @@ -127,7 +127,7 @@ impl<'a> ChannelMixerEditor<'a> { let words = words(ui); let units = Env::of(ui).units(); let mut edit = None; - let shown = FieldGroup::new(label.clone()).show(ui, |ui, cx| { + let shown = editor_group(ui, label.clone(), context, |ui, cx| { let now = read(&mut value, cx).unwrap_or_default(); let mono = now.gray.is_some(); // Monochrome has the one output, Gray. diff --git a/crates/imaging/src/ui/adjust/mixers/selective_color.rs b/crates/imaging/src/ui/adjust/mixers/selective_color.rs index 80d84c500..edd5558b7 100644 --- a/crates/imaging/src/ui/adjust/mixers/selective_color.rs +++ b/crates/imaging/src/ui/adjust/mixers/selective_color.rs @@ -2,13 +2,13 @@ use craft_ui::choice::{Radio, RadioGroup}; use craft_ui::contract::{Bind, EditResponse, Entry, Outcome, Pattern, Props as _, Remember as _, Show as _, ShowContainer as _}; -use craft_ui::form::{Field, FieldGroup}; +use craft_ui::form::Field; use craft_ui::layout_components::Row; use craft_ui::runtime::env::{Label, UnitKind}; use egui::Ui; use super::Tables; -use crate::ui::adjust::sliders::{Run, lifted, read, words}; +use crate::ui::adjust::sliders::{Run, editor_group, lifted, read, words}; use crate::ui::adjust::{AdjustContext, AdjustField}; /// Selective Color's method. @@ -110,7 +110,7 @@ impl<'a> SelectiveColorEditor<'a> { let SelectiveColorEditor { label, mut value, context } = self; let words = words(ui); let mut out = None; - let shown = FieldGroup::new(label).show(ui, |ui, cx| { + let shown = editor_group(ui, label, context, |ui, cx| { let now = read(&mut value, cx).unwrap_or_default(); let memory = cx.memory::().clone(); let range = memory.range.min(RANGES - 1); diff --git a/crates/imaging/src/ui/adjust/sliders.rs b/crates/imaging/src/ui/adjust/sliders.rs index 1fb92793f..4719c16d6 100644 --- a/crates/imaging/src/ui/adjust/sliders.rs +++ b/crates/imaging/src/ui/adjust/sliders.rs @@ -12,9 +12,9 @@ use std::ops::RangeInclusive; use std::sync::Arc; use craft_behavior::{EditEvent, EditEvents, Mixed, Value, decimals_for_step}; -use craft_ui::contract::{Bind, BodyCx, EditResponse, Pattern, Props as _, Remember as _, Show as _, ShowContainer as _}; +use craft_ui::contract::{Bind, BodyCx, EditResponse, Inner, Pattern, Props as _, Remember as _, Show as _, ShowContainer as _}; use craft_ui::form::{FieldGroup, SliderField}; -use craft_ui::runtime::env::{Env, Label, UnitKind}; +use craft_ui::runtime::env::{Env, Label, UnitKind, WheelScope, with_env}; use craft_ui::style::DataColor; use craft_ui::value_drag::TrackFace; use egui::{Response, Ui}; @@ -177,7 +177,7 @@ impl<'a> AdjustSliders<'a> { pub fn show(self, ui: &mut Ui) -> EditResponse> { let AdjustSliders { label, fields, mut values, context } = self; let mut edit = None; - let shown = FieldGroup::new(label).show(ui, |ui, cx| { + let shown = editor_group(ui, label, context, |ui, cx| { let held = read(&mut values, cx).unwrap_or_default(); let now: Vec = fields.iter().enumerate().map(|(i, f)| f.clamp(held.get(i).copied().unwrap_or(f.default))).collect(); let mut run = Run::start(&mut values, context, now.clone(), cx.memory::().began.clone()); @@ -195,6 +195,18 @@ impl<'a> AdjustSliders<'a> { } } +/// The editor's `FieldGroup` around `body`. In a dialog its sliders take the wheel without focus +/// (`WheelScope::Free`); in the Properties panel only while focused or armed, so the panel scrolls over them. +pub(super) fn editor_group( + ui: &mut Ui, + label: Label, + context: AdjustContext, + body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R, +) -> Inner { + let wheel = if context.is_dialog() { WheelScope::Free } else { WheelScope::Plain }; + with_env(ui, |scope| scope.wheel = Some(wheel), |ui| FieldGroup::new(label).show(ui, body)) +} + /// The value of `bind` a body starts its pass from; `None` while it is mixed. pub(super) fn read(bind: &mut Bind<'_, V>, cx: &mut BodyCx<'_>) -> Option { match bind.reborrow().read(cx) { diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 346897c7e..28603a82f 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -570,7 +570,7 @@ impl ShapeHooks for BodyHooks<'_> { .vertical(vertical) .horizontal(horizontal) .scrolls(true) - .gated(crate::runtime::env::Env::of(pcx.ui).wheel_gated) + .gated(crate::runtime::env::Env::of(pcx.ui).wheel == crate::runtime::env::WheelScope::Gated) .notch(notch) .room(WheelRoom::new(positive, negative)), ); diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 8d0a1ed5a..0208112f2 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -13,6 +13,7 @@ use crate::contract::{Bind, Collection, Cx, Internal, ItemCx, ItemOutput as _, P use crate::primitives::{HiddenContent, ScrollBar}; use crate::runtime::access::{self, Role, SetPosition}; use crate::runtime::egui::{alloc, areas, child, response}; +use crate::runtime::env::WheelScope; use crate::runtime::env::{AppValues, ArenaId, Env, ItemFacts}; use crate::runtime::memory::{MemoryStage, sub}; use crate::runtime::{focus, keys, layout_memory, lazy, overlay, wheel}; @@ -295,7 +296,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { .vertical((vertical && max.y > 0.0) || horizontal) .horizontal(horizontal) .scrolls(true) - .gated(cx.core.env.wheel_gated()) + .gated(cx.core.env.wheel() == WheelScope::Gated) .room(WheelRoom::new(offset, max - offset)) .zoom(comp.zooms()), ); diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 88550d322..6ac9f973c 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -676,7 +676,7 @@ fn run_in_host(p: &mut Pass<'_, '_, C>, ui: &mut Ui, host_at: Opti // A body's wheel share is its scroll taker (body step, step 7); the gate value does not take the wheel. A body root that // senses the wheel itself (a canvas) registers like any other component. if !inert && (p.hooks.shape() != ShapeKind::Body || own.intersects(Sense::WHEEL | Sense::WHEEL_X)) { - steps::register_wheel(&ctx, id, rect, own, comp.bind().is_some(), env.wheel_gated); + steps::register_wheel(&ctx, id, rect, own, comp.bind().is_some(), env.wheel); } // 8 sense p.trace.mark(Step::Sense); diff --git a/crates/ui-contract/src/contract/steps/sense.rs b/crates/ui-contract/src/contract/steps/sense.rs index f70553889..5594977b2 100644 --- a/crates/ui-contract/src/contract/steps/sense.rs +++ b/crates/ui-contract/src/contract/steps/sense.rs @@ -1,19 +1,22 @@ //! Step 8, sense: part states from the sensing core and the wheel takers. +use crate::runtime::env::WheelScope; use crate::runtime::wheel; use crate::style::{Flags, StateSet}; use craft_behavior::Sense; use craft_theme::{State, StateMix}; use egui::{Context, Id, Rect}; -/// Registers a wheel taker for next pass's split (8.6) from the wheel bits of `sense` (`WheelTaker::for_sense`); a -/// value control without them takes the vertical wheel only while focused or armed. Inside a wheel-gated scope -/// (`Overrides::wheel_gated`) every taker takes the wheel, on every axis, only while focused or armed. -pub(crate) fn register_wheel(ctx: &Context, id: Id, rect: Rect, sense: Sense, value_control: bool, gated: bool) { - if let Some(taker) = craft_behavior::WheelTaker::for_sense(id, rect, sense) { - wheel::register(ctx, taker.gated(taker.gated || gated)); - } else if value_control { - wheel::register(ctx, craft_behavior::WheelTaker::new(id, rect).vertical(true).gated(true)); +/// Registers a wheel taker for next pass's split (8.6) from the wheel bits of `sense` (`WheelTaker::for_sense`). A +/// value control takes the wheel only while focused or armed, on the axes it senses (the vertical one when it senses +/// none); inside a `WheelScope::Free` scope one that senses the wheel takes it without focus. Inside a +/// `WheelScope::Gated` scope every taker waits for focus or arming. +pub(crate) fn register_wheel(ctx: &Context, id: Id, rect: Rect, sense: Sense, value_control: bool, scope: WheelScope) { + let sensed = craft_behavior::WheelTaker::for_sense(id, rect, sense); + let free = sensed.is_some() && scope == WheelScope::Free; + let taker = sensed.or_else(|| value_control.then(|| craft_behavior::WheelTaker::new(id, rect).vertical(true))); + if let Some(taker) = taker { + wheel::register(ctx, taker.gated(scope == WheelScope::Gated || (value_control && !free))); } } diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index 0d8a63aaa..adc53d97e 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -39,7 +39,7 @@ mod view; pub use app_values::{AppValues, EnvValue, StyleValue}; use host_words::{host_dates, host_format, host_words}; -pub use overrides::{Overrides, ThemeScope, WordsOverride, with_env}; +pub use overrides::{Overrides, ThemeScope, WheelScope, WordsOverride, with_env}; pub use root::{ResolvedRoot, RootEnv}; pub(crate) use tokens::TokenInputs; #[cfg(feature = "testing")] @@ -82,9 +82,8 @@ pub struct Scoped { pub enabled: bool, /// OR of every scope above. pub read_only: bool, - /// OR of every scope above: wheel takers inside register gated, taking the wheel only while focused or armed - /// (a card in a scrolling overview). A layer opened from inside starts ungated. - pub wheel_gated: bool, + /// How wheel takers inside take the wheel (`WheelScope`). A layer opened from inside starts plain. + pub wheel: WheelScope, /// The flow direction: layout order, arrow keys and roving. A container whose axis keeps its order /// (`Props::ltr_axis`) sets it to left to right for what is inside. pub direction: Direction, @@ -345,7 +344,7 @@ impl Env { size: ControlSize::Md, enabled: true, read_only: false, - wheel_gated: false, + wheel: WheelScope::Plain, direction: root.direction, text_direction: root.direction, ids_stable: true, @@ -411,7 +410,7 @@ impl Env { s.roving = None; s.transform = None; s.scroll_owner = None; - s.wheel_gated = false; + s.wheel = WheelScope::Plain; s.unbounded = (false, false); s.lazy = None; s.item = None; diff --git a/crates/ui-contract/src/runtime/env/overrides.rs b/crates/ui-contract/src/runtime/env/overrides.rs index 319603e16..ff098bfee 100644 --- a/crates/ui-contract/src/runtime/env/overrides.rs +++ b/crates/ui-contract/src/runtime/env/overrides.rs @@ -40,6 +40,20 @@ impl std::fmt::Debug for ThemeScope { } } +/// How the wheel takers of a scope take the wheel. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum WheelScope { + /// A value control takes the wheel only while focused or armed; every other taker whenever the pointer is over it. + #[default] + Plain, + /// A value control that senses the wheel takes it without focus: the sliders of an adjustment dialog. + Free, + /// Every taker takes the wheel only while focused or armed: a card in a scrolling overview. Wins over a `Free` set + /// inside it, so a scroll of the overview never stops on a story's slider. + Gated, +} + /// Changes to the inherited values for a stretch of code. #[derive(Default)] #[non_exhaustive] @@ -50,8 +64,8 @@ pub struct Overrides { pub enabled: Option, /// Only `true` has an effect. pub read_only: Option, - /// Only `true` has an effect. - pub wheel_gated: Option, + /// Has no effect inside a `WheelScope::Gated` scope. + pub wheel: Option, /// Can only lower the mode. pub motion: Option, /// Also the child `Ui`'s direction and layout. @@ -83,8 +97,10 @@ impl Overrides { if self.read_only == Some(true) { scoped.read_only = true; } - if self.wheel_gated == Some(true) { - scoped.wheel_gated = true; + if let Some(wheel) = self.wheel + && scoped.wheel != WheelScope::Gated + { + scoped.wheel = wheel; } if let Some(motion) = self.motion { let inherited = scoped.inherited_mut(); diff --git a/crates/ui-contract/src/runtime/env/view.rs b/crates/ui-contract/src/runtime/env/view.rs index 889872b5a..12fbcb330 100644 --- a/crates/ui-contract/src/runtime/env/view.rs +++ b/crates/ui-contract/src/runtime/env/view.rs @@ -33,9 +33,9 @@ impl<'a> EnvView<'a> { self.scoped.facts_of(id) } - /// Whether wheel takers in this scope take the wheel only while focused or armed. - pub(crate) fn wheel_gated(&self) -> bool { - self.scoped.wheel_gated + /// How wheel takers in this scope take the wheel. + pub(crate) fn wheel(&self) -> super::WheelScope { + self.scoped.wheel } /// The innermost scrolling body around the component. diff --git a/crates/ui-gallery/src/chrome/card.rs b/crates/ui-gallery/src/chrome/card.rs index b3d8ec08f..0b1efff9f 100644 --- a/crates/ui-gallery/src/chrome/card.rs +++ b/crates/ui-gallery/src/chrome/card.rs @@ -394,7 +394,7 @@ impl Host<'_> { |env| { env.direction = look.direction; env.size = look.size; - env.wheel_gated = Some(gated); + env.wheel = gated.then_some(craft_ui::runtime::env::WheelScope::Gated); }, |ui| show(ui, data), ) diff --git a/crates/ui-widgets/src/value_drag/angle_dial.rs b/crates/ui-widgets/src/value_drag/angle_dial.rs index 8fd417b28..a2e4ff5b8 100644 --- a/crates/ui-widgets/src/value_drag/angle_dial.rs +++ b/crates/ui-widgets/src/value_drag/angle_dial.rs @@ -269,7 +269,7 @@ impl Component for AngleDial<'_> { } fn sense(&self) -> Sense { - Sense::FOCUS + Sense::FOCUS | Sense::WHEEL_X | Sense::WHEEL_Y } fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { diff --git a/crates/ui-widgets/src/value_drag/point_picker.rs b/crates/ui-widgets/src/value_drag/point_picker.rs index dde4623b8..aef465410 100644 --- a/crates/ui-widgets/src/value_drag/point_picker.rs +++ b/crates/ui-widgets/src/value_drag/point_picker.rs @@ -274,7 +274,7 @@ impl Component for PointPicker<'_> { } fn sense(&self) -> Sense { - Sense::FOCUS + Sense::FOCUS | Sense::WHEEL_X | Sense::WHEEL_Y } fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { diff --git a/crates/ui-widgets/src/value_drag/range_slider.rs b/crates/ui-widgets/src/value_drag/range_slider.rs index c50b3c2a4..3bf00984e 100644 --- a/crates/ui-widgets/src/value_drag/range_slider.rs +++ b/crates/ui-widgets/src/value_drag/range_slider.rs @@ -198,8 +198,10 @@ impl Component for RangeSlider<'_> { Vec2::new(cx.style.length, cx.icon() + self.marks.below(&cx.style.track)) } + /// No wheel bits: as a value control the row takes the vertical wheel only while focused or armed, and no scope + /// frees it, since a wheel over the track has no one thumb to step. fn sense(&self) -> Sense { - Sense::WHEEL | Sense::WHEEL_X + Sense::empty() } /// The track, and a thumb each that is its own Tab stop and takes the arrow keys. diff --git a/crates/ui-widgets/src/value_drag/slider.rs b/crates/ui-widgets/src/value_drag/slider.rs index d3fed5723..c5b01680d 100644 --- a/crates/ui-widgets/src/value_drag/slider.rs +++ b/crates/ui-widgets/src/value_drag/slider.rs @@ -1,7 +1,8 @@ //! `Slider`: a thumb on a track over an `f64` range. A press on the thumb grabs it and a drag moves it by the pointer's //! travel; a press on the track jumps the thumb there; arrows, the wheel and AccessKit step the value; a double click -//! or the reset intent restores the binding's reset value. The track maps positions to values through a -//! [`SliderScale`], linear by default. +//! or the reset intent restores the binding's reset value. The vertical wheel steps it while it is focused or armed, so +//! a panel scrolls over it; inside a `WheelScope::Free` scope (an adjustment dialog) without focus too. The track maps +//! positions to values through a [`SliderScale`], linear by default. use super::track::{self, Along, Fill, MarkInk, MarkSpace, Marks, Pick, Press, Scale, SliderScale, TrackFace, Travel}; use crate::contract::{ @@ -183,7 +184,7 @@ impl Component for Slider<'_> { } fn sense(&self) -> Sense { - Sense::FOCUS | Sense::WHEEL | Sense::WHEEL_X + Sense::FOCUS | Sense::WHEEL_Y } fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { diff --git a/crates/ui-widgets/src/value_drag/thumb_row.rs b/crates/ui-widgets/src/value_drag/thumb_row.rs index 33f9694f2..e08557960 100644 --- a/crates/ui-widgets/src/value_drag/thumb_row.rs +++ b/crates/ui-widgets/src/value_drag/thumb_row.rs @@ -758,8 +758,10 @@ impl Component for ThumbRow<'_, N> { Some(craft_behavior::Axis::X) } + /// No wheel bits: as a value control the row takes the vertical wheel only while focused or armed, and no scope + /// frees it, since a wheel over the track has no one thumb to step. fn sense(&self) -> Sense { - Sense::WHEEL | Sense::WHEEL_X + Sense::empty() } fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { diff --git a/crates/ui-widgets/tests/it/value_drag.rs b/crates/ui-widgets/tests/it/value_drag.rs index 7299caf5c..beaace1b9 100644 --- a/crates/ui-widgets/tests/it/value_drag.rs +++ b/crates/ui-widgets/tests/it/value_drag.rs @@ -126,3 +126,63 @@ fn a_leaf_at_a_degenerate_rect_neither_panics_nor_changes_its_value() { assert_eq!(*value.borrow(), START, "a slider shown at {rect:?} changed its value"); } } + +/// The wheel over a slider in a scrolling panel scrolls the panel while the slider has no focus; it steps the slider +/// once the slider has focus, or without focus inside a `WheelScope::Free` scope (an adjustment dialog). An outer +/// `WheelScope::Gated` scope (a card in the Overview) wins over a free one inside it. +#[trial] +fn the_wheel_steps_a_slider_when_focused_or_freed_and_otherwise_scrolls_the_panel() { + use craft_ui::contract::{ContainerProps, Scroll, ShowContainer as _}; + use craft_ui::runtime::env::{WheelScope, with_env}; + use craft_ui::runtime::focus::{FocusTarget, request}; + use craft_ui::testing::layout::{Block, in_region}; + use craft_ui_test_kit::body::Slab; + use std::cell::Cell; + + /// How far the value and the panel moved after one wheel turn over the slider, shown in the scopes `scopes` + /// (outer first) inside the panel. + fn turned(scopes: &[WheelScope], focused: bool) -> (f64, f32) { + fn nested(ui: &mut Ui, scopes: &[WheelScope], body: &mut dyn FnMut(&mut Ui)) { + match scopes.split_first() { + Some((scope, inner)) => with_env(ui, |set| set.wheel = Some(*scope), |ui| nested(ui, inner, body)), + None => body(ui), + } + } + let value = RefCell::new(START); + let (top, slider) = (Cell::new(0.0_f32), Cell::new(None)); + let mut show = |ui: &mut Ui| { + in_region(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(300.0, 200.0)), |ui| { + Slab::new(ContainerProps::new().scroll(Scroll::vertical())).id_salt("panel").show(ui, |ui, _| { + top.set(ui.next_widget_position().y); + nested(ui, scopes, &mut |ui| { + let mut value = value.borrow_mut(); + let shown = Slider::new(Bind::bound(&mut *value), 0.0..=100.0).label("Opacity").show(ui); + slider.set(Some((shown.response.id, shown.response.rect))); + }); + Block::new(Vec2::new(100.0, 1000.0)).show(ui); + }); + }); + }; + let mut frames = Frames::new(); + frames.idle(3, &mut show); + let (id, rect) = slider.get().expect("the slider showed"); + if focused { + request(&frames.ctx, FocusTarget::Id(id)); + frames.idle(2, &mut show); + } + let (value_before, top_before) = (*value.borrow(), top.get()); + frames.frame(vec![pointer::moved(rect.center())], &mut show); + frames.frame(vec![pointer::wheel(-40.0)], &mut show); + frames.idle(2, &mut show); + (*value.borrow() - value_before, top_before - top.get()) + } + + let (value, panel) = turned(&[], false); + assert!(value == 0.0 && panel > 0.0, "unfocused: the panel scrolls and the value stays ({value}, {panel})"); + let (value, panel) = turned(&[], true); + assert!(value != 0.0 && panel == 0.0, "focused: the slider steps and the panel stays ({value}, {panel})"); + let (value, panel) = turned(&[WheelScope::Free], false); + assert!(value != 0.0 && panel == 0.0, "free scope: the unfocused slider steps ({value}, {panel})"); + let (value, panel) = turned(&[WheelScope::Gated, WheelScope::Free], false); + assert!(value == 0.0 && panel > 0.0, "a gated scope wins over a free one inside it ({value}, {panel})"); +}