cacao/examples/calculator/content_view.rs

165 lines
6.1 KiB
Rust
Raw Normal View History

use cacao::appkit::FocusRingType;
2022-07-16 00:14:02 +10:00
use cacao::button::{BezelStyle, Button};
use cacao::color::Color;
use cacao::layout::{Layout, LayoutConstraint};
use cacao::text::{Font, Label, TextAlign};
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-13 12:57:06 +11:00
use cacao::view::{View, ViewDelegate};
use crate::button_row::ButtonRow;
use crate::calculator::{dispatch, Msg};
pub const BUTTON_WIDTH: f64 = 57.;
pub const BUTTON_HEIGHT: f64 = 47.;
pub fn button(text: &str, msg: Msg) -> Button {
let mut button = Button::new(text);
button.set_bordered(false);
button.set_bezel_style(BezelStyle::SmallSquare);
button.set_focus_ring_type(FocusRingType::None);
button.set_action(move || dispatch(msg.clone()));
button.set_key_equivalent(&*text.to_lowercase());
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-13 12:57:06 +11:00
let font = Font::system(22.);
button.set_font(&font);
button.set_text_color(Color::SystemWhite);
button
}
pub struct CalculatorView {
pub results_wrapper: View,
pub label: Label,
pub row0: ButtonRow,
pub row1: ButtonRow,
pub row2: ButtonRow,
pub row3: ButtonRow,
pub dot: Button,
pub zero: Button,
2022-08-19 07:45:19 +10:00
pub equals: Button
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-13 12:57:06 +11:00
}
impl CalculatorView {
pub fn new() -> Self {
let results_wrapper = View::new();
let label = Label::new();
let font = Font::system(40.);
label.set_font(&font);
label.set_text("0");
label.set_text_color(Color::rgb(255, 255, 255));
label.set_text_alignment(TextAlign::Right);
Self {
results_wrapper,
label,
2022-07-16 00:14:02 +10:00
row0: ButtonRow::new(
[Msg::Clear, Msg::Invert, Msg::Mod, Msg::Divide],
Color::rgb(69, 69, 69),
2022-08-19 07:45:19 +10:00
Color::rgb(255, 148, 10)
2022-07-16 00:14:02 +10:00
),
row1: ButtonRow::new(
[Msg::Push(7), Msg::Push(8), Msg::Push(9), Msg::Multiply],
Color::rgb(100, 100, 100),
2022-08-19 07:45:19 +10:00
Color::rgb(255, 148, 10)
2022-07-16 00:14:02 +10:00
),
row2: ButtonRow::new(
[Msg::Push(4), Msg::Push(5), Msg::Push(6), Msg::Subtract],
Color::rgb(100, 100, 100),
2022-08-19 07:45:19 +10:00
Color::rgb(255, 148, 10)
2022-07-16 00:14:02 +10:00
),
row3: ButtonRow::new(
[Msg::Push(1), Msg::Push(2), Msg::Push(3), Msg::Add],
Color::rgb(100, 100, 100),
2022-08-19 07:45:19 +10:00
Color::rgb(255, 148, 10)
2022-07-16 00:14:02 +10:00
),
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-13 12:57:06 +11:00
zero: button("0", Msg::Push(0)),
dot: button(".", Msg::Decimal),
2022-08-19 07:45:19 +10:00
equals: button("=", Msg::Equals)
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-13 12:57:06 +11:00
}
}
pub fn render_update(&self, message: String) {
self.label.set_text(&message);
}
}
impl ViewDelegate for CalculatorView {
const NAME: &'static str = "CalculatorView";
fn did_load(&mut self, view: View) {
view.set_background_color(Color::rgb(49, 49, 49));
self.zero.set_background_color(Color::rgb(100, 100, 100));
self.dot.set_background_color(Color::rgb(100, 100, 100));
self.equals.set_background_color(Color::rgb(255, 148, 10));
self.zero.set_key_equivalent("0");
view.add_subview(&self.row0.view);
view.add_subview(&self.row1.view);
view.add_subview(&self.row2.view);
view.add_subview(&self.row3.view);
for button in &[&self.zero, &self.dot, &self.equals] {
view.add_subview(button);
}
self.results_wrapper.add_subview(&self.label);
view.add_subview(&self.results_wrapper);
LayoutConstraint::activate(&[
self.results_wrapper.top.constraint_equal_to(&view.top),
self.results_wrapper.leading.constraint_equal_to(&view.leading),
self.results_wrapper.trailing.constraint_equal_to(&view.trailing),
self.results_wrapper.height.constraint_equal_to_constant(80.),
2022-07-16 00:14:02 +10:00
self.label
.leading
.constraint_equal_to(&self.results_wrapper.leading)
.offset(22.),
self.label
.trailing
.constraint_equal_to(&self.results_wrapper.trailing)
.offset(-16.),
self.label
.bottom
.constraint_equal_to(&self.results_wrapper.bottom)
.offset(-4.),
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-13 12:57:06 +11:00
// Buttons laid out from top-left
2022-07-16 00:14:02 +10:00
self.row0
.view
.top
.constraint_equal_to(&self.results_wrapper.bottom)
.offset(1.),
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-13 12:57:06 +11:00
self.row0.view.leading.constraint_equal_to(&view.leading),
self.row0.view.trailing.constraint_equal_to(&view.trailing),
self.row1.view.top.constraint_equal_to(&self.row0.view.bottom).offset(1.),
self.row1.view.leading.constraint_equal_to(&view.leading),
self.row1.view.trailing.constraint_equal_to(&view.trailing),
self.row2.view.top.constraint_equal_to(&self.row1.view.bottom).offset(1.),
self.row2.view.leading.constraint_equal_to(&view.leading),
self.row2.view.trailing.constraint_equal_to(&view.trailing),
self.row3.view.top.constraint_equal_to(&self.row2.view.bottom).offset(1.),
self.row3.view.leading.constraint_equal_to(&view.leading),
self.row3.view.trailing.constraint_equal_to(&view.trailing),
self.zero.top.constraint_equal_to(&self.row3.view.bottom).offset(1.),
self.zero.leading.constraint_equal_to(&view.leading),
self.zero.bottom.constraint_equal_to(&view.bottom),
self.dot.top.constraint_equal_to(&self.row3.view.bottom).offset(1.),
self.dot.leading.constraint_equal_to(&self.zero.trailing).offset(1.),
self.dot.bottom.constraint_equal_to(&view.bottom),
self.dot.width.constraint_equal_to_constant(BUTTON_WIDTH),
self.dot.height.constraint_equal_to_constant(BUTTON_HEIGHT),
self.equals.top.constraint_equal_to(&self.row3.view.bottom).offset(1.),
self.equals.leading.constraint_equal_to(&self.dot.trailing).offset(1.),
self.equals.trailing.constraint_equal_to(&view.trailing),
self.equals.bottom.constraint_equal_to(&view.bottom),
self.equals.width.constraint_equal_to_constant(BUTTON_WIDTH),
2022-08-19 07:45:19 +10:00
self.equals.height.constraint_equal_to_constant(BUTTON_HEIGHT)
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-13 12:57:06 +11:00
])
}
}