Dynamic::try_lock

Plus other minor changes.
This commit is contained in:
Jonathan Johnson 2024-01-26 18:13:42 -08:00
parent 3f8fed65c3
commit 0e5976de10
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
7 changed files with 38 additions and 7 deletions

View file

@ -215,6 +215,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Delimiter` is a new widget that is similar to html's `hr` tag.
- `List` is a new widget that creates lists similar to HTML's `ol` and `ul`
tags.
- `Dynamic::try_lock()` is a panic-free version of `Dynamic::lock()`.
[plotters]: https://github.com/plotters-rs/plotters

View file

@ -1363,7 +1363,7 @@ pub(crate) mod sealed {
fn inner_invalidate_when_changed(&self, handle: WindowHandle, id: WidgetId);
}
#[derive(Default, Clone)]
#[derive(Debug, Default, Clone)]
pub struct InvalidationStatus {
refresh_sent: Arc<AtomicBool>,
invalidated: Arc<Mutex<Set<WidgetId>>>,

View file

@ -1089,6 +1089,23 @@ impl<T> Dynamic<T> {
self.lock_inner()
}
/// Returns an exclusive reference to the contents of this dynamic.
///
/// This call will block until all other guards for this dynamic have been
/// dropped.
///
/// # Errors
///
/// Returns an error if the current thread already holds a lock to this
/// dynamic.
pub fn try_lock(&self) -> Result<DynamicGuard<'_, T>, DeadlockError> {
Ok(DynamicGuard {
guard: self.0.state()?,
accessed_mut: false,
prevent_notifications: false,
})
}
fn lock_inner<const READONLY: bool>(&self) -> DynamicGuard<'_, T, READONLY> {
DynamicGuard {
guard: self.0.state().expect("deadlocked"),

View file

@ -2129,6 +2129,18 @@ impl Dynamic<WidgetList> {
self.clone().into_layers()
}
/// Returns `self` as an unordered [`List`].
#[must_use]
pub fn into_list(self) -> List {
List::new(self)
}
/// Returns `self` as an unordered [`List`].
#[must_use]
pub fn to_list(self) -> List {
self.clone().into_list()
}
/// Returns a [`Wrap`] that lays the children out horizontally, wrapping
/// into additional rows as needed.
#[must_use]

View file

@ -9,7 +9,7 @@ use crate::ConstraintLimit;
///
/// Some parent widgets support weighting children when there is more than one
/// [`Expand`]ed widget.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Expand {
kind: ExpandKind,
child: WidgetRef,

View file

@ -476,7 +476,8 @@ fn build_grid_widgets(style: &ListStyle, children: &WidgetList) -> GridWidgets<2
.list_indicator(index.wrapping_add(1))
.unwrap_or_default(),
)
.align_right(),
.align_right()
.align_top(),
child.clone().align_left().make_widget(),
)
})

View file

@ -1964,7 +1964,7 @@ pub(crate) mod sealed {
pub on_closed: Option<OnceCallback>,
}
#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum WindowCommand {
Redraw,
RequestClose,
@ -2079,7 +2079,7 @@ fn default_family(query: Family<'_>) -> Option<FamilyOwned> {
}
/// A handle to an open Cushy window.
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct WindowHandle {
inner: InnerWindowHandle,
pub(crate) redraw_status: InvalidationStatus,
@ -2140,7 +2140,7 @@ impl Hash for WindowHandle {
}
}
#[derive(Clone)]
#[derive(Debug, Clone)]
enum InnerWindowHandle {
Pending(Arc<PendingWindowHandle>),
Known(kludgine::app::WindowHandle<WindowCommand>),
@ -2233,7 +2233,7 @@ impl PendingWindow {
}
}
#[derive(Default)]
#[derive(Debug, Default)]
struct PendingWindowHandle {
handle: OnceLock<kludgine::app::WindowHandle<WindowCommand>>,
commands: Mutex<Vec<WindowCommand>>,