Skip to content

Commit

Permalink
Simplify Button color caching logic
Browse files Browse the repository at this point in the history
Fixes #180
  • Loading branch information
ecton committed Oct 16, 2024
1 parent ab474e2 commit 5b92832
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
its child changes size.
- `Stack` and `Grid` now properly recompute exact-sized `Lp` children when the
display scale is changed.
- `Button`'s colors are now fully reactive. The caching code to prevent color
duplicate change animations has been simplified to fix this.

### Added

Expand Down
2 changes: 1 addition & 1 deletion src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ pub trait MakeWidget: Sized {
dynamic: impl IntoDynamicComponentValue,
) -> Style
where
Value<C::ComponentType>: IntoComponentValue,
C::ComponentType: IntoComponentValue,
{
Style::new(Styles::new().with_dynamic(name, dynamic), self)
}
Expand Down
25 changes: 7 additions & 18 deletions src/widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use kludgine::shapes::{Shape, StrokeOptions};
use kludgine::Color;

use crate::animation::{AnimationHandle, AnimationTarget, IntoAnimate, LinearInterpolate, Spawn};
use crate::context::{
AsEventContext, EventContext, GraphicsContext, LayoutContext, WidgetCacheKey, WidgetContext,
};
use crate::context::{AsEventContext, EventContext, GraphicsContext, LayoutContext, WidgetContext};
use crate::styles::components::{
AutoFocusableControls, DefaultActiveBackgroundColor, DefaultActiveForegroundColor,
DefaultBackgroundColor, DefaultDisabledBackgroundColor, DefaultDisabledForegroundColor,
Expand Down Expand Up @@ -50,8 +48,7 @@ struct PerWindow {

#[derive(Default, Debug, Eq, PartialEq, Clone, Copy)]
struct CacheState {
key: WidgetCacheKey,
kind: ButtonKind,
style: Option<ButtonColors>,
}

/// The type of a [`Button`] or similar clickable widget.
Expand Down Expand Up @@ -232,13 +229,6 @@ impl Button {
let kind = self.kind.get_tracking_redraw(context);
let visual_state = Self::visual_style(context);

let window_local = self.per_window.entry(context).or_default();

window_local.cached_state = CacheState {
key: context.cache_key(),
kind,
};

if context.is_default() {
kind.colors_for_default(visual_state, context)
} else {
Expand All @@ -253,6 +243,10 @@ impl Button {
fn update_colors(&mut self, context: &mut WidgetContext<'_>, immediate: bool) {
let new_style = self.determine_stateful_colors(context);
let window_local = self.per_window.entry(context).or_default();
if window_local.cached_state.style.as_ref() == Some(&new_style) {
return;
}
window_local.cached_state.style = Some(new_style);

match (immediate, &window_local.active_colors) {
(false, Some(style)) => {
Expand Down Expand Up @@ -371,12 +365,7 @@ impl Widget for Button {
#![allow(clippy::similar_names)]

let current_style = self.kind.get_tracking_redraw(context);
let window_local = self.per_window.entry(context).or_default();
if window_local.cached_state.key != context.cache_key()
|| window_local.cached_state.kind != current_style
{
self.update_colors(context, false);
}
self.update_colors(context, false);

let style = self.current_style(context);
context.fill(style.background);
Expand Down

0 comments on commit 5b92832

Please sign in to comment.