cacao/src/layout/attributes.rs

220 lines
7.2 KiB
Rust
Raw Normal View History

Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-12 17:57:06 -08:00
use crate::foundation::{NSInteger, NSUInteger};
/// Represents whether a layout is vertical or horizontal.
#[derive(Debug)]
pub enum LayoutConstraintOrientation {
/// Horizontal orientation.
Horizontal,
/// Vertical orientation.
Vertical,
/// Represents an unknown value. This should never be constructed, but acts as a guard against
/// a change in representation on the framework side. If a new value was ever introduced, it's
/// caught here, and applications can handle it themselves if need be.
Unknown(NSInteger)
}
impl From<NSInteger> for LayoutConstraintOrientation {
fn from(i: NSInteger) -> Self {
match i {
0 => Self::Horizontal,
1 => Self::Vertical,
i => Self::Unknown(i)
}
}
}
/// Represents a relation between layout constraints. Used mostly internally.
#[derive(Debug)]
pub enum LayoutRelation {
/// Relation is less than or equal to another specified relation.
LessThanOrEqual,
/// Relation is equal to another specified relation.
Equal,
/// Relation is greater than or equal to another specified relation.
GreaterThanOrEqual,
/// Represents an unknown value. This should never be constructed, but acts as a guard against
/// a change in representation on the framework side. If a new value was ever introduced, it's
/// caught here, and applications can handle it themselves if need be.
Unknown(NSInteger)
}
impl From<NSInteger> for LayoutRelation {
fn from(i: NSInteger) -> Self {
match i {
-1 => Self::LessThanOrEqual,
0 => Self::Equal,
1 => Self::GreaterThanOrEqual,
i => Self::Unknown(i)
}
}
}
/// Represents attributes for various layouts and constraints.
///
/// Note that this only covers attributes that are shared across platforms. In general, this is enough
/// to build apps that work everywhere - but if you need to specify something else, you can handle
/// it yourself with the `Unknown` variant.
#[derive(Debug)]
pub enum LayoutAttribute {
/// The left side of the objects alignment rectangle.
Left,
/// The right side of the objects alignment rectangle.
Right,
/// The top of the objects alignment rectangle.
Top,
/// The bottom of the objects alignment rectangle.
Bottom,
/// The leading edge of the objects alignment rectangle.
Leading,
/// The trailing edge of the objects alignment rectangle.
Trailing,
/// The width of the objects alignment rectangle.
Width,
/// The height of the objects alignment rectangle.
Height,
/// The center along the x-axis of the objects alignment rectangle.
CenterX,
/// The center along the y-axis of the objects alignment rectangle.
CenterY,
2022-07-10 17:15:29 +02:00
/// The objects baseline. For objects with more than one line of text,
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-12 17:57:06 -08:00
/// this is the baseline for the bottommost line of text.
LastBaseline,
2022-07-10 17:15:29 +02:00
/// The objects baseline. For objects with more than one line of text,
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-12 17:57:06 -08:00
/// this is the baseline for the topmost line of text.
FirstBaseline,
2022-07-10 17:15:29 +02:00
/// A placeholder value that is used to indicate that the constraints
/// second item and second attribute are not used in any calculations.
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-12 17:57:06 -08:00
///
2022-07-10 17:15:29 +02:00
/// This can be useful constraint that assigns a constant to an attribute.
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-12 17:57:06 -08:00
NotAnAttribute,
/// Represents an unknown value. This should never be constructed, but acts as a guard against
/// a change in representation on the framework side. If a new value was ever introduced, it's
/// caught here, and applications can handle it themselves if need be.
Unknown(NSInteger)
}
impl From<NSInteger> for LayoutAttribute {
fn from(i: NSInteger) -> Self {
match i {
1 => Self::Left,
2 => Self::Right,
3 => Self::Top,
4 => Self::Bottom,
5 => Self::Leading,
6 => Self::Trailing,
7 => Self::Width,
8 => Self::Height,
9 => Self::CenterX,
10 => Self::CenterY,
11 => Self::LastBaseline,
12 => Self::FirstBaseline,
0 => Self::NotAnAttribute,
i => Self::Unknown(i)
}
}
}
/// Represents a layout format.
///
/// Note that this only covers formats that are shared across platforms. In general, this is enough
/// to build apps that work everywhere - but if you need to specify something else, you can handle
/// it yourself with the `Unknown` variant.
#[derive(Debug)]
pub enum LayoutFormat {
/// Align all specified interface elements using NSLayoutAttributeLeft on each.
AlignAllLeft,
/// Align all specified interface elements using NSLayoutAttributeRight on each.
AlignAllRight,
/// Align all specified interface elements using NSLayoutAttributeTop on each.
AlignAllTop,
/// Align all specified interface elements using NSLayoutAttributeBottom on each.
AlignAllBottom,
/// Align all specified interface elements using NSLayoutAttributeLeading on each.
AlignAllLeading,
/// Align all specified interface elements using NSLayoutAttributeTrailing on each.
AlignAllTrailing,
/// Align all specified interface elements using NSLayoutAttributeCenterX on each.
AlignAllCenterX,
/// Align all specified interface elements using NSLayoutAttributeCenterY on each.
AlignAllCenterY,
/// Align all specified interface elements using the last baseline of each one.
AlignAllLastBaseline,
2022-07-10 17:15:29 +02:00
/// Arrange objects in order based on the normal text flow for the current user
/// interface language. In left-to-right languages (like English), this arrangement
/// results in the first object being placed farthest to the left, the next one to
/// its right, and so on. In right-to-left languages (like Arabic or Hebrew), the
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-12 17:57:06 -08:00
/// ordering is reversed.
DirectionLeadingToTrailing,
/// Arrange objects in order from left to right.
DirectionLeftToRight,
/// Arrange objects in order from right to left.
DirectionRightToLeft,
/// Represents an unknown value. This should never be constructed, but acts as a guard against
/// a change in representation on the framework side. If a new value was ever introduced, it's
/// caught here, and applications can handle it themselves if need be.
Unknown(NSUInteger)
}
impl From<NSUInteger> for LayoutFormat {
fn from(i: NSUInteger) -> Self {
match i {
2 => Self::AlignAllLeft,
4 => Self::AlignAllRight,
8 => Self::AlignAllTop,
16 => Self::AlignAllBottom,
32 => Self::AlignAllLeading,
64 => Self::AlignAllTrailing,
512 => Self::AlignAllCenterX,
1024 => Self::AlignAllCenterY,
2048 => Self::AlignAllLastBaseline,
0 => Self::DirectionLeadingToTrailing,
65536 => Self::DirectionLeftToRight,
131072 => Self::DirectionRightToLeft,
i => Self::Unknown(i)
}
}
}
/// Specifies layout priority.
#[derive(Debug)]
pub enum LayoutPriority {
/// Highest priority.
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-12 17:57:06 -08:00
Required,
/// High priority. Will bend if absolutely necessary.
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-12 17:57:06 -08:00
High,
/// Low priority.
Begin reworking many internals to push for v0.1. - Beginning to transition View types to use Rc/RefCell internally, which should provide better guarantees about ownership on the Rust side. This is also important for certain Objective-C side scenarios where we may need to set an ivar after creation, which requires some level of mutability. This may also possibly help bring down the unsafe usage, which would be cool. - Rewrote the Color module; this now handles system colors better, and provides support for dynamic color creation. Supporting combinations of dark/light/contrast is now possible with handler passed in via `Color::dynamic`. This still has work to do in terms of some accessor methods and such, but it works well for now. The `to_platform...` method should be removed before v0.1. - Added a new feature for enabling fallback color usage on older macOS versions that don't support system colors. This may honestly never be used, but it cost nothing to implement. - Fixed a bug in the Autolayout wrapper where dereferencing could cause constraints to crash at runtime. - Support setting text color on labels. - Support setting text color on buttons, albeit very hacky right now. This needs to be extracted and/or cleaned up, but getting it sketched out was important for this commit. - Support setting a key equivalent on buttons. - Creating a local event monitor is now possible. - Examples updated; Calculator clone example added. The only API breaking change in this commit from earlier commits should be `color::rgb` needing to be `color::Color` followed by a `Color::rgb(...)` call.
2021-02-12 17:57:06 -08:00
Low
}