From 5cd59b5636b041408230b381246b8d3d4c432c35 Mon Sep 17 00:00:00 2001 From: Ryan McGrath Date: Fri, 12 Feb 2021 17:57:06 -0800 Subject: [PATCH] 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. --- Cargo.toml | 4 + examples/autolayout.rs | 20 +- examples/calculator/button_row.rs | 82 +++++ examples/calculator/calculator.rs | 105 ++++++ examples/calculator/content_view.rs | 151 ++++++++ examples/calculator/main.rs | 109 ++++++ examples/todos_list/todos/list/row.rs | 6 +- src/button.rs | 176 +++++++++- src/color.rs | 138 -------- src/color/macos_dynamic_color.rs | 298 ++++++++++++++++ src/color/mod.rs | 488 ++++++++++++++++++++++++++ src/layout/attributes.rs | 214 +++++++++++ src/layout/dimension.rs | 6 +- src/layout/horizontal.rs | 6 +- src/layout/mod.rs | 3 + src/layout/vertical.rs | 6 +- src/listview/macos.rs | 6 +- src/listview/row/mod.rs | 37 +- src/macos/enums.rs | 44 +++ src/macos/event/mod.rs | 115 ++++++ src/macos/menu/item.rs | 2 +- src/macos/mod.rs | 6 + src/macos/toolbar/item.rs | 4 +- src/text/label/mod.rs | 11 +- src/utils.rs | 13 +- src/view/macos.rs | 34 +- src/view/mod.rs | 42 ++- src/view/traits.rs | 2 + 28 files changed, 1923 insertions(+), 205 deletions(-) create mode 100644 examples/calculator/button_row.rs create mode 100644 examples/calculator/calculator.rs create mode 100644 examples/calculator/content_view.rs create mode 100644 examples/calculator/main.rs delete mode 100644 src/color.rs create mode 100644 src/color/macos_dynamic_color.rs create mode 100644 src/color/mod.rs create mode 100644 src/layout/attributes.rs create mode 100644 src/macos/enums.rs create mode 100644 src/macos/event/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 6662e4d..7efba77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,11 +25,15 @@ os_info = "3.0.1" uuid = { version = "0.8", features = ["v4"], optional = true } url = "2.1.1" +[dev-dependencies] +eval = "0.4" + [features] default = ["macos"] cloudkit = [] ios = [] macos = [] +color_fallbacks = [] quicklook = [] user-notifications = ["uuid"] webview = [] diff --git a/examples/autolayout.rs b/examples/autolayout.rs index 0bdc947..889cfa6 100644 --- a/examples/autolayout.rs +++ b/examples/autolayout.rs @@ -1,7 +1,7 @@ -//! This example showcases setting up a basic application and window, and setting up some views to -//! work with autolayout. +//! This example showcases setting up a basic application and window, setting up some views to +//! work with autolayout, and some basic ways to handle colors. -use cacao::color::rgb; +use cacao::color::{Color, Theme}; use cacao::layout::{Layout, LayoutConstraint}; use cacao::view::View; @@ -24,8 +24,7 @@ struct AppWindow { content: View, blue: View, red: View, - green: View, - window: Window + green: View } impl WindowDelegate for AppWindow { @@ -35,13 +34,18 @@ impl WindowDelegate for AppWindow { window.set_title("AutoLayout Example"); window.set_minimum_content_size(300., 300.); - self.blue.set_background_color(rgb(105, 162, 176)); + let dynamic = Color::dynamic(|style| match (style.theme, style.contrast) { + (Theme::Dark, _) => Color::SystemGreen, + _ => Color::SystemRed + }); + + self.blue.set_background_color(Color::SystemBlue); self.content.add_subview(&self.blue); - self.red.set_background_color(rgb(224, 82, 99)); + self.red.set_background_color(Color::SystemRed); self.content.add_subview(&self.red); - self.green.set_background_color(rgb(161, 192, 132)); + self.green.set_background_color(dynamic); self.content.add_subview(&self.green); window.set_content_view(&self.content); diff --git a/examples/calculator/button_row.rs b/examples/calculator/button_row.rs new file mode 100644 index 0000000..04166fb --- /dev/null +++ b/examples/calculator/button_row.rs @@ -0,0 +1,82 @@ +use cacao::layout::{LayoutConstraint, Layout}; +use cacao::button::Button; +use cacao::color::Color; +use cacao::view::View; + +use crate::calculator::Msg; +use crate::content_view::{button, BUTTON_WIDTH, BUTTON_HEIGHT}; + +pub struct ButtonRow { + pub view: View, + pub buttons: Vec