Skip to content

Commit

Permalink
Allow wrapping Widgets in Pair (#78)
Browse files Browse the repository at this point in the history
This allows you to track changes to a widget's state.
  • Loading branch information
CryZe committed Nov 13, 2023
1 parent 4e789ef commit d0fbc56
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ jobs:

steps:
- name: Checkout Commit
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Rust
uses: hecrj/setup-rust-action@v1
uses: hecrj/setup-rust-action@v2
with:
rust-version: ${{ matrix.toolchain || 'stable' }}

Expand Down Expand Up @@ -67,10 +67,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Commit
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Rust
uses: hecrj/setup-rust-action@v1
uses: hecrj/setup-rust-action@v2
with:
components: clippy

Expand All @@ -82,10 +82,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Commit
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Rust
uses: hecrj/setup-rust-action@v1
uses: hecrj/setup-rust-action@v2
with:
components: rustfmt

Expand Down
23 changes: 19 additions & 4 deletions src/runtime/settings/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#[cfg(feature = "derive")]
pub use asr_derive::Gui;

use crate::runtime::sys;
use crate::{runtime::sys, watcher::Pair};

use super::map::Map;

Expand Down Expand Up @@ -78,9 +78,7 @@ pub trait Gui {
fn update_from(&mut self, settings_map: &Map);
}

/// A settings widget that can be added to the settings GUI. This is an internal
/// trait that you don't need to worry about.
#[doc(hidden)]
/// A settings widget that can be used as a field when defining a settings [`Gui`].
pub trait Widget {
/// The arguments that are needed to register the widget.
type Args: Default;
Expand Down Expand Up @@ -146,3 +144,20 @@ impl Widget for Title {
#[inline]
fn update_from(&mut self, _settings_map: &Map, _key: &str, _args: Self::Args) {}
}

impl<T: Copy + Widget> Widget for Pair<T> {
type Args = T::Args;

fn register(key: &str, description: &str, args: Self::Args) -> Self {
let value = T::register(key, description, args);
Pair {
old: value,
current: value,
}
}

fn update_from(&mut self, settings_map: &Map, key: &str, args: Self::Args) {
self.old = self.current;
self.current.update_from(settings_map, key, args);
}
}

0 comments on commit d0fbc56

Please sign in to comment.